]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/helper.c
target/arm: Merge zcr reginfo
[mirror_qemu.git] / target / arm / helper.c
CommitLineData
ed3baad1
PMD
1/*
2 * ARM generic helpers.
3 *
4 * This code is licensed under the GNU GPL v2 or later.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
db725815 8
74c21bd0 9#include "qemu/osdep.h"
63159601 10#include "qemu/units.h"
cd617484 11#include "qemu/log.h"
181962fd 12#include "target/arm/idau.h"
194cbc49 13#include "trace.h"
b5ff1b31 14#include "cpu.h"
ccd38087 15#include "internals.h"
2ef6175a 16#include "exec/helper-proto.h"
1de7afc9 17#include "qemu/host-utils.h"
db725815 18#include "qemu/main-loop.h"
b8012ecf 19#include "qemu/timer.h"
1de7afc9 20#include "qemu/bitops.h"
eb0ecd5a 21#include "qemu/crc32c.h"
0442428a 22#include "qemu/qemu-print.h"
63c91552 23#include "exec/exec-all.h"
eb0ecd5a 24#include <zlib.h> /* For crc32 */
64552b6b 25#include "hw/irq.h"
6b5fe137 26#include "semihosting/semihost.h"
b2e23725 27#include "sysemu/cpus.h"
740b1759 28#include "sysemu/cpu-timers.h"
f3a9b694 29#include "sysemu/kvm.h"
9d2b5a58 30#include "qemu/range.h"
7f7b4e7a 31#include "qapi/qapi-commands-machine-target.h"
de390645
RH
32#include "qapi/error.h"
33#include "qemu/guest-random.h"
91f78c58
PMD
34#ifdef CONFIG_TCG
35#include "arm_ldst.h"
7aab5a8c 36#include "exec/cpu_ldst.h"
6b5fe137 37#include "semihosting/common-semi.h"
91f78c58 38#endif
cf7c6d10 39#include "cpregs.h"
0b03bdfc 40
352c98e5 41#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
21c2dd77 42#define PMCR_NUM_COUNTERS 4 /* QEMU IMPDEF choice */
352c98e5 43
4a501606 44#ifndef CONFIG_USER_ONLY
7c2cb42b 45
98e87797 46static bool get_phys_addr_lpae(CPUARMState *env, uint64_t address,
03ae85f8 47 MMUAccessType access_type, ARMMMUIdx mmu_idx,
ff7de2fc 48 bool s1_is_el0,
37785977 49 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 50 target_ulong *page_size_ptr,
7e98e21c
RH
51 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
52 __attribute__((nonnull));
4a501606
PM
53#endif
54
affdb64d 55static void switch_mode(CPUARMState *env, int mode);
ea04dce7 56static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx);
affdb64d 57
c4241c7d 58static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 59{
375421cc 60 assert(ri->fieldoffset);
67ed771d 61 if (cpreg_field_is_64bit(ri)) {
c4241c7d 62 return CPREG_FIELD64(env, ri);
22d9e1a9 63 } else {
c4241c7d 64 return CPREG_FIELD32(env, ri);
22d9e1a9 65 }
d4e6df63
PM
66}
67
c4241c7d
PM
68static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
69 uint64_t value)
d4e6df63 70{
375421cc 71 assert(ri->fieldoffset);
67ed771d 72 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
73 CPREG_FIELD64(env, ri) = value;
74 } else {
75 CPREG_FIELD32(env, ri) = value;
76 }
d4e6df63
PM
77}
78
11f136ee
FA
79static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
80{
81 return (char *)env + ri->fieldoffset;
82}
83
49a66191 84uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 85{
59a1c327 86 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 87 if (ri->type & ARM_CP_CONST) {
59a1c327 88 return ri->resetvalue;
721fae12 89 } else if (ri->raw_readfn) {
59a1c327 90 return ri->raw_readfn(env, ri);
721fae12 91 } else if (ri->readfn) {
59a1c327 92 return ri->readfn(env, ri);
721fae12 93 } else {
59a1c327 94 return raw_read(env, ri);
721fae12 95 }
721fae12
PM
96}
97
59a1c327 98static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 99 uint64_t v)
721fae12
PM
100{
101 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
102 * Note that constant registers are treated as write-ignored; the
103 * caller should check for success by whether a readback gives the
104 * value written.
105 */
106 if (ri->type & ARM_CP_CONST) {
59a1c327 107 return;
721fae12 108 } else if (ri->raw_writefn) {
c4241c7d 109 ri->raw_writefn(env, ri, v);
721fae12 110 } else if (ri->writefn) {
c4241c7d 111 ri->writefn(env, ri, v);
721fae12 112 } else {
afb2530f 113 raw_write(env, ri, v);
721fae12 114 }
721fae12
PM
115}
116
375421cc
PM
117static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
118{
119 /* Return true if the regdef would cause an assertion if you called
120 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
121 * program bug for it not to have the NO_RAW flag).
122 * NB that returning false here doesn't necessarily mean that calling
123 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
124 * read/write access functions which are safe for raw use" from "has
125 * read/write access functions which have side effects but has forgotten
126 * to provide raw access functions".
127 * The tests here line up with the conditions in read/write_raw_cp_reg()
128 * and assertions in raw_read()/raw_write().
129 */
130 if ((ri->type & ARM_CP_CONST) ||
131 ri->fieldoffset ||
132 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
133 return false;
134 }
135 return true;
136}
137
b698e4ee 138bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
721fae12
PM
139{
140 /* Write the coprocessor state from cpu->env to the (index,value) list. */
141 int i;
142 bool ok = true;
143
144 for (i = 0; i < cpu->cpreg_array_len; i++) {
145 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
146 const ARMCPRegInfo *ri;
b698e4ee 147 uint64_t newval;
59a1c327 148
60322b39 149 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
150 if (!ri) {
151 ok = false;
152 continue;
153 }
7a0e58fa 154 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
155 continue;
156 }
b698e4ee
PM
157
158 newval = read_raw_cp_reg(&cpu->env, ri);
159 if (kvm_sync) {
160 /*
161 * Only sync if the previous list->cpustate sync succeeded.
162 * Rather than tracking the success/failure state for every
163 * item in the list, we just recheck "does the raw write we must
164 * have made in write_list_to_cpustate() read back OK" here.
165 */
166 uint64_t oldval = cpu->cpreg_values[i];
167
168 if (oldval == newval) {
169 continue;
170 }
171
172 write_raw_cp_reg(&cpu->env, ri, oldval);
173 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
174 continue;
175 }
176
177 write_raw_cp_reg(&cpu->env, ri, newval);
178 }
179 cpu->cpreg_values[i] = newval;
721fae12
PM
180 }
181 return ok;
182}
183
184bool write_list_to_cpustate(ARMCPU *cpu)
185{
186 int i;
187 bool ok = true;
188
189 for (i = 0; i < cpu->cpreg_array_len; i++) {
190 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
191 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
192 const ARMCPRegInfo *ri;
193
60322b39 194 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
195 if (!ri) {
196 ok = false;
197 continue;
198 }
7a0e58fa 199 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
200 continue;
201 }
202 /* Write value and confirm it reads back as written
203 * (to catch read-only registers and partially read-only
204 * registers where the incoming migration value doesn't match)
205 */
59a1c327
PM
206 write_raw_cp_reg(&cpu->env, ri, v);
207 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
208 ok = false;
209 }
210 }
211 return ok;
212}
213
214static void add_cpreg_to_list(gpointer key, gpointer opaque)
215{
216 ARMCPU *cpu = opaque;
5860362d
RH
217 uint32_t regidx = (uintptr_t)key;
218 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 219
7a0e58fa 220 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
221 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
222 /* The value array need not be initialized at this point */
223 cpu->cpreg_array_len++;
224 }
225}
226
227static void count_cpreg(gpointer key, gpointer opaque)
228{
229 ARMCPU *cpu = opaque;
721fae12
PM
230 const ARMCPRegInfo *ri;
231
5860362d 232 ri = g_hash_table_lookup(cpu->cp_regs, key);
721fae12 233
7a0e58fa 234 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
235 cpu->cpreg_array_len++;
236 }
237}
238
239static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
240{
5860362d
RH
241 uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
242 uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
721fae12 243
cbf239b7
AR
244 if (aidx > bidx) {
245 return 1;
246 }
247 if (aidx < bidx) {
248 return -1;
249 }
250 return 0;
721fae12
PM
251}
252
253void init_cpreg_list(ARMCPU *cpu)
254{
255 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
256 * Note that we require cpreg_tuples[] to be sorted by key ID.
257 */
57b6d95e 258 GList *keys;
721fae12
PM
259 int arraylen;
260
57b6d95e 261 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
262 keys = g_list_sort(keys, cpreg_key_compare);
263
264 cpu->cpreg_array_len = 0;
265
266 g_list_foreach(keys, count_cpreg, cpu);
267
268 arraylen = cpu->cpreg_array_len;
269 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
270 cpu->cpreg_values = g_new(uint64_t, arraylen);
271 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
272 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
273 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
274 cpu->cpreg_array_len = 0;
275
276 g_list_foreach(keys, add_cpreg_to_list, cpu);
277
278 assert(cpu->cpreg_array_len == arraylen);
279
280 g_list_free(keys);
281}
282
68e9c2fe 283/*
93dd1e61 284 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
68e9c2fe
EI
285 */
286static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
287 const ARMCPRegInfo *ri,
288 bool isread)
68e9c2fe 289{
93dd1e61
EI
290 if (!is_a64(env) && arm_current_el(env) == 3 &&
291 arm_is_secure_below_el3(env)) {
68e9c2fe
EI
292 return CP_ACCESS_TRAP_UNCATEGORIZED;
293 }
294 return CP_ACCESS_OK;
295}
296
5513c3ab
PM
297/* Some secure-only AArch32 registers trap to EL3 if used from
298 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
299 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
300 * We assume that the .access field is set to PL1_RW.
301 */
302static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
303 const ARMCPRegInfo *ri,
304 bool isread)
5513c3ab
PM
305{
306 if (arm_current_el(env) == 3) {
307 return CP_ACCESS_OK;
308 }
309 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
310 if (env->cp15.scr_el3 & SCR_EEL2) {
311 return CP_ACCESS_TRAP_EL2;
312 }
5513c3ab
PM
313 return CP_ACCESS_TRAP_EL3;
314 }
315 /* This will be EL1 NS and EL2 NS, which just UNDEF */
316 return CP_ACCESS_TRAP_UNCATEGORIZED;
317}
318
59dd089c
RDC
319static uint64_t arm_mdcr_el2_eff(CPUARMState *env)
320{
321 return arm_is_el2_enabled(env) ? env->cp15.mdcr_el2 : 0;
322}
323
187f678d
PM
324/* Check for traps to "powerdown debug" registers, which are controlled
325 * by MDCR.TDOSA
326 */
327static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
328 bool isread)
329{
330 int el = arm_current_el(env);
59dd089c
RDC
331 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
332 bool mdcr_el2_tdosa = (mdcr_el2 & MDCR_TDOSA) || (mdcr_el2 & MDCR_TDE) ||
7c208e0f 333 (arm_hcr_el2_eff(env) & HCR_TGE);
187f678d 334
59dd089c 335 if (el < 2 && mdcr_el2_tdosa) {
187f678d
PM
336 return CP_ACCESS_TRAP_EL2;
337 }
338 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
339 return CP_ACCESS_TRAP_EL3;
340 }
341 return CP_ACCESS_OK;
342}
343
91b0a238
PM
344/* Check for traps to "debug ROM" registers, which are controlled
345 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
346 */
347static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
348 bool isread)
349{
350 int el = arm_current_el(env);
59dd089c
RDC
351 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
352 bool mdcr_el2_tdra = (mdcr_el2 & MDCR_TDRA) || (mdcr_el2 & MDCR_TDE) ||
7c208e0f 353 (arm_hcr_el2_eff(env) & HCR_TGE);
91b0a238 354
59dd089c 355 if (el < 2 && mdcr_el2_tdra) {
91b0a238
PM
356 return CP_ACCESS_TRAP_EL2;
357 }
358 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
359 return CP_ACCESS_TRAP_EL3;
360 }
361 return CP_ACCESS_OK;
362}
363
d6c8cf81
PM
364/* Check for traps to general debug registers, which are controlled
365 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
366 */
367static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
368 bool isread)
369{
370 int el = arm_current_el(env);
59dd089c
RDC
371 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
372 bool mdcr_el2_tda = (mdcr_el2 & MDCR_TDA) || (mdcr_el2 & MDCR_TDE) ||
7c208e0f 373 (arm_hcr_el2_eff(env) & HCR_TGE);
d6c8cf81 374
59dd089c 375 if (el < 2 && mdcr_el2_tda) {
d6c8cf81
PM
376 return CP_ACCESS_TRAP_EL2;
377 }
378 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
379 return CP_ACCESS_TRAP_EL3;
380 }
381 return CP_ACCESS_OK;
382}
383
1fce1ba9
PM
384/* Check for traps to performance monitor registers, which are controlled
385 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
386 */
387static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
388 bool isread)
389{
390 int el = arm_current_el(env);
59dd089c 391 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1fce1ba9 392
59dd089c 393 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1fce1ba9
PM
394 return CP_ACCESS_TRAP_EL2;
395 }
396 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
397 return CP_ACCESS_TRAP_EL3;
398 }
399 return CP_ACCESS_OK;
400}
401
84929218
RH
402/* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
403static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
404 bool isread)
405{
406 if (arm_current_el(env) == 1) {
407 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
408 if (arm_hcr_el2_eff(env) & trap) {
409 return CP_ACCESS_TRAP_EL2;
410 }
411 }
412 return CP_ACCESS_OK;
413}
414
1803d271
RH
415/* Check for traps from EL1 due to HCR_EL2.TSW. */
416static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
417 bool isread)
418{
419 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
420 return CP_ACCESS_TRAP_EL2;
421 }
422 return CP_ACCESS_OK;
423}
424
99602377
RH
425/* Check for traps from EL1 due to HCR_EL2.TACR. */
426static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
427 bool isread)
428{
429 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
430 return CP_ACCESS_TRAP_EL2;
431 }
432 return CP_ACCESS_OK;
433}
434
30881b73
RH
435/* Check for traps from EL1 due to HCR_EL2.TTLB. */
436static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
437 bool isread)
438{
439 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
440 return CP_ACCESS_TRAP_EL2;
441 }
442 return CP_ACCESS_OK;
443}
444
c4241c7d 445static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 446{
2fc0cc0e 447 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 448
8d5c773e 449 raw_write(env, ri, value);
d10eb08f 450 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
451}
452
c4241c7d 453static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 454{
2fc0cc0e 455 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 456
8d5c773e 457 if (raw_read(env, ri) != value) {
08de207b
PM
458 /* Unlike real hardware the qemu TLB uses virtual addresses,
459 * not modified virtual addresses, so this causes a TLB flush.
460 */
d10eb08f 461 tlb_flush(CPU(cpu));
8d5c773e 462 raw_write(env, ri, value);
08de207b 463 }
08de207b 464}
c4241c7d
PM
465
466static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
467 uint64_t value)
08de207b 468{
2fc0cc0e 469 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 470
452a0955 471 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 472 && !extended_addresses_enabled(env)) {
08de207b
PM
473 /* For VMSA (when not using the LPAE long descriptor page table
474 * format) this register includes the ASID, so do a TLB flush.
475 * For PMSA it is purely a process ID and no action is needed.
476 */
d10eb08f 477 tlb_flush(CPU(cpu));
08de207b 478 }
8d5c773e 479 raw_write(env, ri, value);
08de207b
PM
480}
481
b4ab8ce9
PM
482/* IS variants of TLB operations must affect all cores */
483static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
484 uint64_t value)
485{
29a0af61 486 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
487
488 tlb_flush_all_cpus_synced(cs);
489}
490
491static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
492 uint64_t value)
493{
29a0af61 494 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
495
496 tlb_flush_all_cpus_synced(cs);
497}
498
499static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
500 uint64_t value)
501{
29a0af61 502 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
503
504 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
505}
506
507static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
508 uint64_t value)
509{
29a0af61 510 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
511
512 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
513}
514
515/*
516 * Non-IS variants of TLB operations are upgraded to
373e7ffd 517 * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
b4ab8ce9
PM
518 * force broadcast of these operations.
519 */
520static bool tlb_force_broadcast(CPUARMState *env)
521{
373e7ffd 522 return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
b4ab8ce9
PM
523}
524
c4241c7d
PM
525static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
526 uint64_t value)
d929823f
PM
527{
528 /* Invalidate all (TLBIALL) */
527db2be 529 CPUState *cs = env_cpu(env);
00c8cb0a 530
b4ab8ce9 531 if (tlb_force_broadcast(env)) {
527db2be
RH
532 tlb_flush_all_cpus_synced(cs);
533 } else {
534 tlb_flush(cs);
b4ab8ce9 535 }
d929823f
PM
536}
537
c4241c7d
PM
538static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
539 uint64_t value)
d929823f
PM
540{
541 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
527db2be 542 CPUState *cs = env_cpu(env);
31b030d4 543
527db2be 544 value &= TARGET_PAGE_MASK;
b4ab8ce9 545 if (tlb_force_broadcast(env)) {
527db2be
RH
546 tlb_flush_page_all_cpus_synced(cs, value);
547 } else {
548 tlb_flush_page(cs, value);
b4ab8ce9 549 }
d929823f
PM
550}
551
c4241c7d
PM
552static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
553 uint64_t value)
d929823f
PM
554{
555 /* Invalidate by ASID (TLBIASID) */
527db2be 556 CPUState *cs = env_cpu(env);
00c8cb0a 557
b4ab8ce9 558 if (tlb_force_broadcast(env)) {
527db2be
RH
559 tlb_flush_all_cpus_synced(cs);
560 } else {
561 tlb_flush(cs);
b4ab8ce9 562 }
d929823f
PM
563}
564
c4241c7d
PM
565static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
566 uint64_t value)
d929823f
PM
567{
568 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
527db2be 569 CPUState *cs = env_cpu(env);
31b030d4 570
527db2be 571 value &= TARGET_PAGE_MASK;
b4ab8ce9 572 if (tlb_force_broadcast(env)) {
527db2be
RH
573 tlb_flush_page_all_cpus_synced(cs, value);
574 } else {
575 tlb_flush_page(cs, value);
b4ab8ce9 576 }
fa439fc5
PM
577}
578
541ef8c2
SS
579static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
580 uint64_t value)
581{
29a0af61 582 CPUState *cs = env_cpu(env);
541ef8c2 583
0336cbf8 584 tlb_flush_by_mmuidx(cs,
01b98b68 585 ARMMMUIdxBit_E10_1 |
452ef8cb 586 ARMMMUIdxBit_E10_1_PAN |
bf05340c 587 ARMMMUIdxBit_E10_0);
541ef8c2
SS
588}
589
590static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
591 uint64_t value)
592{
29a0af61 593 CPUState *cs = env_cpu(env);
541ef8c2 594
a67cf277 595 tlb_flush_by_mmuidx_all_cpus_synced(cs,
01b98b68 596 ARMMMUIdxBit_E10_1 |
452ef8cb 597 ARMMMUIdxBit_E10_1_PAN |
bf05340c 598 ARMMMUIdxBit_E10_0);
541ef8c2
SS
599}
600
541ef8c2
SS
601
602static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
603 uint64_t value)
604{
29a0af61 605 CPUState *cs = env_cpu(env);
541ef8c2 606
e013b741 607 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
608}
609
610static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
611 uint64_t value)
612{
29a0af61 613 CPUState *cs = env_cpu(env);
541ef8c2 614
e013b741 615 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
616}
617
618static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
619 uint64_t value)
620{
29a0af61 621 CPUState *cs = env_cpu(env);
541ef8c2
SS
622 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
623
e013b741 624 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
541ef8c2
SS
625}
626
627static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
628 uint64_t value)
629{
29a0af61 630 CPUState *cs = env_cpu(env);
541ef8c2
SS
631 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
632
a67cf277 633 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 634 ARMMMUIdxBit_E2);
541ef8c2
SS
635}
636
e9aa6c21 637static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
638 /* Define the secure and non-secure FCSE identifier CP registers
639 * separately because there is no secure bank in V8 (no _EL3). This allows
640 * the secure register to be properly reset and migrated. There is also no
641 * v8 EL1 version of the register so the non-secure instance stands alone.
642 */
9c513e78 643 { .name = "FCSEIDR",
54bf36ed
FA
644 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
645 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
646 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
647 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9c513e78 648 { .name = "FCSEIDR_S",
54bf36ed
FA
649 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
650 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
651 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 652 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
653 /* Define the secure and non-secure context identifier CP registers
654 * separately because there is no secure bank in V8 (no _EL3). This allows
655 * the secure register to be properly reset and migrated. In the
656 * non-secure case, the 32-bit register will have reset and migration
657 * disabled during registration as it is handled by the 64-bit instance.
658 */
659 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 660 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
661 .access = PL1_RW, .accessfn = access_tvm_trvm,
662 .secure = ARM_CP_SECSTATE_NS,
54bf36ed
FA
663 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
664 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9c513e78 665 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
54bf36ed 666 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
667 .access = PL1_RW, .accessfn = access_tvm_trvm,
668 .secure = ARM_CP_SECSTATE_S,
54bf36ed 669 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 670 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
671};
672
673static const ARMCPRegInfo not_v8_cp_reginfo[] = {
674 /* NB: Some of these registers exist in v8 but with more precise
675 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
676 */
677 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
678 { .name = "DACR",
679 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
84929218 680 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
681 .writefn = dacr_write, .raw_writefn = raw_write,
682 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
683 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
684 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
685 * For v6 and v5, these mappings are overly broad.
4fdd17dd 686 */
a903c449
EI
687 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
688 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
689 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
690 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
691 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
692 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
693 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 694 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
695 /* Cache maintenance ops; some of this space may be overridden later. */
696 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
697 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
698 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
699};
700
7d57f408
PM
701static const ARMCPRegInfo not_v6_cp_reginfo[] = {
702 /* Not all pre-v6 cores implemented this WFI, so this is slightly
703 * over-broad.
704 */
705 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
706 .access = PL1_W, .type = ARM_CP_WFI },
7d57f408
PM
707};
708
709static const ARMCPRegInfo not_v7_cp_reginfo[] = {
710 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
711 * is UNPREDICTABLE; we choose to NOP as most implementations do).
712 */
713 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
714 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
715 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
716 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
717 * OMAPCP will override this space.
718 */
719 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
720 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
721 .resetvalue = 0 },
722 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
723 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
724 .resetvalue = 0 },
776d4e5c
PM
725 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
726 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 727 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 728 .resetvalue = 0 },
50300698
PM
729 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
730 * implementing it as RAZ means the "debug architecture version" bits
731 * will read as a reserved value, which should cause Linux to not try
732 * to use the debug hardware.
733 */
734 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
735 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
736 /* MMU TLB control. Note that the wildcarding means we cover not just
737 * the unified TLB ops but also the dside/iside/inner-shareable variants.
738 */
739 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
740 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 741 .type = ARM_CP_NO_RAW },
995939a6
PM
742 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
743 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 744 .type = ARM_CP_NO_RAW },
995939a6
PM
745 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
746 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 747 .type = ARM_CP_NO_RAW },
995939a6
PM
748 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
749 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 750 .type = ARM_CP_NO_RAW },
a903c449
EI
751 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
752 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
753 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
754 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
755};
756
c4241c7d
PM
757static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
758 uint64_t value)
2771db27 759{
f0aff255
FA
760 uint32_t mask = 0;
761
762 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
763 if (!arm_feature(env, ARM_FEATURE_V8)) {
764 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
765 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
766 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
767 */
7fbc6a40 768 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
f0aff255
FA
769 /* VFP coprocessor: cp10 & cp11 [23:20] */
770 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
771
772 if (!arm_feature(env, ARM_FEATURE_NEON)) {
773 /* ASEDIS [31] bit is RAO/WI */
774 value |= (1 << 31);
775 }
776
777 /* VFPv3 and upwards with NEON implement 32 double precision
778 * registers (D0-D31).
779 */
a6627f5f 780 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
f0aff255
FA
781 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
782 value |= (1 << 30);
783 }
784 }
785 value &= mask;
2771db27 786 }
fc1120a7
PM
787
788 /*
789 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
790 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
791 */
792 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
793 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
794 value &= ~(0xf << 20);
795 value |= env->cp15.cpacr_el1 & (0xf << 20);
796 }
797
7ebd5f2e 798 env->cp15.cpacr_el1 = value;
2771db27
PM
799}
800
fc1120a7
PM
801static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
802{
803 /*
804 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
805 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
806 */
807 uint64_t value = env->cp15.cpacr_el1;
808
809 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
810 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
811 value &= ~(0xf << 20);
812 }
813 return value;
814}
815
816
5deac39c
PM
817static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
818{
819 /* Call cpacr_write() so that we reset with the correct RAO bits set
820 * for our CPU features.
821 */
822 cpacr_write(env, ri, 0);
823}
824
3f208fd7
PM
825static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
826 bool isread)
c6f19164
GB
827{
828 if (arm_feature(env, ARM_FEATURE_V8)) {
829 /* Check if CPACR accesses are to be trapped to EL2 */
e6ef0169
RDC
830 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
831 (env->cp15.cptr_el[2] & CPTR_TCPAC)) {
c6f19164
GB
832 return CP_ACCESS_TRAP_EL2;
833 /* Check if CPACR accesses are to be trapped to EL3 */
834 } else if (arm_current_el(env) < 3 &&
835 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
836 return CP_ACCESS_TRAP_EL3;
837 }
838 }
839
840 return CP_ACCESS_OK;
841}
842
3f208fd7
PM
843static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
844 bool isread)
c6f19164
GB
845{
846 /* Check if CPTR accesses are set to trap to EL3 */
847 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
848 return CP_ACCESS_TRAP_EL3;
849 }
850
851 return CP_ACCESS_OK;
852}
853
7d57f408
PM
854static const ARMCPRegInfo v6_cp_reginfo[] = {
855 /* prefetch by MVA in v6, NOP in v7 */
856 { .name = "MVA_prefetch",
857 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
858 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
859 /* We need to break the TB after ISB to execute self-modifying code
860 * correctly and also to take any pending interrupts immediately.
861 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
862 */
7d57f408 863 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 864 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 865 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 866 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 867 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 868 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 869 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218 870 .access = PL1_RW, .accessfn = access_tvm_trvm,
b848ce2b
FA
871 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
872 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
873 .resetvalue = 0, },
874 /* Watchpoint Fault Address Register : should actually only be present
875 * for 1136, 1176, 11MPCore.
876 */
877 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
878 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 879 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 880 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 881 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
fc1120a7 882 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
7d57f408
PM
883};
884
57a4a11b
AL
885typedef struct pm_event {
886 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
887 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
888 bool (*supported)(CPUARMState *);
889 /*
890 * Retrieve the current count of the underlying event. The programmed
891 * counters hold a difference from the return value from this function
892 */
893 uint64_t (*get_count)(CPUARMState *);
4e7beb0c
AL
894 /*
895 * Return how many nanoseconds it will take (at a minimum) for count events
896 * to occur. A negative value indicates the counter will never overflow, or
897 * that the counter has otherwise arranged for the overflow bit to be set
898 * and the PMU interrupt to be raised on overflow.
899 */
900 int64_t (*ns_per_count)(uint64_t);
57a4a11b
AL
901} pm_event;
902
b2e23725
AL
903static bool event_always_supported(CPUARMState *env)
904{
905 return true;
906}
907
0d4bfd7d
AL
908static uint64_t swinc_get_count(CPUARMState *env)
909{
910 /*
911 * SW_INCR events are written directly to the pmevcntr's by writes to
912 * PMSWINC, so there is no underlying count maintained by the PMU itself
913 */
914 return 0;
915}
916
4e7beb0c
AL
917static int64_t swinc_ns_per(uint64_t ignored)
918{
919 return -1;
920}
921
b2e23725
AL
922/*
923 * Return the underlying cycle count for the PMU cycle counters. If we're in
924 * usermode, simply return 0.
925 */
926static uint64_t cycles_get_count(CPUARMState *env)
927{
928#ifndef CONFIG_USER_ONLY
929 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
930 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
931#else
932 return cpu_get_host_ticks();
933#endif
934}
935
936#ifndef CONFIG_USER_ONLY
4e7beb0c
AL
937static int64_t cycles_ns_per(uint64_t cycles)
938{
939 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
940}
941
b2e23725
AL
942static bool instructions_supported(CPUARMState *env)
943{
740b1759 944 return icount_enabled() == 1; /* Precise instruction counting */
b2e23725
AL
945}
946
947static uint64_t instructions_get_count(CPUARMState *env)
948{
8191d368 949 return (uint64_t)icount_get_raw();
b2e23725 950}
4e7beb0c
AL
951
952static int64_t instructions_ns_per(uint64_t icount)
953{
8191d368 954 return icount_to_ns((int64_t)icount);
4e7beb0c 955}
b2e23725
AL
956#endif
957
0727f63b
PM
958static bool pmu_8_1_events_supported(CPUARMState *env)
959{
960 /* For events which are supported in any v8.1 PMU */
961 return cpu_isar_feature(any_pmu_8_1, env_archcpu(env));
962}
963
15dd1ebd
PM
964static bool pmu_8_4_events_supported(CPUARMState *env)
965{
966 /* For events which are supported in any v8.1 PMU */
967 return cpu_isar_feature(any_pmu_8_4, env_archcpu(env));
968}
969
0727f63b
PM
970static uint64_t zero_event_get_count(CPUARMState *env)
971{
972 /* For events which on QEMU never fire, so their count is always zero */
973 return 0;
974}
975
976static int64_t zero_event_ns_per(uint64_t cycles)
977{
978 /* An event which never fires can never overflow */
979 return -1;
980}
981
57a4a11b 982static const pm_event pm_events[] = {
0d4bfd7d
AL
983 { .number = 0x000, /* SW_INCR */
984 .supported = event_always_supported,
985 .get_count = swinc_get_count,
4e7beb0c 986 .ns_per_count = swinc_ns_per,
0d4bfd7d 987 },
b2e23725
AL
988#ifndef CONFIG_USER_ONLY
989 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
990 .supported = instructions_supported,
991 .get_count = instructions_get_count,
4e7beb0c 992 .ns_per_count = instructions_ns_per,
b2e23725
AL
993 },
994 { .number = 0x011, /* CPU_CYCLES, Cycle */
995 .supported = event_always_supported,
996 .get_count = cycles_get_count,
4e7beb0c 997 .ns_per_count = cycles_ns_per,
0727f63b 998 },
b2e23725 999#endif
0727f63b
PM
1000 { .number = 0x023, /* STALL_FRONTEND */
1001 .supported = pmu_8_1_events_supported,
1002 .get_count = zero_event_get_count,
1003 .ns_per_count = zero_event_ns_per,
1004 },
1005 { .number = 0x024, /* STALL_BACKEND */
1006 .supported = pmu_8_1_events_supported,
1007 .get_count = zero_event_get_count,
1008 .ns_per_count = zero_event_ns_per,
1009 },
15dd1ebd
PM
1010 { .number = 0x03c, /* STALL */
1011 .supported = pmu_8_4_events_supported,
1012 .get_count = zero_event_get_count,
1013 .ns_per_count = zero_event_ns_per,
1014 },
57a4a11b
AL
1015};
1016
1017/*
1018 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1019 * events (i.e. the statistical profiling extension), this implementation
1020 * should first be updated to something sparse instead of the current
1021 * supported_event_map[] array.
1022 */
15dd1ebd 1023#define MAX_EVENT_ID 0x3c
57a4a11b
AL
1024#define UNSUPPORTED_EVENT UINT16_MAX
1025static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1026
1027/*
bf8d0969
AL
1028 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1029 * of ARM event numbers to indices in our pm_events array.
57a4a11b
AL
1030 *
1031 * Note: Events in the 0x40XX range are not currently supported.
1032 */
bf8d0969 1033void pmu_init(ARMCPU *cpu)
57a4a11b 1034{
57a4a11b
AL
1035 unsigned int i;
1036
bf8d0969
AL
1037 /*
1038 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1039 * events to them
1040 */
57a4a11b
AL
1041 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1042 supported_event_map[i] = UNSUPPORTED_EVENT;
1043 }
bf8d0969
AL
1044 cpu->pmceid0 = 0;
1045 cpu->pmceid1 = 0;
57a4a11b
AL
1046
1047 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1048 const pm_event *cnt = &pm_events[i];
1049 assert(cnt->number <= MAX_EVENT_ID);
1050 /* We do not currently support events in the 0x40xx range */
1051 assert(cnt->number <= 0x3f);
1052
bf8d0969 1053 if (cnt->supported(&cpu->env)) {
57a4a11b 1054 supported_event_map[cnt->number] = i;
67da43d6 1055 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
bf8d0969
AL
1056 if (cnt->number & 0x20) {
1057 cpu->pmceid1 |= event_mask;
1058 } else {
1059 cpu->pmceid0 |= event_mask;
1060 }
57a4a11b
AL
1061 }
1062 }
57a4a11b
AL
1063}
1064
5ecdd3e4
AL
1065/*
1066 * Check at runtime whether a PMU event is supported for the current machine
1067 */
1068static bool event_supported(uint16_t number)
1069{
1070 if (number > MAX_EVENT_ID) {
1071 return false;
1072 }
1073 return supported_event_map[number] != UNSUPPORTED_EVENT;
1074}
1075
3f208fd7
PM
1076static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1077 bool isread)
200ac0ef 1078{
3b163b01 1079 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
1080 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1081 * trapping to EL2 or EL3 for other accesses.
200ac0ef 1082 */
1fce1ba9 1083 int el = arm_current_el(env);
59dd089c 1084 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1fce1ba9 1085
6ecd0b6b 1086 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 1087 return CP_ACCESS_TRAP;
200ac0ef 1088 }
59dd089c 1089 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1fce1ba9
PM
1090 return CP_ACCESS_TRAP_EL2;
1091 }
1092 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1093 return CP_ACCESS_TRAP_EL3;
1094 }
1095
fcd25206 1096 return CP_ACCESS_OK;
200ac0ef
PM
1097}
1098
6ecd0b6b
AB
1099static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1100 const ARMCPRegInfo *ri,
1101 bool isread)
1102{
1103 /* ER: event counter read trap control */
1104 if (arm_feature(env, ARM_FEATURE_V8)
1105 && arm_current_el(env) == 0
1106 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1107 && isread) {
1108 return CP_ACCESS_OK;
1109 }
1110
1111 return pmreg_access(env, ri, isread);
1112}
1113
1114static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1115 const ARMCPRegInfo *ri,
1116 bool isread)
1117{
1118 /* SW: software increment write trap control */
1119 if (arm_feature(env, ARM_FEATURE_V8)
1120 && arm_current_el(env) == 0
1121 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1122 && !isread) {
1123 return CP_ACCESS_OK;
1124 }
1125
1126 return pmreg_access(env, ri, isread);
1127}
1128
6ecd0b6b
AB
1129static CPAccessResult pmreg_access_selr(CPUARMState *env,
1130 const ARMCPRegInfo *ri,
1131 bool isread)
1132{
1133 /* ER: event counter read trap control */
1134 if (arm_feature(env, ARM_FEATURE_V8)
1135 && arm_current_el(env) == 0
1136 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1137 return CP_ACCESS_OK;
1138 }
1139
1140 return pmreg_access(env, ri, isread);
1141}
1142
1143static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1144 const ARMCPRegInfo *ri,
1145 bool isread)
1146{
1147 /* CR: cycle counter read trap control */
1148 if (arm_feature(env, ARM_FEATURE_V8)
1149 && arm_current_el(env) == 0
1150 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1151 && isread) {
1152 return CP_ACCESS_OK;
1153 }
1154
1155 return pmreg_access(env, ri, isread);
1156}
1157
033614c4
AL
1158/* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1159 * the current EL, security state, and register configuration.
1160 */
1161static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
87124fde 1162{
033614c4
AL
1163 uint64_t filter;
1164 bool e, p, u, nsk, nsu, nsh, m;
1165 bool enabled, prohibited, filtered;
1166 bool secure = arm_is_secure(env);
1167 int el = arm_current_el(env);
59dd089c
RDC
1168 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1169 uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
87124fde 1170
cbbb3041
AJ
1171 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1172 return false;
1173 }
1174
033614c4
AL
1175 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1176 (counter < hpmn || counter == 31)) {
1177 e = env->cp15.c9_pmcr & PMCRE;
1178 } else {
59dd089c 1179 e = mdcr_el2 & MDCR_HPME;
87124fde 1180 }
033614c4 1181 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
87124fde 1182
033614c4
AL
1183 if (!secure) {
1184 if (el == 2 && (counter < hpmn || counter == 31)) {
59dd089c 1185 prohibited = mdcr_el2 & MDCR_HPMD;
033614c4
AL
1186 } else {
1187 prohibited = false;
1188 }
1189 } else {
1190 prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
db1f3afb 1191 !(env->cp15.mdcr_el3 & MDCR_SPME);
033614c4
AL
1192 }
1193
1194 if (prohibited && counter == 31) {
1195 prohibited = env->cp15.c9_pmcr & PMCRDP;
1196 }
1197
5ecdd3e4
AL
1198 if (counter == 31) {
1199 filter = env->cp15.pmccfiltr_el0;
1200 } else {
1201 filter = env->cp15.c14_pmevtyper[counter];
1202 }
033614c4
AL
1203
1204 p = filter & PMXEVTYPER_P;
1205 u = filter & PMXEVTYPER_U;
1206 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1207 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1208 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1209 m = arm_el_is_aa64(env, 1) &&
1210 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1211
1212 if (el == 0) {
1213 filtered = secure ? u : u != nsu;
1214 } else if (el == 1) {
1215 filtered = secure ? p : p != nsk;
1216 } else if (el == 2) {
1217 filtered = !nsh;
1218 } else { /* EL3 */
1219 filtered = m != p;
1220 }
1221
5ecdd3e4
AL
1222 if (counter != 31) {
1223 /*
1224 * If not checking PMCCNTR, ensure the counter is setup to an event we
1225 * support
1226 */
1227 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1228 if (!event_supported(event)) {
1229 return false;
1230 }
1231 }
1232
033614c4 1233 return enabled && !prohibited && !filtered;
87124fde 1234}
033614c4 1235
f4efb4b2
AL
1236static void pmu_update_irq(CPUARMState *env)
1237{
2fc0cc0e 1238 ARMCPU *cpu = env_archcpu(env);
f4efb4b2
AL
1239 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1240 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1241}
1242
5d05b9d4
AL
1243/*
1244 * Ensure c15_ccnt is the guest-visible count so that operations such as
1245 * enabling/disabling the counter or filtering, modifying the count itself,
1246 * etc. can be done logically. This is essentially a no-op if the counter is
1247 * not enabled at the time of the call.
1248 */
f2b2f53f 1249static void pmccntr_op_start(CPUARMState *env)
ec7b4ce4 1250{
b2e23725 1251 uint64_t cycles = cycles_get_count(env);
ec7b4ce4 1252
033614c4 1253 if (pmu_counter_enabled(env, 31)) {
5d05b9d4
AL
1254 uint64_t eff_cycles = cycles;
1255 if (env->cp15.c9_pmcr & PMCRD) {
1256 /* Increment once every 64 processor clock cycles */
1257 eff_cycles /= 64;
1258 }
1259
f4efb4b2
AL
1260 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1261
1262 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1263 1ull << 63 : 1ull << 31;
1264 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1265 env->cp15.c9_pmovsr |= (1 << 31);
1266 pmu_update_irq(env);
1267 }
1268
1269 env->cp15.c15_ccnt = new_pmccntr;
ec7b4ce4 1270 }
5d05b9d4
AL
1271 env->cp15.c15_ccnt_delta = cycles;
1272}
ec7b4ce4 1273
5d05b9d4
AL
1274/*
1275 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1276 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1277 * pmccntr_op_start.
1278 */
f2b2f53f 1279static void pmccntr_op_finish(CPUARMState *env)
5d05b9d4 1280{
033614c4 1281 if (pmu_counter_enabled(env, 31)) {
4e7beb0c
AL
1282#ifndef CONFIG_USER_ONLY
1283 /* Calculate when the counter will next overflow */
1284 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1285 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1286 remaining_cycles = (uint32_t)remaining_cycles;
1287 }
1288 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1289
1290 if (overflow_in > 0) {
1291 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1292 overflow_in;
2fc0cc0e 1293 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1294 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1295 }
1296#endif
5d05b9d4 1297
4e7beb0c 1298 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
5d05b9d4
AL
1299 if (env->cp15.c9_pmcr & PMCRD) {
1300 /* Increment once every 64 processor clock cycles */
1301 prev_cycles /= 64;
1302 }
5d05b9d4 1303 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
ec7b4ce4
AF
1304 }
1305}
1306
5ecdd3e4
AL
1307static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1308{
1309
1310 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1311 uint64_t count = 0;
1312 if (event_supported(event)) {
1313 uint16_t event_idx = supported_event_map[event];
1314 count = pm_events[event_idx].get_count(env);
1315 }
1316
1317 if (pmu_counter_enabled(env, counter)) {
f4efb4b2
AL
1318 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1319
1320 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1321 env->cp15.c9_pmovsr |= (1 << counter);
1322 pmu_update_irq(env);
1323 }
1324 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
5ecdd3e4
AL
1325 }
1326 env->cp15.c14_pmevcntr_delta[counter] = count;
1327}
1328
1329static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1330{
1331 if (pmu_counter_enabled(env, counter)) {
4e7beb0c
AL
1332#ifndef CONFIG_USER_ONLY
1333 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1334 uint16_t event_idx = supported_event_map[event];
1335 uint64_t delta = UINT32_MAX -
1336 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1337 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1338
1339 if (overflow_in > 0) {
1340 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1341 overflow_in;
2fc0cc0e 1342 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1343 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1344 }
1345#endif
1346
5ecdd3e4
AL
1347 env->cp15.c14_pmevcntr_delta[counter] -=
1348 env->cp15.c14_pmevcntr[counter];
1349 }
1350}
1351
5d05b9d4
AL
1352void pmu_op_start(CPUARMState *env)
1353{
5ecdd3e4 1354 unsigned int i;
5d05b9d4 1355 pmccntr_op_start(env);
5ecdd3e4
AL
1356 for (i = 0; i < pmu_num_counters(env); i++) {
1357 pmevcntr_op_start(env, i);
1358 }
5d05b9d4
AL
1359}
1360
1361void pmu_op_finish(CPUARMState *env)
1362{
5ecdd3e4 1363 unsigned int i;
5d05b9d4 1364 pmccntr_op_finish(env);
5ecdd3e4
AL
1365 for (i = 0; i < pmu_num_counters(env); i++) {
1366 pmevcntr_op_finish(env, i);
1367 }
5d05b9d4
AL
1368}
1369
033614c4
AL
1370void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1371{
1372 pmu_op_start(&cpu->env);
1373}
1374
1375void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1376{
1377 pmu_op_finish(&cpu->env);
1378}
1379
4e7beb0c
AL
1380void arm_pmu_timer_cb(void *opaque)
1381{
1382 ARMCPU *cpu = opaque;
1383
1384 /*
1385 * Update all the counter values based on the current underlying counts,
1386 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1387 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1388 * counter may expire.
1389 */
1390 pmu_op_start(&cpu->env);
1391 pmu_op_finish(&cpu->env);
1392}
1393
c4241c7d
PM
1394static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1395 uint64_t value)
200ac0ef 1396{
5d05b9d4 1397 pmu_op_start(env);
7c2cb42b
AF
1398
1399 if (value & PMCRC) {
1400 /* The counter has been reset */
1401 env->cp15.c15_ccnt = 0;
1402 }
1403
5ecdd3e4
AL
1404 if (value & PMCRP) {
1405 unsigned int i;
1406 for (i = 0; i < pmu_num_counters(env); i++) {
1407 env->cp15.c14_pmevcntr[i] = 0;
1408 }
1409 }
1410
62d96ff4
PM
1411 env->cp15.c9_pmcr &= ~PMCR_WRITEABLE_MASK;
1412 env->cp15.c9_pmcr |= (value & PMCR_WRITEABLE_MASK);
7c2cb42b 1413
5d05b9d4 1414 pmu_op_finish(env);
7c2cb42b
AF
1415}
1416
0d4bfd7d
AL
1417static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1418 uint64_t value)
1419{
1420 unsigned int i;
1421 for (i = 0; i < pmu_num_counters(env); i++) {
1422 /* Increment a counter's count iff: */
1423 if ((value & (1 << i)) && /* counter's bit is set */
1424 /* counter is enabled and not filtered */
1425 pmu_counter_enabled(env, i) &&
1426 /* counter is SW_INCR */
1427 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1428 pmevcntr_op_start(env, i);
f4efb4b2
AL
1429
1430 /*
1431 * Detect if this write causes an overflow since we can't predict
1432 * PMSWINC overflows like we can for other events
1433 */
1434 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1435
1436 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1437 env->cp15.c9_pmovsr |= (1 << i);
1438 pmu_update_irq(env);
1439 }
1440
1441 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1442
0d4bfd7d
AL
1443 pmevcntr_op_finish(env, i);
1444 }
1445 }
1446}
1447
7c2cb42b
AF
1448static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1449{
5d05b9d4
AL
1450 uint64_t ret;
1451 pmccntr_op_start(env);
1452 ret = env->cp15.c15_ccnt;
1453 pmccntr_op_finish(env);
1454 return ret;
7c2cb42b
AF
1455}
1456
6b040780
WH
1457static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1458 uint64_t value)
1459{
1460 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1461 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1462 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1463 * accessed.
1464 */
1465 env->cp15.c9_pmselr = value & 0x1f;
1466}
1467
7c2cb42b
AF
1468static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1469 uint64_t value)
1470{
5d05b9d4
AL
1471 pmccntr_op_start(env);
1472 env->cp15.c15_ccnt = value;
1473 pmccntr_op_finish(env);
200ac0ef 1474}
421c7ebd
PC
1475
1476static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1477 uint64_t value)
1478{
1479 uint64_t cur_val = pmccntr_read(env, NULL);
1480
1481 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1482}
1483
0614601c
AF
1484static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1485 uint64_t value)
1486{
5d05b9d4 1487 pmccntr_op_start(env);
4b8afa1f
AL
1488 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1489 pmccntr_op_finish(env);
1490}
1491
1492static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1493 uint64_t value)
1494{
1495 pmccntr_op_start(env);
1496 /* M is not accessible from AArch32 */
1497 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1498 (value & PMCCFILTR);
5d05b9d4 1499 pmccntr_op_finish(env);
0614601c
AF
1500}
1501
4b8afa1f
AL
1502static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1503{
1504 /* M is not visible in AArch32 */
1505 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1506}
1507
c4241c7d 1508static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1509 uint64_t value)
1510{
7ece99b1 1511 value &= pmu_counter_mask(env);
200ac0ef 1512 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
1513}
1514
c4241c7d
PM
1515static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1516 uint64_t value)
200ac0ef 1517{
7ece99b1 1518 value &= pmu_counter_mask(env);
200ac0ef 1519 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
1520}
1521
c4241c7d
PM
1522static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1523 uint64_t value)
200ac0ef 1524{
599b71e2 1525 value &= pmu_counter_mask(env);
200ac0ef 1526 env->cp15.c9_pmovsr &= ~value;
f4efb4b2 1527 pmu_update_irq(env);
200ac0ef
PM
1528}
1529
327dd510
AL
1530static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1531 uint64_t value)
1532{
1533 value &= pmu_counter_mask(env);
1534 env->cp15.c9_pmovsr |= value;
f4efb4b2 1535 pmu_update_irq(env);
327dd510
AL
1536}
1537
5ecdd3e4
AL
1538static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1539 uint64_t value, const uint8_t counter)
200ac0ef 1540{
5ecdd3e4
AL
1541 if (counter == 31) {
1542 pmccfiltr_write(env, ri, value);
1543 } else if (counter < pmu_num_counters(env)) {
1544 pmevcntr_op_start(env, counter);
1545
1546 /*
1547 * If this counter's event type is changing, store the current
1548 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1549 * pmevcntr_op_finish has the correct baseline when it converts back to
1550 * a delta.
1551 */
1552 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1553 PMXEVTYPER_EVTCOUNT;
1554 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1555 if (old_event != new_event) {
1556 uint64_t count = 0;
1557 if (event_supported(new_event)) {
1558 uint16_t event_idx = supported_event_map[new_event];
1559 count = pm_events[event_idx].get_count(env);
1560 }
1561 env->cp15.c14_pmevcntr_delta[counter] = count;
1562 }
1563
1564 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1565 pmevcntr_op_finish(env, counter);
1566 }
fdb86656
WH
1567 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1568 * PMSELR value is equal to or greater than the number of implemented
1569 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1570 */
5ecdd3e4
AL
1571}
1572
1573static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1574 const uint8_t counter)
1575{
1576 if (counter == 31) {
1577 return env->cp15.pmccfiltr_el0;
1578 } else if (counter < pmu_num_counters(env)) {
1579 return env->cp15.c14_pmevtyper[counter];
1580 } else {
1581 /*
1582 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1583 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1584 */
1585 return 0;
1586 }
1587}
1588
1589static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1590 uint64_t value)
1591{
1592 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1593 pmevtyper_write(env, ri, value, counter);
1594}
1595
1596static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1597 uint64_t value)
1598{
1599 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1600 env->cp15.c14_pmevtyper[counter] = value;
1601
1602 /*
1603 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1604 * pmu_op_finish calls when loading saved state for a migration. Because
1605 * we're potentially updating the type of event here, the value written to
1606 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1607 * different counter type. Therefore, we need to set this value to the
1608 * current count for the counter type we're writing so that pmu_op_finish
1609 * has the correct count for its calculation.
1610 */
1611 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1612 if (event_supported(event)) {
1613 uint16_t event_idx = supported_event_map[event];
1614 env->cp15.c14_pmevcntr_delta[counter] =
1615 pm_events[event_idx].get_count(env);
fdb86656
WH
1616 }
1617}
1618
5ecdd3e4
AL
1619static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1620{
1621 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1622 return pmevtyper_read(env, ri, counter);
1623}
1624
1625static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1626 uint64_t value)
1627{
1628 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1629}
1630
fdb86656
WH
1631static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1632{
5ecdd3e4
AL
1633 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1634}
1635
1636static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1637 uint64_t value, uint8_t counter)
1638{
1639 if (counter < pmu_num_counters(env)) {
1640 pmevcntr_op_start(env, counter);
1641 env->cp15.c14_pmevcntr[counter] = value;
1642 pmevcntr_op_finish(env, counter);
1643 }
1644 /*
1645 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1646 * are CONSTRAINED UNPREDICTABLE.
fdb86656 1647 */
5ecdd3e4
AL
1648}
1649
1650static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1651 uint8_t counter)
1652{
1653 if (counter < pmu_num_counters(env)) {
1654 uint64_t ret;
1655 pmevcntr_op_start(env, counter);
1656 ret = env->cp15.c14_pmevcntr[counter];
1657 pmevcntr_op_finish(env, counter);
1658 return ret;
fdb86656 1659 } else {
5ecdd3e4
AL
1660 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1661 * are CONSTRAINED UNPREDICTABLE. */
fdb86656
WH
1662 return 0;
1663 }
200ac0ef
PM
1664}
1665
5ecdd3e4
AL
1666static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1667 uint64_t value)
1668{
1669 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1670 pmevcntr_write(env, ri, value, counter);
1671}
1672
1673static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1674{
1675 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1676 return pmevcntr_read(env, ri, counter);
1677}
1678
1679static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1680 uint64_t value)
1681{
1682 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1683 assert(counter < pmu_num_counters(env));
1684 env->cp15.c14_pmevcntr[counter] = value;
1685 pmevcntr_write(env, ri, value, counter);
1686}
1687
1688static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1689{
1690 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1691 assert(counter < pmu_num_counters(env));
1692 return env->cp15.c14_pmevcntr[counter];
1693}
1694
1695static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1696 uint64_t value)
1697{
1698 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1699}
1700
1701static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1702{
1703 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1704}
1705
c4241c7d 1706static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1707 uint64_t value)
1708{
6ecd0b6b
AB
1709 if (arm_feature(env, ARM_FEATURE_V8)) {
1710 env->cp15.c9_pmuserenr = value & 0xf;
1711 } else {
1712 env->cp15.c9_pmuserenr = value & 1;
1713 }
200ac0ef
PM
1714}
1715
c4241c7d
PM
1716static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1717 uint64_t value)
200ac0ef
PM
1718{
1719 /* We have no event counters so only the C bit can be changed */
7ece99b1 1720 value &= pmu_counter_mask(env);
200ac0ef 1721 env->cp15.c9_pminten |= value;
f4efb4b2 1722 pmu_update_irq(env);
200ac0ef
PM
1723}
1724
c4241c7d
PM
1725static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1726 uint64_t value)
200ac0ef 1727{
7ece99b1 1728 value &= pmu_counter_mask(env);
200ac0ef 1729 env->cp15.c9_pminten &= ~value;
f4efb4b2 1730 pmu_update_irq(env);
200ac0ef
PM
1731}
1732
c4241c7d
PM
1733static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1734 uint64_t value)
8641136c 1735{
a505d7fe
PM
1736 /* Note that even though the AArch64 view of this register has bits
1737 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1738 * architectural requirements for bits which are RES0 only in some
1739 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1740 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1741 */
855ea66d 1742 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1743}
1744
64e0e2de
EI
1745static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1746{
ea22747c
RH
1747 /* Begin with base v8.0 state. */
1748 uint32_t valid_mask = 0x3fff;
2fc0cc0e 1749 ARMCPU *cpu = env_archcpu(env);
ea22747c 1750
252e8c69 1751 if (ri->state == ARM_CP_STATE_AA64) {
10d0ef3e
MN
1752 if (arm_feature(env, ARM_FEATURE_AARCH64) &&
1753 !cpu_isar_feature(aa64_aa32_el1, cpu)) {
1754 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */
1755 }
ea22747c 1756 valid_mask &= ~SCR_NET;
252e8c69
RH
1757
1758 if (cpu_isar_feature(aa64_lor, cpu)) {
1759 valid_mask |= SCR_TLOR;
1760 }
1761 if (cpu_isar_feature(aa64_pauth, cpu)) {
1762 valid_mask |= SCR_API | SCR_APK;
1763 }
926c1b97
RDC
1764 if (cpu_isar_feature(aa64_sel2, cpu)) {
1765 valid_mask |= SCR_EEL2;
1766 }
8ddb300b
RH
1767 if (cpu_isar_feature(aa64_mte, cpu)) {
1768 valid_mask |= SCR_ATA;
1769 }
ea22747c
RH
1770 } else {
1771 valid_mask &= ~(SCR_RW | SCR_ST);
1772 }
64e0e2de
EI
1773
1774 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1775 valid_mask &= ~SCR_HCE;
1776
1777 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1778 * supported if EL2 exists. The bit is UNK/SBZP when
1779 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1780 * when EL2 is unavailable.
4eb27640 1781 * On ARMv8, this bit is always available.
64e0e2de 1782 */
4eb27640
GB
1783 if (arm_feature(env, ARM_FEATURE_V7) &&
1784 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1785 valid_mask &= ~SCR_SMD;
1786 }
1787 }
1788
1789 /* Clear all-context RES0 bits. */
1790 value &= valid_mask;
1791 raw_write(env, ri, value);
1792}
1793
10d0ef3e
MN
1794static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1795{
1796 /*
1797 * scr_write will set the RES1 bits on an AArch64-only CPU.
1798 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1799 */
1800 scr_write(env, ri, 0);
1801}
1802
630fcd4d
MZ
1803static CPAccessResult access_aa64_tid2(CPUARMState *env,
1804 const ARMCPRegInfo *ri,
1805 bool isread)
1806{
1807 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1808 return CP_ACCESS_TRAP_EL2;
1809 }
1810
1811 return CP_ACCESS_OK;
1812}
1813
c4241c7d 1814static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c 1815{
2fc0cc0e 1816 ARMCPU *cpu = env_archcpu(env);
b85a1fd6
FA
1817
1818 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1819 * bank
1820 */
1821 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1822 ri->secure & ARM_CP_SECSTATE_S);
1823
1824 return cpu->ccsidr[index];
776d4e5c
PM
1825}
1826
c4241c7d
PM
1827static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1828 uint64_t value)
776d4e5c 1829{
8d5c773e 1830 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1831}
1832
1090b9c6
PM
1833static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1834{
29a0af61 1835 CPUState *cs = env_cpu(env);
cc974d5c
RDC
1836 bool el1 = arm_current_el(env) == 1;
1837 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1090b9c6
PM
1838 uint64_t ret = 0;
1839
cc974d5c 1840 if (hcr_el2 & HCR_IMO) {
636540e9
PM
1841 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1842 ret |= CPSR_I;
1843 }
1844 } else {
1845 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1846 ret |= CPSR_I;
1847 }
1090b9c6 1848 }
636540e9 1849
cc974d5c 1850 if (hcr_el2 & HCR_FMO) {
636540e9
PM
1851 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1852 ret |= CPSR_F;
1853 }
1854 } else {
1855 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1856 ret |= CPSR_F;
1857 }
1090b9c6 1858 }
636540e9 1859
1090b9c6
PM
1860 /* External aborts are not possible in QEMU so A bit is always clear */
1861 return ret;
1862}
1863
93fbc983
MZ
1864static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1865 bool isread)
1866{
1867 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1868 return CP_ACCESS_TRAP_EL2;
1869 }
1870
1871 return CP_ACCESS_OK;
1872}
1873
1874static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1875 bool isread)
1876{
1877 if (arm_feature(env, ARM_FEATURE_V8)) {
1878 return access_aa64_tid1(env, ri, isread);
1879 }
1880
1881 return CP_ACCESS_OK;
1882}
1883
e9aa6c21 1884static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
1885 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1886 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1887 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
1888 /* Performance monitors are implementation defined in v7,
1889 * but with an ARM recommended set of registers, which we
ac689a2e 1890 * follow.
200ac0ef
PM
1891 *
1892 * Performance registers fall into three categories:
1893 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1894 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1895 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1896 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1897 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1898 */
1899 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 1900 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 1901 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1902 .writefn = pmcntenset_write,
1903 .accessfn = pmreg_access,
1904 .raw_writefn = raw_write },
8521466b
AF
1905 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1906 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1907 .access = PL0_RW, .accessfn = pmreg_access,
1908 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1909 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 1910 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
1911 .access = PL0_RW,
1912 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1913 .accessfn = pmreg_access,
1914 .writefn = pmcntenclr_write,
7a0e58fa 1915 .type = ARM_CP_ALIAS },
8521466b
AF
1916 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1917 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1918 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 1919 .type = ARM_CP_ALIAS,
8521466b
AF
1920 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1921 .writefn = pmcntenclr_write },
200ac0ef 1922 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
f4efb4b2 1923 .access = PL0_RW, .type = ARM_CP_IO,
e4e91a21 1924 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
1925 .accessfn = pmreg_access,
1926 .writefn = pmovsr_write,
1927 .raw_writefn = raw_write },
978364f1
AF
1928 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1929 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1930 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 1931 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
1932 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1933 .writefn = pmovsr_write,
1934 .raw_writefn = raw_write },
200ac0ef 1935 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
f4efb4b2
AL
1936 .access = PL0_W, .accessfn = pmreg_access_swinc,
1937 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d
AL
1938 .writefn = pmswinc_write },
1939 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1940 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
f4efb4b2
AL
1941 .access = PL0_W, .accessfn = pmreg_access_swinc,
1942 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d 1943 .writefn = pmswinc_write },
6b040780
WH
1944 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1945 .access = PL0_RW, .type = ARM_CP_ALIAS,
1946 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 1947 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
1948 .raw_writefn = raw_write},
1949 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1950 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 1951 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
1952 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1953 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 1954 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 1955 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
421c7ebd 1956 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 1957 .accessfn = pmreg_access_ccntr },
8521466b
AF
1958 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1959 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 1960 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b 1961 .type = ARM_CP_IO,
980ebe87
AL
1962 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1963 .readfn = pmccntr_read, .writefn = pmccntr_write,
1964 .raw_readfn = raw_read, .raw_writefn = raw_write, },
4b8afa1f
AL
1965 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1966 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
1967 .access = PL0_RW, .accessfn = pmreg_access,
1968 .type = ARM_CP_ALIAS | ARM_CP_IO,
1969 .resetvalue = 0, },
8521466b
AF
1970 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1971 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
980ebe87 1972 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
8521466b
AF
1973 .access = PL0_RW, .accessfn = pmreg_access,
1974 .type = ARM_CP_IO,
1975 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1976 .resetvalue = 0, },
200ac0ef 1977 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
5ecdd3e4
AL
1978 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1979 .accessfn = pmreg_access,
fdb86656
WH
1980 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1981 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1982 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
5ecdd3e4
AL
1983 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1984 .accessfn = pmreg_access,
fdb86656 1985 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
200ac0ef 1986 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
5ecdd3e4
AL
1987 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1988 .accessfn = pmreg_access_xevcntr,
1989 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
1990 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
1991 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
1992 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1993 .accessfn = pmreg_access_xevcntr,
1994 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
200ac0ef 1995 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 1996 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 1997 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 1998 .resetvalue = 0,
d4e6df63 1999 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
2000 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2001 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 2002 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
2003 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2004 .resetvalue = 0,
2005 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 2006 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 2007 .access = PL1_RW, .accessfn = access_tpm,
b7d793ad 2008 .type = ARM_CP_ALIAS | ARM_CP_IO,
e6ec5457 2009 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 2010 .resetvalue = 0,
d4e6df63 2011 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
2012 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2013 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2014 .access = PL1_RW, .accessfn = access_tpm,
2015 .type = ARM_CP_IO,
2016 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2017 .writefn = pmintenset_write, .raw_writefn = raw_write,
2018 .resetvalue = 0x0 },
200ac0ef 2019 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
fc5f6856 2020 .access = PL1_RW, .accessfn = access_tpm,
887c0f15 2021 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
200ac0ef 2022 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 2023 .writefn = pmintenclr_write, },
978364f1
AF
2024 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2025 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
fc5f6856 2026 .access = PL1_RW, .accessfn = access_tpm,
887c0f15 2027 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
978364f1
AF
2028 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2029 .writefn = pmintenclr_write },
7da845b0
PM
2030 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2031 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
630fcd4d
MZ
2032 .access = PL1_R,
2033 .accessfn = access_aa64_tid2,
2034 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
2035 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2036 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
630fcd4d
MZ
2037 .access = PL1_RW,
2038 .accessfn = access_aa64_tid2,
2039 .writefn = csselr_write, .resetvalue = 0,
b85a1fd6
FA
2040 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2041 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
2042 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2043 * just RAZ for all cores:
2044 */
0ff644a7
PM
2045 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2046 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
93fbc983
MZ
2047 .access = PL1_R, .type = ARM_CP_CONST,
2048 .accessfn = access_aa64_tid1,
2049 .resetvalue = 0 },
f32cdad5
PM
2050 /* Auxiliary fault status registers: these also are IMPDEF, and we
2051 * choose to RAZ/WI for all cores.
2052 */
2053 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2054 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
84929218
RH
2055 .access = PL1_RW, .accessfn = access_tvm_trvm,
2056 .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
2057 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2058 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
84929218
RH
2059 .access = PL1_RW, .accessfn = access_tvm_trvm,
2060 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
2061 /* MAIR can just read-as-written because we don't implement caches
2062 * and so don't need to care about memory attributes.
2063 */
2064 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2065 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
84929218
RH
2066 .access = PL1_RW, .accessfn = access_tvm_trvm,
2067 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 2068 .resetvalue = 0 },
4cfb8ad8
PM
2069 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2070 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2071 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2072 .resetvalue = 0 },
b0fe2427
PM
2073 /* For non-long-descriptor page tables these are PRRR and NMRR;
2074 * regardless they still act as reads-as-written for QEMU.
b0fe2427 2075 */
1281f8e3 2076 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
2077 * allows them to assign the correct fieldoffset based on the endianness
2078 * handled in the field definitions.
2079 */
a903c449 2080 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
84929218
RH
2081 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2082 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2083 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2084 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 2085 .resetfn = arm_cp_reset_ignore },
a903c449 2086 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
84929218
RH
2087 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2088 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2089 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2090 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 2091 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
2092 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2093 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 2094 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
2095 /* 32 bit ITLB invalidates */
2096 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
30881b73
RH
2097 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2098 .writefn = tlbiall_write },
995939a6 2099 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
30881b73
RH
2100 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2101 .writefn = tlbimva_write },
995939a6 2102 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
30881b73
RH
2103 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2104 .writefn = tlbiasid_write },
995939a6
PM
2105 /* 32 bit DTLB invalidates */
2106 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
30881b73
RH
2107 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2108 .writefn = tlbiall_write },
995939a6 2109 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
30881b73
RH
2110 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2111 .writefn = tlbimva_write },
995939a6 2112 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
30881b73
RH
2113 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2114 .writefn = tlbiasid_write },
995939a6
PM
2115 /* 32 bit TLB invalidates */
2116 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73
RH
2117 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2118 .writefn = tlbiall_write },
995939a6 2119 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73
RH
2120 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2121 .writefn = tlbimva_write },
995939a6 2122 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73
RH
2123 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2124 .writefn = tlbiasid_write },
995939a6 2125 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73
RH
2126 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2127 .writefn = tlbimvaa_write },
995939a6
PM
2128};
2129
2130static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2131 /* 32 bit TLB invalidates, Inner Shareable */
2132 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
30881b73
RH
2133 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2134 .writefn = tlbiall_is_write },
995939a6 2135 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
30881b73
RH
2136 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2137 .writefn = tlbimva_is_write },
995939a6 2138 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
30881b73 2139 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 2140 .writefn = tlbiasid_is_write },
995939a6 2141 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
30881b73 2142 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 2143 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
2144};
2145
327dd510
AL
2146static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2147 /* PMOVSSET is not implemented in v7 before v7ve */
2148 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2149 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2150 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2151 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2152 .writefn = pmovsset_write,
2153 .raw_writefn = raw_write },
2154 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2155 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2156 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2157 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2158 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2159 .writefn = pmovsset_write,
2160 .raw_writefn = raw_write },
327dd510
AL
2161};
2162
c4241c7d
PM
2163static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2164 uint64_t value)
c326b979
PM
2165{
2166 value &= 1;
2167 env->teecr = value;
c326b979
PM
2168}
2169
cc7613bf
PM
2170static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2171 bool isread)
2172{
2173 /*
2174 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2175 * at all, so we don't need to check whether we're v8A.
2176 */
2177 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2178 (env->cp15.hstr_el2 & HSTR_TTEE)) {
2179 return CP_ACCESS_TRAP_EL2;
2180 }
2181 return CP_ACCESS_OK;
2182}
2183
3f208fd7
PM
2184static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2185 bool isread)
c326b979 2186{
dcbff19b 2187 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 2188 return CP_ACCESS_TRAP;
c326b979 2189 }
cc7613bf 2190 return teecr_access(env, ri, isread);
c326b979
PM
2191}
2192
2193static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2194 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2195 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2196 .resetvalue = 0,
cc7613bf 2197 .writefn = teecr_write, .accessfn = teecr_access },
c326b979
PM
2198 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2199 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 2200 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
2201};
2202
4d31c596 2203static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
2204 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2205 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2206 .access = PL0_RW,
54bf36ed 2207 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
2208 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2209 .access = PL0_RW,
54bf36ed
FA
2210 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2211 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
2212 .resetfn = arm_cp_reset_ignore },
2213 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2214 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2215 .access = PL0_R|PL1_W,
54bf36ed
FA
2216 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2217 .resetvalue = 0},
4d31c596
PM
2218 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2219 .access = PL0_R|PL1_W,
54bf36ed
FA
2220 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2221 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 2222 .resetfn = arm_cp_reset_ignore },
54bf36ed 2223 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 2224 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 2225 .access = PL1_RW,
54bf36ed
FA
2226 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2227 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2228 .access = PL1_RW,
2229 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2230 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2231 .resetvalue = 0 },
4d31c596
PM
2232};
2233
55d284af
PM
2234#ifndef CONFIG_USER_ONLY
2235
3f208fd7
PM
2236static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2237 bool isread)
00108f2d 2238{
75502672
PM
2239 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2240 * Writable only at the highest implemented exception level.
2241 */
2242 int el = arm_current_el(env);
5bc84371
RH
2243 uint64_t hcr;
2244 uint32_t cntkctl;
75502672
PM
2245
2246 switch (el) {
2247 case 0:
5bc84371
RH
2248 hcr = arm_hcr_el2_eff(env);
2249 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2250 cntkctl = env->cp15.cnthctl_el2;
2251 } else {
2252 cntkctl = env->cp15.c14_cntkctl;
2253 }
2254 if (!extract32(cntkctl, 0, 2)) {
75502672
PM
2255 return CP_ACCESS_TRAP;
2256 }
2257 break;
2258 case 1:
2259 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2260 arm_is_secure_below_el3(env)) {
2261 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2262 return CP_ACCESS_TRAP_UNCATEGORIZED;
2263 }
2264 break;
2265 case 2:
2266 case 3:
2267 break;
00108f2d 2268 }
75502672
PM
2269
2270 if (!isread && el < arm_highest_el(env)) {
2271 return CP_ACCESS_TRAP_UNCATEGORIZED;
2272 }
2273
00108f2d
PM
2274 return CP_ACCESS_OK;
2275}
2276
3f208fd7
PM
2277static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2278 bool isread)
00108f2d 2279{
0b6440af 2280 unsigned int cur_el = arm_current_el(env);
e6ef0169 2281 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2282 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2283
5bc84371
RH
2284 switch (cur_el) {
2285 case 0:
2286 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2287 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2288 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2289 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2290 }
0b6440af 2291
5bc84371
RH
2292 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2293 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2294 return CP_ACCESS_TRAP;
2295 }
2296
2297 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2298 if (hcr & HCR_E2H) {
2299 if (timeridx == GTIMER_PHYS &&
2300 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2301 return CP_ACCESS_TRAP_EL2;
2302 }
2303 } else {
2304 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
e6ef0169 2305 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2306 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2307 return CP_ACCESS_TRAP_EL2;
2308 }
2309 }
2310 break;
2311
2312 case 1:
2313 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
e6ef0169 2314 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2315 (hcr & HCR_E2H
2316 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2317 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2318 return CP_ACCESS_TRAP_EL2;
2319 }
2320 break;
0b6440af 2321 }
00108f2d
PM
2322 return CP_ACCESS_OK;
2323}
2324
3f208fd7
PM
2325static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2326 bool isread)
00108f2d 2327{
0b6440af 2328 unsigned int cur_el = arm_current_el(env);
e6ef0169 2329 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2330 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2331
5bc84371
RH
2332 switch (cur_el) {
2333 case 0:
2334 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2335 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2336 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2337 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2338 }
0b6440af 2339
5bc84371
RH
2340 /*
2341 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2342 * EL0 if EL0[PV]TEN is zero.
2343 */
2344 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2345 return CP_ACCESS_TRAP;
2346 }
2347 /* fall through */
2348
2349 case 1:
e6ef0169 2350 if (has_el2 && timeridx == GTIMER_PHYS) {
5bc84371
RH
2351 if (hcr & HCR_E2H) {
2352 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2353 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2354 return CP_ACCESS_TRAP_EL2;
2355 }
2356 } else {
2357 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2358 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2359 return CP_ACCESS_TRAP_EL2;
2360 }
2361 }
2362 }
2363 break;
0b6440af 2364 }
00108f2d
PM
2365 return CP_ACCESS_OK;
2366}
2367
2368static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
2369 const ARMCPRegInfo *ri,
2370 bool isread)
00108f2d 2371{
3f208fd7 2372 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2373}
2374
2375static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
2376 const ARMCPRegInfo *ri,
2377 bool isread)
00108f2d 2378{
3f208fd7 2379 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2380}
2381
3f208fd7
PM
2382static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2383 bool isread)
00108f2d 2384{
3f208fd7 2385 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2386}
2387
3f208fd7
PM
2388static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2389 bool isread)
00108f2d 2390{
3f208fd7 2391 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2392}
2393
b4d3978c 2394static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
2395 const ARMCPRegInfo *ri,
2396 bool isread)
b4d3978c
PM
2397{
2398 /* The AArch64 register view of the secure physical timer is
2399 * always accessible from EL3, and configurably accessible from
2400 * Secure EL1.
2401 */
2402 switch (arm_current_el(env)) {
2403 case 1:
2404 if (!arm_is_secure(env)) {
2405 return CP_ACCESS_TRAP;
2406 }
2407 if (!(env->cp15.scr_el3 & SCR_ST)) {
2408 return CP_ACCESS_TRAP_EL3;
2409 }
2410 return CP_ACCESS_OK;
2411 case 0:
2412 case 2:
2413 return CP_ACCESS_TRAP;
2414 case 3:
2415 return CP_ACCESS_OK;
2416 default:
2417 g_assert_not_reached();
2418 }
2419}
2420
55d284af
PM
2421static uint64_t gt_get_countervalue(CPUARMState *env)
2422{
7def8754
AJ
2423 ARMCPU *cpu = env_archcpu(env);
2424
2425 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
55d284af
PM
2426}
2427
2428static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2429{
2430 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2431
2432 if (gt->ctl & 1) {
2433 /* Timer enabled: calculate and set current ISTATUS, irq, and
2434 * reset timer to when ISTATUS next has to change
2435 */
edac4d8a
EI
2436 uint64_t offset = timeridx == GTIMER_VIRT ?
2437 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
2438 uint64_t count = gt_get_countervalue(&cpu->env);
2439 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 2440 int istatus = count - offset >= gt->cval;
55d284af 2441 uint64_t nexttick;
194cbc49 2442 int irqstate;
55d284af
PM
2443
2444 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
2445
2446 irqstate = (istatus && !(gt->ctl & 2));
2447 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2448
55d284af
PM
2449 if (istatus) {
2450 /* Next transition is when count rolls back over to zero */
2451 nexttick = UINT64_MAX;
2452 } else {
2453 /* Next transition is when we hit cval */
edac4d8a 2454 nexttick = gt->cval + offset;
55d284af
PM
2455 }
2456 /* Note that the desired next expiry time might be beyond the
2457 * signed-64-bit range of a QEMUTimer -- in this case we just
2458 * set the timer for as far in the future as possible. When the
2459 * timer expires we will reset the timer for any remaining period.
2460 */
7def8754 2461 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
4a0245b6
AJ
2462 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2463 } else {
2464 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af 2465 }
194cbc49 2466 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
2467 } else {
2468 /* Timer disabled: ISTATUS and timer output always clear */
2469 gt->ctl &= ~4;
2470 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 2471 timer_del(cpu->gt_timer[timeridx]);
194cbc49 2472 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
2473 }
2474}
2475
0e3eca4c
EI
2476static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2477 int timeridx)
55d284af 2478{
2fc0cc0e 2479 ARMCPU *cpu = env_archcpu(env);
55d284af 2480
bc72ad67 2481 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
2482}
2483
c4241c7d 2484static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 2485{
c4241c7d 2486 return gt_get_countervalue(env);
55d284af
PM
2487}
2488
53d1f856
RH
2489static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2490{
2491 uint64_t hcr;
2492
2493 switch (arm_current_el(env)) {
2494 case 2:
2495 hcr = arm_hcr_el2_eff(env);
2496 if (hcr & HCR_E2H) {
2497 return 0;
2498 }
2499 break;
2500 case 0:
2501 hcr = arm_hcr_el2_eff(env);
2502 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2503 return 0;
2504 }
2505 break;
2506 }
2507
2508 return env->cp15.cntvoff_el2;
2509}
2510
edac4d8a
EI
2511static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2512{
53d1f856 2513 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
edac4d8a
EI
2514}
2515
c4241c7d 2516static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2517 int timeridx,
c4241c7d 2518 uint64_t value)
55d284af 2519{
194cbc49 2520 trace_arm_gt_cval_write(timeridx, value);
55d284af 2521 env->cp15.c14_timer[timeridx].cval = value;
2fc0cc0e 2522 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af 2523}
c4241c7d 2524
0e3eca4c
EI
2525static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2526 int timeridx)
55d284af 2527{
53d1f856
RH
2528 uint64_t offset = 0;
2529
2530 switch (timeridx) {
2531 case GTIMER_VIRT:
8c94b071 2532 case GTIMER_HYPVIRT:
53d1f856
RH
2533 offset = gt_virt_cnt_offset(env);
2534 break;
2535 }
55d284af 2536
c4241c7d 2537 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 2538 (gt_get_countervalue(env) - offset));
55d284af
PM
2539}
2540
c4241c7d 2541static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2542 int timeridx,
c4241c7d 2543 uint64_t value)
55d284af 2544{
53d1f856
RH
2545 uint64_t offset = 0;
2546
2547 switch (timeridx) {
2548 case GTIMER_VIRT:
8c94b071 2549 case GTIMER_HYPVIRT:
53d1f856
RH
2550 offset = gt_virt_cnt_offset(env);
2551 break;
2552 }
55d284af 2553
194cbc49 2554 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 2555 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 2556 sextract64(value, 0, 32);
2fc0cc0e 2557 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af
PM
2558}
2559
c4241c7d 2560static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2561 int timeridx,
c4241c7d 2562 uint64_t value)
55d284af 2563{
2fc0cc0e 2564 ARMCPU *cpu = env_archcpu(env);
55d284af
PM
2565 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2566
194cbc49 2567 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 2568 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
2569 if ((oldval ^ value) & 1) {
2570 /* Enable toggled */
2571 gt_recalc_timer(cpu, timeridx);
d3afacc7 2572 } else if ((oldval ^ value) & 2) {
55d284af
PM
2573 /* IMASK toggled: don't need to recalculate,
2574 * just set the interrupt line based on ISTATUS
2575 */
194cbc49
PM
2576 int irqstate = (oldval & 4) && !(value & 2);
2577
2578 trace_arm_gt_imask_toggle(timeridx, irqstate);
2579 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 2580 }
55d284af
PM
2581}
2582
0e3eca4c
EI
2583static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2584{
2585 gt_timer_reset(env, ri, GTIMER_PHYS);
2586}
2587
2588static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2589 uint64_t value)
2590{
2591 gt_cval_write(env, ri, GTIMER_PHYS, value);
2592}
2593
2594static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2595{
2596 return gt_tval_read(env, ri, GTIMER_PHYS);
2597}
2598
2599static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2600 uint64_t value)
2601{
2602 gt_tval_write(env, ri, GTIMER_PHYS, value);
2603}
2604
2605static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2606 uint64_t value)
2607{
2608 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2609}
2610
bb5972e4
RH
2611static int gt_phys_redir_timeridx(CPUARMState *env)
2612{
2613 switch (arm_mmu_idx(env)) {
2614 case ARMMMUIdx_E20_0:
2615 case ARMMMUIdx_E20_2:
452ef8cb 2616 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
2617 case ARMMMUIdx_SE20_0:
2618 case ARMMMUIdx_SE20_2:
2619 case ARMMMUIdx_SE20_2_PAN:
bb5972e4
RH
2620 return GTIMER_HYP;
2621 default:
2622 return GTIMER_PHYS;
2623 }
2624}
2625
2626static int gt_virt_redir_timeridx(CPUARMState *env)
2627{
2628 switch (arm_mmu_idx(env)) {
2629 case ARMMMUIdx_E20_0:
2630 case ARMMMUIdx_E20_2:
452ef8cb 2631 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
2632 case ARMMMUIdx_SE20_0:
2633 case ARMMMUIdx_SE20_2:
2634 case ARMMMUIdx_SE20_2_PAN:
bb5972e4
RH
2635 return GTIMER_HYPVIRT;
2636 default:
2637 return GTIMER_VIRT;
2638 }
2639}
2640
2641static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2642 const ARMCPRegInfo *ri)
2643{
2644 int timeridx = gt_phys_redir_timeridx(env);
2645 return env->cp15.c14_timer[timeridx].cval;
2646}
2647
2648static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2649 uint64_t value)
2650{
2651 int timeridx = gt_phys_redir_timeridx(env);
2652 gt_cval_write(env, ri, timeridx, value);
2653}
2654
2655static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2656 const ARMCPRegInfo *ri)
2657{
2658 int timeridx = gt_phys_redir_timeridx(env);
2659 return gt_tval_read(env, ri, timeridx);
2660}
2661
2662static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2663 uint64_t value)
2664{
2665 int timeridx = gt_phys_redir_timeridx(env);
2666 gt_tval_write(env, ri, timeridx, value);
2667}
2668
2669static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2670 const ARMCPRegInfo *ri)
2671{
2672 int timeridx = gt_phys_redir_timeridx(env);
2673 return env->cp15.c14_timer[timeridx].ctl;
2674}
2675
2676static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2677 uint64_t value)
2678{
2679 int timeridx = gt_phys_redir_timeridx(env);
2680 gt_ctl_write(env, ri, timeridx, value);
2681}
2682
0e3eca4c
EI
2683static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2684{
2685 gt_timer_reset(env, ri, GTIMER_VIRT);
2686}
2687
2688static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2689 uint64_t value)
2690{
2691 gt_cval_write(env, ri, GTIMER_VIRT, value);
2692}
2693
2694static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2695{
2696 return gt_tval_read(env, ri, GTIMER_VIRT);
2697}
2698
2699static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2700 uint64_t value)
2701{
2702 gt_tval_write(env, ri, GTIMER_VIRT, value);
2703}
2704
2705static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2706 uint64_t value)
2707{
2708 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2709}
2710
edac4d8a
EI
2711static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2712 uint64_t value)
2713{
2fc0cc0e 2714 ARMCPU *cpu = env_archcpu(env);
edac4d8a 2715
194cbc49 2716 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
2717 raw_write(env, ri, value);
2718 gt_recalc_timer(cpu, GTIMER_VIRT);
2719}
2720
bb5972e4
RH
2721static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2722 const ARMCPRegInfo *ri)
2723{
2724 int timeridx = gt_virt_redir_timeridx(env);
2725 return env->cp15.c14_timer[timeridx].cval;
2726}
2727
2728static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2729 uint64_t value)
2730{
2731 int timeridx = gt_virt_redir_timeridx(env);
2732 gt_cval_write(env, ri, timeridx, value);
2733}
2734
2735static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2736 const ARMCPRegInfo *ri)
2737{
2738 int timeridx = gt_virt_redir_timeridx(env);
2739 return gt_tval_read(env, ri, timeridx);
2740}
2741
2742static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2743 uint64_t value)
2744{
2745 int timeridx = gt_virt_redir_timeridx(env);
2746 gt_tval_write(env, ri, timeridx, value);
2747}
2748
2749static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2750 const ARMCPRegInfo *ri)
2751{
2752 int timeridx = gt_virt_redir_timeridx(env);
2753 return env->cp15.c14_timer[timeridx].ctl;
2754}
2755
2756static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2757 uint64_t value)
2758{
2759 int timeridx = gt_virt_redir_timeridx(env);
2760 gt_ctl_write(env, ri, timeridx, value);
2761}
2762
b0e66d95
EI
2763static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2764{
2765 gt_timer_reset(env, ri, GTIMER_HYP);
2766}
2767
2768static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2769 uint64_t value)
2770{
2771 gt_cval_write(env, ri, GTIMER_HYP, value);
2772}
2773
2774static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2775{
2776 return gt_tval_read(env, ri, GTIMER_HYP);
2777}
2778
2779static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2780 uint64_t value)
2781{
2782 gt_tval_write(env, ri, GTIMER_HYP, value);
2783}
2784
2785static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2786 uint64_t value)
2787{
2788 gt_ctl_write(env, ri, GTIMER_HYP, value);
2789}
2790
b4d3978c
PM
2791static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2792{
2793 gt_timer_reset(env, ri, GTIMER_SEC);
2794}
2795
2796static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2797 uint64_t value)
2798{
2799 gt_cval_write(env, ri, GTIMER_SEC, value);
2800}
2801
2802static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2803{
2804 return gt_tval_read(env, ri, GTIMER_SEC);
2805}
2806
2807static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2808 uint64_t value)
2809{
2810 gt_tval_write(env, ri, GTIMER_SEC, value);
2811}
2812
2813static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2814 uint64_t value)
2815{
2816 gt_ctl_write(env, ri, GTIMER_SEC, value);
2817}
2818
8c94b071
RH
2819static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2820{
2821 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2822}
2823
2824static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2825 uint64_t value)
2826{
2827 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2828}
2829
2830static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2831{
2832 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2833}
2834
2835static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2836 uint64_t value)
2837{
2838 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2839}
2840
2841static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2842 uint64_t value)
2843{
2844 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2845}
2846
55d284af
PM
2847void arm_gt_ptimer_cb(void *opaque)
2848{
2849 ARMCPU *cpu = opaque;
2850
2851 gt_recalc_timer(cpu, GTIMER_PHYS);
2852}
2853
2854void arm_gt_vtimer_cb(void *opaque)
2855{
2856 ARMCPU *cpu = opaque;
2857
2858 gt_recalc_timer(cpu, GTIMER_VIRT);
2859}
2860
b0e66d95
EI
2861void arm_gt_htimer_cb(void *opaque)
2862{
2863 ARMCPU *cpu = opaque;
2864
2865 gt_recalc_timer(cpu, GTIMER_HYP);
2866}
2867
b4d3978c
PM
2868void arm_gt_stimer_cb(void *opaque)
2869{
2870 ARMCPU *cpu = opaque;
2871
2872 gt_recalc_timer(cpu, GTIMER_SEC);
2873}
2874
8c94b071
RH
2875void arm_gt_hvtimer_cb(void *opaque)
2876{
2877 ARMCPU *cpu = opaque;
2878
2879 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2880}
2881
96eec6b2
AJ
2882static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2883{
2884 ARMCPU *cpu = env_archcpu(env);
2885
2886 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2887}
2888
55d284af
PM
2889static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2890 /* Note that CNTFRQ is purely reads-as-written for the benefit
2891 * of software; writing it doesn't actually change the timer frequency.
2892 * Our reset value matches the fixed frequency we implement the timer at.
2893 */
2894 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2895 .type = ARM_CP_ALIAS,
a7adc4b7
PM
2896 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2897 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
2898 },
2899 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2900 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2901 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af 2902 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
96eec6b2 2903 .resetfn = arm_gt_cntfrq_reset,
55d284af
PM
2904 },
2905 /* overall control: mostly access permissions */
a7adc4b7
PM
2906 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2907 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
2908 .access = PL1_RW,
2909 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2910 .resetvalue = 0,
2911 },
2912 /* per-timer control */
2913 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 2914 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 2915 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
2916 .accessfn = gt_ptimer_access,
2917 .fieldoffset = offsetoflow32(CPUARMState,
2918 cp15.c14_timer[GTIMER_PHYS].ctl),
bb5972e4
RH
2919 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2920 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7 2921 },
9c513e78 2922 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
2923 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2924 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 2925 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
9ff9dd3c
PM
2926 .accessfn = gt_ptimer_access,
2927 .fieldoffset = offsetoflow32(CPUARMState,
2928 cp15.c14_timer[GTIMER_SEC].ctl),
2929 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2930 },
a7adc4b7
PM
2931 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2932 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
daf1dc5f 2933 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 2934 .accessfn = gt_ptimer_access,
55d284af
PM
2935 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2936 .resetvalue = 0,
bb5972e4
RH
2937 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2938 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2939 },
2940 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
daf1dc5f 2941 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
2942 .accessfn = gt_vtimer_access,
2943 .fieldoffset = offsetoflow32(CPUARMState,
2944 cp15.c14_timer[GTIMER_VIRT].ctl),
bb5972e4
RH
2945 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2946 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
2947 },
2948 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2949 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
daf1dc5f 2950 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 2951 .accessfn = gt_vtimer_access,
55d284af
PM
2952 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2953 .resetvalue = 0,
bb5972e4
RH
2954 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2955 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2956 },
2957 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2958 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 2959 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 2960 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 2961 .accessfn = gt_ptimer_access,
bb5972e4 2962 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
55d284af 2963 },
9c513e78 2964 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
2965 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2966 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 2967 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
9ff9dd3c
PM
2968 .accessfn = gt_ptimer_access,
2969 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2970 },
a7adc4b7
PM
2971 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2972 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
daf1dc5f 2973 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 2974 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
bb5972e4 2975 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
a7adc4b7 2976 },
55d284af 2977 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
daf1dc5f 2978 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 2979 .accessfn = gt_vtimer_access,
bb5972e4 2980 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
55d284af 2981 },
a7adc4b7
PM
2982 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2983 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
daf1dc5f 2984 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 2985 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
bb5972e4 2986 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
a7adc4b7 2987 },
55d284af
PM
2988 /* The counter itself */
2989 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 2990 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2991 .accessfn = gt_pct_access,
a7adc4b7
PM
2992 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2993 },
2994 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2995 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 2996 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2997 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
2998 },
2999 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 3000 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3001 .accessfn = gt_vct_access,
edac4d8a 3002 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
3003 },
3004 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3005 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 3006 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3007 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
3008 },
3009 /* Comparison value, indicating when the timer goes off */
3010 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3011 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3012 .access = PL0_RW,
7a0e58fa 3013 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3014 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 3015 .accessfn = gt_ptimer_access,
bb5972e4
RH
3016 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3017 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7 3018 },
9c513e78 3019 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3020 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3021 .access = PL0_RW,
9ff9dd3c
PM
3022 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3023 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3024 .accessfn = gt_ptimer_access,
3025 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3026 },
a7adc4b7
PM
3027 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3028 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
daf1dc5f 3029 .access = PL0_RW,
a7adc4b7
PM
3030 .type = ARM_CP_IO,
3031 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 3032 .resetvalue = 0, .accessfn = gt_ptimer_access,
bb5972e4
RH
3033 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3034 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
55d284af
PM
3035 },
3036 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
daf1dc5f 3037 .access = PL0_RW,
7a0e58fa 3038 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3039 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 3040 .accessfn = gt_vtimer_access,
bb5972e4
RH
3041 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3042 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
3043 },
3044 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3045 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
daf1dc5f 3046 .access = PL0_RW,
a7adc4b7
PM
3047 .type = ARM_CP_IO,
3048 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3049 .resetvalue = 0, .accessfn = gt_vtimer_access,
bb5972e4
RH
3050 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3051 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
55d284af 3052 },
b4d3978c
PM
3053 /* Secure timer -- this is actually restricted to only EL3
3054 * and configurably Secure-EL1 via the accessfn.
3055 */
3056 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3057 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3058 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3059 .accessfn = gt_stimer_access,
3060 .readfn = gt_sec_tval_read,
3061 .writefn = gt_sec_tval_write,
3062 .resetfn = gt_sec_timer_reset,
3063 },
3064 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3065 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3066 .type = ARM_CP_IO, .access = PL1_RW,
3067 .accessfn = gt_stimer_access,
3068 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3069 .resetvalue = 0,
3070 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3071 },
3072 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3073 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3074 .type = ARM_CP_IO, .access = PL1_RW,
3075 .accessfn = gt_stimer_access,
3076 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3077 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3078 },
55d284af
PM
3079};
3080
bb5972e4
RH
3081static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3082 bool isread)
3083{
3084 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3085 return CP_ACCESS_TRAP;
3086 }
3087 return CP_ACCESS_OK;
3088}
3089
55d284af 3090#else
26c4a83b
AB
3091
3092/* In user-mode most of the generic timer registers are inaccessible
3093 * however modern kernels (4.12+) allow access to cntvct_el0
55d284af 3094 */
26c4a83b
AB
3095
3096static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3097{
7def8754
AJ
3098 ARMCPU *cpu = env_archcpu(env);
3099
26c4a83b
AB
3100 /* Currently we have no support for QEMUTimer in linux-user so we
3101 * can't call gt_get_countervalue(env), instead we directly
3102 * call the lower level functions.
3103 */
7def8754 3104 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
26c4a83b
AB
3105}
3106
6cc7a3ae 3107static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
26c4a83b
AB
3108 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3109 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3110 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3111 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3112 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3113 },
3114 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3115 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3116 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3117 .readfn = gt_virt_cnt_read,
3118 },
6cc7a3ae
PM
3119};
3120
55d284af
PM
3121#endif
3122
c4241c7d 3123static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 3124{
891a2fe7 3125 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 3126 raw_write(env, ri, value);
891a2fe7 3127 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 3128 raw_write(env, ri, value & 0xfffff6ff);
4a501606 3129 } else {
8d5c773e 3130 raw_write(env, ri, value & 0xfffff1ff);
4a501606 3131 }
4a501606
PM
3132}
3133
3134#ifndef CONFIG_USER_ONLY
3135/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 3136
3f208fd7
PM
3137static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3138 bool isread)
92611c00
PM
3139{
3140 if (ri->opc2 & 4) {
926c1b97 3141 /* The ATS12NSO* operations must trap to EL3 or EL2 if executed in
87562e4f
PM
3142 * Secure EL1 (which can only happen if EL3 is AArch64).
3143 * They are simply UNDEF if executed from NS EL1.
3144 * They function normally from EL2 or EL3.
92611c00 3145 */
87562e4f
PM
3146 if (arm_current_el(env) == 1) {
3147 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
3148 if (env->cp15.scr_el3 & SCR_EEL2) {
3149 return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3150 }
87562e4f
PM
3151 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3152 }
3153 return CP_ACCESS_TRAP_UNCATEGORIZED;
3154 }
92611c00
PM
3155 }
3156 return CP_ACCESS_OK;
3157}
3158
9fb005b0 3159#ifdef CONFIG_TCG
060e8a48 3160static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
03ae85f8 3161 MMUAccessType access_type, ARMMMUIdx mmu_idx)
4a501606 3162{
a8170e5e 3163 hwaddr phys_addr;
4a501606
PM
3164 target_ulong page_size;
3165 int prot;
b7cc4e82 3166 bool ret;
01c097f7 3167 uint64_t par64;
1313e2d7 3168 bool format64 = false;
8bf5b6a9 3169 MemTxAttrs attrs = {};
e14b5a23 3170 ARMMMUFaultInfo fi = {};
5b2d261d 3171 ARMCacheAttrs cacheattrs = {};
4a501606 3172
5b2d261d 3173 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
bc52bfeb 3174 &prot, &page_size, &fi, &cacheattrs);
1313e2d7 3175
0710b2fa
PM
3176 if (ret) {
3177 /*
3178 * Some kinds of translation fault must cause exceptions rather
3179 * than being reported in the PAR.
3180 */
3181 int current_el = arm_current_el(env);
3182 int target_el;
3183 uint32_t syn, fsr, fsc;
3184 bool take_exc = false;
3185
b1a10c86 3186 if (fi.s1ptw && current_el == 1
fee7aa46 3187 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
0710b2fa
PM
3188 /*
3189 * Synchronous stage 2 fault on an access made as part of the
3190 * translation table walk for AT S1E0* or AT S1E1* insn
3191 * executed from NS EL1. If this is a synchronous external abort
3192 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3193 * to EL3. Otherwise the fault is taken as an exception to EL2,
3194 * and HPFAR_EL2 holds the faulting IPA.
3195 */
3196 if (fi.type == ARMFault_SyncExternalOnWalk &&
3197 (env->cp15.scr_el3 & SCR_EA)) {
3198 target_el = 3;
3199 } else {
3200 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
9861248f
RDC
3201 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3202 env->cp15.hpfar_el2 |= HPFAR_NS;
3203 }
0710b2fa
PM
3204 target_el = 2;
3205 }
3206 take_exc = true;
3207 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3208 /*
3209 * Synchronous external aborts during a translation table walk
3210 * are taken as Data Abort exceptions.
3211 */
3212 if (fi.stage2) {
3213 if (current_el == 3) {
3214 target_el = 3;
3215 } else {
3216 target_el = 2;
3217 }
3218 } else {
3219 target_el = exception_target_el(env);
3220 }
3221 take_exc = true;
3222 }
3223
3224 if (take_exc) {
3225 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3226 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3227 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3228 fsr = arm_fi_to_lfsc(&fi);
3229 fsc = extract32(fsr, 0, 6);
3230 } else {
3231 fsr = arm_fi_to_sfsc(&fi);
3232 fsc = 0x3f;
3233 }
3234 /*
3235 * Report exception with ESR indicating a fault due to a
3236 * translation table walk for a cache maintenance instruction.
3237 */
e24fd076 3238 syn = syn_data_abort_no_iss(current_el == target_el, 0,
0710b2fa
PM
3239 fi.ea, 1, fi.s1ptw, 1, fsc);
3240 env->exception.vaddress = value;
3241 env->exception.fsr = fsr;
3242 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3243 }
3244 }
3245
1313e2d7
EI
3246 if (is_a64(env)) {
3247 format64 = true;
3248 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3249 /*
3250 * ATS1Cxx:
3251 * * TTBCR.EAE determines whether the result is returned using the
3252 * 32-bit or the 64-bit PAR format
3253 * * Instructions executed in Hyp mode always use the 64bit format
3254 *
3255 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3256 * * The Non-secure TTBCR.EAE bit is set to 1
3257 * * The implementation includes EL2, and the value of HCR.VM is 1
3258 *
9d1bab33
PM
3259 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3260 *
23463e0e 3261 * ATS1Hx always uses the 64bit format.
1313e2d7
EI
3262 */
3263 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3264
3265 if (arm_feature(env, ARM_FEATURE_EL2)) {
452ef8cb
RH
3266 if (mmu_idx == ARMMMUIdx_E10_0 ||
3267 mmu_idx == ARMMMUIdx_E10_1 ||
3268 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9d1bab33 3269 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
1313e2d7
EI
3270 } else {
3271 format64 |= arm_current_el(env) == 2;
3272 }
3273 }
3274 }
3275
3276 if (format64) {
5efe9ed4 3277 /* Create a 64-bit PAR */
01c097f7 3278 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 3279 if (!ret) {
702a9357 3280 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
3281 if (!attrs.secure) {
3282 par64 |= (1 << 9); /* NS */
3283 }
5b2d261d
AB
3284 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3285 par64 |= cacheattrs.shareability << 7; /* SH */
4a501606 3286 } else {
5efe9ed4
PM
3287 uint32_t fsr = arm_fi_to_lfsc(&fi);
3288
702a9357 3289 par64 |= 1; /* F */
b7cc4e82 3290 par64 |= (fsr & 0x3f) << 1; /* FS */
0f7b791b
PM
3291 if (fi.stage2) {
3292 par64 |= (1 << 9); /* S */
3293 }
3294 if (fi.s1ptw) {
3295 par64 |= (1 << 8); /* PTW */
3296 }
4a501606
PM
3297 }
3298 } else {
b7cc4e82 3299 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
3300 * translation table format (with WnR always clear).
3301 * Convert it to a 32-bit PAR.
3302 */
b7cc4e82 3303 if (!ret) {
702a9357
PM
3304 /* We do not set any attribute bits in the PAR */
3305 if (page_size == (1 << 24)
3306 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 3307 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 3308 } else {
01c097f7 3309 par64 = phys_addr & 0xfffff000;
702a9357 3310 }
8bf5b6a9
PM
3311 if (!attrs.secure) {
3312 par64 |= (1 << 9); /* NS */
3313 }
702a9357 3314 } else {
5efe9ed4
PM
3315 uint32_t fsr = arm_fi_to_sfsc(&fi);
3316
b7cc4e82
PC
3317 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3318 ((fsr & 0xf) << 1) | 1;
702a9357 3319 }
4a501606 3320 }
060e8a48
PM
3321 return par64;
3322}
9fb005b0 3323#endif /* CONFIG_TCG */
060e8a48
PM
3324
3325static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3326{
9fb005b0 3327#ifdef CONFIG_TCG
03ae85f8 3328 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 3329 uint64_t par64;
d3649702
PM
3330 ARMMMUIdx mmu_idx;
3331 int el = arm_current_el(env);
3332 bool secure = arm_is_secure_below_el3(env);
060e8a48 3333
d3649702
PM
3334 switch (ri->opc2 & 6) {
3335 case 0:
04b07d29 3336 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
d3649702
PM
3337 switch (el) {
3338 case 3:
127b2b08 3339 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3340 break;
3341 case 2:
b6ad6062 3342 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
04b07d29 3343 /* fall through */
d3649702 3344 case 1:
04b07d29 3345 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
b1a10c86 3346 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
04b07d29
RH
3347 : ARMMMUIdx_Stage1_E1_PAN);
3348 } else {
b1a10c86 3349 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
04b07d29 3350 }
d3649702
PM
3351 break;
3352 default:
3353 g_assert_not_reached();
3354 }
3355 break;
3356 case 2:
3357 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3358 switch (el) {
3359 case 3:
fba37aed 3360 mmu_idx = ARMMMUIdx_SE10_0;
d3649702
PM
3361 break;
3362 case 2:
b1a10c86 3363 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
2859d7b5 3364 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3365 break;
3366 case 1:
b1a10c86 3367 mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3368 break;
3369 default:
3370 g_assert_not_reached();
3371 }
3372 break;
3373 case 4:
3374 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
01b98b68 3375 mmu_idx = ARMMMUIdx_E10_1;
d3649702
PM
3376 break;
3377 case 6:
3378 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
01b98b68 3379 mmu_idx = ARMMMUIdx_E10_0;
d3649702
PM
3380 break;
3381 default:
3382 g_assert_not_reached();
3383 }
3384
3385 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
3386
3387 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3388#else
3389 /* Handled by hardware accelerator. */
3390 g_assert_not_reached();
3391#endif /* CONFIG_TCG */
4a501606 3392}
060e8a48 3393
14db7fe0
PM
3394static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3395 uint64_t value)
3396{
9fb005b0 3397#ifdef CONFIG_TCG
03ae85f8 3398 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
3399 uint64_t par64;
3400
e013b741 3401 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
14db7fe0
PM
3402
3403 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3404#else
3405 /* Handled by hardware accelerator. */
3406 g_assert_not_reached();
3407#endif /* CONFIG_TCG */
14db7fe0
PM
3408}
3409
3f208fd7
PM
3410static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3411 bool isread)
2a47df95 3412{
926c1b97
RDC
3413 if (arm_current_el(env) == 3 &&
3414 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
2a47df95
PM
3415 return CP_ACCESS_TRAP;
3416 }
3417 return CP_ACCESS_OK;
3418}
3419
060e8a48
PM
3420static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3421 uint64_t value)
3422{
9fb005b0 3423#ifdef CONFIG_TCG
03ae85f8 3424 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
3425 ARMMMUIdx mmu_idx;
3426 int secure = arm_is_secure_below_el3(env);
3427
3428 switch (ri->opc2 & 6) {
3429 case 0:
3430 switch (ri->opc1) {
04b07d29
RH
3431 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3432 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
b1a10c86 3433 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
04b07d29
RH
3434 : ARMMMUIdx_Stage1_E1_PAN);
3435 } else {
b1a10c86 3436 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
04b07d29 3437 }
d3649702
PM
3438 break;
3439 case 4: /* AT S1E2R, AT S1E2W */
b6ad6062 3440 mmu_idx = secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2;
d3649702
PM
3441 break;
3442 case 6: /* AT S1E3R, AT S1E3W */
127b2b08 3443 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3444 break;
3445 default:
3446 g_assert_not_reached();
3447 }
3448 break;
3449 case 2: /* AT S1E0R, AT S1E0W */
b1a10c86 3450 mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3451 break;
3452 case 4: /* AT S12E1R, AT S12E1W */
fba37aed 3453 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
d3649702
PM
3454 break;
3455 case 6: /* AT S12E0R, AT S12E0W */
fba37aed 3456 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
d3649702
PM
3457 break;
3458 default:
3459 g_assert_not_reached();
3460 }
060e8a48 3461
d3649702 3462 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
9fb005b0
PMD
3463#else
3464 /* Handled by hardware accelerator. */
3465 g_assert_not_reached();
3466#endif /* CONFIG_TCG */
060e8a48 3467}
4a501606
PM
3468#endif
3469
3470static const ARMCPRegInfo vapa_cp_reginfo[] = {
3471 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3472 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
3473 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3474 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
3475 .writefn = par_write },
3476#ifndef CONFIG_USER_ONLY
87562e4f 3477 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 3478 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 3479 .access = PL1_W, .accessfn = ats_access,
0710b2fa 3480 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
4a501606 3481#endif
4a501606
PM
3482};
3483
18032bec
PM
3484/* Return basic MPU access permission bits. */
3485static uint32_t simple_mpu_ap_bits(uint32_t val)
3486{
3487 uint32_t ret;
3488 uint32_t mask;
3489 int i;
3490 ret = 0;
3491 mask = 3;
3492 for (i = 0; i < 16; i += 2) {
3493 ret |= (val >> i) & mask;
3494 mask <<= 2;
3495 }
3496 return ret;
3497}
3498
3499/* Pad basic MPU access permission bits to extended format. */
3500static uint32_t extended_mpu_ap_bits(uint32_t val)
3501{
3502 uint32_t ret;
3503 uint32_t mask;
3504 int i;
3505 ret = 0;
3506 mask = 3;
3507 for (i = 0; i < 16; i += 2) {
3508 ret |= (val & mask) << i;
3509 mask <<= 2;
3510 }
3511 return ret;
3512}
3513
c4241c7d
PM
3514static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3515 uint64_t value)
18032bec 3516{
7e09797c 3517 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
3518}
3519
c4241c7d 3520static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3521{
7e09797c 3522 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
3523}
3524
c4241c7d
PM
3525static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3526 uint64_t value)
18032bec 3527{
7e09797c 3528 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
3529}
3530
c4241c7d 3531static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3532{
7e09797c 3533 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
3534}
3535
6cb0b013
PC
3536static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3537{
3538 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3539
3540 if (!u32p) {
3541 return 0;
3542 }
3543
1bc04a88 3544 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
3545 return *u32p;
3546}
3547
3548static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3549 uint64_t value)
3550{
2fc0cc0e 3551 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3552 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3553
3554 if (!u32p) {
3555 return;
3556 }
3557
1bc04a88 3558 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 3559 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
3560 *u32p = value;
3561}
3562
6cb0b013
PC
3563static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3564 uint64_t value)
3565{
2fc0cc0e 3566 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3567 uint32_t nrgs = cpu->pmsav7_dregion;
3568
3569 if (value >= nrgs) {
3570 qemu_log_mask(LOG_GUEST_ERROR,
3571 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3572 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3573 return;
3574 }
3575
3576 raw_write(env, ri, value);
3577}
3578
3579static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
3580 /* Reset for all these registers is handled in arm_cpu_reset(),
3581 * because the PMSAv7 is also used by M-profile CPUs, which do
3582 * not register cpregs but still need the state to be reset.
3583 */
6cb0b013
PC
3584 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3585 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3586 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
3587 .readfn = pmsav7_read, .writefn = pmsav7_write,
3588 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3589 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3590 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3591 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
3592 .readfn = pmsav7_read, .writefn = pmsav7_write,
3593 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3594 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3595 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3596 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
3597 .readfn = pmsav7_read, .writefn = pmsav7_write,
3598 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3599 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3600 .access = PL1_RW,
1bc04a88 3601 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
3602 .writefn = pmsav7_rgnr_write,
3603 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3604};
3605
18032bec
PM
3606static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3607 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3608 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3609 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
3610 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3611 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 3612 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3613 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
3614 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3615 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3616 .access = PL1_RW,
7e09797c
PM
3617 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3618 .resetvalue = 0, },
18032bec
PM
3619 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3620 .access = PL1_RW,
7e09797c
PM
3621 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3622 .resetvalue = 0, },
ecce5c3c
PM
3623 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3624 .access = PL1_RW,
3625 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3626 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3627 .access = PL1_RW,
3628 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 3629 /* Protection region base and size registers */
e508a92b
PM
3630 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3631 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3632 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3633 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3634 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3635 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3636 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3637 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3638 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3639 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3640 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3641 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3642 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3643 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3644 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3645 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3646 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3647 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3648 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3649 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3650 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3651 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3652 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3653 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
3654};
3655
c4241c7d
PM
3656static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3657 uint64_t value)
ecce5c3c 3658{
11f136ee 3659 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
3660 int maskshift = extract32(value, 0, 3);
3661
e389be16
FA
3662 if (!arm_feature(env, ARM_FEATURE_V8)) {
3663 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3664 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3665 * using Long-desciptor translation table format */
3666 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3667 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3668 /* In an implementation that includes the Security Extensions
3669 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3670 * Short-descriptor translation table format.
3671 */
3672 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3673 } else {
3674 value &= TTBCR_N;
3675 }
e42c4db3 3676 }
e389be16 3677
b6af0975 3678 /* Update the masks corresponding to the TCR bank being written
11f136ee 3679 * Note that we always calculate mask and base_mask, but
e42c4db3 3680 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
3681 * for long-descriptor tables the TCR fields are used differently
3682 * and the mask and base_mask values are meaningless.
e42c4db3 3683 */
11f136ee
FA
3684 tcr->raw_tcr = value;
3685 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3686 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
3687}
3688
c4241c7d
PM
3689static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3690 uint64_t value)
d4e6df63 3691{
2fc0cc0e 3692 ARMCPU *cpu = env_archcpu(env);
ab638a32 3693 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3694
d4e6df63
PM
3695 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3696 /* With LPAE the TTBCR could result in a change of ASID
3697 * via the TTBCR.A1 bit, so do a TLB flush.
3698 */
d10eb08f 3699 tlb_flush(CPU(cpu));
d4e6df63 3700 }
ab638a32
RH
3701 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3702 value = deposit64(tcr->raw_tcr, 0, 32, value);
c4241c7d 3703 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
3704}
3705
ecce5c3c
PM
3706static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3707{
11f136ee
FA
3708 TCR *tcr = raw_ptr(env, ri);
3709
3710 /* Reset both the TCR as well as the masks corresponding to the bank of
3711 * the TCR being reset.
3712 */
3713 tcr->raw_tcr = 0;
3714 tcr->mask = 0;
3715 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
3716}
3717
d06dc933 3718static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
cb2e37df
PM
3719 uint64_t value)
3720{
2fc0cc0e 3721 ARMCPU *cpu = env_archcpu(env);
11f136ee 3722 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3723
cb2e37df 3724 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 3725 tlb_flush(CPU(cpu));
11f136ee 3726 tcr->raw_tcr = value;
cb2e37df
PM
3727}
3728
327ed10f
PM
3729static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3730 uint64_t value)
3731{
93f379b0
RH
3732 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3733 if (cpreg_field_is_64bit(ri) &&
3734 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
2fc0cc0e 3735 ARMCPU *cpu = env_archcpu(env);
d10eb08f 3736 tlb_flush(CPU(cpu));
327ed10f
PM
3737 }
3738 raw_write(env, ri, value);
3739}
3740
ed30da8e
RH
3741static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3742 uint64_t value)
3743{
d06dc933
RH
3744 /*
3745 * If we are running with E2&0 regime, then an ASID is active.
3746 * Flush if that might be changing. Note we're not checking
3747 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3748 * holds the active ASID, only checking the field that might.
3749 */
3750 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3751 (arm_hcr_el2_eff(env) & HCR_E2H)) {
b6ad6062
RDC
3752 uint16_t mask = ARMMMUIdxBit_E20_2 |
3753 ARMMMUIdxBit_E20_2_PAN |
3754 ARMMMUIdxBit_E20_0;
3755
3756 if (arm_is_secure_below_el3(env)) {
3757 mask >>= ARM_MMU_IDX_A_NS;
3758 }
3759
3760 tlb_flush_by_mmuidx(env_cpu(env), mask);
d06dc933 3761 }
ed30da8e
RH
3762 raw_write(env, ri, value);
3763}
3764
b698e9cf
EI
3765static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3766 uint64_t value)
3767{
2fc0cc0e 3768 ARMCPU *cpu = env_archcpu(env);
b698e9cf
EI
3769 CPUState *cs = CPU(cpu);
3770
97fa9350
RH
3771 /*
3772 * A change in VMID to the stage2 page table (Stage2) invalidates
3773 * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3774 */
b698e9cf 3775 if (raw_read(env, ri) != value) {
c4f060e8
RDC
3776 uint16_t mask = ARMMMUIdxBit_E10_1 |
3777 ARMMMUIdxBit_E10_1_PAN |
3778 ARMMMUIdxBit_E10_0;
3779
3780 if (arm_is_secure_below_el3(env)) {
3781 mask >>= ARM_MMU_IDX_A_NS;
3782 }
3783
3784 tlb_flush_by_mmuidx(cs, mask);
b698e9cf
EI
3785 raw_write(env, ri, value);
3786 }
3787}
3788
8e5d75c9 3789static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 3790 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218 3791 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4a7e2d73 3792 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 3793 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 3794 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
84929218 3795 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
88ca1c2d
FA
3796 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3797 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9 3798 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
84929218 3799 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
8e5d75c9
PC
3800 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3801 offsetof(CPUARMState, cp15.dfar_ns) } },
3802 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3803 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218
RH
3804 .access = PL1_RW, .accessfn = access_tvm_trvm,
3805 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
8e5d75c9 3806 .resetvalue = 0, },
8e5d75c9
PC
3807};
3808
3809static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
3810 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3811 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
84929218 3812 .access = PL1_RW, .accessfn = access_tvm_trvm,
d81c519c 3813 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 3814 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 3815 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
84929218
RH
3816 .access = PL1_RW, .accessfn = access_tvm_trvm,
3817 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
3818 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3819 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 3820 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 3821 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
84929218
RH
3822 .access = PL1_RW, .accessfn = access_tvm_trvm,
3823 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
3824 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3825 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
3826 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3827 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
3828 .access = PL1_RW, .accessfn = access_tvm_trvm,
3829 .writefn = vmsa_tcr_el12_write,
cb2e37df 3830 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 3831 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 3832 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
3833 .access = PL1_RW, .accessfn = access_tvm_trvm,
3834 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 3835 .raw_writefn = vmsa_ttbcr_raw_write,
d102058e
RH
3836 /* No offsetoflow32 -- pass the entire TCR to writefn/raw_writefn. */
3837 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.tcr_el[3]),
3838 offsetof(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
3839};
3840
ab638a32
RH
3841/* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3842 * qemu tlbs nor adjusting cached masks.
3843 */
3844static const ARMCPRegInfo ttbcr2_reginfo = {
3845 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
84929218
RH
3846 .access = PL1_RW, .accessfn = access_tvm_trvm,
3847 .type = ARM_CP_ALIAS,
d102058e
RH
3848 .bank_fieldoffsets = {
3849 offsetofhigh32(CPUARMState, cp15.tcr_el[3].raw_tcr),
3850 offsetofhigh32(CPUARMState, cp15.tcr_el[1].raw_tcr),
3851 },
ab638a32
RH
3852};
3853
c4241c7d
PM
3854static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3855 uint64_t value)
1047b9d7
PM
3856{
3857 env->cp15.c15_ticonfig = value & 0xe7;
3858 /* The OS_TYPE bit in this register changes the reported CPUID! */
3859 env->cp15.c0_cpuid = (value & (1 << 5)) ?
3860 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
3861}
3862
c4241c7d
PM
3863static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3864 uint64_t value)
1047b9d7
PM
3865{
3866 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
3867}
3868
c4241c7d
PM
3869static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3870 uint64_t value)
1047b9d7
PM
3871{
3872 /* Wait-for-interrupt (deprecated) */
2fc0cc0e 3873 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
1047b9d7
PM
3874}
3875
c4241c7d
PM
3876static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3877 uint64_t value)
c4804214
PM
3878{
3879 /* On OMAP there are registers indicating the max/min index of dcache lines
3880 * containing a dirty line; cache flush operations have to reset these.
3881 */
3882 env->cp15.c15_i_max = 0x000;
3883 env->cp15.c15_i_min = 0xff0;
c4804214
PM
3884}
3885
18032bec
PM
3886static const ARMCPRegInfo omap_cp_reginfo[] = {
3887 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3888 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 3889 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 3890 .resetvalue = 0, },
1047b9d7
PM
3891 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3892 .access = PL1_RW, .type = ARM_CP_NOP },
3893 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3894 .access = PL1_RW,
3895 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3896 .writefn = omap_ticonfig_write },
3897 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3898 .access = PL1_RW,
3899 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3900 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3901 .access = PL1_RW, .resetvalue = 0xff0,
3902 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3903 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3904 .access = PL1_RW,
3905 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3906 .writefn = omap_threadid_write },
3907 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3908 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 3909 .type = ARM_CP_NO_RAW,
1047b9d7
PM
3910 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3911 /* TODO: Peripheral port remap register:
3912 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3913 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3914 * when MMU is off.
3915 */
c4804214 3916 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 3917 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 3918 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 3919 .writefn = omap_cachemaint_write },
34f90529
PM
3920 { .name = "C9", .cp = 15, .crn = 9,
3921 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3922 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
3923};
3924
c4241c7d
PM
3925static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3926 uint64_t value)
1047b9d7 3927{
c0f4af17 3928 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
3929}
3930
3931static const ARMCPRegInfo xscale_cp_reginfo[] = {
3932 { .name = "XSCALE_CPAR",
3933 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3934 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3935 .writefn = xscale_cpar_write, },
2771db27
PM
3936 { .name = "XSCALE_AUXCR",
3937 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3938 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3939 .resetvalue = 0, },
3b771579
PM
3940 /* XScale specific cache-lockdown: since we have no cache we NOP these
3941 * and hope the guest does not really rely on cache behaviour.
3942 */
3943 { .name = "XSCALE_LOCK_ICACHE_LINE",
3944 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3945 .access = PL1_W, .type = ARM_CP_NOP },
3946 { .name = "XSCALE_UNLOCK_ICACHE",
3947 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3948 .access = PL1_W, .type = ARM_CP_NOP },
3949 { .name = "XSCALE_DCACHE_LOCK",
3950 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3951 .access = PL1_RW, .type = ARM_CP_NOP },
3952 { .name = "XSCALE_UNLOCK_DCACHE",
3953 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3954 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
3955};
3956
3957static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3958 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3959 * implementation of this implementation-defined space.
3960 * Ideally this should eventually disappear in favour of actually
3961 * implementing the correct behaviour for all cores.
3962 */
3963 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3964 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 3965 .access = PL1_RW,
7a0e58fa 3966 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 3967 .resetvalue = 0 },
18032bec
PM
3968};
3969
c4804214
PM
3970static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3971 /* Cache status: RAZ because we have no cache so it's always clean */
3972 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 3973 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 3974 .resetvalue = 0 },
c4804214
PM
3975};
3976
3977static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
3978 /* We never have a a block transfer operation in progress */
3979 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 3980 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 3981 .resetvalue = 0 },
30b05bba
PM
3982 /* The cache ops themselves: these all NOP for QEMU */
3983 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
3984 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3985 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
3986 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3987 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
3988 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3989 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
3990 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3991 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
3992 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3993 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
3994 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
3995};
3996
3997static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
3998 /* The cache test-and-clean instructions always return (1 << 30)
3999 * to indicate that there are no dirty cache lines.
4000 */
4001 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 4002 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4003 .resetvalue = (1 << 30) },
c4804214 4004 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 4005 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4006 .resetvalue = (1 << 30) },
c4804214
PM
4007};
4008
34f90529
PM
4009static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4010 /* Ignore ReadBuffer accesses */
4011 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4012 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 4013 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 4014 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
4015};
4016
731de9e6
EI
4017static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4018{
731de9e6 4019 unsigned int cur_el = arm_current_el(env);
731de9e6 4020
e6ef0169 4021 if (arm_is_el2_enabled(env) && cur_el == 1) {
731de9e6
EI
4022 return env->cp15.vpidr_el2;
4023 }
4024 return raw_read(env, ri);
4025}
4026
06a7e647 4027static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 4028{
2fc0cc0e 4029 ARMCPU *cpu = env_archcpu(env);
eb5e1d3c
PF
4030 uint64_t mpidr = cpu->mp_affinity;
4031
81bdde9d 4032 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 4033 mpidr |= (1U << 31);
81bdde9d
PM
4034 /* Cores which are uniprocessor (non-coherent)
4035 * but still implement the MP extensions set
a8e81b31 4036 * bit 30. (For instance, Cortex-R5).
81bdde9d 4037 */
a8e81b31
PC
4038 if (cpu->mp_is_up) {
4039 mpidr |= (1u << 30);
4040 }
81bdde9d 4041 }
c4241c7d 4042 return mpidr;
81bdde9d
PM
4043}
4044
06a7e647
EI
4045static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4046{
f0d574d6 4047 unsigned int cur_el = arm_current_el(env);
f0d574d6 4048
e6ef0169 4049 if (arm_is_el2_enabled(env) && cur_el == 1) {
f0d574d6
EI
4050 return env->cp15.vmpidr_el2;
4051 }
06a7e647
EI
4052 return mpidr_read_val(env);
4053}
4054
7ac681cf 4055static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 4056 /* NOP AMAIR0/1 */
b0fe2427
PM
4057 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4058 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
84929218
RH
4059 .access = PL1_RW, .accessfn = access_tvm_trvm,
4060 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427 4061 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 4062 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
84929218
RH
4063 .access = PL1_RW, .accessfn = access_tvm_trvm,
4064 .type = ARM_CP_CONST, .resetvalue = 0 },
891a2fe7 4065 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
4066 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4067 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4068 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 4069 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
84929218
RH
4070 .access = PL1_RW, .accessfn = access_tvm_trvm,
4071 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4072 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4073 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 4074 .writefn = vmsa_ttbr_write, },
891a2fe7 4075 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
84929218
RH
4076 .access = PL1_RW, .accessfn = access_tvm_trvm,
4077 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4078 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4079 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 4080 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
4081};
4082
c4241c7d 4083static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4084{
c4241c7d 4085 return vfp_get_fpcr(env);
b0d2b7d0
PM
4086}
4087
c4241c7d
PM
4088static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4089 uint64_t value)
b0d2b7d0
PM
4090{
4091 vfp_set_fpcr(env, value);
b0d2b7d0
PM
4092}
4093
c4241c7d 4094static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4095{
c4241c7d 4096 return vfp_get_fpsr(env);
b0d2b7d0
PM
4097}
4098
c4241c7d
PM
4099static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4100 uint64_t value)
b0d2b7d0
PM
4101{
4102 vfp_set_fpsr(env, value);
b0d2b7d0
PM
4103}
4104
3f208fd7
PM
4105static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4106 bool isread)
c2b820fe 4107{
aaec1432 4108 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
c2b820fe
PM
4109 return CP_ACCESS_TRAP;
4110 }
4111 return CP_ACCESS_OK;
4112}
4113
4114static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4115 uint64_t value)
4116{
4117 env->daif = value & PSTATE_DAIF;
4118}
4119
220f508f
RH
4120static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4121{
4122 return env->pstate & PSTATE_PAN;
4123}
4124
4125static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4126 uint64_t value)
4127{
4128 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4129}
4130
4131static const ARMCPRegInfo pan_reginfo = {
4132 .name = "PAN", .state = ARM_CP_STATE_AA64,
4133 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4134 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4135 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4136};
4137
9eeb7a1c
RH
4138static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4139{
4140 return env->pstate & PSTATE_UAO;
4141}
4142
4143static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4144 uint64_t value)
4145{
4146 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4147}
4148
4149static const ARMCPRegInfo uao_reginfo = {
4150 .name = "UAO", .state = ARM_CP_STATE_AA64,
4151 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4152 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4153 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4154};
4155
dc8b1853
RC
4156static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4157{
4158 return env->pstate & PSTATE_DIT;
4159}
4160
4161static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4162 uint64_t value)
4163{
4164 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4165}
4166
4167static const ARMCPRegInfo dit_reginfo = {
4168 .name = "DIT", .state = ARM_CP_STATE_AA64,
4169 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4170 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4171 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4172};
4173
f2f68a78
RC
4174static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4175{
4176 return env->pstate & PSTATE_SSBS;
4177}
4178
4179static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4180 uint64_t value)
4181{
4182 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4183}
4184
4185static const ARMCPRegInfo ssbs_reginfo = {
4186 .name = "SSBS", .state = ARM_CP_STATE_AA64,
4187 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4188 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4189 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4190};
4191
38262d8a
RH
4192static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4193 const ARMCPRegInfo *ri,
4194 bool isread)
8af35c37 4195{
38262d8a
RH
4196 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4197 switch (arm_current_el(env)) {
4198 case 0:
4199 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4200 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4201 return CP_ACCESS_TRAP;
4202 }
4203 /* fall through */
4204 case 1:
4205 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4206 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4207 return CP_ACCESS_TRAP_EL2;
4208 }
4209 break;
8af35c37
PM
4210 }
4211 return CP_ACCESS_OK;
4212}
4213
38262d8a 4214static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
1bed4d2e
RH
4215 const ARMCPRegInfo *ri,
4216 bool isread)
4217{
38262d8a 4218 /* Cache invalidate/clean to Point of Unification... */
1bed4d2e
RH
4219 switch (arm_current_el(env)) {
4220 case 0:
4221 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4222 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4223 return CP_ACCESS_TRAP;
4224 }
4225 /* fall through */
4226 case 1:
38262d8a
RH
4227 /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set. */
4228 if (arm_hcr_el2_eff(env) & HCR_TPU) {
1bed4d2e
RH
4229 return CP_ACCESS_TRAP_EL2;
4230 }
4231 break;
4232 }
4233 return CP_ACCESS_OK;
4234}
4235
dbb1fb27
AB
4236/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4237 * Page D4-1736 (DDI0487A.b)
4238 */
4239
b7e0730d
RH
4240static int vae1_tlbmask(CPUARMState *env)
4241{
e04a5752 4242 uint64_t hcr = arm_hcr_el2_eff(env);
bc944d3a 4243 uint16_t mask;
e04a5752
RDC
4244
4245 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
bc944d3a
RDC
4246 mask = ARMMMUIdxBit_E20_2 |
4247 ARMMMUIdxBit_E20_2_PAN |
4248 ARMMMUIdxBit_E20_0;
b7e0730d 4249 } else {
bc944d3a 4250 mask = ARMMMUIdxBit_E10_1 |
452ef8cb
RH
4251 ARMMMUIdxBit_E10_1_PAN |
4252 ARMMMUIdxBit_E10_0;
b7e0730d 4253 }
bc944d3a
RDC
4254
4255 if (arm_is_secure_below_el3(env)) {
4256 mask >>= ARM_MMU_IDX_A_NS;
4257 }
4258
4259 return mask;
b7e0730d
RH
4260}
4261
ea04dce7
RH
4262/* Return 56 if TBI is enabled, 64 otherwise. */
4263static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4264 uint64_t addr)
4265{
4266 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
4267 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4268 int select = extract64(addr, 55, 1);
4269
4270 return (tbi >> select) & 1 ? 56 : 64;
4271}
4272
4273static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4274{
b6ad6062 4275 uint64_t hcr = arm_hcr_el2_eff(env);
ea04dce7
RH
4276 ARMMMUIdx mmu_idx;
4277
4278 /* Only the regime of the mmu_idx below is significant. */
b6ad6062 4279 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
ea04dce7
RH
4280 mmu_idx = ARMMMUIdx_E20_0;
4281 } else {
4282 mmu_idx = ARMMMUIdx_E10_0;
4283 }
b6ad6062
RDC
4284
4285 if (arm_is_secure_below_el3(env)) {
4286 mmu_idx &= ~ARM_MMU_IDX_A_NS;
4287 }
4288
ea04dce7
RH
4289 return tlbbits_for_regime(env, mmu_idx, addr);
4290}
4291
fd3ed969
PM
4292static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4293 uint64_t value)
168aa23b 4294{
29a0af61 4295 CPUState *cs = env_cpu(env);
b7e0730d 4296 int mask = vae1_tlbmask(env);
dbb1fb27 4297
b7e0730d 4298 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
168aa23b
PM
4299}
4300
b4ab8ce9
PM
4301static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4302 uint64_t value)
4303{
29a0af61 4304 CPUState *cs = env_cpu(env);
b7e0730d 4305 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4306
4307 if (tlb_force_broadcast(env)) {
527db2be
RH
4308 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4309 } else {
4310 tlb_flush_by_mmuidx(cs, mask);
b4ab8ce9 4311 }
b4ab8ce9
PM
4312}
4313
90c19cdf 4314static int alle1_tlbmask(CPUARMState *env)
168aa23b 4315{
90c19cdf
RH
4316 /*
4317 * Note that the 'ALL' scope must invalidate both stage 1 and
fd3ed969
PM
4318 * stage 2 translations, whereas most other scopes only invalidate
4319 * stage 1 translations.
4320 */
fd3ed969 4321 if (arm_is_secure_below_el3(env)) {
452ef8cb
RH
4322 return ARMMMUIdxBit_SE10_1 |
4323 ARMMMUIdxBit_SE10_1_PAN |
4324 ARMMMUIdxBit_SE10_0;
fd3ed969 4325 } else {
452ef8cb
RH
4326 return ARMMMUIdxBit_E10_1 |
4327 ARMMMUIdxBit_E10_1_PAN |
4328 ARMMMUIdxBit_E10_0;
fd3ed969 4329 }
168aa23b
PM
4330}
4331
85d0dc9f
RH
4332static int e2_tlbmask(CPUARMState *env)
4333{
b6ad6062
RDC
4334 if (arm_is_secure_below_el3(env)) {
4335 return ARMMMUIdxBit_SE20_0 |
4336 ARMMMUIdxBit_SE20_2 |
4337 ARMMMUIdxBit_SE20_2_PAN |
4338 ARMMMUIdxBit_SE2;
4339 } else {
4340 return ARMMMUIdxBit_E20_0 |
4341 ARMMMUIdxBit_E20_2 |
4342 ARMMMUIdxBit_E20_2_PAN |
4343 ARMMMUIdxBit_E2;
4344 }
85d0dc9f
RH
4345}
4346
90c19cdf
RH
4347static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4348 uint64_t value)
4349{
4350 CPUState *cs = env_cpu(env);
4351 int mask = alle1_tlbmask(env);
4352
4353 tlb_flush_by_mmuidx(cs, mask);
4354}
4355
fd3ed969 4356static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
4357 uint64_t value)
4358{
85d0dc9f
RH
4359 CPUState *cs = env_cpu(env);
4360 int mask = e2_tlbmask(env);
fd3ed969 4361
85d0dc9f 4362 tlb_flush_by_mmuidx(cs, mask);
fd3ed969
PM
4363}
4364
43efaa33
PM
4365static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4366 uint64_t value)
4367{
2fc0cc0e 4368 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4369 CPUState *cs = CPU(cpu);
4370
127b2b08 4371 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4372}
4373
fd3ed969
PM
4374static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4375 uint64_t value)
4376{
29a0af61 4377 CPUState *cs = env_cpu(env);
90c19cdf
RH
4378 int mask = alle1_tlbmask(env);
4379
4380 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
fa439fc5
PM
4381}
4382
2bfb9d75
PM
4383static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4384 uint64_t value)
4385{
29a0af61 4386 CPUState *cs = env_cpu(env);
85d0dc9f 4387 int mask = e2_tlbmask(env);
2bfb9d75 4388
85d0dc9f 4389 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
2bfb9d75
PM
4390}
4391
43efaa33
PM
4392static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4393 uint64_t value)
4394{
29a0af61 4395 CPUState *cs = env_cpu(env);
43efaa33 4396
127b2b08 4397 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4398}
4399
fd3ed969
PM
4400static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4401 uint64_t value)
fa439fc5 4402{
fd3ed969
PM
4403 /* Invalidate by VA, EL2
4404 * Currently handles both VAE2 and VALE2, since we don't support
4405 * flush-last-level-only.
4406 */
85d0dc9f
RH
4407 CPUState *cs = env_cpu(env);
4408 int mask = e2_tlbmask(env);
fd3ed969
PM
4409 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4410
85d0dc9f 4411 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
fd3ed969
PM
4412}
4413
43efaa33
PM
4414static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4415 uint64_t value)
4416{
4417 /* Invalidate by VA, EL3
4418 * Currently handles both VAE3 and VALE3, since we don't support
4419 * flush-last-level-only.
4420 */
2fc0cc0e 4421 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4422 CPUState *cs = CPU(cpu);
4423 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4424
127b2b08 4425 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
43efaa33
PM
4426}
4427
fd3ed969
PM
4428static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4429 uint64_t value)
4430{
90c19cdf
RH
4431 CPUState *cs = env_cpu(env);
4432 int mask = vae1_tlbmask(env);
fa439fc5 4433 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4434 int bits = vae1_tlbbits(env, pageaddr);
fa439fc5 4435
ea04dce7 4436 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
fa439fc5
PM
4437}
4438
b4ab8ce9
PM
4439static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4440 uint64_t value)
4441{
4442 /* Invalidate by VA, EL1&0 (AArch64 version).
4443 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4444 * since we don't support flush-for-specific-ASID-only or
4445 * flush-last-level-only.
4446 */
90c19cdf
RH
4447 CPUState *cs = env_cpu(env);
4448 int mask = vae1_tlbmask(env);
b4ab8ce9 4449 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4450 int bits = vae1_tlbbits(env, pageaddr);
b4ab8ce9
PM
4451
4452 if (tlb_force_broadcast(env)) {
ea04dce7 4453 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
527db2be 4454 } else {
ea04dce7 4455 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
b4ab8ce9 4456 }
b4ab8ce9
PM
4457}
4458
fd3ed969
PM
4459static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4460 uint64_t value)
fa439fc5 4461{
29a0af61 4462 CPUState *cs = env_cpu(env);
fd3ed969 4463 uint64_t pageaddr = sextract64(value << 12, 0, 56);
b6ad6062
RDC
4464 bool secure = arm_is_secure_below_el3(env);
4465 int mask = secure ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2;
eb849d8f 4466 int bits = tlbbits_for_regime(env, secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2,
b6ad6062 4467 pageaddr);
fa439fc5 4468
b6ad6062 4469 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
fa439fc5
PM
4470}
4471
43efaa33
PM
4472static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4473 uint64_t value)
4474{
29a0af61 4475 CPUState *cs = env_cpu(env);
43efaa33 4476 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4477 int bits = tlbbits_for_regime(env, ARMMMUIdx_SE3, pageaddr);
43efaa33 4478
ea04dce7
RH
4479 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4480 ARMMMUIdxBit_SE3, bits);
43efaa33
PM
4481}
4482
84940ed8 4483#ifdef TARGET_AARCH64
ab1cdb47
RH
4484typedef struct {
4485 uint64_t base;
84940ed8 4486 uint64_t length;
ab1cdb47
RH
4487} TLBIRange;
4488
4489static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4490 uint64_t value)
4491{
4492 unsigned int page_size_granule, page_shift, num, scale, exponent;
3974ff93
RH
4493 /* Extract one bit to represent the va selector in use. */
4494 uint64_t select = sextract64(value, 36, 1);
4495 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
ab1cdb47 4496 TLBIRange ret = { };
84940ed8 4497
84940ed8
RC
4498 page_size_granule = extract64(value, 46, 2);
4499
3974ff93
RH
4500 /* The granule encoded in value must match the granule in use. */
4501 if (page_size_granule != (param.using64k ? 3 : param.using16k ? 2 : 1)) {
4502 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
84940ed8 4503 page_size_granule);
ab1cdb47 4504 return ret;
84940ed8
RC
4505 }
4506
52a9f609 4507 page_shift = (page_size_granule - 1) * 2 + 12;
ab1cdb47
RH
4508 num = extract64(value, 39, 5);
4509 scale = extract64(value, 44, 2);
84940ed8 4510 exponent = (5 * scale) + 1;
84940ed8 4511
ab1cdb47 4512 ret.length = (num + 1) << (exponent + page_shift);
84940ed8 4513
3974ff93 4514 if (param.select) {
d976de21 4515 ret.base = sextract64(value, 0, 37);
84940ed8 4516 } else {
d976de21 4517 ret.base = extract64(value, 0, 37);
84940ed8 4518 }
ef56c242
RH
4519 if (param.ds) {
4520 /*
4521 * With DS=1, BaseADDR is always shifted 16 so that it is able
4522 * to address all 52 va bits. The input address is perforce
4523 * aligned on a 64k boundary regardless of translation granule.
4524 */
4525 page_shift = 16;
4526 }
d976de21 4527 ret.base <<= page_shift;
84940ed8 4528
ab1cdb47 4529 return ret;
84940ed8
RC
4530}
4531
4532static void do_rvae_write(CPUARMState *env, uint64_t value,
4533 int idxmap, bool synced)
4534{
4535 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
ab1cdb47 4536 TLBIRange range;
84940ed8
RC
4537 int bits;
4538
ab1cdb47
RH
4539 range = tlbi_aa64_get_range(env, one_idx, value);
4540 bits = tlbbits_for_regime(env, one_idx, range.base);
84940ed8
RC
4541
4542 if (synced) {
4543 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
ab1cdb47
RH
4544 range.base,
4545 range.length,
84940ed8
RC
4546 idxmap,
4547 bits);
4548 } else {
ab1cdb47
RH
4549 tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4550 range.length, idxmap, bits);
84940ed8
RC
4551 }
4552}
4553
4554static void tlbi_aa64_rvae1_write(CPUARMState *env,
4555 const ARMCPRegInfo *ri,
4556 uint64_t value)
4557{
4558 /*
4559 * Invalidate by VA range, EL1&0.
4560 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4561 * since we don't support flush-for-specific-ASID-only or
4562 * flush-last-level-only.
4563 */
4564
4565 do_rvae_write(env, value, vae1_tlbmask(env),
4566 tlb_force_broadcast(env));
4567}
4568
4569static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4570 const ARMCPRegInfo *ri,
4571 uint64_t value)
4572{
4573 /*
4574 * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4575 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4576 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4577 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4578 * shareable specific flushes.
4579 */
4580
4581 do_rvae_write(env, value, vae1_tlbmask(env), true);
4582}
4583
4584static int vae2_tlbmask(CPUARMState *env)
4585{
4586 return (arm_is_secure_below_el3(env)
4587 ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2);
4588}
4589
4590static void tlbi_aa64_rvae2_write(CPUARMState *env,
4591 const ARMCPRegInfo *ri,
4592 uint64_t value)
4593{
4594 /*
4595 * Invalidate by VA range, EL2.
4596 * Currently handles all of RVAE2 and RVALE2,
4597 * since we don't support flush-for-specific-ASID-only or
4598 * flush-last-level-only.
4599 */
4600
4601 do_rvae_write(env, value, vae2_tlbmask(env),
4602 tlb_force_broadcast(env));
4603
4604
4605}
4606
4607static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4608 const ARMCPRegInfo *ri,
4609 uint64_t value)
4610{
4611 /*
4612 * Invalidate by VA range, Inner/Outer Shareable, EL2.
4613 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4614 * since we don't support flush-for-specific-ASID-only,
4615 * flush-last-level-only or inner/outer shareable specific flushes.
4616 */
4617
4618 do_rvae_write(env, value, vae2_tlbmask(env), true);
4619
4620}
4621
4622static void tlbi_aa64_rvae3_write(CPUARMState *env,
4623 const ARMCPRegInfo *ri,
4624 uint64_t value)
4625{
4626 /*
4627 * Invalidate by VA range, EL3.
4628 * Currently handles all of RVAE3 and RVALE3,
4629 * since we don't support flush-for-specific-ASID-only or
4630 * flush-last-level-only.
4631 */
4632
4633 do_rvae_write(env, value, ARMMMUIdxBit_SE3,
4634 tlb_force_broadcast(env));
4635}
4636
4637static void tlbi_aa64_rvae3is_write(CPUARMState *env,
4638 const ARMCPRegInfo *ri,
4639 uint64_t value)
4640{
4641 /*
4642 * Invalidate by VA range, EL3, Inner/Outer Shareable.
4643 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
4644 * since we don't support flush-for-specific-ASID-only,
4645 * flush-last-level-only or inner/outer specific flushes.
4646 */
4647
4648 do_rvae_write(env, value, ARMMMUIdxBit_SE3, true);
4649}
4650#endif
4651
3f208fd7
PM
4652static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4653 bool isread)
aca3f40b 4654{
4351cb72
RH
4655 int cur_el = arm_current_el(env);
4656
4657 if (cur_el < 2) {
4658 uint64_t hcr = arm_hcr_el2_eff(env);
4659
4660 if (cur_el == 0) {
4661 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4662 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4663 return CP_ACCESS_TRAP_EL2;
4664 }
4665 } else {
4666 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4667 return CP_ACCESS_TRAP;
4668 }
4669 if (hcr & HCR_TDZ) {
4670 return CP_ACCESS_TRAP_EL2;
4671 }
4672 }
4673 } else if (hcr & HCR_TDZ) {
4674 return CP_ACCESS_TRAP_EL2;
4675 }
aca3f40b
PM
4676 }
4677 return CP_ACCESS_OK;
4678}
4679
4680static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4681{
2fc0cc0e 4682 ARMCPU *cpu = env_archcpu(env);
aca3f40b
PM
4683 int dzp_bit = 1 << 4;
4684
4685 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 4686 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
4687 dzp_bit = 0;
4688 }
4689 return cpu->dcz_blocksize | dzp_bit;
4690}
4691
3f208fd7
PM
4692static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4693 bool isread)
f502cfc2 4694{
cdcf1405 4695 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
4696 /* Access to SP_EL0 is undefined if it's being used as
4697 * the stack pointer.
4698 */
4699 return CP_ACCESS_TRAP_UNCATEGORIZED;
4700 }
4701 return CP_ACCESS_OK;
4702}
4703
4704static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4705{
4706 return env->pstate & PSTATE_SP;
4707}
4708
4709static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4710{
4711 update_spsel(env, val);
4712}
4713
137feaa9
FA
4714static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4715 uint64_t value)
4716{
2fc0cc0e 4717 ARMCPU *cpu = env_archcpu(env);
137feaa9 4718
f00faf13
RH
4719 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4720 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4721 value &= ~SCTLR_M;
4722 }
4723
4724 /* ??? Lots of these bits are not implemented. */
4725
4726 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4727 if (ri->opc1 == 6) { /* SCTLR_EL3 */
4728 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4729 } else {
4730 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4731 SCTLR_ATA0 | SCTLR_ATA);
4732 }
4733 }
4734
137feaa9
FA
4735 if (raw_read(env, ri) == value) {
4736 /* Skip the TLB flush if nothing actually changed; Linux likes
4737 * to do a lot of pointless SCTLR writes.
4738 */
4739 return;
4740 }
4741
4742 raw_write(env, ri, value);
f00faf13 4743
137feaa9 4744 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 4745 tlb_flush(CPU(cpu));
2e5dcf36
RH
4746
4747 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4748 /*
4749 * Normally we would always end the TB on an SCTLR write; see the
4750 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4751 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4752 * of hflags from the translator, so do it here.
4753 */
4754 arm_rebuild_hflags(env);
4755 }
137feaa9
FA
4756}
4757
a8d64e73
PM
4758static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4759 uint64_t value)
4760{
4761 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4762}
4763
b0d2b7d0
PM
4764static const ARMCPRegInfo v8_cp_reginfo[] = {
4765 /* Minimal set of EL0-visible registers. This will need to be expanded
4766 * significantly for system emulation of AArch64 CPUs.
4767 */
4768 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4769 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4770 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
4771 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4772 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 4773 .type = ARM_CP_NO_RAW,
c2b820fe
PM
4774 .access = PL0_RW, .accessfn = aa64_daif_access,
4775 .fieldoffset = offsetof(CPUARMState, daif),
4776 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
4777 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4778 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 4779 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4780 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
4781 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4782 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 4783 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4784 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
4785 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4786 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 4787 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
4788 .readfn = aa64_dczid_read },
4789 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4790 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4791 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4792#ifndef CONFIG_USER_ONLY
4793 /* Avoid overhead of an access check that always passes in user-mode */
4794 .accessfn = aa64_zva_access,
4795#endif
4796 },
0eef9d98
PM
4797 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4798 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4799 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
4800 /* Cache ops: all NOPs since we don't emulate caches */
4801 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4802 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a
RH
4803 .access = PL1_W, .type = ARM_CP_NOP,
4804 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4805 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4806 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a
RH
4807 .access = PL1_W, .type = ARM_CP_NOP,
4808 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4809 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4810 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4811 .access = PL0_W, .type = ARM_CP_NOP,
38262d8a 4812 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4813 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4814 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e
RH
4815 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4816 .type = ARM_CP_NOP },
8af35c37
PM
4817 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4818 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 4819 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
4820 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4821 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4822 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 4823 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
4824 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4825 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 4826 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
4827 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4828 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4829 .access = PL0_W, .type = ARM_CP_NOP,
38262d8a 4830 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4831 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4832 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4833 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 4834 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
4835 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4836 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 4837 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
168aa23b
PM
4838 /* TLBI operations */
4839 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4840 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
30881b73 4841 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4842 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4843 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4844 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
30881b73 4845 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4846 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4847 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4848 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
30881b73 4849 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4850 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4851 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4852 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
30881b73 4853 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4854 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4855 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4856 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
30881b73 4857 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4858 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4859 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4860 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
30881b73 4861 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4862 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4863 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4864 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73 4865 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4866 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4867 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4868 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73 4869 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4870 .writefn = tlbi_aa64_vae1_write },
168aa23b 4871 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4872 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73 4873 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4874 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4875 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4876 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73 4877 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4878 .writefn = tlbi_aa64_vae1_write },
168aa23b 4879 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4880 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73 4881 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4882 .writefn = tlbi_aa64_vae1_write },
168aa23b 4883 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4884 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73 4885 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4886 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
4887 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4888 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
bf05340c 4889 .access = PL2_W, .type = ARM_CP_NOP },
cea66e91
PM
4890 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4891 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
bf05340c 4892 .access = PL2_W, .type = ARM_CP_NOP },
83ddf975
PM
4893 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4894 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4895 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4896 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
4897 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4898 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4899 .access = PL2_W, .type = ARM_CP_NO_RAW,
4900 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
4901 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4902 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
bf05340c 4903 .access = PL2_W, .type = ARM_CP_NOP },
cea66e91
PM
4904 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4905 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
bf05340c 4906 .access = PL2_W, .type = ARM_CP_NOP },
83ddf975
PM
4907 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4908 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4909 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4910 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
4911 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4912 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4913 .access = PL2_W, .type = ARM_CP_NO_RAW,
4914 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
4915#ifndef CONFIG_USER_ONLY
4916 /* 64 bit address translation operations */
4917 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4918 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4919 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4920 .writefn = ats_write64 },
19525524
PM
4921 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4922 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4923 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4924 .writefn = ats_write64 },
19525524
PM
4925 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4926 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
0710b2fa
PM
4927 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4928 .writefn = ats_write64 },
19525524
PM
4929 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4930 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
0710b2fa
PM
4931 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4932 .writefn = ats_write64 },
2a47df95 4933 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 4934 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
0710b2fa
PM
4935 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4936 .writefn = ats_write64 },
2a47df95 4937 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 4938 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
0710b2fa
PM
4939 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4940 .writefn = ats_write64 },
2a47df95 4941 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 4942 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
0710b2fa
PM
4943 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4944 .writefn = ats_write64 },
2a47df95 4945 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 4946 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
0710b2fa
PM
4947 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4948 .writefn = ats_write64 },
2a47df95
PM
4949 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4950 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4951 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4952 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4953 .writefn = ats_write64 },
2a47df95
PM
4954 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4955 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4956 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4957 .writefn = ats_write64 },
c96fc9b5
EI
4958 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4959 .type = ARM_CP_ALIAS,
4960 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4961 .access = PL1_RW, .resetvalue = 0,
4962 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4963 .writefn = par_write },
19525524 4964#endif
995939a6 4965 /* TLB invalidate last level of translation table walk */
9449fdf6 4966 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
30881b73
RH
4967 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4968 .writefn = tlbimva_is_write },
9449fdf6 4969 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
30881b73 4970 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 4971 .writefn = tlbimvaa_is_write },
9449fdf6 4972 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73
RH
4973 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4974 .writefn = tlbimva_write },
9449fdf6 4975 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73
RH
4976 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4977 .writefn = tlbimvaa_write },
541ef8c2
SS
4978 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4979 .type = ARM_CP_NO_RAW, .access = PL2_W,
4980 .writefn = tlbimva_hyp_write },
4981 { .name = "TLBIMVALHIS",
4982 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4983 .type = ARM_CP_NO_RAW, .access = PL2_W,
4984 .writefn = tlbimva_hyp_is_write },
4985 { .name = "TLBIIPAS2",
4986 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
bf05340c 4987 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
4988 { .name = "TLBIIPAS2IS",
4989 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
bf05340c 4990 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
4991 { .name = "TLBIIPAS2L",
4992 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
bf05340c 4993 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
4994 { .name = "TLBIIPAS2LIS",
4995 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
bf05340c 4996 .type = ARM_CP_NOP, .access = PL2_W },
9449fdf6
PM
4997 /* 32 bit cache operations */
4998 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a 4999 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6
PM
5000 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5001 .type = ARM_CP_NOP, .access = PL1_W },
5002 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a 5003 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6 5004 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
38262d8a 5005 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6
PM
5006 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5007 .type = ARM_CP_NOP, .access = PL1_W },
5008 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5009 .type = ARM_CP_NOP, .access = PL1_W },
5010 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e 5011 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5012 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 5013 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5014 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
1bed4d2e 5015 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5016 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 5017 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5018 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
38262d8a 5019 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6 5020 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
1bed4d2e 5021 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5022 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 5023 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5024 /* MMU Domain access control / MPU write buffer control */
0c17d68c 5025 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
84929218 5026 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
5027 .writefn = dacr_write, .raw_writefn = raw_write,
5028 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5029 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 5030 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5031 .type = ARM_CP_ALIAS,
a0618a19 5032 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
5033 .access = PL1_RW,
5034 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 5035 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5036 .type = ARM_CP_ALIAS,
a65f1de9 5037 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5038 .access = PL1_RW,
5039 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
5040 /* We rely on the access checks not allowing the guest to write to the
5041 * state field when SPSel indicates that it's being used as the stack
5042 * pointer.
5043 */
5044 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5045 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5046 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 5047 .type = ARM_CP_ALIAS,
f502cfc2 5048 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
5049 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5050 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5051 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 5052 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
5053 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5054 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 5055 .type = ARM_CP_NO_RAW,
f502cfc2 5056 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
5057 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5058 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
696ba377
RH
5059 .access = PL2_RW,
5060 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
a4c88675 5061 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
6a43e0b6
PM
5062 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5063 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
696ba377 5064 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6a43e0b6
PM
5065 .writefn = dacr_write, .raw_writefn = raw_write,
5066 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5067 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5068 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
696ba377 5069 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6a43e0b6
PM
5070 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5071 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5072 .type = ARM_CP_ALIAS,
5073 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5074 .access = PL2_RW,
5075 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5076 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5077 .type = ARM_CP_ALIAS,
5078 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5079 .access = PL2_RW,
5080 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5081 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5082 .type = ARM_CP_ALIAS,
5083 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5084 .access = PL2_RW,
5085 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5086 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5087 .type = ARM_CP_ALIAS,
5088 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5089 .access = PL2_RW,
5090 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73
PM
5091 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5092 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5093 .resetvalue = 0,
5094 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5095 { .name = "SDCR", .type = ARM_CP_ALIAS,
5096 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5097 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5098 .writefn = sdcr_write,
5099 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
5100};
5101
d1fb4da2 5102static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
f149e3e8 5103{
2fc0cc0e 5104 ARMCPU *cpu = env_archcpu(env);
d1fb4da2
RH
5105
5106 if (arm_feature(env, ARM_FEATURE_V8)) {
5107 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5108 } else {
5109 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5110 }
f149e3e8
EI
5111
5112 if (arm_feature(env, ARM_FEATURE_EL3)) {
5113 valid_mask &= ~HCR_HCD;
77077a83
JK
5114 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5115 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5116 * However, if we're using the SMC PSCI conduit then QEMU is
5117 * effectively acting like EL3 firmware and so the guest at
5118 * EL2 should retain the ability to prevent EL1 from being
5119 * able to make SMC calls into the ersatz firmware, so in
5120 * that case HCR.TSC should be read/write.
5121 */
f149e3e8
EI
5122 valid_mask &= ~HCR_TSC;
5123 }
d1fb4da2
RH
5124
5125 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5126 if (cpu_isar_feature(aa64_vh, cpu)) {
5127 valid_mask |= HCR_E2H;
5128 }
5129 if (cpu_isar_feature(aa64_lor, cpu)) {
5130 valid_mask |= HCR_TLOR;
5131 }
5132 if (cpu_isar_feature(aa64_pauth, cpu)) {
5133 valid_mask |= HCR_API | HCR_APK;
5134 }
8ddb300b
RH
5135 if (cpu_isar_feature(aa64_mte, cpu)) {
5136 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5137 }
ef682cdb 5138 }
f149e3e8
EI
5139
5140 /* Clear RES0 bits. */
5141 value &= valid_mask;
5142
8ddb300b
RH
5143 /*
5144 * These bits change the MMU setup:
f149e3e8
EI
5145 * HCR_VM enables stage 2 translation
5146 * HCR_PTW forbids certain page-table setups
8ddb300b
RH
5147 * HCR_DC disables stage1 and enables stage2 translation
5148 * HCR_DCT enables tagging on (disabled) stage1 translation
f149e3e8 5149 */
8ddb300b 5150 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT)) {
d10eb08f 5151 tlb_flush(CPU(cpu));
f149e3e8 5152 }
ce4afed8 5153 env->cp15.hcr_el2 = value;
89430fc6
PM
5154
5155 /*
5156 * Updates to VI and VF require us to update the status of
5157 * virtual interrupts, which are the logical OR of these bits
5158 * and the state of the input lines from the GIC. (This requires
5159 * that we have the iothread lock, which is done by marking the
5160 * reginfo structs as ARM_CP_IO.)
5161 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5162 * possible for it to be taken immediately, because VIRQ and
5163 * VFIQ are masked unless running at EL0 or EL1, and HCR
5164 * can only be written at EL2.
5165 */
5166 g_assert(qemu_mutex_iothread_locked());
5167 arm_cpu_update_virq(cpu);
5168 arm_cpu_update_vfiq(cpu);
ce4afed8
PM
5169}
5170
d1fb4da2
RH
5171static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5172{
5173 do_hcr_write(env, value, 0);
5174}
5175
ce4afed8
PM
5176static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5177 uint64_t value)
5178{
5179 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5180 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
d1fb4da2 5181 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
ce4afed8
PM
5182}
5183
5184static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5185 uint64_t value)
5186{
5187 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5188 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
d1fb4da2 5189 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
f149e3e8
EI
5190}
5191
f7778444
RH
5192/*
5193 * Return the effective value of HCR_EL2.
5194 * Bits that are not included here:
5195 * RW (read from SCR_EL3.RW as needed)
5196 */
5197uint64_t arm_hcr_el2_eff(CPUARMState *env)
5198{
5199 uint64_t ret = env->cp15.hcr_el2;
5200
e6ef0169 5201 if (!arm_is_el2_enabled(env)) {
f7778444
RH
5202 /*
5203 * "This register has no effect if EL2 is not enabled in the
5204 * current Security state". This is ARMv8.4-SecEL2 speak for
5205 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5206 *
5207 * Prior to that, the language was "In an implementation that
5208 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5209 * as if this field is 0 for all purposes other than a direct
5210 * read or write access of HCR_EL2". With lots of enumeration
5211 * on a per-field basis. In current QEMU, this is condition
5212 * is arm_is_secure_below_el3.
5213 *
5214 * Since the v8.4 language applies to the entire register, and
5215 * appears to be backward compatible, use that.
5216 */
4990e1d3
RH
5217 return 0;
5218 }
5219
5220 /*
5221 * For a cpu that supports both aarch64 and aarch32, we can set bits
5222 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5223 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5224 */
5225 if (!arm_el_is_aa64(env, 2)) {
5226 uint64_t aa32_valid;
5227
5228 /*
5229 * These bits are up-to-date as of ARMv8.6.
5230 * For HCR, it's easiest to list just the 2 bits that are invalid.
5231 * For HCR2, list those that are valid.
5232 */
5233 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5234 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5235 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5236 ret &= aa32_valid;
5237 }
5238
5239 if (ret & HCR_TGE) {
5240 /* These bits are up-to-date as of ARMv8.6. */
f7778444
RH
5241 if (ret & HCR_E2H) {
5242 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5243 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5244 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4990e1d3
RH
5245 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5246 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5247 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
f7778444
RH
5248 } else {
5249 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5250 }
5251 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5252 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5253 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5254 HCR_TLOR);
5255 }
5256
5257 return ret;
5258}
5259
fc1120a7
PM
5260static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5261 uint64_t value)
5262{
5263 /*
5264 * For A-profile AArch32 EL3, if NSACR.CP10
5265 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5266 */
5267 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5268 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5269 value &= ~(0x3 << 10);
5270 value |= env->cp15.cptr_el[2] & (0x3 << 10);
5271 }
5272 env->cp15.cptr_el[2] = value;
5273}
5274
5275static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5276{
5277 /*
5278 * For A-profile AArch32 EL3, if NSACR.CP10
5279 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5280 */
5281 uint64_t value = env->cp15.cptr_el[2];
5282
5283 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5284 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5285 value |= 0x3 << 10;
5286 }
5287 return value;
5288}
5289
4771cd01 5290static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8 5291 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
89430fc6 5292 .type = ARM_CP_IO,
f149e3e8
EI
5293 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5294 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5295 .writefn = hcr_write },
ce4afed8 5296 { .name = "HCR", .state = ARM_CP_STATE_AA32,
89430fc6 5297 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5298 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5299 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5300 .writefn = hcr_writelow },
831a2fca
PM
5301 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5302 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5303 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3b685ba7 5304 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5305 .type = ARM_CP_ALIAS,
3b685ba7
EI
5306 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5307 .access = PL2_RW,
5308 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
68e78e33 5309 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
f2c30f42
EI
5310 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5311 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
cba517c3 5312 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
63b60551
EI
5313 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5314 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
cba517c3
PM
5315 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5316 .type = ARM_CP_ALIAS,
5317 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5318 .access = PL2_RW,
5319 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
3b685ba7 5320 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5321 .type = ARM_CP_ALIAS,
3b685ba7 5322 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5323 .access = PL2_RW,
5324 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d79e0c06 5325 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5326 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5327 .access = PL2_RW, .writefn = vbar_write,
5328 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5329 .resetvalue = 0 },
884b4dee
GB
5330 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5331 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5332 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 5333 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
5334 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5335 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5336 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
fc1120a7
PM
5337 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5338 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
95f949ac
EI
5339 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5340 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5341 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5342 .resetvalue = 0 },
5343 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5344 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac
EI
5345 .access = PL2_RW, .type = ARM_CP_ALIAS,
5346 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
5347 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5348 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5349 .access = PL2_RW, .type = ARM_CP_CONST,
5350 .resetvalue = 0 },
5351 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
55b53c71 5352 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5353 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5354 .access = PL2_RW, .type = ARM_CP_CONST,
5355 .resetvalue = 0 },
37cd6c24
PM
5356 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5357 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5358 .access = PL2_RW, .type = ARM_CP_CONST,
5359 .resetvalue = 0 },
5360 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5361 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5362 .access = PL2_RW, .type = ARM_CP_CONST,
5363 .resetvalue = 0 },
06ec4c8c
EI
5364 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5365 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
d06dc933
RH
5366 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5367 /* no .raw_writefn or .resetfn needed as we never use mask/base_mask */
06ec4c8c 5368 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
5369 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5370 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5371 .type = ARM_CP_ALIAS,
68e9c2fe
EI
5372 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5373 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5374 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5375 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112
PM
5376 .access = PL2_RW,
5377 /* no .writefn needed as this can't cause an ASID change;
5378 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
5379 */
68e9c2fe 5380 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
5381 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5382 .cp = 15, .opc1 = 6, .crm = 2,
5383 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5384 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5385 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5386 .writefn = vttbr_write },
5387 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5388 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5389 .access = PL2_RW, .writefn = vttbr_write,
5390 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
5391 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5392 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5393 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5394 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
5395 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5396 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5397 .access = PL2_RW, .resetvalue = 0,
5398 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
5399 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5400 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
ed30da8e 5401 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
a57633c0
EI
5402 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5403 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5404 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 5405 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
5406 { .name = "TLBIALLNSNH",
5407 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5408 .type = ARM_CP_NO_RAW, .access = PL2_W,
5409 .writefn = tlbiall_nsnh_write },
5410 { .name = "TLBIALLNSNHIS",
5411 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5412 .type = ARM_CP_NO_RAW, .access = PL2_W,
5413 .writefn = tlbiall_nsnh_is_write },
5414 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5415 .type = ARM_CP_NO_RAW, .access = PL2_W,
5416 .writefn = tlbiall_hyp_write },
5417 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5418 .type = ARM_CP_NO_RAW, .access = PL2_W,
5419 .writefn = tlbiall_hyp_is_write },
5420 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5421 .type = ARM_CP_NO_RAW, .access = PL2_W,
5422 .writefn = tlbimva_hyp_write },
5423 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5424 .type = ARM_CP_NO_RAW, .access = PL2_W,
5425 .writefn = tlbimva_hyp_is_write },
51da9014
EI
5426 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5427 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
696ba377 5428 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 5429 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
5430 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5431 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
696ba377 5432 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 5433 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
5434 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5435 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
696ba377 5436 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75
PM
5437 .writefn = tlbi_aa64_vae2_write },
5438 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5439 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
696ba377 5440 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75 5441 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
5442 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5443 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
696ba377 5444 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 5445 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
5446 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5447 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
696ba377 5448 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75 5449 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 5450#ifndef CONFIG_USER_ONLY
2a47df95
PM
5451 /* Unlike the other EL2-related AT operations, these must
5452 * UNDEF from EL3 if EL2 is not implemented, which is why we
5453 * define them here rather than with the rest of the AT ops.
5454 */
5455 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5456 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5457 .access = PL2_W, .accessfn = at_s1e2_access,
696ba377
RH
5458 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5459 .writefn = ats_write64 },
2a47df95
PM
5460 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5461 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5462 .access = PL2_W, .accessfn = at_s1e2_access,
696ba377
RH
5463 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5464 .writefn = ats_write64 },
14db7fe0
PM
5465 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5466 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5467 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5468 * to behave as if SCR.NS was 1.
5469 */
5470 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5471 .access = PL2_W,
0710b2fa 5472 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
14db7fe0
PM
5473 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5474 .access = PL2_W,
0710b2fa 5475 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
0b6440af
EI
5476 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5477 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5478 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5479 * reset values as IMPDEF. We choose to reset to 3 to comply with
5480 * both ARMv7 and ARMv8.
5481 */
5482 .access = PL2_RW, .resetvalue = 3,
5483 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
5484 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5485 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5486 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5487 .writefn = gt_cntvoff_write,
5488 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5489 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5490 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5491 .writefn = gt_cntvoff_write,
5492 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
5493 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5494 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5495 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5496 .type = ARM_CP_IO, .access = PL2_RW,
5497 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5498 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5499 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5500 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5501 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5502 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5503 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 5504 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
5505 .resetfn = gt_hyp_timer_reset,
5506 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5507 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5508 .type = ARM_CP_IO,
5509 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5510 .access = PL2_RW,
5511 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5512 .resetvalue = 0,
5513 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 5514#endif
21c2dd77
PM
5515 /* The only field of MDCR_EL2 that has a defined architectural reset value
5516 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
5517 */
5518 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5519 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
5520 .access = PL2_RW, .resetvalue = PMCR_NUM_COUNTERS,
5521 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
5522 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5523 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5524 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5525 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5526 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5527 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5528 .access = PL2_RW,
5529 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
5530 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5531 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5532 .access = PL2_RW,
5533 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
5534};
5535
ce4afed8
PM
5536static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5537 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
89430fc6 5538 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5539 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5540 .access = PL2_RW,
5541 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5542 .writefn = hcr_writehigh },
ce4afed8
PM
5543};
5544
e9152ee9
RDC
5545static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5546 bool isread)
5547{
5548 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5549 return CP_ACCESS_OK;
5550 }
5551 return CP_ACCESS_TRAP_UNCATEGORIZED;
5552}
5553
5554static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5555 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5556 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5557 .access = PL2_RW, .accessfn = sel2_access,
5558 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5559 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5560 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5561 .access = PL2_RW, .accessfn = sel2_access,
5562 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
e9152ee9
RDC
5563};
5564
2f027fc5
PM
5565static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5566 bool isread)
5567{
5568 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
926c1b97 5569 * At Secure EL1 it traps to EL3 or EL2.
2f027fc5
PM
5570 */
5571 if (arm_current_el(env) == 3) {
5572 return CP_ACCESS_OK;
5573 }
5574 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
5575 if (env->cp15.scr_el3 & SCR_EEL2) {
5576 return CP_ACCESS_TRAP_EL2;
5577 }
2f027fc5
PM
5578 return CP_ACCESS_TRAP_EL3;
5579 }
5580 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5581 if (isread) {
5582 return CP_ACCESS_OK;
5583 }
5584 return CP_ACCESS_TRAP_UNCATEGORIZED;
5585}
5586
60fb1a87
GB
5587static const ARMCPRegInfo el3_cp_reginfo[] = {
5588 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5589 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5590 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
10d0ef3e 5591 .resetfn = scr_reset, .writefn = scr_write },
f80741d1 5592 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
60fb1a87 5593 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
5594 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5595 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 5596 .writefn = scr_write },
60fb1a87
GB
5597 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5598 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5599 .access = PL3_RW, .resetvalue = 0,
5600 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5601 { .name = "SDER",
5602 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5603 .access = PL3_RW, .resetvalue = 0,
5604 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 5605 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
5606 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5607 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 5608 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
5609 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5610 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
f478847f 5611 .access = PL3_RW, .resetvalue = 0,
7dd8c9af 5612 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
5613 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5614 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
5615 .access = PL3_RW,
5616 /* no .writefn needed as this can't cause an ASID change;
811595a2
PM
5617 * we must provide a .raw_writefn and .resetfn because we handle
5618 * reset and migration for the AArch32 TTBCR(S), which might be
5619 * using mask and base_mask.
6459b94c 5620 */
811595a2 5621 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee 5622 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 5623 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5624 .type = ARM_CP_ALIAS,
81547d66
EI
5625 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5626 .access = PL3_RW,
5627 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 5628 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
5629 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5630 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
5631 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5632 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5633 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 5634 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5635 .type = ARM_CP_ALIAS,
81547d66 5636 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5637 .access = PL3_RW,
5638 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
5639 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5640 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5641 .access = PL3_RW, .writefn = vbar_write,
5642 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5643 .resetvalue = 0 },
c6f19164
GB
5644 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5645 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5646 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5647 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
5648 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5649 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5650 .access = PL3_RW, .resetvalue = 0,
5651 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
5652 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5653 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5654 .access = PL3_RW, .type = ARM_CP_CONST,
5655 .resetvalue = 0 },
37cd6c24
PM
5656 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5657 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5658 .access = PL3_RW, .type = ARM_CP_CONST,
5659 .resetvalue = 0 },
5660 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5661 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5662 .access = PL3_RW, .type = ARM_CP_CONST,
5663 .resetvalue = 0 },
43efaa33
PM
5664 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5665 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5666 .access = PL3_W, .type = ARM_CP_NO_RAW,
5667 .writefn = tlbi_aa64_alle3is_write },
5668 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5669 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5670 .access = PL3_W, .type = ARM_CP_NO_RAW,
5671 .writefn = tlbi_aa64_vae3is_write },
5672 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5673 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5674 .access = PL3_W, .type = ARM_CP_NO_RAW,
5675 .writefn = tlbi_aa64_vae3is_write },
5676 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5677 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5678 .access = PL3_W, .type = ARM_CP_NO_RAW,
5679 .writefn = tlbi_aa64_alle3_write },
5680 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5681 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5682 .access = PL3_W, .type = ARM_CP_NO_RAW,
5683 .writefn = tlbi_aa64_vae3_write },
5684 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5685 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5686 .access = PL3_W, .type = ARM_CP_NO_RAW,
5687 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
5688};
5689
e2cce18f
RH
5690#ifndef CONFIG_USER_ONLY
5691/* Test if system register redirection is to occur in the current state. */
5692static bool redirect_for_e2h(CPUARMState *env)
5693{
5694 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5695}
5696
5697static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5698{
5699 CPReadFn *readfn;
5700
5701 if (redirect_for_e2h(env)) {
5702 /* Switch to the saved EL2 version of the register. */
5703 ri = ri->opaque;
5704 readfn = ri->readfn;
5705 } else {
5706 readfn = ri->orig_readfn;
5707 }
5708 if (readfn == NULL) {
5709 readfn = raw_read;
5710 }
5711 return readfn(env, ri);
5712}
5713
5714static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5715 uint64_t value)
5716{
5717 CPWriteFn *writefn;
5718
5719 if (redirect_for_e2h(env)) {
5720 /* Switch to the saved EL2 version of the register. */
5721 ri = ri->opaque;
5722 writefn = ri->writefn;
5723 } else {
5724 writefn = ri->orig_writefn;
5725 }
5726 if (writefn == NULL) {
5727 writefn = raw_write;
5728 }
5729 writefn(env, ri, value);
5730}
5731
5732static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5733{
5734 struct E2HAlias {
5735 uint32_t src_key, dst_key, new_key;
5736 const char *src_name, *dst_name, *new_name;
5737 bool (*feature)(const ARMISARegisters *id);
5738 };
5739
5740#define K(op0, op1, crn, crm, op2) \
5741 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5742
5743 static const struct E2HAlias aliases[] = {
5744 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
5745 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5746 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
5747 "CPACR", "CPTR_EL2", "CPACR_EL12" },
5748 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
5749 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5750 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
5751 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5752 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
5753 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5754 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
5755 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5756 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
5757 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5758 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
5759 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5760 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
5761 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5762 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
5763 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5764 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
5765 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5766 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5767 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5768 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5769 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5770 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5771 "VBAR", "VBAR_EL2", "VBAR_EL12" },
5772 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5773 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5774 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5775 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5776
5777 /*
5778 * Note that redirection of ZCR is mentioned in the description
5779 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5780 * not in the summary table.
5781 */
5782 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
5783 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5784
4b779ceb
RH
5785 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
5786 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5787
e2cce18f
RH
5788 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5789 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5790 };
5791#undef K
5792
5793 size_t i;
5794
5795 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5796 const struct E2HAlias *a = &aliases[i];
9da35a40 5797 ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
9da35a40 5798 bool ok;
e2cce18f
RH
5799
5800 if (a->feature && !a->feature(&cpu->isar)) {
5801 continue;
5802 }
5803
5860362d
RH
5804 src_reg = g_hash_table_lookup(cpu->cp_regs,
5805 (gpointer)(uintptr_t)a->src_key);
5806 dst_reg = g_hash_table_lookup(cpu->cp_regs,
5807 (gpointer)(uintptr_t)a->dst_key);
e2cce18f
RH
5808 g_assert(src_reg != NULL);
5809 g_assert(dst_reg != NULL);
5810
5811 /* Cross-compare names to detect typos in the keys. */
5812 g_assert(strcmp(src_reg->name, a->src_name) == 0);
5813 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5814
5815 /* None of the core system registers use opaque; we will. */
5816 g_assert(src_reg->opaque == NULL);
5817
5818 /* Create alias before redirection so we dup the right data. */
9da35a40 5819 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
9da35a40
RH
5820
5821 new_reg->name = a->new_name;
5822 new_reg->type |= ARM_CP_ALIAS;
5823 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
5824 new_reg->access &= PL2_RW | PL3_RW;
5825
5860362d
RH
5826 ok = g_hash_table_insert(cpu->cp_regs,
5827 (gpointer)(uintptr_t)a->new_key, new_reg);
9da35a40 5828 g_assert(ok);
e2cce18f
RH
5829
5830 src_reg->opaque = dst_reg;
5831 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5832 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5833 if (!src_reg->raw_readfn) {
5834 src_reg->raw_readfn = raw_read;
5835 }
5836 if (!src_reg->raw_writefn) {
5837 src_reg->raw_writefn = raw_write;
5838 }
5839 src_reg->readfn = el2_e2h_read;
5840 src_reg->writefn = el2_e2h_write;
5841 }
5842}
5843#endif
5844
3f208fd7
PM
5845static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5846 bool isread)
7da845b0 5847{
97475a89
RH
5848 int cur_el = arm_current_el(env);
5849
5850 if (cur_el < 2) {
5851 uint64_t hcr = arm_hcr_el2_eff(env);
5852
5853 if (cur_el == 0) {
5854 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5855 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5856 return CP_ACCESS_TRAP_EL2;
5857 }
5858 } else {
5859 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5860 return CP_ACCESS_TRAP;
5861 }
5862 if (hcr & HCR_TID2) {
5863 return CP_ACCESS_TRAP_EL2;
5864 }
5865 }
5866 } else if (hcr & HCR_TID2) {
5867 return CP_ACCESS_TRAP_EL2;
5868 }
7da845b0 5869 }
630fcd4d
MZ
5870
5871 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5872 return CP_ACCESS_TRAP_EL2;
5873 }
5874
7da845b0
PM
5875 return CP_ACCESS_OK;
5876}
5877
1424ca8d
DM
5878static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
5879 uint64_t value)
5880{
5881 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
5882 * read via a bit in OSLSR_EL1.
5883 */
5884 int oslock;
5885
5886 if (ri->state == ARM_CP_STATE_AA32) {
5887 oslock = (value == 0xC5ACCE55);
5888 } else {
5889 oslock = value & 1;
5890 }
5891
5892 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
5893}
5894
50300698 5895static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 5896 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
5897 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
5898 * unlike DBGDRAR it is never accessible from EL0.
5899 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
5900 * accessor.
50300698
PM
5901 */
5902 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
5903 .access = PL0_R, .accessfn = access_tdra,
5904 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
5905 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
5906 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
5907 .access = PL1_R, .accessfn = access_tdra,
5908 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 5909 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
5910 .access = PL0_R, .accessfn = access_tdra,
5911 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 5912 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
5913 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
5914 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 5915 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
5916 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
5917 .resetvalue = 0 },
49a6f3bf
NH
5918 /*
5919 * MDCCSR_EL0[30:29] map to EDSCR[30:29]. Simply RAZ as the external
5920 * Debug Communication Channel is not implemented.
5921 */
5922 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_AA64,
5923 .opc0 = 2, .opc1 = 3, .crn = 0, .crm = 1, .opc2 = 0,
5924 .access = PL0_R, .accessfn = access_tda,
5925 .type = ARM_CP_CONST, .resetvalue = 0 },
5926 /*
5927 * DBGDSCRint[15,12,5:2] map to MDSCR_EL1[15,12,5:2]. Map all bits as
5928 * it is unlikely a guest will care.
5e8b12ff
PM
5929 * We don't implement the configurable EL0 access.
5930 */
49a6f3bf
NH
5931 { .name = "DBGDSCRint", .state = ARM_CP_STATE_AA32,
5932 .cp = 14, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 5933 .type = ARM_CP_ALIAS,
d6c8cf81 5934 .access = PL1_R, .accessfn = access_tda,
b061a82b 5935 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
5936 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
5937 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 5938 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 5939 .accessfn = access_tdosa,
1424ca8d
DM
5940 .writefn = oslar_write },
5941 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
5942 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
5943 .access = PL1_R, .resetvalue = 10,
187f678d 5944 .accessfn = access_tdosa,
1424ca8d 5945 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
5946 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
5947 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
5948 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
5949 .access = PL1_RW, .accessfn = access_tdosa,
5950 .type = ARM_CP_NOP },
5e8b12ff
PM
5951 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
5952 * implement vector catch debug events yet.
5953 */
5954 { .name = "DBGVCR",
5955 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
5956 .access = PL1_RW, .accessfn = access_tda,
5957 .type = ARM_CP_NOP },
4d2ec4da
PM
5958 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
5959 * to save and restore a 32-bit guest's DBGVCR)
5960 */
5961 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
5962 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
5963 .access = PL2_RW, .accessfn = access_tda,
696ba377 5964 .type = ARM_CP_NOP | ARM_CP_EL3_NO_EL2_KEEP },
5dbdc434
PM
5965 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
5966 * Channel but Linux may try to access this register. The 32-bit
5967 * alias is DBGDCCINT.
5968 */
5969 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
5970 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
5971 .access = PL1_RW, .accessfn = access_tda,
5972 .type = ARM_CP_NOP },
50300698
PM
5973};
5974
5975static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
5976 /* 64 bit access versions of the (dummy) debug registers */
5977 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
5978 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
5979 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
5980 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
50300698
PM
5981};
5982
60eed086
RH
5983/* Return the exception level to which exceptions should be taken
5984 * via SVEAccessTrap. If an exception should be routed through
5985 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
5986 * take care of raising that exception.
5987 * C.f. the ARM pseudocode function CheckSVEEnabled.
5be5e8ed 5988 */
ced31551 5989int sve_exception_el(CPUARMState *env, int el)
5be5e8ed
RH
5990{
5991#ifndef CONFIG_USER_ONLY
c2ddb7cf
RH
5992 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
5993
5994 if (el <= 1 && (hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
7701cee5
RH
5995 /* Check CPACR.ZEN. */
5996 switch (extract32(env->cp15.cpacr_el1, 16, 2)) {
5997 case 1:
5998 if (el != 0) {
5999 break;
6000 }
6001 /* fall through */
6002 case 0:
6003 case 2:
60eed086 6004 /* route_to_el2 */
c2ddb7cf 6005 return hcr_el2 & HCR_TGE ? 2 : 1;
5be5e8ed 6006 }
5be5e8ed 6007
60eed086 6008 /* Check CPACR.FPEN. */
7701cee5
RH
6009 switch (extract32(env->cp15.cpacr_el1, 20, 2)) {
6010 case 1:
6011 if (el != 0) {
6012 break;
6013 }
6014 /* fall through */
6015 case 0:
6016 case 2:
60eed086 6017 return 0;
5be5e8ed 6018 }
5be5e8ed
RH
6019 }
6020
d5a6fa2d
RH
6021 /*
6022 * CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE).
60eed086 6023 */
d5a6fa2d
RH
6024 if (el <= 2) {
6025 if (hcr_el2 & HCR_E2H) {
6026 /* Check CPTR_EL2.ZEN. */
6027 switch (extract32(env->cp15.cptr_el[2], 16, 2)) {
6028 case 1:
6029 if (el != 0 || !(hcr_el2 & HCR_TGE)) {
6030 break;
6031 }
6032 /* fall through */
6033 case 0:
6034 case 2:
6035 return 2;
6036 }
6037
6038 /* Check CPTR_EL2.FPEN. */
6039 switch (extract32(env->cp15.cptr_el[2], 20, 2)) {
6040 case 1:
6041 if (el == 2 || !(hcr_el2 & HCR_TGE)) {
6042 break;
6043 }
6044 /* fall through */
6045 case 0:
6046 case 2:
6047 return 0;
6048 }
6049 } else if (arm_is_el2_enabled(env)) {
6050 if (env->cp15.cptr_el[2] & CPTR_TZ) {
6051 return 2;
6052 }
6053 if (env->cp15.cptr_el[2] & CPTR_TFP) {
6054 return 0;
6055 }
60eed086 6056 }
5be5e8ed
RH
6057 }
6058
60eed086
RH
6059 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6060 if (arm_feature(env, ARM_FEATURE_EL3)
6061 && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
5be5e8ed
RH
6062 return 3;
6063 }
6064#endif
6065 return 0;
6066}
6067
ce440581 6068uint32_t aarch64_sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len)
0df9142d 6069{
6e553f2a 6070 uint32_t end_len;
0df9142d 6071
dc0bc8e7
RH
6072 start_len = MIN(start_len, ARM_MAX_VQ - 1);
6073 end_len = start_len;
6074
6e553f2a
RH
6075 if (!test_bit(start_len, cpu->sve_vq_map)) {
6076 end_len = find_last_bit(cpu->sve_vq_map, start_len);
6077 assert(end_len < start_len);
6078 }
6079 return end_len;
0df9142d
AJ
6080}
6081
0ab5953b
RH
6082/*
6083 * Given that SVE is enabled, return the vector length for EL.
6084 */
ced31551 6085uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
0ab5953b 6086{
2fc0cc0e 6087 ARMCPU *cpu = env_archcpu(env);
0ab5953b
RH
6088 uint32_t zcr_len = cpu->sve_max_vq - 1;
6089
63888fa7
RH
6090 if (el <= 1 &&
6091 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
0ab5953b
RH
6092 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
6093 }
6a02a732 6094 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
0ab5953b
RH
6095 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
6096 }
6a02a732 6097 if (arm_feature(env, ARM_FEATURE_EL3)) {
0ab5953b
RH
6098 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
6099 }
0df9142d 6100
ce440581 6101 return aarch64_sve_zcr_get_valid_len(cpu, zcr_len);
0ab5953b
RH
6102}
6103
5be5e8ed
RH
6104static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6105 uint64_t value)
6106{
0ab5953b
RH
6107 int cur_el = arm_current_el(env);
6108 int old_len = sve_zcr_len_for_el(env, cur_el);
6109 int new_len;
6110
5be5e8ed 6111 /* Bits other than [3:0] are RAZ/WI. */
7b351d98 6112 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5be5e8ed 6113 raw_write(env, ri, value & 0xf);
0ab5953b
RH
6114
6115 /*
6116 * Because we arrived here, we know both FP and SVE are enabled;
6117 * otherwise we would have trapped access to the ZCR_ELn register.
6118 */
6119 new_len = sve_zcr_len_for_el(env, cur_el);
6120 if (new_len < old_len) {
6121 aarch64_sve_narrow_vq(env, new_len + 1);
6122 }
5be5e8ed
RH
6123}
6124
60360d82
RH
6125static const ARMCPRegInfo zcr_reginfo[] = {
6126 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6127 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6128 .access = PL1_RW, .type = ARM_CP_SVE,
6129 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6130 .writefn = zcr_write, .raw_writefn = raw_write },
6131 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6132 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6133 .access = PL2_RW, .type = ARM_CP_SVE,
6134 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6135 .writefn = zcr_write, .raw_writefn = raw_write },
6136 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6137 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6138 .access = PL3_RW, .type = ARM_CP_SVE,
6139 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6140 .writefn = zcr_write, .raw_writefn = raw_write },
5be5e8ed
RH
6141};
6142
9ee98ce8
PM
6143void hw_watchpoint_update(ARMCPU *cpu, int n)
6144{
6145 CPUARMState *env = &cpu->env;
6146 vaddr len = 0;
6147 vaddr wvr = env->cp15.dbgwvr[n];
6148 uint64_t wcr = env->cp15.dbgwcr[n];
6149 int mask;
6150 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
6151
6152 if (env->cpu_watchpoint[n]) {
6153 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
6154 env->cpu_watchpoint[n] = NULL;
6155 }
6156
8b7a5bbe 6157 if (!FIELD_EX64(wcr, DBGWCR, E)) {
9ee98ce8
PM
6158 /* E bit clear : watchpoint disabled */
6159 return;
6160 }
6161
8b7a5bbe 6162 switch (FIELD_EX64(wcr, DBGWCR, LSC)) {
9ee98ce8
PM
6163 case 0:
6164 /* LSC 00 is reserved and must behave as if the wp is disabled */
6165 return;
6166 case 1:
6167 flags |= BP_MEM_READ;
6168 break;
6169 case 2:
6170 flags |= BP_MEM_WRITE;
6171 break;
6172 case 3:
6173 flags |= BP_MEM_ACCESS;
6174 break;
6175 }
6176
6177 /* Attempts to use both MASK and BAS fields simultaneously are
6178 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
6179 * thus generating a watchpoint for every byte in the masked region.
6180 */
8b7a5bbe 6181 mask = FIELD_EX64(wcr, DBGWCR, MASK);
9ee98ce8
PM
6182 if (mask == 1 || mask == 2) {
6183 /* Reserved values of MASK; we must act as if the mask value was
6184 * some non-reserved value, or as if the watchpoint were disabled.
6185 * We choose the latter.
6186 */
6187 return;
6188 } else if (mask) {
6189 /* Watchpoint covers an aligned area up to 2GB in size */
6190 len = 1ULL << mask;
6191 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
6192 * whether the watchpoint fires when the unmasked bits match; we opt
6193 * to generate the exceptions.
6194 */
6195 wvr &= ~(len - 1);
6196 } else {
6197 /* Watchpoint covers bytes defined by the byte address select bits */
8b7a5bbe 6198 int bas = FIELD_EX64(wcr, DBGWCR, BAS);
9ee98ce8
PM
6199 int basstart;
6200
9ee98ce8
PM
6201 if (extract64(wvr, 2, 1)) {
6202 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
6203 * ignored, and BAS[3:0] define which bytes to watch.
6204 */
6205 bas &= 0xf;
6206 }
ae1111d4
RH
6207
6208 if (bas == 0) {
6209 /* This must act as if the watchpoint is disabled */
6210 return;
6211 }
6212
9ee98ce8
PM
6213 /* The BAS bits are supposed to be programmed to indicate a contiguous
6214 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
6215 * we fire for each byte in the word/doubleword addressed by the WVR.
6216 * We choose to ignore any non-zero bits after the first range of 1s.
6217 */
6218 basstart = ctz32(bas);
6219 len = cto32(bas >> basstart);
6220 wvr += basstart;
6221 }
6222
6223 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
6224 &env->cpu_watchpoint[n]);
6225}
6226
6227void hw_watchpoint_update_all(ARMCPU *cpu)
6228{
6229 int i;
6230 CPUARMState *env = &cpu->env;
6231
6232 /* Completely clear out existing QEMU watchpoints and our array, to
6233 * avoid possible stale entries following migration load.
6234 */
6235 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
6236 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
6237
6238 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
6239 hw_watchpoint_update(cpu, i);
6240 }
6241}
6242
6243static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6244 uint64_t value)
6245{
2fc0cc0e 6246 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6247 int i = ri->crm;
6248
777ab8d8 6249 /*
9ee98ce8 6250 * Bits [1:0] are RES0.
777ab8d8
RH
6251 *
6252 * It is IMPLEMENTATION DEFINED whether [63:49] ([63:53] with FEAT_LVA)
6253 * are hardwired to the value of bit [48] ([52] with FEAT_LVA), or if
6254 * they contain the value written. It is CONSTRAINED UNPREDICTABLE
6255 * whether the RESS bits are ignored when comparing an address.
6256 *
6257 * Therefore we are allowed to compare the entire register, which lets
6258 * us avoid considering whether or not FEAT_LVA is actually enabled.
9ee98ce8 6259 */
777ab8d8 6260 value &= ~3ULL;
9ee98ce8
PM
6261
6262 raw_write(env, ri, value);
6263 hw_watchpoint_update(cpu, i);
6264}
6265
6266static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6267 uint64_t value)
6268{
2fc0cc0e 6269 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6270 int i = ri->crm;
6271
6272 raw_write(env, ri, value);
6273 hw_watchpoint_update(cpu, i);
6274}
6275
46747d15
PM
6276void hw_breakpoint_update(ARMCPU *cpu, int n)
6277{
6278 CPUARMState *env = &cpu->env;
6279 uint64_t bvr = env->cp15.dbgbvr[n];
6280 uint64_t bcr = env->cp15.dbgbcr[n];
6281 vaddr addr;
6282 int bt;
6283 int flags = BP_CPU;
6284
6285 if (env->cpu_breakpoint[n]) {
6286 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
6287 env->cpu_breakpoint[n] = NULL;
6288 }
6289
6290 if (!extract64(bcr, 0, 1)) {
6291 /* E bit clear : watchpoint disabled */
6292 return;
6293 }
6294
6295 bt = extract64(bcr, 20, 4);
6296
6297 switch (bt) {
6298 case 4: /* unlinked address mismatch (reserved if AArch64) */
6299 case 5: /* linked address mismatch (reserved if AArch64) */
6300 qemu_log_mask(LOG_UNIMP,
0221c8fd 6301 "arm: address mismatch breakpoint types not implemented\n");
46747d15
PM
6302 return;
6303 case 0: /* unlinked address match */
6304 case 1: /* linked address match */
6305 {
777ab8d8
RH
6306 /*
6307 * Bits [1:0] are RES0.
6308 *
6309 * It is IMPLEMENTATION DEFINED whether bits [63:49]
6310 * ([63:53] for FEAT_LVA) are hardwired to a copy of the sign bit
6311 * of the VA field ([48] or [52] for FEAT_LVA), or whether the
6312 * value is read as written. It is CONSTRAINED UNPREDICTABLE
6313 * whether the RESS bits are ignored when comparing an address.
6314 * Therefore we are allowed to compare the entire register, which
6315 * lets us avoid considering whether FEAT_LVA is actually enabled.
6316 *
6317 * The BAS field is used to allow setting breakpoints on 16-bit
6318 * wide instructions; it is CONSTRAINED UNPREDICTABLE whether
46747d15
PM
6319 * a bp will fire if the addresses covered by the bp and the addresses
6320 * covered by the insn overlap but the insn doesn't start at the
6321 * start of the bp address range. We choose to require the insn and
6322 * the bp to have the same address. The constraints on writing to
6323 * BAS enforced in dbgbcr_write mean we have only four cases:
6324 * 0b0000 => no breakpoint
6325 * 0b0011 => breakpoint on addr
6326 * 0b1100 => breakpoint on addr + 2
6327 * 0b1111 => breakpoint on addr
6328 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
6329 */
6330 int bas = extract64(bcr, 5, 4);
777ab8d8 6331 addr = bvr & ~3ULL;
46747d15
PM
6332 if (bas == 0) {
6333 return;
6334 }
6335 if (bas == 0xc) {
6336 addr += 2;
6337 }
6338 break;
6339 }
6340 case 2: /* unlinked context ID match */
6341 case 8: /* unlinked VMID match (reserved if no EL2) */
6342 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
6343 qemu_log_mask(LOG_UNIMP,
0221c8fd 6344 "arm: unlinked context breakpoint types not implemented\n");
46747d15
PM
6345 return;
6346 case 9: /* linked VMID match (reserved if no EL2) */
6347 case 11: /* linked context ID and VMID match (reserved if no EL2) */
6348 case 3: /* linked context ID match */
6349 default:
6350 /* We must generate no events for Linked context matches (unless
6351 * they are linked to by some other bp/wp, which is handled in
6352 * updates for the linking bp/wp). We choose to also generate no events
6353 * for reserved values.
6354 */
6355 return;
6356 }
6357
6358 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
6359}
6360
6361void hw_breakpoint_update_all(ARMCPU *cpu)
6362{
6363 int i;
6364 CPUARMState *env = &cpu->env;
6365
6366 /* Completely clear out existing QEMU breakpoints and our array, to
6367 * avoid possible stale entries following migration load.
6368 */
6369 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
6370 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
6371
6372 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
6373 hw_breakpoint_update(cpu, i);
6374 }
6375}
6376
6377static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6378 uint64_t value)
6379{
2fc0cc0e 6380 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6381 int i = ri->crm;
6382
6383 raw_write(env, ri, value);
6384 hw_breakpoint_update(cpu, i);
6385}
6386
6387static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6388 uint64_t value)
6389{
2fc0cc0e 6390 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6391 int i = ri->crm;
6392
6393 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
6394 * copy of BAS[0].
6395 */
6396 value = deposit64(value, 6, 1, extract64(value, 5, 1));
6397 value = deposit64(value, 8, 1, extract64(value, 7, 1));
6398
6399 raw_write(env, ri, value);
6400 hw_breakpoint_update(cpu, i);
6401}
6402
50300698 6403static void define_debug_regs(ARMCPU *cpu)
0b45451e 6404{
50300698
PM
6405 /* Define v7 and v8 architectural debug registers.
6406 * These are just dummy implementations for now.
0b45451e
PM
6407 */
6408 int i;
3ff6fc91 6409 int wrps, brps, ctx_cmps;
54a78718
RH
6410
6411 /*
6412 * The Arm ARM says DBGDIDR is optional and deprecated if EL1 cannot
6413 * use AArch32. Given that bit 15 is RES1, if the value is 0 then
6414 * the register must not exist for this cpu.
6415 */
6416 if (cpu->isar.dbgdidr != 0) {
6417 ARMCPRegInfo dbgdidr = {
6418 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0,
6419 .opc1 = 0, .opc2 = 0,
6420 .access = PL0_R, .accessfn = access_tda,
6421 .type = ARM_CP_CONST, .resetvalue = cpu->isar.dbgdidr,
6422 };
6423 define_one_arm_cp_reg(cpu, &dbgdidr);
6424 }
48eb3ae6 6425
3ff6fc91 6426 /* Note that all these register fields hold "number of Xs minus 1". */
88ce6c6e
PM
6427 brps = arm_num_brps(cpu);
6428 wrps = arm_num_wrps(cpu);
6429 ctx_cmps = arm_num_ctx_cmps(cpu);
3ff6fc91
PM
6430
6431 assert(ctx_cmps <= brps);
48eb3ae6 6432
50300698
PM
6433 define_arm_cp_regs(cpu, debug_cp_reginfo);
6434
6435 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
6436 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
6437 }
6438
88ce6c6e 6439 for (i = 0; i < brps; i++) {
0b45451e 6440 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6441 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
6442 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 6443 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6444 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
6445 .writefn = dbgbvr_write, .raw_writefn = raw_write
6446 },
10aae104
PM
6447 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
6448 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 6449 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6450 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
6451 .writefn = dbgbcr_write, .raw_writefn = raw_write
6452 },
48eb3ae6
PM
6453 };
6454 define_arm_cp_regs(cpu, dbgregs);
6455 }
6456
88ce6c6e 6457 for (i = 0; i < wrps; i++) {
48eb3ae6 6458 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6459 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
6460 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 6461 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6462 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
6463 .writefn = dbgwvr_write, .raw_writefn = raw_write
6464 },
10aae104
PM
6465 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
6466 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 6467 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6468 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
6469 .writefn = dbgwcr_write, .raw_writefn = raw_write
6470 },
0b45451e
PM
6471 };
6472 define_arm_cp_regs(cpu, dbgregs);
6473 }
6474}
6475
24183fb6
PM
6476static void define_pmu_regs(ARMCPU *cpu)
6477{
6478 /*
6479 * v7 performance monitor control register: same implementor
6480 * field as main ID register, and we implement four counters in
6481 * addition to the cycle count register.
6482 */
21c2dd77 6483 unsigned int i, pmcrn = PMCR_NUM_COUNTERS;
24183fb6
PM
6484 ARMCPRegInfo pmcr = {
6485 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6486 .access = PL0_RW,
6487 .type = ARM_CP_IO | ARM_CP_ALIAS,
6488 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6489 .accessfn = pmreg_access, .writefn = pmcr_write,
6490 .raw_writefn = raw_write,
6491 };
6492 ARMCPRegInfo pmcr64 = {
6493 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6494 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6495 .access = PL0_RW, .accessfn = pmreg_access,
6496 .type = ARM_CP_IO,
6497 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
21c2dd77
PM
6498 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT) |
6499 PMCRLC,
24183fb6
PM
6500 .writefn = pmcr_write, .raw_writefn = raw_write,
6501 };
6502 define_one_arm_cp_reg(cpu, &pmcr);
6503 define_one_arm_cp_reg(cpu, &pmcr64);
6504 for (i = 0; i < pmcrn; i++) {
6505 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6506 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6507 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6508 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6509 ARMCPRegInfo pmev_regs[] = {
6510 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6511 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6512 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6513 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
99a50d1a 6514 .accessfn = pmreg_access_xevcntr },
24183fb6
PM
6515 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6516 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
99a50d1a 6517 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
24183fb6
PM
6518 .type = ARM_CP_IO,
6519 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6520 .raw_readfn = pmevcntr_rawread,
6521 .raw_writefn = pmevcntr_rawwrite },
6522 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6523 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6524 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6525 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6526 .accessfn = pmreg_access },
6527 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6528 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6529 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6530 .type = ARM_CP_IO,
6531 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6532 .raw_writefn = pmevtyper_rawwrite },
24183fb6
PM
6533 };
6534 define_arm_cp_regs(cpu, pmev_regs);
6535 g_free(pmevcntr_name);
6536 g_free(pmevcntr_el0_name);
6537 g_free(pmevtyper_name);
6538 g_free(pmevtyper_el0_name);
6539 }
a6179538 6540 if (cpu_isar_feature(aa32_pmu_8_1, cpu)) {
24183fb6
PM
6541 ARMCPRegInfo v81_pmu_regs[] = {
6542 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6543 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6544 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6545 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6546 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6547 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6548 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6549 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
24183fb6
PM
6550 };
6551 define_arm_cp_regs(cpu, v81_pmu_regs);
6552 }
15dd1ebd
PM
6553 if (cpu_isar_feature(any_pmu_8_4, cpu)) {
6554 static const ARMCPRegInfo v84_pmmir = {
6555 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6556 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6557 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6558 .resetvalue = 0
6559 };
6560 define_one_arm_cp_reg(cpu, &v84_pmmir);
6561 }
24183fb6
PM
6562}
6563
96a8b92e
PM
6564/* We don't know until after realize whether there's a GICv3
6565 * attached, and that is what registers the gicv3 sysregs.
6566 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6567 * at runtime.
6568 */
6569static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6570{
2fc0cc0e 6571 ARMCPU *cpu = env_archcpu(env);
8a130a7b 6572 uint64_t pfr1 = cpu->isar.id_pfr1;
96a8b92e
PM
6573
6574 if (env->gicv3state) {
6575 pfr1 |= 1 << 28;
6576 }
6577 return pfr1;
6578}
6579
976b99b6 6580#ifndef CONFIG_USER_ONLY
96a8b92e
PM
6581static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6582{
2fc0cc0e 6583 ARMCPU *cpu = env_archcpu(env);
47576b94 6584 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
96a8b92e
PM
6585
6586 if (env->gicv3state) {
6587 pfr0 |= 1 << 24;
6588 }
6589 return pfr0;
6590}
976b99b6 6591#endif
96a8b92e 6592
2d7137c1 6593/* Shared logic between LORID and the rest of the LOR* registers.
9bd268ba 6594 * Secure state exclusion has already been dealt with.
2d7137c1 6595 */
9bd268ba
RDC
6596static CPAccessResult access_lor_ns(CPUARMState *env,
6597 const ARMCPRegInfo *ri, bool isread)
2d7137c1
RH
6598{
6599 int el = arm_current_el(env);
6600
6601 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6602 return CP_ACCESS_TRAP_EL2;
6603 }
6604 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6605 return CP_ACCESS_TRAP_EL3;
6606 }
6607 return CP_ACCESS_OK;
6608}
6609
2d7137c1
RH
6610static CPAccessResult access_lor_other(CPUARMState *env,
6611 const ARMCPRegInfo *ri, bool isread)
6612{
6613 if (arm_is_secure_below_el3(env)) {
6614 /* Access denied in secure mode. */
6615 return CP_ACCESS_TRAP;
6616 }
9bd268ba 6617 return access_lor_ns(env, ri, isread);
2d7137c1
RH
6618}
6619
d8564ee4
RH
6620/*
6621 * A trivial implementation of ARMv8.1-LOR leaves all of these
6622 * registers fixed at 0, which indicates that there are zero
6623 * supported Limited Ordering regions.
6624 */
6625static const ARMCPRegInfo lor_reginfo[] = {
6626 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6627 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6628 .access = PL1_RW, .accessfn = access_lor_other,
6629 .type = ARM_CP_CONST, .resetvalue = 0 },
6630 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6631 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6632 .access = PL1_RW, .accessfn = access_lor_other,
6633 .type = ARM_CP_CONST, .resetvalue = 0 },
6634 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6635 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6636 .access = PL1_RW, .accessfn = access_lor_other,
6637 .type = ARM_CP_CONST, .resetvalue = 0 },
6638 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6639 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6640 .access = PL1_RW, .accessfn = access_lor_other,
6641 .type = ARM_CP_CONST, .resetvalue = 0 },
6642 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6643 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
9bd268ba 6644 .access = PL1_R, .accessfn = access_lor_ns,
d8564ee4 6645 .type = ARM_CP_CONST, .resetvalue = 0 },
d8564ee4
RH
6646};
6647
967aa94f
RH
6648#ifdef TARGET_AARCH64
6649static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6650 bool isread)
6651{
6652 int el = arm_current_el(env);
6653
6654 if (el < 2 &&
6655 arm_feature(env, ARM_FEATURE_EL2) &&
6656 !(arm_hcr_el2_eff(env) & HCR_APK)) {
6657 return CP_ACCESS_TRAP_EL2;
6658 }
6659 if (el < 3 &&
6660 arm_feature(env, ARM_FEATURE_EL3) &&
6661 !(env->cp15.scr_el3 & SCR_APK)) {
6662 return CP_ACCESS_TRAP_EL3;
6663 }
6664 return CP_ACCESS_OK;
6665}
6666
6667static const ARMCPRegInfo pauth_reginfo[] = {
6668 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6669 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6670 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6671 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
967aa94f
RH
6672 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6673 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6674 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6675 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
967aa94f
RH
6676 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6677 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6678 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6679 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
967aa94f
RH
6680 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6681 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6682 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6683 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
967aa94f
RH
6684 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6685 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6686 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6687 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
967aa94f
RH
6688 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6689 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6690 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6691 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
967aa94f
RH
6692 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6693 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6694 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6695 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
967aa94f
RH
6696 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6697 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6698 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6699 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
967aa94f
RH
6700 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6701 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6702 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6703 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
967aa94f
RH
6704 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6705 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6706 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6707 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
967aa94f 6708};
de390645 6709
84940ed8
RC
6710static const ARMCPRegInfo tlbirange_reginfo[] = {
6711 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
6712 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
6713 .access = PL1_W, .type = ARM_CP_NO_RAW,
6714 .writefn = tlbi_aa64_rvae1is_write },
6715 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
6716 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
6717 .access = PL1_W, .type = ARM_CP_NO_RAW,
6718 .writefn = tlbi_aa64_rvae1is_write },
6719 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
6720 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
6721 .access = PL1_W, .type = ARM_CP_NO_RAW,
6722 .writefn = tlbi_aa64_rvae1is_write },
6723 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
6724 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
6725 .access = PL1_W, .type = ARM_CP_NO_RAW,
6726 .writefn = tlbi_aa64_rvae1is_write },
6727 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
6728 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
6729 .access = PL1_W, .type = ARM_CP_NO_RAW,
6730 .writefn = tlbi_aa64_rvae1is_write },
6731 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
6732 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
6733 .access = PL1_W, .type = ARM_CP_NO_RAW,
6734 .writefn = tlbi_aa64_rvae1is_write },
6735 { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
6736 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
6737 .access = PL1_W, .type = ARM_CP_NO_RAW,
6738 .writefn = tlbi_aa64_rvae1is_write },
6739 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
6740 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
6741 .access = PL1_W, .type = ARM_CP_NO_RAW,
6742 .writefn = tlbi_aa64_rvae1is_write },
6743 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
6744 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
6745 .access = PL1_W, .type = ARM_CP_NO_RAW,
6746 .writefn = tlbi_aa64_rvae1_write },
6747 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
6748 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
6749 .access = PL1_W, .type = ARM_CP_NO_RAW,
6750 .writefn = tlbi_aa64_rvae1_write },
6751 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
6752 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
6753 .access = PL1_W, .type = ARM_CP_NO_RAW,
6754 .writefn = tlbi_aa64_rvae1_write },
6755 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
6756 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
6757 .access = PL1_W, .type = ARM_CP_NO_RAW,
6758 .writefn = tlbi_aa64_rvae1_write },
6759 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
6760 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
6761 .access = PL2_W, .type = ARM_CP_NOP },
6762 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
6763 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
6764 .access = PL2_W, .type = ARM_CP_NOP },
6765 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
6766 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
696ba377 6767 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6768 .writefn = tlbi_aa64_rvae2is_write },
6769 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
6770 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
696ba377 6771 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6772 .writefn = tlbi_aa64_rvae2is_write },
6773 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
6774 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
6775 .access = PL2_W, .type = ARM_CP_NOP },
6776 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
6777 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
6778 .access = PL2_W, .type = ARM_CP_NOP },
6779 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
6780 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
696ba377 6781 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6782 .writefn = tlbi_aa64_rvae2is_write },
6783 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
6784 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
696ba377 6785 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6786 .writefn = tlbi_aa64_rvae2is_write },
6787 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
6788 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
696ba377 6789 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6790 .writefn = tlbi_aa64_rvae2_write },
6791 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
6792 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
696ba377 6793 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6794 .writefn = tlbi_aa64_rvae2_write },
6795 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
6796 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
6797 .access = PL3_W, .type = ARM_CP_NO_RAW,
6798 .writefn = tlbi_aa64_rvae3is_write },
6799 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
6800 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
6801 .access = PL3_W, .type = ARM_CP_NO_RAW,
6802 .writefn = tlbi_aa64_rvae3is_write },
6803 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
6804 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
6805 .access = PL3_W, .type = ARM_CP_NO_RAW,
6806 .writefn = tlbi_aa64_rvae3is_write },
6807 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
6808 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
6809 .access = PL3_W, .type = ARM_CP_NO_RAW,
6810 .writefn = tlbi_aa64_rvae3is_write },
6811 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
6812 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
6813 .access = PL3_W, .type = ARM_CP_NO_RAW,
6814 .writefn = tlbi_aa64_rvae3_write },
6815 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
6816 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
6817 .access = PL3_W, .type = ARM_CP_NO_RAW,
6818 .writefn = tlbi_aa64_rvae3_write },
84940ed8
RC
6819};
6820
7113d618
RC
6821static const ARMCPRegInfo tlbios_reginfo[] = {
6822 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
6823 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
6824 .access = PL1_W, .type = ARM_CP_NO_RAW,
6825 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
6826 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
6827 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
6828 .access = PL1_W, .type = ARM_CP_NO_RAW,
6829 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
6830 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
6831 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
6832 .access = PL1_W, .type = ARM_CP_NO_RAW,
6833 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
6834 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
6835 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
6836 .access = PL1_W, .type = ARM_CP_NO_RAW,
6837 .writefn = tlbi_aa64_vae1is_write },
6838 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
6839 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
6840 .access = PL1_W, .type = ARM_CP_NO_RAW,
6841 .writefn = tlbi_aa64_vae1is_write },
6842 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
6843 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
6844 .access = PL1_W, .type = ARM_CP_NO_RAW,
6845 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
6846 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
6847 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
696ba377 6848 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7113d618 6849 .writefn = tlbi_aa64_alle2is_write },
b7469ef9
IH
6850 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
6851 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
696ba377 6852 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
b7469ef9 6853 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
6854 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
6855 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
6856 .access = PL2_W, .type = ARM_CP_NO_RAW,
6857 .writefn = tlbi_aa64_alle1is_write },
b7469ef9
IH
6858 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
6859 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
696ba377 6860 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
b7469ef9 6861 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
6862 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
6863 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
6864 .access = PL2_W, .type = ARM_CP_NO_RAW,
6865 .writefn = tlbi_aa64_alle1is_write },
6866 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
6867 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
6868 .access = PL2_W, .type = ARM_CP_NOP },
6869 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
6870 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
6871 .access = PL2_W, .type = ARM_CP_NOP },
6872 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6873 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
6874 .access = PL2_W, .type = ARM_CP_NOP },
6875 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6876 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
6877 .access = PL2_W, .type = ARM_CP_NOP },
6878 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
6879 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
6880 .access = PL3_W, .type = ARM_CP_NO_RAW,
6881 .writefn = tlbi_aa64_alle3is_write },
b7469ef9
IH
6882 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
6883 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
6884 .access = PL3_W, .type = ARM_CP_NO_RAW,
6885 .writefn = tlbi_aa64_vae3is_write },
6886 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
6887 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
6888 .access = PL3_W, .type = ARM_CP_NO_RAW,
6889 .writefn = tlbi_aa64_vae3is_write },
7113d618
RC
6890};
6891
de390645
RH
6892static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6893{
6894 Error *err = NULL;
6895 uint64_t ret;
6896
6897 /* Success sets NZCV = 0000. */
6898 env->NF = env->CF = env->VF = 0, env->ZF = 1;
6899
6900 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6901 /*
6902 * ??? Failed, for unknown reasons in the crypto subsystem.
6903 * The best we can do is log the reason and return the
6904 * timed-out indication to the guest. There is no reason
6905 * we know to expect this failure to be transitory, so the
6906 * guest may well hang retrying the operation.
6907 */
6908 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6909 ri->name, error_get_pretty(err));
6910 error_free(err);
6911
6912 env->ZF = 0; /* NZCF = 0100 */
6913 return 0;
6914 }
6915 return ret;
6916}
6917
6918/* We do not support re-seeding, so the two registers operate the same. */
6919static const ARMCPRegInfo rndr_reginfo[] = {
6920 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6921 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6922 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6923 .access = PL0_R, .readfn = rndr_readfn },
6924 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6925 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6926 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6927 .access = PL0_R, .readfn = rndr_readfn },
de390645 6928};
0d57b499
BM
6929
6930#ifndef CONFIG_USER_ONLY
6931static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6932 uint64_t value)
6933{
6934 ARMCPU *cpu = env_archcpu(env);
6935 /* CTR_EL0 System register -> DminLine, bits [19:16] */
6936 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6937 uint64_t vaddr_in = (uint64_t) value;
6938 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6939 void *haddr;
6940 int mem_idx = cpu_mmu_index(env, false);
6941
6942 /* This won't be crossing page boundaries */
6943 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6944 if (haddr) {
6945
6946 ram_addr_t offset;
6947 MemoryRegion *mr;
6948
6949 /* RCU lock is already being held */
6950 mr = memory_region_from_host(haddr, &offset);
6951
6952 if (mr) {
4dfe59d1 6953 memory_region_writeback(mr, offset, dline_size);
0d57b499
BM
6954 }
6955 }
6956}
6957
6958static const ARMCPRegInfo dcpop_reg[] = {
6959 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6960 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6961 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 6962 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
6963};
6964
6965static const ARMCPRegInfo dcpodp_reg[] = {
6966 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6967 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6968 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 6969 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
6970};
6971#endif /*CONFIG_USER_ONLY*/
6972
4b779ceb
RH
6973static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
6974 bool isread)
6975{
6976 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
6977 return CP_ACCESS_TRAP_EL2;
6978 }
6979
6980 return CP_ACCESS_OK;
6981}
6982
6983static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
6984 bool isread)
6985{
6986 int el = arm_current_el(env);
6987
0da067f2 6988 if (el < 2 && arm_is_el2_enabled(env)) {
4301acd7
RH
6989 uint64_t hcr = arm_hcr_el2_eff(env);
6990 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
6991 return CP_ACCESS_TRAP_EL2;
6992 }
4b779ceb
RH
6993 }
6994 if (el < 3 &&
6995 arm_feature(env, ARM_FEATURE_EL3) &&
6996 !(env->cp15.scr_el3 & SCR_ATA)) {
6997 return CP_ACCESS_TRAP_EL3;
6998 }
6999 return CP_ACCESS_OK;
7000}
7001
7002static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7003{
7004 return env->pstate & PSTATE_TCO;
7005}
7006
7007static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7008{
7009 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7010}
7011
7012static const ARMCPRegInfo mte_reginfo[] = {
7013 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7014 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7015 .access = PL1_RW, .accessfn = access_mte,
7016 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7017 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7018 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7019 .access = PL1_RW, .accessfn = access_mte,
7020 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7021 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7022 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7023 .access = PL2_RW, .accessfn = access_mte,
7024 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7025 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7026 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7027 .access = PL3_RW,
7028 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7029 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7030 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7031 .access = PL1_RW, .accessfn = access_mte,
7032 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7033 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7034 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7035 .access = PL1_RW, .accessfn = access_mte,
7036 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7037 { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7038 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7039 .access = PL1_R, .accessfn = access_aa64_tid5,
7040 .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7041 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7042 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7043 .type = ARM_CP_NO_RAW,
7044 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
5463df16
RH
7045 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7046 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7047 .type = ARM_CP_NOP, .access = PL1_W,
7048 .accessfn = aa64_cacheop_poc_access },
7049 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7050 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7051 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7052 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7053 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7054 .type = ARM_CP_NOP, .access = PL1_W,
7055 .accessfn = aa64_cacheop_poc_access },
7056 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7057 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7058 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7059 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7060 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7061 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7062 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7063 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7064 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7065 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7066 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7067 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7068 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7069 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7070 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
4b779ceb
RH
7071};
7072
7073static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7074 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7075 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7076 .type = ARM_CP_CONST, .access = PL0_RW, },
4b779ceb 7077};
5463df16
RH
7078
7079static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7080 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7081 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7082 .type = ARM_CP_NOP, .access = PL0_W,
7083 .accessfn = aa64_cacheop_poc_access },
7084 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7085 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7086 .type = ARM_CP_NOP, .access = PL0_W,
7087 .accessfn = aa64_cacheop_poc_access },
7088 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7089 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7090 .type = ARM_CP_NOP, .access = PL0_W,
7091 .accessfn = aa64_cacheop_poc_access },
7092 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7093 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7094 .type = ARM_CP_NOP, .access = PL0_W,
7095 .accessfn = aa64_cacheop_poc_access },
7096 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7097 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7098 .type = ARM_CP_NOP, .access = PL0_W,
7099 .accessfn = aa64_cacheop_poc_access },
7100 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7101 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7102 .type = ARM_CP_NOP, .access = PL0_W,
7103 .accessfn = aa64_cacheop_poc_access },
7104 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7105 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7106 .type = ARM_CP_NOP, .access = PL0_W,
7107 .accessfn = aa64_cacheop_poc_access },
7108 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7109 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7110 .type = ARM_CP_NOP, .access = PL0_W,
7111 .accessfn = aa64_cacheop_poc_access },
eb821168
RH
7112 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7113 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7114 .access = PL0_W, .type = ARM_CP_DC_GVA,
7115#ifndef CONFIG_USER_ONLY
7116 /* Avoid overhead of an access check that always passes in user-mode */
7117 .accessfn = aa64_zva_access,
7118#endif
7119 },
7120 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7121 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7122 .access = PL0_W, .type = ARM_CP_DC_GZVA,
7123#ifndef CONFIG_USER_ONLY
7124 /* Avoid overhead of an access check that always passes in user-mode */
7125 .accessfn = aa64_zva_access,
7126#endif
7127 },
5463df16
RH
7128};
7129
967aa94f
RH
7130#endif
7131
cb570bd3
RH
7132static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7133 bool isread)
7134{
7135 int el = arm_current_el(env);
7136
7137 if (el == 0) {
7138 uint64_t sctlr = arm_sctlr(env, el);
7139 if (!(sctlr & SCTLR_EnRCTX)) {
7140 return CP_ACCESS_TRAP;
7141 }
7142 } else if (el == 1) {
7143 uint64_t hcr = arm_hcr_el2_eff(env);
7144 if (hcr & HCR_NV) {
7145 return CP_ACCESS_TRAP_EL2;
7146 }
7147 }
7148 return CP_ACCESS_OK;
7149}
7150
7151static const ARMCPRegInfo predinv_reginfo[] = {
7152 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7153 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7154 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7155 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7156 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7157 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7158 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7159 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7160 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7161 /*
7162 * Note the AArch32 opcodes have a different OPC1.
7163 */
7164 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7165 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7166 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7167 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7168 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7169 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7170 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7171 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7172 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
cb570bd3
RH
7173};
7174
957e6155
PM
7175static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7176{
7177 /* Read the high 32 bits of the current CCSIDR */
7178 return extract64(ccsidr_read(env, ri), 32, 32);
7179}
7180
7181static const ARMCPRegInfo ccsidr2_reginfo[] = {
7182 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7183 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7184 .access = PL1_R,
7185 .accessfn = access_aa64_tid2,
7186 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
957e6155
PM
7187};
7188
6a4ef4e5
MZ
7189static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7190 bool isread)
7191{
7192 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7193 return CP_ACCESS_TRAP_EL2;
7194 }
7195
7196 return CP_ACCESS_OK;
7197}
7198
7199static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7200 bool isread)
7201{
7202 if (arm_feature(env, ARM_FEATURE_V8)) {
7203 return access_aa64_tid3(env, ri, isread);
7204 }
7205
7206 return CP_ACCESS_OK;
7207}
7208
f96f3d5f
MZ
7209static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7210 bool isread)
7211{
7212 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7213 return CP_ACCESS_TRAP_EL2;
7214 }
7215
7216 return CP_ACCESS_OK;
7217}
7218
8e228c9e
PM
7219static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7220 const ARMCPRegInfo *ri, bool isread)
7221{
7222 /*
7223 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7224 * in v7A, not in v8A.
7225 */
7226 if (!arm_feature(env, ARM_FEATURE_V8) &&
7227 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7228 (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7229 return CP_ACCESS_TRAP_EL2;
7230 }
7231 return CP_ACCESS_OK;
7232}
7233
f96f3d5f
MZ
7234static const ARMCPRegInfo jazelle_regs[] = {
7235 { .name = "JIDR",
7236 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7237 .access = PL1_R, .accessfn = access_jazelle,
7238 .type = ARM_CP_CONST, .resetvalue = 0 },
7239 { .name = "JOSCR",
7240 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7241 .accessfn = access_joscr_jmcr,
f96f3d5f
MZ
7242 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7243 { .name = "JMCR",
7244 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7245 .accessfn = access_joscr_jmcr,
f96f3d5f 7246 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
f96f3d5f
MZ
7247};
7248
e2a1a461
RH
7249static const ARMCPRegInfo vhe_reginfo[] = {
7250 { .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7251 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7252 .access = PL2_RW,
7253 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2]) },
ed30da8e
RH
7254 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7255 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7256 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7257 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8c94b071
RH
7258#ifndef CONFIG_USER_ONLY
7259 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7260 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7261 .fieldoffset =
7262 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7263 .type = ARM_CP_IO, .access = PL2_RW,
7264 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7265 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7266 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7267 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7268 .resetfn = gt_hv_timer_reset,
7269 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7270 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7271 .type = ARM_CP_IO,
7272 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7273 .access = PL2_RW,
7274 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7275 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
bb5972e4
RH
7276 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7277 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7278 .type = ARM_CP_IO | ARM_CP_ALIAS,
7279 .access = PL2_RW, .accessfn = e2h_access,
7280 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7281 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7282 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7283 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7284 .type = ARM_CP_IO | ARM_CP_ALIAS,
7285 .access = PL2_RW, .accessfn = e2h_access,
7286 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7287 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7288 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7289 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7290 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7291 .access = PL2_RW, .accessfn = e2h_access,
7292 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7293 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7294 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7295 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7296 .access = PL2_RW, .accessfn = e2h_access,
7297 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7298 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7299 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7300 .type = ARM_CP_IO | ARM_CP_ALIAS,
7301 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7302 .access = PL2_RW, .accessfn = e2h_access,
7303 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7304 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7305 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7306 .type = ARM_CP_IO | ARM_CP_ALIAS,
7307 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7308 .access = PL2_RW, .accessfn = e2h_access,
7309 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8c94b071 7310#endif
e2a1a461
RH
7311};
7312
04b07d29
RH
7313#ifndef CONFIG_USER_ONLY
7314static const ARMCPRegInfo ats1e1_reginfo[] = {
7315 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7316 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7317 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7318 .writefn = ats_write64 },
7319 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7320 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7321 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7322 .writefn = ats_write64 },
04b07d29
RH
7323};
7324
7325static const ARMCPRegInfo ats1cp_reginfo[] = {
7326 { .name = "ATS1CPRP",
7327 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7328 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7329 .writefn = ats_write },
7330 { .name = "ATS1CPWP",
7331 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7332 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7333 .writefn = ats_write },
04b07d29
RH
7334};
7335#endif
7336
f6287c24
PM
7337/*
7338 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7339 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7340 * is non-zero, which is never for ARMv7, optionally in ARMv8
7341 * and mandatorily for ARMv8.2 and up.
7342 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7343 * implementation is RAZ/WI we can ignore this detail, as we
7344 * do for ACTLR.
7345 */
7346static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7347 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7348 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
99602377
RH
7349 .access = PL1_RW, .accessfn = access_tacr,
7350 .type = ARM_CP_CONST, .resetvalue = 0 },
f6287c24
PM
7351 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7352 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7353 .access = PL2_RW, .type = ARM_CP_CONST,
7354 .resetvalue = 0 },
f6287c24
PM
7355};
7356
2ceb98c0
PM
7357void register_cp_regs_for_features(ARMCPU *cpu)
7358{
7359 /* Register all the coprocessor registers based on feature bits */
7360 CPUARMState *env = &cpu->env;
7361 if (arm_feature(env, ARM_FEATURE_M)) {
7362 /* M profile has no coprocessor registers */
7363 return;
7364 }
7365
e9aa6c21 7366 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
7367 if (!arm_feature(env, ARM_FEATURE_V8)) {
7368 /* Must go early as it is full of wildcards that may be
7369 * overridden by later definitions.
7370 */
7371 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7372 }
7373
7d57f408 7374 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
7375 /* The ID registers all have impdef reset values */
7376 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
7377 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7378 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7379 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7380 .accessfn = access_aa32_tid3,
8a130a7b 7381 .resetvalue = cpu->isar.id_pfr0 },
96a8b92e
PM
7382 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7383 * the value of the GIC field until after we define these regs.
7384 */
0ff644a7
PM
7385 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7386 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e 7387 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 7388 .accessfn = access_aa32_tid3,
96a8b92e
PM
7389 .readfn = id_pfr1_read,
7390 .writefn = arm_cp_write_ignore },
0ff644a7
PM
7391 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7392 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7393 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7394 .accessfn = access_aa32_tid3,
a6179538 7395 .resetvalue = cpu->isar.id_dfr0 },
0ff644a7
PM
7396 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7397 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7398 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7399 .accessfn = access_aa32_tid3,
8515a092 7400 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
7401 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7402 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7403 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7404 .accessfn = access_aa32_tid3,
10054016 7405 .resetvalue = cpu->isar.id_mmfr0 },
0ff644a7
PM
7406 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7407 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7408 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7409 .accessfn = access_aa32_tid3,
10054016 7410 .resetvalue = cpu->isar.id_mmfr1 },
0ff644a7
PM
7411 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7412 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7413 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7414 .accessfn = access_aa32_tid3,
10054016 7415 .resetvalue = cpu->isar.id_mmfr2 },
0ff644a7
PM
7416 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7417 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7418 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7419 .accessfn = access_aa32_tid3,
10054016 7420 .resetvalue = cpu->isar.id_mmfr3 },
0ff644a7
PM
7421 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7422 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7423 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7424 .accessfn = access_aa32_tid3,
47576b94 7425 .resetvalue = cpu->isar.id_isar0 },
0ff644a7
PM
7426 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7427 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7428 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7429 .accessfn = access_aa32_tid3,
47576b94 7430 .resetvalue = cpu->isar.id_isar1 },
0ff644a7
PM
7431 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7432 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7433 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7434 .accessfn = access_aa32_tid3,
47576b94 7435 .resetvalue = cpu->isar.id_isar2 },
0ff644a7
PM
7436 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7437 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7438 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7439 .accessfn = access_aa32_tid3,
47576b94 7440 .resetvalue = cpu->isar.id_isar3 },
0ff644a7
PM
7441 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7442 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7443 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7444 .accessfn = access_aa32_tid3,
47576b94 7445 .resetvalue = cpu->isar.id_isar4 },
0ff644a7
PM
7446 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7447 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7448 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7449 .accessfn = access_aa32_tid3,
47576b94 7450 .resetvalue = cpu->isar.id_isar5 },
e20d84c1
PM
7451 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7452 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7453 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7454 .accessfn = access_aa32_tid3,
10054016 7455 .resetvalue = cpu->isar.id_mmfr4 },
802abf40 7456 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7457 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7458 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7459 .accessfn = access_aa32_tid3,
47576b94 7460 .resetvalue = cpu->isar.id_isar6 },
8515a092
PM
7461 };
7462 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
7463 define_arm_cp_regs(cpu, v6_cp_reginfo);
7464 } else {
7465 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7466 }
4d31c596
PM
7467 if (arm_feature(env, ARM_FEATURE_V6K)) {
7468 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7469 }
5e5cf9e3 7470 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 7471 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
7472 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7473 }
327dd510
AL
7474 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7475 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7476 }
e9aa6c21 7477 if (arm_feature(env, ARM_FEATURE_V7)) {
776d4e5c 7478 ARMCPRegInfo clidr = {
7da845b0
PM
7479 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7480 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
630fcd4d
MZ
7481 .access = PL1_R, .type = ARM_CP_CONST,
7482 .accessfn = access_aa64_tid2,
7483 .resetvalue = cpu->clidr
776d4e5c 7484 };
776d4e5c 7485 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 7486 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 7487 define_debug_regs(cpu);
24183fb6 7488 define_pmu_regs(cpu);
7d57f408
PM
7489 } else {
7490 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 7491 }
b0d2b7d0 7492 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
7493 /* AArch64 ID registers, which all have impdef reset values.
7494 * Note that within the ID register ranges the unused slots
7495 * must all RAZ, not UNDEF; future architecture versions may
7496 * define new registers here.
7497 */
e60cef86 7498 ARMCPRegInfo v8_idregs[] = {
976b99b6
AB
7499 /*
7500 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7501 * emulation because we don't know the right value for the
7502 * GIC field until after we define these regs.
96a8b92e 7503 */
e60cef86
PM
7504 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7505 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
976b99b6
AB
7506 .access = PL1_R,
7507#ifdef CONFIG_USER_ONLY
7508 .type = ARM_CP_CONST,
7509 .resetvalue = cpu->isar.id_aa64pfr0
7510#else
7511 .type = ARM_CP_NO_RAW,
6a4ef4e5 7512 .accessfn = access_aa64_tid3,
96a8b92e 7513 .readfn = id_aa64pfr0_read,
976b99b6
AB
7514 .writefn = arm_cp_write_ignore
7515#endif
7516 },
e60cef86
PM
7517 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7518 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7519 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7520 .accessfn = access_aa64_tid3,
47576b94 7521 .resetvalue = cpu->isar.id_aa64pfr1},
e20d84c1
PM
7522 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7523 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7524 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7525 .accessfn = access_aa64_tid3,
e20d84c1
PM
7526 .resetvalue = 0 },
7527 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7528 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7529 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7530 .accessfn = access_aa64_tid3,
e20d84c1 7531 .resetvalue = 0 },
9516d772 7532 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7533 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7534 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7535 .accessfn = access_aa64_tid3,
2dc10fa2 7536 .resetvalue = cpu->isar.id_aa64zfr0 },
e20d84c1
PM
7537 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7538 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7539 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7540 .accessfn = access_aa64_tid3,
e20d84c1
PM
7541 .resetvalue = 0 },
7542 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7543 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7544 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7545 .accessfn = access_aa64_tid3,
e20d84c1
PM
7546 .resetvalue = 0 },
7547 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7548 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7549 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7550 .accessfn = access_aa64_tid3,
e20d84c1 7551 .resetvalue = 0 },
e60cef86
PM
7552 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7553 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7554 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7555 .accessfn = access_aa64_tid3,
2a609df8 7556 .resetvalue = cpu->isar.id_aa64dfr0 },
e60cef86
PM
7557 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7558 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7559 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7560 .accessfn = access_aa64_tid3,
2a609df8 7561 .resetvalue = cpu->isar.id_aa64dfr1 },
e20d84c1
PM
7562 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7563 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7564 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7565 .accessfn = access_aa64_tid3,
e20d84c1
PM
7566 .resetvalue = 0 },
7567 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7568 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7569 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7570 .accessfn = access_aa64_tid3,
e20d84c1 7571 .resetvalue = 0 },
e60cef86
PM
7572 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7573 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7574 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7575 .accessfn = access_aa64_tid3,
e60cef86
PM
7576 .resetvalue = cpu->id_aa64afr0 },
7577 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7578 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7579 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7580 .accessfn = access_aa64_tid3,
e60cef86 7581 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
7582 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7583 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7584 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7585 .accessfn = access_aa64_tid3,
e20d84c1
PM
7586 .resetvalue = 0 },
7587 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7588 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7589 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7590 .accessfn = access_aa64_tid3,
e20d84c1 7591 .resetvalue = 0 },
e60cef86
PM
7592 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7593 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7594 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7595 .accessfn = access_aa64_tid3,
47576b94 7596 .resetvalue = cpu->isar.id_aa64isar0 },
e60cef86
PM
7597 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7598 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7599 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7600 .accessfn = access_aa64_tid3,
47576b94 7601 .resetvalue = cpu->isar.id_aa64isar1 },
e20d84c1
PM
7602 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7603 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7604 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7605 .accessfn = access_aa64_tid3,
e20d84c1
PM
7606 .resetvalue = 0 },
7607 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7608 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7609 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7610 .accessfn = access_aa64_tid3,
e20d84c1
PM
7611 .resetvalue = 0 },
7612 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7613 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7614 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7615 .accessfn = access_aa64_tid3,
e20d84c1
PM
7616 .resetvalue = 0 },
7617 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7618 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7619 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7620 .accessfn = access_aa64_tid3,
e20d84c1
PM
7621 .resetvalue = 0 },
7622 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7623 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7624 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7625 .accessfn = access_aa64_tid3,
e20d84c1
PM
7626 .resetvalue = 0 },
7627 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7628 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7629 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7630 .accessfn = access_aa64_tid3,
e20d84c1 7631 .resetvalue = 0 },
e60cef86
PM
7632 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7633 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7634 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7635 .accessfn = access_aa64_tid3,
3dc91ddb 7636 .resetvalue = cpu->isar.id_aa64mmfr0 },
e60cef86
PM
7637 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7638 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7639 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7640 .accessfn = access_aa64_tid3,
3dc91ddb 7641 .resetvalue = cpu->isar.id_aa64mmfr1 },
64761e10 7642 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7643 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7644 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7645 .accessfn = access_aa64_tid3,
64761e10 7646 .resetvalue = cpu->isar.id_aa64mmfr2 },
e20d84c1
PM
7647 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7648 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7649 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7650 .accessfn = access_aa64_tid3,
e20d84c1
PM
7651 .resetvalue = 0 },
7652 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7653 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7654 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7655 .accessfn = access_aa64_tid3,
e20d84c1
PM
7656 .resetvalue = 0 },
7657 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7658 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7659 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7660 .accessfn = access_aa64_tid3,
e20d84c1
PM
7661 .resetvalue = 0 },
7662 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7663 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7664 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7665 .accessfn = access_aa64_tid3,
e20d84c1
PM
7666 .resetvalue = 0 },
7667 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7668 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7669 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7670 .accessfn = access_aa64_tid3,
e20d84c1 7671 .resetvalue = 0 },
a50c0f51
PM
7672 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7673 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7674 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7675 .accessfn = access_aa64_tid3,
47576b94 7676 .resetvalue = cpu->isar.mvfr0 },
a50c0f51
PM
7677 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7678 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7679 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7680 .accessfn = access_aa64_tid3,
47576b94 7681 .resetvalue = cpu->isar.mvfr1 },
a50c0f51
PM
7682 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7683 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7684 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7685 .accessfn = access_aa64_tid3,
47576b94 7686 .resetvalue = cpu->isar.mvfr2 },
e20d84c1
PM
7687 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7688 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7689 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7690 .accessfn = access_aa64_tid3,
e20d84c1 7691 .resetvalue = 0 },
1d51bc96 7692 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7693 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7694 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7695 .accessfn = access_aa64_tid3,
1d51bc96 7696 .resetvalue = cpu->isar.id_pfr2 },
e20d84c1
PM
7697 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7698 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7699 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7700 .accessfn = access_aa64_tid3,
e20d84c1
PM
7701 .resetvalue = 0 },
7702 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7703 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7704 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7705 .accessfn = access_aa64_tid3,
e20d84c1
PM
7706 .resetvalue = 0 },
7707 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7708 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7709 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7710 .accessfn = access_aa64_tid3,
e20d84c1 7711 .resetvalue = 0 },
4054bfa9
AF
7712 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7713 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7714 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7715 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
4054bfa9
AF
7716 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7717 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7718 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7719 .resetvalue = cpu->pmceid0 },
7720 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7721 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7722 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7723 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
4054bfa9
AF
7724 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7725 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7726 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7727 .resetvalue = cpu->pmceid1 },
e60cef86 7728 };
6c5c0fec 7729#ifdef CONFIG_USER_ONLY
10b0220e 7730 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
6c5c0fec
AB
7731 { .name = "ID_AA64PFR0_EL1",
7732 .exported_bits = 0x000f000f00ff0000,
7733 .fixed_bits = 0x0000000000000011 },
7734 { .name = "ID_AA64PFR1_EL1",
7735 .exported_bits = 0x00000000000000f0 },
d040242e
AB
7736 { .name = "ID_AA64PFR*_EL1_RESERVED",
7737 .is_glob = true },
6c5c0fec
AB
7738 { .name = "ID_AA64ZFR0_EL1" },
7739 { .name = "ID_AA64MMFR0_EL1",
7740 .fixed_bits = 0x00000000ff000000 },
7741 { .name = "ID_AA64MMFR1_EL1" },
d040242e
AB
7742 { .name = "ID_AA64MMFR*_EL1_RESERVED",
7743 .is_glob = true },
6c5c0fec
AB
7744 { .name = "ID_AA64DFR0_EL1",
7745 .fixed_bits = 0x0000000000000006 },
7746 { .name = "ID_AA64DFR1_EL1" },
d040242e
AB
7747 { .name = "ID_AA64DFR*_EL1_RESERVED",
7748 .is_glob = true },
7749 { .name = "ID_AA64AFR*",
7750 .is_glob = true },
6c5c0fec
AB
7751 { .name = "ID_AA64ISAR0_EL1",
7752 .exported_bits = 0x00fffffff0fffff0 },
7753 { .name = "ID_AA64ISAR1_EL1",
7754 .exported_bits = 0x000000f0ffffffff },
d040242e
AB
7755 { .name = "ID_AA64ISAR*_EL1_RESERVED",
7756 .is_glob = true },
6c5c0fec
AB
7757 };
7758 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7759#endif
be8e8128
GB
7760 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7761 if (!arm_feature(env, ARM_FEATURE_EL3) &&
7762 !arm_feature(env, ARM_FEATURE_EL2)) {
7763 ARMCPRegInfo rvbar = {
7764 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7765 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
7766 .access = PL1_R,
7767 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
be8e8128
GB
7768 };
7769 define_one_arm_cp_reg(cpu, &rvbar);
7770 }
e60cef86 7771 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
7772 define_arm_cp_regs(cpu, v8_cp_reginfo);
7773 }
99a90811
RH
7774
7775 /*
7776 * Register the base EL2 cpregs.
7777 * Pre v8, these registers are implemented only as part of the
7778 * Virtualization Extensions (EL2 present). Beginning with v8,
7779 * if EL2 is missing but EL3 is enabled, mostly these become
7780 * RES0 from EL3, with some specific exceptions.
7781 */
7782 if (arm_feature(env, ARM_FEATURE_EL2)
7783 || (arm_feature(env, ARM_FEATURE_EL3)
7784 && arm_feature(env, ARM_FEATURE_V8))) {
f0d574d6 7785 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
7786 ARMCPRegInfo vpidr_regs[] = {
7787 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7788 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7789 .access = PL2_RW, .accessfn = access_el3_aa32ns,
696ba377
RH
7790 .resetvalue = cpu->midr,
7791 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
36476562 7792 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
7793 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7794 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7795 .access = PL2_RW, .resetvalue = cpu->midr,
696ba377 7796 .type = ARM_CP_EL3_NO_EL2_C_NZ,
731de9e6 7797 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7798 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7799 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7800 .access = PL2_RW, .accessfn = access_el3_aa32ns,
696ba377
RH
7801 .resetvalue = vmpidr_def,
7802 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
36476562 7803 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
7804 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7805 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
696ba377
RH
7806 .access = PL2_RW, .resetvalue = vmpidr_def,
7807 .type = ARM_CP_EL3_NO_EL2_C_NZ,
f0d574d6 7808 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
7809 };
7810 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 7811 define_arm_cp_regs(cpu, el2_cp_reginfo);
ce4afed8
PM
7812 if (arm_feature(env, ARM_FEATURE_V8)) {
7813 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7814 }
e9152ee9
RDC
7815 if (cpu_isar_feature(aa64_sel2, cpu)) {
7816 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
7817 }
be8e8128
GB
7818 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7819 if (!arm_feature(env, ARM_FEATURE_EL3)) {
7820 ARMCPRegInfo rvbar = {
7821 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7822 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
7823 .access = PL2_R,
7824 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
be8e8128
GB
7825 };
7826 define_one_arm_cp_reg(cpu, &rvbar);
7827 }
3b685ba7 7828 }
99a90811
RH
7829
7830 /* Register the base EL3 cpregs. */
81547d66 7831 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 7832 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
7833 ARMCPRegInfo el3_regs[] = {
7834 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7835 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
7836 .access = PL3_R,
7837 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7838 },
e24fdd23
PM
7839 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7840 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7841 .access = PL3_RW,
7842 .raw_writefn = raw_write, .writefn = sctlr_write,
7843 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7844 .resetvalue = cpu->reset_sctlr },
be8e8128 7845 };
e24fdd23
PM
7846
7847 define_arm_cp_regs(cpu, el3_regs);
81547d66 7848 }
2f027fc5
PM
7849 /* The behaviour of NSACR is sufficiently various that we don't
7850 * try to describe it in a single reginfo:
7851 * if EL3 is 64 bit, then trap to EL3 from S EL1,
7852 * reads as constant 0xc00 from NS EL1 and NS EL2
7853 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7854 * if v7 without EL3, register doesn't exist
7855 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7856 */
7857 if (arm_feature(env, ARM_FEATURE_EL3)) {
7858 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
10b0220e 7859 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
7860 .name = "NSACR", .type = ARM_CP_CONST,
7861 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7862 .access = PL1_RW, .accessfn = nsacr_access,
7863 .resetvalue = 0xc00
7864 };
7865 define_one_arm_cp_reg(cpu, &nsacr);
7866 } else {
10b0220e 7867 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
7868 .name = "NSACR",
7869 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7870 .access = PL3_RW | PL1_R,
7871 .resetvalue = 0,
7872 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
7873 };
7874 define_one_arm_cp_reg(cpu, &nsacr);
7875 }
7876 } else {
7877 if (arm_feature(env, ARM_FEATURE_V8)) {
10b0220e 7878 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
7879 .name = "NSACR", .type = ARM_CP_CONST,
7880 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7881 .access = PL1_R,
7882 .resetvalue = 0xc00
7883 };
7884 define_one_arm_cp_reg(cpu, &nsacr);
7885 }
7886 }
7887
452a0955 7888 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
7889 if (arm_feature(env, ARM_FEATURE_V6)) {
7890 /* PMSAv6 not implemented */
7891 assert(arm_feature(env, ARM_FEATURE_V7));
7892 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7893 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
7894 } else {
7895 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
7896 }
18032bec 7897 } else {
8e5d75c9 7898 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec 7899 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4036b7d1
PM
7900 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
7901 if (cpu_isar_feature(aa32_hpd, cpu)) {
ab638a32
RH
7902 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
7903 }
18032bec 7904 }
c326b979
PM
7905 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
7906 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
7907 }
6cc7a3ae
PM
7908 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
7909 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
7910 }
4a501606
PM
7911 if (arm_feature(env, ARM_FEATURE_VAPA)) {
7912 define_arm_cp_regs(cpu, vapa_cp_reginfo);
7913 }
c4804214
PM
7914 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
7915 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
7916 }
7917 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
7918 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
7919 }
7920 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
7921 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
7922 }
18032bec
PM
7923 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
7924 define_arm_cp_regs(cpu, omap_cp_reginfo);
7925 }
34f90529
PM
7926 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
7927 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
7928 }
1047b9d7
PM
7929 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7930 define_arm_cp_regs(cpu, xscale_cp_reginfo);
7931 }
7932 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
7933 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
7934 }
7ac681cf
PM
7935 if (arm_feature(env, ARM_FEATURE_LPAE)) {
7936 define_arm_cp_regs(cpu, lpae_cp_reginfo);
7937 }
873b73c0 7938 if (cpu_isar_feature(aa32_jazelle, cpu)) {
f96f3d5f
MZ
7939 define_arm_cp_regs(cpu, jazelle_regs);
7940 }
7884849c
PM
7941 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
7942 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
7943 * be read-only (ie write causes UNDEF exception).
7944 */
7945 {
00a29f3d
PM
7946 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
7947 /* Pre-v8 MIDR space.
7948 * Note that the MIDR isn't a simple constant register because
7884849c
PM
7949 * of the TI925 behaviour where writes to another register can
7950 * cause the MIDR value to change.
97ce8d61
PC
7951 *
7952 * Unimplemented registers in the c15 0 0 0 space default to
7953 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
7954 * and friends override accordingly.
7884849c
PM
7955 */
7956 { .name = "MIDR",
97ce8d61 7957 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 7958 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 7959 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 7960 .readfn = midr_read,
97ce8d61
PC
7961 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7962 .type = ARM_CP_OVERRIDE },
7884849c
PM
7963 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
7964 { .name = "DUMMY",
7965 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
7966 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7967 { .name = "DUMMY",
7968 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
7969 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7970 { .name = "DUMMY",
7971 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
7972 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7973 { .name = "DUMMY",
7974 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
7975 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7976 { .name = "DUMMY",
7977 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
7978 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7884849c 7979 };
00a29f3d 7980 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
7981 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
7982 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
7983 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
7984 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7985 .readfn = midr_read },
ac00c79f
SF
7986 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
7987 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7988 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
7989 .access = PL1_R, .resetvalue = cpu->midr },
7990 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
7991 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
7992 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
7993 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
7994 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
93fbc983
MZ
7995 .access = PL1_R,
7996 .accessfn = access_aa64_tid1,
7997 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
7998 };
7999 ARMCPRegInfo id_cp_reginfo[] = {
8000 /* These are common to v8 and pre-v8 */
8001 { .name = "CTR",
8002 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
630fcd4d
MZ
8003 .access = PL1_R, .accessfn = ctr_el0_access,
8004 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
00a29f3d
PM
8005 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8006 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8007 .access = PL0_R, .accessfn = ctr_el0_access,
8008 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8009 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8010 { .name = "TCMTR",
8011 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
93fbc983
MZ
8012 .access = PL1_R,
8013 .accessfn = access_aa32_tid1,
8014 .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d 8015 };
8085ce63
PC
8016 /* TLBTR is specific to VMSA */
8017 ARMCPRegInfo id_tlbtr_reginfo = {
8018 .name = "TLBTR",
8019 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
93fbc983
MZ
8020 .access = PL1_R,
8021 .accessfn = access_aa32_tid1,
8022 .type = ARM_CP_CONST, .resetvalue = 0,
8085ce63 8023 };
3281af81
PC
8024 /* MPUIR is specific to PMSA V6+ */
8025 ARMCPRegInfo id_mpuir_reginfo = {
8026 .name = "MPUIR",
8027 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8028 .access = PL1_R, .type = ARM_CP_CONST,
8029 .resetvalue = cpu->pmsav7_dregion << 8
8030 };
10b0220e 8031 static const ARMCPRegInfo crn0_wi_reginfo = {
7884849c
PM
8032 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8033 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8034 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8035 };
6c5c0fec 8036#ifdef CONFIG_USER_ONLY
10b0220e 8037 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
6c5c0fec
AB
8038 { .name = "MIDR_EL1",
8039 .exported_bits = 0x00000000ffffffff },
8040 { .name = "REVIDR_EL1" },
6c5c0fec
AB
8041 };
8042 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8043#endif
7884849c
PM
8044 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8045 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5809ac57 8046 size_t i;
7884849c 8047 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
8048 * whole space. Then update the specific ID registers to allow write
8049 * access, so that they ignore writes rather than causing them to
8050 * UNDEF.
7884849c
PM
8051 */
8052 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
5809ac57
RH
8053 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8054 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
00a29f3d 8055 }
5809ac57
RH
8056 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8057 id_cp_reginfo[i].access = PL1_RW;
7884849c 8058 }
10006112 8059 id_mpuir_reginfo.access = PL1_RW;
3281af81 8060 id_tlbtr_reginfo.access = PL1_RW;
7884849c 8061 }
00a29f3d
PM
8062 if (arm_feature(env, ARM_FEATURE_V8)) {
8063 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8064 } else {
8065 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8066 }
a703eda1 8067 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 8068 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 8069 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
8070 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8071 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 8072 }
7884849c
PM
8073 }
8074
97ce8d61 8075 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
52264166
AB
8076 ARMCPRegInfo mpidr_cp_reginfo[] = {
8077 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8078 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8079 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
52264166
AB
8080 };
8081#ifdef CONFIG_USER_ONLY
10b0220e 8082 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
52264166
AB
8083 { .name = "MPIDR_EL1",
8084 .fixed_bits = 0x0000000080000000 },
52264166
AB
8085 };
8086 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8087#endif
97ce8d61
PC
8088 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8089 }
8090
2771db27 8091 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
8092 ARMCPRegInfo auxcr_reginfo[] = {
8093 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8094 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
99602377
RH
8095 .access = PL1_RW, .accessfn = access_tacr,
8096 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
834a6c69
PM
8097 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8098 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8099 .access = PL2_RW, .type = ARM_CP_CONST,
8100 .resetvalue = 0 },
8101 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8102 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8103 .access = PL3_RW, .type = ARM_CP_CONST,
8104 .resetvalue = 0 },
2771db27 8105 };
834a6c69 8106 define_arm_cp_regs(cpu, auxcr_reginfo);
f6287c24
PM
8107 if (cpu_isar_feature(aa32_ac2, cpu)) {
8108 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
0e0456ab 8109 }
2771db27
PM
8110 }
8111
d8ba780b 8112 if (arm_feature(env, ARM_FEATURE_CBAR)) {
d56974af
LM
8113 /*
8114 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8115 * There are two flavours:
8116 * (1) older 32-bit only cores have a simple 32-bit CBAR
8117 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8118 * 32-bit register visible to AArch32 at a different encoding
8119 * to the "flavour 1" register and with the bits rearranged to
8120 * be able to squash a 64-bit address into the 32-bit view.
8121 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8122 * in future if we support AArch32-only configs of some of the
8123 * AArch64 cores we might need to add a specific feature flag
8124 * to indicate cores with "flavour 2" CBAR.
8125 */
f318cec6
PM
8126 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8127 /* 32 bit view is [31:18] 0...0 [43:32]. */
8128 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8129 | extract64(cpu->reset_cbar, 32, 12);
8130 ARMCPRegInfo cbar_reginfo[] = {
8131 { .name = "CBAR",
8132 .type = ARM_CP_CONST,
d56974af
LM
8133 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8134 .access = PL1_R, .resetvalue = cbar32 },
f318cec6
PM
8135 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8136 .type = ARM_CP_CONST,
8137 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
d56974af 8138 .access = PL1_R, .resetvalue = cpu->reset_cbar },
f318cec6
PM
8139 };
8140 /* We don't implement a r/w 64 bit CBAR currently */
8141 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8142 define_arm_cp_regs(cpu, cbar_reginfo);
8143 } else {
8144 ARMCPRegInfo cbar = {
8145 .name = "CBAR",
8146 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8147 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8148 .fieldoffset = offsetof(CPUARMState,
8149 cp15.c15_config_base_address)
8150 };
8151 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8152 cbar.access = PL1_R;
8153 cbar.fieldoffset = 0;
8154 cbar.type = ARM_CP_CONST;
8155 }
8156 define_one_arm_cp_reg(cpu, &cbar);
8157 }
d8ba780b
PC
8158 }
8159
91db4642 8160 if (arm_feature(env, ARM_FEATURE_VBAR)) {
10b0220e 8161 static const ARMCPRegInfo vbar_cp_reginfo[] = {
91db4642
CLG
8162 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8163 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8164 .access = PL1_RW, .writefn = vbar_write,
8165 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8166 offsetof(CPUARMState, cp15.vbar_ns) },
8167 .resetvalue = 0 },
91db4642
CLG
8168 };
8169 define_arm_cp_regs(cpu, vbar_cp_reginfo);
8170 }
8171
2771db27
PM
8172 /* Generic registers whose values depend on the implementation */
8173 {
8174 ARMCPRegInfo sctlr = {
5ebafdf3 8175 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9 8176 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
84929218 8177 .access = PL1_RW, .accessfn = access_tvm_trvm,
137feaa9
FA
8178 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8179 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
8180 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8181 .raw_writefn = raw_write,
2771db27
PM
8182 };
8183 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8184 /* Normally we would always end the TB on an SCTLR write, but Linux
8185 * arch/arm/mach-pxa/sleep.S expects two instructions following
8186 * an MMU enable to execute from cache. Imitate this behaviour.
8187 */
8188 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8189 }
8190 define_one_arm_cp_reg(cpu, &sctlr);
8191 }
5be5e8ed 8192
2d7137c1 8193 if (cpu_isar_feature(aa64_lor, cpu)) {
2d7137c1
RH
8194 define_arm_cp_regs(cpu, lor_reginfo);
8195 }
220f508f
RH
8196 if (cpu_isar_feature(aa64_pan, cpu)) {
8197 define_one_arm_cp_reg(cpu, &pan_reginfo);
8198 }
04b07d29
RH
8199#ifndef CONFIG_USER_ONLY
8200 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8201 define_arm_cp_regs(cpu, ats1e1_reginfo);
8202 }
8203 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8204 define_arm_cp_regs(cpu, ats1cp_reginfo);
8205 }
8206#endif
9eeb7a1c
RH
8207 if (cpu_isar_feature(aa64_uao, cpu)) {
8208 define_one_arm_cp_reg(cpu, &uao_reginfo);
8209 }
2d7137c1 8210
dc8b1853
RC
8211 if (cpu_isar_feature(aa64_dit, cpu)) {
8212 define_one_arm_cp_reg(cpu, &dit_reginfo);
8213 }
f2f68a78
RC
8214 if (cpu_isar_feature(aa64_ssbs, cpu)) {
8215 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8216 }
dc8b1853 8217
e2a1a461
RH
8218 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8219 define_arm_cp_regs(cpu, vhe_reginfo);
8220 }
8221
cd208a1c 8222 if (cpu_isar_feature(aa64_sve, cpu)) {
60360d82 8223 define_arm_cp_regs(cpu, zcr_reginfo);
5be5e8ed 8224 }
967aa94f
RH
8225
8226#ifdef TARGET_AARCH64
8227 if (cpu_isar_feature(aa64_pauth, cpu)) {
8228 define_arm_cp_regs(cpu, pauth_reginfo);
8229 }
de390645
RH
8230 if (cpu_isar_feature(aa64_rndr, cpu)) {
8231 define_arm_cp_regs(cpu, rndr_reginfo);
8232 }
84940ed8
RC
8233 if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8234 define_arm_cp_regs(cpu, tlbirange_reginfo);
8235 }
7113d618
RC
8236 if (cpu_isar_feature(aa64_tlbios, cpu)) {
8237 define_arm_cp_regs(cpu, tlbios_reginfo);
8238 }
0d57b499
BM
8239#ifndef CONFIG_USER_ONLY
8240 /* Data Cache clean instructions up to PoP */
8241 if (cpu_isar_feature(aa64_dcpop, cpu)) {
8242 define_one_arm_cp_reg(cpu, dcpop_reg);
8243
8244 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8245 define_one_arm_cp_reg(cpu, dcpodp_reg);
8246 }
8247 }
8248#endif /*CONFIG_USER_ONLY*/
4b779ceb
RH
8249
8250 /*
8251 * If full MTE is enabled, add all of the system registers.
8252 * If only "instructions available at EL0" are enabled,
8253 * then define only a RAZ/WI version of PSTATE.TCO.
8254 */
8255 if (cpu_isar_feature(aa64_mte, cpu)) {
8256 define_arm_cp_regs(cpu, mte_reginfo);
5463df16 8257 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb
RH
8258 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8259 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
5463df16 8260 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb 8261 }
967aa94f 8262#endif
cb570bd3 8263
22e57073 8264 if (cpu_isar_feature(any_predinv, cpu)) {
cb570bd3
RH
8265 define_arm_cp_regs(cpu, predinv_reginfo);
8266 }
e2cce18f 8267
957e6155
PM
8268 if (cpu_isar_feature(any_ccidx, cpu)) {
8269 define_arm_cp_regs(cpu, ccsidr2_reginfo);
8270 }
8271
e2cce18f
RH
8272#ifndef CONFIG_USER_ONLY
8273 /*
8274 * Register redirections and aliases must be done last,
8275 * after the registers from the other extensions have been defined.
8276 */
8277 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8278 define_arm_vh_e2h_redirects_aliases(cpu);
8279 }
8280#endif
2ceb98c0
PM
8281}
8282
777dc784
PM
8283/* Sort alphabetically by type name, except for "any". */
8284static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 8285{
777dc784
PM
8286 ObjectClass *class_a = (ObjectClass *)a;
8287 ObjectClass *class_b = (ObjectClass *)b;
8288 const char *name_a, *name_b;
5adb4839 8289
777dc784
PM
8290 name_a = object_class_get_name(class_a);
8291 name_b = object_class_get_name(class_b);
51492fd1 8292 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 8293 return 1;
51492fd1 8294 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
8295 return -1;
8296 } else {
8297 return strcmp(name_a, name_b);
5adb4839
PB
8298 }
8299}
8300
777dc784 8301static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 8302{
777dc784 8303 ObjectClass *oc = data;
51492fd1
AF
8304 const char *typename;
8305 char *name;
3371d272 8306
51492fd1
AF
8307 typename = object_class_get_name(oc);
8308 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
0442428a 8309 qemu_printf(" %s\n", name);
51492fd1 8310 g_free(name);
777dc784
PM
8311}
8312
0442428a 8313void arm_cpu_list(void)
777dc784 8314{
777dc784
PM
8315 GSList *list;
8316
8317 list = object_class_get_list(TYPE_ARM_CPU, false);
8318 list = g_slist_sort(list, arm_cpu_list_compare);
0442428a
MA
8319 qemu_printf("Available CPUs:\n");
8320 g_slist_foreach(list, arm_cpu_list_entry, NULL);
777dc784 8321 g_slist_free(list);
40f137e1
PB
8322}
8323
78027bb6
CR
8324static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8325{
8326 ObjectClass *oc = data;
8327 CpuDefinitionInfoList **cpu_list = user_data;
78027bb6
CR
8328 CpuDefinitionInfo *info;
8329 const char *typename;
8330
8331 typename = object_class_get_name(oc);
8332 info = g_malloc0(sizeof(*info));
8333 info->name = g_strndup(typename,
8334 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 8335 info->q_typename = g_strdup(typename);
78027bb6 8336
54aa3de7 8337 QAPI_LIST_PREPEND(*cpu_list, info);
78027bb6
CR
8338}
8339
25a9d6ca 8340CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
78027bb6
CR
8341{
8342 CpuDefinitionInfoList *cpu_list = NULL;
8343 GSList *list;
8344
8345 list = object_class_get_list(TYPE_ARM_CPU, false);
8346 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8347 g_slist_free(list);
8348
8349 return cpu_list;
8350}
8351
1859f8c3
RH
8352/*
8353 * Private utility function for define_one_arm_cp_reg_with_opaque():
8354 * add a single reginfo struct to the hash table.
8355 */
6e6efd61 8356static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
cbe64585
RH
8357 void *opaque, CPState state,
8358 CPSecureState secstate,
9c513e78
AB
8359 int crm, int opc1, int opc2,
8360 const char *name)
6e6efd61 8361{
696ba377 8362 CPUARMState *env = &cpu->env;
5860362d 8363 uint32_t key;
c27f5d3a 8364 ARMCPRegInfo *r2;
4c8c4541
RH
8365 bool is64 = r->type & ARM_CP_64BIT;
8366 bool ns = secstate & ARM_CP_SECSTATE_NS;
cac65299 8367 int cp = r->cp;
c27f5d3a 8368 size_t name_len;
696ba377 8369 bool make_const;
c27f5d3a 8370
cac65299
RH
8371 switch (state) {
8372 case ARM_CP_STATE_AA32:
8373 /* We assume it is a cp15 register if the .cp field is left unset. */
8374 if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
8375 cp = 15;
8376 }
8377 key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
8378 break;
8379 case ARM_CP_STATE_AA64:
8380 /*
8381 * To allow abbreviation of ARMCPRegInfo definitions, we treat
8382 * cp == 0 as equivalent to the value for "standard guest-visible
8383 * sysreg". STATE_BOTH definitions are also always "standard sysreg"
8384 * in their AArch64 view (the .cp value may be non-zero for the
8385 * benefit of the AArch32 view).
8386 */
8387 if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8388 cp = CP_REG_ARM64_SYSREG_CP;
8389 }
8390 key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
8391 break;
8392 default:
8393 g_assert_not_reached();
8394 }
8395
dc44545b
RH
8396 /* Overriding of an existing definition must be explicitly requested. */
8397 if (!(r->type & ARM_CP_OVERRIDE)) {
8398 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
8399 if (oldreg) {
8400 assert(oldreg->type & ARM_CP_OVERRIDE);
8401 }
8402 }
8403
696ba377
RH
8404 /*
8405 * Eliminate registers that are not present because the EL is missing.
8406 * Doing this here makes it easier to put all registers for a given
8407 * feature into the same ARMCPRegInfo array and define them all at once.
8408 */
8409 make_const = false;
8410 if (arm_feature(env, ARM_FEATURE_EL3)) {
8411 /*
8412 * An EL2 register without EL2 but with EL3 is (usually) RES0.
8413 * See rule RJFFP in section D1.1.3 of DDI0487H.a.
8414 */
8415 int min_el = ctz32(r->access) / 2;
8416 if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
8417 if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
8418 return;
8419 }
8420 make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
8421 }
8422 } else {
8423 CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
8424 ? PL2_RW : PL1_RW);
8425 if ((r->access & max_el) == 0) {
8426 return;
8427 }
8428 }
8429
c27f5d3a
RH
8430 /* Combine cpreg and name into one allocation. */
8431 name_len = strlen(name) + 1;
8432 r2 = g_malloc(sizeof(*r2) + name_len);
8433 *r2 = *r;
8434 r2->name = memcpy(r2 + 1, name, name_len);
3f3c82a5 8435
cc946d96
RH
8436 /*
8437 * Update fields to match the instantiation, overwiting wildcards
8438 * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
3f3c82a5 8439 */
cc946d96
RH
8440 r2->cp = cp;
8441 r2->crm = crm;
8442 r2->opc1 = opc1;
8443 r2->opc2 = opc2;
8444 r2->state = state;
3f3c82a5 8445 r2->secure = secstate;
cc946d96
RH
8446 if (opaque) {
8447 r2->opaque = opaque;
8448 }
3f3c82a5 8449
696ba377
RH
8450 if (make_const) {
8451 /* This should not have been a very special register to begin. */
8452 int old_special = r2->type & ARM_CP_SPECIAL_MASK;
8453 assert(old_special == 0 || old_special == ARM_CP_NOP);
1859f8c3 8454 /*
696ba377
RH
8455 * Set the special function to CONST, retaining the other flags.
8456 * This is important for e.g. ARM_CP_SVE so that we still
8457 * take the SVE trap if CPTR_EL3.EZ == 0.
f5a0a5a5 8458 */
696ba377
RH
8459 r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
8460 /*
8461 * Usually, these registers become RES0, but there are a few
8462 * special cases like VPIDR_EL2 which have a constant non-zero
8463 * value with writes ignored.
8464 */
8465 if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
8466 r2->resetvalue = 0;
8467 }
8468 /*
8469 * ARM_CP_CONST has precedence, so removing the callbacks and
8470 * offsets are not strictly necessary, but it is potentially
8471 * less confusing to debug later.
8472 */
8473 r2->readfn = NULL;
8474 r2->writefn = NULL;
8475 r2->raw_readfn = NULL;
8476 r2->raw_writefn = NULL;
8477 r2->resetfn = NULL;
8478 r2->fieldoffset = 0;
8479 r2->bank_fieldoffsets[0] = 0;
8480 r2->bank_fieldoffsets[1] = 0;
8481 } else {
8482 bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
3f3c82a5 8483
10748a96 8484 if (isbanked) {
1859f8c3 8485 /*
696ba377
RH
8486 * Register is banked (using both entries in array).
8487 * Overwriting fieldoffset as the array is only used to define
8488 * banked registers but later only fieldoffset is used.
3f3c82a5 8489 */
696ba377
RH
8490 r2->fieldoffset = r->bank_fieldoffsets[ns];
8491 }
8492 if (state == ARM_CP_STATE_AA32) {
8493 if (isbanked) {
8494 /*
8495 * If the register is banked then we don't need to migrate or
8496 * reset the 32-bit instance in certain cases:
8497 *
8498 * 1) If the register has both 32-bit and 64-bit instances
8499 * then we can count on the 64-bit instance taking care
8500 * of the non-secure bank.
8501 * 2) If ARMv8 is enabled then we can count on a 64-bit
8502 * version taking care of the secure bank. This requires
8503 * that separate 32 and 64-bit definitions are provided.
8504 */
8505 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8506 (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
8507 r2->type |= ARM_CP_ALIAS;
8508 }
8509 } else if ((secstate != r->secure) && !ns) {
8510 /*
8511 * The register is not banked so we only want to allow
8512 * migration of the non-secure instance.
8513 */
7a0e58fa 8514 r2->type |= ARM_CP_ALIAS;
3f3c82a5 8515 }
3f3c82a5 8516
696ba377
RH
8517 if (HOST_BIG_ENDIAN &&
8518 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
8519 r2->fieldoffset += sizeof(uint32_t);
8520 }
3f3c82a5 8521 }
f5a0a5a5 8522 }
cc946d96 8523
1859f8c3
RH
8524 /*
8525 * By convention, for wildcarded registers only the first
6e6efd61 8526 * entry is used for migration; the others are marked as
7a0e58fa 8527 * ALIAS so we don't try to transfer the register
6e6efd61 8528 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 8529 * never migratable and not even raw-accessible.
6e6efd61 8530 */
696ba377 8531 if (r2->type & ARM_CP_SPECIAL_MASK) {
7a0e58fa
PM
8532 r2->type |= ARM_CP_NO_RAW;
8533 }
8534 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
8535 ((r->opc1 == CP_ANY) && opc1 != 0) ||
8536 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 8537 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
8538 }
8539
1859f8c3
RH
8540 /*
8541 * Check that raw accesses are either forbidden or handled. Note that
375421cc
PM
8542 * we can't assert this earlier because the setup of fieldoffset for
8543 * banked registers has to be done first.
8544 */
8545 if (!(r2->type & ARM_CP_NO_RAW)) {
8546 assert(!raw_accessors_invalid(r2));
8547 }
8548
5860362d 8549 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
6e6efd61
PM
8550}
8551
8552
4b6a83fb
PM
8553void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8554 const ARMCPRegInfo *r, void *opaque)
8555{
8556 /* Define implementations of coprocessor registers.
8557 * We store these in a hashtable because typically
8558 * there are less than 150 registers in a space which
8559 * is 16*16*16*8*8 = 262144 in size.
8560 * Wildcarding is supported for the crm, opc1 and opc2 fields.
8561 * If a register is defined twice then the second definition is
8562 * used, so this can be used to define some generic registers and
8563 * then override them with implementation specific variations.
8564 * At least one of the original and the second definition should
8565 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8566 * against accidental use.
f5a0a5a5
PM
8567 *
8568 * The state field defines whether the register is to be
8569 * visible in the AArch32 or AArch64 execution state. If the
8570 * state is set to ARM_CP_STATE_BOTH then we synthesise a
8571 * reginfo structure for the AArch32 view, which sees the lower
8572 * 32 bits of the 64 bit register.
8573 *
8574 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8575 * be wildcarded. AArch64 registers are always considered to be 64
8576 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8577 * the register, if any.
4b6a83fb 8578 */
d95101d6 8579 int crm, opc1, opc2;
4b6a83fb
PM
8580 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8581 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8582 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8583 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8584 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8585 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
d95101d6
RH
8586 CPState state;
8587
4b6a83fb
PM
8588 /* 64 bit registers have only CRm and Opc1 fields */
8589 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
8590 /* op0 only exists in the AArch64 encodings */
8591 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8592 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8593 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
cd8be50e
PM
8594 /*
8595 * This API is only for Arm's system coprocessors (14 and 15) or
8596 * (M-profile or v7A-and-earlier only) for implementation defined
8597 * coprocessors in the range 0..7. Our decode assumes this, since
8598 * 8..13 can be used for other insns including VFP and Neon. See
8599 * valid_cp() in translate.c. Assert here that we haven't tried
8600 * to use an invalid coprocessor number.
8601 */
8602 switch (r->state) {
8603 case ARM_CP_STATE_BOTH:
8604 /* 0 has a special meaning, but otherwise the same rules as AA32. */
8605 if (r->cp == 0) {
8606 break;
8607 }
8608 /* fall through */
8609 case ARM_CP_STATE_AA32:
8610 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8611 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8612 assert(r->cp >= 14 && r->cp <= 15);
8613 } else {
8614 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8615 }
8616 break;
8617 case ARM_CP_STATE_AA64:
8618 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8619 break;
8620 default:
8621 g_assert_not_reached();
8622 }
f5a0a5a5
PM
8623 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8624 * encodes a minimum access level for the register. We roll this
8625 * runtime check into our general permission check code, so check
8626 * here that the reginfo's specified permissions are strict enough
8627 * to encompass the generic architectural permission check.
8628 */
8629 if (r->state != ARM_CP_STATE_AA32) {
39107337 8630 CPAccessRights mask;
f5a0a5a5 8631 switch (r->opc1) {
b5bd7440
AB
8632 case 0:
8633 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8634 mask = PL0U_R | PL1_RW;
8635 break;
8636 case 1: case 2:
f5a0a5a5
PM
8637 /* min_EL EL1 */
8638 mask = PL1_RW;
8639 break;
8640 case 3:
8641 /* min_EL EL0 */
8642 mask = PL0_RW;
8643 break;
8644 case 4:
b4ecf60f 8645 case 5:
f5a0a5a5
PM
8646 /* min_EL EL2 */
8647 mask = PL2_RW;
8648 break;
f5a0a5a5
PM
8649 case 6:
8650 /* min_EL EL3 */
8651 mask = PL3_RW;
8652 break;
8653 case 7:
8654 /* min_EL EL1, secure mode only (we don't check the latter) */
8655 mask = PL1_RW;
8656 break;
8657 default:
8658 /* broken reginfo with out-of-range opc1 */
d385a605 8659 g_assert_not_reached();
f5a0a5a5
PM
8660 }
8661 /* assert our permissions are not too lax (stricter is fine) */
8662 assert((r->access & ~mask) == 0);
8663 }
8664
4b6a83fb
PM
8665 /* Check that the register definition has enough info to handle
8666 * reads and writes if they are permitted.
8667 */
87c3f0f2 8668 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
4b6a83fb 8669 if (r->access & PL3_R) {
3f3c82a5
FA
8670 assert((r->fieldoffset ||
8671 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8672 r->readfn);
4b6a83fb
PM
8673 }
8674 if (r->access & PL3_W) {
3f3c82a5
FA
8675 assert((r->fieldoffset ||
8676 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8677 r->writefn);
4b6a83fb
PM
8678 }
8679 }
5809ac57 8680
4b6a83fb
PM
8681 for (crm = crmmin; crm <= crmmax; crm++) {
8682 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8683 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
8684 for (state = ARM_CP_STATE_AA32;
8685 state <= ARM_CP_STATE_AA64; state++) {
8686 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8687 continue;
8688 }
3f3c82a5
FA
8689 if (state == ARM_CP_STATE_AA32) {
8690 /* Under AArch32 CP registers can be common
8691 * (same for secure and non-secure world) or banked.
8692 */
9c513e78
AB
8693 char *name;
8694
3f3c82a5
FA
8695 switch (r->secure) {
8696 case ARM_CP_SECSTATE_S:
8697 case ARM_CP_SECSTATE_NS:
8698 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
8699 r->secure, crm, opc1, opc2,
8700 r->name);
3f3c82a5 8701 break;
cbe64585 8702 case ARM_CP_SECSTATE_BOTH:
9c513e78 8703 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
8704 add_cpreg_to_hashtable(cpu, r, opaque, state,
8705 ARM_CP_SECSTATE_S,
9c513e78
AB
8706 crm, opc1, opc2, name);
8707 g_free(name);
3f3c82a5
FA
8708 add_cpreg_to_hashtable(cpu, r, opaque, state,
8709 ARM_CP_SECSTATE_NS,
9c513e78 8710 crm, opc1, opc2, r->name);
3f3c82a5 8711 break;
cbe64585
RH
8712 default:
8713 g_assert_not_reached();
3f3c82a5
FA
8714 }
8715 } else {
8716 /* AArch64 registers get mapped to non-secure instance
8717 * of AArch32 */
8718 add_cpreg_to_hashtable(cpu, r, opaque, state,
8719 ARM_CP_SECSTATE_NS,
9c513e78 8720 crm, opc1, opc2, r->name);
3f3c82a5 8721 }
f5a0a5a5 8722 }
4b6a83fb
PM
8723 }
8724 }
8725 }
8726}
8727
5809ac57
RH
8728/* Define a whole list of registers */
8729void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
8730 void *opaque, size_t len)
4b6a83fb 8731{
5809ac57
RH
8732 size_t i;
8733 for (i = 0; i < len; ++i) {
8734 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
4b6a83fb
PM
8735 }
8736}
8737
6c5c0fec
AB
8738/*
8739 * Modify ARMCPRegInfo for access from userspace.
8740 *
8741 * This is a data driven modification directed by
8742 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8743 * user-space cannot alter any values and dynamic values pertaining to
8744 * execution state are hidden from user space view anyway.
8745 */
5809ac57
RH
8746void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
8747 const ARMCPRegUserSpaceInfo *mods,
8748 size_t mods_len)
6c5c0fec 8749{
5809ac57
RH
8750 for (size_t mi = 0; mi < mods_len; ++mi) {
8751 const ARMCPRegUserSpaceInfo *m = mods + mi;
d040242e 8752 GPatternSpec *pat = NULL;
5809ac57 8753
d040242e
AB
8754 if (m->is_glob) {
8755 pat = g_pattern_spec_new(m->name);
8756 }
5809ac57
RH
8757 for (size_t ri = 0; ri < regs_len; ++ri) {
8758 ARMCPRegInfo *r = regs + ri;
8759
d040242e
AB
8760 if (pat && g_pattern_match_string(pat, r->name)) {
8761 r->type = ARM_CP_CONST;
8762 r->access = PL0U_R;
8763 r->resetvalue = 0;
8764 /* continue */
8765 } else if (strcmp(r->name, m->name) == 0) {
6c5c0fec
AB
8766 r->type = ARM_CP_CONST;
8767 r->access = PL0U_R;
8768 r->resetvalue &= m->exported_bits;
8769 r->resetvalue |= m->fixed_bits;
8770 break;
8771 }
8772 }
d040242e
AB
8773 if (pat) {
8774 g_pattern_spec_free(pat);
8775 }
6c5c0fec
AB
8776 }
8777}
8778
60322b39 8779const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 8780{
5860362d 8781 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
4b6a83fb
PM
8782}
8783
c4241c7d
PM
8784void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8785 uint64_t value)
4b6a83fb
PM
8786{
8787 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
8788}
8789
c4241c7d 8790uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
8791{
8792 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
8793 return 0;
8794}
8795
f5a0a5a5
PM
8796void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8797{
8798 /* Helper coprocessor reset function for do-nothing-on-reset registers */
8799}
8800
af393ffc 8801static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
8802{
8803 /* Return true if it is not valid for us to switch to
8804 * this CPU mode (ie all the UNPREDICTABLE cases in
8805 * the ARM ARM CPSRWriteByInstr pseudocode).
8806 */
af393ffc
PM
8807
8808 /* Changes to or from Hyp via MSR and CPS are illegal. */
8809 if (write_type == CPSRWriteByInstr &&
8810 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8811 mode == ARM_CPU_MODE_HYP)) {
8812 return 1;
8813 }
8814
37064a8b
PM
8815 switch (mode) {
8816 case ARM_CPU_MODE_USR:
10eacda7 8817 return 0;
37064a8b
PM
8818 case ARM_CPU_MODE_SYS:
8819 case ARM_CPU_MODE_SVC:
8820 case ARM_CPU_MODE_ABT:
8821 case ARM_CPU_MODE_UND:
8822 case ARM_CPU_MODE_IRQ:
8823 case ARM_CPU_MODE_FIQ:
52ff951b
PM
8824 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8825 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8826 */
10eacda7
PM
8827 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8828 * and CPS are treated as illegal mode changes.
8829 */
8830 if (write_type == CPSRWriteByInstr &&
10eacda7 8831 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7c208e0f 8832 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10eacda7
PM
8833 return 1;
8834 }
37064a8b 8835 return 0;
e6c8fc07 8836 case ARM_CPU_MODE_HYP:
e6ef0169 8837 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
027fc527 8838 case ARM_CPU_MODE_MON:
58ae2d1f 8839 return arm_current_el(env) < 3;
37064a8b
PM
8840 default:
8841 return 1;
8842 }
8843}
8844
2f4a40e5
AZ
8845uint32_t cpsr_read(CPUARMState *env)
8846{
8847 int ZF;
6fbe23d5
PB
8848 ZF = (env->ZF == 0);
8849 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
8850 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
8851 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
8852 | ((env->condexec_bits & 0xfc) << 8)
af519934 8853 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
8854}
8855
50866ba5
PM
8856void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
8857 CPSRWriteType write_type)
2f4a40e5 8858{
6e8801f9 8859 uint32_t changed_daif;
e784807c
PM
8860 bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
8861 (mask & (CPSR_M | CPSR_E | CPSR_IL));
6e8801f9 8862
2f4a40e5 8863 if (mask & CPSR_NZCV) {
6fbe23d5
PB
8864 env->ZF = (~val) & CPSR_Z;
8865 env->NF = val;
2f4a40e5
AZ
8866 env->CF = (val >> 29) & 1;
8867 env->VF = (val << 3) & 0x80000000;
8868 }
8869 if (mask & CPSR_Q)
8870 env->QF = ((val & CPSR_Q) != 0);
8871 if (mask & CPSR_T)
8872 env->thumb = ((val & CPSR_T) != 0);
8873 if (mask & CPSR_IT_0_1) {
8874 env->condexec_bits &= ~3;
8875 env->condexec_bits |= (val >> 25) & 3;
8876 }
8877 if (mask & CPSR_IT_2_7) {
8878 env->condexec_bits &= 3;
8879 env->condexec_bits |= (val >> 8) & 0xfc;
8880 }
8881 if (mask & CPSR_GE) {
8882 env->GE = (val >> 16) & 0xf;
8883 }
8884
6e8801f9
FA
8885 /* In a V7 implementation that includes the security extensions but does
8886 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
8887 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
8888 * bits respectively.
8889 *
8890 * In a V8 implementation, it is permitted for privileged software to
8891 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
8892 */
f8c88bbc 8893 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
8894 arm_feature(env, ARM_FEATURE_EL3) &&
8895 !arm_feature(env, ARM_FEATURE_EL2) &&
8896 !arm_is_secure(env)) {
8897
8898 changed_daif = (env->daif ^ val) & mask;
8899
8900 if (changed_daif & CPSR_A) {
8901 /* Check to see if we are allowed to change the masking of async
8902 * abort exceptions from a non-secure state.
8903 */
8904 if (!(env->cp15.scr_el3 & SCR_AW)) {
8905 qemu_log_mask(LOG_GUEST_ERROR,
8906 "Ignoring attempt to switch CPSR_A flag from "
8907 "non-secure world with SCR.AW bit clear\n");
8908 mask &= ~CPSR_A;
8909 }
8910 }
8911
8912 if (changed_daif & CPSR_F) {
8913 /* Check to see if we are allowed to change the masking of FIQ
8914 * exceptions from a non-secure state.
8915 */
8916 if (!(env->cp15.scr_el3 & SCR_FW)) {
8917 qemu_log_mask(LOG_GUEST_ERROR,
8918 "Ignoring attempt to switch CPSR_F flag from "
8919 "non-secure world with SCR.FW bit clear\n");
8920 mask &= ~CPSR_F;
8921 }
8922
8923 /* Check whether non-maskable FIQ (NMFI) support is enabled.
8924 * If this bit is set software is not allowed to mask
8925 * FIQs, but is allowed to set CPSR_F to 0.
8926 */
8927 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
8928 (val & CPSR_F)) {
8929 qemu_log_mask(LOG_GUEST_ERROR,
8930 "Ignoring attempt to enable CPSR_F flag "
8931 "(non-maskable FIQ [NMFI] support enabled)\n");
8932 mask &= ~CPSR_F;
8933 }
8934 }
8935 }
8936
4cc35614
PM
8937 env->daif &= ~(CPSR_AIF & mask);
8938 env->daif |= val & CPSR_AIF & mask;
8939
f8c88bbc
PM
8940 if (write_type != CPSRWriteRaw &&
8941 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
8942 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
8943 /* Note that we can only get here in USR mode if this is a
8944 * gdb stub write; for this case we follow the architectural
8945 * behaviour for guest writes in USR mode of ignoring an attempt
8946 * to switch mode. (Those are caught by translate.c for writes
8947 * triggered by guest instructions.)
8948 */
8949 mask &= ~CPSR_M;
8950 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
8951 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
8952 * v7, and has defined behaviour in v8:
8953 * + leave CPSR.M untouched
8954 * + allow changes to the other CPSR fields
8955 * + set PSTATE.IL
8956 * For user changes via the GDB stub, we don't set PSTATE.IL,
8957 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
8958 */
8959 mask &= ~CPSR_M;
81907a58
PM
8960 if (write_type != CPSRWriteByGDBStub &&
8961 arm_feature(env, ARM_FEATURE_V8)) {
8962 mask |= CPSR_IL;
8963 val |= CPSR_IL;
8964 }
81e37284
PM
8965 qemu_log_mask(LOG_GUEST_ERROR,
8966 "Illegal AArch32 mode switch attempt from %s to %s\n",
8967 aarch32_mode_name(env->uncached_cpsr),
8968 aarch32_mode_name(val));
37064a8b 8969 } else {
81e37284
PM
8970 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
8971 write_type == CPSRWriteExceptionReturn ?
8972 "Exception return from AArch32" :
8973 "AArch32 mode switch from",
8974 aarch32_mode_name(env->uncached_cpsr),
8975 aarch32_mode_name(val), env->regs[15]);
37064a8b
PM
8976 switch_mode(env, val & CPSR_M);
8977 }
2f4a40e5
AZ
8978 }
8979 mask &= ~CACHED_CPSR_BITS;
8980 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
e784807c
PM
8981 if (rebuild_hflags) {
8982 arm_rebuild_hflags(env);
8983 }
2f4a40e5
AZ
8984}
8985
b26eefb6
PB
8986/* Sign/zero extend */
8987uint32_t HELPER(sxtb16)(uint32_t x)
8988{
8989 uint32_t res;
8990 res = (uint16_t)(int8_t)x;
8991 res |= (uint32_t)(int8_t)(x >> 16) << 16;
8992 return res;
8993}
8994
e5346292
PM
8995static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
8996{
8997 /*
8998 * Take a division-by-zero exception if necessary; otherwise return
8999 * to get the usual non-trapping division behaviour (result of 0)
9000 */
9001 if (arm_feature(env, ARM_FEATURE_M)
9002 && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9003 raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9004 }
9005}
9006
b26eefb6
PB
9007uint32_t HELPER(uxtb16)(uint32_t x)
9008{
9009 uint32_t res;
9010 res = (uint16_t)(uint8_t)x;
9011 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9012 return res;
9013}
9014
e5346292 9015int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
3670669c 9016{
fc7a5038 9017 if (den == 0) {
e5346292 9018 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9019 return 0;
9020 }
9021 if (num == INT_MIN && den == -1) {
9022 return INT_MIN;
9023 }
3670669c
PB
9024 return num / den;
9025}
9026
e5346292 9027uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
3670669c 9028{
fc7a5038 9029 if (den == 0) {
e5346292 9030 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9031 return 0;
9032 }
3670669c
PB
9033 return num / den;
9034}
9035
9036uint32_t HELPER(rbit)(uint32_t x)
9037{
42fedbca 9038 return revbit32(x);
3670669c
PB
9039}
9040
c47eaf9f 9041#ifdef CONFIG_USER_ONLY
b5ff1b31 9042
affdb64d 9043static void switch_mode(CPUARMState *env, int mode)
b5ff1b31 9044{
2fc0cc0e 9045 ARMCPU *cpu = env_archcpu(env);
a47dddd7
AF
9046
9047 if (mode != ARM_CPU_MODE_USR) {
9048 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9049 }
b5ff1b31
FB
9050}
9051
012a906b
GB
9052uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9053 uint32_t cur_el, bool secure)
9e729b57
EI
9054{
9055 return 1;
9056}
9057
ce02049d
GB
9058void aarch64_sync_64_to_32(CPUARMState *env)
9059{
9060 g_assert_not_reached();
9061}
9062
b5ff1b31
FB
9063#else
9064
affdb64d 9065static void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
9066{
9067 int old_mode;
9068 int i;
9069
9070 old_mode = env->uncached_cpsr & CPSR_M;
9071 if (mode == old_mode)
9072 return;
9073
9074 if (old_mode == ARM_CPU_MODE_FIQ) {
9075 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 9076 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
9077 } else if (mode == ARM_CPU_MODE_FIQ) {
9078 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 9079 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
9080 }
9081
f5206413 9082 i = bank_number(old_mode);
b5ff1b31 9083 env->banked_r13[i] = env->regs[13];
b5ff1b31
FB
9084 env->banked_spsr[i] = env->spsr;
9085
f5206413 9086 i = bank_number(mode);
b5ff1b31 9087 env->regs[13] = env->banked_r13[i];
b5ff1b31 9088 env->spsr = env->banked_spsr[i];
593cfa2b
PM
9089
9090 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9091 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
b5ff1b31
FB
9092}
9093
0eeb17d6
GB
9094/* Physical Interrupt Target EL Lookup Table
9095 *
9096 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9097 *
9098 * The below multi-dimensional table is used for looking up the target
9099 * exception level given numerous condition criteria. Specifically, the
9100 * target EL is based on SCR and HCR routing controls as well as the
9101 * currently executing EL and secure state.
9102 *
9103 * Dimensions:
9104 * target_el_table[2][2][2][2][2][4]
9105 * | | | | | +--- Current EL
9106 * | | | | +------ Non-secure(0)/Secure(1)
9107 * | | | +--------- HCR mask override
9108 * | | +------------ SCR exec state control
9109 * | +--------------- SCR mask override
9110 * +------------------ 32-bit(0)/64-bit(1) EL3
9111 *
9112 * The table values are as such:
9113 * 0-3 = EL0-EL3
9114 * -1 = Cannot occur
9115 *
9116 * The ARM ARM target EL table includes entries indicating that an "exception
9117 * is not taken". The two cases where this is applicable are:
9118 * 1) An exception is taken from EL3 but the SCR does not have the exception
9119 * routed to EL3.
9120 * 2) An exception is taken from EL2 but the HCR does not have the exception
9121 * routed to EL2.
9122 * In these two cases, the below table contain a target of EL1. This value is
9123 * returned as it is expected that the consumer of the table data will check
9124 * for "target EL >= current EL" to ensure the exception is not taken.
9125 *
9126 * SCR HCR
9127 * 64 EA AMO From
9128 * BIT IRQ IMO Non-secure Secure
9129 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
9130 */
82c39f6a 9131static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
9132 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9133 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
9134 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9135 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
9136 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9137 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
9138 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9139 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
9140 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6c85f906
RDC
9141 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
9142 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
9143 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
0eeb17d6
GB
9144 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
9145 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6c85f906
RDC
9146 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
9147 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
0eeb17d6
GB
9148};
9149
9150/*
9151 * Determine the target EL for physical exceptions
9152 */
012a906b
GB
9153uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9154 uint32_t cur_el, bool secure)
0eeb17d6
GB
9155{
9156 CPUARMState *env = cs->env_ptr;
f7778444
RH
9157 bool rw;
9158 bool scr;
9159 bool hcr;
0eeb17d6 9160 int target_el;
2cde031f 9161 /* Is the highest EL AArch64? */
f7778444
RH
9162 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9163 uint64_t hcr_el2;
2cde031f
SS
9164
9165 if (arm_feature(env, ARM_FEATURE_EL3)) {
9166 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9167 } else {
9168 /* Either EL2 is the highest EL (and so the EL2 register width
9169 * is given by is64); or there is no EL2 or EL3, in which case
9170 * the value of 'rw' does not affect the table lookup anyway.
9171 */
9172 rw = is64;
9173 }
0eeb17d6 9174
f7778444 9175 hcr_el2 = arm_hcr_el2_eff(env);
0eeb17d6
GB
9176 switch (excp_idx) {
9177 case EXCP_IRQ:
9178 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
f7778444 9179 hcr = hcr_el2 & HCR_IMO;
0eeb17d6
GB
9180 break;
9181 case EXCP_FIQ:
9182 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
f7778444 9183 hcr = hcr_el2 & HCR_FMO;
0eeb17d6
GB
9184 break;
9185 default:
9186 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
f7778444 9187 hcr = hcr_el2 & HCR_AMO;
0eeb17d6
GB
9188 break;
9189 };
9190
d1b31428
RH
9191 /*
9192 * For these purposes, TGE and AMO/IMO/FMO both force the
9193 * interrupt to EL2. Fold TGE into the bit extracted above.
9194 */
9195 hcr |= (hcr_el2 & HCR_TGE) != 0;
9196
0eeb17d6
GB
9197 /* Perform a table-lookup for the target EL given the current state */
9198 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9199
9200 assert(target_el > 0);
9201
9202 return target_el;
9203}
9204
fc6177af 9205void arm_log_exception(CPUState *cs)
b59f479b 9206{
fc6177af
PM
9207 int idx = cs->exception_index;
9208
b59f479b
PMD
9209 if (qemu_loglevel_mask(CPU_LOG_INT)) {
9210 const char *exc = NULL;
9211 static const char * const excnames[] = {
9212 [EXCP_UDEF] = "Undefined Instruction",
9213 [EXCP_SWI] = "SVC",
9214 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9215 [EXCP_DATA_ABORT] = "Data Abort",
9216 [EXCP_IRQ] = "IRQ",
9217 [EXCP_FIQ] = "FIQ",
9218 [EXCP_BKPT] = "Breakpoint",
9219 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9220 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9221 [EXCP_HVC] = "Hypervisor Call",
9222 [EXCP_HYP_TRAP] = "Hypervisor Trap",
9223 [EXCP_SMC] = "Secure Monitor Call",
9224 [EXCP_VIRQ] = "Virtual IRQ",
9225 [EXCP_VFIQ] = "Virtual FIQ",
9226 [EXCP_SEMIHOST] = "Semihosting call",
9227 [EXCP_NOCP] = "v7M NOCP UsageFault",
9228 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9229 [EXCP_STKOF] = "v8M STKOF UsageFault",
9230 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9231 [EXCP_LSERR] = "v8M LSERR UsageFault",
9232 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
e5346292 9233 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
b59f479b
PMD
9234 };
9235
9236 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9237 exc = excnames[idx];
9238 }
9239 if (!exc) {
9240 exc = "unknown";
9241 }
fc6177af
PM
9242 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9243 idx, exc, cs->cpu_index);
b59f479b
PMD
9244 }
9245}
9246
a356dacf 9247/*
7aab5a8c
PMD
9248 * Function used to synchronize QEMU's AArch64 register set with AArch32
9249 * register set. This is necessary when switching between AArch32 and AArch64
9250 * execution state.
a356dacf 9251 */
7aab5a8c 9252void aarch64_sync_32_to_64(CPUARMState *env)
9ee6e8bb 9253{
7aab5a8c
PMD
9254 int i;
9255 uint32_t mode = env->uncached_cpsr & CPSR_M;
9256
9257 /* We can blanket copy R[0:7] to X[0:7] */
9258 for (i = 0; i < 8; i++) {
9259 env->xregs[i] = env->regs[i];
fd592d89 9260 }
70d74660 9261
9a223097 9262 /*
7aab5a8c
PMD
9263 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9264 * Otherwise, they come from the banked user regs.
fd592d89 9265 */
7aab5a8c
PMD
9266 if (mode == ARM_CPU_MODE_FIQ) {
9267 for (i = 8; i < 13; i++) {
9268 env->xregs[i] = env->usr_regs[i - 8];
9269 }
9270 } else {
9271 for (i = 8; i < 13; i++) {
9272 env->xregs[i] = env->regs[i];
9273 }
fd592d89 9274 }
9ee6e8bb 9275
7aab5a8c
PMD
9276 /*
9277 * Registers x13-x23 are the various mode SP and FP registers. Registers
9278 * r13 and r14 are only copied if we are in that mode, otherwise we copy
9279 * from the mode banked register.
9280 */
9281 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9282 env->xregs[13] = env->regs[13];
9283 env->xregs[14] = env->regs[14];
9284 } else {
9285 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9286 /* HYP is an exception in that it is copied from r14 */
9287 if (mode == ARM_CPU_MODE_HYP) {
9288 env->xregs[14] = env->regs[14];
95695eff 9289 } else {
7aab5a8c 9290 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
95695eff 9291 }
95695eff
PM
9292 }
9293
7aab5a8c
PMD
9294 if (mode == ARM_CPU_MODE_HYP) {
9295 env->xregs[15] = env->regs[13];
9296 } else {
9297 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
95695eff
PM
9298 }
9299
7aab5a8c
PMD
9300 if (mode == ARM_CPU_MODE_IRQ) {
9301 env->xregs[16] = env->regs[14];
9302 env->xregs[17] = env->regs[13];
9303 } else {
9304 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9305 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9306 }
95695eff 9307
7aab5a8c
PMD
9308 if (mode == ARM_CPU_MODE_SVC) {
9309 env->xregs[18] = env->regs[14];
9310 env->xregs[19] = env->regs[13];
9311 } else {
9312 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9313 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9314 }
95695eff 9315
7aab5a8c
PMD
9316 if (mode == ARM_CPU_MODE_ABT) {
9317 env->xregs[20] = env->regs[14];
9318 env->xregs[21] = env->regs[13];
9319 } else {
9320 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9321 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9322 }
e33cf0f8 9323
7aab5a8c
PMD
9324 if (mode == ARM_CPU_MODE_UND) {
9325 env->xregs[22] = env->regs[14];
9326 env->xregs[23] = env->regs[13];
9327 } else {
9328 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9329 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
e33cf0f8
PM
9330 }
9331
9332 /*
7aab5a8c
PMD
9333 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9334 * mode, then we can copy from r8-r14. Otherwise, we copy from the
9335 * FIQ bank for r8-r14.
e33cf0f8 9336 */
7aab5a8c
PMD
9337 if (mode == ARM_CPU_MODE_FIQ) {
9338 for (i = 24; i < 31; i++) {
9339 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
9340 }
9341 } else {
9342 for (i = 24; i < 29; i++) {
9343 env->xregs[i] = env->fiq_regs[i - 24];
e33cf0f8 9344 }
7aab5a8c
PMD
9345 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9346 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
e33cf0f8 9347 }
7aab5a8c
PMD
9348
9349 env->pc = env->regs[15];
e33cf0f8
PM
9350}
9351
9a223097 9352/*
7aab5a8c
PMD
9353 * Function used to synchronize QEMU's AArch32 register set with AArch64
9354 * register set. This is necessary when switching between AArch32 and AArch64
9355 * execution state.
de2db7ec 9356 */
7aab5a8c 9357void aarch64_sync_64_to_32(CPUARMState *env)
9ee6e8bb 9358{
7aab5a8c
PMD
9359 int i;
9360 uint32_t mode = env->uncached_cpsr & CPSR_M;
abc24d86 9361
7aab5a8c
PMD
9362 /* We can blanket copy X[0:7] to R[0:7] */
9363 for (i = 0; i < 8; i++) {
9364 env->regs[i] = env->xregs[i];
de2db7ec 9365 }
3f0cddee 9366
9a223097 9367 /*
7aab5a8c
PMD
9368 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9369 * Otherwise, we copy x8-x12 into the banked user regs.
de2db7ec 9370 */
7aab5a8c
PMD
9371 if (mode == ARM_CPU_MODE_FIQ) {
9372 for (i = 8; i < 13; i++) {
9373 env->usr_regs[i - 8] = env->xregs[i];
9374 }
9375 } else {
9376 for (i = 8; i < 13; i++) {
9377 env->regs[i] = env->xregs[i];
9378 }
fb602cb7
PM
9379 }
9380
9a223097 9381 /*
7aab5a8c
PMD
9382 * Registers r13 & r14 depend on the current mode.
9383 * If we are in a given mode, we copy the corresponding x registers to r13
9384 * and r14. Otherwise, we copy the x register to the banked r13 and r14
9385 * for the mode.
fb602cb7 9386 */
7aab5a8c
PMD
9387 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9388 env->regs[13] = env->xregs[13];
9389 env->regs[14] = env->xregs[14];
fb602cb7 9390 } else {
7aab5a8c 9391 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
fb602cb7 9392
7aab5a8c
PMD
9393 /*
9394 * HYP is an exception in that it does not have its own banked r14 but
9395 * shares the USR r14
9396 */
9397 if (mode == ARM_CPU_MODE_HYP) {
9398 env->regs[14] = env->xregs[14];
9399 } else {
9400 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9401 }
9402 }
fb602cb7 9403
7aab5a8c
PMD
9404 if (mode == ARM_CPU_MODE_HYP) {
9405 env->regs[13] = env->xregs[15];
fb602cb7 9406 } else {
7aab5a8c 9407 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
fb602cb7 9408 }
d02a8698 9409
7aab5a8c
PMD
9410 if (mode == ARM_CPU_MODE_IRQ) {
9411 env->regs[14] = env->xregs[16];
9412 env->regs[13] = env->xregs[17];
d02a8698 9413 } else {
7aab5a8c
PMD
9414 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9415 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
d02a8698
PM
9416 }
9417
7aab5a8c
PMD
9418 if (mode == ARM_CPU_MODE_SVC) {
9419 env->regs[14] = env->xregs[18];
9420 env->regs[13] = env->xregs[19];
9421 } else {
9422 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9423 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
fb602cb7
PM
9424 }
9425
7aab5a8c
PMD
9426 if (mode == ARM_CPU_MODE_ABT) {
9427 env->regs[14] = env->xregs[20];
9428 env->regs[13] = env->xregs[21];
9429 } else {
9430 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9431 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
9432 }
9433
9434 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
9435 env->regs[14] = env->xregs[22];
9436 env->regs[13] = env->xregs[23];
ce02049d 9437 } else {
593cfa2b 9438 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
3a9148d0 9439 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
9440 }
9441
9442 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9443 * mode, then we can copy to r8-r14. Otherwise, we copy to the
9444 * FIQ bank for r8-r14.
9445 */
9446 if (mode == ARM_CPU_MODE_FIQ) {
9447 for (i = 24; i < 31; i++) {
9448 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
9449 }
9450 } else {
9451 for (i = 24; i < 29; i++) {
9452 env->fiq_regs[i - 24] = env->xregs[i];
9453 }
9454 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
593cfa2b 9455 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
ce02049d
GB
9456 }
9457
9458 env->regs[15] = env->pc;
9459}
9460
dea8378b
PM
9461static void take_aarch32_exception(CPUARMState *env, int new_mode,
9462 uint32_t mask, uint32_t offset,
9463 uint32_t newpc)
9464{
4a2696c0
RH
9465 int new_el;
9466
dea8378b
PM
9467 /* Change the CPU state so as to actually take the exception. */
9468 switch_mode(env, new_mode);
4a2696c0 9469
dea8378b
PM
9470 /*
9471 * For exceptions taken to AArch32 we must clear the SS bit in both
9472 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9473 */
f944a854 9474 env->pstate &= ~PSTATE_SS;
dea8378b
PM
9475 env->spsr = cpsr_read(env);
9476 /* Clear IT bits. */
9477 env->condexec_bits = 0;
9478 /* Switch to the new mode, and to the correct instruction set. */
9479 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
88828bf1
CD
9480
9481 /* This must be after mode switching. */
9482 new_el = arm_current_el(env);
9483
dea8378b
PM
9484 /* Set new mode endianness */
9485 env->uncached_cpsr &= ~CPSR_E;
4a2696c0 9486 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
dea8378b
PM
9487 env->uncached_cpsr |= CPSR_E;
9488 }
829f9fd3
PM
9489 /* J and IL must always be cleared for exception entry */
9490 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
dea8378b
PM
9491 env->daif |= mask;
9492
f2f68a78
RC
9493 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9494 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9495 env->uncached_cpsr |= CPSR_SSBS;
9496 } else {
9497 env->uncached_cpsr &= ~CPSR_SSBS;
9498 }
9499 }
9500
dea8378b
PM
9501 if (new_mode == ARM_CPU_MODE_HYP) {
9502 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9503 env->elr_el[2] = env->regs[15];
9504 } else {
4a2696c0 9505 /* CPSR.PAN is normally preserved preserved unless... */
f8af1143 9506 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
4a2696c0
RH
9507 switch (new_el) {
9508 case 3:
9509 if (!arm_is_secure_below_el3(env)) {
9510 /* ... the target is EL3, from non-secure state. */
9511 env->uncached_cpsr &= ~CPSR_PAN;
9512 break;
9513 }
9514 /* ... the target is EL3, from secure state ... */
9515 /* fall through */
9516 case 1:
9517 /* ... the target is EL1 and SCTLR.SPAN is 0. */
9518 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9519 env->uncached_cpsr |= CPSR_PAN;
9520 }
9521 break;
9522 }
9523 }
dea8378b
PM
9524 /*
9525 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9526 * and we should just guard the thumb mode on V4
9527 */
9528 if (arm_feature(env, ARM_FEATURE_V4T)) {
9529 env->thumb =
9530 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9531 }
9532 env->regs[14] = env->regs[15] + offset;
9533 }
9534 env->regs[15] = newpc;
a8a79c7a 9535 arm_rebuild_hflags(env);
dea8378b
PM
9536}
9537
b9bc21ff
PM
9538static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9539{
9540 /*
9541 * Handle exception entry to Hyp mode; this is sufficiently
9542 * different to entry to other AArch32 modes that we handle it
9543 * separately here.
9544 *
9545 * The vector table entry used is always the 0x14 Hyp mode entry point,
2c023d36 9546 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
b9bc21ff
PM
9547 * The offset applied to the preferred return address is always zero
9548 * (see DDI0487C.a section G1.12.3).
9549 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9550 */
9551 uint32_t addr, mask;
9552 ARMCPU *cpu = ARM_CPU(cs);
9553 CPUARMState *env = &cpu->env;
9554
9555 switch (cs->exception_index) {
9556 case EXCP_UDEF:
9557 addr = 0x04;
9558 break;
9559 case EXCP_SWI:
2c023d36 9560 addr = 0x08;
b9bc21ff
PM
9561 break;
9562 case EXCP_BKPT:
9563 /* Fall through to prefetch abort. */
9564 case EXCP_PREFETCH_ABORT:
9565 env->cp15.ifar_s = env->exception.vaddress;
9566 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9567 (uint32_t)env->exception.vaddress);
9568 addr = 0x0c;
9569 break;
9570 case EXCP_DATA_ABORT:
9571 env->cp15.dfar_s = env->exception.vaddress;
9572 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9573 (uint32_t)env->exception.vaddress);
9574 addr = 0x10;
9575 break;
9576 case EXCP_IRQ:
9577 addr = 0x18;
9578 break;
9579 case EXCP_FIQ:
9580 addr = 0x1c;
9581 break;
9582 case EXCP_HVC:
9583 addr = 0x08;
9584 break;
9585 case EXCP_HYP_TRAP:
9586 addr = 0x14;
9bbb4ef9 9587 break;
b9bc21ff
PM
9588 default:
9589 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9590 }
9591
9592 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
2ed08180
PM
9593 if (!arm_feature(env, ARM_FEATURE_V8)) {
9594 /*
9595 * QEMU syndrome values are v8-style. v7 has the IL bit
9596 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9597 * If this is a v7 CPU, squash the IL bit in those cases.
9598 */
9599 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9600 (cs->exception_index == EXCP_DATA_ABORT &&
9601 !(env->exception.syndrome & ARM_EL_ISV)) ||
9602 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9603 env->exception.syndrome &= ~ARM_EL_IL;
9604 }
9605 }
b9bc21ff
PM
9606 env->cp15.esr_el[2] = env->exception.syndrome;
9607 }
9608
9609 if (arm_current_el(env) != 2 && addr < 0x14) {
9610 addr = 0x14;
9611 }
9612
9613 mask = 0;
9614 if (!(env->cp15.scr_el3 & SCR_EA)) {
9615 mask |= CPSR_A;
9616 }
9617 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9618 mask |= CPSR_I;
9619 }
9620 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9621 mask |= CPSR_F;
9622 }
9623
9624 addr += env->cp15.hvbar;
9625
9626 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9627}
9628
966f758c 9629static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 9630{
97a8ea5a
AF
9631 ARMCPU *cpu = ARM_CPU(cs);
9632 CPUARMState *env = &cpu->env;
b5ff1b31
FB
9633 uint32_t addr;
9634 uint32_t mask;
9635 int new_mode;
9636 uint32_t offset;
16a906fd 9637 uint32_t moe;
b5ff1b31 9638
16a906fd 9639 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
64b91e3f 9640 switch (syn_get_ec(env->exception.syndrome)) {
16a906fd
PM
9641 case EC_BREAKPOINT:
9642 case EC_BREAKPOINT_SAME_EL:
9643 moe = 1;
9644 break;
9645 case EC_WATCHPOINT:
9646 case EC_WATCHPOINT_SAME_EL:
9647 moe = 10;
9648 break;
9649 case EC_AA32_BKPT:
9650 moe = 3;
9651 break;
9652 case EC_VECTORCATCH:
9653 moe = 5;
9654 break;
9655 default:
9656 moe = 0;
9657 break;
9658 }
9659
9660 if (moe) {
9661 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9662 }
9663
b9bc21ff
PM
9664 if (env->exception.target_el == 2) {
9665 arm_cpu_do_interrupt_aarch32_hyp(cs);
9666 return;
9667 }
9668
27103424 9669 switch (cs->exception_index) {
b5ff1b31
FB
9670 case EXCP_UDEF:
9671 new_mode = ARM_CPU_MODE_UND;
9672 addr = 0x04;
9673 mask = CPSR_I;
9674 if (env->thumb)
9675 offset = 2;
9676 else
9677 offset = 4;
9678 break;
9679 case EXCP_SWI:
9680 new_mode = ARM_CPU_MODE_SVC;
9681 addr = 0x08;
9682 mask = CPSR_I;
601d70b9 9683 /* The PC already points to the next instruction. */
b5ff1b31
FB
9684 offset = 0;
9685 break;
06c949e6 9686 case EXCP_BKPT:
9ee6e8bb
PB
9687 /* Fall through to prefetch abort. */
9688 case EXCP_PREFETCH_ABORT:
88ca1c2d 9689 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 9690 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 9691 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 9692 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9693 new_mode = ARM_CPU_MODE_ABT;
9694 addr = 0x0c;
9695 mask = CPSR_A | CPSR_I;
9696 offset = 4;
9697 break;
9698 case EXCP_DATA_ABORT:
4a7e2d73 9699 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 9700 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 9701 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 9702 env->exception.fsr,
6cd8a264 9703 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9704 new_mode = ARM_CPU_MODE_ABT;
9705 addr = 0x10;
9706 mask = CPSR_A | CPSR_I;
9707 offset = 8;
9708 break;
9709 case EXCP_IRQ:
9710 new_mode = ARM_CPU_MODE_IRQ;
9711 addr = 0x18;
9712 /* Disable IRQ and imprecise data aborts. */
9713 mask = CPSR_A | CPSR_I;
9714 offset = 4;
de38d23b
FA
9715 if (env->cp15.scr_el3 & SCR_IRQ) {
9716 /* IRQ routed to monitor mode */
9717 new_mode = ARM_CPU_MODE_MON;
9718 mask |= CPSR_F;
9719 }
b5ff1b31
FB
9720 break;
9721 case EXCP_FIQ:
9722 new_mode = ARM_CPU_MODE_FIQ;
9723 addr = 0x1c;
9724 /* Disable FIQ, IRQ and imprecise data aborts. */
9725 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
9726 if (env->cp15.scr_el3 & SCR_FIQ) {
9727 /* FIQ routed to monitor mode */
9728 new_mode = ARM_CPU_MODE_MON;
9729 }
b5ff1b31
FB
9730 offset = 4;
9731 break;
87a4b270
PM
9732 case EXCP_VIRQ:
9733 new_mode = ARM_CPU_MODE_IRQ;
9734 addr = 0x18;
9735 /* Disable IRQ and imprecise data aborts. */
9736 mask = CPSR_A | CPSR_I;
9737 offset = 4;
9738 break;
9739 case EXCP_VFIQ:
9740 new_mode = ARM_CPU_MODE_FIQ;
9741 addr = 0x1c;
9742 /* Disable FIQ, IRQ and imprecise data aborts. */
9743 mask = CPSR_A | CPSR_I | CPSR_F;
9744 offset = 4;
9745 break;
dbe9d163
FA
9746 case EXCP_SMC:
9747 new_mode = ARM_CPU_MODE_MON;
9748 addr = 0x08;
9749 mask = CPSR_A | CPSR_I | CPSR_F;
9750 offset = 0;
9751 break;
b5ff1b31 9752 default:
a47dddd7 9753 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
9754 return; /* Never happens. Keep compiler happy. */
9755 }
e89e51a1
FA
9756
9757 if (new_mode == ARM_CPU_MODE_MON) {
9758 addr += env->cp15.mvbar;
137feaa9 9759 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 9760 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 9761 addr += 0xffff0000;
8641136c
NR
9762 } else {
9763 /* ARM v7 architectures provide a vector base address register to remap
9764 * the interrupt vector table.
e89e51a1 9765 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
9766 * Note: only bits 31:5 are valid.
9767 */
fb6c91ba 9768 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 9769 }
dbe9d163
FA
9770
9771 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9772 env->cp15.scr_el3 &= ~SCR_NS;
9773 }
9774
dea8378b 9775 take_aarch32_exception(env, new_mode, mask, offset, addr);
b5ff1b31
FB
9776}
9777
a65dabf7
PM
9778static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
9779{
9780 /*
9781 * Return the register number of the AArch64 view of the AArch32
9782 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
9783 * be that of the AArch32 mode the exception came from.
9784 */
9785 int mode = env->uncached_cpsr & CPSR_M;
9786
9787 switch (aarch32_reg) {
9788 case 0 ... 7:
9789 return aarch32_reg;
9790 case 8 ... 12:
9791 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
9792 case 13:
9793 switch (mode) {
9794 case ARM_CPU_MODE_USR:
9795 case ARM_CPU_MODE_SYS:
9796 return 13;
9797 case ARM_CPU_MODE_HYP:
9798 return 15;
9799 case ARM_CPU_MODE_IRQ:
9800 return 17;
9801 case ARM_CPU_MODE_SVC:
9802 return 19;
9803 case ARM_CPU_MODE_ABT:
9804 return 21;
9805 case ARM_CPU_MODE_UND:
9806 return 23;
9807 case ARM_CPU_MODE_FIQ:
9808 return 29;
9809 default:
9810 g_assert_not_reached();
9811 }
9812 case 14:
9813 switch (mode) {
9814 case ARM_CPU_MODE_USR:
9815 case ARM_CPU_MODE_SYS:
9816 case ARM_CPU_MODE_HYP:
9817 return 14;
9818 case ARM_CPU_MODE_IRQ:
9819 return 16;
9820 case ARM_CPU_MODE_SVC:
9821 return 18;
9822 case ARM_CPU_MODE_ABT:
9823 return 20;
9824 case ARM_CPU_MODE_UND:
9825 return 22;
9826 case ARM_CPU_MODE_FIQ:
9827 return 30;
9828 default:
9829 g_assert_not_reached();
9830 }
9831 case 15:
9832 return 31;
9833 default:
9834 g_assert_not_reached();
9835 }
9836}
9837
f944a854
RC
9838static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
9839{
9840 uint32_t ret = cpsr_read(env);
9841
9842 /* Move DIT to the correct location for SPSR_ELx */
9843 if (ret & CPSR_DIT) {
9844 ret &= ~CPSR_DIT;
9845 ret |= PSTATE_DIT;
9846 }
9847 /* Merge PSTATE.SS into SPSR_ELx */
9848 ret |= env->pstate & PSTATE_SS;
9849
9850 return ret;
9851}
9852
966f758c
PM
9853/* Handle exception entry to a target EL which is using AArch64 */
9854static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
9855{
9856 ARMCPU *cpu = ARM_CPU(cs);
9857 CPUARMState *env = &cpu->env;
9858 unsigned int new_el = env->exception.target_el;
9859 target_ulong addr = env->cp15.vbar_el[new_el];
9860 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
4a2696c0 9861 unsigned int old_mode;
0ab5953b 9862 unsigned int cur_el = arm_current_el(env);
a65dabf7 9863 int rt;
0ab5953b 9864
9a05f7b6
RH
9865 /*
9866 * Note that new_el can never be 0. If cur_el is 0, then
9867 * el0_a64 is is_a64(), else el0_a64 is ignored.
9868 */
9869 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
f3a9b694 9870
0ab5953b 9871 if (cur_el < new_el) {
3d6f7617
PM
9872 /* Entry vector offset depends on whether the implemented EL
9873 * immediately lower than the target level is using AArch32 or AArch64
9874 */
9875 bool is_aa64;
cb092fbb 9876 uint64_t hcr;
3d6f7617
PM
9877
9878 switch (new_el) {
9879 case 3:
9880 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
9881 break;
9882 case 2:
cb092fbb
RH
9883 hcr = arm_hcr_el2_eff(env);
9884 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
9885 is_aa64 = (hcr & HCR_RW) != 0;
9886 break;
9887 }
9888 /* fall through */
3d6f7617
PM
9889 case 1:
9890 is_aa64 = is_a64(env);
9891 break;
9892 default:
9893 g_assert_not_reached();
9894 }
9895
9896 if (is_aa64) {
f3a9b694
PM
9897 addr += 0x400;
9898 } else {
9899 addr += 0x600;
9900 }
9901 } else if (pstate_read(env) & PSTATE_SP) {
9902 addr += 0x200;
9903 }
9904
f3a9b694
PM
9905 switch (cs->exception_index) {
9906 case EXCP_PREFETCH_ABORT:
9907 case EXCP_DATA_ABORT:
9908 env->cp15.far_el[new_el] = env->exception.vaddress;
9909 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
9910 env->cp15.far_el[new_el]);
9911 /* fall through */
9912 case EXCP_BKPT:
9913 case EXCP_UDEF:
9914 case EXCP_SWI:
9915 case EXCP_HVC:
9916 case EXCP_HYP_TRAP:
9917 case EXCP_SMC:
a65dabf7
PM
9918 switch (syn_get_ec(env->exception.syndrome)) {
9919 case EC_ADVSIMDFPACCESSTRAP:
4be42f40
PM
9920 /*
9921 * QEMU internal FP/SIMD syndromes from AArch32 include the
9922 * TA and coproc fields which are only exposed if the exception
9923 * is taken to AArch32 Hyp mode. Mask them out to get a valid
9924 * AArch64 format syndrome.
9925 */
9926 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
a65dabf7
PM
9927 break;
9928 case EC_CP14RTTRAP:
9929 case EC_CP15RTTRAP:
9930 case EC_CP14DTTRAP:
9931 /*
9932 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
9933 * the raw register field from the insn; when taking this to
9934 * AArch64 we must convert it to the AArch64 view of the register
9935 * number. Notice that we read a 4-bit AArch32 register number and
9936 * write back a 5-bit AArch64 one.
9937 */
9938 rt = extract32(env->exception.syndrome, 5, 4);
9939 rt = aarch64_regnum(env, rt);
9940 env->exception.syndrome = deposit32(env->exception.syndrome,
9941 5, 5, rt);
9942 break;
9943 case EC_CP15RRTTRAP:
9944 case EC_CP14RRTTRAP:
9945 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
9946 rt = extract32(env->exception.syndrome, 5, 4);
9947 rt = aarch64_regnum(env, rt);
9948 env->exception.syndrome = deposit32(env->exception.syndrome,
9949 5, 5, rt);
9950 rt = extract32(env->exception.syndrome, 10, 4);
9951 rt = aarch64_regnum(env, rt);
9952 env->exception.syndrome = deposit32(env->exception.syndrome,
9953 10, 5, rt);
9954 break;
4be42f40 9955 }
f3a9b694
PM
9956 env->cp15.esr_el[new_el] = env->exception.syndrome;
9957 break;
9958 case EXCP_IRQ:
9959 case EXCP_VIRQ:
9960 addr += 0x80;
9961 break;
9962 case EXCP_FIQ:
9963 case EXCP_VFIQ:
9964 addr += 0x100;
9965 break;
f3a9b694
PM
9966 default:
9967 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9968 }
9969
9970 if (is_a64(env)) {
4a2696c0 9971 old_mode = pstate_read(env);
f3a9b694
PM
9972 aarch64_save_sp(env, arm_current_el(env));
9973 env->elr_el[new_el] = env->pc;
9974 } else {
f944a854 9975 old_mode = cpsr_read_for_spsr_elx(env);
f3a9b694
PM
9976 env->elr_el[new_el] = env->regs[15];
9977
9978 aarch64_sync_32_to_64(env);
9979
9980 env->condexec_bits = 0;
9981 }
4a2696c0
RH
9982 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
9983
f3a9b694
PM
9984 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
9985 env->elr_el[new_el]);
9986
4a2696c0
RH
9987 if (cpu_isar_feature(aa64_pan, cpu)) {
9988 /* The value of PSTATE.PAN is normally preserved, except when ... */
9989 new_mode |= old_mode & PSTATE_PAN;
9990 switch (new_el) {
9991 case 2:
9992 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
9993 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
9994 != (HCR_E2H | HCR_TGE)) {
9995 break;
9996 }
9997 /* fall through */
9998 case 1:
9999 /* ... the target is EL1 ... */
10000 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
10001 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10002 new_mode |= PSTATE_PAN;
10003 }
10004 break;
10005 }
10006 }
34669338
RH
10007 if (cpu_isar_feature(aa64_mte, cpu)) {
10008 new_mode |= PSTATE_TCO;
10009 }
4a2696c0 10010
f2f68a78
RC
10011 if (cpu_isar_feature(aa64_ssbs, cpu)) {
10012 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10013 new_mode |= PSTATE_SSBS;
10014 } else {
10015 new_mode &= ~PSTATE_SSBS;
10016 }
10017 }
10018
f3a9b694 10019 pstate_write(env, PSTATE_DAIF | new_mode);
53221552 10020 env->aarch64 = true;
f3a9b694 10021 aarch64_restore_sp(env, new_el);
a8a79c7a 10022 helper_rebuild_hflags_a64(env, new_el);
f3a9b694
PM
10023
10024 env->pc = addr;
10025
10026 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10027 new_el, env->pc, pstate_read(env));
966f758c
PM
10028}
10029
ed6e6ba9
AB
10030/*
10031 * Do semihosting call and set the appropriate return value. All the
10032 * permission and validity checks have been done at translate time.
10033 *
10034 * We only see semihosting exceptions in TCG only as they are not
10035 * trapped to the hypervisor in KVM.
10036 */
91f78c58 10037#ifdef CONFIG_TCG
ed6e6ba9
AB
10038static void handle_semihosting(CPUState *cs)
10039{
904c04de
PM
10040 ARMCPU *cpu = ARM_CPU(cs);
10041 CPUARMState *env = &cpu->env;
10042
10043 if (is_a64(env)) {
ed6e6ba9
AB
10044 qemu_log_mask(CPU_LOG_INT,
10045 "...handling as semihosting call 0x%" PRIx64 "\n",
10046 env->xregs[0]);
0bb446d8 10047 env->xregs[0] = do_common_semihosting(cs);
4ff5ef9e 10048 env->pc += 4;
904c04de 10049 } else {
904c04de
PM
10050 qemu_log_mask(CPU_LOG_INT,
10051 "...handling as semihosting call 0x%x\n",
10052 env->regs[0]);
0bb446d8 10053 env->regs[0] = do_common_semihosting(cs);
4ff5ef9e 10054 env->regs[15] += env->thumb ? 2 : 4;
904c04de
PM
10055 }
10056}
ed6e6ba9 10057#endif
904c04de 10058
966f758c
PM
10059/* Handle a CPU exception for A and R profile CPUs.
10060 * Do any appropriate logging, handle PSCI calls, and then hand off
10061 * to the AArch64-entry or AArch32-entry function depending on the
10062 * target exception level's register width.
853bfef4
CF
10063 *
10064 * Note: this is used for both TCG (as the do_interrupt tcg op),
10065 * and KVM to re-inject guest debug exceptions, and to
10066 * inject a Synchronous-External-Abort.
966f758c
PM
10067 */
10068void arm_cpu_do_interrupt(CPUState *cs)
10069{
10070 ARMCPU *cpu = ARM_CPU(cs);
10071 CPUARMState *env = &cpu->env;
10072 unsigned int new_el = env->exception.target_el;
10073
531c60a9 10074 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c 10075
fc6177af 10076 arm_log_exception(cs);
966f758c
PM
10077 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10078 new_el);
10079 if (qemu_loglevel_mask(CPU_LOG_INT)
10080 && !excp_is_internal(cs->exception_index)) {
6568da45 10081 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
64b91e3f 10082 syn_get_ec(env->exception.syndrome),
966f758c
PM
10083 env->exception.syndrome);
10084 }
10085
10086 if (arm_is_psci_call(cpu, cs->exception_index)) {
10087 arm_handle_psci_call(cpu);
10088 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10089 return;
10090 }
10091
ed6e6ba9
AB
10092 /*
10093 * Semihosting semantics depend on the register width of the code
10094 * that caused the exception, not the target exception level, so
10095 * must be handled here.
966f758c 10096 */
ed6e6ba9
AB
10097#ifdef CONFIG_TCG
10098 if (cs->exception_index == EXCP_SEMIHOST) {
10099 handle_semihosting(cs);
904c04de
PM
10100 return;
10101 }
ed6e6ba9 10102#endif
904c04de 10103
b5c53d1b
AL
10104 /* Hooks may change global state so BQL should be held, also the
10105 * BQL needs to be held for any modification of
10106 * cs->interrupt_request.
10107 */
10108 g_assert(qemu_mutex_iothread_locked());
10109
10110 arm_call_pre_el_change_hook(cpu);
10111
904c04de
PM
10112 assert(!excp_is_internal(cs->exception_index));
10113 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
10114 arm_cpu_do_interrupt_aarch64(cs);
10115 } else {
10116 arm_cpu_do_interrupt_aarch32(cs);
10117 }
f3a9b694 10118
bd7d00fc
PM
10119 arm_call_el_change_hook(cpu);
10120
f3a9b694
PM
10121 if (!kvm_enabled()) {
10122 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10123 }
10124}
c47eaf9f 10125#endif /* !CONFIG_USER_ONLY */
0480f69a 10126
aaec1432
RH
10127uint64_t arm_sctlr(CPUARMState *env, int el)
10128{
10129 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10130 if (el == 0) {
10131 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
b6ad6062
RDC
10132 el = (mmu_idx == ARMMMUIdx_E20_0 || mmu_idx == ARMMMUIdx_SE20_0)
10133 ? 2 : 1;
aaec1432
RH
10134 }
10135 return env->cp15.sctlr_el[el];
10136}
c47eaf9f 10137
0480f69a 10138/* Return the SCTLR value which controls this address translation regime */
aaec1432 10139static inline uint64_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
0480f69a
PM
10140{
10141 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
10142}
10143
aaec1432
RH
10144#ifndef CONFIG_USER_ONLY
10145
0480f69a
PM
10146/* Return true if the specified stage of address translation is disabled */
10147static inline bool regime_translation_disabled(CPUARMState *env,
10148 ARMMMUIdx mmu_idx)
10149{
e04a5752
RDC
10150 uint64_t hcr_el2;
10151
29c483a5 10152 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 10153 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
10154 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
10155 case R_V7M_MPU_CTRL_ENABLE_MASK:
10156 /* Enabled, but not for HardFault and NMI */
62593718 10157 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
3bef7012
PM
10158 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
10159 /* Enabled for all cases */
10160 return false;
10161 case 0:
10162 default:
10163 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
10164 * we warned about that in armv7m_nvic.c when the guest set it.
10165 */
10166 return true;
10167 }
29c483a5
MD
10168 }
10169
e04a5752
RDC
10170 hcr_el2 = arm_hcr_el2_eff(env);
10171
b1a10c86 10172 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
9d1bab33 10173 /* HCR.DC means HCR.VM behaves as 1 */
e04a5752 10174 return (hcr_el2 & (HCR_DC | HCR_VM)) == 0;
0480f69a 10175 }
3d0e3080 10176
e04a5752 10177 if (hcr_el2 & HCR_TGE) {
3d0e3080
PM
10178 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
10179 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
10180 return true;
10181 }
10182 }
10183
e04a5752 10184 if ((hcr_el2 & HCR_DC) && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
9d1bab33
PM
10185 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
10186 return true;
10187 }
10188
0480f69a
PM
10189 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
10190}
10191
73462ddd
PC
10192static inline bool regime_translation_big_endian(CPUARMState *env,
10193 ARMMMUIdx mmu_idx)
10194{
10195 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
10196}
10197
c47eaf9f
PM
10198/* Return the TTBR associated with this translation regime */
10199static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
10200 int ttbrn)
10201{
97fa9350 10202 if (mmu_idx == ARMMMUIdx_Stage2) {
c47eaf9f
PM
10203 return env->cp15.vttbr_el2;
10204 }
b1a10c86
RDC
10205 if (mmu_idx == ARMMMUIdx_Stage2_S) {
10206 return env->cp15.vsttbr_el2;
10207 }
c47eaf9f
PM
10208 if (ttbrn == 0) {
10209 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
10210 } else {
10211 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
10212 }
10213}
10214
10215#endif /* !CONFIG_USER_ONLY */
10216
8bd5c820
PM
10217/* Convert a possible stage1+2 MMU index into the appropriate
10218 * stage 1 MMU index
10219 */
10220static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
10221{
b9f6033c 10222 switch (mmu_idx) {
b1a10c86
RDC
10223 case ARMMMUIdx_SE10_0:
10224 return ARMMMUIdx_Stage1_SE0;
10225 case ARMMMUIdx_SE10_1:
10226 return ARMMMUIdx_Stage1_SE1;
10227 case ARMMMUIdx_SE10_1_PAN:
10228 return ARMMMUIdx_Stage1_SE1_PAN;
b9f6033c
RH
10229 case ARMMMUIdx_E10_0:
10230 return ARMMMUIdx_Stage1_E0;
10231 case ARMMMUIdx_E10_1:
10232 return ARMMMUIdx_Stage1_E1;
452ef8cb
RH
10233 case ARMMMUIdx_E10_1_PAN:
10234 return ARMMMUIdx_Stage1_E1_PAN;
b9f6033c
RH
10235 default:
10236 return mmu_idx;
8bd5c820 10237 }
8bd5c820
PM
10238}
10239
0480f69a
PM
10240/* Return true if the translation regime is using LPAE format page tables */
10241static inline bool regime_using_lpae_format(CPUARMState *env,
10242 ARMMMUIdx mmu_idx)
10243{
10244 int el = regime_el(env, mmu_idx);
10245 if (el == 2 || arm_el_is_aa64(env, el)) {
10246 return true;
10247 }
10248 if (arm_feature(env, ARM_FEATURE_LPAE)
10249 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
10250 return true;
10251 }
10252 return false;
10253}
10254
deb2db99
AR
10255/* Returns true if the stage 1 translation regime is using LPAE format page
10256 * tables. Used when raising alignment exceptions, whose FSR changes depending
10257 * on whether the long or short descriptor format is in use. */
10258bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 10259{
8bd5c820 10260 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 10261
30901475
AB
10262 return regime_using_lpae_format(env, mmu_idx);
10263}
10264
c47eaf9f 10265#ifndef CONFIG_USER_ONLY
0480f69a
PM
10266static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
10267{
10268 switch (mmu_idx) {
fba37aed 10269 case ARMMMUIdx_SE10_0:
b9f6033c 10270 case ARMMMUIdx_E20_0:
b6ad6062 10271 case ARMMMUIdx_SE20_0:
2859d7b5 10272 case ARMMMUIdx_Stage1_E0:
b1a10c86 10273 case ARMMMUIdx_Stage1_SE0:
e7b921c2 10274 case ARMMMUIdx_MUser:
871bec7c 10275 case ARMMMUIdx_MSUser:
62593718
PM
10276 case ARMMMUIdx_MUserNegPri:
10277 case ARMMMUIdx_MSUserNegPri:
0480f69a
PM
10278 return true;
10279 default:
10280 return false;
01b98b68
RH
10281 case ARMMMUIdx_E10_0:
10282 case ARMMMUIdx_E10_1:
452ef8cb 10283 case ARMMMUIdx_E10_1_PAN:
0480f69a
PM
10284 g_assert_not_reached();
10285 }
10286}
10287
0fbf5238
AJ
10288/* Translate section/page access permissions to page
10289 * R/W protection flags
d76951b6
AJ
10290 *
10291 * @env: CPUARMState
10292 * @mmu_idx: MMU index indicating required translation regime
10293 * @ap: The 3-bit access permissions (AP[2:0])
10294 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
10295 */
10296static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
10297 int ap, int domain_prot)
10298{
554b0b09
PM
10299 bool is_user = regime_is_user(env, mmu_idx);
10300
10301 if (domain_prot == 3) {
10302 return PAGE_READ | PAGE_WRITE;
10303 }
10304
554b0b09
PM
10305 switch (ap) {
10306 case 0:
10307 if (arm_feature(env, ARM_FEATURE_V7)) {
10308 return 0;
10309 }
554b0b09
PM
10310 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
10311 case SCTLR_S:
10312 return is_user ? 0 : PAGE_READ;
10313 case SCTLR_R:
10314 return PAGE_READ;
10315 default:
10316 return 0;
10317 }
10318 case 1:
10319 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10320 case 2:
87c3d486 10321 if (is_user) {
0fbf5238 10322 return PAGE_READ;
87c3d486 10323 } else {
554b0b09 10324 return PAGE_READ | PAGE_WRITE;
87c3d486 10325 }
554b0b09
PM
10326 case 3:
10327 return PAGE_READ | PAGE_WRITE;
10328 case 4: /* Reserved. */
10329 return 0;
10330 case 5:
0fbf5238 10331 return is_user ? 0 : PAGE_READ;
554b0b09 10332 case 6:
0fbf5238 10333 return PAGE_READ;
554b0b09 10334 case 7:
87c3d486 10335 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 10336 return 0;
87c3d486 10337 }
0fbf5238 10338 return PAGE_READ;
554b0b09 10339 default:
0fbf5238 10340 g_assert_not_reached();
554b0b09 10341 }
b5ff1b31
FB
10342}
10343
d76951b6
AJ
10344/* Translate section/page access permissions to page
10345 * R/W protection flags.
10346 *
d76951b6 10347 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 10348 * @is_user: TRUE if accessing from PL0
d76951b6 10349 */
d8e052b3 10350static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 10351{
d76951b6
AJ
10352 switch (ap) {
10353 case 0:
10354 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10355 case 1:
10356 return PAGE_READ | PAGE_WRITE;
10357 case 2:
10358 return is_user ? 0 : PAGE_READ;
10359 case 3:
10360 return PAGE_READ;
10361 default:
10362 g_assert_not_reached();
10363 }
10364}
10365
d8e052b3
AJ
10366static inline int
10367simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
10368{
10369 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
10370}
10371
6ab1a5ee
EI
10372/* Translate S2 section/page access permissions to protection flags
10373 *
10374 * @env: CPUARMState
10375 * @s2ap: The 2-bit stage2 access permissions (S2AP)
ce3125be
PM
10376 * @xn: XN (execute-never) bits
10377 * @s1_is_el0: true if this is S2 of an S1+2 walk for EL0
6ab1a5ee 10378 */
ce3125be 10379static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0)
6ab1a5ee
EI
10380{
10381 int prot = 0;
10382
10383 if (s2ap & 1) {
10384 prot |= PAGE_READ;
10385 }
10386 if (s2ap & 2) {
10387 prot |= PAGE_WRITE;
10388 }
ce3125be
PM
10389
10390 if (cpu_isar_feature(any_tts2uxn, env_archcpu(env))) {
10391 switch (xn) {
10392 case 0:
dfda6837 10393 prot |= PAGE_EXEC;
ce3125be
PM
10394 break;
10395 case 1:
10396 if (s1_is_el0) {
10397 prot |= PAGE_EXEC;
10398 }
10399 break;
10400 case 2:
10401 break;
10402 case 3:
10403 if (!s1_is_el0) {
10404 prot |= PAGE_EXEC;
10405 }
10406 break;
10407 default:
10408 g_assert_not_reached();
10409 }
10410 } else {
10411 if (!extract32(xn, 1, 1)) {
10412 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
10413 prot |= PAGE_EXEC;
10414 }
dfda6837 10415 }
6ab1a5ee
EI
10416 }
10417 return prot;
10418}
10419
d8e052b3
AJ
10420/* Translate section/page access permissions to protection flags
10421 *
10422 * @env: CPUARMState
10423 * @mmu_idx: MMU index indicating required translation regime
10424 * @is_aa64: TRUE if AArch64
10425 * @ap: The 2-bit simple AP (AP[2:1])
10426 * @ns: NS (non-secure) bit
10427 * @xn: XN (execute-never) bit
10428 * @pxn: PXN (privileged execute-never) bit
10429 */
10430static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
10431 int ap, int ns, int xn, int pxn)
10432{
10433 bool is_user = regime_is_user(env, mmu_idx);
10434 int prot_rw, user_rw;
10435 bool have_wxn;
10436 int wxn = 0;
10437
97fa9350 10438 assert(mmu_idx != ARMMMUIdx_Stage2);
b1a10c86 10439 assert(mmu_idx != ARMMMUIdx_Stage2_S);
d8e052b3
AJ
10440
10441 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
10442 if (is_user) {
10443 prot_rw = user_rw;
10444 } else {
81636b70 10445 if (user_rw && regime_is_pan(env, mmu_idx)) {
f4e1dbc5
PM
10446 /* PAN forbids data accesses but doesn't affect insn fetch */
10447 prot_rw = 0;
10448 } else {
10449 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
81636b70 10450 }
d8e052b3
AJ
10451 }
10452
10453 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
10454 return prot_rw;
10455 }
10456
10457 /* TODO have_wxn should be replaced with
10458 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
10459 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
10460 * compatible processors have EL2, which is required for [U]WXN.
10461 */
10462 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
10463
10464 if (have_wxn) {
10465 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
10466 }
10467
10468 if (is_aa64) {
339370b9
RH
10469 if (regime_has_2_ranges(mmu_idx) && !is_user) {
10470 xn = pxn || (user_rw & PAGE_WRITE);
d8e052b3
AJ
10471 }
10472 } else if (arm_feature(env, ARM_FEATURE_V7)) {
10473 switch (regime_el(env, mmu_idx)) {
10474 case 1:
10475 case 3:
10476 if (is_user) {
10477 xn = xn || !(user_rw & PAGE_READ);
10478 } else {
10479 int uwxn = 0;
10480 if (have_wxn) {
10481 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
10482 }
10483 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
10484 (uwxn && (user_rw & PAGE_WRITE));
10485 }
10486 break;
10487 case 2:
10488 break;
10489 }
10490 } else {
10491 xn = wxn = 0;
10492 }
10493
10494 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
10495 return prot_rw;
10496 }
10497 return prot_rw | PAGE_EXEC;
10498}
10499
0480f69a
PM
10500static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
10501 uint32_t *table, uint32_t address)
b2fa1797 10502{
0480f69a 10503 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 10504 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 10505
11f136ee
FA
10506 if (address & tcr->mask) {
10507 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
10508 /* Translation table walk disabled for TTBR1 */
10509 return false;
10510 }
aef878be 10511 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 10512 } else {
11f136ee 10513 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
10514 /* Translation table walk disabled for TTBR0 */
10515 return false;
10516 }
aef878be 10517 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
10518 }
10519 *table |= (address >> 18) & 0x3ffc;
10520 return true;
b2fa1797
PB
10521}
10522
37785977
EI
10523/* Translate a S1 pagetable walk through S2 if needed. */
10524static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
3d4bd397 10525 hwaddr addr, bool *is_secure,
37785977
EI
10526 ARMMMUFaultInfo *fi)
10527{
fee7aa46 10528 if (arm_mmu_idx_is_stage1_of_2(mmu_idx) &&
97fa9350 10529 !regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
37785977
EI
10530 target_ulong s2size;
10531 hwaddr s2pa;
10532 int s2prot;
10533 int ret;
b1a10c86
RDC
10534 ARMMMUIdx s2_mmu_idx = *is_secure ? ARMMMUIdx_Stage2_S
10535 : ARMMMUIdx_Stage2;
eadb2feb 10536 ARMCacheAttrs cacheattrs = {};
3d4bd397
RDC
10537 MemTxAttrs txattrs = {};
10538
b1a10c86 10539 ret = get_phys_addr_lpae(env, addr, MMU_DATA_LOAD, s2_mmu_idx, false,
59dff859 10540 &s2pa, &txattrs, &s2prot, &s2size, fi,
a6d6f37a 10541 &cacheattrs);
37785977 10542 if (ret) {
3b39d734 10543 assert(fi->type != ARMFault_None);
37785977
EI
10544 fi->s2addr = addr;
10545 fi->stage2 = true;
10546 fi->s1ptw = true;
9861248f 10547 fi->s1ns = !*is_secure;
37785977
EI
10548 return ~0;
10549 }
e04a5752
RDC
10550 if ((arm_hcr_el2_eff(env) & HCR_PTW) &&
10551 (cacheattrs.attrs & 0xf0) == 0) {
a6d6f37a
RH
10552 /*
10553 * PTW set and S1 walk touched S2 Device memory:
10554 * generate Permission fault.
10555 */
eadb2feb
PM
10556 fi->type = ARMFault_Permission;
10557 fi->s2addr = addr;
10558 fi->stage2 = true;
10559 fi->s1ptw = true;
9861248f 10560 fi->s1ns = !*is_secure;
eadb2feb
PM
10561 return ~0;
10562 }
588c6dd1
RDC
10563
10564 if (arm_is_secure_below_el3(env)) {
10565 /* Check if page table walk is to secure or non-secure PA space. */
10566 if (*is_secure) {
10567 *is_secure = !(env->cp15.vstcr_el2.raw_tcr & VSTCR_SW);
10568 } else {
10569 *is_secure = !(env->cp15.vtcr_el2.raw_tcr & VTCR_NSW);
10570 }
10571 } else {
10572 assert(!*is_secure);
10573 }
10574
37785977
EI
10575 addr = s2pa;
10576 }
10577 return addr;
10578}
10579
14577270 10580/* All loads done in the course of a page table walk go through here. */
a614e698 10581static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 10582 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 10583{
a614e698
EI
10584 ARMCPU *cpu = ARM_CPU(cs);
10585 CPUARMState *env = &cpu->env;
ebca90e4 10586 MemTxAttrs attrs = {};
3b39d734 10587 MemTxResult result = MEMTX_OK;
5ce4ff65 10588 AddressSpace *as;
3b39d734 10589 uint32_t data;
ebca90e4 10590
3d4bd397 10591 addr = S1_ptw_translate(env, mmu_idx, addr, &is_secure, fi);
ebca90e4 10592 attrs.secure = is_secure;
5ce4ff65 10593 as = arm_addressspace(cs, attrs);
a614e698
EI
10594 if (fi->s1ptw) {
10595 return 0;
10596 }
73462ddd 10597 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 10598 data = address_space_ldl_be(as, addr, attrs, &result);
73462ddd 10599 } else {
3b39d734 10600 data = address_space_ldl_le(as, addr, attrs, &result);
73462ddd 10601 }
3b39d734
PM
10602 if (result == MEMTX_OK) {
10603 return data;
10604 }
10605 fi->type = ARMFault_SyncExternalOnWalk;
10606 fi->ea = arm_extabort_type(result);
10607 return 0;
ebca90e4
PM
10608}
10609
37785977 10610static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 10611 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 10612{
37785977
EI
10613 ARMCPU *cpu = ARM_CPU(cs);
10614 CPUARMState *env = &cpu->env;
ebca90e4 10615 MemTxAttrs attrs = {};
3b39d734 10616 MemTxResult result = MEMTX_OK;
5ce4ff65 10617 AddressSpace *as;
9aea1ea3 10618 uint64_t data;
ebca90e4 10619
3d4bd397 10620 addr = S1_ptw_translate(env, mmu_idx, addr, &is_secure, fi);
ebca90e4 10621 attrs.secure = is_secure;
5ce4ff65 10622 as = arm_addressspace(cs, attrs);
37785977
EI
10623 if (fi->s1ptw) {
10624 return 0;
10625 }
73462ddd 10626 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 10627 data = address_space_ldq_be(as, addr, attrs, &result);
73462ddd 10628 } else {
3b39d734
PM
10629 data = address_space_ldq_le(as, addr, attrs, &result);
10630 }
10631 if (result == MEMTX_OK) {
10632 return data;
73462ddd 10633 }
3b39d734
PM
10634 fi->type = ARMFault_SyncExternalOnWalk;
10635 fi->ea = arm_extabort_type(result);
10636 return 0;
ebca90e4
PM
10637}
10638
b7cc4e82 10639static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 10640 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10641 hwaddr *phys_ptr, int *prot,
f989983e 10642 target_ulong *page_size,
e14b5a23 10643 ARMMMUFaultInfo *fi)
b5ff1b31 10644{
2fc0cc0e 10645 CPUState *cs = env_cpu(env);
f989983e 10646 int level = 1;
b5ff1b31
FB
10647 uint32_t table;
10648 uint32_t desc;
10649 int type;
10650 int ap;
e389be16 10651 int domain = 0;
dd4ebc2e 10652 int domain_prot;
a8170e5e 10653 hwaddr phys_addr;
0480f69a 10654 uint32_t dacr;
b5ff1b31 10655
9ee6e8bb
PB
10656 /* Pagetable walk. */
10657 /* Lookup l1 descriptor. */
0480f69a 10658 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10659 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f989983e 10660 fi->type = ARMFault_Translation;
e389be16
FA
10661 goto do_fault;
10662 }
a614e698 10663 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10664 mmu_idx, fi);
3b39d734
PM
10665 if (fi->type != ARMFault_None) {
10666 goto do_fault;
10667 }
9ee6e8bb 10668 type = (desc & 3);
dd4ebc2e 10669 domain = (desc >> 5) & 0x0f;
0480f69a
PM
10670 if (regime_el(env, mmu_idx) == 1) {
10671 dacr = env->cp15.dacr_ns;
10672 } else {
10673 dacr = env->cp15.dacr_s;
10674 }
10675 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 10676 if (type == 0) {
601d70b9 10677 /* Section translation fault. */
f989983e 10678 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10679 goto do_fault;
10680 }
f989983e
PM
10681 if (type != 2) {
10682 level = 2;
10683 }
dd4ebc2e 10684 if (domain_prot == 0 || domain_prot == 2) {
f989983e 10685 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10686 goto do_fault;
10687 }
10688 if (type == 2) {
10689 /* 1Mb section. */
10690 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
10691 ap = (desc >> 10) & 3;
d4c430a8 10692 *page_size = 1024 * 1024;
9ee6e8bb
PB
10693 } else {
10694 /* Lookup l2 entry. */
554b0b09
PM
10695 if (type == 1) {
10696 /* Coarse pagetable. */
10697 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
10698 } else {
10699 /* Fine pagetable. */
10700 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
10701 }
a614e698 10702 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10703 mmu_idx, fi);
3b39d734
PM
10704 if (fi->type != ARMFault_None) {
10705 goto do_fault;
10706 }
9ee6e8bb
PB
10707 switch (desc & 3) {
10708 case 0: /* Page translation fault. */
f989983e 10709 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10710 goto do_fault;
10711 case 1: /* 64k page. */
10712 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10713 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 10714 *page_size = 0x10000;
ce819861 10715 break;
9ee6e8bb
PB
10716 case 2: /* 4k page. */
10717 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 10718 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 10719 *page_size = 0x1000;
ce819861 10720 break;
fc1891c7 10721 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 10722 if (type == 1) {
fc1891c7
PM
10723 /* ARMv6/XScale extended small page format */
10724 if (arm_feature(env, ARM_FEATURE_XSCALE)
10725 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 10726 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 10727 *page_size = 0x1000;
554b0b09 10728 } else {
fc1891c7
PM
10729 /* UNPREDICTABLE in ARMv5; we choose to take a
10730 * page translation fault.
10731 */
f989983e 10732 fi->type = ARMFault_Translation;
554b0b09
PM
10733 goto do_fault;
10734 }
10735 } else {
10736 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 10737 *page_size = 0x400;
554b0b09 10738 }
9ee6e8bb 10739 ap = (desc >> 4) & 3;
ce819861
PB
10740 break;
10741 default:
9ee6e8bb 10742 /* Never happens, but compiler isn't smart enough to tell. */
d385a605 10743 g_assert_not_reached();
ce819861 10744 }
9ee6e8bb 10745 }
0fbf5238
AJ
10746 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
10747 *prot |= *prot ? PAGE_EXEC : 0;
10748 if (!(*prot & (1 << access_type))) {
9ee6e8bb 10749 /* Access permission fault. */
f989983e 10750 fi->type = ARMFault_Permission;
9ee6e8bb
PB
10751 goto do_fault;
10752 }
10753 *phys_ptr = phys_addr;
b7cc4e82 10754 return false;
9ee6e8bb 10755do_fault:
f989983e
PM
10756 fi->domain = domain;
10757 fi->level = level;
b7cc4e82 10758 return true;
9ee6e8bb
PB
10759}
10760
b7cc4e82 10761static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 10762 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10763 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
f06cf243 10764 target_ulong *page_size, ARMMMUFaultInfo *fi)
9ee6e8bb 10765{
2fc0cc0e 10766 CPUState *cs = env_cpu(env);
0ae0326b 10767 ARMCPU *cpu = env_archcpu(env);
f06cf243 10768 int level = 1;
9ee6e8bb
PB
10769 uint32_t table;
10770 uint32_t desc;
10771 uint32_t xn;
de9b05b8 10772 uint32_t pxn = 0;
9ee6e8bb
PB
10773 int type;
10774 int ap;
de9b05b8 10775 int domain = 0;
dd4ebc2e 10776 int domain_prot;
a8170e5e 10777 hwaddr phys_addr;
0480f69a 10778 uint32_t dacr;
8bf5b6a9 10779 bool ns;
9ee6e8bb
PB
10780
10781 /* Pagetable walk. */
10782 /* Lookup l1 descriptor. */
0480f69a 10783 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10784 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f06cf243 10785 fi->type = ARMFault_Translation;
e389be16
FA
10786 goto do_fault;
10787 }
a614e698 10788 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10789 mmu_idx, fi);
3b39d734
PM
10790 if (fi->type != ARMFault_None) {
10791 goto do_fault;
10792 }
9ee6e8bb 10793 type = (desc & 3);
0ae0326b 10794 if (type == 0 || (type == 3 && !cpu_isar_feature(aa32_pxn, cpu))) {
de9b05b8
PM
10795 /* Section translation fault, or attempt to use the encoding
10796 * which is Reserved on implementations without PXN.
10797 */
f06cf243 10798 fi->type = ARMFault_Translation;
9ee6e8bb 10799 goto do_fault;
de9b05b8
PM
10800 }
10801 if ((type == 1) || !(desc & (1 << 18))) {
10802 /* Page or Section. */
dd4ebc2e 10803 domain = (desc >> 5) & 0x0f;
9ee6e8bb 10804 }
0480f69a
PM
10805 if (regime_el(env, mmu_idx) == 1) {
10806 dacr = env->cp15.dacr_ns;
10807 } else {
10808 dacr = env->cp15.dacr_s;
10809 }
f06cf243
PM
10810 if (type == 1) {
10811 level = 2;
10812 }
0480f69a 10813 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 10814 if (domain_prot == 0 || domain_prot == 2) {
f06cf243
PM
10815 /* Section or Page domain fault */
10816 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10817 goto do_fault;
10818 }
de9b05b8 10819 if (type != 1) {
9ee6e8bb
PB
10820 if (desc & (1 << 18)) {
10821 /* Supersection. */
10822 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
10823 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
10824 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 10825 *page_size = 0x1000000;
b5ff1b31 10826 } else {
9ee6e8bb
PB
10827 /* Section. */
10828 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 10829 *page_size = 0x100000;
b5ff1b31 10830 }
9ee6e8bb
PB
10831 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
10832 xn = desc & (1 << 4);
de9b05b8 10833 pxn = desc & 1;
8bf5b6a9 10834 ns = extract32(desc, 19, 1);
9ee6e8bb 10835 } else {
0ae0326b 10836 if (cpu_isar_feature(aa32_pxn, cpu)) {
de9b05b8
PM
10837 pxn = (desc >> 2) & 1;
10838 }
8bf5b6a9 10839 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
10840 /* Lookup l2 entry. */
10841 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698 10842 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10843 mmu_idx, fi);
3b39d734
PM
10844 if (fi->type != ARMFault_None) {
10845 goto do_fault;
10846 }
9ee6e8bb
PB
10847 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
10848 switch (desc & 3) {
10849 case 0: /* Page translation fault. */
f06cf243 10850 fi->type = ARMFault_Translation;
b5ff1b31 10851 goto do_fault;
9ee6e8bb
PB
10852 case 1: /* 64k page. */
10853 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10854 xn = desc & (1 << 15);
d4c430a8 10855 *page_size = 0x10000;
9ee6e8bb
PB
10856 break;
10857 case 2: case 3: /* 4k page. */
10858 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10859 xn = desc & 1;
d4c430a8 10860 *page_size = 0x1000;
9ee6e8bb
PB
10861 break;
10862 default:
10863 /* Never happens, but compiler isn't smart enough to tell. */
d385a605 10864 g_assert_not_reached();
b5ff1b31 10865 }
9ee6e8bb 10866 }
dd4ebc2e 10867 if (domain_prot == 3) {
c0034328
JR
10868 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10869 } else {
0480f69a 10870 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
10871 xn = 1;
10872 }
f06cf243
PM
10873 if (xn && access_type == MMU_INST_FETCH) {
10874 fi->type = ARMFault_Permission;
c0034328 10875 goto do_fault;
f06cf243 10876 }
9ee6e8bb 10877
d76951b6
AJ
10878 if (arm_feature(env, ARM_FEATURE_V6K) &&
10879 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
10880 /* The simplified model uses AP[0] as an access control bit. */
10881 if ((ap & 1) == 0) {
10882 /* Access flag fault. */
f06cf243 10883 fi->type = ARMFault_AccessFlag;
d76951b6
AJ
10884 goto do_fault;
10885 }
10886 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
10887 } else {
10888 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 10889 }
0fbf5238
AJ
10890 if (*prot && !xn) {
10891 *prot |= PAGE_EXEC;
10892 }
10893 if (!(*prot & (1 << access_type))) {
c0034328 10894 /* Access permission fault. */
f06cf243 10895 fi->type = ARMFault_Permission;
c0034328
JR
10896 goto do_fault;
10897 }
3ad493fc 10898 }
8bf5b6a9
PM
10899 if (ns) {
10900 /* The NS bit will (as required by the architecture) have no effect if
10901 * the CPU doesn't support TZ or this is a non-secure translation
10902 * regime, because the attribute will already be non-secure.
10903 */
10904 attrs->secure = false;
10905 }
9ee6e8bb 10906 *phys_ptr = phys_addr;
b7cc4e82 10907 return false;
b5ff1b31 10908do_fault:
f06cf243
PM
10909 fi->domain = domain;
10910 fi->level = level;
b7cc4e82 10911 return true;
b5ff1b31
FB
10912}
10913
1853d5a9 10914/*
a0e966c9 10915 * check_s2_mmu_setup
1853d5a9
EI
10916 * @cpu: ARMCPU
10917 * @is_aa64: True if the translation regime is in AArch64 state
10918 * @startlevel: Suggested starting level
10919 * @inputsize: Bitsize of IPAs
10920 * @stride: Page-table stride (See the ARM ARM)
10921 *
a0e966c9
EI
10922 * Returns true if the suggested S2 translation parameters are OK and
10923 * false otherwise.
1853d5a9 10924 */
a0e966c9 10925static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
49ba115b 10926 int inputsize, int stride, int outputsize)
1853d5a9 10927{
98d68ec2
EI
10928 const int grainsize = stride + 3;
10929 int startsizecheck;
10930
ef56c242
RH
10931 /*
10932 * Negative levels are usually not allowed...
10933 * Except for FEAT_LPA2, 4k page table, 52-bit address space, which
10934 * begins with level -1. Note that previous feature tests will have
10935 * eliminated this combination if it is not enabled.
10936 */
10937 if (level < (inputsize == 52 && stride == 9 ? -1 : 0)) {
1853d5a9
EI
10938 return false;
10939 }
10940
98d68ec2
EI
10941 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
10942 if (startsizecheck < 1 || startsizecheck > stride + 4) {
10943 return false;
10944 }
10945
1853d5a9 10946 if (is_aa64) {
1853d5a9
EI
10947 switch (stride) {
10948 case 13: /* 64KB Pages. */
49ba115b 10949 if (level == 0 || (level == 1 && outputsize <= 42)) {
1853d5a9
EI
10950 return false;
10951 }
10952 break;
10953 case 11: /* 16KB Pages. */
49ba115b 10954 if (level == 0 || (level == 1 && outputsize <= 40)) {
1853d5a9
EI
10955 return false;
10956 }
10957 break;
10958 case 9: /* 4KB Pages. */
49ba115b 10959 if (level == 0 && outputsize <= 42) {
1853d5a9
EI
10960 return false;
10961 }
10962 break;
10963 default:
10964 g_assert_not_reached();
10965 }
3526423e
EI
10966
10967 /* Inputsize checks. */
49ba115b
RH
10968 if (inputsize > outputsize &&
10969 (arm_el_is_aa64(&cpu->env, 1) || inputsize > 40)) {
3526423e
EI
10970 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
10971 return false;
10972 }
1853d5a9 10973 } else {
1853d5a9
EI
10974 /* AArch32 only supports 4KB pages. Assert on that. */
10975 assert(stride == 9);
10976
10977 if (level == 0) {
10978 return false;
10979 }
1853d5a9
EI
10980 }
10981 return true;
10982}
10983
5b2d261d
AB
10984/* Translate from the 4-bit stage 2 representation of
10985 * memory attributes (without cache-allocation hints) to
10986 * the 8-bit representation of the stage 1 MAIR registers
10987 * (which includes allocation hints).
10988 *
10989 * ref: shared/translation/attrs/S2AttrDecode()
10990 * .../S2ConvertAttrsHints()
10991 */
10992static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
10993{
10994 uint8_t hiattr = extract32(s2attrs, 2, 2);
10995 uint8_t loattr = extract32(s2attrs, 0, 2);
10996 uint8_t hihint = 0, lohint = 0;
10997
10998 if (hiattr != 0) { /* normal memory */
e04a5752 10999 if (arm_hcr_el2_eff(env) & HCR_CD) { /* cache disabled */
5b2d261d
AB
11000 hiattr = loattr = 1; /* non-cacheable */
11001 } else {
11002 if (hiattr != 1) { /* Write-through or write-back */
11003 hihint = 3; /* RW allocate */
11004 }
11005 if (loattr != 1) { /* Write-through or write-back */
11006 lohint = 3; /* RW allocate */
11007 }
11008 }
11009 }
11010
11011 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
11012}
c47eaf9f 11013#endif /* !CONFIG_USER_ONLY */
5b2d261d 11014
f4ecc015
RH
11015/* This mapping is common between ID_AA64MMFR0.PARANGE and TCR_ELx.{I}PS. */
11016static const uint8_t pamax_map[] = {
11017 [0] = 32,
11018 [1] = 36,
11019 [2] = 40,
11020 [3] = 42,
11021 [4] = 44,
11022 [5] = 48,
7a928f43 11023 [6] = 52,
f4ecc015
RH
11024};
11025
71a77257
RH
11026/* The cpu-specific constant value of PAMax; also used by hw/arm/virt. */
11027unsigned int arm_pamax(ARMCPU *cpu)
11028{
71a77257
RH
11029 unsigned int parange =
11030 FIELD_EX64(cpu->isar.id_aa64mmfr0, ID_AA64MMFR0, PARANGE);
11031
11032 /*
11033 * id_aa64mmfr0 is a read-only register so values outside of the
11034 * supported mappings can be considered an implementation error.
11035 */
11036 assert(parange < ARRAY_SIZE(pamax_map));
11037 return pamax_map[parange];
11038}
11039
b830a5ee
RH
11040static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
11041{
11042 if (regime_has_2_ranges(mmu_idx)) {
11043 return extract64(tcr, 37, 2);
b1a10c86 11044 } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
b830a5ee
RH
11045 return 0; /* VTCR_EL2 */
11046 } else {
3e270f67
RH
11047 /* Replicate the single TBI bit so we always have 2 bits. */
11048 return extract32(tcr, 20, 1) * 3;
b830a5ee
RH
11049 }
11050}
11051
11052static int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
11053{
11054 if (regime_has_2_ranges(mmu_idx)) {
11055 return extract64(tcr, 51, 2);
b1a10c86 11056 } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
b830a5ee
RH
11057 return 0; /* VTCR_EL2 */
11058 } else {
3e270f67
RH
11059 /* Replicate the single TBID bit so we always have 2 bits. */
11060 return extract32(tcr, 29, 1) * 3;
b830a5ee
RH
11061 }
11062}
11063
81ae05fa
RH
11064static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
11065{
11066 if (regime_has_2_ranges(mmu_idx)) {
11067 return extract64(tcr, 57, 2);
11068 } else {
11069 /* Replicate the single TCMA bit so we always have 2 bits. */
11070 return extract32(tcr, 30, 1) * 3;
11071 }
11072}
11073
b830a5ee
RH
11074ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11075 ARMMMUIdx mmu_idx, bool data)
ba97be9f
RH
11076{
11077 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
ef56c242
RH
11078 bool epd, hpd, using16k, using64k, tsz_oob, ds;
11079 int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11080 ARMCPU *cpu = env_archcpu(env);
ba97be9f 11081
339370b9 11082 if (!regime_has_2_ranges(mmu_idx)) {
71d18164 11083 select = 0;
ba97be9f
RH
11084 tsz = extract32(tcr, 0, 6);
11085 using64k = extract32(tcr, 14, 1);
11086 using16k = extract32(tcr, 15, 1);
b1a10c86 11087 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
ba97be9f 11088 /* VTCR_EL2 */
b830a5ee 11089 hpd = false;
ba97be9f 11090 } else {
ba97be9f
RH
11091 hpd = extract32(tcr, 24, 1);
11092 }
11093 epd = false;
ef56c242 11094 sh = extract32(tcr, 12, 2);
f4ecc015 11095 ps = extract32(tcr, 16, 3);
ef56c242 11096 ds = extract64(tcr, 32, 1);
ba97be9f 11097 } else {
71d18164
RH
11098 /*
11099 * Bit 55 is always between the two regions, and is canonical for
11100 * determining if address tagging is enabled.
11101 */
11102 select = extract64(va, 55, 1);
11103 if (!select) {
11104 tsz = extract32(tcr, 0, 6);
11105 epd = extract32(tcr, 7, 1);
ef56c242 11106 sh = extract32(tcr, 12, 2);
71d18164
RH
11107 using64k = extract32(tcr, 14, 1);
11108 using16k = extract32(tcr, 15, 1);
71d18164 11109 hpd = extract64(tcr, 41, 1);
71d18164
RH
11110 } else {
11111 int tg = extract32(tcr, 30, 2);
11112 using16k = tg == 1;
11113 using64k = tg == 3;
11114 tsz = extract32(tcr, 16, 6);
11115 epd = extract32(tcr, 23, 1);
ef56c242 11116 sh = extract32(tcr, 28, 2);
71d18164 11117 hpd = extract64(tcr, 42, 1);
71d18164 11118 }
f4ecc015 11119 ps = extract64(tcr, 32, 3);
ef56c242 11120 ds = extract64(tcr, 59, 1);
ba97be9f 11121 }
c36c65ea 11122
ef56c242 11123 if (cpu_isar_feature(aa64_st, cpu)) {
c36c65ea
RDC
11124 max_tsz = 48 - using64k;
11125 } else {
11126 max_tsz = 39;
11127 }
0af312b6 11128
ef56c242
RH
11129 /*
11130 * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11131 * adjust the effective value of DS, as documented.
11132 */
0af312b6
RH
11133 min_tsz = 16;
11134 if (using64k) {
ef56c242
RH
11135 if (cpu_isar_feature(aa64_lva, cpu)) {
11136 min_tsz = 12;
11137 }
11138 ds = false;
11139 } else if (ds) {
11140 switch (mmu_idx) {
11141 case ARMMMUIdx_Stage2:
11142 case ARMMMUIdx_Stage2_S:
11143 if (using16k) {
11144 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11145 } else {
11146 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11147 }
11148 break;
11149 default:
11150 if (using16k) {
11151 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11152 } else {
11153 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11154 }
11155 break;
11156 }
11157 if (ds) {
0af312b6
RH
11158 min_tsz = 12;
11159 }
11160 }
c36c65ea 11161
ebf93ce7
RH
11162 if (tsz > max_tsz) {
11163 tsz = max_tsz;
11164 tsz_oob = true;
11165 } else if (tsz < min_tsz) {
11166 tsz = min_tsz;
11167 tsz_oob = true;
11168 } else {
11169 tsz_oob = false;
11170 }
ba97be9f 11171
b830a5ee
RH
11172 /* Present TBI as a composite with TBID. */
11173 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11174 if (!data) {
11175 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11176 }
11177 tbi = (tbi >> select) & 1;
11178
ba97be9f
RH
11179 return (ARMVAParameters) {
11180 .tsz = tsz,
f4ecc015 11181 .ps = ps,
ef56c242 11182 .sh = sh,
ba97be9f
RH
11183 .select = select,
11184 .tbi = tbi,
11185 .epd = epd,
11186 .hpd = hpd,
11187 .using16k = using16k,
11188 .using64k = using64k,
ebf93ce7 11189 .tsz_oob = tsz_oob,
ef56c242 11190 .ds = ds,
ba97be9f
RH
11191 };
11192}
11193
c47eaf9f 11194#ifndef CONFIG_USER_ONLY
ba97be9f
RH
11195static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
11196 ARMMMUIdx mmu_idx)
11197{
11198 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
11199 uint32_t el = regime_el(env, mmu_idx);
11200 int select, tsz;
11201 bool epd, hpd;
11202
b1a10c86
RDC
11203 assert(mmu_idx != ARMMMUIdx_Stage2_S);
11204
97fa9350 11205 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f
RH
11206 /* VTCR */
11207 bool sext = extract32(tcr, 4, 1);
11208 bool sign = extract32(tcr, 3, 1);
11209
11210 /*
11211 * If the sign-extend bit is not the same as t0sz[3], the result
11212 * is unpredictable. Flag this as a guest error.
11213 */
11214 if (sign != sext) {
11215 qemu_log_mask(LOG_GUEST_ERROR,
11216 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
11217 }
11218 tsz = sextract32(tcr, 0, 4) + 8;
11219 select = 0;
11220 hpd = false;
11221 epd = false;
11222 } else if (el == 2) {
11223 /* HTCR */
11224 tsz = extract32(tcr, 0, 3);
11225 select = 0;
11226 hpd = extract64(tcr, 24, 1);
11227 epd = false;
11228 } else {
11229 int t0sz = extract32(tcr, 0, 3);
11230 int t1sz = extract32(tcr, 16, 3);
11231
11232 if (t1sz == 0) {
11233 select = va > (0xffffffffu >> t0sz);
11234 } else {
11235 /* Note that we will detect errors later. */
11236 select = va >= ~(0xffffffffu >> t1sz);
11237 }
11238 if (!select) {
11239 tsz = t0sz;
11240 epd = extract32(tcr, 7, 1);
11241 hpd = extract64(tcr, 41, 1);
11242 } else {
11243 tsz = t1sz;
11244 epd = extract32(tcr, 23, 1);
11245 hpd = extract64(tcr, 42, 1);
11246 }
11247 /* For aarch32, hpd0 is not enabled without t2e as well. */
11248 hpd &= extract32(tcr, 6, 1);
11249 }
11250
11251 return (ARMVAParameters) {
11252 .tsz = tsz,
11253 .select = select,
11254 .epd = epd,
11255 .hpd = hpd,
11256 };
11257}
11258
ff7de2fc
PM
11259/**
11260 * get_phys_addr_lpae: perform one stage of page table walk, LPAE format
11261 *
11262 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
11263 * prot and page_size may not be filled in, and the populated fsr value provides
11264 * information on why the translation aborted, in the format of a long-format
11265 * DFSR/IFSR fault register, with the following caveats:
11266 * * the WnR bit is never set (the caller must do this).
11267 *
11268 * @env: CPUARMState
11269 * @address: virtual address to get physical address for
11270 * @access_type: MMU_DATA_LOAD, MMU_DATA_STORE or MMU_INST_FETCH
11271 * @mmu_idx: MMU index indicating required translation regime
11272 * @s1_is_el0: if @mmu_idx is ARMMMUIdx_Stage2 (so this is a stage 2 page table
11273 * walk), must be true if this is stage 2 of a stage 1+2 walk for an
11274 * EL0 access). If @mmu_idx is anything else, @s1_is_el0 is ignored.
11275 * @phys_ptr: set to the physical address corresponding to the virtual address
11276 * @attrs: set to the memory transaction attributes to use
11277 * @prot: set to the permissions for the page containing phys_ptr
11278 * @page_size_ptr: set to the size of the page containing phys_ptr
11279 * @fi: set to fault info if the translation fails
11280 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
11281 */
98e87797 11282static bool get_phys_addr_lpae(CPUARMState *env, uint64_t address,
03ae85f8 11283 MMUAccessType access_type, ARMMMUIdx mmu_idx,
ff7de2fc 11284 bool s1_is_el0,
b7cc4e82 11285 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 11286 target_ulong *page_size_ptr,
5b2d261d 11287 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
3dde962f 11288{
2fc0cc0e 11289 ARMCPU *cpu = env_archcpu(env);
1853d5a9 11290 CPUState *cs = CPU(cpu);
3dde962f 11291 /* Read an LPAE long-descriptor translation table. */
da909b2c 11292 ARMFaultType fault_type = ARMFault_Translation;
1b4093ea 11293 uint32_t level;
ba97be9f 11294 ARMVAParameters param;
3dde962f 11295 uint64_t ttbr;
dddb5223 11296 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f 11297 uint32_t tableattrs;
36d820af 11298 target_ulong page_size;
3dde962f 11299 uint32_t attrs;
ba97be9f 11300 int32_t stride;
49ba115b 11301 int addrsize, inputsize, outputsize;
0480f69a 11302 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 11303 int ap, ns, xn, pxn;
88e8add8 11304 uint32_t el = regime_el(env, mmu_idx);
6109769a 11305 uint64_t descaddrmask;
6e99f762 11306 bool aarch64 = arm_el_is_aa64(env, el);
1bafc2ba 11307 bool guarded = false;
0480f69a 11308
07d1be3b 11309 /* TODO: This code does not support shareability levels. */
6e99f762 11310 if (aarch64) {
f4ecc015
RH
11311 int ps;
11312
ba97be9f
RH
11313 param = aa64_va_parameters(env, address, mmu_idx,
11314 access_type != MMU_INST_FETCH);
1b4093ea 11315 level = 0;
ebf93ce7
RH
11316
11317 /*
11318 * If TxSZ is programmed to a value larger than the maximum,
11319 * or smaller than the effective minimum, it is IMPLEMENTATION
11320 * DEFINED whether we behave as if the field were programmed
11321 * within bounds, or if a level 0 Translation fault is generated.
11322 *
11323 * With FEAT_LVA, fault on less than minimum becomes required,
11324 * so our choice is to always raise the fault.
11325 */
11326 if (param.tsz_oob) {
11327 fault_type = ARMFault_Translation;
11328 goto do_fault;
11329 }
11330
ba97be9f
RH
11331 addrsize = 64 - 8 * param.tbi;
11332 inputsize = 64 - param.tsz;
f4ecc015
RH
11333
11334 /*
11335 * Bound PS by PARANGE to find the effective output address size.
11336 * ID_AA64MMFR0 is a read-only register so values outside of the
11337 * supported mappings can be considered an implementation error.
11338 */
11339 ps = FIELD_EX64(cpu->isar.id_aa64mmfr0, ID_AA64MMFR0, PARANGE);
11340 ps = MIN(ps, param.ps);
11341 assert(ps < ARRAY_SIZE(pamax_map));
11342 outputsize = pamax_map[ps];
d0a2cbce 11343 } else {
ba97be9f 11344 param = aa32_va_parameters(env, address, mmu_idx);
1b4093ea 11345 level = 1;
97fa9350 11346 addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32);
ba97be9f 11347 inputsize = addrsize - param.tsz;
49ba115b 11348 outputsize = 40;
2c8dd318 11349 }
3dde962f 11350
ba97be9f
RH
11351 /*
11352 * We determined the region when collecting the parameters, but we
11353 * have not yet validated that the address is valid for the region.
11354 * Extract the top bits and verify that they all match select.
36d820af
RH
11355 *
11356 * For aa32, if inputsize == addrsize, then we have selected the
11357 * region by exclusion in aa32_va_parameters and there is no more
11358 * validation to do here.
11359 */
11360 if (inputsize < addrsize) {
11361 target_ulong top_bits = sextract64(address, inputsize,
11362 addrsize - inputsize);
03f27724 11363 if (-top_bits != param.select) {
36d820af
RH
11364 /* The gap between the two regions is a Translation fault */
11365 fault_type = ARMFault_Translation;
11366 goto do_fault;
11367 }
3dde962f
PM
11368 }
11369
ba97be9f
RH
11370 if (param.using64k) {
11371 stride = 13;
11372 } else if (param.using16k) {
11373 stride = 11;
11374 } else {
11375 stride = 9;
11376 }
11377
3dde962f
PM
11378 /* Note that QEMU ignores shareability and cacheability attributes,
11379 * so we don't need to do anything with the SH, ORGN, IRGN fields
11380 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
11381 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
11382 * implement any ASID-like capability so we can ignore it (instead
11383 * we will always flush the TLB any time the ASID is changed).
11384 */
ba97be9f 11385 ttbr = regime_ttbr(env, mmu_idx, param.select);
3dde962f 11386
0480f69a 11387 /* Here we should have set up all the parameters for the translation:
6e99f762 11388 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
11389 */
11390
ba97be9f 11391 if (param.epd) {
88e8add8
GB
11392 /* Translation table walk disabled => Translation fault on TLB miss
11393 * Note: This is always 0 on 64-bit EL2 and EL3.
11394 */
3dde962f
PM
11395 goto do_fault;
11396 }
11397
b1a10c86 11398 if (mmu_idx != ARMMMUIdx_Stage2 && mmu_idx != ARMMMUIdx_Stage2_S) {
1853d5a9
EI
11399 /* The starting level depends on the virtual address size (which can
11400 * be up to 48 bits) and the translation granule size. It indicates
11401 * the number of strides (stride bits at a time) needed to
11402 * consume the bits of the input address. In the pseudocode this is:
11403 * level = 4 - RoundUp((inputsize - grainsize) / stride)
11404 * where their 'inputsize' is our 'inputsize', 'grainsize' is
11405 * our 'stride + 3' and 'stride' is our 'stride'.
11406 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
11407 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
11408 * = 4 - (inputsize - 4) / stride;
11409 */
11410 level = 4 - (inputsize - 4) / stride;
11411 } else {
11412 /* For stage 2 translations the starting level is specified by the
11413 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
11414 */
1b4093ea 11415 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
ef56c242 11416 uint32_t sl2 = extract64(tcr->raw_tcr, 33, 1);
1b4093ea 11417 uint32_t startlevel;
1853d5a9
EI
11418 bool ok;
11419
ef56c242
RH
11420 /* SL2 is RES0 unless DS=1 & 4kb granule. */
11421 if (param.ds && stride == 9 && sl2) {
11422 if (sl0 != 0) {
11423 level = 0;
11424 fault_type = ARMFault_Translation;
11425 goto do_fault;
11426 }
11427 startlevel = -1;
11428 } else if (!aarch64 || stride == 9) {
1853d5a9 11429 /* AArch32 or 4KB pages */
1b4093ea 11430 startlevel = 2 - sl0;
c36c65ea
RDC
11431
11432 if (cpu_isar_feature(aa64_st, cpu)) {
11433 startlevel &= 3;
11434 }
1853d5a9
EI
11435 } else {
11436 /* 16KB or 64KB pages */
1b4093ea 11437 startlevel = 3 - sl0;
1853d5a9
EI
11438 }
11439
11440 /* Check that the starting level is valid. */
6e99f762 11441 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
49ba115b 11442 inputsize, stride, outputsize);
1853d5a9 11443 if (!ok) {
da909b2c 11444 fault_type = ARMFault_Translation;
1853d5a9
EI
11445 goto do_fault;
11446 }
1b4093ea 11447 level = startlevel;
1853d5a9 11448 }
3dde962f 11449
d06449f2
RH
11450 indexmask_grainsize = MAKE_64BIT_MASK(0, stride + 3);
11451 indexmask = MAKE_64BIT_MASK(0, inputsize - (stride * (4 - level)));
3dde962f
PM
11452
11453 /* Now we can extract the actual base address from the TTBR */
2c8dd318 11454 descaddr = extract64(ttbr, 0, 48);
f4ecc015
RH
11455
11456 /*
7a928f43
RH
11457 * For FEAT_LPA and PS=6, bits [51:48] of descaddr are in [5:2] of TTBR.
11458 *
11459 * Otherwise, if the base address is out of range, raise AddressSizeFault.
f4ecc015
RH
11460 * In the pseudocode, this is !IsZero(baseregister<47:outputsize>),
11461 * but we've just cleared the bits above 47, so simplify the test.
11462 */
7a928f43
RH
11463 if (outputsize > 48) {
11464 descaddr |= extract64(ttbr, 2, 4) << 48;
11465 } else if (descaddr >> outputsize) {
f4ecc015
RH
11466 level = 0;
11467 fault_type = ARMFault_AddressSize;
11468 goto do_fault;
11469 }
11470
41a4bf1f
PM
11471 /*
11472 * We rely on this masking to clear the RES0 bits at the bottom of the TTBR
11473 * and also to mask out CnP (bit 0) which could validly be non-zero.
11474 */
dddb5223 11475 descaddr &= ~indexmask;
3dde962f 11476
f4ecc015
RH
11477 /*
11478 * For AArch32, the address field in the descriptor goes up to bit 39
11479 * for both v7 and v8. However, for v8 the SBZ bits [47:40] must be 0
11480 * or an AddressSize fault is raised. So for v8 we extract those SBZ
11481 * bits as part of the address, which will be checked via outputsize.
ef56c242
RH
11482 * For AArch64, the address field goes up to bit 47, or 49 with FEAT_LPA2;
11483 * the highest bits of a 52-bit output are placed elsewhere.
6109769a 11484 */
ef56c242
RH
11485 if (param.ds) {
11486 descaddrmask = MAKE_64BIT_MASK(0, 50);
11487 } else if (arm_feature(env, ARM_FEATURE_V8)) {
f4ecc015
RH
11488 descaddrmask = MAKE_64BIT_MASK(0, 48);
11489 } else {
11490 descaddrmask = MAKE_64BIT_MASK(0, 40);
11491 }
11492 descaddrmask &= ~indexmask_grainsize;
6109769a 11493
ebca90e4
PM
11494 /* Secure accesses start with the page table in secure memory and
11495 * can be downgraded to non-secure at any step. Non-secure accesses
11496 * remain non-secure. We implement this by just ORing in the NSTable/NS
11497 * bits at each step.
11498 */
11499 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
11500 for (;;) {
11501 uint64_t descriptor;
ebca90e4 11502 bool nstable;
3dde962f 11503
dddb5223 11504 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 11505 descaddr &= ~7ULL;
ebca90e4 11506 nstable = extract32(tableattrs, 4, 1);
3795a6de 11507 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
3b39d734 11508 if (fi->type != ARMFault_None) {
37785977
EI
11509 goto do_fault;
11510 }
11511
3dde962f
PM
11512 if (!(descriptor & 1) ||
11513 (!(descriptor & 2) && (level == 3))) {
11514 /* Invalid, or the Reserved level 3 encoding */
11515 goto do_fault;
11516 }
f4ecc015 11517
6109769a 11518 descaddr = descriptor & descaddrmask;
7a928f43
RH
11519
11520 /*
11521 * For FEAT_LPA and PS=6, bits [51:48] of descaddr are in [15:12]
ef56c242
RH
11522 * of descriptor. For FEAT_LPA2 and effective DS, bits [51:50] of
11523 * descaddr are in [9:8]. Otherwise, if descaddr is out of range,
11524 * raise AddressSizeFault.
7a928f43
RH
11525 */
11526 if (outputsize > 48) {
ef56c242
RH
11527 if (param.ds) {
11528 descaddr |= extract64(descriptor, 8, 2) << 50;
11529 } else {
11530 descaddr |= extract64(descriptor, 12, 4) << 48;
11531 }
7a928f43 11532 } else if (descaddr >> outputsize) {
f4ecc015
RH
11533 fault_type = ARMFault_AddressSize;
11534 goto do_fault;
11535 }
3dde962f
PM
11536
11537 if ((descriptor & 2) && (level < 3)) {
037c13c5 11538 /* Table entry. The top five bits are attributes which may
3dde962f
PM
11539 * propagate down through lower levels of the table (and
11540 * which are all arranged so that 0 means "no effect", so
11541 * we can gather them up by ORing in the bits at each level).
11542 */
11543 tableattrs |= extract64(descriptor, 59, 5);
11544 level++;
dddb5223 11545 indexmask = indexmask_grainsize;
3dde962f
PM
11546 continue;
11547 }
39a1fd25
PM
11548 /*
11549 * Block entry at level 1 or 2, or page entry at level 3.
3dde962f 11550 * These are basically the same thing, although the number
39a1fd25
PM
11551 * of bits we pull in from the vaddr varies. Note that although
11552 * descaddrmask masks enough of the low bits of the descriptor
11553 * to give a correct page or table address, the address field
11554 * in a block descriptor is smaller; so we need to explicitly
11555 * clear the lower bits here before ORing in the low vaddr bits.
3dde962f 11556 */
973a5434 11557 page_size = (1ULL << ((stride * (4 - level)) + 3));
39a1fd25 11558 descaddr &= ~(page_size - 1);
3dde962f 11559 descaddr |= (address & (page_size - 1));
6ab1a5ee 11560 /* Extract attributes from the descriptor */
d615efac
IC
11561 attrs = extract64(descriptor, 2, 10)
11562 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee 11563
b1a10c86 11564 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
6ab1a5ee
EI
11565 /* Stage 2 table descriptors do not include any attribute fields */
11566 break;
11567 }
11568 /* Merge in attributes from table descriptors */
037c13c5 11569 attrs |= nstable << 3; /* NS */
1bafc2ba 11570 guarded = extract64(descriptor, 50, 1); /* GP */
ba97be9f 11571 if (param.hpd) {
037c13c5
RH
11572 /* HPD disables all the table attributes except NSTable. */
11573 break;
11574 }
11575 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
3dde962f
PM
11576 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
11577 * means "force PL1 access only", which means forcing AP[1] to 0.
11578 */
037c13c5
RH
11579 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
11580 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
3dde962f
PM
11581 break;
11582 }
11583 /* Here descaddr is the final physical address, and attributes
11584 * are all in attrs.
11585 */
da909b2c 11586 fault_type = ARMFault_AccessFlag;
3dde962f
PM
11587 if ((attrs & (1 << 8)) == 0) {
11588 /* Access flag */
11589 goto do_fault;
11590 }
d8e052b3
AJ
11591
11592 ap = extract32(attrs, 4, 2);
d8e052b3 11593
b1a10c86
RDC
11594 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11595 ns = mmu_idx == ARMMMUIdx_Stage2;
ce3125be
PM
11596 xn = extract32(attrs, 11, 2);
11597 *prot = get_S2prot(env, ap, xn, s1_is_el0);
6ab1a5ee
EI
11598 } else {
11599 ns = extract32(attrs, 3, 1);
ce3125be 11600 xn = extract32(attrs, 12, 1);
6ab1a5ee 11601 pxn = extract32(attrs, 11, 1);
6e99f762 11602 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 11603 }
d8e052b3 11604
da909b2c 11605 fault_type = ARMFault_Permission;
d8e052b3 11606 if (!(*prot & (1 << access_type))) {
3dde962f
PM
11607 goto do_fault;
11608 }
3dde962f 11609
8bf5b6a9
PM
11610 if (ns) {
11611 /* The NS bit will (as required by the architecture) have no effect if
11612 * the CPU doesn't support TZ or this is a non-secure translation
11613 * regime, because the attribute will already be non-secure.
11614 */
11615 txattrs->secure = false;
11616 }
1bafc2ba
RH
11617 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
11618 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
149d3b31 11619 arm_tlb_bti_gp(txattrs) = true;
1bafc2ba 11620 }
5b2d261d 11621
b1a10c86 11622 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
7e98e21c
RH
11623 cacheattrs->attrs = convert_stage2_attrs(env, extract32(attrs, 0, 4));
11624 } else {
11625 /* Index into MAIR registers for cache attributes */
11626 uint8_t attrindx = extract32(attrs, 0, 3);
11627 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
11628 assert(attrindx <= 7);
11629 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
5b2d261d 11630 }
ef56c242
RH
11631
11632 /*
11633 * For FEAT_LPA2 and effective DS, the SH field in the attributes
11634 * was re-purposed for output address bits. The SH attribute in
11635 * that case comes from TCR_ELx, which we extracted earlier.
11636 */
11637 if (param.ds) {
11638 cacheattrs->shareability = param.sh;
11639 } else {
11640 cacheattrs->shareability = extract32(attrs, 6, 2);
11641 }
5b2d261d 11642
3dde962f
PM
11643 *phys_ptr = descaddr;
11644 *page_size_ptr = page_size;
b7cc4e82 11645 return false;
3dde962f
PM
11646
11647do_fault:
da909b2c
PM
11648 fi->type = fault_type;
11649 fi->level = level;
37785977 11650 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
b1a10c86
RDC
11651 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_Stage2 ||
11652 mmu_idx == ARMMMUIdx_Stage2_S);
9861248f 11653 fi->s1ns = mmu_idx == ARMMMUIdx_Stage2;
b7cc4e82 11654 return true;
3dde962f
PM
11655}
11656
f6bda88f
PC
11657static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
11658 ARMMMUIdx mmu_idx,
11659 int32_t address, int *prot)
11660{
3a00d560
MD
11661 if (!arm_feature(env, ARM_FEATURE_M)) {
11662 *prot = PAGE_READ | PAGE_WRITE;
11663 switch (address) {
11664 case 0xF0000000 ... 0xFFFFFFFF:
11665 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
11666 /* hivecs execing is ok */
11667 *prot |= PAGE_EXEC;
11668 }
11669 break;
11670 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 11671 *prot |= PAGE_EXEC;
3a00d560
MD
11672 break;
11673 }
11674 } else {
11675 /* Default system address map for M profile cores.
11676 * The architecture specifies which regions are execute-never;
11677 * at the MPU level no other checks are defined.
11678 */
11679 switch (address) {
11680 case 0x00000000 ... 0x1fffffff: /* ROM */
11681 case 0x20000000 ... 0x3fffffff: /* SRAM */
11682 case 0x60000000 ... 0x7fffffff: /* RAM */
11683 case 0x80000000 ... 0x9fffffff: /* RAM */
11684 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11685 break;
11686 case 0x40000000 ... 0x5fffffff: /* Peripheral */
11687 case 0xa0000000 ... 0xbfffffff: /* Device */
11688 case 0xc0000000 ... 0xdfffffff: /* Device */
11689 case 0xe0000000 ... 0xffffffff: /* System */
11690 *prot = PAGE_READ | PAGE_WRITE;
11691 break;
11692 default:
11693 g_assert_not_reached();
f6bda88f 11694 }
f6bda88f 11695 }
f6bda88f
PC
11696}
11697
29c483a5
MD
11698static bool pmsav7_use_background_region(ARMCPU *cpu,
11699 ARMMMUIdx mmu_idx, bool is_user)
11700{
11701 /* Return true if we should use the default memory map as a
11702 * "background" region if there are no hits against any MPU regions.
11703 */
11704 CPUARMState *env = &cpu->env;
11705
11706 if (is_user) {
11707 return false;
11708 }
11709
11710 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
11711 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
11712 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
11713 } else {
11714 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
11715 }
11716}
11717
38aaa60c
PM
11718static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
11719{
11720 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
11721 return arm_feature(env, ARM_FEATURE_M) &&
11722 extract32(address, 20, 12) == 0xe00;
11723}
11724
bf446a11
PM
11725static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
11726{
11727 /* True if address is in the M profile system region
11728 * 0xe0000000 - 0xffffffff
11729 */
11730 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
11731}
11732
f6bda88f 11733static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 11734 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9375ad15 11735 hwaddr *phys_ptr, int *prot,
e5e40999 11736 target_ulong *page_size,
9375ad15 11737 ARMMMUFaultInfo *fi)
f6bda88f 11738{
2fc0cc0e 11739 ARMCPU *cpu = env_archcpu(env);
f6bda88f
PC
11740 int n;
11741 bool is_user = regime_is_user(env, mmu_idx);
11742
11743 *phys_ptr = address;
e5e40999 11744 *page_size = TARGET_PAGE_SIZE;
f6bda88f
PC
11745 *prot = 0;
11746
38aaa60c
PM
11747 if (regime_translation_disabled(env, mmu_idx) ||
11748 m_is_ppb_region(env, address)) {
11749 /* MPU disabled or M profile PPB access: use default memory map.
11750 * The other case which uses the default memory map in the
11751 * v7M ARM ARM pseudocode is exception vector reads from the vector
11752 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
11753 * which always does a direct read using address_space_ldl(), rather
11754 * than going via this function, so we don't need to check that here.
11755 */
f6bda88f
PC
11756 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11757 } else { /* MPU enabled */
11758 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11759 /* region search */
11760 uint32_t base = env->pmsav7.drbar[n];
11761 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
11762 uint32_t rmask;
11763 bool srdis = false;
11764
11765 if (!(env->pmsav7.drsr[n] & 0x1)) {
11766 continue;
11767 }
11768
11769 if (!rsize) {
c9f9f124
MD
11770 qemu_log_mask(LOG_GUEST_ERROR,
11771 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
11772 continue;
11773 }
11774 rsize++;
11775 rmask = (1ull << rsize) - 1;
11776
11777 if (base & rmask) {
c9f9f124
MD
11778 qemu_log_mask(LOG_GUEST_ERROR,
11779 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
11780 "to DRSR region size, mask = 0x%" PRIx32 "\n",
11781 n, base, rmask);
f6bda88f
PC
11782 continue;
11783 }
11784
11785 if (address < base || address > base + rmask) {
9d2b5a58
PM
11786 /*
11787 * Address not in this region. We must check whether the
11788 * region covers addresses in the same page as our address.
11789 * In that case we must not report a size that covers the
11790 * whole page for a subsequent hit against a different MPU
11791 * region or the background region, because it would result in
11792 * incorrect TLB hits for subsequent accesses to addresses that
11793 * are in this MPU region.
11794 */
11795 if (ranges_overlap(base, rmask,
11796 address & TARGET_PAGE_MASK,
11797 TARGET_PAGE_SIZE)) {
11798 *page_size = 1;
11799 }
f6bda88f
PC
11800 continue;
11801 }
11802
11803 /* Region matched */
11804
11805 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
11806 int i, snd;
11807 uint32_t srdis_mask;
11808
11809 rsize -= 3; /* sub region size (power of 2) */
11810 snd = ((address - base) >> rsize) & 0x7;
11811 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
11812
11813 srdis_mask = srdis ? 0x3 : 0x0;
11814 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
11815 /* This will check in groups of 2, 4 and then 8, whether
11816 * the subregion bits are consistent. rsize is incremented
11817 * back up to give the region size, considering consistent
11818 * adjacent subregions as one region. Stop testing if rsize
11819 * is already big enough for an entire QEMU page.
11820 */
11821 int snd_rounded = snd & ~(i - 1);
11822 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
11823 snd_rounded + 8, i);
11824 if (srdis_mask ^ srdis_multi) {
11825 break;
11826 }
11827 srdis_mask = (srdis_mask << i) | srdis_mask;
11828 rsize++;
11829 }
11830 }
f6bda88f
PC
11831 if (srdis) {
11832 continue;
11833 }
e5e40999
PM
11834 if (rsize < TARGET_PAGE_BITS) {
11835 *page_size = 1 << rsize;
11836 }
f6bda88f
PC
11837 break;
11838 }
11839
11840 if (n == -1) { /* no hits */
29c483a5 11841 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f 11842 /* background fault */
9375ad15 11843 fi->type = ARMFault_Background;
f6bda88f
PC
11844 return true;
11845 }
11846 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11847 } else { /* a MPU hit! */
11848 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
11849 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
11850
11851 if (m_is_system_region(env, address)) {
11852 /* System space is always execute never */
11853 xn = 1;
11854 }
f6bda88f
PC
11855
11856 if (is_user) { /* User mode AP bit decoding */
11857 switch (ap) {
11858 case 0:
11859 case 1:
11860 case 5:
11861 break; /* no access */
11862 case 3:
11863 *prot |= PAGE_WRITE;
11864 /* fall through */
11865 case 2:
11866 case 6:
11867 *prot |= PAGE_READ | PAGE_EXEC;
11868 break;
8638f1ad
PM
11869 case 7:
11870 /* for v7M, same as 6; for R profile a reserved value */
11871 if (arm_feature(env, ARM_FEATURE_M)) {
11872 *prot |= PAGE_READ | PAGE_EXEC;
11873 break;
11874 }
11875 /* fall through */
f6bda88f
PC
11876 default:
11877 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
11878 "DRACR[%d]: Bad value for AP bits: 0x%"
11879 PRIx32 "\n", n, ap);
f6bda88f
PC
11880 }
11881 } else { /* Priv. mode AP bits decoding */
11882 switch (ap) {
11883 case 0:
11884 break; /* no access */
11885 case 1:
11886 case 2:
11887 case 3:
11888 *prot |= PAGE_WRITE;
11889 /* fall through */
11890 case 5:
11891 case 6:
11892 *prot |= PAGE_READ | PAGE_EXEC;
11893 break;
8638f1ad
PM
11894 case 7:
11895 /* for v7M, same as 6; for R profile a reserved value */
11896 if (arm_feature(env, ARM_FEATURE_M)) {
11897 *prot |= PAGE_READ | PAGE_EXEC;
11898 break;
11899 }
11900 /* fall through */
f6bda88f
PC
11901 default:
11902 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
11903 "DRACR[%d]: Bad value for AP bits: 0x%"
11904 PRIx32 "\n", n, ap);
f6bda88f
PC
11905 }
11906 }
11907
11908 /* execute never */
bf446a11 11909 if (xn) {
f6bda88f
PC
11910 *prot &= ~PAGE_EXEC;
11911 }
11912 }
11913 }
11914
9375ad15
PM
11915 fi->type = ARMFault_Permission;
11916 fi->level = 1;
f6bda88f
PC
11917 return !(*prot & (1 << access_type));
11918}
11919
35337cc3
PM
11920static bool v8m_is_sau_exempt(CPUARMState *env,
11921 uint32_t address, MMUAccessType access_type)
11922{
11923 /* The architecture specifies that certain address ranges are
11924 * exempt from v8M SAU/IDAU checks.
11925 */
11926 return
11927 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
11928 (address >= 0xe0000000 && address <= 0xe0002fff) ||
11929 (address >= 0xe000e000 && address <= 0xe000efff) ||
11930 (address >= 0xe002e000 && address <= 0xe002efff) ||
11931 (address >= 0xe0040000 && address <= 0xe0041fff) ||
11932 (address >= 0xe00ff000 && address <= 0xe00fffff);
11933}
11934
787a7e76 11935void v8m_security_lookup(CPUARMState *env, uint32_t address,
35337cc3
PM
11936 MMUAccessType access_type, ARMMMUIdx mmu_idx,
11937 V8M_SAttributes *sattrs)
11938{
11939 /* Look up the security attributes for this address. Compare the
11940 * pseudocode SecurityCheck() function.
11941 * We assume the caller has zero-initialized *sattrs.
11942 */
2fc0cc0e 11943 ARMCPU *cpu = env_archcpu(env);
35337cc3 11944 int r;
181962fd
PM
11945 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
11946 int idau_region = IREGION_NOTVALID;
72042435
PM
11947 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
11948 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
35337cc3 11949
181962fd
PM
11950 if (cpu->idau) {
11951 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
11952 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
11953
11954 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
11955 &idau_nsc);
11956 }
35337cc3
PM
11957
11958 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
11959 /* 0xf0000000..0xffffffff is always S for insn fetches */
11960 return;
11961 }
11962
181962fd 11963 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
35337cc3
PM
11964 sattrs->ns = !regime_is_secure(env, mmu_idx);
11965 return;
11966 }
11967
181962fd
PM
11968 if (idau_region != IREGION_NOTVALID) {
11969 sattrs->irvalid = true;
11970 sattrs->iregion = idau_region;
11971 }
11972
35337cc3
PM
11973 switch (env->sau.ctrl & 3) {
11974 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
11975 break;
11976 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
11977 sattrs->ns = true;
11978 break;
11979 default: /* SAU.ENABLE == 1 */
11980 for (r = 0; r < cpu->sau_sregion; r++) {
11981 if (env->sau.rlar[r] & 1) {
11982 uint32_t base = env->sau.rbar[r] & ~0x1f;
11983 uint32_t limit = env->sau.rlar[r] | 0x1f;
11984
11985 if (base <= address && limit >= address) {
72042435
PM
11986 if (base > addr_page_base || limit < addr_page_limit) {
11987 sattrs->subpage = true;
11988 }
35337cc3
PM
11989 if (sattrs->srvalid) {
11990 /* If we hit in more than one region then we must report
11991 * as Secure, not NS-Callable, with no valid region
11992 * number info.
11993 */
11994 sattrs->ns = false;
11995 sattrs->nsc = false;
11996 sattrs->sregion = 0;
11997 sattrs->srvalid = false;
11998 break;
11999 } else {
12000 if (env->sau.rlar[r] & 2) {
12001 sattrs->nsc = true;
12002 } else {
12003 sattrs->ns = true;
12004 }
12005 sattrs->srvalid = true;
12006 sattrs->sregion = r;
12007 }
9d2b5a58
PM
12008 } else {
12009 /*
12010 * Address not in this region. We must check whether the
12011 * region covers addresses in the same page as our address.
12012 * In that case we must not report a size that covers the
12013 * whole page for a subsequent hit against a different MPU
12014 * region or the background region, because it would result
12015 * in incorrect TLB hits for subsequent accesses to
12016 * addresses that are in this MPU region.
12017 */
12018 if (limit >= base &&
12019 ranges_overlap(base, limit - base + 1,
12020 addr_page_base,
12021 TARGET_PAGE_SIZE)) {
12022 sattrs->subpage = true;
12023 }
35337cc3
PM
12024 }
12025 }
12026 }
7e3f1223
TR
12027 break;
12028 }
35337cc3 12029
7e3f1223
TR
12030 /*
12031 * The IDAU will override the SAU lookup results if it specifies
12032 * higher security than the SAU does.
12033 */
12034 if (!idau_ns) {
12035 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
12036 sattrs->ns = false;
12037 sattrs->nsc = idau_nsc;
181962fd 12038 }
35337cc3
PM
12039 }
12040}
12041
787a7e76 12042bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
54317c0f
PM
12043 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12044 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
12045 int *prot, bool *is_subpage,
12046 ARMMMUFaultInfo *fi, uint32_t *mregion)
54317c0f
PM
12047{
12048 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
12049 * that a full phys-to-virt translation does).
12050 * mregion is (if not NULL) set to the region number which matched,
12051 * or -1 if no region number is returned (MPU off, address did not
12052 * hit a region, address hit in multiple regions).
72042435
PM
12053 * We set is_subpage to true if the region hit doesn't cover the
12054 * entire TARGET_PAGE the address is within.
54317c0f 12055 */
2fc0cc0e 12056 ARMCPU *cpu = env_archcpu(env);
504e3cc3 12057 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 12058 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
12059 int n;
12060 int matchregion = -1;
12061 bool hit = false;
72042435
PM
12062 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
12063 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
504e3cc3 12064
72042435 12065 *is_subpage = false;
504e3cc3
PM
12066 *phys_ptr = address;
12067 *prot = 0;
54317c0f
PM
12068 if (mregion) {
12069 *mregion = -1;
35337cc3
PM
12070 }
12071
504e3cc3
PM
12072 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
12073 * was an exception vector read from the vector table (which is always
12074 * done using the default system address map), because those accesses
12075 * are done in arm_v7m_load_vector(), which always does a direct
12076 * read using address_space_ldl(), rather than going via this function.
12077 */
12078 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
12079 hit = true;
12080 } else if (m_is_ppb_region(env, address)) {
12081 hit = true;
504e3cc3 12082 } else {
cff21316
PM
12083 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
12084 hit = true;
12085 }
12086
504e3cc3
PM
12087 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
12088 /* region search */
12089 /* Note that the base address is bits [31:5] from the register
12090 * with bits [4:0] all zeroes, but the limit address is bits
12091 * [31:5] from the register with bits [4:0] all ones.
12092 */
62c58ee0
PM
12093 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
12094 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 12095
62c58ee0 12096 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
12097 /* Region disabled */
12098 continue;
12099 }
12100
12101 if (address < base || address > limit) {
9d2b5a58
PM
12102 /*
12103 * Address not in this region. We must check whether the
12104 * region covers addresses in the same page as our address.
12105 * In that case we must not report a size that covers the
12106 * whole page for a subsequent hit against a different MPU
12107 * region or the background region, because it would result in
12108 * incorrect TLB hits for subsequent accesses to addresses that
12109 * are in this MPU region.
12110 */
12111 if (limit >= base &&
12112 ranges_overlap(base, limit - base + 1,
12113 addr_page_base,
12114 TARGET_PAGE_SIZE)) {
12115 *is_subpage = true;
12116 }
504e3cc3
PM
12117 continue;
12118 }
12119
72042435
PM
12120 if (base > addr_page_base || limit < addr_page_limit) {
12121 *is_subpage = true;
12122 }
12123
cff21316 12124 if (matchregion != -1) {
504e3cc3
PM
12125 /* Multiple regions match -- always a failure (unlike
12126 * PMSAv7 where highest-numbered-region wins)
12127 */
3f551b5b
PM
12128 fi->type = ARMFault_Permission;
12129 fi->level = 1;
504e3cc3
PM
12130 return true;
12131 }
12132
12133 matchregion = n;
12134 hit = true;
504e3cc3
PM
12135 }
12136 }
12137
12138 if (!hit) {
12139 /* background fault */
3f551b5b 12140 fi->type = ARMFault_Background;
504e3cc3
PM
12141 return true;
12142 }
12143
12144 if (matchregion == -1) {
12145 /* hit using the background region */
12146 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
12147 } else {
62c58ee0
PM
12148 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
12149 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
cad8e2e3
PM
12150 bool pxn = false;
12151
12152 if (arm_feature(env, ARM_FEATURE_V8_1M)) {
12153 pxn = extract32(env->pmsav8.rlar[secure][matchregion], 4, 1);
12154 }
504e3cc3
PM
12155
12156 if (m_is_system_region(env, address)) {
12157 /* System space is always execute never */
12158 xn = 1;
12159 }
12160
12161 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
cad8e2e3 12162 if (*prot && !xn && !(pxn && !is_user)) {
504e3cc3
PM
12163 *prot |= PAGE_EXEC;
12164 }
12165 /* We don't need to look the attribute up in the MAIR0/MAIR1
12166 * registers because that only tells us about cacheability.
12167 */
54317c0f
PM
12168 if (mregion) {
12169 *mregion = matchregion;
12170 }
504e3cc3
PM
12171 }
12172
3f551b5b
PM
12173 fi->type = ARMFault_Permission;
12174 fi->level = 1;
504e3cc3
PM
12175 return !(*prot & (1 << access_type));
12176}
12177
54317c0f
PM
12178
12179static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
12180 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12181 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
12182 int *prot, target_ulong *page_size,
12183 ARMMMUFaultInfo *fi)
54317c0f
PM
12184{
12185 uint32_t secure = regime_is_secure(env, mmu_idx);
12186 V8M_SAttributes sattrs = {};
72042435
PM
12187 bool ret;
12188 bool mpu_is_subpage;
54317c0f
PM
12189
12190 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
12191 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
12192 if (access_type == MMU_INST_FETCH) {
12193 /* Instruction fetches always use the MMU bank and the
12194 * transaction attribute determined by the fetch address,
12195 * regardless of CPU state. This is painful for QEMU
12196 * to handle, because it would mean we need to encode
12197 * into the mmu_idx not just the (user, negpri) information
12198 * for the current security state but also that for the
12199 * other security state, which would balloon the number
12200 * of mmu_idx values needed alarmingly.
12201 * Fortunately we can avoid this because it's not actually
12202 * possible to arbitrarily execute code from memory with
12203 * the wrong security attribute: it will always generate
12204 * an exception of some kind or another, apart from the
12205 * special case of an NS CPU executing an SG instruction
12206 * in S&NSC memory. So we always just fail the translation
12207 * here and sort things out in the exception handler
12208 * (including possibly emulating an SG instruction).
12209 */
12210 if (sattrs.ns != !secure) {
3f551b5b
PM
12211 if (sattrs.nsc) {
12212 fi->type = ARMFault_QEMU_NSCExec;
12213 } else {
12214 fi->type = ARMFault_QEMU_SFault;
12215 }
72042435 12216 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
12217 *phys_ptr = address;
12218 *prot = 0;
12219 return true;
12220 }
12221 } else {
12222 /* For data accesses we always use the MMU bank indicated
12223 * by the current CPU state, but the security attributes
12224 * might downgrade a secure access to nonsecure.
12225 */
12226 if (sattrs.ns) {
12227 txattrs->secure = false;
12228 } else if (!secure) {
12229 /* NS access to S memory must fault.
12230 * Architecturally we should first check whether the
12231 * MPU information for this address indicates that we
12232 * are doing an unaligned access to Device memory, which
12233 * should generate a UsageFault instead. QEMU does not
12234 * currently check for that kind of unaligned access though.
12235 * If we added it we would need to do so as a special case
12236 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
12237 */
3f551b5b 12238 fi->type = ARMFault_QEMU_SFault;
72042435 12239 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
12240 *phys_ptr = address;
12241 *prot = 0;
12242 return true;
12243 }
12244 }
12245 }
12246
72042435
PM
12247 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
12248 txattrs, prot, &mpu_is_subpage, fi, NULL);
72042435
PM
12249 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
12250 return ret;
54317c0f
PM
12251}
12252
13689d43 12253static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 12254 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53a4e5c5
PM
12255 hwaddr *phys_ptr, int *prot,
12256 ARMMMUFaultInfo *fi)
9ee6e8bb
PB
12257{
12258 int n;
12259 uint32_t mask;
12260 uint32_t base;
0480f69a 12261 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 12262
3279adb9
PM
12263 if (regime_translation_disabled(env, mmu_idx)) {
12264 /* MPU disabled. */
12265 *phys_ptr = address;
12266 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
12267 return false;
12268 }
12269
9ee6e8bb
PB
12270 *phys_ptr = address;
12271 for (n = 7; n >= 0; n--) {
554b0b09 12272 base = env->cp15.c6_region[n];
87c3d486 12273 if ((base & 1) == 0) {
554b0b09 12274 continue;
87c3d486 12275 }
554b0b09
PM
12276 mask = 1 << ((base >> 1) & 0x1f);
12277 /* Keep this shift separate from the above to avoid an
12278 (undefined) << 32. */
12279 mask = (mask << 1) - 1;
87c3d486 12280 if (((base ^ address) & ~mask) == 0) {
554b0b09 12281 break;
87c3d486 12282 }
9ee6e8bb 12283 }
87c3d486 12284 if (n < 0) {
53a4e5c5 12285 fi->type = ARMFault_Background;
b7cc4e82 12286 return true;
87c3d486 12287 }
9ee6e8bb 12288
03ae85f8 12289 if (access_type == MMU_INST_FETCH) {
7e09797c 12290 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 12291 } else {
7e09797c 12292 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
12293 }
12294 mask = (mask >> (n * 4)) & 0xf;
12295 switch (mask) {
12296 case 0:
53a4e5c5
PM
12297 fi->type = ARMFault_Permission;
12298 fi->level = 1;
b7cc4e82 12299 return true;
9ee6e8bb 12300 case 1:
87c3d486 12301 if (is_user) {
53a4e5c5
PM
12302 fi->type = ARMFault_Permission;
12303 fi->level = 1;
b7cc4e82 12304 return true;
87c3d486 12305 }
554b0b09
PM
12306 *prot = PAGE_READ | PAGE_WRITE;
12307 break;
9ee6e8bb 12308 case 2:
554b0b09 12309 *prot = PAGE_READ;
87c3d486 12310 if (!is_user) {
554b0b09 12311 *prot |= PAGE_WRITE;
87c3d486 12312 }
554b0b09 12313 break;
9ee6e8bb 12314 case 3:
554b0b09
PM
12315 *prot = PAGE_READ | PAGE_WRITE;
12316 break;
9ee6e8bb 12317 case 5:
87c3d486 12318 if (is_user) {
53a4e5c5
PM
12319 fi->type = ARMFault_Permission;
12320 fi->level = 1;
b7cc4e82 12321 return true;
87c3d486 12322 }
554b0b09
PM
12323 *prot = PAGE_READ;
12324 break;
9ee6e8bb 12325 case 6:
554b0b09
PM
12326 *prot = PAGE_READ;
12327 break;
9ee6e8bb 12328 default:
554b0b09 12329 /* Bad permission. */
53a4e5c5
PM
12330 fi->type = ARMFault_Permission;
12331 fi->level = 1;
b7cc4e82 12332 return true;
9ee6e8bb 12333 }
3ad493fc 12334 *prot |= PAGE_EXEC;
b7cc4e82 12335 return false;
9ee6e8bb
PB
12336}
12337
5b2d261d
AB
12338/* Combine either inner or outer cacheability attributes for normal
12339 * memory, according to table D4-42 and pseudocode procedure
12340 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
12341 *
12342 * NB: only stage 1 includes allocation hints (RW bits), leading to
12343 * some asymmetry.
12344 */
12345static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
12346{
12347 if (s1 == 4 || s2 == 4) {
12348 /* non-cacheable has precedence */
12349 return 4;
12350 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
12351 /* stage 1 write-through takes precedence */
12352 return s1;
12353 } else if (extract32(s2, 2, 2) == 2) {
12354 /* stage 2 write-through takes precedence, but the allocation hint
12355 * is still taken from stage 1
12356 */
12357 return (2 << 2) | extract32(s1, 0, 2);
12358 } else { /* write-back */
12359 return s1;
12360 }
12361}
12362
12363/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
12364 * and CombineS1S2Desc()
12365 *
12366 * @s1: Attributes from stage 1 walk
12367 * @s2: Attributes from stage 2 walk
12368 */
12369static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
12370{
337a03f0 12371 uint8_t s1lo, s2lo, s1hi, s2hi;
5b2d261d 12372 ARMCacheAttrs ret;
337a03f0
RH
12373 bool tagged = false;
12374
12375 if (s1.attrs == 0xf0) {
12376 tagged = true;
12377 s1.attrs = 0xff;
12378 }
12379
12380 s1lo = extract32(s1.attrs, 0, 4);
12381 s2lo = extract32(s2.attrs, 0, 4);
12382 s1hi = extract32(s1.attrs, 4, 4);
12383 s2hi = extract32(s2.attrs, 4, 4);
5b2d261d
AB
12384
12385 /* Combine shareability attributes (table D4-43) */
12386 if (s1.shareability == 2 || s2.shareability == 2) {
12387 /* if either are outer-shareable, the result is outer-shareable */
12388 ret.shareability = 2;
12389 } else if (s1.shareability == 3 || s2.shareability == 3) {
12390 /* if either are inner-shareable, the result is inner-shareable */
12391 ret.shareability = 3;
12392 } else {
12393 /* both non-shareable */
12394 ret.shareability = 0;
12395 }
12396
12397 /* Combine memory type and cacheability attributes */
12398 if (s1hi == 0 || s2hi == 0) {
12399 /* Device has precedence over normal */
12400 if (s1lo == 0 || s2lo == 0) {
12401 /* nGnRnE has precedence over anything */
12402 ret.attrs = 0;
12403 } else if (s1lo == 4 || s2lo == 4) {
12404 /* non-Reordering has precedence over Reordering */
12405 ret.attrs = 4; /* nGnRE */
12406 } else if (s1lo == 8 || s2lo == 8) {
12407 /* non-Gathering has precedence over Gathering */
12408 ret.attrs = 8; /* nGRE */
12409 } else {
12410 ret.attrs = 0xc; /* GRE */
12411 }
12412
12413 /* Any location for which the resultant memory type is any
12414 * type of Device memory is always treated as Outer Shareable.
12415 */
12416 ret.shareability = 2;
12417 } else { /* Normal memory */
12418 /* Outer/inner cacheability combine independently */
12419 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
12420 | combine_cacheattr_nibble(s1lo, s2lo);
12421
12422 if (ret.attrs == 0x44) {
12423 /* Any location for which the resultant memory type is Normal
12424 * Inner Non-cacheable, Outer Non-cacheable is always treated
12425 * as Outer Shareable.
12426 */
12427 ret.shareability = 2;
12428 }
12429 }
12430
337a03f0
RH
12431 /* TODO: CombineS1S2Desc does not consider transient, only WB, RWA. */
12432 if (tagged && ret.attrs == 0xff) {
12433 ret.attrs = 0xf0;
12434 }
12435
5b2d261d
AB
12436 return ret;
12437}
12438
12439
702a9357
PM
12440/* get_phys_addr - get the physical address for this virtual address
12441 *
12442 * Find the physical address corresponding to the given virtual address,
12443 * by doing a translation table walk on MMU based systems or using the
12444 * MPU state on MPU based systems.
12445 *
b7cc4e82
PC
12446 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
12447 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
12448 * information on why the translation aborted, in the format of a
12449 * DFSR/IFSR fault register, with the following caveats:
12450 * * we honour the short vs long DFSR format differences.
12451 * * the WnR bit is never set (the caller must do this).
f6bda88f 12452 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
12453 * value.
12454 *
12455 * @env: CPUARMState
12456 * @address: virtual address to get physical address for
12457 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 12458 * @mmu_idx: MMU index indicating required translation regime
702a9357 12459 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 12460 * @attrs: set to the memory transaction attributes to use
702a9357
PM
12461 * @prot: set to the permissions for the page containing phys_ptr
12462 * @page_size: set to the size of the page containing phys_ptr
5b2d261d
AB
12463 * @fi: set to fault info if the translation fails
12464 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
702a9357 12465 */
ebae861f
PMD
12466bool get_phys_addr(CPUARMState *env, target_ulong address,
12467 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12468 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
12469 target_ulong *page_size,
12470 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9ee6e8bb 12471{
7879460a
RDC
12472 ARMMMUIdx s1_mmu_idx = stage_1_mmu_idx(mmu_idx);
12473
12474 if (mmu_idx != s1_mmu_idx) {
9b539263 12475 /* Call ourselves recursively to do the stage 1 and then stage 2
7879460a 12476 * translations if mmu_idx is a two-stage regime.
0480f69a 12477 */
9b539263
EI
12478 if (arm_feature(env, ARM_FEATURE_EL2)) {
12479 hwaddr ipa;
12480 int s2_prot;
12481 int ret;
6c05a866 12482 bool ipa_secure;
5b2d261d 12483 ARMCacheAttrs cacheattrs2 = {};
b1a10c86
RDC
12484 ARMMMUIdx s2_mmu_idx;
12485 bool is_el0;
9b539263 12486
7879460a
RDC
12487 ret = get_phys_addr(env, address, access_type, s1_mmu_idx, &ipa,
12488 attrs, prot, page_size, fi, cacheattrs);
9b539263
EI
12489
12490 /* If S1 fails or S2 is disabled, return early. */
97fa9350 12491 if (ret || regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
9b539263
EI
12492 *phys_ptr = ipa;
12493 return ret;
12494 }
12495
6c05a866 12496 ipa_secure = attrs->secure;
bcd7a8cf 12497 if (arm_is_secure_below_el3(env)) {
6c05a866 12498 if (ipa_secure) {
bcd7a8cf
IH
12499 attrs->secure = !(env->cp15.vstcr_el2.raw_tcr & VSTCR_SW);
12500 } else {
12501 attrs->secure = !(env->cp15.vtcr_el2.raw_tcr & VTCR_NSW);
12502 }
12503 } else {
6c05a866 12504 assert(!ipa_secure);
bcd7a8cf
IH
12505 }
12506
b1a10c86
RDC
12507 s2_mmu_idx = attrs->secure ? ARMMMUIdx_Stage2_S : ARMMMUIdx_Stage2;
12508 is_el0 = mmu_idx == ARMMMUIdx_E10_0 || mmu_idx == ARMMMUIdx_SE10_0;
12509
9b539263 12510 /* S1 is done. Now do S2 translation. */
b1a10c86 12511 ret = get_phys_addr_lpae(env, ipa, access_type, s2_mmu_idx, is_el0,
9b539263 12512 phys_ptr, attrs, &s2_prot,
7e98e21c 12513 page_size, fi, &cacheattrs2);
9b539263
EI
12514 fi->s2addr = ipa;
12515 /* Combine the S1 and S2 perms. */
12516 *prot &= s2_prot;
5b2d261d 12517
7e98e21c
RH
12518 /* If S2 fails, return early. */
12519 if (ret) {
12520 return ret;
5b2d261d
AB
12521 }
12522
7e98e21c 12523 /* Combine the S1 and S2 cache attributes. */
e04a5752 12524 if (arm_hcr_el2_eff(env) & HCR_DC) {
7e98e21c
RH
12525 /*
12526 * HCR.DC forces the first stage attributes to
12527 * Normal Non-Shareable,
12528 * Inner Write-Back Read-Allocate Write-Allocate,
12529 * Outer Write-Back Read-Allocate Write-Allocate.
337a03f0 12530 * Do not overwrite Tagged within attrs.
7e98e21c 12531 */
337a03f0
RH
12532 if (cacheattrs->attrs != 0xf0) {
12533 cacheattrs->attrs = 0xff;
12534 }
7e98e21c
RH
12535 cacheattrs->shareability = 0;
12536 }
12537 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
b1a10c86
RDC
12538
12539 /* Check if IPA translates to secure or non-secure PA space. */
12540 if (arm_is_secure_below_el3(env)) {
6c05a866 12541 if (ipa_secure) {
b1a10c86
RDC
12542 attrs->secure =
12543 !(env->cp15.vstcr_el2.raw_tcr & (VSTCR_SA | VSTCR_SW));
12544 } else {
12545 attrs->secure =
12546 !((env->cp15.vtcr_el2.raw_tcr & (VTCR_NSA | VTCR_NSW))
d3b2d191 12547 || (env->cp15.vstcr_el2.raw_tcr & (VSTCR_SA | VSTCR_SW)));
b1a10c86
RDC
12548 }
12549 }
7e98e21c 12550 return 0;
9b539263
EI
12551 } else {
12552 /*
12553 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
12554 */
8bd5c820 12555 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 12556 }
0480f69a 12557 }
d3649702 12558
8bf5b6a9
PM
12559 /* The page table entries may downgrade secure to non-secure, but
12560 * cannot upgrade an non-secure translation regime's attributes
12561 * to secure.
12562 */
12563 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 12564 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 12565
0480f69a
PM
12566 /* Fast Context Switch Extension. This doesn't exist at all in v8.
12567 * In v7 and earlier it affects all stage 1 translations.
12568 */
97fa9350 12569 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2
0480f69a
PM
12570 && !arm_feature(env, ARM_FEATURE_V8)) {
12571 if (regime_el(env, mmu_idx) == 3) {
12572 address += env->cp15.fcseidr_s;
12573 } else {
12574 address += env->cp15.fcseidr_ns;
12575 }
54bf36ed 12576 }
9ee6e8bb 12577
3279adb9 12578 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 12579 bool ret;
f6bda88f 12580 *page_size = TARGET_PAGE_SIZE;
3279adb9 12581
504e3cc3
PM
12582 if (arm_feature(env, ARM_FEATURE_V8)) {
12583 /* PMSAv8 */
12584 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
72042435 12585 phys_ptr, attrs, prot, page_size, fi);
504e3cc3 12586 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
12587 /* PMSAv7 */
12588 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
e5e40999 12589 phys_ptr, prot, page_size, fi);
3279adb9
PM
12590 } else {
12591 /* Pre-v7 MPU */
12592 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
53a4e5c5 12593 phys_ptr, prot, fi);
3279adb9
PM
12594 }
12595 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 12596 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
12597 access_type == MMU_DATA_LOAD ? "reading" :
12598 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
12599 (uint32_t)address, mmu_idx,
12600 ret ? "Miss" : "Hit",
12601 *prot & PAGE_READ ? 'r' : '-',
12602 *prot & PAGE_WRITE ? 'w' : '-',
12603 *prot & PAGE_EXEC ? 'x' : '-');
12604
12605 return ret;
f6bda88f
PC
12606 }
12607
3279adb9
PM
12608 /* Definitely a real MMU, not an MPU */
12609
0480f69a 12610 if (regime_translation_disabled(env, mmu_idx)) {
337a03f0
RH
12611 uint64_t hcr;
12612 uint8_t memattr;
12613
cebfb648
RH
12614 /*
12615 * MMU disabled. S1 addresses within aa64 translation regimes are
12616 * still checked for bounds -- see AArch64.TranslateAddressS1Off.
12617 */
b1a10c86 12618 if (mmu_idx != ARMMMUIdx_Stage2 && mmu_idx != ARMMMUIdx_Stage2_S) {
cebfb648
RH
12619 int r_el = regime_el(env, mmu_idx);
12620 if (arm_el_is_aa64(env, r_el)) {
12621 int pamax = arm_pamax(env_archcpu(env));
12622 uint64_t tcr = env->cp15.tcr_el[r_el].raw_tcr;
12623 int addrtop, tbi;
12624
12625 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
12626 if (access_type == MMU_INST_FETCH) {
12627 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
12628 }
12629 tbi = (tbi >> extract64(address, 55, 1)) & 1;
12630 addrtop = (tbi ? 55 : 63);
12631
12632 if (extract64(address, pamax, addrtop - pamax + 1) != 0) {
12633 fi->type = ARMFault_AddressSize;
12634 fi->level = 0;
12635 fi->stage2 = false;
12636 return 1;
12637 }
12638
12639 /*
12640 * When TBI is disabled, we've just validated that all of the
12641 * bits above PAMax are zero, so logically we only need to
12642 * clear the top byte for TBI. But it's clearer to follow
12643 * the pseudocode set of addrdesc.paddress.
12644 */
12645 address = extract64(address, 0, 52);
12646 }
12647 }
9ee6e8bb 12648 *phys_ptr = address;
3ad493fc 12649 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 12650 *page_size = TARGET_PAGE_SIZE;
337a03f0
RH
12651
12652 /* Fill in cacheattr a-la AArch64.TranslateAddressS1Off. */
12653 hcr = arm_hcr_el2_eff(env);
12654 cacheattrs->shareability = 0;
12655 if (hcr & HCR_DC) {
12656 if (hcr & HCR_DCT) {
12657 memattr = 0xf0; /* Tagged, Normal, WB, RWA */
12658 } else {
12659 memattr = 0xff; /* Normal, WB, RWA */
12660 }
12661 } else if (access_type == MMU_INST_FETCH) {
12662 if (regime_sctlr(env, mmu_idx) & SCTLR_I) {
12663 memattr = 0xee; /* Normal, WT, RA, NT */
12664 } else {
12665 memattr = 0x44; /* Normal, NC, No */
12666 }
12667 cacheattrs->shareability = 2; /* outer sharable */
12668 } else {
12669 memattr = 0x00; /* Device, nGnRnE */
12670 }
12671 cacheattrs->attrs = memattr;
9ee6e8bb 12672 return 0;
0480f69a
PM
12673 }
12674
0480f69a 12675 if (regime_using_lpae_format(env, mmu_idx)) {
ff7de2fc 12676 return get_phys_addr_lpae(env, address, access_type, mmu_idx, false,
bc52bfeb
PM
12677 phys_ptr, attrs, prot, page_size,
12678 fi, cacheattrs);
0480f69a 12679 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
bc52bfeb
PM
12680 return get_phys_addr_v6(env, address, access_type, mmu_idx,
12681 phys_ptr, attrs, prot, page_size, fi);
9ee6e8bb 12682 } else {
bc52bfeb 12683 return get_phys_addr_v5(env, address, access_type, mmu_idx,
f989983e 12684 phys_ptr, prot, page_size, fi);
9ee6e8bb
PB
12685 }
12686}
12687
0faea0c7
PM
12688hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
12689 MemTxAttrs *attrs)
b5ff1b31 12690{
00b941e5 12691 ARMCPU *cpu = ARM_CPU(cs);
d3649702 12692 CPUARMState *env = &cpu->env;
a8170e5e 12693 hwaddr phys_addr;
d4c430a8 12694 target_ulong page_size;
b5ff1b31 12695 int prot;
b7cc4e82 12696 bool ret;
e14b5a23 12697 ARMMMUFaultInfo fi = {};
50494a27 12698 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
7e98e21c 12699 ARMCacheAttrs cacheattrs = {};
b5ff1b31 12700
0faea0c7
PM
12701 *attrs = (MemTxAttrs) {};
12702
a9dd161f 12703 ret = get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &phys_addr,
7e98e21c 12704 attrs, &prot, &page_size, &fi, &cacheattrs);
b5ff1b31 12705
b7cc4e82 12706 if (ret) {
b5ff1b31 12707 return -1;
00b941e5 12708 }
b5ff1b31
FB
12709 return phys_addr;
12710}
12711
b5ff1b31 12712#endif
6ddbc6e4
PB
12713
12714/* Note that signed overflow is undefined in C. The following routines are
12715 careful to use unsigned types where modulo arithmetic is required.
12716 Failure to do so _will_ break on newer gcc. */
12717
12718/* Signed saturating arithmetic. */
12719
1654b2d6 12720/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
12721static inline uint16_t add16_sat(uint16_t a, uint16_t b)
12722{
12723 uint16_t res;
12724
12725 res = a + b;
12726 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
12727 if (a & 0x8000)
12728 res = 0x8000;
12729 else
12730 res = 0x7fff;
12731 }
12732 return res;
12733}
12734
1654b2d6 12735/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
12736static inline uint8_t add8_sat(uint8_t a, uint8_t b)
12737{
12738 uint8_t res;
12739
12740 res = a + b;
12741 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
12742 if (a & 0x80)
12743 res = 0x80;
12744 else
12745 res = 0x7f;
12746 }
12747 return res;
12748}
12749
1654b2d6 12750/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
12751static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
12752{
12753 uint16_t res;
12754
12755 res = a - b;
12756 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
12757 if (a & 0x8000)
12758 res = 0x8000;
12759 else
12760 res = 0x7fff;
12761 }
12762 return res;
12763}
12764
1654b2d6 12765/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
12766static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
12767{
12768 uint8_t res;
12769
12770 res = a - b;
12771 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
12772 if (a & 0x80)
12773 res = 0x80;
12774 else
12775 res = 0x7f;
12776 }
12777 return res;
12778}
12779
12780#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
12781#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
12782#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
12783#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
12784#define PFX q
12785
12786#include "op_addsub.h"
12787
12788/* Unsigned saturating arithmetic. */
460a09c1 12789static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
12790{
12791 uint16_t res;
12792 res = a + b;
12793 if (res < a)
12794 res = 0xffff;
12795 return res;
12796}
12797
460a09c1 12798static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 12799{
4c4fd3f8 12800 if (a > b)
6ddbc6e4
PB
12801 return a - b;
12802 else
12803 return 0;
12804}
12805
12806static inline uint8_t add8_usat(uint8_t a, uint8_t b)
12807{
12808 uint8_t res;
12809 res = a + b;
12810 if (res < a)
12811 res = 0xff;
12812 return res;
12813}
12814
12815static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
12816{
4c4fd3f8 12817 if (a > b)
6ddbc6e4
PB
12818 return a - b;
12819 else
12820 return 0;
12821}
12822
12823#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
12824#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
12825#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
12826#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
12827#define PFX uq
12828
12829#include "op_addsub.h"
12830
12831/* Signed modulo arithmetic. */
12832#define SARITH16(a, b, n, op) do { \
12833 int32_t sum; \
db6e2e65 12834 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
12835 RESULT(sum, n, 16); \
12836 if (sum >= 0) \
12837 ge |= 3 << (n * 2); \
12838 } while(0)
12839
12840#define SARITH8(a, b, n, op) do { \
12841 int32_t sum; \
db6e2e65 12842 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
12843 RESULT(sum, n, 8); \
12844 if (sum >= 0) \
12845 ge |= 1 << n; \
12846 } while(0)
12847
12848
12849#define ADD16(a, b, n) SARITH16(a, b, n, +)
12850#define SUB16(a, b, n) SARITH16(a, b, n, -)
12851#define ADD8(a, b, n) SARITH8(a, b, n, +)
12852#define SUB8(a, b, n) SARITH8(a, b, n, -)
12853#define PFX s
12854#define ARITH_GE
12855
12856#include "op_addsub.h"
12857
12858/* Unsigned modulo arithmetic. */
12859#define ADD16(a, b, n) do { \
12860 uint32_t sum; \
12861 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
12862 RESULT(sum, n, 16); \
a87aa10b 12863 if ((sum >> 16) == 1) \
6ddbc6e4
PB
12864 ge |= 3 << (n * 2); \
12865 } while(0)
12866
12867#define ADD8(a, b, n) do { \
12868 uint32_t sum; \
12869 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
12870 RESULT(sum, n, 8); \
a87aa10b
AZ
12871 if ((sum >> 8) == 1) \
12872 ge |= 1 << n; \
6ddbc6e4
PB
12873 } while(0)
12874
12875#define SUB16(a, b, n) do { \
12876 uint32_t sum; \
12877 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
12878 RESULT(sum, n, 16); \
12879 if ((sum >> 16) == 0) \
12880 ge |= 3 << (n * 2); \
12881 } while(0)
12882
12883#define SUB8(a, b, n) do { \
12884 uint32_t sum; \
12885 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
12886 RESULT(sum, n, 8); \
12887 if ((sum >> 8) == 0) \
a87aa10b 12888 ge |= 1 << n; \
6ddbc6e4
PB
12889 } while(0)
12890
12891#define PFX u
12892#define ARITH_GE
12893
12894#include "op_addsub.h"
12895
12896/* Halved signed arithmetic. */
12897#define ADD16(a, b, n) \
12898 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
12899#define SUB16(a, b, n) \
12900 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
12901#define ADD8(a, b, n) \
12902 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
12903#define SUB8(a, b, n) \
12904 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
12905#define PFX sh
12906
12907#include "op_addsub.h"
12908
12909/* Halved unsigned arithmetic. */
12910#define ADD16(a, b, n) \
12911 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12912#define SUB16(a, b, n) \
12913 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12914#define ADD8(a, b, n) \
12915 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12916#define SUB8(a, b, n) \
12917 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12918#define PFX uh
12919
12920#include "op_addsub.h"
12921
12922static inline uint8_t do_usad(uint8_t a, uint8_t b)
12923{
12924 if (a > b)
12925 return a - b;
12926 else
12927 return b - a;
12928}
12929
12930/* Unsigned sum of absolute byte differences. */
12931uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
12932{
12933 uint32_t sum;
12934 sum = do_usad(a, b);
12935 sum += do_usad(a >> 8, b >> 8);
bdc3b6f5 12936 sum += do_usad(a >> 16, b >> 16);
6ddbc6e4
PB
12937 sum += do_usad(a >> 24, b >> 24);
12938 return sum;
12939}
12940
12941/* For ARMv6 SEL instruction. */
12942uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
12943{
12944 uint32_t mask;
12945
12946 mask = 0;
12947 if (flags & 1)
12948 mask |= 0xff;
12949 if (flags & 2)
12950 mask |= 0xff00;
12951 if (flags & 4)
12952 mask |= 0xff0000;
12953 if (flags & 8)
12954 mask |= 0xff000000;
12955 return (a & mask) | (b & ~mask);
12956}
12957
aa633469
PM
12958/* CRC helpers.
12959 * The upper bytes of val (above the number specified by 'bytes') must have
12960 * been zeroed out by the caller.
12961 */
eb0ecd5a
WN
12962uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12963{
12964 uint8_t buf[4];
12965
aa633469 12966 stl_le_p(buf, val);
eb0ecd5a
WN
12967
12968 /* zlib crc32 converts the accumulator and output to one's complement. */
12969 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12970}
12971
12972uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12973{
12974 uint8_t buf[4];
12975
aa633469 12976 stl_le_p(buf, val);
eb0ecd5a
WN
12977
12978 /* Linux crc32c converts the output to one's complement. */
12979 return crc32c(acc, buf, bytes) ^ 0xffffffff;
12980}
a9e01311
RH
12981
12982/* Return the exception level to which FP-disabled exceptions should
12983 * be taken, or 0 if FP is enabled.
12984 */
ced31551 12985int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 12986{
55faa212 12987#ifndef CONFIG_USER_ONLY
d5a6fa2d
RH
12988 uint64_t hcr_el2;
12989
a9e01311
RH
12990 /* CPACR and the CPTR registers don't exist before v6, so FP is
12991 * always accessible
12992 */
12993 if (!arm_feature(env, ARM_FEATURE_V6)) {
12994 return 0;
12995 }
12996
d87513c0
PM
12997 if (arm_feature(env, ARM_FEATURE_M)) {
12998 /* CPACR can cause a NOCP UsageFault taken to current security state */
12999 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
13000 return 1;
13001 }
13002
13003 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
13004 if (!extract32(env->v7m.nsacr, 10, 1)) {
13005 /* FP insns cause a NOCP UsageFault taken to Secure */
13006 return 3;
13007 }
13008 }
13009
13010 return 0;
13011 }
13012
d5a6fa2d
RH
13013 hcr_el2 = arm_hcr_el2_eff(env);
13014
a9e01311
RH
13015 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
13016 * 0, 2 : trap EL0 and EL1/PL1 accesses
13017 * 1 : trap only EL0 accesses
13018 * 3 : trap no accesses
c2ddb7cf 13019 * This register is ignored if E2H+TGE are both set.
a9e01311 13020 */
d5a6fa2d 13021 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
c2ddb7cf
RH
13022 int fpen = extract32(env->cp15.cpacr_el1, 20, 2);
13023
13024 switch (fpen) {
13025 case 0:
13026 case 2:
13027 if (cur_el == 0 || cur_el == 1) {
13028 /* Trap to PL1, which might be EL1 or EL3 */
13029 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
13030 return 3;
13031 }
13032 return 1;
13033 }
13034 if (cur_el == 3 && !is_a64(env)) {
13035 /* Secure PL1 running at EL3 */
a9e01311
RH
13036 return 3;
13037 }
c2ddb7cf
RH
13038 break;
13039 case 1:
13040 if (cur_el == 0) {
13041 return 1;
13042 }
13043 break;
13044 case 3:
13045 break;
a9e01311 13046 }
a9e01311
RH
13047 }
13048
fc1120a7
PM
13049 /*
13050 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
13051 * to control non-secure access to the FPU. It doesn't have any
13052 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
13053 */
13054 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
13055 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
13056 if (!extract32(env->cp15.nsacr, 10, 1)) {
13057 /* FP insns act as UNDEF */
13058 return cur_el == 2 ? 2 : 1;
13059 }
13060 }
13061
d5a6fa2d
RH
13062 /*
13063 * CPTR_EL2 is present in v7VE or v8, and changes format
13064 * with HCR_EL2.E2H (regardless of TGE).
a9e01311 13065 */
d5a6fa2d
RH
13066 if (cur_el <= 2) {
13067 if (hcr_el2 & HCR_E2H) {
13068 /* Check CPTR_EL2.FPEN. */
13069 switch (extract32(env->cp15.cptr_el[2], 20, 2)) {
13070 case 1:
13071 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
13072 break;
13073 }
13074 /* fall through */
13075 case 0:
13076 case 2:
13077 return 2;
13078 }
13079 } else if (arm_is_el2_enabled(env)) {
13080 if (env->cp15.cptr_el[2] & CPTR_TFP) {
13081 return 2;
13082 }
13083 }
a9e01311
RH
13084 }
13085
13086 /* CPTR_EL3 : present in v8 */
a7b66ada 13087 if (env->cp15.cptr_el[3] & CPTR_TFP) {
a9e01311
RH
13088 /* Trap all FP ops to EL3 */
13089 return 3;
13090 }
55faa212 13091#endif
a9e01311
RH
13092 return 0;
13093}
13094
b9f6033c
RH
13095/* Return the exception level we're running at if this is our mmu_idx */
13096int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
13097{
13098 if (mmu_idx & ARM_MMU_IDX_M) {
13099 return mmu_idx & ARM_MMU_IDX_M_PRIV;
13100 }
13101
13102 switch (mmu_idx) {
13103 case ARMMMUIdx_E10_0:
13104 case ARMMMUIdx_E20_0:
13105 case ARMMMUIdx_SE10_0:
b6ad6062 13106 case ARMMMUIdx_SE20_0:
b9f6033c
RH
13107 return 0;
13108 case ARMMMUIdx_E10_1:
452ef8cb 13109 case ARMMMUIdx_E10_1_PAN:
b9f6033c 13110 case ARMMMUIdx_SE10_1:
452ef8cb 13111 case ARMMMUIdx_SE10_1_PAN:
b9f6033c
RH
13112 return 1;
13113 case ARMMMUIdx_E2:
13114 case ARMMMUIdx_E20_2:
452ef8cb 13115 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
13116 case ARMMMUIdx_SE2:
13117 case ARMMMUIdx_SE20_2:
13118 case ARMMMUIdx_SE20_2_PAN:
b9f6033c
RH
13119 return 2;
13120 case ARMMMUIdx_SE3:
13121 return 3;
13122 default:
13123 g_assert_not_reached();
13124 }
13125}
13126
7aab5a8c 13127#ifndef CONFIG_TCG
65e4655c
RH
13128ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
13129{
7aab5a8c 13130 g_assert_not_reached();
65e4655c 13131}
7aab5a8c 13132#endif
65e4655c 13133
164690b2 13134ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 13135{
b6ad6062
RDC
13136 ARMMMUIdx idx;
13137 uint64_t hcr;
13138
65e4655c 13139 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 13140 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
13141 }
13142
6003d980 13143 /* See ARM pseudo-function ELIsInHost. */
b9f6033c
RH
13144 switch (el) {
13145 case 0:
b6ad6062
RDC
13146 hcr = arm_hcr_el2_eff(env);
13147 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
13148 idx = ARMMMUIdx_E20_0;
13149 } else {
13150 idx = ARMMMUIdx_E10_0;
6003d980 13151 }
b6ad6062 13152 break;
b9f6033c 13153 case 1:
66412260 13154 if (env->pstate & PSTATE_PAN) {
b6ad6062
RDC
13155 idx = ARMMMUIdx_E10_1_PAN;
13156 } else {
13157 idx = ARMMMUIdx_E10_1;
66412260 13158 }
b6ad6062 13159 break;
b9f6033c 13160 case 2:
6003d980 13161 /* Note that TGE does not apply at EL2. */
b6ad6062 13162 if (arm_hcr_el2_eff(env) & HCR_E2H) {
66412260 13163 if (env->pstate & PSTATE_PAN) {
b6ad6062
RDC
13164 idx = ARMMMUIdx_E20_2_PAN;
13165 } else {
13166 idx = ARMMMUIdx_E20_2;
66412260 13167 }
b6ad6062
RDC
13168 } else {
13169 idx = ARMMMUIdx_E2;
6003d980 13170 }
b6ad6062 13171 break;
b9f6033c
RH
13172 case 3:
13173 return ARMMMUIdx_SE3;
13174 default:
13175 g_assert_not_reached();
65e4655c 13176 }
b6ad6062
RDC
13177
13178 if (arm_is_secure_below_el3(env)) {
13179 idx &= ~ARM_MMU_IDX_A_NS;
13180 }
13181
13182 return idx;
50494a27
RH
13183}
13184
164690b2
RH
13185ARMMMUIdx arm_mmu_idx(CPUARMState *env)
13186{
13187 return arm_mmu_idx_el(env, arm_current_el(env));
13188}
13189
64be86ab
RH
13190#ifndef CONFIG_USER_ONLY
13191ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
13192{
13193 return stage_1_mmu_idx(arm_mmu_idx(env));
13194}
13195#endif
13196
3902bfc6
RH
13197static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
13198 ARMMMUIdx mmu_idx,
13199 CPUARMTBFlags flags)
fdd1b228 13200{
a729a46b
RH
13201 DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
13202 DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
fdd1b228 13203
fdd1b228 13204 if (arm_singlestep_active(env)) {
a729a46b 13205 DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
fdd1b228
RH
13206 }
13207 return flags;
13208}
13209
3902bfc6
RH
13210static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
13211 ARMMMUIdx mmu_idx,
13212 CPUARMTBFlags flags)
43eccfb6 13213{
8061a649
RH
13214 bool sctlr_b = arm_sctlr_b(env);
13215
13216 if (sctlr_b) {
a729a46b 13217 DP_TBFLAG_A32(flags, SCTLR__B, 1);
8061a649
RH
13218 }
13219 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
a729a46b 13220 DP_TBFLAG_ANY(flags, BE_DATA, 1);
8061a649 13221 }
a729a46b 13222 DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
43eccfb6
RH
13223
13224 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
13225}
13226
3902bfc6
RH
13227static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
13228 ARMMMUIdx mmu_idx)
6e33ced5 13229{
3902bfc6 13230 CPUARMTBFlags flags = {};
4479ec30
RH
13231 uint32_t ccr = env->v7m.ccr[env->v7m.secure];
13232
13233 /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
13234 if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
13235 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13236 }
6e33ced5
RH
13237
13238 if (arm_v7m_is_handler_mode(env)) {
a729a46b 13239 DP_TBFLAG_M32(flags, HANDLER, 1);
6e33ced5
RH
13240 }
13241
13242 /*
13243 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
13244 * is suppressing them because the requested execution priority
13245 * is less than 0.
13246 */
13247 if (arm_feature(env, ARM_FEATURE_V8) &&
13248 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
4479ec30 13249 (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
a729a46b 13250 DP_TBFLAG_M32(flags, STACKCHECK, 1);
6e33ced5
RH
13251 }
13252
13253 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
13254}
13255
3902bfc6 13256static CPUARMTBFlags rebuild_hflags_aprofile(CPUARMState *env)
83f4baef 13257{
3902bfc6 13258 CPUARMTBFlags flags = {};
83f4baef 13259
a729a46b 13260 DP_TBFLAG_ANY(flags, DEBUG_TARGET_EL, arm_debug_target_el(env));
83f4baef
RH
13261 return flags;
13262}
13263
3902bfc6
RH
13264static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
13265 ARMMMUIdx mmu_idx)
c747224c 13266{
3902bfc6 13267 CPUARMTBFlags flags = rebuild_hflags_aprofile(env);
4479ec30
RH
13268 int el = arm_current_el(env);
13269
13270 if (arm_sctlr(env, el) & SCTLR_A) {
13271 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13272 }
0a54d68e
RH
13273
13274 if (arm_el_is_aa64(env, 1)) {
a729a46b 13275 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 13276 }
5bb0a20b 13277
4479ec30 13278 if (el < 2 && env->cp15.hstr_el2 &&
5bb0a20b 13279 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
a729a46b 13280 DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
5bb0a20b
MZ
13281 }
13282
520d1621
PM
13283 if (env->uncached_cpsr & CPSR_IL) {
13284 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
13285 }
13286
83f4baef 13287 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
c747224c
RH
13288}
13289
3902bfc6
RH
13290static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
13291 ARMMMUIdx mmu_idx)
a9e01311 13292{
3902bfc6 13293 CPUARMTBFlags flags = rebuild_hflags_aprofile(env);
d4d7503a 13294 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
b830a5ee 13295 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
d4d7503a
RH
13296 uint64_t sctlr;
13297 int tbii, tbid;
b9adaa70 13298
a729a46b 13299 DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
cd208a1c 13300
339370b9 13301 /* Get control bits for tagged addresses. */
b830a5ee
RH
13302 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
13303 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
5d8634f5 13304
a729a46b
RH
13305 DP_TBFLAG_A64(flags, TBII, tbii);
13306 DP_TBFLAG_A64(flags, TBID, tbid);
d4d7503a
RH
13307
13308 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
13309 int sve_el = sve_exception_el(env, el);
13310 uint32_t zcr_len;
5d8634f5 13311
d4d7503a
RH
13312 /*
13313 * If SVE is disabled, but FP is enabled,
13314 * then the effective len is 0.
13315 */
13316 if (sve_el != 0 && fp_el == 0) {
13317 zcr_len = 0;
13318 } else {
13319 zcr_len = sve_zcr_len_for_el(env, el);
5d8634f5 13320 }
a729a46b
RH
13321 DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
13322 DP_TBFLAG_A64(flags, ZCR_LEN, zcr_len);
d4d7503a 13323 }
1db5e96c 13324
aaec1432 13325 sctlr = regime_sctlr(env, stage1);
1db5e96c 13326
4479ec30
RH
13327 if (sctlr & SCTLR_A) {
13328 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13329 }
13330
8061a649 13331 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
a729a46b 13332 DP_TBFLAG_ANY(flags, BE_DATA, 1);
8061a649
RH
13333 }
13334
d4d7503a
RH
13335 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
13336 /*
13337 * In order to save space in flags, we record only whether
13338 * pauth is "inactive", meaning all insns are implemented as
13339 * a nop, or "active" when some action must be performed.
13340 * The decision of which action to take is left to a helper.
13341 */
13342 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
a729a46b 13343 DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
1db5e96c 13344 }
d4d7503a 13345 }
0816ef1b 13346
d4d7503a
RH
13347 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
13348 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
13349 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
a729a46b 13350 DP_TBFLAG_A64(flags, BT, 1);
0816ef1b 13351 }
d4d7503a 13352 }
08f1434a 13353
cc28fc30 13354 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
7a8014ab
RH
13355 if (!(env->pstate & PSTATE_UAO)) {
13356 switch (mmu_idx) {
13357 case ARMMMUIdx_E10_1:
13358 case ARMMMUIdx_E10_1_PAN:
13359 case ARMMMUIdx_SE10_1:
13360 case ARMMMUIdx_SE10_1_PAN:
13361 /* TODO: ARMv8.3-NV */
a729a46b 13362 DP_TBFLAG_A64(flags, UNPRIV, 1);
7a8014ab
RH
13363 break;
13364 case ARMMMUIdx_E20_2:
13365 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
13366 case ARMMMUIdx_SE20_2:
13367 case ARMMMUIdx_SE20_2_PAN:
7a8014ab
RH
13368 /*
13369 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
13370 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
13371 */
13372 if (env->cp15.hcr_el2 & HCR_TGE) {
a729a46b 13373 DP_TBFLAG_A64(flags, UNPRIV, 1);
7a8014ab
RH
13374 }
13375 break;
13376 default:
13377 break;
cc28fc30 13378 }
cc28fc30
RH
13379 }
13380
520d1621
PM
13381 if (env->pstate & PSTATE_IL) {
13382 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
13383 }
13384
81ae05fa
RH
13385 if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
13386 /*
13387 * Set MTE_ACTIVE if any access may be Checked, and leave clear
13388 * if all accesses must be Unchecked:
13389 * 1) If no TBI, then there are no tags in the address to check,
13390 * 2) If Tag Check Override, then all accesses are Unchecked,
13391 * 3) If Tag Check Fail == 0, then Checked access have no effect,
13392 * 4) If no Allocation Tag Access, then all accesses are Unchecked.
13393 */
13394 if (allocation_tag_access_enabled(env, el, sctlr)) {
a729a46b 13395 DP_TBFLAG_A64(flags, ATA, 1);
81ae05fa
RH
13396 if (tbid
13397 && !(env->pstate & PSTATE_TCO)
13398 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
a729a46b 13399 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
81ae05fa
RH
13400 }
13401 }
13402 /* And again for unprivileged accesses, if required. */
a729a46b 13403 if (EX_TBFLAG_A64(flags, UNPRIV)
81ae05fa
RH
13404 && tbid
13405 && !(env->pstate & PSTATE_TCO)
2d928adf 13406 && (sctlr & SCTLR_TCF0)
81ae05fa 13407 && allocation_tag_access_enabled(env, 0, sctlr)) {
a729a46b 13408 DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
81ae05fa
RH
13409 }
13410 /* Cache TCMA as well as TBI. */
a729a46b 13411 DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
81ae05fa
RH
13412 }
13413
d4d7503a
RH
13414 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
13415}
13416
3902bfc6 13417static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
3d74e2e9
RH
13418{
13419 int el = arm_current_el(env);
13420 int fp_el = fp_exception_el(env, el);
164690b2 13421 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3d74e2e9
RH
13422
13423 if (is_a64(env)) {
13424 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
13425 } else if (arm_feature(env, ARM_FEATURE_M)) {
13426 return rebuild_hflags_m32(env, fp_el, mmu_idx);
13427 } else {
13428 return rebuild_hflags_a32(env, fp_el, mmu_idx);
13429 }
13430}
13431
13432void arm_rebuild_hflags(CPUARMState *env)
13433{
13434 env->hflags = rebuild_hflags_internal(env);
13435}
13436
19717e9b
PM
13437/*
13438 * If we have triggered a EL state change we can't rely on the
13439 * translator having passed it to us, we need to recompute.
13440 */
13441void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
13442{
13443 int el = arm_current_el(env);
13444 int fp_el = fp_exception_el(env, el);
13445 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3902bfc6 13446
19717e9b
PM
13447 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
13448}
13449
14f3c588
RH
13450void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
13451{
13452 int fp_el = fp_exception_el(env, el);
13453 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13454
13455 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
13456}
13457
f80741d1
AB
13458/*
13459 * If we have triggered a EL state change we can't rely on the
563152e0 13460 * translator having passed it to us, we need to recompute.
f80741d1
AB
13461 */
13462void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
13463{
13464 int el = arm_current_el(env);
13465 int fp_el = fp_exception_el(env, el);
13466 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13467 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
13468}
13469
14f3c588
RH
13470void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
13471{
13472 int fp_el = fp_exception_el(env, el);
13473 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13474
13475 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
13476}
13477
13478void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
13479{
13480 int fp_el = fp_exception_el(env, el);
13481 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13482
13483 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
13484}
13485
0ee8b24a
PMD
13486static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
13487{
13488#ifdef CONFIG_DEBUG_TCG
3902bfc6
RH
13489 CPUARMTBFlags c = env->hflags;
13490 CPUARMTBFlags r = rebuild_hflags_internal(env);
0ee8b24a 13491
a378206a
RH
13492 if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
13493 fprintf(stderr, "TCG hflags mismatch "
13494 "(current:(0x%08x,0x" TARGET_FMT_lx ")"
13495 " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
13496 c.flags, c.flags2, r.flags, r.flags2);
0ee8b24a
PMD
13497 abort();
13498 }
13499#endif
13500}
13501
26702213
PM
13502static bool mve_no_pred(CPUARMState *env)
13503{
13504 /*
13505 * Return true if there is definitely no predication of MVE
13506 * instructions by VPR or LTPSIZE. (Returning false even if there
13507 * isn't any predication is OK; generated code will just be
13508 * a little worse.)
13509 * If the CPU does not implement MVE then this TB flag is always 0.
13510 *
13511 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
13512 * logic in gen_update_fp_context() needs to be updated to match.
13513 *
13514 * We do not include the effect of the ECI bits here -- they are
13515 * tracked in other TB flags. This simplifies the logic for
13516 * "when did we emit code that changes the MVE_NO_PRED TB flag
13517 * and thus need to end the TB?".
13518 */
13519 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
13520 return false;
13521 }
13522 if (env->v7m.vpr) {
13523 return false;
13524 }
13525 if (env->v7m.ltpsize < 4) {
13526 return false;
13527 }
13528 return true;
13529}
13530
d4d7503a
RH
13531void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
13532 target_ulong *cs_base, uint32_t *pflags)
13533{
3902bfc6 13534 CPUARMTBFlags flags;
d4d7503a 13535
0ee8b24a 13536 assert_hflags_rebuild_correctly(env);
3902bfc6 13537 flags = env->hflags;
3d74e2e9 13538
a729a46b 13539 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
d4d7503a 13540 *pc = env->pc;
d4d7503a 13541 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
a729a46b 13542 DP_TBFLAG_A64(flags, BTYPE, env->btype);
08f1434a 13543 }
a9e01311
RH
13544 } else {
13545 *pc = env->regs[15];
6e33ced5
RH
13546
13547 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
13548 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
13549 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
13550 != env->v7m.secure) {
a729a46b 13551 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
9550d1bd
RH
13552 }
13553
13554 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
13555 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
13556 (env->v7m.secure &&
13557 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
13558 /*
13559 * ASPEN is set, but FPCA/SFPA indicate that there is no
13560 * active FP context; we must create a new FP context before
13561 * executing any FP insn.
13562 */
a729a46b 13563 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
9550d1bd
RH
13564 }
13565
13566 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
13567 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
a729a46b 13568 DP_TBFLAG_M32(flags, LSPACT, 1);
9550d1bd 13569 }
26702213
PM
13570
13571 if (mve_no_pred(env)) {
13572 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
13573 }
6e33ced5 13574 } else {
bbad7c62
RH
13575 /*
13576 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
13577 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
13578 */
13579 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
a729a46b 13580 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
bbad7c62 13581 } else {
a729a46b
RH
13582 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
13583 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
bbad7c62 13584 }
0a54d68e 13585 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
a729a46b 13586 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 13587 }
6e33ced5
RH
13588 }
13589
a729a46b
RH
13590 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
13591 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
d4d7503a 13592 }
a9e01311 13593
60e12c37
RH
13594 /*
13595 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
13596 * states defined in the ARM ARM for software singlestep:
13597 * SS_ACTIVE PSTATE.SS State
13598 * 0 x Inactive (the TB flag for SS is always 0)
13599 * 1 0 Active-pending
13600 * 1 1 Active-not-pending
ae6eb1e9 13601 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
a9e01311 13602 */
a729a46b
RH
13603 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
13604 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
a9e01311 13605 }
a9e01311 13606
3902bfc6 13607 *pflags = flags.flags;
a378206a 13608 *cs_base = flags.flags2;
a9e01311 13609}
0ab5953b
RH
13610
13611#ifdef TARGET_AARCH64
13612/*
13613 * The manual says that when SVE is enabled and VQ is widened the
13614 * implementation is allowed to zero the previously inaccessible
13615 * portion of the registers. The corollary to that is that when
13616 * SVE is enabled and VQ is narrowed we are also allowed to zero
13617 * the now inaccessible portion of the registers.
13618 *
13619 * The intent of this is that no predicate bit beyond VQ is ever set.
13620 * Which means that some operations on predicate registers themselves
13621 * may operate on full uint64_t or even unrolled across the maximum
13622 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
13623 * may well be cheaper than conditionals to restrict the operation
13624 * to the relevant portion of a uint16_t[16].
13625 */
13626void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
13627{
13628 int i, j;
13629 uint64_t pmask;
13630
13631 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 13632 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
13633
13634 /* Zap the high bits of the zregs. */
13635 for (i = 0; i < 32; i++) {
13636 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
13637 }
13638
13639 /* Zap the high bits of the pregs and ffr. */
13640 pmask = 0;
13641 if (vq & 3) {
13642 pmask = ~(-1ULL << (16 * (vq & 3)));
13643 }
13644 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
13645 for (i = 0; i < 17; ++i) {
13646 env->vfp.pregs[i].p[j] &= pmask;
13647 }
13648 pmask = 0;
13649 }
13650}
13651
13652/*
13653 * Notice a change in SVE vector size when changing EL.
13654 */
9a05f7b6
RH
13655void aarch64_sve_change_el(CPUARMState *env, int old_el,
13656 int new_el, bool el0_a64)
0ab5953b 13657{
2fc0cc0e 13658 ARMCPU *cpu = env_archcpu(env);
0ab5953b 13659 int old_len, new_len;
9a05f7b6 13660 bool old_a64, new_a64;
0ab5953b
RH
13661
13662 /* Nothing to do if no SVE. */
cd208a1c 13663 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
13664 return;
13665 }
13666
13667 /* Nothing to do if FP is disabled in either EL. */
13668 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
13669 return;
13670 }
13671
13672 /*
13673 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
13674 * at ELx, or not available because the EL is in AArch32 state, then
13675 * for all purposes other than a direct read, the ZCR_ELx.LEN field
13676 * has an effective value of 0".
13677 *
13678 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
13679 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
13680 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
13681 * we already have the correct register contents when encountering the
13682 * vq0->vq0 transition between EL0->EL1.
13683 */
9a05f7b6
RH
13684 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
13685 old_len = (old_a64 && !sve_exception_el(env, old_el)
0ab5953b 13686 ? sve_zcr_len_for_el(env, old_el) : 0);
9a05f7b6
RH
13687 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
13688 new_len = (new_a64 && !sve_exception_el(env, new_el)
0ab5953b
RH
13689 ? sve_zcr_len_for_el(env, new_el) : 0);
13690
13691 /* When changing vector length, clear inaccessible state. */
13692 if (new_len < old_len) {
13693 aarch64_sve_narrow_vq(env, new_len + 1);
13694 }
13695}
13696#endif