blob: dc9c29f998f3b1b69cdae83bbfb6f4ed09d2cefb [file] [log] [blame]
Philippe Mathieu-Daudéed3baad2019-07-01 17:26:19 +01001/*
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 */
Markus Armbrusterdb725812019-08-12 07:23:50 +02008
Peter Maydell74c21bd2015-12-07 16:23:44 +00009#include "qemu/osdep.h"
Peter Maydell63159602019-05-07 12:55:04 +010010#include "qemu/units.h"
Peter Maydell181962f2018-03-02 10:45:36 +000011#include "target/arm/idau.h"
Peter Maydell194cbc42016-10-12 18:54:34 +010012#include "trace.h"
bellardb5ff1b32005-11-26 10:38:39 +000013#include "cpu.h"
Peter Maydellccd38082014-04-15 19:18:37 +010014#include "internals.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010015#include "exec/gdbstub.h"
Richard Henderson2ef61752014-04-07 22:31:41 -070016#include "exec/helper-proto.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010017#include "qemu/host-utils.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020018#include "qemu/main-loop.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010019#include "qemu/bitops.h"
Will Newtoneb0ecd52014-02-26 17:20:07 +000020#include "qemu/crc32c.h"
Markus Armbruster04424282019-04-17 21:17:57 +020021#include "qemu/qemu-print.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010022#include "exec/exec-all.h"
Will Newtoneb0ecd52014-02-26 17:20:07 +000023#include <zlib.h> /* For crc32 */
Markus Armbruster64552b62019-08-12 07:23:42 +020024#include "hw/irq.h"
Alex Bennéef1672e62019-05-13 14:43:57 +010025#include "hw/semihosting/semihost.h"
Aaron Lindsayb2e23722019-01-21 10:23:14 +000026#include "sysemu/cpus.h"
Peter Maydellf3a9b692016-01-21 14:15:08 +000027#include "sysemu/kvm.h"
Peter Maydell2a609df2020-02-14 17:51:04 +000028#include "sysemu/tcg.h"
Peter Maydell9d2b5a52018-07-23 15:21:26 +010029#include "qemu/range.h"
Markus Armbruster7f7b4e72019-06-19 22:10:45 +020030#include "qapi/qapi-commands-machine-target.h"
Richard Hendersonde390642019-03-12 21:57:35 -070031#include "qapi/error.h"
32#include "qemu/guest-random.h"
Philippe Mathieu-Daudé91f78c52019-07-04 17:14:43 +010033#ifdef CONFIG_TCG
34#include "arm_ldst.h"
Philippe Mathieu-Daudé7aab5a82019-07-04 17:14:43 +010035#include "exec/cpu_ldst.h"
Philippe Mathieu-Daudé91f78c52019-07-04 17:14:43 +010036#endif
Peter Maydell0b03bdf2012-01-25 12:42:29 +000037
Laurent Vivier352c98e2015-08-25 17:09:36 +020038#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
39
Peter Maydell4a501602012-06-20 11:57:16 +000040#ifndef CONFIG_USER_ONLY
Alistair Francis7c2cb422014-03-10 14:56:28 +000041
Edgar E. Iglesias37785972015-10-26 14:02:04 +010042static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
Peter Maydell03ae85f2017-09-04 15:21:51 +010043 MMUAccessType access_type, ARMMMUIdx mmu_idx,
Peter Maydellff7de2f2020-03-30 22:03:59 +010044 bool s1_is_el0,
Edgar E. Iglesias37785972015-10-26 14:02:04 +010045 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
Peter Maydellda909b22017-12-13 17:59:24 +000046 target_ulong *page_size_ptr,
Richard Henderson7e98e212020-06-25 20:31:39 -070047 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
48 __attribute__((nonnull));
Peter Maydell4a501602012-06-20 11:57:16 +000049#endif
50
Peter Maydellaffdb642018-10-24 07:50:17 +010051static void switch_mode(CPUARMState *env, int mode);
52
Alex Bennéea010bdb2020-03-16 17:21:41 +000053static int vfp_gdb_get_reg(CPUARMState *env, GByteArray *buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +000054{
Richard Hendersona6627f52020-02-14 10:15:31 -080055 ARMCPU *cpu = env_archcpu(env);
56 int nregs = cpu_isar_feature(aa32_simd_r32, cpu) ? 32 : 16;
pbrook56aebc82008-10-11 17:55:29 +000057
58 /* VFP data registers are always little-endian. */
pbrook56aebc82008-10-11 17:55:29 +000059 if (reg < nregs) {
Alex Bennéea010bdb2020-03-16 17:21:41 +000060 return gdb_get_reg64(buf, *aa32_vfp_dreg(env, reg));
pbrook56aebc82008-10-11 17:55:29 +000061 }
62 if (arm_feature(env, ARM_FEATURE_NEON)) {
63 /* Aliases for Q regs. */
64 nregs += 16;
65 if (reg < nregs) {
Richard Henderson9a2b5252018-01-25 11:45:29 +000066 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
Alex Bennéea010bdb2020-03-16 17:21:41 +000067 return gdb_get_reg128(buf, q[0], q[1]);
pbrook56aebc82008-10-11 17:55:29 +000068 }
69 }
70 switch (reg - nregs) {
Alex Bennéea010bdb2020-03-16 17:21:41 +000071 case 0: return gdb_get_reg32(buf, env->vfp.xregs[ARM_VFP_FPSID]); break;
72 case 1: return gdb_get_reg32(buf, vfp_get_fpscr(env)); break;
73 case 2: return gdb_get_reg32(buf, env->vfp.xregs[ARM_VFP_FPEXC]); break;
pbrook56aebc82008-10-11 17:55:29 +000074 }
75 return 0;
76}
77
Andreas Färber0ecb72a2012-03-14 01:38:21 +010078static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +000079{
Richard Hendersona6627f52020-02-14 10:15:31 -080080 ARMCPU *cpu = env_archcpu(env);
81 int nregs = cpu_isar_feature(aa32_simd_r32, cpu) ? 32 : 16;
pbrook56aebc82008-10-11 17:55:29 +000082
pbrook56aebc82008-10-11 17:55:29 +000083 if (reg < nregs) {
Richard Henderson9a2b5252018-01-25 11:45:29 +000084 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf);
pbrook56aebc82008-10-11 17:55:29 +000085 return 8;
86 }
87 if (arm_feature(env, ARM_FEATURE_NEON)) {
88 nregs += 16;
89 if (reg < nregs) {
Richard Henderson9a2b5252018-01-25 11:45:29 +000090 uint64_t *q = aa32_vfp_qreg(env, reg - 32);
91 q[0] = ldq_le_p(buf);
92 q[1] = ldq_le_p(buf + 8);
pbrook56aebc82008-10-11 17:55:29 +000093 return 16;
94 }
95 }
96 switch (reg - nregs) {
97 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
Richard Hendersonb0a909a2019-02-15 09:56:40 +000098 case 1: vfp_set_fpscr(env, ldl_p(buf)); return 4;
Juha Riihimäki71b3c3d2009-10-26 11:46:42 +020099 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
pbrook56aebc82008-10-11 17:55:29 +0000100 }
101 return 0;
102}
103
Alex Bennéea010bdb2020-03-16 17:21:41 +0000104static int aarch64_fpu_gdb_get_reg(CPUARMState *env, GByteArray *buf, int reg)
Peter Maydell6a669422013-12-17 19:42:32 +0000105{
106 switch (reg) {
107 case 0 ... 31:
Alex Bennée8b1ca582020-03-16 17:21:38 +0000108 {
109 /* 128 bit FP register - quads are in LE order */
110 uint64_t *q = aa64_vfp_qreg(env, reg);
111 return gdb_get_reg128(buf, q[1], q[0]);
112 }
Peter Maydell6a669422013-12-17 19:42:32 +0000113 case 32:
114 /* FPSR */
Alex Bennée8b1ca582020-03-16 17:21:38 +0000115 return gdb_get_reg32(buf, vfp_get_fpsr(env));
Peter Maydell6a669422013-12-17 19:42:32 +0000116 case 33:
117 /* FPCR */
Alex Bennée8b1ca582020-03-16 17:21:38 +0000118 return gdb_get_reg32(buf,vfp_get_fpcr(env));
Peter Maydell6a669422013-12-17 19:42:32 +0000119 default:
120 return 0;
121 }
122}
123
124static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
125{
126 switch (reg) {
127 case 0 ... 31:
128 /* 128 bit FP register */
Richard Henderson9a2b5252018-01-25 11:45:29 +0000129 {
130 uint64_t *q = aa64_vfp_qreg(env, reg);
131 q[0] = ldq_le_p(buf);
132 q[1] = ldq_le_p(buf + 8);
133 return 16;
134 }
Peter Maydell6a669422013-12-17 19:42:32 +0000135 case 32:
136 /* FPSR */
137 vfp_set_fpsr(env, ldl_p(buf));
138 return 4;
139 case 33:
140 /* FPCR */
141 vfp_set_fpcr(env, ldl_p(buf));
142 return 4;
143 default:
144 return 0;
145 }
146}
147
Peter Maydellc4241c72014-02-20 10:35:54 +0000148static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydelld4e6df62013-06-25 18:16:07 +0100149{
Peter Maydell375421c2015-02-05 13:37:22 +0000150 assert(ri->fieldoffset);
Peter Maydell67ed7712014-02-26 17:20:01 +0000151 if (cpreg_field_is_64bit(ri)) {
Peter Maydellc4241c72014-02-20 10:35:54 +0000152 return CPREG_FIELD64(env, ri);
Peter Maydell22d9e1a2013-08-20 14:54:31 +0100153 } else {
Peter Maydellc4241c72014-02-20 10:35:54 +0000154 return CPREG_FIELD32(env, ri);
Peter Maydell22d9e1a2013-08-20 14:54:31 +0100155 }
Peter Maydelld4e6df62013-06-25 18:16:07 +0100156}
157
Peter Maydellc4241c72014-02-20 10:35:54 +0000158static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
159 uint64_t value)
Peter Maydelld4e6df62013-06-25 18:16:07 +0100160{
Peter Maydell375421c2015-02-05 13:37:22 +0000161 assert(ri->fieldoffset);
Peter Maydell67ed7712014-02-26 17:20:01 +0000162 if (cpreg_field_is_64bit(ri)) {
Peter Maydell22d9e1a2013-08-20 14:54:31 +0100163 CPREG_FIELD64(env, ri) = value;
164 } else {
165 CPREG_FIELD32(env, ri) = value;
166 }
Peter Maydelld4e6df62013-06-25 18:16:07 +0100167}
168
Fabian Aggeler11f136e2014-12-11 12:07:51 +0000169static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
170{
171 return (char *)env + ri->fieldoffset;
172}
173
Peter Maydell49a66192015-08-13 11:26:21 +0100174uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydell721fae12013-06-25 18:16:07 +0100175{
Peter Maydell59a1c322014-02-20 10:35:54 +0000176 /* Raw read of a coprocessor register (as needed for migration, etc). */
Peter Maydell721fae12013-06-25 18:16:07 +0100177 if (ri->type & ARM_CP_CONST) {
Peter Maydell59a1c322014-02-20 10:35:54 +0000178 return ri->resetvalue;
Peter Maydell721fae12013-06-25 18:16:07 +0100179 } else if (ri->raw_readfn) {
Peter Maydell59a1c322014-02-20 10:35:54 +0000180 return ri->raw_readfn(env, ri);
Peter Maydell721fae12013-06-25 18:16:07 +0100181 } else if (ri->readfn) {
Peter Maydell59a1c322014-02-20 10:35:54 +0000182 return ri->readfn(env, ri);
Peter Maydell721fae12013-06-25 18:16:07 +0100183 } else {
Peter Maydell59a1c322014-02-20 10:35:54 +0000184 return raw_read(env, ri);
Peter Maydell721fae12013-06-25 18:16:07 +0100185 }
Peter Maydell721fae12013-06-25 18:16:07 +0100186}
187
Peter Maydell59a1c322014-02-20 10:35:54 +0000188static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
Peter Maydell7900e9f2014-02-20 10:35:54 +0000189 uint64_t v)
Peter Maydell721fae12013-06-25 18:16:07 +0100190{
191 /* Raw write of a coprocessor register (as needed for migration, etc).
Peter Maydell721fae12013-06-25 18:16:07 +0100192 * Note that constant registers are treated as write-ignored; the
193 * caller should check for success by whether a readback gives the
194 * value written.
195 */
196 if (ri->type & ARM_CP_CONST) {
Peter Maydell59a1c322014-02-20 10:35:54 +0000197 return;
Peter Maydell721fae12013-06-25 18:16:07 +0100198 } else if (ri->raw_writefn) {
Peter Maydellc4241c72014-02-20 10:35:54 +0000199 ri->raw_writefn(env, ri, v);
Peter Maydell721fae12013-06-25 18:16:07 +0100200 } else if (ri->writefn) {
Peter Maydellc4241c72014-02-20 10:35:54 +0000201 ri->writefn(env, ri, v);
Peter Maydell721fae12013-06-25 18:16:07 +0100202 } else {
Peter Crosthwaiteafb25302014-01-01 23:58:20 -0800203 raw_write(env, ri, v);
Peter Maydell721fae12013-06-25 18:16:07 +0100204 }
Peter Maydell721fae12013-06-25 18:16:07 +0100205}
206
Alex Bennéed12379c2020-03-16 17:21:45 +0000207/**
208 * arm_get/set_gdb_*: get/set a gdb register
209 * @env: the CPU state
210 * @buf: a buffer to copy to/from
211 * @reg: register number (offset from start of group)
212 *
213 * We return the number of bytes copied
214 */
215
Alex Bennéea010bdb2020-03-16 17:21:41 +0000216static int arm_gdb_get_sysreg(CPUARMState *env, GByteArray *buf, int reg)
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100217{
Richard Henderson2fc0cc02019-03-22 17:41:14 -0700218 ARMCPU *cpu = env_archcpu(env);
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100219 const ARMCPRegInfo *ri;
220 uint32_t key;
221
Alex Bennée448d4d12020-03-16 17:21:42 +0000222 key = cpu->dyn_sysreg_xml.data.cpregs.keys[reg];
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100223 ri = get_arm_cp_reginfo(cpu->cp_regs, key);
224 if (ri) {
225 if (cpreg_field_is_64bit(ri)) {
226 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri));
227 } else {
228 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri));
229 }
230 }
231 return 0;
232}
233
234static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg)
235{
236 return 0;
237}
238
Alex Bennéed12379c2020-03-16 17:21:45 +0000239#ifdef TARGET_AARCH64
240static int arm_gdb_get_svereg(CPUARMState *env, GByteArray *buf, int reg)
241{
242 ARMCPU *cpu = env_archcpu(env);
243
244 switch (reg) {
245 /* The first 32 registers are the zregs */
246 case 0 ... 31:
247 {
248 int vq, len = 0;
249 for (vq = 0; vq < cpu->sve_max_vq; vq++) {
250 len += gdb_get_reg128(buf,
251 env->vfp.zregs[reg].d[vq * 2 + 1],
252 env->vfp.zregs[reg].d[vq * 2]);
253 }
254 return len;
255 }
256 case 32:
257 return gdb_get_reg32(buf, vfp_get_fpsr(env));
258 case 33:
259 return gdb_get_reg32(buf, vfp_get_fpcr(env));
260 /* then 16 predicates and the ffr */
261 case 34 ... 50:
262 {
263 int preg = reg - 34;
264 int vq, len = 0;
265 for (vq = 0; vq < cpu->sve_max_vq; vq = vq + 4) {
266 len += gdb_get_reg64(buf, env->vfp.pregs[preg].p[vq / 4]);
267 }
268 return len;
269 }
270 case 51:
271 {
272 /*
273 * We report in Vector Granules (VG) which is 64bit in a Z reg
274 * while the ZCR works in Vector Quads (VQ) which is 128bit chunks.
275 */
276 int vq = sve_zcr_len_for_el(env, arm_current_el(env)) + 1;
277 return gdb_get_reg32(buf, vq * 2);
278 }
279 default:
280 /* gdbstub asked for something out our range */
281 qemu_log_mask(LOG_UNIMP, "%s: out of range register %d", __func__, reg);
282 break;
283 }
284
285 return 0;
286}
287
288static int arm_gdb_set_svereg(CPUARMState *env, uint8_t *buf, int reg)
289{
290 ARMCPU *cpu = env_archcpu(env);
291
292 /* The first 32 registers are the zregs */
293 switch (reg) {
294 /* The first 32 registers are the zregs */
295 case 0 ... 31:
296 {
297 int vq, len = 0;
298 uint64_t *p = (uint64_t *) buf;
299 for (vq = 0; vq < cpu->sve_max_vq; vq++) {
300 env->vfp.zregs[reg].d[vq * 2 + 1] = *p++;
301 env->vfp.zregs[reg].d[vq * 2] = *p++;
302 len += 16;
303 }
304 return len;
305 }
306 case 32:
307 vfp_set_fpsr(env, *(uint32_t *)buf);
308 return 4;
309 case 33:
310 vfp_set_fpcr(env, *(uint32_t *)buf);
311 return 4;
312 case 34 ... 50:
313 {
314 int preg = reg - 34;
315 int vq, len = 0;
316 uint64_t *p = (uint64_t *) buf;
317 for (vq = 0; vq < cpu->sve_max_vq; vq = vq + 4) {
318 env->vfp.pregs[preg].p[vq / 4] = *p++;
319 len += 8;
320 }
321 return len;
322 }
323 case 51:
324 /* cannot set vg via gdbstub */
325 return 0;
326 default:
327 /* gdbstub asked for something out our range */
328 break;
329 }
330
331 return 0;
332}
333#endif /* TARGET_AARCH64 */
334
Peter Maydell375421c2015-02-05 13:37:22 +0000335static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
336{
337 /* Return true if the regdef would cause an assertion if you called
338 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
339 * program bug for it not to have the NO_RAW flag).
340 * NB that returning false here doesn't necessarily mean that calling
341 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
342 * read/write access functions which are safe for raw use" from "has
343 * read/write access functions which have side effects but has forgotten
344 * to provide raw access functions".
345 * The tests here line up with the conditions in read/write_raw_cp_reg()
346 * and assertions in raw_read()/raw_write().
347 */
348 if ((ri->type & ARM_CP_CONST) ||
349 ri->fieldoffset ||
350 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
351 return false;
352 }
353 return true;
354}
355
Peter Maydellb698e4e2019-05-07 12:55:02 +0100356bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
Peter Maydell721fae12013-06-25 18:16:07 +0100357{
358 /* Write the coprocessor state from cpu->env to the (index,value) list. */
359 int i;
360 bool ok = true;
361
362 for (i = 0; i < cpu->cpreg_array_len; i++) {
363 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
364 const ARMCPRegInfo *ri;
Peter Maydellb698e4e2019-05-07 12:55:02 +0100365 uint64_t newval;
Peter Maydell59a1c322014-02-20 10:35:54 +0000366
Peter Maydell60322b32014-01-04 22:15:44 +0000367 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
Peter Maydell721fae12013-06-25 18:16:07 +0100368 if (!ri) {
369 ok = false;
370 continue;
371 }
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000372 if (ri->type & ARM_CP_NO_RAW) {
Peter Maydell721fae12013-06-25 18:16:07 +0100373 continue;
374 }
Peter Maydellb698e4e2019-05-07 12:55:02 +0100375
376 newval = read_raw_cp_reg(&cpu->env, ri);
377 if (kvm_sync) {
378 /*
379 * Only sync if the previous list->cpustate sync succeeded.
380 * Rather than tracking the success/failure state for every
381 * item in the list, we just recheck "does the raw write we must
382 * have made in write_list_to_cpustate() read back OK" here.
383 */
384 uint64_t oldval = cpu->cpreg_values[i];
385
386 if (oldval == newval) {
387 continue;
388 }
389
390 write_raw_cp_reg(&cpu->env, ri, oldval);
391 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
392 continue;
393 }
394
395 write_raw_cp_reg(&cpu->env, ri, newval);
396 }
397 cpu->cpreg_values[i] = newval;
Peter Maydell721fae12013-06-25 18:16:07 +0100398 }
399 return ok;
400}
401
402bool write_list_to_cpustate(ARMCPU *cpu)
403{
404 int i;
405 bool ok = true;
406
407 for (i = 0; i < cpu->cpreg_array_len; i++) {
408 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
409 uint64_t v = cpu->cpreg_values[i];
Peter Maydell721fae12013-06-25 18:16:07 +0100410 const ARMCPRegInfo *ri;
411
Peter Maydell60322b32014-01-04 22:15:44 +0000412 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
Peter Maydell721fae12013-06-25 18:16:07 +0100413 if (!ri) {
414 ok = false;
415 continue;
416 }
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000417 if (ri->type & ARM_CP_NO_RAW) {
Peter Maydell721fae12013-06-25 18:16:07 +0100418 continue;
419 }
420 /* Write value and confirm it reads back as written
421 * (to catch read-only registers and partially read-only
422 * registers where the incoming migration value doesn't match)
423 */
Peter Maydell59a1c322014-02-20 10:35:54 +0000424 write_raw_cp_reg(&cpu->env, ri, v);
425 if (read_raw_cp_reg(&cpu->env, ri) != v) {
Peter Maydell721fae12013-06-25 18:16:07 +0100426 ok = false;
427 }
428 }
429 return ok;
430}
431
432static void add_cpreg_to_list(gpointer key, gpointer opaque)
433{
434 ARMCPU *cpu = opaque;
435 uint64_t regidx;
436 const ARMCPRegInfo *ri;
437
438 regidx = *(uint32_t *)key;
Peter Maydell60322b32014-01-04 22:15:44 +0000439 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
Peter Maydell721fae12013-06-25 18:16:07 +0100440
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000441 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
Peter Maydell721fae12013-06-25 18:16:07 +0100442 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
443 /* The value array need not be initialized at this point */
444 cpu->cpreg_array_len++;
445 }
446}
447
448static void count_cpreg(gpointer key, gpointer opaque)
449{
450 ARMCPU *cpu = opaque;
451 uint64_t regidx;
452 const ARMCPRegInfo *ri;
453
454 regidx = *(uint32_t *)key;
Peter Maydell60322b32014-01-04 22:15:44 +0000455 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
Peter Maydell721fae12013-06-25 18:16:07 +0100456
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000457 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
Peter Maydell721fae12013-06-25 18:16:07 +0100458 cpu->cpreg_array_len++;
459 }
460}
461
462static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
463{
Alvise Rigocbf239b2013-10-11 19:38:44 +0200464 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
465 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
Peter Maydell721fae12013-06-25 18:16:07 +0100466
Alvise Rigocbf239b2013-10-11 19:38:44 +0200467 if (aidx > bidx) {
468 return 1;
469 }
470 if (aidx < bidx) {
471 return -1;
472 }
473 return 0;
Peter Maydell721fae12013-06-25 18:16:07 +0100474}
475
476void init_cpreg_list(ARMCPU *cpu)
477{
478 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
479 * Note that we require cpreg_tuples[] to be sorted by key ID.
480 */
Markus Armbruster57b6d952015-06-02 14:56:22 +0100481 GList *keys;
Peter Maydell721fae12013-06-25 18:16:07 +0100482 int arraylen;
483
Markus Armbruster57b6d952015-06-02 14:56:22 +0100484 keys = g_hash_table_get_keys(cpu->cp_regs);
Peter Maydell721fae12013-06-25 18:16:07 +0100485 keys = g_list_sort(keys, cpreg_key_compare);
486
487 cpu->cpreg_array_len = 0;
488
489 g_list_foreach(keys, count_cpreg, cpu);
490
491 arraylen = cpu->cpreg_array_len;
492 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
493 cpu->cpreg_values = g_new(uint64_t, arraylen);
494 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
495 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
496 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
497 cpu->cpreg_array_len = 0;
498
499 g_list_foreach(keys, add_cpreg_to_list, cpu);
500
501 assert(cpu->cpreg_array_len == arraylen);
502
503 g_list_free(keys);
504}
505
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +0100506/*
Edgar E. Iglesias93dd1e62020-05-05 16:17:29 +0200507 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +0100508 */
509static CPAccessResult access_el3_aa32ns(CPUARMState *env,
Peter Maydell3f208fd2016-02-11 11:17:31 +0000510 const ARMCPRegInfo *ri,
511 bool isread)
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +0100512{
Edgar E. Iglesias93dd1e62020-05-05 16:17:29 +0200513 if (!is_a64(env) && arm_current_el(env) == 3 &&
514 arm_is_secure_below_el3(env)) {
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +0100515 return CP_ACCESS_TRAP_UNCATEGORIZED;
516 }
517 return CP_ACCESS_OK;
518}
519
Peter Maydell5513c3a2016-02-11 11:17:30 +0000520/* Some secure-only AArch32 registers trap to EL3 if used from
521 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
522 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
523 * We assume that the .access field is set to PL1_RW.
524 */
525static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
Peter Maydell3f208fd2016-02-11 11:17:31 +0000526 const ARMCPRegInfo *ri,
527 bool isread)
Peter Maydell5513c3a2016-02-11 11:17:30 +0000528{
529 if (arm_current_el(env) == 3) {
530 return CP_ACCESS_OK;
531 }
532 if (arm_is_secure_below_el3(env)) {
533 return CP_ACCESS_TRAP_EL3;
534 }
535 /* This will be EL1 NS and EL2 NS, which just UNDEF */
536 return CP_ACCESS_TRAP_UNCATEGORIZED;
537}
538
Peter Maydell187f6782016-02-18 14:16:15 +0000539/* Check for traps to "powerdown debug" registers, which are controlled
540 * by MDCR.TDOSA
541 */
542static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
543 bool isread)
544{
545 int el = arm_current_el(env);
Peter Maydell30ac6332018-08-14 17:17:21 +0100546 bool mdcr_el2_tdosa = (env->cp15.mdcr_el2 & MDCR_TDOSA) ||
547 (env->cp15.mdcr_el2 & MDCR_TDE) ||
Richard Henderson7c208e02018-12-13 13:48:08 +0000548 (arm_hcr_el2_eff(env) & HCR_TGE);
Peter Maydell187f6782016-02-18 14:16:15 +0000549
Peter Maydell30ac6332018-08-14 17:17:21 +0100550 if (el < 2 && mdcr_el2_tdosa && !arm_is_secure_below_el3(env)) {
Peter Maydell187f6782016-02-18 14:16:15 +0000551 return CP_ACCESS_TRAP_EL2;
552 }
553 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
554 return CP_ACCESS_TRAP_EL3;
555 }
556 return CP_ACCESS_OK;
557}
558
Peter Maydell91b0a232016-02-18 14:16:15 +0000559/* Check for traps to "debug ROM" registers, which are controlled
560 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
561 */
562static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
563 bool isread)
564{
565 int el = arm_current_el(env);
Peter Maydell30ac6332018-08-14 17:17:21 +0100566 bool mdcr_el2_tdra = (env->cp15.mdcr_el2 & MDCR_TDRA) ||
567 (env->cp15.mdcr_el2 & MDCR_TDE) ||
Richard Henderson7c208e02018-12-13 13:48:08 +0000568 (arm_hcr_el2_eff(env) & HCR_TGE);
Peter Maydell91b0a232016-02-18 14:16:15 +0000569
Peter Maydell30ac6332018-08-14 17:17:21 +0100570 if (el < 2 && mdcr_el2_tdra && !arm_is_secure_below_el3(env)) {
Peter Maydell91b0a232016-02-18 14:16:15 +0000571 return CP_ACCESS_TRAP_EL2;
572 }
573 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
574 return CP_ACCESS_TRAP_EL3;
575 }
576 return CP_ACCESS_OK;
577}
578
Peter Maydelld6c8cf82016-02-18 14:16:15 +0000579/* Check for traps to general debug registers, which are controlled
580 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
581 */
582static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
583 bool isread)
584{
585 int el = arm_current_el(env);
Peter Maydell30ac6332018-08-14 17:17:21 +0100586 bool mdcr_el2_tda = (env->cp15.mdcr_el2 & MDCR_TDA) ||
587 (env->cp15.mdcr_el2 & MDCR_TDE) ||
Richard Henderson7c208e02018-12-13 13:48:08 +0000588 (arm_hcr_el2_eff(env) & HCR_TGE);
Peter Maydelld6c8cf82016-02-18 14:16:15 +0000589
Peter Maydell30ac6332018-08-14 17:17:21 +0100590 if (el < 2 && mdcr_el2_tda && !arm_is_secure_below_el3(env)) {
Peter Maydelld6c8cf82016-02-18 14:16:15 +0000591 return CP_ACCESS_TRAP_EL2;
592 }
593 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
594 return CP_ACCESS_TRAP_EL3;
595 }
596 return CP_ACCESS_OK;
597}
598
Peter Maydell1fce1ba2016-02-19 14:39:44 +0000599/* Check for traps to performance monitor registers, which are controlled
600 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
601 */
602static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
603 bool isread)
604{
605 int el = arm_current_el(env);
606
607 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
608 && !arm_is_secure_below_el3(env)) {
609 return CP_ACCESS_TRAP_EL2;
610 }
611 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
612 return CP_ACCESS_TRAP_EL3;
613 }
614 return CP_ACCESS_OK;
615}
616
Richard Henderson84929212020-03-05 16:09:17 +0000617/* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
618static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
619 bool isread)
620{
621 if (arm_current_el(env) == 1) {
622 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
623 if (arm_hcr_el2_eff(env) & trap) {
624 return CP_ACCESS_TRAP_EL2;
625 }
626 }
627 return CP_ACCESS_OK;
628}
629
Richard Henderson1803d272020-03-05 16:09:18 +0000630/* Check for traps from EL1 due to HCR_EL2.TSW. */
631static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
632 bool isread)
633{
634 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
635 return CP_ACCESS_TRAP_EL2;
636 }
637 return CP_ACCESS_OK;
638}
639
Richard Henderson99602372020-03-05 16:09:18 +0000640/* Check for traps from EL1 due to HCR_EL2.TACR. */
641static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
642 bool isread)
643{
644 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
645 return CP_ACCESS_TRAP_EL2;
646 }
647 return CP_ACCESS_OK;
648}
649
Richard Henderson30881b72020-03-05 16:09:18 +0000650/* Check for traps from EL1 due to HCR_EL2.TTLB. */
651static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
652 bool isread)
653{
654 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
655 return CP_ACCESS_TRAP_EL2;
656 }
657 return CP_ACCESS_OK;
658}
659
Peter Maydellc4241c72014-02-20 10:35:54 +0000660static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
Peter Maydellc983fe62012-06-20 11:57:13 +0000661{
Richard Henderson2fc0cc02019-03-22 17:41:14 -0700662 ARMCPU *cpu = env_archcpu(env);
Andreas Färber00c8cb02013-09-04 02:19:44 +0200663
Fabian Aggeler8d5c7732014-06-09 15:43:22 +0100664 raw_write(env, ri, value);
Alex Bennéed10eb082016-11-14 14:17:28 +0000665 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
Peter Maydellc983fe62012-06-20 11:57:13 +0000666}
667
Peter Maydellc4241c72014-02-20 10:35:54 +0000668static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
Peter Maydell08de2072012-06-20 11:57:14 +0000669{
Richard Henderson2fc0cc02019-03-22 17:41:14 -0700670 ARMCPU *cpu = env_archcpu(env);
Andreas Färber00c8cb02013-09-04 02:19:44 +0200671
Fabian Aggeler8d5c7732014-06-09 15:43:22 +0100672 if (raw_read(env, ri) != value) {
Peter Maydell08de2072012-06-20 11:57:14 +0000673 /* Unlike real hardware the qemu TLB uses virtual addresses,
674 * not modified virtual addresses, so this causes a TLB flush.
675 */
Alex Bennéed10eb082016-11-14 14:17:28 +0000676 tlb_flush(CPU(cpu));
Fabian Aggeler8d5c7732014-06-09 15:43:22 +0100677 raw_write(env, ri, value);
Peter Maydell08de2072012-06-20 11:57:14 +0000678 }
Peter Maydell08de2072012-06-20 11:57:14 +0000679}
Peter Maydellc4241c72014-02-20 10:35:54 +0000680
681static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
682 uint64_t value)
Peter Maydell08de2072012-06-20 11:57:14 +0000683{
Richard Henderson2fc0cc02019-03-22 17:41:14 -0700684 ARMCPU *cpu = env_archcpu(env);
Andreas Färber00c8cb02013-09-04 02:19:44 +0200685
Peter Maydell452a0952017-06-02 11:51:47 +0100686 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
Peter Maydell014406b2014-04-15 19:18:45 +0100687 && !extended_addresses_enabled(env)) {
Peter Maydell08de2072012-06-20 11:57:14 +0000688 /* For VMSA (when not using the LPAE long descriptor page table
689 * format) this register includes the ASID, so do a TLB flush.
690 * For PMSA it is purely a process ID and no action is needed.
691 */
Alex Bennéed10eb082016-11-14 14:17:28 +0000692 tlb_flush(CPU(cpu));
Peter Maydell08de2072012-06-20 11:57:14 +0000693 }
Fabian Aggeler8d5c7732014-06-09 15:43:22 +0100694 raw_write(env, ri, value);
Peter Maydell08de2072012-06-20 11:57:14 +0000695}
696
Peter Maydellfa439fc2014-09-12 14:06:50 +0100697/* IS variants of TLB operations must affect all cores */
698static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
699 uint64_t value)
700{
Richard Henderson29a0af62019-03-22 16:07:18 -0700701 CPUState *cs = env_cpu(env);
Peter Maydellfa439fc2014-09-12 14:06:50 +0100702
Alex Bennéea67cf272017-02-23 18:29:25 +0000703 tlb_flush_all_cpus_synced(cs);
Peter Maydellfa439fc2014-09-12 14:06:50 +0100704}
705
706static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
707 uint64_t value)
708{
Richard Henderson29a0af62019-03-22 16:07:18 -0700709 CPUState *cs = env_cpu(env);
Peter Maydellfa439fc2014-09-12 14:06:50 +0100710
Alex Bennéea67cf272017-02-23 18:29:25 +0000711 tlb_flush_all_cpus_synced(cs);
Peter Maydellfa439fc2014-09-12 14:06:50 +0100712}
713
714static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
715 uint64_t value)
716{
Richard Henderson29a0af62019-03-22 16:07:18 -0700717 CPUState *cs = env_cpu(env);
Peter Maydellfa439fc2014-09-12 14:06:50 +0100718
Alex Bennéea67cf272017-02-23 18:29:25 +0000719 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
Peter Maydellfa439fc2014-09-12 14:06:50 +0100720}
721
722static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
723 uint64_t value)
724{
Richard Henderson29a0af62019-03-22 16:07:18 -0700725 CPUState *cs = env_cpu(env);
Peter Maydellfa439fc2014-09-12 14:06:50 +0100726
Alex Bennéea67cf272017-02-23 18:29:25 +0000727 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
Peter Maydellfa439fc2014-09-12 14:06:50 +0100728}
729
Peter Maydellb4ab8ce2018-10-24 07:50:17 +0100730/*
731 * Non-IS variants of TLB operations are upgraded to
732 * IS versions if we are at NS EL1 and HCR_EL2.FB is set to
733 * force broadcast of these operations.
734 */
735static bool tlb_force_broadcast(CPUARMState *env)
736{
737 return (env->cp15.hcr_el2 & HCR_FB) &&
738 arm_current_el(env) == 1 && arm_is_secure_below_el3(env);
739}
740
741static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
742 uint64_t value)
743{
744 /* Invalidate all (TLBIALL) */
Richard Henderson527db2b2020-02-07 14:04:22 +0000745 CPUState *cs = env_cpu(env);
Peter Maydellb4ab8ce2018-10-24 07:50:17 +0100746
747 if (tlb_force_broadcast(env)) {
Richard Henderson527db2b2020-02-07 14:04:22 +0000748 tlb_flush_all_cpus_synced(cs);
749 } else {
750 tlb_flush(cs);
Peter Maydellb4ab8ce2018-10-24 07:50:17 +0100751 }
Peter Maydellb4ab8ce2018-10-24 07:50:17 +0100752}
753
754static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
755 uint64_t value)
756{
757 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
Richard Henderson527db2b2020-02-07 14:04:22 +0000758 CPUState *cs = env_cpu(env);
Peter Maydellb4ab8ce2018-10-24 07:50:17 +0100759
Richard Henderson527db2b2020-02-07 14:04:22 +0000760 value &= TARGET_PAGE_MASK;
Peter Maydellb4ab8ce2018-10-24 07:50:17 +0100761 if (tlb_force_broadcast(env)) {
Richard Henderson527db2b2020-02-07 14:04:22 +0000762 tlb_flush_page_all_cpus_synced(cs, value);
763 } else {
764 tlb_flush_page(cs, value);
Peter Maydellb4ab8ce2018-10-24 07:50:17 +0100765 }
Peter Maydellb4ab8ce2018-10-24 07:50:17 +0100766}
767
768static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
769 uint64_t value)
770{
771 /* Invalidate by ASID (TLBIASID) */
Richard Henderson527db2b2020-02-07 14:04:22 +0000772 CPUState *cs = env_cpu(env);
Peter Maydellb4ab8ce2018-10-24 07:50:17 +0100773
774 if (tlb_force_broadcast(env)) {
Richard Henderson527db2b2020-02-07 14:04:22 +0000775 tlb_flush_all_cpus_synced(cs);
776 } else {
777 tlb_flush(cs);
Peter Maydellb4ab8ce2018-10-24 07:50:17 +0100778 }
Peter Maydellb4ab8ce2018-10-24 07:50:17 +0100779}
780
781static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
782 uint64_t value)
783{
784 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
Richard Henderson527db2b2020-02-07 14:04:22 +0000785 CPUState *cs = env_cpu(env);
Peter Maydellb4ab8ce2018-10-24 07:50:17 +0100786
Richard Henderson527db2b2020-02-07 14:04:22 +0000787 value &= TARGET_PAGE_MASK;
Peter Maydellb4ab8ce2018-10-24 07:50:17 +0100788 if (tlb_force_broadcast(env)) {
Richard Henderson527db2b2020-02-07 14:04:22 +0000789 tlb_flush_page_all_cpus_synced(cs, value);
790 } else {
791 tlb_flush_page(cs, value);
Peter Maydellb4ab8ce2018-10-24 07:50:17 +0100792 }
Peter Maydellb4ab8ce2018-10-24 07:50:17 +0100793}
794
Sergey Sorokin541ef8c2016-07-14 16:51:37 +0100795static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
796 uint64_t value)
797{
Richard Henderson29a0af62019-03-22 16:07:18 -0700798 CPUState *cs = env_cpu(env);
Sergey Sorokin541ef8c2016-07-14 16:51:37 +0100799
Alex Bennée0336cbf2017-02-23 18:29:19 +0000800 tlb_flush_by_mmuidx(cs,
Richard Henderson01b98b62020-02-07 14:04:22 +0000801 ARMMMUIdxBit_E10_1 |
Richard Henderson452ef8c2020-02-08 12:57:58 +0000802 ARMMMUIdxBit_E10_1_PAN |
Peter Maydellbf053402020-03-30 22:03:57 +0100803 ARMMMUIdxBit_E10_0);
Sergey Sorokin541ef8c2016-07-14 16:51:37 +0100804}
805
806static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
807 uint64_t value)
808{
Richard Henderson29a0af62019-03-22 16:07:18 -0700809 CPUState *cs = env_cpu(env);
Sergey Sorokin541ef8c2016-07-14 16:51:37 +0100810
Alex Bennéea67cf272017-02-23 18:29:25 +0000811 tlb_flush_by_mmuidx_all_cpus_synced(cs,
Richard Henderson01b98b62020-02-07 14:04:22 +0000812 ARMMMUIdxBit_E10_1 |
Richard Henderson452ef8c2020-02-08 12:57:58 +0000813 ARMMMUIdxBit_E10_1_PAN |
Peter Maydellbf053402020-03-30 22:03:57 +0100814 ARMMMUIdxBit_E10_0);
Sergey Sorokin541ef8c2016-07-14 16:51:37 +0100815}
816
Sergey Sorokin541ef8c2016-07-14 16:51:37 +0100817
818static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
819 uint64_t value)
820{
Richard Henderson29a0af62019-03-22 16:07:18 -0700821 CPUState *cs = env_cpu(env);
Sergey Sorokin541ef8c2016-07-14 16:51:37 +0100822
Richard Hendersone013b742020-02-07 14:04:23 +0000823 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
Sergey Sorokin541ef8c2016-07-14 16:51:37 +0100824}
825
826static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
827 uint64_t value)
828{
Richard Henderson29a0af62019-03-22 16:07:18 -0700829 CPUState *cs = env_cpu(env);
Sergey Sorokin541ef8c2016-07-14 16:51:37 +0100830
Richard Hendersone013b742020-02-07 14:04:23 +0000831 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
Sergey Sorokin541ef8c2016-07-14 16:51:37 +0100832}
833
834static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
835 uint64_t value)
836{
Richard Henderson29a0af62019-03-22 16:07:18 -0700837 CPUState *cs = env_cpu(env);
Sergey Sorokin541ef8c2016-07-14 16:51:37 +0100838 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
839
Richard Hendersone013b742020-02-07 14:04:23 +0000840 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
Sergey Sorokin541ef8c2016-07-14 16:51:37 +0100841}
842
843static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
844 uint64_t value)
845{
Richard Henderson29a0af62019-03-22 16:07:18 -0700846 CPUState *cs = env_cpu(env);
Sergey Sorokin541ef8c2016-07-14 16:51:37 +0100847 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
848
Alex Bennéea67cf272017-02-23 18:29:25 +0000849 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
Richard Hendersone013b742020-02-07 14:04:23 +0000850 ARMMMUIdxBit_E2);
Sergey Sorokin541ef8c2016-07-14 16:51:37 +0100851}
852
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000853static const ARMCPRegInfo cp_reginfo[] = {
Fabian Aggeler54bf36e2014-12-11 12:07:52 +0000854 /* Define the secure and non-secure FCSE identifier CP registers
855 * separately because there is no secure bank in V8 (no _EL3). This allows
856 * the secure register to be properly reset and migrated. There is also no
857 * v8 EL1 version of the register so the non-secure instance stands alone.
858 */
Abdallah Bouassida9c513e72018-05-18 17:48:07 +0100859 { .name = "FCSEIDR",
Fabian Aggeler54bf36e2014-12-11 12:07:52 +0000860 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
861 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
862 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100863 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
Abdallah Bouassida9c513e72018-05-18 17:48:07 +0100864 { .name = "FCSEIDR_S",
Fabian Aggeler54bf36e2014-12-11 12:07:52 +0000865 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
866 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
867 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
868 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
869 /* Define the secure and non-secure context identifier CP registers
870 * separately because there is no secure bank in V8 (no _EL3). This allows
871 * the secure register to be properly reset and migrated. In the
872 * non-secure case, the 32-bit register will have reset and migration
873 * disabled during registration as it is handled by the 64-bit instance.
874 */
875 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
Peter Maydell014406b2014-04-15 19:18:45 +0100876 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
Richard Henderson84929212020-03-05 16:09:17 +0000877 .access = PL1_RW, .accessfn = access_tvm_trvm,
878 .secure = ARM_CP_SECSTATE_NS,
Fabian Aggeler54bf36e2014-12-11 12:07:52 +0000879 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
880 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
Abdallah Bouassida9c513e72018-05-18 17:48:07 +0100881 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
Fabian Aggeler54bf36e2014-12-11 12:07:52 +0000882 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
Richard Henderson84929212020-03-05 16:09:17 +0000883 .access = PL1_RW, .accessfn = access_tvm_trvm,
884 .secure = ARM_CP_SECSTATE_S,
Fabian Aggeler54bf36e2014-12-11 12:07:52 +0000885 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100886 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
Peter Maydell9449fdf2014-04-15 19:18:47 +0100887 REGINFO_SENTINEL
888};
889
890static const ARMCPRegInfo not_v8_cp_reginfo[] = {
891 /* NB: Some of these registers exist in v8 but with more precise
892 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
893 */
894 /* MMU Domain access control / MPU write buffer control */
Fabian Aggeler0c17d682014-12-11 12:07:51 +0000895 { .name = "DACR",
896 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
Richard Henderson84929212020-03-05 16:09:17 +0000897 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
Fabian Aggeler0c17d682014-12-11 12:07:51 +0000898 .writefn = dacr_write, .raw_writefn = raw_write,
899 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
900 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
Edgar E. Iglesiasa903c442015-06-02 14:56:21 +0100901 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
902 * For v6 and v5, these mappings are overly broad.
Peter Maydell4fdd17d2012-06-20 11:57:15 +0000903 */
Edgar E. Iglesiasa903c442015-06-02 14:56:21 +0100904 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
905 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
906 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
907 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
908 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
909 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
910 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
Peter Maydell4fdd17d2012-06-20 11:57:15 +0000911 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
Peter Maydellc4804212012-06-20 11:57:17 +0000912 /* Cache maintenance ops; some of this space may be overridden later. */
913 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
914 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
915 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000916 REGINFO_SENTINEL
917};
918
Peter Maydell7d57f402012-06-20 11:57:11 +0000919static const ARMCPRegInfo not_v6_cp_reginfo[] = {
920 /* Not all pre-v6 cores implemented this WFI, so this is slightly
921 * over-broad.
922 */
923 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
924 .access = PL1_W, .type = ARM_CP_WFI },
925 REGINFO_SENTINEL
926};
927
928static const ARMCPRegInfo not_v7_cp_reginfo[] = {
929 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
930 * is UNPREDICTABLE; we choose to NOP as most implementations do).
931 */
932 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
933 .access = PL1_W, .type = ARM_CP_WFI },
Peter Maydell34f90522012-06-20 11:57:18 +0000934 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
935 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
936 * OMAPCP will override this space.
937 */
938 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
939 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
940 .resetvalue = 0 },
941 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
942 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
943 .resetvalue = 0 },
Peter Maydell776d4e52012-06-20 11:57:19 +0000944 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
945 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000946 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100947 .resetvalue = 0 },
Peter Maydell50300692014-08-19 18:56:25 +0100948 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
949 * implementing it as RAZ means the "debug architecture version" bits
950 * will read as a reserved value, which should cause Linux to not try
951 * to use the debug hardware.
952 */
953 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
954 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell995939a2014-09-12 14:06:50 +0100955 /* MMU TLB control. Note that the wildcarding means we cover not just
956 * the unified TLB ops but also the dside/iside/inner-shareable variants.
957 */
958 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
959 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000960 .type = ARM_CP_NO_RAW },
Peter Maydell995939a2014-09-12 14:06:50 +0100961 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
962 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000963 .type = ARM_CP_NO_RAW },
Peter Maydell995939a2014-09-12 14:06:50 +0100964 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
965 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000966 .type = ARM_CP_NO_RAW },
Peter Maydell995939a2014-09-12 14:06:50 +0100967 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
968 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000969 .type = ARM_CP_NO_RAW },
Edgar E. Iglesiasa903c442015-06-02 14:56:21 +0100970 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
971 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
972 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
973 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
Peter Maydell7d57f402012-06-20 11:57:11 +0000974 REGINFO_SENTINEL
975};
976
Peter Maydellc4241c72014-02-20 10:35:54 +0000977static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
978 uint64_t value)
Peter Maydell2771db22012-06-20 11:57:18 +0000979{
Fabian Aggelerf0aff252014-05-27 17:09:49 +0100980 uint32_t mask = 0;
981
982 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
983 if (!arm_feature(env, ARM_FEATURE_V8)) {
984 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
985 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
986 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
987 */
Richard Henderson7fbc6a42020-02-24 14:22:16 -0800988 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
Fabian Aggelerf0aff252014-05-27 17:09:49 +0100989 /* VFP coprocessor: cp10 & cp11 [23:20] */
990 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
991
992 if (!arm_feature(env, ARM_FEATURE_NEON)) {
993 /* ASEDIS [31] bit is RAO/WI */
994 value |= (1 << 31);
995 }
996
997 /* VFPv3 and upwards with NEON implement 32 double precision
998 * registers (D0-D31).
999 */
Richard Hendersona6627f52020-02-14 10:15:31 -08001000 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
Fabian Aggelerf0aff252014-05-27 17:09:49 +01001001 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
1002 value |= (1 << 30);
1003 }
1004 }
1005 value &= mask;
Peter Maydell2771db22012-06-20 11:57:18 +00001006 }
Peter Maydellfc1120a2019-05-10 12:03:57 +01001007
1008 /*
1009 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
1010 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
1011 */
1012 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
1013 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
1014 value &= ~(0xf << 20);
1015 value |= env->cp15.cpacr_el1 & (0xf << 20);
1016 }
1017
Sergey Fedorov7ebd5f22015-04-26 16:49:25 +01001018 env->cp15.cpacr_el1 = value;
Peter Maydell2771db22012-06-20 11:57:18 +00001019}
1020
Peter Maydellfc1120a2019-05-10 12:03:57 +01001021static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1022{
1023 /*
1024 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
1025 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
1026 */
1027 uint64_t value = env->cp15.cpacr_el1;
1028
1029 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
1030 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
1031 value &= ~(0xf << 20);
1032 }
1033 return value;
1034}
1035
1036
Peter Maydell5deac392018-05-31 14:50:52 +01001037static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1038{
1039 /* Call cpacr_write() so that we reset with the correct RAO bits set
1040 * for our CPU features.
1041 */
1042 cpacr_write(env, ri, 0);
1043}
1044
Peter Maydell3f208fd2016-02-11 11:17:31 +00001045static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1046 bool isread)
Greg Bellowsc6f19162015-05-29 11:28:52 +01001047{
1048 if (arm_feature(env, ARM_FEATURE_V8)) {
1049 /* Check if CPACR accesses are to be trapped to EL2 */
1050 if (arm_current_el(env) == 1 &&
1051 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
1052 return CP_ACCESS_TRAP_EL2;
1053 /* Check if CPACR accesses are to be trapped to EL3 */
1054 } else if (arm_current_el(env) < 3 &&
1055 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
1056 return CP_ACCESS_TRAP_EL3;
1057 }
1058 }
1059
1060 return CP_ACCESS_OK;
1061}
1062
Peter Maydell3f208fd2016-02-11 11:17:31 +00001063static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1064 bool isread)
Greg Bellowsc6f19162015-05-29 11:28:52 +01001065{
1066 /* Check if CPTR accesses are set to trap to EL3 */
1067 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
1068 return CP_ACCESS_TRAP_EL3;
1069 }
1070
1071 return CP_ACCESS_OK;
1072}
1073
Peter Maydell7d57f402012-06-20 11:57:11 +00001074static const ARMCPRegInfo v6_cp_reginfo[] = {
1075 /* prefetch by MVA in v6, NOP in v7 */
1076 { .name = "MVA_prefetch",
1077 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
1078 .access = PL1_W, .type = ARM_CP_NOP },
Sergey Sorokin6df99de2015-10-16 11:14:52 +01001079 /* We need to break the TB after ISB to execute self-modifying code
1080 * correctly and also to take any pending interrupts immediately.
1081 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
1082 */
Peter Maydell7d57f402012-06-20 11:57:11 +00001083 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
Sergey Sorokin6df99de2015-10-16 11:14:52 +01001084 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
Peter Maydell091fd172012-07-12 10:58:36 +00001085 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
Peter Maydell7d57f402012-06-20 11:57:11 +00001086 .access = PL0_W, .type = ARM_CP_NOP },
Peter Maydell091fd172012-07-12 10:58:36 +00001087 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
Peter Maydell7d57f402012-06-20 11:57:11 +00001088 .access = PL0_W, .type = ARM_CP_NOP },
Peter Maydell06d76f32012-06-20 11:57:17 +00001089 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
Richard Henderson84929212020-03-05 16:09:17 +00001090 .access = PL1_RW, .accessfn = access_tvm_trvm,
Fabian Aggelerb848ce22014-12-11 12:07:51 +00001091 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
1092 offsetof(CPUARMState, cp15.ifar_ns) },
Peter Maydell06d76f32012-06-20 11:57:17 +00001093 .resetvalue = 0, },
1094 /* Watchpoint Fault Address Register : should actually only be present
1095 * for 1136, 1176, 11MPCore.
1096 */
1097 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
1098 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
Peter Maydell34222fb2014-02-26 17:20:06 +00001099 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
Greg Bellowsc6f19162015-05-29 11:28:52 +01001100 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
Sergey Fedorov7ebd5f22015-04-26 16:49:25 +01001101 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
Peter Maydellfc1120a2019-05-10 12:03:57 +01001102 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
Peter Maydell7d57f402012-06-20 11:57:11 +00001103 REGINFO_SENTINEL
1104};
1105
Aaron Lindsay7ece99b2018-04-26 11:04:39 +01001106/* Definitions for the PMU registers */
1107#define PMCRN_MASK 0xf800
1108#define PMCRN_SHIFT 11
Aaron Lindsay OSf4efb4b2019-02-01 14:55:45 +00001109#define PMCRLC 0x40
Peter Maydella1ed04d2020-02-14 17:51:11 +00001110#define PMCRDP 0x20
1111#define PMCRX 0x10
Aaron Lindsay7ece99b2018-04-26 11:04:39 +01001112#define PMCRD 0x8
1113#define PMCRC 0x4
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001114#define PMCRP 0x2
Aaron Lindsay7ece99b2018-04-26 11:04:39 +01001115#define PMCRE 0x1
Peter Maydell62d96ff2020-02-14 17:51:12 +00001116/*
1117 * Mask of PMCR bits writeable by guest (not including WO bits like C, P,
1118 * which can be written as 1 to trigger behaviour but which stay RAZ).
1119 */
1120#define PMCR_WRITEABLE_MASK (PMCRLC | PMCRDP | PMCRX | PMCRD | PMCRE)
Aaron Lindsay7ece99b2018-04-26 11:04:39 +01001121
Aaron Lindsay033614c2019-01-21 10:23:14 +00001122#define PMXEVTYPER_P 0x80000000
1123#define PMXEVTYPER_U 0x40000000
1124#define PMXEVTYPER_NSK 0x20000000
1125#define PMXEVTYPER_NSU 0x10000000
1126#define PMXEVTYPER_NSH 0x08000000
1127#define PMXEVTYPER_M 0x04000000
1128#define PMXEVTYPER_MT 0x02000000
1129#define PMXEVTYPER_EVTCOUNT 0x0000ffff
1130#define PMXEVTYPER_MASK (PMXEVTYPER_P | PMXEVTYPER_U | PMXEVTYPER_NSK | \
1131 PMXEVTYPER_NSU | PMXEVTYPER_NSH | \
1132 PMXEVTYPER_M | PMXEVTYPER_MT | \
1133 PMXEVTYPER_EVTCOUNT)
1134
Aaron Lindsay4b8afa12019-01-21 10:23:14 +00001135#define PMCCFILTR 0xf8000000
1136#define PMCCFILTR_M PMXEVTYPER_M
1137#define PMCCFILTR_EL0 (PMCCFILTR | PMCCFILTR_M)
1138
Aaron Lindsay7ece99b2018-04-26 11:04:39 +01001139static inline uint32_t pmu_num_counters(CPUARMState *env)
1140{
1141 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT;
1142}
1143
1144/* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */
1145static inline uint64_t pmu_counter_mask(CPUARMState *env)
1146{
1147 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1);
1148}
1149
Aaron Lindsay57a4a112019-01-21 10:23:14 +00001150typedef struct pm_event {
1151 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
1152 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
1153 bool (*supported)(CPUARMState *);
1154 /*
1155 * Retrieve the current count of the underlying event. The programmed
1156 * counters hold a difference from the return value from this function
1157 */
1158 uint64_t (*get_count)(CPUARMState *);
Aaron Lindsay OS4e7beb02019-02-01 14:55:45 +00001159 /*
1160 * Return how many nanoseconds it will take (at a minimum) for count events
1161 * to occur. A negative value indicates the counter will never overflow, or
1162 * that the counter has otherwise arranged for the overflow bit to be set
1163 * and the PMU interrupt to be raised on overflow.
1164 */
1165 int64_t (*ns_per_count)(uint64_t);
Aaron Lindsay57a4a112019-01-21 10:23:14 +00001166} pm_event;
1167
Aaron Lindsayb2e23722019-01-21 10:23:14 +00001168static bool event_always_supported(CPUARMState *env)
1169{
1170 return true;
1171}
1172
Aaron Lindsay0d4bfd72019-01-21 10:23:14 +00001173static uint64_t swinc_get_count(CPUARMState *env)
1174{
1175 /*
1176 * SW_INCR events are written directly to the pmevcntr's by writes to
1177 * PMSWINC, so there is no underlying count maintained by the PMU itself
1178 */
1179 return 0;
1180}
1181
Aaron Lindsay OS4e7beb02019-02-01 14:55:45 +00001182static int64_t swinc_ns_per(uint64_t ignored)
1183{
1184 return -1;
1185}
1186
Aaron Lindsayb2e23722019-01-21 10:23:14 +00001187/*
1188 * Return the underlying cycle count for the PMU cycle counters. If we're in
1189 * usermode, simply return 0.
1190 */
1191static uint64_t cycles_get_count(CPUARMState *env)
1192{
1193#ifndef CONFIG_USER_ONLY
1194 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1195 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
1196#else
1197 return cpu_get_host_ticks();
1198#endif
1199}
1200
1201#ifndef CONFIG_USER_ONLY
Aaron Lindsay OS4e7beb02019-02-01 14:55:45 +00001202static int64_t cycles_ns_per(uint64_t cycles)
1203{
1204 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
1205}
1206
Aaron Lindsayb2e23722019-01-21 10:23:14 +00001207static bool instructions_supported(CPUARMState *env)
1208{
1209 return use_icount == 1 /* Precise instruction counting */;
1210}
1211
1212static uint64_t instructions_get_count(CPUARMState *env)
1213{
1214 return (uint64_t)cpu_get_icount_raw();
1215}
Aaron Lindsay OS4e7beb02019-02-01 14:55:45 +00001216
1217static int64_t instructions_ns_per(uint64_t icount)
1218{
1219 return cpu_icount_to_ns((int64_t)icount);
1220}
Aaron Lindsayb2e23722019-01-21 10:23:14 +00001221#endif
1222
Peter Maydell0727f632020-02-14 17:51:08 +00001223static bool pmu_8_1_events_supported(CPUARMState *env)
1224{
1225 /* For events which are supported in any v8.1 PMU */
1226 return cpu_isar_feature(any_pmu_8_1, env_archcpu(env));
1227}
1228
Peter Maydell15dd1eb2020-02-14 17:51:09 +00001229static bool pmu_8_4_events_supported(CPUARMState *env)
1230{
1231 /* For events which are supported in any v8.1 PMU */
1232 return cpu_isar_feature(any_pmu_8_4, env_archcpu(env));
1233}
1234
Peter Maydell0727f632020-02-14 17:51:08 +00001235static uint64_t zero_event_get_count(CPUARMState *env)
1236{
1237 /* For events which on QEMU never fire, so their count is always zero */
1238 return 0;
1239}
1240
1241static int64_t zero_event_ns_per(uint64_t cycles)
1242{
1243 /* An event which never fires can never overflow */
1244 return -1;
1245}
1246
Aaron Lindsay57a4a112019-01-21 10:23:14 +00001247static const pm_event pm_events[] = {
Aaron Lindsay0d4bfd72019-01-21 10:23:14 +00001248 { .number = 0x000, /* SW_INCR */
1249 .supported = event_always_supported,
1250 .get_count = swinc_get_count,
Aaron Lindsay OS4e7beb02019-02-01 14:55:45 +00001251 .ns_per_count = swinc_ns_per,
Aaron Lindsay0d4bfd72019-01-21 10:23:14 +00001252 },
Aaron Lindsayb2e23722019-01-21 10:23:14 +00001253#ifndef CONFIG_USER_ONLY
1254 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
1255 .supported = instructions_supported,
1256 .get_count = instructions_get_count,
Aaron Lindsay OS4e7beb02019-02-01 14:55:45 +00001257 .ns_per_count = instructions_ns_per,
Aaron Lindsayb2e23722019-01-21 10:23:14 +00001258 },
1259 { .number = 0x011, /* CPU_CYCLES, Cycle */
1260 .supported = event_always_supported,
1261 .get_count = cycles_get_count,
Aaron Lindsay OS4e7beb02019-02-01 14:55:45 +00001262 .ns_per_count = cycles_ns_per,
Peter Maydell0727f632020-02-14 17:51:08 +00001263 },
Aaron Lindsayb2e23722019-01-21 10:23:14 +00001264#endif
Peter Maydell0727f632020-02-14 17:51:08 +00001265 { .number = 0x023, /* STALL_FRONTEND */
1266 .supported = pmu_8_1_events_supported,
1267 .get_count = zero_event_get_count,
1268 .ns_per_count = zero_event_ns_per,
1269 },
1270 { .number = 0x024, /* STALL_BACKEND */
1271 .supported = pmu_8_1_events_supported,
1272 .get_count = zero_event_get_count,
1273 .ns_per_count = zero_event_ns_per,
1274 },
Peter Maydell15dd1eb2020-02-14 17:51:09 +00001275 { .number = 0x03c, /* STALL */
1276 .supported = pmu_8_4_events_supported,
1277 .get_count = zero_event_get_count,
1278 .ns_per_count = zero_event_ns_per,
1279 },
Aaron Lindsay57a4a112019-01-21 10:23:14 +00001280};
1281
1282/*
1283 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1284 * events (i.e. the statistical profiling extension), this implementation
1285 * should first be updated to something sparse instead of the current
1286 * supported_event_map[] array.
1287 */
Peter Maydell15dd1eb2020-02-14 17:51:09 +00001288#define MAX_EVENT_ID 0x3c
Aaron Lindsay57a4a112019-01-21 10:23:14 +00001289#define UNSUPPORTED_EVENT UINT16_MAX
1290static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1291
1292/*
Aaron Lindsay OSbf8d0962019-01-29 11:46:04 +00001293 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1294 * of ARM event numbers to indices in our pm_events array.
Aaron Lindsay57a4a112019-01-21 10:23:14 +00001295 *
1296 * Note: Events in the 0x40XX range are not currently supported.
1297 */
Aaron Lindsay OSbf8d0962019-01-29 11:46:04 +00001298void pmu_init(ARMCPU *cpu)
Aaron Lindsay57a4a112019-01-21 10:23:14 +00001299{
Aaron Lindsay57a4a112019-01-21 10:23:14 +00001300 unsigned int i;
1301
Aaron Lindsay OSbf8d0962019-01-29 11:46:04 +00001302 /*
1303 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1304 * events to them
1305 */
Aaron Lindsay57a4a112019-01-21 10:23:14 +00001306 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1307 supported_event_map[i] = UNSUPPORTED_EVENT;
1308 }
Aaron Lindsay OSbf8d0962019-01-29 11:46:04 +00001309 cpu->pmceid0 = 0;
1310 cpu->pmceid1 = 0;
Aaron Lindsay57a4a112019-01-21 10:23:14 +00001311
1312 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1313 const pm_event *cnt = &pm_events[i];
1314 assert(cnt->number <= MAX_EVENT_ID);
1315 /* We do not currently support events in the 0x40xx range */
1316 assert(cnt->number <= 0x3f);
1317
Aaron Lindsay OSbf8d0962019-01-29 11:46:04 +00001318 if (cnt->supported(&cpu->env)) {
Aaron Lindsay57a4a112019-01-21 10:23:14 +00001319 supported_event_map[cnt->number] = i;
Aaron Lindsay OS67da43d2019-02-21 18:17:45 +00001320 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
Aaron Lindsay OSbf8d0962019-01-29 11:46:04 +00001321 if (cnt->number & 0x20) {
1322 cpu->pmceid1 |= event_mask;
1323 } else {
1324 cpu->pmceid0 |= event_mask;
1325 }
Aaron Lindsay57a4a112019-01-21 10:23:14 +00001326 }
1327 }
Aaron Lindsay57a4a112019-01-21 10:23:14 +00001328}
1329
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001330/*
1331 * Check at runtime whether a PMU event is supported for the current machine
1332 */
1333static bool event_supported(uint16_t number)
1334{
1335 if (number > MAX_EVENT_ID) {
1336 return false;
1337 }
1338 return supported_event_map[number] != UNSUPPORTED_EVENT;
1339}
1340
Peter Maydell3f208fd2016-02-11 11:17:31 +00001341static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1342 bool isread)
Peter Maydell200ac0e2012-06-20 11:57:12 +00001343{
Stefan Weil3b163b02014-03-07 19:48:59 +01001344 /* Performance monitor registers user accessibility is controlled
Peter Maydell1fce1ba2016-02-19 14:39:44 +00001345 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1346 * trapping to EL2 or EL3 for other accesses.
Peter Maydell200ac0e2012-06-20 11:57:12 +00001347 */
Peter Maydell1fce1ba2016-02-19 14:39:44 +00001348 int el = arm_current_el(env);
1349
Andrew Baumann6ecd0b62017-03-14 11:28:54 +00001350 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
Peter Maydellfcd25202014-02-20 10:35:52 +00001351 return CP_ACCESS_TRAP;
Peter Maydell200ac0e2012-06-20 11:57:12 +00001352 }
Peter Maydell1fce1ba2016-02-19 14:39:44 +00001353 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
1354 && !arm_is_secure_below_el3(env)) {
1355 return CP_ACCESS_TRAP_EL2;
1356 }
1357 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1358 return CP_ACCESS_TRAP_EL3;
1359 }
1360
Peter Maydellfcd25202014-02-20 10:35:52 +00001361 return CP_ACCESS_OK;
Peter Maydell200ac0e2012-06-20 11:57:12 +00001362}
1363
Andrew Baumann6ecd0b62017-03-14 11:28:54 +00001364static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1365 const ARMCPRegInfo *ri,
1366 bool isread)
1367{
1368 /* ER: event counter read trap control */
1369 if (arm_feature(env, ARM_FEATURE_V8)
1370 && arm_current_el(env) == 0
1371 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1372 && isread) {
1373 return CP_ACCESS_OK;
1374 }
1375
1376 return pmreg_access(env, ri, isread);
1377}
1378
1379static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1380 const ARMCPRegInfo *ri,
1381 bool isread)
1382{
1383 /* SW: software increment write trap control */
1384 if (arm_feature(env, ARM_FEATURE_V8)
1385 && arm_current_el(env) == 0
1386 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1387 && !isread) {
1388 return CP_ACCESS_OK;
1389 }
1390
1391 return pmreg_access(env, ri, isread);
1392}
1393
Andrew Baumann6ecd0b62017-03-14 11:28:54 +00001394static CPAccessResult pmreg_access_selr(CPUARMState *env,
1395 const ARMCPRegInfo *ri,
1396 bool isread)
1397{
1398 /* ER: event counter read trap control */
1399 if (arm_feature(env, ARM_FEATURE_V8)
1400 && arm_current_el(env) == 0
1401 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1402 return CP_ACCESS_OK;
1403 }
1404
1405 return pmreg_access(env, ri, isread);
1406}
1407
1408static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1409 const ARMCPRegInfo *ri,
1410 bool isread)
1411{
1412 /* CR: cycle counter read trap control */
1413 if (arm_feature(env, ARM_FEATURE_V8)
1414 && arm_current_el(env) == 0
1415 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1416 && isread) {
1417 return CP_ACCESS_OK;
1418 }
1419
1420 return pmreg_access(env, ri, isread);
1421}
1422
Aaron Lindsay033614c2019-01-21 10:23:14 +00001423/* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1424 * the current EL, security state, and register configuration.
1425 */
1426static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
Alistair Francis87124fd2014-08-29 15:00:29 +01001427{
Aaron Lindsay033614c2019-01-21 10:23:14 +00001428 uint64_t filter;
1429 bool e, p, u, nsk, nsu, nsh, m;
1430 bool enabled, prohibited, filtered;
1431 bool secure = arm_is_secure(env);
1432 int el = arm_current_el(env);
1433 uint8_t hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
Alistair Francis87124fd2014-08-29 15:00:29 +01001434
Andrew Jonescbbb3042019-03-25 14:16:47 +00001435 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1436 return false;
1437 }
1438
Aaron Lindsay033614c2019-01-21 10:23:14 +00001439 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1440 (counter < hpmn || counter == 31)) {
1441 e = env->cp15.c9_pmcr & PMCRE;
1442 } else {
1443 e = env->cp15.mdcr_el2 & MDCR_HPME;
1444 }
1445 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1446
1447 if (!secure) {
1448 if (el == 2 && (counter < hpmn || counter == 31)) {
1449 prohibited = env->cp15.mdcr_el2 & MDCR_HPMD;
1450 } else {
1451 prohibited = false;
1452 }
1453 } else {
1454 prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
1455 (env->cp15.mdcr_el3 & MDCR_SPME);
Alistair Francis87124fd2014-08-29 15:00:29 +01001456 }
1457
Aaron Lindsay033614c2019-01-21 10:23:14 +00001458 if (prohibited && counter == 31) {
1459 prohibited = env->cp15.c9_pmcr & PMCRDP;
1460 }
1461
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001462 if (counter == 31) {
1463 filter = env->cp15.pmccfiltr_el0;
1464 } else {
1465 filter = env->cp15.c14_pmevtyper[counter];
1466 }
Aaron Lindsay033614c2019-01-21 10:23:14 +00001467
1468 p = filter & PMXEVTYPER_P;
1469 u = filter & PMXEVTYPER_U;
1470 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1471 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1472 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1473 m = arm_el_is_aa64(env, 1) &&
1474 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1475
1476 if (el == 0) {
1477 filtered = secure ? u : u != nsu;
1478 } else if (el == 1) {
1479 filtered = secure ? p : p != nsk;
1480 } else if (el == 2) {
1481 filtered = !nsh;
1482 } else { /* EL3 */
1483 filtered = m != p;
1484 }
1485
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001486 if (counter != 31) {
1487 /*
1488 * If not checking PMCCNTR, ensure the counter is setup to an event we
1489 * support
1490 */
1491 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1492 if (!event_supported(event)) {
1493 return false;
1494 }
1495 }
1496
Aaron Lindsay033614c2019-01-21 10:23:14 +00001497 return enabled && !prohibited && !filtered;
Alistair Francis87124fd2014-08-29 15:00:29 +01001498}
Aaron Lindsay033614c2019-01-21 10:23:14 +00001499
Aaron Lindsay OSf4efb4b2019-02-01 14:55:45 +00001500static void pmu_update_irq(CPUARMState *env)
1501{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07001502 ARMCPU *cpu = env_archcpu(env);
Aaron Lindsay OSf4efb4b2019-02-01 14:55:45 +00001503 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1504 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1505}
1506
Aaron Lindsay5d05b9d2019-01-21 10:23:13 +00001507/*
1508 * Ensure c15_ccnt is the guest-visible count so that operations such as
1509 * enabling/disabling the counter or filtering, modifying the count itself,
1510 * etc. can be done logically. This is essentially a no-op if the counter is
1511 * not enabled at the time of the call.
1512 */
Andrew Jonesf2b2f532019-03-25 14:16:47 +00001513static void pmccntr_op_start(CPUARMState *env)
Alistair Francisec7b4ce2014-08-29 15:00:29 +01001514{
Aaron Lindsayb2e23722019-01-21 10:23:14 +00001515 uint64_t cycles = cycles_get_count(env);
Alistair Francisec7b4ce2014-08-29 15:00:29 +01001516
Aaron Lindsay033614c2019-01-21 10:23:14 +00001517 if (pmu_counter_enabled(env, 31)) {
Aaron Lindsay5d05b9d2019-01-21 10:23:13 +00001518 uint64_t eff_cycles = cycles;
1519 if (env->cp15.c9_pmcr & PMCRD) {
1520 /* Increment once every 64 processor clock cycles */
1521 eff_cycles /= 64;
1522 }
1523
Aaron Lindsay OSf4efb4b2019-02-01 14:55:45 +00001524 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1525
1526 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1527 1ull << 63 : 1ull << 31;
1528 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1529 env->cp15.c9_pmovsr |= (1 << 31);
1530 pmu_update_irq(env);
1531 }
1532
1533 env->cp15.c15_ccnt = new_pmccntr;
Alistair Francisec7b4ce2014-08-29 15:00:29 +01001534 }
Aaron Lindsay5d05b9d2019-01-21 10:23:13 +00001535 env->cp15.c15_ccnt_delta = cycles;
1536}
1537
1538/*
1539 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1540 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1541 * pmccntr_op_start.
1542 */
Andrew Jonesf2b2f532019-03-25 14:16:47 +00001543static void pmccntr_op_finish(CPUARMState *env)
Aaron Lindsay5d05b9d2019-01-21 10:23:13 +00001544{
Aaron Lindsay033614c2019-01-21 10:23:14 +00001545 if (pmu_counter_enabled(env, 31)) {
Aaron Lindsay OS4e7beb02019-02-01 14:55:45 +00001546#ifndef CONFIG_USER_ONLY
1547 /* Calculate when the counter will next overflow */
1548 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1549 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1550 remaining_cycles = (uint32_t)remaining_cycles;
1551 }
1552 int64_t overflow_in = cycles_ns_per(remaining_cycles);
Aaron Lindsay5d05b9d2019-01-21 10:23:13 +00001553
Aaron Lindsay OS4e7beb02019-02-01 14:55:45 +00001554 if (overflow_in > 0) {
1555 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1556 overflow_in;
Richard Henderson2fc0cc02019-03-22 17:41:14 -07001557 ARMCPU *cpu = env_archcpu(env);
Aaron Lindsay OS4e7beb02019-02-01 14:55:45 +00001558 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1559 }
1560#endif
1561
1562 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
Aaron Lindsay5d05b9d2019-01-21 10:23:13 +00001563 if (env->cp15.c9_pmcr & PMCRD) {
1564 /* Increment once every 64 processor clock cycles */
1565 prev_cycles /= 64;
1566 }
Aaron Lindsay5d05b9d2019-01-21 10:23:13 +00001567 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1568 }
1569}
1570
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001571static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1572{
1573
1574 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1575 uint64_t count = 0;
1576 if (event_supported(event)) {
1577 uint16_t event_idx = supported_event_map[event];
1578 count = pm_events[event_idx].get_count(env);
1579 }
1580
1581 if (pmu_counter_enabled(env, counter)) {
Aaron Lindsay OSf4efb4b2019-02-01 14:55:45 +00001582 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1583
1584 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1585 env->cp15.c9_pmovsr |= (1 << counter);
1586 pmu_update_irq(env);
1587 }
1588 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001589 }
1590 env->cp15.c14_pmevcntr_delta[counter] = count;
1591}
1592
1593static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1594{
1595 if (pmu_counter_enabled(env, counter)) {
Aaron Lindsay OS4e7beb02019-02-01 14:55:45 +00001596#ifndef CONFIG_USER_ONLY
1597 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1598 uint16_t event_idx = supported_event_map[event];
1599 uint64_t delta = UINT32_MAX -
1600 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1601 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1602
1603 if (overflow_in > 0) {
1604 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1605 overflow_in;
Richard Henderson2fc0cc02019-03-22 17:41:14 -07001606 ARMCPU *cpu = env_archcpu(env);
Aaron Lindsay OS4e7beb02019-02-01 14:55:45 +00001607 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1608 }
1609#endif
1610
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001611 env->cp15.c14_pmevcntr_delta[counter] -=
1612 env->cp15.c14_pmevcntr[counter];
1613 }
1614}
1615
Aaron Lindsay5d05b9d2019-01-21 10:23:13 +00001616void pmu_op_start(CPUARMState *env)
1617{
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001618 unsigned int i;
Aaron Lindsay5d05b9d2019-01-21 10:23:13 +00001619 pmccntr_op_start(env);
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001620 for (i = 0; i < pmu_num_counters(env); i++) {
1621 pmevcntr_op_start(env, i);
1622 }
Aaron Lindsay5d05b9d2019-01-21 10:23:13 +00001623}
1624
1625void pmu_op_finish(CPUARMState *env)
1626{
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001627 unsigned int i;
Aaron Lindsay5d05b9d2019-01-21 10:23:13 +00001628 pmccntr_op_finish(env);
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001629 for (i = 0; i < pmu_num_counters(env); i++) {
1630 pmevcntr_op_finish(env, i);
1631 }
Alistair Francisec7b4ce2014-08-29 15:00:29 +01001632}
1633
Aaron Lindsay033614c2019-01-21 10:23:14 +00001634void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1635{
1636 pmu_op_start(&cpu->env);
1637}
1638
1639void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1640{
1641 pmu_op_finish(&cpu->env);
1642}
1643
Aaron Lindsay OS4e7beb02019-02-01 14:55:45 +00001644void arm_pmu_timer_cb(void *opaque)
1645{
1646 ARMCPU *cpu = opaque;
1647
1648 /*
1649 * Update all the counter values based on the current underlying counts,
1650 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1651 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1652 * counter may expire.
1653 */
1654 pmu_op_start(&cpu->env);
1655 pmu_op_finish(&cpu->env);
1656}
1657
Peter Maydellc4241c72014-02-20 10:35:54 +00001658static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1659 uint64_t value)
Peter Maydell200ac0e2012-06-20 11:57:12 +00001660{
Aaron Lindsay5d05b9d2019-01-21 10:23:13 +00001661 pmu_op_start(env);
Alistair Francis7c2cb422014-03-10 14:56:28 +00001662
1663 if (value & PMCRC) {
1664 /* The counter has been reset */
1665 env->cp15.c15_ccnt = 0;
1666 }
1667
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001668 if (value & PMCRP) {
1669 unsigned int i;
1670 for (i = 0; i < pmu_num_counters(env); i++) {
1671 env->cp15.c14_pmevcntr[i] = 0;
1672 }
1673 }
1674
Peter Maydell62d96ff2020-02-14 17:51:12 +00001675 env->cp15.c9_pmcr &= ~PMCR_WRITEABLE_MASK;
1676 env->cp15.c9_pmcr |= (value & PMCR_WRITEABLE_MASK);
Alistair Francis7c2cb422014-03-10 14:56:28 +00001677
Aaron Lindsay5d05b9d2019-01-21 10:23:13 +00001678 pmu_op_finish(env);
Peter Maydell200ac0e2012-06-20 11:57:12 +00001679}
1680
Aaron Lindsay0d4bfd72019-01-21 10:23:14 +00001681static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1682 uint64_t value)
1683{
1684 unsigned int i;
1685 for (i = 0; i < pmu_num_counters(env); i++) {
1686 /* Increment a counter's count iff: */
1687 if ((value & (1 << i)) && /* counter's bit is set */
1688 /* counter is enabled and not filtered */
1689 pmu_counter_enabled(env, i) &&
1690 /* counter is SW_INCR */
1691 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1692 pmevcntr_op_start(env, i);
Aaron Lindsay OSf4efb4b2019-02-01 14:55:45 +00001693
1694 /*
1695 * Detect if this write causes an overflow since we can't predict
1696 * PMSWINC overflows like we can for other events
1697 */
1698 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1699
1700 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1701 env->cp15.c9_pmovsr |= (1 << i);
1702 pmu_update_irq(env);
1703 }
1704
1705 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1706
Aaron Lindsay0d4bfd72019-01-21 10:23:14 +00001707 pmevcntr_op_finish(env, i);
1708 }
1709 }
1710}
1711
Alistair Francis7c2cb422014-03-10 14:56:28 +00001712static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1713{
Aaron Lindsay5d05b9d2019-01-21 10:23:13 +00001714 uint64_t ret;
1715 pmccntr_op_start(env);
1716 ret = env->cp15.c15_ccnt;
1717 pmccntr_op_finish(env);
1718 return ret;
Alistair Francis7c2cb422014-03-10 14:56:28 +00001719}
1720
Wei Huang6b040782017-02-10 17:40:28 +00001721static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1722 uint64_t value)
1723{
1724 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1725 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1726 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1727 * accessed.
1728 */
1729 env->cp15.c9_pmselr = value & 0x1f;
1730}
1731
Alistair Francis7c2cb422014-03-10 14:56:28 +00001732static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1733 uint64_t value)
1734{
Aaron Lindsay5d05b9d2019-01-21 10:23:13 +00001735 pmccntr_op_start(env);
1736 env->cp15.c15_ccnt = value;
1737 pmccntr_op_finish(env);
Alistair Francis7c2cb422014-03-10 14:56:28 +00001738}
Peter Crosthwaite421c7eb2014-08-29 15:00:29 +01001739
1740static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1741 uint64_t value)
1742{
1743 uint64_t cur_val = pmccntr_read(env, NULL);
1744
1745 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1746}
1747
Alistair Francis06146012014-08-29 15:00:30 +01001748static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1749 uint64_t value)
1750{
Aaron Lindsay5d05b9d2019-01-21 10:23:13 +00001751 pmccntr_op_start(env);
Aaron Lindsay4b8afa12019-01-21 10:23:14 +00001752 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
Aaron Lindsay5d05b9d2019-01-21 10:23:13 +00001753 pmccntr_op_finish(env);
Alistair Francis06146012014-08-29 15:00:30 +01001754}
1755
Aaron Lindsay4b8afa12019-01-21 10:23:14 +00001756static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1757 uint64_t value)
1758{
1759 pmccntr_op_start(env);
1760 /* M is not accessible from AArch32 */
1761 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1762 (value & PMCCFILTR);
1763 pmccntr_op_finish(env);
1764}
1765
1766static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1767{
1768 /* M is not visible in AArch32 */
1769 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1770}
1771
Peter Maydellc4241c72014-02-20 10:35:54 +00001772static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
Peter Maydell200ac0e2012-06-20 11:57:12 +00001773 uint64_t value)
1774{
Aaron Lindsay7ece99b2018-04-26 11:04:39 +01001775 value &= pmu_counter_mask(env);
Peter Maydell200ac0e2012-06-20 11:57:12 +00001776 env->cp15.c9_pmcnten |= value;
Peter Maydell200ac0e2012-06-20 11:57:12 +00001777}
1778
Peter Maydellc4241c72014-02-20 10:35:54 +00001779static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1780 uint64_t value)
Peter Maydell200ac0e2012-06-20 11:57:12 +00001781{
Aaron Lindsay7ece99b2018-04-26 11:04:39 +01001782 value &= pmu_counter_mask(env);
Peter Maydell200ac0e2012-06-20 11:57:12 +00001783 env->cp15.c9_pmcnten &= ~value;
Peter Maydell200ac0e2012-06-20 11:57:12 +00001784}
1785
Peter Maydellc4241c72014-02-20 10:35:54 +00001786static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1787 uint64_t value)
Peter Maydell200ac0e2012-06-20 11:57:12 +00001788{
Aaron Lindsay599b71e2018-10-10 16:37:23 -04001789 value &= pmu_counter_mask(env);
Peter Maydell200ac0e2012-06-20 11:57:12 +00001790 env->cp15.c9_pmovsr &= ~value;
Aaron Lindsay OSf4efb4b2019-02-01 14:55:45 +00001791 pmu_update_irq(env);
Peter Maydell200ac0e2012-06-20 11:57:12 +00001792}
1793
Aaron Lindsay327dd512019-01-21 10:23:14 +00001794static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1795 uint64_t value)
1796{
1797 value &= pmu_counter_mask(env);
1798 env->cp15.c9_pmovsr |= value;
Aaron Lindsay OSf4efb4b2019-02-01 14:55:45 +00001799 pmu_update_irq(env);
Aaron Lindsay327dd512019-01-21 10:23:14 +00001800}
1801
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001802static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1803 uint64_t value, const uint8_t counter)
Peter Maydell200ac0e2012-06-20 11:57:12 +00001804{
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001805 if (counter == 31) {
1806 pmccfiltr_write(env, ri, value);
1807 } else if (counter < pmu_num_counters(env)) {
1808 pmevcntr_op_start(env, counter);
1809
1810 /*
1811 * If this counter's event type is changing, store the current
1812 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1813 * pmevcntr_op_finish has the correct baseline when it converts back to
1814 * a delta.
1815 */
1816 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1817 PMXEVTYPER_EVTCOUNT;
1818 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1819 if (old_event != new_event) {
1820 uint64_t count = 0;
1821 if (event_supported(new_event)) {
1822 uint16_t event_idx = supported_event_map[new_event];
1823 count = pm_events[event_idx].get_count(env);
1824 }
1825 env->cp15.c14_pmevcntr_delta[counter] = count;
1826 }
1827
1828 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1829 pmevcntr_op_finish(env, counter);
1830 }
Wei Huangfdb86652017-02-10 17:40:28 +00001831 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1832 * PMSELR value is equal to or greater than the number of implemented
1833 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1834 */
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001835}
1836
1837static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1838 const uint8_t counter)
1839{
1840 if (counter == 31) {
1841 return env->cp15.pmccfiltr_el0;
1842 } else if (counter < pmu_num_counters(env)) {
1843 return env->cp15.c14_pmevtyper[counter];
1844 } else {
1845 /*
1846 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1847 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1848 */
1849 return 0;
Wei Huangfdb86652017-02-10 17:40:28 +00001850 }
1851}
1852
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001853static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1854 uint64_t value)
1855{
1856 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1857 pmevtyper_write(env, ri, value, counter);
1858}
1859
1860static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1861 uint64_t value)
1862{
1863 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1864 env->cp15.c14_pmevtyper[counter] = value;
1865
1866 /*
1867 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1868 * pmu_op_finish calls when loading saved state for a migration. Because
1869 * we're potentially updating the type of event here, the value written to
1870 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1871 * different counter type. Therefore, we need to set this value to the
1872 * current count for the counter type we're writing so that pmu_op_finish
1873 * has the correct count for its calculation.
1874 */
1875 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1876 if (event_supported(event)) {
1877 uint16_t event_idx = supported_event_map[event];
1878 env->cp15.c14_pmevcntr_delta[counter] =
1879 pm_events[event_idx].get_count(env);
1880 }
1881}
1882
1883static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1884{
1885 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1886 return pmevtyper_read(env, ri, counter);
1887}
1888
1889static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1890 uint64_t value)
1891{
1892 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1893}
1894
Wei Huangfdb86652017-02-10 17:40:28 +00001895static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1896{
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001897 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1898}
1899
1900static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1901 uint64_t value, uint8_t counter)
1902{
1903 if (counter < pmu_num_counters(env)) {
1904 pmevcntr_op_start(env, counter);
1905 env->cp15.c14_pmevcntr[counter] = value;
1906 pmevcntr_op_finish(env, counter);
1907 }
1908 /*
1909 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1910 * are CONSTRAINED UNPREDICTABLE.
Wei Huangfdb86652017-02-10 17:40:28 +00001911 */
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001912}
1913
1914static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1915 uint8_t counter)
1916{
1917 if (counter < pmu_num_counters(env)) {
1918 uint64_t ret;
1919 pmevcntr_op_start(env, counter);
1920 ret = env->cp15.c14_pmevcntr[counter];
1921 pmevcntr_op_finish(env, counter);
1922 return ret;
Wei Huangfdb86652017-02-10 17:40:28 +00001923 } else {
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001924 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1925 * are CONSTRAINED UNPREDICTABLE. */
Wei Huangfdb86652017-02-10 17:40:28 +00001926 return 0;
1927 }
Peter Maydell200ac0e2012-06-20 11:57:12 +00001928}
1929
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00001930static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1931 uint64_t value)
1932{
1933 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1934 pmevcntr_write(env, ri, value, counter);
1935}
1936
1937static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1938{
1939 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1940 return pmevcntr_read(env, ri, counter);
1941}
1942
1943static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1944 uint64_t value)
1945{
1946 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1947 assert(counter < pmu_num_counters(env));
1948 env->cp15.c14_pmevcntr[counter] = value;
1949 pmevcntr_write(env, ri, value, counter);
1950}
1951
1952static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1953{
1954 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1955 assert(counter < pmu_num_counters(env));
1956 return env->cp15.c14_pmevcntr[counter];
1957}
1958
1959static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1960 uint64_t value)
1961{
1962 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1963}
1964
1965static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1966{
1967 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1968}
1969
Peter Maydellc4241c72014-02-20 10:35:54 +00001970static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
Peter Maydell200ac0e2012-06-20 11:57:12 +00001971 uint64_t value)
1972{
Andrew Baumann6ecd0b62017-03-14 11:28:54 +00001973 if (arm_feature(env, ARM_FEATURE_V8)) {
1974 env->cp15.c9_pmuserenr = value & 0xf;
1975 } else {
1976 env->cp15.c9_pmuserenr = value & 1;
1977 }
Peter Maydell200ac0e2012-06-20 11:57:12 +00001978}
1979
Peter Maydellc4241c72014-02-20 10:35:54 +00001980static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1981 uint64_t value)
Peter Maydell200ac0e2012-06-20 11:57:12 +00001982{
1983 /* We have no event counters so only the C bit can be changed */
Aaron Lindsay7ece99b2018-04-26 11:04:39 +01001984 value &= pmu_counter_mask(env);
Peter Maydell200ac0e2012-06-20 11:57:12 +00001985 env->cp15.c9_pminten |= value;
Aaron Lindsay OSf4efb4b2019-02-01 14:55:45 +00001986 pmu_update_irq(env);
Peter Maydell200ac0e2012-06-20 11:57:12 +00001987}
1988
Peter Maydellc4241c72014-02-20 10:35:54 +00001989static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1990 uint64_t value)
Peter Maydell200ac0e2012-06-20 11:57:12 +00001991{
Aaron Lindsay7ece99b2018-04-26 11:04:39 +01001992 value &= pmu_counter_mask(env);
Peter Maydell200ac0e2012-06-20 11:57:12 +00001993 env->cp15.c9_pminten &= ~value;
Aaron Lindsay OSf4efb4b2019-02-01 14:55:45 +00001994 pmu_update_irq(env);
Peter Maydell200ac0e2012-06-20 11:57:12 +00001995}
1996
Peter Maydellc4241c72014-02-20 10:35:54 +00001997static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1998 uint64_t value)
Nathan Rossi86411362013-10-25 15:44:38 +01001999{
Peter Maydella505d7f2014-02-26 17:20:04 +00002000 /* Note that even though the AArch64 view of this register has bits
2001 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
2002 * architectural requirements for bits which are RES0 only in some
2003 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
2004 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
2005 */
Edgar E. Iglesias855ea662014-05-27 17:09:54 +01002006 raw_write(env, ri, value & ~0x1FULL);
Nathan Rossi86411362013-10-25 15:44:38 +01002007}
2008
Edgar E. Iglesias64e0e2d2014-09-29 18:48:49 +01002009static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
2010{
Richard Hendersonea227472018-12-13 13:48:05 +00002011 /* Begin with base v8.0 state. */
2012 uint32_t valid_mask = 0x3fff;
Richard Henderson2fc0cc02019-03-22 17:41:14 -07002013 ARMCPU *cpu = env_archcpu(env);
Richard Hendersonea227472018-12-13 13:48:05 +00002014
Richard Henderson252e8c62020-06-25 20:31:00 -07002015 if (ri->state == ARM_CP_STATE_AA64) {
Richard Hendersonea227472018-12-13 13:48:05 +00002016 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */
2017 valid_mask &= ~SCR_NET;
Richard Henderson252e8c62020-06-25 20:31:00 -07002018
2019 if (cpu_isar_feature(aa64_lor, cpu)) {
2020 valid_mask |= SCR_TLOR;
2021 }
2022 if (cpu_isar_feature(aa64_pauth, cpu)) {
2023 valid_mask |= SCR_API | SCR_APK;
2024 }
Richard Henderson8ddb3002020-06-25 20:31:02 -07002025 if (cpu_isar_feature(aa64_mte, cpu)) {
2026 valid_mask |= SCR_ATA;
2027 }
Richard Hendersonea227472018-12-13 13:48:05 +00002028 } else {
2029 valid_mask &= ~(SCR_RW | SCR_ST);
2030 }
Edgar E. Iglesias64e0e2d2014-09-29 18:48:49 +01002031
2032 if (!arm_feature(env, ARM_FEATURE_EL2)) {
2033 valid_mask &= ~SCR_HCE;
2034
2035 /* On ARMv7, SMD (or SCD as it is called in v7) is only
2036 * supported if EL2 exists. The bit is UNK/SBZP when
2037 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
2038 * when EL2 is unavailable.
Greg Bellows4eb27642015-04-26 16:49:26 +01002039 * On ARMv8, this bit is always available.
Edgar E. Iglesias64e0e2d2014-09-29 18:48:49 +01002040 */
Greg Bellows4eb27642015-04-26 16:49:26 +01002041 if (arm_feature(env, ARM_FEATURE_V7) &&
2042 !arm_feature(env, ARM_FEATURE_V8)) {
Edgar E. Iglesias64e0e2d2014-09-29 18:48:49 +01002043 valid_mask &= ~SCR_SMD;
2044 }
2045 }
2046
2047 /* Clear all-context RES0 bits. */
2048 value &= valid_mask;
2049 raw_write(env, ri, value);
2050}
2051
Marc Zyngier630fcd42019-12-01 12:20:14 +00002052static CPAccessResult access_aa64_tid2(CPUARMState *env,
2053 const ARMCPRegInfo *ri,
2054 bool isread)
2055{
2056 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
2057 return CP_ACCESS_TRAP_EL2;
2058 }
2059
2060 return CP_ACCESS_OK;
2061}
2062
Peter Maydellc4241c72014-02-20 10:35:54 +00002063static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydell776d4e52012-06-20 11:57:19 +00002064{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07002065 ARMCPU *cpu = env_archcpu(env);
Fabian Aggelerb85a1fd2014-12-11 12:07:50 +00002066
2067 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
2068 * bank
2069 */
2070 uint32_t index = A32_BANKED_REG_GET(env, csselr,
2071 ri->secure & ARM_CP_SECSTATE_S);
2072
2073 return cpu->ccsidr[index];
Peter Maydell776d4e52012-06-20 11:57:19 +00002074}
2075
Peter Maydellc4241c72014-02-20 10:35:54 +00002076static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2077 uint64_t value)
Peter Maydell776d4e52012-06-20 11:57:19 +00002078{
Fabian Aggeler8d5c7732014-06-09 15:43:22 +01002079 raw_write(env, ri, value & 0xf);
Peter Maydell776d4e52012-06-20 11:57:19 +00002080}
2081
Peter Maydell1090b9c2014-04-15 19:18:46 +01002082static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2083{
Richard Henderson29a0af62019-03-22 16:07:18 -07002084 CPUState *cs = env_cpu(env);
Richard Hendersonf7778442018-12-13 13:48:07 +00002085 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
Peter Maydell1090b9c2014-04-15 19:18:46 +01002086 uint64_t ret = 0;
Marc Zyngier7cf95ae2019-11-26 13:55:36 +00002087 bool allow_virt = (arm_current_el(env) == 1 &&
2088 (!arm_is_secure_below_el3(env) ||
2089 (env->cp15.scr_el3 & SCR_EEL2)));
Peter Maydell1090b9c2014-04-15 19:18:46 +01002090
Marc Zyngier7cf95ae2019-11-26 13:55:36 +00002091 if (allow_virt && (hcr_el2 & HCR_IMO)) {
Peter Maydell636540e2018-10-24 07:50:17 +01002092 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
2093 ret |= CPSR_I;
2094 }
2095 } else {
2096 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
2097 ret |= CPSR_I;
2098 }
Peter Maydell1090b9c2014-04-15 19:18:46 +01002099 }
Peter Maydell636540e2018-10-24 07:50:17 +01002100
Marc Zyngier7cf95ae2019-11-26 13:55:36 +00002101 if (allow_virt && (hcr_el2 & HCR_FMO)) {
Peter Maydell636540e2018-10-24 07:50:17 +01002102 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
2103 ret |= CPSR_F;
2104 }
2105 } else {
2106 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
2107 ret |= CPSR_F;
2108 }
Peter Maydell1090b9c2014-04-15 19:18:46 +01002109 }
Peter Maydell636540e2018-10-24 07:50:17 +01002110
Peter Maydell1090b9c2014-04-15 19:18:46 +01002111 /* External aborts are not possible in QEMU so A bit is always clear */
2112 return ret;
2113}
2114
Marc Zyngier93fbc982019-12-01 12:20:15 +00002115static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2116 bool isread)
2117{
2118 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2119 return CP_ACCESS_TRAP_EL2;
2120 }
2121
2122 return CP_ACCESS_OK;
2123}
2124
2125static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2126 bool isread)
2127{
2128 if (arm_feature(env, ARM_FEATURE_V8)) {
2129 return access_aa64_tid1(env, ri, isread);
2130 }
2131
2132 return CP_ACCESS_OK;
2133}
2134
Peter Maydelle9aa6c22012-06-20 11:57:09 +00002135static const ARMCPRegInfo v7_cp_reginfo[] = {
Peter Maydell7d57f402012-06-20 11:57:11 +00002136 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2137 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2138 .access = PL1_W, .type = ARM_CP_NOP },
Peter Maydell200ac0e2012-06-20 11:57:12 +00002139 /* Performance monitors are implementation defined in v7,
2140 * but with an ARM recommended set of registers, which we
Aaron Lindsayac689a22019-01-21 10:23:14 +00002141 * follow.
Peter Maydell200ac0e2012-06-20 11:57:12 +00002142 *
2143 * Performance registers fall into three categories:
2144 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2145 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2146 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2147 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2148 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2149 */
2150 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002151 .access = PL0_RW, .type = ARM_CP_ALIAS,
Alistair Francis85214662014-08-29 15:00:29 +01002152 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
Peter Maydellfcd25202014-02-20 10:35:52 +00002153 .writefn = pmcntenset_write,
2154 .accessfn = pmreg_access,
2155 .raw_writefn = raw_write },
Alistair Francis85214662014-08-29 15:00:29 +01002156 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
2157 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2158 .access = PL0_RW, .accessfn = pmreg_access,
2159 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2160 .writefn = pmcntenset_write, .raw_writefn = raw_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +00002161 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
Alistair Francis85214662014-08-29 15:00:29 +01002162 .access = PL0_RW,
2163 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
Peter Maydellfcd25202014-02-20 10:35:52 +00002164 .accessfn = pmreg_access,
2165 .writefn = pmcntenclr_write,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002166 .type = ARM_CP_ALIAS },
Alistair Francis85214662014-08-29 15:00:29 +01002167 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2168 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2169 .access = PL0_RW, .accessfn = pmreg_access,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002170 .type = ARM_CP_ALIAS,
Alistair Francis85214662014-08-29 15:00:29 +01002171 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2172 .writefn = pmcntenclr_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +00002173 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
Aaron Lindsay OSf4efb4b2019-02-01 14:55:45 +00002174 .access = PL0_RW, .type = ARM_CP_IO,
Aaron Lindsaye4e91a22018-04-26 11:04:39 +01002175 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
Peter Maydellfcd25202014-02-20 10:35:52 +00002176 .accessfn = pmreg_access,
2177 .writefn = pmovsr_write,
2178 .raw_writefn = raw_write },
Alistair Francis978364f2016-02-18 14:16:17 +00002179 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2180 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2181 .access = PL0_RW, .accessfn = pmreg_access,
Aaron Lindsay OSf4efb4b2019-02-01 14:55:45 +00002182 .type = ARM_CP_ALIAS | ARM_CP_IO,
Alistair Francis978364f2016-02-18 14:16:17 +00002183 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2184 .writefn = pmovsr_write,
2185 .raw_writefn = raw_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +00002186 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
Aaron Lindsay OSf4efb4b2019-02-01 14:55:45 +00002187 .access = PL0_W, .accessfn = pmreg_access_swinc,
2188 .type = ARM_CP_NO_RAW | ARM_CP_IO,
Aaron Lindsay0d4bfd72019-01-21 10:23:14 +00002189 .writefn = pmswinc_write },
2190 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2191 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
Aaron Lindsay OSf4efb4b2019-02-01 14:55:45 +00002192 .access = PL0_W, .accessfn = pmreg_access_swinc,
2193 .type = ARM_CP_NO_RAW | ARM_CP_IO,
Aaron Lindsay0d4bfd72019-01-21 10:23:14 +00002194 .writefn = pmswinc_write },
Wei Huang6b040782017-02-10 17:40:28 +00002195 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2196 .access = PL0_RW, .type = ARM_CP_ALIAS,
2197 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
Andrew Baumann6ecd0b62017-03-14 11:28:54 +00002198 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
Wei Huang6b040782017-02-10 17:40:28 +00002199 .raw_writefn = raw_write},
2200 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2201 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
Andrew Baumann6ecd0b62017-03-14 11:28:54 +00002202 .access = PL0_RW, .accessfn = pmreg_access_selr,
Wei Huang6b040782017-02-10 17:40:28 +00002203 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2204 .writefn = pmselr_write, .raw_writefn = raw_write, },
Peter Maydell200ac0e2012-06-20 11:57:12 +00002205 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
Aaron Lindsay169c8932018-04-26 11:04:39 +01002206 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
Peter Crosthwaite421c7eb2014-08-29 15:00:29 +01002207 .readfn = pmccntr_read, .writefn = pmccntr_write32,
Andrew Baumann6ecd0b62017-03-14 11:28:54 +00002208 .accessfn = pmreg_access_ccntr },
Alistair Francis85214662014-08-29 15:00:29 +01002209 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2210 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
Andrew Baumann6ecd0b62017-03-14 11:28:54 +00002211 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
Alistair Francis85214662014-08-29 15:00:29 +01002212 .type = ARM_CP_IO,
Aaron Lindsay980ebe82019-01-21 10:23:14 +00002213 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2214 .readfn = pmccntr_read, .writefn = pmccntr_write,
2215 .raw_readfn = raw_read, .raw_writefn = raw_write, },
Aaron Lindsay4b8afa12019-01-21 10:23:14 +00002216 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2217 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2218 .access = PL0_RW, .accessfn = pmreg_access,
2219 .type = ARM_CP_ALIAS | ARM_CP_IO,
2220 .resetvalue = 0, },
Alistair Francis85214662014-08-29 15:00:29 +01002221 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2222 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
Aaron Lindsay980ebe82019-01-21 10:23:14 +00002223 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
Alistair Francis85214662014-08-29 15:00:29 +01002224 .access = PL0_RW, .accessfn = pmreg_access,
2225 .type = ARM_CP_IO,
2226 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2227 .resetvalue = 0, },
Peter Maydell200ac0e2012-06-20 11:57:12 +00002228 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00002229 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2230 .accessfn = pmreg_access,
Wei Huangfdb86652017-02-10 17:40:28 +00002231 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2232 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2233 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00002234 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2235 .accessfn = pmreg_access,
Wei Huangfdb86652017-02-10 17:40:28 +00002236 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
Peter Maydell200ac0e2012-06-20 11:57:12 +00002237 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00002238 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2239 .accessfn = pmreg_access_xevcntr,
2240 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2241 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2242 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2243 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2244 .accessfn = pmreg_access_xevcntr,
2245 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
Peter Maydell200ac0e2012-06-20 11:57:12 +00002246 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
Peter Maydell1fce1ba2016-02-19 14:39:44 +00002247 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
Aaron Lindsaye4e91a22018-04-26 11:04:39 +01002248 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
Peter Maydell200ac0e2012-06-20 11:57:12 +00002249 .resetvalue = 0,
Peter Maydelld4e6df62013-06-25 18:16:07 +01002250 .writefn = pmuserenr_write, .raw_writefn = raw_write },
Alistair Francis8a83ffc2016-02-18 14:16:17 +00002251 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2252 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
Peter Maydell1fce1ba2016-02-19 14:39:44 +00002253 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
Alistair Francis8a83ffc2016-02-18 14:16:17 +00002254 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2255 .resetvalue = 0,
2256 .writefn = pmuserenr_write, .raw_writefn = raw_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +00002257 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
Peter Maydell1fce1ba2016-02-19 14:39:44 +00002258 .access = PL1_RW, .accessfn = access_tpm,
Aaron Lindsayb7d793a2018-06-29 15:11:18 +01002259 .type = ARM_CP_ALIAS | ARM_CP_IO,
Wei Huange6ec5452017-02-10 17:40:28 +00002260 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
Peter Maydell200ac0e2012-06-20 11:57:12 +00002261 .resetvalue = 0,
Peter Maydelld4e6df62013-06-25 18:16:07 +01002262 .writefn = pmintenset_write, .raw_writefn = raw_write },
Wei Huange6ec5452017-02-10 17:40:28 +00002263 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2264 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2265 .access = PL1_RW, .accessfn = access_tpm,
2266 .type = ARM_CP_IO,
2267 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2268 .writefn = pmintenset_write, .raw_writefn = raw_write,
2269 .resetvalue = 0x0 },
Peter Maydell200ac0e2012-06-20 11:57:12 +00002270 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
Aaron Lindsayfc5f6852018-10-10 16:37:22 -04002271 .access = PL1_RW, .accessfn = access_tpm,
2272 .type = ARM_CP_ALIAS | ARM_CP_IO,
Peter Maydell200ac0e2012-06-20 11:57:12 +00002273 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
Sergey Fedorovb061a822015-06-19 14:17:44 +01002274 .writefn = pmintenclr_write, },
Alistair Francis978364f2016-02-18 14:16:17 +00002275 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2276 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
Aaron Lindsayfc5f6852018-10-10 16:37:22 -04002277 .access = PL1_RW, .accessfn = access_tpm,
2278 .type = ARM_CP_ALIAS | ARM_CP_IO,
Alistair Francis978364f2016-02-18 14:16:17 +00002279 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2280 .writefn = pmintenclr_write },
Peter Maydell7da845b2014-02-26 17:20:01 +00002281 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2282 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
Marc Zyngier630fcd42019-12-01 12:20:14 +00002283 .access = PL1_R,
2284 .accessfn = access_aa64_tid2,
2285 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
Peter Maydell7da845b2014-02-26 17:20:01 +00002286 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2287 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
Marc Zyngier630fcd42019-12-01 12:20:14 +00002288 .access = PL1_RW,
2289 .accessfn = access_aa64_tid2,
2290 .writefn = csselr_write, .resetvalue = 0,
Fabian Aggelerb85a1fd2014-12-11 12:07:50 +00002291 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2292 offsetof(CPUARMState, cp15.csselr_ns) } },
Peter Maydell776d4e52012-06-20 11:57:19 +00002293 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2294 * just RAZ for all cores:
2295 */
Peter Maydell0ff644a2014-04-15 19:18:45 +01002296 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2297 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
Marc Zyngier93fbc982019-12-01 12:20:15 +00002298 .access = PL1_R, .type = ARM_CP_CONST,
2299 .accessfn = access_aa64_tid1,
2300 .resetvalue = 0 },
Peter Maydellf32cdad2014-04-15 19:18:47 +01002301 /* Auxiliary fault status registers: these also are IMPDEF, and we
2302 * choose to RAZ/WI for all cores.
2303 */
2304 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2305 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
Richard Henderson84929212020-03-05 16:09:17 +00002306 .access = PL1_RW, .accessfn = access_tvm_trvm,
2307 .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydellf32cdad2014-04-15 19:18:47 +01002308 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2309 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
Richard Henderson84929212020-03-05 16:09:17 +00002310 .access = PL1_RW, .accessfn = access_tvm_trvm,
2311 .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydellb0fe2422014-02-26 17:20:03 +00002312 /* MAIR can just read-as-written because we don't implement caches
2313 * and so don't need to care about memory attributes.
2314 */
2315 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2316 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
Richard Henderson84929212020-03-05 16:09:17 +00002317 .access = PL1_RW, .accessfn = access_tvm_trvm,
2318 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
Peter Maydellb0fe2422014-02-26 17:20:03 +00002319 .resetvalue = 0 },
Peter Maydell4cfb8ad2015-08-25 15:45:07 +01002320 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2321 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2322 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2323 .resetvalue = 0 },
Peter Maydellb0fe2422014-02-26 17:20:03 +00002324 /* For non-long-descriptor page tables these are PRRR and NMRR;
2325 * regardless they still act as reads-as-written for QEMU.
Peter Maydellb0fe2422014-02-26 17:20:03 +00002326 */
Stefan Weil1281f8e2015-01-03 14:41:38 +01002327 /* MAIR0/1 are defined separately from their 64-bit counterpart which
Greg Bellowsbe693c82014-12-11 12:07:52 +00002328 * allows them to assign the correct fieldoffset based on the endianness
2329 * handled in the field definitions.
2330 */
Edgar E. Iglesiasa903c442015-06-02 14:56:21 +01002331 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
Richard Henderson84929212020-03-05 16:09:17 +00002332 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2333 .access = PL1_RW, .accessfn = access_tvm_trvm,
Greg Bellowsbe693c82014-12-11 12:07:52 +00002334 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2335 offsetof(CPUARMState, cp15.mair0_ns) },
Peter Maydellb0fe2422014-02-26 17:20:03 +00002336 .resetfn = arm_cp_reset_ignore },
Edgar E. Iglesiasa903c442015-06-02 14:56:21 +01002337 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
Richard Henderson84929212020-03-05 16:09:17 +00002338 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2339 .access = PL1_RW, .accessfn = access_tvm_trvm,
Greg Bellowsbe693c82014-12-11 12:07:52 +00002340 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2341 offsetof(CPUARMState, cp15.mair1_ns) },
Peter Maydellb0fe2422014-02-26 17:20:03 +00002342 .resetfn = arm_cp_reset_ignore },
Peter Maydell1090b9c2014-04-15 19:18:46 +01002343 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2344 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002345 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
Peter Maydell995939a2014-09-12 14:06:50 +01002346 /* 32 bit ITLB invalidates */
2347 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
Richard Henderson30881b72020-03-05 16:09:18 +00002348 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2349 .writefn = tlbiall_write },
Peter Maydell995939a2014-09-12 14:06:50 +01002350 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
Richard Henderson30881b72020-03-05 16:09:18 +00002351 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2352 .writefn = tlbimva_write },
Peter Maydell995939a2014-09-12 14:06:50 +01002353 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
Richard Henderson30881b72020-03-05 16:09:18 +00002354 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2355 .writefn = tlbiasid_write },
Peter Maydell995939a2014-09-12 14:06:50 +01002356 /* 32 bit DTLB invalidates */
2357 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
Richard Henderson30881b72020-03-05 16:09:18 +00002358 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2359 .writefn = tlbiall_write },
Peter Maydell995939a2014-09-12 14:06:50 +01002360 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
Richard Henderson30881b72020-03-05 16:09:18 +00002361 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2362 .writefn = tlbimva_write },
Peter Maydell995939a2014-09-12 14:06:50 +01002363 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
Richard Henderson30881b72020-03-05 16:09:18 +00002364 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2365 .writefn = tlbiasid_write },
Peter Maydell995939a2014-09-12 14:06:50 +01002366 /* 32 bit TLB invalidates */
2367 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
Richard Henderson30881b72020-03-05 16:09:18 +00002368 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2369 .writefn = tlbiall_write },
Peter Maydell995939a2014-09-12 14:06:50 +01002370 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
Richard Henderson30881b72020-03-05 16:09:18 +00002371 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2372 .writefn = tlbimva_write },
Peter Maydell995939a2014-09-12 14:06:50 +01002373 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
Richard Henderson30881b72020-03-05 16:09:18 +00002374 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2375 .writefn = tlbiasid_write },
Peter Maydell995939a2014-09-12 14:06:50 +01002376 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
Richard Henderson30881b72020-03-05 16:09:18 +00002377 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2378 .writefn = tlbimvaa_write },
Peter Maydell995939a2014-09-12 14:06:50 +01002379 REGINFO_SENTINEL
2380};
2381
2382static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2383 /* 32 bit TLB invalidates, Inner Shareable */
2384 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
Richard Henderson30881b72020-03-05 16:09:18 +00002385 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2386 .writefn = tlbiall_is_write },
Peter Maydell995939a2014-09-12 14:06:50 +01002387 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
Richard Henderson30881b72020-03-05 16:09:18 +00002388 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2389 .writefn = tlbimva_is_write },
Peter Maydell995939a2014-09-12 14:06:50 +01002390 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
Richard Henderson30881b72020-03-05 16:09:18 +00002391 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
Peter Maydellfa439fc2014-09-12 14:06:50 +01002392 .writefn = tlbiasid_is_write },
Peter Maydell995939a2014-09-12 14:06:50 +01002393 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
Richard Henderson30881b72020-03-05 16:09:18 +00002394 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
Peter Maydellfa439fc2014-09-12 14:06:50 +01002395 .writefn = tlbimvaa_is_write },
Peter Maydelle9aa6c22012-06-20 11:57:09 +00002396 REGINFO_SENTINEL
2397};
2398
Aaron Lindsay327dd512019-01-21 10:23:14 +00002399static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2400 /* PMOVSSET is not implemented in v7 before v7ve */
2401 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2402 .access = PL0_RW, .accessfn = pmreg_access,
Aaron Lindsay OSf4efb4b2019-02-01 14:55:45 +00002403 .type = ARM_CP_ALIAS | ARM_CP_IO,
Aaron Lindsay327dd512019-01-21 10:23:14 +00002404 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2405 .writefn = pmovsset_write,
2406 .raw_writefn = raw_write },
2407 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2408 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2409 .access = PL0_RW, .accessfn = pmreg_access,
Aaron Lindsay OSf4efb4b2019-02-01 14:55:45 +00002410 .type = ARM_CP_ALIAS | ARM_CP_IO,
Aaron Lindsay327dd512019-01-21 10:23:14 +00002411 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2412 .writefn = pmovsset_write,
2413 .raw_writefn = raw_write },
2414 REGINFO_SENTINEL
2415};
2416
Peter Maydellc4241c72014-02-20 10:35:54 +00002417static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2418 uint64_t value)
Peter Maydellc326b972012-06-20 11:57:10 +00002419{
2420 value &= 1;
2421 env->teecr = value;
Peter Maydellc326b972012-06-20 11:57:10 +00002422}
2423
Peter Maydell3f208fd2016-02-11 11:17:31 +00002424static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2425 bool isread)
Peter Maydellc326b972012-06-20 11:57:10 +00002426{
Greg Bellowsdcbff192014-10-24 12:19:14 +01002427 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
Peter Maydell92611c02014-02-20 10:35:53 +00002428 return CP_ACCESS_TRAP;
Peter Maydellc326b972012-06-20 11:57:10 +00002429 }
Peter Maydell92611c02014-02-20 10:35:53 +00002430 return CP_ACCESS_OK;
Peter Maydellc326b972012-06-20 11:57:10 +00002431}
2432
2433static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2434 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2435 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2436 .resetvalue = 0,
2437 .writefn = teecr_write },
2438 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2439 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
Peter Maydell92611c02014-02-20 10:35:53 +00002440 .accessfn = teehbr_access, .resetvalue = 0 },
Peter Maydellc326b972012-06-20 11:57:10 +00002441 REGINFO_SENTINEL
2442};
2443
Peter Maydell4d31c592012-06-20 11:57:11 +00002444static const ARMCPRegInfo v6k_cp_reginfo[] = {
Peter Maydelle4fe8302014-01-04 22:15:45 +00002445 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2446 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2447 .access = PL0_RW,
Fabian Aggeler54bf36e2014-12-11 12:07:52 +00002448 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
Peter Maydell4d31c592012-06-20 11:57:11 +00002449 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2450 .access = PL0_RW,
Fabian Aggeler54bf36e2014-12-11 12:07:52 +00002451 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2452 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
Peter Maydelle4fe8302014-01-04 22:15:45 +00002453 .resetfn = arm_cp_reset_ignore },
2454 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2455 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2456 .access = PL0_R|PL1_W,
Fabian Aggeler54bf36e2014-12-11 12:07:52 +00002457 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2458 .resetvalue = 0},
Peter Maydell4d31c592012-06-20 11:57:11 +00002459 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2460 .access = PL0_R|PL1_W,
Fabian Aggeler54bf36e2014-12-11 12:07:52 +00002461 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2462 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
Peter Maydelle4fe8302014-01-04 22:15:45 +00002463 .resetfn = arm_cp_reset_ignore },
Fabian Aggeler54bf36e2014-12-11 12:07:52 +00002464 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
Peter Maydelle4fe8302014-01-04 22:15:45 +00002465 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
Peter Maydell4d31c592012-06-20 11:57:11 +00002466 .access = PL1_RW,
Fabian Aggeler54bf36e2014-12-11 12:07:52 +00002467 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2468 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2469 .access = PL1_RW,
2470 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2471 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2472 .resetvalue = 0 },
Peter Maydell4d31c592012-06-20 11:57:11 +00002473 REGINFO_SENTINEL
2474};
2475
Peter Maydell55d284a2013-08-20 14:54:31 +01002476#ifndef CONFIG_USER_ONLY
2477
Peter Maydell3f208fd2016-02-11 11:17:31 +00002478static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2479 bool isread)
Peter Maydell00108f22014-02-20 10:35:53 +00002480{
Peter Maydell75502672016-02-18 14:16:15 +00002481 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2482 * Writable only at the highest implemented exception level.
2483 */
2484 int el = arm_current_el(env);
Richard Henderson5bc84372020-02-07 14:04:25 +00002485 uint64_t hcr;
2486 uint32_t cntkctl;
Peter Maydell75502672016-02-18 14:16:15 +00002487
2488 switch (el) {
2489 case 0:
Richard Henderson5bc84372020-02-07 14:04:25 +00002490 hcr = arm_hcr_el2_eff(env);
2491 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2492 cntkctl = env->cp15.cnthctl_el2;
2493 } else {
2494 cntkctl = env->cp15.c14_cntkctl;
2495 }
2496 if (!extract32(cntkctl, 0, 2)) {
Peter Maydell75502672016-02-18 14:16:15 +00002497 return CP_ACCESS_TRAP;
2498 }
2499 break;
2500 case 1:
2501 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2502 arm_is_secure_below_el3(env)) {
2503 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2504 return CP_ACCESS_TRAP_UNCATEGORIZED;
2505 }
2506 break;
2507 case 2:
2508 case 3:
2509 break;
Peter Maydell00108f22014-02-20 10:35:53 +00002510 }
Peter Maydell75502672016-02-18 14:16:15 +00002511
2512 if (!isread && el < arm_highest_el(env)) {
2513 return CP_ACCESS_TRAP_UNCATEGORIZED;
2514 }
2515
Peter Maydell00108f22014-02-20 10:35:53 +00002516 return CP_ACCESS_OK;
2517}
2518
Peter Maydell3f208fd2016-02-11 11:17:31 +00002519static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2520 bool isread)
Peter Maydell00108f22014-02-20 10:35:53 +00002521{
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01002522 unsigned int cur_el = arm_current_el(env);
2523 bool secure = arm_is_secure(env);
Richard Henderson5bc84372020-02-07 14:04:25 +00002524 uint64_t hcr = arm_hcr_el2_eff(env);
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01002525
Richard Henderson5bc84372020-02-07 14:04:25 +00002526 switch (cur_el) {
2527 case 0:
2528 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2529 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2530 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2531 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2532 }
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01002533
Richard Henderson5bc84372020-02-07 14:04:25 +00002534 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2535 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2536 return CP_ACCESS_TRAP;
2537 }
2538
2539 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2540 if (hcr & HCR_E2H) {
2541 if (timeridx == GTIMER_PHYS &&
2542 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2543 return CP_ACCESS_TRAP_EL2;
2544 }
2545 } else {
2546 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2547 if (arm_feature(env, ARM_FEATURE_EL2) &&
2548 timeridx == GTIMER_PHYS && !secure &&
2549 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2550 return CP_ACCESS_TRAP_EL2;
2551 }
2552 }
2553 break;
2554
2555 case 1:
2556 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2557 if (arm_feature(env, ARM_FEATURE_EL2) &&
2558 timeridx == GTIMER_PHYS && !secure &&
2559 (hcr & HCR_E2H
2560 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2561 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2562 return CP_ACCESS_TRAP_EL2;
2563 }
2564 break;
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01002565 }
Peter Maydell00108f22014-02-20 10:35:53 +00002566 return CP_ACCESS_OK;
2567}
2568
Peter Maydell3f208fd2016-02-11 11:17:31 +00002569static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2570 bool isread)
Peter Maydell00108f22014-02-20 10:35:53 +00002571{
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01002572 unsigned int cur_el = arm_current_el(env);
2573 bool secure = arm_is_secure(env);
Richard Henderson5bc84372020-02-07 14:04:25 +00002574 uint64_t hcr = arm_hcr_el2_eff(env);
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01002575
Richard Henderson5bc84372020-02-07 14:04:25 +00002576 switch (cur_el) {
2577 case 0:
2578 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2579 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2580 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2581 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2582 }
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01002583
Richard Henderson5bc84372020-02-07 14:04:25 +00002584 /*
2585 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2586 * EL0 if EL0[PV]TEN is zero.
2587 */
2588 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2589 return CP_ACCESS_TRAP;
2590 }
2591 /* fall through */
2592
2593 case 1:
2594 if (arm_feature(env, ARM_FEATURE_EL2) &&
2595 timeridx == GTIMER_PHYS && !secure) {
2596 if (hcr & HCR_E2H) {
2597 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2598 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2599 return CP_ACCESS_TRAP_EL2;
2600 }
2601 } else {
2602 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2603 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2604 return CP_ACCESS_TRAP_EL2;
2605 }
2606 }
2607 }
2608 break;
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01002609 }
Peter Maydell00108f22014-02-20 10:35:53 +00002610 return CP_ACCESS_OK;
2611}
2612
2613static CPAccessResult gt_pct_access(CPUARMState *env,
Peter Maydell3f208fd2016-02-11 11:17:31 +00002614 const ARMCPRegInfo *ri,
2615 bool isread)
Peter Maydell00108f22014-02-20 10:35:53 +00002616{
Peter Maydell3f208fd2016-02-11 11:17:31 +00002617 return gt_counter_access(env, GTIMER_PHYS, isread);
Peter Maydell00108f22014-02-20 10:35:53 +00002618}
2619
2620static CPAccessResult gt_vct_access(CPUARMState *env,
Peter Maydell3f208fd2016-02-11 11:17:31 +00002621 const ARMCPRegInfo *ri,
2622 bool isread)
Peter Maydell00108f22014-02-20 10:35:53 +00002623{
Peter Maydell3f208fd2016-02-11 11:17:31 +00002624 return gt_counter_access(env, GTIMER_VIRT, isread);
Peter Maydell00108f22014-02-20 10:35:53 +00002625}
2626
Peter Maydell3f208fd2016-02-11 11:17:31 +00002627static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2628 bool isread)
Peter Maydell00108f22014-02-20 10:35:53 +00002629{
Peter Maydell3f208fd2016-02-11 11:17:31 +00002630 return gt_timer_access(env, GTIMER_PHYS, isread);
Peter Maydell00108f22014-02-20 10:35:53 +00002631}
2632
Peter Maydell3f208fd2016-02-11 11:17:31 +00002633static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2634 bool isread)
Peter Maydell00108f22014-02-20 10:35:53 +00002635{
Peter Maydell3f208fd2016-02-11 11:17:31 +00002636 return gt_timer_access(env, GTIMER_VIRT, isread);
Peter Maydell00108f22014-02-20 10:35:53 +00002637}
2638
Peter Maydellb4d39782015-08-13 11:26:22 +01002639static CPAccessResult gt_stimer_access(CPUARMState *env,
Peter Maydell3f208fd2016-02-11 11:17:31 +00002640 const ARMCPRegInfo *ri,
2641 bool isread)
Peter Maydellb4d39782015-08-13 11:26:22 +01002642{
2643 /* The AArch64 register view of the secure physical timer is
2644 * always accessible from EL3, and configurably accessible from
2645 * Secure EL1.
2646 */
2647 switch (arm_current_el(env)) {
2648 case 1:
2649 if (!arm_is_secure(env)) {
2650 return CP_ACCESS_TRAP;
2651 }
2652 if (!(env->cp15.scr_el3 & SCR_ST)) {
2653 return CP_ACCESS_TRAP_EL3;
2654 }
2655 return CP_ACCESS_OK;
2656 case 0:
2657 case 2:
2658 return CP_ACCESS_TRAP;
2659 case 3:
2660 return CP_ACCESS_OK;
2661 default:
2662 g_assert_not_reached();
2663 }
2664}
2665
Peter Maydell55d284a2013-08-20 14:54:31 +01002666static uint64_t gt_get_countervalue(CPUARMState *env)
2667{
Andrew Jeffery7def8752019-12-20 14:02:59 +00002668 ARMCPU *cpu = env_archcpu(env);
2669
2670 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
Peter Maydell55d284a2013-08-20 14:54:31 +01002671}
2672
2673static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2674{
2675 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2676
2677 if (gt->ctl & 1) {
2678 /* Timer enabled: calculate and set current ISTATUS, irq, and
2679 * reset timer to when ISTATUS next has to change
2680 */
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01002681 uint64_t offset = timeridx == GTIMER_VIRT ?
2682 cpu->env.cp15.cntvoff_el2 : 0;
Peter Maydell55d284a2013-08-20 14:54:31 +01002683 uint64_t count = gt_get_countervalue(&cpu->env);
2684 /* Note that this must be unsigned 64 bit arithmetic: */
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01002685 int istatus = count - offset >= gt->cval;
Peter Maydell55d284a2013-08-20 14:54:31 +01002686 uint64_t nexttick;
Peter Maydell194cbc42016-10-12 18:54:34 +01002687 int irqstate;
Peter Maydell55d284a2013-08-20 14:54:31 +01002688
2689 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
Peter Maydell194cbc42016-10-12 18:54:34 +01002690
2691 irqstate = (istatus && !(gt->ctl & 2));
2692 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2693
Peter Maydell55d284a2013-08-20 14:54:31 +01002694 if (istatus) {
2695 /* Next transition is when count rolls back over to zero */
2696 nexttick = UINT64_MAX;
2697 } else {
2698 /* Next transition is when we hit cval */
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01002699 nexttick = gt->cval + offset;
Peter Maydell55d284a2013-08-20 14:54:31 +01002700 }
2701 /* Note that the desired next expiry time might be beyond the
2702 * signed-64-bit range of a QEMUTimer -- in this case we just
2703 * set the timer for as far in the future as possible. When the
2704 * timer expires we will reset the timer for any remaining period.
2705 */
Andrew Jeffery7def8752019-12-20 14:02:59 +00002706 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
Andrew Jeffery4a0245b2019-12-20 14:02:59 +00002707 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2708 } else {
2709 timer_mod(cpu->gt_timer[timeridx], nexttick);
Peter Maydell55d284a2013-08-20 14:54:31 +01002710 }
Peter Maydell194cbc42016-10-12 18:54:34 +01002711 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
Peter Maydell55d284a2013-08-20 14:54:31 +01002712 } else {
2713 /* Timer disabled: ISTATUS and timer output always clear */
2714 gt->ctl &= ~4;
2715 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
Alex Blighbc72ad62013-08-21 16:03:08 +01002716 timer_del(cpu->gt_timer[timeridx]);
Peter Maydell194cbc42016-10-12 18:54:34 +01002717 trace_arm_gt_recalc_disabled(timeridx);
Peter Maydell55d284a2013-08-20 14:54:31 +01002718 }
2719}
2720
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01002721static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2722 int timeridx)
Peter Maydell55d284a2013-08-20 14:54:31 +01002723{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07002724 ARMCPU *cpu = env_archcpu(env);
Peter Maydell55d284a2013-08-20 14:54:31 +01002725
Alex Blighbc72ad62013-08-21 16:03:08 +01002726 timer_del(cpu->gt_timer[timeridx]);
Peter Maydell55d284a2013-08-20 14:54:31 +01002727}
2728
Peter Maydellc4241c72014-02-20 10:35:54 +00002729static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydell55d284a2013-08-20 14:54:31 +01002730{
Peter Maydellc4241c72014-02-20 10:35:54 +00002731 return gt_get_countervalue(env);
Peter Maydell55d284a2013-08-20 14:54:31 +01002732}
2733
Richard Henderson53d1f852020-02-07 14:04:21 +00002734static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2735{
2736 uint64_t hcr;
2737
2738 switch (arm_current_el(env)) {
2739 case 2:
2740 hcr = arm_hcr_el2_eff(env);
2741 if (hcr & HCR_E2H) {
2742 return 0;
2743 }
2744 break;
2745 case 0:
2746 hcr = arm_hcr_el2_eff(env);
2747 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2748 return 0;
2749 }
2750 break;
2751 }
2752
2753 return env->cp15.cntvoff_el2;
2754}
2755
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01002756static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2757{
Richard Henderson53d1f852020-02-07 14:04:21 +00002758 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01002759}
2760
Peter Maydellc4241c72014-02-20 10:35:54 +00002761static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01002762 int timeridx,
Peter Maydellc4241c72014-02-20 10:35:54 +00002763 uint64_t value)
Peter Maydell55d284a2013-08-20 14:54:31 +01002764{
Peter Maydell194cbc42016-10-12 18:54:34 +01002765 trace_arm_gt_cval_write(timeridx, value);
Peter Maydell55d284a2013-08-20 14:54:31 +01002766 env->cp15.c14_timer[timeridx].cval = value;
Richard Henderson2fc0cc02019-03-22 17:41:14 -07002767 gt_recalc_timer(env_archcpu(env), timeridx);
Peter Maydell55d284a2013-08-20 14:54:31 +01002768}
Peter Maydellc4241c72014-02-20 10:35:54 +00002769
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01002770static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2771 int timeridx)
Peter Maydell55d284a2013-08-20 14:54:31 +01002772{
Richard Henderson53d1f852020-02-07 14:04:21 +00002773 uint64_t offset = 0;
2774
2775 switch (timeridx) {
2776 case GTIMER_VIRT:
Richard Henderson8c94b072020-02-07 14:04:25 +00002777 case GTIMER_HYPVIRT:
Richard Henderson53d1f852020-02-07 14:04:21 +00002778 offset = gt_virt_cnt_offset(env);
2779 break;
2780 }
Peter Maydell55d284a2013-08-20 14:54:31 +01002781
Peter Maydellc4241c72014-02-20 10:35:54 +00002782 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01002783 (gt_get_countervalue(env) - offset));
Peter Maydell55d284a2013-08-20 14:54:31 +01002784}
2785
Peter Maydellc4241c72014-02-20 10:35:54 +00002786static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01002787 int timeridx,
Peter Maydellc4241c72014-02-20 10:35:54 +00002788 uint64_t value)
Peter Maydell55d284a2013-08-20 14:54:31 +01002789{
Richard Henderson53d1f852020-02-07 14:04:21 +00002790 uint64_t offset = 0;
2791
2792 switch (timeridx) {
2793 case GTIMER_VIRT:
Richard Henderson8c94b072020-02-07 14:04:25 +00002794 case GTIMER_HYPVIRT:
Richard Henderson53d1f852020-02-07 14:04:21 +00002795 offset = gt_virt_cnt_offset(env);
2796 break;
2797 }
Peter Maydell55d284a2013-08-20 14:54:31 +01002798
Peter Maydell194cbc42016-10-12 18:54:34 +01002799 trace_arm_gt_tval_write(timeridx, value);
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01002800 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
Edgar E. Iglesias18084b22015-05-13 16:52:28 +10002801 sextract64(value, 0, 32);
Richard Henderson2fc0cc02019-03-22 17:41:14 -07002802 gt_recalc_timer(env_archcpu(env), timeridx);
Peter Maydell55d284a2013-08-20 14:54:31 +01002803}
2804
Peter Maydellc4241c72014-02-20 10:35:54 +00002805static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01002806 int timeridx,
Peter Maydellc4241c72014-02-20 10:35:54 +00002807 uint64_t value)
Peter Maydell55d284a2013-08-20 14:54:31 +01002808{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07002809 ARMCPU *cpu = env_archcpu(env);
Peter Maydell55d284a2013-08-20 14:54:31 +01002810 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2811
Peter Maydell194cbc42016-10-12 18:54:34 +01002812 trace_arm_gt_ctl_write(timeridx, value);
Peter Maydelld3afacc2014-06-09 15:43:26 +01002813 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
Peter Maydell55d284a2013-08-20 14:54:31 +01002814 if ((oldval ^ value) & 1) {
2815 /* Enable toggled */
2816 gt_recalc_timer(cpu, timeridx);
Peter Maydelld3afacc2014-06-09 15:43:26 +01002817 } else if ((oldval ^ value) & 2) {
Peter Maydell55d284a2013-08-20 14:54:31 +01002818 /* IMASK toggled: don't need to recalculate,
2819 * just set the interrupt line based on ISTATUS
2820 */
Peter Maydell194cbc42016-10-12 18:54:34 +01002821 int irqstate = (oldval & 4) && !(value & 2);
2822
2823 trace_arm_gt_imask_toggle(timeridx, irqstate);
2824 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
Peter Maydell55d284a2013-08-20 14:54:31 +01002825 }
Peter Maydell55d284a2013-08-20 14:54:31 +01002826}
2827
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01002828static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2829{
2830 gt_timer_reset(env, ri, GTIMER_PHYS);
2831}
2832
2833static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2834 uint64_t value)
2835{
2836 gt_cval_write(env, ri, GTIMER_PHYS, value);
2837}
2838
2839static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2840{
2841 return gt_tval_read(env, ri, GTIMER_PHYS);
2842}
2843
2844static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2845 uint64_t value)
2846{
2847 gt_tval_write(env, ri, GTIMER_PHYS, value);
2848}
2849
2850static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2851 uint64_t value)
2852{
2853 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2854}
2855
Richard Hendersonbb5972e2020-02-07 14:04:25 +00002856static int gt_phys_redir_timeridx(CPUARMState *env)
2857{
2858 switch (arm_mmu_idx(env)) {
2859 case ARMMMUIdx_E20_0:
2860 case ARMMMUIdx_E20_2:
Richard Henderson452ef8c2020-02-08 12:57:58 +00002861 case ARMMMUIdx_E20_2_PAN:
Richard Hendersonbb5972e2020-02-07 14:04:25 +00002862 return GTIMER_HYP;
2863 default:
2864 return GTIMER_PHYS;
2865 }
2866}
2867
2868static int gt_virt_redir_timeridx(CPUARMState *env)
2869{
2870 switch (arm_mmu_idx(env)) {
2871 case ARMMMUIdx_E20_0:
2872 case ARMMMUIdx_E20_2:
Richard Henderson452ef8c2020-02-08 12:57:58 +00002873 case ARMMMUIdx_E20_2_PAN:
Richard Hendersonbb5972e2020-02-07 14:04:25 +00002874 return GTIMER_HYPVIRT;
2875 default:
2876 return GTIMER_VIRT;
2877 }
2878}
2879
2880static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2881 const ARMCPRegInfo *ri)
2882{
2883 int timeridx = gt_phys_redir_timeridx(env);
2884 return env->cp15.c14_timer[timeridx].cval;
2885}
2886
2887static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2888 uint64_t value)
2889{
2890 int timeridx = gt_phys_redir_timeridx(env);
2891 gt_cval_write(env, ri, timeridx, value);
2892}
2893
2894static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2895 const ARMCPRegInfo *ri)
2896{
2897 int timeridx = gt_phys_redir_timeridx(env);
2898 return gt_tval_read(env, ri, timeridx);
2899}
2900
2901static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2902 uint64_t value)
2903{
2904 int timeridx = gt_phys_redir_timeridx(env);
2905 gt_tval_write(env, ri, timeridx, value);
2906}
2907
2908static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2909 const ARMCPRegInfo *ri)
2910{
2911 int timeridx = gt_phys_redir_timeridx(env);
2912 return env->cp15.c14_timer[timeridx].ctl;
2913}
2914
2915static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2916 uint64_t value)
2917{
2918 int timeridx = gt_phys_redir_timeridx(env);
2919 gt_ctl_write(env, ri, timeridx, value);
2920}
2921
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01002922static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2923{
2924 gt_timer_reset(env, ri, GTIMER_VIRT);
2925}
2926
2927static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2928 uint64_t value)
2929{
2930 gt_cval_write(env, ri, GTIMER_VIRT, value);
2931}
2932
2933static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2934{
2935 return gt_tval_read(env, ri, GTIMER_VIRT);
2936}
2937
2938static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2939 uint64_t value)
2940{
2941 gt_tval_write(env, ri, GTIMER_VIRT, value);
2942}
2943
2944static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2945 uint64_t value)
2946{
2947 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2948}
2949
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01002950static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2951 uint64_t value)
2952{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07002953 ARMCPU *cpu = env_archcpu(env);
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01002954
Peter Maydell194cbc42016-10-12 18:54:34 +01002955 trace_arm_gt_cntvoff_write(value);
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01002956 raw_write(env, ri, value);
2957 gt_recalc_timer(cpu, GTIMER_VIRT);
2958}
2959
Richard Hendersonbb5972e2020-02-07 14:04:25 +00002960static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2961 const ARMCPRegInfo *ri)
2962{
2963 int timeridx = gt_virt_redir_timeridx(env);
2964 return env->cp15.c14_timer[timeridx].cval;
2965}
2966
2967static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2968 uint64_t value)
2969{
2970 int timeridx = gt_virt_redir_timeridx(env);
2971 gt_cval_write(env, ri, timeridx, value);
2972}
2973
2974static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2975 const ARMCPRegInfo *ri)
2976{
2977 int timeridx = gt_virt_redir_timeridx(env);
2978 return gt_tval_read(env, ri, timeridx);
2979}
2980
2981static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2982 uint64_t value)
2983{
2984 int timeridx = gt_virt_redir_timeridx(env);
2985 gt_tval_write(env, ri, timeridx, value);
2986}
2987
2988static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2989 const ARMCPRegInfo *ri)
2990{
2991 int timeridx = gt_virt_redir_timeridx(env);
2992 return env->cp15.c14_timer[timeridx].ctl;
2993}
2994
2995static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2996 uint64_t value)
2997{
2998 int timeridx = gt_virt_redir_timeridx(env);
2999 gt_ctl_write(env, ri, timeridx, value);
3000}
3001
Edgar E. Iglesiasb0e66d92015-08-13 11:26:18 +01003002static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3003{
3004 gt_timer_reset(env, ri, GTIMER_HYP);
3005}
3006
3007static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3008 uint64_t value)
3009{
3010 gt_cval_write(env, ri, GTIMER_HYP, value);
3011}
3012
3013static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3014{
3015 return gt_tval_read(env, ri, GTIMER_HYP);
3016}
3017
3018static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3019 uint64_t value)
3020{
3021 gt_tval_write(env, ri, GTIMER_HYP, value);
3022}
3023
3024static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3025 uint64_t value)
3026{
3027 gt_ctl_write(env, ri, GTIMER_HYP, value);
3028}
3029
Peter Maydellb4d39782015-08-13 11:26:22 +01003030static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3031{
3032 gt_timer_reset(env, ri, GTIMER_SEC);
3033}
3034
3035static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3036 uint64_t value)
3037{
3038 gt_cval_write(env, ri, GTIMER_SEC, value);
3039}
3040
3041static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3042{
3043 return gt_tval_read(env, ri, GTIMER_SEC);
3044}
3045
3046static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3047 uint64_t value)
3048{
3049 gt_tval_write(env, ri, GTIMER_SEC, value);
3050}
3051
3052static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3053 uint64_t value)
3054{
3055 gt_ctl_write(env, ri, GTIMER_SEC, value);
3056}
3057
Richard Henderson8c94b072020-02-07 14:04:25 +00003058static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3059{
3060 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3061}
3062
3063static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3064 uint64_t value)
3065{
3066 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3067}
3068
3069static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3070{
3071 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3072}
3073
3074static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3075 uint64_t value)
3076{
3077 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3078}
3079
3080static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3081 uint64_t value)
3082{
3083 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3084}
3085
Peter Maydell55d284a2013-08-20 14:54:31 +01003086void arm_gt_ptimer_cb(void *opaque)
3087{
3088 ARMCPU *cpu = opaque;
3089
3090 gt_recalc_timer(cpu, GTIMER_PHYS);
3091}
3092
3093void arm_gt_vtimer_cb(void *opaque)
3094{
3095 ARMCPU *cpu = opaque;
3096
3097 gt_recalc_timer(cpu, GTIMER_VIRT);
3098}
3099
Edgar E. Iglesiasb0e66d92015-08-13 11:26:18 +01003100void arm_gt_htimer_cb(void *opaque)
3101{
3102 ARMCPU *cpu = opaque;
3103
3104 gt_recalc_timer(cpu, GTIMER_HYP);
3105}
3106
Peter Maydellb4d39782015-08-13 11:26:22 +01003107void arm_gt_stimer_cb(void *opaque)
3108{
3109 ARMCPU *cpu = opaque;
3110
3111 gt_recalc_timer(cpu, GTIMER_SEC);
3112}
3113
Richard Henderson8c94b072020-02-07 14:04:25 +00003114void arm_gt_hvtimer_cb(void *opaque)
3115{
3116 ARMCPU *cpu = opaque;
3117
3118 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3119}
3120
Andrew Jeffery96eec6b2019-12-20 14:02:59 +00003121static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3122{
3123 ARMCPU *cpu = env_archcpu(env);
3124
3125 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3126}
3127
Peter Maydell6cc7a3a2012-06-20 11:57:12 +00003128static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
Peter Maydell55d284a2013-08-20 14:54:31 +01003129 /* Note that CNTFRQ is purely reads-as-written for the benefit
3130 * of software; writing it doesn't actually change the timer frequency.
3131 * Our reset value matches the fixed frequency we implement the timer at.
3132 */
3133 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003134 .type = ARM_CP_ALIAS,
Peter Maydella7adc4b2014-02-26 17:20:05 +00003135 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3136 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
Peter Maydella7adc4b2014-02-26 17:20:05 +00003137 },
3138 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3139 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3140 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
Peter Maydell55d284a2013-08-20 14:54:31 +01003141 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
Andrew Jeffery96eec6b2019-12-20 14:02:59 +00003142 .resetfn = arm_gt_cntfrq_reset,
Peter Maydell55d284a2013-08-20 14:54:31 +01003143 },
3144 /* overall control: mostly access permissions */
Peter Maydella7adc4b2014-02-26 17:20:05 +00003145 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3146 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
Peter Maydell55d284a2013-08-20 14:54:31 +01003147 .access = PL1_RW,
3148 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3149 .resetvalue = 0,
3150 },
3151 /* per-timer control */
3152 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
Peter Maydell9ff9dd32015-08-13 11:26:22 +01003153 .secure = ARM_CP_SECSTATE_NS,
Dongjiu Gengdaf1dc52019-03-15 11:12:29 +00003154 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
Peter Maydella7adc4b2014-02-26 17:20:05 +00003155 .accessfn = gt_ptimer_access,
3156 .fieldoffset = offsetoflow32(CPUARMState,
3157 cp15.c14_timer[GTIMER_PHYS].ctl),
Richard Hendersonbb5972e2020-02-07 14:04:25 +00003158 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3159 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
Peter Maydella7adc4b2014-02-26 17:20:05 +00003160 },
Abdallah Bouassida9c513e72018-05-18 17:48:07 +01003161 { .name = "CNTP_CTL_S",
Peter Maydell9ff9dd32015-08-13 11:26:22 +01003162 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3163 .secure = ARM_CP_SECSTATE_S,
Dongjiu Gengdaf1dc52019-03-15 11:12:29 +00003164 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
Peter Maydell9ff9dd32015-08-13 11:26:22 +01003165 .accessfn = gt_ptimer_access,
3166 .fieldoffset = offsetoflow32(CPUARMState,
3167 cp15.c14_timer[GTIMER_SEC].ctl),
3168 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3169 },
Peter Maydella7adc4b2014-02-26 17:20:05 +00003170 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3171 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
Dongjiu Gengdaf1dc52019-03-15 11:12:29 +00003172 .type = ARM_CP_IO, .access = PL0_RW,
Peter Maydella7adc4b2014-02-26 17:20:05 +00003173 .accessfn = gt_ptimer_access,
Peter Maydell55d284a2013-08-20 14:54:31 +01003174 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3175 .resetvalue = 0,
Richard Hendersonbb5972e2020-02-07 14:04:25 +00003176 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3177 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
Peter Maydell55d284a2013-08-20 14:54:31 +01003178 },
3179 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
Dongjiu Gengdaf1dc52019-03-15 11:12:29 +00003180 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
Peter Maydella7adc4b2014-02-26 17:20:05 +00003181 .accessfn = gt_vtimer_access,
3182 .fieldoffset = offsetoflow32(CPUARMState,
3183 cp15.c14_timer[GTIMER_VIRT].ctl),
Richard Hendersonbb5972e2020-02-07 14:04:25 +00003184 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3185 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
Peter Maydella7adc4b2014-02-26 17:20:05 +00003186 },
3187 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3188 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
Dongjiu Gengdaf1dc52019-03-15 11:12:29 +00003189 .type = ARM_CP_IO, .access = PL0_RW,
Peter Maydella7adc4b2014-02-26 17:20:05 +00003190 .accessfn = gt_vtimer_access,
Peter Maydell55d284a2013-08-20 14:54:31 +01003191 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3192 .resetvalue = 0,
Richard Hendersonbb5972e2020-02-07 14:04:25 +00003193 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3194 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
Peter Maydell55d284a2013-08-20 14:54:31 +01003195 },
3196 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3197 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
Peter Maydell9ff9dd32015-08-13 11:26:22 +01003198 .secure = ARM_CP_SECSTATE_NS,
Dongjiu Gengdaf1dc52019-03-15 11:12:29 +00003199 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
Peter Maydell00108f22014-02-20 10:35:53 +00003200 .accessfn = gt_ptimer_access,
Richard Hendersonbb5972e2020-02-07 14:04:25 +00003201 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
Peter Maydell55d284a2013-08-20 14:54:31 +01003202 },
Abdallah Bouassida9c513e72018-05-18 17:48:07 +01003203 { .name = "CNTP_TVAL_S",
Peter Maydell9ff9dd32015-08-13 11:26:22 +01003204 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3205 .secure = ARM_CP_SECSTATE_S,
Dongjiu Gengdaf1dc52019-03-15 11:12:29 +00003206 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
Peter Maydell9ff9dd32015-08-13 11:26:22 +01003207 .accessfn = gt_ptimer_access,
3208 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3209 },
Peter Maydella7adc4b2014-02-26 17:20:05 +00003210 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3211 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
Dongjiu Gengdaf1dc52019-03-15 11:12:29 +00003212 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01003213 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
Richard Hendersonbb5972e2020-02-07 14:04:25 +00003214 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
Peter Maydella7adc4b2014-02-26 17:20:05 +00003215 },
Peter Maydell55d284a2013-08-20 14:54:31 +01003216 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
Dongjiu Gengdaf1dc52019-03-15 11:12:29 +00003217 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
Peter Maydell00108f22014-02-20 10:35:53 +00003218 .accessfn = gt_vtimer_access,
Richard Hendersonbb5972e2020-02-07 14:04:25 +00003219 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
Peter Maydell55d284a2013-08-20 14:54:31 +01003220 },
Peter Maydella7adc4b2014-02-26 17:20:05 +00003221 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3222 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
Dongjiu Gengdaf1dc52019-03-15 11:12:29 +00003223 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01003224 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
Richard Hendersonbb5972e2020-02-07 14:04:25 +00003225 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
Peter Maydella7adc4b2014-02-26 17:20:05 +00003226 },
Peter Maydell55d284a2013-08-20 14:54:31 +01003227 /* The counter itself */
3228 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003229 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
Peter Maydell00108f22014-02-20 10:35:53 +00003230 .accessfn = gt_pct_access,
Peter Maydella7adc4b2014-02-26 17:20:05 +00003231 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3232 },
3233 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3234 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003235 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
Edgar E. Iglesiasd57b9ee2015-08-13 11:26:18 +01003236 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
Peter Maydell55d284a2013-08-20 14:54:31 +01003237 },
3238 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003239 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
Peter Maydell00108f22014-02-20 10:35:53 +00003240 .accessfn = gt_vct_access,
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01003241 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
Peter Maydella7adc4b2014-02-26 17:20:05 +00003242 },
3243 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3244 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003245 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
Edgar E. Iglesiasd57b9ee2015-08-13 11:26:18 +01003246 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
Peter Maydell55d284a2013-08-20 14:54:31 +01003247 },
3248 /* Comparison value, indicating when the timer goes off */
3249 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
Peter Maydell9ff9dd32015-08-13 11:26:22 +01003250 .secure = ARM_CP_SECSTATE_NS,
Dongjiu Gengdaf1dc52019-03-15 11:12:29 +00003251 .access = PL0_RW,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003252 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
Peter Maydell55d284a2013-08-20 14:54:31 +01003253 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
Sergey Fedorovb061a822015-06-19 14:17:44 +01003254 .accessfn = gt_ptimer_access,
Richard Hendersonbb5972e2020-02-07 14:04:25 +00003255 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3256 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
Peter Maydella7adc4b2014-02-26 17:20:05 +00003257 },
Abdallah Bouassida9c513e72018-05-18 17:48:07 +01003258 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
Peter Maydell9ff9dd32015-08-13 11:26:22 +01003259 .secure = ARM_CP_SECSTATE_S,
Dongjiu Gengdaf1dc52019-03-15 11:12:29 +00003260 .access = PL0_RW,
Peter Maydell9ff9dd32015-08-13 11:26:22 +01003261 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3262 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3263 .accessfn = gt_ptimer_access,
3264 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3265 },
Peter Maydella7adc4b2014-02-26 17:20:05 +00003266 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3267 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
Dongjiu Gengdaf1dc52019-03-15 11:12:29 +00003268 .access = PL0_RW,
Peter Maydella7adc4b2014-02-26 17:20:05 +00003269 .type = ARM_CP_IO,
3270 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
Edgar E. Iglesias12cde082015-05-13 16:52:26 +10003271 .resetvalue = 0, .accessfn = gt_ptimer_access,
Richard Hendersonbb5972e2020-02-07 14:04:25 +00003272 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3273 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
Peter Maydell55d284a2013-08-20 14:54:31 +01003274 },
3275 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
Dongjiu Gengdaf1dc52019-03-15 11:12:29 +00003276 .access = PL0_RW,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003277 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
Peter Maydell55d284a2013-08-20 14:54:31 +01003278 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
Sergey Fedorovb061a822015-06-19 14:17:44 +01003279 .accessfn = gt_vtimer_access,
Richard Hendersonbb5972e2020-02-07 14:04:25 +00003280 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3281 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
Peter Maydella7adc4b2014-02-26 17:20:05 +00003282 },
3283 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3284 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
Dongjiu Gengdaf1dc52019-03-15 11:12:29 +00003285 .access = PL0_RW,
Peter Maydella7adc4b2014-02-26 17:20:05 +00003286 .type = ARM_CP_IO,
3287 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3288 .resetvalue = 0, .accessfn = gt_vtimer_access,
Richard Hendersonbb5972e2020-02-07 14:04:25 +00003289 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3290 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
Peter Maydell55d284a2013-08-20 14:54:31 +01003291 },
Peter Maydellb4d39782015-08-13 11:26:22 +01003292 /* Secure timer -- this is actually restricted to only EL3
3293 * and configurably Secure-EL1 via the accessfn.
3294 */
3295 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3296 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3297 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3298 .accessfn = gt_stimer_access,
3299 .readfn = gt_sec_tval_read,
3300 .writefn = gt_sec_tval_write,
3301 .resetfn = gt_sec_timer_reset,
3302 },
3303 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3304 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3305 .type = ARM_CP_IO, .access = PL1_RW,
3306 .accessfn = gt_stimer_access,
3307 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3308 .resetvalue = 0,
3309 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3310 },
3311 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3312 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3313 .type = ARM_CP_IO, .access = PL1_RW,
3314 .accessfn = gt_stimer_access,
3315 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3316 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3317 },
Peter Maydell6cc7a3a2012-06-20 11:57:12 +00003318 REGINFO_SENTINEL
3319};
3320
Richard Hendersonbb5972e2020-02-07 14:04:25 +00003321static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3322 bool isread)
3323{
3324 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3325 return CP_ACCESS_TRAP;
3326 }
3327 return CP_ACCESS_OK;
3328}
3329
Peter Maydell55d284a2013-08-20 14:54:31 +01003330#else
Alex Bennée26c4a832018-06-29 15:11:16 +01003331
3332/* In user-mode most of the generic timer registers are inaccessible
3333 * however modern kernels (4.12+) allow access to cntvct_el0
Peter Maydell55d284a2013-08-20 14:54:31 +01003334 */
Alex Bennée26c4a832018-06-29 15:11:16 +01003335
3336static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3337{
Andrew Jeffery7def8752019-12-20 14:02:59 +00003338 ARMCPU *cpu = env_archcpu(env);
3339
Alex Bennée26c4a832018-06-29 15:11:16 +01003340 /* Currently we have no support for QEMUTimer in linux-user so we
3341 * can't call gt_get_countervalue(env), instead we directly
3342 * call the lower level functions.
3343 */
Andrew Jeffery7def8752019-12-20 14:02:59 +00003344 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
Alex Bennée26c4a832018-06-29 15:11:16 +01003345}
3346
Peter Maydell55d284a2013-08-20 14:54:31 +01003347static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
Alex Bennée26c4a832018-06-29 15:11:16 +01003348 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3349 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3350 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3351 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3352 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3353 },
3354 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3355 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3356 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3357 .readfn = gt_virt_cnt_read,
3358 },
Peter Maydell55d284a2013-08-20 14:54:31 +01003359 REGINFO_SENTINEL
3360};
3361
3362#endif
3363
Peter Maydellc4241c72014-02-20 10:35:54 +00003364static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
Peter Maydell4a501602012-06-20 11:57:16 +00003365{
Peter Maydell891a2fe2012-07-12 10:59:09 +00003366 if (arm_feature(env, ARM_FEATURE_LPAE)) {
Fabian Aggeler8d5c7732014-06-09 15:43:22 +01003367 raw_write(env, ri, value);
Peter Maydell891a2fe2012-07-12 10:59:09 +00003368 } else if (arm_feature(env, ARM_FEATURE_V7)) {
Fabian Aggeler8d5c7732014-06-09 15:43:22 +01003369 raw_write(env, ri, value & 0xfffff6ff);
Peter Maydell4a501602012-06-20 11:57:16 +00003370 } else {
Fabian Aggeler8d5c7732014-06-09 15:43:22 +01003371 raw_write(env, ri, value & 0xfffff1ff);
Peter Maydell4a501602012-06-20 11:57:16 +00003372 }
Peter Maydell4a501602012-06-20 11:57:16 +00003373}
3374
3375#ifndef CONFIG_USER_ONLY
3376/* get_phys_addr() isn't present for user-mode-only targets */
Peter Maydell702a9352012-07-12 10:59:10 +00003377
Peter Maydell3f208fd2016-02-11 11:17:31 +00003378static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3379 bool isread)
Peter Maydell92611c02014-02-20 10:35:53 +00003380{
3381 if (ri->opc2 & 4) {
Peter Maydell87562e42015-08-25 15:45:08 +01003382 /* The ATS12NSO* operations must trap to EL3 if executed in
3383 * Secure EL1 (which can only happen if EL3 is AArch64).
3384 * They are simply UNDEF if executed from NS EL1.
3385 * They function normally from EL2 or EL3.
Peter Maydell92611c02014-02-20 10:35:53 +00003386 */
Peter Maydell87562e42015-08-25 15:45:08 +01003387 if (arm_current_el(env) == 1) {
3388 if (arm_is_secure_below_el3(env)) {
3389 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3390 }
3391 return CP_ACCESS_TRAP_UNCATEGORIZED;
3392 }
Peter Maydell92611c02014-02-20 10:35:53 +00003393 }
3394 return CP_ACCESS_OK;
3395}
3396
Philippe Mathieu-Daudé9fb005b2020-04-23 09:33:54 +02003397#ifdef CONFIG_TCG
Peter Maydell060e8a42015-02-05 13:37:24 +00003398static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
Peter Maydell03ae85f2017-09-04 15:21:51 +01003399 MMUAccessType access_type, ARMMMUIdx mmu_idx)
Peter Maydell4a501602012-06-20 11:57:16 +00003400{
Avi Kivitya8170e52012-10-23 12:30:10 +02003401 hwaddr phys_addr;
Peter Maydell4a501602012-06-20 11:57:16 +00003402 target_ulong page_size;
3403 int prot;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01003404 bool ret;
Fabian Aggeler01c097f2014-12-11 12:07:52 +00003405 uint64_t par64;
Edgar E. Iglesias1313e2d2017-12-13 17:59:25 +00003406 bool format64 = false;
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01003407 MemTxAttrs attrs = {};
Edgar E. Iglesiase14b5a22015-10-26 14:02:03 +01003408 ARMMMUFaultInfo fi = {};
Andrew Baumann5b2d2612017-11-07 13:03:51 +00003409 ARMCacheAttrs cacheattrs = {};
Peter Maydell4a501602012-06-20 11:57:16 +00003410
Andrew Baumann5b2d2612017-11-07 13:03:51 +00003411 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
Peter Maydellbc52bfe2017-12-13 17:59:25 +00003412 &prot, &page_size, &fi, &cacheattrs);
Edgar E. Iglesias1313e2d2017-12-13 17:59:25 +00003413
Peter Maydell0710b2f2019-08-16 13:58:02 +01003414 if (ret) {
3415 /*
3416 * Some kinds of translation fault must cause exceptions rather
3417 * than being reported in the PAR.
3418 */
3419 int current_el = arm_current_el(env);
3420 int target_el;
3421 uint32_t syn, fsr, fsc;
3422 bool take_exc = false;
3423
3424 if (fi.s1ptw && current_el == 1 && !arm_is_secure(env)
Richard Hendersonfee7aa42020-02-08 12:57:57 +00003425 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
Peter Maydell0710b2f2019-08-16 13:58:02 +01003426 /*
3427 * Synchronous stage 2 fault on an access made as part of the
3428 * translation table walk for AT S1E0* or AT S1E1* insn
3429 * executed from NS EL1. If this is a synchronous external abort
3430 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3431 * to EL3. Otherwise the fault is taken as an exception to EL2,
3432 * and HPFAR_EL2 holds the faulting IPA.
3433 */
3434 if (fi.type == ARMFault_SyncExternalOnWalk &&
3435 (env->cp15.scr_el3 & SCR_EA)) {
3436 target_el = 3;
3437 } else {
3438 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3439 target_el = 2;
3440 }
3441 take_exc = true;
3442 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3443 /*
3444 * Synchronous external aborts during a translation table walk
3445 * are taken as Data Abort exceptions.
3446 */
3447 if (fi.stage2) {
3448 if (current_el == 3) {
3449 target_el = 3;
3450 } else {
3451 target_el = 2;
3452 }
3453 } else {
3454 target_el = exception_target_el(env);
3455 }
3456 take_exc = true;
3457 }
3458
3459 if (take_exc) {
3460 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3461 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3462 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3463 fsr = arm_fi_to_lfsc(&fi);
3464 fsc = extract32(fsr, 0, 6);
3465 } else {
3466 fsr = arm_fi_to_sfsc(&fi);
3467 fsc = 0x3f;
3468 }
3469 /*
3470 * Report exception with ESR indicating a fault due to a
3471 * translation table walk for a cache maintenance instruction.
3472 */
Dongjiu Genge24fd072020-05-12 11:06:08 +08003473 syn = syn_data_abort_no_iss(current_el == target_el, 0,
Peter Maydell0710b2f2019-08-16 13:58:02 +01003474 fi.ea, 1, fi.s1ptw, 1, fsc);
3475 env->exception.vaddress = value;
3476 env->exception.fsr = fsr;
3477 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3478 }
3479 }
3480
Edgar E. Iglesias1313e2d2017-12-13 17:59:25 +00003481 if (is_a64(env)) {
3482 format64 = true;
3483 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3484 /*
3485 * ATS1Cxx:
3486 * * TTBCR.EAE determines whether the result is returned using the
3487 * 32-bit or the 64-bit PAR format
3488 * * Instructions executed in Hyp mode always use the 64bit format
3489 *
3490 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3491 * * The Non-secure TTBCR.EAE bit is set to 1
3492 * * The implementation includes EL2, and the value of HCR.VM is 1
3493 *
Peter Maydell9d1bab32018-10-24 07:50:17 +01003494 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3495 *
Peter Maydell23463e02018-11-06 11:32:14 +00003496 * ATS1Hx always uses the 64bit format.
Edgar E. Iglesias1313e2d2017-12-13 17:59:25 +00003497 */
3498 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3499
3500 if (arm_feature(env, ARM_FEATURE_EL2)) {
Richard Henderson452ef8c2020-02-08 12:57:58 +00003501 if (mmu_idx == ARMMMUIdx_E10_0 ||
3502 mmu_idx == ARMMMUIdx_E10_1 ||
3503 mmu_idx == ARMMMUIdx_E10_1_PAN) {
Peter Maydell9d1bab32018-10-24 07:50:17 +01003504 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
Edgar E. Iglesias1313e2d2017-12-13 17:59:25 +00003505 } else {
3506 format64 |= arm_current_el(env) == 2;
3507 }
3508 }
3509 }
3510
3511 if (format64) {
Peter Maydell5efe9ed2017-12-13 17:59:25 +00003512 /* Create a 64-bit PAR */
Fabian Aggeler01c097f2014-12-11 12:07:52 +00003513 par64 = (1 << 11); /* LPAE bit always set */
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01003514 if (!ret) {
Peter Maydell702a9352012-07-12 10:59:10 +00003515 par64 |= phys_addr & ~0xfffULL;
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01003516 if (!attrs.secure) {
3517 par64 |= (1 << 9); /* NS */
3518 }
Andrew Baumann5b2d2612017-11-07 13:03:51 +00003519 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3520 par64 |= cacheattrs.shareability << 7; /* SH */
Peter Maydell4a501602012-06-20 11:57:16 +00003521 } else {
Peter Maydell5efe9ed2017-12-13 17:59:25 +00003522 uint32_t fsr = arm_fi_to_lfsc(&fi);
3523
Peter Maydell702a9352012-07-12 10:59:10 +00003524 par64 |= 1; /* F */
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01003525 par64 |= (fsr & 0x3f) << 1; /* FS */
Peter Maydell0f7b7912018-11-06 11:32:14 +00003526 if (fi.stage2) {
3527 par64 |= (1 << 9); /* S */
3528 }
3529 if (fi.s1ptw) {
3530 par64 |= (1 << 8); /* PTW */
3531 }
Peter Maydell4a501602012-06-20 11:57:16 +00003532 }
3533 } else {
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01003534 /* fsr is a DFSR/IFSR value for the short descriptor
Peter Maydell702a9352012-07-12 10:59:10 +00003535 * translation table format (with WnR always clear).
3536 * Convert it to a 32-bit PAR.
3537 */
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01003538 if (!ret) {
Peter Maydell702a9352012-07-12 10:59:10 +00003539 /* We do not set any attribute bits in the PAR */
3540 if (page_size == (1 << 24)
3541 && arm_feature(env, ARM_FEATURE_V7)) {
Fabian Aggeler01c097f2014-12-11 12:07:52 +00003542 par64 = (phys_addr & 0xff000000) | (1 << 1);
Peter Maydell702a9352012-07-12 10:59:10 +00003543 } else {
Fabian Aggeler01c097f2014-12-11 12:07:52 +00003544 par64 = phys_addr & 0xfffff000;
Peter Maydell702a9352012-07-12 10:59:10 +00003545 }
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01003546 if (!attrs.secure) {
3547 par64 |= (1 << 9); /* NS */
3548 }
Peter Maydell702a9352012-07-12 10:59:10 +00003549 } else {
Peter Maydell5efe9ed2017-12-13 17:59:25 +00003550 uint32_t fsr = arm_fi_to_sfsc(&fi);
3551
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01003552 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3553 ((fsr & 0xf) << 1) | 1;
Peter Maydell702a9352012-07-12 10:59:10 +00003554 }
Peter Maydell4a501602012-06-20 11:57:16 +00003555 }
Peter Maydell060e8a42015-02-05 13:37:24 +00003556 return par64;
3557}
Philippe Mathieu-Daudé9fb005b2020-04-23 09:33:54 +02003558#endif /* CONFIG_TCG */
Peter Maydell060e8a42015-02-05 13:37:24 +00003559
3560static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3561{
Philippe Mathieu-Daudé9fb005b2020-04-23 09:33:54 +02003562#ifdef CONFIG_TCG
Peter Maydell03ae85f2017-09-04 15:21:51 +01003563 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
Peter Maydell060e8a42015-02-05 13:37:24 +00003564 uint64_t par64;
Peter Maydelld3649702015-02-05 13:37:24 +00003565 ARMMMUIdx mmu_idx;
3566 int el = arm_current_el(env);
3567 bool secure = arm_is_secure_below_el3(env);
Peter Maydell060e8a42015-02-05 13:37:24 +00003568
Peter Maydelld3649702015-02-05 13:37:24 +00003569 switch (ri->opc2 & 6) {
3570 case 0:
Richard Henderson04b07d22020-02-08 12:58:11 +00003571 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
Peter Maydelld3649702015-02-05 13:37:24 +00003572 switch (el) {
3573 case 3:
Richard Henderson127b2b02020-02-07 14:04:23 +00003574 mmu_idx = ARMMMUIdx_SE3;
Peter Maydelld3649702015-02-05 13:37:24 +00003575 break;
3576 case 2:
Richard Henderson04b07d22020-02-08 12:58:11 +00003577 g_assert(!secure); /* TODO: ARMv8.4-SecEL2 */
3578 /* fall through */
Peter Maydelld3649702015-02-05 13:37:24 +00003579 case 1:
Richard Henderson04b07d22020-02-08 12:58:11 +00003580 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
3581 mmu_idx = (secure ? ARMMMUIdx_SE10_1_PAN
3582 : ARMMMUIdx_Stage1_E1_PAN);
3583 } else {
3584 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1;
3585 }
Peter Maydelld3649702015-02-05 13:37:24 +00003586 break;
3587 default:
3588 g_assert_not_reached();
3589 }
3590 break;
3591 case 2:
3592 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3593 switch (el) {
3594 case 3:
Richard Hendersonfba37ae2020-02-07 14:04:23 +00003595 mmu_idx = ARMMMUIdx_SE10_0;
Peter Maydelld3649702015-02-05 13:37:24 +00003596 break;
3597 case 2:
Richard Henderson2859d7b2020-02-07 14:04:22 +00003598 mmu_idx = ARMMMUIdx_Stage1_E0;
Peter Maydelld3649702015-02-05 13:37:24 +00003599 break;
3600 case 1:
Richard Hendersonfba37ae2020-02-07 14:04:23 +00003601 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0;
Peter Maydelld3649702015-02-05 13:37:24 +00003602 break;
3603 default:
3604 g_assert_not_reached();
3605 }
3606 break;
3607 case 4:
3608 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
Richard Henderson01b98b62020-02-07 14:04:22 +00003609 mmu_idx = ARMMMUIdx_E10_1;
Peter Maydelld3649702015-02-05 13:37:24 +00003610 break;
3611 case 6:
3612 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
Richard Henderson01b98b62020-02-07 14:04:22 +00003613 mmu_idx = ARMMMUIdx_E10_0;
Peter Maydelld3649702015-02-05 13:37:24 +00003614 break;
3615 default:
3616 g_assert_not_reached();
3617 }
3618
3619 par64 = do_ats_write(env, value, access_type, mmu_idx);
Fabian Aggeler01c097f2014-12-11 12:07:52 +00003620
3621 A32_BANKED_CURRENT_REG_SET(env, par, par64);
Philippe Mathieu-Daudé9fb005b2020-04-23 09:33:54 +02003622#else
3623 /* Handled by hardware accelerator. */
3624 g_assert_not_reached();
3625#endif /* CONFIG_TCG */
Peter Maydell4a501602012-06-20 11:57:16 +00003626}
Peter Maydell060e8a42015-02-05 13:37:24 +00003627
Peter Maydell14db7fe2015-08-25 15:45:08 +01003628static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3629 uint64_t value)
3630{
Philippe Mathieu-Daudé9fb005b2020-04-23 09:33:54 +02003631#ifdef CONFIG_TCG
Peter Maydell03ae85f2017-09-04 15:21:51 +01003632 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
Peter Maydell14db7fe2015-08-25 15:45:08 +01003633 uint64_t par64;
3634
Richard Hendersone013b742020-02-07 14:04:23 +00003635 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
Peter Maydell14db7fe2015-08-25 15:45:08 +01003636
3637 A32_BANKED_CURRENT_REG_SET(env, par, par64);
Philippe Mathieu-Daudé9fb005b2020-04-23 09:33:54 +02003638#else
3639 /* Handled by hardware accelerator. */
3640 g_assert_not_reached();
3641#endif /* CONFIG_TCG */
Peter Maydell14db7fe2015-08-25 15:45:08 +01003642}
3643
Peter Maydell3f208fd2016-02-11 11:17:31 +00003644static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3645 bool isread)
Peter Maydell2a47df92015-08-25 15:45:08 +01003646{
3647 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
3648 return CP_ACCESS_TRAP;
3649 }
3650 return CP_ACCESS_OK;
3651}
3652
Peter Maydell060e8a42015-02-05 13:37:24 +00003653static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3654 uint64_t value)
3655{
Philippe Mathieu-Daudé9fb005b2020-04-23 09:33:54 +02003656#ifdef CONFIG_TCG
Peter Maydell03ae85f2017-09-04 15:21:51 +01003657 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
Peter Maydelld3649702015-02-05 13:37:24 +00003658 ARMMMUIdx mmu_idx;
3659 int secure = arm_is_secure_below_el3(env);
Peter Maydell060e8a42015-02-05 13:37:24 +00003660
Peter Maydelld3649702015-02-05 13:37:24 +00003661 switch (ri->opc2 & 6) {
3662 case 0:
3663 switch (ri->opc1) {
Richard Henderson04b07d22020-02-08 12:58:11 +00003664 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3665 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
3666 mmu_idx = (secure ? ARMMMUIdx_SE10_1_PAN
3667 : ARMMMUIdx_Stage1_E1_PAN);
3668 } else {
3669 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1;
3670 }
Peter Maydelld3649702015-02-05 13:37:24 +00003671 break;
3672 case 4: /* AT S1E2R, AT S1E2W */
Richard Hendersone013b742020-02-07 14:04:23 +00003673 mmu_idx = ARMMMUIdx_E2;
Peter Maydelld3649702015-02-05 13:37:24 +00003674 break;
3675 case 6: /* AT S1E3R, AT S1E3W */
Richard Henderson127b2b02020-02-07 14:04:23 +00003676 mmu_idx = ARMMMUIdx_SE3;
Peter Maydelld3649702015-02-05 13:37:24 +00003677 break;
3678 default:
3679 g_assert_not_reached();
3680 }
3681 break;
3682 case 2: /* AT S1E0R, AT S1E0W */
Richard Hendersonfba37ae2020-02-07 14:04:23 +00003683 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0;
Peter Maydelld3649702015-02-05 13:37:24 +00003684 break;
3685 case 4: /* AT S12E1R, AT S12E1W */
Richard Hendersonfba37ae2020-02-07 14:04:23 +00003686 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
Peter Maydelld3649702015-02-05 13:37:24 +00003687 break;
3688 case 6: /* AT S12E0R, AT S12E0W */
Richard Hendersonfba37ae2020-02-07 14:04:23 +00003689 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
Peter Maydelld3649702015-02-05 13:37:24 +00003690 break;
3691 default:
3692 g_assert_not_reached();
3693 }
3694
3695 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
Philippe Mathieu-Daudé9fb005b2020-04-23 09:33:54 +02003696#else
3697 /* Handled by hardware accelerator. */
3698 g_assert_not_reached();
3699#endif /* CONFIG_TCG */
Peter Maydell060e8a42015-02-05 13:37:24 +00003700}
Peter Maydell4a501602012-06-20 11:57:16 +00003701#endif
3702
3703static const ARMCPRegInfo vapa_cp_reginfo[] = {
3704 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3705 .access = PL1_RW, .resetvalue = 0,
Fabian Aggeler01c097f2014-12-11 12:07:52 +00003706 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3707 offsetoflow32(CPUARMState, cp15.par_ns) },
Peter Maydell4a501602012-06-20 11:57:16 +00003708 .writefn = par_write },
3709#ifndef CONFIG_USER_ONLY
Peter Maydell87562e42015-08-25 15:45:08 +01003710 /* This underdecoding is safe because the reginfo is NO_RAW. */
Peter Maydell4a501602012-06-20 11:57:16 +00003711 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
Peter Maydell92611c02014-02-20 10:35:53 +00003712 .access = PL1_W, .accessfn = ats_access,
Peter Maydell0710b2f2019-08-16 13:58:02 +01003713 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
Peter Maydell4a501602012-06-20 11:57:16 +00003714#endif
3715 REGINFO_SENTINEL
3716};
3717
Peter Maydell18032be2012-06-20 11:57:13 +00003718/* Return basic MPU access permission bits. */
3719static uint32_t simple_mpu_ap_bits(uint32_t val)
3720{
3721 uint32_t ret;
3722 uint32_t mask;
3723 int i;
3724 ret = 0;
3725 mask = 3;
3726 for (i = 0; i < 16; i += 2) {
3727 ret |= (val >> i) & mask;
3728 mask <<= 2;
3729 }
3730 return ret;
3731}
3732
3733/* Pad basic MPU access permission bits to extended format. */
3734static uint32_t extended_mpu_ap_bits(uint32_t val)
3735{
3736 uint32_t ret;
3737 uint32_t mask;
3738 int i;
3739 ret = 0;
3740 mask = 3;
3741 for (i = 0; i < 16; i += 2) {
3742 ret |= (val & mask) << i;
3743 mask <<= 2;
3744 }
3745 return ret;
3746}
3747
Peter Maydellc4241c72014-02-20 10:35:54 +00003748static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3749 uint64_t value)
Peter Maydell18032be2012-06-20 11:57:13 +00003750{
Peter Maydell7e097972014-04-15 19:18:41 +01003751 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
Peter Maydell18032be2012-06-20 11:57:13 +00003752}
3753
Peter Maydellc4241c72014-02-20 10:35:54 +00003754static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydell18032be2012-06-20 11:57:13 +00003755{
Peter Maydell7e097972014-04-15 19:18:41 +01003756 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
Peter Maydell18032be2012-06-20 11:57:13 +00003757}
3758
Peter Maydellc4241c72014-02-20 10:35:54 +00003759static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3760 uint64_t value)
Peter Maydell18032be2012-06-20 11:57:13 +00003761{
Peter Maydell7e097972014-04-15 19:18:41 +01003762 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
Peter Maydell18032be2012-06-20 11:57:13 +00003763}
3764
Peter Maydellc4241c72014-02-20 10:35:54 +00003765static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydell18032be2012-06-20 11:57:13 +00003766{
Peter Maydell7e097972014-04-15 19:18:41 +01003767 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
Peter Maydell18032be2012-06-20 11:57:13 +00003768}
3769
Peter Crosthwaite6cb0b012015-06-19 14:17:44 +01003770static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3771{
3772 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3773
3774 if (!u32p) {
3775 return 0;
3776 }
3777
Peter Maydell1bc04a82017-09-07 13:54:53 +01003778 u32p += env->pmsav7.rnr[M_REG_NS];
Peter Crosthwaite6cb0b012015-06-19 14:17:44 +01003779 return *u32p;
3780}
3781
3782static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3783 uint64_t value)
3784{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07003785 ARMCPU *cpu = env_archcpu(env);
Peter Crosthwaite6cb0b012015-06-19 14:17:44 +01003786 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3787
3788 if (!u32p) {
3789 return;
3790 }
3791
Peter Maydell1bc04a82017-09-07 13:54:53 +01003792 u32p += env->pmsav7.rnr[M_REG_NS];
Alex Bennéed10eb082016-11-14 14:17:28 +00003793 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
Peter Crosthwaite6cb0b012015-06-19 14:17:44 +01003794 *u32p = value;
3795}
3796
Peter Crosthwaite6cb0b012015-06-19 14:17:44 +01003797static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3798 uint64_t value)
3799{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07003800 ARMCPU *cpu = env_archcpu(env);
Peter Crosthwaite6cb0b012015-06-19 14:17:44 +01003801 uint32_t nrgs = cpu->pmsav7_dregion;
3802
3803 if (value >= nrgs) {
3804 qemu_log_mask(LOG_GUEST_ERROR,
3805 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3806 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3807 return;
3808 }
3809
3810 raw_write(env, ri, value);
3811}
3812
3813static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
Peter Maydell69ceea62017-07-27 11:59:09 +01003814 /* Reset for all these registers is handled in arm_cpu_reset(),
3815 * because the PMSAv7 is also used by M-profile CPUs, which do
3816 * not register cpregs but still need the state to be reset.
3817 */
Peter Crosthwaite6cb0b012015-06-19 14:17:44 +01003818 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3819 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3820 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
Peter Maydell69ceea62017-07-27 11:59:09 +01003821 .readfn = pmsav7_read, .writefn = pmsav7_write,
3822 .resetfn = arm_cp_reset_ignore },
Peter Crosthwaite6cb0b012015-06-19 14:17:44 +01003823 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3824 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3825 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
Peter Maydell69ceea62017-07-27 11:59:09 +01003826 .readfn = pmsav7_read, .writefn = pmsav7_write,
3827 .resetfn = arm_cp_reset_ignore },
Peter Crosthwaite6cb0b012015-06-19 14:17:44 +01003828 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3829 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3830 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
Peter Maydell69ceea62017-07-27 11:59:09 +01003831 .readfn = pmsav7_read, .writefn = pmsav7_write,
3832 .resetfn = arm_cp_reset_ignore },
Peter Crosthwaite6cb0b012015-06-19 14:17:44 +01003833 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3834 .access = PL1_RW,
Peter Maydell1bc04a82017-09-07 13:54:53 +01003835 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
Peter Maydell69ceea62017-07-27 11:59:09 +01003836 .writefn = pmsav7_rgnr_write,
3837 .resetfn = arm_cp_reset_ignore },
Peter Crosthwaite6cb0b012015-06-19 14:17:44 +01003838 REGINFO_SENTINEL
3839};
3840
Peter Maydell18032be2012-06-20 11:57:13 +00003841static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3842 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003843 .access = PL1_RW, .type = ARM_CP_ALIAS,
Peter Maydell7e097972014-04-15 19:18:41 +01003844 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
Peter Maydell18032be2012-06-20 11:57:13 +00003845 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3846 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003847 .access = PL1_RW, .type = ARM_CP_ALIAS,
Peter Maydell7e097972014-04-15 19:18:41 +01003848 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
Peter Maydell18032be2012-06-20 11:57:13 +00003849 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3850 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3851 .access = PL1_RW,
Peter Maydell7e097972014-04-15 19:18:41 +01003852 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3853 .resetvalue = 0, },
Peter Maydell18032be2012-06-20 11:57:13 +00003854 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3855 .access = PL1_RW,
Peter Maydell7e097972014-04-15 19:18:41 +01003856 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3857 .resetvalue = 0, },
Peter Maydellecce5c32012-06-20 11:57:14 +00003858 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3859 .access = PL1_RW,
3860 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3861 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3862 .access = PL1_RW,
3863 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
Peter Maydell06d76f32012-06-20 11:57:17 +00003864 /* Protection region base and size registers */
Peter Maydelle508a922014-02-20 10:35:52 +00003865 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3866 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3867 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3868 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3869 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3870 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3871 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3872 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3873 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3874 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3875 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3876 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3877 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3878 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3879 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3880 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3881 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3882 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3883 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3884 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3885 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3886 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3887 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3888 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
Peter Maydell18032be2012-06-20 11:57:13 +00003889 REGINFO_SENTINEL
3890};
3891
Peter Maydellc4241c72014-02-20 10:35:54 +00003892static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3893 uint64_t value)
Peter Maydellecce5c32012-06-20 11:57:14 +00003894{
Fabian Aggeler11f136e2014-12-11 12:07:51 +00003895 TCR *tcr = raw_ptr(env, ri);
Peter Maydell2ebcebe2013-06-27 16:38:47 +01003896 int maskshift = extract32(value, 0, 3);
3897
Fabian Aggelere389be12014-06-19 18:06:24 +01003898 if (!arm_feature(env, ARM_FEATURE_V8)) {
3899 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3900 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3901 * using Long-desciptor translation table format */
3902 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3903 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3904 /* In an implementation that includes the Security Extensions
3905 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3906 * Short-descriptor translation table format.
3907 */
3908 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3909 } else {
3910 value &= TTBCR_N;
3911 }
Peter Maydelle42c4db2012-07-12 10:59:11 +00003912 }
Fabian Aggelere389be12014-06-19 18:06:24 +01003913
Daniel P. Berrangeb6af0972015-08-26 12:17:13 +01003914 /* Update the masks corresponding to the TCR bank being written
Fabian Aggeler11f136e2014-12-11 12:07:51 +00003915 * Note that we always calculate mask and base_mask, but
Peter Maydelle42c4db2012-07-12 10:59:11 +00003916 * they are only used for short-descriptor tables (ie if EAE is 0);
Fabian Aggeler11f136e2014-12-11 12:07:51 +00003917 * for long-descriptor tables the TCR fields are used differently
3918 * and the mask and base_mask values are meaningless.
Peter Maydelle42c4db2012-07-12 10:59:11 +00003919 */
Fabian Aggeler11f136e2014-12-11 12:07:51 +00003920 tcr->raw_tcr = value;
3921 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3922 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
Peter Maydellecce5c32012-06-20 11:57:14 +00003923}
3924
Peter Maydellc4241c72014-02-20 10:35:54 +00003925static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3926 uint64_t value)
Peter Maydelld4e6df62013-06-25 18:16:07 +01003927{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07003928 ARMCPU *cpu = env_archcpu(env);
Richard Hendersonab638a32018-12-13 13:48:07 +00003929 TCR *tcr = raw_ptr(env, ri);
Andreas Färber00c8cb02013-09-04 02:19:44 +02003930
Peter Maydelld4e6df62013-06-25 18:16:07 +01003931 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3932 /* With LPAE the TTBCR could result in a change of ASID
3933 * via the TTBCR.A1 bit, so do a TLB flush.
3934 */
Alex Bennéed10eb082016-11-14 14:17:28 +00003935 tlb_flush(CPU(cpu));
Peter Maydelld4e6df62013-06-25 18:16:07 +01003936 }
Richard Hendersonab638a32018-12-13 13:48:07 +00003937 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3938 value = deposit64(tcr->raw_tcr, 0, 32, value);
Peter Maydellc4241c72014-02-20 10:35:54 +00003939 vmsa_ttbcr_raw_write(env, ri, value);
Peter Maydelld4e6df62013-06-25 18:16:07 +01003940}
3941
Peter Maydellecce5c32012-06-20 11:57:14 +00003942static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3943{
Fabian Aggeler11f136e2014-12-11 12:07:51 +00003944 TCR *tcr = raw_ptr(env, ri);
3945
3946 /* Reset both the TCR as well as the masks corresponding to the bank of
3947 * the TCR being reset.
3948 */
3949 tcr->raw_tcr = 0;
3950 tcr->mask = 0;
3951 tcr->base_mask = 0xffffc000u;
Peter Maydellecce5c32012-06-20 11:57:14 +00003952}
3953
Richard Hendersond06dc932020-02-07 14:04:26 +00003954static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
Peter Maydellcb2e37d2014-02-26 17:20:04 +00003955 uint64_t value)
3956{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07003957 ARMCPU *cpu = env_archcpu(env);
Fabian Aggeler11f136e2014-12-11 12:07:51 +00003958 TCR *tcr = raw_ptr(env, ri);
Andreas Färber00c8cb02013-09-04 02:19:44 +02003959
Peter Maydellcb2e37d2014-02-26 17:20:04 +00003960 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
Alex Bennéed10eb082016-11-14 14:17:28 +00003961 tlb_flush(CPU(cpu));
Fabian Aggeler11f136e2014-12-11 12:07:51 +00003962 tcr->raw_tcr = value;
Peter Maydellcb2e37d2014-02-26 17:20:04 +00003963}
3964
Peter Maydell327ed102014-02-26 17:20:04 +00003965static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3966 uint64_t value)
3967{
Richard Henderson93f379b2018-10-24 07:50:20 +01003968 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3969 if (cpreg_field_is_64bit(ri) &&
3970 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
Richard Henderson2fc0cc02019-03-22 17:41:14 -07003971 ARMCPU *cpu = env_archcpu(env);
Alex Bennéed10eb082016-11-14 14:17:28 +00003972 tlb_flush(CPU(cpu));
Peter Maydell327ed102014-02-26 17:20:04 +00003973 }
3974 raw_write(env, ri, value);
3975}
3976
Richard Hendersoned30da82020-02-07 14:04:21 +00003977static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3978 uint64_t value)
3979{
Richard Hendersond06dc932020-02-07 14:04:26 +00003980 /*
3981 * If we are running with E2&0 regime, then an ASID is active.
3982 * Flush if that might be changing. Note we're not checking
3983 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3984 * holds the active ASID, only checking the field that might.
3985 */
3986 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3987 (arm_hcr_el2_eff(env) & HCR_E2H)) {
3988 tlb_flush_by_mmuidx(env_cpu(env),
Richard Henderson452ef8c2020-02-08 12:57:58 +00003989 ARMMMUIdxBit_E20_2 |
3990 ARMMMUIdxBit_E20_2_PAN |
3991 ARMMMUIdxBit_E20_0);
Richard Hendersond06dc932020-02-07 14:04:26 +00003992 }
Richard Hendersoned30da82020-02-07 14:04:21 +00003993 raw_write(env, ri, value);
3994}
3995
Edgar E. Iglesiasb698e9c2015-09-14 14:39:50 +01003996static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3997 uint64_t value)
3998{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07003999 ARMCPU *cpu = env_archcpu(env);
Edgar E. Iglesiasb698e9c2015-09-14 14:39:50 +01004000 CPUState *cs = CPU(cpu);
4001
Richard Henderson97fa9352020-02-07 14:04:22 +00004002 /*
4003 * A change in VMID to the stage2 page table (Stage2) invalidates
4004 * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
4005 */
Edgar E. Iglesiasb698e9c2015-09-14 14:39:50 +01004006 if (raw_read(env, ri) != value) {
Alex Bennée0336cbf2017-02-23 18:29:19 +00004007 tlb_flush_by_mmuidx(cs,
Richard Henderson01b98b62020-02-07 14:04:22 +00004008 ARMMMUIdxBit_E10_1 |
Richard Henderson452ef8c2020-02-08 12:57:58 +00004009 ARMMMUIdxBit_E10_1_PAN |
Peter Maydellbf053402020-03-30 22:03:57 +01004010 ARMMMUIdxBit_E10_0);
Edgar E. Iglesiasb698e9c2015-09-14 14:39:50 +01004011 raw_write(env, ri, value);
4012 }
4013}
4014
Peter Crosthwaite8e5d75c2015-06-15 18:06:10 +01004015static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
Peter Maydell18032be2012-06-20 11:57:13 +00004016 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
Richard Henderson84929212020-03-05 16:09:17 +00004017 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
Fabian Aggeler4a7e2d72014-12-11 12:07:51 +00004018 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
Sergey Fedorovb061a822015-06-19 14:17:44 +01004019 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
Peter Maydell18032be2012-06-20 11:57:13 +00004020 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
Richard Henderson84929212020-03-05 16:09:17 +00004021 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
Fabian Aggeler88ca1c22014-12-11 12:07:51 +00004022 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4023 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
Peter Crosthwaite8e5d75c2015-06-15 18:06:10 +01004024 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
Richard Henderson84929212020-03-05 16:09:17 +00004025 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
Peter Crosthwaite8e5d75c2015-06-15 18:06:10 +01004026 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4027 offsetof(CPUARMState, cp15.dfar_ns) } },
4028 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4029 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
Richard Henderson84929212020-03-05 16:09:17 +00004030 .access = PL1_RW, .accessfn = access_tvm_trvm,
4031 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
Peter Crosthwaite8e5d75c2015-06-15 18:06:10 +01004032 .resetvalue = 0, },
4033 REGINFO_SENTINEL
4034};
4035
4036static const ARMCPRegInfo vmsa_cp_reginfo[] = {
Rob Herring6cd8a262014-04-15 19:18:42 +01004037 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4038 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
Richard Henderson84929212020-03-05 16:09:17 +00004039 .access = PL1_RW, .accessfn = access_tvm_trvm,
Edgar E. Iglesiasd81c5192014-05-27 17:09:51 +01004040 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
Peter Maydell327ed102014-02-26 17:20:04 +00004041 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
Fabian Aggeler7dd8c9a2014-12-11 12:07:51 +00004042 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
Richard Henderson84929212020-03-05 16:09:17 +00004043 .access = PL1_RW, .accessfn = access_tvm_trvm,
4044 .writefn = vmsa_ttbr_write, .resetvalue = 0,
Fabian Aggeler7dd8c9a2014-12-11 12:07:51 +00004045 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4046 offsetof(CPUARMState, cp15.ttbr0_ns) } },
Peter Maydell327ed102014-02-26 17:20:04 +00004047 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
Fabian Aggeler7dd8c9a2014-12-11 12:07:51 +00004048 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
Richard Henderson84929212020-03-05 16:09:17 +00004049 .access = PL1_RW, .accessfn = access_tvm_trvm,
4050 .writefn = vmsa_ttbr_write, .resetvalue = 0,
Fabian Aggeler7dd8c9a2014-12-11 12:07:51 +00004051 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4052 offsetof(CPUARMState, cp15.ttbr1_ns) } },
Peter Maydellcb2e37d2014-02-26 17:20:04 +00004053 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4054 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
Richard Henderson84929212020-03-05 16:09:17 +00004055 .access = PL1_RW, .accessfn = access_tvm_trvm,
4056 .writefn = vmsa_tcr_el12_write,
Peter Maydellcb2e37d2014-02-26 17:20:04 +00004057 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
Fabian Aggeler11f136e2014-12-11 12:07:51 +00004058 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
Peter Maydellcb2e37d2014-02-26 17:20:04 +00004059 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
Richard Henderson84929212020-03-05 16:09:17 +00004060 .access = PL1_RW, .accessfn = access_tvm_trvm,
4061 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
Sergey Fedorovb061a822015-06-19 14:17:44 +01004062 .raw_writefn = vmsa_ttbcr_raw_write,
Fabian Aggeler11f136e2014-12-11 12:07:51 +00004063 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4064 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
Peter Maydell18032be2012-06-20 11:57:13 +00004065 REGINFO_SENTINEL
4066};
4067
Richard Hendersonab638a32018-12-13 13:48:07 +00004068/* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4069 * qemu tlbs nor adjusting cached masks.
4070 */
4071static const ARMCPRegInfo ttbcr2_reginfo = {
4072 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
Richard Henderson84929212020-03-05 16:09:17 +00004073 .access = PL1_RW, .accessfn = access_tvm_trvm,
4074 .type = ARM_CP_ALIAS,
Richard Hendersonab638a32018-12-13 13:48:07 +00004075 .bank_fieldoffsets = { offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4076 offsetofhigh32(CPUARMState, cp15.tcr_el[1]) },
4077};
4078
Peter Maydellc4241c72014-02-20 10:35:54 +00004079static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4080 uint64_t value)
Peter Maydell1047b9d2012-06-20 11:57:15 +00004081{
4082 env->cp15.c15_ticonfig = value & 0xe7;
4083 /* The OS_TYPE bit in this register changes the reported CPUID! */
4084 env->cp15.c0_cpuid = (value & (1 << 5)) ?
4085 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
Peter Maydell1047b9d2012-06-20 11:57:15 +00004086}
4087
Peter Maydellc4241c72014-02-20 10:35:54 +00004088static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4089 uint64_t value)
Peter Maydell1047b9d2012-06-20 11:57:15 +00004090{
4091 env->cp15.c15_threadid = value & 0xffff;
Peter Maydell1047b9d2012-06-20 11:57:15 +00004092}
4093
Peter Maydellc4241c72014-02-20 10:35:54 +00004094static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4095 uint64_t value)
Peter Maydell1047b9d2012-06-20 11:57:15 +00004096{
4097 /* Wait-for-interrupt (deprecated) */
Richard Henderson2fc0cc02019-03-22 17:41:14 -07004098 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
Peter Maydell1047b9d2012-06-20 11:57:15 +00004099}
4100
Peter Maydellc4241c72014-02-20 10:35:54 +00004101static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4102 uint64_t value)
Peter Maydellc4804212012-06-20 11:57:17 +00004103{
4104 /* On OMAP there are registers indicating the max/min index of dcache lines
4105 * containing a dirty line; cache flush operations have to reset these.
4106 */
4107 env->cp15.c15_i_max = 0x000;
4108 env->cp15.c15_i_min = 0xff0;
Peter Maydellc4804212012-06-20 11:57:17 +00004109}
4110
Peter Maydell18032be2012-06-20 11:57:13 +00004111static const ARMCPRegInfo omap_cp_reginfo[] = {
4112 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4113 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
Edgar E. Iglesiasd81c5192014-05-27 17:09:51 +01004114 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
Rob Herring6cd8a262014-04-15 19:18:42 +01004115 .resetvalue = 0, },
Peter Maydell1047b9d2012-06-20 11:57:15 +00004116 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4117 .access = PL1_RW, .type = ARM_CP_NOP },
4118 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4119 .access = PL1_RW,
4120 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4121 .writefn = omap_ticonfig_write },
4122 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4123 .access = PL1_RW,
4124 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4125 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4126 .access = PL1_RW, .resetvalue = 0xff0,
4127 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4128 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4129 .access = PL1_RW,
4130 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4131 .writefn = omap_threadid_write },
4132 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4133 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00004134 .type = ARM_CP_NO_RAW,
Peter Maydell1047b9d2012-06-20 11:57:15 +00004135 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4136 /* TODO: Peripheral port remap register:
4137 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4138 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4139 * when MMU is off.
4140 */
Peter Maydellc4804212012-06-20 11:57:17 +00004141 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +01004142 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00004143 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
Peter Maydellc4804212012-06-20 11:57:17 +00004144 .writefn = omap_cachemaint_write },
Peter Maydell34f90522012-06-20 11:57:18 +00004145 { .name = "C9", .cp = 15, .crn = 9,
4146 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4147 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
Peter Maydell1047b9d2012-06-20 11:57:15 +00004148 REGINFO_SENTINEL
4149};
4150
Peter Maydellc4241c72014-02-20 10:35:54 +00004151static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4152 uint64_t value)
Peter Maydell1047b9d2012-06-20 11:57:15 +00004153{
Peter Maydellc0f4af12014-09-29 18:48:48 +01004154 env->cp15.c15_cpar = value & 0x3fff;
Peter Maydell1047b9d2012-06-20 11:57:15 +00004155}
4156
4157static const ARMCPRegInfo xscale_cp_reginfo[] = {
4158 { .name = "XSCALE_CPAR",
4159 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4160 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4161 .writefn = xscale_cpar_write, },
Peter Maydell2771db22012-06-20 11:57:18 +00004162 { .name = "XSCALE_AUXCR",
4163 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4164 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4165 .resetvalue = 0, },
Peter Maydell3b771572014-05-01 15:24:44 +01004166 /* XScale specific cache-lockdown: since we have no cache we NOP these
4167 * and hope the guest does not really rely on cache behaviour.
4168 */
4169 { .name = "XSCALE_LOCK_ICACHE_LINE",
4170 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4171 .access = PL1_W, .type = ARM_CP_NOP },
4172 { .name = "XSCALE_UNLOCK_ICACHE",
4173 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4174 .access = PL1_W, .type = ARM_CP_NOP },
4175 { .name = "XSCALE_DCACHE_LOCK",
4176 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4177 .access = PL1_RW, .type = ARM_CP_NOP },
4178 { .name = "XSCALE_UNLOCK_DCACHE",
4179 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4180 .access = PL1_W, .type = ARM_CP_NOP },
Peter Maydell1047b9d2012-06-20 11:57:15 +00004181 REGINFO_SENTINEL
4182};
4183
4184static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4185 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
4186 * implementation of this implementation-defined space.
4187 * Ideally this should eventually disappear in favour of actually
4188 * implementing the correct behaviour for all cores.
4189 */
4190 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4191 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
Peter Crosthwaite3671cd82013-12-17 19:42:27 +00004192 .access = PL1_RW,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00004193 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
Peter Maydelld4e6df62013-06-25 18:16:07 +01004194 .resetvalue = 0 },
Peter Maydell18032be2012-06-20 11:57:13 +00004195 REGINFO_SENTINEL
4196};
4197
Peter Maydellc4804212012-06-20 11:57:17 +00004198static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4199 /* Cache status: RAZ because we have no cache so it's always clean */
4200 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00004201 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
Peter Maydelld4e6df62013-06-25 18:16:07 +01004202 .resetvalue = 0 },
Peter Maydellc4804212012-06-20 11:57:17 +00004203 REGINFO_SENTINEL
4204};
4205
4206static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4207 /* We never have a a block transfer operation in progress */
4208 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00004209 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
Peter Maydelld4e6df62013-06-25 18:16:07 +01004210 .resetvalue = 0 },
Peter Maydell30b05bb2012-06-20 11:57:22 +00004211 /* The cache ops themselves: these all NOP for QEMU */
4212 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4213 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4214 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4215 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4216 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4217 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4218 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4219 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4220 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4221 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4222 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4223 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
Peter Maydellc4804212012-06-20 11:57:17 +00004224 REGINFO_SENTINEL
4225};
4226
4227static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4228 /* The cache test-and-clean instructions always return (1 << 30)
4229 * to indicate that there are no dirty cache lines.
4230 */
4231 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00004232 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
Peter Maydelld4e6df62013-06-25 18:16:07 +01004233 .resetvalue = (1 << 30) },
Peter Maydellc4804212012-06-20 11:57:17 +00004234 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00004235 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
Peter Maydelld4e6df62013-06-25 18:16:07 +01004236 .resetvalue = (1 << 30) },
Peter Maydellc4804212012-06-20 11:57:17 +00004237 REGINFO_SENTINEL
4238};
4239
Peter Maydell34f90522012-06-20 11:57:18 +00004240static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4241 /* Ignore ReadBuffer accesses */
4242 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4243 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +01004244 .access = PL1_RW, .resetvalue = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00004245 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
Peter Maydell34f90522012-06-20 11:57:18 +00004246 REGINFO_SENTINEL
4247};
4248
Edgar E. Iglesias731de9e2015-09-14 14:39:50 +01004249static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4250{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07004251 ARMCPU *cpu = env_archcpu(env);
Edgar E. Iglesias731de9e2015-09-14 14:39:50 +01004252 unsigned int cur_el = arm_current_el(env);
4253 bool secure = arm_is_secure(env);
4254
4255 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
4256 return env->cp15.vpidr_el2;
4257 }
4258 return raw_read(env, ri);
4259}
4260
Edgar E. Iglesias06a7e642015-09-14 14:39:51 +01004261static uint64_t mpidr_read_val(CPUARMState *env)
Peter Maydell81bdde92012-06-20 11:57:20 +00004262{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07004263 ARMCPU *cpu = env_archcpu(env);
Pavel Fedineb5e1d32015-06-15 18:06:09 +01004264 uint64_t mpidr = cpu->mp_affinity;
4265
Peter Maydell81bdde92012-06-20 11:57:20 +00004266 if (arm_feature(env, ARM_FEATURE_V7MP)) {
Peter Maydell78dbbbe2013-09-10 19:09:32 +01004267 mpidr |= (1U << 31);
Peter Maydell81bdde92012-06-20 11:57:20 +00004268 /* Cores which are uniprocessor (non-coherent)
4269 * but still implement the MP extensions set
Peter Crosthwaitea8e81b32015-06-15 18:06:10 +01004270 * bit 30. (For instance, Cortex-R5).
Peter Maydell81bdde92012-06-20 11:57:20 +00004271 */
Peter Crosthwaitea8e81b32015-06-15 18:06:10 +01004272 if (cpu->mp_is_up) {
4273 mpidr |= (1u << 30);
4274 }
Peter Maydell81bdde92012-06-20 11:57:20 +00004275 }
Peter Maydellc4241c72014-02-20 10:35:54 +00004276 return mpidr;
Peter Maydell81bdde92012-06-20 11:57:20 +00004277}
4278
Edgar E. Iglesias06a7e642015-09-14 14:39:51 +01004279static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4280{
Edgar E. Iglesiasf0d574d2015-09-14 14:39:51 +01004281 unsigned int cur_el = arm_current_el(env);
4282 bool secure = arm_is_secure(env);
4283
4284 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
4285 return env->cp15.vmpidr_el2;
4286 }
Edgar E. Iglesias06a7e642015-09-14 14:39:51 +01004287 return mpidr_read_val(env);
4288}
4289
Peter Maydell7ac681c2012-07-12 10:59:07 +00004290static const ARMCPRegInfo lpae_cp_reginfo[] = {
Edgar E. Iglesiasa903c442015-06-02 14:56:21 +01004291 /* NOP AMAIR0/1 */
Peter Maydellb0fe2422014-02-26 17:20:03 +00004292 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4293 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
Richard Henderson84929212020-03-05 16:09:17 +00004294 .access = PL1_RW, .accessfn = access_tvm_trvm,
4295 .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydellb0fe2422014-02-26 17:20:03 +00004296 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
Peter Maydell7ac681c2012-07-12 10:59:07 +00004297 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
Richard Henderson84929212020-03-05 16:09:17 +00004298 .access = PL1_RW, .accessfn = access_tvm_trvm,
4299 .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell891a2fe2012-07-12 10:59:09 +00004300 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
Fabian Aggeler01c097f2014-12-11 12:07:52 +00004301 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4302 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4303 offsetof(CPUARMState, cp15.par_ns)} },
Peter Maydell891a2fe2012-07-12 10:59:09 +00004304 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
Richard Henderson84929212020-03-05 16:09:17 +00004305 .access = PL1_RW, .accessfn = access_tvm_trvm,
4306 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
Fabian Aggeler7dd8c9a2014-12-11 12:07:51 +00004307 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4308 offsetof(CPUARMState, cp15.ttbr0_ns) },
Sergey Fedorovb061a822015-06-19 14:17:44 +01004309 .writefn = vmsa_ttbr_write, },
Peter Maydell891a2fe2012-07-12 10:59:09 +00004310 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
Richard Henderson84929212020-03-05 16:09:17 +00004311 .access = PL1_RW, .accessfn = access_tvm_trvm,
4312 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
Fabian Aggeler7dd8c9a2014-12-11 12:07:51 +00004313 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4314 offsetof(CPUARMState, cp15.ttbr1_ns) },
Sergey Fedorovb061a822015-06-19 14:17:44 +01004315 .writefn = vmsa_ttbr_write, },
Peter Maydell7ac681c2012-07-12 10:59:07 +00004316 REGINFO_SENTINEL
4317};
4318
Peter Maydellc4241c72014-02-20 10:35:54 +00004319static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00004320{
Peter Maydellc4241c72014-02-20 10:35:54 +00004321 return vfp_get_fpcr(env);
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00004322}
4323
Peter Maydellc4241c72014-02-20 10:35:54 +00004324static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4325 uint64_t value)
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00004326{
4327 vfp_set_fpcr(env, value);
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00004328}
4329
Peter Maydellc4241c72014-02-20 10:35:54 +00004330static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00004331{
Peter Maydellc4241c72014-02-20 10:35:54 +00004332 return vfp_get_fpsr(env);
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00004333}
4334
Peter Maydellc4241c72014-02-20 10:35:54 +00004335static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4336 uint64_t value)
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00004337{
4338 vfp_set_fpsr(env, value);
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00004339}
4340
Peter Maydell3f208fd2016-02-11 11:17:31 +00004341static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4342 bool isread)
Peter Maydellc2b820f2014-04-15 19:18:37 +01004343{
Richard Hendersonaaec1432020-02-07 14:04:24 +00004344 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
Peter Maydellc2b820f2014-04-15 19:18:37 +01004345 return CP_ACCESS_TRAP;
4346 }
4347 return CP_ACCESS_OK;
4348}
4349
4350static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4351 uint64_t value)
4352{
4353 env->daif = value & PSTATE_DAIF;
4354}
4355
Richard Henderson220f5082020-02-08 12:58:07 +00004356static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4357{
4358 return env->pstate & PSTATE_PAN;
4359}
4360
4361static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4362 uint64_t value)
4363{
4364 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4365}
4366
4367static const ARMCPRegInfo pan_reginfo = {
4368 .name = "PAN", .state = ARM_CP_STATE_AA64,
4369 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4370 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4371 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4372};
4373
Richard Henderson9eeb7a12020-02-08 12:58:14 +00004374static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4375{
4376 return env->pstate & PSTATE_UAO;
4377}
4378
4379static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4380 uint64_t value)
4381{
4382 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4383}
4384
4385static const ARMCPRegInfo uao_reginfo = {
4386 .name = "UAO", .state = ARM_CP_STATE_AA64,
4387 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4388 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4389 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4390};
4391
Richard Henderson1bed4d22020-03-05 16:09:18 +00004392static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4393 const ARMCPRegInfo *ri,
4394 bool isread)
4395{
4396 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4397 switch (arm_current_el(env)) {
4398 case 0:
4399 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4400 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4401 return CP_ACCESS_TRAP;
4402 }
4403 /* fall through */
4404 case 1:
4405 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4406 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4407 return CP_ACCESS_TRAP_EL2;
4408 }
4409 break;
4410 }
4411 return CP_ACCESS_OK;
4412}
4413
Richard Henderson38262d82020-03-05 16:09:18 +00004414static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
4415 const ARMCPRegInfo *ri,
4416 bool isread)
4417{
4418 /* Cache invalidate/clean to Point of Unification... */
4419 switch (arm_current_el(env)) {
4420 case 0:
4421 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4422 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4423 return CP_ACCESS_TRAP;
4424 }
4425 /* fall through */
4426 case 1:
4427 /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set. */
4428 if (arm_hcr_el2_eff(env) & HCR_TPU) {
4429 return CP_ACCESS_TRAP_EL2;
4430 }
4431 break;
4432 }
4433 return CP_ACCESS_OK;
4434}
4435
Alex Bennéedbb1fb22014-08-04 14:41:56 +01004436/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4437 * Page D4-1736 (DDI0487A.b)
4438 */
4439
Richard Hendersonb7e07302020-02-07 14:04:22 +00004440static int vae1_tlbmask(CPUARMState *env)
4441{
Richard Henderson85d0dc92020-02-07 14:04:26 +00004442 /* Since we exclude secure first, we may read HCR_EL2 directly. */
Richard Hendersonb7e07302020-02-07 14:04:22 +00004443 if (arm_is_secure_below_el3(env)) {
Richard Henderson452ef8c2020-02-08 12:57:58 +00004444 return ARMMMUIdxBit_SE10_1 |
4445 ARMMMUIdxBit_SE10_1_PAN |
4446 ARMMMUIdxBit_SE10_0;
Richard Henderson85d0dc92020-02-07 14:04:26 +00004447 } else if ((env->cp15.hcr_el2 & (HCR_E2H | HCR_TGE))
4448 == (HCR_E2H | HCR_TGE)) {
Richard Henderson452ef8c2020-02-08 12:57:58 +00004449 return ARMMMUIdxBit_E20_2 |
4450 ARMMMUIdxBit_E20_2_PAN |
4451 ARMMMUIdxBit_E20_0;
Richard Hendersonb7e07302020-02-07 14:04:22 +00004452 } else {
Richard Henderson452ef8c2020-02-08 12:57:58 +00004453 return ARMMMUIdxBit_E10_1 |
4454 ARMMMUIdxBit_E10_1_PAN |
4455 ARMMMUIdxBit_E10_0;
Richard Hendersonb7e07302020-02-07 14:04:22 +00004456 }
4457}
4458
Peter Maydellfd3ed962015-08-25 15:45:09 +01004459static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4460 uint64_t value)
Peter Maydell168aa232014-02-26 17:20:03 +00004461{
Richard Henderson29a0af62019-03-22 16:07:18 -07004462 CPUState *cs = env_cpu(env);
Richard Hendersonb7e07302020-02-07 14:04:22 +00004463 int mask = vae1_tlbmask(env);
Alex Bennéedbb1fb22014-08-04 14:41:56 +01004464
Richard Hendersonb7e07302020-02-07 14:04:22 +00004465 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
Peter Maydell168aa232014-02-26 17:20:03 +00004466}
4467
Peter Maydellb4ab8ce2018-10-24 07:50:17 +01004468static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4469 uint64_t value)
4470{
Richard Henderson29a0af62019-03-22 16:07:18 -07004471 CPUState *cs = env_cpu(env);
Richard Hendersonb7e07302020-02-07 14:04:22 +00004472 int mask = vae1_tlbmask(env);
Peter Maydellb4ab8ce2018-10-24 07:50:17 +01004473
4474 if (tlb_force_broadcast(env)) {
Richard Henderson527db2b2020-02-07 14:04:22 +00004475 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4476 } else {
4477 tlb_flush_by_mmuidx(cs, mask);
Peter Maydellb4ab8ce2018-10-24 07:50:17 +01004478 }
Peter Maydellb4ab8ce2018-10-24 07:50:17 +01004479}
4480
Richard Henderson90c19cd2020-02-07 14:04:22 +00004481static int alle1_tlbmask(CPUARMState *env)
Peter Maydellfd3ed962015-08-25 15:45:09 +01004482{
Richard Henderson90c19cd2020-02-07 14:04:22 +00004483 /*
4484 * Note that the 'ALL' scope must invalidate both stage 1 and
Peter Maydellfd3ed962015-08-25 15:45:09 +01004485 * stage 2 translations, whereas most other scopes only invalidate
4486 * stage 1 translations.
4487 */
Peter Maydellfd3ed962015-08-25 15:45:09 +01004488 if (arm_is_secure_below_el3(env)) {
Richard Henderson452ef8c2020-02-08 12:57:58 +00004489 return ARMMMUIdxBit_SE10_1 |
4490 ARMMMUIdxBit_SE10_1_PAN |
4491 ARMMMUIdxBit_SE10_0;
Peter Maydellfd3ed962015-08-25 15:45:09 +01004492 } else {
Richard Henderson452ef8c2020-02-08 12:57:58 +00004493 return ARMMMUIdxBit_E10_1 |
4494 ARMMMUIdxBit_E10_1_PAN |
4495 ARMMMUIdxBit_E10_0;
Peter Maydellfd3ed962015-08-25 15:45:09 +01004496 }
4497}
4498
Richard Henderson85d0dc92020-02-07 14:04:26 +00004499static int e2_tlbmask(CPUARMState *env)
4500{
4501 /* TODO: ARMv8.4-SecEL2 */
Richard Henderson452ef8c2020-02-08 12:57:58 +00004502 return ARMMMUIdxBit_E20_0 |
4503 ARMMMUIdxBit_E20_2 |
4504 ARMMMUIdxBit_E20_2_PAN |
4505 ARMMMUIdxBit_E2;
Richard Henderson85d0dc92020-02-07 14:04:26 +00004506}
4507
Richard Henderson90c19cd2020-02-07 14:04:22 +00004508static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4509 uint64_t value)
4510{
4511 CPUState *cs = env_cpu(env);
4512 int mask = alle1_tlbmask(env);
4513
4514 tlb_flush_by_mmuidx(cs, mask);
4515}
4516
Peter Maydellfd3ed962015-08-25 15:45:09 +01004517static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4518 uint64_t value)
4519{
Richard Henderson85d0dc92020-02-07 14:04:26 +00004520 CPUState *cs = env_cpu(env);
4521 int mask = e2_tlbmask(env);
Peter Maydellfd3ed962015-08-25 15:45:09 +01004522
Richard Henderson85d0dc92020-02-07 14:04:26 +00004523 tlb_flush_by_mmuidx(cs, mask);
Peter Maydellfd3ed962015-08-25 15:45:09 +01004524}
4525
Peter Maydell43efaa32015-08-25 15:45:10 +01004526static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4527 uint64_t value)
4528{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07004529 ARMCPU *cpu = env_archcpu(env);
Peter Maydell43efaa32015-08-25 15:45:10 +01004530 CPUState *cs = CPU(cpu);
4531
Richard Henderson127b2b02020-02-07 14:04:23 +00004532 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
Peter Maydell43efaa32015-08-25 15:45:10 +01004533}
4534
Peter Maydellfd3ed962015-08-25 15:45:09 +01004535static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4536 uint64_t value)
4537{
Richard Henderson29a0af62019-03-22 16:07:18 -07004538 CPUState *cs = env_cpu(env);
Richard Henderson90c19cd2020-02-07 14:04:22 +00004539 int mask = alle1_tlbmask(env);
Peter Maydellfd3ed962015-08-25 15:45:09 +01004540
Richard Henderson90c19cd2020-02-07 14:04:22 +00004541 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
Peter Maydellfd3ed962015-08-25 15:45:09 +01004542}
4543
Peter Maydell2bfb9d72015-08-25 15:45:09 +01004544static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4545 uint64_t value)
4546{
Richard Henderson29a0af62019-03-22 16:07:18 -07004547 CPUState *cs = env_cpu(env);
Richard Henderson85d0dc92020-02-07 14:04:26 +00004548 int mask = e2_tlbmask(env);
Peter Maydell2bfb9d72015-08-25 15:45:09 +01004549
Richard Henderson85d0dc92020-02-07 14:04:26 +00004550 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
Peter Maydell2bfb9d72015-08-25 15:45:09 +01004551}
4552
Peter Maydell43efaa32015-08-25 15:45:10 +01004553static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4554 uint64_t value)
4555{
Richard Henderson29a0af62019-03-22 16:07:18 -07004556 CPUState *cs = env_cpu(env);
Peter Maydell43efaa32015-08-25 15:45:10 +01004557
Richard Henderson127b2b02020-02-07 14:04:23 +00004558 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
Peter Maydell43efaa32015-08-25 15:45:10 +01004559}
4560
Peter Maydellfd3ed962015-08-25 15:45:09 +01004561static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4562 uint64_t value)
4563{
4564 /* Invalidate by VA, EL2
4565 * Currently handles both VAE2 and VALE2, since we don't support
4566 * flush-last-level-only.
4567 */
Richard Henderson85d0dc92020-02-07 14:04:26 +00004568 CPUState *cs = env_cpu(env);
4569 int mask = e2_tlbmask(env);
Peter Maydellfd3ed962015-08-25 15:45:09 +01004570 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4571
Richard Henderson85d0dc92020-02-07 14:04:26 +00004572 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
Peter Maydellfd3ed962015-08-25 15:45:09 +01004573}
4574
Peter Maydell43efaa32015-08-25 15:45:10 +01004575static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4576 uint64_t value)
4577{
4578 /* Invalidate by VA, EL3
4579 * Currently handles both VAE3 and VALE3, since we don't support
4580 * flush-last-level-only.
4581 */
Richard Henderson2fc0cc02019-03-22 17:41:14 -07004582 ARMCPU *cpu = env_archcpu(env);
Peter Maydell43efaa32015-08-25 15:45:10 +01004583 CPUState *cs = CPU(cpu);
4584 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4585
Richard Henderson127b2b02020-02-07 14:04:23 +00004586 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
Peter Maydell43efaa32015-08-25 15:45:10 +01004587}
4588
Peter Maydellfd3ed962015-08-25 15:45:09 +01004589static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4590 uint64_t value)
4591{
Richard Henderson90c19cd2020-02-07 14:04:22 +00004592 CPUState *cs = env_cpu(env);
4593 int mask = vae1_tlbmask(env);
Peter Maydellfd3ed962015-08-25 15:45:09 +01004594 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4595
Richard Henderson90c19cd2020-02-07 14:04:22 +00004596 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
Peter Maydellfd3ed962015-08-25 15:45:09 +01004597}
4598
Peter Maydellb4ab8ce2018-10-24 07:50:17 +01004599static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4600 uint64_t value)
4601{
4602 /* Invalidate by VA, EL1&0 (AArch64 version).
4603 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4604 * since we don't support flush-for-specific-ASID-only or
4605 * flush-last-level-only.
4606 */
Richard Henderson90c19cd2020-02-07 14:04:22 +00004607 CPUState *cs = env_cpu(env);
4608 int mask = vae1_tlbmask(env);
Peter Maydellb4ab8ce2018-10-24 07:50:17 +01004609 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4610
4611 if (tlb_force_broadcast(env)) {
Richard Henderson527db2b2020-02-07 14:04:22 +00004612 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4613 } else {
4614 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
Peter Maydellb4ab8ce2018-10-24 07:50:17 +01004615 }
Peter Maydellb4ab8ce2018-10-24 07:50:17 +01004616}
4617
Peter Maydellfd3ed962015-08-25 15:45:09 +01004618static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4619 uint64_t value)
Peter Maydellfa439fc2014-09-12 14:06:50 +01004620{
Richard Henderson29a0af62019-03-22 16:07:18 -07004621 CPUState *cs = env_cpu(env);
Peter Maydellfa439fc2014-09-12 14:06:50 +01004622 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4623
Alex Bennéea67cf272017-02-23 18:29:25 +00004624 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
Richard Hendersone013b742020-02-07 14:04:23 +00004625 ARMMMUIdxBit_E2);
Peter Maydellfa439fc2014-09-12 14:06:50 +01004626}
4627
Peter Maydell43efaa32015-08-25 15:45:10 +01004628static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4629 uint64_t value)
4630{
Richard Henderson29a0af62019-03-22 16:07:18 -07004631 CPUState *cs = env_cpu(env);
Peter Maydell43efaa32015-08-25 15:45:10 +01004632 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4633
Alex Bennéea67cf272017-02-23 18:29:25 +00004634 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
Richard Henderson127b2b02020-02-07 14:04:23 +00004635 ARMMMUIdxBit_SE3);
Peter Maydell43efaa32015-08-25 15:45:10 +01004636}
4637
Peter Maydell3f208fd2016-02-11 11:17:31 +00004638static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4639 bool isread)
Peter Maydellaca3f402014-04-15 19:18:41 +01004640{
Richard Henderson4351cb72020-02-07 14:04:24 +00004641 int cur_el = arm_current_el(env);
4642
4643 if (cur_el < 2) {
4644 uint64_t hcr = arm_hcr_el2_eff(env);
4645
4646 if (cur_el == 0) {
4647 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4648 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4649 return CP_ACCESS_TRAP_EL2;
4650 }
4651 } else {
4652 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4653 return CP_ACCESS_TRAP;
4654 }
4655 if (hcr & HCR_TDZ) {
4656 return CP_ACCESS_TRAP_EL2;
4657 }
4658 }
4659 } else if (hcr & HCR_TDZ) {
4660 return CP_ACCESS_TRAP_EL2;
4661 }
Peter Maydellaca3f402014-04-15 19:18:41 +01004662 }
4663 return CP_ACCESS_OK;
4664}
4665
4666static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4667{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07004668 ARMCPU *cpu = env_archcpu(env);
Peter Maydellaca3f402014-04-15 19:18:41 +01004669 int dzp_bit = 1 << 4;
4670
4671 /* DZP indicates whether DC ZVA access is allowed */
Peter Maydell3f208fd2016-02-11 11:17:31 +00004672 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
Peter Maydellaca3f402014-04-15 19:18:41 +01004673 dzp_bit = 0;
4674 }
4675 return cpu->dcz_blocksize | dzp_bit;
4676}
4677
Peter Maydell3f208fd2016-02-11 11:17:31 +00004678static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4679 bool isread)
Peter Maydellf502cfc2014-04-15 19:18:43 +01004680{
Stefan Weilcdcf1402014-08-04 14:41:55 +01004681 if (!(env->pstate & PSTATE_SP)) {
Peter Maydellf502cfc2014-04-15 19:18:43 +01004682 /* Access to SP_EL0 is undefined if it's being used as
4683 * the stack pointer.
4684 */
4685 return CP_ACCESS_TRAP_UNCATEGORIZED;
4686 }
4687 return CP_ACCESS_OK;
4688}
4689
4690static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4691{
4692 return env->pstate & PSTATE_SP;
4693}
4694
4695static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4696{
4697 update_spsel(env, val);
4698}
4699
Fabian Aggeler137feaa2014-12-11 12:07:50 +00004700static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4701 uint64_t value)
4702{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07004703 ARMCPU *cpu = env_archcpu(env);
Fabian Aggeler137feaa2014-12-11 12:07:50 +00004704
Richard Hendersonf00faf12020-06-25 20:31:01 -07004705 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4706 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4707 value &= ~SCTLR_M;
4708 }
4709
4710 /* ??? Lots of these bits are not implemented. */
4711
4712 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4713 if (ri->opc1 == 6) { /* SCTLR_EL3 */
4714 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4715 } else {
4716 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4717 SCTLR_ATA0 | SCTLR_ATA);
4718 }
4719 }
4720
Fabian Aggeler137feaa2014-12-11 12:07:50 +00004721 if (raw_read(env, ri) == value) {
4722 /* Skip the TLB flush if nothing actually changed; Linux likes
4723 * to do a lot of pointless SCTLR writes.
4724 */
4725 return;
4726 }
4727
4728 raw_write(env, ri, value);
Richard Hendersonf00faf12020-06-25 20:31:01 -07004729
Fabian Aggeler137feaa2014-12-11 12:07:50 +00004730 /* This may enable/disable the MMU, so do a TLB flush. */
Alex Bennéed10eb082016-11-14 14:17:28 +00004731 tlb_flush(CPU(cpu));
Richard Henderson2e5dcf32019-10-23 11:00:52 -04004732
4733 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4734 /*
4735 * Normally we would always end the TB on an SCTLR write; see the
4736 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4737 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4738 * of hflags from the translator, so do it here.
4739 */
4740 arm_rebuild_hflags(env);
4741 }
Fabian Aggeler137feaa2014-12-11 12:07:50 +00004742}
4743
Peter Maydell3f208fd2016-02-11 11:17:31 +00004744static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
4745 bool isread)
Peter Maydell03fbf202016-01-21 14:15:09 +00004746{
4747 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
Peter Maydellf2cae602016-02-18 14:16:16 +00004748 return CP_ACCESS_TRAP_FP_EL2;
Peter Maydell03fbf202016-01-21 14:15:09 +00004749 }
4750 if (env->cp15.cptr_el[3] & CPTR_TFP) {
Peter Maydellf2cae602016-02-18 14:16:16 +00004751 return CP_ACCESS_TRAP_FP_EL3;
Peter Maydell03fbf202016-01-21 14:15:09 +00004752 }
4753 return CP_ACCESS_OK;
4754}
4755
Peter Maydella8d64e72016-02-19 14:39:43 +00004756static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4757 uint64_t value)
4758{
4759 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4760}
4761
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00004762static const ARMCPRegInfo v8_cp_reginfo[] = {
4763 /* Minimal set of EL0-visible registers. This will need to be expanded
4764 * significantly for system emulation of AArch64 CPUs.
4765 */
4766 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4767 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4768 .access = PL0_RW, .type = ARM_CP_NZCV },
Peter Maydellc2b820f2014-04-15 19:18:37 +01004769 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4770 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00004771 .type = ARM_CP_NO_RAW,
Peter Maydellc2b820f2014-04-15 19:18:37 +01004772 .access = PL0_RW, .accessfn = aa64_daif_access,
4773 .fieldoffset = offsetof(CPUARMState, daif),
4774 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00004775 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4776 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
Richard Hendersonb916c9c2018-02-15 18:29:37 +00004777 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
Richard Hendersonfe03d452018-02-15 18:29:36 +00004778 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00004779 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4780 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
Richard Hendersonb916c9c2018-02-15 18:29:37 +00004781 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
Richard Hendersonfe03d452018-02-15 18:29:36 +00004782 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00004783 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4784 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00004785 .access = PL0_R, .type = ARM_CP_NO_RAW,
Peter Maydellaca3f402014-04-15 19:18:41 +01004786 .readfn = aa64_dczid_read },
4787 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4788 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4789 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4790#ifndef CONFIG_USER_ONLY
4791 /* Avoid overhead of an access check that always passes in user-mode */
4792 .accessfn = aa64_zva_access,
4793#endif
4794 },
Peter Maydell0eef9d92014-02-26 17:20:02 +00004795 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4796 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4797 .access = PL1_R, .type = ARM_CP_CURRENTEL },
Peter Maydell8af35c32014-02-26 17:20:02 +00004798 /* Cache ops: all NOPs since we don't emulate caches */
4799 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4800 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
Richard Henderson38262d82020-03-05 16:09:18 +00004801 .access = PL1_W, .type = ARM_CP_NOP,
4802 .accessfn = aa64_cacheop_pou_access },
Peter Maydell8af35c32014-02-26 17:20:02 +00004803 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4804 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
Richard Henderson38262d82020-03-05 16:09:18 +00004805 .access = PL1_W, .type = ARM_CP_NOP,
4806 .accessfn = aa64_cacheop_pou_access },
Peter Maydell8af35c32014-02-26 17:20:02 +00004807 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4808 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4809 .access = PL0_W, .type = ARM_CP_NOP,
Richard Henderson38262d82020-03-05 16:09:18 +00004810 .accessfn = aa64_cacheop_pou_access },
Peter Maydell8af35c32014-02-26 17:20:02 +00004811 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4812 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
Richard Henderson1bed4d22020-03-05 16:09:18 +00004813 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4814 .type = ARM_CP_NOP },
Peter Maydell8af35c32014-02-26 17:20:02 +00004815 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4816 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
Richard Henderson1803d272020-03-05 16:09:18 +00004817 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
Peter Maydell8af35c32014-02-26 17:20:02 +00004818 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4819 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4820 .access = PL0_W, .type = ARM_CP_NOP,
Richard Henderson1bed4d22020-03-05 16:09:18 +00004821 .accessfn = aa64_cacheop_poc_access },
Peter Maydell8af35c32014-02-26 17:20:02 +00004822 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4823 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
Richard Henderson1803d272020-03-05 16:09:18 +00004824 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
Peter Maydell8af35c32014-02-26 17:20:02 +00004825 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4826 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4827 .access = PL0_W, .type = ARM_CP_NOP,
Richard Henderson38262d82020-03-05 16:09:18 +00004828 .accessfn = aa64_cacheop_pou_access },
Peter Maydell8af35c32014-02-26 17:20:02 +00004829 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4830 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4831 .access = PL0_W, .type = ARM_CP_NOP,
Richard Henderson1bed4d22020-03-05 16:09:18 +00004832 .accessfn = aa64_cacheop_poc_access },
Peter Maydell8af35c32014-02-26 17:20:02 +00004833 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4834 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
Richard Henderson1803d272020-03-05 16:09:18 +00004835 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
Peter Maydell168aa232014-02-26 17:20:03 +00004836 /* TLBI operations */
4837 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01004838 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
Richard Henderson30881b72020-03-05 16:09:18 +00004839 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01004840 .writefn = tlbi_aa64_vmalle1is_write },
Peter Maydell168aa232014-02-26 17:20:03 +00004841 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01004842 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
Richard Henderson30881b72020-03-05 16:09:18 +00004843 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01004844 .writefn = tlbi_aa64_vae1is_write },
Peter Maydell168aa232014-02-26 17:20:03 +00004845 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01004846 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
Richard Henderson30881b72020-03-05 16:09:18 +00004847 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01004848 .writefn = tlbi_aa64_vmalle1is_write },
Peter Maydell168aa232014-02-26 17:20:03 +00004849 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01004850 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
Richard Henderson30881b72020-03-05 16:09:18 +00004851 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01004852 .writefn = tlbi_aa64_vae1is_write },
Peter Maydell168aa232014-02-26 17:20:03 +00004853 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01004854 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
Richard Henderson30881b72020-03-05 16:09:18 +00004855 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01004856 .writefn = tlbi_aa64_vae1is_write },
Peter Maydell168aa232014-02-26 17:20:03 +00004857 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01004858 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
Richard Henderson30881b72020-03-05 16:09:18 +00004859 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01004860 .writefn = tlbi_aa64_vae1is_write },
Peter Maydell168aa232014-02-26 17:20:03 +00004861 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01004862 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
Richard Henderson30881b72020-03-05 16:09:18 +00004863 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01004864 .writefn = tlbi_aa64_vmalle1_write },
Peter Maydell168aa232014-02-26 17:20:03 +00004865 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01004866 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
Richard Henderson30881b72020-03-05 16:09:18 +00004867 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01004868 .writefn = tlbi_aa64_vae1_write },
Peter Maydell168aa232014-02-26 17:20:03 +00004869 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01004870 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
Richard Henderson30881b72020-03-05 16:09:18 +00004871 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01004872 .writefn = tlbi_aa64_vmalle1_write },
Peter Maydell168aa232014-02-26 17:20:03 +00004873 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01004874 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
Richard Henderson30881b72020-03-05 16:09:18 +00004875 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01004876 .writefn = tlbi_aa64_vae1_write },
Peter Maydell168aa232014-02-26 17:20:03 +00004877 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01004878 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
Richard Henderson30881b72020-03-05 16:09:18 +00004879 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01004880 .writefn = tlbi_aa64_vae1_write },
Peter Maydell168aa232014-02-26 17:20:03 +00004881 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01004882 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
Richard Henderson30881b72020-03-05 16:09:18 +00004883 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01004884 .writefn = tlbi_aa64_vae1_write },
Peter Maydellcea66e92015-08-25 15:45:10 +01004885 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4886 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
Peter Maydellbf053402020-03-30 22:03:57 +01004887 .access = PL2_W, .type = ARM_CP_NOP },
Peter Maydellcea66e92015-08-25 15:45:10 +01004888 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4889 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
Peter Maydellbf053402020-03-30 22:03:57 +01004890 .access = PL2_W, .type = ARM_CP_NOP },
Peter Maydell83ddf972015-08-25 15:45:09 +01004891 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4892 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4893 .access = PL2_W, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01004894 .writefn = tlbi_aa64_alle1is_write },
Peter Maydell43efaa32015-08-25 15:45:10 +01004895 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4896 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4897 .access = PL2_W, .type = ARM_CP_NO_RAW,
4898 .writefn = tlbi_aa64_alle1is_write },
Peter Maydellcea66e92015-08-25 15:45:10 +01004899 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4900 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
Peter Maydellbf053402020-03-30 22:03:57 +01004901 .access = PL2_W, .type = ARM_CP_NOP },
Peter Maydellcea66e92015-08-25 15:45:10 +01004902 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4903 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
Peter Maydellbf053402020-03-30 22:03:57 +01004904 .access = PL2_W, .type = ARM_CP_NOP },
Peter Maydell83ddf972015-08-25 15:45:09 +01004905 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4906 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4907 .access = PL2_W, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01004908 .writefn = tlbi_aa64_alle1_write },
Peter Maydell43efaa32015-08-25 15:45:10 +01004909 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4910 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4911 .access = PL2_W, .type = ARM_CP_NO_RAW,
4912 .writefn = tlbi_aa64_alle1is_write },
Peter Maydell19525522014-04-15 19:18:48 +01004913#ifndef CONFIG_USER_ONLY
4914 /* 64 bit address translation operations */
4915 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4916 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
Peter Maydell0710b2f2019-08-16 13:58:02 +01004917 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4918 .writefn = ats_write64 },
Peter Maydell19525522014-04-15 19:18:48 +01004919 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4920 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
Peter Maydell0710b2f2019-08-16 13:58:02 +01004921 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4922 .writefn = ats_write64 },
Peter Maydell19525522014-04-15 19:18:48 +01004923 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4924 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
Peter Maydell0710b2f2019-08-16 13:58:02 +01004925 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4926 .writefn = ats_write64 },
Peter Maydell19525522014-04-15 19:18:48 +01004927 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4928 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
Peter Maydell0710b2f2019-08-16 13:58:02 +01004929 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4930 .writefn = ats_write64 },
Peter Maydell2a47df92015-08-25 15:45:08 +01004931 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias7a379c72015-09-08 17:38:44 +01004932 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
Peter Maydell0710b2f2019-08-16 13:58:02 +01004933 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4934 .writefn = ats_write64 },
Peter Maydell2a47df92015-08-25 15:45:08 +01004935 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias7a379c72015-09-08 17:38:44 +01004936 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
Peter Maydell0710b2f2019-08-16 13:58:02 +01004937 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4938 .writefn = ats_write64 },
Peter Maydell2a47df92015-08-25 15:45:08 +01004939 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias7a379c72015-09-08 17:38:44 +01004940 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
Peter Maydell0710b2f2019-08-16 13:58:02 +01004941 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4942 .writefn = ats_write64 },
Peter Maydell2a47df92015-08-25 15:45:08 +01004943 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias7a379c72015-09-08 17:38:44 +01004944 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
Peter Maydell0710b2f2019-08-16 13:58:02 +01004945 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4946 .writefn = ats_write64 },
Peter Maydell2a47df92015-08-25 15:45:08 +01004947 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4948 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4949 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
Peter Maydell0710b2f2019-08-16 13:58:02 +01004950 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4951 .writefn = ats_write64 },
Peter Maydell2a47df92015-08-25 15:45:08 +01004952 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4953 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
Peter Maydell0710b2f2019-08-16 13:58:02 +01004954 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4955 .writefn = ats_write64 },
Edgar E. Iglesiasc96fc9b2015-09-08 17:38:44 +01004956 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4957 .type = ARM_CP_ALIAS,
4958 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4959 .access = PL1_RW, .resetvalue = 0,
4960 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4961 .writefn = par_write },
Peter Maydell19525522014-04-15 19:18:48 +01004962#endif
Peter Maydell995939a2014-09-12 14:06:50 +01004963 /* TLB invalidate last level of translation table walk */
Peter Maydell9449fdf2014-04-15 19:18:47 +01004964 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
Richard Henderson30881b72020-03-05 16:09:18 +00004965 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4966 .writefn = tlbimva_is_write },
Peter Maydell9449fdf2014-04-15 19:18:47 +01004967 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
Richard Henderson30881b72020-03-05 16:09:18 +00004968 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
Peter Maydellfa439fc2014-09-12 14:06:50 +01004969 .writefn = tlbimvaa_is_write },
Peter Maydell9449fdf2014-04-15 19:18:47 +01004970 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
Richard Henderson30881b72020-03-05 16:09:18 +00004971 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4972 .writefn = tlbimva_write },
Peter Maydell9449fdf2014-04-15 19:18:47 +01004973 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
Richard Henderson30881b72020-03-05 16:09:18 +00004974 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4975 .writefn = tlbimvaa_write },
Sergey Sorokin541ef8c2016-07-14 16:51:37 +01004976 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4977 .type = ARM_CP_NO_RAW, .access = PL2_W,
4978 .writefn = tlbimva_hyp_write },
4979 { .name = "TLBIMVALHIS",
4980 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4981 .type = ARM_CP_NO_RAW, .access = PL2_W,
4982 .writefn = tlbimva_hyp_is_write },
4983 { .name = "TLBIIPAS2",
4984 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
Peter Maydellbf053402020-03-30 22:03:57 +01004985 .type = ARM_CP_NOP, .access = PL2_W },
Sergey Sorokin541ef8c2016-07-14 16:51:37 +01004986 { .name = "TLBIIPAS2IS",
4987 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
Peter Maydellbf053402020-03-30 22:03:57 +01004988 .type = ARM_CP_NOP, .access = PL2_W },
Sergey Sorokin541ef8c2016-07-14 16:51:37 +01004989 { .name = "TLBIIPAS2L",
4990 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
Peter Maydellbf053402020-03-30 22:03:57 +01004991 .type = ARM_CP_NOP, .access = PL2_W },
Sergey Sorokin541ef8c2016-07-14 16:51:37 +01004992 { .name = "TLBIIPAS2LIS",
4993 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
Peter Maydellbf053402020-03-30 22:03:57 +01004994 .type = ARM_CP_NOP, .access = PL2_W },
Peter Maydell9449fdf2014-04-15 19:18:47 +01004995 /* 32 bit cache operations */
4996 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
Richard Henderson38262d82020-03-05 16:09:18 +00004997 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
Peter Maydell9449fdf2014-04-15 19:18:47 +01004998 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
4999 .type = ARM_CP_NOP, .access = PL1_W },
5000 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
Richard Henderson38262d82020-03-05 16:09:18 +00005001 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
Peter Maydell9449fdf2014-04-15 19:18:47 +01005002 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
Richard Henderson38262d82020-03-05 16:09:18 +00005003 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
Peter Maydell9449fdf2014-04-15 19:18:47 +01005004 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5005 .type = ARM_CP_NOP, .access = PL1_W },
5006 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5007 .type = ARM_CP_NOP, .access = PL1_W },
5008 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
Richard Henderson1bed4d22020-03-05 16:09:18 +00005009 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
Peter Maydell9449fdf2014-04-15 19:18:47 +01005010 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
Richard Henderson1803d272020-03-05 16:09:18 +00005011 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
Peter Maydell9449fdf2014-04-15 19:18:47 +01005012 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
Richard Henderson1bed4d22020-03-05 16:09:18 +00005013 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
Peter Maydell9449fdf2014-04-15 19:18:47 +01005014 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
Richard Henderson1803d272020-03-05 16:09:18 +00005015 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
Peter Maydell9449fdf2014-04-15 19:18:47 +01005016 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
Richard Henderson38262d82020-03-05 16:09:18 +00005017 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
Peter Maydell9449fdf2014-04-15 19:18:47 +01005018 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
Richard Henderson1bed4d22020-03-05 16:09:18 +00005019 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
Peter Maydell9449fdf2014-04-15 19:18:47 +01005020 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
Richard Henderson1803d272020-03-05 16:09:18 +00005021 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
Peter Maydell9449fdf2014-04-15 19:18:47 +01005022 /* MMU Domain access control / MPU write buffer control */
Fabian Aggeler0c17d682014-12-11 12:07:51 +00005023 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
Richard Henderson84929212020-03-05 16:09:17 +00005024 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
Fabian Aggeler0c17d682014-12-11 12:07:51 +00005025 .writefn = dacr_write, .raw_writefn = raw_write,
5026 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5027 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
Peter Maydella0618a12014-04-15 19:18:42 +01005028 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00005029 .type = ARM_CP_ALIAS,
Peter Maydella0618a12014-04-15 19:18:42 +01005030 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
Edgar E. Iglesias6947f052014-05-27 17:09:51 +01005031 .access = PL1_RW,
5032 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
Peter Maydella65f1de2014-04-15 19:18:43 +01005033 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00005034 .type = ARM_CP_ALIAS,
Peter Maydella65f1de2014-04-15 19:18:43 +01005035 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
Soren Brinkmann99a99c12015-11-03 13:49:41 +00005036 .access = PL1_RW,
5037 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
Peter Maydellf502cfc2014-04-15 19:18:43 +01005038 /* We rely on the access checks not allowing the guest to write to the
5039 * state field when SPSel indicates that it's being used as the stack
5040 * pointer.
5041 */
5042 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5043 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5044 .access = PL1_RW, .accessfn = sp_el0_access,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00005045 .type = ARM_CP_ALIAS,
Peter Maydellf502cfc2014-04-15 19:18:43 +01005046 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
Greg Bellows884b4de2015-02-05 13:37:22 +00005047 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5048 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00005049 .access = PL2_RW, .type = ARM_CP_ALIAS,
Greg Bellows884b4de2015-02-05 13:37:22 +00005050 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
Peter Maydellf502cfc2014-04-15 19:18:43 +01005051 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5052 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00005053 .type = ARM_CP_NO_RAW,
Peter Maydellf502cfc2014-04-15 19:18:43 +01005054 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
Peter Maydell03fbf202016-01-21 14:15:09 +00005055 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5056 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5057 .type = ARM_CP_ALIAS,
5058 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
5059 .access = PL2_RW, .accessfn = fpexc32_access },
Peter Maydell6a43e0b2016-02-03 13:46:33 +00005060 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5061 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5062 .access = PL2_RW, .resetvalue = 0,
5063 .writefn = dacr_write, .raw_writefn = raw_write,
5064 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5065 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5066 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5067 .access = PL2_RW, .resetvalue = 0,
5068 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5069 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5070 .type = ARM_CP_ALIAS,
5071 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5072 .access = PL2_RW,
5073 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5074 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5075 .type = ARM_CP_ALIAS,
5076 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5077 .access = PL2_RW,
5078 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5079 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5080 .type = ARM_CP_ALIAS,
5081 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5082 .access = PL2_RW,
5083 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5084 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5085 .type = ARM_CP_ALIAS,
5086 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5087 .access = PL2_RW,
5088 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
Peter Maydella8d64e72016-02-19 14:39:43 +00005089 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5090 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5091 .resetvalue = 0,
5092 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5093 { .name = "SDCR", .type = ARM_CP_ALIAS,
5094 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5095 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5096 .writefn = sdcr_write,
5097 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00005098 REGINFO_SENTINEL
5099};
5100
Edgar E. Iglesiasd42e3c22014-05-27 17:09:54 +01005101/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
Peter Maydell4771cd02015-06-01 19:18:36 +01005102static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
Peter Maydelld79e0c02018-08-20 11:24:32 +01005103 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
Edgar E. Iglesiasd42e3c22014-05-27 17:09:54 +01005104 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5105 .access = PL2_RW,
5106 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
Peter Maydellce4afed2018-08-24 13:17:37 +01005107 { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00005108 .type = ARM_CP_NO_RAW,
Edgar E. Iglesiasf149e3e2014-09-29 18:48:48 +01005109 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5110 .access = PL2_RW,
Peter Maydellce4afed2018-08-24 13:17:37 +01005111 .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell831a2fc2019-02-15 09:56:38 +00005112 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5113 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5114 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell68e78e32018-08-20 11:24:32 +01005115 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5116 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5117 .access = PL2_RW,
5118 .type = ARM_CP_CONST, .resetvalue = 0 },
Greg Bellowsc6f19162015-05-29 11:28:52 +01005119 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5120 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5121 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesias95f949a2015-06-02 14:56:21 +01005122 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5123 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5124 .access = PL2_RW, .type = ARM_CP_CONST,
5125 .resetvalue = 0 },
5126 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
Peter Maydellb5ede852018-08-20 11:24:31 +01005127 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
Edgar E. Iglesias95f949a2015-06-02 14:56:21 +01005128 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell2179ef92015-08-25 15:45:07 +01005129 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5130 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5131 .access = PL2_RW, .type = ARM_CP_CONST,
5132 .resetvalue = 0 },
Peter Maydell55b53c72018-08-20 11:24:31 +01005133 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
Peter Maydellb5ede852018-08-20 11:24:31 +01005134 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
Peter Maydell2179ef92015-08-25 15:45:07 +01005135 .access = PL2_RW, .type = ARM_CP_CONST,
5136 .resetvalue = 0 },
Peter Maydell37cd6c22015-08-25 15:45:07 +01005137 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5138 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5139 .access = PL2_RW, .type = ARM_CP_CONST,
5140 .resetvalue = 0 },
5141 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5142 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5143 .access = PL2_RW, .type = ARM_CP_CONST,
5144 .resetvalue = 0 },
Edgar E. Iglesias06ec4c82015-06-02 14:56:21 +01005145 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5146 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5147 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +01005148 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
5149 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
Edgar E. Iglesias93dd1e62020-05-05 16:17:29 +02005150 .access = PL2_RW, .accessfn = access_el3_aa32ns,
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +01005151 .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesiasb698e9c2015-09-14 14:39:50 +01005152 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5153 .cp = 15, .opc1 = 6, .crm = 2,
5154 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5155 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
5156 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5157 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5158 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesiasb9cb5322015-06-02 14:56:21 +01005159 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5160 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5161 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesiasff05f372015-06-02 14:56:21 +01005162 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5163 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5164 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesiasa57633c2015-06-02 14:56:21 +01005165 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5166 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5167 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5168 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5169 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5170 .resetvalue = 0 },
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01005171 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5172 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5173 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01005174 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5175 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5176 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5177 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5178 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5179 .resetvalue = 0 },
Edgar E. Iglesiasb0e66d92015-08-13 11:26:18 +01005180 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5181 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5182 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5183 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5184 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5185 .resetvalue = 0 },
5186 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5187 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5188 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5189 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5190 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5191 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Sergey Fedorov14cc7b52015-10-16 11:14:54 +01005192 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5193 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00005194 .access = PL2_RW, .accessfn = access_tda,
5195 .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesias59e05532015-10-26 14:01:54 +01005196 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
5197 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
Edgar E. Iglesias93dd1e62020-05-05 16:17:29 +02005198 .access = PL2_RW, .accessfn = access_el3_aa32ns,
Edgar E. Iglesias59e05532015-10-26 14:01:54 +01005199 .type = ARM_CP_CONST, .resetvalue = 0 },
Alistair Francis2a5a9ab2016-06-06 16:59:28 +01005200 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5201 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5202 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydellcba517c2018-08-20 11:24:32 +01005203 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5204 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5205 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5206 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5207 .type = ARM_CP_CONST,
5208 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5209 .access = PL2_RW, .resetvalue = 0 },
Edgar E. Iglesiasd42e3c22014-05-27 17:09:54 +01005210 REGINFO_SENTINEL
5211};
5212
Peter Maydellce4afed2018-08-24 13:17:37 +01005213/* Ditto, but for registers which exist in ARMv8 but not v7 */
5214static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = {
5215 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5216 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5217 .access = PL2_RW,
5218 .type = ARM_CP_CONST, .resetvalue = 0 },
5219 REGINFO_SENTINEL
5220};
5221
Richard Hendersond1fb4da2020-03-05 16:09:16 +00005222static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
Edgar E. Iglesiasf149e3e2014-09-29 18:48:48 +01005223{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07005224 ARMCPU *cpu = env_archcpu(env);
Richard Hendersond1fb4da2020-03-05 16:09:16 +00005225
5226 if (arm_feature(env, ARM_FEATURE_V8)) {
5227 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5228 } else {
5229 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5230 }
Edgar E. Iglesiasf149e3e2014-09-29 18:48:48 +01005231
5232 if (arm_feature(env, ARM_FEATURE_EL3)) {
5233 valid_mask &= ~HCR_HCD;
Jan Kiszka77077a82017-10-06 16:46:47 +01005234 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5235 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5236 * However, if we're using the SMC PSCI conduit then QEMU is
5237 * effectively acting like EL3 firmware and so the guest at
5238 * EL2 should retain the ability to prevent EL1 from being
5239 * able to make SMC calls into the ersatz firmware, so in
5240 * that case HCR.TSC should be read/write.
5241 */
Edgar E. Iglesiasf149e3e2014-09-29 18:48:48 +01005242 valid_mask &= ~HCR_TSC;
5243 }
Richard Hendersond1fb4da2020-03-05 16:09:16 +00005244
5245 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5246 if (cpu_isar_feature(aa64_vh, cpu)) {
5247 valid_mask |= HCR_E2H;
5248 }
5249 if (cpu_isar_feature(aa64_lor, cpu)) {
5250 valid_mask |= HCR_TLOR;
5251 }
5252 if (cpu_isar_feature(aa64_pauth, cpu)) {
5253 valid_mask |= HCR_API | HCR_APK;
5254 }
Richard Henderson8ddb3002020-06-25 20:31:02 -07005255 if (cpu_isar_feature(aa64_mte, cpu)) {
5256 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5257 }
Richard Hendersonef682cd2019-02-01 14:55:46 +00005258 }
Edgar E. Iglesiasf149e3e2014-09-29 18:48:48 +01005259
5260 /* Clear RES0 bits. */
5261 value &= valid_mask;
5262
Richard Henderson8ddb3002020-06-25 20:31:02 -07005263 /*
5264 * These bits change the MMU setup:
Edgar E. Iglesiasf149e3e2014-09-29 18:48:48 +01005265 * HCR_VM enables stage 2 translation
5266 * HCR_PTW forbids certain page-table setups
Richard Henderson8ddb3002020-06-25 20:31:02 -07005267 * HCR_DC disables stage1 and enables stage2 translation
5268 * HCR_DCT enables tagging on (disabled) stage1 translation
Edgar E. Iglesiasf149e3e2014-09-29 18:48:48 +01005269 */
Richard Henderson8ddb3002020-06-25 20:31:02 -07005270 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT)) {
Alex Bennéed10eb082016-11-14 14:17:28 +00005271 tlb_flush(CPU(cpu));
Edgar E. Iglesiasf149e3e2014-09-29 18:48:48 +01005272 }
Peter Maydellce4afed2018-08-24 13:17:37 +01005273 env->cp15.hcr_el2 = value;
Peter Maydell89430fc2018-11-13 10:47:59 +00005274
5275 /*
5276 * Updates to VI and VF require us to update the status of
5277 * virtual interrupts, which are the logical OR of these bits
5278 * and the state of the input lines from the GIC. (This requires
5279 * that we have the iothread lock, which is done by marking the
5280 * reginfo structs as ARM_CP_IO.)
5281 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5282 * possible for it to be taken immediately, because VIRQ and
5283 * VFIQ are masked unless running at EL0 or EL1, and HCR
5284 * can only be written at EL2.
5285 */
5286 g_assert(qemu_mutex_iothread_locked());
5287 arm_cpu_update_virq(cpu);
5288 arm_cpu_update_vfiq(cpu);
Peter Maydellce4afed2018-08-24 13:17:37 +01005289}
5290
Richard Hendersond1fb4da2020-03-05 16:09:16 +00005291static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5292{
5293 do_hcr_write(env, value, 0);
5294}
5295
Peter Maydellce4afed2018-08-24 13:17:37 +01005296static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5297 uint64_t value)
5298{
5299 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5300 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
Richard Hendersond1fb4da2020-03-05 16:09:16 +00005301 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
Peter Maydellce4afed2018-08-24 13:17:37 +01005302}
5303
5304static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5305 uint64_t value)
5306{
5307 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5308 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
Richard Hendersond1fb4da2020-03-05 16:09:16 +00005309 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
Edgar E. Iglesiasf149e3e2014-09-29 18:48:48 +01005310}
5311
Richard Hendersonf7778442018-12-13 13:48:07 +00005312/*
5313 * Return the effective value of HCR_EL2.
5314 * Bits that are not included here:
5315 * RW (read from SCR_EL3.RW as needed)
5316 */
5317uint64_t arm_hcr_el2_eff(CPUARMState *env)
5318{
5319 uint64_t ret = env->cp15.hcr_el2;
5320
5321 if (arm_is_secure_below_el3(env)) {
5322 /*
5323 * "This register has no effect if EL2 is not enabled in the
5324 * current Security state". This is ARMv8.4-SecEL2 speak for
5325 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5326 *
5327 * Prior to that, the language was "In an implementation that
5328 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5329 * as if this field is 0 for all purposes other than a direct
5330 * read or write access of HCR_EL2". With lots of enumeration
5331 * on a per-field basis. In current QEMU, this is condition
5332 * is arm_is_secure_below_el3.
5333 *
5334 * Since the v8.4 language applies to the entire register, and
5335 * appears to be backward compatible, use that.
5336 */
Richard Henderson4990e1d2020-03-05 16:09:17 +00005337 return 0;
5338 }
5339
5340 /*
5341 * For a cpu that supports both aarch64 and aarch32, we can set bits
5342 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5343 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5344 */
5345 if (!arm_el_is_aa64(env, 2)) {
5346 uint64_t aa32_valid;
5347
5348 /*
5349 * These bits are up-to-date as of ARMv8.6.
5350 * For HCR, it's easiest to list just the 2 bits that are invalid.
5351 * For HCR2, list those that are valid.
5352 */
5353 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5354 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5355 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5356 ret &= aa32_valid;
5357 }
5358
5359 if (ret & HCR_TGE) {
5360 /* These bits are up-to-date as of ARMv8.6. */
Richard Hendersonf7778442018-12-13 13:48:07 +00005361 if (ret & HCR_E2H) {
5362 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5363 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5364 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
Richard Henderson4990e1d2020-03-05 16:09:17 +00005365 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5366 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5367 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
Richard Hendersonf7778442018-12-13 13:48:07 +00005368 } else {
5369 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5370 }
5371 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5372 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5373 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5374 HCR_TLOR);
5375 }
5376
5377 return ret;
5378}
5379
Peter Maydellfc1120a2019-05-10 12:03:57 +01005380static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5381 uint64_t value)
5382{
5383 /*
5384 * For A-profile AArch32 EL3, if NSACR.CP10
5385 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5386 */
5387 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5388 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5389 value &= ~(0x3 << 10);
5390 value |= env->cp15.cptr_el[2] & (0x3 << 10);
5391 }
5392 env->cp15.cptr_el[2] = value;
5393}
5394
5395static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5396{
5397 /*
5398 * For A-profile AArch32 EL3, if NSACR.CP10
5399 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5400 */
5401 uint64_t value = env->cp15.cptr_el[2];
5402
5403 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5404 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5405 value |= 0x3 << 10;
5406 }
5407 return value;
5408}
5409
Peter Maydell4771cd02015-06-01 19:18:36 +01005410static const ARMCPRegInfo el2_cp_reginfo[] = {
Edgar E. Iglesiasf149e3e2014-09-29 18:48:48 +01005411 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
Peter Maydell89430fc2018-11-13 10:47:59 +00005412 .type = ARM_CP_IO,
Edgar E. Iglesiasf149e3e2014-09-29 18:48:48 +01005413 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5414 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
Peter Maydellc624ea02018-11-13 10:47:59 +00005415 .writefn = hcr_write },
Peter Maydellce4afed2018-08-24 13:17:37 +01005416 { .name = "HCR", .state = ARM_CP_STATE_AA32,
Peter Maydell89430fc2018-11-13 10:47:59 +00005417 .type = ARM_CP_ALIAS | ARM_CP_IO,
Peter Maydellce4afed2018-08-24 13:17:37 +01005418 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5419 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
Peter Maydellc624ea02018-11-13 10:47:59 +00005420 .writefn = hcr_writelow },
Peter Maydell831a2fc2019-02-15 09:56:38 +00005421 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5422 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5423 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesias3b685ba2014-05-27 17:09:53 +01005424 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00005425 .type = ARM_CP_ALIAS,
Edgar E. Iglesias3b685ba2014-05-27 17:09:53 +01005426 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5427 .access = PL2_RW,
5428 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
Peter Maydell68e78e32018-08-20 11:24:32 +01005429 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
Edgar E. Iglesiasf2c30f42014-08-04 14:41:55 +01005430 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5431 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
Peter Maydellcba517c2018-08-20 11:24:32 +01005432 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
Edgar E. Iglesias63b60552014-08-04 14:41:55 +01005433 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5434 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
Peter Maydellcba517c2018-08-20 11:24:32 +01005435 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5436 .type = ARM_CP_ALIAS,
5437 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5438 .access = PL2_RW,
5439 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
Edgar E. Iglesias3b685ba2014-05-27 17:09:53 +01005440 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00005441 .type = ARM_CP_ALIAS,
Edgar E. Iglesias3b685ba2014-05-27 17:09:53 +01005442 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
Soren Brinkmann99a99c12015-11-03 13:49:41 +00005443 .access = PL2_RW,
5444 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
Peter Maydelld79e0c02018-08-20 11:24:32 +01005445 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
Edgar E. Iglesiasd42e3c22014-05-27 17:09:54 +01005446 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5447 .access = PL2_RW, .writefn = vbar_write,
5448 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5449 .resetvalue = 0 },
Greg Bellows884b4de2015-02-05 13:37:22 +00005450 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5451 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00005452 .access = PL3_RW, .type = ARM_CP_ALIAS,
Greg Bellows884b4de2015-02-05 13:37:22 +00005453 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
Greg Bellowsc6f19162015-05-29 11:28:52 +01005454 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5455 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5456 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
Peter Maydellfc1120a2019-05-10 12:03:57 +01005457 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5458 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
Edgar E. Iglesias95f949a2015-06-02 14:56:21 +01005459 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5460 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5461 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5462 .resetvalue = 0 },
5463 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
Peter Maydellb5ede852018-08-20 11:24:31 +01005464 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
Edgar E. Iglesias95f949a2015-06-02 14:56:21 +01005465 .access = PL2_RW, .type = ARM_CP_ALIAS,
5466 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
Peter Maydell2179ef92015-08-25 15:45:07 +01005467 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5468 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5469 .access = PL2_RW, .type = ARM_CP_CONST,
5470 .resetvalue = 0 },
5471 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
Peter Maydell55b53c72018-08-20 11:24:31 +01005472 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
Peter Maydellb5ede852018-08-20 11:24:31 +01005473 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
Peter Maydell2179ef92015-08-25 15:45:07 +01005474 .access = PL2_RW, .type = ARM_CP_CONST,
5475 .resetvalue = 0 },
Peter Maydell37cd6c22015-08-25 15:45:07 +01005476 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5477 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5478 .access = PL2_RW, .type = ARM_CP_CONST,
5479 .resetvalue = 0 },
5480 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5481 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5482 .access = PL2_RW, .type = ARM_CP_CONST,
5483 .resetvalue = 0 },
Edgar E. Iglesias06ec4c82015-06-02 14:56:21 +01005484 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5485 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
Richard Hendersond06dc932020-02-07 14:04:26 +00005486 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5487 /* no .raw_writefn or .resetfn needed as we never use mask/base_mask */
Edgar E. Iglesias06ec4c82015-06-02 14:56:21 +01005488 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +01005489 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5490 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
Peter Maydellbf06c112016-04-04 17:33:52 +01005491 .type = ARM_CP_ALIAS,
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +01005492 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5493 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5494 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5495 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
Peter Maydellbf06c112016-04-04 17:33:52 +01005496 .access = PL2_RW,
5497 /* no .writefn needed as this can't cause an ASID change;
5498 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
5499 */
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +01005500 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
Edgar E. Iglesiasb698e9c2015-09-14 14:39:50 +01005501 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5502 .cp = 15, .opc1 = 6, .crm = 2,
5503 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5504 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5505 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5506 .writefn = vttbr_write },
5507 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5508 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5509 .access = PL2_RW, .writefn = vttbr_write,
5510 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
Edgar E. Iglesiasb9cb5322015-06-02 14:56:21 +01005511 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5512 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5513 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5514 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
Edgar E. Iglesiasff05f372015-06-02 14:56:21 +01005515 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5516 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5517 .access = PL2_RW, .resetvalue = 0,
5518 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
Edgar E. Iglesiasa57633c2015-06-02 14:56:21 +01005519 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5520 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
Richard Hendersoned30da82020-02-07 14:04:21 +00005521 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
Edgar E. Iglesiasa57633c2015-06-02 14:56:21 +01005522 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5523 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5524 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
Edgar E. Iglesiasa57633c2015-06-02 14:56:21 +01005525 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
Sergey Sorokin541ef8c2016-07-14 16:51:37 +01005526 { .name = "TLBIALLNSNH",
5527 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5528 .type = ARM_CP_NO_RAW, .access = PL2_W,
5529 .writefn = tlbiall_nsnh_write },
5530 { .name = "TLBIALLNSNHIS",
5531 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5532 .type = ARM_CP_NO_RAW, .access = PL2_W,
5533 .writefn = tlbiall_nsnh_is_write },
5534 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5535 .type = ARM_CP_NO_RAW, .access = PL2_W,
5536 .writefn = tlbiall_hyp_write },
5537 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5538 .type = ARM_CP_NO_RAW, .access = PL2_W,
5539 .writefn = tlbiall_hyp_is_write },
5540 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5541 .type = ARM_CP_NO_RAW, .access = PL2_W,
5542 .writefn = tlbimva_hyp_write },
5543 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5544 .type = ARM_CP_NO_RAW, .access = PL2_W,
5545 .writefn = tlbimva_hyp_is_write },
Edgar E. Iglesias51da9012015-06-02 14:56:22 +01005546 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5547 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5548 .type = ARM_CP_NO_RAW, .access = PL2_W,
Peter Maydellfd3ed962015-08-25 15:45:09 +01005549 .writefn = tlbi_aa64_alle2_write },
Edgar E. Iglesias8742d492015-06-02 14:56:22 +01005550 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5551 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5552 .type = ARM_CP_NO_RAW, .access = PL2_W,
Peter Maydellfd3ed962015-08-25 15:45:09 +01005553 .writefn = tlbi_aa64_vae2_write },
Peter Maydell2bfb9d72015-08-25 15:45:09 +01005554 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5555 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5556 .access = PL2_W, .type = ARM_CP_NO_RAW,
5557 .writefn = tlbi_aa64_vae2_write },
5558 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5559 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5560 .access = PL2_W, .type = ARM_CP_NO_RAW,
5561 .writefn = tlbi_aa64_alle2is_write },
Edgar E. Iglesias8742d492015-06-02 14:56:22 +01005562 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5563 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5564 .type = ARM_CP_NO_RAW, .access = PL2_W,
Peter Maydellfd3ed962015-08-25 15:45:09 +01005565 .writefn = tlbi_aa64_vae2is_write },
Peter Maydell2bfb9d72015-08-25 15:45:09 +01005566 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5567 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5568 .access = PL2_W, .type = ARM_CP_NO_RAW,
5569 .writefn = tlbi_aa64_vae2is_write },
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01005570#ifndef CONFIG_USER_ONLY
Peter Maydell2a47df92015-08-25 15:45:08 +01005571 /* Unlike the other EL2-related AT operations, these must
5572 * UNDEF from EL3 if EL2 is not implemented, which is why we
5573 * define them here rather than with the rest of the AT ops.
5574 */
5575 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5576 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5577 .access = PL2_W, .accessfn = at_s1e2_access,
Peter Maydell0710b2f2019-08-16 13:58:02 +01005578 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
Peter Maydell2a47df92015-08-25 15:45:08 +01005579 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5580 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5581 .access = PL2_W, .accessfn = at_s1e2_access,
Peter Maydell0710b2f2019-08-16 13:58:02 +01005582 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
Peter Maydell14db7fe2015-08-25 15:45:08 +01005583 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5584 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5585 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5586 * to behave as if SCR.NS was 1.
5587 */
5588 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5589 .access = PL2_W,
Peter Maydell0710b2f2019-08-16 13:58:02 +01005590 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
Peter Maydell14db7fe2015-08-25 15:45:08 +01005591 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5592 .access = PL2_W,
Peter Maydell0710b2f2019-08-16 13:58:02 +01005593 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01005594 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5595 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5596 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5597 * reset values as IMPDEF. We choose to reset to 3 to comply with
5598 * both ARMv7 and ARMv8.
5599 */
5600 .access = PL2_RW, .resetvalue = 3,
5601 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01005602 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5603 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5604 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5605 .writefn = gt_cntvoff_write,
5606 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5607 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5608 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5609 .writefn = gt_cntvoff_write,
5610 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
Edgar E. Iglesiasb0e66d92015-08-13 11:26:18 +01005611 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5612 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5613 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5614 .type = ARM_CP_IO, .access = PL2_RW,
5615 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5616 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5617 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5618 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5619 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5620 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5621 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
Edgar E. Iglesiasd44ec152016-02-26 13:45:39 +01005622 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
Edgar E. Iglesiasb0e66d92015-08-13 11:26:18 +01005623 .resetfn = gt_hyp_timer_reset,
5624 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5625 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5626 .type = ARM_CP_IO,
5627 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5628 .access = PL2_RW,
5629 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5630 .resetvalue = 0,
5631 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01005632#endif
Sergey Fedorov14cc7b52015-10-16 11:14:54 +01005633 /* The only field of MDCR_EL2 that has a defined architectural reset value
5634 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
Aaron Lindsay5ecdd3e2019-01-21 10:23:14 +00005635 * don't implement any PMU event counters, so using zero as a reset
Sergey Fedorov14cc7b52015-10-16 11:14:54 +01005636 * value for MDCR_EL2 is okay
5637 */
5638 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5639 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
5640 .access = PL2_RW, .resetvalue = 0,
5641 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
Edgar E. Iglesias59e05532015-10-26 14:01:54 +01005642 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5643 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5644 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5645 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5646 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5647 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5648 .access = PL2_RW,
5649 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
Alistair Francis2a5a9ab2016-06-06 16:59:28 +01005650 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5651 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5652 .access = PL2_RW,
5653 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
Edgar E. Iglesias3b685ba2014-05-27 17:09:53 +01005654 REGINFO_SENTINEL
5655};
5656
Peter Maydellce4afed2018-08-24 13:17:37 +01005657static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5658 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
Peter Maydell89430fc2018-11-13 10:47:59 +00005659 .type = ARM_CP_ALIAS | ARM_CP_IO,
Peter Maydellce4afed2018-08-24 13:17:37 +01005660 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5661 .access = PL2_RW,
5662 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5663 .writefn = hcr_writehigh },
5664 REGINFO_SENTINEL
5665};
5666
Peter Maydell2f027fc2016-02-11 11:17:31 +00005667static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5668 bool isread)
5669{
5670 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5671 * At Secure EL1 it traps to EL3.
5672 */
5673 if (arm_current_el(env) == 3) {
5674 return CP_ACCESS_OK;
5675 }
5676 if (arm_is_secure_below_el3(env)) {
5677 return CP_ACCESS_TRAP_EL3;
5678 }
5679 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5680 if (isread) {
5681 return CP_ACCESS_OK;
5682 }
5683 return CP_ACCESS_TRAP_UNCATEGORIZED;
5684}
5685
Greg Bellows60fb1a82014-12-16 16:58:05 +00005686static const ARMCPRegInfo el3_cp_reginfo[] = {
5687 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5688 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5689 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5690 .resetvalue = 0, .writefn = scr_write },
Alex Bennéef80741d2019-12-12 11:47:34 +00005691 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
Greg Bellows60fb1a82014-12-16 16:58:05 +00005692 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
Peter Maydellefe4a272016-02-11 11:17:30 +00005693 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5694 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
Sergey Fedorovb061a822015-06-19 14:17:44 +01005695 .writefn = scr_write },
Greg Bellows60fb1a82014-12-16 16:58:05 +00005696 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5697 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5698 .access = PL3_RW, .resetvalue = 0,
5699 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5700 { .name = "SDER",
5701 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5702 .access = PL3_RW, .resetvalue = 0,
5703 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
Greg Bellows60fb1a82014-12-16 16:58:05 +00005704 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
Peter Maydellefe4a272016-02-11 11:17:30 +00005705 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5706 .writefn = vbar_write, .resetvalue = 0,
Greg Bellows60fb1a82014-12-16 16:58:05 +00005707 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
Fabian Aggeler7dd8c9a2014-12-11 12:07:51 +00005708 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5709 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
Richard Hendersonf4788472018-10-24 07:50:20 +01005710 .access = PL3_RW, .resetvalue = 0,
Fabian Aggeler7dd8c9a2014-12-11 12:07:51 +00005711 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
Fabian Aggeler11f136e2014-12-11 12:07:51 +00005712 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5713 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
Peter Maydell6459b942016-05-12 13:22:30 +01005714 .access = PL3_RW,
5715 /* no .writefn needed as this can't cause an ASID change;
Peter Maydell811595a2016-06-14 15:59:12 +01005716 * we must provide a .raw_writefn and .resetfn because we handle
5717 * reset and migration for the AArch32 TTBCR(S), which might be
5718 * using mask and base_mask.
Peter Maydell6459b942016-05-12 13:22:30 +01005719 */
Peter Maydell811595a2016-06-14 15:59:12 +01005720 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
Fabian Aggeler11f136e2014-12-11 12:07:51 +00005721 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
Edgar E. Iglesias81547d62014-05-27 17:09:53 +01005722 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00005723 .type = ARM_CP_ALIAS,
Edgar E. Iglesias81547d62014-05-27 17:09:53 +01005724 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5725 .access = PL3_RW,
5726 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
Edgar E. Iglesiasf2c30f42014-08-04 14:41:55 +01005727 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesiasf2c30f42014-08-04 14:41:55 +01005728 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5729 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
Edgar E. Iglesias63b60552014-08-04 14:41:55 +01005730 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5731 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5732 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
Edgar E. Iglesias81547d62014-05-27 17:09:53 +01005733 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00005734 .type = ARM_CP_ALIAS,
Edgar E. Iglesias81547d62014-05-27 17:09:53 +01005735 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
Soren Brinkmann99a99c12015-11-03 13:49:41 +00005736 .access = PL3_RW,
5737 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
Edgar E. Iglesiasa1ba1252014-05-27 17:09:55 +01005738 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5739 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5740 .access = PL3_RW, .writefn = vbar_write,
5741 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5742 .resetvalue = 0 },
Greg Bellowsc6f19162015-05-29 11:28:52 +01005743 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5744 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5745 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5746 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
Peter Maydell4cfb8ad2015-08-25 15:45:07 +01005747 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5748 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5749 .access = PL3_RW, .resetvalue = 0,
5750 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
Peter Maydell2179ef92015-08-25 15:45:07 +01005751 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5752 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5753 .access = PL3_RW, .type = ARM_CP_CONST,
5754 .resetvalue = 0 },
Peter Maydell37cd6c22015-08-25 15:45:07 +01005755 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5756 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5757 .access = PL3_RW, .type = ARM_CP_CONST,
5758 .resetvalue = 0 },
5759 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5760 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5761 .access = PL3_RW, .type = ARM_CP_CONST,
5762 .resetvalue = 0 },
Peter Maydell43efaa32015-08-25 15:45:10 +01005763 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5764 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5765 .access = PL3_W, .type = ARM_CP_NO_RAW,
5766 .writefn = tlbi_aa64_alle3is_write },
5767 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5768 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5769 .access = PL3_W, .type = ARM_CP_NO_RAW,
5770 .writefn = tlbi_aa64_vae3is_write },
5771 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5772 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5773 .access = PL3_W, .type = ARM_CP_NO_RAW,
5774 .writefn = tlbi_aa64_vae3is_write },
5775 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5776 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5777 .access = PL3_W, .type = ARM_CP_NO_RAW,
5778 .writefn = tlbi_aa64_alle3_write },
5779 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5780 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5781 .access = PL3_W, .type = ARM_CP_NO_RAW,
5782 .writefn = tlbi_aa64_vae3_write },
5783 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5784 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5785 .access = PL3_W, .type = ARM_CP_NO_RAW,
5786 .writefn = tlbi_aa64_vae3_write },
Fabian Aggeler0f1a3b22014-12-11 12:07:49 +00005787 REGINFO_SENTINEL
5788};
5789
Richard Hendersone2cce182020-02-07 14:04:25 +00005790#ifndef CONFIG_USER_ONLY
5791/* Test if system register redirection is to occur in the current state. */
5792static bool redirect_for_e2h(CPUARMState *env)
5793{
5794 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5795}
5796
5797static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5798{
5799 CPReadFn *readfn;
5800
5801 if (redirect_for_e2h(env)) {
5802 /* Switch to the saved EL2 version of the register. */
5803 ri = ri->opaque;
5804 readfn = ri->readfn;
5805 } else {
5806 readfn = ri->orig_readfn;
5807 }
5808 if (readfn == NULL) {
5809 readfn = raw_read;
5810 }
5811 return readfn(env, ri);
5812}
5813
5814static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5815 uint64_t value)
5816{
5817 CPWriteFn *writefn;
5818
5819 if (redirect_for_e2h(env)) {
5820 /* Switch to the saved EL2 version of the register. */
5821 ri = ri->opaque;
5822 writefn = ri->writefn;
5823 } else {
5824 writefn = ri->orig_writefn;
5825 }
5826 if (writefn == NULL) {
5827 writefn = raw_write;
5828 }
5829 writefn(env, ri, value);
5830}
5831
5832static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5833{
5834 struct E2HAlias {
5835 uint32_t src_key, dst_key, new_key;
5836 const char *src_name, *dst_name, *new_name;
5837 bool (*feature)(const ARMISARegisters *id);
5838 };
5839
5840#define K(op0, op1, crn, crm, op2) \
5841 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5842
5843 static const struct E2HAlias aliases[] = {
5844 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
5845 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5846 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
5847 "CPACR", "CPTR_EL2", "CPACR_EL12" },
5848 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
5849 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5850 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
5851 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5852 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
5853 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5854 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
5855 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5856 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
5857 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5858 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
5859 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5860 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
5861 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5862 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
5863 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5864 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
5865 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5866 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5867 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5868 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5869 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5870 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5871 "VBAR", "VBAR_EL2", "VBAR_EL12" },
5872 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5873 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5874 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5875 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5876
5877 /*
5878 * Note that redirection of ZCR is mentioned in the description
5879 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5880 * not in the summary table.
5881 */
5882 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
5883 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5884
Richard Henderson4b779ce2020-06-25 20:31:05 -07005885 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
5886 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5887
Richard Hendersone2cce182020-02-07 14:04:25 +00005888 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5889 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5890 };
5891#undef K
5892
5893 size_t i;
5894
5895 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5896 const struct E2HAlias *a = &aliases[i];
5897 ARMCPRegInfo *src_reg, *dst_reg;
5898
5899 if (a->feature && !a->feature(&cpu->isar)) {
5900 continue;
5901 }
5902
5903 src_reg = g_hash_table_lookup(cpu->cp_regs, &a->src_key);
5904 dst_reg = g_hash_table_lookup(cpu->cp_regs, &a->dst_key);
5905 g_assert(src_reg != NULL);
5906 g_assert(dst_reg != NULL);
5907
5908 /* Cross-compare names to detect typos in the keys. */
5909 g_assert(strcmp(src_reg->name, a->src_name) == 0);
5910 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5911
5912 /* None of the core system registers use opaque; we will. */
5913 g_assert(src_reg->opaque == NULL);
5914
5915 /* Create alias before redirection so we dup the right data. */
5916 if (a->new_key) {
5917 ARMCPRegInfo *new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
5918 uint32_t *new_key = g_memdup(&a->new_key, sizeof(uint32_t));
5919 bool ok;
5920
5921 new_reg->name = a->new_name;
5922 new_reg->type |= ARM_CP_ALIAS;
5923 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
5924 new_reg->access &= PL2_RW | PL3_RW;
5925
5926 ok = g_hash_table_insert(cpu->cp_regs, new_key, new_reg);
5927 g_assert(ok);
5928 }
5929
5930 src_reg->opaque = dst_reg;
5931 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5932 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5933 if (!src_reg->raw_readfn) {
5934 src_reg->raw_readfn = raw_read;
5935 }
5936 if (!src_reg->raw_writefn) {
5937 src_reg->raw_writefn = raw_write;
5938 }
5939 src_reg->readfn = el2_e2h_read;
5940 src_reg->writefn = el2_e2h_write;
5941 }
5942}
5943#endif
5944
Peter Maydell3f208fd2016-02-11 11:17:31 +00005945static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5946 bool isread)
Peter Maydell7da845b2014-02-26 17:20:01 +00005947{
Richard Henderson97475a82020-02-07 14:04:25 +00005948 int cur_el = arm_current_el(env);
5949
5950 if (cur_el < 2) {
5951 uint64_t hcr = arm_hcr_el2_eff(env);
5952
5953 if (cur_el == 0) {
5954 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5955 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5956 return CP_ACCESS_TRAP_EL2;
5957 }
5958 } else {
5959 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5960 return CP_ACCESS_TRAP;
5961 }
5962 if (hcr & HCR_TID2) {
5963 return CP_ACCESS_TRAP_EL2;
5964 }
5965 }
5966 } else if (hcr & HCR_TID2) {
5967 return CP_ACCESS_TRAP_EL2;
5968 }
Peter Maydell7da845b2014-02-26 17:20:01 +00005969 }
Marc Zyngier630fcd42019-12-01 12:20:14 +00005970
5971 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5972 return CP_ACCESS_TRAP_EL2;
5973 }
5974
Peter Maydell7da845b2014-02-26 17:20:01 +00005975 return CP_ACCESS_OK;
5976}
5977
Davorin Mista1424ca82015-10-16 11:14:53 +01005978static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
5979 uint64_t value)
5980{
5981 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
5982 * read via a bit in OSLSR_EL1.
5983 */
5984 int oslock;
5985
5986 if (ri->state == ARM_CP_STATE_AA32) {
5987 oslock = (value == 0xC5ACCE55);
5988 } else {
5989 oslock = value & 1;
5990 }
5991
5992 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
5993}
5994
Peter Maydell50300692014-08-19 18:56:25 +01005995static const ARMCPRegInfo debug_cp_reginfo[] = {
Peter Maydell50300692014-08-19 18:56:25 +01005996 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
Peter Maydell10aae102014-08-19 18:56:25 +01005997 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
5998 * unlike DBGDRAR it is never accessible from EL0.
5999 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
6000 * accessor.
Peter Maydell50300692014-08-19 18:56:25 +01006001 */
6002 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
Peter Maydell91b0a232016-02-18 14:16:15 +00006003 .access = PL0_R, .accessfn = access_tdra,
6004 .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell10aae102014-08-19 18:56:25 +01006005 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
6006 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
Peter Maydell91b0a232016-02-18 14:16:15 +00006007 .access = PL1_R, .accessfn = access_tdra,
6008 .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell50300692014-08-19 18:56:25 +01006009 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
Peter Maydell91b0a232016-02-18 14:16:15 +00006010 .access = PL0_R, .accessfn = access_tdra,
6011 .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell17a9eb52014-09-12 14:06:49 +01006012 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
Peter Maydell10aae102014-08-19 18:56:25 +01006013 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
6014 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00006015 .access = PL1_RW, .accessfn = access_tda,
Peter Maydell0e5e8932014-08-19 18:56:27 +01006016 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
6017 .resetvalue = 0 },
Peter Maydell5e8b12f2014-09-12 14:06:50 +01006018 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
6019 * We don't implement the configurable EL0 access.
6020 */
6021 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
6022 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00006023 .type = ARM_CP_ALIAS,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00006024 .access = PL1_R, .accessfn = access_tda,
Sergey Fedorovb061a822015-06-19 14:17:44 +01006025 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
Peter Maydell10aae102014-08-19 18:56:25 +01006026 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
6027 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
Davorin Mista1424ca82015-10-16 11:14:53 +01006028 .access = PL1_W, .type = ARM_CP_NO_RAW,
Peter Maydell187f6782016-02-18 14:16:15 +00006029 .accessfn = access_tdosa,
Davorin Mista1424ca82015-10-16 11:14:53 +01006030 .writefn = oslar_write },
6031 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
6032 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
6033 .access = PL1_R, .resetvalue = 10,
Peter Maydell187f6782016-02-18 14:16:15 +00006034 .accessfn = access_tdosa,
Davorin Mista1424ca82015-10-16 11:14:53 +01006035 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
Peter Maydell5e8b12f2014-09-12 14:06:50 +01006036 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
6037 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
6038 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
Peter Maydell187f6782016-02-18 14:16:15 +00006039 .access = PL1_RW, .accessfn = access_tdosa,
6040 .type = ARM_CP_NOP },
Peter Maydell5e8b12f2014-09-12 14:06:50 +01006041 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
6042 * implement vector catch debug events yet.
6043 */
6044 { .name = "DBGVCR",
6045 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00006046 .access = PL1_RW, .accessfn = access_tda,
6047 .type = ARM_CP_NOP },
Peter Maydell4d2ec4d2017-01-20 11:15:07 +00006048 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
6049 * to save and restore a 32-bit guest's DBGVCR)
6050 */
6051 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
6052 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
6053 .access = PL2_RW, .accessfn = access_tda,
6054 .type = ARM_CP_NOP },
Peter Maydell5dbdc432016-10-12 18:54:33 +01006055 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
6056 * Channel but Linux may try to access this register. The 32-bit
6057 * alias is DBGDCCINT.
6058 */
6059 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
6060 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
6061 .access = PL1_RW, .accessfn = access_tda,
6062 .type = ARM_CP_NOP },
Peter Maydell50300692014-08-19 18:56:25 +01006063 REGINFO_SENTINEL
6064};
6065
6066static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
6067 /* 64 bit access versions of the (dummy) debug registers */
6068 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
6069 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6070 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
6071 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6072 REGINFO_SENTINEL
6073};
6074
Richard Henderson60eed082018-10-08 14:55:02 +01006075/* Return the exception level to which exceptions should be taken
6076 * via SVEAccessTrap. If an exception should be routed through
6077 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
6078 * take care of raising that exception.
6079 * C.f. the ARM pseudocode function CheckSVEEnabled.
Richard Henderson5be5e8e2018-01-22 19:53:48 -08006080 */
Richard Hendersonced31552018-10-08 14:55:03 +01006081int sve_exception_el(CPUARMState *env, int el)
Richard Henderson5be5e8e2018-01-22 19:53:48 -08006082{
6083#ifndef CONFIG_USER_ONLY
Richard Hendersonc2ddb7c2020-02-07 14:04:26 +00006084 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
6085
6086 if (el <= 1 && (hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
Richard Henderson60eed082018-10-08 14:55:02 +01006087 bool disabled = false;
6088
6089 /* The CPACR.ZEN controls traps to EL1:
6090 * 0, 2 : trap EL0 and EL1 accesses
6091 * 1 : trap only EL0 accesses
6092 * 3 : trap no accesses
6093 */
6094 if (!extract32(env->cp15.cpacr_el1, 16, 1)) {
6095 disabled = true;
6096 } else if (!extract32(env->cp15.cpacr_el1, 17, 1)) {
Richard Henderson2de7ace2018-10-08 14:55:02 +01006097 disabled = el == 0;
Richard Henderson60eed082018-10-08 14:55:02 +01006098 }
6099 if (disabled) {
6100 /* route_to_el2 */
Richard Hendersonc2ddb7c2020-02-07 14:04:26 +00006101 return hcr_el2 & HCR_TGE ? 2 : 1;
Richard Henderson60eed082018-10-08 14:55:02 +01006102 }
6103
6104 /* Check CPACR.FPEN. */
6105 if (!extract32(env->cp15.cpacr_el1, 20, 1)) {
6106 disabled = true;
6107 } else if (!extract32(env->cp15.cpacr_el1, 21, 1)) {
Richard Henderson2de7ace2018-10-08 14:55:02 +01006108 disabled = el == 0;
Richard Henderson60eed082018-10-08 14:55:02 +01006109 }
6110 if (disabled) {
6111 return 0;
6112 }
6113 }
6114
6115 /* CPTR_EL2. Since TZ and TFP are positive,
6116 * they will be zero when EL2 is not present.
Richard Henderson5be5e8e2018-01-22 19:53:48 -08006117 */
Richard Henderson2de7ace2018-10-08 14:55:02 +01006118 if (el <= 2 && !arm_is_secure_below_el3(env)) {
Richard Henderson60eed082018-10-08 14:55:02 +01006119 if (env->cp15.cptr_el[2] & CPTR_TZ) {
6120 return 2;
Richard Henderson5be5e8e2018-01-22 19:53:48 -08006121 }
Richard Henderson60eed082018-10-08 14:55:02 +01006122 if (env->cp15.cptr_el[2] & CPTR_TFP) {
6123 return 0;
Richard Henderson5be5e8e2018-01-22 19:53:48 -08006124 }
Richard Henderson5be5e8e2018-01-22 19:53:48 -08006125 }
6126
Richard Henderson60eed082018-10-08 14:55:02 +01006127 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6128 if (arm_feature(env, ARM_FEATURE_EL3)
6129 && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
Richard Henderson5be5e8e2018-01-22 19:53:48 -08006130 return 3;
6131 }
6132#endif
6133 return 0;
6134}
6135
Andrew Jones0df91422019-10-31 15:27:29 +01006136static uint32_t sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len)
6137{
Richard Henderson6e553f22019-11-19 13:20:27 +00006138 uint32_t end_len;
Andrew Jones0df91422019-10-31 15:27:29 +01006139
Richard Henderson6e553f22019-11-19 13:20:27 +00006140 end_len = start_len &= 0xf;
6141 if (!test_bit(start_len, cpu->sve_vq_map)) {
6142 end_len = find_last_bit(cpu->sve_vq_map, start_len);
6143 assert(end_len < start_len);
6144 }
6145 return end_len;
Andrew Jones0df91422019-10-31 15:27:29 +01006146}
6147
Richard Henderson0ab59532018-10-08 14:55:02 +01006148/*
6149 * Given that SVE is enabled, return the vector length for EL.
6150 */
Richard Hendersonced31552018-10-08 14:55:03 +01006151uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
Richard Henderson0ab59532018-10-08 14:55:02 +01006152{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07006153 ARMCPU *cpu = env_archcpu(env);
Richard Henderson0ab59532018-10-08 14:55:02 +01006154 uint32_t zcr_len = cpu->sve_max_vq - 1;
6155
6156 if (el <= 1) {
6157 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
6158 }
Richard Henderson6a02a732019-07-08 14:11:30 +01006159 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
Richard Henderson0ab59532018-10-08 14:55:02 +01006160 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
6161 }
Richard Henderson6a02a732019-07-08 14:11:30 +01006162 if (arm_feature(env, ARM_FEATURE_EL3)) {
Richard Henderson0ab59532018-10-08 14:55:02 +01006163 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
6164 }
Andrew Jones0df91422019-10-31 15:27:29 +01006165
6166 return sve_zcr_get_valid_len(cpu, zcr_len);
Richard Henderson0ab59532018-10-08 14:55:02 +01006167}
6168
Richard Henderson5be5e8e2018-01-22 19:53:48 -08006169static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6170 uint64_t value)
6171{
Richard Henderson0ab59532018-10-08 14:55:02 +01006172 int cur_el = arm_current_el(env);
6173 int old_len = sve_zcr_len_for_el(env, cur_el);
6174 int new_len;
6175
Richard Henderson5be5e8e2018-01-22 19:53:48 -08006176 /* Bits other than [3:0] are RAZ/WI. */
Andrew Jones7b351d92019-08-02 14:25:30 +02006177 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
Richard Henderson5be5e8e2018-01-22 19:53:48 -08006178 raw_write(env, ri, value & 0xf);
Richard Henderson0ab59532018-10-08 14:55:02 +01006179
6180 /*
6181 * Because we arrived here, we know both FP and SVE are enabled;
6182 * otherwise we would have trapped access to the ZCR_ELn register.
6183 */
6184 new_len = sve_zcr_len_for_el(env, cur_el);
6185 if (new_len < old_len) {
6186 aarch64_sve_narrow_vq(env, new_len + 1);
6187 }
Richard Henderson5be5e8e2018-01-22 19:53:48 -08006188}
6189
6190static const ARMCPRegInfo zcr_el1_reginfo = {
6191 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6192 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
Richard Henderson11d78702018-06-28 17:15:34 -07006193 .access = PL1_RW, .type = ARM_CP_SVE,
Richard Henderson5be5e8e2018-01-22 19:53:48 -08006194 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6195 .writefn = zcr_write, .raw_writefn = raw_write
6196};
6197
6198static const ARMCPRegInfo zcr_el2_reginfo = {
6199 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6200 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
Richard Henderson11d78702018-06-28 17:15:34 -07006201 .access = PL2_RW, .type = ARM_CP_SVE,
Richard Henderson5be5e8e2018-01-22 19:53:48 -08006202 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6203 .writefn = zcr_write, .raw_writefn = raw_write
6204};
6205
6206static const ARMCPRegInfo zcr_no_el2_reginfo = {
6207 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6208 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
Richard Henderson11d78702018-06-28 17:15:34 -07006209 .access = PL2_RW, .type = ARM_CP_SVE,
Richard Henderson5be5e8e2018-01-22 19:53:48 -08006210 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
6211};
6212
6213static const ARMCPRegInfo zcr_el3_reginfo = {
6214 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6215 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
Richard Henderson11d78702018-06-28 17:15:34 -07006216 .access = PL3_RW, .type = ARM_CP_SVE,
Richard Henderson5be5e8e2018-01-22 19:53:48 -08006217 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6218 .writefn = zcr_write, .raw_writefn = raw_write
6219};
6220
Peter Maydell9ee98ce2014-09-12 14:06:49 +01006221void hw_watchpoint_update(ARMCPU *cpu, int n)
6222{
6223 CPUARMState *env = &cpu->env;
6224 vaddr len = 0;
6225 vaddr wvr = env->cp15.dbgwvr[n];
6226 uint64_t wcr = env->cp15.dbgwcr[n];
6227 int mask;
6228 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
6229
6230 if (env->cpu_watchpoint[n]) {
6231 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
6232 env->cpu_watchpoint[n] = NULL;
6233 }
6234
6235 if (!extract64(wcr, 0, 1)) {
6236 /* E bit clear : watchpoint disabled */
6237 return;
6238 }
6239
6240 switch (extract64(wcr, 3, 2)) {
6241 case 0:
6242 /* LSC 00 is reserved and must behave as if the wp is disabled */
6243 return;
6244 case 1:
6245 flags |= BP_MEM_READ;
6246 break;
6247 case 2:
6248 flags |= BP_MEM_WRITE;
6249 break;
6250 case 3:
6251 flags |= BP_MEM_ACCESS;
6252 break;
6253 }
6254
6255 /* Attempts to use both MASK and BAS fields simultaneously are
6256 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
6257 * thus generating a watchpoint for every byte in the masked region.
6258 */
6259 mask = extract64(wcr, 24, 4);
6260 if (mask == 1 || mask == 2) {
6261 /* Reserved values of MASK; we must act as if the mask value was
6262 * some non-reserved value, or as if the watchpoint were disabled.
6263 * We choose the latter.
6264 */
6265 return;
6266 } else if (mask) {
6267 /* Watchpoint covers an aligned area up to 2GB in size */
6268 len = 1ULL << mask;
6269 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
6270 * whether the watchpoint fires when the unmasked bits match; we opt
6271 * to generate the exceptions.
6272 */
6273 wvr &= ~(len - 1);
6274 } else {
6275 /* Watchpoint covers bytes defined by the byte address select bits */
6276 int bas = extract64(wcr, 5, 8);
6277 int basstart;
6278
Peter Maydell9ee98ce2014-09-12 14:06:49 +01006279 if (extract64(wvr, 2, 1)) {
6280 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
6281 * ignored, and BAS[3:0] define which bytes to watch.
6282 */
6283 bas &= 0xf;
6284 }
Richard Hendersonae1111d2020-03-23 17:22:30 +00006285
6286 if (bas == 0) {
6287 /* This must act as if the watchpoint is disabled */
6288 return;
6289 }
6290
Peter Maydell9ee98ce2014-09-12 14:06:49 +01006291 /* The BAS bits are supposed to be programmed to indicate a contiguous
6292 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
6293 * we fire for each byte in the word/doubleword addressed by the WVR.
6294 * We choose to ignore any non-zero bits after the first range of 1s.
6295 */
6296 basstart = ctz32(bas);
6297 len = cto32(bas >> basstart);
6298 wvr += basstart;
6299 }
6300
6301 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
6302 &env->cpu_watchpoint[n]);
6303}
6304
6305void hw_watchpoint_update_all(ARMCPU *cpu)
6306{
6307 int i;
6308 CPUARMState *env = &cpu->env;
6309
6310 /* Completely clear out existing QEMU watchpoints and our array, to
6311 * avoid possible stale entries following migration load.
6312 */
6313 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
6314 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
6315
6316 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
6317 hw_watchpoint_update(cpu, i);
6318 }
6319}
6320
6321static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6322 uint64_t value)
6323{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07006324 ARMCPU *cpu = env_archcpu(env);
Peter Maydell9ee98ce2014-09-12 14:06:49 +01006325 int i = ri->crm;
6326
6327 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
6328 * register reads and behaves as if values written are sign extended.
6329 * Bits [1:0] are RES0.
6330 */
6331 value = sextract64(value, 0, 49) & ~3ULL;
6332
6333 raw_write(env, ri, value);
6334 hw_watchpoint_update(cpu, i);
6335}
6336
6337static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6338 uint64_t value)
6339{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07006340 ARMCPU *cpu = env_archcpu(env);
Peter Maydell9ee98ce2014-09-12 14:06:49 +01006341 int i = ri->crm;
6342
6343 raw_write(env, ri, value);
6344 hw_watchpoint_update(cpu, i);
6345}
6346
Peter Maydell46747d12014-09-29 18:48:46 +01006347void hw_breakpoint_update(ARMCPU *cpu, int n)
6348{
6349 CPUARMState *env = &cpu->env;
6350 uint64_t bvr = env->cp15.dbgbvr[n];
6351 uint64_t bcr = env->cp15.dbgbcr[n];
6352 vaddr addr;
6353 int bt;
6354 int flags = BP_CPU;
6355
6356 if (env->cpu_breakpoint[n]) {
6357 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
6358 env->cpu_breakpoint[n] = NULL;
6359 }
6360
6361 if (!extract64(bcr, 0, 1)) {
6362 /* E bit clear : watchpoint disabled */
6363 return;
6364 }
6365
6366 bt = extract64(bcr, 20, 4);
6367
6368 switch (bt) {
6369 case 4: /* unlinked address mismatch (reserved if AArch64) */
6370 case 5: /* linked address mismatch (reserved if AArch64) */
6371 qemu_log_mask(LOG_UNIMP,
Philippe Mathieu-Daudé0221c8f2018-06-08 13:15:33 +01006372 "arm: address mismatch breakpoint types not implemented\n");
Peter Maydell46747d12014-09-29 18:48:46 +01006373 return;
6374 case 0: /* unlinked address match */
6375 case 1: /* linked address match */
6376 {
6377 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
6378 * we behave as if the register was sign extended. Bits [1:0] are
6379 * RES0. The BAS field is used to allow setting breakpoints on 16
6380 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
6381 * a bp will fire if the addresses covered by the bp and the addresses
6382 * covered by the insn overlap but the insn doesn't start at the
6383 * start of the bp address range. We choose to require the insn and
6384 * the bp to have the same address. The constraints on writing to
6385 * BAS enforced in dbgbcr_write mean we have only four cases:
6386 * 0b0000 => no breakpoint
6387 * 0b0011 => breakpoint on addr
6388 * 0b1100 => breakpoint on addr + 2
6389 * 0b1111 => breakpoint on addr
6390 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
6391 */
6392 int bas = extract64(bcr, 5, 4);
6393 addr = sextract64(bvr, 0, 49) & ~3ULL;
6394 if (bas == 0) {
6395 return;
6396 }
6397 if (bas == 0xc) {
6398 addr += 2;
6399 }
6400 break;
6401 }
6402 case 2: /* unlinked context ID match */
6403 case 8: /* unlinked VMID match (reserved if no EL2) */
6404 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
6405 qemu_log_mask(LOG_UNIMP,
Philippe Mathieu-Daudé0221c8f2018-06-08 13:15:33 +01006406 "arm: unlinked context breakpoint types not implemented\n");
Peter Maydell46747d12014-09-29 18:48:46 +01006407 return;
6408 case 9: /* linked VMID match (reserved if no EL2) */
6409 case 11: /* linked context ID and VMID match (reserved if no EL2) */
6410 case 3: /* linked context ID match */
6411 default:
6412 /* We must generate no events for Linked context matches (unless
6413 * they are linked to by some other bp/wp, which is handled in
6414 * updates for the linking bp/wp). We choose to also generate no events
6415 * for reserved values.
6416 */
6417 return;
6418 }
6419
6420 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
6421}
6422
6423void hw_breakpoint_update_all(ARMCPU *cpu)
6424{
6425 int i;
6426 CPUARMState *env = &cpu->env;
6427
6428 /* Completely clear out existing QEMU breakpoints and our array, to
6429 * avoid possible stale entries following migration load.
6430 */
6431 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
6432 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
6433
6434 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
6435 hw_breakpoint_update(cpu, i);
6436 }
6437}
6438
6439static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6440 uint64_t value)
6441{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07006442 ARMCPU *cpu = env_archcpu(env);
Peter Maydell46747d12014-09-29 18:48:46 +01006443 int i = ri->crm;
6444
6445 raw_write(env, ri, value);
6446 hw_breakpoint_update(cpu, i);
6447}
6448
6449static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6450 uint64_t value)
6451{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07006452 ARMCPU *cpu = env_archcpu(env);
Peter Maydell46747d12014-09-29 18:48:46 +01006453 int i = ri->crm;
6454
6455 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
6456 * copy of BAS[0].
6457 */
6458 value = deposit64(value, 6, 1, extract64(value, 5, 1));
6459 value = deposit64(value, 8, 1, extract64(value, 7, 1));
6460
6461 raw_write(env, ri, value);
6462 hw_breakpoint_update(cpu, i);
6463}
6464
Peter Maydell50300692014-08-19 18:56:25 +01006465static void define_debug_regs(ARMCPU *cpu)
Peter Maydell0b454512014-02-26 17:20:05 +00006466{
Peter Maydell50300692014-08-19 18:56:25 +01006467 /* Define v7 and v8 architectural debug registers.
6468 * These are just dummy implementations for now.
Peter Maydell0b454512014-02-26 17:20:05 +00006469 */
6470 int i;
Peter Maydell3ff6fc92014-09-12 14:06:49 +01006471 int wrps, brps, ctx_cmps;
Peter Maydell48eb3ae2014-08-19 18:56:25 +01006472 ARMCPRegInfo dbgdidr = {
6473 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00006474 .access = PL0_R, .accessfn = access_tda,
Peter Maydell4426d362020-02-14 17:51:06 +00006475 .type = ARM_CP_CONST, .resetvalue = cpu->isar.dbgdidr,
Peter Maydell48eb3ae2014-08-19 18:56:25 +01006476 };
Peter Maydell0b454512014-02-26 17:20:05 +00006477
Peter Maydell3ff6fc92014-09-12 14:06:49 +01006478 /* Note that all these register fields hold "number of Xs minus 1". */
Peter Maydell88ce6c62020-02-14 17:51:05 +00006479 brps = arm_num_brps(cpu);
6480 wrps = arm_num_wrps(cpu);
6481 ctx_cmps = arm_num_ctx_cmps(cpu);
Peter Maydell3ff6fc92014-09-12 14:06:49 +01006482
6483 assert(ctx_cmps <= brps);
Peter Maydell48eb3ae2014-08-19 18:56:25 +01006484
Peter Maydell48eb3ae2014-08-19 18:56:25 +01006485 define_one_arm_cp_reg(cpu, &dbgdidr);
Peter Maydell50300692014-08-19 18:56:25 +01006486 define_arm_cp_regs(cpu, debug_cp_reginfo);
6487
6488 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
6489 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
6490 }
6491
Peter Maydell88ce6c62020-02-14 17:51:05 +00006492 for (i = 0; i < brps; i++) {
Peter Maydell0b454512014-02-26 17:20:05 +00006493 ARMCPRegInfo dbgregs[] = {
Peter Maydell10aae102014-08-19 18:56:25 +01006494 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
6495 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00006496 .access = PL1_RW, .accessfn = access_tda,
Peter Maydell46747d12014-09-29 18:48:46 +01006497 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
6498 .writefn = dbgbvr_write, .raw_writefn = raw_write
6499 },
Peter Maydell10aae102014-08-19 18:56:25 +01006500 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
6501 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00006502 .access = PL1_RW, .accessfn = access_tda,
Peter Maydell46747d12014-09-29 18:48:46 +01006503 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
6504 .writefn = dbgbcr_write, .raw_writefn = raw_write
6505 },
Peter Maydell48eb3ae2014-08-19 18:56:25 +01006506 REGINFO_SENTINEL
6507 };
6508 define_arm_cp_regs(cpu, dbgregs);
6509 }
6510
Peter Maydell88ce6c62020-02-14 17:51:05 +00006511 for (i = 0; i < wrps; i++) {
Peter Maydell48eb3ae2014-08-19 18:56:25 +01006512 ARMCPRegInfo dbgregs[] = {
Peter Maydell10aae102014-08-19 18:56:25 +01006513 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
6514 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00006515 .access = PL1_RW, .accessfn = access_tda,
Peter Maydell9ee98ce2014-09-12 14:06:49 +01006516 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
6517 .writefn = dbgwvr_write, .raw_writefn = raw_write
6518 },
Peter Maydell10aae102014-08-19 18:56:25 +01006519 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
6520 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00006521 .access = PL1_RW, .accessfn = access_tda,
Peter Maydell9ee98ce2014-09-12 14:06:49 +01006522 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
6523 .writefn = dbgwcr_write, .raw_writefn = raw_write
6524 },
6525 REGINFO_SENTINEL
Peter Maydell0b454512014-02-26 17:20:05 +00006526 };
6527 define_arm_cp_regs(cpu, dbgregs);
6528 }
6529}
6530
Peter Maydell24183fb2020-02-14 17:51:00 +00006531static void define_pmu_regs(ARMCPU *cpu)
6532{
6533 /*
6534 * v7 performance monitor control register: same implementor
6535 * field as main ID register, and we implement four counters in
6536 * addition to the cycle count register.
6537 */
6538 unsigned int i, pmcrn = 4;
6539 ARMCPRegInfo pmcr = {
6540 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6541 .access = PL0_RW,
6542 .type = ARM_CP_IO | ARM_CP_ALIAS,
6543 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6544 .accessfn = pmreg_access, .writefn = pmcr_write,
6545 .raw_writefn = raw_write,
6546 };
6547 ARMCPRegInfo pmcr64 = {
6548 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6549 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6550 .access = PL0_RW, .accessfn = pmreg_access,
6551 .type = ARM_CP_IO,
6552 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
Peter Maydell62d96ff2020-02-14 17:51:12 +00006553 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT) |
6554 PMCRLC,
Peter Maydell24183fb2020-02-14 17:51:00 +00006555 .writefn = pmcr_write, .raw_writefn = raw_write,
6556 };
6557 define_one_arm_cp_reg(cpu, &pmcr);
6558 define_one_arm_cp_reg(cpu, &pmcr64);
6559 for (i = 0; i < pmcrn; i++) {
6560 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6561 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6562 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6563 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6564 ARMCPRegInfo pmev_regs[] = {
6565 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6566 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6567 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6568 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6569 .accessfn = pmreg_access },
6570 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6571 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6572 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6573 .type = ARM_CP_IO,
6574 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6575 .raw_readfn = pmevcntr_rawread,
6576 .raw_writefn = pmevcntr_rawwrite },
6577 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6578 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6579 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6580 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6581 .accessfn = pmreg_access },
6582 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6583 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6584 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6585 .type = ARM_CP_IO,
6586 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6587 .raw_writefn = pmevtyper_rawwrite },
6588 REGINFO_SENTINEL
6589 };
6590 define_arm_cp_regs(cpu, pmev_regs);
6591 g_free(pmevcntr_name);
6592 g_free(pmevcntr_el0_name);
6593 g_free(pmevtyper_name);
6594 g_free(pmevtyper_el0_name);
6595 }
Peter Maydella6179532020-02-14 17:51:03 +00006596 if (cpu_isar_feature(aa32_pmu_8_1, cpu)) {
Peter Maydell24183fb2020-02-14 17:51:00 +00006597 ARMCPRegInfo v81_pmu_regs[] = {
6598 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6599 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6600 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6601 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6602 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6603 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6604 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6605 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6606 REGINFO_SENTINEL
6607 };
6608 define_arm_cp_regs(cpu, v81_pmu_regs);
6609 }
Peter Maydell15dd1eb2020-02-14 17:51:09 +00006610 if (cpu_isar_feature(any_pmu_8_4, cpu)) {
6611 static const ARMCPRegInfo v84_pmmir = {
6612 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6613 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6614 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6615 .resetvalue = 0
6616 };
6617 define_one_arm_cp_reg(cpu, &v84_pmmir);
6618 }
Peter Maydell24183fb2020-02-14 17:51:00 +00006619}
6620
Peter Maydell96a8b922017-11-07 15:01:38 +00006621/* We don't know until after realize whether there's a GICv3
6622 * attached, and that is what registers the gicv3 sysregs.
6623 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6624 * at runtime.
6625 */
6626static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6627{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07006628 ARMCPU *cpu = env_archcpu(env);
Peter Maydell96a8b922017-11-07 15:01:38 +00006629 uint64_t pfr1 = cpu->id_pfr1;
6630
6631 if (env->gicv3state) {
6632 pfr1 |= 1 << 28;
6633 }
6634 return pfr1;
6635}
6636
Alex Bennée976b99b2020-03-16 17:21:46 +00006637#ifndef CONFIG_USER_ONLY
Peter Maydell96a8b922017-11-07 15:01:38 +00006638static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6639{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07006640 ARMCPU *cpu = env_archcpu(env);
Richard Henderson47576b92018-10-24 07:50:16 +01006641 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
Peter Maydell96a8b922017-11-07 15:01:38 +00006642
6643 if (env->gicv3state) {
6644 pfr0 |= 1 << 24;
6645 }
6646 return pfr0;
6647}
Alex Bennée976b99b2020-03-16 17:21:46 +00006648#endif
Peter Maydell96a8b922017-11-07 15:01:38 +00006649
Richard Henderson2d7137c2018-12-13 13:48:08 +00006650/* Shared logic between LORID and the rest of the LOR* registers.
6651 * Secure state has already been delt with.
6652 */
6653static CPAccessResult access_lor_ns(CPUARMState *env)
6654{
6655 int el = arm_current_el(env);
6656
6657 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6658 return CP_ACCESS_TRAP_EL2;
6659 }
6660 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6661 return CP_ACCESS_TRAP_EL3;
6662 }
6663 return CP_ACCESS_OK;
6664}
6665
6666static CPAccessResult access_lorid(CPUARMState *env, const ARMCPRegInfo *ri,
6667 bool isread)
6668{
6669 if (arm_is_secure_below_el3(env)) {
6670 /* Access ok in secure mode. */
6671 return CP_ACCESS_OK;
6672 }
6673 return access_lor_ns(env);
6674}
6675
6676static CPAccessResult access_lor_other(CPUARMState *env,
6677 const ARMCPRegInfo *ri, bool isread)
6678{
6679 if (arm_is_secure_below_el3(env)) {
6680 /* Access denied in secure mode. */
6681 return CP_ACCESS_TRAP;
6682 }
6683 return access_lor_ns(env);
6684}
6685
Richard Hendersond8564ee2020-02-08 12:58:00 +00006686/*
6687 * A trivial implementation of ARMv8.1-LOR leaves all of these
6688 * registers fixed at 0, which indicates that there are zero
6689 * supported Limited Ordering regions.
6690 */
6691static const ARMCPRegInfo lor_reginfo[] = {
6692 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6693 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6694 .access = PL1_RW, .accessfn = access_lor_other,
6695 .type = ARM_CP_CONST, .resetvalue = 0 },
6696 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6697 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6698 .access = PL1_RW, .accessfn = access_lor_other,
6699 .type = ARM_CP_CONST, .resetvalue = 0 },
6700 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6701 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6702 .access = PL1_RW, .accessfn = access_lor_other,
6703 .type = ARM_CP_CONST, .resetvalue = 0 },
6704 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6705 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6706 .access = PL1_RW, .accessfn = access_lor_other,
6707 .type = ARM_CP_CONST, .resetvalue = 0 },
6708 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6709 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
6710 .access = PL1_R, .accessfn = access_lorid,
6711 .type = ARM_CP_CONST, .resetvalue = 0 },
6712 REGINFO_SENTINEL
6713};
6714
Richard Henderson967aa942019-01-21 10:23:13 +00006715#ifdef TARGET_AARCH64
6716static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6717 bool isread)
6718{
6719 int el = arm_current_el(env);
6720
6721 if (el < 2 &&
6722 arm_feature(env, ARM_FEATURE_EL2) &&
6723 !(arm_hcr_el2_eff(env) & HCR_APK)) {
6724 return CP_ACCESS_TRAP_EL2;
6725 }
6726 if (el < 3 &&
6727 arm_feature(env, ARM_FEATURE_EL3) &&
6728 !(env->cp15.scr_el3 & SCR_APK)) {
6729 return CP_ACCESS_TRAP_EL3;
6730 }
6731 return CP_ACCESS_OK;
6732}
6733
6734static const ARMCPRegInfo pauth_reginfo[] = {
6735 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6736 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6737 .access = PL1_RW, .accessfn = access_pauth,
Richard Henderson108b3ba2019-03-14 17:28:32 -07006738 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
Richard Henderson967aa942019-01-21 10:23:13 +00006739 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6740 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6741 .access = PL1_RW, .accessfn = access_pauth,
Richard Henderson108b3ba2019-03-14 17:28:32 -07006742 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
Richard Henderson967aa942019-01-21 10:23:13 +00006743 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6744 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6745 .access = PL1_RW, .accessfn = access_pauth,
Richard Henderson108b3ba2019-03-14 17:28:32 -07006746 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
Richard Henderson967aa942019-01-21 10:23:13 +00006747 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6748 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6749 .access = PL1_RW, .accessfn = access_pauth,
Richard Henderson108b3ba2019-03-14 17:28:32 -07006750 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
Richard Henderson967aa942019-01-21 10:23:13 +00006751 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6752 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6753 .access = PL1_RW, .accessfn = access_pauth,
Richard Henderson108b3ba2019-03-14 17:28:32 -07006754 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
Richard Henderson967aa942019-01-21 10:23:13 +00006755 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6756 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6757 .access = PL1_RW, .accessfn = access_pauth,
Richard Henderson108b3ba2019-03-14 17:28:32 -07006758 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
Richard Henderson967aa942019-01-21 10:23:13 +00006759 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6760 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6761 .access = PL1_RW, .accessfn = access_pauth,
Richard Henderson108b3ba2019-03-14 17:28:32 -07006762 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
Richard Henderson967aa942019-01-21 10:23:13 +00006763 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6764 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6765 .access = PL1_RW, .accessfn = access_pauth,
Richard Henderson108b3ba2019-03-14 17:28:32 -07006766 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
Richard Henderson967aa942019-01-21 10:23:13 +00006767 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6768 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6769 .access = PL1_RW, .accessfn = access_pauth,
Richard Henderson108b3ba2019-03-14 17:28:32 -07006770 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
Richard Henderson967aa942019-01-21 10:23:13 +00006771 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6772 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6773 .access = PL1_RW, .accessfn = access_pauth,
Richard Henderson108b3ba2019-03-14 17:28:32 -07006774 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
Richard Henderson967aa942019-01-21 10:23:13 +00006775 REGINFO_SENTINEL
6776};
Richard Hendersonde390642019-03-12 21:57:35 -07006777
6778static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6779{
6780 Error *err = NULL;
6781 uint64_t ret;
6782
6783 /* Success sets NZCV = 0000. */
6784 env->NF = env->CF = env->VF = 0, env->ZF = 1;
6785
6786 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6787 /*
6788 * ??? Failed, for unknown reasons in the crypto subsystem.
6789 * The best we can do is log the reason and return the
6790 * timed-out indication to the guest. There is no reason
6791 * we know to expect this failure to be transitory, so the
6792 * guest may well hang retrying the operation.
6793 */
6794 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6795 ri->name, error_get_pretty(err));
6796 error_free(err);
6797
6798 env->ZF = 0; /* NZCF = 0100 */
6799 return 0;
6800 }
6801 return ret;
6802}
6803
6804/* We do not support re-seeding, so the two registers operate the same. */
6805static const ARMCPRegInfo rndr_reginfo[] = {
6806 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6807 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6808 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6809 .access = PL0_R, .readfn = rndr_readfn },
6810 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6811 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6812 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6813 .access = PL0_R, .readfn = rndr_readfn },
6814 REGINFO_SENTINEL
6815};
Beata Michalska0d57b492019-11-21 00:08:43 +00006816
6817#ifndef CONFIG_USER_ONLY
6818static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6819 uint64_t value)
6820{
6821 ARMCPU *cpu = env_archcpu(env);
6822 /* CTR_EL0 System register -> DminLine, bits [19:16] */
6823 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6824 uint64_t vaddr_in = (uint64_t) value;
6825 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6826 void *haddr;
6827 int mem_idx = cpu_mmu_index(env, false);
6828
6829 /* This won't be crossing page boundaries */
6830 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6831 if (haddr) {
6832
6833 ram_addr_t offset;
6834 MemoryRegion *mr;
6835
6836 /* RCU lock is already being held */
6837 mr = memory_region_from_host(haddr, &offset);
6838
6839 if (mr) {
Philippe Mathieu-Daudé4dfe59d2020-05-08 08:24:53 +02006840 memory_region_writeback(mr, offset, dline_size);
Beata Michalska0d57b492019-11-21 00:08:43 +00006841 }
6842 }
6843}
6844
6845static const ARMCPRegInfo dcpop_reg[] = {
6846 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6847 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6848 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
Richard Henderson1bed4d22020-03-05 16:09:18 +00006849 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
Beata Michalska0d57b492019-11-21 00:08:43 +00006850 REGINFO_SENTINEL
6851};
6852
6853static const ARMCPRegInfo dcpodp_reg[] = {
6854 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6855 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6856 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
Richard Henderson1bed4d22020-03-05 16:09:18 +00006857 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
Beata Michalska0d57b492019-11-21 00:08:43 +00006858 REGINFO_SENTINEL
6859};
6860#endif /*CONFIG_USER_ONLY*/
6861
Richard Henderson4b779ce2020-06-25 20:31:05 -07006862static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
6863 bool isread)
6864{
6865 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
6866 return CP_ACCESS_TRAP_EL2;
6867 }
6868
6869 return CP_ACCESS_OK;
6870}
6871
6872static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
6873 bool isread)
6874{
6875 int el = arm_current_el(env);
6876
6877 if (el < 2 &&
6878 arm_feature(env, ARM_FEATURE_EL2) &&
6879 !(arm_hcr_el2_eff(env) & HCR_ATA)) {
6880 return CP_ACCESS_TRAP_EL2;
6881 }
6882 if (el < 3 &&
6883 arm_feature(env, ARM_FEATURE_EL3) &&
6884 !(env->cp15.scr_el3 & SCR_ATA)) {
6885 return CP_ACCESS_TRAP_EL3;
6886 }
6887 return CP_ACCESS_OK;
6888}
6889
6890static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
6891{
6892 return env->pstate & PSTATE_TCO;
6893}
6894
6895static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6896{
6897 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
6898}
6899
6900static const ARMCPRegInfo mte_reginfo[] = {
6901 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
6902 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
6903 .access = PL1_RW, .accessfn = access_mte,
6904 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
6905 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
6906 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
6907 .access = PL1_RW, .accessfn = access_mte,
6908 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
6909 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
6910 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
6911 .access = PL2_RW, .accessfn = access_mte,
6912 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
6913 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
6914 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
6915 .access = PL3_RW,
6916 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
6917 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
6918 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
6919 .access = PL1_RW, .accessfn = access_mte,
6920 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
6921 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
6922 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
6923 .access = PL1_RW, .accessfn = access_mte,
6924 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
6925 { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
6926 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
6927 .access = PL1_R, .accessfn = access_aa64_tid5,
6928 .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
6929 { .name = "TCO", .state = ARM_CP_STATE_AA64,
6930 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
6931 .type = ARM_CP_NO_RAW,
6932 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
Richard Henderson5463df12020-06-25 20:31:18 -07006933 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
6934 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
6935 .type = ARM_CP_NOP, .access = PL1_W,
6936 .accessfn = aa64_cacheop_poc_access },
6937 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
6938 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
6939 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6940 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
6941 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
6942 .type = ARM_CP_NOP, .access = PL1_W,
6943 .accessfn = aa64_cacheop_poc_access },
6944 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
6945 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
6946 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6947 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
6948 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
6949 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6950 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
6951 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
6952 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6953 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
6954 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
6955 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6956 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
6957 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
6958 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
Richard Henderson4b779ce2020-06-25 20:31:05 -07006959 REGINFO_SENTINEL
6960};
6961
6962static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
6963 { .name = "TCO", .state = ARM_CP_STATE_AA64,
6964 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
6965 .type = ARM_CP_CONST, .access = PL0_RW, },
6966 REGINFO_SENTINEL
6967};
Richard Henderson5463df12020-06-25 20:31:18 -07006968
6969static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
6970 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
6971 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
6972 .type = ARM_CP_NOP, .access = PL0_W,
6973 .accessfn = aa64_cacheop_poc_access },
6974 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
6975 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
6976 .type = ARM_CP_NOP, .access = PL0_W,
6977 .accessfn = aa64_cacheop_poc_access },
6978 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
6979 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
6980 .type = ARM_CP_NOP, .access = PL0_W,
6981 .accessfn = aa64_cacheop_poc_access },
6982 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
6983 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
6984 .type = ARM_CP_NOP, .access = PL0_W,
6985 .accessfn = aa64_cacheop_poc_access },
6986 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
6987 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
6988 .type = ARM_CP_NOP, .access = PL0_W,
6989 .accessfn = aa64_cacheop_poc_access },
6990 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
6991 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
6992 .type = ARM_CP_NOP, .access = PL0_W,
6993 .accessfn = aa64_cacheop_poc_access },
6994 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
6995 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
6996 .type = ARM_CP_NOP, .access = PL0_W,
6997 .accessfn = aa64_cacheop_poc_access },
6998 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
6999 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7000 .type = ARM_CP_NOP, .access = PL0_W,
7001 .accessfn = aa64_cacheop_poc_access },
Richard Hendersoneb821162020-06-25 20:31:37 -07007002 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7003 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7004 .access = PL0_W, .type = ARM_CP_DC_GVA,
7005#ifndef CONFIG_USER_ONLY
7006 /* Avoid overhead of an access check that always passes in user-mode */
7007 .accessfn = aa64_zva_access,
7008#endif
7009 },
7010 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7011 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7012 .access = PL0_W, .type = ARM_CP_DC_GZVA,
7013#ifndef CONFIG_USER_ONLY
7014 /* Avoid overhead of an access check that always passes in user-mode */
7015 .accessfn = aa64_zva_access,
7016#endif
7017 },
Richard Henderson5463df12020-06-25 20:31:18 -07007018 REGINFO_SENTINEL
7019};
7020
Richard Henderson967aa942019-01-21 10:23:13 +00007021#endif
7022
Richard Hendersoncb570bd2019-03-01 12:04:54 -08007023static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7024 bool isread)
7025{
7026 int el = arm_current_el(env);
7027
7028 if (el == 0) {
7029 uint64_t sctlr = arm_sctlr(env, el);
7030 if (!(sctlr & SCTLR_EnRCTX)) {
7031 return CP_ACCESS_TRAP;
7032 }
7033 } else if (el == 1) {
7034 uint64_t hcr = arm_hcr_el2_eff(env);
7035 if (hcr & HCR_NV) {
7036 return CP_ACCESS_TRAP_EL2;
7037 }
7038 }
7039 return CP_ACCESS_OK;
7040}
7041
7042static const ARMCPRegInfo predinv_reginfo[] = {
7043 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7044 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7045 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7046 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7047 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7048 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7049 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7050 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7051 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7052 /*
7053 * Note the AArch32 opcodes have a different OPC1.
7054 */
7055 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7056 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7057 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7058 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7059 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7060 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7061 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7062 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7063 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7064 REGINFO_SENTINEL
7065};
7066
Peter Maydell957e6152020-02-24 18:26:26 +00007067static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7068{
7069 /* Read the high 32 bits of the current CCSIDR */
7070 return extract64(ccsidr_read(env, ri), 32, 32);
7071}
7072
7073static const ARMCPRegInfo ccsidr2_reginfo[] = {
7074 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7075 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7076 .access = PL1_R,
7077 .accessfn = access_aa64_tid2,
7078 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7079 REGINFO_SENTINEL
7080};
7081
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007082static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7083 bool isread)
7084{
7085 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7086 return CP_ACCESS_TRAP_EL2;
7087 }
7088
7089 return CP_ACCESS_OK;
7090}
7091
7092static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7093 bool isread)
7094{
7095 if (arm_feature(env, ARM_FEATURE_V8)) {
7096 return access_aa64_tid3(env, ri, isread);
7097 }
7098
7099 return CP_ACCESS_OK;
7100}
7101
Marc Zyngierf96f3d52019-12-01 12:20:18 +00007102static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7103 bool isread)
7104{
7105 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7106 return CP_ACCESS_TRAP_EL2;
7107 }
7108
7109 return CP_ACCESS_OK;
7110}
7111
7112static const ARMCPRegInfo jazelle_regs[] = {
7113 { .name = "JIDR",
7114 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7115 .access = PL1_R, .accessfn = access_jazelle,
7116 .type = ARM_CP_CONST, .resetvalue = 0 },
7117 { .name = "JOSCR",
7118 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7119 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7120 { .name = "JMCR",
7121 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7122 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7123 REGINFO_SENTINEL
7124};
7125
Richard Hendersone2a1a462020-02-07 14:04:21 +00007126static const ARMCPRegInfo vhe_reginfo[] = {
7127 { .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7128 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7129 .access = PL2_RW,
7130 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2]) },
Richard Hendersoned30da82020-02-07 14:04:21 +00007131 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7132 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7133 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7134 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
Richard Henderson8c94b072020-02-07 14:04:25 +00007135#ifndef CONFIG_USER_ONLY
7136 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7137 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7138 .fieldoffset =
7139 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7140 .type = ARM_CP_IO, .access = PL2_RW,
7141 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7142 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7143 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7144 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7145 .resetfn = gt_hv_timer_reset,
7146 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7147 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7148 .type = ARM_CP_IO,
7149 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7150 .access = PL2_RW,
7151 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7152 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
Richard Hendersonbb5972e2020-02-07 14:04:25 +00007153 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7154 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7155 .type = ARM_CP_IO | ARM_CP_ALIAS,
7156 .access = PL2_RW, .accessfn = e2h_access,
7157 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7158 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7159 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7160 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7161 .type = ARM_CP_IO | ARM_CP_ALIAS,
7162 .access = PL2_RW, .accessfn = e2h_access,
7163 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7164 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7165 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7166 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7167 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7168 .access = PL2_RW, .accessfn = e2h_access,
7169 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7170 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7171 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7172 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7173 .access = PL2_RW, .accessfn = e2h_access,
7174 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7175 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7176 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7177 .type = ARM_CP_IO | ARM_CP_ALIAS,
7178 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7179 .access = PL2_RW, .accessfn = e2h_access,
7180 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7181 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7182 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7183 .type = ARM_CP_IO | ARM_CP_ALIAS,
7184 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7185 .access = PL2_RW, .accessfn = e2h_access,
7186 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
Richard Henderson8c94b072020-02-07 14:04:25 +00007187#endif
Richard Hendersone2a1a462020-02-07 14:04:21 +00007188 REGINFO_SENTINEL
7189};
7190
Richard Henderson04b07d22020-02-08 12:58:11 +00007191#ifndef CONFIG_USER_ONLY
7192static const ARMCPRegInfo ats1e1_reginfo[] = {
7193 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7194 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7195 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7196 .writefn = ats_write64 },
7197 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7198 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7199 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7200 .writefn = ats_write64 },
7201 REGINFO_SENTINEL
7202};
7203
7204static const ARMCPRegInfo ats1cp_reginfo[] = {
7205 { .name = "ATS1CPRP",
7206 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7207 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7208 .writefn = ats_write },
7209 { .name = "ATS1CPWP",
7210 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7211 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7212 .writefn = ats_write },
7213 REGINFO_SENTINEL
7214};
7215#endif
7216
Peter Maydellf6287c22020-02-14 17:51:16 +00007217/*
7218 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7219 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7220 * is non-zero, which is never for ARMv7, optionally in ARMv8
7221 * and mandatorily for ARMv8.2 and up.
7222 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7223 * implementation is RAZ/WI we can ignore this detail, as we
7224 * do for ACTLR.
7225 */
7226static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7227 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7228 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
Richard Henderson99602372020-03-05 16:09:18 +00007229 .access = PL1_RW, .accessfn = access_tacr,
7230 .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydellf6287c22020-02-14 17:51:16 +00007231 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7232 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7233 .access = PL2_RW, .type = ARM_CP_CONST,
7234 .resetvalue = 0 },
7235 REGINFO_SENTINEL
7236};
7237
Peter Maydell2ceb98c2012-06-20 11:57:09 +00007238void register_cp_regs_for_features(ARMCPU *cpu)
7239{
7240 /* Register all the coprocessor registers based on feature bits */
7241 CPUARMState *env = &cpu->env;
7242 if (arm_feature(env, ARM_FEATURE_M)) {
7243 /* M profile has no coprocessor registers */
7244 return;
7245 }
7246
Peter Maydelle9aa6c22012-06-20 11:57:09 +00007247 define_arm_cp_regs(cpu, cp_reginfo);
Peter Maydell9449fdf2014-04-15 19:18:47 +01007248 if (!arm_feature(env, ARM_FEATURE_V8)) {
7249 /* Must go early as it is full of wildcards that may be
7250 * overridden by later definitions.
7251 */
7252 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7253 }
7254
Peter Maydell7d57f402012-06-20 11:57:11 +00007255 if (arm_feature(env, ARM_FEATURE_V6)) {
Peter Maydell8515a092012-06-20 11:57:19 +00007256 /* The ID registers all have impdef reset values */
7257 ARMCPRegInfo v6_idregs[] = {
Peter Maydell0ff644a2014-04-15 19:18:45 +01007258 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7259 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7260 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007261 .accessfn = access_aa32_tid3,
Peter Maydell8515a092012-06-20 11:57:19 +00007262 .resetvalue = cpu->id_pfr0 },
Peter Maydell96a8b922017-11-07 15:01:38 +00007263 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7264 * the value of the GIC field until after we define these regs.
7265 */
Peter Maydell0ff644a2014-04-15 19:18:45 +01007266 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7267 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
Peter Maydell96a8b922017-11-07 15:01:38 +00007268 .access = PL1_R, .type = ARM_CP_NO_RAW,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007269 .accessfn = access_aa32_tid3,
Peter Maydell96a8b922017-11-07 15:01:38 +00007270 .readfn = id_pfr1_read,
7271 .writefn = arm_cp_write_ignore },
Peter Maydell0ff644a2014-04-15 19:18:45 +01007272 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7273 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7274 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007275 .accessfn = access_aa32_tid3,
Peter Maydella6179532020-02-14 17:51:03 +00007276 .resetvalue = cpu->isar.id_dfr0 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01007277 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7278 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7279 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007280 .accessfn = access_aa32_tid3,
Peter Maydell8515a092012-06-20 11:57:19 +00007281 .resetvalue = cpu->id_afr0 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01007282 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7283 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7284 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007285 .accessfn = access_aa32_tid3,
Peter Maydell10054012020-02-14 17:51:13 +00007286 .resetvalue = cpu->isar.id_mmfr0 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01007287 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7288 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7289 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007290 .accessfn = access_aa32_tid3,
Peter Maydell10054012020-02-14 17:51:13 +00007291 .resetvalue = cpu->isar.id_mmfr1 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01007292 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7293 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7294 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007295 .accessfn = access_aa32_tid3,
Peter Maydell10054012020-02-14 17:51:13 +00007296 .resetvalue = cpu->isar.id_mmfr2 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01007297 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7298 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7299 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007300 .accessfn = access_aa32_tid3,
Peter Maydell10054012020-02-14 17:51:13 +00007301 .resetvalue = cpu->isar.id_mmfr3 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01007302 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7303 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7304 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007305 .accessfn = access_aa32_tid3,
Richard Henderson47576b92018-10-24 07:50:16 +01007306 .resetvalue = cpu->isar.id_isar0 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01007307 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7308 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7309 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007310 .accessfn = access_aa32_tid3,
Richard Henderson47576b92018-10-24 07:50:16 +01007311 .resetvalue = cpu->isar.id_isar1 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01007312 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7313 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7314 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007315 .accessfn = access_aa32_tid3,
Richard Henderson47576b92018-10-24 07:50:16 +01007316 .resetvalue = cpu->isar.id_isar2 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01007317 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7318 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7319 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007320 .accessfn = access_aa32_tid3,
Richard Henderson47576b92018-10-24 07:50:16 +01007321 .resetvalue = cpu->isar.id_isar3 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01007322 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7323 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7324 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007325 .accessfn = access_aa32_tid3,
Richard Henderson47576b92018-10-24 07:50:16 +01007326 .resetvalue = cpu->isar.id_isar4 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01007327 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7328 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7329 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007330 .accessfn = access_aa32_tid3,
Richard Henderson47576b92018-10-24 07:50:16 +01007331 .resetvalue = cpu->isar.id_isar5 },
Peter Maydelle20d84c2016-02-19 14:07:43 +00007332 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7333 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7334 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007335 .accessfn = access_aa32_tid3,
Peter Maydell10054012020-02-14 17:51:13 +00007336 .resetvalue = cpu->isar.id_mmfr4 },
Richard Henderson802abf42018-06-28 17:15:37 -07007337 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007338 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7339 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007340 .accessfn = access_aa32_tid3,
Richard Henderson47576b92018-10-24 07:50:16 +01007341 .resetvalue = cpu->isar.id_isar6 },
Peter Maydell8515a092012-06-20 11:57:19 +00007342 REGINFO_SENTINEL
7343 };
7344 define_arm_cp_regs(cpu, v6_idregs);
Peter Maydell7d57f402012-06-20 11:57:11 +00007345 define_arm_cp_regs(cpu, v6_cp_reginfo);
7346 } else {
7347 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7348 }
Peter Maydell4d31c592012-06-20 11:57:11 +00007349 if (arm_feature(env, ARM_FEATURE_V6K)) {
7350 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7351 }
Peter Crosthwaite5e5cf9e2015-06-15 18:06:10 +01007352 if (arm_feature(env, ARM_FEATURE_V7MP) &&
Peter Maydell452a0952017-06-02 11:51:47 +01007353 !arm_feature(env, ARM_FEATURE_PMSA)) {
Peter Maydell995939a2014-09-12 14:06:50 +01007354 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7355 }
Aaron Lindsay327dd512019-01-21 10:23:14 +00007356 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7357 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7358 }
Peter Maydelle9aa6c22012-06-20 11:57:09 +00007359 if (arm_feature(env, ARM_FEATURE_V7)) {
Peter Maydell776d4e52012-06-20 11:57:19 +00007360 ARMCPRegInfo clidr = {
Peter Maydell7da845b2014-02-26 17:20:01 +00007361 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7362 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
Marc Zyngier630fcd42019-12-01 12:20:14 +00007363 .access = PL1_R, .type = ARM_CP_CONST,
7364 .accessfn = access_aa64_tid2,
7365 .resetvalue = cpu->clidr
Peter Maydell776d4e52012-06-20 11:57:19 +00007366 };
Peter Maydell776d4e52012-06-20 11:57:19 +00007367 define_one_arm_cp_reg(cpu, &clidr);
Peter Maydelle9aa6c22012-06-20 11:57:09 +00007368 define_arm_cp_regs(cpu, v7_cp_reginfo);
Peter Maydell50300692014-08-19 18:56:25 +01007369 define_debug_regs(cpu);
Peter Maydell24183fb2020-02-14 17:51:00 +00007370 define_pmu_regs(cpu);
Peter Maydell7d57f402012-06-20 11:57:11 +00007371 } else {
7372 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
Peter Maydelle9aa6c22012-06-20 11:57:09 +00007373 }
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00007374 if (arm_feature(env, ARM_FEATURE_V8)) {
Peter Maydelle20d84c2016-02-19 14:07:43 +00007375 /* AArch64 ID registers, which all have impdef reset values.
7376 * Note that within the ID register ranges the unused slots
7377 * must all RAZ, not UNDEF; future architecture versions may
7378 * define new registers here.
7379 */
Peter Maydelle60cef82014-02-26 17:20:05 +00007380 ARMCPRegInfo v8_idregs[] = {
Alex Bennée976b99b2020-03-16 17:21:46 +00007381 /*
7382 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7383 * emulation because we don't know the right value for the
7384 * GIC field until after we define these regs.
Peter Maydell96a8b922017-11-07 15:01:38 +00007385 */
Peter Maydelle60cef82014-02-26 17:20:05 +00007386 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7387 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
Alex Bennée976b99b2020-03-16 17:21:46 +00007388 .access = PL1_R,
7389#ifdef CONFIG_USER_ONLY
7390 .type = ARM_CP_CONST,
7391 .resetvalue = cpu->isar.id_aa64pfr0
7392#else
7393 .type = ARM_CP_NO_RAW,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007394 .accessfn = access_aa64_tid3,
Peter Maydell96a8b922017-11-07 15:01:38 +00007395 .readfn = id_aa64pfr0_read,
Alex Bennée976b99b2020-03-16 17:21:46 +00007396 .writefn = arm_cp_write_ignore
7397#endif
7398 },
Peter Maydelle60cef82014-02-26 17:20:05 +00007399 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7400 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7401 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007402 .accessfn = access_aa64_tid3,
Richard Henderson47576b92018-10-24 07:50:16 +01007403 .resetvalue = cpu->isar.id_aa64pfr1},
Peter Maydelle20d84c2016-02-19 14:07:43 +00007404 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7405 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7406 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007407 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007408 .resetvalue = 0 },
7409 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7410 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7411 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007412 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007413 .resetvalue = 0 },
Richard Henderson9516d772018-10-08 14:55:02 +01007414 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007415 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7416 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007417 .accessfn = access_aa64_tid3,
Richard Henderson9516d772018-10-08 14:55:02 +01007418 /* At present, only SVEver == 0 is defined anyway. */
Peter Maydelle20d84c2016-02-19 14:07:43 +00007419 .resetvalue = 0 },
7420 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7421 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7422 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007423 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007424 .resetvalue = 0 },
7425 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7426 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7427 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007428 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007429 .resetvalue = 0 },
7430 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7431 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7432 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007433 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007434 .resetvalue = 0 },
Peter Maydelle60cef82014-02-26 17:20:05 +00007435 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7436 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7437 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007438 .accessfn = access_aa64_tid3,
Peter Maydell2a609df2020-02-14 17:51:04 +00007439 .resetvalue = cpu->isar.id_aa64dfr0 },
Peter Maydelle60cef82014-02-26 17:20:05 +00007440 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7441 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7442 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007443 .accessfn = access_aa64_tid3,
Peter Maydell2a609df2020-02-14 17:51:04 +00007444 .resetvalue = cpu->isar.id_aa64dfr1 },
Peter Maydelle20d84c2016-02-19 14:07:43 +00007445 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7446 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7447 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007448 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007449 .resetvalue = 0 },
7450 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7451 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7452 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007453 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007454 .resetvalue = 0 },
Peter Maydelle60cef82014-02-26 17:20:05 +00007455 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7456 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7457 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007458 .accessfn = access_aa64_tid3,
Peter Maydelle60cef82014-02-26 17:20:05 +00007459 .resetvalue = cpu->id_aa64afr0 },
7460 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7461 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7462 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007463 .accessfn = access_aa64_tid3,
Peter Maydelle60cef82014-02-26 17:20:05 +00007464 .resetvalue = cpu->id_aa64afr1 },
Peter Maydelle20d84c2016-02-19 14:07:43 +00007465 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7466 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7467 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007468 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007469 .resetvalue = 0 },
7470 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7471 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7472 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007473 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007474 .resetvalue = 0 },
Peter Maydelle60cef82014-02-26 17:20:05 +00007475 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7476 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7477 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007478 .accessfn = access_aa64_tid3,
Richard Henderson47576b92018-10-24 07:50:16 +01007479 .resetvalue = cpu->isar.id_aa64isar0 },
Peter Maydelle60cef82014-02-26 17:20:05 +00007480 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7481 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7482 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007483 .accessfn = access_aa64_tid3,
Richard Henderson47576b92018-10-24 07:50:16 +01007484 .resetvalue = cpu->isar.id_aa64isar1 },
Peter Maydelle20d84c2016-02-19 14:07:43 +00007485 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7486 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7487 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007488 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007489 .resetvalue = 0 },
7490 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7491 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7492 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007493 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007494 .resetvalue = 0 },
7495 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7496 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7497 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007498 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007499 .resetvalue = 0 },
7500 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7501 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7502 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007503 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007504 .resetvalue = 0 },
7505 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7506 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7507 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007508 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007509 .resetvalue = 0 },
7510 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7511 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7512 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007513 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007514 .resetvalue = 0 },
Peter Maydelle60cef82014-02-26 17:20:05 +00007515 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7516 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7517 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007518 .accessfn = access_aa64_tid3,
Peter Maydell3dc91dd2018-12-13 14:40:56 +00007519 .resetvalue = cpu->isar.id_aa64mmfr0 },
Peter Maydelle60cef82014-02-26 17:20:05 +00007520 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7521 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7522 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007523 .accessfn = access_aa64_tid3,
Peter Maydell3dc91dd2018-12-13 14:40:56 +00007524 .resetvalue = cpu->isar.id_aa64mmfr1 },
Richard Henderson64761e12020-02-08 12:58:13 +00007525 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007526 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7527 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007528 .accessfn = access_aa64_tid3,
Richard Henderson64761e12020-02-08 12:58:13 +00007529 .resetvalue = cpu->isar.id_aa64mmfr2 },
Peter Maydelle20d84c2016-02-19 14:07:43 +00007530 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7531 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7532 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007533 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007534 .resetvalue = 0 },
7535 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7536 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7537 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007538 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007539 .resetvalue = 0 },
7540 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7541 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7542 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007543 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007544 .resetvalue = 0 },
7545 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7546 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7547 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007548 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007549 .resetvalue = 0 },
7550 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7551 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7552 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007553 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007554 .resetvalue = 0 },
Peter Maydella50c0f52014-04-15 19:18:44 +01007555 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7556 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7557 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007558 .accessfn = access_aa64_tid3,
Richard Henderson47576b92018-10-24 07:50:16 +01007559 .resetvalue = cpu->isar.mvfr0 },
Peter Maydella50c0f52014-04-15 19:18:44 +01007560 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7561 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7562 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007563 .accessfn = access_aa64_tid3,
Richard Henderson47576b92018-10-24 07:50:16 +01007564 .resetvalue = cpu->isar.mvfr1 },
Peter Maydella50c0f52014-04-15 19:18:44 +01007565 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7566 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7567 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007568 .accessfn = access_aa64_tid3,
Richard Henderson47576b92018-10-24 07:50:16 +01007569 .resetvalue = cpu->isar.mvfr2 },
Peter Maydelle20d84c2016-02-19 14:07:43 +00007570 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7571 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7572 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007573 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007574 .resetvalue = 0 },
7575 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7576 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7577 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007578 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007579 .resetvalue = 0 },
7580 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7581 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7582 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007583 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007584 .resetvalue = 0 },
7585 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7586 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7587 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007588 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007589 .resetvalue = 0 },
7590 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7591 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7592 .access = PL1_R, .type = ARM_CP_CONST,
Marc Zyngier6a4ef4e2019-11-26 13:55:37 +00007593 .accessfn = access_aa64_tid3,
Peter Maydelle20d84c2016-02-19 14:07:43 +00007594 .resetvalue = 0 },
Alistair Francis4054bfa2016-02-18 14:16:17 +00007595 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7596 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7597 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
Aaron Lindsaycad86732019-01-21 10:23:14 +00007598 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
Alistair Francis4054bfa2016-02-18 14:16:17 +00007599 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7600 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7601 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7602 .resetvalue = cpu->pmceid0 },
7603 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7604 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7605 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
Aaron Lindsaycad86732019-01-21 10:23:14 +00007606 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
Alistair Francis4054bfa2016-02-18 14:16:17 +00007607 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7608 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7609 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7610 .resetvalue = cpu->pmceid1 },
Peter Maydelle60cef82014-02-26 17:20:05 +00007611 REGINFO_SENTINEL
7612 };
Alex Bennée6c5c0fe2019-02-15 09:56:38 +00007613#ifdef CONFIG_USER_ONLY
7614 ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7615 { .name = "ID_AA64PFR0_EL1",
7616 .exported_bits = 0x000f000f00ff0000,
7617 .fixed_bits = 0x0000000000000011 },
7618 { .name = "ID_AA64PFR1_EL1",
7619 .exported_bits = 0x00000000000000f0 },
Alex Bennéed0402422019-02-15 09:56:39 +00007620 { .name = "ID_AA64PFR*_EL1_RESERVED",
7621 .is_glob = true },
Alex Bennée6c5c0fe2019-02-15 09:56:38 +00007622 { .name = "ID_AA64ZFR0_EL1" },
7623 { .name = "ID_AA64MMFR0_EL1",
7624 .fixed_bits = 0x00000000ff000000 },
7625 { .name = "ID_AA64MMFR1_EL1" },
Alex Bennéed0402422019-02-15 09:56:39 +00007626 { .name = "ID_AA64MMFR*_EL1_RESERVED",
7627 .is_glob = true },
Alex Bennée6c5c0fe2019-02-15 09:56:38 +00007628 { .name = "ID_AA64DFR0_EL1",
7629 .fixed_bits = 0x0000000000000006 },
7630 { .name = "ID_AA64DFR1_EL1" },
Alex Bennéed0402422019-02-15 09:56:39 +00007631 { .name = "ID_AA64DFR*_EL1_RESERVED",
7632 .is_glob = true },
7633 { .name = "ID_AA64AFR*",
7634 .is_glob = true },
Alex Bennée6c5c0fe2019-02-15 09:56:38 +00007635 { .name = "ID_AA64ISAR0_EL1",
7636 .exported_bits = 0x00fffffff0fffff0 },
7637 { .name = "ID_AA64ISAR1_EL1",
7638 .exported_bits = 0x000000f0ffffffff },
Alex Bennéed0402422019-02-15 09:56:39 +00007639 { .name = "ID_AA64ISAR*_EL1_RESERVED",
7640 .is_glob = true },
Alex Bennée6c5c0fe2019-02-15 09:56:38 +00007641 REGUSERINFO_SENTINEL
7642 };
7643 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7644#endif
Greg Bellowsbe8e8122015-02-05 13:37:22 +00007645 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7646 if (!arm_feature(env, ARM_FEATURE_EL3) &&
7647 !arm_feature(env, ARM_FEATURE_EL2)) {
7648 ARMCPRegInfo rvbar = {
7649 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7650 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
7651 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
7652 };
7653 define_one_arm_cp_reg(cpu, &rvbar);
7654 }
Peter Maydelle60cef82014-02-26 17:20:05 +00007655 define_arm_cp_regs(cpu, v8_idregs);
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00007656 define_arm_cp_regs(cpu, v8_cp_reginfo);
7657 }
Edgar E. Iglesias3b685ba2014-05-27 17:09:53 +01007658 if (arm_feature(env, ARM_FEATURE_EL2)) {
Edgar E. Iglesiasf0d574d2015-09-14 14:39:51 +01007659 uint64_t vmpidr_def = mpidr_read_val(env);
Edgar E. Iglesias731de9e2015-09-14 14:39:50 +01007660 ARMCPRegInfo vpidr_regs[] = {
7661 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7662 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7663 .access = PL2_RW, .accessfn = access_el3_aa32ns,
Peter Maydell36476562018-02-22 15:12:51 +00007664 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
7665 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
Edgar E. Iglesias731de9e2015-09-14 14:39:50 +01007666 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7667 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7668 .access = PL2_RW, .resetvalue = cpu->midr,
7669 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
Edgar E. Iglesiasf0d574d2015-09-14 14:39:51 +01007670 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7671 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7672 .access = PL2_RW, .accessfn = access_el3_aa32ns,
Peter Maydell36476562018-02-22 15:12:51 +00007673 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
7674 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
Edgar E. Iglesiasf0d574d2015-09-14 14:39:51 +01007675 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7676 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7677 .access = PL2_RW,
7678 .resetvalue = vmpidr_def,
7679 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
Edgar E. Iglesias731de9e2015-09-14 14:39:50 +01007680 REGINFO_SENTINEL
7681 };
7682 define_arm_cp_regs(cpu, vpidr_regs);
Peter Maydell4771cd02015-06-01 19:18:36 +01007683 define_arm_cp_regs(cpu, el2_cp_reginfo);
Peter Maydellce4afed2018-08-24 13:17:37 +01007684 if (arm_feature(env, ARM_FEATURE_V8)) {
7685 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7686 }
Greg Bellowsbe8e8122015-02-05 13:37:22 +00007687 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7688 if (!arm_feature(env, ARM_FEATURE_EL3)) {
7689 ARMCPRegInfo rvbar = {
7690 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7691 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
7692 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
7693 };
7694 define_one_arm_cp_reg(cpu, &rvbar);
7695 }
Edgar E. Iglesiasd42e3c22014-05-27 17:09:54 +01007696 } else {
7697 /* If EL2 is missing but higher ELs are enabled, we need to
7698 * register the no_el2 reginfos.
7699 */
7700 if (arm_feature(env, ARM_FEATURE_EL3)) {
Edgar E. Iglesiasf0d574d2015-09-14 14:39:51 +01007701 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
7702 * of MIDR_EL1 and MPIDR_EL1.
Edgar E. Iglesias731de9e2015-09-14 14:39:50 +01007703 */
7704 ARMCPRegInfo vpidr_regs[] = {
7705 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7706 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
Edgar E. Iglesias93dd1e62020-05-05 16:17:29 +02007707 .access = PL2_RW, .accessfn = access_el3_aa32ns,
Edgar E. Iglesias731de9e2015-09-14 14:39:50 +01007708 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
7709 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
Edgar E. Iglesiasf0d574d2015-09-14 14:39:51 +01007710 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7711 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
Edgar E. Iglesias93dd1e62020-05-05 16:17:29 +02007712 .access = PL2_RW, .accessfn = access_el3_aa32ns,
Edgar E. Iglesiasf0d574d2015-09-14 14:39:51 +01007713 .type = ARM_CP_NO_RAW,
7714 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
Edgar E. Iglesias731de9e2015-09-14 14:39:50 +01007715 REGINFO_SENTINEL
7716 };
7717 define_arm_cp_regs(cpu, vpidr_regs);
Peter Maydell4771cd02015-06-01 19:18:36 +01007718 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
Peter Maydellce4afed2018-08-24 13:17:37 +01007719 if (arm_feature(env, ARM_FEATURE_V8)) {
7720 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo);
7721 }
Edgar E. Iglesiasd42e3c22014-05-27 17:09:54 +01007722 }
Edgar E. Iglesias3b685ba2014-05-27 17:09:53 +01007723 }
Edgar E. Iglesias81547d62014-05-27 17:09:53 +01007724 if (arm_feature(env, ARM_FEATURE_EL3)) {
Fabian Aggeler0f1a3b22014-12-11 12:07:49 +00007725 define_arm_cp_regs(cpu, el3_cp_reginfo);
Peter Maydelle24fdd22016-04-04 17:33:51 +01007726 ARMCPRegInfo el3_regs[] = {
7727 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7728 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
7729 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
7730 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7731 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7732 .access = PL3_RW,
7733 .raw_writefn = raw_write, .writefn = sctlr_write,
7734 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7735 .resetvalue = cpu->reset_sctlr },
7736 REGINFO_SENTINEL
Greg Bellowsbe8e8122015-02-05 13:37:22 +00007737 };
Peter Maydelle24fdd22016-04-04 17:33:51 +01007738
7739 define_arm_cp_regs(cpu, el3_regs);
Edgar E. Iglesias81547d62014-05-27 17:09:53 +01007740 }
Peter Maydell2f027fc2016-02-11 11:17:31 +00007741 /* The behaviour of NSACR is sufficiently various that we don't
7742 * try to describe it in a single reginfo:
7743 * if EL3 is 64 bit, then trap to EL3 from S EL1,
7744 * reads as constant 0xc00 from NS EL1 and NS EL2
7745 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7746 * if v7 without EL3, register doesn't exist
7747 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7748 */
7749 if (arm_feature(env, ARM_FEATURE_EL3)) {
7750 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
7751 ARMCPRegInfo nsacr = {
7752 .name = "NSACR", .type = ARM_CP_CONST,
7753 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7754 .access = PL1_RW, .accessfn = nsacr_access,
7755 .resetvalue = 0xc00
7756 };
7757 define_one_arm_cp_reg(cpu, &nsacr);
7758 } else {
7759 ARMCPRegInfo nsacr = {
7760 .name = "NSACR",
7761 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7762 .access = PL3_RW | PL1_R,
7763 .resetvalue = 0,
7764 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
7765 };
7766 define_one_arm_cp_reg(cpu, &nsacr);
7767 }
7768 } else {
7769 if (arm_feature(env, ARM_FEATURE_V8)) {
7770 ARMCPRegInfo nsacr = {
7771 .name = "NSACR", .type = ARM_CP_CONST,
7772 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7773 .access = PL1_R,
7774 .resetvalue = 0xc00
7775 };
7776 define_one_arm_cp_reg(cpu, &nsacr);
7777 }
7778 }
7779
Peter Maydell452a0952017-06-02 11:51:47 +01007780 if (arm_feature(env, ARM_FEATURE_PMSA)) {
Peter Crosthwaite6cb0b012015-06-19 14:17:44 +01007781 if (arm_feature(env, ARM_FEATURE_V6)) {
7782 /* PMSAv6 not implemented */
7783 assert(arm_feature(env, ARM_FEATURE_V7));
7784 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7785 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
7786 } else {
7787 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
7788 }
Peter Maydell18032be2012-06-20 11:57:13 +00007789 } else {
Peter Crosthwaite8e5d75c2015-06-15 18:06:10 +01007790 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
Peter Maydell18032be2012-06-20 11:57:13 +00007791 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
Peter Maydell4036b7d2020-02-14 17:51:14 +00007792 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
7793 if (cpu_isar_feature(aa32_hpd, cpu)) {
Richard Hendersonab638a32018-12-13 13:48:07 +00007794 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
7795 }
Peter Maydell18032be2012-06-20 11:57:13 +00007796 }
Peter Maydellc326b972012-06-20 11:57:10 +00007797 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
7798 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
7799 }
Peter Maydell6cc7a3a2012-06-20 11:57:12 +00007800 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
7801 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
7802 }
Peter Maydell4a501602012-06-20 11:57:16 +00007803 if (arm_feature(env, ARM_FEATURE_VAPA)) {
7804 define_arm_cp_regs(cpu, vapa_cp_reginfo);
7805 }
Peter Maydellc4804212012-06-20 11:57:17 +00007806 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
7807 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
7808 }
7809 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
7810 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
7811 }
7812 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
7813 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
7814 }
Peter Maydell18032be2012-06-20 11:57:13 +00007815 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
7816 define_arm_cp_regs(cpu, omap_cp_reginfo);
7817 }
Peter Maydell34f90522012-06-20 11:57:18 +00007818 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
7819 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
7820 }
Peter Maydell1047b9d2012-06-20 11:57:15 +00007821 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7822 define_arm_cp_regs(cpu, xscale_cp_reginfo);
7823 }
7824 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
7825 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
7826 }
Peter Maydell7ac681c2012-07-12 10:59:07 +00007827 if (arm_feature(env, ARM_FEATURE_LPAE)) {
7828 define_arm_cp_regs(cpu, lpae_cp_reginfo);
7829 }
Peter Maydell873b73c2020-02-14 17:50:56 +00007830 if (cpu_isar_feature(aa32_jazelle, cpu)) {
Marc Zyngierf96f3d52019-12-01 12:20:18 +00007831 define_arm_cp_regs(cpu, jazelle_regs);
7832 }
Peter Maydell78848492012-06-20 11:57:20 +00007833 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
7834 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
7835 * be read-only (ie write causes UNDEF exception).
7836 */
7837 {
Peter Maydell00a29f32014-04-15 19:18:47 +01007838 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
7839 /* Pre-v8 MIDR space.
7840 * Note that the MIDR isn't a simple constant register because
Peter Maydell78848492012-06-20 11:57:20 +00007841 * of the TI925 behaviour where writes to another register can
7842 * cause the MIDR value to change.
Peter Crosthwaite97ce8d62013-07-10 14:22:21 +10007843 *
7844 * Unimplemented registers in the c15 0 0 0 space default to
7845 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
7846 * and friends override accordingly.
Peter Maydell78848492012-06-20 11:57:20 +00007847 */
7848 { .name = "MIDR",
Peter Crosthwaite97ce8d62013-07-10 14:22:21 +10007849 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
Peter Maydell78848492012-06-20 11:57:20 +00007850 .access = PL1_R, .resetvalue = cpu->midr,
Peter Maydelld4e6df62013-06-25 18:16:07 +01007851 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
Edgar E. Iglesias731de9e2015-09-14 14:39:50 +01007852 .readfn = midr_read,
Peter Crosthwaite97ce8d62013-07-10 14:22:21 +10007853 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7854 .type = ARM_CP_OVERRIDE },
Peter Maydell78848492012-06-20 11:57:20 +00007855 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
7856 { .name = "DUMMY",
7857 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
7858 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7859 { .name = "DUMMY",
7860 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
7861 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7862 { .name = "DUMMY",
7863 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
7864 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7865 { .name = "DUMMY",
7866 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
7867 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7868 { .name = "DUMMY",
7869 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
7870 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7871 REGINFO_SENTINEL
7872 };
Peter Maydell00a29f32014-04-15 19:18:47 +01007873 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
Peter Maydell00a29f32014-04-15 19:18:47 +01007874 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
7875 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
Edgar E. Iglesias731de9e2015-09-14 14:39:50 +01007876 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
7877 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7878 .readfn = midr_read },
Sergey Fedorovac00c792015-06-15 18:06:08 +01007879 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
7880 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7881 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7882 .access = PL1_R, .resetvalue = cpu->midr },
7883 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7884 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
7885 .access = PL1_R, .resetvalue = cpu->midr },
Peter Maydell00a29f32014-04-15 19:18:47 +01007886 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
7887 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
Marc Zyngier93fbc982019-12-01 12:20:15 +00007888 .access = PL1_R,
7889 .accessfn = access_aa64_tid1,
7890 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
Peter Maydell00a29f32014-04-15 19:18:47 +01007891 REGINFO_SENTINEL
7892 };
7893 ARMCPRegInfo id_cp_reginfo[] = {
7894 /* These are common to v8 and pre-v8 */
7895 { .name = "CTR",
7896 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
Marc Zyngier630fcd42019-12-01 12:20:14 +00007897 .access = PL1_R, .accessfn = ctr_el0_access,
7898 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
Peter Maydell00a29f32014-04-15 19:18:47 +01007899 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
7900 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
7901 .access = PL0_R, .accessfn = ctr_el0_access,
7902 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
7903 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
7904 { .name = "TCMTR",
7905 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
Marc Zyngier93fbc982019-12-01 12:20:15 +00007906 .access = PL1_R,
7907 .accessfn = access_aa32_tid1,
7908 .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell00a29f32014-04-15 19:18:47 +01007909 REGINFO_SENTINEL
7910 };
Peter Crosthwaite8085ce62015-06-15 18:06:10 +01007911 /* TLBTR is specific to VMSA */
7912 ARMCPRegInfo id_tlbtr_reginfo = {
7913 .name = "TLBTR",
7914 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
Marc Zyngier93fbc982019-12-01 12:20:15 +00007915 .access = PL1_R,
7916 .accessfn = access_aa32_tid1,
7917 .type = ARM_CP_CONST, .resetvalue = 0,
Peter Crosthwaite8085ce62015-06-15 18:06:10 +01007918 };
Peter Crosthwaite3281af82015-06-19 14:17:44 +01007919 /* MPUIR is specific to PMSA V6+ */
7920 ARMCPRegInfo id_mpuir_reginfo = {
7921 .name = "MPUIR",
7922 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7923 .access = PL1_R, .type = ARM_CP_CONST,
7924 .resetvalue = cpu->pmsav7_dregion << 8
7925 };
Peter Maydell78848492012-06-20 11:57:20 +00007926 ARMCPRegInfo crn0_wi_reginfo = {
7927 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
7928 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
7929 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
7930 };
Alex Bennée6c5c0fe2019-02-15 09:56:38 +00007931#ifdef CONFIG_USER_ONLY
7932 ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
7933 { .name = "MIDR_EL1",
7934 .exported_bits = 0x00000000ffffffff },
7935 { .name = "REVIDR_EL1" },
7936 REGUSERINFO_SENTINEL
7937 };
7938 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
7939#endif
Peter Maydell78848492012-06-20 11:57:20 +00007940 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
7941 arm_feature(env, ARM_FEATURE_STRONGARM)) {
7942 ARMCPRegInfo *r;
7943 /* Register the blanket "writes ignored" value first to cover the
Peter Crosthwaitea703eda2013-07-10 14:21:42 +10007944 * whole space. Then update the specific ID registers to allow write
7945 * access, so that they ignore writes rather than causing them to
7946 * UNDEF.
Peter Maydell78848492012-06-20 11:57:20 +00007947 */
7948 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
Peter Maydell00a29f32014-04-15 19:18:47 +01007949 for (r = id_pre_v8_midr_cp_reginfo;
7950 r->type != ARM_CP_SENTINEL; r++) {
7951 r->access = PL1_RW;
7952 }
Peter Maydell78848492012-06-20 11:57:20 +00007953 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
7954 r->access = PL1_RW;
Peter Maydell78848492012-06-20 11:57:20 +00007955 }
Mathew Maidment10006112018-05-04 18:05:50 +01007956 id_mpuir_reginfo.access = PL1_RW;
Peter Crosthwaite3281af82015-06-19 14:17:44 +01007957 id_tlbtr_reginfo.access = PL1_RW;
Peter Maydell78848492012-06-20 11:57:20 +00007958 }
Peter Maydell00a29f32014-04-15 19:18:47 +01007959 if (arm_feature(env, ARM_FEATURE_V8)) {
7960 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
7961 } else {
7962 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
7963 }
Peter Crosthwaitea703eda2013-07-10 14:21:42 +10007964 define_arm_cp_regs(cpu, id_cp_reginfo);
Peter Maydell452a0952017-06-02 11:51:47 +01007965 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
Peter Crosthwaite8085ce62015-06-15 18:06:10 +01007966 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
Peter Crosthwaite3281af82015-06-19 14:17:44 +01007967 } else if (arm_feature(env, ARM_FEATURE_V7)) {
7968 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
Peter Crosthwaite8085ce62015-06-15 18:06:10 +01007969 }
Peter Maydell78848492012-06-20 11:57:20 +00007970 }
7971
Peter Crosthwaite97ce8d62013-07-10 14:22:21 +10007972 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
Alex Bennée52264162019-02-15 09:56:38 +00007973 ARMCPRegInfo mpidr_cp_reginfo[] = {
7974 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
7975 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
7976 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
7977 REGINFO_SENTINEL
7978 };
7979#ifdef CONFIG_USER_ONLY
7980 ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
7981 { .name = "MPIDR_EL1",
7982 .fixed_bits = 0x0000000080000000 },
7983 REGUSERINFO_SENTINEL
7984 };
7985 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
7986#endif
Peter Crosthwaite97ce8d62013-07-10 14:22:21 +10007987 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
7988 }
7989
Peter Maydell2771db22012-06-20 11:57:18 +00007990 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
Peter Maydell834a6c62015-08-25 15:45:07 +01007991 ARMCPRegInfo auxcr_reginfo[] = {
7992 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
7993 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
Richard Henderson99602372020-03-05 16:09:18 +00007994 .access = PL1_RW, .accessfn = access_tacr,
7995 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
Peter Maydell834a6c62015-08-25 15:45:07 +01007996 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
7997 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
7998 .access = PL2_RW, .type = ARM_CP_CONST,
7999 .resetvalue = 0 },
8000 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8001 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8002 .access = PL3_RW, .type = ARM_CP_CONST,
8003 .resetvalue = 0 },
8004 REGINFO_SENTINEL
Peter Maydell2771db22012-06-20 11:57:18 +00008005 };
Peter Maydell834a6c62015-08-25 15:45:07 +01008006 define_arm_cp_regs(cpu, auxcr_reginfo);
Peter Maydellf6287c22020-02-14 17:51:16 +00008007 if (cpu_isar_feature(aa32_ac2, cpu)) {
8008 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
Peter Maydell0e0456a2018-08-24 13:17:36 +01008009 }
Peter Maydell2771db22012-06-20 11:57:18 +00008010 }
8011
Peter Crosthwaited8ba7802013-12-17 19:42:28 +00008012 if (arm_feature(env, ARM_FEATURE_CBAR)) {
Luc Micheld56974a2019-09-19 14:18:40 +01008013 /*
8014 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8015 * There are two flavours:
8016 * (1) older 32-bit only cores have a simple 32-bit CBAR
8017 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8018 * 32-bit register visible to AArch32 at a different encoding
8019 * to the "flavour 1" register and with the bits rearranged to
8020 * be able to squash a 64-bit address into the 32-bit view.
8021 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8022 * in future if we support AArch32-only configs of some of the
8023 * AArch64 cores we might need to add a specific feature flag
8024 * to indicate cores with "flavour 2" CBAR.
8025 */
Peter Maydellf318cec2014-04-15 19:18:49 +01008026 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8027 /* 32 bit view is [31:18] 0...0 [43:32]. */
8028 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8029 | extract64(cpu->reset_cbar, 32, 12);
8030 ARMCPRegInfo cbar_reginfo[] = {
8031 { .name = "CBAR",
8032 .type = ARM_CP_CONST,
Luc Micheld56974a2019-09-19 14:18:40 +01008033 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8034 .access = PL1_R, .resetvalue = cbar32 },
Peter Maydellf318cec2014-04-15 19:18:49 +01008035 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8036 .type = ARM_CP_CONST,
8037 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
Luc Micheld56974a2019-09-19 14:18:40 +01008038 .access = PL1_R, .resetvalue = cpu->reset_cbar },
Peter Maydellf318cec2014-04-15 19:18:49 +01008039 REGINFO_SENTINEL
8040 };
8041 /* We don't implement a r/w 64 bit CBAR currently */
8042 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8043 define_arm_cp_regs(cpu, cbar_reginfo);
8044 } else {
8045 ARMCPRegInfo cbar = {
8046 .name = "CBAR",
8047 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8048 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8049 .fieldoffset = offsetof(CPUARMState,
8050 cp15.c15_config_base_address)
8051 };
8052 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8053 cbar.access = PL1_R;
8054 cbar.fieldoffset = 0;
8055 cbar.type = ARM_CP_CONST;
8056 }
8057 define_one_arm_cp_reg(cpu, &cbar);
8058 }
Peter Crosthwaited8ba7802013-12-17 19:42:28 +00008059 }
8060
Cédric Le Goater91db4642016-12-27 14:59:30 +00008061 if (arm_feature(env, ARM_FEATURE_VBAR)) {
8062 ARMCPRegInfo vbar_cp_reginfo[] = {
8063 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8064 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8065 .access = PL1_RW, .writefn = vbar_write,
8066 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8067 offsetof(CPUARMState, cp15.vbar_ns) },
8068 .resetvalue = 0 },
8069 REGINFO_SENTINEL
8070 };
8071 define_arm_cp_regs(cpu, vbar_cp_reginfo);
8072 }
8073
Peter Maydell2771db22012-06-20 11:57:18 +00008074 /* Generic registers whose values depend on the implementation */
8075 {
8076 ARMCPRegInfo sctlr = {
Peter Maydell5ebafdf2014-02-26 17:20:03 +00008077 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
Fabian Aggeler137feaa2014-12-11 12:07:50 +00008078 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
Richard Henderson84929212020-03-05 16:09:17 +00008079 .access = PL1_RW, .accessfn = access_tvm_trvm,
Fabian Aggeler137feaa2014-12-11 12:07:50 +00008080 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8081 offsetof(CPUARMState, cp15.sctlr_ns) },
Peter Maydelld4e6df62013-06-25 18:16:07 +01008082 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8083 .raw_writefn = raw_write,
Peter Maydell2771db22012-06-20 11:57:18 +00008084 };
8085 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8086 /* Normally we would always end the TB on an SCTLR write, but Linux
8087 * arch/arm/mach-pxa/sleep.S expects two instructions following
8088 * an MMU enable to execute from cache. Imitate this behaviour.
8089 */
8090 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8091 }
8092 define_one_arm_cp_reg(cpu, &sctlr);
8093 }
Richard Henderson5be5e8e2018-01-22 19:53:48 -08008094
Richard Henderson2d7137c2018-12-13 13:48:08 +00008095 if (cpu_isar_feature(aa64_lor, cpu)) {
Richard Henderson2d7137c2018-12-13 13:48:08 +00008096 define_arm_cp_regs(cpu, lor_reginfo);
8097 }
Richard Henderson220f5082020-02-08 12:58:07 +00008098 if (cpu_isar_feature(aa64_pan, cpu)) {
8099 define_one_arm_cp_reg(cpu, &pan_reginfo);
8100 }
Richard Henderson04b07d22020-02-08 12:58:11 +00008101#ifndef CONFIG_USER_ONLY
8102 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8103 define_arm_cp_regs(cpu, ats1e1_reginfo);
8104 }
8105 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8106 define_arm_cp_regs(cpu, ats1cp_reginfo);
8107 }
8108#endif
Richard Henderson9eeb7a12020-02-08 12:58:14 +00008109 if (cpu_isar_feature(aa64_uao, cpu)) {
8110 define_one_arm_cp_reg(cpu, &uao_reginfo);
8111 }
Richard Henderson2d7137c2018-12-13 13:48:08 +00008112
Richard Hendersone2a1a462020-02-07 14:04:21 +00008113 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8114 define_arm_cp_regs(cpu, vhe_reginfo);
8115 }
8116
Richard Hendersoncd208a12018-10-24 07:50:17 +01008117 if (cpu_isar_feature(aa64_sve, cpu)) {
Richard Henderson5be5e8e2018-01-22 19:53:48 -08008118 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
8119 if (arm_feature(env, ARM_FEATURE_EL2)) {
8120 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
8121 } else {
8122 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
8123 }
8124 if (arm_feature(env, ARM_FEATURE_EL3)) {
8125 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
8126 }
8127 }
Richard Henderson967aa942019-01-21 10:23:13 +00008128
8129#ifdef TARGET_AARCH64
8130 if (cpu_isar_feature(aa64_pauth, cpu)) {
8131 define_arm_cp_regs(cpu, pauth_reginfo);
8132 }
Richard Hendersonde390642019-03-12 21:57:35 -07008133 if (cpu_isar_feature(aa64_rndr, cpu)) {
8134 define_arm_cp_regs(cpu, rndr_reginfo);
8135 }
Beata Michalska0d57b492019-11-21 00:08:43 +00008136#ifndef CONFIG_USER_ONLY
8137 /* Data Cache clean instructions up to PoP */
8138 if (cpu_isar_feature(aa64_dcpop, cpu)) {
8139 define_one_arm_cp_reg(cpu, dcpop_reg);
8140
8141 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8142 define_one_arm_cp_reg(cpu, dcpodp_reg);
8143 }
8144 }
8145#endif /*CONFIG_USER_ONLY*/
Richard Henderson4b779ce2020-06-25 20:31:05 -07008146
8147 /*
8148 * If full MTE is enabled, add all of the system registers.
8149 * If only "instructions available at EL0" are enabled,
8150 * then define only a RAZ/WI version of PSTATE.TCO.
8151 */
8152 if (cpu_isar_feature(aa64_mte, cpu)) {
8153 define_arm_cp_regs(cpu, mte_reginfo);
Richard Henderson5463df12020-06-25 20:31:18 -07008154 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
Richard Henderson4b779ce2020-06-25 20:31:05 -07008155 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8156 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
Richard Henderson5463df12020-06-25 20:31:18 -07008157 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
Richard Henderson4b779ce2020-06-25 20:31:05 -07008158 }
Richard Henderson967aa942019-01-21 10:23:13 +00008159#endif
Richard Hendersoncb570bd2019-03-01 12:04:54 -08008160
Peter Maydell22e57072020-02-14 17:50:59 +00008161 if (cpu_isar_feature(any_predinv, cpu)) {
Richard Hendersoncb570bd2019-03-01 12:04:54 -08008162 define_arm_cp_regs(cpu, predinv_reginfo);
8163 }
Richard Hendersone2cce182020-02-07 14:04:25 +00008164
Peter Maydell957e6152020-02-24 18:26:26 +00008165 if (cpu_isar_feature(any_ccidx, cpu)) {
8166 define_arm_cp_regs(cpu, ccsidr2_reginfo);
8167 }
8168
Richard Hendersone2cce182020-02-07 14:04:25 +00008169#ifndef CONFIG_USER_ONLY
8170 /*
8171 * Register redirections and aliases must be done last,
8172 * after the registers from the other extensions have been defined.
8173 */
8174 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8175 define_arm_vh_e2h_redirects_aliases(cpu);
8176 }
8177#endif
Peter Maydell2ceb98c2012-06-20 11:57:09 +00008178}
8179
Andreas Färber14969262013-01-05 10:18:18 +01008180void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
8181{
Andreas Färber22169d42013-06-28 21:27:39 +02008182 CPUState *cs = CPU(cpu);
Andreas Färber14969262013-01-05 10:18:18 +01008183 CPUARMState *env = &cpu->env;
8184
Peter Maydell6a669422013-12-17 19:42:32 +00008185 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
Alex Bennéed12379c2020-03-16 17:21:45 +00008186 /*
8187 * The lower part of each SVE register aliases to the FPU
8188 * registers so we don't need to include both.
8189 */
8190#ifdef TARGET_AARCH64
8191 if (isar_feature_aa64_sve(&cpu->isar)) {
8192 gdb_register_coprocessor(cs, arm_gdb_get_svereg, arm_gdb_set_svereg,
8193 arm_gen_dynamic_svereg_xml(cs, cs->gdb_num_regs),
8194 "sve-registers.xml", 0);
8195 } else
8196#endif
8197 {
8198 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
8199 aarch64_fpu_gdb_set_reg,
8200 34, "aarch64-fpu.xml", 0);
8201 }
Peter Maydell6a669422013-12-17 19:42:32 +00008202 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
Andreas Färber22169d42013-06-28 21:27:39 +02008203 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
pbrook56aebc82008-10-11 17:55:29 +00008204 51, "arm-neon.xml", 0);
Richard Hendersona6627f52020-02-14 10:15:31 -08008205 } else if (cpu_isar_feature(aa32_simd_r32, cpu)) {
Andreas Färber22169d42013-06-28 21:27:39 +02008206 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
pbrook56aebc82008-10-11 17:55:29 +00008207 35, "arm-vfp3.xml", 0);
Richard Henderson7fbc6a42020-02-24 14:22:16 -08008208 } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) {
Andreas Färber22169d42013-06-28 21:27:39 +02008209 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
pbrook56aebc82008-10-11 17:55:29 +00008210 19, "arm-vfp.xml", 0);
8211 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +01008212 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg,
Alex Bennée32d6e322020-03-16 17:21:43 +00008213 arm_gen_dynamic_sysreg_xml(cs, cs->gdb_num_regs),
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +01008214 "system-registers.xml", 0);
Alex Bennéed12379c2020-03-16 17:21:45 +00008215
pbrook40f137e2006-02-20 00:33:36 +00008216}
8217
Peter Maydell777dc782012-04-20 17:58:31 +00008218/* Sort alphabetically by type name, except for "any". */
8219static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
pbrook5adb4832007-03-08 03:15:18 +00008220{
Peter Maydell777dc782012-04-20 17:58:31 +00008221 ObjectClass *class_a = (ObjectClass *)a;
8222 ObjectClass *class_b = (ObjectClass *)b;
8223 const char *name_a, *name_b;
pbrook5adb4832007-03-08 03:15:18 +00008224
Peter Maydell777dc782012-04-20 17:58:31 +00008225 name_a = object_class_get_name(class_a);
8226 name_b = object_class_get_name(class_b);
Andreas Färber51492fd2013-01-27 17:30:10 +01008227 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
Peter Maydell777dc782012-04-20 17:58:31 +00008228 return 1;
Andreas Färber51492fd2013-01-27 17:30:10 +01008229 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
Peter Maydell777dc782012-04-20 17:58:31 +00008230 return -1;
8231 } else {
8232 return strcmp(name_a, name_b);
pbrook5adb4832007-03-08 03:15:18 +00008233 }
8234}
8235
Peter Maydell777dc782012-04-20 17:58:31 +00008236static void arm_cpu_list_entry(gpointer data, gpointer user_data)
pbrook40f137e2006-02-20 00:33:36 +00008237{
Peter Maydell777dc782012-04-20 17:58:31 +00008238 ObjectClass *oc = data;
Andreas Färber51492fd2013-01-27 17:30:10 +01008239 const char *typename;
8240 char *name;
pbrook3371d272007-03-08 03:04:12 +00008241
Andreas Färber51492fd2013-01-27 17:30:10 +01008242 typename = object_class_get_name(oc);
8243 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
Markus Armbruster04424282019-04-17 21:17:57 +02008244 qemu_printf(" %s\n", name);
Andreas Färber51492fd2013-01-27 17:30:10 +01008245 g_free(name);
Peter Maydell777dc782012-04-20 17:58:31 +00008246}
8247
Markus Armbruster04424282019-04-17 21:17:57 +02008248void arm_cpu_list(void)
Peter Maydell777dc782012-04-20 17:58:31 +00008249{
Peter Maydell777dc782012-04-20 17:58:31 +00008250 GSList *list;
8251
8252 list = object_class_get_list(TYPE_ARM_CPU, false);
8253 list = g_slist_sort(list, arm_cpu_list_compare);
Markus Armbruster04424282019-04-17 21:17:57 +02008254 qemu_printf("Available CPUs:\n");
8255 g_slist_foreach(list, arm_cpu_list_entry, NULL);
Peter Maydell777dc782012-04-20 17:58:31 +00008256 g_slist_free(list);
pbrook40f137e2006-02-20 00:33:36 +00008257}
8258
Cole Robinson78027bb2013-09-10 19:09:33 +01008259static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8260{
8261 ObjectClass *oc = data;
8262 CpuDefinitionInfoList **cpu_list = user_data;
8263 CpuDefinitionInfoList *entry;
8264 CpuDefinitionInfo *info;
8265 const char *typename;
8266
8267 typename = object_class_get_name(oc);
8268 info = g_malloc0(sizeof(*info));
8269 info->name = g_strndup(typename,
8270 strlen(typename) - strlen("-" TYPE_ARM_CPU));
Eduardo Habkost8ed877b2016-11-16 16:21:39 -02008271 info->q_typename = g_strdup(typename);
Cole Robinson78027bb2013-09-10 19:09:33 +01008272
8273 entry = g_malloc0(sizeof(*entry));
8274 entry->value = info;
8275 entry->next = *cpu_list;
8276 *cpu_list = entry;
8277}
8278
Marc-André Lureau25a9d6c2019-02-14 16:22:47 +01008279CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
Cole Robinson78027bb2013-09-10 19:09:33 +01008280{
8281 CpuDefinitionInfoList *cpu_list = NULL;
8282 GSList *list;
8283
8284 list = object_class_get_list(TYPE_ARM_CPU, false);
8285 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8286 g_slist_free(list);
8287
8288 return cpu_list;
8289}
8290
Peter Maydell6e6efd62013-12-22 22:32:30 +00008291static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
Peter Maydell51a79b02014-12-11 12:07:49 +00008292 void *opaque, int state, int secstate,
Abdallah Bouassida9c513e72018-05-18 17:48:07 +01008293 int crm, int opc1, int opc2,
8294 const char *name)
Peter Maydell6e6efd62013-12-22 22:32:30 +00008295{
8296 /* Private utility function for define_one_arm_cp_reg_with_opaque():
8297 * add a single reginfo struct to the hash table.
8298 */
8299 uint32_t *key = g_new(uint32_t, 1);
8300 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
8301 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00008302 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
8303
Abdallah Bouassida9c513e72018-05-18 17:48:07 +01008304 r2->name = g_strdup(name);
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00008305 /* Reset the secure state to the specific incoming state. This is
8306 * necessary as the register may have been defined with both states.
8307 */
8308 r2->secure = secstate;
8309
8310 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8311 /* Register is banked (using both entries in array).
8312 * Overwriting fieldoffset as the array is only used to define
8313 * banked registers but later only fieldoffset is used.
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00008314 */
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00008315 r2->fieldoffset = r->bank_fieldoffsets[ns];
8316 }
8317
8318 if (state == ARM_CP_STATE_AA32) {
8319 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8320 /* If the register is banked then we don't need to migrate or
8321 * reset the 32-bit instance in certain cases:
8322 *
8323 * 1) If the register has both 32-bit and 64-bit instances then we
8324 * can count on the 64-bit instance taking care of the
8325 * non-secure bank.
8326 * 2) If ARMv8 is enabled then we can count on a 64-bit version
8327 * taking care of the secure bank. This requires that separate
8328 * 32 and 64-bit definitions are provided.
8329 */
8330 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8331 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
Peter Maydell7a0e58f2015-02-05 13:37:22 +00008332 r2->type |= ARM_CP_ALIAS;
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00008333 }
8334 } else if ((secstate != r->secure) && !ns) {
8335 /* The register is not banked so we only want to allow migration of
8336 * the non-secure instance.
8337 */
Peter Maydell7a0e58f2015-02-05 13:37:22 +00008338 r2->type |= ARM_CP_ALIAS;
Peter Maydell58a1d8c2014-08-19 18:56:25 +01008339 }
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00008340
8341 if (r->state == ARM_CP_STATE_BOTH) {
8342 /* We assume it is a cp15 register if the .cp field is left unset.
8343 */
8344 if (r2->cp == 0) {
8345 r2->cp = 15;
8346 }
8347
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00008348#ifdef HOST_WORDS_BIGENDIAN
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00008349 if (r2->fieldoffset) {
8350 r2->fieldoffset += sizeof(uint32_t);
8351 }
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00008352#endif
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00008353 }
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00008354 }
8355 if (state == ARM_CP_STATE_AA64) {
8356 /* To allow abbreviation of ARMCPRegInfo
8357 * definitions, we treat cp == 0 as equivalent to
8358 * the value for "standard guest-visible sysreg".
Peter Maydell58a1d8c2014-08-19 18:56:25 +01008359 * STATE_BOTH definitions are also always "standard
8360 * sysreg" in their AArch64 view (the .cp value may
8361 * be non-zero for the benefit of the AArch32 view).
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00008362 */
Peter Maydell58a1d8c2014-08-19 18:56:25 +01008363 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00008364 r2->cp = CP_REG_ARM64_SYSREG_CP;
8365 }
8366 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
8367 r2->opc0, opc1, opc2);
8368 } else {
Peter Maydell51a79b02014-12-11 12:07:49 +00008369 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00008370 }
Peter Maydell6e6efd62013-12-22 22:32:30 +00008371 if (opaque) {
8372 r2->opaque = opaque;
8373 }
Peter Maydell67ed7712014-02-26 17:20:01 +00008374 /* reginfo passed to helpers is correct for the actual access,
8375 * and is never ARM_CP_STATE_BOTH:
8376 */
8377 r2->state = state;
Peter Maydell6e6efd62013-12-22 22:32:30 +00008378 /* Make sure reginfo passed to helpers for wildcarded regs
8379 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
8380 */
8381 r2->crm = crm;
8382 r2->opc1 = opc1;
8383 r2->opc2 = opc2;
8384 /* By convention, for wildcarded registers only the first
8385 * entry is used for migration; the others are marked as
Peter Maydell7a0e58f2015-02-05 13:37:22 +00008386 * ALIAS so we don't try to transfer the register
Peter Maydell6e6efd62013-12-22 22:32:30 +00008387 * multiple times. Special registers (ie NOP/WFI) are
Peter Maydell7a0e58f2015-02-05 13:37:22 +00008388 * never migratable and not even raw-accessible.
Peter Maydell6e6efd62013-12-22 22:32:30 +00008389 */
Peter Maydell7a0e58f2015-02-05 13:37:22 +00008390 if ((r->type & ARM_CP_SPECIAL)) {
8391 r2->type |= ARM_CP_NO_RAW;
8392 }
8393 if (((r->crm == CP_ANY) && crm != 0) ||
Peter Maydell6e6efd62013-12-22 22:32:30 +00008394 ((r->opc1 == CP_ANY) && opc1 != 0) ||
8395 ((r->opc2 == CP_ANY) && opc2 != 0)) {
Abdallah Bouassida1f163782018-05-18 17:48:07 +01008396 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
Peter Maydell6e6efd62013-12-22 22:32:30 +00008397 }
8398
Peter Maydell375421c2015-02-05 13:37:22 +00008399 /* Check that raw accesses are either forbidden or handled. Note that
8400 * we can't assert this earlier because the setup of fieldoffset for
8401 * banked registers has to be done first.
8402 */
8403 if (!(r2->type & ARM_CP_NO_RAW)) {
8404 assert(!raw_accessors_invalid(r2));
8405 }
8406
Peter Maydell6e6efd62013-12-22 22:32:30 +00008407 /* Overriding of an existing definition must be explicitly
8408 * requested.
8409 */
8410 if (!(r->type & ARM_CP_OVERRIDE)) {
8411 ARMCPRegInfo *oldreg;
8412 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
8413 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
8414 fprintf(stderr, "Register redefined: cp=%d %d bit "
8415 "crn=%d crm=%d opc1=%d opc2=%d, "
8416 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
8417 r2->crn, r2->crm, r2->opc1, r2->opc2,
8418 oldreg->name, r2->name);
8419 g_assert_not_reached();
8420 }
8421 }
8422 g_hash_table_insert(cpu->cp_regs, key, r2);
8423}
8424
8425
Peter Maydell4b6a83f2012-06-20 11:57:06 +00008426void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8427 const ARMCPRegInfo *r, void *opaque)
8428{
8429 /* Define implementations of coprocessor registers.
8430 * We store these in a hashtable because typically
8431 * there are less than 150 registers in a space which
8432 * is 16*16*16*8*8 = 262144 in size.
8433 * Wildcarding is supported for the crm, opc1 and opc2 fields.
8434 * If a register is defined twice then the second definition is
8435 * used, so this can be used to define some generic registers and
8436 * then override them with implementation specific variations.
8437 * At least one of the original and the second definition should
8438 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8439 * against accidental use.
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00008440 *
8441 * The state field defines whether the register is to be
8442 * visible in the AArch32 or AArch64 execution state. If the
8443 * state is set to ARM_CP_STATE_BOTH then we synthesise a
8444 * reginfo structure for the AArch32 view, which sees the lower
8445 * 32 bits of the 64 bit register.
8446 *
8447 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8448 * be wildcarded. AArch64 registers are always considered to be 64
8449 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8450 * the register, if any.
Peter Maydell4b6a83f2012-06-20 11:57:06 +00008451 */
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00008452 int crm, opc1, opc2, state;
Peter Maydell4b6a83f2012-06-20 11:57:06 +00008453 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8454 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8455 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8456 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8457 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8458 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8459 /* 64 bit registers have only CRm and Opc1 fields */
8460 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00008461 /* op0 only exists in the AArch64 encodings */
8462 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8463 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8464 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
8465 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8466 * encodes a minimum access level for the register. We roll this
8467 * runtime check into our general permission check code, so check
8468 * here that the reginfo's specified permissions are strict enough
8469 * to encompass the generic architectural permission check.
8470 */
8471 if (r->state != ARM_CP_STATE_AA32) {
8472 int mask = 0;
8473 switch (r->opc1) {
Alex Bennéeb5bd7442019-02-15 09:56:38 +00008474 case 0:
8475 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8476 mask = PL0U_R | PL1_RW;
8477 break;
8478 case 1: case 2:
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00008479 /* min_EL EL1 */
8480 mask = PL1_RW;
8481 break;
8482 case 3:
8483 /* min_EL EL0 */
8484 mask = PL0_RW;
8485 break;
8486 case 4:
Richard Hendersonb4ecf602020-02-07 14:04:25 +00008487 case 5:
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00008488 /* min_EL EL2 */
8489 mask = PL2_RW;
8490 break;
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00008491 case 6:
8492 /* min_EL EL3 */
8493 mask = PL3_RW;
8494 break;
8495 case 7:
8496 /* min_EL EL1, secure mode only (we don't check the latter) */
8497 mask = PL1_RW;
8498 break;
8499 default:
8500 /* broken reginfo with out-of-range opc1 */
8501 assert(false);
8502 break;
8503 }
8504 /* assert our permissions are not too lax (stricter is fine) */
8505 assert((r->access & ~mask) == 0);
8506 }
8507
Peter Maydell4b6a83f2012-06-20 11:57:06 +00008508 /* Check that the register definition has enough info to handle
8509 * reads and writes if they are permitted.
8510 */
8511 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
8512 if (r->access & PL3_R) {
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00008513 assert((r->fieldoffset ||
8514 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8515 r->readfn);
Peter Maydell4b6a83f2012-06-20 11:57:06 +00008516 }
8517 if (r->access & PL3_W) {
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00008518 assert((r->fieldoffset ||
8519 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8520 r->writefn);
Peter Maydell4b6a83f2012-06-20 11:57:06 +00008521 }
8522 }
8523 /* Bad type field probably means missing sentinel at end of reg list */
8524 assert(cptype_valid(r->type));
8525 for (crm = crmmin; crm <= crmmax; crm++) {
8526 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8527 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00008528 for (state = ARM_CP_STATE_AA32;
8529 state <= ARM_CP_STATE_AA64; state++) {
8530 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8531 continue;
8532 }
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00008533 if (state == ARM_CP_STATE_AA32) {
8534 /* Under AArch32 CP registers can be common
8535 * (same for secure and non-secure world) or banked.
8536 */
Abdallah Bouassida9c513e72018-05-18 17:48:07 +01008537 char *name;
8538
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00008539 switch (r->secure) {
8540 case ARM_CP_SECSTATE_S:
8541 case ARM_CP_SECSTATE_NS:
8542 add_cpreg_to_hashtable(cpu, r, opaque, state,
Abdallah Bouassida9c513e72018-05-18 17:48:07 +01008543 r->secure, crm, opc1, opc2,
8544 r->name);
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00008545 break;
8546 default:
Abdallah Bouassida9c513e72018-05-18 17:48:07 +01008547 name = g_strdup_printf("%s_S", r->name);
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00008548 add_cpreg_to_hashtable(cpu, r, opaque, state,
8549 ARM_CP_SECSTATE_S,
Abdallah Bouassida9c513e72018-05-18 17:48:07 +01008550 crm, opc1, opc2, name);
8551 g_free(name);
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00008552 add_cpreg_to_hashtable(cpu, r, opaque, state,
8553 ARM_CP_SECSTATE_NS,
Abdallah Bouassida9c513e72018-05-18 17:48:07 +01008554 crm, opc1, opc2, r->name);
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00008555 break;
8556 }
8557 } else {
8558 /* AArch64 registers get mapped to non-secure instance
8559 * of AArch32 */
8560 add_cpreg_to_hashtable(cpu, r, opaque, state,
8561 ARM_CP_SECSTATE_NS,
Abdallah Bouassida9c513e72018-05-18 17:48:07 +01008562 crm, opc1, opc2, r->name);
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00008563 }
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00008564 }
Peter Maydell4b6a83f2012-06-20 11:57:06 +00008565 }
8566 }
8567 }
8568}
8569
8570void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
8571 const ARMCPRegInfo *regs, void *opaque)
8572{
8573 /* Define a whole list of registers */
8574 const ARMCPRegInfo *r;
8575 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
8576 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
8577 }
8578}
8579
Alex Bennée6c5c0fe2019-02-15 09:56:38 +00008580/*
8581 * Modify ARMCPRegInfo for access from userspace.
8582 *
8583 * This is a data driven modification directed by
8584 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8585 * user-space cannot alter any values and dynamic values pertaining to
8586 * execution state are hidden from user space view anyway.
8587 */
8588void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods)
8589{
8590 const ARMCPRegUserSpaceInfo *m;
8591 ARMCPRegInfo *r;
8592
8593 for (m = mods; m->name; m++) {
Alex Bennéed0402422019-02-15 09:56:39 +00008594 GPatternSpec *pat = NULL;
8595 if (m->is_glob) {
8596 pat = g_pattern_spec_new(m->name);
8597 }
Alex Bennée6c5c0fe2019-02-15 09:56:38 +00008598 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
Alex Bennéed0402422019-02-15 09:56:39 +00008599 if (pat && g_pattern_match_string(pat, r->name)) {
8600 r->type = ARM_CP_CONST;
8601 r->access = PL0U_R;
8602 r->resetvalue = 0;
8603 /* continue */
8604 } else if (strcmp(r->name, m->name) == 0) {
Alex Bennée6c5c0fe2019-02-15 09:56:38 +00008605 r->type = ARM_CP_CONST;
8606 r->access = PL0U_R;
8607 r->resetvalue &= m->exported_bits;
8608 r->resetvalue |= m->fixed_bits;
8609 break;
8610 }
8611 }
Alex Bennéed0402422019-02-15 09:56:39 +00008612 if (pat) {
8613 g_pattern_spec_free(pat);
8614 }
Alex Bennée6c5c0fe2019-02-15 09:56:38 +00008615 }
8616}
8617
Peter Maydell60322b32014-01-04 22:15:44 +00008618const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
Peter Maydell4b6a83f2012-06-20 11:57:06 +00008619{
Peter Maydell60322b32014-01-04 22:15:44 +00008620 return g_hash_table_lookup(cpregs, &encoded_cp);
Peter Maydell4b6a83f2012-06-20 11:57:06 +00008621}
8622
Peter Maydellc4241c72014-02-20 10:35:54 +00008623void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8624 uint64_t value)
Peter Maydell4b6a83f2012-06-20 11:57:06 +00008625{
8626 /* Helper coprocessor write function for write-ignore registers */
Peter Maydell4b6a83f2012-06-20 11:57:06 +00008627}
8628
Peter Maydellc4241c72014-02-20 10:35:54 +00008629uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydell4b6a83f2012-06-20 11:57:06 +00008630{
8631 /* Helper coprocessor write function for read-as-zero registers */
Peter Maydell4b6a83f2012-06-20 11:57:06 +00008632 return 0;
8633}
8634
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00008635void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8636{
8637 /* Helper coprocessor reset function for do-nothing-on-reset registers */
8638}
8639
Peter Maydellaf393ff2016-02-23 15:36:44 +00008640static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
Peter Maydell37064a82012-01-05 15:49:06 +00008641{
8642 /* Return true if it is not valid for us to switch to
8643 * this CPU mode (ie all the UNPREDICTABLE cases in
8644 * the ARM ARM CPSRWriteByInstr pseudocode).
8645 */
Peter Maydellaf393ff2016-02-23 15:36:44 +00008646
8647 /* Changes to or from Hyp via MSR and CPS are illegal. */
8648 if (write_type == CPSRWriteByInstr &&
8649 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8650 mode == ARM_CPU_MODE_HYP)) {
8651 return 1;
8652 }
8653
Peter Maydell37064a82012-01-05 15:49:06 +00008654 switch (mode) {
8655 case ARM_CPU_MODE_USR:
Peter Maydell10eacda2016-02-23 15:36:44 +00008656 return 0;
Peter Maydell37064a82012-01-05 15:49:06 +00008657 case ARM_CPU_MODE_SYS:
8658 case ARM_CPU_MODE_SVC:
8659 case ARM_CPU_MODE_ABT:
8660 case ARM_CPU_MODE_UND:
8661 case ARM_CPU_MODE_IRQ:
8662 case ARM_CPU_MODE_FIQ:
Peter Maydell52ff9512016-02-23 15:36:44 +00008663 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8664 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8665 */
Peter Maydell10eacda2016-02-23 15:36:44 +00008666 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8667 * and CPS are treated as illegal mode changes.
8668 */
8669 if (write_type == CPSRWriteByInstr &&
Peter Maydell10eacda2016-02-23 15:36:44 +00008670 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
Richard Henderson7c208e02018-12-13 13:48:08 +00008671 (arm_hcr_el2_eff(env) & HCR_TGE)) {
Peter Maydell10eacda2016-02-23 15:36:44 +00008672 return 1;
8673 }
Peter Maydell37064a82012-01-05 15:49:06 +00008674 return 0;
Peter Maydelle6c8fc02016-02-23 15:36:44 +00008675 case ARM_CPU_MODE_HYP:
8676 return !arm_feature(env, ARM_FEATURE_EL2)
Alexander Graf2d2a4542019-01-21 10:23:11 +00008677 || arm_current_el(env) < 2 || arm_is_secure_below_el3(env);
Sergey Fedorov027fc522014-10-24 12:19:14 +01008678 case ARM_CPU_MODE_MON:
Peter Maydell58ae2d12016-02-23 15:36:44 +00008679 return arm_current_el(env) < 3;
Peter Maydell37064a82012-01-05 15:49:06 +00008680 default:
8681 return 1;
8682 }
8683}
8684
balrog2f4a40e2007-11-13 01:50:15 +00008685uint32_t cpsr_read(CPUARMState *env)
8686{
8687 int ZF;
pbrook6fbe23d2008-04-01 17:19:11 +00008688 ZF = (env->ZF == 0);
8689 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
balrog2f4a40e2007-11-13 01:50:15 +00008690 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
8691 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
8692 | ((env->condexec_bits & 0xfc) << 8)
Peter Maydellaf519932014-03-10 14:56:28 +00008693 | (env->GE << 16) | (env->daif & CPSR_AIF);
balrog2f4a40e2007-11-13 01:50:15 +00008694}
8695
Peter Maydell50866ba2016-02-23 15:36:43 +00008696void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
8697 CPSRWriteType write_type)
balrog2f4a40e2007-11-13 01:50:15 +00008698{
Fabian Aggeler6e8801f2014-12-11 12:07:50 +00008699 uint32_t changed_daif;
8700
balrog2f4a40e2007-11-13 01:50:15 +00008701 if (mask & CPSR_NZCV) {
pbrook6fbe23d2008-04-01 17:19:11 +00008702 env->ZF = (~val) & CPSR_Z;
8703 env->NF = val;
balrog2f4a40e2007-11-13 01:50:15 +00008704 env->CF = (val >> 29) & 1;
8705 env->VF = (val << 3) & 0x80000000;
8706 }
8707 if (mask & CPSR_Q)
8708 env->QF = ((val & CPSR_Q) != 0);
8709 if (mask & CPSR_T)
8710 env->thumb = ((val & CPSR_T) != 0);
8711 if (mask & CPSR_IT_0_1) {
8712 env->condexec_bits &= ~3;
8713 env->condexec_bits |= (val >> 25) & 3;
8714 }
8715 if (mask & CPSR_IT_2_7) {
8716 env->condexec_bits &= 3;
8717 env->condexec_bits |= (val >> 8) & 0xfc;
8718 }
8719 if (mask & CPSR_GE) {
8720 env->GE = (val >> 16) & 0xf;
8721 }
8722
Fabian Aggeler6e8801f2014-12-11 12:07:50 +00008723 /* In a V7 implementation that includes the security extensions but does
8724 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
8725 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
8726 * bits respectively.
8727 *
8728 * In a V8 implementation, it is permitted for privileged software to
8729 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
8730 */
Peter Maydellf8c88bb2016-02-23 15:36:43 +00008731 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
Fabian Aggeler6e8801f2014-12-11 12:07:50 +00008732 arm_feature(env, ARM_FEATURE_EL3) &&
8733 !arm_feature(env, ARM_FEATURE_EL2) &&
8734 !arm_is_secure(env)) {
8735
8736 changed_daif = (env->daif ^ val) & mask;
8737
8738 if (changed_daif & CPSR_A) {
8739 /* Check to see if we are allowed to change the masking of async
8740 * abort exceptions from a non-secure state.
8741 */
8742 if (!(env->cp15.scr_el3 & SCR_AW)) {
8743 qemu_log_mask(LOG_GUEST_ERROR,
8744 "Ignoring attempt to switch CPSR_A flag from "
8745 "non-secure world with SCR.AW bit clear\n");
8746 mask &= ~CPSR_A;
8747 }
8748 }
8749
8750 if (changed_daif & CPSR_F) {
8751 /* Check to see if we are allowed to change the masking of FIQ
8752 * exceptions from a non-secure state.
8753 */
8754 if (!(env->cp15.scr_el3 & SCR_FW)) {
8755 qemu_log_mask(LOG_GUEST_ERROR,
8756 "Ignoring attempt to switch CPSR_F flag from "
8757 "non-secure world with SCR.FW bit clear\n");
8758 mask &= ~CPSR_F;
8759 }
8760
8761 /* Check whether non-maskable FIQ (NMFI) support is enabled.
8762 * If this bit is set software is not allowed to mask
8763 * FIQs, but is allowed to set CPSR_F to 0.
8764 */
8765 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
8766 (val & CPSR_F)) {
8767 qemu_log_mask(LOG_GUEST_ERROR,
8768 "Ignoring attempt to enable CPSR_F flag "
8769 "(non-maskable FIQ [NMFI] support enabled)\n");
8770 mask &= ~CPSR_F;
8771 }
8772 }
8773 }
8774
Peter Maydell4cc35612014-02-26 17:20:06 +00008775 env->daif &= ~(CPSR_AIF & mask);
8776 env->daif |= val & CPSR_AIF & mask;
8777
Peter Maydellf8c88bb2016-02-23 15:36:43 +00008778 if (write_type != CPSRWriteRaw &&
8779 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
Peter Maydell8c4f0eb2016-03-04 11:30:16 +00008780 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
8781 /* Note that we can only get here in USR mode if this is a
8782 * gdb stub write; for this case we follow the architectural
8783 * behaviour for guest writes in USR mode of ignoring an attempt
8784 * to switch mode. (Those are caught by translate.c for writes
8785 * triggered by guest instructions.)
8786 */
8787 mask &= ~CPSR_M;
8788 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
Peter Maydell81907a52016-02-23 15:36:44 +00008789 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
8790 * v7, and has defined behaviour in v8:
8791 * + leave CPSR.M untouched
8792 * + allow changes to the other CPSR fields
8793 * + set PSTATE.IL
8794 * For user changes via the GDB stub, we don't set PSTATE.IL,
8795 * as this would be unnecessarily harsh for a user error.
Peter Maydell37064a82012-01-05 15:49:06 +00008796 */
8797 mask &= ~CPSR_M;
Peter Maydell81907a52016-02-23 15:36:44 +00008798 if (write_type != CPSRWriteByGDBStub &&
8799 arm_feature(env, ARM_FEATURE_V8)) {
8800 mask |= CPSR_IL;
8801 val |= CPSR_IL;
8802 }
Peter Maydell81e37282018-10-24 07:50:17 +01008803 qemu_log_mask(LOG_GUEST_ERROR,
8804 "Illegal AArch32 mode switch attempt from %s to %s\n",
8805 aarch32_mode_name(env->uncached_cpsr),
8806 aarch32_mode_name(val));
Peter Maydell37064a82012-01-05 15:49:06 +00008807 } else {
Peter Maydell81e37282018-10-24 07:50:17 +01008808 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
8809 write_type == CPSRWriteExceptionReturn ?
8810 "Exception return from AArch32" :
8811 "AArch32 mode switch from",
8812 aarch32_mode_name(env->uncached_cpsr),
8813 aarch32_mode_name(val), env->regs[15]);
Peter Maydell37064a82012-01-05 15:49:06 +00008814 switch_mode(env, val & CPSR_M);
8815 }
balrog2f4a40e2007-11-13 01:50:15 +00008816 }
8817 mask &= ~CACHED_CPSR_BITS;
8818 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
8819}
8820
pbrookb26eefb2008-03-31 03:44:26 +00008821/* Sign/zero extend */
8822uint32_t HELPER(sxtb16)(uint32_t x)
8823{
8824 uint32_t res;
8825 res = (uint16_t)(int8_t)x;
8826 res |= (uint32_t)(int8_t)(x >> 16) << 16;
8827 return res;
8828}
8829
8830uint32_t HELPER(uxtb16)(uint32_t x)
8831{
8832 uint32_t res;
8833 res = (uint16_t)(uint8_t)x;
8834 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
8835 return res;
8836}
8837
pbrook36706692008-03-31 03:46:19 +00008838int32_t HELPER(sdiv)(int32_t num, int32_t den)
8839{
8840 if (den == 0)
8841 return 0;
Aurelien Jarno686eeb92009-10-15 23:08:46 +02008842 if (num == INT_MIN && den == -1)
8843 return INT_MIN;
pbrook36706692008-03-31 03:46:19 +00008844 return num / den;
8845}
8846
8847uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
8848{
8849 if (den == 0)
8850 return 0;
8851 return num / den;
8852}
8853
8854uint32_t HELPER(rbit)(uint32_t x)
8855{
Richard Henderson42fedbc2015-09-14 13:38:53 -07008856 return revbit32(x);
pbrook36706692008-03-31 03:46:19 +00008857}
8858
Peter Maydellc47eaf9f2019-02-05 16:52:40 +00008859#ifdef CONFIG_USER_ONLY
bellardb5ff1b32005-11-26 10:38:39 +00008860
Peter Maydellaffdb642018-10-24 07:50:17 +01008861static void switch_mode(CPUARMState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +00008862{
Richard Henderson2fc0cc02019-03-22 17:41:14 -07008863 ARMCPU *cpu = env_archcpu(env);
Andreas Färbera47dddd2013-09-03 17:38:47 +02008864
8865 if (mode != ARM_CPU_MODE_USR) {
8866 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
8867 }
bellardb5ff1b32005-11-26 10:38:39 +00008868}
8869
Greg Bellows012a9062015-05-29 11:28:51 +01008870uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
8871 uint32_t cur_el, bool secure)
Edgar E. Iglesias9e729b52014-09-29 18:48:49 +01008872{
8873 return 1;
8874}
8875
Greg Bellowsce020492015-02-13 05:46:08 +00008876void aarch64_sync_64_to_32(CPUARMState *env)
8877{
8878 g_assert_not_reached();
8879}
8880
bellardb5ff1b32005-11-26 10:38:39 +00008881#else
8882
Peter Maydellaffdb642018-10-24 07:50:17 +01008883static void switch_mode(CPUARMState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +00008884{
8885 int old_mode;
8886 int i;
8887
8888 old_mode = env->uncached_cpsr & CPSR_M;
8889 if (mode == old_mode)
8890 return;
8891
8892 if (old_mode == ARM_CPU_MODE_FIQ) {
8893 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +00008894 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +00008895 } else if (mode == ARM_CPU_MODE_FIQ) {
8896 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +00008897 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +00008898 }
8899
Peter Maydellf5206412013-03-05 00:34:40 +00008900 i = bank_number(old_mode);
bellardb5ff1b32005-11-26 10:38:39 +00008901 env->banked_r13[i] = env->regs[13];
bellardb5ff1b32005-11-26 10:38:39 +00008902 env->banked_spsr[i] = env->spsr;
8903
Peter Maydellf5206412013-03-05 00:34:40 +00008904 i = bank_number(mode);
bellardb5ff1b32005-11-26 10:38:39 +00008905 env->regs[13] = env->banked_r13[i];
bellardb5ff1b32005-11-26 10:38:39 +00008906 env->spsr = env->banked_spsr[i];
Peter Maydell593cfa22018-11-13 10:47:59 +00008907
8908 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
8909 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
bellardb5ff1b32005-11-26 10:38:39 +00008910}
8911
Greg Bellows0eeb17d2014-12-11 12:07:48 +00008912/* Physical Interrupt Target EL Lookup Table
8913 *
8914 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
8915 *
8916 * The below multi-dimensional table is used for looking up the target
8917 * exception level given numerous condition criteria. Specifically, the
8918 * target EL is based on SCR and HCR routing controls as well as the
8919 * currently executing EL and secure state.
8920 *
8921 * Dimensions:
8922 * target_el_table[2][2][2][2][2][4]
8923 * | | | | | +--- Current EL
8924 * | | | | +------ Non-secure(0)/Secure(1)
8925 * | | | +--------- HCR mask override
8926 * | | +------------ SCR exec state control
8927 * | +--------------- SCR mask override
8928 * +------------------ 32-bit(0)/64-bit(1) EL3
8929 *
8930 * The table values are as such:
8931 * 0-3 = EL0-EL3
8932 * -1 = Cannot occur
8933 *
8934 * The ARM ARM target EL table includes entries indicating that an "exception
8935 * is not taken". The two cases where this is applicable are:
8936 * 1) An exception is taken from EL3 but the SCR does not have the exception
8937 * routed to EL3.
8938 * 2) An exception is taken from EL2 but the HCR does not have the exception
8939 * routed to EL2.
8940 * In these two cases, the below table contain a target of EL1. This value is
8941 * returned as it is expected that the consumer of the table data will check
8942 * for "target EL >= current EL" to ensure the exception is not taken.
8943 *
8944 * SCR HCR
8945 * 64 EA AMO From
8946 * BIT IRQ IMO Non-secure Secure
8947 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
8948 */
Stefan Weil82c39f62015-10-16 11:14:52 +01008949static const int8_t target_el_table[2][2][2][2][2][4] = {
Greg Bellows0eeb17d2014-12-11 12:07:48 +00008950 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
8951 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
8952 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
8953 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
8954 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
8955 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
8956 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
8957 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
8958 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
8959 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
8960 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
8961 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
8962 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
8963 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
8964 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
8965 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
8966};
8967
8968/*
8969 * Determine the target EL for physical exceptions
8970 */
Greg Bellows012a9062015-05-29 11:28:51 +01008971uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
8972 uint32_t cur_el, bool secure)
Greg Bellows0eeb17d2014-12-11 12:07:48 +00008973{
8974 CPUARMState *env = cs->env_ptr;
Richard Hendersonf7778442018-12-13 13:48:07 +00008975 bool rw;
8976 bool scr;
8977 bool hcr;
Greg Bellows0eeb17d2014-12-11 12:07:48 +00008978 int target_el;
Sergey Sorokin2cde0312015-10-16 11:14:52 +01008979 /* Is the highest EL AArch64? */
Richard Hendersonf7778442018-12-13 13:48:07 +00008980 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
8981 uint64_t hcr_el2;
Sergey Sorokin2cde0312015-10-16 11:14:52 +01008982
8983 if (arm_feature(env, ARM_FEATURE_EL3)) {
8984 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
8985 } else {
8986 /* Either EL2 is the highest EL (and so the EL2 register width
8987 * is given by is64); or there is no EL2 or EL3, in which case
8988 * the value of 'rw' does not affect the table lookup anyway.
8989 */
8990 rw = is64;
8991 }
Greg Bellows0eeb17d2014-12-11 12:07:48 +00008992
Richard Hendersonf7778442018-12-13 13:48:07 +00008993 hcr_el2 = arm_hcr_el2_eff(env);
Greg Bellows0eeb17d2014-12-11 12:07:48 +00008994 switch (excp_idx) {
8995 case EXCP_IRQ:
8996 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
Richard Hendersonf7778442018-12-13 13:48:07 +00008997 hcr = hcr_el2 & HCR_IMO;
Greg Bellows0eeb17d2014-12-11 12:07:48 +00008998 break;
8999 case EXCP_FIQ:
9000 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
Richard Hendersonf7778442018-12-13 13:48:07 +00009001 hcr = hcr_el2 & HCR_FMO;
Greg Bellows0eeb17d2014-12-11 12:07:48 +00009002 break;
9003 default:
9004 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
Richard Hendersonf7778442018-12-13 13:48:07 +00009005 hcr = hcr_el2 & HCR_AMO;
Greg Bellows0eeb17d2014-12-11 12:07:48 +00009006 break;
9007 };
9008
Richard Hendersond1b31422020-02-07 14:04:26 +00009009 /*
9010 * For these purposes, TGE and AMO/IMO/FMO both force the
9011 * interrupt to EL2. Fold TGE into the bit extracted above.
9012 */
9013 hcr |= (hcr_el2 & HCR_TGE) != 0;
9014
Greg Bellows0eeb17d2014-12-11 12:07:48 +00009015 /* Perform a table-lookup for the target EL given the current state */
9016 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9017
9018 assert(target_el > 0);
9019
9020 return target_el;
9021}
9022
Philippe Mathieu-Daudéb59f4792019-07-01 17:26:22 +01009023void arm_log_exception(int idx)
9024{
9025 if (qemu_loglevel_mask(CPU_LOG_INT)) {
9026 const char *exc = NULL;
9027 static const char * const excnames[] = {
9028 [EXCP_UDEF] = "Undefined Instruction",
9029 [EXCP_SWI] = "SVC",
9030 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9031 [EXCP_DATA_ABORT] = "Data Abort",
9032 [EXCP_IRQ] = "IRQ",
9033 [EXCP_FIQ] = "FIQ",
9034 [EXCP_BKPT] = "Breakpoint",
9035 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9036 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9037 [EXCP_HVC] = "Hypervisor Call",
9038 [EXCP_HYP_TRAP] = "Hypervisor Trap",
9039 [EXCP_SMC] = "Secure Monitor Call",
9040 [EXCP_VIRQ] = "Virtual IRQ",
9041 [EXCP_VFIQ] = "Virtual FIQ",
9042 [EXCP_SEMIHOST] = "Semihosting call",
9043 [EXCP_NOCP] = "v7M NOCP UsageFault",
9044 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9045 [EXCP_STKOF] = "v8M STKOF UsageFault",
9046 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9047 [EXCP_LSERR] = "v8M LSERR UsageFault",
9048 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9049 };
9050
9051 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9052 exc = excnames[idx];
9053 }
9054 if (!exc) {
9055 exc = "unknown";
9056 }
9057 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
9058 }
9059}
9060
Peter Maydelld87513c2019-04-29 17:35:58 +01009061/*
Philippe Mathieu-Daudé9a223092019-07-01 17:26:20 +01009062 * Function used to synchronize QEMU's AArch64 register set with AArch32
Greg Bellowsce020492015-02-13 05:46:08 +00009063 * register set. This is necessary when switching between AArch32 and AArch64
9064 * execution state.
9065 */
9066void aarch64_sync_32_to_64(CPUARMState *env)
9067{
9068 int i;
9069 uint32_t mode = env->uncached_cpsr & CPSR_M;
9070
9071 /* We can blanket copy R[0:7] to X[0:7] */
9072 for (i = 0; i < 8; i++) {
9073 env->xregs[i] = env->regs[i];
9074 }
9075
Philippe Mathieu-Daudé9a223092019-07-01 17:26:20 +01009076 /*
9077 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
Greg Bellowsce020492015-02-13 05:46:08 +00009078 * Otherwise, they come from the banked user regs.
9079 */
9080 if (mode == ARM_CPU_MODE_FIQ) {
9081 for (i = 8; i < 13; i++) {
9082 env->xregs[i] = env->usr_regs[i - 8];
9083 }
9084 } else {
9085 for (i = 8; i < 13; i++) {
9086 env->xregs[i] = env->regs[i];
9087 }
9088 }
9089
Philippe Mathieu-Daudé9a223092019-07-01 17:26:20 +01009090 /*
9091 * Registers x13-x23 are the various mode SP and FP registers. Registers
Greg Bellowsce020492015-02-13 05:46:08 +00009092 * r13 and r14 are only copied if we are in that mode, otherwise we copy
9093 * from the mode banked register.
9094 */
9095 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9096 env->xregs[13] = env->regs[13];
9097 env->xregs[14] = env->regs[14];
9098 } else {
9099 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9100 /* HYP is an exception in that it is copied from r14 */
9101 if (mode == ARM_CPU_MODE_HYP) {
9102 env->xregs[14] = env->regs[14];
9103 } else {
Peter Maydell593cfa22018-11-13 10:47:59 +00009104 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
Greg Bellowsce020492015-02-13 05:46:08 +00009105 }
9106 }
9107
9108 if (mode == ARM_CPU_MODE_HYP) {
9109 env->xregs[15] = env->regs[13];
9110 } else {
9111 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9112 }
9113
9114 if (mode == ARM_CPU_MODE_IRQ) {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01009115 env->xregs[16] = env->regs[14];
9116 env->xregs[17] = env->regs[13];
Greg Bellowsce020492015-02-13 05:46:08 +00009117 } else {
Peter Maydell593cfa22018-11-13 10:47:59 +00009118 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01009119 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
Greg Bellowsce020492015-02-13 05:46:08 +00009120 }
9121
9122 if (mode == ARM_CPU_MODE_SVC) {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01009123 env->xregs[18] = env->regs[14];
9124 env->xregs[19] = env->regs[13];
Greg Bellowsce020492015-02-13 05:46:08 +00009125 } else {
Peter Maydell593cfa22018-11-13 10:47:59 +00009126 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01009127 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
Greg Bellowsce020492015-02-13 05:46:08 +00009128 }
9129
9130 if (mode == ARM_CPU_MODE_ABT) {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01009131 env->xregs[20] = env->regs[14];
9132 env->xregs[21] = env->regs[13];
Greg Bellowsce020492015-02-13 05:46:08 +00009133 } else {
Peter Maydell593cfa22018-11-13 10:47:59 +00009134 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01009135 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
Greg Bellowsce020492015-02-13 05:46:08 +00009136 }
9137
9138 if (mode == ARM_CPU_MODE_UND) {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01009139 env->xregs[22] = env->regs[14];
9140 env->xregs[23] = env->regs[13];
Greg Bellowsce020492015-02-13 05:46:08 +00009141 } else {
Peter Maydell593cfa22018-11-13 10:47:59 +00009142 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01009143 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
Greg Bellowsce020492015-02-13 05:46:08 +00009144 }
9145
Philippe Mathieu-Daudé9a223092019-07-01 17:26:20 +01009146 /*
9147 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
Greg Bellowsce020492015-02-13 05:46:08 +00009148 * mode, then we can copy from r8-r14. Otherwise, we copy from the
9149 * FIQ bank for r8-r14.
9150 */
9151 if (mode == ARM_CPU_MODE_FIQ) {
9152 for (i = 24; i < 31; i++) {
9153 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
9154 }
9155 } else {
9156 for (i = 24; i < 29; i++) {
9157 env->xregs[i] = env->fiq_regs[i - 24];
9158 }
9159 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
Peter Maydell593cfa22018-11-13 10:47:59 +00009160 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
Greg Bellowsce020492015-02-13 05:46:08 +00009161 }
9162
9163 env->pc = env->regs[15];
9164}
9165
Philippe Mathieu-Daudé9a223092019-07-01 17:26:20 +01009166/*
9167 * Function used to synchronize QEMU's AArch32 register set with AArch64
Greg Bellowsce020492015-02-13 05:46:08 +00009168 * register set. This is necessary when switching between AArch32 and AArch64
9169 * execution state.
9170 */
9171void aarch64_sync_64_to_32(CPUARMState *env)
9172{
9173 int i;
9174 uint32_t mode = env->uncached_cpsr & CPSR_M;
9175
9176 /* We can blanket copy X[0:7] to R[0:7] */
9177 for (i = 0; i < 8; i++) {
9178 env->regs[i] = env->xregs[i];
9179 }
9180
Philippe Mathieu-Daudé9a223092019-07-01 17:26:20 +01009181 /*
9182 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
Greg Bellowsce020492015-02-13 05:46:08 +00009183 * Otherwise, we copy x8-x12 into the banked user regs.
9184 */
9185 if (mode == ARM_CPU_MODE_FIQ) {
9186 for (i = 8; i < 13; i++) {
9187 env->usr_regs[i - 8] = env->xregs[i];
9188 }
9189 } else {
9190 for (i = 8; i < 13; i++) {
9191 env->regs[i] = env->xregs[i];
9192 }
9193 }
9194
Philippe Mathieu-Daudé9a223092019-07-01 17:26:20 +01009195 /*
9196 * Registers r13 & r14 depend on the current mode.
Greg Bellowsce020492015-02-13 05:46:08 +00009197 * If we are in a given mode, we copy the corresponding x registers to r13
9198 * and r14. Otherwise, we copy the x register to the banked r13 and r14
9199 * for the mode.
9200 */
9201 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9202 env->regs[13] = env->xregs[13];
9203 env->regs[14] = env->xregs[14];
9204 } else {
9205 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
9206
Philippe Mathieu-Daudé9a223092019-07-01 17:26:20 +01009207 /*
9208 * HYP is an exception in that it does not have its own banked r14 but
Greg Bellowsce020492015-02-13 05:46:08 +00009209 * shares the USR r14
9210 */
9211 if (mode == ARM_CPU_MODE_HYP) {
9212 env->regs[14] = env->xregs[14];
9213 } else {
Peter Maydell593cfa22018-11-13 10:47:59 +00009214 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
Greg Bellowsce020492015-02-13 05:46:08 +00009215 }
9216 }
9217
9218 if (mode == ARM_CPU_MODE_HYP) {
9219 env->regs[13] = env->xregs[15];
9220 } else {
9221 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
9222 }
9223
9224 if (mode == ARM_CPU_MODE_IRQ) {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01009225 env->regs[14] = env->xregs[16];
9226 env->regs[13] = env->xregs[17];
Greg Bellowsce020492015-02-13 05:46:08 +00009227 } else {
Peter Maydell593cfa22018-11-13 10:47:59 +00009228 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01009229 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
Greg Bellowsce020492015-02-13 05:46:08 +00009230 }
9231
9232 if (mode == ARM_CPU_MODE_SVC) {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01009233 env->regs[14] = env->xregs[18];
9234 env->regs[13] = env->xregs[19];
Greg Bellowsce020492015-02-13 05:46:08 +00009235 } else {
Peter Maydell593cfa22018-11-13 10:47:59 +00009236 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01009237 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
Greg Bellowsce020492015-02-13 05:46:08 +00009238 }
9239
9240 if (mode == ARM_CPU_MODE_ABT) {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01009241 env->regs[14] = env->xregs[20];
9242 env->regs[13] = env->xregs[21];
Greg Bellowsce020492015-02-13 05:46:08 +00009243 } else {
Peter Maydell593cfa22018-11-13 10:47:59 +00009244 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01009245 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
Greg Bellowsce020492015-02-13 05:46:08 +00009246 }
9247
9248 if (mode == ARM_CPU_MODE_UND) {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01009249 env->regs[14] = env->xregs[22];
9250 env->regs[13] = env->xregs[23];
Greg Bellowsce020492015-02-13 05:46:08 +00009251 } else {
Peter Maydell593cfa22018-11-13 10:47:59 +00009252 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01009253 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
Greg Bellowsce020492015-02-13 05:46:08 +00009254 }
9255
9256 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9257 * mode, then we can copy to r8-r14. Otherwise, we copy to the
9258 * FIQ bank for r8-r14.
9259 */
9260 if (mode == ARM_CPU_MODE_FIQ) {
9261 for (i = 24; i < 31; i++) {
9262 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
9263 }
9264 } else {
9265 for (i = 24; i < 29; i++) {
9266 env->fiq_regs[i - 24] = env->xregs[i];
9267 }
9268 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
Peter Maydell593cfa22018-11-13 10:47:59 +00009269 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
Greg Bellowsce020492015-02-13 05:46:08 +00009270 }
9271
9272 env->regs[15] = env->pc;
9273}
9274
Peter Maydelldea83782018-08-24 13:17:37 +01009275static void take_aarch32_exception(CPUARMState *env, int new_mode,
9276 uint32_t mask, uint32_t offset,
9277 uint32_t newpc)
9278{
Richard Henderson4a2696c2020-02-08 12:58:10 +00009279 int new_el;
9280
Peter Maydelldea83782018-08-24 13:17:37 +01009281 /* Change the CPU state so as to actually take the exception. */
9282 switch_mode(env, new_mode);
Richard Henderson4a2696c2020-02-08 12:58:10 +00009283
Peter Maydelldea83782018-08-24 13:17:37 +01009284 /*
9285 * For exceptions taken to AArch32 we must clear the SS bit in both
9286 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9287 */
9288 env->uncached_cpsr &= ~PSTATE_SS;
9289 env->spsr = cpsr_read(env);
9290 /* Clear IT bits. */
9291 env->condexec_bits = 0;
9292 /* Switch to the new mode, and to the correct instruction set. */
9293 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
Changbin Du88828bf2020-03-28 22:02:32 +08009294
9295 /* This must be after mode switching. */
9296 new_el = arm_current_el(env);
9297
Peter Maydelldea83782018-08-24 13:17:37 +01009298 /* Set new mode endianness */
9299 env->uncached_cpsr &= ~CPSR_E;
Richard Henderson4a2696c2020-02-08 12:58:10 +00009300 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
Peter Maydelldea83782018-08-24 13:17:37 +01009301 env->uncached_cpsr |= CPSR_E;
9302 }
Peter Maydell829f9fd2018-08-24 13:17:38 +01009303 /* J and IL must always be cleared for exception entry */
9304 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
Peter Maydelldea83782018-08-24 13:17:37 +01009305 env->daif |= mask;
9306
9307 if (new_mode == ARM_CPU_MODE_HYP) {
9308 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9309 env->elr_el[2] = env->regs[15];
9310 } else {
Richard Henderson4a2696c2020-02-08 12:58:10 +00009311 /* CPSR.PAN is normally preserved preserved unless... */
Peter Maydellf8af1142020-02-14 17:50:57 +00009312 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
Richard Henderson4a2696c2020-02-08 12:58:10 +00009313 switch (new_el) {
9314 case 3:
9315 if (!arm_is_secure_below_el3(env)) {
9316 /* ... the target is EL3, from non-secure state. */
9317 env->uncached_cpsr &= ~CPSR_PAN;
9318 break;
9319 }
9320 /* ... the target is EL3, from secure state ... */
9321 /* fall through */
9322 case 1:
9323 /* ... the target is EL1 and SCTLR.SPAN is 0. */
9324 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9325 env->uncached_cpsr |= CPSR_PAN;
9326 }
9327 break;
9328 }
9329 }
Peter Maydelldea83782018-08-24 13:17:37 +01009330 /*
9331 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9332 * and we should just guard the thumb mode on V4
9333 */
9334 if (arm_feature(env, ARM_FEATURE_V4T)) {
9335 env->thumb =
9336 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9337 }
9338 env->regs[14] = env->regs[15] + offset;
9339 }
9340 env->regs[15] = newpc;
Richard Hendersona8a79c72019-10-23 11:00:49 -04009341 arm_rebuild_hflags(env);
Peter Maydelldea83782018-08-24 13:17:37 +01009342}
9343
Peter Maydellb9bc21f2018-08-24 13:17:38 +01009344static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9345{
9346 /*
9347 * Handle exception entry to Hyp mode; this is sufficiently
9348 * different to entry to other AArch32 modes that we handle it
9349 * separately here.
9350 *
9351 * The vector table entry used is always the 0x14 Hyp mode entry point,
9352 * unless this is an UNDEF/HVC/abort taken from Hyp to Hyp.
9353 * The offset applied to the preferred return address is always zero
9354 * (see DDI0487C.a section G1.12.3).
9355 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9356 */
9357 uint32_t addr, mask;
9358 ARMCPU *cpu = ARM_CPU(cs);
9359 CPUARMState *env = &cpu->env;
9360
9361 switch (cs->exception_index) {
9362 case EXCP_UDEF:
9363 addr = 0x04;
9364 break;
9365 case EXCP_SWI:
9366 addr = 0x14;
9367 break;
9368 case EXCP_BKPT:
9369 /* Fall through to prefetch abort. */
9370 case EXCP_PREFETCH_ABORT:
9371 env->cp15.ifar_s = env->exception.vaddress;
9372 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9373 (uint32_t)env->exception.vaddress);
9374 addr = 0x0c;
9375 break;
9376 case EXCP_DATA_ABORT:
9377 env->cp15.dfar_s = env->exception.vaddress;
9378 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9379 (uint32_t)env->exception.vaddress);
9380 addr = 0x10;
9381 break;
9382 case EXCP_IRQ:
9383 addr = 0x18;
9384 break;
9385 case EXCP_FIQ:
9386 addr = 0x1c;
9387 break;
9388 case EXCP_HVC:
9389 addr = 0x08;
9390 break;
9391 case EXCP_HYP_TRAP:
9392 addr = 0x14;
Philippe Mathieu-Daudé9bbb4ef2019-07-22 14:07:38 +01009393 break;
Peter Maydellb9bc21f2018-08-24 13:17:38 +01009394 default:
9395 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9396 }
9397
9398 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
Peter Maydell2ed08182018-10-24 07:50:18 +01009399 if (!arm_feature(env, ARM_FEATURE_V8)) {
9400 /*
9401 * QEMU syndrome values are v8-style. v7 has the IL bit
9402 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9403 * If this is a v7 CPU, squash the IL bit in those cases.
9404 */
9405 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9406 (cs->exception_index == EXCP_DATA_ABORT &&
9407 !(env->exception.syndrome & ARM_EL_ISV)) ||
9408 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9409 env->exception.syndrome &= ~ARM_EL_IL;
9410 }
9411 }
Peter Maydellb9bc21f2018-08-24 13:17:38 +01009412 env->cp15.esr_el[2] = env->exception.syndrome;
9413 }
9414
9415 if (arm_current_el(env) != 2 && addr < 0x14) {
9416 addr = 0x14;
9417 }
9418
9419 mask = 0;
9420 if (!(env->cp15.scr_el3 & SCR_EA)) {
9421 mask |= CPSR_A;
9422 }
9423 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9424 mask |= CPSR_I;
9425 }
9426 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9427 mask |= CPSR_F;
9428 }
9429
9430 addr += env->cp15.hvbar;
9431
9432 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9433}
9434
Peter Maydell966f7582016-01-21 14:15:08 +00009435static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
bellardb5ff1b32005-11-26 10:38:39 +00009436{
Andreas Färber97a8ea52013-02-02 10:57:51 +01009437 ARMCPU *cpu = ARM_CPU(cs);
9438 CPUARMState *env = &cpu->env;
bellardb5ff1b32005-11-26 10:38:39 +00009439 uint32_t addr;
9440 uint32_t mask;
9441 int new_mode;
9442 uint32_t offset;
Peter Maydell16a906f2014-09-12 14:06:49 +01009443 uint32_t moe;
bellardb5ff1b32005-11-26 10:38:39 +00009444
Peter Maydell16a906f2014-09-12 14:06:49 +01009445 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
Peter Maydell64b91e32018-10-24 07:50:18 +01009446 switch (syn_get_ec(env->exception.syndrome)) {
Peter Maydell16a906f2014-09-12 14:06:49 +01009447 case EC_BREAKPOINT:
9448 case EC_BREAKPOINT_SAME_EL:
9449 moe = 1;
9450 break;
9451 case EC_WATCHPOINT:
9452 case EC_WATCHPOINT_SAME_EL:
9453 moe = 10;
9454 break;
9455 case EC_AA32_BKPT:
9456 moe = 3;
9457 break;
9458 case EC_VECTORCATCH:
9459 moe = 5;
9460 break;
9461 default:
9462 moe = 0;
9463 break;
9464 }
9465
9466 if (moe) {
9467 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9468 }
9469
Peter Maydellb9bc21f2018-08-24 13:17:38 +01009470 if (env->exception.target_el == 2) {
9471 arm_cpu_do_interrupt_aarch32_hyp(cs);
9472 return;
9473 }
9474
Andreas Färber27103422013-08-26 08:31:06 +02009475 switch (cs->exception_index) {
bellardb5ff1b32005-11-26 10:38:39 +00009476 case EXCP_UDEF:
9477 new_mode = ARM_CPU_MODE_UND;
9478 addr = 0x04;
9479 mask = CPSR_I;
9480 if (env->thumb)
9481 offset = 2;
9482 else
9483 offset = 4;
9484 break;
9485 case EXCP_SWI:
9486 new_mode = ARM_CPU_MODE_SVC;
9487 addr = 0x08;
9488 mask = CPSR_I;
balrog601d70b2008-04-20 01:03:45 +00009489 /* The PC already points to the next instruction. */
bellardb5ff1b32005-11-26 10:38:39 +00009490 offset = 0;
9491 break;
pbrook06c949e2006-02-04 19:35:26 +00009492 case EXCP_BKPT:
pbrook9ee6e8b2007-11-11 00:04:49 +00009493 /* Fall through to prefetch abort. */
9494 case EXCP_PREFETCH_ABORT:
Fabian Aggeler88ca1c22014-12-11 12:07:51 +00009495 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
Fabian Aggelerb848ce22014-12-11 12:07:51 +00009496 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
Peter Maydell3f1beac2013-08-20 14:54:28 +01009497 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
Fabian Aggeler88ca1c22014-12-11 12:07:51 +00009498 env->exception.fsr, (uint32_t)env->exception.vaddress);
bellardb5ff1b32005-11-26 10:38:39 +00009499 new_mode = ARM_CPU_MODE_ABT;
9500 addr = 0x0c;
9501 mask = CPSR_A | CPSR_I;
9502 offset = 4;
9503 break;
9504 case EXCP_DATA_ABORT:
Fabian Aggeler4a7e2d72014-12-11 12:07:51 +00009505 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
Fabian Aggelerb848ce22014-12-11 12:07:51 +00009506 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
Peter Maydell3f1beac2013-08-20 14:54:28 +01009507 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
Fabian Aggeler4a7e2d72014-12-11 12:07:51 +00009508 env->exception.fsr,
Rob Herring6cd8a262014-04-15 19:18:42 +01009509 (uint32_t)env->exception.vaddress);
bellardb5ff1b32005-11-26 10:38:39 +00009510 new_mode = ARM_CPU_MODE_ABT;
9511 addr = 0x10;
9512 mask = CPSR_A | CPSR_I;
9513 offset = 8;
9514 break;
9515 case EXCP_IRQ:
9516 new_mode = ARM_CPU_MODE_IRQ;
9517 addr = 0x18;
9518 /* Disable IRQ and imprecise data aborts. */
9519 mask = CPSR_A | CPSR_I;
9520 offset = 4;
Fabian Aggelerde38d232014-12-11 12:07:49 +00009521 if (env->cp15.scr_el3 & SCR_IRQ) {
9522 /* IRQ routed to monitor mode */
9523 new_mode = ARM_CPU_MODE_MON;
9524 mask |= CPSR_F;
9525 }
bellardb5ff1b32005-11-26 10:38:39 +00009526 break;
9527 case EXCP_FIQ:
9528 new_mode = ARM_CPU_MODE_FIQ;
9529 addr = 0x1c;
9530 /* Disable FIQ, IRQ and imprecise data aborts. */
9531 mask = CPSR_A | CPSR_I | CPSR_F;
Fabian Aggelerde38d232014-12-11 12:07:49 +00009532 if (env->cp15.scr_el3 & SCR_FIQ) {
9533 /* FIQ routed to monitor mode */
9534 new_mode = ARM_CPU_MODE_MON;
9535 }
bellardb5ff1b32005-11-26 10:38:39 +00009536 offset = 4;
9537 break;
Peter Maydell87a4b272017-01-20 11:15:07 +00009538 case EXCP_VIRQ:
9539 new_mode = ARM_CPU_MODE_IRQ;
9540 addr = 0x18;
9541 /* Disable IRQ and imprecise data aborts. */
9542 mask = CPSR_A | CPSR_I;
9543 offset = 4;
9544 break;
9545 case EXCP_VFIQ:
9546 new_mode = ARM_CPU_MODE_FIQ;
9547 addr = 0x1c;
9548 /* Disable FIQ, IRQ and imprecise data aborts. */
9549 mask = CPSR_A | CPSR_I | CPSR_F;
9550 offset = 4;
9551 break;
Fabian Aggelerdbe9d162014-10-24 12:19:15 +01009552 case EXCP_SMC:
9553 new_mode = ARM_CPU_MODE_MON;
9554 addr = 0x08;
9555 mask = CPSR_A | CPSR_I | CPSR_F;
9556 offset = 0;
9557 break;
bellardb5ff1b32005-11-26 10:38:39 +00009558 default:
Andreas Färbera47dddd2013-09-03 17:38:47 +02009559 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
bellardb5ff1b32005-11-26 10:38:39 +00009560 return; /* Never happens. Keep compiler happy. */
9561 }
Fabian Aggelere89e51a2014-12-11 12:07:50 +00009562
9563 if (new_mode == ARM_CPU_MODE_MON) {
9564 addr += env->cp15.mvbar;
Fabian Aggeler137feaa2014-12-11 12:07:50 +00009565 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
Fabian Aggelere89e51a2014-12-11 12:07:50 +00009566 /* High vectors. When enabled, base address cannot be remapped. */
bellardb5ff1b32005-11-26 10:38:39 +00009567 addr += 0xffff0000;
Nathan Rossi86411362013-10-25 15:44:38 +01009568 } else {
9569 /* ARM v7 architectures provide a vector base address register to remap
9570 * the interrupt vector table.
Fabian Aggelere89e51a2014-12-11 12:07:50 +00009571 * This register is only followed in non-monitor mode, and is banked.
Nathan Rossi86411362013-10-25 15:44:38 +01009572 * Note: only bits 31:5 are valid.
9573 */
Greg Bellowsfb6c91b2014-12-11 12:07:52 +00009574 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
bellardb5ff1b32005-11-26 10:38:39 +00009575 }
Fabian Aggelerdbe9d162014-10-24 12:19:15 +01009576
9577 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9578 env->cp15.scr_el3 &= ~SCR_NS;
9579 }
9580
Peter Maydelldea83782018-08-24 13:17:37 +01009581 take_aarch32_exception(env, new_mode, mask, offset, addr);
bellardb5ff1b32005-11-26 10:38:39 +00009582}
9583
Peter Maydell966f7582016-01-21 14:15:08 +00009584/* Handle exception entry to a target EL which is using AArch64 */
9585static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
Peter Maydellf3a9b692016-01-21 14:15:08 +00009586{
9587 ARMCPU *cpu = ARM_CPU(cs);
9588 CPUARMState *env = &cpu->env;
9589 unsigned int new_el = env->exception.target_el;
9590 target_ulong addr = env->cp15.vbar_el[new_el];
9591 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
Richard Henderson4a2696c2020-02-08 12:58:10 +00009592 unsigned int old_mode;
Richard Henderson0ab59532018-10-08 14:55:02 +01009593 unsigned int cur_el = arm_current_el(env);
Peter Maydellf3a9b692016-01-21 14:15:08 +00009594
Richard Henderson9a05f7b2018-10-08 14:21:56 -07009595 /*
9596 * Note that new_el can never be 0. If cur_el is 0, then
9597 * el0_a64 is is_a64(), else el0_a64 is ignored.
9598 */
9599 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
Richard Henderson0ab59532018-10-08 14:55:02 +01009600
9601 if (cur_el < new_el) {
Peter Maydell3d6f7612016-01-21 14:15:08 +00009602 /* Entry vector offset depends on whether the implemented EL
9603 * immediately lower than the target level is using AArch32 or AArch64
9604 */
9605 bool is_aa64;
Richard Hendersoncb092fb2020-02-07 14:04:27 +00009606 uint64_t hcr;
Peter Maydell3d6f7612016-01-21 14:15:08 +00009607
9608 switch (new_el) {
9609 case 3:
9610 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
9611 break;
9612 case 2:
Richard Hendersoncb092fb2020-02-07 14:04:27 +00009613 hcr = arm_hcr_el2_eff(env);
9614 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
9615 is_aa64 = (hcr & HCR_RW) != 0;
9616 break;
9617 }
9618 /* fall through */
Peter Maydell3d6f7612016-01-21 14:15:08 +00009619 case 1:
9620 is_aa64 = is_a64(env);
9621 break;
9622 default:
9623 g_assert_not_reached();
9624 }
9625
9626 if (is_aa64) {
Peter Maydellf3a9b692016-01-21 14:15:08 +00009627 addr += 0x400;
9628 } else {
9629 addr += 0x600;
9630 }
9631 } else if (pstate_read(env) & PSTATE_SP) {
9632 addr += 0x200;
9633 }
9634
Peter Maydellf3a9b692016-01-21 14:15:08 +00009635 switch (cs->exception_index) {
9636 case EXCP_PREFETCH_ABORT:
9637 case EXCP_DATA_ABORT:
9638 env->cp15.far_el[new_el] = env->exception.vaddress;
9639 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
9640 env->cp15.far_el[new_el]);
9641 /* fall through */
9642 case EXCP_BKPT:
9643 case EXCP_UDEF:
9644 case EXCP_SWI:
9645 case EXCP_HVC:
9646 case EXCP_HYP_TRAP:
9647 case EXCP_SMC:
Peter Maydell4be42f42018-10-24 07:50:18 +01009648 if (syn_get_ec(env->exception.syndrome) == EC_ADVSIMDFPACCESSTRAP) {
9649 /*
9650 * QEMU internal FP/SIMD syndromes from AArch32 include the
9651 * TA and coproc fields which are only exposed if the exception
9652 * is taken to AArch32 Hyp mode. Mask them out to get a valid
9653 * AArch64 format syndrome.
9654 */
9655 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
9656 }
Peter Maydellf3a9b692016-01-21 14:15:08 +00009657 env->cp15.esr_el[new_el] = env->exception.syndrome;
9658 break;
9659 case EXCP_IRQ:
9660 case EXCP_VIRQ:
9661 addr += 0x80;
9662 break;
9663 case EXCP_FIQ:
9664 case EXCP_VFIQ:
9665 addr += 0x100;
9666 break;
Peter Maydellf3a9b692016-01-21 14:15:08 +00009667 default:
9668 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9669 }
9670
9671 if (is_a64(env)) {
Richard Henderson4a2696c2020-02-08 12:58:10 +00009672 old_mode = pstate_read(env);
Peter Maydellf3a9b692016-01-21 14:15:08 +00009673 aarch64_save_sp(env, arm_current_el(env));
9674 env->elr_el[new_el] = env->pc;
9675 } else {
Richard Henderson4a2696c2020-02-08 12:58:10 +00009676 old_mode = cpsr_read(env);
Peter Maydellf3a9b692016-01-21 14:15:08 +00009677 env->elr_el[new_el] = env->regs[15];
9678
9679 aarch64_sync_32_to_64(env);
9680
9681 env->condexec_bits = 0;
9682 }
Richard Henderson4a2696c2020-02-08 12:58:10 +00009683 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
9684
Peter Maydellf3a9b692016-01-21 14:15:08 +00009685 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
9686 env->elr_el[new_el]);
9687
Richard Henderson4a2696c2020-02-08 12:58:10 +00009688 if (cpu_isar_feature(aa64_pan, cpu)) {
9689 /* The value of PSTATE.PAN is normally preserved, except when ... */
9690 new_mode |= old_mode & PSTATE_PAN;
9691 switch (new_el) {
9692 case 2:
9693 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
9694 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
9695 != (HCR_E2H | HCR_TGE)) {
9696 break;
9697 }
9698 /* fall through */
9699 case 1:
9700 /* ... the target is EL1 ... */
9701 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
9702 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
9703 new_mode |= PSTATE_PAN;
9704 }
9705 break;
9706 }
9707 }
Richard Henderson34669332020-06-25 20:31:38 -07009708 if (cpu_isar_feature(aa64_mte, cpu)) {
9709 new_mode |= PSTATE_TCO;
9710 }
Richard Henderson4a2696c2020-02-08 12:58:10 +00009711
Peter Maydellf3a9b692016-01-21 14:15:08 +00009712 pstate_write(env, PSTATE_DAIF | new_mode);
9713 env->aarch64 = 1;
9714 aarch64_restore_sp(env, new_el);
Richard Hendersona8a79c72019-10-23 11:00:49 -04009715 helper_rebuild_hflags_a64(env, new_el);
Peter Maydellf3a9b692016-01-21 14:15:08 +00009716
9717 env->pc = addr;
9718
9719 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
9720 new_el, env->pc, pstate_read(env));
Peter Maydell966f7582016-01-21 14:15:08 +00009721}
9722
Alex Bennéeed6e6ba2019-09-19 14:18:41 +01009723/*
9724 * Do semihosting call and set the appropriate return value. All the
9725 * permission and validity checks have been done at translate time.
9726 *
9727 * We only see semihosting exceptions in TCG only as they are not
9728 * trapped to the hypervisor in KVM.
9729 */
Philippe Mathieu-Daudé91f78c52019-07-04 17:14:43 +01009730#ifdef CONFIG_TCG
Alex Bennéeed6e6ba2019-09-19 14:18:41 +01009731static void handle_semihosting(CPUState *cs)
9732{
Peter Maydell904c04d2016-01-21 14:15:08 +00009733 ARMCPU *cpu = ARM_CPU(cs);
9734 CPUARMState *env = &cpu->env;
9735
9736 if (is_a64(env)) {
Alex Bennéeed6e6ba2019-09-19 14:18:41 +01009737 qemu_log_mask(CPU_LOG_INT,
9738 "...handling as semihosting call 0x%" PRIx64 "\n",
9739 env->xregs[0]);
9740 env->xregs[0] = do_arm_semihosting(env);
Alex Bennée4ff5ef92019-12-17 15:08:57 +00009741 env->pc += 4;
Peter Maydell904c04d2016-01-21 14:15:08 +00009742 } else {
Peter Maydell904c04d2016-01-21 14:15:08 +00009743 qemu_log_mask(CPU_LOG_INT,
9744 "...handling as semihosting call 0x%x\n",
9745 env->regs[0]);
9746 env->regs[0] = do_arm_semihosting(env);
Alex Bennée4ff5ef92019-12-17 15:08:57 +00009747 env->regs[15] += env->thumb ? 2 : 4;
Peter Maydell904c04d2016-01-21 14:15:08 +00009748 }
9749}
Alex Bennéeed6e6ba2019-09-19 14:18:41 +01009750#endif
Peter Maydell904c04d2016-01-21 14:15:08 +00009751
Peter Maydell966f7582016-01-21 14:15:08 +00009752/* Handle a CPU exception for A and R profile CPUs.
9753 * Do any appropriate logging, handle PSCI calls, and then hand off
9754 * to the AArch64-entry or AArch32-entry function depending on the
9755 * target exception level's register width.
9756 */
9757void arm_cpu_do_interrupt(CPUState *cs)
9758{
9759 ARMCPU *cpu = ARM_CPU(cs);
9760 CPUARMState *env = &cpu->env;
9761 unsigned int new_el = env->exception.target_el;
9762
Peter Maydell531c60a2017-01-27 15:20:22 +00009763 assert(!arm_feature(env, ARM_FEATURE_M));
Peter Maydell966f7582016-01-21 14:15:08 +00009764
9765 arm_log_exception(cs->exception_index);
9766 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
9767 new_el);
9768 if (qemu_loglevel_mask(CPU_LOG_INT)
9769 && !excp_is_internal(cs->exception_index)) {
Alex Bennée6568da42017-03-02 14:07:16 +00009770 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
Peter Maydell64b91e32018-10-24 07:50:18 +01009771 syn_get_ec(env->exception.syndrome),
Peter Maydell966f7582016-01-21 14:15:08 +00009772 env->exception.syndrome);
9773 }
9774
9775 if (arm_is_psci_call(cpu, cs->exception_index)) {
9776 arm_handle_psci_call(cpu);
9777 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
9778 return;
9779 }
9780
Alex Bennéeed6e6ba2019-09-19 14:18:41 +01009781 /*
9782 * Semihosting semantics depend on the register width of the code
9783 * that caused the exception, not the target exception level, so
9784 * must be handled here.
Peter Maydell966f7582016-01-21 14:15:08 +00009785 */
Alex Bennéeed6e6ba2019-09-19 14:18:41 +01009786#ifdef CONFIG_TCG
9787 if (cs->exception_index == EXCP_SEMIHOST) {
9788 handle_semihosting(cs);
Peter Maydell904c04d2016-01-21 14:15:08 +00009789 return;
9790 }
Alex Bennéeed6e6ba2019-09-19 14:18:41 +01009791#endif
Peter Maydell904c04d2016-01-21 14:15:08 +00009792
Aaron Lindsayb5c53d12018-04-26 11:04:39 +01009793 /* Hooks may change global state so BQL should be held, also the
9794 * BQL needs to be held for any modification of
9795 * cs->interrupt_request.
9796 */
9797 g_assert(qemu_mutex_iothread_locked());
9798
9799 arm_call_pre_el_change_hook(cpu);
9800
Peter Maydell904c04d2016-01-21 14:15:08 +00009801 assert(!excp_is_internal(cs->exception_index));
9802 if (arm_el_is_aa64(env, new_el)) {
Peter Maydell966f7582016-01-21 14:15:08 +00009803 arm_cpu_do_interrupt_aarch64(cs);
9804 } else {
9805 arm_cpu_do_interrupt_aarch32(cs);
9806 }
Peter Maydellf3a9b692016-01-21 14:15:08 +00009807
Peter Maydellbd7d00f2016-06-17 15:23:46 +01009808 arm_call_el_change_hook(cpu);
9809
Peter Maydellf3a9b692016-01-21 14:15:08 +00009810 if (!kvm_enabled()) {
9811 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
9812 }
9813}
Peter Maydellc47eaf9f2019-02-05 16:52:40 +00009814#endif /* !CONFIG_USER_ONLY */
Peter Maydell0480f692015-02-05 13:37:24 +00009815
Richard Hendersonaaec1432020-02-07 14:04:24 +00009816uint64_t arm_sctlr(CPUARMState *env, int el)
9817{
9818 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
9819 if (el == 0) {
9820 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
9821 el = (mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1);
9822 }
9823 return env->cp15.sctlr_el[el];
9824}
Peter Maydellc47eaf9f2019-02-05 16:52:40 +00009825
Peter Maydell0480f692015-02-05 13:37:24 +00009826/* Return the SCTLR value which controls this address translation regime */
Richard Hendersonaaec1432020-02-07 14:04:24 +00009827static inline uint64_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
Peter Maydell0480f692015-02-05 13:37:24 +00009828{
9829 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
9830}
9831
Richard Hendersonaaec1432020-02-07 14:04:24 +00009832#ifndef CONFIG_USER_ONLY
9833
Peter Maydell0480f692015-02-05 13:37:24 +00009834/* Return true if the specified stage of address translation is disabled */
9835static inline bool regime_translation_disabled(CPUARMState *env,
9836 ARMMMUIdx mmu_idx)
9837{
Michael Davidsaver29c483a2017-06-02 11:51:48 +01009838 if (arm_feature(env, ARM_FEATURE_M)) {
Peter Maydellecf5e8e2017-09-07 13:54:53 +01009839 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
Peter Maydell3bef7012017-06-02 11:51:49 +01009840 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
9841 case R_V7M_MPU_CTRL_ENABLE_MASK:
9842 /* Enabled, but not for HardFault and NMI */
Peter Maydell62593712017-12-13 17:59:23 +00009843 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
Peter Maydell3bef7012017-06-02 11:51:49 +01009844 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
9845 /* Enabled for all cases */
9846 return false;
9847 case 0:
9848 default:
9849 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
9850 * we warned about that in armv7m_nvic.c when the guest set it.
9851 */
9852 return true;
9853 }
Michael Davidsaver29c483a2017-06-02 11:51:48 +01009854 }
9855
Richard Henderson97fa9352020-02-07 14:04:22 +00009856 if (mmu_idx == ARMMMUIdx_Stage2) {
Peter Maydell9d1bab32018-10-24 07:50:17 +01009857 /* HCR.DC means HCR.VM behaves as 1 */
9858 return (env->cp15.hcr_el2 & (HCR_DC | HCR_VM)) == 0;
Peter Maydell0480f692015-02-05 13:37:24 +00009859 }
Peter Maydell3d0e3082018-08-14 17:17:21 +01009860
9861 if (env->cp15.hcr_el2 & HCR_TGE) {
9862 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
9863 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
9864 return true;
9865 }
9866 }
9867
Richard Hendersonfee7aa42020-02-08 12:57:57 +00009868 if ((env->cp15.hcr_el2 & HCR_DC) && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
Peter Maydell9d1bab32018-10-24 07:50:17 +01009869 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
9870 return true;
9871 }
9872
Peter Maydell0480f692015-02-05 13:37:24 +00009873 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
9874}
9875
Peter Crosthwaite73462dd2016-03-04 11:30:20 +00009876static inline bool regime_translation_big_endian(CPUARMState *env,
9877 ARMMMUIdx mmu_idx)
9878{
9879 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
9880}
9881
Peter Maydellc47eaf9f2019-02-05 16:52:40 +00009882/* Return the TTBR associated with this translation regime */
9883static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
9884 int ttbrn)
9885{
Richard Henderson97fa9352020-02-07 14:04:22 +00009886 if (mmu_idx == ARMMMUIdx_Stage2) {
Peter Maydellc47eaf9f2019-02-05 16:52:40 +00009887 return env->cp15.vttbr_el2;
9888 }
9889 if (ttbrn == 0) {
9890 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
9891 } else {
9892 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
9893 }
9894}
9895
9896#endif /* !CONFIG_USER_ONLY */
9897
Peter Maydell8bd5c822017-06-02 11:51:47 +01009898/* Convert a possible stage1+2 MMU index into the appropriate
9899 * stage 1 MMU index
9900 */
9901static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
9902{
Richard Hendersonb9f60332020-02-07 14:04:24 +00009903 switch (mmu_idx) {
9904 case ARMMMUIdx_E10_0:
9905 return ARMMMUIdx_Stage1_E0;
9906 case ARMMMUIdx_E10_1:
9907 return ARMMMUIdx_Stage1_E1;
Richard Henderson452ef8c2020-02-08 12:57:58 +00009908 case ARMMMUIdx_E10_1_PAN:
9909 return ARMMMUIdx_Stage1_E1_PAN;
Richard Hendersonb9f60332020-02-07 14:04:24 +00009910 default:
9911 return mmu_idx;
Peter Maydell8bd5c822017-06-02 11:51:47 +01009912 }
Peter Maydell8bd5c822017-06-02 11:51:47 +01009913}
9914
Peter Maydell0480f692015-02-05 13:37:24 +00009915/* Return true if the translation regime is using LPAE format page tables */
9916static inline bool regime_using_lpae_format(CPUARMState *env,
9917 ARMMMUIdx mmu_idx)
9918{
9919 int el = regime_el(env, mmu_idx);
9920 if (el == 2 || arm_el_is_aa64(env, el)) {
9921 return true;
9922 }
9923 if (arm_feature(env, ARM_FEATURE_LPAE)
9924 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
9925 return true;
9926 }
9927 return false;
9928}
9929
Alvise Rigodeb2db92016-01-15 11:37:42 +01009930/* Returns true if the stage 1 translation regime is using LPAE format page
9931 * tables. Used when raising alignment exceptions, whose FSR changes depending
9932 * on whether the long or short descriptor format is in use. */
9933bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
Andrew Baumann30901472015-12-17 13:37:13 +00009934{
Peter Maydell8bd5c822017-06-02 11:51:47 +01009935 mmu_idx = stage_1_mmu_idx(mmu_idx);
Alvise Rigodeb2db92016-01-15 11:37:42 +01009936
Andrew Baumann30901472015-12-17 13:37:13 +00009937 return regime_using_lpae_format(env, mmu_idx);
9938}
9939
Peter Maydellc47eaf9f2019-02-05 16:52:40 +00009940#ifndef CONFIG_USER_ONLY
Peter Maydell0480f692015-02-05 13:37:24 +00009941static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
9942{
9943 switch (mmu_idx) {
Richard Hendersonfba37ae2020-02-07 14:04:23 +00009944 case ARMMMUIdx_SE10_0:
Richard Hendersonb9f60332020-02-07 14:04:24 +00009945 case ARMMMUIdx_E20_0:
Richard Henderson2859d7b2020-02-07 14:04:22 +00009946 case ARMMMUIdx_Stage1_E0:
Peter Maydelle7b921c2017-06-02 11:51:47 +01009947 case ARMMMUIdx_MUser:
Peter Maydell871bec72017-12-13 17:59:23 +00009948 case ARMMMUIdx_MSUser:
Peter Maydell62593712017-12-13 17:59:23 +00009949 case ARMMMUIdx_MUserNegPri:
9950 case ARMMMUIdx_MSUserNegPri:
Peter Maydell0480f692015-02-05 13:37:24 +00009951 return true;
9952 default:
9953 return false;
Richard Henderson01b98b62020-02-07 14:04:22 +00009954 case ARMMMUIdx_E10_0:
9955 case ARMMMUIdx_E10_1:
Richard Henderson452ef8c2020-02-08 12:57:58 +00009956 case ARMMMUIdx_E10_1_PAN:
Peter Maydell0480f692015-02-05 13:37:24 +00009957 g_assert_not_reached();
9958 }
9959}
9960
Andrew Jones0fbf5232015-03-16 12:30:46 +00009961/* Translate section/page access permissions to page
9962 * R/W protection flags
Andrew Jonesd76951b2015-03-16 12:30:46 +00009963 *
9964 * @env: CPUARMState
9965 * @mmu_idx: MMU index indicating required translation regime
9966 * @ap: The 3-bit access permissions (AP[2:0])
9967 * @domain_prot: The 2-bit domain access permissions
Andrew Jones0fbf5232015-03-16 12:30:46 +00009968 */
9969static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
9970 int ap, int domain_prot)
bellardb5ff1b32005-11-26 10:38:39 +00009971{
Peter Maydell554b0b02015-02-05 13:37:24 +00009972 bool is_user = regime_is_user(env, mmu_idx);
pbrook9ee6e8b2007-11-11 00:04:49 +00009973
Peter Maydell554b0b02015-02-05 13:37:24 +00009974 if (domain_prot == 3) {
9975 return PAGE_READ | PAGE_WRITE;
9976 }
bellardb5ff1b32005-11-26 10:38:39 +00009977
Peter Maydell554b0b02015-02-05 13:37:24 +00009978 switch (ap) {
9979 case 0:
9980 if (arm_feature(env, ARM_FEATURE_V7)) {
9981 return 0;
9982 }
Peter Maydell554b0b02015-02-05 13:37:24 +00009983 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
9984 case SCTLR_S:
9985 return is_user ? 0 : PAGE_READ;
9986 case SCTLR_R:
9987 return PAGE_READ;
9988 default:
9989 return 0;
9990 }
9991 case 1:
9992 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
9993 case 2:
Peter Maydell87c3d482015-02-05 13:37:24 +00009994 if (is_user) {
Andrew Jones0fbf5232015-03-16 12:30:46 +00009995 return PAGE_READ;
Peter Maydell87c3d482015-02-05 13:37:24 +00009996 } else {
Peter Maydell554b0b02015-02-05 13:37:24 +00009997 return PAGE_READ | PAGE_WRITE;
Peter Maydell87c3d482015-02-05 13:37:24 +00009998 }
Peter Maydell554b0b02015-02-05 13:37:24 +00009999 case 3:
10000 return PAGE_READ | PAGE_WRITE;
10001 case 4: /* Reserved. */
10002 return 0;
10003 case 5:
Andrew Jones0fbf5232015-03-16 12:30:46 +000010004 return is_user ? 0 : PAGE_READ;
Peter Maydell554b0b02015-02-05 13:37:24 +000010005 case 6:
Andrew Jones0fbf5232015-03-16 12:30:46 +000010006 return PAGE_READ;
Peter Maydell554b0b02015-02-05 13:37:24 +000010007 case 7:
Peter Maydell87c3d482015-02-05 13:37:24 +000010008 if (!arm_feature(env, ARM_FEATURE_V6K)) {
Peter Maydell554b0b02015-02-05 13:37:24 +000010009 return 0;
Peter Maydell87c3d482015-02-05 13:37:24 +000010010 }
Andrew Jones0fbf5232015-03-16 12:30:46 +000010011 return PAGE_READ;
Peter Maydell554b0b02015-02-05 13:37:24 +000010012 default:
Andrew Jones0fbf5232015-03-16 12:30:46 +000010013 g_assert_not_reached();
Peter Maydell554b0b02015-02-05 13:37:24 +000010014 }
bellardb5ff1b32005-11-26 10:38:39 +000010015}
10016
Andrew Jonesd76951b2015-03-16 12:30:46 +000010017/* Translate section/page access permissions to page
10018 * R/W protection flags.
10019 *
Andrew Jonesd76951b2015-03-16 12:30:46 +000010020 * @ap: The 2-bit simple AP (AP[2:1])
Andrew Jonesd8e052b2015-03-16 12:30:46 +000010021 * @is_user: TRUE if accessing from PL0
Andrew Jonesd76951b2015-03-16 12:30:46 +000010022 */
Andrew Jonesd8e052b2015-03-16 12:30:46 +000010023static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
Andrew Jonesd76951b2015-03-16 12:30:46 +000010024{
Andrew Jonesd76951b2015-03-16 12:30:46 +000010025 switch (ap) {
10026 case 0:
10027 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10028 case 1:
10029 return PAGE_READ | PAGE_WRITE;
10030 case 2:
10031 return is_user ? 0 : PAGE_READ;
10032 case 3:
10033 return PAGE_READ;
10034 default:
10035 g_assert_not_reached();
10036 }
10037}
10038
Andrew Jonesd8e052b2015-03-16 12:30:46 +000010039static inline int
10040simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
10041{
10042 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
10043}
10044
Edgar E. Iglesias6ab1a5e2015-10-26 14:02:01 +010010045/* Translate S2 section/page access permissions to protection flags
10046 *
10047 * @env: CPUARMState
10048 * @s2ap: The 2-bit stage2 access permissions (S2AP)
Peter Maydellce3125b2020-03-30 22:04:00 +010010049 * @xn: XN (execute-never) bits
10050 * @s1_is_el0: true if this is S2 of an S1+2 walk for EL0
Edgar E. Iglesias6ab1a5e2015-10-26 14:02:01 +010010051 */
Peter Maydellce3125b2020-03-30 22:04:00 +010010052static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0)
Edgar E. Iglesias6ab1a5e2015-10-26 14:02:01 +010010053{
10054 int prot = 0;
10055
10056 if (s2ap & 1) {
10057 prot |= PAGE_READ;
10058 }
10059 if (s2ap & 2) {
10060 prot |= PAGE_WRITE;
10061 }
Peter Maydellce3125b2020-03-30 22:04:00 +010010062
10063 if (cpu_isar_feature(any_tts2uxn, env_archcpu(env))) {
10064 switch (xn) {
10065 case 0:
Sergey Sorokindfda6832016-05-12 13:22:26 +010010066 prot |= PAGE_EXEC;
Peter Maydellce3125b2020-03-30 22:04:00 +010010067 break;
10068 case 1:
10069 if (s1_is_el0) {
10070 prot |= PAGE_EXEC;
10071 }
10072 break;
10073 case 2:
10074 break;
10075 case 3:
10076 if (!s1_is_el0) {
10077 prot |= PAGE_EXEC;
10078 }
10079 break;
10080 default:
10081 g_assert_not_reached();
10082 }
10083 } else {
10084 if (!extract32(xn, 1, 1)) {
10085 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
10086 prot |= PAGE_EXEC;
10087 }
Sergey Sorokindfda6832016-05-12 13:22:26 +010010088 }
Edgar E. Iglesias6ab1a5e2015-10-26 14:02:01 +010010089 }
10090 return prot;
10091}
10092
Andrew Jonesd8e052b2015-03-16 12:30:46 +000010093/* Translate section/page access permissions to protection flags
10094 *
10095 * @env: CPUARMState
10096 * @mmu_idx: MMU index indicating required translation regime
10097 * @is_aa64: TRUE if AArch64
10098 * @ap: The 2-bit simple AP (AP[2:1])
10099 * @ns: NS (non-secure) bit
10100 * @xn: XN (execute-never) bit
10101 * @pxn: PXN (privileged execute-never) bit
10102 */
10103static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
10104 int ap, int ns, int xn, int pxn)
10105{
10106 bool is_user = regime_is_user(env, mmu_idx);
10107 int prot_rw, user_rw;
10108 bool have_wxn;
10109 int wxn = 0;
10110
Richard Henderson97fa9352020-02-07 14:04:22 +000010111 assert(mmu_idx != ARMMMUIdx_Stage2);
Andrew Jonesd8e052b2015-03-16 12:30:46 +000010112
10113 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
10114 if (is_user) {
10115 prot_rw = user_rw;
10116 } else {
Richard Henderson81636b72020-02-08 12:58:09 +000010117 if (user_rw && regime_is_pan(env, mmu_idx)) {
Peter Maydellf4e1dbc2020-03-30 18:06:51 +010010118 /* PAN forbids data accesses but doesn't affect insn fetch */
10119 prot_rw = 0;
10120 } else {
10121 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
Richard Henderson81636b72020-02-08 12:58:09 +000010122 }
Andrew Jonesd8e052b2015-03-16 12:30:46 +000010123 }
10124
10125 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
10126 return prot_rw;
10127 }
10128
10129 /* TODO have_wxn should be replaced with
10130 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
10131 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
10132 * compatible processors have EL2, which is required for [U]WXN.
10133 */
10134 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
10135
10136 if (have_wxn) {
10137 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
10138 }
10139
10140 if (is_aa64) {
Richard Henderson339370b2020-02-07 14:04:24 +000010141 if (regime_has_2_ranges(mmu_idx) && !is_user) {
10142 xn = pxn || (user_rw & PAGE_WRITE);
Andrew Jonesd8e052b2015-03-16 12:30:46 +000010143 }
10144 } else if (arm_feature(env, ARM_FEATURE_V7)) {
10145 switch (regime_el(env, mmu_idx)) {
10146 case 1:
10147 case 3:
10148 if (is_user) {
10149 xn = xn || !(user_rw & PAGE_READ);
10150 } else {
10151 int uwxn = 0;
10152 if (have_wxn) {
10153 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
10154 }
10155 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
10156 (uwxn && (user_rw & PAGE_WRITE));
10157 }
10158 break;
10159 case 2:
10160 break;
10161 }
10162 } else {
10163 xn = wxn = 0;
10164 }
10165
10166 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
10167 return prot_rw;
10168 }
10169 return prot_rw | PAGE_EXEC;
10170}
10171
Peter Maydell0480f692015-02-05 13:37:24 +000010172static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
10173 uint32_t *table, uint32_t address)
pbrookb2fa1792008-10-22 19:22:30 +000010174{
Peter Maydell0480f692015-02-05 13:37:24 +000010175 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
Peter Maydell0480f692015-02-05 13:37:24 +000010176 TCR *tcr = regime_tcr(env, mmu_idx);
Fabian Aggeler11f136e2014-12-11 12:07:51 +000010177
Fabian Aggeler11f136e2014-12-11 12:07:51 +000010178 if (address & tcr->mask) {
10179 if (tcr->raw_tcr & TTBCR_PD1) {
Fabian Aggelere389be12014-06-19 18:06:24 +010010180 /* Translation table walk disabled for TTBR1 */
10181 return false;
10182 }
Greg Bellowsaef878b2015-04-22 12:09:18 -050010183 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
Fabian Aggelere389be12014-06-19 18:06:24 +010010184 } else {
Fabian Aggeler11f136e2014-12-11 12:07:51 +000010185 if (tcr->raw_tcr & TTBCR_PD0) {
Fabian Aggelere389be12014-06-19 18:06:24 +010010186 /* Translation table walk disabled for TTBR0 */
10187 return false;
10188 }
Greg Bellowsaef878b2015-04-22 12:09:18 -050010189 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
Fabian Aggelere389be12014-06-19 18:06:24 +010010190 }
10191 *table |= (address >> 18) & 0x3ffc;
10192 return true;
pbrookb2fa1792008-10-22 19:22:30 +000010193}
10194
Edgar E. Iglesias37785972015-10-26 14:02:04 +010010195/* Translate a S1 pagetable walk through S2 if needed. */
10196static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
10197 hwaddr addr, MemTxAttrs txattrs,
Edgar E. Iglesias37785972015-10-26 14:02:04 +010010198 ARMMMUFaultInfo *fi)
10199{
Richard Hendersonfee7aa42020-02-08 12:57:57 +000010200 if (arm_mmu_idx_is_stage1_of_2(mmu_idx) &&
Richard Henderson97fa9352020-02-07 14:04:22 +000010201 !regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
Edgar E. Iglesias37785972015-10-26 14:02:04 +010010202 target_ulong s2size;
10203 hwaddr s2pa;
10204 int s2prot;
10205 int ret;
Peter Maydelleadb2fe2018-10-24 07:50:18 +010010206 ARMCacheAttrs cacheattrs = {};
10207 ARMCacheAttrs *pcacheattrs = NULL;
10208
10209 if (env->cp15.hcr_el2 & HCR_PTW) {
10210 /*
10211 * PTW means we must fault if this S1 walk touches S2 Device
10212 * memory; otherwise we don't care about the attributes and can
10213 * save the S2 translation the effort of computing them.
10214 */
10215 pcacheattrs = &cacheattrs;
10216 }
Edgar E. Iglesias37785972015-10-26 14:02:04 +010010217
Peter Maydell59dff852020-03-30 22:03:58 +010010218 ret = get_phys_addr_lpae(env, addr, MMU_DATA_LOAD, ARMMMUIdx_Stage2,
Peter Maydellff7de2f2020-03-30 22:03:59 +010010219 false,
Peter Maydell59dff852020-03-30 22:03:58 +010010220 &s2pa, &txattrs, &s2prot, &s2size, fi,
10221 pcacheattrs);
Edgar E. Iglesias37785972015-10-26 14:02:04 +010010222 if (ret) {
Peter Maydell3b39d732018-01-16 13:28:11 +000010223 assert(fi->type != ARMFault_None);
Edgar E. Iglesias37785972015-10-26 14:02:04 +010010224 fi->s2addr = addr;
10225 fi->stage2 = true;
10226 fi->s1ptw = true;
10227 return ~0;
10228 }
Peter Maydelleadb2fe2018-10-24 07:50:18 +010010229 if (pcacheattrs && (pcacheattrs->attrs & 0xf0) == 0) {
10230 /* Access was to Device memory: generate Permission fault */
10231 fi->type = ARMFault_Permission;
10232 fi->s2addr = addr;
10233 fi->stage2 = true;
10234 fi->s1ptw = true;
10235 return ~0;
10236 }
Edgar E. Iglesias37785972015-10-26 14:02:04 +010010237 addr = s2pa;
10238 }
10239 return addr;
10240}
10241
Peter Maydell14577272018-04-26 11:04:38 +010010242/* All loads done in the course of a page table walk go through here. */
Edgar E. Iglesiasa614e692015-10-26 14:02:05 +010010243static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
Peter Maydell3795a6d2017-12-13 17:59:24 +000010244 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
Peter Maydellebca90e2015-04-26 16:49:25 +010010245{
Edgar E. Iglesiasa614e692015-10-26 14:02:05 +010010246 ARMCPU *cpu = ARM_CPU(cs);
10247 CPUARMState *env = &cpu->env;
Peter Maydellebca90e2015-04-26 16:49:25 +010010248 MemTxAttrs attrs = {};
Peter Maydell3b39d732018-01-16 13:28:11 +000010249 MemTxResult result = MEMTX_OK;
Peter Maydell5ce4ff62016-01-21 14:15:07 +000010250 AddressSpace *as;
Peter Maydell3b39d732018-01-16 13:28:11 +000010251 uint32_t data;
Peter Maydellebca90e2015-04-26 16:49:25 +010010252
10253 attrs.secure = is_secure;
Peter Maydell5ce4ff62016-01-21 14:15:07 +000010254 as = arm_addressspace(cs, attrs);
Peter Maydell3795a6d2017-12-13 17:59:24 +000010255 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
Edgar E. Iglesiasa614e692015-10-26 14:02:05 +010010256 if (fi->s1ptw) {
10257 return 0;
10258 }
Peter Crosthwaite73462dd2016-03-04 11:30:20 +000010259 if (regime_translation_big_endian(env, mmu_idx)) {
Peter Maydell3b39d732018-01-16 13:28:11 +000010260 data = address_space_ldl_be(as, addr, attrs, &result);
Peter Crosthwaite73462dd2016-03-04 11:30:20 +000010261 } else {
Peter Maydell3b39d732018-01-16 13:28:11 +000010262 data = address_space_ldl_le(as, addr, attrs, &result);
Peter Crosthwaite73462dd2016-03-04 11:30:20 +000010263 }
Peter Maydell3b39d732018-01-16 13:28:11 +000010264 if (result == MEMTX_OK) {
10265 return data;
10266 }
10267 fi->type = ARMFault_SyncExternalOnWalk;
10268 fi->ea = arm_extabort_type(result);
10269 return 0;
Peter Maydellebca90e2015-04-26 16:49:25 +010010270}
10271
Edgar E. Iglesias37785972015-10-26 14:02:04 +010010272static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
Peter Maydell3795a6d2017-12-13 17:59:24 +000010273 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
Peter Maydellebca90e2015-04-26 16:49:25 +010010274{
Edgar E. Iglesias37785972015-10-26 14:02:04 +010010275 ARMCPU *cpu = ARM_CPU(cs);
10276 CPUARMState *env = &cpu->env;
Peter Maydellebca90e2015-04-26 16:49:25 +010010277 MemTxAttrs attrs = {};
Peter Maydell3b39d732018-01-16 13:28:11 +000010278 MemTxResult result = MEMTX_OK;
Peter Maydell5ce4ff62016-01-21 14:15:07 +000010279 AddressSpace *as;
Ard Biesheuvel9aea1ea2018-01-25 11:45:28 +000010280 uint64_t data;
Peter Maydellebca90e2015-04-26 16:49:25 +010010281
10282 attrs.secure = is_secure;
Peter Maydell5ce4ff62016-01-21 14:15:07 +000010283 as = arm_addressspace(cs, attrs);
Peter Maydell3795a6d2017-12-13 17:59:24 +000010284 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi);
Edgar E. Iglesias37785972015-10-26 14:02:04 +010010285 if (fi->s1ptw) {
10286 return 0;
10287 }
Peter Crosthwaite73462dd2016-03-04 11:30:20 +000010288 if (regime_translation_big_endian(env, mmu_idx)) {
Peter Maydell3b39d732018-01-16 13:28:11 +000010289 data = address_space_ldq_be(as, addr, attrs, &result);
Peter Crosthwaite73462dd2016-03-04 11:30:20 +000010290 } else {
Peter Maydell3b39d732018-01-16 13:28:11 +000010291 data = address_space_ldq_le(as, addr, attrs, &result);
Peter Crosthwaite73462dd2016-03-04 11:30:20 +000010292 }
Peter Maydell3b39d732018-01-16 13:28:11 +000010293 if (result == MEMTX_OK) {
10294 return data;
10295 }
10296 fi->type = ARMFault_SyncExternalOnWalk;
10297 fi->ea = arm_extabort_type(result);
10298 return 0;
Peter Maydellebca90e2015-04-26 16:49:25 +010010299}
10300
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010010301static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
Peter Maydell03ae85f2017-09-04 15:21:51 +010010302 MMUAccessType access_type, ARMMMUIdx mmu_idx,
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010010303 hwaddr *phys_ptr, int *prot,
Peter Maydellf9899832017-12-13 17:59:24 +000010304 target_ulong *page_size,
Edgar E. Iglesiase14b5a22015-10-26 14:02:03 +010010305 ARMMMUFaultInfo *fi)
bellardb5ff1b32005-11-26 10:38:39 +000010306{
Richard Henderson2fc0cc02019-03-22 17:41:14 -070010307 CPUState *cs = env_cpu(env);
Peter Maydellf9899832017-12-13 17:59:24 +000010308 int level = 1;
bellardb5ff1b32005-11-26 10:38:39 +000010309 uint32_t table;
10310 uint32_t desc;
10311 int type;
10312 int ap;
Fabian Aggelere389be12014-06-19 18:06:24 +010010313 int domain = 0;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +000010314 int domain_prot;
Avi Kivitya8170e52012-10-23 12:30:10 +020010315 hwaddr phys_addr;
Peter Maydell0480f692015-02-05 13:37:24 +000010316 uint32_t dacr;
bellardb5ff1b32005-11-26 10:38:39 +000010317
pbrook9ee6e8b2007-11-11 00:04:49 +000010318 /* Pagetable walk. */
10319 /* Lookup l1 descriptor. */
Peter Maydell0480f692015-02-05 13:37:24 +000010320 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
Fabian Aggelere389be12014-06-19 18:06:24 +010010321 /* Section translation fault if page walk is disabled by PD0 or PD1 */
Peter Maydellf9899832017-12-13 17:59:24 +000010322 fi->type = ARMFault_Translation;
Fabian Aggelere389be12014-06-19 18:06:24 +010010323 goto do_fault;
10324 }
Edgar E. Iglesiasa614e692015-10-26 14:02:05 +010010325 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
Peter Maydell3795a6d2017-12-13 17:59:24 +000010326 mmu_idx, fi);
Peter Maydell3b39d732018-01-16 13:28:11 +000010327 if (fi->type != ARMFault_None) {
10328 goto do_fault;
10329 }
pbrook9ee6e8b2007-11-11 00:04:49 +000010330 type = (desc & 3);
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +000010331 domain = (desc >> 5) & 0x0f;
Peter Maydell0480f692015-02-05 13:37:24 +000010332 if (regime_el(env, mmu_idx) == 1) {
10333 dacr = env->cp15.dacr_ns;
10334 } else {
10335 dacr = env->cp15.dacr_s;
10336 }
10337 domain_prot = (dacr >> (domain * 2)) & 3;
pbrook9ee6e8b2007-11-11 00:04:49 +000010338 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +000010339 /* Section translation fault. */
Peter Maydellf9899832017-12-13 17:59:24 +000010340 fi->type = ARMFault_Translation;
pbrook9ee6e8b2007-11-11 00:04:49 +000010341 goto do_fault;
10342 }
Peter Maydellf9899832017-12-13 17:59:24 +000010343 if (type != 2) {
10344 level = 2;
10345 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +000010346 if (domain_prot == 0 || domain_prot == 2) {
Peter Maydellf9899832017-12-13 17:59:24 +000010347 fi->type = ARMFault_Domain;
pbrook9ee6e8b2007-11-11 00:04:49 +000010348 goto do_fault;
10349 }
10350 if (type == 2) {
10351 /* 1Mb section. */
10352 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
10353 ap = (desc >> 10) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +000010354 *page_size = 1024 * 1024;
pbrook9ee6e8b2007-11-11 00:04:49 +000010355 } else {
10356 /* Lookup l2 entry. */
Peter Maydell554b0b02015-02-05 13:37:24 +000010357 if (type == 1) {
10358 /* Coarse pagetable. */
10359 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
10360 } else {
10361 /* Fine pagetable. */
10362 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
10363 }
Edgar E. Iglesiasa614e692015-10-26 14:02:05 +010010364 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
Peter Maydell3795a6d2017-12-13 17:59:24 +000010365 mmu_idx, fi);
Peter Maydell3b39d732018-01-16 13:28:11 +000010366 if (fi->type != ARMFault_None) {
10367 goto do_fault;
10368 }
pbrook9ee6e8b2007-11-11 00:04:49 +000010369 switch (desc & 3) {
10370 case 0: /* Page translation fault. */
Peter Maydellf9899832017-12-13 17:59:24 +000010371 fi->type = ARMFault_Translation;
pbrook9ee6e8b2007-11-11 00:04:49 +000010372 goto do_fault;
10373 case 1: /* 64k page. */
10374 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10375 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +000010376 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +000010377 break;
10378 case 2: /* 4k page. */
10379 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
Peter Maydellc10f7fc2014-02-26 17:19:59 +000010380 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +000010381 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +000010382 break;
Peter Maydellfc1891c2015-06-15 18:06:07 +010010383 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
Peter Maydell554b0b02015-02-05 13:37:24 +000010384 if (type == 1) {
Peter Maydellfc1891c2015-06-15 18:06:07 +010010385 /* ARMv6/XScale extended small page format */
10386 if (arm_feature(env, ARM_FEATURE_XSCALE)
10387 || arm_feature(env, ARM_FEATURE_V6)) {
Peter Maydell554b0b02015-02-05 13:37:24 +000010388 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
Peter Maydellfc1891c2015-06-15 18:06:07 +010010389 *page_size = 0x1000;
Peter Maydell554b0b02015-02-05 13:37:24 +000010390 } else {
Peter Maydellfc1891c2015-06-15 18:06:07 +010010391 /* UNPREDICTABLE in ARMv5; we choose to take a
10392 * page translation fault.
10393 */
Peter Maydellf9899832017-12-13 17:59:24 +000010394 fi->type = ARMFault_Translation;
Peter Maydell554b0b02015-02-05 13:37:24 +000010395 goto do_fault;
10396 }
10397 } else {
10398 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
Peter Maydellfc1891c2015-06-15 18:06:07 +010010399 *page_size = 0x400;
Peter Maydell554b0b02015-02-05 13:37:24 +000010400 }
pbrook9ee6e8b2007-11-11 00:04:49 +000010401 ap = (desc >> 4) & 3;
10402 break;
10403 default:
10404 /* Never happens, but compiler isn't smart enough to tell. */
10405 abort();
10406 }
pbrook9ee6e8b2007-11-11 00:04:49 +000010407 }
Andrew Jones0fbf5232015-03-16 12:30:46 +000010408 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
10409 *prot |= *prot ? PAGE_EXEC : 0;
10410 if (!(*prot & (1 << access_type))) {
pbrook9ee6e8b2007-11-11 00:04:49 +000010411 /* Access permission fault. */
Peter Maydellf9899832017-12-13 17:59:24 +000010412 fi->type = ARMFault_Permission;
pbrook9ee6e8b2007-11-11 00:04:49 +000010413 goto do_fault;
10414 }
10415 *phys_ptr = phys_addr;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010010416 return false;
pbrook9ee6e8b2007-11-11 00:04:49 +000010417do_fault:
Peter Maydellf9899832017-12-13 17:59:24 +000010418 fi->domain = domain;
10419 fi->level = level;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010010420 return true;
pbrook9ee6e8b2007-11-11 00:04:49 +000010421}
10422
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010010423static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
Peter Maydell03ae85f2017-09-04 15:21:51 +010010424 MMUAccessType access_type, ARMMMUIdx mmu_idx,
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010010425 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
Peter Maydellf06cf242017-12-13 17:59:24 +000010426 target_ulong *page_size, ARMMMUFaultInfo *fi)
pbrook9ee6e8b2007-11-11 00:04:49 +000010427{
Richard Henderson2fc0cc02019-03-22 17:41:14 -070010428 CPUState *cs = env_cpu(env);
Peter Maydellf06cf242017-12-13 17:59:24 +000010429 int level = 1;
pbrook9ee6e8b2007-11-11 00:04:49 +000010430 uint32_t table;
10431 uint32_t desc;
10432 uint32_t xn;
Peter Maydellde9b05b2012-07-12 10:59:05 +000010433 uint32_t pxn = 0;
pbrook9ee6e8b2007-11-11 00:04:49 +000010434 int type;
10435 int ap;
Peter Maydellde9b05b2012-07-12 10:59:05 +000010436 int domain = 0;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +000010437 int domain_prot;
Avi Kivitya8170e52012-10-23 12:30:10 +020010438 hwaddr phys_addr;
Peter Maydell0480f692015-02-05 13:37:24 +000010439 uint32_t dacr;
Peter Maydell8bf5b6a2015-04-26 16:49:25 +010010440 bool ns;
pbrook9ee6e8b2007-11-11 00:04:49 +000010441
10442 /* Pagetable walk. */
10443 /* Lookup l1 descriptor. */
Peter Maydell0480f692015-02-05 13:37:24 +000010444 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
Fabian Aggelere389be12014-06-19 18:06:24 +010010445 /* Section translation fault if page walk is disabled by PD0 or PD1 */
Peter Maydellf06cf242017-12-13 17:59:24 +000010446 fi->type = ARMFault_Translation;
Fabian Aggelere389be12014-06-19 18:06:24 +010010447 goto do_fault;
10448 }
Edgar E. Iglesiasa614e692015-10-26 14:02:05 +010010449 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
Peter Maydell3795a6d2017-12-13 17:59:24 +000010450 mmu_idx, fi);
Peter Maydell3b39d732018-01-16 13:28:11 +000010451 if (fi->type != ARMFault_None) {
10452 goto do_fault;
10453 }
pbrook9ee6e8b2007-11-11 00:04:49 +000010454 type = (desc & 3);
Peter Maydellde9b05b2012-07-12 10:59:05 +000010455 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
10456 /* Section translation fault, or attempt to use the encoding
10457 * which is Reserved on implementations without PXN.
10458 */
Peter Maydellf06cf242017-12-13 17:59:24 +000010459 fi->type = ARMFault_Translation;
pbrook9ee6e8b2007-11-11 00:04:49 +000010460 goto do_fault;
Peter Maydellde9b05b2012-07-12 10:59:05 +000010461 }
10462 if ((type == 1) || !(desc & (1 << 18))) {
10463 /* Page or Section. */
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +000010464 domain = (desc >> 5) & 0x0f;
pbrook9ee6e8b2007-11-11 00:04:49 +000010465 }
Peter Maydell0480f692015-02-05 13:37:24 +000010466 if (regime_el(env, mmu_idx) == 1) {
10467 dacr = env->cp15.dacr_ns;
10468 } else {
10469 dacr = env->cp15.dacr_s;
10470 }
Peter Maydellf06cf242017-12-13 17:59:24 +000010471 if (type == 1) {
10472 level = 2;
10473 }
Peter Maydell0480f692015-02-05 13:37:24 +000010474 domain_prot = (dacr >> (domain * 2)) & 3;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +000010475 if (domain_prot == 0 || domain_prot == 2) {
Peter Maydellf06cf242017-12-13 17:59:24 +000010476 /* Section or Page domain fault */
10477 fi->type = ARMFault_Domain;
pbrook9ee6e8b2007-11-11 00:04:49 +000010478 goto do_fault;
10479 }
Peter Maydellde9b05b2012-07-12 10:59:05 +000010480 if (type != 1) {
pbrook9ee6e8b2007-11-11 00:04:49 +000010481 if (desc & (1 << 18)) {
10482 /* Supersection. */
10483 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
Sergey Fedorov4e42a6c2015-06-15 18:06:07 +010010484 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
10485 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
Paul Brookd4c430a2010-03-17 02:14:28 +000010486 *page_size = 0x1000000;
pbrook9ee6e8b2007-11-11 00:04:49 +000010487 } else {
10488 /* Section. */
10489 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
Paul Brookd4c430a2010-03-17 02:14:28 +000010490 *page_size = 0x100000;
pbrook9ee6e8b2007-11-11 00:04:49 +000010491 }
10492 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
10493 xn = desc & (1 << 4);
Peter Maydellde9b05b2012-07-12 10:59:05 +000010494 pxn = desc & 1;
Peter Maydell8bf5b6a2015-04-26 16:49:25 +010010495 ns = extract32(desc, 19, 1);
pbrook9ee6e8b2007-11-11 00:04:49 +000010496 } else {
Peter Maydellde9b05b2012-07-12 10:59:05 +000010497 if (arm_feature(env, ARM_FEATURE_PXN)) {
10498 pxn = (desc >> 2) & 1;
10499 }
Peter Maydell8bf5b6a2015-04-26 16:49:25 +010010500 ns = extract32(desc, 3, 1);
pbrook9ee6e8b2007-11-11 00:04:49 +000010501 /* Lookup l2 entry. */
10502 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
Edgar E. Iglesiasa614e692015-10-26 14:02:05 +010010503 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
Peter Maydell3795a6d2017-12-13 17:59:24 +000010504 mmu_idx, fi);
Peter Maydell3b39d732018-01-16 13:28:11 +000010505 if (fi->type != ARMFault_None) {
10506 goto do_fault;
10507 }
pbrook9ee6e8b2007-11-11 00:04:49 +000010508 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
10509 switch (desc & 3) {
10510 case 0: /* Page translation fault. */
Peter Maydellf06cf242017-12-13 17:59:24 +000010511 fi->type = ARMFault_Translation;
pbrook9ee6e8b2007-11-11 00:04:49 +000010512 goto do_fault;
10513 case 1: /* 64k page. */
10514 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10515 xn = desc & (1 << 15);
Paul Brookd4c430a2010-03-17 02:14:28 +000010516 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +000010517 break;
10518 case 2: case 3: /* 4k page. */
10519 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10520 xn = desc & 1;
Paul Brookd4c430a2010-03-17 02:14:28 +000010521 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +000010522 break;
10523 default:
10524 /* Never happens, but compiler isn't smart enough to tell. */
10525 abort();
10526 }
pbrook9ee6e8b2007-11-11 00:04:49 +000010527 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +000010528 if (domain_prot == 3) {
Juha Riihimäkic0034322010-12-08 13:15:16 +020010529 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10530 } else {
Peter Maydell0480f692015-02-05 13:37:24 +000010531 if (pxn && !regime_is_user(env, mmu_idx)) {
Peter Maydellde9b05b2012-07-12 10:59:05 +000010532 xn = 1;
10533 }
Peter Maydellf06cf242017-12-13 17:59:24 +000010534 if (xn && access_type == MMU_INST_FETCH) {
10535 fi->type = ARMFault_Permission;
Juha Riihimäkic0034322010-12-08 13:15:16 +020010536 goto do_fault;
Peter Maydellf06cf242017-12-13 17:59:24 +000010537 }
pbrook9ee6e8b2007-11-11 00:04:49 +000010538
Andrew Jonesd76951b2015-03-16 12:30:46 +000010539 if (arm_feature(env, ARM_FEATURE_V6K) &&
10540 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
10541 /* The simplified model uses AP[0] as an access control bit. */
10542 if ((ap & 1) == 0) {
10543 /* Access flag fault. */
Peter Maydellf06cf242017-12-13 17:59:24 +000010544 fi->type = ARMFault_AccessFlag;
Andrew Jonesd76951b2015-03-16 12:30:46 +000010545 goto do_fault;
10546 }
10547 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
10548 } else {
10549 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
Juha Riihimäkic0034322010-12-08 13:15:16 +020010550 }
Andrew Jones0fbf5232015-03-16 12:30:46 +000010551 if (*prot && !xn) {
10552 *prot |= PAGE_EXEC;
10553 }
10554 if (!(*prot & (1 << access_type))) {
Juha Riihimäkic0034322010-12-08 13:15:16 +020010555 /* Access permission fault. */
Peter Maydellf06cf242017-12-13 17:59:24 +000010556 fi->type = ARMFault_Permission;
Juha Riihimäkic0034322010-12-08 13:15:16 +020010557 goto do_fault;
10558 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +053010559 }
Peter Maydell8bf5b6a2015-04-26 16:49:25 +010010560 if (ns) {
10561 /* The NS bit will (as required by the architecture) have no effect if
10562 * the CPU doesn't support TZ or this is a non-secure translation
10563 * regime, because the attribute will already be non-secure.
10564 */
10565 attrs->secure = false;
10566 }
pbrook9ee6e8b2007-11-11 00:04:49 +000010567 *phys_ptr = phys_addr;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010010568 return false;
pbrook9ee6e8b2007-11-11 00:04:49 +000010569do_fault:
Peter Maydellf06cf242017-12-13 17:59:24 +000010570 fi->domain = domain;
10571 fi->level = level;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010010572 return true;
pbrook9ee6e8b2007-11-11 00:04:49 +000010573}
10574
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010575/*
Edgar E. Iglesiasa0e966c2016-02-03 13:46:33 +000010576 * check_s2_mmu_setup
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010577 * @cpu: ARMCPU
10578 * @is_aa64: True if the translation regime is in AArch64 state
10579 * @startlevel: Suggested starting level
10580 * @inputsize: Bitsize of IPAs
10581 * @stride: Page-table stride (See the ARM ARM)
10582 *
Edgar E. Iglesiasa0e966c2016-02-03 13:46:33 +000010583 * Returns true if the suggested S2 translation parameters are OK and
10584 * false otherwise.
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010585 */
Edgar E. Iglesiasa0e966c2016-02-03 13:46:33 +000010586static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
10587 int inputsize, int stride)
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010588{
Edgar E. Iglesias98d68ec2016-02-03 13:46:33 +000010589 const int grainsize = stride + 3;
10590 int startsizecheck;
10591
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010592 /* Negative levels are never allowed. */
10593 if (level < 0) {
10594 return false;
10595 }
10596
Edgar E. Iglesias98d68ec2016-02-03 13:46:33 +000010597 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
10598 if (startsizecheck < 1 || startsizecheck > stride + 4) {
10599 return false;
10600 }
10601
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010602 if (is_aa64) {
Edgar E. Iglesias35264232016-02-03 13:46:33 +000010603 CPUARMState *env = &cpu->env;
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010604 unsigned int pamax = arm_pamax(cpu);
10605
10606 switch (stride) {
10607 case 13: /* 64KB Pages. */
10608 if (level == 0 || (level == 1 && pamax <= 42)) {
10609 return false;
10610 }
10611 break;
10612 case 11: /* 16KB Pages. */
10613 if (level == 0 || (level == 1 && pamax <= 40)) {
10614 return false;
10615 }
10616 break;
10617 case 9: /* 4KB Pages. */
10618 if (level == 0 && pamax <= 42) {
10619 return false;
10620 }
10621 break;
10622 default:
10623 g_assert_not_reached();
10624 }
Edgar E. Iglesias35264232016-02-03 13:46:33 +000010625
10626 /* Inputsize checks. */
10627 if (inputsize > pamax &&
10628 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
10629 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
10630 return false;
10631 }
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010632 } else {
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010633 /* AArch32 only supports 4KB pages. Assert on that. */
10634 assert(stride == 9);
10635
10636 if (level == 0) {
10637 return false;
10638 }
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010639 }
10640 return true;
10641}
10642
Andrew Baumann5b2d2612017-11-07 13:03:51 +000010643/* Translate from the 4-bit stage 2 representation of
10644 * memory attributes (without cache-allocation hints) to
10645 * the 8-bit representation of the stage 1 MAIR registers
10646 * (which includes allocation hints).
10647 *
10648 * ref: shared/translation/attrs/S2AttrDecode()
10649 * .../S2ConvertAttrsHints()
10650 */
10651static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
10652{
10653 uint8_t hiattr = extract32(s2attrs, 2, 2);
10654 uint8_t loattr = extract32(s2attrs, 0, 2);
10655 uint8_t hihint = 0, lohint = 0;
10656
10657 if (hiattr != 0) { /* normal memory */
10658 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */
10659 hiattr = loattr = 1; /* non-cacheable */
10660 } else {
10661 if (hiattr != 1) { /* Write-through or write-back */
10662 hihint = 3; /* RW allocate */
10663 }
10664 if (loattr != 1) { /* Write-through or write-back */
10665 lohint = 3; /* RW allocate */
10666 }
10667 }
10668 }
10669
10670 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
10671}
Peter Maydellc47eaf9f2019-02-05 16:52:40 +000010672#endif /* !CONFIG_USER_ONLY */
Andrew Baumann5b2d2612017-11-07 13:03:51 +000010673
Richard Hendersonb830a5e2020-02-16 11:43:43 -080010674static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10675{
10676 if (regime_has_2_ranges(mmu_idx)) {
10677 return extract64(tcr, 37, 2);
10678 } else if (mmu_idx == ARMMMUIdx_Stage2) {
10679 return 0; /* VTCR_EL2 */
10680 } else {
Richard Henderson3e270f62020-03-05 16:09:20 +000010681 /* Replicate the single TBI bit so we always have 2 bits. */
10682 return extract32(tcr, 20, 1) * 3;
Richard Hendersonb830a5e2020-02-16 11:43:43 -080010683 }
10684}
10685
10686static int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10687{
10688 if (regime_has_2_ranges(mmu_idx)) {
10689 return extract64(tcr, 51, 2);
10690 } else if (mmu_idx == ARMMMUIdx_Stage2) {
10691 return 0; /* VTCR_EL2 */
10692 } else {
Richard Henderson3e270f62020-03-05 16:09:20 +000010693 /* Replicate the single TBID bit so we always have 2 bits. */
10694 return extract32(tcr, 29, 1) * 3;
Richard Hendersonb830a5e2020-02-16 11:43:43 -080010695 }
10696}
10697
Richard Henderson81ae05f2020-06-25 20:31:06 -070010698static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10699{
10700 if (regime_has_2_ranges(mmu_idx)) {
10701 return extract64(tcr, 57, 2);
10702 } else {
10703 /* Replicate the single TCMA bit so we always have 2 bits. */
10704 return extract32(tcr, 30, 1) * 3;
10705 }
10706}
10707
Richard Hendersonb830a5e2020-02-16 11:43:43 -080010708ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10709 ARMMMUIdx mmu_idx, bool data)
Richard Hendersonba97be92019-01-21 10:23:12 +000010710{
10711 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
Richard Hendersonb830a5e2020-02-16 11:43:43 -080010712 bool epd, hpd, using16k, using64k;
10713 int select, tsz, tbi;
Richard Hendersonba97be92019-01-21 10:23:12 +000010714
Richard Henderson339370b2020-02-07 14:04:24 +000010715 if (!regime_has_2_ranges(mmu_idx)) {
Richard Henderson71d18162020-02-16 11:43:41 -080010716 select = 0;
Richard Hendersonba97be92019-01-21 10:23:12 +000010717 tsz = extract32(tcr, 0, 6);
10718 using64k = extract32(tcr, 14, 1);
10719 using16k = extract32(tcr, 15, 1);
Richard Henderson97fa9352020-02-07 14:04:22 +000010720 if (mmu_idx == ARMMMUIdx_Stage2) {
Richard Hendersonba97be92019-01-21 10:23:12 +000010721 /* VTCR_EL2 */
Richard Hendersonb830a5e2020-02-16 11:43:43 -080010722 hpd = false;
Richard Hendersonba97be92019-01-21 10:23:12 +000010723 } else {
Richard Hendersonba97be92019-01-21 10:23:12 +000010724 hpd = extract32(tcr, 24, 1);
10725 }
10726 epd = false;
Richard Hendersonba97be92019-01-21 10:23:12 +000010727 } else {
Richard Henderson71d18162020-02-16 11:43:41 -080010728 /*
10729 * Bit 55 is always between the two regions, and is canonical for
10730 * determining if address tagging is enabled.
10731 */
10732 select = extract64(va, 55, 1);
10733 if (!select) {
10734 tsz = extract32(tcr, 0, 6);
10735 epd = extract32(tcr, 7, 1);
10736 using64k = extract32(tcr, 14, 1);
10737 using16k = extract32(tcr, 15, 1);
Richard Henderson71d18162020-02-16 11:43:41 -080010738 hpd = extract64(tcr, 41, 1);
Richard Henderson71d18162020-02-16 11:43:41 -080010739 } else {
10740 int tg = extract32(tcr, 30, 2);
10741 using16k = tg == 1;
10742 using64k = tg == 3;
10743 tsz = extract32(tcr, 16, 6);
10744 epd = extract32(tcr, 23, 1);
Richard Henderson71d18162020-02-16 11:43:41 -080010745 hpd = extract64(tcr, 42, 1);
Richard Henderson71d18162020-02-16 11:43:41 -080010746 }
Richard Hendersonba97be92019-01-21 10:23:12 +000010747 }
10748 tsz = MIN(tsz, 39); /* TODO: ARMv8.4-TTST */
10749 tsz = MAX(tsz, 16); /* TODO: ARMv8.2-LVA */
10750
Richard Hendersonb830a5e2020-02-16 11:43:43 -080010751 /* Present TBI as a composite with TBID. */
10752 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10753 if (!data) {
10754 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10755 }
10756 tbi = (tbi >> select) & 1;
10757
Richard Hendersonba97be92019-01-21 10:23:12 +000010758 return (ARMVAParameters) {
10759 .tsz = tsz,
10760 .select = select,
10761 .tbi = tbi,
10762 .epd = epd,
10763 .hpd = hpd,
10764 .using16k = using16k,
10765 .using64k = using64k,
10766 };
10767}
10768
Peter Maydellc47eaf9f2019-02-05 16:52:40 +000010769#ifndef CONFIG_USER_ONLY
Richard Hendersonba97be92019-01-21 10:23:12 +000010770static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
10771 ARMMMUIdx mmu_idx)
10772{
10773 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
10774 uint32_t el = regime_el(env, mmu_idx);
10775 int select, tsz;
10776 bool epd, hpd;
10777
Richard Henderson97fa9352020-02-07 14:04:22 +000010778 if (mmu_idx == ARMMMUIdx_Stage2) {
Richard Hendersonba97be92019-01-21 10:23:12 +000010779 /* VTCR */
10780 bool sext = extract32(tcr, 4, 1);
10781 bool sign = extract32(tcr, 3, 1);
10782
10783 /*
10784 * If the sign-extend bit is not the same as t0sz[3], the result
10785 * is unpredictable. Flag this as a guest error.
10786 */
10787 if (sign != sext) {
10788 qemu_log_mask(LOG_GUEST_ERROR,
10789 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
10790 }
10791 tsz = sextract32(tcr, 0, 4) + 8;
10792 select = 0;
10793 hpd = false;
10794 epd = false;
10795 } else if (el == 2) {
10796 /* HTCR */
10797 tsz = extract32(tcr, 0, 3);
10798 select = 0;
10799 hpd = extract64(tcr, 24, 1);
10800 epd = false;
10801 } else {
10802 int t0sz = extract32(tcr, 0, 3);
10803 int t1sz = extract32(tcr, 16, 3);
10804
10805 if (t1sz == 0) {
10806 select = va > (0xffffffffu >> t0sz);
10807 } else {
10808 /* Note that we will detect errors later. */
10809 select = va >= ~(0xffffffffu >> t1sz);
10810 }
10811 if (!select) {
10812 tsz = t0sz;
10813 epd = extract32(tcr, 7, 1);
10814 hpd = extract64(tcr, 41, 1);
10815 } else {
10816 tsz = t1sz;
10817 epd = extract32(tcr, 23, 1);
10818 hpd = extract64(tcr, 42, 1);
10819 }
10820 /* For aarch32, hpd0 is not enabled without t2e as well. */
10821 hpd &= extract32(tcr, 6, 1);
10822 }
10823
10824 return (ARMVAParameters) {
10825 .tsz = tsz,
10826 .select = select,
10827 .epd = epd,
10828 .hpd = hpd,
10829 };
10830}
10831
Peter Maydellff7de2f2020-03-30 22:03:59 +010010832/**
10833 * get_phys_addr_lpae: perform one stage of page table walk, LPAE format
10834 *
10835 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
10836 * prot and page_size may not be filled in, and the populated fsr value provides
10837 * information on why the translation aborted, in the format of a long-format
10838 * DFSR/IFSR fault register, with the following caveats:
10839 * * the WnR bit is never set (the caller must do this).
10840 *
10841 * @env: CPUARMState
10842 * @address: virtual address to get physical address for
10843 * @access_type: MMU_DATA_LOAD, MMU_DATA_STORE or MMU_INST_FETCH
10844 * @mmu_idx: MMU index indicating required translation regime
10845 * @s1_is_el0: if @mmu_idx is ARMMMUIdx_Stage2 (so this is a stage 2 page table
10846 * walk), must be true if this is stage 2 of a stage 1+2 walk for an
10847 * EL0 access). If @mmu_idx is anything else, @s1_is_el0 is ignored.
10848 * @phys_ptr: set to the physical address corresponding to the virtual address
10849 * @attrs: set to the memory transaction attributes to use
10850 * @prot: set to the permissions for the page containing phys_ptr
10851 * @page_size_ptr: set to the size of the page containing phys_ptr
10852 * @fi: set to fault info if the translation fails
10853 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
10854 */
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010010855static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
Peter Maydell03ae85f2017-09-04 15:21:51 +010010856 MMUAccessType access_type, ARMMMUIdx mmu_idx,
Peter Maydellff7de2f2020-03-30 22:03:59 +010010857 bool s1_is_el0,
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010010858 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
Peter Maydellda909b22017-12-13 17:59:24 +000010859 target_ulong *page_size_ptr,
Andrew Baumann5b2d2612017-11-07 13:03:51 +000010860 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
Peter Maydell3dde9622012-07-12 10:59:12 +000010861{
Richard Henderson2fc0cc02019-03-22 17:41:14 -070010862 ARMCPU *cpu = env_archcpu(env);
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010863 CPUState *cs = CPU(cpu);
Peter Maydell3dde9622012-07-12 10:59:12 +000010864 /* Read an LPAE long-descriptor translation table. */
Peter Maydellda909b22017-12-13 17:59:24 +000010865 ARMFaultType fault_type = ARMFault_Translation;
Sergey Sorokin1b4093e2016-03-16 17:05:58 +000010866 uint32_t level;
Richard Hendersonba97be92019-01-21 10:23:12 +000010867 ARMVAParameters param;
Peter Maydell3dde9622012-07-12 10:59:12 +000010868 uint64_t ttbr;
Sergey Sorokindddb5222016-05-12 13:22:26 +010010869 hwaddr descaddr, indexmask, indexmask_grainsize;
Peter Maydell3dde9622012-07-12 10:59:12 +000010870 uint32_t tableattrs;
Richard Henderson36d820a2019-01-29 11:46:03 +000010871 target_ulong page_size;
Peter Maydell3dde9622012-07-12 10:59:12 +000010872 uint32_t attrs;
Richard Hendersonba97be92019-01-21 10:23:12 +000010873 int32_t stride;
10874 int addrsize, inputsize;
Peter Maydell0480f692015-02-05 13:37:24 +000010875 TCR *tcr = regime_tcr(env, mmu_idx);
Andrew Jonesd8e052b2015-03-16 12:30:46 +000010876 int ap, ns, xn, pxn;
Greg Bellows88e8add2015-04-22 12:09:19 -050010877 uint32_t el = regime_el(env, mmu_idx);
Peter Maydell61097692015-11-24 14:12:15 +000010878 uint64_t descaddrmask;
Sergey Sorokin6e99f762016-06-06 16:59:32 +010010879 bool aarch64 = arm_el_is_aa64(env, el);
Richard Henderson1bafc2b2019-02-05 16:52:37 +000010880 bool guarded = false;
Rob Herring2c8dd312014-04-15 19:18:40 +010010881
Peter Maydell07d1be32020-03-31 15:34:07 +010010882 /* TODO: This code does not support shareability levels. */
Sergey Sorokin6e99f762016-06-06 16:59:32 +010010883 if (aarch64) {
Richard Hendersonba97be92019-01-21 10:23:12 +000010884 param = aa64_va_parameters(env, address, mmu_idx,
10885 access_type != MMU_INST_FETCH);
Sergey Sorokin1b4093e2016-03-16 17:05:58 +000010886 level = 0;
Richard Hendersonba97be92019-01-21 10:23:12 +000010887 addrsize = 64 - 8 * param.tbi;
10888 inputsize = 64 - param.tsz;
Peter Maydelld0a2cbc2015-08-25 15:45:08 +010010889 } else {
Richard Hendersonba97be92019-01-21 10:23:12 +000010890 param = aa32_va_parameters(env, address, mmu_idx);
Sergey Sorokin1b4093e2016-03-16 17:05:58 +000010891 level = 1;
Richard Henderson97fa9352020-02-07 14:04:22 +000010892 addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32);
Richard Hendersonba97be92019-01-21 10:23:12 +000010893 inputsize = addrsize - param.tsz;
Rob Herring2c8dd312014-04-15 19:18:40 +010010894 }
Peter Maydell3dde9622012-07-12 10:59:12 +000010895
Richard Hendersonba97be92019-01-21 10:23:12 +000010896 /*
10897 * We determined the region when collecting the parameters, but we
10898 * have not yet validated that the address is valid for the region.
10899 * Extract the top bits and verify that they all match select.
Richard Henderson36d820a2019-01-29 11:46:03 +000010900 *
10901 * For aa32, if inputsize == addrsize, then we have selected the
10902 * region by exclusion in aa32_va_parameters and there is no more
10903 * validation to do here.
Peter Maydell3dde9622012-07-12 10:59:12 +000010904 */
Richard Henderson36d820a2019-01-29 11:46:03 +000010905 if (inputsize < addrsize) {
10906 target_ulong top_bits = sextract64(address, inputsize,
10907 addrsize - inputsize);
Richard Henderson03f27722020-02-16 11:43:42 -080010908 if (-top_bits != param.select) {
Richard Henderson36d820a2019-01-29 11:46:03 +000010909 /* The gap between the two regions is a Translation fault */
10910 fault_type = ARMFault_Translation;
10911 goto do_fault;
10912 }
Peter Maydell3dde9622012-07-12 10:59:12 +000010913 }
10914
Richard Hendersonba97be92019-01-21 10:23:12 +000010915 if (param.using64k) {
10916 stride = 13;
10917 } else if (param.using16k) {
10918 stride = 11;
10919 } else {
10920 stride = 9;
10921 }
10922
Peter Maydell3dde9622012-07-12 10:59:12 +000010923 /* Note that QEMU ignores shareability and cacheability attributes,
10924 * so we don't need to do anything with the SH, ORGN, IRGN fields
10925 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
10926 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
10927 * implement any ASID-like capability so we can ignore it (instead
10928 * we will always flush the TLB any time the ASID is changed).
10929 */
Richard Hendersonba97be92019-01-21 10:23:12 +000010930 ttbr = regime_ttbr(env, mmu_idx, param.select);
Peter Maydell3dde9622012-07-12 10:59:12 +000010931
Peter Maydell0480f692015-02-05 13:37:24 +000010932 /* Here we should have set up all the parameters for the translation:
Sergey Sorokin6e99f762016-06-06 16:59:32 +010010933 * inputsize, ttbr, epd, stride, tbi
Peter Maydell0480f692015-02-05 13:37:24 +000010934 */
10935
Richard Hendersonba97be92019-01-21 10:23:12 +000010936 if (param.epd) {
Greg Bellows88e8add2015-04-22 12:09:19 -050010937 /* Translation table walk disabled => Translation fault on TLB miss
10938 * Note: This is always 0 on 64-bit EL2 and EL3.
10939 */
Peter Maydell3dde9622012-07-12 10:59:12 +000010940 goto do_fault;
10941 }
10942
Richard Henderson97fa9352020-02-07 14:04:22 +000010943 if (mmu_idx != ARMMMUIdx_Stage2) {
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010944 /* The starting level depends on the virtual address size (which can
10945 * be up to 48 bits) and the translation granule size. It indicates
10946 * the number of strides (stride bits at a time) needed to
10947 * consume the bits of the input address. In the pseudocode this is:
10948 * level = 4 - RoundUp((inputsize - grainsize) / stride)
10949 * where their 'inputsize' is our 'inputsize', 'grainsize' is
10950 * our 'stride + 3' and 'stride' is our 'stride'.
10951 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
10952 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
10953 * = 4 - (inputsize - 4) / stride;
10954 */
10955 level = 4 - (inputsize - 4) / stride;
10956 } else {
10957 /* For stage 2 translations the starting level is specified by the
10958 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
10959 */
Sergey Sorokin1b4093e2016-03-16 17:05:58 +000010960 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
10961 uint32_t startlevel;
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010962 bool ok;
10963
Sergey Sorokin6e99f762016-06-06 16:59:32 +010010964 if (!aarch64 || stride == 9) {
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010965 /* AArch32 or 4KB pages */
Sergey Sorokin1b4093e2016-03-16 17:05:58 +000010966 startlevel = 2 - sl0;
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010967 } else {
10968 /* 16KB or 64KB pages */
Sergey Sorokin1b4093e2016-03-16 17:05:58 +000010969 startlevel = 3 - sl0;
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010970 }
10971
10972 /* Check that the starting level is valid. */
Sergey Sorokin6e99f762016-06-06 16:59:32 +010010973 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
Sergey Sorokin1b4093e2016-03-16 17:05:58 +000010974 inputsize, stride);
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010975 if (!ok) {
Peter Maydellda909b22017-12-13 17:59:24 +000010976 fault_type = ARMFault_Translation;
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010977 goto do_fault;
10978 }
Sergey Sorokin1b4093e2016-03-16 17:05:58 +000010979 level = startlevel;
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +010010980 }
Peter Maydell3dde9622012-07-12 10:59:12 +000010981
Sergey Sorokindddb5222016-05-12 13:22:26 +010010982 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
10983 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
Peter Maydell3dde9622012-07-12 10:59:12 +000010984
10985 /* Now we can extract the actual base address from the TTBR */
Rob Herring2c8dd312014-04-15 19:18:40 +010010986 descaddr = extract64(ttbr, 0, 48);
Peter Maydell41a4bf12020-03-05 16:09:14 +000010987 /*
10988 * We rely on this masking to clear the RES0 bits at the bottom of the TTBR
10989 * and also to mask out CnP (bit 0) which could validly be non-zero.
10990 */
Sergey Sorokindddb5222016-05-12 13:22:26 +010010991 descaddr &= ~indexmask;
Peter Maydell3dde9622012-07-12 10:59:12 +000010992
Peter Maydell61097692015-11-24 14:12:15 +000010993 /* The address field in the descriptor goes up to bit 39 for ARMv7
Sergey Sorokindddb5222016-05-12 13:22:26 +010010994 * but up to bit 47 for ARMv8, but we use the descaddrmask
10995 * up to bit 39 for AArch32, because we don't need other bits in that case
10996 * to construct next descriptor address (anyway they should be all zeroes).
Peter Maydell61097692015-11-24 14:12:15 +000010997 */
Sergey Sorokin6e99f762016-06-06 16:59:32 +010010998 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) &
Sergey Sorokindddb5222016-05-12 13:22:26 +010010999 ~indexmask_grainsize;
Peter Maydell61097692015-11-24 14:12:15 +000011000
Peter Maydellebca90e2015-04-26 16:49:25 +010011001 /* Secure accesses start with the page table in secure memory and
11002 * can be downgraded to non-secure at any step. Non-secure accesses
11003 * remain non-secure. We implement this by just ORing in the NSTable/NS
11004 * bits at each step.
11005 */
11006 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
Peter Maydell3dde9622012-07-12 10:59:12 +000011007 for (;;) {
11008 uint64_t descriptor;
Peter Maydellebca90e2015-04-26 16:49:25 +010011009 bool nstable;
Peter Maydell3dde9622012-07-12 10:59:12 +000011010
Sergey Sorokindddb5222016-05-12 13:22:26 +010011011 descaddr |= (address >> (stride * (4 - level))) & indexmask;
Rob Herring2c8dd312014-04-15 19:18:40 +010011012 descaddr &= ~7ULL;
Peter Maydellebca90e2015-04-26 16:49:25 +010011013 nstable = extract32(tableattrs, 4, 1);
Peter Maydell3795a6d2017-12-13 17:59:24 +000011014 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
Peter Maydell3b39d732018-01-16 13:28:11 +000011015 if (fi->type != ARMFault_None) {
Edgar E. Iglesias37785972015-10-26 14:02:04 +010011016 goto do_fault;
11017 }
11018
Peter Maydell3dde9622012-07-12 10:59:12 +000011019 if (!(descriptor & 1) ||
11020 (!(descriptor & 2) && (level == 3))) {
11021 /* Invalid, or the Reserved level 3 encoding */
11022 goto do_fault;
11023 }
Peter Maydell61097692015-11-24 14:12:15 +000011024 descaddr = descriptor & descaddrmask;
Peter Maydell3dde9622012-07-12 10:59:12 +000011025
11026 if ((descriptor & 2) && (level < 3)) {
Richard Henderson037c13c2018-12-13 13:48:06 +000011027 /* Table entry. The top five bits are attributes which may
Peter Maydell3dde9622012-07-12 10:59:12 +000011028 * propagate down through lower levels of the table (and
11029 * which are all arranged so that 0 means "no effect", so
11030 * we can gather them up by ORing in the bits at each level).
11031 */
11032 tableattrs |= extract64(descriptor, 59, 5);
11033 level++;
Sergey Sorokindddb5222016-05-12 13:22:26 +010011034 indexmask = indexmask_grainsize;
Peter Maydell3dde9622012-07-12 10:59:12 +000011035 continue;
11036 }
11037 /* Block entry at level 1 or 2, or page entry at level 3.
11038 * These are basically the same thing, although the number
11039 * of bits we pull in from the vaddr varies.
11040 */
Edgar E. Iglesias973a5432015-10-26 14:01:59 +010011041 page_size = (1ULL << ((stride * (4 - level)) + 3));
Peter Maydell3dde9622012-07-12 10:59:12 +000011042 descaddr |= (address & (page_size - 1));
Edgar E. Iglesias6ab1a5e2015-10-26 14:02:01 +010011043 /* Extract attributes from the descriptor */
Ian Campbelld615efa2014-06-09 15:43:23 +010011044 attrs = extract64(descriptor, 2, 10)
11045 | (extract64(descriptor, 52, 12) << 10);
Edgar E. Iglesias6ab1a5e2015-10-26 14:02:01 +010011046
Richard Henderson97fa9352020-02-07 14:04:22 +000011047 if (mmu_idx == ARMMMUIdx_Stage2) {
Edgar E. Iglesias6ab1a5e2015-10-26 14:02:01 +010011048 /* Stage 2 table descriptors do not include any attribute fields */
11049 break;
11050 }
11051 /* Merge in attributes from table descriptors */
Richard Henderson037c13c2018-12-13 13:48:06 +000011052 attrs |= nstable << 3; /* NS */
Richard Henderson1bafc2b2019-02-05 16:52:37 +000011053 guarded = extract64(descriptor, 50, 1); /* GP */
Richard Hendersonba97be92019-01-21 10:23:12 +000011054 if (param.hpd) {
Richard Henderson037c13c2018-12-13 13:48:06 +000011055 /* HPD disables all the table attributes except NSTable. */
11056 break;
11057 }
11058 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
Peter Maydell3dde9622012-07-12 10:59:12 +000011059 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
11060 * means "force PL1 access only", which means forcing AP[1] to 0.
11061 */
Richard Henderson037c13c2018-12-13 13:48:06 +000011062 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
11063 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
Peter Maydell3dde9622012-07-12 10:59:12 +000011064 break;
11065 }
11066 /* Here descaddr is the final physical address, and attributes
11067 * are all in attrs.
11068 */
Peter Maydellda909b22017-12-13 17:59:24 +000011069 fault_type = ARMFault_AccessFlag;
Peter Maydell3dde9622012-07-12 10:59:12 +000011070 if ((attrs & (1 << 8)) == 0) {
11071 /* Access flag */
11072 goto do_fault;
11073 }
Andrew Jonesd8e052b2015-03-16 12:30:46 +000011074
11075 ap = extract32(attrs, 4, 2);
Andrew Jonesd8e052b2015-03-16 12:30:46 +000011076
Richard Henderson97fa9352020-02-07 14:04:22 +000011077 if (mmu_idx == ARMMMUIdx_Stage2) {
Edgar E. Iglesias6ab1a5e2015-10-26 14:02:01 +010011078 ns = true;
Peter Maydellce3125b2020-03-30 22:04:00 +010011079 xn = extract32(attrs, 11, 2);
11080 *prot = get_S2prot(env, ap, xn, s1_is_el0);
Edgar E. Iglesias6ab1a5e2015-10-26 14:02:01 +010011081 } else {
11082 ns = extract32(attrs, 3, 1);
Peter Maydellce3125b2020-03-30 22:04:00 +010011083 xn = extract32(attrs, 12, 1);
Edgar E. Iglesias6ab1a5e2015-10-26 14:02:01 +010011084 pxn = extract32(attrs, 11, 1);
Sergey Sorokin6e99f762016-06-06 16:59:32 +010011085 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
Edgar E. Iglesias6ab1a5e2015-10-26 14:02:01 +010011086 }
Andrew Jonesd8e052b2015-03-16 12:30:46 +000011087
Peter Maydellda909b22017-12-13 17:59:24 +000011088 fault_type = ARMFault_Permission;
Andrew Jonesd8e052b2015-03-16 12:30:46 +000011089 if (!(*prot & (1 << access_type))) {
Peter Maydell3dde9622012-07-12 10:59:12 +000011090 goto do_fault;
11091 }
Peter Maydell3dde9622012-07-12 10:59:12 +000011092
Peter Maydell8bf5b6a2015-04-26 16:49:25 +010011093 if (ns) {
11094 /* The NS bit will (as required by the architecture) have no effect if
11095 * the CPU doesn't support TZ or this is a non-secure translation
11096 * regime, because the attribute will already be non-secure.
11097 */
11098 txattrs->secure = false;
11099 }
Richard Henderson1bafc2b2019-02-05 16:52:37 +000011100 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
11101 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
Richard Henderson149d3b32020-06-25 20:31:30 -070011102 arm_tlb_bti_gp(txattrs) = true;
Richard Henderson1bafc2b2019-02-05 16:52:37 +000011103 }
Andrew Baumann5b2d2612017-11-07 13:03:51 +000011104
Richard Henderson7e98e212020-06-25 20:31:39 -070011105 if (mmu_idx == ARMMMUIdx_Stage2) {
11106 cacheattrs->attrs = convert_stage2_attrs(env, extract32(attrs, 0, 4));
11107 } else {
11108 /* Index into MAIR registers for cache attributes */
11109 uint8_t attrindx = extract32(attrs, 0, 3);
11110 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
11111 assert(attrindx <= 7);
11112 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
Andrew Baumann5b2d2612017-11-07 13:03:51 +000011113 }
Richard Henderson7e98e212020-06-25 20:31:39 -070011114 cacheattrs->shareability = extract32(attrs, 6, 2);
Andrew Baumann5b2d2612017-11-07 13:03:51 +000011115
Peter Maydell3dde9622012-07-12 10:59:12 +000011116 *phys_ptr = descaddr;
11117 *page_size_ptr = page_size;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010011118 return false;
Peter Maydell3dde9622012-07-12 10:59:12 +000011119
11120do_fault:
Peter Maydellda909b22017-12-13 17:59:24 +000011121 fi->type = fault_type;
11122 fi->level = level;
Edgar E. Iglesias37785972015-10-26 14:02:04 +010011123 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
Richard Henderson97fa9352020-02-07 14:04:22 +000011124 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_Stage2);
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010011125 return true;
Peter Maydell3dde9622012-07-12 10:59:12 +000011126}
11127
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011128static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
11129 ARMMMUIdx mmu_idx,
11130 int32_t address, int *prot)
11131{
Michael Davidsaver3a00d562017-06-02 11:51:48 +010011132 if (!arm_feature(env, ARM_FEATURE_M)) {
11133 *prot = PAGE_READ | PAGE_WRITE;
11134 switch (address) {
11135 case 0xF0000000 ... 0xFFFFFFFF:
11136 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
11137 /* hivecs execing is ok */
11138 *prot |= PAGE_EXEC;
11139 }
11140 break;
11141 case 0x00000000 ... 0x7FFFFFFF:
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011142 *prot |= PAGE_EXEC;
Michael Davidsaver3a00d562017-06-02 11:51:48 +010011143 break;
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011144 }
Michael Davidsaver3a00d562017-06-02 11:51:48 +010011145 } else {
11146 /* Default system address map for M profile cores.
11147 * The architecture specifies which regions are execute-never;
11148 * at the MPU level no other checks are defined.
11149 */
11150 switch (address) {
11151 case 0x00000000 ... 0x1fffffff: /* ROM */
11152 case 0x20000000 ... 0x3fffffff: /* SRAM */
11153 case 0x60000000 ... 0x7fffffff: /* RAM */
11154 case 0x80000000 ... 0x9fffffff: /* RAM */
11155 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11156 break;
11157 case 0x40000000 ... 0x5fffffff: /* Peripheral */
11158 case 0xa0000000 ... 0xbfffffff: /* Device */
11159 case 0xc0000000 ... 0xdfffffff: /* Device */
11160 case 0xe0000000 ... 0xffffffff: /* System */
11161 *prot = PAGE_READ | PAGE_WRITE;
11162 break;
11163 default:
11164 g_assert_not_reached();
11165 }
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011166 }
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011167}
11168
Michael Davidsaver29c483a2017-06-02 11:51:48 +010011169static bool pmsav7_use_background_region(ARMCPU *cpu,
11170 ARMMMUIdx mmu_idx, bool is_user)
11171{
11172 /* Return true if we should use the default memory map as a
11173 * "background" region if there are no hits against any MPU regions.
11174 */
11175 CPUARMState *env = &cpu->env;
11176
11177 if (is_user) {
11178 return false;
11179 }
11180
11181 if (arm_feature(env, ARM_FEATURE_M)) {
Peter Maydellecf5e8e2017-09-07 13:54:53 +010011182 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
11183 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
Michael Davidsaver29c483a2017-06-02 11:51:48 +010011184 } else {
11185 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
11186 }
11187}
11188
Peter Maydell38aaa602017-07-27 11:59:06 +010011189static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
11190{
11191 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
11192 return arm_feature(env, ARM_FEATURE_M) &&
11193 extract32(address, 20, 12) == 0xe00;
11194}
11195
Peter Maydellbf446a12017-07-27 11:59:07 +010011196static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
11197{
11198 /* True if address is in the M profile system region
11199 * 0xe0000000 - 0xffffffff
11200 */
11201 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
11202}
11203
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011204static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
Peter Maydell03ae85f2017-09-04 15:21:51 +010011205 MMUAccessType access_type, ARMMMUIdx mmu_idx,
Peter Maydell9375ad12017-12-13 17:59:25 +000011206 hwaddr *phys_ptr, int *prot,
Peter Maydelle5e40992018-06-26 17:50:41 +010011207 target_ulong *page_size,
Peter Maydell9375ad12017-12-13 17:59:25 +000011208 ARMMMUFaultInfo *fi)
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011209{
Richard Henderson2fc0cc02019-03-22 17:41:14 -070011210 ARMCPU *cpu = env_archcpu(env);
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011211 int n;
11212 bool is_user = regime_is_user(env, mmu_idx);
11213
11214 *phys_ptr = address;
Peter Maydelle5e40992018-06-26 17:50:41 +010011215 *page_size = TARGET_PAGE_SIZE;
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011216 *prot = 0;
11217
Peter Maydell38aaa602017-07-27 11:59:06 +010011218 if (regime_translation_disabled(env, mmu_idx) ||
11219 m_is_ppb_region(env, address)) {
11220 /* MPU disabled or M profile PPB access: use default memory map.
11221 * The other case which uses the default memory map in the
11222 * v7M ARM ARM pseudocode is exception vector reads from the vector
11223 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
11224 * which always does a direct read using address_space_ldl(), rather
11225 * than going via this function, so we don't need to check that here.
11226 */
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011227 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11228 } else { /* MPU enabled */
11229 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11230 /* region search */
11231 uint32_t base = env->pmsav7.drbar[n];
11232 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
11233 uint32_t rmask;
11234 bool srdis = false;
11235
11236 if (!(env->pmsav7.drsr[n] & 0x1)) {
11237 continue;
11238 }
11239
11240 if (!rsize) {
Michael Davidsaverc9f9f122017-06-02 11:51:48 +010011241 qemu_log_mask(LOG_GUEST_ERROR,
11242 "DRSR[%d]: Rsize field cannot be 0\n", n);
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011243 continue;
11244 }
11245 rsize++;
11246 rmask = (1ull << rsize) - 1;
11247
11248 if (base & rmask) {
Michael Davidsaverc9f9f122017-06-02 11:51:48 +010011249 qemu_log_mask(LOG_GUEST_ERROR,
11250 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
11251 "to DRSR region size, mask = 0x%" PRIx32 "\n",
11252 n, base, rmask);
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011253 continue;
11254 }
11255
11256 if (address < base || address > base + rmask) {
Peter Maydell9d2b5a52018-07-23 15:21:26 +010011257 /*
11258 * Address not in this region. We must check whether the
11259 * region covers addresses in the same page as our address.
11260 * In that case we must not report a size that covers the
11261 * whole page for a subsequent hit against a different MPU
11262 * region or the background region, because it would result in
11263 * incorrect TLB hits for subsequent accesses to addresses that
11264 * are in this MPU region.
11265 */
11266 if (ranges_overlap(base, rmask,
11267 address & TARGET_PAGE_MASK,
11268 TARGET_PAGE_SIZE)) {
11269 *page_size = 1;
11270 }
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011271 continue;
11272 }
11273
11274 /* Region matched */
11275
11276 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
11277 int i, snd;
11278 uint32_t srdis_mask;
11279
11280 rsize -= 3; /* sub region size (power of 2) */
11281 snd = ((address - base) >> rsize) & 0x7;
11282 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
11283
11284 srdis_mask = srdis ? 0x3 : 0x0;
11285 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
11286 /* This will check in groups of 2, 4 and then 8, whether
11287 * the subregion bits are consistent. rsize is incremented
11288 * back up to give the region size, considering consistent
11289 * adjacent subregions as one region. Stop testing if rsize
11290 * is already big enough for an entire QEMU page.
11291 */
11292 int snd_rounded = snd & ~(i - 1);
11293 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
11294 snd_rounded + 8, i);
11295 if (srdis_mask ^ srdis_multi) {
11296 break;
11297 }
11298 srdis_mask = (srdis_mask << i) | srdis_mask;
11299 rsize++;
11300 }
11301 }
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011302 if (srdis) {
11303 continue;
11304 }
Peter Maydelle5e40992018-06-26 17:50:41 +010011305 if (rsize < TARGET_PAGE_BITS) {
11306 *page_size = 1 << rsize;
11307 }
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011308 break;
11309 }
11310
11311 if (n == -1) { /* no hits */
Michael Davidsaver29c483a2017-06-02 11:51:48 +010011312 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011313 /* background fault */
Peter Maydell9375ad12017-12-13 17:59:25 +000011314 fi->type = ARMFault_Background;
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011315 return true;
11316 }
11317 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11318 } else { /* a MPU hit! */
11319 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
Peter Maydellbf446a12017-07-27 11:59:07 +010011320 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
11321
11322 if (m_is_system_region(env, address)) {
11323 /* System space is always execute never */
11324 xn = 1;
11325 }
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011326
11327 if (is_user) { /* User mode AP bit decoding */
11328 switch (ap) {
11329 case 0:
11330 case 1:
11331 case 5:
11332 break; /* no access */
11333 case 3:
11334 *prot |= PAGE_WRITE;
11335 /* fall through */
11336 case 2:
11337 case 6:
11338 *prot |= PAGE_READ | PAGE_EXEC;
11339 break;
Peter Maydell8638f1a2018-01-16 13:28:10 +000011340 case 7:
11341 /* for v7M, same as 6; for R profile a reserved value */
11342 if (arm_feature(env, ARM_FEATURE_M)) {
11343 *prot |= PAGE_READ | PAGE_EXEC;
11344 break;
11345 }
11346 /* fall through */
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011347 default:
11348 qemu_log_mask(LOG_GUEST_ERROR,
Michael Davidsaverc9f9f122017-06-02 11:51:48 +010011349 "DRACR[%d]: Bad value for AP bits: 0x%"
11350 PRIx32 "\n", n, ap);
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011351 }
11352 } else { /* Priv. mode AP bits decoding */
11353 switch (ap) {
11354 case 0:
11355 break; /* no access */
11356 case 1:
11357 case 2:
11358 case 3:
11359 *prot |= PAGE_WRITE;
11360 /* fall through */
11361 case 5:
11362 case 6:
11363 *prot |= PAGE_READ | PAGE_EXEC;
11364 break;
Peter Maydell8638f1a2018-01-16 13:28:10 +000011365 case 7:
11366 /* for v7M, same as 6; for R profile a reserved value */
11367 if (arm_feature(env, ARM_FEATURE_M)) {
11368 *prot |= PAGE_READ | PAGE_EXEC;
11369 break;
11370 }
11371 /* fall through */
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011372 default:
11373 qemu_log_mask(LOG_GUEST_ERROR,
Michael Davidsaverc9f9f122017-06-02 11:51:48 +010011374 "DRACR[%d]: Bad value for AP bits: 0x%"
11375 PRIx32 "\n", n, ap);
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011376 }
11377 }
11378
11379 /* execute never */
Peter Maydellbf446a12017-07-27 11:59:07 +010011380 if (xn) {
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011381 *prot &= ~PAGE_EXEC;
11382 }
11383 }
11384 }
11385
Peter Maydell9375ad12017-12-13 17:59:25 +000011386 fi->type = ARMFault_Permission;
11387 fi->level = 1;
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011388 return !(*prot & (1 << access_type));
11389}
11390
Peter Maydell35337cc2017-10-06 16:46:49 +010011391static bool v8m_is_sau_exempt(CPUARMState *env,
11392 uint32_t address, MMUAccessType access_type)
11393{
11394 /* The architecture specifies that certain address ranges are
11395 * exempt from v8M SAU/IDAU checks.
11396 */
11397 return
11398 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
11399 (address >= 0xe0000000 && address <= 0xe0002fff) ||
11400 (address >= 0xe000e000 && address <= 0xe000efff) ||
11401 (address >= 0xe002e000 && address <= 0xe002efff) ||
11402 (address >= 0xe0040000 && address <= 0xe0041fff) ||
11403 (address >= 0xe00ff000 && address <= 0xe00fffff);
11404}
11405
Philippe Mathieu-Daudé787a7e72019-07-01 17:26:22 +010011406void v8m_security_lookup(CPUARMState *env, uint32_t address,
Peter Maydell35337cc2017-10-06 16:46:49 +010011407 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11408 V8M_SAttributes *sattrs)
11409{
11410 /* Look up the security attributes for this address. Compare the
11411 * pseudocode SecurityCheck() function.
11412 * We assume the caller has zero-initialized *sattrs.
11413 */
Richard Henderson2fc0cc02019-03-22 17:41:14 -070011414 ARMCPU *cpu = env_archcpu(env);
Peter Maydell35337cc2017-10-06 16:46:49 +010011415 int r;
Peter Maydell181962f2018-03-02 10:45:36 +000011416 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
11417 int idau_region = IREGION_NOTVALID;
Peter Maydell72042432018-06-26 17:50:42 +010011418 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11419 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
Peter Maydell35337cc2017-10-06 16:46:49 +010011420
Peter Maydell181962f2018-03-02 10:45:36 +000011421 if (cpu->idau) {
11422 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
11423 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
11424
11425 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
11426 &idau_nsc);
11427 }
Peter Maydell35337cc2017-10-06 16:46:49 +010011428
11429 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
11430 /* 0xf0000000..0xffffffff is always S for insn fetches */
11431 return;
11432 }
11433
Peter Maydell181962f2018-03-02 10:45:36 +000011434 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
Peter Maydell35337cc2017-10-06 16:46:49 +010011435 sattrs->ns = !regime_is_secure(env, mmu_idx);
11436 return;
11437 }
11438
Peter Maydell181962f2018-03-02 10:45:36 +000011439 if (idau_region != IREGION_NOTVALID) {
11440 sattrs->irvalid = true;
11441 sattrs->iregion = idau_region;
11442 }
11443
Peter Maydell35337cc2017-10-06 16:46:49 +010011444 switch (env->sau.ctrl & 3) {
11445 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
11446 break;
11447 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
11448 sattrs->ns = true;
11449 break;
11450 default: /* SAU.ENABLE == 1 */
11451 for (r = 0; r < cpu->sau_sregion; r++) {
11452 if (env->sau.rlar[r] & 1) {
11453 uint32_t base = env->sau.rbar[r] & ~0x1f;
11454 uint32_t limit = env->sau.rlar[r] | 0x1f;
11455
11456 if (base <= address && limit >= address) {
Peter Maydell72042432018-06-26 17:50:42 +010011457 if (base > addr_page_base || limit < addr_page_limit) {
11458 sattrs->subpage = true;
11459 }
Peter Maydell35337cc2017-10-06 16:46:49 +010011460 if (sattrs->srvalid) {
11461 /* If we hit in more than one region then we must report
11462 * as Secure, not NS-Callable, with no valid region
11463 * number info.
11464 */
11465 sattrs->ns = false;
11466 sattrs->nsc = false;
11467 sattrs->sregion = 0;
11468 sattrs->srvalid = false;
11469 break;
11470 } else {
11471 if (env->sau.rlar[r] & 2) {
11472 sattrs->nsc = true;
11473 } else {
11474 sattrs->ns = true;
11475 }
11476 sattrs->srvalid = true;
11477 sattrs->sregion = r;
11478 }
Peter Maydell9d2b5a52018-07-23 15:21:26 +010011479 } else {
11480 /*
11481 * Address not in this region. We must check whether the
11482 * region covers addresses in the same page as our address.
11483 * In that case we must not report a size that covers the
11484 * whole page for a subsequent hit against a different MPU
11485 * region or the background region, because it would result
11486 * in incorrect TLB hits for subsequent accesses to
11487 * addresses that are in this MPU region.
11488 */
11489 if (limit >= base &&
11490 ranges_overlap(base, limit - base + 1,
11491 addr_page_base,
11492 TARGET_PAGE_SIZE)) {
11493 sattrs->subpage = true;
11494 }
Peter Maydell35337cc2017-10-06 16:46:49 +010011495 }
11496 }
11497 }
Peter Maydell35337cc2017-10-06 16:46:49 +010011498 break;
11499 }
Thomas Roth7e3f1222019-01-29 11:46:03 +000011500
11501 /*
11502 * The IDAU will override the SAU lookup results if it specifies
11503 * higher security than the SAU does.
11504 */
11505 if (!idau_ns) {
11506 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
11507 sattrs->ns = false;
11508 sattrs->nsc = idau_nsc;
11509 }
11510 }
Peter Maydell35337cc2017-10-06 16:46:49 +010011511}
11512
Philippe Mathieu-Daudé787a7e72019-07-01 17:26:22 +010011513bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
Peter Maydell54317c02017-12-13 17:59:24 +000011514 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11515 hwaddr *phys_ptr, MemTxAttrs *txattrs,
Peter Maydell72042432018-06-26 17:50:42 +010011516 int *prot, bool *is_subpage,
11517 ARMMMUFaultInfo *fi, uint32_t *mregion)
Peter Maydell504e3cc2017-09-07 13:54:51 +010011518{
Peter Maydell54317c02017-12-13 17:59:24 +000011519 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
11520 * that a full phys-to-virt translation does).
11521 * mregion is (if not NULL) set to the region number which matched,
11522 * or -1 if no region number is returned (MPU off, address did not
11523 * hit a region, address hit in multiple regions).
Peter Maydell72042432018-06-26 17:50:42 +010011524 * We set is_subpage to true if the region hit doesn't cover the
11525 * entire TARGET_PAGE the address is within.
Peter Maydell54317c02017-12-13 17:59:24 +000011526 */
Richard Henderson2fc0cc02019-03-22 17:41:14 -070011527 ARMCPU *cpu = env_archcpu(env);
Peter Maydell504e3cc2017-09-07 13:54:51 +010011528 bool is_user = regime_is_user(env, mmu_idx);
Peter Maydell62c58ee2017-09-07 13:54:53 +010011529 uint32_t secure = regime_is_secure(env, mmu_idx);
Peter Maydell504e3cc2017-09-07 13:54:51 +010011530 int n;
11531 int matchregion = -1;
11532 bool hit = false;
Peter Maydell72042432018-06-26 17:50:42 +010011533 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11534 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
Peter Maydell504e3cc2017-09-07 13:54:51 +010011535
Peter Maydell72042432018-06-26 17:50:42 +010011536 *is_subpage = false;
Peter Maydell504e3cc2017-09-07 13:54:51 +010011537 *phys_ptr = address;
11538 *prot = 0;
Peter Maydell54317c02017-12-13 17:59:24 +000011539 if (mregion) {
11540 *mregion = -1;
Peter Maydell35337cc2017-10-06 16:46:49 +010011541 }
11542
Peter Maydell504e3cc2017-09-07 13:54:51 +010011543 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
11544 * was an exception vector read from the vector table (which is always
11545 * done using the default system address map), because those accesses
11546 * are done in arm_v7m_load_vector(), which always does a direct
11547 * read using address_space_ldl(), rather than going via this function.
11548 */
11549 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
11550 hit = true;
11551 } else if (m_is_ppb_region(env, address)) {
11552 hit = true;
Peter Maydell504e3cc2017-09-07 13:54:51 +010011553 } else {
Peter Maydellcff21312019-02-21 18:17:45 +000011554 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
11555 hit = true;
11556 }
11557
Peter Maydell504e3cc2017-09-07 13:54:51 +010011558 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11559 /* region search */
11560 /* Note that the base address is bits [31:5] from the register
11561 * with bits [4:0] all zeroes, but the limit address is bits
11562 * [31:5] from the register with bits [4:0] all ones.
11563 */
Peter Maydell62c58ee2017-09-07 13:54:53 +010011564 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
11565 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
Peter Maydell504e3cc2017-09-07 13:54:51 +010011566
Peter Maydell62c58ee2017-09-07 13:54:53 +010011567 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
Peter Maydell504e3cc2017-09-07 13:54:51 +010011568 /* Region disabled */
11569 continue;
11570 }
11571
11572 if (address < base || address > limit) {
Peter Maydell9d2b5a52018-07-23 15:21:26 +010011573 /*
11574 * Address not in this region. We must check whether the
11575 * region covers addresses in the same page as our address.
11576 * In that case we must not report a size that covers the
11577 * whole page for a subsequent hit against a different MPU
11578 * region or the background region, because it would result in
11579 * incorrect TLB hits for subsequent accesses to addresses that
11580 * are in this MPU region.
11581 */
11582 if (limit >= base &&
11583 ranges_overlap(base, limit - base + 1,
11584 addr_page_base,
11585 TARGET_PAGE_SIZE)) {
11586 *is_subpage = true;
11587 }
Peter Maydell504e3cc2017-09-07 13:54:51 +010011588 continue;
11589 }
11590
Peter Maydell72042432018-06-26 17:50:42 +010011591 if (base > addr_page_base || limit < addr_page_limit) {
11592 *is_subpage = true;
11593 }
11594
Peter Maydellcff21312019-02-21 18:17:45 +000011595 if (matchregion != -1) {
Peter Maydell504e3cc2017-09-07 13:54:51 +010011596 /* Multiple regions match -- always a failure (unlike
11597 * PMSAv7 where highest-numbered-region wins)
11598 */
Peter Maydell3f551b52017-12-13 17:59:25 +000011599 fi->type = ARMFault_Permission;
11600 fi->level = 1;
Peter Maydell504e3cc2017-09-07 13:54:51 +010011601 return true;
11602 }
11603
11604 matchregion = n;
11605 hit = true;
Peter Maydell504e3cc2017-09-07 13:54:51 +010011606 }
11607 }
11608
11609 if (!hit) {
11610 /* background fault */
Peter Maydell3f551b52017-12-13 17:59:25 +000011611 fi->type = ARMFault_Background;
Peter Maydell504e3cc2017-09-07 13:54:51 +010011612 return true;
11613 }
11614
11615 if (matchregion == -1) {
11616 /* hit using the background region */
11617 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11618 } else {
Peter Maydell62c58ee2017-09-07 13:54:53 +010011619 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
11620 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
Peter Maydell504e3cc2017-09-07 13:54:51 +010011621
11622 if (m_is_system_region(env, address)) {
11623 /* System space is always execute never */
11624 xn = 1;
11625 }
11626
11627 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
11628 if (*prot && !xn) {
11629 *prot |= PAGE_EXEC;
11630 }
11631 /* We don't need to look the attribute up in the MAIR0/MAIR1
11632 * registers because that only tells us about cacheability.
11633 */
Peter Maydell54317c02017-12-13 17:59:24 +000011634 if (mregion) {
11635 *mregion = matchregion;
11636 }
Peter Maydell504e3cc2017-09-07 13:54:51 +010011637 }
11638
Peter Maydell3f551b52017-12-13 17:59:25 +000011639 fi->type = ARMFault_Permission;
11640 fi->level = 1;
Peter Maydell504e3cc2017-09-07 13:54:51 +010011641 return !(*prot & (1 << access_type));
11642}
11643
Peter Maydell54317c02017-12-13 17:59:24 +000011644
11645static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
11646 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11647 hwaddr *phys_ptr, MemTxAttrs *txattrs,
Peter Maydell72042432018-06-26 17:50:42 +010011648 int *prot, target_ulong *page_size,
11649 ARMMMUFaultInfo *fi)
Peter Maydell54317c02017-12-13 17:59:24 +000011650{
11651 uint32_t secure = regime_is_secure(env, mmu_idx);
11652 V8M_SAttributes sattrs = {};
Peter Maydell72042432018-06-26 17:50:42 +010011653 bool ret;
11654 bool mpu_is_subpage;
Peter Maydell54317c02017-12-13 17:59:24 +000011655
11656 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
11657 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
11658 if (access_type == MMU_INST_FETCH) {
11659 /* Instruction fetches always use the MMU bank and the
11660 * transaction attribute determined by the fetch address,
11661 * regardless of CPU state. This is painful for QEMU
11662 * to handle, because it would mean we need to encode
11663 * into the mmu_idx not just the (user, negpri) information
11664 * for the current security state but also that for the
11665 * other security state, which would balloon the number
11666 * of mmu_idx values needed alarmingly.
11667 * Fortunately we can avoid this because it's not actually
11668 * possible to arbitrarily execute code from memory with
11669 * the wrong security attribute: it will always generate
11670 * an exception of some kind or another, apart from the
11671 * special case of an NS CPU executing an SG instruction
11672 * in S&NSC memory. So we always just fail the translation
11673 * here and sort things out in the exception handler
11674 * (including possibly emulating an SG instruction).
11675 */
11676 if (sattrs.ns != !secure) {
Peter Maydell3f551b52017-12-13 17:59:25 +000011677 if (sattrs.nsc) {
11678 fi->type = ARMFault_QEMU_NSCExec;
11679 } else {
11680 fi->type = ARMFault_QEMU_SFault;
11681 }
Peter Maydell72042432018-06-26 17:50:42 +010011682 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
Peter Maydell54317c02017-12-13 17:59:24 +000011683 *phys_ptr = address;
11684 *prot = 0;
11685 return true;
11686 }
11687 } else {
11688 /* For data accesses we always use the MMU bank indicated
11689 * by the current CPU state, but the security attributes
11690 * might downgrade a secure access to nonsecure.
11691 */
11692 if (sattrs.ns) {
11693 txattrs->secure = false;
11694 } else if (!secure) {
11695 /* NS access to S memory must fault.
11696 * Architecturally we should first check whether the
11697 * MPU information for this address indicates that we
11698 * are doing an unaligned access to Device memory, which
11699 * should generate a UsageFault instead. QEMU does not
11700 * currently check for that kind of unaligned access though.
11701 * If we added it we would need to do so as a special case
11702 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
11703 */
Peter Maydell3f551b52017-12-13 17:59:25 +000011704 fi->type = ARMFault_QEMU_SFault;
Peter Maydell72042432018-06-26 17:50:42 +010011705 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
Peter Maydell54317c02017-12-13 17:59:24 +000011706 *phys_ptr = address;
11707 *prot = 0;
11708 return true;
11709 }
11710 }
11711 }
11712
Peter Maydell72042432018-06-26 17:50:42 +010011713 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
11714 txattrs, prot, &mpu_is_subpage, fi, NULL);
Peter Maydell72042432018-06-26 17:50:42 +010011715 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
11716 return ret;
Peter Maydell54317c02017-12-13 17:59:24 +000011717}
11718
Peter Crosthwaite13689d42015-06-15 18:06:10 +010011719static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
Peter Maydell03ae85f2017-09-04 15:21:51 +010011720 MMUAccessType access_type, ARMMMUIdx mmu_idx,
Peter Maydell53a4e5c2017-12-13 17:59:25 +000011721 hwaddr *phys_ptr, int *prot,
11722 ARMMMUFaultInfo *fi)
pbrook9ee6e8b2007-11-11 00:04:49 +000011723{
11724 int n;
11725 uint32_t mask;
11726 uint32_t base;
Peter Maydell0480f692015-02-05 13:37:24 +000011727 bool is_user = regime_is_user(env, mmu_idx);
pbrook9ee6e8b2007-11-11 00:04:49 +000011728
Peter Maydell3279adb2017-09-04 15:21:51 +010011729 if (regime_translation_disabled(env, mmu_idx)) {
11730 /* MPU disabled. */
11731 *phys_ptr = address;
11732 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11733 return false;
11734 }
11735
pbrook9ee6e8b2007-11-11 00:04:49 +000011736 *phys_ptr = address;
11737 for (n = 7; n >= 0; n--) {
Peter Maydell554b0b02015-02-05 13:37:24 +000011738 base = env->cp15.c6_region[n];
Peter Maydell87c3d482015-02-05 13:37:24 +000011739 if ((base & 1) == 0) {
Peter Maydell554b0b02015-02-05 13:37:24 +000011740 continue;
Peter Maydell87c3d482015-02-05 13:37:24 +000011741 }
Peter Maydell554b0b02015-02-05 13:37:24 +000011742 mask = 1 << ((base >> 1) & 0x1f);
11743 /* Keep this shift separate from the above to avoid an
11744 (undefined) << 32. */
11745 mask = (mask << 1) - 1;
Peter Maydell87c3d482015-02-05 13:37:24 +000011746 if (((base ^ address) & ~mask) == 0) {
Peter Maydell554b0b02015-02-05 13:37:24 +000011747 break;
Peter Maydell87c3d482015-02-05 13:37:24 +000011748 }
pbrook9ee6e8b2007-11-11 00:04:49 +000011749 }
Peter Maydell87c3d482015-02-05 13:37:24 +000011750 if (n < 0) {
Peter Maydell53a4e5c2017-12-13 17:59:25 +000011751 fi->type = ARMFault_Background;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010011752 return true;
Peter Maydell87c3d482015-02-05 13:37:24 +000011753 }
pbrook9ee6e8b2007-11-11 00:04:49 +000011754
Peter Maydell03ae85f2017-09-04 15:21:51 +010011755 if (access_type == MMU_INST_FETCH) {
Peter Maydell7e097972014-04-15 19:18:41 +010011756 mask = env->cp15.pmsav5_insn_ap;
pbrook9ee6e8b2007-11-11 00:04:49 +000011757 } else {
Peter Maydell7e097972014-04-15 19:18:41 +010011758 mask = env->cp15.pmsav5_data_ap;
pbrook9ee6e8b2007-11-11 00:04:49 +000011759 }
11760 mask = (mask >> (n * 4)) & 0xf;
11761 switch (mask) {
11762 case 0:
Peter Maydell53a4e5c2017-12-13 17:59:25 +000011763 fi->type = ARMFault_Permission;
11764 fi->level = 1;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010011765 return true;
pbrook9ee6e8b2007-11-11 00:04:49 +000011766 case 1:
Peter Maydell87c3d482015-02-05 13:37:24 +000011767 if (is_user) {
Peter Maydell53a4e5c2017-12-13 17:59:25 +000011768 fi->type = ARMFault_Permission;
11769 fi->level = 1;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010011770 return true;
Peter Maydell87c3d482015-02-05 13:37:24 +000011771 }
Peter Maydell554b0b02015-02-05 13:37:24 +000011772 *prot = PAGE_READ | PAGE_WRITE;
11773 break;
pbrook9ee6e8b2007-11-11 00:04:49 +000011774 case 2:
Peter Maydell554b0b02015-02-05 13:37:24 +000011775 *prot = PAGE_READ;
Peter Maydell87c3d482015-02-05 13:37:24 +000011776 if (!is_user) {
Peter Maydell554b0b02015-02-05 13:37:24 +000011777 *prot |= PAGE_WRITE;
Peter Maydell87c3d482015-02-05 13:37:24 +000011778 }
Peter Maydell554b0b02015-02-05 13:37:24 +000011779 break;
pbrook9ee6e8b2007-11-11 00:04:49 +000011780 case 3:
Peter Maydell554b0b02015-02-05 13:37:24 +000011781 *prot = PAGE_READ | PAGE_WRITE;
11782 break;
pbrook9ee6e8b2007-11-11 00:04:49 +000011783 case 5:
Peter Maydell87c3d482015-02-05 13:37:24 +000011784 if (is_user) {
Peter Maydell53a4e5c2017-12-13 17:59:25 +000011785 fi->type = ARMFault_Permission;
11786 fi->level = 1;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010011787 return true;
Peter Maydell87c3d482015-02-05 13:37:24 +000011788 }
Peter Maydell554b0b02015-02-05 13:37:24 +000011789 *prot = PAGE_READ;
11790 break;
pbrook9ee6e8b2007-11-11 00:04:49 +000011791 case 6:
Peter Maydell554b0b02015-02-05 13:37:24 +000011792 *prot = PAGE_READ;
11793 break;
pbrook9ee6e8b2007-11-11 00:04:49 +000011794 default:
Peter Maydell554b0b02015-02-05 13:37:24 +000011795 /* Bad permission. */
Peter Maydell53a4e5c2017-12-13 17:59:25 +000011796 fi->type = ARMFault_Permission;
11797 fi->level = 1;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010011798 return true;
pbrook9ee6e8b2007-11-11 00:04:49 +000011799 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +053011800 *prot |= PAGE_EXEC;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010011801 return false;
pbrook9ee6e8b2007-11-11 00:04:49 +000011802}
11803
Andrew Baumann5b2d2612017-11-07 13:03:51 +000011804/* Combine either inner or outer cacheability attributes for normal
11805 * memory, according to table D4-42 and pseudocode procedure
11806 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
11807 *
11808 * NB: only stage 1 includes allocation hints (RW bits), leading to
11809 * some asymmetry.
11810 */
11811static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
11812{
11813 if (s1 == 4 || s2 == 4) {
11814 /* non-cacheable has precedence */
11815 return 4;
11816 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
11817 /* stage 1 write-through takes precedence */
11818 return s1;
11819 } else if (extract32(s2, 2, 2) == 2) {
11820 /* stage 2 write-through takes precedence, but the allocation hint
11821 * is still taken from stage 1
11822 */
11823 return (2 << 2) | extract32(s1, 0, 2);
11824 } else { /* write-back */
11825 return s1;
11826 }
11827}
11828
11829/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
11830 * and CombineS1S2Desc()
11831 *
11832 * @s1: Attributes from stage 1 walk
11833 * @s2: Attributes from stage 2 walk
11834 */
11835static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
11836{
Richard Henderson337a03f2020-06-25 20:31:40 -070011837 uint8_t s1lo, s2lo, s1hi, s2hi;
Andrew Baumann5b2d2612017-11-07 13:03:51 +000011838 ARMCacheAttrs ret;
Richard Henderson337a03f2020-06-25 20:31:40 -070011839 bool tagged = false;
11840
11841 if (s1.attrs == 0xf0) {
11842 tagged = true;
11843 s1.attrs = 0xff;
11844 }
11845
11846 s1lo = extract32(s1.attrs, 0, 4);
11847 s2lo = extract32(s2.attrs, 0, 4);
11848 s1hi = extract32(s1.attrs, 4, 4);
11849 s2hi = extract32(s2.attrs, 4, 4);
Andrew Baumann5b2d2612017-11-07 13:03:51 +000011850
11851 /* Combine shareability attributes (table D4-43) */
11852 if (s1.shareability == 2 || s2.shareability == 2) {
11853 /* if either are outer-shareable, the result is outer-shareable */
11854 ret.shareability = 2;
11855 } else if (s1.shareability == 3 || s2.shareability == 3) {
11856 /* if either are inner-shareable, the result is inner-shareable */
11857 ret.shareability = 3;
11858 } else {
11859 /* both non-shareable */
11860 ret.shareability = 0;
11861 }
11862
11863 /* Combine memory type and cacheability attributes */
11864 if (s1hi == 0 || s2hi == 0) {
11865 /* Device has precedence over normal */
11866 if (s1lo == 0 || s2lo == 0) {
11867 /* nGnRnE has precedence over anything */
11868 ret.attrs = 0;
11869 } else if (s1lo == 4 || s2lo == 4) {
11870 /* non-Reordering has precedence over Reordering */
11871 ret.attrs = 4; /* nGnRE */
11872 } else if (s1lo == 8 || s2lo == 8) {
11873 /* non-Gathering has precedence over Gathering */
11874 ret.attrs = 8; /* nGRE */
11875 } else {
11876 ret.attrs = 0xc; /* GRE */
11877 }
11878
11879 /* Any location for which the resultant memory type is any
11880 * type of Device memory is always treated as Outer Shareable.
11881 */
11882 ret.shareability = 2;
11883 } else { /* Normal memory */
11884 /* Outer/inner cacheability combine independently */
11885 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
11886 | combine_cacheattr_nibble(s1lo, s2lo);
11887
11888 if (ret.attrs == 0x44) {
11889 /* Any location for which the resultant memory type is Normal
11890 * Inner Non-cacheable, Outer Non-cacheable is always treated
11891 * as Outer Shareable.
11892 */
11893 ret.shareability = 2;
11894 }
11895 }
11896
Richard Henderson337a03f2020-06-25 20:31:40 -070011897 /* TODO: CombineS1S2Desc does not consider transient, only WB, RWA. */
11898 if (tagged && ret.attrs == 0xff) {
11899 ret.attrs = 0xf0;
11900 }
11901
Andrew Baumann5b2d2612017-11-07 13:03:51 +000011902 return ret;
11903}
11904
11905
Peter Maydell702a9352012-07-12 10:59:10 +000011906/* get_phys_addr - get the physical address for this virtual address
11907 *
11908 * Find the physical address corresponding to the given virtual address,
11909 * by doing a translation table walk on MMU based systems or using the
11910 * MPU state on MPU based systems.
11911 *
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010011912 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
11913 * prot and page_size may not be filled in, and the populated fsr value provides
Peter Maydell702a9352012-07-12 10:59:10 +000011914 * information on why the translation aborted, in the format of a
11915 * DFSR/IFSR fault register, with the following caveats:
11916 * * we honour the short vs long DFSR format differences.
11917 * * the WnR bit is never set (the caller must do this).
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010011918 * * for PSMAv5 based systems we don't bother to return a full FSR format
Peter Maydell702a9352012-07-12 10:59:10 +000011919 * value.
11920 *
11921 * @env: CPUARMState
11922 * @address: virtual address to get physical address for
11923 * @access_type: 0 for read, 1 for write, 2 for execute
Peter Maydelld3649702015-02-05 13:37:24 +000011924 * @mmu_idx: MMU index indicating required translation regime
Peter Maydell702a9352012-07-12 10:59:10 +000011925 * @phys_ptr: set to the physical address corresponding to the virtual address
Peter Maydell8bf5b6a2015-04-26 16:49:25 +010011926 * @attrs: set to the memory transaction attributes to use
Peter Maydell702a9352012-07-12 10:59:10 +000011927 * @prot: set to the permissions for the page containing phys_ptr
11928 * @page_size: set to the size of the page containing phys_ptr
Andrew Baumann5b2d2612017-11-07 13:03:51 +000011929 * @fi: set to fault info if the translation fails
11930 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
Peter Maydell702a9352012-07-12 10:59:10 +000011931 */
Philippe Mathieu-Daudéebae8612019-07-01 17:26:20 +010011932bool get_phys_addr(CPUARMState *env, target_ulong address,
11933 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11934 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
11935 target_ulong *page_size,
11936 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
pbrook9ee6e8b2007-11-11 00:04:49 +000011937{
Richard Henderson452ef8c2020-02-08 12:57:58 +000011938 if (mmu_idx == ARMMMUIdx_E10_0 ||
11939 mmu_idx == ARMMMUIdx_E10_1 ||
11940 mmu_idx == ARMMMUIdx_E10_1_PAN) {
Edgar E. Iglesias9b539262015-10-26 14:02:07 +010011941 /* Call ourselves recursively to do the stage 1 and then stage 2
11942 * translations.
Peter Maydell0480f692015-02-05 13:37:24 +000011943 */
Edgar E. Iglesias9b539262015-10-26 14:02:07 +010011944 if (arm_feature(env, ARM_FEATURE_EL2)) {
11945 hwaddr ipa;
11946 int s2_prot;
11947 int ret;
Andrew Baumann5b2d2612017-11-07 13:03:51 +000011948 ARMCacheAttrs cacheattrs2 = {};
Edgar E. Iglesias9b539262015-10-26 14:02:07 +010011949
11950 ret = get_phys_addr(env, address, access_type,
Peter Maydell8bd5c822017-06-02 11:51:47 +010011951 stage_1_mmu_idx(mmu_idx), &ipa, attrs,
Peter Maydellbc52bfe2017-12-13 17:59:25 +000011952 prot, page_size, fi, cacheattrs);
Edgar E. Iglesias9b539262015-10-26 14:02:07 +010011953
11954 /* If S1 fails or S2 is disabled, return early. */
Richard Henderson97fa9352020-02-07 14:04:22 +000011955 if (ret || regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
Edgar E. Iglesias9b539262015-10-26 14:02:07 +010011956 *phys_ptr = ipa;
11957 return ret;
11958 }
11959
11960 /* S1 is done. Now do S2 translation. */
Richard Henderson97fa9352020-02-07 14:04:22 +000011961 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_Stage2,
Peter Maydellff7de2f2020-03-30 22:03:59 +010011962 mmu_idx == ARMMMUIdx_E10_0,
Edgar E. Iglesias9b539262015-10-26 14:02:07 +010011963 phys_ptr, attrs, &s2_prot,
Richard Henderson7e98e212020-06-25 20:31:39 -070011964 page_size, fi, &cacheattrs2);
Edgar E. Iglesias9b539262015-10-26 14:02:07 +010011965 fi->s2addr = ipa;
11966 /* Combine the S1 and S2 perms. */
11967 *prot &= s2_prot;
Andrew Baumann5b2d2612017-11-07 13:03:51 +000011968
Richard Henderson7e98e212020-06-25 20:31:39 -070011969 /* If S2 fails, return early. */
11970 if (ret) {
11971 return ret;
Andrew Baumann5b2d2612017-11-07 13:03:51 +000011972 }
11973
Richard Henderson7e98e212020-06-25 20:31:39 -070011974 /* Combine the S1 and S2 cache attributes. */
11975 if (env->cp15.hcr_el2 & HCR_DC) {
11976 /*
11977 * HCR.DC forces the first stage attributes to
11978 * Normal Non-Shareable,
11979 * Inner Write-Back Read-Allocate Write-Allocate,
11980 * Outer Write-Back Read-Allocate Write-Allocate.
Richard Henderson337a03f2020-06-25 20:31:40 -070011981 * Do not overwrite Tagged within attrs.
Richard Henderson7e98e212020-06-25 20:31:39 -070011982 */
Richard Henderson337a03f2020-06-25 20:31:40 -070011983 if (cacheattrs->attrs != 0xf0) {
11984 cacheattrs->attrs = 0xff;
11985 }
Richard Henderson7e98e212020-06-25 20:31:39 -070011986 cacheattrs->shareability = 0;
11987 }
11988 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
11989 return 0;
Edgar E. Iglesias9b539262015-10-26 14:02:07 +010011990 } else {
11991 /*
11992 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
11993 */
Peter Maydell8bd5c822017-06-02 11:51:47 +010011994 mmu_idx = stage_1_mmu_idx(mmu_idx);
Edgar E. Iglesias9b539262015-10-26 14:02:07 +010011995 }
Fabian Aggeler54bf36e2014-12-11 12:07:52 +000011996 }
bellardb5ff1b32005-11-26 10:38:39 +000011997
Peter Maydell8bf5b6a2015-04-26 16:49:25 +010011998 /* The page table entries may downgrade secure to non-secure, but
11999 * cannot upgrade an non-secure translation regime's attributes
12000 * to secure.
12001 */
12002 attrs->secure = regime_is_secure(env, mmu_idx);
Peter Maydell0995bf82015-04-26 16:49:25 +010012003 attrs->user = regime_is_user(env, mmu_idx);
Peter Maydell8bf5b6a2015-04-26 16:49:25 +010012004
Peter Maydell0480f692015-02-05 13:37:24 +000012005 /* Fast Context Switch Extension. This doesn't exist at all in v8.
12006 * In v7 and earlier it affects all stage 1 translations.
12007 */
Richard Henderson97fa9352020-02-07 14:04:22 +000012008 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2
Peter Maydell0480f692015-02-05 13:37:24 +000012009 && !arm_feature(env, ARM_FEATURE_V8)) {
12010 if (regime_el(env, mmu_idx) == 3) {
12011 address += env->cp15.fcseidr_s;
12012 } else {
12013 address += env->cp15.fcseidr_ns;
12014 }
12015 }
12016
Peter Maydell3279adb2017-09-04 15:21:51 +010012017 if (arm_feature(env, ARM_FEATURE_PMSA)) {
Michael Davidsaverc9f9f122017-06-02 11:51:48 +010012018 bool ret;
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010012019 *page_size = TARGET_PAGE_SIZE;
Peter Maydell3279adb2017-09-04 15:21:51 +010012020
Peter Maydell504e3cc2017-09-07 13:54:51 +010012021 if (arm_feature(env, ARM_FEATURE_V8)) {
12022 /* PMSAv8 */
12023 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
Peter Maydell72042432018-06-26 17:50:42 +010012024 phys_ptr, attrs, prot, page_size, fi);
Peter Maydell504e3cc2017-09-07 13:54:51 +010012025 } else if (arm_feature(env, ARM_FEATURE_V7)) {
Peter Maydell3279adb2017-09-04 15:21:51 +010012026 /* PMSAv7 */
12027 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
Peter Maydelle5e40992018-06-26 17:50:41 +010012028 phys_ptr, prot, page_size, fi);
Peter Maydell3279adb2017-09-04 15:21:51 +010012029 } else {
12030 /* Pre-v7 MPU */
12031 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
Peter Maydell53a4e5c2017-12-13 17:59:25 +000012032 phys_ptr, prot, fi);
Peter Maydell3279adb2017-09-04 15:21:51 +010012033 }
12034 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
Michael Davidsaverc9f9f122017-06-02 11:51:48 +010012035 " mmu_idx %u -> %s (prot %c%c%c)\n",
Peter Maydell709e4402017-07-24 15:33:12 +010012036 access_type == MMU_DATA_LOAD ? "reading" :
12037 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
Michael Davidsaverc9f9f122017-06-02 11:51:48 +010012038 (uint32_t)address, mmu_idx,
12039 ret ? "Miss" : "Hit",
12040 *prot & PAGE_READ ? 'r' : '-',
12041 *prot & PAGE_WRITE ? 'w' : '-',
12042 *prot & PAGE_EXEC ? 'x' : '-');
12043
12044 return ret;
Peter Crosthwaitef6bda882015-06-19 14:17:45 +010012045 }
12046
Peter Maydell3279adb2017-09-04 15:21:51 +010012047 /* Definitely a real MMU, not an MPU */
12048
Peter Maydell0480f692015-02-05 13:37:24 +000012049 if (regime_translation_disabled(env, mmu_idx)) {
Richard Henderson337a03f2020-06-25 20:31:40 -070012050 uint64_t hcr;
12051 uint8_t memattr;
12052
Richard Hendersoncebfb642020-03-07 17:29:45 -080012053 /*
12054 * MMU disabled. S1 addresses within aa64 translation regimes are
12055 * still checked for bounds -- see AArch64.TranslateAddressS1Off.
12056 */
12057 if (mmu_idx != ARMMMUIdx_Stage2) {
12058 int r_el = regime_el(env, mmu_idx);
12059 if (arm_el_is_aa64(env, r_el)) {
12060 int pamax = arm_pamax(env_archcpu(env));
12061 uint64_t tcr = env->cp15.tcr_el[r_el].raw_tcr;
12062 int addrtop, tbi;
12063
12064 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
12065 if (access_type == MMU_INST_FETCH) {
12066 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
12067 }
12068 tbi = (tbi >> extract64(address, 55, 1)) & 1;
12069 addrtop = (tbi ? 55 : 63);
12070
12071 if (extract64(address, pamax, addrtop - pamax + 1) != 0) {
12072 fi->type = ARMFault_AddressSize;
12073 fi->level = 0;
12074 fi->stage2 = false;
12075 return 1;
12076 }
12077
12078 /*
12079 * When TBI is disabled, we've just validated that all of the
12080 * bits above PAMax are zero, so logically we only need to
12081 * clear the top byte for TBI. But it's clearer to follow
12082 * the pseudocode set of addrdesc.paddress.
12083 */
12084 address = extract64(address, 0, 52);
12085 }
12086 }
bellardb5ff1b32005-11-26 10:38:39 +000012087 *phys_ptr = address;
Rabin Vincent3ad493f2010-03-20 02:28:03 +053012088 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
Paul Brookd4c430a2010-03-17 02:14:28 +000012089 *page_size = TARGET_PAGE_SIZE;
Richard Henderson337a03f2020-06-25 20:31:40 -070012090
12091 /* Fill in cacheattr a-la AArch64.TranslateAddressS1Off. */
12092 hcr = arm_hcr_el2_eff(env);
12093 cacheattrs->shareability = 0;
12094 if (hcr & HCR_DC) {
12095 if (hcr & HCR_DCT) {
12096 memattr = 0xf0; /* Tagged, Normal, WB, RWA */
12097 } else {
12098 memattr = 0xff; /* Normal, WB, RWA */
12099 }
12100 } else if (access_type == MMU_INST_FETCH) {
12101 if (regime_sctlr(env, mmu_idx) & SCTLR_I) {
12102 memattr = 0xee; /* Normal, WT, RA, NT */
12103 } else {
12104 memattr = 0x44; /* Normal, NC, No */
12105 }
12106 cacheattrs->shareability = 2; /* outer sharable */
12107 } else {
12108 memattr = 0x00; /* Device, nGnRnE */
12109 }
12110 cacheattrs->attrs = memattr;
pbrook9ee6e8b2007-11-11 00:04:49 +000012111 return 0;
Peter Maydell0480f692015-02-05 13:37:24 +000012112 }
12113
Peter Maydell0480f692015-02-05 13:37:24 +000012114 if (regime_using_lpae_format(env, mmu_idx)) {
Peter Maydellff7de2f2020-03-30 22:03:59 +010012115 return get_phys_addr_lpae(env, address, access_type, mmu_idx, false,
Peter Maydellbc52bfe2017-12-13 17:59:25 +000012116 phys_ptr, attrs, prot, page_size,
12117 fi, cacheattrs);
Peter Maydell0480f692015-02-05 13:37:24 +000012118 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
Peter Maydellbc52bfe2017-12-13 17:59:25 +000012119 return get_phys_addr_v6(env, address, access_type, mmu_idx,
12120 phys_ptr, attrs, prot, page_size, fi);
bellardb5ff1b32005-11-26 10:38:39 +000012121 } else {
Peter Maydellbc52bfe2017-12-13 17:59:25 +000012122 return get_phys_addr_v5(env, address, access_type, mmu_idx,
Peter Maydellf9899832017-12-13 17:59:24 +000012123 phys_ptr, prot, page_size, fi);
bellardb5ff1b32005-11-26 10:38:39 +000012124 }
bellardb5ff1b32005-11-26 10:38:39 +000012125}
12126
Peter Maydell0faea0c2016-01-21 14:15:07 +000012127hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
12128 MemTxAttrs *attrs)
bellardb5ff1b32005-11-26 10:38:39 +000012129{
Andreas Färber00b941e2013-06-29 18:55:54 +020012130 ARMCPU *cpu = ARM_CPU(cs);
Peter Maydelld3649702015-02-05 13:37:24 +000012131 CPUARMState *env = &cpu->env;
Avi Kivitya8170e52012-10-23 12:30:10 +020012132 hwaddr phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +000012133 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +000012134 int prot;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010012135 bool ret;
Edgar E. Iglesiase14b5a22015-10-26 14:02:03 +010012136 ARMMMUFaultInfo fi = {};
Richard Henderson50494a22019-01-21 10:23:12 +000012137 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
Richard Henderson7e98e212020-06-25 20:31:39 -070012138 ARMCacheAttrs cacheattrs = {};
bellardb5ff1b32005-11-26 10:38:39 +000012139
Peter Maydell0faea0c2016-01-21 14:15:07 +000012140 *attrs = (MemTxAttrs) {};
12141
Peter Maydell8bd5c822017-06-02 11:51:47 +010012142 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr,
Richard Henderson7e98e212020-06-25 20:31:39 -070012143 attrs, &prot, &page_size, &fi, &cacheattrs);
bellardb5ff1b32005-11-26 10:38:39 +000012144
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +010012145 if (ret) {
bellardb5ff1b32005-11-26 10:38:39 +000012146 return -1;
Andreas Färber00b941e2013-06-29 18:55:54 +020012147 }
bellardb5ff1b32005-11-26 10:38:39 +000012148 return phys_addr;
12149}
12150
bellardb5ff1b32005-11-26 10:38:39 +000012151#endif
pbrook6ddbc6e2008-03-31 03:46:33 +000012152
12153/* Note that signed overflow is undefined in C. The following routines are
12154 careful to use unsigned types where modulo arithmetic is required.
12155 Failure to do so _will_ break on newer gcc. */
12156
12157/* Signed saturating arithmetic. */
12158
aurel321654b2d2008-04-11 04:55:07 +000012159/* Perform 16-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +000012160static inline uint16_t add16_sat(uint16_t a, uint16_t b)
12161{
12162 uint16_t res;
12163
12164 res = a + b;
12165 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
12166 if (a & 0x8000)
12167 res = 0x8000;
12168 else
12169 res = 0x7fff;
12170 }
12171 return res;
12172}
12173
aurel321654b2d2008-04-11 04:55:07 +000012174/* Perform 8-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +000012175static inline uint8_t add8_sat(uint8_t a, uint8_t b)
12176{
12177 uint8_t res;
12178
12179 res = a + b;
12180 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
12181 if (a & 0x80)
12182 res = 0x80;
12183 else
12184 res = 0x7f;
12185 }
12186 return res;
12187}
12188
aurel321654b2d2008-04-11 04:55:07 +000012189/* Perform 16-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +000012190static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
12191{
12192 uint16_t res;
12193
12194 res = a - b;
12195 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
12196 if (a & 0x8000)
12197 res = 0x8000;
12198 else
12199 res = 0x7fff;
12200 }
12201 return res;
12202}
12203
aurel321654b2d2008-04-11 04:55:07 +000012204/* Perform 8-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +000012205static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
12206{
12207 uint8_t res;
12208
12209 res = a - b;
12210 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
12211 if (a & 0x80)
12212 res = 0x80;
12213 else
12214 res = 0x7f;
12215 }
12216 return res;
12217}
12218
12219#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
12220#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
12221#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
12222#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
12223#define PFX q
12224
12225#include "op_addsub.h"
12226
12227/* Unsigned saturating arithmetic. */
pbrook460a09c2008-05-01 12:04:35 +000012228static inline uint16_t add16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +000012229{
12230 uint16_t res;
12231 res = a + b;
12232 if (res < a)
12233 res = 0xffff;
12234 return res;
12235}
12236
pbrook460a09c2008-05-01 12:04:35 +000012237static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +000012238{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +080012239 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +000012240 return a - b;
12241 else
12242 return 0;
12243}
12244
12245static inline uint8_t add8_usat(uint8_t a, uint8_t b)
12246{
12247 uint8_t res;
12248 res = a + b;
12249 if (res < a)
12250 res = 0xff;
12251 return res;
12252}
12253
12254static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
12255{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +080012256 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +000012257 return a - b;
12258 else
12259 return 0;
12260}
12261
12262#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
12263#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
12264#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
12265#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
12266#define PFX uq
12267
12268#include "op_addsub.h"
12269
12270/* Signed modulo arithmetic. */
12271#define SARITH16(a, b, n, op) do { \
12272 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +000012273 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +000012274 RESULT(sum, n, 16); \
12275 if (sum >= 0) \
12276 ge |= 3 << (n * 2); \
12277 } while(0)
12278
12279#define SARITH8(a, b, n, op) do { \
12280 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +000012281 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +000012282 RESULT(sum, n, 8); \
12283 if (sum >= 0) \
12284 ge |= 1 << n; \
12285 } while(0)
12286
12287
12288#define ADD16(a, b, n) SARITH16(a, b, n, +)
12289#define SUB16(a, b, n) SARITH16(a, b, n, -)
12290#define ADD8(a, b, n) SARITH8(a, b, n, +)
12291#define SUB8(a, b, n) SARITH8(a, b, n, -)
12292#define PFX s
12293#define ARITH_GE
12294
12295#include "op_addsub.h"
12296
12297/* Unsigned modulo arithmetic. */
12298#define ADD16(a, b, n) do { \
12299 uint32_t sum; \
12300 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
12301 RESULT(sum, n, 16); \
balroga87aa102008-07-19 10:46:13 +000012302 if ((sum >> 16) == 1) \
pbrook6ddbc6e2008-03-31 03:46:33 +000012303 ge |= 3 << (n * 2); \
12304 } while(0)
12305
12306#define ADD8(a, b, n) do { \
12307 uint32_t sum; \
12308 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
12309 RESULT(sum, n, 8); \
balroga87aa102008-07-19 10:46:13 +000012310 if ((sum >> 8) == 1) \
12311 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +000012312 } while(0)
12313
12314#define SUB16(a, b, n) do { \
12315 uint32_t sum; \
12316 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
12317 RESULT(sum, n, 16); \
12318 if ((sum >> 16) == 0) \
12319 ge |= 3 << (n * 2); \
12320 } while(0)
12321
12322#define SUB8(a, b, n) do { \
12323 uint32_t sum; \
12324 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
12325 RESULT(sum, n, 8); \
12326 if ((sum >> 8) == 0) \
balroga87aa102008-07-19 10:46:13 +000012327 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +000012328 } while(0)
12329
12330#define PFX u
12331#define ARITH_GE
12332
12333#include "op_addsub.h"
12334
12335/* Halved signed arithmetic. */
12336#define ADD16(a, b, n) \
12337 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
12338#define SUB16(a, b, n) \
12339 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
12340#define ADD8(a, b, n) \
12341 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
12342#define SUB8(a, b, n) \
12343 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
12344#define PFX sh
12345
12346#include "op_addsub.h"
12347
12348/* Halved unsigned arithmetic. */
12349#define ADD16(a, b, n) \
12350 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12351#define SUB16(a, b, n) \
12352 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12353#define ADD8(a, b, n) \
12354 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12355#define SUB8(a, b, n) \
12356 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12357#define PFX uh
12358
12359#include "op_addsub.h"
12360
12361static inline uint8_t do_usad(uint8_t a, uint8_t b)
12362{
12363 if (a > b)
12364 return a - b;
12365 else
12366 return b - a;
12367}
12368
12369/* Unsigned sum of absolute byte differences. */
12370uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
12371{
12372 uint32_t sum;
12373 sum = do_usad(a, b);
12374 sum += do_usad(a >> 8, b >> 8);
12375 sum += do_usad(a >> 16, b >>16);
12376 sum += do_usad(a >> 24, b >> 24);
12377 return sum;
12378}
12379
12380/* For ARMv6 SEL instruction. */
12381uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
12382{
12383 uint32_t mask;
12384
12385 mask = 0;
12386 if (flags & 1)
12387 mask |= 0xff;
12388 if (flags & 2)
12389 mask |= 0xff00;
12390 if (flags & 4)
12391 mask |= 0xff0000;
12392 if (flags & 8)
12393 mask |= 0xff000000;
12394 return (a & mask) | (b & ~mask);
12395}
12396
Peter Maydellaa633462014-06-09 15:43:25 +010012397/* CRC helpers.
12398 * The upper bytes of val (above the number specified by 'bytes') must have
12399 * been zeroed out by the caller.
12400 */
Will Newtoneb0ecd52014-02-26 17:20:07 +000012401uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12402{
12403 uint8_t buf[4];
12404
Peter Maydellaa633462014-06-09 15:43:25 +010012405 stl_le_p(buf, val);
Will Newtoneb0ecd52014-02-26 17:20:07 +000012406
12407 /* zlib crc32 converts the accumulator and output to one's complement. */
12408 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12409}
12410
12411uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12412{
12413 uint8_t buf[4];
12414
Peter Maydellaa633462014-06-09 15:43:25 +010012415 stl_le_p(buf, val);
Will Newtoneb0ecd52014-02-26 17:20:07 +000012416
12417 /* Linux crc32c converts the output to one's complement. */
12418 return crc32c(acc, buf, bytes) ^ 0xffffffff;
12419}
Richard Hendersona9e01312018-01-25 11:45:29 +000012420
12421/* Return the exception level to which FP-disabled exceptions should
12422 * be taken, or 0 if FP is enabled.
12423 */
Richard Hendersonced31552018-10-08 14:55:03 +010012424int fp_exception_el(CPUARMState *env, int cur_el)
Richard Hendersona9e01312018-01-25 11:45:29 +000012425{
Richard Henderson55faa212018-01-25 11:45:29 +000012426#ifndef CONFIG_USER_ONLY
Richard Hendersona9e01312018-01-25 11:45:29 +000012427 /* CPACR and the CPTR registers don't exist before v6, so FP is
12428 * always accessible
12429 */
12430 if (!arm_feature(env, ARM_FEATURE_V6)) {
12431 return 0;
12432 }
12433
Peter Maydelld87513c2019-04-29 17:35:58 +010012434 if (arm_feature(env, ARM_FEATURE_M)) {
12435 /* CPACR can cause a NOCP UsageFault taken to current security state */
12436 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
12437 return 1;
12438 }
12439
12440 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
12441 if (!extract32(env->v7m.nsacr, 10, 1)) {
12442 /* FP insns cause a NOCP UsageFault taken to Secure */
12443 return 3;
12444 }
12445 }
12446
12447 return 0;
12448 }
12449
Richard Hendersona9e01312018-01-25 11:45:29 +000012450 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12451 * 0, 2 : trap EL0 and EL1/PL1 accesses
12452 * 1 : trap only EL0 accesses
12453 * 3 : trap no accesses
Richard Hendersonc2ddb7c2020-02-07 14:04:26 +000012454 * This register is ignored if E2H+TGE are both set.
Richard Hendersona9e01312018-01-25 11:45:29 +000012455 */
Richard Hendersonc2ddb7c2020-02-07 14:04:26 +000012456 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12457 int fpen = extract32(env->cp15.cpacr_el1, 20, 2);
12458
12459 switch (fpen) {
12460 case 0:
12461 case 2:
12462 if (cur_el == 0 || cur_el == 1) {
12463 /* Trap to PL1, which might be EL1 or EL3 */
12464 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
12465 return 3;
12466 }
12467 return 1;
12468 }
12469 if (cur_el == 3 && !is_a64(env)) {
12470 /* Secure PL1 running at EL3 */
Richard Hendersona9e01312018-01-25 11:45:29 +000012471 return 3;
12472 }
Richard Hendersonc2ddb7c2020-02-07 14:04:26 +000012473 break;
12474 case 1:
12475 if (cur_el == 0) {
12476 return 1;
12477 }
12478 break;
12479 case 3:
12480 break;
Richard Hendersona9e01312018-01-25 11:45:29 +000012481 }
Richard Hendersona9e01312018-01-25 11:45:29 +000012482 }
12483
Peter Maydellfc1120a2019-05-10 12:03:57 +010012484 /*
12485 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
12486 * to control non-secure access to the FPU. It doesn't have any
12487 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
12488 */
12489 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
12490 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
12491 if (!extract32(env->cp15.nsacr, 10, 1)) {
12492 /* FP insns act as UNDEF */
12493 return cur_el == 2 ? 2 : 1;
12494 }
12495 }
12496
Richard Hendersona9e01312018-01-25 11:45:29 +000012497 /* For the CPTR registers we don't need to guard with an ARM_FEATURE
12498 * check because zero bits in the registers mean "don't trap".
12499 */
12500
12501 /* CPTR_EL2 : present in v7VE or v8 */
12502 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1)
12503 && !arm_is_secure_below_el3(env)) {
12504 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */
12505 return 2;
12506 }
12507
12508 /* CPTR_EL3 : present in v8 */
12509 if (extract32(env->cp15.cptr_el[3], 10, 1)) {
12510 /* Trap all FP ops to EL3 */
12511 return 3;
12512 }
Richard Henderson55faa212018-01-25 11:45:29 +000012513#endif
Richard Hendersona9e01312018-01-25 11:45:29 +000012514 return 0;
12515}
12516
Richard Hendersonb9f60332020-02-07 14:04:24 +000012517/* Return the exception level we're running at if this is our mmu_idx */
12518int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12519{
12520 if (mmu_idx & ARM_MMU_IDX_M) {
12521 return mmu_idx & ARM_MMU_IDX_M_PRIV;
12522 }
12523
12524 switch (mmu_idx) {
12525 case ARMMMUIdx_E10_0:
12526 case ARMMMUIdx_E20_0:
12527 case ARMMMUIdx_SE10_0:
12528 return 0;
12529 case ARMMMUIdx_E10_1:
Richard Henderson452ef8c2020-02-08 12:57:58 +000012530 case ARMMMUIdx_E10_1_PAN:
Richard Hendersonb9f60332020-02-07 14:04:24 +000012531 case ARMMMUIdx_SE10_1:
Richard Henderson452ef8c2020-02-08 12:57:58 +000012532 case ARMMMUIdx_SE10_1_PAN:
Richard Hendersonb9f60332020-02-07 14:04:24 +000012533 return 1;
12534 case ARMMMUIdx_E2:
12535 case ARMMMUIdx_E20_2:
Richard Henderson452ef8c2020-02-08 12:57:58 +000012536 case ARMMMUIdx_E20_2_PAN:
Richard Hendersonb9f60332020-02-07 14:04:24 +000012537 return 2;
12538 case ARMMMUIdx_SE3:
12539 return 3;
12540 default:
12541 g_assert_not_reached();
12542 }
12543}
12544
Philippe Mathieu-Daudé7aab5a82019-07-04 17:14:43 +010012545#ifndef CONFIG_TCG
Richard Henderson65e46552019-01-21 10:23:12 +000012546ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12547{
Philippe Mathieu-Daudé7aab5a82019-07-04 17:14:43 +010012548 g_assert_not_reached();
Richard Henderson65e46552019-01-21 10:23:12 +000012549}
Philippe Mathieu-Daudé7aab5a82019-07-04 17:14:43 +010012550#endif
Richard Henderson65e46552019-01-21 10:23:12 +000012551
Richard Henderson164690b2019-10-23 11:00:46 -040012552ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
Richard Henderson65e46552019-01-21 10:23:12 +000012553{
Richard Henderson65e46552019-01-21 10:23:12 +000012554 if (arm_feature(env, ARM_FEATURE_M)) {
Richard Henderson50494a22019-01-21 10:23:12 +000012555 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
Richard Henderson65e46552019-01-21 10:23:12 +000012556 }
12557
Richard Henderson6003d982020-02-07 14:04:24 +000012558 /* See ARM pseudo-function ELIsInHost. */
Richard Hendersonb9f60332020-02-07 14:04:24 +000012559 switch (el) {
12560 case 0:
Richard Hendersonb9f60332020-02-07 14:04:24 +000012561 if (arm_is_secure_below_el3(env)) {
12562 return ARMMMUIdx_SE10_0;
12563 }
Richard Henderson6003d982020-02-07 14:04:24 +000012564 if ((env->cp15.hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)
12565 && arm_el_is_aa64(env, 2)) {
12566 return ARMMMUIdx_E20_0;
12567 }
Richard Hendersonb9f60332020-02-07 14:04:24 +000012568 return ARMMMUIdx_E10_0;
12569 case 1:
12570 if (arm_is_secure_below_el3(env)) {
Richard Henderson66412262020-02-08 12:58:08 +000012571 if (env->pstate & PSTATE_PAN) {
12572 return ARMMMUIdx_SE10_1_PAN;
12573 }
Richard Hendersonb9f60332020-02-07 14:04:24 +000012574 return ARMMMUIdx_SE10_1;
12575 }
Richard Henderson66412262020-02-08 12:58:08 +000012576 if (env->pstate & PSTATE_PAN) {
12577 return ARMMMUIdx_E10_1_PAN;
12578 }
Richard Hendersonb9f60332020-02-07 14:04:24 +000012579 return ARMMMUIdx_E10_1;
12580 case 2:
Richard Hendersonb9f60332020-02-07 14:04:24 +000012581 /* TODO: ARMv8.4-SecEL2 */
Richard Henderson6003d982020-02-07 14:04:24 +000012582 /* Note that TGE does not apply at EL2. */
12583 if ((env->cp15.hcr_el2 & HCR_E2H) && arm_el_is_aa64(env, 2)) {
Richard Henderson66412262020-02-08 12:58:08 +000012584 if (env->pstate & PSTATE_PAN) {
12585 return ARMMMUIdx_E20_2_PAN;
12586 }
Richard Henderson6003d982020-02-07 14:04:24 +000012587 return ARMMMUIdx_E20_2;
12588 }
Richard Hendersonb9f60332020-02-07 14:04:24 +000012589 return ARMMMUIdx_E2;
12590 case 3:
12591 return ARMMMUIdx_SE3;
12592 default:
12593 g_assert_not_reached();
Richard Henderson65e46552019-01-21 10:23:12 +000012594 }
Richard Henderson50494a22019-01-21 10:23:12 +000012595}
12596
Richard Henderson164690b2019-10-23 11:00:46 -040012597ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12598{
12599 return arm_mmu_idx_el(env, arm_current_el(env));
12600}
12601
Richard Henderson64be86a2019-01-21 10:23:12 +000012602#ifndef CONFIG_USER_ONLY
12603ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
12604{
12605 return stage_1_mmu_idx(arm_mmu_idx(env));
12606}
12607#endif
12608
Richard Hendersonfdd1b222019-10-23 11:00:34 -040012609static uint32_t rebuild_hflags_common(CPUARMState *env, int fp_el,
12610 ARMMMUIdx mmu_idx, uint32_t flags)
12611{
12612 flags = FIELD_DP32(flags, TBFLAG_ANY, FPEXC_EL, fp_el);
12613 flags = FIELD_DP32(flags, TBFLAG_ANY, MMUIDX,
12614 arm_to_core_mmu_idx(mmu_idx));
12615
Richard Hendersonfdd1b222019-10-23 11:00:34 -040012616 if (arm_singlestep_active(env)) {
12617 flags = FIELD_DP32(flags, TBFLAG_ANY, SS_ACTIVE, 1);
12618 }
12619 return flags;
12620}
12621
Richard Henderson43eccfb2019-10-23 11:00:36 -040012622static uint32_t rebuild_hflags_common_32(CPUARMState *env, int fp_el,
12623 ARMMMUIdx mmu_idx, uint32_t flags)
12624{
Richard Henderson8061a642019-10-23 11:00:37 -040012625 bool sctlr_b = arm_sctlr_b(env);
12626
12627 if (sctlr_b) {
12628 flags = FIELD_DP32(flags, TBFLAG_A32, SCTLR_B, 1);
12629 }
12630 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
12631 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
12632 }
Richard Henderson43eccfb2019-10-23 11:00:36 -040012633 flags = FIELD_DP32(flags, TBFLAG_A32, NS, !access_secure_reg(env));
12634
12635 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
12636}
12637
Richard Henderson6e33ced2019-10-23 11:00:38 -040012638static uint32_t rebuild_hflags_m32(CPUARMState *env, int fp_el,
12639 ARMMMUIdx mmu_idx)
12640{
12641 uint32_t flags = 0;
12642
12643 if (arm_v7m_is_handler_mode(env)) {
Richard Henderson79cabf12020-02-07 14:04:23 +000012644 flags = FIELD_DP32(flags, TBFLAG_M32, HANDLER, 1);
Richard Henderson6e33ced2019-10-23 11:00:38 -040012645 }
12646
12647 /*
12648 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
12649 * is suppressing them because the requested execution priority
12650 * is less than 0.
12651 */
12652 if (arm_feature(env, ARM_FEATURE_V8) &&
12653 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
12654 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
Richard Henderson79cabf12020-02-07 14:04:23 +000012655 flags = FIELD_DP32(flags, TBFLAG_M32, STACKCHECK, 1);
Richard Henderson6e33ced2019-10-23 11:00:38 -040012656 }
12657
12658 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
12659}
12660
Richard Henderson83f4bae2019-10-23 11:00:41 -040012661static uint32_t rebuild_hflags_aprofile(CPUARMState *env)
12662{
12663 int flags = 0;
12664
12665 flags = FIELD_DP32(flags, TBFLAG_ANY, DEBUG_TARGET_EL,
12666 arm_debug_target_el(env));
12667 return flags;
12668}
12669
Richard Hendersonc7472242019-10-23 11:00:40 -040012670static uint32_t rebuild_hflags_a32(CPUARMState *env, int fp_el,
12671 ARMMMUIdx mmu_idx)
12672{
Richard Henderson83f4bae2019-10-23 11:00:41 -040012673 uint32_t flags = rebuild_hflags_aprofile(env);
Richard Henderson0a54d682019-10-23 11:00:44 -040012674
12675 if (arm_el_is_aa64(env, 1)) {
12676 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
12677 }
Marc Zyngier5bb0a202019-12-01 12:20:17 +000012678
12679 if (arm_current_el(env) < 2 && env->cp15.hstr_el2 &&
12680 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12681 flags = FIELD_DP32(flags, TBFLAG_A32, HSTR_ACTIVE, 1);
12682 }
12683
Richard Henderson83f4bae2019-10-23 11:00:41 -040012684 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
Richard Hendersonc7472242019-10-23 11:00:40 -040012685}
12686
Richard Hendersond4d75032019-10-23 11:00:35 -040012687static uint32_t rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
12688 ARMMMUIdx mmu_idx)
12689{
Richard Henderson83f4bae2019-10-23 11:00:41 -040012690 uint32_t flags = rebuild_hflags_aprofile(env);
Richard Hendersond4d75032019-10-23 11:00:35 -040012691 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
Richard Hendersonb830a5e2020-02-16 11:43:43 -080012692 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
Richard Hendersond4d75032019-10-23 11:00:35 -040012693 uint64_t sctlr;
12694 int tbii, tbid;
12695
12696 flags = FIELD_DP32(flags, TBFLAG_ANY, AARCH64_STATE, 1);
12697
Richard Henderson339370b2020-02-07 14:04:24 +000012698 /* Get control bits for tagged addresses. */
Richard Hendersonb830a5e2020-02-16 11:43:43 -080012699 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
12700 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
Richard Hendersond4d75032019-10-23 11:00:35 -040012701
12702 flags = FIELD_DP32(flags, TBFLAG_A64, TBII, tbii);
12703 flags = FIELD_DP32(flags, TBFLAG_A64, TBID, tbid);
12704
12705 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
12706 int sve_el = sve_exception_el(env, el);
12707 uint32_t zcr_len;
12708
12709 /*
12710 * If SVE is disabled, but FP is enabled,
12711 * then the effective len is 0.
12712 */
12713 if (sve_el != 0 && fp_el == 0) {
12714 zcr_len = 0;
12715 } else {
12716 zcr_len = sve_zcr_len_for_el(env, el);
12717 }
12718 flags = FIELD_DP32(flags, TBFLAG_A64, SVEEXC_EL, sve_el);
12719 flags = FIELD_DP32(flags, TBFLAG_A64, ZCR_LEN, zcr_len);
12720 }
12721
Richard Hendersonaaec1432020-02-07 14:04:24 +000012722 sctlr = regime_sctlr(env, stage1);
Richard Hendersond4d75032019-10-23 11:00:35 -040012723
Richard Henderson8061a642019-10-23 11:00:37 -040012724 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
12725 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1);
12726 }
12727
Richard Hendersond4d75032019-10-23 11:00:35 -040012728 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
12729 /*
12730 * In order to save space in flags, we record only whether
12731 * pauth is "inactive", meaning all insns are implemented as
12732 * a nop, or "active" when some action must be performed.
12733 * The decision of which action to take is left to a helper.
12734 */
12735 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
12736 flags = FIELD_DP32(flags, TBFLAG_A64, PAUTH_ACTIVE, 1);
12737 }
12738 }
12739
12740 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12741 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
12742 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
12743 flags = FIELD_DP32(flags, TBFLAG_A64, BT, 1);
12744 }
12745 }
12746
Richard Hendersoncc28fc32020-02-07 14:04:26 +000012747 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
Richard Henderson7a8014a2020-02-08 12:58:15 +000012748 if (!(env->pstate & PSTATE_UAO)) {
12749 switch (mmu_idx) {
12750 case ARMMMUIdx_E10_1:
12751 case ARMMMUIdx_E10_1_PAN:
12752 case ARMMMUIdx_SE10_1:
12753 case ARMMMUIdx_SE10_1_PAN:
12754 /* TODO: ARMv8.3-NV */
Richard Hendersoncc28fc32020-02-07 14:04:26 +000012755 flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1);
Richard Henderson7a8014a2020-02-08 12:58:15 +000012756 break;
12757 case ARMMMUIdx_E20_2:
12758 case ARMMMUIdx_E20_2_PAN:
12759 /* TODO: ARMv8.4-SecEL2 */
12760 /*
12761 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
12762 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
12763 */
12764 if (env->cp15.hcr_el2 & HCR_TGE) {
12765 flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1);
12766 }
12767 break;
12768 default:
12769 break;
Richard Hendersoncc28fc32020-02-07 14:04:26 +000012770 }
Richard Hendersoncc28fc32020-02-07 14:04:26 +000012771 }
12772
Richard Henderson81ae05f2020-06-25 20:31:06 -070012773 if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
12774 /*
12775 * Set MTE_ACTIVE if any access may be Checked, and leave clear
12776 * if all accesses must be Unchecked:
12777 * 1) If no TBI, then there are no tags in the address to check,
12778 * 2) If Tag Check Override, then all accesses are Unchecked,
12779 * 3) If Tag Check Fail == 0, then Checked access have no effect,
12780 * 4) If no Allocation Tag Access, then all accesses are Unchecked.
12781 */
12782 if (allocation_tag_access_enabled(env, el, sctlr)) {
12783 flags = FIELD_DP32(flags, TBFLAG_A64, ATA, 1);
12784 if (tbid
12785 && !(env->pstate & PSTATE_TCO)
12786 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
12787 flags = FIELD_DP32(flags, TBFLAG_A64, MTE_ACTIVE, 1);
12788 }
12789 }
12790 /* And again for unprivileged accesses, if required. */
12791 if (FIELD_EX32(flags, TBFLAG_A64, UNPRIV)
12792 && tbid
12793 && !(env->pstate & PSTATE_TCO)
12794 && (sctlr & SCTLR_TCF0)
12795 && allocation_tag_access_enabled(env, 0, sctlr)) {
12796 flags = FIELD_DP32(flags, TBFLAG_A64, MTE0_ACTIVE, 1);
12797 }
12798 /* Cache TCMA as well as TBI. */
12799 flags = FIELD_DP32(flags, TBFLAG_A64, TCMA,
12800 aa64_va_parameter_tcma(tcr, mmu_idx));
12801 }
12802
Richard Hendersond4d75032019-10-23 11:00:35 -040012803 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
12804}
12805
Richard Henderson3d74e2e2019-10-23 11:00:45 -040012806static uint32_t rebuild_hflags_internal(CPUARMState *env)
12807{
12808 int el = arm_current_el(env);
12809 int fp_el = fp_exception_el(env, el);
Richard Henderson164690b2019-10-23 11:00:46 -040012810 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
Richard Henderson3d74e2e2019-10-23 11:00:45 -040012811
12812 if (is_a64(env)) {
12813 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
12814 } else if (arm_feature(env, ARM_FEATURE_M)) {
12815 return rebuild_hflags_m32(env, fp_el, mmu_idx);
12816 } else {
12817 return rebuild_hflags_a32(env, fp_el, mmu_idx);
12818 }
12819}
12820
12821void arm_rebuild_hflags(CPUARMState *env)
12822{
12823 env->hflags = rebuild_hflags_internal(env);
12824}
12825
Peter Maydell19717e92020-03-03 17:49:49 +000012826/*
12827 * If we have triggered a EL state change we can't rely on the
12828 * translator having passed it to us, we need to recompute.
12829 */
12830void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
12831{
12832 int el = arm_current_el(env);
12833 int fp_el = fp_exception_el(env, el);
12834 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12835 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
12836}
12837
Richard Henderson14f3c582019-10-23 11:00:48 -040012838void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
12839{
12840 int fp_el = fp_exception_el(env, el);
12841 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12842
12843 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
12844}
12845
Alex Bennéef80741d2019-12-12 11:47:34 +000012846/*
12847 * If we have triggered a EL state change we can't rely on the
Peter Maydell563152e2020-03-03 17:49:50 +000012848 * translator having passed it to us, we need to recompute.
Alex Bennéef80741d2019-12-12 11:47:34 +000012849 */
12850void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
12851{
12852 int el = arm_current_el(env);
12853 int fp_el = fp_exception_el(env, el);
12854 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12855 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
12856}
12857
Richard Henderson14f3c582019-10-23 11:00:48 -040012858void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
12859{
12860 int fp_el = fp_exception_el(env, el);
12861 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12862
12863 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
12864}
12865
12866void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
12867{
12868 int fp_el = fp_exception_el(env, el);
12869 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12870
12871 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
12872}
12873
Philippe Mathieu-Daudé0ee8b242019-12-20 14:03:00 +000012874static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
12875{
12876#ifdef CONFIG_DEBUG_TCG
12877 uint32_t env_flags_current = env->hflags;
12878 uint32_t env_flags_rebuilt = rebuild_hflags_internal(env);
12879
12880 if (unlikely(env_flags_current != env_flags_rebuilt)) {
12881 fprintf(stderr, "TCG hflags mismatch (current:0x%08x rebuilt:0x%08x)\n",
12882 env_flags_current, env_flags_rebuilt);
12883 abort();
12884 }
12885#endif
12886}
12887
Richard Hendersona9e01312018-01-25 11:45:29 +000012888void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
Richard Hendersonb9adaa72018-01-25 11:45:29 +000012889 target_ulong *cs_base, uint32_t *pflags)
Richard Hendersona9e01312018-01-25 11:45:29 +000012890{
Richard Hendersone9799722019-10-23 11:00:57 -040012891 uint32_t flags = env->hflags;
12892 uint32_t pstate_for_ss;
Richard Hendersonb9adaa72018-01-25 11:45:29 +000012893
Richard Henderson9b253fe2019-10-23 11:00:47 -040012894 *cs_base = 0;
Philippe Mathieu-Daudé0ee8b242019-12-20 14:03:00 +000012895 assert_hflags_rebuild_correctly(env);
Richard Henderson3d74e2e2019-10-23 11:00:45 -040012896
Richard Hendersone9799722019-10-23 11:00:57 -040012897 if (FIELD_EX32(flags, TBFLAG_ANY, AARCH64_STATE)) {
Richard Hendersona9e01312018-01-25 11:45:29 +000012898 *pc = env->pc;
Richard Hendersond4d75032019-10-23 11:00:35 -040012899 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
Richard Henderson08f14342019-02-05 16:52:36 +000012900 flags = FIELD_DP32(flags, TBFLAG_A64, BTYPE, env->btype);
12901 }
Richard Henderson60e12c32019-10-23 11:00:43 -040012902 pstate_for_ss = env->pstate;
Richard Hendersona9e01312018-01-25 11:45:29 +000012903 } else {
12904 *pc = env->regs[15];
Richard Henderson6e33ced2019-10-23 11:00:38 -040012905
12906 if (arm_feature(env, ARM_FEATURE_M)) {
Richard Henderson9550d1b2019-10-23 11:00:39 -040012907 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12908 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12909 != env->v7m.secure) {
Richard Henderson79cabf12020-02-07 14:04:23 +000012910 flags = FIELD_DP32(flags, TBFLAG_M32, FPCCR_S_WRONG, 1);
Richard Henderson9550d1b2019-10-23 11:00:39 -040012911 }
12912
12913 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12914 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12915 (env->v7m.secure &&
12916 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12917 /*
12918 * ASPEN is set, but FPCA/SFPA indicate that there is no
12919 * active FP context; we must create a new FP context before
12920 * executing any FP insn.
12921 */
Richard Henderson79cabf12020-02-07 14:04:23 +000012922 flags = FIELD_DP32(flags, TBFLAG_M32, NEW_FP_CTXT_NEEDED, 1);
Richard Henderson9550d1b2019-10-23 11:00:39 -040012923 }
12924
12925 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12926 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
Richard Henderson79cabf12020-02-07 14:04:23 +000012927 flags = FIELD_DP32(flags, TBFLAG_M32, LSPACT, 1);
Richard Henderson9550d1b2019-10-23 11:00:39 -040012928 }
Richard Henderson6e33ced2019-10-23 11:00:38 -040012929 } else {
Richard Hendersonbbad7c62019-10-23 11:00:42 -040012930 /*
12931 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12932 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12933 */
12934 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12935 flags = FIELD_DP32(flags, TBFLAG_A32,
12936 XSCALE_CPAR, env->cp15.c15_cpar);
12937 } else {
12938 flags = FIELD_DP32(flags, TBFLAG_A32, VECLEN,
12939 env->vfp.vec_len);
12940 flags = FIELD_DP32(flags, TBFLAG_A32, VECSTRIDE,
12941 env->vfp.vec_stride);
12942 }
Richard Henderson0a54d682019-10-23 11:00:44 -040012943 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12944 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1);
12945 }
Richard Henderson6e33ced2019-10-23 11:00:38 -040012946 }
12947
Richard Henderson79cabf12020-02-07 14:04:23 +000012948 flags = FIELD_DP32(flags, TBFLAG_AM32, THUMB, env->thumb);
12949 flags = FIELD_DP32(flags, TBFLAG_AM32, CONDEXEC, env->condexec_bits);
Richard Henderson60e12c32019-10-23 11:00:43 -040012950 pstate_for_ss = env->uncached_cpsr;
Richard Hendersond4d75032019-10-23 11:00:35 -040012951 }
Richard Hendersona9e01312018-01-25 11:45:29 +000012952
Richard Henderson60e12c32019-10-23 11:00:43 -040012953 /*
12954 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
Richard Hendersona9e01312018-01-25 11:45:29 +000012955 * states defined in the ARM ARM for software singlestep:
12956 * SS_ACTIVE PSTATE.SS State
12957 * 0 x Inactive (the TB flag for SS is always 0)
12958 * 1 0 Active-pending
12959 * 1 1 Active-not-pending
Richard Hendersonfdd1b222019-10-23 11:00:34 -040012960 * SS_ACTIVE is set in hflags; PSTATE_SS is computed every TB.
Richard Hendersona9e01312018-01-25 11:45:29 +000012961 */
Richard Henderson60e12c32019-10-23 11:00:43 -040012962 if (FIELD_EX32(flags, TBFLAG_ANY, SS_ACTIVE) &&
12963 (pstate_for_ss & PSTATE_SS)) {
12964 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1);
Richard Hendersona9e01312018-01-25 11:45:29 +000012965 }
Richard Hendersona9e01312018-01-25 11:45:29 +000012966
Richard Hendersonb9adaa72018-01-25 11:45:29 +000012967 *pflags = flags;
Richard Hendersona9e01312018-01-25 11:45:29 +000012968}
Richard Henderson0ab59532018-10-08 14:55:02 +010012969
12970#ifdef TARGET_AARCH64
12971/*
12972 * The manual says that when SVE is enabled and VQ is widened the
12973 * implementation is allowed to zero the previously inaccessible
12974 * portion of the registers. The corollary to that is that when
12975 * SVE is enabled and VQ is narrowed we are also allowed to zero
12976 * the now inaccessible portion of the registers.
12977 *
12978 * The intent of this is that no predicate bit beyond VQ is ever set.
12979 * Which means that some operations on predicate registers themselves
12980 * may operate on full uint64_t or even unrolled across the maximum
12981 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
12982 * may well be cheaper than conditionals to restrict the operation
12983 * to the relevant portion of a uint16_t[16].
12984 */
12985void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12986{
12987 int i, j;
12988 uint64_t pmask;
12989
12990 assert(vq >= 1 && vq <= ARM_MAX_VQ);
Richard Henderson2fc0cc02019-03-22 17:41:14 -070012991 assert(vq <= env_archcpu(env)->sve_max_vq);
Richard Henderson0ab59532018-10-08 14:55:02 +010012992
12993 /* Zap the high bits of the zregs. */
12994 for (i = 0; i < 32; i++) {
12995 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12996 }
12997
12998 /* Zap the high bits of the pregs and ffr. */
12999 pmask = 0;
13000 if (vq & 3) {
13001 pmask = ~(-1ULL << (16 * (vq & 3)));
13002 }
13003 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
13004 for (i = 0; i < 17; ++i) {
13005 env->vfp.pregs[i].p[j] &= pmask;
13006 }
13007 pmask = 0;
13008 }
13009}
13010
13011/*
13012 * Notice a change in SVE vector size when changing EL.
13013 */
Richard Henderson9a05f7b2018-10-08 14:21:56 -070013014void aarch64_sve_change_el(CPUARMState *env, int old_el,
13015 int new_el, bool el0_a64)
Richard Henderson0ab59532018-10-08 14:55:02 +010013016{
Richard Henderson2fc0cc02019-03-22 17:41:14 -070013017 ARMCPU *cpu = env_archcpu(env);
Richard Henderson0ab59532018-10-08 14:55:02 +010013018 int old_len, new_len;
Richard Henderson9a05f7b2018-10-08 14:21:56 -070013019 bool old_a64, new_a64;
Richard Henderson0ab59532018-10-08 14:55:02 +010013020
13021 /* Nothing to do if no SVE. */
Richard Hendersoncd208a12018-10-24 07:50:17 +010013022 if (!cpu_isar_feature(aa64_sve, cpu)) {
Richard Henderson0ab59532018-10-08 14:55:02 +010013023 return;
13024 }
13025
13026 /* Nothing to do if FP is disabled in either EL. */
13027 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
13028 return;
13029 }
13030
13031 /*
13032 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
13033 * at ELx, or not available because the EL is in AArch32 state, then
13034 * for all purposes other than a direct read, the ZCR_ELx.LEN field
13035 * has an effective value of 0".
13036 *
13037 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
13038 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
13039 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
13040 * we already have the correct register contents when encountering the
13041 * vq0->vq0 transition between EL0->EL1.
13042 */
Richard Henderson9a05f7b2018-10-08 14:21:56 -070013043 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
13044 old_len = (old_a64 && !sve_exception_el(env, old_el)
Richard Henderson0ab59532018-10-08 14:55:02 +010013045 ? sve_zcr_len_for_el(env, old_el) : 0);
Richard Henderson9a05f7b2018-10-08 14:21:56 -070013046 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
13047 new_len = (new_a64 && !sve_exception_el(env, new_el)
Richard Henderson0ab59532018-10-08 14:55:02 +010013048 ? sve_zcr_len_for_el(env, new_el) : 0);
13049
13050 /* When changing vector length, clear inaccessible state. */
13051 if (new_len < old_len) {
13052 aarch64_sve_narrow_vq(env, new_len + 1);
13053 }
13054}
13055#endif