]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/helper.c
Merge tag 'for-upstream' of https://gitlab.com/bonzini/qemu into staging
[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"
2a609df8 30#include "sysemu/tcg.h"
9d2b5a58 31#include "qemu/range.h"
7f7b4e7a 32#include "qapi/qapi-commands-machine-target.h"
de390645
RH
33#include "qapi/error.h"
34#include "qemu/guest-random.h"
91f78c58
PMD
35#ifdef CONFIG_TCG
36#include "arm_ldst.h"
7aab5a8c 37#include "exec/cpu_ldst.h"
6b5fe137 38#include "semihosting/common-semi.h"
91f78c58 39#endif
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;
217 uint64_t regidx;
218 const ARMCPRegInfo *ri;
219
220 regidx = *(uint32_t *)key;
60322b39 221 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 222
7a0e58fa 223 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
224 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
225 /* The value array need not be initialized at this point */
226 cpu->cpreg_array_len++;
227 }
228}
229
230static void count_cpreg(gpointer key, gpointer opaque)
231{
232 ARMCPU *cpu = opaque;
233 uint64_t regidx;
234 const ARMCPRegInfo *ri;
235
236 regidx = *(uint32_t *)key;
60322b39 237 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 238
7a0e58fa 239 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
240 cpu->cpreg_array_len++;
241 }
242}
243
244static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
245{
cbf239b7
AR
246 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
247 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
721fae12 248
cbf239b7
AR
249 if (aidx > bidx) {
250 return 1;
251 }
252 if (aidx < bidx) {
253 return -1;
254 }
255 return 0;
721fae12
PM
256}
257
258void init_cpreg_list(ARMCPU *cpu)
259{
260 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
261 * Note that we require cpreg_tuples[] to be sorted by key ID.
262 */
57b6d95e 263 GList *keys;
721fae12
PM
264 int arraylen;
265
57b6d95e 266 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
267 keys = g_list_sort(keys, cpreg_key_compare);
268
269 cpu->cpreg_array_len = 0;
270
271 g_list_foreach(keys, count_cpreg, cpu);
272
273 arraylen = cpu->cpreg_array_len;
274 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
275 cpu->cpreg_values = g_new(uint64_t, arraylen);
276 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
277 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
278 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
279 cpu->cpreg_array_len = 0;
280
281 g_list_foreach(keys, add_cpreg_to_list, cpu);
282
283 assert(cpu->cpreg_array_len == arraylen);
284
285 g_list_free(keys);
286}
287
68e9c2fe 288/*
93dd1e61 289 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
68e9c2fe
EI
290 */
291static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
292 const ARMCPRegInfo *ri,
293 bool isread)
68e9c2fe 294{
93dd1e61
EI
295 if (!is_a64(env) && arm_current_el(env) == 3 &&
296 arm_is_secure_below_el3(env)) {
68e9c2fe
EI
297 return CP_ACCESS_TRAP_UNCATEGORIZED;
298 }
299 return CP_ACCESS_OK;
300}
301
5513c3ab
PM
302/* Some secure-only AArch32 registers trap to EL3 if used from
303 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
304 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
305 * We assume that the .access field is set to PL1_RW.
306 */
307static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
308 const ARMCPRegInfo *ri,
309 bool isread)
5513c3ab
PM
310{
311 if (arm_current_el(env) == 3) {
312 return CP_ACCESS_OK;
313 }
314 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
315 if (env->cp15.scr_el3 & SCR_EEL2) {
316 return CP_ACCESS_TRAP_EL2;
317 }
5513c3ab
PM
318 return CP_ACCESS_TRAP_EL3;
319 }
320 /* This will be EL1 NS and EL2 NS, which just UNDEF */
321 return CP_ACCESS_TRAP_UNCATEGORIZED;
322}
323
59dd089c
RDC
324static uint64_t arm_mdcr_el2_eff(CPUARMState *env)
325{
326 return arm_is_el2_enabled(env) ? env->cp15.mdcr_el2 : 0;
327}
328
187f678d
PM
329/* Check for traps to "powerdown debug" registers, which are controlled
330 * by MDCR.TDOSA
331 */
332static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
333 bool isread)
334{
335 int el = arm_current_el(env);
59dd089c
RDC
336 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
337 bool mdcr_el2_tdosa = (mdcr_el2 & MDCR_TDOSA) || (mdcr_el2 & MDCR_TDE) ||
7c208e0f 338 (arm_hcr_el2_eff(env) & HCR_TGE);
187f678d 339
59dd089c 340 if (el < 2 && mdcr_el2_tdosa) {
187f678d
PM
341 return CP_ACCESS_TRAP_EL2;
342 }
343 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
344 return CP_ACCESS_TRAP_EL3;
345 }
346 return CP_ACCESS_OK;
347}
348
91b0a238
PM
349/* Check for traps to "debug ROM" registers, which are controlled
350 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
351 */
352static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
353 bool isread)
354{
355 int el = arm_current_el(env);
59dd089c
RDC
356 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
357 bool mdcr_el2_tdra = (mdcr_el2 & MDCR_TDRA) || (mdcr_el2 & MDCR_TDE) ||
7c208e0f 358 (arm_hcr_el2_eff(env) & HCR_TGE);
91b0a238 359
59dd089c 360 if (el < 2 && mdcr_el2_tdra) {
91b0a238
PM
361 return CP_ACCESS_TRAP_EL2;
362 }
363 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
364 return CP_ACCESS_TRAP_EL3;
365 }
366 return CP_ACCESS_OK;
367}
368
d6c8cf81
PM
369/* Check for traps to general debug registers, which are controlled
370 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
371 */
372static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
373 bool isread)
374{
375 int el = arm_current_el(env);
59dd089c
RDC
376 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
377 bool mdcr_el2_tda = (mdcr_el2 & MDCR_TDA) || (mdcr_el2 & MDCR_TDE) ||
7c208e0f 378 (arm_hcr_el2_eff(env) & HCR_TGE);
d6c8cf81 379
59dd089c 380 if (el < 2 && mdcr_el2_tda) {
d6c8cf81
PM
381 return CP_ACCESS_TRAP_EL2;
382 }
383 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
384 return CP_ACCESS_TRAP_EL3;
385 }
386 return CP_ACCESS_OK;
387}
388
1fce1ba9
PM
389/* Check for traps to performance monitor registers, which are controlled
390 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
391 */
392static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
393 bool isread)
394{
395 int el = arm_current_el(env);
59dd089c 396 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1fce1ba9 397
59dd089c 398 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1fce1ba9
PM
399 return CP_ACCESS_TRAP_EL2;
400 }
401 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
402 return CP_ACCESS_TRAP_EL3;
403 }
404 return CP_ACCESS_OK;
405}
406
84929218
RH
407/* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
408static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
409 bool isread)
410{
411 if (arm_current_el(env) == 1) {
412 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
413 if (arm_hcr_el2_eff(env) & trap) {
414 return CP_ACCESS_TRAP_EL2;
415 }
416 }
417 return CP_ACCESS_OK;
418}
419
1803d271
RH
420/* Check for traps from EL1 due to HCR_EL2.TSW. */
421static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
422 bool isread)
423{
424 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
425 return CP_ACCESS_TRAP_EL2;
426 }
427 return CP_ACCESS_OK;
428}
429
99602377
RH
430/* Check for traps from EL1 due to HCR_EL2.TACR. */
431static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
432 bool isread)
433{
434 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
435 return CP_ACCESS_TRAP_EL2;
436 }
437 return CP_ACCESS_OK;
438}
439
30881b73
RH
440/* Check for traps from EL1 due to HCR_EL2.TTLB. */
441static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
442 bool isread)
443{
444 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
445 return CP_ACCESS_TRAP_EL2;
446 }
447 return CP_ACCESS_OK;
448}
449
c4241c7d 450static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 451{
2fc0cc0e 452 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 453
8d5c773e 454 raw_write(env, ri, value);
d10eb08f 455 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
456}
457
c4241c7d 458static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 459{
2fc0cc0e 460 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 461
8d5c773e 462 if (raw_read(env, ri) != value) {
08de207b
PM
463 /* Unlike real hardware the qemu TLB uses virtual addresses,
464 * not modified virtual addresses, so this causes a TLB flush.
465 */
d10eb08f 466 tlb_flush(CPU(cpu));
8d5c773e 467 raw_write(env, ri, value);
08de207b 468 }
08de207b 469}
c4241c7d
PM
470
471static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
472 uint64_t value)
08de207b 473{
2fc0cc0e 474 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 475
452a0955 476 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 477 && !extended_addresses_enabled(env)) {
08de207b
PM
478 /* For VMSA (when not using the LPAE long descriptor page table
479 * format) this register includes the ASID, so do a TLB flush.
480 * For PMSA it is purely a process ID and no action is needed.
481 */
d10eb08f 482 tlb_flush(CPU(cpu));
08de207b 483 }
8d5c773e 484 raw_write(env, ri, value);
08de207b
PM
485}
486
b4ab8ce9
PM
487/* IS variants of TLB operations must affect all cores */
488static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
489 uint64_t value)
490{
29a0af61 491 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
492
493 tlb_flush_all_cpus_synced(cs);
494}
495
496static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
497 uint64_t value)
498{
29a0af61 499 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
500
501 tlb_flush_all_cpus_synced(cs);
502}
503
504static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
505 uint64_t value)
506{
29a0af61 507 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
508
509 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
510}
511
512static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
513 uint64_t value)
514{
29a0af61 515 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
516
517 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
518}
519
520/*
521 * Non-IS variants of TLB operations are upgraded to
373e7ffd 522 * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
b4ab8ce9
PM
523 * force broadcast of these operations.
524 */
525static bool tlb_force_broadcast(CPUARMState *env)
526{
373e7ffd 527 return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
b4ab8ce9
PM
528}
529
c4241c7d
PM
530static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
531 uint64_t value)
d929823f
PM
532{
533 /* Invalidate all (TLBIALL) */
527db2be 534 CPUState *cs = env_cpu(env);
00c8cb0a 535
b4ab8ce9 536 if (tlb_force_broadcast(env)) {
527db2be
RH
537 tlb_flush_all_cpus_synced(cs);
538 } else {
539 tlb_flush(cs);
b4ab8ce9 540 }
d929823f
PM
541}
542
c4241c7d
PM
543static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
544 uint64_t value)
d929823f
PM
545{
546 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
527db2be 547 CPUState *cs = env_cpu(env);
31b030d4 548
527db2be 549 value &= TARGET_PAGE_MASK;
b4ab8ce9 550 if (tlb_force_broadcast(env)) {
527db2be
RH
551 tlb_flush_page_all_cpus_synced(cs, value);
552 } else {
553 tlb_flush_page(cs, value);
b4ab8ce9 554 }
d929823f
PM
555}
556
c4241c7d
PM
557static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
558 uint64_t value)
d929823f
PM
559{
560 /* Invalidate by ASID (TLBIASID) */
527db2be 561 CPUState *cs = env_cpu(env);
00c8cb0a 562
b4ab8ce9 563 if (tlb_force_broadcast(env)) {
527db2be
RH
564 tlb_flush_all_cpus_synced(cs);
565 } else {
566 tlb_flush(cs);
b4ab8ce9 567 }
d929823f
PM
568}
569
c4241c7d
PM
570static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
571 uint64_t value)
d929823f
PM
572{
573 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
527db2be 574 CPUState *cs = env_cpu(env);
31b030d4 575
527db2be 576 value &= TARGET_PAGE_MASK;
b4ab8ce9 577 if (tlb_force_broadcast(env)) {
527db2be
RH
578 tlb_flush_page_all_cpus_synced(cs, value);
579 } else {
580 tlb_flush_page(cs, value);
b4ab8ce9 581 }
fa439fc5
PM
582}
583
541ef8c2
SS
584static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
585 uint64_t value)
586{
29a0af61 587 CPUState *cs = env_cpu(env);
541ef8c2 588
0336cbf8 589 tlb_flush_by_mmuidx(cs,
01b98b68 590 ARMMMUIdxBit_E10_1 |
452ef8cb 591 ARMMMUIdxBit_E10_1_PAN |
bf05340c 592 ARMMMUIdxBit_E10_0);
541ef8c2
SS
593}
594
595static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
596 uint64_t value)
597{
29a0af61 598 CPUState *cs = env_cpu(env);
541ef8c2 599
a67cf277 600 tlb_flush_by_mmuidx_all_cpus_synced(cs,
01b98b68 601 ARMMMUIdxBit_E10_1 |
452ef8cb 602 ARMMMUIdxBit_E10_1_PAN |
bf05340c 603 ARMMMUIdxBit_E10_0);
541ef8c2
SS
604}
605
541ef8c2
SS
606
607static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
608 uint64_t value)
609{
29a0af61 610 CPUState *cs = env_cpu(env);
541ef8c2 611
e013b741 612 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
613}
614
615static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
616 uint64_t value)
617{
29a0af61 618 CPUState *cs = env_cpu(env);
541ef8c2 619
e013b741 620 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
621}
622
623static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
624 uint64_t value)
625{
29a0af61 626 CPUState *cs = env_cpu(env);
541ef8c2
SS
627 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
628
e013b741 629 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
541ef8c2
SS
630}
631
632static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
633 uint64_t value)
634{
29a0af61 635 CPUState *cs = env_cpu(env);
541ef8c2
SS
636 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
637
a67cf277 638 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 639 ARMMMUIdxBit_E2);
541ef8c2
SS
640}
641
e9aa6c21 642static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
643 /* Define the secure and non-secure FCSE identifier CP registers
644 * separately because there is no secure bank in V8 (no _EL3). This allows
645 * the secure register to be properly reset and migrated. There is also no
646 * v8 EL1 version of the register so the non-secure instance stands alone.
647 */
9c513e78 648 { .name = "FCSEIDR",
54bf36ed
FA
649 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
650 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
651 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
652 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9c513e78 653 { .name = "FCSEIDR_S",
54bf36ed
FA
654 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
655 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
656 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 657 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
658 /* Define the secure and non-secure context identifier CP registers
659 * separately because there is no secure bank in V8 (no _EL3). This allows
660 * the secure register to be properly reset and migrated. In the
661 * non-secure case, the 32-bit register will have reset and migration
662 * disabled during registration as it is handled by the 64-bit instance.
663 */
664 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 665 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
666 .access = PL1_RW, .accessfn = access_tvm_trvm,
667 .secure = ARM_CP_SECSTATE_NS,
54bf36ed
FA
668 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
669 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9c513e78 670 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
54bf36ed 671 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
672 .access = PL1_RW, .accessfn = access_tvm_trvm,
673 .secure = ARM_CP_SECSTATE_S,
54bf36ed 674 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 675 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
676 REGINFO_SENTINEL
677};
678
679static const ARMCPRegInfo not_v8_cp_reginfo[] = {
680 /* NB: Some of these registers exist in v8 but with more precise
681 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
682 */
683 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
684 { .name = "DACR",
685 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
84929218 686 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
687 .writefn = dacr_write, .raw_writefn = raw_write,
688 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
689 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
690 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
691 * For v6 and v5, these mappings are overly broad.
4fdd17dd 692 */
a903c449
EI
693 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
694 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
695 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
696 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
697 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
698 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
699 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 700 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
701 /* Cache maintenance ops; some of this space may be overridden later. */
702 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
703 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
704 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
705 REGINFO_SENTINEL
706};
707
7d57f408
PM
708static const ARMCPRegInfo not_v6_cp_reginfo[] = {
709 /* Not all pre-v6 cores implemented this WFI, so this is slightly
710 * over-broad.
711 */
712 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
713 .access = PL1_W, .type = ARM_CP_WFI },
714 REGINFO_SENTINEL
715};
716
717static const ARMCPRegInfo not_v7_cp_reginfo[] = {
718 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
719 * is UNPREDICTABLE; we choose to NOP as most implementations do).
720 */
721 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
722 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
723 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
724 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
725 * OMAPCP will override this space.
726 */
727 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
728 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
729 .resetvalue = 0 },
730 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
731 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
732 .resetvalue = 0 },
776d4e5c
PM
733 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
734 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 735 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 736 .resetvalue = 0 },
50300698
PM
737 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
738 * implementing it as RAZ means the "debug architecture version" bits
739 * will read as a reserved value, which should cause Linux to not try
740 * to use the debug hardware.
741 */
742 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
743 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
744 /* MMU TLB control. Note that the wildcarding means we cover not just
745 * the unified TLB ops but also the dside/iside/inner-shareable variants.
746 */
747 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
748 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 749 .type = ARM_CP_NO_RAW },
995939a6
PM
750 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
751 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 752 .type = ARM_CP_NO_RAW },
995939a6
PM
753 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
754 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 755 .type = ARM_CP_NO_RAW },
995939a6
PM
756 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
757 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 758 .type = ARM_CP_NO_RAW },
a903c449
EI
759 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
760 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
761 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
762 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
763 REGINFO_SENTINEL
764};
765
c4241c7d
PM
766static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
767 uint64_t value)
2771db27 768{
f0aff255
FA
769 uint32_t mask = 0;
770
771 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
772 if (!arm_feature(env, ARM_FEATURE_V8)) {
773 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
774 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
775 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
776 */
7fbc6a40 777 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
f0aff255
FA
778 /* VFP coprocessor: cp10 & cp11 [23:20] */
779 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
780
781 if (!arm_feature(env, ARM_FEATURE_NEON)) {
782 /* ASEDIS [31] bit is RAO/WI */
783 value |= (1 << 31);
784 }
785
786 /* VFPv3 and upwards with NEON implement 32 double precision
787 * registers (D0-D31).
788 */
a6627f5f 789 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
f0aff255
FA
790 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
791 value |= (1 << 30);
792 }
793 }
794 value &= mask;
2771db27 795 }
fc1120a7
PM
796
797 /*
798 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
799 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
800 */
801 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
802 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
803 value &= ~(0xf << 20);
804 value |= env->cp15.cpacr_el1 & (0xf << 20);
805 }
806
7ebd5f2e 807 env->cp15.cpacr_el1 = value;
2771db27
PM
808}
809
fc1120a7
PM
810static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
811{
812 /*
813 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
814 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
815 */
816 uint64_t value = env->cp15.cpacr_el1;
817
818 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
819 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
820 value &= ~(0xf << 20);
821 }
822 return value;
823}
824
825
5deac39c
PM
826static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
827{
828 /* Call cpacr_write() so that we reset with the correct RAO bits set
829 * for our CPU features.
830 */
831 cpacr_write(env, ri, 0);
832}
833
3f208fd7
PM
834static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
835 bool isread)
c6f19164
GB
836{
837 if (arm_feature(env, ARM_FEATURE_V8)) {
838 /* Check if CPACR accesses are to be trapped to EL2 */
e6ef0169
RDC
839 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
840 (env->cp15.cptr_el[2] & CPTR_TCPAC)) {
c6f19164
GB
841 return CP_ACCESS_TRAP_EL2;
842 /* Check if CPACR accesses are to be trapped to EL3 */
843 } else if (arm_current_el(env) < 3 &&
844 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
845 return CP_ACCESS_TRAP_EL3;
846 }
847 }
848
849 return CP_ACCESS_OK;
850}
851
3f208fd7
PM
852static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
853 bool isread)
c6f19164
GB
854{
855 /* Check if CPTR accesses are set to trap to EL3 */
856 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
857 return CP_ACCESS_TRAP_EL3;
858 }
859
860 return CP_ACCESS_OK;
861}
862
7d57f408
PM
863static const ARMCPRegInfo v6_cp_reginfo[] = {
864 /* prefetch by MVA in v6, NOP in v7 */
865 { .name = "MVA_prefetch",
866 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
867 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
868 /* We need to break the TB after ISB to execute self-modifying code
869 * correctly and also to take any pending interrupts immediately.
870 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
871 */
7d57f408 872 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 873 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 874 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 875 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 876 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 877 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 878 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218 879 .access = PL1_RW, .accessfn = access_tvm_trvm,
b848ce2b
FA
880 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
881 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
882 .resetvalue = 0, },
883 /* Watchpoint Fault Address Register : should actually only be present
884 * for 1136, 1176, 11MPCore.
885 */
886 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
887 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 888 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 889 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 890 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
fc1120a7 891 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
7d57f408
PM
892 REGINFO_SENTINEL
893};
894
57a4a11b
AL
895typedef struct pm_event {
896 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
897 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
898 bool (*supported)(CPUARMState *);
899 /*
900 * Retrieve the current count of the underlying event. The programmed
901 * counters hold a difference from the return value from this function
902 */
903 uint64_t (*get_count)(CPUARMState *);
4e7beb0c
AL
904 /*
905 * Return how many nanoseconds it will take (at a minimum) for count events
906 * to occur. A negative value indicates the counter will never overflow, or
907 * that the counter has otherwise arranged for the overflow bit to be set
908 * and the PMU interrupt to be raised on overflow.
909 */
910 int64_t (*ns_per_count)(uint64_t);
57a4a11b
AL
911} pm_event;
912
b2e23725
AL
913static bool event_always_supported(CPUARMState *env)
914{
915 return true;
916}
917
0d4bfd7d
AL
918static uint64_t swinc_get_count(CPUARMState *env)
919{
920 /*
921 * SW_INCR events are written directly to the pmevcntr's by writes to
922 * PMSWINC, so there is no underlying count maintained by the PMU itself
923 */
924 return 0;
925}
926
4e7beb0c
AL
927static int64_t swinc_ns_per(uint64_t ignored)
928{
929 return -1;
930}
931
b2e23725
AL
932/*
933 * Return the underlying cycle count for the PMU cycle counters. If we're in
934 * usermode, simply return 0.
935 */
936static uint64_t cycles_get_count(CPUARMState *env)
937{
938#ifndef CONFIG_USER_ONLY
939 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
940 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
941#else
942 return cpu_get_host_ticks();
943#endif
944}
945
946#ifndef CONFIG_USER_ONLY
4e7beb0c
AL
947static int64_t cycles_ns_per(uint64_t cycles)
948{
949 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
950}
951
b2e23725
AL
952static bool instructions_supported(CPUARMState *env)
953{
740b1759 954 return icount_enabled() == 1; /* Precise instruction counting */
b2e23725
AL
955}
956
957static uint64_t instructions_get_count(CPUARMState *env)
958{
8191d368 959 return (uint64_t)icount_get_raw();
b2e23725 960}
4e7beb0c
AL
961
962static int64_t instructions_ns_per(uint64_t icount)
963{
8191d368 964 return icount_to_ns((int64_t)icount);
4e7beb0c 965}
b2e23725
AL
966#endif
967
0727f63b
PM
968static bool pmu_8_1_events_supported(CPUARMState *env)
969{
970 /* For events which are supported in any v8.1 PMU */
971 return cpu_isar_feature(any_pmu_8_1, env_archcpu(env));
972}
973
15dd1ebd
PM
974static bool pmu_8_4_events_supported(CPUARMState *env)
975{
976 /* For events which are supported in any v8.1 PMU */
977 return cpu_isar_feature(any_pmu_8_4, env_archcpu(env));
978}
979
0727f63b
PM
980static uint64_t zero_event_get_count(CPUARMState *env)
981{
982 /* For events which on QEMU never fire, so their count is always zero */
983 return 0;
984}
985
986static int64_t zero_event_ns_per(uint64_t cycles)
987{
988 /* An event which never fires can never overflow */
989 return -1;
990}
991
57a4a11b 992static const pm_event pm_events[] = {
0d4bfd7d
AL
993 { .number = 0x000, /* SW_INCR */
994 .supported = event_always_supported,
995 .get_count = swinc_get_count,
4e7beb0c 996 .ns_per_count = swinc_ns_per,
0d4bfd7d 997 },
b2e23725
AL
998#ifndef CONFIG_USER_ONLY
999 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
1000 .supported = instructions_supported,
1001 .get_count = instructions_get_count,
4e7beb0c 1002 .ns_per_count = instructions_ns_per,
b2e23725
AL
1003 },
1004 { .number = 0x011, /* CPU_CYCLES, Cycle */
1005 .supported = event_always_supported,
1006 .get_count = cycles_get_count,
4e7beb0c 1007 .ns_per_count = cycles_ns_per,
0727f63b 1008 },
b2e23725 1009#endif
0727f63b
PM
1010 { .number = 0x023, /* STALL_FRONTEND */
1011 .supported = pmu_8_1_events_supported,
1012 .get_count = zero_event_get_count,
1013 .ns_per_count = zero_event_ns_per,
1014 },
1015 { .number = 0x024, /* STALL_BACKEND */
1016 .supported = pmu_8_1_events_supported,
1017 .get_count = zero_event_get_count,
1018 .ns_per_count = zero_event_ns_per,
1019 },
15dd1ebd
PM
1020 { .number = 0x03c, /* STALL */
1021 .supported = pmu_8_4_events_supported,
1022 .get_count = zero_event_get_count,
1023 .ns_per_count = zero_event_ns_per,
1024 },
57a4a11b
AL
1025};
1026
1027/*
1028 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1029 * events (i.e. the statistical profiling extension), this implementation
1030 * should first be updated to something sparse instead of the current
1031 * supported_event_map[] array.
1032 */
15dd1ebd 1033#define MAX_EVENT_ID 0x3c
57a4a11b
AL
1034#define UNSUPPORTED_EVENT UINT16_MAX
1035static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1036
1037/*
bf8d0969
AL
1038 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1039 * of ARM event numbers to indices in our pm_events array.
57a4a11b
AL
1040 *
1041 * Note: Events in the 0x40XX range are not currently supported.
1042 */
bf8d0969 1043void pmu_init(ARMCPU *cpu)
57a4a11b 1044{
57a4a11b
AL
1045 unsigned int i;
1046
bf8d0969
AL
1047 /*
1048 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1049 * events to them
1050 */
57a4a11b
AL
1051 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1052 supported_event_map[i] = UNSUPPORTED_EVENT;
1053 }
bf8d0969
AL
1054 cpu->pmceid0 = 0;
1055 cpu->pmceid1 = 0;
57a4a11b
AL
1056
1057 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1058 const pm_event *cnt = &pm_events[i];
1059 assert(cnt->number <= MAX_EVENT_ID);
1060 /* We do not currently support events in the 0x40xx range */
1061 assert(cnt->number <= 0x3f);
1062
bf8d0969 1063 if (cnt->supported(&cpu->env)) {
57a4a11b 1064 supported_event_map[cnt->number] = i;
67da43d6 1065 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
bf8d0969
AL
1066 if (cnt->number & 0x20) {
1067 cpu->pmceid1 |= event_mask;
1068 } else {
1069 cpu->pmceid0 |= event_mask;
1070 }
57a4a11b
AL
1071 }
1072 }
57a4a11b
AL
1073}
1074
5ecdd3e4
AL
1075/*
1076 * Check at runtime whether a PMU event is supported for the current machine
1077 */
1078static bool event_supported(uint16_t number)
1079{
1080 if (number > MAX_EVENT_ID) {
1081 return false;
1082 }
1083 return supported_event_map[number] != UNSUPPORTED_EVENT;
1084}
1085
3f208fd7
PM
1086static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1087 bool isread)
200ac0ef 1088{
3b163b01 1089 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
1090 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1091 * trapping to EL2 or EL3 for other accesses.
200ac0ef 1092 */
1fce1ba9 1093 int el = arm_current_el(env);
59dd089c 1094 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1fce1ba9 1095
6ecd0b6b 1096 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 1097 return CP_ACCESS_TRAP;
200ac0ef 1098 }
59dd089c 1099 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1fce1ba9
PM
1100 return CP_ACCESS_TRAP_EL2;
1101 }
1102 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1103 return CP_ACCESS_TRAP_EL3;
1104 }
1105
fcd25206 1106 return CP_ACCESS_OK;
200ac0ef
PM
1107}
1108
6ecd0b6b
AB
1109static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1110 const ARMCPRegInfo *ri,
1111 bool isread)
1112{
1113 /* ER: event counter read trap control */
1114 if (arm_feature(env, ARM_FEATURE_V8)
1115 && arm_current_el(env) == 0
1116 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1117 && isread) {
1118 return CP_ACCESS_OK;
1119 }
1120
1121 return pmreg_access(env, ri, isread);
1122}
1123
1124static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1125 const ARMCPRegInfo *ri,
1126 bool isread)
1127{
1128 /* SW: software increment write trap control */
1129 if (arm_feature(env, ARM_FEATURE_V8)
1130 && arm_current_el(env) == 0
1131 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1132 && !isread) {
1133 return CP_ACCESS_OK;
1134 }
1135
1136 return pmreg_access(env, ri, isread);
1137}
1138
6ecd0b6b
AB
1139static CPAccessResult pmreg_access_selr(CPUARMState *env,
1140 const ARMCPRegInfo *ri,
1141 bool isread)
1142{
1143 /* ER: event counter read trap control */
1144 if (arm_feature(env, ARM_FEATURE_V8)
1145 && arm_current_el(env) == 0
1146 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1147 return CP_ACCESS_OK;
1148 }
1149
1150 return pmreg_access(env, ri, isread);
1151}
1152
1153static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1154 const ARMCPRegInfo *ri,
1155 bool isread)
1156{
1157 /* CR: cycle counter read trap control */
1158 if (arm_feature(env, ARM_FEATURE_V8)
1159 && arm_current_el(env) == 0
1160 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1161 && isread) {
1162 return CP_ACCESS_OK;
1163 }
1164
1165 return pmreg_access(env, ri, isread);
1166}
1167
033614c4
AL
1168/* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1169 * the current EL, security state, and register configuration.
1170 */
1171static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
87124fde 1172{
033614c4
AL
1173 uint64_t filter;
1174 bool e, p, u, nsk, nsu, nsh, m;
1175 bool enabled, prohibited, filtered;
1176 bool secure = arm_is_secure(env);
1177 int el = arm_current_el(env);
59dd089c
RDC
1178 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1179 uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
87124fde 1180
cbbb3041
AJ
1181 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1182 return false;
1183 }
1184
033614c4
AL
1185 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1186 (counter < hpmn || counter == 31)) {
1187 e = env->cp15.c9_pmcr & PMCRE;
1188 } else {
59dd089c 1189 e = mdcr_el2 & MDCR_HPME;
87124fde 1190 }
033614c4 1191 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
87124fde 1192
033614c4
AL
1193 if (!secure) {
1194 if (el == 2 && (counter < hpmn || counter == 31)) {
59dd089c 1195 prohibited = mdcr_el2 & MDCR_HPMD;
033614c4
AL
1196 } else {
1197 prohibited = false;
1198 }
1199 } else {
1200 prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
db1f3afb 1201 !(env->cp15.mdcr_el3 & MDCR_SPME);
033614c4
AL
1202 }
1203
1204 if (prohibited && counter == 31) {
1205 prohibited = env->cp15.c9_pmcr & PMCRDP;
1206 }
1207
5ecdd3e4
AL
1208 if (counter == 31) {
1209 filter = env->cp15.pmccfiltr_el0;
1210 } else {
1211 filter = env->cp15.c14_pmevtyper[counter];
1212 }
033614c4
AL
1213
1214 p = filter & PMXEVTYPER_P;
1215 u = filter & PMXEVTYPER_U;
1216 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1217 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1218 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1219 m = arm_el_is_aa64(env, 1) &&
1220 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1221
1222 if (el == 0) {
1223 filtered = secure ? u : u != nsu;
1224 } else if (el == 1) {
1225 filtered = secure ? p : p != nsk;
1226 } else if (el == 2) {
1227 filtered = !nsh;
1228 } else { /* EL3 */
1229 filtered = m != p;
1230 }
1231
5ecdd3e4
AL
1232 if (counter != 31) {
1233 /*
1234 * If not checking PMCCNTR, ensure the counter is setup to an event we
1235 * support
1236 */
1237 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1238 if (!event_supported(event)) {
1239 return false;
1240 }
1241 }
1242
033614c4 1243 return enabled && !prohibited && !filtered;
87124fde 1244}
033614c4 1245
f4efb4b2
AL
1246static void pmu_update_irq(CPUARMState *env)
1247{
2fc0cc0e 1248 ARMCPU *cpu = env_archcpu(env);
f4efb4b2
AL
1249 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1250 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1251}
1252
5d05b9d4
AL
1253/*
1254 * Ensure c15_ccnt is the guest-visible count so that operations such as
1255 * enabling/disabling the counter or filtering, modifying the count itself,
1256 * etc. can be done logically. This is essentially a no-op if the counter is
1257 * not enabled at the time of the call.
1258 */
f2b2f53f 1259static void pmccntr_op_start(CPUARMState *env)
ec7b4ce4 1260{
b2e23725 1261 uint64_t cycles = cycles_get_count(env);
ec7b4ce4 1262
033614c4 1263 if (pmu_counter_enabled(env, 31)) {
5d05b9d4
AL
1264 uint64_t eff_cycles = cycles;
1265 if (env->cp15.c9_pmcr & PMCRD) {
1266 /* Increment once every 64 processor clock cycles */
1267 eff_cycles /= 64;
1268 }
1269
f4efb4b2
AL
1270 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1271
1272 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1273 1ull << 63 : 1ull << 31;
1274 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1275 env->cp15.c9_pmovsr |= (1 << 31);
1276 pmu_update_irq(env);
1277 }
1278
1279 env->cp15.c15_ccnt = new_pmccntr;
ec7b4ce4 1280 }
5d05b9d4
AL
1281 env->cp15.c15_ccnt_delta = cycles;
1282}
ec7b4ce4 1283
5d05b9d4
AL
1284/*
1285 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1286 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1287 * pmccntr_op_start.
1288 */
f2b2f53f 1289static void pmccntr_op_finish(CPUARMState *env)
5d05b9d4 1290{
033614c4 1291 if (pmu_counter_enabled(env, 31)) {
4e7beb0c
AL
1292#ifndef CONFIG_USER_ONLY
1293 /* Calculate when the counter will next overflow */
1294 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1295 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1296 remaining_cycles = (uint32_t)remaining_cycles;
1297 }
1298 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1299
1300 if (overflow_in > 0) {
1301 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1302 overflow_in;
2fc0cc0e 1303 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1304 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1305 }
1306#endif
5d05b9d4 1307
4e7beb0c 1308 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
5d05b9d4
AL
1309 if (env->cp15.c9_pmcr & PMCRD) {
1310 /* Increment once every 64 processor clock cycles */
1311 prev_cycles /= 64;
1312 }
5d05b9d4 1313 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
ec7b4ce4
AF
1314 }
1315}
1316
5ecdd3e4
AL
1317static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1318{
1319
1320 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1321 uint64_t count = 0;
1322 if (event_supported(event)) {
1323 uint16_t event_idx = supported_event_map[event];
1324 count = pm_events[event_idx].get_count(env);
1325 }
1326
1327 if (pmu_counter_enabled(env, counter)) {
f4efb4b2
AL
1328 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1329
1330 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1331 env->cp15.c9_pmovsr |= (1 << counter);
1332 pmu_update_irq(env);
1333 }
1334 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
5ecdd3e4
AL
1335 }
1336 env->cp15.c14_pmevcntr_delta[counter] = count;
1337}
1338
1339static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1340{
1341 if (pmu_counter_enabled(env, counter)) {
4e7beb0c
AL
1342#ifndef CONFIG_USER_ONLY
1343 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1344 uint16_t event_idx = supported_event_map[event];
1345 uint64_t delta = UINT32_MAX -
1346 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1347 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1348
1349 if (overflow_in > 0) {
1350 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1351 overflow_in;
2fc0cc0e 1352 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1353 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1354 }
1355#endif
1356
5ecdd3e4
AL
1357 env->cp15.c14_pmevcntr_delta[counter] -=
1358 env->cp15.c14_pmevcntr[counter];
1359 }
1360}
1361
5d05b9d4
AL
1362void pmu_op_start(CPUARMState *env)
1363{
5ecdd3e4 1364 unsigned int i;
5d05b9d4 1365 pmccntr_op_start(env);
5ecdd3e4
AL
1366 for (i = 0; i < pmu_num_counters(env); i++) {
1367 pmevcntr_op_start(env, i);
1368 }
5d05b9d4
AL
1369}
1370
1371void pmu_op_finish(CPUARMState *env)
1372{
5ecdd3e4 1373 unsigned int i;
5d05b9d4 1374 pmccntr_op_finish(env);
5ecdd3e4
AL
1375 for (i = 0; i < pmu_num_counters(env); i++) {
1376 pmevcntr_op_finish(env, i);
1377 }
5d05b9d4
AL
1378}
1379
033614c4
AL
1380void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1381{
1382 pmu_op_start(&cpu->env);
1383}
1384
1385void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1386{
1387 pmu_op_finish(&cpu->env);
1388}
1389
4e7beb0c
AL
1390void arm_pmu_timer_cb(void *opaque)
1391{
1392 ARMCPU *cpu = opaque;
1393
1394 /*
1395 * Update all the counter values based on the current underlying counts,
1396 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1397 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1398 * counter may expire.
1399 */
1400 pmu_op_start(&cpu->env);
1401 pmu_op_finish(&cpu->env);
1402}
1403
c4241c7d
PM
1404static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1405 uint64_t value)
200ac0ef 1406{
5d05b9d4 1407 pmu_op_start(env);
7c2cb42b
AF
1408
1409 if (value & PMCRC) {
1410 /* The counter has been reset */
1411 env->cp15.c15_ccnt = 0;
1412 }
1413
5ecdd3e4
AL
1414 if (value & PMCRP) {
1415 unsigned int i;
1416 for (i = 0; i < pmu_num_counters(env); i++) {
1417 env->cp15.c14_pmevcntr[i] = 0;
1418 }
1419 }
1420
62d96ff4
PM
1421 env->cp15.c9_pmcr &= ~PMCR_WRITEABLE_MASK;
1422 env->cp15.c9_pmcr |= (value & PMCR_WRITEABLE_MASK);
7c2cb42b 1423
5d05b9d4 1424 pmu_op_finish(env);
7c2cb42b
AF
1425}
1426
0d4bfd7d
AL
1427static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1428 uint64_t value)
1429{
1430 unsigned int i;
1431 for (i = 0; i < pmu_num_counters(env); i++) {
1432 /* Increment a counter's count iff: */
1433 if ((value & (1 << i)) && /* counter's bit is set */
1434 /* counter is enabled and not filtered */
1435 pmu_counter_enabled(env, i) &&
1436 /* counter is SW_INCR */
1437 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1438 pmevcntr_op_start(env, i);
f4efb4b2
AL
1439
1440 /*
1441 * Detect if this write causes an overflow since we can't predict
1442 * PMSWINC overflows like we can for other events
1443 */
1444 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1445
1446 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1447 env->cp15.c9_pmovsr |= (1 << i);
1448 pmu_update_irq(env);
1449 }
1450
1451 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1452
0d4bfd7d
AL
1453 pmevcntr_op_finish(env, i);
1454 }
1455 }
1456}
1457
7c2cb42b
AF
1458static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1459{
5d05b9d4
AL
1460 uint64_t ret;
1461 pmccntr_op_start(env);
1462 ret = env->cp15.c15_ccnt;
1463 pmccntr_op_finish(env);
1464 return ret;
7c2cb42b
AF
1465}
1466
6b040780
WH
1467static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1468 uint64_t value)
1469{
1470 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1471 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1472 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1473 * accessed.
1474 */
1475 env->cp15.c9_pmselr = value & 0x1f;
1476}
1477
7c2cb42b
AF
1478static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1479 uint64_t value)
1480{
5d05b9d4
AL
1481 pmccntr_op_start(env);
1482 env->cp15.c15_ccnt = value;
1483 pmccntr_op_finish(env);
200ac0ef 1484}
421c7ebd
PC
1485
1486static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1487 uint64_t value)
1488{
1489 uint64_t cur_val = pmccntr_read(env, NULL);
1490
1491 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1492}
1493
0614601c
AF
1494static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1495 uint64_t value)
1496{
5d05b9d4 1497 pmccntr_op_start(env);
4b8afa1f
AL
1498 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1499 pmccntr_op_finish(env);
1500}
1501
1502static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1503 uint64_t value)
1504{
1505 pmccntr_op_start(env);
1506 /* M is not accessible from AArch32 */
1507 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1508 (value & PMCCFILTR);
5d05b9d4 1509 pmccntr_op_finish(env);
0614601c
AF
1510}
1511
4b8afa1f
AL
1512static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1513{
1514 /* M is not visible in AArch32 */
1515 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1516}
1517
c4241c7d 1518static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1519 uint64_t value)
1520{
7ece99b1 1521 value &= pmu_counter_mask(env);
200ac0ef 1522 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
1523}
1524
c4241c7d
PM
1525static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1526 uint64_t value)
200ac0ef 1527{
7ece99b1 1528 value &= pmu_counter_mask(env);
200ac0ef 1529 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
1530}
1531
c4241c7d
PM
1532static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1533 uint64_t value)
200ac0ef 1534{
599b71e2 1535 value &= pmu_counter_mask(env);
200ac0ef 1536 env->cp15.c9_pmovsr &= ~value;
f4efb4b2 1537 pmu_update_irq(env);
200ac0ef
PM
1538}
1539
327dd510
AL
1540static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1541 uint64_t value)
1542{
1543 value &= pmu_counter_mask(env);
1544 env->cp15.c9_pmovsr |= value;
f4efb4b2 1545 pmu_update_irq(env);
327dd510
AL
1546}
1547
5ecdd3e4
AL
1548static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1549 uint64_t value, const uint8_t counter)
200ac0ef 1550{
5ecdd3e4
AL
1551 if (counter == 31) {
1552 pmccfiltr_write(env, ri, value);
1553 } else if (counter < pmu_num_counters(env)) {
1554 pmevcntr_op_start(env, counter);
1555
1556 /*
1557 * If this counter's event type is changing, store the current
1558 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1559 * pmevcntr_op_finish has the correct baseline when it converts back to
1560 * a delta.
1561 */
1562 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1563 PMXEVTYPER_EVTCOUNT;
1564 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1565 if (old_event != new_event) {
1566 uint64_t count = 0;
1567 if (event_supported(new_event)) {
1568 uint16_t event_idx = supported_event_map[new_event];
1569 count = pm_events[event_idx].get_count(env);
1570 }
1571 env->cp15.c14_pmevcntr_delta[counter] = count;
1572 }
1573
1574 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1575 pmevcntr_op_finish(env, counter);
1576 }
fdb86656
WH
1577 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1578 * PMSELR value is equal to or greater than the number of implemented
1579 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1580 */
5ecdd3e4
AL
1581}
1582
1583static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1584 const uint8_t counter)
1585{
1586 if (counter == 31) {
1587 return env->cp15.pmccfiltr_el0;
1588 } else if (counter < pmu_num_counters(env)) {
1589 return env->cp15.c14_pmevtyper[counter];
1590 } else {
1591 /*
1592 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1593 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1594 */
1595 return 0;
1596 }
1597}
1598
1599static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1600 uint64_t value)
1601{
1602 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1603 pmevtyper_write(env, ri, value, counter);
1604}
1605
1606static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1607 uint64_t value)
1608{
1609 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1610 env->cp15.c14_pmevtyper[counter] = value;
1611
1612 /*
1613 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1614 * pmu_op_finish calls when loading saved state for a migration. Because
1615 * we're potentially updating the type of event here, the value written to
1616 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1617 * different counter type. Therefore, we need to set this value to the
1618 * current count for the counter type we're writing so that pmu_op_finish
1619 * has the correct count for its calculation.
1620 */
1621 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1622 if (event_supported(event)) {
1623 uint16_t event_idx = supported_event_map[event];
1624 env->cp15.c14_pmevcntr_delta[counter] =
1625 pm_events[event_idx].get_count(env);
fdb86656
WH
1626 }
1627}
1628
5ecdd3e4
AL
1629static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1630{
1631 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1632 return pmevtyper_read(env, ri, counter);
1633}
1634
1635static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1636 uint64_t value)
1637{
1638 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1639}
1640
fdb86656
WH
1641static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1642{
5ecdd3e4
AL
1643 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1644}
1645
1646static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1647 uint64_t value, uint8_t counter)
1648{
1649 if (counter < pmu_num_counters(env)) {
1650 pmevcntr_op_start(env, counter);
1651 env->cp15.c14_pmevcntr[counter] = value;
1652 pmevcntr_op_finish(env, counter);
1653 }
1654 /*
1655 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1656 * are CONSTRAINED UNPREDICTABLE.
fdb86656 1657 */
5ecdd3e4
AL
1658}
1659
1660static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1661 uint8_t counter)
1662{
1663 if (counter < pmu_num_counters(env)) {
1664 uint64_t ret;
1665 pmevcntr_op_start(env, counter);
1666 ret = env->cp15.c14_pmevcntr[counter];
1667 pmevcntr_op_finish(env, counter);
1668 return ret;
fdb86656 1669 } else {
5ecdd3e4
AL
1670 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1671 * are CONSTRAINED UNPREDICTABLE. */
fdb86656
WH
1672 return 0;
1673 }
200ac0ef
PM
1674}
1675
5ecdd3e4
AL
1676static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1677 uint64_t value)
1678{
1679 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1680 pmevcntr_write(env, ri, value, counter);
1681}
1682
1683static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1684{
1685 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1686 return pmevcntr_read(env, ri, counter);
1687}
1688
1689static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1690 uint64_t value)
1691{
1692 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1693 assert(counter < pmu_num_counters(env));
1694 env->cp15.c14_pmevcntr[counter] = value;
1695 pmevcntr_write(env, ri, value, counter);
1696}
1697
1698static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1699{
1700 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1701 assert(counter < pmu_num_counters(env));
1702 return env->cp15.c14_pmevcntr[counter];
1703}
1704
1705static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1706 uint64_t value)
1707{
1708 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1709}
1710
1711static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1712{
1713 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1714}
1715
c4241c7d 1716static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1717 uint64_t value)
1718{
6ecd0b6b
AB
1719 if (arm_feature(env, ARM_FEATURE_V8)) {
1720 env->cp15.c9_pmuserenr = value & 0xf;
1721 } else {
1722 env->cp15.c9_pmuserenr = value & 1;
1723 }
200ac0ef
PM
1724}
1725
c4241c7d
PM
1726static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1727 uint64_t value)
200ac0ef
PM
1728{
1729 /* We have no event counters so only the C bit can be changed */
7ece99b1 1730 value &= pmu_counter_mask(env);
200ac0ef 1731 env->cp15.c9_pminten |= value;
f4efb4b2 1732 pmu_update_irq(env);
200ac0ef
PM
1733}
1734
c4241c7d
PM
1735static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1736 uint64_t value)
200ac0ef 1737{
7ece99b1 1738 value &= pmu_counter_mask(env);
200ac0ef 1739 env->cp15.c9_pminten &= ~value;
f4efb4b2 1740 pmu_update_irq(env);
200ac0ef
PM
1741}
1742
c4241c7d
PM
1743static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1744 uint64_t value)
8641136c 1745{
a505d7fe
PM
1746 /* Note that even though the AArch64 view of this register has bits
1747 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1748 * architectural requirements for bits which are RES0 only in some
1749 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1750 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1751 */
855ea66d 1752 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1753}
1754
64e0e2de
EI
1755static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1756{
ea22747c
RH
1757 /* Begin with base v8.0 state. */
1758 uint32_t valid_mask = 0x3fff;
2fc0cc0e 1759 ARMCPU *cpu = env_archcpu(env);
ea22747c 1760
252e8c69 1761 if (ri->state == ARM_CP_STATE_AA64) {
10d0ef3e
MN
1762 if (arm_feature(env, ARM_FEATURE_AARCH64) &&
1763 !cpu_isar_feature(aa64_aa32_el1, cpu)) {
1764 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */
1765 }
ea22747c 1766 valid_mask &= ~SCR_NET;
252e8c69
RH
1767
1768 if (cpu_isar_feature(aa64_lor, cpu)) {
1769 valid_mask |= SCR_TLOR;
1770 }
1771 if (cpu_isar_feature(aa64_pauth, cpu)) {
1772 valid_mask |= SCR_API | SCR_APK;
1773 }
926c1b97
RDC
1774 if (cpu_isar_feature(aa64_sel2, cpu)) {
1775 valid_mask |= SCR_EEL2;
1776 }
8ddb300b
RH
1777 if (cpu_isar_feature(aa64_mte, cpu)) {
1778 valid_mask |= SCR_ATA;
1779 }
ea22747c
RH
1780 } else {
1781 valid_mask &= ~(SCR_RW | SCR_ST);
1782 }
64e0e2de
EI
1783
1784 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1785 valid_mask &= ~SCR_HCE;
1786
1787 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1788 * supported if EL2 exists. The bit is UNK/SBZP when
1789 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1790 * when EL2 is unavailable.
4eb27640 1791 * On ARMv8, this bit is always available.
64e0e2de 1792 */
4eb27640
GB
1793 if (arm_feature(env, ARM_FEATURE_V7) &&
1794 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1795 valid_mask &= ~SCR_SMD;
1796 }
1797 }
1798
1799 /* Clear all-context RES0 bits. */
1800 value &= valid_mask;
1801 raw_write(env, ri, value);
1802}
1803
10d0ef3e
MN
1804static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1805{
1806 /*
1807 * scr_write will set the RES1 bits on an AArch64-only CPU.
1808 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1809 */
1810 scr_write(env, ri, 0);
1811}
1812
630fcd4d
MZ
1813static CPAccessResult access_aa64_tid2(CPUARMState *env,
1814 const ARMCPRegInfo *ri,
1815 bool isread)
1816{
1817 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1818 return CP_ACCESS_TRAP_EL2;
1819 }
1820
1821 return CP_ACCESS_OK;
1822}
1823
c4241c7d 1824static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c 1825{
2fc0cc0e 1826 ARMCPU *cpu = env_archcpu(env);
b85a1fd6
FA
1827
1828 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1829 * bank
1830 */
1831 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1832 ri->secure & ARM_CP_SECSTATE_S);
1833
1834 return cpu->ccsidr[index];
776d4e5c
PM
1835}
1836
c4241c7d
PM
1837static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1838 uint64_t value)
776d4e5c 1839{
8d5c773e 1840 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1841}
1842
1090b9c6
PM
1843static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1844{
29a0af61 1845 CPUState *cs = env_cpu(env);
cc974d5c
RDC
1846 bool el1 = arm_current_el(env) == 1;
1847 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1090b9c6
PM
1848 uint64_t ret = 0;
1849
cc974d5c 1850 if (hcr_el2 & HCR_IMO) {
636540e9
PM
1851 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1852 ret |= CPSR_I;
1853 }
1854 } else {
1855 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1856 ret |= CPSR_I;
1857 }
1090b9c6 1858 }
636540e9 1859
cc974d5c 1860 if (hcr_el2 & HCR_FMO) {
636540e9
PM
1861 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1862 ret |= CPSR_F;
1863 }
1864 } else {
1865 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1866 ret |= CPSR_F;
1867 }
1090b9c6 1868 }
636540e9 1869
1090b9c6
PM
1870 /* External aborts are not possible in QEMU so A bit is always clear */
1871 return ret;
1872}
1873
93fbc983
MZ
1874static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1875 bool isread)
1876{
1877 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1878 return CP_ACCESS_TRAP_EL2;
1879 }
1880
1881 return CP_ACCESS_OK;
1882}
1883
1884static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1885 bool isread)
1886{
1887 if (arm_feature(env, ARM_FEATURE_V8)) {
1888 return access_aa64_tid1(env, ri, isread);
1889 }
1890
1891 return CP_ACCESS_OK;
1892}
1893
e9aa6c21 1894static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
1895 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1896 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1897 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
1898 /* Performance monitors are implementation defined in v7,
1899 * but with an ARM recommended set of registers, which we
ac689a2e 1900 * follow.
200ac0ef
PM
1901 *
1902 * Performance registers fall into three categories:
1903 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1904 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1905 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1906 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1907 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1908 */
1909 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 1910 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 1911 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1912 .writefn = pmcntenset_write,
1913 .accessfn = pmreg_access,
1914 .raw_writefn = raw_write },
8521466b
AF
1915 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1916 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1917 .access = PL0_RW, .accessfn = pmreg_access,
1918 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1919 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 1920 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
1921 .access = PL0_RW,
1922 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1923 .accessfn = pmreg_access,
1924 .writefn = pmcntenclr_write,
7a0e58fa 1925 .type = ARM_CP_ALIAS },
8521466b
AF
1926 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1927 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1928 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 1929 .type = ARM_CP_ALIAS,
8521466b
AF
1930 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1931 .writefn = pmcntenclr_write },
200ac0ef 1932 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
f4efb4b2 1933 .access = PL0_RW, .type = ARM_CP_IO,
e4e91a21 1934 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
1935 .accessfn = pmreg_access,
1936 .writefn = pmovsr_write,
1937 .raw_writefn = raw_write },
978364f1
AF
1938 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1939 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1940 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 1941 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
1942 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1943 .writefn = pmovsr_write,
1944 .raw_writefn = raw_write },
200ac0ef 1945 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
f4efb4b2
AL
1946 .access = PL0_W, .accessfn = pmreg_access_swinc,
1947 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d
AL
1948 .writefn = pmswinc_write },
1949 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1950 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
f4efb4b2
AL
1951 .access = PL0_W, .accessfn = pmreg_access_swinc,
1952 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d 1953 .writefn = pmswinc_write },
6b040780
WH
1954 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1955 .access = PL0_RW, .type = ARM_CP_ALIAS,
1956 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 1957 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
1958 .raw_writefn = raw_write},
1959 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1960 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 1961 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
1962 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1963 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 1964 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 1965 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
421c7ebd 1966 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 1967 .accessfn = pmreg_access_ccntr },
8521466b
AF
1968 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1969 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 1970 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b 1971 .type = ARM_CP_IO,
980ebe87
AL
1972 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1973 .readfn = pmccntr_read, .writefn = pmccntr_write,
1974 .raw_readfn = raw_read, .raw_writefn = raw_write, },
4b8afa1f
AL
1975 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1976 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
1977 .access = PL0_RW, .accessfn = pmreg_access,
1978 .type = ARM_CP_ALIAS | ARM_CP_IO,
1979 .resetvalue = 0, },
8521466b
AF
1980 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1981 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
980ebe87 1982 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
8521466b
AF
1983 .access = PL0_RW, .accessfn = pmreg_access,
1984 .type = ARM_CP_IO,
1985 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1986 .resetvalue = 0, },
200ac0ef 1987 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
5ecdd3e4
AL
1988 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1989 .accessfn = pmreg_access,
fdb86656
WH
1990 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1991 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1992 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
5ecdd3e4
AL
1993 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1994 .accessfn = pmreg_access,
fdb86656 1995 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
200ac0ef 1996 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
5ecdd3e4
AL
1997 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1998 .accessfn = pmreg_access_xevcntr,
1999 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2000 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2001 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2002 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2003 .accessfn = pmreg_access_xevcntr,
2004 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
200ac0ef 2005 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 2006 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 2007 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 2008 .resetvalue = 0,
d4e6df63 2009 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
2010 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2011 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 2012 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
2013 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2014 .resetvalue = 0,
2015 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 2016 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 2017 .access = PL1_RW, .accessfn = access_tpm,
b7d793ad 2018 .type = ARM_CP_ALIAS | ARM_CP_IO,
e6ec5457 2019 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 2020 .resetvalue = 0,
d4e6df63 2021 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
2022 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2023 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2024 .access = PL1_RW, .accessfn = access_tpm,
2025 .type = ARM_CP_IO,
2026 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2027 .writefn = pmintenset_write, .raw_writefn = raw_write,
2028 .resetvalue = 0x0 },
200ac0ef 2029 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
fc5f6856 2030 .access = PL1_RW, .accessfn = access_tpm,
887c0f15 2031 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
200ac0ef 2032 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 2033 .writefn = pmintenclr_write, },
978364f1
AF
2034 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2035 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
fc5f6856 2036 .access = PL1_RW, .accessfn = access_tpm,
887c0f15 2037 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
978364f1
AF
2038 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2039 .writefn = pmintenclr_write },
7da845b0
PM
2040 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2041 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
630fcd4d
MZ
2042 .access = PL1_R,
2043 .accessfn = access_aa64_tid2,
2044 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
2045 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2046 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
630fcd4d
MZ
2047 .access = PL1_RW,
2048 .accessfn = access_aa64_tid2,
2049 .writefn = csselr_write, .resetvalue = 0,
b85a1fd6
FA
2050 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2051 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
2052 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2053 * just RAZ for all cores:
2054 */
0ff644a7
PM
2055 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2056 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
93fbc983
MZ
2057 .access = PL1_R, .type = ARM_CP_CONST,
2058 .accessfn = access_aa64_tid1,
2059 .resetvalue = 0 },
f32cdad5
PM
2060 /* Auxiliary fault status registers: these also are IMPDEF, and we
2061 * choose to RAZ/WI for all cores.
2062 */
2063 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2064 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
84929218
RH
2065 .access = PL1_RW, .accessfn = access_tvm_trvm,
2066 .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
2067 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2068 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
84929218
RH
2069 .access = PL1_RW, .accessfn = access_tvm_trvm,
2070 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
2071 /* MAIR can just read-as-written because we don't implement caches
2072 * and so don't need to care about memory attributes.
2073 */
2074 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2075 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
84929218
RH
2076 .access = PL1_RW, .accessfn = access_tvm_trvm,
2077 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 2078 .resetvalue = 0 },
4cfb8ad8
PM
2079 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2080 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2081 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2082 .resetvalue = 0 },
b0fe2427
PM
2083 /* For non-long-descriptor page tables these are PRRR and NMRR;
2084 * regardless they still act as reads-as-written for QEMU.
b0fe2427 2085 */
1281f8e3 2086 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
2087 * allows them to assign the correct fieldoffset based on the endianness
2088 * handled in the field definitions.
2089 */
a903c449 2090 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
84929218
RH
2091 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2092 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2093 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2094 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 2095 .resetfn = arm_cp_reset_ignore },
a903c449 2096 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
84929218
RH
2097 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2098 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2099 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2100 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 2101 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
2102 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2103 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 2104 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
2105 /* 32 bit ITLB invalidates */
2106 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
30881b73
RH
2107 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2108 .writefn = tlbiall_write },
995939a6 2109 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
30881b73
RH
2110 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2111 .writefn = tlbimva_write },
995939a6 2112 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .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 DTLB invalidates */
2116 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
30881b73
RH
2117 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2118 .writefn = tlbiall_write },
995939a6 2119 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
30881b73
RH
2120 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2121 .writefn = tlbimva_write },
995939a6 2122 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
30881b73
RH
2123 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2124 .writefn = tlbiasid_write },
995939a6
PM
2125 /* 32 bit TLB invalidates */
2126 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73
RH
2127 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2128 .writefn = tlbiall_write },
995939a6 2129 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73
RH
2130 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2131 .writefn = tlbimva_write },
995939a6 2132 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73
RH
2133 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2134 .writefn = tlbiasid_write },
995939a6 2135 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73
RH
2136 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2137 .writefn = tlbimvaa_write },
995939a6
PM
2138 REGINFO_SENTINEL
2139};
2140
2141static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2142 /* 32 bit TLB invalidates, Inner Shareable */
2143 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
30881b73
RH
2144 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2145 .writefn = tlbiall_is_write },
995939a6 2146 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
30881b73
RH
2147 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2148 .writefn = tlbimva_is_write },
995939a6 2149 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
30881b73 2150 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 2151 .writefn = tlbiasid_is_write },
995939a6 2152 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
30881b73 2153 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 2154 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
2155 REGINFO_SENTINEL
2156};
2157
327dd510
AL
2158static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2159 /* PMOVSSET is not implemented in v7 before v7ve */
2160 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2161 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2162 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2163 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2164 .writefn = pmovsset_write,
2165 .raw_writefn = raw_write },
2166 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2167 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2168 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2169 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2170 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2171 .writefn = pmovsset_write,
2172 .raw_writefn = raw_write },
2173 REGINFO_SENTINEL
2174};
2175
c4241c7d
PM
2176static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2177 uint64_t value)
c326b979
PM
2178{
2179 value &= 1;
2180 env->teecr = value;
c326b979
PM
2181}
2182
cc7613bf
PM
2183static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2184 bool isread)
2185{
2186 /*
2187 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2188 * at all, so we don't need to check whether we're v8A.
2189 */
2190 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2191 (env->cp15.hstr_el2 & HSTR_TTEE)) {
2192 return CP_ACCESS_TRAP_EL2;
2193 }
2194 return CP_ACCESS_OK;
2195}
2196
3f208fd7
PM
2197static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2198 bool isread)
c326b979 2199{
dcbff19b 2200 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 2201 return CP_ACCESS_TRAP;
c326b979 2202 }
cc7613bf 2203 return teecr_access(env, ri, isread);
c326b979
PM
2204}
2205
2206static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2207 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2208 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2209 .resetvalue = 0,
cc7613bf 2210 .writefn = teecr_write, .accessfn = teecr_access },
c326b979
PM
2211 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2212 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 2213 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
2214 REGINFO_SENTINEL
2215};
2216
4d31c596 2217static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
2218 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2219 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2220 .access = PL0_RW,
54bf36ed 2221 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
2222 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2223 .access = PL0_RW,
54bf36ed
FA
2224 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2225 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
2226 .resetfn = arm_cp_reset_ignore },
2227 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2228 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2229 .access = PL0_R|PL1_W,
54bf36ed
FA
2230 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2231 .resetvalue = 0},
4d31c596
PM
2232 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2233 .access = PL0_R|PL1_W,
54bf36ed
FA
2234 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2235 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 2236 .resetfn = arm_cp_reset_ignore },
54bf36ed 2237 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 2238 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 2239 .access = PL1_RW,
54bf36ed
FA
2240 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2241 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2242 .access = PL1_RW,
2243 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2244 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2245 .resetvalue = 0 },
4d31c596
PM
2246 REGINFO_SENTINEL
2247};
2248
55d284af
PM
2249#ifndef CONFIG_USER_ONLY
2250
3f208fd7
PM
2251static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2252 bool isread)
00108f2d 2253{
75502672
PM
2254 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2255 * Writable only at the highest implemented exception level.
2256 */
2257 int el = arm_current_el(env);
5bc84371
RH
2258 uint64_t hcr;
2259 uint32_t cntkctl;
75502672
PM
2260
2261 switch (el) {
2262 case 0:
5bc84371
RH
2263 hcr = arm_hcr_el2_eff(env);
2264 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2265 cntkctl = env->cp15.cnthctl_el2;
2266 } else {
2267 cntkctl = env->cp15.c14_cntkctl;
2268 }
2269 if (!extract32(cntkctl, 0, 2)) {
75502672
PM
2270 return CP_ACCESS_TRAP;
2271 }
2272 break;
2273 case 1:
2274 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2275 arm_is_secure_below_el3(env)) {
2276 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2277 return CP_ACCESS_TRAP_UNCATEGORIZED;
2278 }
2279 break;
2280 case 2:
2281 case 3:
2282 break;
00108f2d 2283 }
75502672
PM
2284
2285 if (!isread && el < arm_highest_el(env)) {
2286 return CP_ACCESS_TRAP_UNCATEGORIZED;
2287 }
2288
00108f2d
PM
2289 return CP_ACCESS_OK;
2290}
2291
3f208fd7
PM
2292static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2293 bool isread)
00108f2d 2294{
0b6440af 2295 unsigned int cur_el = arm_current_el(env);
e6ef0169 2296 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2297 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2298
5bc84371
RH
2299 switch (cur_el) {
2300 case 0:
2301 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2302 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2303 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2304 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2305 }
0b6440af 2306
5bc84371
RH
2307 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2308 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2309 return CP_ACCESS_TRAP;
2310 }
2311
2312 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2313 if (hcr & HCR_E2H) {
2314 if (timeridx == GTIMER_PHYS &&
2315 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2316 return CP_ACCESS_TRAP_EL2;
2317 }
2318 } else {
2319 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
e6ef0169 2320 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2321 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2322 return CP_ACCESS_TRAP_EL2;
2323 }
2324 }
2325 break;
2326
2327 case 1:
2328 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
e6ef0169 2329 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2330 (hcr & HCR_E2H
2331 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2332 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2333 return CP_ACCESS_TRAP_EL2;
2334 }
2335 break;
0b6440af 2336 }
00108f2d
PM
2337 return CP_ACCESS_OK;
2338}
2339
3f208fd7
PM
2340static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2341 bool isread)
00108f2d 2342{
0b6440af 2343 unsigned int cur_el = arm_current_el(env);
e6ef0169 2344 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2345 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2346
5bc84371
RH
2347 switch (cur_el) {
2348 case 0:
2349 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2350 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2351 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2352 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2353 }
0b6440af 2354
5bc84371
RH
2355 /*
2356 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2357 * EL0 if EL0[PV]TEN is zero.
2358 */
2359 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2360 return CP_ACCESS_TRAP;
2361 }
2362 /* fall through */
2363
2364 case 1:
e6ef0169 2365 if (has_el2 && timeridx == GTIMER_PHYS) {
5bc84371
RH
2366 if (hcr & HCR_E2H) {
2367 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2368 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2369 return CP_ACCESS_TRAP_EL2;
2370 }
2371 } else {
2372 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2373 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2374 return CP_ACCESS_TRAP_EL2;
2375 }
2376 }
2377 }
2378 break;
0b6440af 2379 }
00108f2d
PM
2380 return CP_ACCESS_OK;
2381}
2382
2383static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
2384 const ARMCPRegInfo *ri,
2385 bool isread)
00108f2d 2386{
3f208fd7 2387 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2388}
2389
2390static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
2391 const ARMCPRegInfo *ri,
2392 bool isread)
00108f2d 2393{
3f208fd7 2394 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2395}
2396
3f208fd7
PM
2397static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2398 bool isread)
00108f2d 2399{
3f208fd7 2400 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2401}
2402
3f208fd7
PM
2403static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2404 bool isread)
00108f2d 2405{
3f208fd7 2406 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2407}
2408
b4d3978c 2409static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
2410 const ARMCPRegInfo *ri,
2411 bool isread)
b4d3978c
PM
2412{
2413 /* The AArch64 register view of the secure physical timer is
2414 * always accessible from EL3, and configurably accessible from
2415 * Secure EL1.
2416 */
2417 switch (arm_current_el(env)) {
2418 case 1:
2419 if (!arm_is_secure(env)) {
2420 return CP_ACCESS_TRAP;
2421 }
2422 if (!(env->cp15.scr_el3 & SCR_ST)) {
2423 return CP_ACCESS_TRAP_EL3;
2424 }
2425 return CP_ACCESS_OK;
2426 case 0:
2427 case 2:
2428 return CP_ACCESS_TRAP;
2429 case 3:
2430 return CP_ACCESS_OK;
2431 default:
2432 g_assert_not_reached();
2433 }
2434}
2435
55d284af
PM
2436static uint64_t gt_get_countervalue(CPUARMState *env)
2437{
7def8754
AJ
2438 ARMCPU *cpu = env_archcpu(env);
2439
2440 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
55d284af
PM
2441}
2442
2443static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2444{
2445 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2446
2447 if (gt->ctl & 1) {
2448 /* Timer enabled: calculate and set current ISTATUS, irq, and
2449 * reset timer to when ISTATUS next has to change
2450 */
edac4d8a
EI
2451 uint64_t offset = timeridx == GTIMER_VIRT ?
2452 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
2453 uint64_t count = gt_get_countervalue(&cpu->env);
2454 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 2455 int istatus = count - offset >= gt->cval;
55d284af 2456 uint64_t nexttick;
194cbc49 2457 int irqstate;
55d284af
PM
2458
2459 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
2460
2461 irqstate = (istatus && !(gt->ctl & 2));
2462 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2463
55d284af
PM
2464 if (istatus) {
2465 /* Next transition is when count rolls back over to zero */
2466 nexttick = UINT64_MAX;
2467 } else {
2468 /* Next transition is when we hit cval */
edac4d8a 2469 nexttick = gt->cval + offset;
55d284af
PM
2470 }
2471 /* Note that the desired next expiry time might be beyond the
2472 * signed-64-bit range of a QEMUTimer -- in this case we just
2473 * set the timer for as far in the future as possible. When the
2474 * timer expires we will reset the timer for any remaining period.
2475 */
7def8754 2476 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
4a0245b6
AJ
2477 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2478 } else {
2479 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af 2480 }
194cbc49 2481 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
2482 } else {
2483 /* Timer disabled: ISTATUS and timer output always clear */
2484 gt->ctl &= ~4;
2485 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 2486 timer_del(cpu->gt_timer[timeridx]);
194cbc49 2487 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
2488 }
2489}
2490
0e3eca4c
EI
2491static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2492 int timeridx)
55d284af 2493{
2fc0cc0e 2494 ARMCPU *cpu = env_archcpu(env);
55d284af 2495
bc72ad67 2496 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
2497}
2498
c4241c7d 2499static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 2500{
c4241c7d 2501 return gt_get_countervalue(env);
55d284af
PM
2502}
2503
53d1f856
RH
2504static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2505{
2506 uint64_t hcr;
2507
2508 switch (arm_current_el(env)) {
2509 case 2:
2510 hcr = arm_hcr_el2_eff(env);
2511 if (hcr & HCR_E2H) {
2512 return 0;
2513 }
2514 break;
2515 case 0:
2516 hcr = arm_hcr_el2_eff(env);
2517 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2518 return 0;
2519 }
2520 break;
2521 }
2522
2523 return env->cp15.cntvoff_el2;
2524}
2525
edac4d8a
EI
2526static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2527{
53d1f856 2528 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
edac4d8a
EI
2529}
2530
c4241c7d 2531static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2532 int timeridx,
c4241c7d 2533 uint64_t value)
55d284af 2534{
194cbc49 2535 trace_arm_gt_cval_write(timeridx, value);
55d284af 2536 env->cp15.c14_timer[timeridx].cval = value;
2fc0cc0e 2537 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af 2538}
c4241c7d 2539
0e3eca4c
EI
2540static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2541 int timeridx)
55d284af 2542{
53d1f856
RH
2543 uint64_t offset = 0;
2544
2545 switch (timeridx) {
2546 case GTIMER_VIRT:
8c94b071 2547 case GTIMER_HYPVIRT:
53d1f856
RH
2548 offset = gt_virt_cnt_offset(env);
2549 break;
2550 }
55d284af 2551
c4241c7d 2552 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 2553 (gt_get_countervalue(env) - offset));
55d284af
PM
2554}
2555
c4241c7d 2556static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2557 int timeridx,
c4241c7d 2558 uint64_t value)
55d284af 2559{
53d1f856
RH
2560 uint64_t offset = 0;
2561
2562 switch (timeridx) {
2563 case GTIMER_VIRT:
8c94b071 2564 case GTIMER_HYPVIRT:
53d1f856
RH
2565 offset = gt_virt_cnt_offset(env);
2566 break;
2567 }
55d284af 2568
194cbc49 2569 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 2570 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 2571 sextract64(value, 0, 32);
2fc0cc0e 2572 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af
PM
2573}
2574
c4241c7d 2575static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2576 int timeridx,
c4241c7d 2577 uint64_t value)
55d284af 2578{
2fc0cc0e 2579 ARMCPU *cpu = env_archcpu(env);
55d284af
PM
2580 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2581
194cbc49 2582 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 2583 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
2584 if ((oldval ^ value) & 1) {
2585 /* Enable toggled */
2586 gt_recalc_timer(cpu, timeridx);
d3afacc7 2587 } else if ((oldval ^ value) & 2) {
55d284af
PM
2588 /* IMASK toggled: don't need to recalculate,
2589 * just set the interrupt line based on ISTATUS
2590 */
194cbc49
PM
2591 int irqstate = (oldval & 4) && !(value & 2);
2592
2593 trace_arm_gt_imask_toggle(timeridx, irqstate);
2594 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 2595 }
55d284af
PM
2596}
2597
0e3eca4c
EI
2598static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2599{
2600 gt_timer_reset(env, ri, GTIMER_PHYS);
2601}
2602
2603static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2604 uint64_t value)
2605{
2606 gt_cval_write(env, ri, GTIMER_PHYS, value);
2607}
2608
2609static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2610{
2611 return gt_tval_read(env, ri, GTIMER_PHYS);
2612}
2613
2614static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2615 uint64_t value)
2616{
2617 gt_tval_write(env, ri, GTIMER_PHYS, value);
2618}
2619
2620static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2621 uint64_t value)
2622{
2623 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2624}
2625
bb5972e4
RH
2626static int gt_phys_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_HYP;
2636 default:
2637 return GTIMER_PHYS;
2638 }
2639}
2640
2641static int gt_virt_redir_timeridx(CPUARMState *env)
2642{
2643 switch (arm_mmu_idx(env)) {
2644 case ARMMMUIdx_E20_0:
2645 case ARMMMUIdx_E20_2:
452ef8cb 2646 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
2647 case ARMMMUIdx_SE20_0:
2648 case ARMMMUIdx_SE20_2:
2649 case ARMMMUIdx_SE20_2_PAN:
bb5972e4
RH
2650 return GTIMER_HYPVIRT;
2651 default:
2652 return GTIMER_VIRT;
2653 }
2654}
2655
2656static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2657 const ARMCPRegInfo *ri)
2658{
2659 int timeridx = gt_phys_redir_timeridx(env);
2660 return env->cp15.c14_timer[timeridx].cval;
2661}
2662
2663static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2664 uint64_t value)
2665{
2666 int timeridx = gt_phys_redir_timeridx(env);
2667 gt_cval_write(env, ri, timeridx, value);
2668}
2669
2670static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2671 const ARMCPRegInfo *ri)
2672{
2673 int timeridx = gt_phys_redir_timeridx(env);
2674 return gt_tval_read(env, ri, timeridx);
2675}
2676
2677static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2678 uint64_t value)
2679{
2680 int timeridx = gt_phys_redir_timeridx(env);
2681 gt_tval_write(env, ri, timeridx, value);
2682}
2683
2684static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2685 const ARMCPRegInfo *ri)
2686{
2687 int timeridx = gt_phys_redir_timeridx(env);
2688 return env->cp15.c14_timer[timeridx].ctl;
2689}
2690
2691static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2692 uint64_t value)
2693{
2694 int timeridx = gt_phys_redir_timeridx(env);
2695 gt_ctl_write(env, ri, timeridx, value);
2696}
2697
0e3eca4c
EI
2698static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2699{
2700 gt_timer_reset(env, ri, GTIMER_VIRT);
2701}
2702
2703static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2704 uint64_t value)
2705{
2706 gt_cval_write(env, ri, GTIMER_VIRT, value);
2707}
2708
2709static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2710{
2711 return gt_tval_read(env, ri, GTIMER_VIRT);
2712}
2713
2714static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2715 uint64_t value)
2716{
2717 gt_tval_write(env, ri, GTIMER_VIRT, value);
2718}
2719
2720static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2721 uint64_t value)
2722{
2723 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2724}
2725
edac4d8a
EI
2726static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2727 uint64_t value)
2728{
2fc0cc0e 2729 ARMCPU *cpu = env_archcpu(env);
edac4d8a 2730
194cbc49 2731 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
2732 raw_write(env, ri, value);
2733 gt_recalc_timer(cpu, GTIMER_VIRT);
2734}
2735
bb5972e4
RH
2736static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2737 const ARMCPRegInfo *ri)
2738{
2739 int timeridx = gt_virt_redir_timeridx(env);
2740 return env->cp15.c14_timer[timeridx].cval;
2741}
2742
2743static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2744 uint64_t value)
2745{
2746 int timeridx = gt_virt_redir_timeridx(env);
2747 gt_cval_write(env, ri, timeridx, value);
2748}
2749
2750static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2751 const ARMCPRegInfo *ri)
2752{
2753 int timeridx = gt_virt_redir_timeridx(env);
2754 return gt_tval_read(env, ri, timeridx);
2755}
2756
2757static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2758 uint64_t value)
2759{
2760 int timeridx = gt_virt_redir_timeridx(env);
2761 gt_tval_write(env, ri, timeridx, value);
2762}
2763
2764static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2765 const ARMCPRegInfo *ri)
2766{
2767 int timeridx = gt_virt_redir_timeridx(env);
2768 return env->cp15.c14_timer[timeridx].ctl;
2769}
2770
2771static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2772 uint64_t value)
2773{
2774 int timeridx = gt_virt_redir_timeridx(env);
2775 gt_ctl_write(env, ri, timeridx, value);
2776}
2777
b0e66d95
EI
2778static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2779{
2780 gt_timer_reset(env, ri, GTIMER_HYP);
2781}
2782
2783static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2784 uint64_t value)
2785{
2786 gt_cval_write(env, ri, GTIMER_HYP, value);
2787}
2788
2789static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2790{
2791 return gt_tval_read(env, ri, GTIMER_HYP);
2792}
2793
2794static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2795 uint64_t value)
2796{
2797 gt_tval_write(env, ri, GTIMER_HYP, value);
2798}
2799
2800static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2801 uint64_t value)
2802{
2803 gt_ctl_write(env, ri, GTIMER_HYP, value);
2804}
2805
b4d3978c
PM
2806static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2807{
2808 gt_timer_reset(env, ri, GTIMER_SEC);
2809}
2810
2811static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2812 uint64_t value)
2813{
2814 gt_cval_write(env, ri, GTIMER_SEC, value);
2815}
2816
2817static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2818{
2819 return gt_tval_read(env, ri, GTIMER_SEC);
2820}
2821
2822static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2823 uint64_t value)
2824{
2825 gt_tval_write(env, ri, GTIMER_SEC, value);
2826}
2827
2828static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2829 uint64_t value)
2830{
2831 gt_ctl_write(env, ri, GTIMER_SEC, value);
2832}
2833
8c94b071
RH
2834static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2835{
2836 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2837}
2838
2839static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2840 uint64_t value)
2841{
2842 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2843}
2844
2845static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2846{
2847 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2848}
2849
2850static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2851 uint64_t value)
2852{
2853 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2854}
2855
2856static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2857 uint64_t value)
2858{
2859 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2860}
2861
55d284af
PM
2862void arm_gt_ptimer_cb(void *opaque)
2863{
2864 ARMCPU *cpu = opaque;
2865
2866 gt_recalc_timer(cpu, GTIMER_PHYS);
2867}
2868
2869void arm_gt_vtimer_cb(void *opaque)
2870{
2871 ARMCPU *cpu = opaque;
2872
2873 gt_recalc_timer(cpu, GTIMER_VIRT);
2874}
2875
b0e66d95
EI
2876void arm_gt_htimer_cb(void *opaque)
2877{
2878 ARMCPU *cpu = opaque;
2879
2880 gt_recalc_timer(cpu, GTIMER_HYP);
2881}
2882
b4d3978c
PM
2883void arm_gt_stimer_cb(void *opaque)
2884{
2885 ARMCPU *cpu = opaque;
2886
2887 gt_recalc_timer(cpu, GTIMER_SEC);
2888}
2889
8c94b071
RH
2890void arm_gt_hvtimer_cb(void *opaque)
2891{
2892 ARMCPU *cpu = opaque;
2893
2894 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2895}
2896
96eec6b2
AJ
2897static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2898{
2899 ARMCPU *cpu = env_archcpu(env);
2900
2901 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2902}
2903
55d284af
PM
2904static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2905 /* Note that CNTFRQ is purely reads-as-written for the benefit
2906 * of software; writing it doesn't actually change the timer frequency.
2907 * Our reset value matches the fixed frequency we implement the timer at.
2908 */
2909 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2910 .type = ARM_CP_ALIAS,
a7adc4b7
PM
2911 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2912 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
2913 },
2914 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2915 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2916 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af 2917 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
96eec6b2 2918 .resetfn = arm_gt_cntfrq_reset,
55d284af
PM
2919 },
2920 /* overall control: mostly access permissions */
a7adc4b7
PM
2921 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2922 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
2923 .access = PL1_RW,
2924 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2925 .resetvalue = 0,
2926 },
2927 /* per-timer control */
2928 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 2929 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 2930 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
2931 .accessfn = gt_ptimer_access,
2932 .fieldoffset = offsetoflow32(CPUARMState,
2933 cp15.c14_timer[GTIMER_PHYS].ctl),
bb5972e4
RH
2934 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2935 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7 2936 },
9c513e78 2937 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
2938 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2939 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 2940 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
9ff9dd3c
PM
2941 .accessfn = gt_ptimer_access,
2942 .fieldoffset = offsetoflow32(CPUARMState,
2943 cp15.c14_timer[GTIMER_SEC].ctl),
2944 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2945 },
a7adc4b7
PM
2946 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2947 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
daf1dc5f 2948 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 2949 .accessfn = gt_ptimer_access,
55d284af
PM
2950 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2951 .resetvalue = 0,
bb5972e4
RH
2952 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2953 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2954 },
2955 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
daf1dc5f 2956 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
2957 .accessfn = gt_vtimer_access,
2958 .fieldoffset = offsetoflow32(CPUARMState,
2959 cp15.c14_timer[GTIMER_VIRT].ctl),
bb5972e4
RH
2960 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2961 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
2962 },
2963 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2964 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
daf1dc5f 2965 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 2966 .accessfn = gt_vtimer_access,
55d284af
PM
2967 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2968 .resetvalue = 0,
bb5972e4
RH
2969 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2970 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2971 },
2972 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2973 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 2974 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 2975 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 2976 .accessfn = gt_ptimer_access,
bb5972e4 2977 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
55d284af 2978 },
9c513e78 2979 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
2980 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2981 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 2982 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
9ff9dd3c
PM
2983 .accessfn = gt_ptimer_access,
2984 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2985 },
a7adc4b7
PM
2986 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2987 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
daf1dc5f 2988 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 2989 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
bb5972e4 2990 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
a7adc4b7 2991 },
55d284af 2992 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
daf1dc5f 2993 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 2994 .accessfn = gt_vtimer_access,
bb5972e4 2995 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
55d284af 2996 },
a7adc4b7
PM
2997 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2998 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
daf1dc5f 2999 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3000 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
bb5972e4 3001 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
a7adc4b7 3002 },
55d284af
PM
3003 /* The counter itself */
3004 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 3005 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3006 .accessfn = gt_pct_access,
a7adc4b7
PM
3007 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3008 },
3009 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3010 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 3011 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3012 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
3013 },
3014 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 3015 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3016 .accessfn = gt_vct_access,
edac4d8a 3017 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
3018 },
3019 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3020 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 3021 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3022 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
3023 },
3024 /* Comparison value, indicating when the timer goes off */
3025 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3026 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3027 .access = PL0_RW,
7a0e58fa 3028 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3029 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 3030 .accessfn = gt_ptimer_access,
bb5972e4
RH
3031 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3032 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7 3033 },
9c513e78 3034 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3035 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3036 .access = PL0_RW,
9ff9dd3c
PM
3037 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3038 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3039 .accessfn = gt_ptimer_access,
3040 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3041 },
a7adc4b7
PM
3042 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3043 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
daf1dc5f 3044 .access = PL0_RW,
a7adc4b7
PM
3045 .type = ARM_CP_IO,
3046 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 3047 .resetvalue = 0, .accessfn = gt_ptimer_access,
bb5972e4
RH
3048 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3049 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
55d284af
PM
3050 },
3051 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
daf1dc5f 3052 .access = PL0_RW,
7a0e58fa 3053 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3054 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 3055 .accessfn = gt_vtimer_access,
bb5972e4
RH
3056 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3057 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
3058 },
3059 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3060 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
daf1dc5f 3061 .access = PL0_RW,
a7adc4b7
PM
3062 .type = ARM_CP_IO,
3063 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3064 .resetvalue = 0, .accessfn = gt_vtimer_access,
bb5972e4
RH
3065 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3066 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
55d284af 3067 },
b4d3978c
PM
3068 /* Secure timer -- this is actually restricted to only EL3
3069 * and configurably Secure-EL1 via the accessfn.
3070 */
3071 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3072 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3073 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3074 .accessfn = gt_stimer_access,
3075 .readfn = gt_sec_tval_read,
3076 .writefn = gt_sec_tval_write,
3077 .resetfn = gt_sec_timer_reset,
3078 },
3079 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3080 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3081 .type = ARM_CP_IO, .access = PL1_RW,
3082 .accessfn = gt_stimer_access,
3083 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3084 .resetvalue = 0,
3085 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3086 },
3087 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3088 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3089 .type = ARM_CP_IO, .access = PL1_RW,
3090 .accessfn = gt_stimer_access,
3091 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3092 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3093 },
55d284af
PM
3094 REGINFO_SENTINEL
3095};
3096
bb5972e4
RH
3097static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3098 bool isread)
3099{
3100 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3101 return CP_ACCESS_TRAP;
3102 }
3103 return CP_ACCESS_OK;
3104}
3105
55d284af 3106#else
26c4a83b
AB
3107
3108/* In user-mode most of the generic timer registers are inaccessible
3109 * however modern kernels (4.12+) allow access to cntvct_el0
55d284af 3110 */
26c4a83b
AB
3111
3112static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3113{
7def8754
AJ
3114 ARMCPU *cpu = env_archcpu(env);
3115
26c4a83b
AB
3116 /* Currently we have no support for QEMUTimer in linux-user so we
3117 * can't call gt_get_countervalue(env), instead we directly
3118 * call the lower level functions.
3119 */
7def8754 3120 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
26c4a83b
AB
3121}
3122
6cc7a3ae 3123static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
26c4a83b
AB
3124 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3125 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3126 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3127 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3128 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3129 },
3130 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3131 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3132 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3133 .readfn = gt_virt_cnt_read,
3134 },
6cc7a3ae
PM
3135 REGINFO_SENTINEL
3136};
3137
55d284af
PM
3138#endif
3139
c4241c7d 3140static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 3141{
891a2fe7 3142 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 3143 raw_write(env, ri, value);
891a2fe7 3144 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 3145 raw_write(env, ri, value & 0xfffff6ff);
4a501606 3146 } else {
8d5c773e 3147 raw_write(env, ri, value & 0xfffff1ff);
4a501606 3148 }
4a501606
PM
3149}
3150
3151#ifndef CONFIG_USER_ONLY
3152/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 3153
3f208fd7
PM
3154static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3155 bool isread)
92611c00
PM
3156{
3157 if (ri->opc2 & 4) {
926c1b97 3158 /* The ATS12NSO* operations must trap to EL3 or EL2 if executed in
87562e4f
PM
3159 * Secure EL1 (which can only happen if EL3 is AArch64).
3160 * They are simply UNDEF if executed from NS EL1.
3161 * They function normally from EL2 or EL3.
92611c00 3162 */
87562e4f
PM
3163 if (arm_current_el(env) == 1) {
3164 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
3165 if (env->cp15.scr_el3 & SCR_EEL2) {
3166 return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3167 }
87562e4f
PM
3168 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3169 }
3170 return CP_ACCESS_TRAP_UNCATEGORIZED;
3171 }
92611c00
PM
3172 }
3173 return CP_ACCESS_OK;
3174}
3175
9fb005b0 3176#ifdef CONFIG_TCG
060e8a48 3177static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
03ae85f8 3178 MMUAccessType access_type, ARMMMUIdx mmu_idx)
4a501606 3179{
a8170e5e 3180 hwaddr phys_addr;
4a501606
PM
3181 target_ulong page_size;
3182 int prot;
b7cc4e82 3183 bool ret;
01c097f7 3184 uint64_t par64;
1313e2d7 3185 bool format64 = false;
8bf5b6a9 3186 MemTxAttrs attrs = {};
e14b5a23 3187 ARMMMUFaultInfo fi = {};
5b2d261d 3188 ARMCacheAttrs cacheattrs = {};
4a501606 3189
5b2d261d 3190 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
bc52bfeb 3191 &prot, &page_size, &fi, &cacheattrs);
1313e2d7 3192
0710b2fa
PM
3193 if (ret) {
3194 /*
3195 * Some kinds of translation fault must cause exceptions rather
3196 * than being reported in the PAR.
3197 */
3198 int current_el = arm_current_el(env);
3199 int target_el;
3200 uint32_t syn, fsr, fsc;
3201 bool take_exc = false;
3202
b1a10c86 3203 if (fi.s1ptw && current_el == 1
fee7aa46 3204 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
0710b2fa
PM
3205 /*
3206 * Synchronous stage 2 fault on an access made as part of the
3207 * translation table walk for AT S1E0* or AT S1E1* insn
3208 * executed from NS EL1. If this is a synchronous external abort
3209 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3210 * to EL3. Otherwise the fault is taken as an exception to EL2,
3211 * and HPFAR_EL2 holds the faulting IPA.
3212 */
3213 if (fi.type == ARMFault_SyncExternalOnWalk &&
3214 (env->cp15.scr_el3 & SCR_EA)) {
3215 target_el = 3;
3216 } else {
3217 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
9861248f
RDC
3218 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3219 env->cp15.hpfar_el2 |= HPFAR_NS;
3220 }
0710b2fa
PM
3221 target_el = 2;
3222 }
3223 take_exc = true;
3224 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3225 /*
3226 * Synchronous external aborts during a translation table walk
3227 * are taken as Data Abort exceptions.
3228 */
3229 if (fi.stage2) {
3230 if (current_el == 3) {
3231 target_el = 3;
3232 } else {
3233 target_el = 2;
3234 }
3235 } else {
3236 target_el = exception_target_el(env);
3237 }
3238 take_exc = true;
3239 }
3240
3241 if (take_exc) {
3242 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3243 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3244 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3245 fsr = arm_fi_to_lfsc(&fi);
3246 fsc = extract32(fsr, 0, 6);
3247 } else {
3248 fsr = arm_fi_to_sfsc(&fi);
3249 fsc = 0x3f;
3250 }
3251 /*
3252 * Report exception with ESR indicating a fault due to a
3253 * translation table walk for a cache maintenance instruction.
3254 */
e24fd076 3255 syn = syn_data_abort_no_iss(current_el == target_el, 0,
0710b2fa
PM
3256 fi.ea, 1, fi.s1ptw, 1, fsc);
3257 env->exception.vaddress = value;
3258 env->exception.fsr = fsr;
3259 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3260 }
3261 }
3262
1313e2d7
EI
3263 if (is_a64(env)) {
3264 format64 = true;
3265 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3266 /*
3267 * ATS1Cxx:
3268 * * TTBCR.EAE determines whether the result is returned using the
3269 * 32-bit or the 64-bit PAR format
3270 * * Instructions executed in Hyp mode always use the 64bit format
3271 *
3272 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3273 * * The Non-secure TTBCR.EAE bit is set to 1
3274 * * The implementation includes EL2, and the value of HCR.VM is 1
3275 *
9d1bab33
PM
3276 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3277 *
23463e0e 3278 * ATS1Hx always uses the 64bit format.
1313e2d7
EI
3279 */
3280 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3281
3282 if (arm_feature(env, ARM_FEATURE_EL2)) {
452ef8cb
RH
3283 if (mmu_idx == ARMMMUIdx_E10_0 ||
3284 mmu_idx == ARMMMUIdx_E10_1 ||
3285 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9d1bab33 3286 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
1313e2d7
EI
3287 } else {
3288 format64 |= arm_current_el(env) == 2;
3289 }
3290 }
3291 }
3292
3293 if (format64) {
5efe9ed4 3294 /* Create a 64-bit PAR */
01c097f7 3295 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 3296 if (!ret) {
702a9357 3297 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
3298 if (!attrs.secure) {
3299 par64 |= (1 << 9); /* NS */
3300 }
5b2d261d
AB
3301 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3302 par64 |= cacheattrs.shareability << 7; /* SH */
4a501606 3303 } else {
5efe9ed4
PM
3304 uint32_t fsr = arm_fi_to_lfsc(&fi);
3305
702a9357 3306 par64 |= 1; /* F */
b7cc4e82 3307 par64 |= (fsr & 0x3f) << 1; /* FS */
0f7b791b
PM
3308 if (fi.stage2) {
3309 par64 |= (1 << 9); /* S */
3310 }
3311 if (fi.s1ptw) {
3312 par64 |= (1 << 8); /* PTW */
3313 }
4a501606
PM
3314 }
3315 } else {
b7cc4e82 3316 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
3317 * translation table format (with WnR always clear).
3318 * Convert it to a 32-bit PAR.
3319 */
b7cc4e82 3320 if (!ret) {
702a9357
PM
3321 /* We do not set any attribute bits in the PAR */
3322 if (page_size == (1 << 24)
3323 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 3324 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 3325 } else {
01c097f7 3326 par64 = phys_addr & 0xfffff000;
702a9357 3327 }
8bf5b6a9
PM
3328 if (!attrs.secure) {
3329 par64 |= (1 << 9); /* NS */
3330 }
702a9357 3331 } else {
5efe9ed4
PM
3332 uint32_t fsr = arm_fi_to_sfsc(&fi);
3333
b7cc4e82
PC
3334 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3335 ((fsr & 0xf) << 1) | 1;
702a9357 3336 }
4a501606 3337 }
060e8a48
PM
3338 return par64;
3339}
9fb005b0 3340#endif /* CONFIG_TCG */
060e8a48
PM
3341
3342static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3343{
9fb005b0 3344#ifdef CONFIG_TCG
03ae85f8 3345 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 3346 uint64_t par64;
d3649702
PM
3347 ARMMMUIdx mmu_idx;
3348 int el = arm_current_el(env);
3349 bool secure = arm_is_secure_below_el3(env);
060e8a48 3350
d3649702
PM
3351 switch (ri->opc2 & 6) {
3352 case 0:
04b07d29 3353 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
d3649702
PM
3354 switch (el) {
3355 case 3:
127b2b08 3356 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3357 break;
3358 case 2:
b6ad6062 3359 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
04b07d29 3360 /* fall through */
d3649702 3361 case 1:
04b07d29 3362 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
b1a10c86 3363 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
04b07d29
RH
3364 : ARMMMUIdx_Stage1_E1_PAN);
3365 } else {
b1a10c86 3366 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
04b07d29 3367 }
d3649702
PM
3368 break;
3369 default:
3370 g_assert_not_reached();
3371 }
3372 break;
3373 case 2:
3374 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3375 switch (el) {
3376 case 3:
fba37aed 3377 mmu_idx = ARMMMUIdx_SE10_0;
d3649702
PM
3378 break;
3379 case 2:
b1a10c86 3380 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
2859d7b5 3381 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3382 break;
3383 case 1:
b1a10c86 3384 mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3385 break;
3386 default:
3387 g_assert_not_reached();
3388 }
3389 break;
3390 case 4:
3391 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
01b98b68 3392 mmu_idx = ARMMMUIdx_E10_1;
d3649702
PM
3393 break;
3394 case 6:
3395 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
01b98b68 3396 mmu_idx = ARMMMUIdx_E10_0;
d3649702
PM
3397 break;
3398 default:
3399 g_assert_not_reached();
3400 }
3401
3402 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
3403
3404 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3405#else
3406 /* Handled by hardware accelerator. */
3407 g_assert_not_reached();
3408#endif /* CONFIG_TCG */
4a501606 3409}
060e8a48 3410
14db7fe0
PM
3411static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3412 uint64_t value)
3413{
9fb005b0 3414#ifdef CONFIG_TCG
03ae85f8 3415 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
3416 uint64_t par64;
3417
e013b741 3418 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
14db7fe0
PM
3419
3420 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3421#else
3422 /* Handled by hardware accelerator. */
3423 g_assert_not_reached();
3424#endif /* CONFIG_TCG */
14db7fe0
PM
3425}
3426
3f208fd7
PM
3427static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3428 bool isread)
2a47df95 3429{
926c1b97
RDC
3430 if (arm_current_el(env) == 3 &&
3431 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
2a47df95
PM
3432 return CP_ACCESS_TRAP;
3433 }
3434 return CP_ACCESS_OK;
3435}
3436
060e8a48
PM
3437static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3438 uint64_t value)
3439{
9fb005b0 3440#ifdef CONFIG_TCG
03ae85f8 3441 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
3442 ARMMMUIdx mmu_idx;
3443 int secure = arm_is_secure_below_el3(env);
3444
3445 switch (ri->opc2 & 6) {
3446 case 0:
3447 switch (ri->opc1) {
04b07d29
RH
3448 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3449 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
b1a10c86 3450 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
04b07d29
RH
3451 : ARMMMUIdx_Stage1_E1_PAN);
3452 } else {
b1a10c86 3453 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
04b07d29 3454 }
d3649702
PM
3455 break;
3456 case 4: /* AT S1E2R, AT S1E2W */
b6ad6062 3457 mmu_idx = secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2;
d3649702
PM
3458 break;
3459 case 6: /* AT S1E3R, AT S1E3W */
127b2b08 3460 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3461 break;
3462 default:
3463 g_assert_not_reached();
3464 }
3465 break;
3466 case 2: /* AT S1E0R, AT S1E0W */
b1a10c86 3467 mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3468 break;
3469 case 4: /* AT S12E1R, AT S12E1W */
fba37aed 3470 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
d3649702
PM
3471 break;
3472 case 6: /* AT S12E0R, AT S12E0W */
fba37aed 3473 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
d3649702
PM
3474 break;
3475 default:
3476 g_assert_not_reached();
3477 }
060e8a48 3478
d3649702 3479 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
9fb005b0
PMD
3480#else
3481 /* Handled by hardware accelerator. */
3482 g_assert_not_reached();
3483#endif /* CONFIG_TCG */
060e8a48 3484}
4a501606
PM
3485#endif
3486
3487static const ARMCPRegInfo vapa_cp_reginfo[] = {
3488 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3489 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
3490 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3491 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
3492 .writefn = par_write },
3493#ifndef CONFIG_USER_ONLY
87562e4f 3494 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 3495 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 3496 .access = PL1_W, .accessfn = ats_access,
0710b2fa 3497 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
4a501606
PM
3498#endif
3499 REGINFO_SENTINEL
3500};
3501
18032bec
PM
3502/* Return basic MPU access permission bits. */
3503static uint32_t simple_mpu_ap_bits(uint32_t val)
3504{
3505 uint32_t ret;
3506 uint32_t mask;
3507 int i;
3508 ret = 0;
3509 mask = 3;
3510 for (i = 0; i < 16; i += 2) {
3511 ret |= (val >> i) & mask;
3512 mask <<= 2;
3513 }
3514 return ret;
3515}
3516
3517/* Pad basic MPU access permission bits to extended format. */
3518static uint32_t extended_mpu_ap_bits(uint32_t val)
3519{
3520 uint32_t ret;
3521 uint32_t mask;
3522 int i;
3523 ret = 0;
3524 mask = 3;
3525 for (i = 0; i < 16; i += 2) {
3526 ret |= (val & mask) << i;
3527 mask <<= 2;
3528 }
3529 return ret;
3530}
3531
c4241c7d
PM
3532static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3533 uint64_t value)
18032bec 3534{
7e09797c 3535 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
3536}
3537
c4241c7d 3538static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3539{
7e09797c 3540 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
3541}
3542
c4241c7d
PM
3543static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3544 uint64_t value)
18032bec 3545{
7e09797c 3546 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
3547}
3548
c4241c7d 3549static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3550{
7e09797c 3551 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
3552}
3553
6cb0b013
PC
3554static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3555{
3556 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3557
3558 if (!u32p) {
3559 return 0;
3560 }
3561
1bc04a88 3562 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
3563 return *u32p;
3564}
3565
3566static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3567 uint64_t value)
3568{
2fc0cc0e 3569 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3570 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3571
3572 if (!u32p) {
3573 return;
3574 }
3575
1bc04a88 3576 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 3577 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
3578 *u32p = value;
3579}
3580
6cb0b013
PC
3581static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3582 uint64_t value)
3583{
2fc0cc0e 3584 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3585 uint32_t nrgs = cpu->pmsav7_dregion;
3586
3587 if (value >= nrgs) {
3588 qemu_log_mask(LOG_GUEST_ERROR,
3589 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3590 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3591 return;
3592 }
3593
3594 raw_write(env, ri, value);
3595}
3596
3597static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
3598 /* Reset for all these registers is handled in arm_cpu_reset(),
3599 * because the PMSAv7 is also used by M-profile CPUs, which do
3600 * not register cpregs but still need the state to be reset.
3601 */
6cb0b013
PC
3602 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3603 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3604 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
3605 .readfn = pmsav7_read, .writefn = pmsav7_write,
3606 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3607 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3608 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3609 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
3610 .readfn = pmsav7_read, .writefn = pmsav7_write,
3611 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3612 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3613 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3614 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
3615 .readfn = pmsav7_read, .writefn = pmsav7_write,
3616 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3617 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3618 .access = PL1_RW,
1bc04a88 3619 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
3620 .writefn = pmsav7_rgnr_write,
3621 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3622 REGINFO_SENTINEL
3623};
3624
18032bec
PM
3625static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3626 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3627 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3628 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
3629 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3630 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 3631 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3632 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
3633 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3634 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3635 .access = PL1_RW,
7e09797c
PM
3636 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3637 .resetvalue = 0, },
18032bec
PM
3638 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3639 .access = PL1_RW,
7e09797c
PM
3640 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3641 .resetvalue = 0, },
ecce5c3c
PM
3642 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3643 .access = PL1_RW,
3644 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3645 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3646 .access = PL1_RW,
3647 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 3648 /* Protection region base and size registers */
e508a92b
PM
3649 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3650 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3651 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3652 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3653 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3654 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3655 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3656 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3657 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3658 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3659 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3660 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3661 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3662 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3663 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3664 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3665 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3666 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3667 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3668 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3669 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3670 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3671 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3672 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
3673 REGINFO_SENTINEL
3674};
3675
c4241c7d
PM
3676static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3677 uint64_t value)
ecce5c3c 3678{
11f136ee 3679 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
3680 int maskshift = extract32(value, 0, 3);
3681
e389be16
FA
3682 if (!arm_feature(env, ARM_FEATURE_V8)) {
3683 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3684 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3685 * using Long-desciptor translation table format */
3686 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3687 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3688 /* In an implementation that includes the Security Extensions
3689 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3690 * Short-descriptor translation table format.
3691 */
3692 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3693 } else {
3694 value &= TTBCR_N;
3695 }
e42c4db3 3696 }
e389be16 3697
b6af0975 3698 /* Update the masks corresponding to the TCR bank being written
11f136ee 3699 * Note that we always calculate mask and base_mask, but
e42c4db3 3700 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
3701 * for long-descriptor tables the TCR fields are used differently
3702 * and the mask and base_mask values are meaningless.
e42c4db3 3703 */
11f136ee
FA
3704 tcr->raw_tcr = value;
3705 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3706 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
3707}
3708
c4241c7d
PM
3709static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3710 uint64_t value)
d4e6df63 3711{
2fc0cc0e 3712 ARMCPU *cpu = env_archcpu(env);
ab638a32 3713 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3714
d4e6df63
PM
3715 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3716 /* With LPAE the TTBCR could result in a change of ASID
3717 * via the TTBCR.A1 bit, so do a TLB flush.
3718 */
d10eb08f 3719 tlb_flush(CPU(cpu));
d4e6df63 3720 }
ab638a32
RH
3721 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3722 value = deposit64(tcr->raw_tcr, 0, 32, value);
c4241c7d 3723 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
3724}
3725
ecce5c3c
PM
3726static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3727{
11f136ee
FA
3728 TCR *tcr = raw_ptr(env, ri);
3729
3730 /* Reset both the TCR as well as the masks corresponding to the bank of
3731 * the TCR being reset.
3732 */
3733 tcr->raw_tcr = 0;
3734 tcr->mask = 0;
3735 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
3736}
3737
d06dc933 3738static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
cb2e37df
PM
3739 uint64_t value)
3740{
2fc0cc0e 3741 ARMCPU *cpu = env_archcpu(env);
11f136ee 3742 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3743
cb2e37df 3744 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 3745 tlb_flush(CPU(cpu));
11f136ee 3746 tcr->raw_tcr = value;
cb2e37df
PM
3747}
3748
327ed10f
PM
3749static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3750 uint64_t value)
3751{
93f379b0
RH
3752 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3753 if (cpreg_field_is_64bit(ri) &&
3754 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
2fc0cc0e 3755 ARMCPU *cpu = env_archcpu(env);
d10eb08f 3756 tlb_flush(CPU(cpu));
327ed10f
PM
3757 }
3758 raw_write(env, ri, value);
3759}
3760
ed30da8e
RH
3761static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3762 uint64_t value)
3763{
d06dc933
RH
3764 /*
3765 * If we are running with E2&0 regime, then an ASID is active.
3766 * Flush if that might be changing. Note we're not checking
3767 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3768 * holds the active ASID, only checking the field that might.
3769 */
3770 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3771 (arm_hcr_el2_eff(env) & HCR_E2H)) {
b6ad6062
RDC
3772 uint16_t mask = ARMMMUIdxBit_E20_2 |
3773 ARMMMUIdxBit_E20_2_PAN |
3774 ARMMMUIdxBit_E20_0;
3775
3776 if (arm_is_secure_below_el3(env)) {
3777 mask >>= ARM_MMU_IDX_A_NS;
3778 }
3779
3780 tlb_flush_by_mmuidx(env_cpu(env), mask);
d06dc933 3781 }
ed30da8e
RH
3782 raw_write(env, ri, value);
3783}
3784
b698e9cf
EI
3785static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3786 uint64_t value)
3787{
2fc0cc0e 3788 ARMCPU *cpu = env_archcpu(env);
b698e9cf
EI
3789 CPUState *cs = CPU(cpu);
3790
97fa9350
RH
3791 /*
3792 * A change in VMID to the stage2 page table (Stage2) invalidates
3793 * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3794 */
b698e9cf 3795 if (raw_read(env, ri) != value) {
c4f060e8
RDC
3796 uint16_t mask = ARMMMUIdxBit_E10_1 |
3797 ARMMMUIdxBit_E10_1_PAN |
3798 ARMMMUIdxBit_E10_0;
3799
3800 if (arm_is_secure_below_el3(env)) {
3801 mask >>= ARM_MMU_IDX_A_NS;
3802 }
3803
3804 tlb_flush_by_mmuidx(cs, mask);
b698e9cf
EI
3805 raw_write(env, ri, value);
3806 }
3807}
3808
8e5d75c9 3809static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 3810 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218 3811 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4a7e2d73 3812 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 3813 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 3814 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
84929218 3815 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
88ca1c2d
FA
3816 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3817 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9 3818 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
84929218 3819 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
8e5d75c9
PC
3820 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3821 offsetof(CPUARMState, cp15.dfar_ns) } },
3822 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3823 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218
RH
3824 .access = PL1_RW, .accessfn = access_tvm_trvm,
3825 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
8e5d75c9
PC
3826 .resetvalue = 0, },
3827 REGINFO_SENTINEL
3828};
3829
3830static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
3831 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3832 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
84929218 3833 .access = PL1_RW, .accessfn = access_tvm_trvm,
d81c519c 3834 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 3835 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 3836 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
84929218
RH
3837 .access = PL1_RW, .accessfn = access_tvm_trvm,
3838 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
3839 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3840 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 3841 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 3842 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
84929218
RH
3843 .access = PL1_RW, .accessfn = access_tvm_trvm,
3844 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
3845 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3846 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
3847 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3848 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
3849 .access = PL1_RW, .accessfn = access_tvm_trvm,
3850 .writefn = vmsa_tcr_el12_write,
cb2e37df 3851 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 3852 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 3853 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
3854 .access = PL1_RW, .accessfn = access_tvm_trvm,
3855 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 3856 .raw_writefn = vmsa_ttbcr_raw_write,
d102058e
RH
3857 /* No offsetoflow32 -- pass the entire TCR to writefn/raw_writefn. */
3858 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.tcr_el[3]),
3859 offsetof(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
3860 REGINFO_SENTINEL
3861};
3862
ab638a32
RH
3863/* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3864 * qemu tlbs nor adjusting cached masks.
3865 */
3866static const ARMCPRegInfo ttbcr2_reginfo = {
3867 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
84929218
RH
3868 .access = PL1_RW, .accessfn = access_tvm_trvm,
3869 .type = ARM_CP_ALIAS,
d102058e
RH
3870 .bank_fieldoffsets = {
3871 offsetofhigh32(CPUARMState, cp15.tcr_el[3].raw_tcr),
3872 offsetofhigh32(CPUARMState, cp15.tcr_el[1].raw_tcr),
3873 },
ab638a32
RH
3874};
3875
c4241c7d
PM
3876static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3877 uint64_t value)
1047b9d7
PM
3878{
3879 env->cp15.c15_ticonfig = value & 0xe7;
3880 /* The OS_TYPE bit in this register changes the reported CPUID! */
3881 env->cp15.c0_cpuid = (value & (1 << 5)) ?
3882 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
3883}
3884
c4241c7d
PM
3885static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3886 uint64_t value)
1047b9d7
PM
3887{
3888 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
3889}
3890
c4241c7d
PM
3891static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3892 uint64_t value)
1047b9d7
PM
3893{
3894 /* Wait-for-interrupt (deprecated) */
2fc0cc0e 3895 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
1047b9d7
PM
3896}
3897
c4241c7d
PM
3898static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3899 uint64_t value)
c4804214
PM
3900{
3901 /* On OMAP there are registers indicating the max/min index of dcache lines
3902 * containing a dirty line; cache flush operations have to reset these.
3903 */
3904 env->cp15.c15_i_max = 0x000;
3905 env->cp15.c15_i_min = 0xff0;
c4804214
PM
3906}
3907
18032bec
PM
3908static const ARMCPRegInfo omap_cp_reginfo[] = {
3909 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3910 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 3911 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 3912 .resetvalue = 0, },
1047b9d7
PM
3913 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3914 .access = PL1_RW, .type = ARM_CP_NOP },
3915 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3916 .access = PL1_RW,
3917 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3918 .writefn = omap_ticonfig_write },
3919 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3920 .access = PL1_RW,
3921 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3922 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3923 .access = PL1_RW, .resetvalue = 0xff0,
3924 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3925 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3926 .access = PL1_RW,
3927 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3928 .writefn = omap_threadid_write },
3929 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3930 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 3931 .type = ARM_CP_NO_RAW,
1047b9d7
PM
3932 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3933 /* TODO: Peripheral port remap register:
3934 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3935 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3936 * when MMU is off.
3937 */
c4804214 3938 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 3939 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 3940 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 3941 .writefn = omap_cachemaint_write },
34f90529
PM
3942 { .name = "C9", .cp = 15, .crn = 9,
3943 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3944 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
3945 REGINFO_SENTINEL
3946};
3947
c4241c7d
PM
3948static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3949 uint64_t value)
1047b9d7 3950{
c0f4af17 3951 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
3952}
3953
3954static const ARMCPRegInfo xscale_cp_reginfo[] = {
3955 { .name = "XSCALE_CPAR",
3956 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3957 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3958 .writefn = xscale_cpar_write, },
2771db27
PM
3959 { .name = "XSCALE_AUXCR",
3960 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3961 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3962 .resetvalue = 0, },
3b771579
PM
3963 /* XScale specific cache-lockdown: since we have no cache we NOP these
3964 * and hope the guest does not really rely on cache behaviour.
3965 */
3966 { .name = "XSCALE_LOCK_ICACHE_LINE",
3967 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3968 .access = PL1_W, .type = ARM_CP_NOP },
3969 { .name = "XSCALE_UNLOCK_ICACHE",
3970 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3971 .access = PL1_W, .type = ARM_CP_NOP },
3972 { .name = "XSCALE_DCACHE_LOCK",
3973 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3974 .access = PL1_RW, .type = ARM_CP_NOP },
3975 { .name = "XSCALE_UNLOCK_DCACHE",
3976 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3977 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
3978 REGINFO_SENTINEL
3979};
3980
3981static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3982 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3983 * implementation of this implementation-defined space.
3984 * Ideally this should eventually disappear in favour of actually
3985 * implementing the correct behaviour for all cores.
3986 */
3987 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3988 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 3989 .access = PL1_RW,
7a0e58fa 3990 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 3991 .resetvalue = 0 },
18032bec
PM
3992 REGINFO_SENTINEL
3993};
3994
c4804214
PM
3995static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3996 /* Cache status: RAZ because we have no cache so it's always clean */
3997 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 3998 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 3999 .resetvalue = 0 },
c4804214
PM
4000 REGINFO_SENTINEL
4001};
4002
4003static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4004 /* We never have a a block transfer operation in progress */
4005 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 4006 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4007 .resetvalue = 0 },
30b05bba
PM
4008 /* The cache ops themselves: these all NOP for QEMU */
4009 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4010 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4011 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4012 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4013 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4014 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4015 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4016 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4017 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4018 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4019 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4020 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
4021 REGINFO_SENTINEL
4022};
4023
4024static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4025 /* The cache test-and-clean instructions always return (1 << 30)
4026 * to indicate that there are no dirty cache lines.
4027 */
4028 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 4029 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4030 .resetvalue = (1 << 30) },
c4804214 4031 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 4032 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4033 .resetvalue = (1 << 30) },
c4804214
PM
4034 REGINFO_SENTINEL
4035};
4036
34f90529
PM
4037static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4038 /* Ignore ReadBuffer accesses */
4039 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4040 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 4041 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 4042 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
4043 REGINFO_SENTINEL
4044};
4045
731de9e6
EI
4046static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4047{
731de9e6 4048 unsigned int cur_el = arm_current_el(env);
731de9e6 4049
e6ef0169 4050 if (arm_is_el2_enabled(env) && cur_el == 1) {
731de9e6
EI
4051 return env->cp15.vpidr_el2;
4052 }
4053 return raw_read(env, ri);
4054}
4055
06a7e647 4056static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 4057{
2fc0cc0e 4058 ARMCPU *cpu = env_archcpu(env);
eb5e1d3c
PF
4059 uint64_t mpidr = cpu->mp_affinity;
4060
81bdde9d 4061 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 4062 mpidr |= (1U << 31);
81bdde9d
PM
4063 /* Cores which are uniprocessor (non-coherent)
4064 * but still implement the MP extensions set
a8e81b31 4065 * bit 30. (For instance, Cortex-R5).
81bdde9d 4066 */
a8e81b31
PC
4067 if (cpu->mp_is_up) {
4068 mpidr |= (1u << 30);
4069 }
81bdde9d 4070 }
c4241c7d 4071 return mpidr;
81bdde9d
PM
4072}
4073
06a7e647
EI
4074static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4075{
f0d574d6 4076 unsigned int cur_el = arm_current_el(env);
f0d574d6 4077
e6ef0169 4078 if (arm_is_el2_enabled(env) && cur_el == 1) {
f0d574d6
EI
4079 return env->cp15.vmpidr_el2;
4080 }
06a7e647
EI
4081 return mpidr_read_val(env);
4082}
4083
7ac681cf 4084static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 4085 /* NOP AMAIR0/1 */
b0fe2427
PM
4086 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4087 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
84929218
RH
4088 .access = PL1_RW, .accessfn = access_tvm_trvm,
4089 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427 4090 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 4091 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
84929218
RH
4092 .access = PL1_RW, .accessfn = access_tvm_trvm,
4093 .type = ARM_CP_CONST, .resetvalue = 0 },
891a2fe7 4094 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
4095 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4096 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4097 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 4098 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
84929218
RH
4099 .access = PL1_RW, .accessfn = access_tvm_trvm,
4100 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4101 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4102 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 4103 .writefn = vmsa_ttbr_write, },
891a2fe7 4104 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
84929218
RH
4105 .access = PL1_RW, .accessfn = access_tvm_trvm,
4106 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4107 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4108 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 4109 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
4110 REGINFO_SENTINEL
4111};
4112
c4241c7d 4113static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4114{
c4241c7d 4115 return vfp_get_fpcr(env);
b0d2b7d0
PM
4116}
4117
c4241c7d
PM
4118static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4119 uint64_t value)
b0d2b7d0
PM
4120{
4121 vfp_set_fpcr(env, value);
b0d2b7d0
PM
4122}
4123
c4241c7d 4124static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4125{
c4241c7d 4126 return vfp_get_fpsr(env);
b0d2b7d0
PM
4127}
4128
c4241c7d
PM
4129static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4130 uint64_t value)
b0d2b7d0
PM
4131{
4132 vfp_set_fpsr(env, value);
b0d2b7d0
PM
4133}
4134
3f208fd7
PM
4135static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4136 bool isread)
c2b820fe 4137{
aaec1432 4138 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
c2b820fe
PM
4139 return CP_ACCESS_TRAP;
4140 }
4141 return CP_ACCESS_OK;
4142}
4143
4144static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4145 uint64_t value)
4146{
4147 env->daif = value & PSTATE_DAIF;
4148}
4149
220f508f
RH
4150static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4151{
4152 return env->pstate & PSTATE_PAN;
4153}
4154
4155static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4156 uint64_t value)
4157{
4158 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4159}
4160
4161static const ARMCPRegInfo pan_reginfo = {
4162 .name = "PAN", .state = ARM_CP_STATE_AA64,
4163 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4164 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4165 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4166};
4167
9eeb7a1c
RH
4168static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4169{
4170 return env->pstate & PSTATE_UAO;
4171}
4172
4173static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4174 uint64_t value)
4175{
4176 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4177}
4178
4179static const ARMCPRegInfo uao_reginfo = {
4180 .name = "UAO", .state = ARM_CP_STATE_AA64,
4181 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4182 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4183 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4184};
4185
dc8b1853
RC
4186static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4187{
4188 return env->pstate & PSTATE_DIT;
4189}
4190
4191static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4192 uint64_t value)
4193{
4194 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4195}
4196
4197static const ARMCPRegInfo dit_reginfo = {
4198 .name = "DIT", .state = ARM_CP_STATE_AA64,
4199 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4200 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4201 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4202};
4203
f2f68a78
RC
4204static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4205{
4206 return env->pstate & PSTATE_SSBS;
4207}
4208
4209static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4210 uint64_t value)
4211{
4212 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4213}
4214
4215static const ARMCPRegInfo ssbs_reginfo = {
4216 .name = "SSBS", .state = ARM_CP_STATE_AA64,
4217 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4218 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4219 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4220};
4221
38262d8a
RH
4222static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4223 const ARMCPRegInfo *ri,
4224 bool isread)
8af35c37 4225{
38262d8a
RH
4226 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4227 switch (arm_current_el(env)) {
4228 case 0:
4229 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4230 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4231 return CP_ACCESS_TRAP;
4232 }
4233 /* fall through */
4234 case 1:
4235 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4236 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4237 return CP_ACCESS_TRAP_EL2;
4238 }
4239 break;
8af35c37
PM
4240 }
4241 return CP_ACCESS_OK;
4242}
4243
38262d8a 4244static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
1bed4d2e
RH
4245 const ARMCPRegInfo *ri,
4246 bool isread)
4247{
38262d8a 4248 /* Cache invalidate/clean to Point of Unification... */
1bed4d2e
RH
4249 switch (arm_current_el(env)) {
4250 case 0:
4251 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4252 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4253 return CP_ACCESS_TRAP;
4254 }
4255 /* fall through */
4256 case 1:
38262d8a
RH
4257 /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set. */
4258 if (arm_hcr_el2_eff(env) & HCR_TPU) {
1bed4d2e
RH
4259 return CP_ACCESS_TRAP_EL2;
4260 }
4261 break;
4262 }
4263 return CP_ACCESS_OK;
4264}
4265
dbb1fb27
AB
4266/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4267 * Page D4-1736 (DDI0487A.b)
4268 */
4269
b7e0730d
RH
4270static int vae1_tlbmask(CPUARMState *env)
4271{
e04a5752 4272 uint64_t hcr = arm_hcr_el2_eff(env);
bc944d3a 4273 uint16_t mask;
e04a5752
RDC
4274
4275 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
bc944d3a
RDC
4276 mask = ARMMMUIdxBit_E20_2 |
4277 ARMMMUIdxBit_E20_2_PAN |
4278 ARMMMUIdxBit_E20_0;
b7e0730d 4279 } else {
bc944d3a 4280 mask = ARMMMUIdxBit_E10_1 |
452ef8cb
RH
4281 ARMMMUIdxBit_E10_1_PAN |
4282 ARMMMUIdxBit_E10_0;
b7e0730d 4283 }
bc944d3a
RDC
4284
4285 if (arm_is_secure_below_el3(env)) {
4286 mask >>= ARM_MMU_IDX_A_NS;
4287 }
4288
4289 return mask;
b7e0730d
RH
4290}
4291
ea04dce7
RH
4292/* Return 56 if TBI is enabled, 64 otherwise. */
4293static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4294 uint64_t addr)
4295{
4296 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
4297 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4298 int select = extract64(addr, 55, 1);
4299
4300 return (tbi >> select) & 1 ? 56 : 64;
4301}
4302
4303static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4304{
b6ad6062 4305 uint64_t hcr = arm_hcr_el2_eff(env);
ea04dce7
RH
4306 ARMMMUIdx mmu_idx;
4307
4308 /* Only the regime of the mmu_idx below is significant. */
b6ad6062 4309 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
ea04dce7
RH
4310 mmu_idx = ARMMMUIdx_E20_0;
4311 } else {
4312 mmu_idx = ARMMMUIdx_E10_0;
4313 }
b6ad6062
RDC
4314
4315 if (arm_is_secure_below_el3(env)) {
4316 mmu_idx &= ~ARM_MMU_IDX_A_NS;
4317 }
4318
ea04dce7
RH
4319 return tlbbits_for_regime(env, mmu_idx, addr);
4320}
4321
fd3ed969
PM
4322static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4323 uint64_t value)
168aa23b 4324{
29a0af61 4325 CPUState *cs = env_cpu(env);
b7e0730d 4326 int mask = vae1_tlbmask(env);
dbb1fb27 4327
b7e0730d 4328 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
168aa23b
PM
4329}
4330
b4ab8ce9
PM
4331static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4332 uint64_t value)
4333{
29a0af61 4334 CPUState *cs = env_cpu(env);
b7e0730d 4335 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4336
4337 if (tlb_force_broadcast(env)) {
527db2be
RH
4338 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4339 } else {
4340 tlb_flush_by_mmuidx(cs, mask);
b4ab8ce9 4341 }
b4ab8ce9
PM
4342}
4343
90c19cdf 4344static int alle1_tlbmask(CPUARMState *env)
168aa23b 4345{
90c19cdf
RH
4346 /*
4347 * Note that the 'ALL' scope must invalidate both stage 1 and
fd3ed969
PM
4348 * stage 2 translations, whereas most other scopes only invalidate
4349 * stage 1 translations.
4350 */
fd3ed969 4351 if (arm_is_secure_below_el3(env)) {
452ef8cb
RH
4352 return ARMMMUIdxBit_SE10_1 |
4353 ARMMMUIdxBit_SE10_1_PAN |
4354 ARMMMUIdxBit_SE10_0;
fd3ed969 4355 } else {
452ef8cb
RH
4356 return ARMMMUIdxBit_E10_1 |
4357 ARMMMUIdxBit_E10_1_PAN |
4358 ARMMMUIdxBit_E10_0;
fd3ed969 4359 }
168aa23b
PM
4360}
4361
85d0dc9f
RH
4362static int e2_tlbmask(CPUARMState *env)
4363{
b6ad6062
RDC
4364 if (arm_is_secure_below_el3(env)) {
4365 return ARMMMUIdxBit_SE20_0 |
4366 ARMMMUIdxBit_SE20_2 |
4367 ARMMMUIdxBit_SE20_2_PAN |
4368 ARMMMUIdxBit_SE2;
4369 } else {
4370 return ARMMMUIdxBit_E20_0 |
4371 ARMMMUIdxBit_E20_2 |
4372 ARMMMUIdxBit_E20_2_PAN |
4373 ARMMMUIdxBit_E2;
4374 }
85d0dc9f
RH
4375}
4376
90c19cdf
RH
4377static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4378 uint64_t value)
4379{
4380 CPUState *cs = env_cpu(env);
4381 int mask = alle1_tlbmask(env);
4382
4383 tlb_flush_by_mmuidx(cs, mask);
4384}
4385
fd3ed969 4386static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
4387 uint64_t value)
4388{
85d0dc9f
RH
4389 CPUState *cs = env_cpu(env);
4390 int mask = e2_tlbmask(env);
fd3ed969 4391
85d0dc9f 4392 tlb_flush_by_mmuidx(cs, mask);
fd3ed969
PM
4393}
4394
43efaa33
PM
4395static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4396 uint64_t value)
4397{
2fc0cc0e 4398 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4399 CPUState *cs = CPU(cpu);
4400
127b2b08 4401 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4402}
4403
fd3ed969
PM
4404static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4405 uint64_t value)
4406{
29a0af61 4407 CPUState *cs = env_cpu(env);
90c19cdf
RH
4408 int mask = alle1_tlbmask(env);
4409
4410 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
fa439fc5
PM
4411}
4412
2bfb9d75
PM
4413static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4414 uint64_t value)
4415{
29a0af61 4416 CPUState *cs = env_cpu(env);
85d0dc9f 4417 int mask = e2_tlbmask(env);
2bfb9d75 4418
85d0dc9f 4419 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
2bfb9d75
PM
4420}
4421
43efaa33
PM
4422static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4423 uint64_t value)
4424{
29a0af61 4425 CPUState *cs = env_cpu(env);
43efaa33 4426
127b2b08 4427 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4428}
4429
fd3ed969
PM
4430static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4431 uint64_t value)
fa439fc5 4432{
fd3ed969
PM
4433 /* Invalidate by VA, EL2
4434 * Currently handles both VAE2 and VALE2, since we don't support
4435 * flush-last-level-only.
4436 */
85d0dc9f
RH
4437 CPUState *cs = env_cpu(env);
4438 int mask = e2_tlbmask(env);
fd3ed969
PM
4439 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4440
85d0dc9f 4441 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
fd3ed969
PM
4442}
4443
43efaa33
PM
4444static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4445 uint64_t value)
4446{
4447 /* Invalidate by VA, EL3
4448 * Currently handles both VAE3 and VALE3, since we don't support
4449 * flush-last-level-only.
4450 */
2fc0cc0e 4451 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4452 CPUState *cs = CPU(cpu);
4453 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4454
127b2b08 4455 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
43efaa33
PM
4456}
4457
fd3ed969
PM
4458static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4459 uint64_t value)
4460{
90c19cdf
RH
4461 CPUState *cs = env_cpu(env);
4462 int mask = vae1_tlbmask(env);
fa439fc5 4463 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4464 int bits = vae1_tlbbits(env, pageaddr);
fa439fc5 4465
ea04dce7 4466 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
fa439fc5
PM
4467}
4468
b4ab8ce9
PM
4469static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4470 uint64_t value)
4471{
4472 /* Invalidate by VA, EL1&0 (AArch64 version).
4473 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4474 * since we don't support flush-for-specific-ASID-only or
4475 * flush-last-level-only.
4476 */
90c19cdf
RH
4477 CPUState *cs = env_cpu(env);
4478 int mask = vae1_tlbmask(env);
b4ab8ce9 4479 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4480 int bits = vae1_tlbbits(env, pageaddr);
b4ab8ce9
PM
4481
4482 if (tlb_force_broadcast(env)) {
ea04dce7 4483 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
527db2be 4484 } else {
ea04dce7 4485 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
b4ab8ce9 4486 }
b4ab8ce9
PM
4487}
4488
fd3ed969
PM
4489static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4490 uint64_t value)
fa439fc5 4491{
29a0af61 4492 CPUState *cs = env_cpu(env);
fd3ed969 4493 uint64_t pageaddr = sextract64(value << 12, 0, 56);
b6ad6062
RDC
4494 bool secure = arm_is_secure_below_el3(env);
4495 int mask = secure ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2;
eb849d8f 4496 int bits = tlbbits_for_regime(env, secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2,
b6ad6062 4497 pageaddr);
fa439fc5 4498
b6ad6062 4499 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
fa439fc5
PM
4500}
4501
43efaa33
PM
4502static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4503 uint64_t value)
4504{
29a0af61 4505 CPUState *cs = env_cpu(env);
43efaa33 4506 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4507 int bits = tlbbits_for_regime(env, ARMMMUIdx_SE3, pageaddr);
43efaa33 4508
ea04dce7
RH
4509 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4510 ARMMMUIdxBit_SE3, bits);
43efaa33
PM
4511}
4512
84940ed8 4513#ifdef TARGET_AARCH64
ab1cdb47
RH
4514typedef struct {
4515 uint64_t base;
84940ed8 4516 uint64_t length;
ab1cdb47
RH
4517} TLBIRange;
4518
4519static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4520 uint64_t value)
4521{
4522 unsigned int page_size_granule, page_shift, num, scale, exponent;
3974ff93
RH
4523 /* Extract one bit to represent the va selector in use. */
4524 uint64_t select = sextract64(value, 36, 1);
4525 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
ab1cdb47 4526 TLBIRange ret = { };
84940ed8 4527
84940ed8
RC
4528 page_size_granule = extract64(value, 46, 2);
4529
3974ff93
RH
4530 /* The granule encoded in value must match the granule in use. */
4531 if (page_size_granule != (param.using64k ? 3 : param.using16k ? 2 : 1)) {
4532 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
84940ed8 4533 page_size_granule);
ab1cdb47 4534 return ret;
84940ed8
RC
4535 }
4536
52a9f609 4537 page_shift = (page_size_granule - 1) * 2 + 12;
ab1cdb47
RH
4538 num = extract64(value, 39, 5);
4539 scale = extract64(value, 44, 2);
84940ed8 4540 exponent = (5 * scale) + 1;
84940ed8 4541
ab1cdb47 4542 ret.length = (num + 1) << (exponent + page_shift);
84940ed8 4543
3974ff93 4544 if (param.select) {
d976de21 4545 ret.base = sextract64(value, 0, 37);
84940ed8 4546 } else {
d976de21 4547 ret.base = extract64(value, 0, 37);
84940ed8 4548 }
ef56c242
RH
4549 if (param.ds) {
4550 /*
4551 * With DS=1, BaseADDR is always shifted 16 so that it is able
4552 * to address all 52 va bits. The input address is perforce
4553 * aligned on a 64k boundary regardless of translation granule.
4554 */
4555 page_shift = 16;
4556 }
d976de21 4557 ret.base <<= page_shift;
84940ed8 4558
ab1cdb47 4559 return ret;
84940ed8
RC
4560}
4561
4562static void do_rvae_write(CPUARMState *env, uint64_t value,
4563 int idxmap, bool synced)
4564{
4565 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
ab1cdb47 4566 TLBIRange range;
84940ed8
RC
4567 int bits;
4568
ab1cdb47
RH
4569 range = tlbi_aa64_get_range(env, one_idx, value);
4570 bits = tlbbits_for_regime(env, one_idx, range.base);
84940ed8
RC
4571
4572 if (synced) {
4573 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
ab1cdb47
RH
4574 range.base,
4575 range.length,
84940ed8
RC
4576 idxmap,
4577 bits);
4578 } else {
ab1cdb47
RH
4579 tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4580 range.length, idxmap, bits);
84940ed8
RC
4581 }
4582}
4583
4584static void tlbi_aa64_rvae1_write(CPUARMState *env,
4585 const ARMCPRegInfo *ri,
4586 uint64_t value)
4587{
4588 /*
4589 * Invalidate by VA range, EL1&0.
4590 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4591 * since we don't support flush-for-specific-ASID-only or
4592 * flush-last-level-only.
4593 */
4594
4595 do_rvae_write(env, value, vae1_tlbmask(env),
4596 tlb_force_broadcast(env));
4597}
4598
4599static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4600 const ARMCPRegInfo *ri,
4601 uint64_t value)
4602{
4603 /*
4604 * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4605 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4606 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4607 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4608 * shareable specific flushes.
4609 */
4610
4611 do_rvae_write(env, value, vae1_tlbmask(env), true);
4612}
4613
4614static int vae2_tlbmask(CPUARMState *env)
4615{
4616 return (arm_is_secure_below_el3(env)
4617 ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2);
4618}
4619
4620static void tlbi_aa64_rvae2_write(CPUARMState *env,
4621 const ARMCPRegInfo *ri,
4622 uint64_t value)
4623{
4624 /*
4625 * Invalidate by VA range, EL2.
4626 * Currently handles all of RVAE2 and RVALE2,
4627 * since we don't support flush-for-specific-ASID-only or
4628 * flush-last-level-only.
4629 */
4630
4631 do_rvae_write(env, value, vae2_tlbmask(env),
4632 tlb_force_broadcast(env));
4633
4634
4635}
4636
4637static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4638 const ARMCPRegInfo *ri,
4639 uint64_t value)
4640{
4641 /*
4642 * Invalidate by VA range, Inner/Outer Shareable, EL2.
4643 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4644 * since we don't support flush-for-specific-ASID-only,
4645 * flush-last-level-only or inner/outer shareable specific flushes.
4646 */
4647
4648 do_rvae_write(env, value, vae2_tlbmask(env), true);
4649
4650}
4651
4652static void tlbi_aa64_rvae3_write(CPUARMState *env,
4653 const ARMCPRegInfo *ri,
4654 uint64_t value)
4655{
4656 /*
4657 * Invalidate by VA range, EL3.
4658 * Currently handles all of RVAE3 and RVALE3,
4659 * since we don't support flush-for-specific-ASID-only or
4660 * flush-last-level-only.
4661 */
4662
4663 do_rvae_write(env, value, ARMMMUIdxBit_SE3,
4664 tlb_force_broadcast(env));
4665}
4666
4667static void tlbi_aa64_rvae3is_write(CPUARMState *env,
4668 const ARMCPRegInfo *ri,
4669 uint64_t value)
4670{
4671 /*
4672 * Invalidate by VA range, EL3, Inner/Outer Shareable.
4673 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
4674 * since we don't support flush-for-specific-ASID-only,
4675 * flush-last-level-only or inner/outer specific flushes.
4676 */
4677
4678 do_rvae_write(env, value, ARMMMUIdxBit_SE3, true);
4679}
4680#endif
4681
3f208fd7
PM
4682static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4683 bool isread)
aca3f40b 4684{
4351cb72
RH
4685 int cur_el = arm_current_el(env);
4686
4687 if (cur_el < 2) {
4688 uint64_t hcr = arm_hcr_el2_eff(env);
4689
4690 if (cur_el == 0) {
4691 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4692 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4693 return CP_ACCESS_TRAP_EL2;
4694 }
4695 } else {
4696 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4697 return CP_ACCESS_TRAP;
4698 }
4699 if (hcr & HCR_TDZ) {
4700 return CP_ACCESS_TRAP_EL2;
4701 }
4702 }
4703 } else if (hcr & HCR_TDZ) {
4704 return CP_ACCESS_TRAP_EL2;
4705 }
aca3f40b
PM
4706 }
4707 return CP_ACCESS_OK;
4708}
4709
4710static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4711{
2fc0cc0e 4712 ARMCPU *cpu = env_archcpu(env);
aca3f40b
PM
4713 int dzp_bit = 1 << 4;
4714
4715 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 4716 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
4717 dzp_bit = 0;
4718 }
4719 return cpu->dcz_blocksize | dzp_bit;
4720}
4721
3f208fd7
PM
4722static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4723 bool isread)
f502cfc2 4724{
cdcf1405 4725 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
4726 /* Access to SP_EL0 is undefined if it's being used as
4727 * the stack pointer.
4728 */
4729 return CP_ACCESS_TRAP_UNCATEGORIZED;
4730 }
4731 return CP_ACCESS_OK;
4732}
4733
4734static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4735{
4736 return env->pstate & PSTATE_SP;
4737}
4738
4739static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4740{
4741 update_spsel(env, val);
4742}
4743
137feaa9
FA
4744static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4745 uint64_t value)
4746{
2fc0cc0e 4747 ARMCPU *cpu = env_archcpu(env);
137feaa9 4748
f00faf13
RH
4749 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4750 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4751 value &= ~SCTLR_M;
4752 }
4753
4754 /* ??? Lots of these bits are not implemented. */
4755
4756 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4757 if (ri->opc1 == 6) { /* SCTLR_EL3 */
4758 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4759 } else {
4760 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4761 SCTLR_ATA0 | SCTLR_ATA);
4762 }
4763 }
4764
137feaa9
FA
4765 if (raw_read(env, ri) == value) {
4766 /* Skip the TLB flush if nothing actually changed; Linux likes
4767 * to do a lot of pointless SCTLR writes.
4768 */
4769 return;
4770 }
4771
4772 raw_write(env, ri, value);
f00faf13 4773
137feaa9 4774 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 4775 tlb_flush(CPU(cpu));
2e5dcf36
RH
4776
4777 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4778 /*
4779 * Normally we would always end the TB on an SCTLR write; see the
4780 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4781 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4782 * of hflags from the translator, so do it here.
4783 */
4784 arm_rebuild_hflags(env);
4785 }
137feaa9
FA
4786}
4787
3f208fd7
PM
4788static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
4789 bool isread)
03fbf20f
PM
4790{
4791 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
f2cae609 4792 return CP_ACCESS_TRAP_FP_EL2;
03fbf20f
PM
4793 }
4794 if (env->cp15.cptr_el[3] & CPTR_TFP) {
f2cae609 4795 return CP_ACCESS_TRAP_FP_EL3;
03fbf20f
PM
4796 }
4797 return CP_ACCESS_OK;
4798}
4799
a8d64e73
PM
4800static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4801 uint64_t value)
4802{
4803 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4804}
4805
b0d2b7d0
PM
4806static const ARMCPRegInfo v8_cp_reginfo[] = {
4807 /* Minimal set of EL0-visible registers. This will need to be expanded
4808 * significantly for system emulation of AArch64 CPUs.
4809 */
4810 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4811 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4812 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
4813 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4814 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 4815 .type = ARM_CP_NO_RAW,
c2b820fe
PM
4816 .access = PL0_RW, .accessfn = aa64_daif_access,
4817 .fieldoffset = offsetof(CPUARMState, daif),
4818 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
4819 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4820 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 4821 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4822 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
4823 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4824 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 4825 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4826 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
4827 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4828 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 4829 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
4830 .readfn = aa64_dczid_read },
4831 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4832 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4833 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4834#ifndef CONFIG_USER_ONLY
4835 /* Avoid overhead of an access check that always passes in user-mode */
4836 .accessfn = aa64_zva_access,
4837#endif
4838 },
0eef9d98
PM
4839 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4840 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4841 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
4842 /* Cache ops: all NOPs since we don't emulate caches */
4843 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4844 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a
RH
4845 .access = PL1_W, .type = ARM_CP_NOP,
4846 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4847 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4848 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a
RH
4849 .access = PL1_W, .type = ARM_CP_NOP,
4850 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4851 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4852 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4853 .access = PL0_W, .type = ARM_CP_NOP,
38262d8a 4854 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4855 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4856 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e
RH
4857 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4858 .type = ARM_CP_NOP },
8af35c37
PM
4859 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4860 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 4861 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
4862 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4863 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4864 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 4865 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
4866 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4867 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 4868 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
4869 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4870 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4871 .access = PL0_W, .type = ARM_CP_NOP,
38262d8a 4872 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4873 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4874 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4875 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 4876 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
4877 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4878 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 4879 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
168aa23b
PM
4880 /* TLBI operations */
4881 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4882 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
30881b73 4883 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4884 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4885 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4886 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
30881b73 4887 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4888 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4889 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4890 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
30881b73 4891 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4892 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4893 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4894 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
30881b73 4895 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4896 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4897 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4898 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
30881b73 4899 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4900 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4901 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4902 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
30881b73 4903 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4904 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4905 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4906 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73 4907 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4908 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4909 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4910 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73 4911 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4912 .writefn = tlbi_aa64_vae1_write },
168aa23b 4913 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4914 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73 4915 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4916 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4917 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4918 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73 4919 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4920 .writefn = tlbi_aa64_vae1_write },
168aa23b 4921 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4922 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73 4923 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4924 .writefn = tlbi_aa64_vae1_write },
168aa23b 4925 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4926 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73 4927 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4928 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
4929 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4930 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
bf05340c 4931 .access = PL2_W, .type = ARM_CP_NOP },
cea66e91
PM
4932 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4933 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
bf05340c 4934 .access = PL2_W, .type = ARM_CP_NOP },
83ddf975
PM
4935 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4936 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4937 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4938 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
4939 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4940 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4941 .access = PL2_W, .type = ARM_CP_NO_RAW,
4942 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
4943 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4944 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
bf05340c 4945 .access = PL2_W, .type = ARM_CP_NOP },
cea66e91
PM
4946 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4947 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
bf05340c 4948 .access = PL2_W, .type = ARM_CP_NOP },
83ddf975
PM
4949 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4950 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4951 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4952 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
4953 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4954 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4955 .access = PL2_W, .type = ARM_CP_NO_RAW,
4956 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
4957#ifndef CONFIG_USER_ONLY
4958 /* 64 bit address translation operations */
4959 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4960 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4961 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4962 .writefn = ats_write64 },
19525524
PM
4963 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4964 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4965 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4966 .writefn = ats_write64 },
19525524
PM
4967 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4968 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
0710b2fa
PM
4969 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4970 .writefn = ats_write64 },
19525524
PM
4971 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4972 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
0710b2fa
PM
4973 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4974 .writefn = ats_write64 },
2a47df95 4975 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 4976 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
0710b2fa
PM
4977 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4978 .writefn = ats_write64 },
2a47df95 4979 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 4980 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
0710b2fa
PM
4981 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4982 .writefn = ats_write64 },
2a47df95 4983 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 4984 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
0710b2fa
PM
4985 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4986 .writefn = ats_write64 },
2a47df95 4987 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 4988 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
0710b2fa
PM
4989 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4990 .writefn = ats_write64 },
2a47df95
PM
4991 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4992 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4993 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4994 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4995 .writefn = ats_write64 },
2a47df95
PM
4996 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4997 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4998 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4999 .writefn = ats_write64 },
c96fc9b5
EI
5000 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5001 .type = ARM_CP_ALIAS,
5002 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5003 .access = PL1_RW, .resetvalue = 0,
5004 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5005 .writefn = par_write },
19525524 5006#endif
995939a6 5007 /* TLB invalidate last level of translation table walk */
9449fdf6 5008 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
30881b73
RH
5009 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5010 .writefn = tlbimva_is_write },
9449fdf6 5011 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
30881b73 5012 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 5013 .writefn = tlbimvaa_is_write },
9449fdf6 5014 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73
RH
5015 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5016 .writefn = tlbimva_write },
9449fdf6 5017 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73
RH
5018 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5019 .writefn = tlbimvaa_write },
541ef8c2
SS
5020 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5021 .type = ARM_CP_NO_RAW, .access = PL2_W,
5022 .writefn = tlbimva_hyp_write },
5023 { .name = "TLBIMVALHIS",
5024 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5025 .type = ARM_CP_NO_RAW, .access = PL2_W,
5026 .writefn = tlbimva_hyp_is_write },
5027 { .name = "TLBIIPAS2",
5028 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
bf05340c 5029 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
5030 { .name = "TLBIIPAS2IS",
5031 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
bf05340c 5032 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
5033 { .name = "TLBIIPAS2L",
5034 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
bf05340c 5035 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
5036 { .name = "TLBIIPAS2LIS",
5037 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
bf05340c 5038 .type = ARM_CP_NOP, .access = PL2_W },
9449fdf6
PM
5039 /* 32 bit cache operations */
5040 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a 5041 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6
PM
5042 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5043 .type = ARM_CP_NOP, .access = PL1_W },
5044 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a 5045 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6 5046 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
38262d8a 5047 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6
PM
5048 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5049 .type = ARM_CP_NOP, .access = PL1_W },
5050 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5051 .type = ARM_CP_NOP, .access = PL1_W },
5052 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e 5053 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5054 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 5055 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5056 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
1bed4d2e 5057 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5058 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 5059 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5060 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
38262d8a 5061 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6 5062 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
1bed4d2e 5063 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5064 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 5065 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5066 /* MMU Domain access control / MPU write buffer control */
0c17d68c 5067 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
84929218 5068 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
5069 .writefn = dacr_write, .raw_writefn = raw_write,
5070 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5071 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 5072 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5073 .type = ARM_CP_ALIAS,
a0618a19 5074 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
5075 .access = PL1_RW,
5076 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 5077 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5078 .type = ARM_CP_ALIAS,
a65f1de9 5079 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5080 .access = PL1_RW,
5081 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
5082 /* We rely on the access checks not allowing the guest to write to the
5083 * state field when SPSel indicates that it's being used as the stack
5084 * pointer.
5085 */
5086 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5087 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5088 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 5089 .type = ARM_CP_ALIAS,
f502cfc2 5090 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
5091 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5092 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5093 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 5094 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
5095 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5096 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 5097 .type = ARM_CP_NO_RAW,
f502cfc2 5098 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
5099 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5100 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5101 .type = ARM_CP_ALIAS,
5102 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
5103 .access = PL2_RW, .accessfn = fpexc32_access },
6a43e0b6
PM
5104 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5105 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5106 .access = PL2_RW, .resetvalue = 0,
5107 .writefn = dacr_write, .raw_writefn = raw_write,
5108 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5109 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5110 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5111 .access = PL2_RW, .resetvalue = 0,
5112 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5113 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5114 .type = ARM_CP_ALIAS,
5115 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5116 .access = PL2_RW,
5117 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5118 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5119 .type = ARM_CP_ALIAS,
5120 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5121 .access = PL2_RW,
5122 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5123 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5124 .type = ARM_CP_ALIAS,
5125 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5126 .access = PL2_RW,
5127 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5128 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5129 .type = ARM_CP_ALIAS,
5130 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5131 .access = PL2_RW,
5132 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73
PM
5133 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5134 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5135 .resetvalue = 0,
5136 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5137 { .name = "SDCR", .type = ARM_CP_ALIAS,
5138 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5139 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5140 .writefn = sdcr_write,
5141 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
5142 REGINFO_SENTINEL
5143};
5144
d42e3c26 5145/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4771cd01 5146static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
d79e0c06 5147 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5148 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5149 .access = PL2_RW,
5150 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
ce4afed8 5151 { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH,
f149e3e8
EI
5152 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5153 .access = PL2_RW,
ce4afed8 5154 .type = ARM_CP_CONST, .resetvalue = 0 },
831a2fca
PM
5155 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5156 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5157 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e78e33
PM
5158 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5159 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5160 .access = PL2_RW,
5161 .type = ARM_CP_CONST, .resetvalue = 0 },
c6f19164
GB
5162 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5163 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5164 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
95f949ac
EI
5165 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5166 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5167 .access = PL2_RW, .type = ARM_CP_CONST,
5168 .resetvalue = 0 },
5169 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5170 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac 5171 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2179ef95
PM
5172 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5173 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5174 .access = PL2_RW, .type = ARM_CP_CONST,
5175 .resetvalue = 0 },
55b53c71 5176 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5177 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5178 .access = PL2_RW, .type = ARM_CP_CONST,
5179 .resetvalue = 0 },
37cd6c24
PM
5180 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5181 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5182 .access = PL2_RW, .type = ARM_CP_CONST,
5183 .resetvalue = 0 },
5184 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5185 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5186 .access = PL2_RW, .type = ARM_CP_CONST,
5187 .resetvalue = 0 },
06ec4c8c
EI
5188 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5189 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5190 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e9c2fe
EI
5191 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
5192 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
93dd1e61 5193 .access = PL2_RW, .accessfn = access_el3_aa32ns,
68e9c2fe 5194 .type = ARM_CP_CONST, .resetvalue = 0 },
b698e9cf
EI
5195 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5196 .cp = 15, .opc1 = 6, .crm = 2,
5197 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5198 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
5199 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5200 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5201 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b9cb5323
EI
5202 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5203 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5204 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
ff05f37b
EI
5205 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5206 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5207 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
a57633c0
EI
5208 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5209 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5210 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5211 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5212 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5213 .resetvalue = 0 },
0b6440af
EI
5214 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5215 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5216 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
edac4d8a
EI
5217 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5218 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5219 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5220 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5221 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5222 .resetvalue = 0 },
b0e66d95
EI
5223 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5224 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5225 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5226 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5227 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5228 .resetvalue = 0 },
5229 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5230 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5231 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5232 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5233 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5234 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
14cc7b54
SF
5235 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5236 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
d6c8cf81
PM
5237 .access = PL2_RW, .accessfn = access_tda,
5238 .type = ARM_CP_CONST, .resetvalue = 0 },
59e05530
EI
5239 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
5240 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
93dd1e61 5241 .access = PL2_RW, .accessfn = access_el3_aa32ns,
59e05530 5242 .type = ARM_CP_CONST, .resetvalue = 0 },
2a5a9abd
AF
5243 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5244 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5245 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
cba517c3
PM
5246 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5247 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5248 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5249 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5250 .type = ARM_CP_CONST,
5251 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5252 .access = PL2_RW, .resetvalue = 0 },
d42e3c26
EI
5253 REGINFO_SENTINEL
5254};
5255
ce4afed8
PM
5256/* Ditto, but for registers which exist in ARMv8 but not v7 */
5257static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = {
5258 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5259 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5260 .access = PL2_RW,
5261 .type = ARM_CP_CONST, .resetvalue = 0 },
5262 REGINFO_SENTINEL
5263};
5264
d1fb4da2 5265static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
f149e3e8 5266{
2fc0cc0e 5267 ARMCPU *cpu = env_archcpu(env);
d1fb4da2
RH
5268
5269 if (arm_feature(env, ARM_FEATURE_V8)) {
5270 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5271 } else {
5272 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5273 }
f149e3e8
EI
5274
5275 if (arm_feature(env, ARM_FEATURE_EL3)) {
5276 valid_mask &= ~HCR_HCD;
77077a83
JK
5277 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5278 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5279 * However, if we're using the SMC PSCI conduit then QEMU is
5280 * effectively acting like EL3 firmware and so the guest at
5281 * EL2 should retain the ability to prevent EL1 from being
5282 * able to make SMC calls into the ersatz firmware, so in
5283 * that case HCR.TSC should be read/write.
5284 */
f149e3e8
EI
5285 valid_mask &= ~HCR_TSC;
5286 }
d1fb4da2
RH
5287
5288 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5289 if (cpu_isar_feature(aa64_vh, cpu)) {
5290 valid_mask |= HCR_E2H;
5291 }
5292 if (cpu_isar_feature(aa64_lor, cpu)) {
5293 valid_mask |= HCR_TLOR;
5294 }
5295 if (cpu_isar_feature(aa64_pauth, cpu)) {
5296 valid_mask |= HCR_API | HCR_APK;
5297 }
8ddb300b
RH
5298 if (cpu_isar_feature(aa64_mte, cpu)) {
5299 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5300 }
ef682cdb 5301 }
f149e3e8
EI
5302
5303 /* Clear RES0 bits. */
5304 value &= valid_mask;
5305
8ddb300b
RH
5306 /*
5307 * These bits change the MMU setup:
f149e3e8
EI
5308 * HCR_VM enables stage 2 translation
5309 * HCR_PTW forbids certain page-table setups
8ddb300b
RH
5310 * HCR_DC disables stage1 and enables stage2 translation
5311 * HCR_DCT enables tagging on (disabled) stage1 translation
f149e3e8 5312 */
8ddb300b 5313 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT)) {
d10eb08f 5314 tlb_flush(CPU(cpu));
f149e3e8 5315 }
ce4afed8 5316 env->cp15.hcr_el2 = value;
89430fc6
PM
5317
5318 /*
5319 * Updates to VI and VF require us to update the status of
5320 * virtual interrupts, which are the logical OR of these bits
5321 * and the state of the input lines from the GIC. (This requires
5322 * that we have the iothread lock, which is done by marking the
5323 * reginfo structs as ARM_CP_IO.)
5324 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5325 * possible for it to be taken immediately, because VIRQ and
5326 * VFIQ are masked unless running at EL0 or EL1, and HCR
5327 * can only be written at EL2.
5328 */
5329 g_assert(qemu_mutex_iothread_locked());
5330 arm_cpu_update_virq(cpu);
5331 arm_cpu_update_vfiq(cpu);
ce4afed8
PM
5332}
5333
d1fb4da2
RH
5334static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5335{
5336 do_hcr_write(env, value, 0);
5337}
5338
ce4afed8
PM
5339static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5340 uint64_t value)
5341{
5342 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5343 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
d1fb4da2 5344 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
ce4afed8
PM
5345}
5346
5347static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5348 uint64_t value)
5349{
5350 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5351 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
d1fb4da2 5352 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
f149e3e8
EI
5353}
5354
f7778444
RH
5355/*
5356 * Return the effective value of HCR_EL2.
5357 * Bits that are not included here:
5358 * RW (read from SCR_EL3.RW as needed)
5359 */
5360uint64_t arm_hcr_el2_eff(CPUARMState *env)
5361{
5362 uint64_t ret = env->cp15.hcr_el2;
5363
e6ef0169 5364 if (!arm_is_el2_enabled(env)) {
f7778444
RH
5365 /*
5366 * "This register has no effect if EL2 is not enabled in the
5367 * current Security state". This is ARMv8.4-SecEL2 speak for
5368 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5369 *
5370 * Prior to that, the language was "In an implementation that
5371 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5372 * as if this field is 0 for all purposes other than a direct
5373 * read or write access of HCR_EL2". With lots of enumeration
5374 * on a per-field basis. In current QEMU, this is condition
5375 * is arm_is_secure_below_el3.
5376 *
5377 * Since the v8.4 language applies to the entire register, and
5378 * appears to be backward compatible, use that.
5379 */
4990e1d3
RH
5380 return 0;
5381 }
5382
5383 /*
5384 * For a cpu that supports both aarch64 and aarch32, we can set bits
5385 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5386 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5387 */
5388 if (!arm_el_is_aa64(env, 2)) {
5389 uint64_t aa32_valid;
5390
5391 /*
5392 * These bits are up-to-date as of ARMv8.6.
5393 * For HCR, it's easiest to list just the 2 bits that are invalid.
5394 * For HCR2, list those that are valid.
5395 */
5396 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5397 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5398 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5399 ret &= aa32_valid;
5400 }
5401
5402 if (ret & HCR_TGE) {
5403 /* These bits are up-to-date as of ARMv8.6. */
f7778444
RH
5404 if (ret & HCR_E2H) {
5405 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5406 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5407 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4990e1d3
RH
5408 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5409 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5410 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
f7778444
RH
5411 } else {
5412 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5413 }
5414 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5415 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5416 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5417 HCR_TLOR);
5418 }
5419
5420 return ret;
5421}
5422
fc1120a7
PM
5423static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5424 uint64_t value)
5425{
5426 /*
5427 * For A-profile AArch32 EL3, if NSACR.CP10
5428 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5429 */
5430 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5431 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5432 value &= ~(0x3 << 10);
5433 value |= env->cp15.cptr_el[2] & (0x3 << 10);
5434 }
5435 env->cp15.cptr_el[2] = value;
5436}
5437
5438static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5439{
5440 /*
5441 * For A-profile AArch32 EL3, if NSACR.CP10
5442 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5443 */
5444 uint64_t value = env->cp15.cptr_el[2];
5445
5446 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5447 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5448 value |= 0x3 << 10;
5449 }
5450 return value;
5451}
5452
4771cd01 5453static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8 5454 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
89430fc6 5455 .type = ARM_CP_IO,
f149e3e8
EI
5456 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5457 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5458 .writefn = hcr_write },
ce4afed8 5459 { .name = "HCR", .state = ARM_CP_STATE_AA32,
89430fc6 5460 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5461 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5462 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5463 .writefn = hcr_writelow },
831a2fca
PM
5464 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5465 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5466 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3b685ba7 5467 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5468 .type = ARM_CP_ALIAS,
3b685ba7
EI
5469 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5470 .access = PL2_RW,
5471 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
68e78e33 5472 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
f2c30f42
EI
5473 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5474 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
cba517c3 5475 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
63b60551
EI
5476 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5477 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
cba517c3
PM
5478 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5479 .type = ARM_CP_ALIAS,
5480 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5481 .access = PL2_RW,
5482 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
3b685ba7 5483 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5484 .type = ARM_CP_ALIAS,
3b685ba7 5485 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5486 .access = PL2_RW,
5487 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d79e0c06 5488 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5489 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5490 .access = PL2_RW, .writefn = vbar_write,
5491 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5492 .resetvalue = 0 },
884b4dee
GB
5493 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5494 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5495 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 5496 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
5497 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5498 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5499 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
fc1120a7
PM
5500 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5501 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
95f949ac
EI
5502 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5503 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5504 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5505 .resetvalue = 0 },
5506 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5507 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac
EI
5508 .access = PL2_RW, .type = ARM_CP_ALIAS,
5509 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
5510 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5511 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5512 .access = PL2_RW, .type = ARM_CP_CONST,
5513 .resetvalue = 0 },
5514 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
55b53c71 5515 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5516 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5517 .access = PL2_RW, .type = ARM_CP_CONST,
5518 .resetvalue = 0 },
37cd6c24
PM
5519 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5520 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5521 .access = PL2_RW, .type = ARM_CP_CONST,
5522 .resetvalue = 0 },
5523 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5524 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5525 .access = PL2_RW, .type = ARM_CP_CONST,
5526 .resetvalue = 0 },
06ec4c8c
EI
5527 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5528 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
d06dc933
RH
5529 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5530 /* no .raw_writefn or .resetfn needed as we never use mask/base_mask */
06ec4c8c 5531 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
5532 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5533 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5534 .type = ARM_CP_ALIAS,
68e9c2fe
EI
5535 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5536 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5537 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5538 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112
PM
5539 .access = PL2_RW,
5540 /* no .writefn needed as this can't cause an ASID change;
5541 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
5542 */
68e9c2fe 5543 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
5544 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5545 .cp = 15, .opc1 = 6, .crm = 2,
5546 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5547 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5548 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5549 .writefn = vttbr_write },
5550 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5551 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5552 .access = PL2_RW, .writefn = vttbr_write,
5553 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
5554 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5555 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5556 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5557 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
5558 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5559 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5560 .access = PL2_RW, .resetvalue = 0,
5561 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
5562 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5563 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
ed30da8e 5564 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
a57633c0
EI
5565 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5566 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5567 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 5568 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
5569 { .name = "TLBIALLNSNH",
5570 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5571 .type = ARM_CP_NO_RAW, .access = PL2_W,
5572 .writefn = tlbiall_nsnh_write },
5573 { .name = "TLBIALLNSNHIS",
5574 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5575 .type = ARM_CP_NO_RAW, .access = PL2_W,
5576 .writefn = tlbiall_nsnh_is_write },
5577 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5578 .type = ARM_CP_NO_RAW, .access = PL2_W,
5579 .writefn = tlbiall_hyp_write },
5580 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5581 .type = ARM_CP_NO_RAW, .access = PL2_W,
5582 .writefn = tlbiall_hyp_is_write },
5583 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5584 .type = ARM_CP_NO_RAW, .access = PL2_W,
5585 .writefn = tlbimva_hyp_write },
5586 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5587 .type = ARM_CP_NO_RAW, .access = PL2_W,
5588 .writefn = tlbimva_hyp_is_write },
51da9014
EI
5589 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5590 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5591 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5592 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
5593 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5594 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5595 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5596 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
5597 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5598 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5599 .access = PL2_W, .type = ARM_CP_NO_RAW,
5600 .writefn = tlbi_aa64_vae2_write },
5601 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5602 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5603 .access = PL2_W, .type = ARM_CP_NO_RAW,
5604 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
5605 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5606 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5607 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5608 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
5609 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5610 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5611 .access = PL2_W, .type = ARM_CP_NO_RAW,
5612 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 5613#ifndef CONFIG_USER_ONLY
2a47df95
PM
5614 /* Unlike the other EL2-related AT operations, these must
5615 * UNDEF from EL3 if EL2 is not implemented, which is why we
5616 * define them here rather than with the rest of the AT ops.
5617 */
5618 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5619 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5620 .access = PL2_W, .accessfn = at_s1e2_access,
0710b2fa 5621 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
2a47df95
PM
5622 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5623 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5624 .access = PL2_W, .accessfn = at_s1e2_access,
0710b2fa 5625 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
14db7fe0
PM
5626 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5627 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5628 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5629 * to behave as if SCR.NS was 1.
5630 */
5631 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5632 .access = PL2_W,
0710b2fa 5633 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
14db7fe0
PM
5634 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5635 .access = PL2_W,
0710b2fa 5636 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
0b6440af
EI
5637 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5638 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5639 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5640 * reset values as IMPDEF. We choose to reset to 3 to comply with
5641 * both ARMv7 and ARMv8.
5642 */
5643 .access = PL2_RW, .resetvalue = 3,
5644 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
5645 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5646 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5647 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5648 .writefn = gt_cntvoff_write,
5649 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5650 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5651 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5652 .writefn = gt_cntvoff_write,
5653 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
5654 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5655 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5656 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5657 .type = ARM_CP_IO, .access = PL2_RW,
5658 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5659 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5660 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5661 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5662 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5663 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5664 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 5665 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
5666 .resetfn = gt_hyp_timer_reset,
5667 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5668 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5669 .type = ARM_CP_IO,
5670 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5671 .access = PL2_RW,
5672 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5673 .resetvalue = 0,
5674 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 5675#endif
21c2dd77
PM
5676 /* The only field of MDCR_EL2 that has a defined architectural reset value
5677 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
5678 */
5679 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5680 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
5681 .access = PL2_RW, .resetvalue = PMCR_NUM_COUNTERS,
5682 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
5683 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5684 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5685 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5686 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5687 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5688 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5689 .access = PL2_RW,
5690 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
5691 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5692 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5693 .access = PL2_RW,
5694 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
5695 REGINFO_SENTINEL
5696};
5697
ce4afed8
PM
5698static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5699 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
89430fc6 5700 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5701 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5702 .access = PL2_RW,
5703 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5704 .writefn = hcr_writehigh },
5705 REGINFO_SENTINEL
5706};
5707
e9152ee9
RDC
5708static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5709 bool isread)
5710{
5711 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5712 return CP_ACCESS_OK;
5713 }
5714 return CP_ACCESS_TRAP_UNCATEGORIZED;
5715}
5716
5717static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5718 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5719 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5720 .access = PL2_RW, .accessfn = sel2_access,
5721 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5722 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5723 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5724 .access = PL2_RW, .accessfn = sel2_access,
5725 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5726 REGINFO_SENTINEL
5727};
5728
2f027fc5
PM
5729static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5730 bool isread)
5731{
5732 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
926c1b97 5733 * At Secure EL1 it traps to EL3 or EL2.
2f027fc5
PM
5734 */
5735 if (arm_current_el(env) == 3) {
5736 return CP_ACCESS_OK;
5737 }
5738 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
5739 if (env->cp15.scr_el3 & SCR_EEL2) {
5740 return CP_ACCESS_TRAP_EL2;
5741 }
2f027fc5
PM
5742 return CP_ACCESS_TRAP_EL3;
5743 }
5744 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5745 if (isread) {
5746 return CP_ACCESS_OK;
5747 }
5748 return CP_ACCESS_TRAP_UNCATEGORIZED;
5749}
5750
60fb1a87
GB
5751static const ARMCPRegInfo el3_cp_reginfo[] = {
5752 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5753 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5754 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
10d0ef3e 5755 .resetfn = scr_reset, .writefn = scr_write },
f80741d1 5756 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
60fb1a87 5757 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
5758 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5759 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 5760 .writefn = scr_write },
60fb1a87
GB
5761 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5762 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5763 .access = PL3_RW, .resetvalue = 0,
5764 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5765 { .name = "SDER",
5766 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5767 .access = PL3_RW, .resetvalue = 0,
5768 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 5769 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
5770 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5771 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 5772 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
5773 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5774 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
f478847f 5775 .access = PL3_RW, .resetvalue = 0,
7dd8c9af 5776 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
5777 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5778 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
5779 .access = PL3_RW,
5780 /* no .writefn needed as this can't cause an ASID change;
811595a2
PM
5781 * we must provide a .raw_writefn and .resetfn because we handle
5782 * reset and migration for the AArch32 TTBCR(S), which might be
5783 * using mask and base_mask.
6459b94c 5784 */
811595a2 5785 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee 5786 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 5787 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5788 .type = ARM_CP_ALIAS,
81547d66
EI
5789 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5790 .access = PL3_RW,
5791 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 5792 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
5793 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5794 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
5795 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5796 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5797 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 5798 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5799 .type = ARM_CP_ALIAS,
81547d66 5800 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5801 .access = PL3_RW,
5802 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
5803 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5804 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5805 .access = PL3_RW, .writefn = vbar_write,
5806 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5807 .resetvalue = 0 },
c6f19164
GB
5808 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5809 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5810 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5811 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
5812 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5813 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5814 .access = PL3_RW, .resetvalue = 0,
5815 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
5816 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5817 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5818 .access = PL3_RW, .type = ARM_CP_CONST,
5819 .resetvalue = 0 },
37cd6c24
PM
5820 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5821 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5822 .access = PL3_RW, .type = ARM_CP_CONST,
5823 .resetvalue = 0 },
5824 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5825 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5826 .access = PL3_RW, .type = ARM_CP_CONST,
5827 .resetvalue = 0 },
43efaa33
PM
5828 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5829 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5830 .access = PL3_W, .type = ARM_CP_NO_RAW,
5831 .writefn = tlbi_aa64_alle3is_write },
5832 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5833 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5834 .access = PL3_W, .type = ARM_CP_NO_RAW,
5835 .writefn = tlbi_aa64_vae3is_write },
5836 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5837 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5838 .access = PL3_W, .type = ARM_CP_NO_RAW,
5839 .writefn = tlbi_aa64_vae3is_write },
5840 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5841 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5842 .access = PL3_W, .type = ARM_CP_NO_RAW,
5843 .writefn = tlbi_aa64_alle3_write },
5844 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5845 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5846 .access = PL3_W, .type = ARM_CP_NO_RAW,
5847 .writefn = tlbi_aa64_vae3_write },
5848 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5849 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5850 .access = PL3_W, .type = ARM_CP_NO_RAW,
5851 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
5852 REGINFO_SENTINEL
5853};
5854
e2cce18f
RH
5855#ifndef CONFIG_USER_ONLY
5856/* Test if system register redirection is to occur in the current state. */
5857static bool redirect_for_e2h(CPUARMState *env)
5858{
5859 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5860}
5861
5862static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5863{
5864 CPReadFn *readfn;
5865
5866 if (redirect_for_e2h(env)) {
5867 /* Switch to the saved EL2 version of the register. */
5868 ri = ri->opaque;
5869 readfn = ri->readfn;
5870 } else {
5871 readfn = ri->orig_readfn;
5872 }
5873 if (readfn == NULL) {
5874 readfn = raw_read;
5875 }
5876 return readfn(env, ri);
5877}
5878
5879static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5880 uint64_t value)
5881{
5882 CPWriteFn *writefn;
5883
5884 if (redirect_for_e2h(env)) {
5885 /* Switch to the saved EL2 version of the register. */
5886 ri = ri->opaque;
5887 writefn = ri->writefn;
5888 } else {
5889 writefn = ri->orig_writefn;
5890 }
5891 if (writefn == NULL) {
5892 writefn = raw_write;
5893 }
5894 writefn(env, ri, value);
5895}
5896
5897static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5898{
5899 struct E2HAlias {
5900 uint32_t src_key, dst_key, new_key;
5901 const char *src_name, *dst_name, *new_name;
5902 bool (*feature)(const ARMISARegisters *id);
5903 };
5904
5905#define K(op0, op1, crn, crm, op2) \
5906 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5907
5908 static const struct E2HAlias aliases[] = {
5909 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
5910 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5911 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
5912 "CPACR", "CPTR_EL2", "CPACR_EL12" },
5913 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
5914 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5915 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
5916 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5917 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
5918 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5919 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
5920 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5921 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
5922 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5923 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
5924 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5925 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
5926 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5927 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
5928 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5929 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
5930 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5931 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5932 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5933 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5934 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5935 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5936 "VBAR", "VBAR_EL2", "VBAR_EL12" },
5937 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5938 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5939 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5940 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5941
5942 /*
5943 * Note that redirection of ZCR is mentioned in the description
5944 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5945 * not in the summary table.
5946 */
5947 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
5948 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5949
4b779ceb
RH
5950 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
5951 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5952
e2cce18f
RH
5953 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5954 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5955 };
5956#undef K
5957
5958 size_t i;
5959
5960 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5961 const struct E2HAlias *a = &aliases[i];
5962 ARMCPRegInfo *src_reg, *dst_reg;
5963
5964 if (a->feature && !a->feature(&cpu->isar)) {
5965 continue;
5966 }
5967
5968 src_reg = g_hash_table_lookup(cpu->cp_regs, &a->src_key);
5969 dst_reg = g_hash_table_lookup(cpu->cp_regs, &a->dst_key);
5970 g_assert(src_reg != NULL);
5971 g_assert(dst_reg != NULL);
5972
5973 /* Cross-compare names to detect typos in the keys. */
5974 g_assert(strcmp(src_reg->name, a->src_name) == 0);
5975 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5976
5977 /* None of the core system registers use opaque; we will. */
5978 g_assert(src_reg->opaque == NULL);
5979
5980 /* Create alias before redirection so we dup the right data. */
5981 if (a->new_key) {
5982 ARMCPRegInfo *new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
5983 uint32_t *new_key = g_memdup(&a->new_key, sizeof(uint32_t));
5984 bool ok;
5985
5986 new_reg->name = a->new_name;
5987 new_reg->type |= ARM_CP_ALIAS;
5988 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
5989 new_reg->access &= PL2_RW | PL3_RW;
5990
5991 ok = g_hash_table_insert(cpu->cp_regs, new_key, new_reg);
5992 g_assert(ok);
5993 }
5994
5995 src_reg->opaque = dst_reg;
5996 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5997 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5998 if (!src_reg->raw_readfn) {
5999 src_reg->raw_readfn = raw_read;
6000 }
6001 if (!src_reg->raw_writefn) {
6002 src_reg->raw_writefn = raw_write;
6003 }
6004 src_reg->readfn = el2_e2h_read;
6005 src_reg->writefn = el2_e2h_write;
6006 }
6007}
6008#endif
6009
3f208fd7
PM
6010static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6011 bool isread)
7da845b0 6012{
97475a89
RH
6013 int cur_el = arm_current_el(env);
6014
6015 if (cur_el < 2) {
6016 uint64_t hcr = arm_hcr_el2_eff(env);
6017
6018 if (cur_el == 0) {
6019 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6020 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6021 return CP_ACCESS_TRAP_EL2;
6022 }
6023 } else {
6024 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6025 return CP_ACCESS_TRAP;
6026 }
6027 if (hcr & HCR_TID2) {
6028 return CP_ACCESS_TRAP_EL2;
6029 }
6030 }
6031 } else if (hcr & HCR_TID2) {
6032 return CP_ACCESS_TRAP_EL2;
6033 }
7da845b0 6034 }
630fcd4d
MZ
6035
6036 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6037 return CP_ACCESS_TRAP_EL2;
6038 }
6039
7da845b0
PM
6040 return CP_ACCESS_OK;
6041}
6042
1424ca8d
DM
6043static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
6044 uint64_t value)
6045{
6046 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
6047 * read via a bit in OSLSR_EL1.
6048 */
6049 int oslock;
6050
6051 if (ri->state == ARM_CP_STATE_AA32) {
6052 oslock = (value == 0xC5ACCE55);
6053 } else {
6054 oslock = value & 1;
6055 }
6056
6057 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
6058}
6059
50300698 6060static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 6061 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
6062 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
6063 * unlike DBGDRAR it is never accessible from EL0.
6064 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
6065 * accessor.
50300698
PM
6066 */
6067 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
6068 .access = PL0_R, .accessfn = access_tdra,
6069 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
6070 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
6071 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
6072 .access = PL1_R, .accessfn = access_tdra,
6073 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 6074 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
6075 .access = PL0_R, .accessfn = access_tdra,
6076 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 6077 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
6078 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
6079 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 6080 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
6081 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
6082 .resetvalue = 0 },
49a6f3bf
NH
6083 /*
6084 * MDCCSR_EL0[30:29] map to EDSCR[30:29]. Simply RAZ as the external
6085 * Debug Communication Channel is not implemented.
6086 */
6087 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_AA64,
6088 .opc0 = 2, .opc1 = 3, .crn = 0, .crm = 1, .opc2 = 0,
6089 .access = PL0_R, .accessfn = access_tda,
6090 .type = ARM_CP_CONST, .resetvalue = 0 },
6091 /*
6092 * DBGDSCRint[15,12,5:2] map to MDSCR_EL1[15,12,5:2]. Map all bits as
6093 * it is unlikely a guest will care.
5e8b12ff
PM
6094 * We don't implement the configurable EL0 access.
6095 */
49a6f3bf
NH
6096 { .name = "DBGDSCRint", .state = ARM_CP_STATE_AA32,
6097 .cp = 14, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 6098 .type = ARM_CP_ALIAS,
d6c8cf81 6099 .access = PL1_R, .accessfn = access_tda,
b061a82b 6100 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
6101 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
6102 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 6103 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 6104 .accessfn = access_tdosa,
1424ca8d
DM
6105 .writefn = oslar_write },
6106 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
6107 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
6108 .access = PL1_R, .resetvalue = 10,
187f678d 6109 .accessfn = access_tdosa,
1424ca8d 6110 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
6111 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
6112 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
6113 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
6114 .access = PL1_RW, .accessfn = access_tdosa,
6115 .type = ARM_CP_NOP },
5e8b12ff
PM
6116 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
6117 * implement vector catch debug events yet.
6118 */
6119 { .name = "DBGVCR",
6120 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
6121 .access = PL1_RW, .accessfn = access_tda,
6122 .type = ARM_CP_NOP },
4d2ec4da
PM
6123 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
6124 * to save and restore a 32-bit guest's DBGVCR)
6125 */
6126 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
6127 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
6128 .access = PL2_RW, .accessfn = access_tda,
6129 .type = ARM_CP_NOP },
5dbdc434
PM
6130 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
6131 * Channel but Linux may try to access this register. The 32-bit
6132 * alias is DBGDCCINT.
6133 */
6134 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
6135 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
6136 .access = PL1_RW, .accessfn = access_tda,
6137 .type = ARM_CP_NOP },
50300698
PM
6138 REGINFO_SENTINEL
6139};
6140
6141static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
6142 /* 64 bit access versions of the (dummy) debug registers */
6143 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
6144 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6145 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
6146 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6147 REGINFO_SENTINEL
6148};
6149
60eed086
RH
6150/* Return the exception level to which exceptions should be taken
6151 * via SVEAccessTrap. If an exception should be routed through
6152 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
6153 * take care of raising that exception.
6154 * C.f. the ARM pseudocode function CheckSVEEnabled.
5be5e8ed 6155 */
ced31551 6156int sve_exception_el(CPUARMState *env, int el)
5be5e8ed
RH
6157{
6158#ifndef CONFIG_USER_ONLY
c2ddb7cf
RH
6159 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
6160
6161 if (el <= 1 && (hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
7701cee5
RH
6162 /* Check CPACR.ZEN. */
6163 switch (extract32(env->cp15.cpacr_el1, 16, 2)) {
6164 case 1:
6165 if (el != 0) {
6166 break;
6167 }
6168 /* fall through */
6169 case 0:
6170 case 2:
60eed086 6171 /* route_to_el2 */
c2ddb7cf 6172 return hcr_el2 & HCR_TGE ? 2 : 1;
5be5e8ed 6173 }
5be5e8ed 6174
60eed086 6175 /* Check CPACR.FPEN. */
7701cee5
RH
6176 switch (extract32(env->cp15.cpacr_el1, 20, 2)) {
6177 case 1:
6178 if (el != 0) {
6179 break;
6180 }
6181 /* fall through */
6182 case 0:
6183 case 2:
60eed086 6184 return 0;
5be5e8ed 6185 }
5be5e8ed
RH
6186 }
6187
d5a6fa2d
RH
6188 /*
6189 * CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE).
60eed086 6190 */
d5a6fa2d
RH
6191 if (el <= 2) {
6192 if (hcr_el2 & HCR_E2H) {
6193 /* Check CPTR_EL2.ZEN. */
6194 switch (extract32(env->cp15.cptr_el[2], 16, 2)) {
6195 case 1:
6196 if (el != 0 || !(hcr_el2 & HCR_TGE)) {
6197 break;
6198 }
6199 /* fall through */
6200 case 0:
6201 case 2:
6202 return 2;
6203 }
6204
6205 /* Check CPTR_EL2.FPEN. */
6206 switch (extract32(env->cp15.cptr_el[2], 20, 2)) {
6207 case 1:
6208 if (el == 2 || !(hcr_el2 & HCR_TGE)) {
6209 break;
6210 }
6211 /* fall through */
6212 case 0:
6213 case 2:
6214 return 0;
6215 }
6216 } else if (arm_is_el2_enabled(env)) {
6217 if (env->cp15.cptr_el[2] & CPTR_TZ) {
6218 return 2;
6219 }
6220 if (env->cp15.cptr_el[2] & CPTR_TFP) {
6221 return 0;
6222 }
60eed086 6223 }
5be5e8ed
RH
6224 }
6225
60eed086
RH
6226 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6227 if (arm_feature(env, ARM_FEATURE_EL3)
6228 && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
5be5e8ed
RH
6229 return 3;
6230 }
6231#endif
6232 return 0;
6233}
6234
ce440581 6235uint32_t aarch64_sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len)
0df9142d 6236{
6e553f2a 6237 uint32_t end_len;
0df9142d 6238
dc0bc8e7
RH
6239 start_len = MIN(start_len, ARM_MAX_VQ - 1);
6240 end_len = start_len;
6241
6e553f2a
RH
6242 if (!test_bit(start_len, cpu->sve_vq_map)) {
6243 end_len = find_last_bit(cpu->sve_vq_map, start_len);
6244 assert(end_len < start_len);
6245 }
6246 return end_len;
0df9142d
AJ
6247}
6248
0ab5953b
RH
6249/*
6250 * Given that SVE is enabled, return the vector length for EL.
6251 */
ced31551 6252uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
0ab5953b 6253{
2fc0cc0e 6254 ARMCPU *cpu = env_archcpu(env);
0ab5953b
RH
6255 uint32_t zcr_len = cpu->sve_max_vq - 1;
6256
63888fa7
RH
6257 if (el <= 1 &&
6258 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
0ab5953b
RH
6259 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
6260 }
6a02a732 6261 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
0ab5953b
RH
6262 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
6263 }
6a02a732 6264 if (arm_feature(env, ARM_FEATURE_EL3)) {
0ab5953b
RH
6265 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
6266 }
0df9142d 6267
ce440581 6268 return aarch64_sve_zcr_get_valid_len(cpu, zcr_len);
0ab5953b
RH
6269}
6270
5be5e8ed
RH
6271static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6272 uint64_t value)
6273{
0ab5953b
RH
6274 int cur_el = arm_current_el(env);
6275 int old_len = sve_zcr_len_for_el(env, cur_el);
6276 int new_len;
6277
5be5e8ed 6278 /* Bits other than [3:0] are RAZ/WI. */
7b351d98 6279 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5be5e8ed 6280 raw_write(env, ri, value & 0xf);
0ab5953b
RH
6281
6282 /*
6283 * Because we arrived here, we know both FP and SVE are enabled;
6284 * otherwise we would have trapped access to the ZCR_ELn register.
6285 */
6286 new_len = sve_zcr_len_for_el(env, cur_el);
6287 if (new_len < old_len) {
6288 aarch64_sve_narrow_vq(env, new_len + 1);
6289 }
5be5e8ed
RH
6290}
6291
6292static const ARMCPRegInfo zcr_el1_reginfo = {
6293 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6294 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6295 .access = PL1_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6296 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6297 .writefn = zcr_write, .raw_writefn = raw_write
6298};
6299
6300static const ARMCPRegInfo zcr_el2_reginfo = {
6301 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6302 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6303 .access = PL2_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6304 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6305 .writefn = zcr_write, .raw_writefn = raw_write
6306};
6307
6308static const ARMCPRegInfo zcr_no_el2_reginfo = {
6309 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6310 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6311 .access = PL2_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6312 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
6313};
6314
6315static const ARMCPRegInfo zcr_el3_reginfo = {
6316 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6317 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6318 .access = PL3_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6319 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6320 .writefn = zcr_write, .raw_writefn = raw_write
6321};
6322
9ee98ce8
PM
6323void hw_watchpoint_update(ARMCPU *cpu, int n)
6324{
6325 CPUARMState *env = &cpu->env;
6326 vaddr len = 0;
6327 vaddr wvr = env->cp15.dbgwvr[n];
6328 uint64_t wcr = env->cp15.dbgwcr[n];
6329 int mask;
6330 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
6331
6332 if (env->cpu_watchpoint[n]) {
6333 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
6334 env->cpu_watchpoint[n] = NULL;
6335 }
6336
6337 if (!extract64(wcr, 0, 1)) {
6338 /* E bit clear : watchpoint disabled */
6339 return;
6340 }
6341
6342 switch (extract64(wcr, 3, 2)) {
6343 case 0:
6344 /* LSC 00 is reserved and must behave as if the wp is disabled */
6345 return;
6346 case 1:
6347 flags |= BP_MEM_READ;
6348 break;
6349 case 2:
6350 flags |= BP_MEM_WRITE;
6351 break;
6352 case 3:
6353 flags |= BP_MEM_ACCESS;
6354 break;
6355 }
6356
6357 /* Attempts to use both MASK and BAS fields simultaneously are
6358 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
6359 * thus generating a watchpoint for every byte in the masked region.
6360 */
6361 mask = extract64(wcr, 24, 4);
6362 if (mask == 1 || mask == 2) {
6363 /* Reserved values of MASK; we must act as if the mask value was
6364 * some non-reserved value, or as if the watchpoint were disabled.
6365 * We choose the latter.
6366 */
6367 return;
6368 } else if (mask) {
6369 /* Watchpoint covers an aligned area up to 2GB in size */
6370 len = 1ULL << mask;
6371 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
6372 * whether the watchpoint fires when the unmasked bits match; we opt
6373 * to generate the exceptions.
6374 */
6375 wvr &= ~(len - 1);
6376 } else {
6377 /* Watchpoint covers bytes defined by the byte address select bits */
6378 int bas = extract64(wcr, 5, 8);
6379 int basstart;
6380
9ee98ce8
PM
6381 if (extract64(wvr, 2, 1)) {
6382 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
6383 * ignored, and BAS[3:0] define which bytes to watch.
6384 */
6385 bas &= 0xf;
6386 }
ae1111d4
RH
6387
6388 if (bas == 0) {
6389 /* This must act as if the watchpoint is disabled */
6390 return;
6391 }
6392
9ee98ce8
PM
6393 /* The BAS bits are supposed to be programmed to indicate a contiguous
6394 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
6395 * we fire for each byte in the word/doubleword addressed by the WVR.
6396 * We choose to ignore any non-zero bits after the first range of 1s.
6397 */
6398 basstart = ctz32(bas);
6399 len = cto32(bas >> basstart);
6400 wvr += basstart;
6401 }
6402
6403 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
6404 &env->cpu_watchpoint[n]);
6405}
6406
6407void hw_watchpoint_update_all(ARMCPU *cpu)
6408{
6409 int i;
6410 CPUARMState *env = &cpu->env;
6411
6412 /* Completely clear out existing QEMU watchpoints and our array, to
6413 * avoid possible stale entries following migration load.
6414 */
6415 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
6416 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
6417
6418 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
6419 hw_watchpoint_update(cpu, i);
6420 }
6421}
6422
6423static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6424 uint64_t value)
6425{
2fc0cc0e 6426 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6427 int i = ri->crm;
6428
777ab8d8 6429 /*
9ee98ce8 6430 * Bits [1:0] are RES0.
777ab8d8
RH
6431 *
6432 * It is IMPLEMENTATION DEFINED whether [63:49] ([63:53] with FEAT_LVA)
6433 * are hardwired to the value of bit [48] ([52] with FEAT_LVA), or if
6434 * they contain the value written. It is CONSTRAINED UNPREDICTABLE
6435 * whether the RESS bits are ignored when comparing an address.
6436 *
6437 * Therefore we are allowed to compare the entire register, which lets
6438 * us avoid considering whether or not FEAT_LVA is actually enabled.
9ee98ce8 6439 */
777ab8d8 6440 value &= ~3ULL;
9ee98ce8
PM
6441
6442 raw_write(env, ri, value);
6443 hw_watchpoint_update(cpu, i);
6444}
6445
6446static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6447 uint64_t value)
6448{
2fc0cc0e 6449 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6450 int i = ri->crm;
6451
6452 raw_write(env, ri, value);
6453 hw_watchpoint_update(cpu, i);
6454}
6455
46747d15
PM
6456void hw_breakpoint_update(ARMCPU *cpu, int n)
6457{
6458 CPUARMState *env = &cpu->env;
6459 uint64_t bvr = env->cp15.dbgbvr[n];
6460 uint64_t bcr = env->cp15.dbgbcr[n];
6461 vaddr addr;
6462 int bt;
6463 int flags = BP_CPU;
6464
6465 if (env->cpu_breakpoint[n]) {
6466 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
6467 env->cpu_breakpoint[n] = NULL;
6468 }
6469
6470 if (!extract64(bcr, 0, 1)) {
6471 /* E bit clear : watchpoint disabled */
6472 return;
6473 }
6474
6475 bt = extract64(bcr, 20, 4);
6476
6477 switch (bt) {
6478 case 4: /* unlinked address mismatch (reserved if AArch64) */
6479 case 5: /* linked address mismatch (reserved if AArch64) */
6480 qemu_log_mask(LOG_UNIMP,
0221c8fd 6481 "arm: address mismatch breakpoint types not implemented\n");
46747d15
PM
6482 return;
6483 case 0: /* unlinked address match */
6484 case 1: /* linked address match */
6485 {
777ab8d8
RH
6486 /*
6487 * Bits [1:0] are RES0.
6488 *
6489 * It is IMPLEMENTATION DEFINED whether bits [63:49]
6490 * ([63:53] for FEAT_LVA) are hardwired to a copy of the sign bit
6491 * of the VA field ([48] or [52] for FEAT_LVA), or whether the
6492 * value is read as written. It is CONSTRAINED UNPREDICTABLE
6493 * whether the RESS bits are ignored when comparing an address.
6494 * Therefore we are allowed to compare the entire register, which
6495 * lets us avoid considering whether FEAT_LVA is actually enabled.
6496 *
6497 * The BAS field is used to allow setting breakpoints on 16-bit
6498 * wide instructions; it is CONSTRAINED UNPREDICTABLE whether
46747d15
PM
6499 * a bp will fire if the addresses covered by the bp and the addresses
6500 * covered by the insn overlap but the insn doesn't start at the
6501 * start of the bp address range. We choose to require the insn and
6502 * the bp to have the same address. The constraints on writing to
6503 * BAS enforced in dbgbcr_write mean we have only four cases:
6504 * 0b0000 => no breakpoint
6505 * 0b0011 => breakpoint on addr
6506 * 0b1100 => breakpoint on addr + 2
6507 * 0b1111 => breakpoint on addr
6508 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
6509 */
6510 int bas = extract64(bcr, 5, 4);
777ab8d8 6511 addr = bvr & ~3ULL;
46747d15
PM
6512 if (bas == 0) {
6513 return;
6514 }
6515 if (bas == 0xc) {
6516 addr += 2;
6517 }
6518 break;
6519 }
6520 case 2: /* unlinked context ID match */
6521 case 8: /* unlinked VMID match (reserved if no EL2) */
6522 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
6523 qemu_log_mask(LOG_UNIMP,
0221c8fd 6524 "arm: unlinked context breakpoint types not implemented\n");
46747d15
PM
6525 return;
6526 case 9: /* linked VMID match (reserved if no EL2) */
6527 case 11: /* linked context ID and VMID match (reserved if no EL2) */
6528 case 3: /* linked context ID match */
6529 default:
6530 /* We must generate no events for Linked context matches (unless
6531 * they are linked to by some other bp/wp, which is handled in
6532 * updates for the linking bp/wp). We choose to also generate no events
6533 * for reserved values.
6534 */
6535 return;
6536 }
6537
6538 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
6539}
6540
6541void hw_breakpoint_update_all(ARMCPU *cpu)
6542{
6543 int i;
6544 CPUARMState *env = &cpu->env;
6545
6546 /* Completely clear out existing QEMU breakpoints and our array, to
6547 * avoid possible stale entries following migration load.
6548 */
6549 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
6550 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
6551
6552 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
6553 hw_breakpoint_update(cpu, i);
6554 }
6555}
6556
6557static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6558 uint64_t value)
6559{
2fc0cc0e 6560 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6561 int i = ri->crm;
6562
6563 raw_write(env, ri, value);
6564 hw_breakpoint_update(cpu, i);
6565}
6566
6567static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6568 uint64_t value)
6569{
2fc0cc0e 6570 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6571 int i = ri->crm;
6572
6573 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
6574 * copy of BAS[0].
6575 */
6576 value = deposit64(value, 6, 1, extract64(value, 5, 1));
6577 value = deposit64(value, 8, 1, extract64(value, 7, 1));
6578
6579 raw_write(env, ri, value);
6580 hw_breakpoint_update(cpu, i);
6581}
6582
50300698 6583static void define_debug_regs(ARMCPU *cpu)
0b45451e 6584{
50300698
PM
6585 /* Define v7 and v8 architectural debug registers.
6586 * These are just dummy implementations for now.
0b45451e
PM
6587 */
6588 int i;
3ff6fc91 6589 int wrps, brps, ctx_cmps;
54a78718
RH
6590
6591 /*
6592 * The Arm ARM says DBGDIDR is optional and deprecated if EL1 cannot
6593 * use AArch32. Given that bit 15 is RES1, if the value is 0 then
6594 * the register must not exist for this cpu.
6595 */
6596 if (cpu->isar.dbgdidr != 0) {
6597 ARMCPRegInfo dbgdidr = {
6598 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0,
6599 .opc1 = 0, .opc2 = 0,
6600 .access = PL0_R, .accessfn = access_tda,
6601 .type = ARM_CP_CONST, .resetvalue = cpu->isar.dbgdidr,
6602 };
6603 define_one_arm_cp_reg(cpu, &dbgdidr);
6604 }
48eb3ae6 6605
3ff6fc91 6606 /* Note that all these register fields hold "number of Xs minus 1". */
88ce6c6e
PM
6607 brps = arm_num_brps(cpu);
6608 wrps = arm_num_wrps(cpu);
6609 ctx_cmps = arm_num_ctx_cmps(cpu);
3ff6fc91
PM
6610
6611 assert(ctx_cmps <= brps);
48eb3ae6 6612
50300698
PM
6613 define_arm_cp_regs(cpu, debug_cp_reginfo);
6614
6615 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
6616 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
6617 }
6618
88ce6c6e 6619 for (i = 0; i < brps; i++) {
0b45451e 6620 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6621 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
6622 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 6623 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6624 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
6625 .writefn = dbgbvr_write, .raw_writefn = raw_write
6626 },
10aae104
PM
6627 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
6628 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 6629 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6630 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
6631 .writefn = dbgbcr_write, .raw_writefn = raw_write
6632 },
48eb3ae6
PM
6633 REGINFO_SENTINEL
6634 };
6635 define_arm_cp_regs(cpu, dbgregs);
6636 }
6637
88ce6c6e 6638 for (i = 0; i < wrps; i++) {
48eb3ae6 6639 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6640 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
6641 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 6642 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6643 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
6644 .writefn = dbgwvr_write, .raw_writefn = raw_write
6645 },
10aae104
PM
6646 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
6647 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 6648 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6649 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
6650 .writefn = dbgwcr_write, .raw_writefn = raw_write
6651 },
6652 REGINFO_SENTINEL
0b45451e
PM
6653 };
6654 define_arm_cp_regs(cpu, dbgregs);
6655 }
6656}
6657
24183fb6
PM
6658static void define_pmu_regs(ARMCPU *cpu)
6659{
6660 /*
6661 * v7 performance monitor control register: same implementor
6662 * field as main ID register, and we implement four counters in
6663 * addition to the cycle count register.
6664 */
21c2dd77 6665 unsigned int i, pmcrn = PMCR_NUM_COUNTERS;
24183fb6
PM
6666 ARMCPRegInfo pmcr = {
6667 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6668 .access = PL0_RW,
6669 .type = ARM_CP_IO | ARM_CP_ALIAS,
6670 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6671 .accessfn = pmreg_access, .writefn = pmcr_write,
6672 .raw_writefn = raw_write,
6673 };
6674 ARMCPRegInfo pmcr64 = {
6675 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6676 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6677 .access = PL0_RW, .accessfn = pmreg_access,
6678 .type = ARM_CP_IO,
6679 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
21c2dd77
PM
6680 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT) |
6681 PMCRLC,
24183fb6
PM
6682 .writefn = pmcr_write, .raw_writefn = raw_write,
6683 };
6684 define_one_arm_cp_reg(cpu, &pmcr);
6685 define_one_arm_cp_reg(cpu, &pmcr64);
6686 for (i = 0; i < pmcrn; i++) {
6687 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6688 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6689 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6690 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6691 ARMCPRegInfo pmev_regs[] = {
6692 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6693 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6694 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6695 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6696 .accessfn = pmreg_access },
6697 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6698 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6699 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6700 .type = ARM_CP_IO,
6701 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6702 .raw_readfn = pmevcntr_rawread,
6703 .raw_writefn = pmevcntr_rawwrite },
6704 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6705 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6706 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6707 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6708 .accessfn = pmreg_access },
6709 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6710 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6711 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6712 .type = ARM_CP_IO,
6713 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6714 .raw_writefn = pmevtyper_rawwrite },
6715 REGINFO_SENTINEL
6716 };
6717 define_arm_cp_regs(cpu, pmev_regs);
6718 g_free(pmevcntr_name);
6719 g_free(pmevcntr_el0_name);
6720 g_free(pmevtyper_name);
6721 g_free(pmevtyper_el0_name);
6722 }
a6179538 6723 if (cpu_isar_feature(aa32_pmu_8_1, cpu)) {
24183fb6
PM
6724 ARMCPRegInfo v81_pmu_regs[] = {
6725 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6726 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6727 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6728 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6729 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6730 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6731 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6732 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6733 REGINFO_SENTINEL
6734 };
6735 define_arm_cp_regs(cpu, v81_pmu_regs);
6736 }
15dd1ebd
PM
6737 if (cpu_isar_feature(any_pmu_8_4, cpu)) {
6738 static const ARMCPRegInfo v84_pmmir = {
6739 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6740 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6741 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6742 .resetvalue = 0
6743 };
6744 define_one_arm_cp_reg(cpu, &v84_pmmir);
6745 }
24183fb6
PM
6746}
6747
96a8b92e
PM
6748/* We don't know until after realize whether there's a GICv3
6749 * attached, and that is what registers the gicv3 sysregs.
6750 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6751 * at runtime.
6752 */
6753static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6754{
2fc0cc0e 6755 ARMCPU *cpu = env_archcpu(env);
8a130a7b 6756 uint64_t pfr1 = cpu->isar.id_pfr1;
96a8b92e
PM
6757
6758 if (env->gicv3state) {
6759 pfr1 |= 1 << 28;
6760 }
6761 return pfr1;
6762}
6763
976b99b6 6764#ifndef CONFIG_USER_ONLY
96a8b92e
PM
6765static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6766{
2fc0cc0e 6767 ARMCPU *cpu = env_archcpu(env);
47576b94 6768 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
96a8b92e
PM
6769
6770 if (env->gicv3state) {
6771 pfr0 |= 1 << 24;
6772 }
6773 return pfr0;
6774}
976b99b6 6775#endif
96a8b92e 6776
2d7137c1 6777/* Shared logic between LORID and the rest of the LOR* registers.
9bd268ba 6778 * Secure state exclusion has already been dealt with.
2d7137c1 6779 */
9bd268ba
RDC
6780static CPAccessResult access_lor_ns(CPUARMState *env,
6781 const ARMCPRegInfo *ri, bool isread)
2d7137c1
RH
6782{
6783 int el = arm_current_el(env);
6784
6785 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6786 return CP_ACCESS_TRAP_EL2;
6787 }
6788 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6789 return CP_ACCESS_TRAP_EL3;
6790 }
6791 return CP_ACCESS_OK;
6792}
6793
2d7137c1
RH
6794static CPAccessResult access_lor_other(CPUARMState *env,
6795 const ARMCPRegInfo *ri, bool isread)
6796{
6797 if (arm_is_secure_below_el3(env)) {
6798 /* Access denied in secure mode. */
6799 return CP_ACCESS_TRAP;
6800 }
9bd268ba 6801 return access_lor_ns(env, ri, isread);
2d7137c1
RH
6802}
6803
d8564ee4
RH
6804/*
6805 * A trivial implementation of ARMv8.1-LOR leaves all of these
6806 * registers fixed at 0, which indicates that there are zero
6807 * supported Limited Ordering regions.
6808 */
6809static const ARMCPRegInfo lor_reginfo[] = {
6810 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6811 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6812 .access = PL1_RW, .accessfn = access_lor_other,
6813 .type = ARM_CP_CONST, .resetvalue = 0 },
6814 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6815 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6816 .access = PL1_RW, .accessfn = access_lor_other,
6817 .type = ARM_CP_CONST, .resetvalue = 0 },
6818 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6819 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6820 .access = PL1_RW, .accessfn = access_lor_other,
6821 .type = ARM_CP_CONST, .resetvalue = 0 },
6822 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6823 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6824 .access = PL1_RW, .accessfn = access_lor_other,
6825 .type = ARM_CP_CONST, .resetvalue = 0 },
6826 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6827 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
9bd268ba 6828 .access = PL1_R, .accessfn = access_lor_ns,
d8564ee4
RH
6829 .type = ARM_CP_CONST, .resetvalue = 0 },
6830 REGINFO_SENTINEL
6831};
6832
967aa94f
RH
6833#ifdef TARGET_AARCH64
6834static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6835 bool isread)
6836{
6837 int el = arm_current_el(env);
6838
6839 if (el < 2 &&
6840 arm_feature(env, ARM_FEATURE_EL2) &&
6841 !(arm_hcr_el2_eff(env) & HCR_APK)) {
6842 return CP_ACCESS_TRAP_EL2;
6843 }
6844 if (el < 3 &&
6845 arm_feature(env, ARM_FEATURE_EL3) &&
6846 !(env->cp15.scr_el3 & SCR_APK)) {
6847 return CP_ACCESS_TRAP_EL3;
6848 }
6849 return CP_ACCESS_OK;
6850}
6851
6852static const ARMCPRegInfo pauth_reginfo[] = {
6853 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6854 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6855 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6856 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
967aa94f
RH
6857 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6858 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6859 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6860 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
967aa94f
RH
6861 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6862 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6863 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6864 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
967aa94f
RH
6865 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6866 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6867 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6868 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
967aa94f
RH
6869 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6870 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6871 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6872 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
967aa94f
RH
6873 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6874 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6875 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6876 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
967aa94f
RH
6877 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6878 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6879 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6880 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
967aa94f
RH
6881 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6882 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6883 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6884 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
967aa94f
RH
6885 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6886 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6887 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6888 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
967aa94f
RH
6889 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6890 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6891 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6892 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
967aa94f
RH
6893 REGINFO_SENTINEL
6894};
de390645 6895
84940ed8
RC
6896static const ARMCPRegInfo tlbirange_reginfo[] = {
6897 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
6898 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
6899 .access = PL1_W, .type = ARM_CP_NO_RAW,
6900 .writefn = tlbi_aa64_rvae1is_write },
6901 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
6902 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
6903 .access = PL1_W, .type = ARM_CP_NO_RAW,
6904 .writefn = tlbi_aa64_rvae1is_write },
6905 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
6906 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
6907 .access = PL1_W, .type = ARM_CP_NO_RAW,
6908 .writefn = tlbi_aa64_rvae1is_write },
6909 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
6910 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
6911 .access = PL1_W, .type = ARM_CP_NO_RAW,
6912 .writefn = tlbi_aa64_rvae1is_write },
6913 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
6914 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
6915 .access = PL1_W, .type = ARM_CP_NO_RAW,
6916 .writefn = tlbi_aa64_rvae1is_write },
6917 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
6918 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
6919 .access = PL1_W, .type = ARM_CP_NO_RAW,
6920 .writefn = tlbi_aa64_rvae1is_write },
6921 { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
6922 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
6923 .access = PL1_W, .type = ARM_CP_NO_RAW,
6924 .writefn = tlbi_aa64_rvae1is_write },
6925 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
6926 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
6927 .access = PL1_W, .type = ARM_CP_NO_RAW,
6928 .writefn = tlbi_aa64_rvae1is_write },
6929 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
6930 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
6931 .access = PL1_W, .type = ARM_CP_NO_RAW,
6932 .writefn = tlbi_aa64_rvae1_write },
6933 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
6934 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
6935 .access = PL1_W, .type = ARM_CP_NO_RAW,
6936 .writefn = tlbi_aa64_rvae1_write },
6937 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
6938 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
6939 .access = PL1_W, .type = ARM_CP_NO_RAW,
6940 .writefn = tlbi_aa64_rvae1_write },
6941 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
6942 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
6943 .access = PL1_W, .type = ARM_CP_NO_RAW,
6944 .writefn = tlbi_aa64_rvae1_write },
6945 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
6946 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
6947 .access = PL2_W, .type = ARM_CP_NOP },
6948 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
6949 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
6950 .access = PL2_W, .type = ARM_CP_NOP },
6951 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
6952 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
6953 .access = PL2_W, .type = ARM_CP_NO_RAW,
6954 .writefn = tlbi_aa64_rvae2is_write },
6955 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
6956 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
6957 .access = PL2_W, .type = ARM_CP_NO_RAW,
6958 .writefn = tlbi_aa64_rvae2is_write },
6959 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
6960 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
6961 .access = PL2_W, .type = ARM_CP_NOP },
6962 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
6963 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
6964 .access = PL2_W, .type = ARM_CP_NOP },
6965 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
6966 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
6967 .access = PL2_W, .type = ARM_CP_NO_RAW,
6968 .writefn = tlbi_aa64_rvae2is_write },
6969 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
6970 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
6971 .access = PL2_W, .type = ARM_CP_NO_RAW,
6972 .writefn = tlbi_aa64_rvae2is_write },
6973 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
6974 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
6975 .access = PL2_W, .type = ARM_CP_NO_RAW,
6976 .writefn = tlbi_aa64_rvae2_write },
6977 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
6978 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
6979 .access = PL2_W, .type = ARM_CP_NO_RAW,
6980 .writefn = tlbi_aa64_rvae2_write },
6981 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
6982 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
6983 .access = PL3_W, .type = ARM_CP_NO_RAW,
6984 .writefn = tlbi_aa64_rvae3is_write },
6985 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
6986 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
6987 .access = PL3_W, .type = ARM_CP_NO_RAW,
6988 .writefn = tlbi_aa64_rvae3is_write },
6989 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
6990 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
6991 .access = PL3_W, .type = ARM_CP_NO_RAW,
6992 .writefn = tlbi_aa64_rvae3is_write },
6993 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
6994 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
6995 .access = PL3_W, .type = ARM_CP_NO_RAW,
6996 .writefn = tlbi_aa64_rvae3is_write },
6997 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
6998 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
6999 .access = PL3_W, .type = ARM_CP_NO_RAW,
7000 .writefn = tlbi_aa64_rvae3_write },
7001 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7002 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7003 .access = PL3_W, .type = ARM_CP_NO_RAW,
7004 .writefn = tlbi_aa64_rvae3_write },
7005 REGINFO_SENTINEL
7006};
7007
7113d618
RC
7008static const ARMCPRegInfo tlbios_reginfo[] = {
7009 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7010 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
7011 .access = PL1_W, .type = ARM_CP_NO_RAW,
7012 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
7013 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7014 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
7015 .access = PL1_W, .type = ARM_CP_NO_RAW,
7016 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
7017 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7018 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
7019 .access = PL1_W, .type = ARM_CP_NO_RAW,
7020 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
7021 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7022 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
7023 .access = PL1_W, .type = ARM_CP_NO_RAW,
7024 .writefn = tlbi_aa64_vae1is_write },
7025 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7026 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
7027 .access = PL1_W, .type = ARM_CP_NO_RAW,
7028 .writefn = tlbi_aa64_vae1is_write },
7029 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7030 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
7031 .access = PL1_W, .type = ARM_CP_NO_RAW,
7032 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
7033 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7034 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
7035 .access = PL2_W, .type = ARM_CP_NO_RAW,
7036 .writefn = tlbi_aa64_alle2is_write },
b7469ef9
IH
7037 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7038 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
7039 .access = PL2_W, .type = ARM_CP_NO_RAW,
7040 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
7041 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7042 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7043 .access = PL2_W, .type = ARM_CP_NO_RAW,
7044 .writefn = tlbi_aa64_alle1is_write },
b7469ef9
IH
7045 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7046 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
7047 .access = PL2_W, .type = ARM_CP_NO_RAW,
7048 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
7049 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7050 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7051 .access = PL2_W, .type = ARM_CP_NO_RAW,
7052 .writefn = tlbi_aa64_alle1is_write },
7053 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7054 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7055 .access = PL2_W, .type = ARM_CP_NOP },
7056 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7057 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7058 .access = PL2_W, .type = ARM_CP_NOP },
7059 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7060 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7061 .access = PL2_W, .type = ARM_CP_NOP },
7062 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7063 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7064 .access = PL2_W, .type = ARM_CP_NOP },
7065 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7066 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7067 .access = PL3_W, .type = ARM_CP_NO_RAW,
7068 .writefn = tlbi_aa64_alle3is_write },
b7469ef9
IH
7069 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7070 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7071 .access = PL3_W, .type = ARM_CP_NO_RAW,
7072 .writefn = tlbi_aa64_vae3is_write },
7073 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7074 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7075 .access = PL3_W, .type = ARM_CP_NO_RAW,
7076 .writefn = tlbi_aa64_vae3is_write },
7113d618
RC
7077 REGINFO_SENTINEL
7078};
7079
de390645
RH
7080static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7081{
7082 Error *err = NULL;
7083 uint64_t ret;
7084
7085 /* Success sets NZCV = 0000. */
7086 env->NF = env->CF = env->VF = 0, env->ZF = 1;
7087
7088 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7089 /*
7090 * ??? Failed, for unknown reasons in the crypto subsystem.
7091 * The best we can do is log the reason and return the
7092 * timed-out indication to the guest. There is no reason
7093 * we know to expect this failure to be transitory, so the
7094 * guest may well hang retrying the operation.
7095 */
7096 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7097 ri->name, error_get_pretty(err));
7098 error_free(err);
7099
7100 env->ZF = 0; /* NZCF = 0100 */
7101 return 0;
7102 }
7103 return ret;
7104}
7105
7106/* We do not support re-seeding, so the two registers operate the same. */
7107static const ARMCPRegInfo rndr_reginfo[] = {
7108 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7109 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7110 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7111 .access = PL0_R, .readfn = rndr_readfn },
7112 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7113 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7114 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7115 .access = PL0_R, .readfn = rndr_readfn },
7116 REGINFO_SENTINEL
7117};
0d57b499
BM
7118
7119#ifndef CONFIG_USER_ONLY
7120static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7121 uint64_t value)
7122{
7123 ARMCPU *cpu = env_archcpu(env);
7124 /* CTR_EL0 System register -> DminLine, bits [19:16] */
7125 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7126 uint64_t vaddr_in = (uint64_t) value;
7127 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7128 void *haddr;
7129 int mem_idx = cpu_mmu_index(env, false);
7130
7131 /* This won't be crossing page boundaries */
7132 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7133 if (haddr) {
7134
7135 ram_addr_t offset;
7136 MemoryRegion *mr;
7137
7138 /* RCU lock is already being held */
7139 mr = memory_region_from_host(haddr, &offset);
7140
7141 if (mr) {
4dfe59d1 7142 memory_region_writeback(mr, offset, dline_size);
0d57b499
BM
7143 }
7144 }
7145}
7146
7147static const ARMCPRegInfo dcpop_reg[] = {
7148 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7149 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7150 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 7151 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
7152 REGINFO_SENTINEL
7153};
7154
7155static const ARMCPRegInfo dcpodp_reg[] = {
7156 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7157 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7158 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 7159 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
7160 REGINFO_SENTINEL
7161};
7162#endif /*CONFIG_USER_ONLY*/
7163
4b779ceb
RH
7164static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7165 bool isread)
7166{
7167 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7168 return CP_ACCESS_TRAP_EL2;
7169 }
7170
7171 return CP_ACCESS_OK;
7172}
7173
7174static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7175 bool isread)
7176{
7177 int el = arm_current_el(env);
7178
0da067f2 7179 if (el < 2 && arm_is_el2_enabled(env)) {
4301acd7
RH
7180 uint64_t hcr = arm_hcr_el2_eff(env);
7181 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7182 return CP_ACCESS_TRAP_EL2;
7183 }
4b779ceb
RH
7184 }
7185 if (el < 3 &&
7186 arm_feature(env, ARM_FEATURE_EL3) &&
7187 !(env->cp15.scr_el3 & SCR_ATA)) {
7188 return CP_ACCESS_TRAP_EL3;
7189 }
7190 return CP_ACCESS_OK;
7191}
7192
7193static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7194{
7195 return env->pstate & PSTATE_TCO;
7196}
7197
7198static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7199{
7200 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7201}
7202
7203static const ARMCPRegInfo mte_reginfo[] = {
7204 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7205 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7206 .access = PL1_RW, .accessfn = access_mte,
7207 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7208 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7209 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7210 .access = PL1_RW, .accessfn = access_mte,
7211 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7212 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7213 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7214 .access = PL2_RW, .accessfn = access_mte,
7215 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7216 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7217 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7218 .access = PL3_RW,
7219 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7220 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7221 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7222 .access = PL1_RW, .accessfn = access_mte,
7223 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7224 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7225 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7226 .access = PL1_RW, .accessfn = access_mte,
7227 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7228 { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7229 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7230 .access = PL1_R, .accessfn = access_aa64_tid5,
7231 .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7232 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7233 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7234 .type = ARM_CP_NO_RAW,
7235 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
5463df16
RH
7236 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7237 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7238 .type = ARM_CP_NOP, .access = PL1_W,
7239 .accessfn = aa64_cacheop_poc_access },
7240 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7241 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7242 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7243 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7244 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7245 .type = ARM_CP_NOP, .access = PL1_W,
7246 .accessfn = aa64_cacheop_poc_access },
7247 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7248 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7249 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7250 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7251 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7252 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7253 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7254 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7255 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7256 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7257 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7258 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7259 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7260 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7261 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
4b779ceb
RH
7262 REGINFO_SENTINEL
7263};
7264
7265static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7266 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7267 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7268 .type = ARM_CP_CONST, .access = PL0_RW, },
7269 REGINFO_SENTINEL
7270};
5463df16
RH
7271
7272static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7273 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7274 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7275 .type = ARM_CP_NOP, .access = PL0_W,
7276 .accessfn = aa64_cacheop_poc_access },
7277 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7278 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7279 .type = ARM_CP_NOP, .access = PL0_W,
7280 .accessfn = aa64_cacheop_poc_access },
7281 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7282 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7283 .type = ARM_CP_NOP, .access = PL0_W,
7284 .accessfn = aa64_cacheop_poc_access },
7285 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7286 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7287 .type = ARM_CP_NOP, .access = PL0_W,
7288 .accessfn = aa64_cacheop_poc_access },
7289 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7290 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7291 .type = ARM_CP_NOP, .access = PL0_W,
7292 .accessfn = aa64_cacheop_poc_access },
7293 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7294 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7295 .type = ARM_CP_NOP, .access = PL0_W,
7296 .accessfn = aa64_cacheop_poc_access },
7297 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7298 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7299 .type = ARM_CP_NOP, .access = PL0_W,
7300 .accessfn = aa64_cacheop_poc_access },
7301 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7302 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7303 .type = ARM_CP_NOP, .access = PL0_W,
7304 .accessfn = aa64_cacheop_poc_access },
eb821168
RH
7305 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7306 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7307 .access = PL0_W, .type = ARM_CP_DC_GVA,
7308#ifndef CONFIG_USER_ONLY
7309 /* Avoid overhead of an access check that always passes in user-mode */
7310 .accessfn = aa64_zva_access,
7311#endif
7312 },
7313 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7314 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7315 .access = PL0_W, .type = ARM_CP_DC_GZVA,
7316#ifndef CONFIG_USER_ONLY
7317 /* Avoid overhead of an access check that always passes in user-mode */
7318 .accessfn = aa64_zva_access,
7319#endif
7320 },
5463df16
RH
7321 REGINFO_SENTINEL
7322};
7323
967aa94f
RH
7324#endif
7325
cb570bd3
RH
7326static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7327 bool isread)
7328{
7329 int el = arm_current_el(env);
7330
7331 if (el == 0) {
7332 uint64_t sctlr = arm_sctlr(env, el);
7333 if (!(sctlr & SCTLR_EnRCTX)) {
7334 return CP_ACCESS_TRAP;
7335 }
7336 } else if (el == 1) {
7337 uint64_t hcr = arm_hcr_el2_eff(env);
7338 if (hcr & HCR_NV) {
7339 return CP_ACCESS_TRAP_EL2;
7340 }
7341 }
7342 return CP_ACCESS_OK;
7343}
7344
7345static const ARMCPRegInfo predinv_reginfo[] = {
7346 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7347 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7348 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7349 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7350 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7351 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7352 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7353 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7354 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7355 /*
7356 * Note the AArch32 opcodes have a different OPC1.
7357 */
7358 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7359 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7360 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7361 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7362 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7363 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7364 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7365 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7366 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7367 REGINFO_SENTINEL
7368};
7369
957e6155
PM
7370static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7371{
7372 /* Read the high 32 bits of the current CCSIDR */
7373 return extract64(ccsidr_read(env, ri), 32, 32);
7374}
7375
7376static const ARMCPRegInfo ccsidr2_reginfo[] = {
7377 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7378 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7379 .access = PL1_R,
7380 .accessfn = access_aa64_tid2,
7381 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7382 REGINFO_SENTINEL
7383};
7384
6a4ef4e5
MZ
7385static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7386 bool isread)
7387{
7388 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7389 return CP_ACCESS_TRAP_EL2;
7390 }
7391
7392 return CP_ACCESS_OK;
7393}
7394
7395static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7396 bool isread)
7397{
7398 if (arm_feature(env, ARM_FEATURE_V8)) {
7399 return access_aa64_tid3(env, ri, isread);
7400 }
7401
7402 return CP_ACCESS_OK;
7403}
7404
f96f3d5f
MZ
7405static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7406 bool isread)
7407{
7408 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7409 return CP_ACCESS_TRAP_EL2;
7410 }
7411
7412 return CP_ACCESS_OK;
7413}
7414
8e228c9e
PM
7415static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7416 const ARMCPRegInfo *ri, bool isread)
7417{
7418 /*
7419 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7420 * in v7A, not in v8A.
7421 */
7422 if (!arm_feature(env, ARM_FEATURE_V8) &&
7423 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7424 (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7425 return CP_ACCESS_TRAP_EL2;
7426 }
7427 return CP_ACCESS_OK;
7428}
7429
f96f3d5f
MZ
7430static const ARMCPRegInfo jazelle_regs[] = {
7431 { .name = "JIDR",
7432 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7433 .access = PL1_R, .accessfn = access_jazelle,
7434 .type = ARM_CP_CONST, .resetvalue = 0 },
7435 { .name = "JOSCR",
7436 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7437 .accessfn = access_joscr_jmcr,
f96f3d5f
MZ
7438 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7439 { .name = "JMCR",
7440 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7441 .accessfn = access_joscr_jmcr,
f96f3d5f
MZ
7442 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7443 REGINFO_SENTINEL
7444};
7445
e2a1a461
RH
7446static const ARMCPRegInfo vhe_reginfo[] = {
7447 { .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7448 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7449 .access = PL2_RW,
7450 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2]) },
ed30da8e
RH
7451 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7452 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7453 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7454 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8c94b071
RH
7455#ifndef CONFIG_USER_ONLY
7456 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7457 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7458 .fieldoffset =
7459 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7460 .type = ARM_CP_IO, .access = PL2_RW,
7461 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7462 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7463 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7464 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7465 .resetfn = gt_hv_timer_reset,
7466 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7467 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7468 .type = ARM_CP_IO,
7469 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7470 .access = PL2_RW,
7471 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7472 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
bb5972e4
RH
7473 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7474 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7475 .type = ARM_CP_IO | ARM_CP_ALIAS,
7476 .access = PL2_RW, .accessfn = e2h_access,
7477 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7478 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7479 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7480 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7481 .type = ARM_CP_IO | ARM_CP_ALIAS,
7482 .access = PL2_RW, .accessfn = e2h_access,
7483 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7484 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7485 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7486 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7487 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7488 .access = PL2_RW, .accessfn = e2h_access,
7489 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7490 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7491 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7492 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7493 .access = PL2_RW, .accessfn = e2h_access,
7494 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7495 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7496 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7497 .type = ARM_CP_IO | ARM_CP_ALIAS,
7498 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7499 .access = PL2_RW, .accessfn = e2h_access,
7500 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7501 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7502 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7503 .type = ARM_CP_IO | ARM_CP_ALIAS,
7504 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7505 .access = PL2_RW, .accessfn = e2h_access,
7506 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8c94b071 7507#endif
e2a1a461
RH
7508 REGINFO_SENTINEL
7509};
7510
04b07d29
RH
7511#ifndef CONFIG_USER_ONLY
7512static const ARMCPRegInfo ats1e1_reginfo[] = {
7513 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7514 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7515 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7516 .writefn = ats_write64 },
7517 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7518 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7519 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7520 .writefn = ats_write64 },
7521 REGINFO_SENTINEL
7522};
7523
7524static const ARMCPRegInfo ats1cp_reginfo[] = {
7525 { .name = "ATS1CPRP",
7526 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7527 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7528 .writefn = ats_write },
7529 { .name = "ATS1CPWP",
7530 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7531 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7532 .writefn = ats_write },
7533 REGINFO_SENTINEL
7534};
7535#endif
7536
f6287c24
PM
7537/*
7538 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7539 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7540 * is non-zero, which is never for ARMv7, optionally in ARMv8
7541 * and mandatorily for ARMv8.2 and up.
7542 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7543 * implementation is RAZ/WI we can ignore this detail, as we
7544 * do for ACTLR.
7545 */
7546static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7547 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7548 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
99602377
RH
7549 .access = PL1_RW, .accessfn = access_tacr,
7550 .type = ARM_CP_CONST, .resetvalue = 0 },
f6287c24
PM
7551 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7552 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7553 .access = PL2_RW, .type = ARM_CP_CONST,
7554 .resetvalue = 0 },
7555 REGINFO_SENTINEL
7556};
7557
2ceb98c0
PM
7558void register_cp_regs_for_features(ARMCPU *cpu)
7559{
7560 /* Register all the coprocessor registers based on feature bits */
7561 CPUARMState *env = &cpu->env;
7562 if (arm_feature(env, ARM_FEATURE_M)) {
7563 /* M profile has no coprocessor registers */
7564 return;
7565 }
7566
e9aa6c21 7567 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
7568 if (!arm_feature(env, ARM_FEATURE_V8)) {
7569 /* Must go early as it is full of wildcards that may be
7570 * overridden by later definitions.
7571 */
7572 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7573 }
7574
7d57f408 7575 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
7576 /* The ID registers all have impdef reset values */
7577 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
7578 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7579 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7580 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7581 .accessfn = access_aa32_tid3,
8a130a7b 7582 .resetvalue = cpu->isar.id_pfr0 },
96a8b92e
PM
7583 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7584 * the value of the GIC field until after we define these regs.
7585 */
0ff644a7
PM
7586 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7587 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e 7588 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 7589 .accessfn = access_aa32_tid3,
96a8b92e
PM
7590 .readfn = id_pfr1_read,
7591 .writefn = arm_cp_write_ignore },
0ff644a7
PM
7592 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7593 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7594 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7595 .accessfn = access_aa32_tid3,
a6179538 7596 .resetvalue = cpu->isar.id_dfr0 },
0ff644a7
PM
7597 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7598 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7599 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7600 .accessfn = access_aa32_tid3,
8515a092 7601 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
7602 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7603 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7604 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7605 .accessfn = access_aa32_tid3,
10054016 7606 .resetvalue = cpu->isar.id_mmfr0 },
0ff644a7
PM
7607 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7608 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7609 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7610 .accessfn = access_aa32_tid3,
10054016 7611 .resetvalue = cpu->isar.id_mmfr1 },
0ff644a7
PM
7612 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7613 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7614 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7615 .accessfn = access_aa32_tid3,
10054016 7616 .resetvalue = cpu->isar.id_mmfr2 },
0ff644a7
PM
7617 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7618 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7619 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7620 .accessfn = access_aa32_tid3,
10054016 7621 .resetvalue = cpu->isar.id_mmfr3 },
0ff644a7
PM
7622 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7623 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7624 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7625 .accessfn = access_aa32_tid3,
47576b94 7626 .resetvalue = cpu->isar.id_isar0 },
0ff644a7
PM
7627 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7628 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7629 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7630 .accessfn = access_aa32_tid3,
47576b94 7631 .resetvalue = cpu->isar.id_isar1 },
0ff644a7
PM
7632 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7633 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7634 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7635 .accessfn = access_aa32_tid3,
47576b94 7636 .resetvalue = cpu->isar.id_isar2 },
0ff644a7
PM
7637 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7638 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7639 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7640 .accessfn = access_aa32_tid3,
47576b94 7641 .resetvalue = cpu->isar.id_isar3 },
0ff644a7
PM
7642 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7643 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7644 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7645 .accessfn = access_aa32_tid3,
47576b94 7646 .resetvalue = cpu->isar.id_isar4 },
0ff644a7
PM
7647 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7648 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7649 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7650 .accessfn = access_aa32_tid3,
47576b94 7651 .resetvalue = cpu->isar.id_isar5 },
e20d84c1
PM
7652 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7653 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7654 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7655 .accessfn = access_aa32_tid3,
10054016 7656 .resetvalue = cpu->isar.id_mmfr4 },
802abf40 7657 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7658 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7659 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7660 .accessfn = access_aa32_tid3,
47576b94 7661 .resetvalue = cpu->isar.id_isar6 },
8515a092
PM
7662 REGINFO_SENTINEL
7663 };
7664 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
7665 define_arm_cp_regs(cpu, v6_cp_reginfo);
7666 } else {
7667 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7668 }
4d31c596
PM
7669 if (arm_feature(env, ARM_FEATURE_V6K)) {
7670 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7671 }
5e5cf9e3 7672 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 7673 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
7674 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7675 }
327dd510
AL
7676 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7677 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7678 }
e9aa6c21 7679 if (arm_feature(env, ARM_FEATURE_V7)) {
776d4e5c 7680 ARMCPRegInfo clidr = {
7da845b0
PM
7681 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7682 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
630fcd4d
MZ
7683 .access = PL1_R, .type = ARM_CP_CONST,
7684 .accessfn = access_aa64_tid2,
7685 .resetvalue = cpu->clidr
776d4e5c 7686 };
776d4e5c 7687 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 7688 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 7689 define_debug_regs(cpu);
24183fb6 7690 define_pmu_regs(cpu);
7d57f408
PM
7691 } else {
7692 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 7693 }
b0d2b7d0 7694 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
7695 /* AArch64 ID registers, which all have impdef reset values.
7696 * Note that within the ID register ranges the unused slots
7697 * must all RAZ, not UNDEF; future architecture versions may
7698 * define new registers here.
7699 */
e60cef86 7700 ARMCPRegInfo v8_idregs[] = {
976b99b6
AB
7701 /*
7702 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7703 * emulation because we don't know the right value for the
7704 * GIC field until after we define these regs.
96a8b92e 7705 */
e60cef86
PM
7706 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7707 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
976b99b6
AB
7708 .access = PL1_R,
7709#ifdef CONFIG_USER_ONLY
7710 .type = ARM_CP_CONST,
7711 .resetvalue = cpu->isar.id_aa64pfr0
7712#else
7713 .type = ARM_CP_NO_RAW,
6a4ef4e5 7714 .accessfn = access_aa64_tid3,
96a8b92e 7715 .readfn = id_aa64pfr0_read,
976b99b6
AB
7716 .writefn = arm_cp_write_ignore
7717#endif
7718 },
e60cef86
PM
7719 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7720 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7721 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7722 .accessfn = access_aa64_tid3,
47576b94 7723 .resetvalue = cpu->isar.id_aa64pfr1},
e20d84c1
PM
7724 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7725 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7726 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7727 .accessfn = access_aa64_tid3,
e20d84c1
PM
7728 .resetvalue = 0 },
7729 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7730 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7731 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7732 .accessfn = access_aa64_tid3,
e20d84c1 7733 .resetvalue = 0 },
9516d772 7734 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7735 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7736 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7737 .accessfn = access_aa64_tid3,
2dc10fa2 7738 .resetvalue = cpu->isar.id_aa64zfr0 },
e20d84c1
PM
7739 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7740 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7741 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7742 .accessfn = access_aa64_tid3,
e20d84c1
PM
7743 .resetvalue = 0 },
7744 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7745 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7746 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7747 .accessfn = access_aa64_tid3,
e20d84c1
PM
7748 .resetvalue = 0 },
7749 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7750 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7751 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7752 .accessfn = access_aa64_tid3,
e20d84c1 7753 .resetvalue = 0 },
e60cef86
PM
7754 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7755 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7756 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7757 .accessfn = access_aa64_tid3,
2a609df8 7758 .resetvalue = cpu->isar.id_aa64dfr0 },
e60cef86
PM
7759 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7760 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7761 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7762 .accessfn = access_aa64_tid3,
2a609df8 7763 .resetvalue = cpu->isar.id_aa64dfr1 },
e20d84c1
PM
7764 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7765 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7766 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7767 .accessfn = access_aa64_tid3,
e20d84c1
PM
7768 .resetvalue = 0 },
7769 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7770 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7771 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7772 .accessfn = access_aa64_tid3,
e20d84c1 7773 .resetvalue = 0 },
e60cef86
PM
7774 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7775 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7776 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7777 .accessfn = access_aa64_tid3,
e60cef86
PM
7778 .resetvalue = cpu->id_aa64afr0 },
7779 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7780 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7781 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7782 .accessfn = access_aa64_tid3,
e60cef86 7783 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
7784 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7785 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7786 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7787 .accessfn = access_aa64_tid3,
e20d84c1
PM
7788 .resetvalue = 0 },
7789 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7790 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7791 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7792 .accessfn = access_aa64_tid3,
e20d84c1 7793 .resetvalue = 0 },
e60cef86
PM
7794 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7795 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7796 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7797 .accessfn = access_aa64_tid3,
47576b94 7798 .resetvalue = cpu->isar.id_aa64isar0 },
e60cef86
PM
7799 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7800 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7801 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7802 .accessfn = access_aa64_tid3,
47576b94 7803 .resetvalue = cpu->isar.id_aa64isar1 },
e20d84c1
PM
7804 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7805 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7806 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7807 .accessfn = access_aa64_tid3,
e20d84c1
PM
7808 .resetvalue = 0 },
7809 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7810 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7811 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7812 .accessfn = access_aa64_tid3,
e20d84c1
PM
7813 .resetvalue = 0 },
7814 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7815 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7816 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7817 .accessfn = access_aa64_tid3,
e20d84c1
PM
7818 .resetvalue = 0 },
7819 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7820 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7821 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7822 .accessfn = access_aa64_tid3,
e20d84c1
PM
7823 .resetvalue = 0 },
7824 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7825 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7826 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7827 .accessfn = access_aa64_tid3,
e20d84c1
PM
7828 .resetvalue = 0 },
7829 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7830 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7831 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7832 .accessfn = access_aa64_tid3,
e20d84c1 7833 .resetvalue = 0 },
e60cef86
PM
7834 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7835 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7836 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7837 .accessfn = access_aa64_tid3,
3dc91ddb 7838 .resetvalue = cpu->isar.id_aa64mmfr0 },
e60cef86
PM
7839 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7840 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7841 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7842 .accessfn = access_aa64_tid3,
3dc91ddb 7843 .resetvalue = cpu->isar.id_aa64mmfr1 },
64761e10 7844 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7845 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7846 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7847 .accessfn = access_aa64_tid3,
64761e10 7848 .resetvalue = cpu->isar.id_aa64mmfr2 },
e20d84c1
PM
7849 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7850 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7851 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7852 .accessfn = access_aa64_tid3,
e20d84c1
PM
7853 .resetvalue = 0 },
7854 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7855 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7856 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7857 .accessfn = access_aa64_tid3,
e20d84c1
PM
7858 .resetvalue = 0 },
7859 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7860 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7861 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7862 .accessfn = access_aa64_tid3,
e20d84c1
PM
7863 .resetvalue = 0 },
7864 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7865 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7866 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7867 .accessfn = access_aa64_tid3,
e20d84c1
PM
7868 .resetvalue = 0 },
7869 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7870 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7871 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7872 .accessfn = access_aa64_tid3,
e20d84c1 7873 .resetvalue = 0 },
a50c0f51
PM
7874 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7875 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7876 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7877 .accessfn = access_aa64_tid3,
47576b94 7878 .resetvalue = cpu->isar.mvfr0 },
a50c0f51
PM
7879 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7880 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7881 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7882 .accessfn = access_aa64_tid3,
47576b94 7883 .resetvalue = cpu->isar.mvfr1 },
a50c0f51
PM
7884 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7885 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7886 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7887 .accessfn = access_aa64_tid3,
47576b94 7888 .resetvalue = cpu->isar.mvfr2 },
e20d84c1
PM
7889 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7890 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7891 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7892 .accessfn = access_aa64_tid3,
e20d84c1 7893 .resetvalue = 0 },
1d51bc96 7894 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7895 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7896 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7897 .accessfn = access_aa64_tid3,
1d51bc96 7898 .resetvalue = cpu->isar.id_pfr2 },
e20d84c1
PM
7899 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7900 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7901 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7902 .accessfn = access_aa64_tid3,
e20d84c1
PM
7903 .resetvalue = 0 },
7904 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7905 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7906 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7907 .accessfn = access_aa64_tid3,
e20d84c1
PM
7908 .resetvalue = 0 },
7909 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7910 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7911 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7912 .accessfn = access_aa64_tid3,
e20d84c1 7913 .resetvalue = 0 },
4054bfa9
AF
7914 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7915 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7916 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7917 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
4054bfa9
AF
7918 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7919 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7920 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7921 .resetvalue = cpu->pmceid0 },
7922 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7923 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7924 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7925 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
4054bfa9
AF
7926 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7927 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7928 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7929 .resetvalue = cpu->pmceid1 },
e60cef86
PM
7930 REGINFO_SENTINEL
7931 };
6c5c0fec
AB
7932#ifdef CONFIG_USER_ONLY
7933 ARMCPRegUserSpaceInfo v8_user_idregs[] = {
7934 { .name = "ID_AA64PFR0_EL1",
7935 .exported_bits = 0x000f000f00ff0000,
7936 .fixed_bits = 0x0000000000000011 },
7937 { .name = "ID_AA64PFR1_EL1",
7938 .exported_bits = 0x00000000000000f0 },
d040242e
AB
7939 { .name = "ID_AA64PFR*_EL1_RESERVED",
7940 .is_glob = true },
6c5c0fec
AB
7941 { .name = "ID_AA64ZFR0_EL1" },
7942 { .name = "ID_AA64MMFR0_EL1",
7943 .fixed_bits = 0x00000000ff000000 },
7944 { .name = "ID_AA64MMFR1_EL1" },
d040242e
AB
7945 { .name = "ID_AA64MMFR*_EL1_RESERVED",
7946 .is_glob = true },
6c5c0fec
AB
7947 { .name = "ID_AA64DFR0_EL1",
7948 .fixed_bits = 0x0000000000000006 },
7949 { .name = "ID_AA64DFR1_EL1" },
d040242e
AB
7950 { .name = "ID_AA64DFR*_EL1_RESERVED",
7951 .is_glob = true },
7952 { .name = "ID_AA64AFR*",
7953 .is_glob = true },
6c5c0fec
AB
7954 { .name = "ID_AA64ISAR0_EL1",
7955 .exported_bits = 0x00fffffff0fffff0 },
7956 { .name = "ID_AA64ISAR1_EL1",
7957 .exported_bits = 0x000000f0ffffffff },
d040242e
AB
7958 { .name = "ID_AA64ISAR*_EL1_RESERVED",
7959 .is_glob = true },
6c5c0fec
AB
7960 REGUSERINFO_SENTINEL
7961 };
7962 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7963#endif
be8e8128
GB
7964 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7965 if (!arm_feature(env, ARM_FEATURE_EL3) &&
7966 !arm_feature(env, ARM_FEATURE_EL2)) {
7967 ARMCPRegInfo rvbar = {
7968 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7969 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
7970 .access = PL1_R,
7971 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
be8e8128
GB
7972 };
7973 define_one_arm_cp_reg(cpu, &rvbar);
7974 }
e60cef86 7975 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
7976 define_arm_cp_regs(cpu, v8_cp_reginfo);
7977 }
3b685ba7 7978 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 7979 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
7980 ARMCPRegInfo vpidr_regs[] = {
7981 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7982 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7983 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
7984 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
7985 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
7986 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7987 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7988 .access = PL2_RW, .resetvalue = cpu->midr,
7989 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7990 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7991 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7992 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
7993 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
7994 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
7995 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7996 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7997 .access = PL2_RW,
7998 .resetvalue = vmpidr_def,
7999 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
8000 REGINFO_SENTINEL
8001 };
8002 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 8003 define_arm_cp_regs(cpu, el2_cp_reginfo);
ce4afed8
PM
8004 if (arm_feature(env, ARM_FEATURE_V8)) {
8005 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8006 }
e9152ee9
RDC
8007 if (cpu_isar_feature(aa64_sel2, cpu)) {
8008 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8009 }
be8e8128
GB
8010 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
8011 if (!arm_feature(env, ARM_FEATURE_EL3)) {
8012 ARMCPRegInfo rvbar = {
8013 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8014 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
8015 .access = PL2_R,
8016 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
be8e8128
GB
8017 };
8018 define_one_arm_cp_reg(cpu, &rvbar);
8019 }
d42e3c26
EI
8020 } else {
8021 /* If EL2 is missing but higher ELs are enabled, we need to
8022 * register the no_el2 reginfos.
8023 */
8024 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
8025 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
8026 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
8027 */
8028 ARMCPRegInfo vpidr_regs[] = {
8029 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
8030 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
93dd1e61 8031 .access = PL2_RW, .accessfn = access_el3_aa32ns,
731de9e6
EI
8032 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
8033 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
8034 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
8035 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
93dd1e61 8036 .access = PL2_RW, .accessfn = access_el3_aa32ns,
f0d574d6
EI
8037 .type = ARM_CP_NO_RAW,
8038 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
8039 REGINFO_SENTINEL
8040 };
8041 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 8042 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
ce4afed8
PM
8043 if (arm_feature(env, ARM_FEATURE_V8)) {
8044 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo);
8045 }
d42e3c26 8046 }
3b685ba7 8047 }
81547d66 8048 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 8049 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
8050 ARMCPRegInfo el3_regs[] = {
8051 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8052 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
8053 .access = PL3_R,
8054 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8055 },
e24fdd23
PM
8056 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8057 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8058 .access = PL3_RW,
8059 .raw_writefn = raw_write, .writefn = sctlr_write,
8060 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8061 .resetvalue = cpu->reset_sctlr },
8062 REGINFO_SENTINEL
be8e8128 8063 };
e24fdd23
PM
8064
8065 define_arm_cp_regs(cpu, el3_regs);
81547d66 8066 }
2f027fc5
PM
8067 /* The behaviour of NSACR is sufficiently various that we don't
8068 * try to describe it in a single reginfo:
8069 * if EL3 is 64 bit, then trap to EL3 from S EL1,
8070 * reads as constant 0xc00 from NS EL1 and NS EL2
8071 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8072 * if v7 without EL3, register doesn't exist
8073 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8074 */
8075 if (arm_feature(env, ARM_FEATURE_EL3)) {
8076 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8077 ARMCPRegInfo nsacr = {
8078 .name = "NSACR", .type = ARM_CP_CONST,
8079 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8080 .access = PL1_RW, .accessfn = nsacr_access,
8081 .resetvalue = 0xc00
8082 };
8083 define_one_arm_cp_reg(cpu, &nsacr);
8084 } else {
8085 ARMCPRegInfo nsacr = {
8086 .name = "NSACR",
8087 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8088 .access = PL3_RW | PL1_R,
8089 .resetvalue = 0,
8090 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8091 };
8092 define_one_arm_cp_reg(cpu, &nsacr);
8093 }
8094 } else {
8095 if (arm_feature(env, ARM_FEATURE_V8)) {
8096 ARMCPRegInfo nsacr = {
8097 .name = "NSACR", .type = ARM_CP_CONST,
8098 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8099 .access = PL1_R,
8100 .resetvalue = 0xc00
8101 };
8102 define_one_arm_cp_reg(cpu, &nsacr);
8103 }
8104 }
8105
452a0955 8106 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
8107 if (arm_feature(env, ARM_FEATURE_V6)) {
8108 /* PMSAv6 not implemented */
8109 assert(arm_feature(env, ARM_FEATURE_V7));
8110 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8111 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8112 } else {
8113 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8114 }
18032bec 8115 } else {
8e5d75c9 8116 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec 8117 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4036b7d1
PM
8118 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
8119 if (cpu_isar_feature(aa32_hpd, cpu)) {
ab638a32
RH
8120 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8121 }
18032bec 8122 }
c326b979
PM
8123 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8124 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8125 }
6cc7a3ae
PM
8126 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8127 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8128 }
4a501606
PM
8129 if (arm_feature(env, ARM_FEATURE_VAPA)) {
8130 define_arm_cp_regs(cpu, vapa_cp_reginfo);
8131 }
c4804214
PM
8132 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8133 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8134 }
8135 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8136 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8137 }
8138 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8139 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8140 }
18032bec
PM
8141 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8142 define_arm_cp_regs(cpu, omap_cp_reginfo);
8143 }
34f90529
PM
8144 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8145 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8146 }
1047b9d7
PM
8147 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8148 define_arm_cp_regs(cpu, xscale_cp_reginfo);
8149 }
8150 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8151 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8152 }
7ac681cf
PM
8153 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8154 define_arm_cp_regs(cpu, lpae_cp_reginfo);
8155 }
873b73c0 8156 if (cpu_isar_feature(aa32_jazelle, cpu)) {
f96f3d5f
MZ
8157 define_arm_cp_regs(cpu, jazelle_regs);
8158 }
7884849c
PM
8159 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
8160 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8161 * be read-only (ie write causes UNDEF exception).
8162 */
8163 {
00a29f3d
PM
8164 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8165 /* Pre-v8 MIDR space.
8166 * Note that the MIDR isn't a simple constant register because
7884849c
PM
8167 * of the TI925 behaviour where writes to another register can
8168 * cause the MIDR value to change.
97ce8d61
PC
8169 *
8170 * Unimplemented registers in the c15 0 0 0 space default to
8171 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8172 * and friends override accordingly.
7884849c
PM
8173 */
8174 { .name = "MIDR",
97ce8d61 8175 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 8176 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 8177 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 8178 .readfn = midr_read,
97ce8d61
PC
8179 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8180 .type = ARM_CP_OVERRIDE },
7884849c
PM
8181 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8182 { .name = "DUMMY",
8183 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8184 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8185 { .name = "DUMMY",
8186 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8187 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8188 { .name = "DUMMY",
8189 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8190 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8191 { .name = "DUMMY",
8192 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8193 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8194 { .name = "DUMMY",
8195 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8196 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8197 REGINFO_SENTINEL
8198 };
00a29f3d 8199 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
8200 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8201 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
8202 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8203 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8204 .readfn = midr_read },
ac00c79f
SF
8205 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
8206 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8207 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8208 .access = PL1_R, .resetvalue = cpu->midr },
8209 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8210 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8211 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
8212 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8213 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
93fbc983
MZ
8214 .access = PL1_R,
8215 .accessfn = access_aa64_tid1,
8216 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
8217 REGINFO_SENTINEL
8218 };
8219 ARMCPRegInfo id_cp_reginfo[] = {
8220 /* These are common to v8 and pre-v8 */
8221 { .name = "CTR",
8222 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
630fcd4d
MZ
8223 .access = PL1_R, .accessfn = ctr_el0_access,
8224 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
00a29f3d
PM
8225 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8226 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8227 .access = PL0_R, .accessfn = ctr_el0_access,
8228 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8229 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8230 { .name = "TCMTR",
8231 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
93fbc983
MZ
8232 .access = PL1_R,
8233 .accessfn = access_aa32_tid1,
8234 .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d
PM
8235 REGINFO_SENTINEL
8236 };
8085ce63
PC
8237 /* TLBTR is specific to VMSA */
8238 ARMCPRegInfo id_tlbtr_reginfo = {
8239 .name = "TLBTR",
8240 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
93fbc983
MZ
8241 .access = PL1_R,
8242 .accessfn = access_aa32_tid1,
8243 .type = ARM_CP_CONST, .resetvalue = 0,
8085ce63 8244 };
3281af81
PC
8245 /* MPUIR is specific to PMSA V6+ */
8246 ARMCPRegInfo id_mpuir_reginfo = {
8247 .name = "MPUIR",
8248 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8249 .access = PL1_R, .type = ARM_CP_CONST,
8250 .resetvalue = cpu->pmsav7_dregion << 8
8251 };
7884849c
PM
8252 ARMCPRegInfo crn0_wi_reginfo = {
8253 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8254 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8255 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8256 };
6c5c0fec
AB
8257#ifdef CONFIG_USER_ONLY
8258 ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8259 { .name = "MIDR_EL1",
8260 .exported_bits = 0x00000000ffffffff },
8261 { .name = "REVIDR_EL1" },
8262 REGUSERINFO_SENTINEL
8263 };
8264 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8265#endif
7884849c
PM
8266 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8267 arm_feature(env, ARM_FEATURE_STRONGARM)) {
8268 ARMCPRegInfo *r;
8269 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
8270 * whole space. Then update the specific ID registers to allow write
8271 * access, so that they ignore writes rather than causing them to
8272 * UNDEF.
7884849c
PM
8273 */
8274 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
00a29f3d
PM
8275 for (r = id_pre_v8_midr_cp_reginfo;
8276 r->type != ARM_CP_SENTINEL; r++) {
8277 r->access = PL1_RW;
8278 }
7884849c
PM
8279 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
8280 r->access = PL1_RW;
7884849c 8281 }
10006112 8282 id_mpuir_reginfo.access = PL1_RW;
3281af81 8283 id_tlbtr_reginfo.access = PL1_RW;
7884849c 8284 }
00a29f3d
PM
8285 if (arm_feature(env, ARM_FEATURE_V8)) {
8286 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8287 } else {
8288 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8289 }
a703eda1 8290 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 8291 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 8292 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
8293 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8294 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 8295 }
7884849c
PM
8296 }
8297
97ce8d61 8298 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
52264166
AB
8299 ARMCPRegInfo mpidr_cp_reginfo[] = {
8300 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8301 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8302 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8303 REGINFO_SENTINEL
8304 };
8305#ifdef CONFIG_USER_ONLY
8306 ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8307 { .name = "MPIDR_EL1",
8308 .fixed_bits = 0x0000000080000000 },
8309 REGUSERINFO_SENTINEL
8310 };
8311 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8312#endif
97ce8d61
PC
8313 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8314 }
8315
2771db27 8316 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
8317 ARMCPRegInfo auxcr_reginfo[] = {
8318 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8319 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
99602377
RH
8320 .access = PL1_RW, .accessfn = access_tacr,
8321 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
834a6c69
PM
8322 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8323 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8324 .access = PL2_RW, .type = ARM_CP_CONST,
8325 .resetvalue = 0 },
8326 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8327 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8328 .access = PL3_RW, .type = ARM_CP_CONST,
8329 .resetvalue = 0 },
8330 REGINFO_SENTINEL
2771db27 8331 };
834a6c69 8332 define_arm_cp_regs(cpu, auxcr_reginfo);
f6287c24
PM
8333 if (cpu_isar_feature(aa32_ac2, cpu)) {
8334 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
0e0456ab 8335 }
2771db27
PM
8336 }
8337
d8ba780b 8338 if (arm_feature(env, ARM_FEATURE_CBAR)) {
d56974af
LM
8339 /*
8340 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8341 * There are two flavours:
8342 * (1) older 32-bit only cores have a simple 32-bit CBAR
8343 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8344 * 32-bit register visible to AArch32 at a different encoding
8345 * to the "flavour 1" register and with the bits rearranged to
8346 * be able to squash a 64-bit address into the 32-bit view.
8347 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8348 * in future if we support AArch32-only configs of some of the
8349 * AArch64 cores we might need to add a specific feature flag
8350 * to indicate cores with "flavour 2" CBAR.
8351 */
f318cec6
PM
8352 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8353 /* 32 bit view is [31:18] 0...0 [43:32]. */
8354 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8355 | extract64(cpu->reset_cbar, 32, 12);
8356 ARMCPRegInfo cbar_reginfo[] = {
8357 { .name = "CBAR",
8358 .type = ARM_CP_CONST,
d56974af
LM
8359 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8360 .access = PL1_R, .resetvalue = cbar32 },
f318cec6
PM
8361 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8362 .type = ARM_CP_CONST,
8363 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
d56974af 8364 .access = PL1_R, .resetvalue = cpu->reset_cbar },
f318cec6
PM
8365 REGINFO_SENTINEL
8366 };
8367 /* We don't implement a r/w 64 bit CBAR currently */
8368 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8369 define_arm_cp_regs(cpu, cbar_reginfo);
8370 } else {
8371 ARMCPRegInfo cbar = {
8372 .name = "CBAR",
8373 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8374 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8375 .fieldoffset = offsetof(CPUARMState,
8376 cp15.c15_config_base_address)
8377 };
8378 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8379 cbar.access = PL1_R;
8380 cbar.fieldoffset = 0;
8381 cbar.type = ARM_CP_CONST;
8382 }
8383 define_one_arm_cp_reg(cpu, &cbar);
8384 }
d8ba780b
PC
8385 }
8386
91db4642
CLG
8387 if (arm_feature(env, ARM_FEATURE_VBAR)) {
8388 ARMCPRegInfo vbar_cp_reginfo[] = {
8389 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8390 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8391 .access = PL1_RW, .writefn = vbar_write,
8392 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8393 offsetof(CPUARMState, cp15.vbar_ns) },
8394 .resetvalue = 0 },
8395 REGINFO_SENTINEL
8396 };
8397 define_arm_cp_regs(cpu, vbar_cp_reginfo);
8398 }
8399
2771db27
PM
8400 /* Generic registers whose values depend on the implementation */
8401 {
8402 ARMCPRegInfo sctlr = {
5ebafdf3 8403 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9 8404 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
84929218 8405 .access = PL1_RW, .accessfn = access_tvm_trvm,
137feaa9
FA
8406 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8407 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
8408 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8409 .raw_writefn = raw_write,
2771db27
PM
8410 };
8411 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8412 /* Normally we would always end the TB on an SCTLR write, but Linux
8413 * arch/arm/mach-pxa/sleep.S expects two instructions following
8414 * an MMU enable to execute from cache. Imitate this behaviour.
8415 */
8416 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8417 }
8418 define_one_arm_cp_reg(cpu, &sctlr);
8419 }
5be5e8ed 8420
2d7137c1 8421 if (cpu_isar_feature(aa64_lor, cpu)) {
2d7137c1
RH
8422 define_arm_cp_regs(cpu, lor_reginfo);
8423 }
220f508f
RH
8424 if (cpu_isar_feature(aa64_pan, cpu)) {
8425 define_one_arm_cp_reg(cpu, &pan_reginfo);
8426 }
04b07d29
RH
8427#ifndef CONFIG_USER_ONLY
8428 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8429 define_arm_cp_regs(cpu, ats1e1_reginfo);
8430 }
8431 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8432 define_arm_cp_regs(cpu, ats1cp_reginfo);
8433 }
8434#endif
9eeb7a1c
RH
8435 if (cpu_isar_feature(aa64_uao, cpu)) {
8436 define_one_arm_cp_reg(cpu, &uao_reginfo);
8437 }
2d7137c1 8438
dc8b1853
RC
8439 if (cpu_isar_feature(aa64_dit, cpu)) {
8440 define_one_arm_cp_reg(cpu, &dit_reginfo);
8441 }
f2f68a78
RC
8442 if (cpu_isar_feature(aa64_ssbs, cpu)) {
8443 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8444 }
dc8b1853 8445
e2a1a461
RH
8446 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8447 define_arm_cp_regs(cpu, vhe_reginfo);
8448 }
8449
cd208a1c 8450 if (cpu_isar_feature(aa64_sve, cpu)) {
5be5e8ed
RH
8451 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
8452 if (arm_feature(env, ARM_FEATURE_EL2)) {
8453 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
8454 } else {
8455 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
8456 }
8457 if (arm_feature(env, ARM_FEATURE_EL3)) {
8458 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
8459 }
8460 }
967aa94f
RH
8461
8462#ifdef TARGET_AARCH64
8463 if (cpu_isar_feature(aa64_pauth, cpu)) {
8464 define_arm_cp_regs(cpu, pauth_reginfo);
8465 }
de390645
RH
8466 if (cpu_isar_feature(aa64_rndr, cpu)) {
8467 define_arm_cp_regs(cpu, rndr_reginfo);
8468 }
84940ed8
RC
8469 if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8470 define_arm_cp_regs(cpu, tlbirange_reginfo);
8471 }
7113d618
RC
8472 if (cpu_isar_feature(aa64_tlbios, cpu)) {
8473 define_arm_cp_regs(cpu, tlbios_reginfo);
8474 }
0d57b499
BM
8475#ifndef CONFIG_USER_ONLY
8476 /* Data Cache clean instructions up to PoP */
8477 if (cpu_isar_feature(aa64_dcpop, cpu)) {
8478 define_one_arm_cp_reg(cpu, dcpop_reg);
8479
8480 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8481 define_one_arm_cp_reg(cpu, dcpodp_reg);
8482 }
8483 }
8484#endif /*CONFIG_USER_ONLY*/
4b779ceb
RH
8485
8486 /*
8487 * If full MTE is enabled, add all of the system registers.
8488 * If only "instructions available at EL0" are enabled,
8489 * then define only a RAZ/WI version of PSTATE.TCO.
8490 */
8491 if (cpu_isar_feature(aa64_mte, cpu)) {
8492 define_arm_cp_regs(cpu, mte_reginfo);
5463df16 8493 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb
RH
8494 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8495 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
5463df16 8496 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb 8497 }
967aa94f 8498#endif
cb570bd3 8499
22e57073 8500 if (cpu_isar_feature(any_predinv, cpu)) {
cb570bd3
RH
8501 define_arm_cp_regs(cpu, predinv_reginfo);
8502 }
e2cce18f 8503
957e6155
PM
8504 if (cpu_isar_feature(any_ccidx, cpu)) {
8505 define_arm_cp_regs(cpu, ccsidr2_reginfo);
8506 }
8507
e2cce18f
RH
8508#ifndef CONFIG_USER_ONLY
8509 /*
8510 * Register redirections and aliases must be done last,
8511 * after the registers from the other extensions have been defined.
8512 */
8513 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8514 define_arm_vh_e2h_redirects_aliases(cpu);
8515 }
8516#endif
2ceb98c0
PM
8517}
8518
777dc784
PM
8519/* Sort alphabetically by type name, except for "any". */
8520static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 8521{
777dc784
PM
8522 ObjectClass *class_a = (ObjectClass *)a;
8523 ObjectClass *class_b = (ObjectClass *)b;
8524 const char *name_a, *name_b;
5adb4839 8525
777dc784
PM
8526 name_a = object_class_get_name(class_a);
8527 name_b = object_class_get_name(class_b);
51492fd1 8528 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 8529 return 1;
51492fd1 8530 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
8531 return -1;
8532 } else {
8533 return strcmp(name_a, name_b);
5adb4839
PB
8534 }
8535}
8536
777dc784 8537static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 8538{
777dc784 8539 ObjectClass *oc = data;
51492fd1
AF
8540 const char *typename;
8541 char *name;
3371d272 8542
51492fd1
AF
8543 typename = object_class_get_name(oc);
8544 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
0442428a 8545 qemu_printf(" %s\n", name);
51492fd1 8546 g_free(name);
777dc784
PM
8547}
8548
0442428a 8549void arm_cpu_list(void)
777dc784 8550{
777dc784
PM
8551 GSList *list;
8552
8553 list = object_class_get_list(TYPE_ARM_CPU, false);
8554 list = g_slist_sort(list, arm_cpu_list_compare);
0442428a
MA
8555 qemu_printf("Available CPUs:\n");
8556 g_slist_foreach(list, arm_cpu_list_entry, NULL);
777dc784 8557 g_slist_free(list);
40f137e1
PB
8558}
8559
78027bb6
CR
8560static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8561{
8562 ObjectClass *oc = data;
8563 CpuDefinitionInfoList **cpu_list = user_data;
78027bb6
CR
8564 CpuDefinitionInfo *info;
8565 const char *typename;
8566
8567 typename = object_class_get_name(oc);
8568 info = g_malloc0(sizeof(*info));
8569 info->name = g_strndup(typename,
8570 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 8571 info->q_typename = g_strdup(typename);
78027bb6 8572
54aa3de7 8573 QAPI_LIST_PREPEND(*cpu_list, info);
78027bb6
CR
8574}
8575
25a9d6ca 8576CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
78027bb6
CR
8577{
8578 CpuDefinitionInfoList *cpu_list = NULL;
8579 GSList *list;
8580
8581 list = object_class_get_list(TYPE_ARM_CPU, false);
8582 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8583 g_slist_free(list);
8584
8585 return cpu_list;
8586}
8587
6e6efd61 8588static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
51a79b03 8589 void *opaque, int state, int secstate,
9c513e78
AB
8590 int crm, int opc1, int opc2,
8591 const char *name)
6e6efd61
PM
8592{
8593 /* Private utility function for define_one_arm_cp_reg_with_opaque():
8594 * add a single reginfo struct to the hash table.
8595 */
8596 uint32_t *key = g_new(uint32_t, 1);
8597 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
8598 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5
FA
8599 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
8600
9c513e78 8601 r2->name = g_strdup(name);
3f3c82a5
FA
8602 /* Reset the secure state to the specific incoming state. This is
8603 * necessary as the register may have been defined with both states.
8604 */
8605 r2->secure = secstate;
8606
8607 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8608 /* Register is banked (using both entries in array).
8609 * Overwriting fieldoffset as the array is only used to define
8610 * banked registers but later only fieldoffset is used.
f5a0a5a5 8611 */
3f3c82a5
FA
8612 r2->fieldoffset = r->bank_fieldoffsets[ns];
8613 }
8614
8615 if (state == ARM_CP_STATE_AA32) {
8616 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8617 /* If the register is banked then we don't need to migrate or
8618 * reset the 32-bit instance in certain cases:
8619 *
8620 * 1) If the register has both 32-bit and 64-bit instances then we
8621 * can count on the 64-bit instance taking care of the
8622 * non-secure bank.
8623 * 2) If ARMv8 is enabled then we can count on a 64-bit version
8624 * taking care of the secure bank. This requires that separate
8625 * 32 and 64-bit definitions are provided.
8626 */
8627 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8628 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 8629 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
8630 }
8631 } else if ((secstate != r->secure) && !ns) {
8632 /* The register is not banked so we only want to allow migration of
8633 * the non-secure instance.
8634 */
7a0e58fa 8635 r2->type |= ARM_CP_ALIAS;
58a1d8ce 8636 }
3f3c82a5
FA
8637
8638 if (r->state == ARM_CP_STATE_BOTH) {
8639 /* We assume it is a cp15 register if the .cp field is left unset.
8640 */
8641 if (r2->cp == 0) {
8642 r2->cp = 15;
8643 }
8644
e03b5686 8645#if HOST_BIG_ENDIAN
3f3c82a5
FA
8646 if (r2->fieldoffset) {
8647 r2->fieldoffset += sizeof(uint32_t);
8648 }
f5a0a5a5 8649#endif
3f3c82a5 8650 }
f5a0a5a5
PM
8651 }
8652 if (state == ARM_CP_STATE_AA64) {
8653 /* To allow abbreviation of ARMCPRegInfo
8654 * definitions, we treat cp == 0 as equivalent to
8655 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
8656 * STATE_BOTH definitions are also always "standard
8657 * sysreg" in their AArch64 view (the .cp value may
8658 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 8659 */
58a1d8ce 8660 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
8661 r2->cp = CP_REG_ARM64_SYSREG_CP;
8662 }
8663 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
8664 r2->opc0, opc1, opc2);
8665 } else {
51a79b03 8666 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 8667 }
6e6efd61
PM
8668 if (opaque) {
8669 r2->opaque = opaque;
8670 }
67ed771d
PM
8671 /* reginfo passed to helpers is correct for the actual access,
8672 * and is never ARM_CP_STATE_BOTH:
8673 */
8674 r2->state = state;
6e6efd61
PM
8675 /* Make sure reginfo passed to helpers for wildcarded regs
8676 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
8677 */
8678 r2->crm = crm;
8679 r2->opc1 = opc1;
8680 r2->opc2 = opc2;
8681 /* By convention, for wildcarded registers only the first
8682 * entry is used for migration; the others are marked as
7a0e58fa 8683 * ALIAS so we don't try to transfer the register
6e6efd61 8684 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 8685 * never migratable and not even raw-accessible.
6e6efd61 8686 */
7a0e58fa
PM
8687 if ((r->type & ARM_CP_SPECIAL)) {
8688 r2->type |= ARM_CP_NO_RAW;
8689 }
8690 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
8691 ((r->opc1 == CP_ANY) && opc1 != 0) ||
8692 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 8693 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
8694 }
8695
375421cc
PM
8696 /* Check that raw accesses are either forbidden or handled. Note that
8697 * we can't assert this earlier because the setup of fieldoffset for
8698 * banked registers has to be done first.
8699 */
8700 if (!(r2->type & ARM_CP_NO_RAW)) {
8701 assert(!raw_accessors_invalid(r2));
8702 }
8703
6e6efd61
PM
8704 /* Overriding of an existing definition must be explicitly
8705 * requested.
8706 */
8707 if (!(r->type & ARM_CP_OVERRIDE)) {
8708 ARMCPRegInfo *oldreg;
8709 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
8710 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
8711 fprintf(stderr, "Register redefined: cp=%d %d bit "
8712 "crn=%d crm=%d opc1=%d opc2=%d, "
8713 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
8714 r2->crn, r2->crm, r2->opc1, r2->opc2,
8715 oldreg->name, r2->name);
8716 g_assert_not_reached();
8717 }
8718 }
8719 g_hash_table_insert(cpu->cp_regs, key, r2);
8720}
8721
8722
4b6a83fb
PM
8723void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8724 const ARMCPRegInfo *r, void *opaque)
8725{
8726 /* Define implementations of coprocessor registers.
8727 * We store these in a hashtable because typically
8728 * there are less than 150 registers in a space which
8729 * is 16*16*16*8*8 = 262144 in size.
8730 * Wildcarding is supported for the crm, opc1 and opc2 fields.
8731 * If a register is defined twice then the second definition is
8732 * used, so this can be used to define some generic registers and
8733 * then override them with implementation specific variations.
8734 * At least one of the original and the second definition should
8735 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8736 * against accidental use.
f5a0a5a5
PM
8737 *
8738 * The state field defines whether the register is to be
8739 * visible in the AArch32 or AArch64 execution state. If the
8740 * state is set to ARM_CP_STATE_BOTH then we synthesise a
8741 * reginfo structure for the AArch32 view, which sees the lower
8742 * 32 bits of the 64 bit register.
8743 *
8744 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8745 * be wildcarded. AArch64 registers are always considered to be 64
8746 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8747 * the register, if any.
4b6a83fb 8748 */
f5a0a5a5 8749 int crm, opc1, opc2, state;
4b6a83fb
PM
8750 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8751 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8752 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8753 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8754 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8755 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
8756 /* 64 bit registers have only CRm and Opc1 fields */
8757 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
8758 /* op0 only exists in the AArch64 encodings */
8759 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8760 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8761 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
cd8be50e
PM
8762 /*
8763 * This API is only for Arm's system coprocessors (14 and 15) or
8764 * (M-profile or v7A-and-earlier only) for implementation defined
8765 * coprocessors in the range 0..7. Our decode assumes this, since
8766 * 8..13 can be used for other insns including VFP and Neon. See
8767 * valid_cp() in translate.c. Assert here that we haven't tried
8768 * to use an invalid coprocessor number.
8769 */
8770 switch (r->state) {
8771 case ARM_CP_STATE_BOTH:
8772 /* 0 has a special meaning, but otherwise the same rules as AA32. */
8773 if (r->cp == 0) {
8774 break;
8775 }
8776 /* fall through */
8777 case ARM_CP_STATE_AA32:
8778 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8779 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8780 assert(r->cp >= 14 && r->cp <= 15);
8781 } else {
8782 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8783 }
8784 break;
8785 case ARM_CP_STATE_AA64:
8786 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8787 break;
8788 default:
8789 g_assert_not_reached();
8790 }
f5a0a5a5
PM
8791 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8792 * encodes a minimum access level for the register. We roll this
8793 * runtime check into our general permission check code, so check
8794 * here that the reginfo's specified permissions are strict enough
8795 * to encompass the generic architectural permission check.
8796 */
8797 if (r->state != ARM_CP_STATE_AA32) {
8798 int mask = 0;
8799 switch (r->opc1) {
b5bd7440
AB
8800 case 0:
8801 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8802 mask = PL0U_R | PL1_RW;
8803 break;
8804 case 1: case 2:
f5a0a5a5
PM
8805 /* min_EL EL1 */
8806 mask = PL1_RW;
8807 break;
8808 case 3:
8809 /* min_EL EL0 */
8810 mask = PL0_RW;
8811 break;
8812 case 4:
b4ecf60f 8813 case 5:
f5a0a5a5
PM
8814 /* min_EL EL2 */
8815 mask = PL2_RW;
8816 break;
f5a0a5a5
PM
8817 case 6:
8818 /* min_EL EL3 */
8819 mask = PL3_RW;
8820 break;
8821 case 7:
8822 /* min_EL EL1, secure mode only (we don't check the latter) */
8823 mask = PL1_RW;
8824 break;
8825 default:
8826 /* broken reginfo with out-of-range opc1 */
8827 assert(false);
8828 break;
8829 }
8830 /* assert our permissions are not too lax (stricter is fine) */
8831 assert((r->access & ~mask) == 0);
8832 }
8833
4b6a83fb
PM
8834 /* Check that the register definition has enough info to handle
8835 * reads and writes if they are permitted.
8836 */
8837 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
8838 if (r->access & PL3_R) {
3f3c82a5
FA
8839 assert((r->fieldoffset ||
8840 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8841 r->readfn);
4b6a83fb
PM
8842 }
8843 if (r->access & PL3_W) {
3f3c82a5
FA
8844 assert((r->fieldoffset ||
8845 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8846 r->writefn);
4b6a83fb
PM
8847 }
8848 }
8849 /* Bad type field probably means missing sentinel at end of reg list */
8850 assert(cptype_valid(r->type));
8851 for (crm = crmmin; crm <= crmmax; crm++) {
8852 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8853 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
8854 for (state = ARM_CP_STATE_AA32;
8855 state <= ARM_CP_STATE_AA64; state++) {
8856 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8857 continue;
8858 }
3f3c82a5
FA
8859 if (state == ARM_CP_STATE_AA32) {
8860 /* Under AArch32 CP registers can be common
8861 * (same for secure and non-secure world) or banked.
8862 */
9c513e78
AB
8863 char *name;
8864
3f3c82a5
FA
8865 switch (r->secure) {
8866 case ARM_CP_SECSTATE_S:
8867 case ARM_CP_SECSTATE_NS:
8868 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
8869 r->secure, crm, opc1, opc2,
8870 r->name);
3f3c82a5
FA
8871 break;
8872 default:
9c513e78 8873 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
8874 add_cpreg_to_hashtable(cpu, r, opaque, state,
8875 ARM_CP_SECSTATE_S,
9c513e78
AB
8876 crm, opc1, opc2, name);
8877 g_free(name);
3f3c82a5
FA
8878 add_cpreg_to_hashtable(cpu, r, opaque, state,
8879 ARM_CP_SECSTATE_NS,
9c513e78 8880 crm, opc1, opc2, r->name);
3f3c82a5
FA
8881 break;
8882 }
8883 } else {
8884 /* AArch64 registers get mapped to non-secure instance
8885 * of AArch32 */
8886 add_cpreg_to_hashtable(cpu, r, opaque, state,
8887 ARM_CP_SECSTATE_NS,
9c513e78 8888 crm, opc1, opc2, r->name);
3f3c82a5 8889 }
f5a0a5a5 8890 }
4b6a83fb
PM
8891 }
8892 }
8893 }
8894}
8895
8896void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
8897 const ARMCPRegInfo *regs, void *opaque)
8898{
8899 /* Define a whole list of registers */
8900 const ARMCPRegInfo *r;
8901 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
8902 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
8903 }
8904}
8905
6c5c0fec
AB
8906/*
8907 * Modify ARMCPRegInfo for access from userspace.
8908 *
8909 * This is a data driven modification directed by
8910 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8911 * user-space cannot alter any values and dynamic values pertaining to
8912 * execution state are hidden from user space view anyway.
8913 */
8914void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods)
8915{
8916 const ARMCPRegUserSpaceInfo *m;
8917 ARMCPRegInfo *r;
8918
8919 for (m = mods; m->name; m++) {
d040242e
AB
8920 GPatternSpec *pat = NULL;
8921 if (m->is_glob) {
8922 pat = g_pattern_spec_new(m->name);
8923 }
6c5c0fec 8924 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
d040242e
AB
8925 if (pat && g_pattern_match_string(pat, r->name)) {
8926 r->type = ARM_CP_CONST;
8927 r->access = PL0U_R;
8928 r->resetvalue = 0;
8929 /* continue */
8930 } else if (strcmp(r->name, m->name) == 0) {
6c5c0fec
AB
8931 r->type = ARM_CP_CONST;
8932 r->access = PL0U_R;
8933 r->resetvalue &= m->exported_bits;
8934 r->resetvalue |= m->fixed_bits;
8935 break;
8936 }
8937 }
d040242e
AB
8938 if (pat) {
8939 g_pattern_spec_free(pat);
8940 }
6c5c0fec
AB
8941 }
8942}
8943
60322b39 8944const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 8945{
60322b39 8946 return g_hash_table_lookup(cpregs, &encoded_cp);
4b6a83fb
PM
8947}
8948
c4241c7d
PM
8949void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8950 uint64_t value)
4b6a83fb
PM
8951{
8952 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
8953}
8954
c4241c7d 8955uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
8956{
8957 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
8958 return 0;
8959}
8960
f5a0a5a5
PM
8961void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8962{
8963 /* Helper coprocessor reset function for do-nothing-on-reset registers */
8964}
8965
af393ffc 8966static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
8967{
8968 /* Return true if it is not valid for us to switch to
8969 * this CPU mode (ie all the UNPREDICTABLE cases in
8970 * the ARM ARM CPSRWriteByInstr pseudocode).
8971 */
af393ffc
PM
8972
8973 /* Changes to or from Hyp via MSR and CPS are illegal. */
8974 if (write_type == CPSRWriteByInstr &&
8975 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8976 mode == ARM_CPU_MODE_HYP)) {
8977 return 1;
8978 }
8979
37064a8b
PM
8980 switch (mode) {
8981 case ARM_CPU_MODE_USR:
10eacda7 8982 return 0;
37064a8b
PM
8983 case ARM_CPU_MODE_SYS:
8984 case ARM_CPU_MODE_SVC:
8985 case ARM_CPU_MODE_ABT:
8986 case ARM_CPU_MODE_UND:
8987 case ARM_CPU_MODE_IRQ:
8988 case ARM_CPU_MODE_FIQ:
52ff951b
PM
8989 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8990 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8991 */
10eacda7
PM
8992 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8993 * and CPS are treated as illegal mode changes.
8994 */
8995 if (write_type == CPSRWriteByInstr &&
10eacda7 8996 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7c208e0f 8997 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10eacda7
PM
8998 return 1;
8999 }
37064a8b 9000 return 0;
e6c8fc07 9001 case ARM_CPU_MODE_HYP:
e6ef0169 9002 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
027fc527 9003 case ARM_CPU_MODE_MON:
58ae2d1f 9004 return arm_current_el(env) < 3;
37064a8b
PM
9005 default:
9006 return 1;
9007 }
9008}
9009
2f4a40e5
AZ
9010uint32_t cpsr_read(CPUARMState *env)
9011{
9012 int ZF;
6fbe23d5
PB
9013 ZF = (env->ZF == 0);
9014 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
9015 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9016 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9017 | ((env->condexec_bits & 0xfc) << 8)
af519934 9018 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
9019}
9020
50866ba5
PM
9021void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9022 CPSRWriteType write_type)
2f4a40e5 9023{
6e8801f9 9024 uint32_t changed_daif;
e784807c
PM
9025 bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9026 (mask & (CPSR_M | CPSR_E | CPSR_IL));
6e8801f9 9027
2f4a40e5 9028 if (mask & CPSR_NZCV) {
6fbe23d5
PB
9029 env->ZF = (~val) & CPSR_Z;
9030 env->NF = val;
2f4a40e5
AZ
9031 env->CF = (val >> 29) & 1;
9032 env->VF = (val << 3) & 0x80000000;
9033 }
9034 if (mask & CPSR_Q)
9035 env->QF = ((val & CPSR_Q) != 0);
9036 if (mask & CPSR_T)
9037 env->thumb = ((val & CPSR_T) != 0);
9038 if (mask & CPSR_IT_0_1) {
9039 env->condexec_bits &= ~3;
9040 env->condexec_bits |= (val >> 25) & 3;
9041 }
9042 if (mask & CPSR_IT_2_7) {
9043 env->condexec_bits &= 3;
9044 env->condexec_bits |= (val >> 8) & 0xfc;
9045 }
9046 if (mask & CPSR_GE) {
9047 env->GE = (val >> 16) & 0xf;
9048 }
9049
6e8801f9
FA
9050 /* In a V7 implementation that includes the security extensions but does
9051 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9052 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9053 * bits respectively.
9054 *
9055 * In a V8 implementation, it is permitted for privileged software to
9056 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9057 */
f8c88bbc 9058 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
9059 arm_feature(env, ARM_FEATURE_EL3) &&
9060 !arm_feature(env, ARM_FEATURE_EL2) &&
9061 !arm_is_secure(env)) {
9062
9063 changed_daif = (env->daif ^ val) & mask;
9064
9065 if (changed_daif & CPSR_A) {
9066 /* Check to see if we are allowed to change the masking of async
9067 * abort exceptions from a non-secure state.
9068 */
9069 if (!(env->cp15.scr_el3 & SCR_AW)) {
9070 qemu_log_mask(LOG_GUEST_ERROR,
9071 "Ignoring attempt to switch CPSR_A flag from "
9072 "non-secure world with SCR.AW bit clear\n");
9073 mask &= ~CPSR_A;
9074 }
9075 }
9076
9077 if (changed_daif & CPSR_F) {
9078 /* Check to see if we are allowed to change the masking of FIQ
9079 * exceptions from a non-secure state.
9080 */
9081 if (!(env->cp15.scr_el3 & SCR_FW)) {
9082 qemu_log_mask(LOG_GUEST_ERROR,
9083 "Ignoring attempt to switch CPSR_F flag from "
9084 "non-secure world with SCR.FW bit clear\n");
9085 mask &= ~CPSR_F;
9086 }
9087
9088 /* Check whether non-maskable FIQ (NMFI) support is enabled.
9089 * If this bit is set software is not allowed to mask
9090 * FIQs, but is allowed to set CPSR_F to 0.
9091 */
9092 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9093 (val & CPSR_F)) {
9094 qemu_log_mask(LOG_GUEST_ERROR,
9095 "Ignoring attempt to enable CPSR_F flag "
9096 "(non-maskable FIQ [NMFI] support enabled)\n");
9097 mask &= ~CPSR_F;
9098 }
9099 }
9100 }
9101
4cc35614
PM
9102 env->daif &= ~(CPSR_AIF & mask);
9103 env->daif |= val & CPSR_AIF & mask;
9104
f8c88bbc
PM
9105 if (write_type != CPSRWriteRaw &&
9106 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
9107 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9108 /* Note that we can only get here in USR mode if this is a
9109 * gdb stub write; for this case we follow the architectural
9110 * behaviour for guest writes in USR mode of ignoring an attempt
9111 * to switch mode. (Those are caught by translate.c for writes
9112 * triggered by guest instructions.)
9113 */
9114 mask &= ~CPSR_M;
9115 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
9116 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9117 * v7, and has defined behaviour in v8:
9118 * + leave CPSR.M untouched
9119 * + allow changes to the other CPSR fields
9120 * + set PSTATE.IL
9121 * For user changes via the GDB stub, we don't set PSTATE.IL,
9122 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
9123 */
9124 mask &= ~CPSR_M;
81907a58
PM
9125 if (write_type != CPSRWriteByGDBStub &&
9126 arm_feature(env, ARM_FEATURE_V8)) {
9127 mask |= CPSR_IL;
9128 val |= CPSR_IL;
9129 }
81e37284
PM
9130 qemu_log_mask(LOG_GUEST_ERROR,
9131 "Illegal AArch32 mode switch attempt from %s to %s\n",
9132 aarch32_mode_name(env->uncached_cpsr),
9133 aarch32_mode_name(val));
37064a8b 9134 } else {
81e37284
PM
9135 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9136 write_type == CPSRWriteExceptionReturn ?
9137 "Exception return from AArch32" :
9138 "AArch32 mode switch from",
9139 aarch32_mode_name(env->uncached_cpsr),
9140 aarch32_mode_name(val), env->regs[15]);
37064a8b
PM
9141 switch_mode(env, val & CPSR_M);
9142 }
2f4a40e5
AZ
9143 }
9144 mask &= ~CACHED_CPSR_BITS;
9145 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
e784807c
PM
9146 if (rebuild_hflags) {
9147 arm_rebuild_hflags(env);
9148 }
2f4a40e5
AZ
9149}
9150
b26eefb6
PB
9151/* Sign/zero extend */
9152uint32_t HELPER(sxtb16)(uint32_t x)
9153{
9154 uint32_t res;
9155 res = (uint16_t)(int8_t)x;
9156 res |= (uint32_t)(int8_t)(x >> 16) << 16;
9157 return res;
9158}
9159
e5346292
PM
9160static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9161{
9162 /*
9163 * Take a division-by-zero exception if necessary; otherwise return
9164 * to get the usual non-trapping division behaviour (result of 0)
9165 */
9166 if (arm_feature(env, ARM_FEATURE_M)
9167 && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9168 raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9169 }
9170}
9171
b26eefb6
PB
9172uint32_t HELPER(uxtb16)(uint32_t x)
9173{
9174 uint32_t res;
9175 res = (uint16_t)(uint8_t)x;
9176 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9177 return res;
9178}
9179
e5346292 9180int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
3670669c 9181{
fc7a5038 9182 if (den == 0) {
e5346292 9183 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9184 return 0;
9185 }
9186 if (num == INT_MIN && den == -1) {
9187 return INT_MIN;
9188 }
3670669c
PB
9189 return num / den;
9190}
9191
e5346292 9192uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
3670669c 9193{
fc7a5038 9194 if (den == 0) {
e5346292 9195 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9196 return 0;
9197 }
3670669c
PB
9198 return num / den;
9199}
9200
9201uint32_t HELPER(rbit)(uint32_t x)
9202{
42fedbca 9203 return revbit32(x);
3670669c
PB
9204}
9205
c47eaf9f 9206#ifdef CONFIG_USER_ONLY
b5ff1b31 9207
affdb64d 9208static void switch_mode(CPUARMState *env, int mode)
b5ff1b31 9209{
2fc0cc0e 9210 ARMCPU *cpu = env_archcpu(env);
a47dddd7
AF
9211
9212 if (mode != ARM_CPU_MODE_USR) {
9213 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9214 }
b5ff1b31
FB
9215}
9216
012a906b
GB
9217uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9218 uint32_t cur_el, bool secure)
9e729b57
EI
9219{
9220 return 1;
9221}
9222
ce02049d
GB
9223void aarch64_sync_64_to_32(CPUARMState *env)
9224{
9225 g_assert_not_reached();
9226}
9227
b5ff1b31
FB
9228#else
9229
affdb64d 9230static void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
9231{
9232 int old_mode;
9233 int i;
9234
9235 old_mode = env->uncached_cpsr & CPSR_M;
9236 if (mode == old_mode)
9237 return;
9238
9239 if (old_mode == ARM_CPU_MODE_FIQ) {
9240 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 9241 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
9242 } else if (mode == ARM_CPU_MODE_FIQ) {
9243 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 9244 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
9245 }
9246
f5206413 9247 i = bank_number(old_mode);
b5ff1b31 9248 env->banked_r13[i] = env->regs[13];
b5ff1b31
FB
9249 env->banked_spsr[i] = env->spsr;
9250
f5206413 9251 i = bank_number(mode);
b5ff1b31 9252 env->regs[13] = env->banked_r13[i];
b5ff1b31 9253 env->spsr = env->banked_spsr[i];
593cfa2b
PM
9254
9255 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9256 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
b5ff1b31
FB
9257}
9258
0eeb17d6
GB
9259/* Physical Interrupt Target EL Lookup Table
9260 *
9261 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9262 *
9263 * The below multi-dimensional table is used for looking up the target
9264 * exception level given numerous condition criteria. Specifically, the
9265 * target EL is based on SCR and HCR routing controls as well as the
9266 * currently executing EL and secure state.
9267 *
9268 * Dimensions:
9269 * target_el_table[2][2][2][2][2][4]
9270 * | | | | | +--- Current EL
9271 * | | | | +------ Non-secure(0)/Secure(1)
9272 * | | | +--------- HCR mask override
9273 * | | +------------ SCR exec state control
9274 * | +--------------- SCR mask override
9275 * +------------------ 32-bit(0)/64-bit(1) EL3
9276 *
9277 * The table values are as such:
9278 * 0-3 = EL0-EL3
9279 * -1 = Cannot occur
9280 *
9281 * The ARM ARM target EL table includes entries indicating that an "exception
9282 * is not taken". The two cases where this is applicable are:
9283 * 1) An exception is taken from EL3 but the SCR does not have the exception
9284 * routed to EL3.
9285 * 2) An exception is taken from EL2 but the HCR does not have the exception
9286 * routed to EL2.
9287 * In these two cases, the below table contain a target of EL1. This value is
9288 * returned as it is expected that the consumer of the table data will check
9289 * for "target EL >= current EL" to ensure the exception is not taken.
9290 *
9291 * SCR HCR
9292 * 64 EA AMO From
9293 * BIT IRQ IMO Non-secure Secure
9294 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
9295 */
82c39f6a 9296static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
9297 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9298 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
9299 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9300 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
9301 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9302 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
9303 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9304 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
9305 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6c85f906
RDC
9306 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
9307 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
9308 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
0eeb17d6
GB
9309 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
9310 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6c85f906
RDC
9311 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
9312 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
0eeb17d6
GB
9313};
9314
9315/*
9316 * Determine the target EL for physical exceptions
9317 */
012a906b
GB
9318uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9319 uint32_t cur_el, bool secure)
0eeb17d6
GB
9320{
9321 CPUARMState *env = cs->env_ptr;
f7778444
RH
9322 bool rw;
9323 bool scr;
9324 bool hcr;
0eeb17d6 9325 int target_el;
2cde031f 9326 /* Is the highest EL AArch64? */
f7778444
RH
9327 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9328 uint64_t hcr_el2;
2cde031f
SS
9329
9330 if (arm_feature(env, ARM_FEATURE_EL3)) {
9331 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9332 } else {
9333 /* Either EL2 is the highest EL (and so the EL2 register width
9334 * is given by is64); or there is no EL2 or EL3, in which case
9335 * the value of 'rw' does not affect the table lookup anyway.
9336 */
9337 rw = is64;
9338 }
0eeb17d6 9339
f7778444 9340 hcr_el2 = arm_hcr_el2_eff(env);
0eeb17d6
GB
9341 switch (excp_idx) {
9342 case EXCP_IRQ:
9343 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
f7778444 9344 hcr = hcr_el2 & HCR_IMO;
0eeb17d6
GB
9345 break;
9346 case EXCP_FIQ:
9347 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
f7778444 9348 hcr = hcr_el2 & HCR_FMO;
0eeb17d6
GB
9349 break;
9350 default:
9351 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
f7778444 9352 hcr = hcr_el2 & HCR_AMO;
0eeb17d6
GB
9353 break;
9354 };
9355
d1b31428
RH
9356 /*
9357 * For these purposes, TGE and AMO/IMO/FMO both force the
9358 * interrupt to EL2. Fold TGE into the bit extracted above.
9359 */
9360 hcr |= (hcr_el2 & HCR_TGE) != 0;
9361
0eeb17d6
GB
9362 /* Perform a table-lookup for the target EL given the current state */
9363 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9364
9365 assert(target_el > 0);
9366
9367 return target_el;
9368}
9369
fc6177af 9370void arm_log_exception(CPUState *cs)
b59f479b 9371{
fc6177af
PM
9372 int idx = cs->exception_index;
9373
b59f479b
PMD
9374 if (qemu_loglevel_mask(CPU_LOG_INT)) {
9375 const char *exc = NULL;
9376 static const char * const excnames[] = {
9377 [EXCP_UDEF] = "Undefined Instruction",
9378 [EXCP_SWI] = "SVC",
9379 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9380 [EXCP_DATA_ABORT] = "Data Abort",
9381 [EXCP_IRQ] = "IRQ",
9382 [EXCP_FIQ] = "FIQ",
9383 [EXCP_BKPT] = "Breakpoint",
9384 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9385 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9386 [EXCP_HVC] = "Hypervisor Call",
9387 [EXCP_HYP_TRAP] = "Hypervisor Trap",
9388 [EXCP_SMC] = "Secure Monitor Call",
9389 [EXCP_VIRQ] = "Virtual IRQ",
9390 [EXCP_VFIQ] = "Virtual FIQ",
9391 [EXCP_SEMIHOST] = "Semihosting call",
9392 [EXCP_NOCP] = "v7M NOCP UsageFault",
9393 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9394 [EXCP_STKOF] = "v8M STKOF UsageFault",
9395 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9396 [EXCP_LSERR] = "v8M LSERR UsageFault",
9397 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
e5346292 9398 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
b59f479b
PMD
9399 };
9400
9401 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9402 exc = excnames[idx];
9403 }
9404 if (!exc) {
9405 exc = "unknown";
9406 }
fc6177af
PM
9407 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9408 idx, exc, cs->cpu_index);
b59f479b
PMD
9409 }
9410}
9411
a356dacf 9412/*
7aab5a8c
PMD
9413 * Function used to synchronize QEMU's AArch64 register set with AArch32
9414 * register set. This is necessary when switching between AArch32 and AArch64
9415 * execution state.
a356dacf 9416 */
7aab5a8c 9417void aarch64_sync_32_to_64(CPUARMState *env)
9ee6e8bb 9418{
7aab5a8c
PMD
9419 int i;
9420 uint32_t mode = env->uncached_cpsr & CPSR_M;
9421
9422 /* We can blanket copy R[0:7] to X[0:7] */
9423 for (i = 0; i < 8; i++) {
9424 env->xregs[i] = env->regs[i];
fd592d89 9425 }
70d74660 9426
9a223097 9427 /*
7aab5a8c
PMD
9428 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9429 * Otherwise, they come from the banked user regs.
fd592d89 9430 */
7aab5a8c
PMD
9431 if (mode == ARM_CPU_MODE_FIQ) {
9432 for (i = 8; i < 13; i++) {
9433 env->xregs[i] = env->usr_regs[i - 8];
9434 }
9435 } else {
9436 for (i = 8; i < 13; i++) {
9437 env->xregs[i] = env->regs[i];
9438 }
fd592d89 9439 }
9ee6e8bb 9440
7aab5a8c
PMD
9441 /*
9442 * Registers x13-x23 are the various mode SP and FP registers. Registers
9443 * r13 and r14 are only copied if we are in that mode, otherwise we copy
9444 * from the mode banked register.
9445 */
9446 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9447 env->xregs[13] = env->regs[13];
9448 env->xregs[14] = env->regs[14];
9449 } else {
9450 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9451 /* HYP is an exception in that it is copied from r14 */
9452 if (mode == ARM_CPU_MODE_HYP) {
9453 env->xregs[14] = env->regs[14];
95695eff 9454 } else {
7aab5a8c 9455 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
95695eff 9456 }
95695eff
PM
9457 }
9458
7aab5a8c
PMD
9459 if (mode == ARM_CPU_MODE_HYP) {
9460 env->xregs[15] = env->regs[13];
9461 } else {
9462 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
95695eff
PM
9463 }
9464
7aab5a8c
PMD
9465 if (mode == ARM_CPU_MODE_IRQ) {
9466 env->xregs[16] = env->regs[14];
9467 env->xregs[17] = env->regs[13];
9468 } else {
9469 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9470 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9471 }
95695eff 9472
7aab5a8c
PMD
9473 if (mode == ARM_CPU_MODE_SVC) {
9474 env->xregs[18] = env->regs[14];
9475 env->xregs[19] = env->regs[13];
9476 } else {
9477 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9478 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9479 }
95695eff 9480
7aab5a8c
PMD
9481 if (mode == ARM_CPU_MODE_ABT) {
9482 env->xregs[20] = env->regs[14];
9483 env->xregs[21] = env->regs[13];
9484 } else {
9485 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9486 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9487 }
e33cf0f8 9488
7aab5a8c
PMD
9489 if (mode == ARM_CPU_MODE_UND) {
9490 env->xregs[22] = env->regs[14];
9491 env->xregs[23] = env->regs[13];
9492 } else {
9493 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9494 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
e33cf0f8
PM
9495 }
9496
9497 /*
7aab5a8c
PMD
9498 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9499 * mode, then we can copy from r8-r14. Otherwise, we copy from the
9500 * FIQ bank for r8-r14.
e33cf0f8 9501 */
7aab5a8c
PMD
9502 if (mode == ARM_CPU_MODE_FIQ) {
9503 for (i = 24; i < 31; i++) {
9504 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
9505 }
9506 } else {
9507 for (i = 24; i < 29; i++) {
9508 env->xregs[i] = env->fiq_regs[i - 24];
e33cf0f8 9509 }
7aab5a8c
PMD
9510 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9511 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
e33cf0f8 9512 }
7aab5a8c
PMD
9513
9514 env->pc = env->regs[15];
e33cf0f8
PM
9515}
9516
9a223097 9517/*
7aab5a8c
PMD
9518 * Function used to synchronize QEMU's AArch32 register set with AArch64
9519 * register set. This is necessary when switching between AArch32 and AArch64
9520 * execution state.
de2db7ec 9521 */
7aab5a8c 9522void aarch64_sync_64_to_32(CPUARMState *env)
9ee6e8bb 9523{
7aab5a8c
PMD
9524 int i;
9525 uint32_t mode = env->uncached_cpsr & CPSR_M;
abc24d86 9526
7aab5a8c
PMD
9527 /* We can blanket copy X[0:7] to R[0:7] */
9528 for (i = 0; i < 8; i++) {
9529 env->regs[i] = env->xregs[i];
de2db7ec 9530 }
3f0cddee 9531
9a223097 9532 /*
7aab5a8c
PMD
9533 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9534 * Otherwise, we copy x8-x12 into the banked user regs.
de2db7ec 9535 */
7aab5a8c
PMD
9536 if (mode == ARM_CPU_MODE_FIQ) {
9537 for (i = 8; i < 13; i++) {
9538 env->usr_regs[i - 8] = env->xregs[i];
9539 }
9540 } else {
9541 for (i = 8; i < 13; i++) {
9542 env->regs[i] = env->xregs[i];
9543 }
fb602cb7
PM
9544 }
9545
9a223097 9546 /*
7aab5a8c
PMD
9547 * Registers r13 & r14 depend on the current mode.
9548 * If we are in a given mode, we copy the corresponding x registers to r13
9549 * and r14. Otherwise, we copy the x register to the banked r13 and r14
9550 * for the mode.
fb602cb7 9551 */
7aab5a8c
PMD
9552 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9553 env->regs[13] = env->xregs[13];
9554 env->regs[14] = env->xregs[14];
fb602cb7 9555 } else {
7aab5a8c 9556 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
fb602cb7 9557
7aab5a8c
PMD
9558 /*
9559 * HYP is an exception in that it does not have its own banked r14 but
9560 * shares the USR r14
9561 */
9562 if (mode == ARM_CPU_MODE_HYP) {
9563 env->regs[14] = env->xregs[14];
9564 } else {
9565 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9566 }
9567 }
fb602cb7 9568
7aab5a8c
PMD
9569 if (mode == ARM_CPU_MODE_HYP) {
9570 env->regs[13] = env->xregs[15];
fb602cb7 9571 } else {
7aab5a8c 9572 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
fb602cb7 9573 }
d02a8698 9574
7aab5a8c
PMD
9575 if (mode == ARM_CPU_MODE_IRQ) {
9576 env->regs[14] = env->xregs[16];
9577 env->regs[13] = env->xregs[17];
d02a8698 9578 } else {
7aab5a8c
PMD
9579 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9580 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
d02a8698
PM
9581 }
9582
7aab5a8c
PMD
9583 if (mode == ARM_CPU_MODE_SVC) {
9584 env->regs[14] = env->xregs[18];
9585 env->regs[13] = env->xregs[19];
9586 } else {
9587 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9588 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
fb602cb7
PM
9589 }
9590
7aab5a8c
PMD
9591 if (mode == ARM_CPU_MODE_ABT) {
9592 env->regs[14] = env->xregs[20];
9593 env->regs[13] = env->xregs[21];
9594 } else {
9595 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9596 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
9597 }
9598
9599 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
9600 env->regs[14] = env->xregs[22];
9601 env->regs[13] = env->xregs[23];
ce02049d 9602 } else {
593cfa2b 9603 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
3a9148d0 9604 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
9605 }
9606
9607 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9608 * mode, then we can copy to r8-r14. Otherwise, we copy to the
9609 * FIQ bank for r8-r14.
9610 */
9611 if (mode == ARM_CPU_MODE_FIQ) {
9612 for (i = 24; i < 31; i++) {
9613 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
9614 }
9615 } else {
9616 for (i = 24; i < 29; i++) {
9617 env->fiq_regs[i - 24] = env->xregs[i];
9618 }
9619 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
593cfa2b 9620 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
ce02049d
GB
9621 }
9622
9623 env->regs[15] = env->pc;
9624}
9625
dea8378b
PM
9626static void take_aarch32_exception(CPUARMState *env, int new_mode,
9627 uint32_t mask, uint32_t offset,
9628 uint32_t newpc)
9629{
4a2696c0
RH
9630 int new_el;
9631
dea8378b
PM
9632 /* Change the CPU state so as to actually take the exception. */
9633 switch_mode(env, new_mode);
4a2696c0 9634
dea8378b
PM
9635 /*
9636 * For exceptions taken to AArch32 we must clear the SS bit in both
9637 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9638 */
f944a854 9639 env->pstate &= ~PSTATE_SS;
dea8378b
PM
9640 env->spsr = cpsr_read(env);
9641 /* Clear IT bits. */
9642 env->condexec_bits = 0;
9643 /* Switch to the new mode, and to the correct instruction set. */
9644 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
88828bf1
CD
9645
9646 /* This must be after mode switching. */
9647 new_el = arm_current_el(env);
9648
dea8378b
PM
9649 /* Set new mode endianness */
9650 env->uncached_cpsr &= ~CPSR_E;
4a2696c0 9651 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
dea8378b
PM
9652 env->uncached_cpsr |= CPSR_E;
9653 }
829f9fd3
PM
9654 /* J and IL must always be cleared for exception entry */
9655 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
dea8378b
PM
9656 env->daif |= mask;
9657
f2f68a78
RC
9658 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9659 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9660 env->uncached_cpsr |= CPSR_SSBS;
9661 } else {
9662 env->uncached_cpsr &= ~CPSR_SSBS;
9663 }
9664 }
9665
dea8378b
PM
9666 if (new_mode == ARM_CPU_MODE_HYP) {
9667 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9668 env->elr_el[2] = env->regs[15];
9669 } else {
4a2696c0 9670 /* CPSR.PAN is normally preserved preserved unless... */
f8af1143 9671 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
4a2696c0
RH
9672 switch (new_el) {
9673 case 3:
9674 if (!arm_is_secure_below_el3(env)) {
9675 /* ... the target is EL3, from non-secure state. */
9676 env->uncached_cpsr &= ~CPSR_PAN;
9677 break;
9678 }
9679 /* ... the target is EL3, from secure state ... */
9680 /* fall through */
9681 case 1:
9682 /* ... the target is EL1 and SCTLR.SPAN is 0. */
9683 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9684 env->uncached_cpsr |= CPSR_PAN;
9685 }
9686 break;
9687 }
9688 }
dea8378b
PM
9689 /*
9690 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9691 * and we should just guard the thumb mode on V4
9692 */
9693 if (arm_feature(env, ARM_FEATURE_V4T)) {
9694 env->thumb =
9695 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9696 }
9697 env->regs[14] = env->regs[15] + offset;
9698 }
9699 env->regs[15] = newpc;
a8a79c7a 9700 arm_rebuild_hflags(env);
dea8378b
PM
9701}
9702
b9bc21ff
PM
9703static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9704{
9705 /*
9706 * Handle exception entry to Hyp mode; this is sufficiently
9707 * different to entry to other AArch32 modes that we handle it
9708 * separately here.
9709 *
9710 * The vector table entry used is always the 0x14 Hyp mode entry point,
2c023d36 9711 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
b9bc21ff
PM
9712 * The offset applied to the preferred return address is always zero
9713 * (see DDI0487C.a section G1.12.3).
9714 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9715 */
9716 uint32_t addr, mask;
9717 ARMCPU *cpu = ARM_CPU(cs);
9718 CPUARMState *env = &cpu->env;
9719
9720 switch (cs->exception_index) {
9721 case EXCP_UDEF:
9722 addr = 0x04;
9723 break;
9724 case EXCP_SWI:
2c023d36 9725 addr = 0x08;
b9bc21ff
PM
9726 break;
9727 case EXCP_BKPT:
9728 /* Fall through to prefetch abort. */
9729 case EXCP_PREFETCH_ABORT:
9730 env->cp15.ifar_s = env->exception.vaddress;
9731 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9732 (uint32_t)env->exception.vaddress);
9733 addr = 0x0c;
9734 break;
9735 case EXCP_DATA_ABORT:
9736 env->cp15.dfar_s = env->exception.vaddress;
9737 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9738 (uint32_t)env->exception.vaddress);
9739 addr = 0x10;
9740 break;
9741 case EXCP_IRQ:
9742 addr = 0x18;
9743 break;
9744 case EXCP_FIQ:
9745 addr = 0x1c;
9746 break;
9747 case EXCP_HVC:
9748 addr = 0x08;
9749 break;
9750 case EXCP_HYP_TRAP:
9751 addr = 0x14;
9bbb4ef9 9752 break;
b9bc21ff
PM
9753 default:
9754 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9755 }
9756
9757 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
2ed08180
PM
9758 if (!arm_feature(env, ARM_FEATURE_V8)) {
9759 /*
9760 * QEMU syndrome values are v8-style. v7 has the IL bit
9761 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9762 * If this is a v7 CPU, squash the IL bit in those cases.
9763 */
9764 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9765 (cs->exception_index == EXCP_DATA_ABORT &&
9766 !(env->exception.syndrome & ARM_EL_ISV)) ||
9767 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9768 env->exception.syndrome &= ~ARM_EL_IL;
9769 }
9770 }
b9bc21ff
PM
9771 env->cp15.esr_el[2] = env->exception.syndrome;
9772 }
9773
9774 if (arm_current_el(env) != 2 && addr < 0x14) {
9775 addr = 0x14;
9776 }
9777
9778 mask = 0;
9779 if (!(env->cp15.scr_el3 & SCR_EA)) {
9780 mask |= CPSR_A;
9781 }
9782 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9783 mask |= CPSR_I;
9784 }
9785 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9786 mask |= CPSR_F;
9787 }
9788
9789 addr += env->cp15.hvbar;
9790
9791 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9792}
9793
966f758c 9794static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 9795{
97a8ea5a
AF
9796 ARMCPU *cpu = ARM_CPU(cs);
9797 CPUARMState *env = &cpu->env;
b5ff1b31
FB
9798 uint32_t addr;
9799 uint32_t mask;
9800 int new_mode;
9801 uint32_t offset;
16a906fd 9802 uint32_t moe;
b5ff1b31 9803
16a906fd 9804 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
64b91e3f 9805 switch (syn_get_ec(env->exception.syndrome)) {
16a906fd
PM
9806 case EC_BREAKPOINT:
9807 case EC_BREAKPOINT_SAME_EL:
9808 moe = 1;
9809 break;
9810 case EC_WATCHPOINT:
9811 case EC_WATCHPOINT_SAME_EL:
9812 moe = 10;
9813 break;
9814 case EC_AA32_BKPT:
9815 moe = 3;
9816 break;
9817 case EC_VECTORCATCH:
9818 moe = 5;
9819 break;
9820 default:
9821 moe = 0;
9822 break;
9823 }
9824
9825 if (moe) {
9826 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9827 }
9828
b9bc21ff
PM
9829 if (env->exception.target_el == 2) {
9830 arm_cpu_do_interrupt_aarch32_hyp(cs);
9831 return;
9832 }
9833
27103424 9834 switch (cs->exception_index) {
b5ff1b31
FB
9835 case EXCP_UDEF:
9836 new_mode = ARM_CPU_MODE_UND;
9837 addr = 0x04;
9838 mask = CPSR_I;
9839 if (env->thumb)
9840 offset = 2;
9841 else
9842 offset = 4;
9843 break;
9844 case EXCP_SWI:
9845 new_mode = ARM_CPU_MODE_SVC;
9846 addr = 0x08;
9847 mask = CPSR_I;
601d70b9 9848 /* The PC already points to the next instruction. */
b5ff1b31
FB
9849 offset = 0;
9850 break;
06c949e6 9851 case EXCP_BKPT:
9ee6e8bb
PB
9852 /* Fall through to prefetch abort. */
9853 case EXCP_PREFETCH_ABORT:
88ca1c2d 9854 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 9855 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 9856 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 9857 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9858 new_mode = ARM_CPU_MODE_ABT;
9859 addr = 0x0c;
9860 mask = CPSR_A | CPSR_I;
9861 offset = 4;
9862 break;
9863 case EXCP_DATA_ABORT:
4a7e2d73 9864 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 9865 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 9866 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 9867 env->exception.fsr,
6cd8a264 9868 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9869 new_mode = ARM_CPU_MODE_ABT;
9870 addr = 0x10;
9871 mask = CPSR_A | CPSR_I;
9872 offset = 8;
9873 break;
9874 case EXCP_IRQ:
9875 new_mode = ARM_CPU_MODE_IRQ;
9876 addr = 0x18;
9877 /* Disable IRQ and imprecise data aborts. */
9878 mask = CPSR_A | CPSR_I;
9879 offset = 4;
de38d23b
FA
9880 if (env->cp15.scr_el3 & SCR_IRQ) {
9881 /* IRQ routed to monitor mode */
9882 new_mode = ARM_CPU_MODE_MON;
9883 mask |= CPSR_F;
9884 }
b5ff1b31
FB
9885 break;
9886 case EXCP_FIQ:
9887 new_mode = ARM_CPU_MODE_FIQ;
9888 addr = 0x1c;
9889 /* Disable FIQ, IRQ and imprecise data aborts. */
9890 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
9891 if (env->cp15.scr_el3 & SCR_FIQ) {
9892 /* FIQ routed to monitor mode */
9893 new_mode = ARM_CPU_MODE_MON;
9894 }
b5ff1b31
FB
9895 offset = 4;
9896 break;
87a4b270
PM
9897 case EXCP_VIRQ:
9898 new_mode = ARM_CPU_MODE_IRQ;
9899 addr = 0x18;
9900 /* Disable IRQ and imprecise data aborts. */
9901 mask = CPSR_A | CPSR_I;
9902 offset = 4;
9903 break;
9904 case EXCP_VFIQ:
9905 new_mode = ARM_CPU_MODE_FIQ;
9906 addr = 0x1c;
9907 /* Disable FIQ, IRQ and imprecise data aborts. */
9908 mask = CPSR_A | CPSR_I | CPSR_F;
9909 offset = 4;
9910 break;
dbe9d163
FA
9911 case EXCP_SMC:
9912 new_mode = ARM_CPU_MODE_MON;
9913 addr = 0x08;
9914 mask = CPSR_A | CPSR_I | CPSR_F;
9915 offset = 0;
9916 break;
b5ff1b31 9917 default:
a47dddd7 9918 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
9919 return; /* Never happens. Keep compiler happy. */
9920 }
e89e51a1
FA
9921
9922 if (new_mode == ARM_CPU_MODE_MON) {
9923 addr += env->cp15.mvbar;
137feaa9 9924 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 9925 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 9926 addr += 0xffff0000;
8641136c
NR
9927 } else {
9928 /* ARM v7 architectures provide a vector base address register to remap
9929 * the interrupt vector table.
e89e51a1 9930 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
9931 * Note: only bits 31:5 are valid.
9932 */
fb6c91ba 9933 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 9934 }
dbe9d163
FA
9935
9936 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9937 env->cp15.scr_el3 &= ~SCR_NS;
9938 }
9939
dea8378b 9940 take_aarch32_exception(env, new_mode, mask, offset, addr);
b5ff1b31
FB
9941}
9942
a65dabf7
PM
9943static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
9944{
9945 /*
9946 * Return the register number of the AArch64 view of the AArch32
9947 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
9948 * be that of the AArch32 mode the exception came from.
9949 */
9950 int mode = env->uncached_cpsr & CPSR_M;
9951
9952 switch (aarch32_reg) {
9953 case 0 ... 7:
9954 return aarch32_reg;
9955 case 8 ... 12:
9956 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
9957 case 13:
9958 switch (mode) {
9959 case ARM_CPU_MODE_USR:
9960 case ARM_CPU_MODE_SYS:
9961 return 13;
9962 case ARM_CPU_MODE_HYP:
9963 return 15;
9964 case ARM_CPU_MODE_IRQ:
9965 return 17;
9966 case ARM_CPU_MODE_SVC:
9967 return 19;
9968 case ARM_CPU_MODE_ABT:
9969 return 21;
9970 case ARM_CPU_MODE_UND:
9971 return 23;
9972 case ARM_CPU_MODE_FIQ:
9973 return 29;
9974 default:
9975 g_assert_not_reached();
9976 }
9977 case 14:
9978 switch (mode) {
9979 case ARM_CPU_MODE_USR:
9980 case ARM_CPU_MODE_SYS:
9981 case ARM_CPU_MODE_HYP:
9982 return 14;
9983 case ARM_CPU_MODE_IRQ:
9984 return 16;
9985 case ARM_CPU_MODE_SVC:
9986 return 18;
9987 case ARM_CPU_MODE_ABT:
9988 return 20;
9989 case ARM_CPU_MODE_UND:
9990 return 22;
9991 case ARM_CPU_MODE_FIQ:
9992 return 30;
9993 default:
9994 g_assert_not_reached();
9995 }
9996 case 15:
9997 return 31;
9998 default:
9999 g_assert_not_reached();
10000 }
10001}
10002
f944a854
RC
10003static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10004{
10005 uint32_t ret = cpsr_read(env);
10006
10007 /* Move DIT to the correct location for SPSR_ELx */
10008 if (ret & CPSR_DIT) {
10009 ret &= ~CPSR_DIT;
10010 ret |= PSTATE_DIT;
10011 }
10012 /* Merge PSTATE.SS into SPSR_ELx */
10013 ret |= env->pstate & PSTATE_SS;
10014
10015 return ret;
10016}
10017
966f758c
PM
10018/* Handle exception entry to a target EL which is using AArch64 */
10019static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
10020{
10021 ARMCPU *cpu = ARM_CPU(cs);
10022 CPUARMState *env = &cpu->env;
10023 unsigned int new_el = env->exception.target_el;
10024 target_ulong addr = env->cp15.vbar_el[new_el];
10025 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
4a2696c0 10026 unsigned int old_mode;
0ab5953b 10027 unsigned int cur_el = arm_current_el(env);
a65dabf7 10028 int rt;
0ab5953b 10029
9a05f7b6
RH
10030 /*
10031 * Note that new_el can never be 0. If cur_el is 0, then
10032 * el0_a64 is is_a64(), else el0_a64 is ignored.
10033 */
10034 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
f3a9b694 10035
0ab5953b 10036 if (cur_el < new_el) {
3d6f7617
PM
10037 /* Entry vector offset depends on whether the implemented EL
10038 * immediately lower than the target level is using AArch32 or AArch64
10039 */
10040 bool is_aa64;
cb092fbb 10041 uint64_t hcr;
3d6f7617
PM
10042
10043 switch (new_el) {
10044 case 3:
10045 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10046 break;
10047 case 2:
cb092fbb
RH
10048 hcr = arm_hcr_el2_eff(env);
10049 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10050 is_aa64 = (hcr & HCR_RW) != 0;
10051 break;
10052 }
10053 /* fall through */
3d6f7617
PM
10054 case 1:
10055 is_aa64 = is_a64(env);
10056 break;
10057 default:
10058 g_assert_not_reached();
10059 }
10060
10061 if (is_aa64) {
f3a9b694
PM
10062 addr += 0x400;
10063 } else {
10064 addr += 0x600;
10065 }
10066 } else if (pstate_read(env) & PSTATE_SP) {
10067 addr += 0x200;
10068 }
10069
f3a9b694
PM
10070 switch (cs->exception_index) {
10071 case EXCP_PREFETCH_ABORT:
10072 case EXCP_DATA_ABORT:
10073 env->cp15.far_el[new_el] = env->exception.vaddress;
10074 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10075 env->cp15.far_el[new_el]);
10076 /* fall through */
10077 case EXCP_BKPT:
10078 case EXCP_UDEF:
10079 case EXCP_SWI:
10080 case EXCP_HVC:
10081 case EXCP_HYP_TRAP:
10082 case EXCP_SMC:
a65dabf7
PM
10083 switch (syn_get_ec(env->exception.syndrome)) {
10084 case EC_ADVSIMDFPACCESSTRAP:
4be42f40
PM
10085 /*
10086 * QEMU internal FP/SIMD syndromes from AArch32 include the
10087 * TA and coproc fields which are only exposed if the exception
10088 * is taken to AArch32 Hyp mode. Mask them out to get a valid
10089 * AArch64 format syndrome.
10090 */
10091 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
a65dabf7
PM
10092 break;
10093 case EC_CP14RTTRAP:
10094 case EC_CP15RTTRAP:
10095 case EC_CP14DTTRAP:
10096 /*
10097 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10098 * the raw register field from the insn; when taking this to
10099 * AArch64 we must convert it to the AArch64 view of the register
10100 * number. Notice that we read a 4-bit AArch32 register number and
10101 * write back a 5-bit AArch64 one.
10102 */
10103 rt = extract32(env->exception.syndrome, 5, 4);
10104 rt = aarch64_regnum(env, rt);
10105 env->exception.syndrome = deposit32(env->exception.syndrome,
10106 5, 5, rt);
10107 break;
10108 case EC_CP15RRTTRAP:
10109 case EC_CP14RRTTRAP:
10110 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10111 rt = extract32(env->exception.syndrome, 5, 4);
10112 rt = aarch64_regnum(env, rt);
10113 env->exception.syndrome = deposit32(env->exception.syndrome,
10114 5, 5, rt);
10115 rt = extract32(env->exception.syndrome, 10, 4);
10116 rt = aarch64_regnum(env, rt);
10117 env->exception.syndrome = deposit32(env->exception.syndrome,
10118 10, 5, rt);
10119 break;
4be42f40 10120 }
f3a9b694
PM
10121 env->cp15.esr_el[new_el] = env->exception.syndrome;
10122 break;
10123 case EXCP_IRQ:
10124 case EXCP_VIRQ:
10125 addr += 0x80;
10126 break;
10127 case EXCP_FIQ:
10128 case EXCP_VFIQ:
10129 addr += 0x100;
10130 break;
f3a9b694
PM
10131 default:
10132 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10133 }
10134
10135 if (is_a64(env)) {
4a2696c0 10136 old_mode = pstate_read(env);
f3a9b694
PM
10137 aarch64_save_sp(env, arm_current_el(env));
10138 env->elr_el[new_el] = env->pc;
10139 } else {
f944a854 10140 old_mode = cpsr_read_for_spsr_elx(env);
f3a9b694
PM
10141 env->elr_el[new_el] = env->regs[15];
10142
10143 aarch64_sync_32_to_64(env);
10144
10145 env->condexec_bits = 0;
10146 }
4a2696c0
RH
10147 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10148
f3a9b694
PM
10149 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10150 env->elr_el[new_el]);
10151
4a2696c0
RH
10152 if (cpu_isar_feature(aa64_pan, cpu)) {
10153 /* The value of PSTATE.PAN is normally preserved, except when ... */
10154 new_mode |= old_mode & PSTATE_PAN;
10155 switch (new_el) {
10156 case 2:
10157 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
10158 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10159 != (HCR_E2H | HCR_TGE)) {
10160 break;
10161 }
10162 /* fall through */
10163 case 1:
10164 /* ... the target is EL1 ... */
10165 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
10166 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10167 new_mode |= PSTATE_PAN;
10168 }
10169 break;
10170 }
10171 }
34669338
RH
10172 if (cpu_isar_feature(aa64_mte, cpu)) {
10173 new_mode |= PSTATE_TCO;
10174 }
4a2696c0 10175
f2f68a78
RC
10176 if (cpu_isar_feature(aa64_ssbs, cpu)) {
10177 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10178 new_mode |= PSTATE_SSBS;
10179 } else {
10180 new_mode &= ~PSTATE_SSBS;
10181 }
10182 }
10183
f3a9b694
PM
10184 pstate_write(env, PSTATE_DAIF | new_mode);
10185 env->aarch64 = 1;
10186 aarch64_restore_sp(env, new_el);
a8a79c7a 10187 helper_rebuild_hflags_a64(env, new_el);
f3a9b694
PM
10188
10189 env->pc = addr;
10190
10191 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10192 new_el, env->pc, pstate_read(env));
966f758c
PM
10193}
10194
ed6e6ba9
AB
10195/*
10196 * Do semihosting call and set the appropriate return value. All the
10197 * permission and validity checks have been done at translate time.
10198 *
10199 * We only see semihosting exceptions in TCG only as they are not
10200 * trapped to the hypervisor in KVM.
10201 */
91f78c58 10202#ifdef CONFIG_TCG
ed6e6ba9
AB
10203static void handle_semihosting(CPUState *cs)
10204{
904c04de
PM
10205 ARMCPU *cpu = ARM_CPU(cs);
10206 CPUARMState *env = &cpu->env;
10207
10208 if (is_a64(env)) {
ed6e6ba9
AB
10209 qemu_log_mask(CPU_LOG_INT,
10210 "...handling as semihosting call 0x%" PRIx64 "\n",
10211 env->xregs[0]);
0bb446d8 10212 env->xregs[0] = do_common_semihosting(cs);
4ff5ef9e 10213 env->pc += 4;
904c04de 10214 } else {
904c04de
PM
10215 qemu_log_mask(CPU_LOG_INT,
10216 "...handling as semihosting call 0x%x\n",
10217 env->regs[0]);
0bb446d8 10218 env->regs[0] = do_common_semihosting(cs);
4ff5ef9e 10219 env->regs[15] += env->thumb ? 2 : 4;
904c04de
PM
10220 }
10221}
ed6e6ba9 10222#endif
904c04de 10223
966f758c
PM
10224/* Handle a CPU exception for A and R profile CPUs.
10225 * Do any appropriate logging, handle PSCI calls, and then hand off
10226 * to the AArch64-entry or AArch32-entry function depending on the
10227 * target exception level's register width.
853bfef4
CF
10228 *
10229 * Note: this is used for both TCG (as the do_interrupt tcg op),
10230 * and KVM to re-inject guest debug exceptions, and to
10231 * inject a Synchronous-External-Abort.
966f758c
PM
10232 */
10233void arm_cpu_do_interrupt(CPUState *cs)
10234{
10235 ARMCPU *cpu = ARM_CPU(cs);
10236 CPUARMState *env = &cpu->env;
10237 unsigned int new_el = env->exception.target_el;
10238
531c60a9 10239 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c 10240
fc6177af 10241 arm_log_exception(cs);
966f758c
PM
10242 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10243 new_el);
10244 if (qemu_loglevel_mask(CPU_LOG_INT)
10245 && !excp_is_internal(cs->exception_index)) {
6568da45 10246 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
64b91e3f 10247 syn_get_ec(env->exception.syndrome),
966f758c
PM
10248 env->exception.syndrome);
10249 }
10250
10251 if (arm_is_psci_call(cpu, cs->exception_index)) {
10252 arm_handle_psci_call(cpu);
10253 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10254 return;
10255 }
10256
ed6e6ba9
AB
10257 /*
10258 * Semihosting semantics depend on the register width of the code
10259 * that caused the exception, not the target exception level, so
10260 * must be handled here.
966f758c 10261 */
ed6e6ba9
AB
10262#ifdef CONFIG_TCG
10263 if (cs->exception_index == EXCP_SEMIHOST) {
10264 handle_semihosting(cs);
904c04de
PM
10265 return;
10266 }
ed6e6ba9 10267#endif
904c04de 10268
b5c53d1b
AL
10269 /* Hooks may change global state so BQL should be held, also the
10270 * BQL needs to be held for any modification of
10271 * cs->interrupt_request.
10272 */
10273 g_assert(qemu_mutex_iothread_locked());
10274
10275 arm_call_pre_el_change_hook(cpu);
10276
904c04de
PM
10277 assert(!excp_is_internal(cs->exception_index));
10278 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
10279 arm_cpu_do_interrupt_aarch64(cs);
10280 } else {
10281 arm_cpu_do_interrupt_aarch32(cs);
10282 }
f3a9b694 10283
bd7d00fc
PM
10284 arm_call_el_change_hook(cpu);
10285
f3a9b694
PM
10286 if (!kvm_enabled()) {
10287 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10288 }
10289}
c47eaf9f 10290#endif /* !CONFIG_USER_ONLY */
0480f69a 10291
aaec1432
RH
10292uint64_t arm_sctlr(CPUARMState *env, int el)
10293{
10294 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10295 if (el == 0) {
10296 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
b6ad6062
RDC
10297 el = (mmu_idx == ARMMMUIdx_E20_0 || mmu_idx == ARMMMUIdx_SE20_0)
10298 ? 2 : 1;
aaec1432
RH
10299 }
10300 return env->cp15.sctlr_el[el];
10301}
c47eaf9f 10302
0480f69a 10303/* Return the SCTLR value which controls this address translation regime */
aaec1432 10304static inline uint64_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
0480f69a
PM
10305{
10306 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
10307}
10308
aaec1432
RH
10309#ifndef CONFIG_USER_ONLY
10310
0480f69a
PM
10311/* Return true if the specified stage of address translation is disabled */
10312static inline bool regime_translation_disabled(CPUARMState *env,
10313 ARMMMUIdx mmu_idx)
10314{
e04a5752
RDC
10315 uint64_t hcr_el2;
10316
29c483a5 10317 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 10318 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
10319 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
10320 case R_V7M_MPU_CTRL_ENABLE_MASK:
10321 /* Enabled, but not for HardFault and NMI */
62593718 10322 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
3bef7012
PM
10323 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
10324 /* Enabled for all cases */
10325 return false;
10326 case 0:
10327 default:
10328 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
10329 * we warned about that in armv7m_nvic.c when the guest set it.
10330 */
10331 return true;
10332 }
29c483a5
MD
10333 }
10334
e04a5752
RDC
10335 hcr_el2 = arm_hcr_el2_eff(env);
10336
b1a10c86 10337 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
9d1bab33 10338 /* HCR.DC means HCR.VM behaves as 1 */
e04a5752 10339 return (hcr_el2 & (HCR_DC | HCR_VM)) == 0;
0480f69a 10340 }
3d0e3080 10341
e04a5752 10342 if (hcr_el2 & HCR_TGE) {
3d0e3080
PM
10343 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
10344 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
10345 return true;
10346 }
10347 }
10348
e04a5752 10349 if ((hcr_el2 & HCR_DC) && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
9d1bab33
PM
10350 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
10351 return true;
10352 }
10353
0480f69a
PM
10354 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
10355}
10356
73462ddd
PC
10357static inline bool regime_translation_big_endian(CPUARMState *env,
10358 ARMMMUIdx mmu_idx)
10359{
10360 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
10361}
10362
c47eaf9f
PM
10363/* Return the TTBR associated with this translation regime */
10364static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
10365 int ttbrn)
10366{
97fa9350 10367 if (mmu_idx == ARMMMUIdx_Stage2) {
c47eaf9f
PM
10368 return env->cp15.vttbr_el2;
10369 }
b1a10c86
RDC
10370 if (mmu_idx == ARMMMUIdx_Stage2_S) {
10371 return env->cp15.vsttbr_el2;
10372 }
c47eaf9f
PM
10373 if (ttbrn == 0) {
10374 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
10375 } else {
10376 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
10377 }
10378}
10379
10380#endif /* !CONFIG_USER_ONLY */
10381
8bd5c820
PM
10382/* Convert a possible stage1+2 MMU index into the appropriate
10383 * stage 1 MMU index
10384 */
10385static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
10386{
b9f6033c 10387 switch (mmu_idx) {
b1a10c86
RDC
10388 case ARMMMUIdx_SE10_0:
10389 return ARMMMUIdx_Stage1_SE0;
10390 case ARMMMUIdx_SE10_1:
10391 return ARMMMUIdx_Stage1_SE1;
10392 case ARMMMUIdx_SE10_1_PAN:
10393 return ARMMMUIdx_Stage1_SE1_PAN;
b9f6033c
RH
10394 case ARMMMUIdx_E10_0:
10395 return ARMMMUIdx_Stage1_E0;
10396 case ARMMMUIdx_E10_1:
10397 return ARMMMUIdx_Stage1_E1;
452ef8cb
RH
10398 case ARMMMUIdx_E10_1_PAN:
10399 return ARMMMUIdx_Stage1_E1_PAN;
b9f6033c
RH
10400 default:
10401 return mmu_idx;
8bd5c820 10402 }
8bd5c820
PM
10403}
10404
0480f69a
PM
10405/* Return true if the translation regime is using LPAE format page tables */
10406static inline bool regime_using_lpae_format(CPUARMState *env,
10407 ARMMMUIdx mmu_idx)
10408{
10409 int el = regime_el(env, mmu_idx);
10410 if (el == 2 || arm_el_is_aa64(env, el)) {
10411 return true;
10412 }
10413 if (arm_feature(env, ARM_FEATURE_LPAE)
10414 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
10415 return true;
10416 }
10417 return false;
10418}
10419
deb2db99
AR
10420/* Returns true if the stage 1 translation regime is using LPAE format page
10421 * tables. Used when raising alignment exceptions, whose FSR changes depending
10422 * on whether the long or short descriptor format is in use. */
10423bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 10424{
8bd5c820 10425 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 10426
30901475
AB
10427 return regime_using_lpae_format(env, mmu_idx);
10428}
10429
c47eaf9f 10430#ifndef CONFIG_USER_ONLY
0480f69a
PM
10431static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
10432{
10433 switch (mmu_idx) {
fba37aed 10434 case ARMMMUIdx_SE10_0:
b9f6033c 10435 case ARMMMUIdx_E20_0:
b6ad6062 10436 case ARMMMUIdx_SE20_0:
2859d7b5 10437 case ARMMMUIdx_Stage1_E0:
b1a10c86 10438 case ARMMMUIdx_Stage1_SE0:
e7b921c2 10439 case ARMMMUIdx_MUser:
871bec7c 10440 case ARMMMUIdx_MSUser:
62593718
PM
10441 case ARMMMUIdx_MUserNegPri:
10442 case ARMMMUIdx_MSUserNegPri:
0480f69a
PM
10443 return true;
10444 default:
10445 return false;
01b98b68
RH
10446 case ARMMMUIdx_E10_0:
10447 case ARMMMUIdx_E10_1:
452ef8cb 10448 case ARMMMUIdx_E10_1_PAN:
0480f69a
PM
10449 g_assert_not_reached();
10450 }
10451}
10452
0fbf5238
AJ
10453/* Translate section/page access permissions to page
10454 * R/W protection flags
d76951b6
AJ
10455 *
10456 * @env: CPUARMState
10457 * @mmu_idx: MMU index indicating required translation regime
10458 * @ap: The 3-bit access permissions (AP[2:0])
10459 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
10460 */
10461static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
10462 int ap, int domain_prot)
10463{
554b0b09
PM
10464 bool is_user = regime_is_user(env, mmu_idx);
10465
10466 if (domain_prot == 3) {
10467 return PAGE_READ | PAGE_WRITE;
10468 }
10469
554b0b09
PM
10470 switch (ap) {
10471 case 0:
10472 if (arm_feature(env, ARM_FEATURE_V7)) {
10473 return 0;
10474 }
554b0b09
PM
10475 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
10476 case SCTLR_S:
10477 return is_user ? 0 : PAGE_READ;
10478 case SCTLR_R:
10479 return PAGE_READ;
10480 default:
10481 return 0;
10482 }
10483 case 1:
10484 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10485 case 2:
87c3d486 10486 if (is_user) {
0fbf5238 10487 return PAGE_READ;
87c3d486 10488 } else {
554b0b09 10489 return PAGE_READ | PAGE_WRITE;
87c3d486 10490 }
554b0b09
PM
10491 case 3:
10492 return PAGE_READ | PAGE_WRITE;
10493 case 4: /* Reserved. */
10494 return 0;
10495 case 5:
0fbf5238 10496 return is_user ? 0 : PAGE_READ;
554b0b09 10497 case 6:
0fbf5238 10498 return PAGE_READ;
554b0b09 10499 case 7:
87c3d486 10500 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 10501 return 0;
87c3d486 10502 }
0fbf5238 10503 return PAGE_READ;
554b0b09 10504 default:
0fbf5238 10505 g_assert_not_reached();
554b0b09 10506 }
b5ff1b31
FB
10507}
10508
d76951b6
AJ
10509/* Translate section/page access permissions to page
10510 * R/W protection flags.
10511 *
d76951b6 10512 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 10513 * @is_user: TRUE if accessing from PL0
d76951b6 10514 */
d8e052b3 10515static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 10516{
d76951b6
AJ
10517 switch (ap) {
10518 case 0:
10519 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10520 case 1:
10521 return PAGE_READ | PAGE_WRITE;
10522 case 2:
10523 return is_user ? 0 : PAGE_READ;
10524 case 3:
10525 return PAGE_READ;
10526 default:
10527 g_assert_not_reached();
10528 }
10529}
10530
d8e052b3
AJ
10531static inline int
10532simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
10533{
10534 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
10535}
10536
6ab1a5ee
EI
10537/* Translate S2 section/page access permissions to protection flags
10538 *
10539 * @env: CPUARMState
10540 * @s2ap: The 2-bit stage2 access permissions (S2AP)
ce3125be
PM
10541 * @xn: XN (execute-never) bits
10542 * @s1_is_el0: true if this is S2 of an S1+2 walk for EL0
6ab1a5ee 10543 */
ce3125be 10544static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0)
6ab1a5ee
EI
10545{
10546 int prot = 0;
10547
10548 if (s2ap & 1) {
10549 prot |= PAGE_READ;
10550 }
10551 if (s2ap & 2) {
10552 prot |= PAGE_WRITE;
10553 }
ce3125be
PM
10554
10555 if (cpu_isar_feature(any_tts2uxn, env_archcpu(env))) {
10556 switch (xn) {
10557 case 0:
dfda6837 10558 prot |= PAGE_EXEC;
ce3125be
PM
10559 break;
10560 case 1:
10561 if (s1_is_el0) {
10562 prot |= PAGE_EXEC;
10563 }
10564 break;
10565 case 2:
10566 break;
10567 case 3:
10568 if (!s1_is_el0) {
10569 prot |= PAGE_EXEC;
10570 }
10571 break;
10572 default:
10573 g_assert_not_reached();
10574 }
10575 } else {
10576 if (!extract32(xn, 1, 1)) {
10577 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
10578 prot |= PAGE_EXEC;
10579 }
dfda6837 10580 }
6ab1a5ee
EI
10581 }
10582 return prot;
10583}
10584
d8e052b3
AJ
10585/* Translate section/page access permissions to protection flags
10586 *
10587 * @env: CPUARMState
10588 * @mmu_idx: MMU index indicating required translation regime
10589 * @is_aa64: TRUE if AArch64
10590 * @ap: The 2-bit simple AP (AP[2:1])
10591 * @ns: NS (non-secure) bit
10592 * @xn: XN (execute-never) bit
10593 * @pxn: PXN (privileged execute-never) bit
10594 */
10595static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
10596 int ap, int ns, int xn, int pxn)
10597{
10598 bool is_user = regime_is_user(env, mmu_idx);
10599 int prot_rw, user_rw;
10600 bool have_wxn;
10601 int wxn = 0;
10602
97fa9350 10603 assert(mmu_idx != ARMMMUIdx_Stage2);
b1a10c86 10604 assert(mmu_idx != ARMMMUIdx_Stage2_S);
d8e052b3
AJ
10605
10606 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
10607 if (is_user) {
10608 prot_rw = user_rw;
10609 } else {
81636b70 10610 if (user_rw && regime_is_pan(env, mmu_idx)) {
f4e1dbc5
PM
10611 /* PAN forbids data accesses but doesn't affect insn fetch */
10612 prot_rw = 0;
10613 } else {
10614 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
81636b70 10615 }
d8e052b3
AJ
10616 }
10617
10618 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
10619 return prot_rw;
10620 }
10621
10622 /* TODO have_wxn should be replaced with
10623 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
10624 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
10625 * compatible processors have EL2, which is required for [U]WXN.
10626 */
10627 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
10628
10629 if (have_wxn) {
10630 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
10631 }
10632
10633 if (is_aa64) {
339370b9
RH
10634 if (regime_has_2_ranges(mmu_idx) && !is_user) {
10635 xn = pxn || (user_rw & PAGE_WRITE);
d8e052b3
AJ
10636 }
10637 } else if (arm_feature(env, ARM_FEATURE_V7)) {
10638 switch (regime_el(env, mmu_idx)) {
10639 case 1:
10640 case 3:
10641 if (is_user) {
10642 xn = xn || !(user_rw & PAGE_READ);
10643 } else {
10644 int uwxn = 0;
10645 if (have_wxn) {
10646 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
10647 }
10648 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
10649 (uwxn && (user_rw & PAGE_WRITE));
10650 }
10651 break;
10652 case 2:
10653 break;
10654 }
10655 } else {
10656 xn = wxn = 0;
10657 }
10658
10659 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
10660 return prot_rw;
10661 }
10662 return prot_rw | PAGE_EXEC;
10663}
10664
0480f69a
PM
10665static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
10666 uint32_t *table, uint32_t address)
b2fa1797 10667{
0480f69a 10668 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 10669 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 10670
11f136ee
FA
10671 if (address & tcr->mask) {
10672 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
10673 /* Translation table walk disabled for TTBR1 */
10674 return false;
10675 }
aef878be 10676 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 10677 } else {
11f136ee 10678 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
10679 /* Translation table walk disabled for TTBR0 */
10680 return false;
10681 }
aef878be 10682 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
10683 }
10684 *table |= (address >> 18) & 0x3ffc;
10685 return true;
b2fa1797
PB
10686}
10687
37785977
EI
10688/* Translate a S1 pagetable walk through S2 if needed. */
10689static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
3d4bd397 10690 hwaddr addr, bool *is_secure,
37785977
EI
10691 ARMMMUFaultInfo *fi)
10692{
fee7aa46 10693 if (arm_mmu_idx_is_stage1_of_2(mmu_idx) &&
97fa9350 10694 !regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
37785977
EI
10695 target_ulong s2size;
10696 hwaddr s2pa;
10697 int s2prot;
10698 int ret;
b1a10c86
RDC
10699 ARMMMUIdx s2_mmu_idx = *is_secure ? ARMMMUIdx_Stage2_S
10700 : ARMMMUIdx_Stage2;
eadb2feb 10701 ARMCacheAttrs cacheattrs = {};
3d4bd397
RDC
10702 MemTxAttrs txattrs = {};
10703
b1a10c86 10704 ret = get_phys_addr_lpae(env, addr, MMU_DATA_LOAD, s2_mmu_idx, false,
59dff859 10705 &s2pa, &txattrs, &s2prot, &s2size, fi,
a6d6f37a 10706 &cacheattrs);
37785977 10707 if (ret) {
3b39d734 10708 assert(fi->type != ARMFault_None);
37785977
EI
10709 fi->s2addr = addr;
10710 fi->stage2 = true;
10711 fi->s1ptw = true;
9861248f 10712 fi->s1ns = !*is_secure;
37785977
EI
10713 return ~0;
10714 }
e04a5752
RDC
10715 if ((arm_hcr_el2_eff(env) & HCR_PTW) &&
10716 (cacheattrs.attrs & 0xf0) == 0) {
a6d6f37a
RH
10717 /*
10718 * PTW set and S1 walk touched S2 Device memory:
10719 * generate Permission fault.
10720 */
eadb2feb
PM
10721 fi->type = ARMFault_Permission;
10722 fi->s2addr = addr;
10723 fi->stage2 = true;
10724 fi->s1ptw = true;
9861248f 10725 fi->s1ns = !*is_secure;
eadb2feb
PM
10726 return ~0;
10727 }
588c6dd1
RDC
10728
10729 if (arm_is_secure_below_el3(env)) {
10730 /* Check if page table walk is to secure or non-secure PA space. */
10731 if (*is_secure) {
10732 *is_secure = !(env->cp15.vstcr_el2.raw_tcr & VSTCR_SW);
10733 } else {
10734 *is_secure = !(env->cp15.vtcr_el2.raw_tcr & VTCR_NSW);
10735 }
10736 } else {
10737 assert(!*is_secure);
10738 }
10739
37785977
EI
10740 addr = s2pa;
10741 }
10742 return addr;
10743}
10744
14577270 10745/* All loads done in the course of a page table walk go through here. */
a614e698 10746static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 10747 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 10748{
a614e698
EI
10749 ARMCPU *cpu = ARM_CPU(cs);
10750 CPUARMState *env = &cpu->env;
ebca90e4 10751 MemTxAttrs attrs = {};
3b39d734 10752 MemTxResult result = MEMTX_OK;
5ce4ff65 10753 AddressSpace *as;
3b39d734 10754 uint32_t data;
ebca90e4 10755
3d4bd397 10756 addr = S1_ptw_translate(env, mmu_idx, addr, &is_secure, fi);
ebca90e4 10757 attrs.secure = is_secure;
5ce4ff65 10758 as = arm_addressspace(cs, attrs);
a614e698
EI
10759 if (fi->s1ptw) {
10760 return 0;
10761 }
73462ddd 10762 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 10763 data = address_space_ldl_be(as, addr, attrs, &result);
73462ddd 10764 } else {
3b39d734 10765 data = address_space_ldl_le(as, addr, attrs, &result);
73462ddd 10766 }
3b39d734
PM
10767 if (result == MEMTX_OK) {
10768 return data;
10769 }
10770 fi->type = ARMFault_SyncExternalOnWalk;
10771 fi->ea = arm_extabort_type(result);
10772 return 0;
ebca90e4
PM
10773}
10774
37785977 10775static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 10776 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 10777{
37785977
EI
10778 ARMCPU *cpu = ARM_CPU(cs);
10779 CPUARMState *env = &cpu->env;
ebca90e4 10780 MemTxAttrs attrs = {};
3b39d734 10781 MemTxResult result = MEMTX_OK;
5ce4ff65 10782 AddressSpace *as;
9aea1ea3 10783 uint64_t data;
ebca90e4 10784
3d4bd397 10785 addr = S1_ptw_translate(env, mmu_idx, addr, &is_secure, fi);
ebca90e4 10786 attrs.secure = is_secure;
5ce4ff65 10787 as = arm_addressspace(cs, attrs);
37785977
EI
10788 if (fi->s1ptw) {
10789 return 0;
10790 }
73462ddd 10791 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 10792 data = address_space_ldq_be(as, addr, attrs, &result);
73462ddd 10793 } else {
3b39d734
PM
10794 data = address_space_ldq_le(as, addr, attrs, &result);
10795 }
10796 if (result == MEMTX_OK) {
10797 return data;
73462ddd 10798 }
3b39d734
PM
10799 fi->type = ARMFault_SyncExternalOnWalk;
10800 fi->ea = arm_extabort_type(result);
10801 return 0;
ebca90e4
PM
10802}
10803
b7cc4e82 10804static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 10805 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10806 hwaddr *phys_ptr, int *prot,
f989983e 10807 target_ulong *page_size,
e14b5a23 10808 ARMMMUFaultInfo *fi)
b5ff1b31 10809{
2fc0cc0e 10810 CPUState *cs = env_cpu(env);
f989983e 10811 int level = 1;
b5ff1b31
FB
10812 uint32_t table;
10813 uint32_t desc;
10814 int type;
10815 int ap;
e389be16 10816 int domain = 0;
dd4ebc2e 10817 int domain_prot;
a8170e5e 10818 hwaddr phys_addr;
0480f69a 10819 uint32_t dacr;
b5ff1b31 10820
9ee6e8bb
PB
10821 /* Pagetable walk. */
10822 /* Lookup l1 descriptor. */
0480f69a 10823 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10824 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f989983e 10825 fi->type = ARMFault_Translation;
e389be16
FA
10826 goto do_fault;
10827 }
a614e698 10828 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10829 mmu_idx, fi);
3b39d734
PM
10830 if (fi->type != ARMFault_None) {
10831 goto do_fault;
10832 }
9ee6e8bb 10833 type = (desc & 3);
dd4ebc2e 10834 domain = (desc >> 5) & 0x0f;
0480f69a
PM
10835 if (regime_el(env, mmu_idx) == 1) {
10836 dacr = env->cp15.dacr_ns;
10837 } else {
10838 dacr = env->cp15.dacr_s;
10839 }
10840 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 10841 if (type == 0) {
601d70b9 10842 /* Section translation fault. */
f989983e 10843 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10844 goto do_fault;
10845 }
f989983e
PM
10846 if (type != 2) {
10847 level = 2;
10848 }
dd4ebc2e 10849 if (domain_prot == 0 || domain_prot == 2) {
f989983e 10850 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10851 goto do_fault;
10852 }
10853 if (type == 2) {
10854 /* 1Mb section. */
10855 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
10856 ap = (desc >> 10) & 3;
d4c430a8 10857 *page_size = 1024 * 1024;
9ee6e8bb
PB
10858 } else {
10859 /* Lookup l2 entry. */
554b0b09
PM
10860 if (type == 1) {
10861 /* Coarse pagetable. */
10862 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
10863 } else {
10864 /* Fine pagetable. */
10865 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
10866 }
a614e698 10867 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10868 mmu_idx, fi);
3b39d734
PM
10869 if (fi->type != ARMFault_None) {
10870 goto do_fault;
10871 }
9ee6e8bb
PB
10872 switch (desc & 3) {
10873 case 0: /* Page translation fault. */
f989983e 10874 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10875 goto do_fault;
10876 case 1: /* 64k page. */
10877 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10878 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 10879 *page_size = 0x10000;
ce819861 10880 break;
9ee6e8bb
PB
10881 case 2: /* 4k page. */
10882 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 10883 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 10884 *page_size = 0x1000;
ce819861 10885 break;
fc1891c7 10886 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 10887 if (type == 1) {
fc1891c7
PM
10888 /* ARMv6/XScale extended small page format */
10889 if (arm_feature(env, ARM_FEATURE_XSCALE)
10890 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 10891 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 10892 *page_size = 0x1000;
554b0b09 10893 } else {
fc1891c7
PM
10894 /* UNPREDICTABLE in ARMv5; we choose to take a
10895 * page translation fault.
10896 */
f989983e 10897 fi->type = ARMFault_Translation;
554b0b09
PM
10898 goto do_fault;
10899 }
10900 } else {
10901 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 10902 *page_size = 0x400;
554b0b09 10903 }
9ee6e8bb 10904 ap = (desc >> 4) & 3;
ce819861
PB
10905 break;
10906 default:
9ee6e8bb
PB
10907 /* Never happens, but compiler isn't smart enough to tell. */
10908 abort();
ce819861 10909 }
9ee6e8bb 10910 }
0fbf5238
AJ
10911 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
10912 *prot |= *prot ? PAGE_EXEC : 0;
10913 if (!(*prot & (1 << access_type))) {
9ee6e8bb 10914 /* Access permission fault. */
f989983e 10915 fi->type = ARMFault_Permission;
9ee6e8bb
PB
10916 goto do_fault;
10917 }
10918 *phys_ptr = phys_addr;
b7cc4e82 10919 return false;
9ee6e8bb 10920do_fault:
f989983e
PM
10921 fi->domain = domain;
10922 fi->level = level;
b7cc4e82 10923 return true;
9ee6e8bb
PB
10924}
10925
b7cc4e82 10926static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 10927 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10928 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
f06cf243 10929 target_ulong *page_size, ARMMMUFaultInfo *fi)
9ee6e8bb 10930{
2fc0cc0e 10931 CPUState *cs = env_cpu(env);
0ae0326b 10932 ARMCPU *cpu = env_archcpu(env);
f06cf243 10933 int level = 1;
9ee6e8bb
PB
10934 uint32_t table;
10935 uint32_t desc;
10936 uint32_t xn;
de9b05b8 10937 uint32_t pxn = 0;
9ee6e8bb
PB
10938 int type;
10939 int ap;
de9b05b8 10940 int domain = 0;
dd4ebc2e 10941 int domain_prot;
a8170e5e 10942 hwaddr phys_addr;
0480f69a 10943 uint32_t dacr;
8bf5b6a9 10944 bool ns;
9ee6e8bb
PB
10945
10946 /* Pagetable walk. */
10947 /* Lookup l1 descriptor. */
0480f69a 10948 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10949 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f06cf243 10950 fi->type = ARMFault_Translation;
e389be16
FA
10951 goto do_fault;
10952 }
a614e698 10953 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10954 mmu_idx, fi);
3b39d734
PM
10955 if (fi->type != ARMFault_None) {
10956 goto do_fault;
10957 }
9ee6e8bb 10958 type = (desc & 3);
0ae0326b 10959 if (type == 0 || (type == 3 && !cpu_isar_feature(aa32_pxn, cpu))) {
de9b05b8
PM
10960 /* Section translation fault, or attempt to use the encoding
10961 * which is Reserved on implementations without PXN.
10962 */
f06cf243 10963 fi->type = ARMFault_Translation;
9ee6e8bb 10964 goto do_fault;
de9b05b8
PM
10965 }
10966 if ((type == 1) || !(desc & (1 << 18))) {
10967 /* Page or Section. */
dd4ebc2e 10968 domain = (desc >> 5) & 0x0f;
9ee6e8bb 10969 }
0480f69a
PM
10970 if (regime_el(env, mmu_idx) == 1) {
10971 dacr = env->cp15.dacr_ns;
10972 } else {
10973 dacr = env->cp15.dacr_s;
10974 }
f06cf243
PM
10975 if (type == 1) {
10976 level = 2;
10977 }
0480f69a 10978 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 10979 if (domain_prot == 0 || domain_prot == 2) {
f06cf243
PM
10980 /* Section or Page domain fault */
10981 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10982 goto do_fault;
10983 }
de9b05b8 10984 if (type != 1) {
9ee6e8bb
PB
10985 if (desc & (1 << 18)) {
10986 /* Supersection. */
10987 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
10988 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
10989 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 10990 *page_size = 0x1000000;
b5ff1b31 10991 } else {
9ee6e8bb
PB
10992 /* Section. */
10993 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 10994 *page_size = 0x100000;
b5ff1b31 10995 }
9ee6e8bb
PB
10996 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
10997 xn = desc & (1 << 4);
de9b05b8 10998 pxn = desc & 1;
8bf5b6a9 10999 ns = extract32(desc, 19, 1);
9ee6e8bb 11000 } else {
0ae0326b 11001 if (cpu_isar_feature(aa32_pxn, cpu)) {
de9b05b8
PM
11002 pxn = (desc >> 2) & 1;
11003 }
8bf5b6a9 11004 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
11005 /* Lookup l2 entry. */
11006 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698 11007 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 11008 mmu_idx, fi);
3b39d734
PM
11009 if (fi->type != ARMFault_None) {
11010 goto do_fault;
11011 }
9ee6e8bb
PB
11012 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
11013 switch (desc & 3) {
11014 case 0: /* Page translation fault. */
f06cf243 11015 fi->type = ARMFault_Translation;
b5ff1b31 11016 goto do_fault;
9ee6e8bb
PB
11017 case 1: /* 64k page. */
11018 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
11019 xn = desc & (1 << 15);
d4c430a8 11020 *page_size = 0x10000;
9ee6e8bb
PB
11021 break;
11022 case 2: case 3: /* 4k page. */
11023 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
11024 xn = desc & 1;
d4c430a8 11025 *page_size = 0x1000;
9ee6e8bb
PB
11026 break;
11027 default:
11028 /* Never happens, but compiler isn't smart enough to tell. */
11029 abort();
b5ff1b31 11030 }
9ee6e8bb 11031 }
dd4ebc2e 11032 if (domain_prot == 3) {
c0034328
JR
11033 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11034 } else {
0480f69a 11035 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
11036 xn = 1;
11037 }
f06cf243
PM
11038 if (xn && access_type == MMU_INST_FETCH) {
11039 fi->type = ARMFault_Permission;
c0034328 11040 goto do_fault;
f06cf243 11041 }
9ee6e8bb 11042
d76951b6
AJ
11043 if (arm_feature(env, ARM_FEATURE_V6K) &&
11044 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
11045 /* The simplified model uses AP[0] as an access control bit. */
11046 if ((ap & 1) == 0) {
11047 /* Access flag fault. */
f06cf243 11048 fi->type = ARMFault_AccessFlag;
d76951b6
AJ
11049 goto do_fault;
11050 }
11051 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
11052 } else {
11053 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 11054 }
0fbf5238
AJ
11055 if (*prot && !xn) {
11056 *prot |= PAGE_EXEC;
11057 }
11058 if (!(*prot & (1 << access_type))) {
c0034328 11059 /* Access permission fault. */
f06cf243 11060 fi->type = ARMFault_Permission;
c0034328
JR
11061 goto do_fault;
11062 }
3ad493fc 11063 }
8bf5b6a9
PM
11064 if (ns) {
11065 /* The NS bit will (as required by the architecture) have no effect if
11066 * the CPU doesn't support TZ or this is a non-secure translation
11067 * regime, because the attribute will already be non-secure.
11068 */
11069 attrs->secure = false;
11070 }
9ee6e8bb 11071 *phys_ptr = phys_addr;
b7cc4e82 11072 return false;
b5ff1b31 11073do_fault:
f06cf243
PM
11074 fi->domain = domain;
11075 fi->level = level;
b7cc4e82 11076 return true;
b5ff1b31
FB
11077}
11078
1853d5a9 11079/*
a0e966c9 11080 * check_s2_mmu_setup
1853d5a9
EI
11081 * @cpu: ARMCPU
11082 * @is_aa64: True if the translation regime is in AArch64 state
11083 * @startlevel: Suggested starting level
11084 * @inputsize: Bitsize of IPAs
11085 * @stride: Page-table stride (See the ARM ARM)
11086 *
a0e966c9
EI
11087 * Returns true if the suggested S2 translation parameters are OK and
11088 * false otherwise.
1853d5a9 11089 */
a0e966c9 11090static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
49ba115b 11091 int inputsize, int stride, int outputsize)
1853d5a9 11092{
98d68ec2
EI
11093 const int grainsize = stride + 3;
11094 int startsizecheck;
11095
ef56c242
RH
11096 /*
11097 * Negative levels are usually not allowed...
11098 * Except for FEAT_LPA2, 4k page table, 52-bit address space, which
11099 * begins with level -1. Note that previous feature tests will have
11100 * eliminated this combination if it is not enabled.
11101 */
11102 if (level < (inputsize == 52 && stride == 9 ? -1 : 0)) {
1853d5a9
EI
11103 return false;
11104 }
11105
98d68ec2
EI
11106 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
11107 if (startsizecheck < 1 || startsizecheck > stride + 4) {
11108 return false;
11109 }
11110
1853d5a9 11111 if (is_aa64) {
1853d5a9
EI
11112 switch (stride) {
11113 case 13: /* 64KB Pages. */
49ba115b 11114 if (level == 0 || (level == 1 && outputsize <= 42)) {
1853d5a9
EI
11115 return false;
11116 }
11117 break;
11118 case 11: /* 16KB Pages. */
49ba115b 11119 if (level == 0 || (level == 1 && outputsize <= 40)) {
1853d5a9
EI
11120 return false;
11121 }
11122 break;
11123 case 9: /* 4KB Pages. */
49ba115b 11124 if (level == 0 && outputsize <= 42) {
1853d5a9
EI
11125 return false;
11126 }
11127 break;
11128 default:
11129 g_assert_not_reached();
11130 }
3526423e
EI
11131
11132 /* Inputsize checks. */
49ba115b
RH
11133 if (inputsize > outputsize &&
11134 (arm_el_is_aa64(&cpu->env, 1) || inputsize > 40)) {
3526423e
EI
11135 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
11136 return false;
11137 }
1853d5a9 11138 } else {
1853d5a9
EI
11139 /* AArch32 only supports 4KB pages. Assert on that. */
11140 assert(stride == 9);
11141
11142 if (level == 0) {
11143 return false;
11144 }
1853d5a9
EI
11145 }
11146 return true;
11147}
11148
5b2d261d
AB
11149/* Translate from the 4-bit stage 2 representation of
11150 * memory attributes (without cache-allocation hints) to
11151 * the 8-bit representation of the stage 1 MAIR registers
11152 * (which includes allocation hints).
11153 *
11154 * ref: shared/translation/attrs/S2AttrDecode()
11155 * .../S2ConvertAttrsHints()
11156 */
11157static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
11158{
11159 uint8_t hiattr = extract32(s2attrs, 2, 2);
11160 uint8_t loattr = extract32(s2attrs, 0, 2);
11161 uint8_t hihint = 0, lohint = 0;
11162
11163 if (hiattr != 0) { /* normal memory */
e04a5752 11164 if (arm_hcr_el2_eff(env) & HCR_CD) { /* cache disabled */
5b2d261d
AB
11165 hiattr = loattr = 1; /* non-cacheable */
11166 } else {
11167 if (hiattr != 1) { /* Write-through or write-back */
11168 hihint = 3; /* RW allocate */
11169 }
11170 if (loattr != 1) { /* Write-through or write-back */
11171 lohint = 3; /* RW allocate */
11172 }
11173 }
11174 }
11175
11176 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
11177}
c47eaf9f 11178#endif /* !CONFIG_USER_ONLY */
5b2d261d 11179
f4ecc015
RH
11180/* This mapping is common between ID_AA64MMFR0.PARANGE and TCR_ELx.{I}PS. */
11181static const uint8_t pamax_map[] = {
11182 [0] = 32,
11183 [1] = 36,
11184 [2] = 40,
11185 [3] = 42,
11186 [4] = 44,
11187 [5] = 48,
7a928f43 11188 [6] = 52,
f4ecc015
RH
11189};
11190
71a77257
RH
11191/* The cpu-specific constant value of PAMax; also used by hw/arm/virt. */
11192unsigned int arm_pamax(ARMCPU *cpu)
11193{
71a77257
RH
11194 unsigned int parange =
11195 FIELD_EX64(cpu->isar.id_aa64mmfr0, ID_AA64MMFR0, PARANGE);
11196
11197 /*
11198 * id_aa64mmfr0 is a read-only register so values outside of the
11199 * supported mappings can be considered an implementation error.
11200 */
11201 assert(parange < ARRAY_SIZE(pamax_map));
11202 return pamax_map[parange];
11203}
11204
b830a5ee
RH
11205static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
11206{
11207 if (regime_has_2_ranges(mmu_idx)) {
11208 return extract64(tcr, 37, 2);
b1a10c86 11209 } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
b830a5ee
RH
11210 return 0; /* VTCR_EL2 */
11211 } else {
3e270f67
RH
11212 /* Replicate the single TBI bit so we always have 2 bits. */
11213 return extract32(tcr, 20, 1) * 3;
b830a5ee
RH
11214 }
11215}
11216
11217static int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
11218{
11219 if (regime_has_2_ranges(mmu_idx)) {
11220 return extract64(tcr, 51, 2);
b1a10c86 11221 } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
b830a5ee
RH
11222 return 0; /* VTCR_EL2 */
11223 } else {
3e270f67
RH
11224 /* Replicate the single TBID bit so we always have 2 bits. */
11225 return extract32(tcr, 29, 1) * 3;
b830a5ee
RH
11226 }
11227}
11228
81ae05fa
RH
11229static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
11230{
11231 if (regime_has_2_ranges(mmu_idx)) {
11232 return extract64(tcr, 57, 2);
11233 } else {
11234 /* Replicate the single TCMA bit so we always have 2 bits. */
11235 return extract32(tcr, 30, 1) * 3;
11236 }
11237}
11238
b830a5ee
RH
11239ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11240 ARMMMUIdx mmu_idx, bool data)
ba97be9f
RH
11241{
11242 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
ef56c242
RH
11243 bool epd, hpd, using16k, using64k, tsz_oob, ds;
11244 int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11245 ARMCPU *cpu = env_archcpu(env);
ba97be9f 11246
339370b9 11247 if (!regime_has_2_ranges(mmu_idx)) {
71d18164 11248 select = 0;
ba97be9f
RH
11249 tsz = extract32(tcr, 0, 6);
11250 using64k = extract32(tcr, 14, 1);
11251 using16k = extract32(tcr, 15, 1);
b1a10c86 11252 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
ba97be9f 11253 /* VTCR_EL2 */
b830a5ee 11254 hpd = false;
ba97be9f 11255 } else {
ba97be9f
RH
11256 hpd = extract32(tcr, 24, 1);
11257 }
11258 epd = false;
ef56c242 11259 sh = extract32(tcr, 12, 2);
f4ecc015 11260 ps = extract32(tcr, 16, 3);
ef56c242 11261 ds = extract64(tcr, 32, 1);
ba97be9f 11262 } else {
71d18164
RH
11263 /*
11264 * Bit 55 is always between the two regions, and is canonical for
11265 * determining if address tagging is enabled.
11266 */
11267 select = extract64(va, 55, 1);
11268 if (!select) {
11269 tsz = extract32(tcr, 0, 6);
11270 epd = extract32(tcr, 7, 1);
ef56c242 11271 sh = extract32(tcr, 12, 2);
71d18164
RH
11272 using64k = extract32(tcr, 14, 1);
11273 using16k = extract32(tcr, 15, 1);
71d18164 11274 hpd = extract64(tcr, 41, 1);
71d18164
RH
11275 } else {
11276 int tg = extract32(tcr, 30, 2);
11277 using16k = tg == 1;
11278 using64k = tg == 3;
11279 tsz = extract32(tcr, 16, 6);
11280 epd = extract32(tcr, 23, 1);
ef56c242 11281 sh = extract32(tcr, 28, 2);
71d18164 11282 hpd = extract64(tcr, 42, 1);
71d18164 11283 }
f4ecc015 11284 ps = extract64(tcr, 32, 3);
ef56c242 11285 ds = extract64(tcr, 59, 1);
ba97be9f 11286 }
c36c65ea 11287
ef56c242 11288 if (cpu_isar_feature(aa64_st, cpu)) {
c36c65ea
RDC
11289 max_tsz = 48 - using64k;
11290 } else {
11291 max_tsz = 39;
11292 }
0af312b6 11293
ef56c242
RH
11294 /*
11295 * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11296 * adjust the effective value of DS, as documented.
11297 */
0af312b6
RH
11298 min_tsz = 16;
11299 if (using64k) {
ef56c242
RH
11300 if (cpu_isar_feature(aa64_lva, cpu)) {
11301 min_tsz = 12;
11302 }
11303 ds = false;
11304 } else if (ds) {
11305 switch (mmu_idx) {
11306 case ARMMMUIdx_Stage2:
11307 case ARMMMUIdx_Stage2_S:
11308 if (using16k) {
11309 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11310 } else {
11311 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11312 }
11313 break;
11314 default:
11315 if (using16k) {
11316 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11317 } else {
11318 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11319 }
11320 break;
11321 }
11322 if (ds) {
0af312b6
RH
11323 min_tsz = 12;
11324 }
11325 }
c36c65ea 11326
ebf93ce7
RH
11327 if (tsz > max_tsz) {
11328 tsz = max_tsz;
11329 tsz_oob = true;
11330 } else if (tsz < min_tsz) {
11331 tsz = min_tsz;
11332 tsz_oob = true;
11333 } else {
11334 tsz_oob = false;
11335 }
ba97be9f 11336
b830a5ee
RH
11337 /* Present TBI as a composite with TBID. */
11338 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11339 if (!data) {
11340 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11341 }
11342 tbi = (tbi >> select) & 1;
11343
ba97be9f
RH
11344 return (ARMVAParameters) {
11345 .tsz = tsz,
f4ecc015 11346 .ps = ps,
ef56c242 11347 .sh = sh,
ba97be9f
RH
11348 .select = select,
11349 .tbi = tbi,
11350 .epd = epd,
11351 .hpd = hpd,
11352 .using16k = using16k,
11353 .using64k = using64k,
ebf93ce7 11354 .tsz_oob = tsz_oob,
ef56c242 11355 .ds = ds,
ba97be9f
RH
11356 };
11357}
11358
c47eaf9f 11359#ifndef CONFIG_USER_ONLY
ba97be9f
RH
11360static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
11361 ARMMMUIdx mmu_idx)
11362{
11363 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
11364 uint32_t el = regime_el(env, mmu_idx);
11365 int select, tsz;
11366 bool epd, hpd;
11367
b1a10c86
RDC
11368 assert(mmu_idx != ARMMMUIdx_Stage2_S);
11369
97fa9350 11370 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f
RH
11371 /* VTCR */
11372 bool sext = extract32(tcr, 4, 1);
11373 bool sign = extract32(tcr, 3, 1);
11374
11375 /*
11376 * If the sign-extend bit is not the same as t0sz[3], the result
11377 * is unpredictable. Flag this as a guest error.
11378 */
11379 if (sign != sext) {
11380 qemu_log_mask(LOG_GUEST_ERROR,
11381 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
11382 }
11383 tsz = sextract32(tcr, 0, 4) + 8;
11384 select = 0;
11385 hpd = false;
11386 epd = false;
11387 } else if (el == 2) {
11388 /* HTCR */
11389 tsz = extract32(tcr, 0, 3);
11390 select = 0;
11391 hpd = extract64(tcr, 24, 1);
11392 epd = false;
11393 } else {
11394 int t0sz = extract32(tcr, 0, 3);
11395 int t1sz = extract32(tcr, 16, 3);
11396
11397 if (t1sz == 0) {
11398 select = va > (0xffffffffu >> t0sz);
11399 } else {
11400 /* Note that we will detect errors later. */
11401 select = va >= ~(0xffffffffu >> t1sz);
11402 }
11403 if (!select) {
11404 tsz = t0sz;
11405 epd = extract32(tcr, 7, 1);
11406 hpd = extract64(tcr, 41, 1);
11407 } else {
11408 tsz = t1sz;
11409 epd = extract32(tcr, 23, 1);
11410 hpd = extract64(tcr, 42, 1);
11411 }
11412 /* For aarch32, hpd0 is not enabled without t2e as well. */
11413 hpd &= extract32(tcr, 6, 1);
11414 }
11415
11416 return (ARMVAParameters) {
11417 .tsz = tsz,
11418 .select = select,
11419 .epd = epd,
11420 .hpd = hpd,
11421 };
11422}
11423
ff7de2fc
PM
11424/**
11425 * get_phys_addr_lpae: perform one stage of page table walk, LPAE format
11426 *
11427 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
11428 * prot and page_size may not be filled in, and the populated fsr value provides
11429 * information on why the translation aborted, in the format of a long-format
11430 * DFSR/IFSR fault register, with the following caveats:
11431 * * the WnR bit is never set (the caller must do this).
11432 *
11433 * @env: CPUARMState
11434 * @address: virtual address to get physical address for
11435 * @access_type: MMU_DATA_LOAD, MMU_DATA_STORE or MMU_INST_FETCH
11436 * @mmu_idx: MMU index indicating required translation regime
11437 * @s1_is_el0: if @mmu_idx is ARMMMUIdx_Stage2 (so this is a stage 2 page table
11438 * walk), must be true if this is stage 2 of a stage 1+2 walk for an
11439 * EL0 access). If @mmu_idx is anything else, @s1_is_el0 is ignored.
11440 * @phys_ptr: set to the physical address corresponding to the virtual address
11441 * @attrs: set to the memory transaction attributes to use
11442 * @prot: set to the permissions for the page containing phys_ptr
11443 * @page_size_ptr: set to the size of the page containing phys_ptr
11444 * @fi: set to fault info if the translation fails
11445 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
11446 */
98e87797 11447static bool get_phys_addr_lpae(CPUARMState *env, uint64_t address,
03ae85f8 11448 MMUAccessType access_type, ARMMMUIdx mmu_idx,
ff7de2fc 11449 bool s1_is_el0,
b7cc4e82 11450 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 11451 target_ulong *page_size_ptr,
5b2d261d 11452 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
3dde962f 11453{
2fc0cc0e 11454 ARMCPU *cpu = env_archcpu(env);
1853d5a9 11455 CPUState *cs = CPU(cpu);
3dde962f 11456 /* Read an LPAE long-descriptor translation table. */
da909b2c 11457 ARMFaultType fault_type = ARMFault_Translation;
1b4093ea 11458 uint32_t level;
ba97be9f 11459 ARMVAParameters param;
3dde962f 11460 uint64_t ttbr;
dddb5223 11461 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f 11462 uint32_t tableattrs;
36d820af 11463 target_ulong page_size;
3dde962f 11464 uint32_t attrs;
ba97be9f 11465 int32_t stride;
49ba115b 11466 int addrsize, inputsize, outputsize;
0480f69a 11467 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 11468 int ap, ns, xn, pxn;
88e8add8 11469 uint32_t el = regime_el(env, mmu_idx);
6109769a 11470 uint64_t descaddrmask;
6e99f762 11471 bool aarch64 = arm_el_is_aa64(env, el);
1bafc2ba 11472 bool guarded = false;
0480f69a 11473
07d1be3b 11474 /* TODO: This code does not support shareability levels. */
6e99f762 11475 if (aarch64) {
f4ecc015
RH
11476 int ps;
11477
ba97be9f
RH
11478 param = aa64_va_parameters(env, address, mmu_idx,
11479 access_type != MMU_INST_FETCH);
1b4093ea 11480 level = 0;
ebf93ce7
RH
11481
11482 /*
11483 * If TxSZ is programmed to a value larger than the maximum,
11484 * or smaller than the effective minimum, it is IMPLEMENTATION
11485 * DEFINED whether we behave as if the field were programmed
11486 * within bounds, or if a level 0 Translation fault is generated.
11487 *
11488 * With FEAT_LVA, fault on less than minimum becomes required,
11489 * so our choice is to always raise the fault.
11490 */
11491 if (param.tsz_oob) {
11492 fault_type = ARMFault_Translation;
11493 goto do_fault;
11494 }
11495
ba97be9f
RH
11496 addrsize = 64 - 8 * param.tbi;
11497 inputsize = 64 - param.tsz;
f4ecc015
RH
11498
11499 /*
11500 * Bound PS by PARANGE to find the effective output address size.
11501 * ID_AA64MMFR0 is a read-only register so values outside of the
11502 * supported mappings can be considered an implementation error.
11503 */
11504 ps = FIELD_EX64(cpu->isar.id_aa64mmfr0, ID_AA64MMFR0, PARANGE);
11505 ps = MIN(ps, param.ps);
11506 assert(ps < ARRAY_SIZE(pamax_map));
11507 outputsize = pamax_map[ps];
d0a2cbce 11508 } else {
ba97be9f 11509 param = aa32_va_parameters(env, address, mmu_idx);
1b4093ea 11510 level = 1;
97fa9350 11511 addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32);
ba97be9f 11512 inputsize = addrsize - param.tsz;
49ba115b 11513 outputsize = 40;
2c8dd318 11514 }
3dde962f 11515
ba97be9f
RH
11516 /*
11517 * We determined the region when collecting the parameters, but we
11518 * have not yet validated that the address is valid for the region.
11519 * Extract the top bits and verify that they all match select.
36d820af
RH
11520 *
11521 * For aa32, if inputsize == addrsize, then we have selected the
11522 * region by exclusion in aa32_va_parameters and there is no more
11523 * validation to do here.
11524 */
11525 if (inputsize < addrsize) {
11526 target_ulong top_bits = sextract64(address, inputsize,
11527 addrsize - inputsize);
03f27724 11528 if (-top_bits != param.select) {
36d820af
RH
11529 /* The gap between the two regions is a Translation fault */
11530 fault_type = ARMFault_Translation;
11531 goto do_fault;
11532 }
3dde962f
PM
11533 }
11534
ba97be9f
RH
11535 if (param.using64k) {
11536 stride = 13;
11537 } else if (param.using16k) {
11538 stride = 11;
11539 } else {
11540 stride = 9;
11541 }
11542
3dde962f
PM
11543 /* Note that QEMU ignores shareability and cacheability attributes,
11544 * so we don't need to do anything with the SH, ORGN, IRGN fields
11545 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
11546 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
11547 * implement any ASID-like capability so we can ignore it (instead
11548 * we will always flush the TLB any time the ASID is changed).
11549 */
ba97be9f 11550 ttbr = regime_ttbr(env, mmu_idx, param.select);
3dde962f 11551
0480f69a 11552 /* Here we should have set up all the parameters for the translation:
6e99f762 11553 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
11554 */
11555
ba97be9f 11556 if (param.epd) {
88e8add8
GB
11557 /* Translation table walk disabled => Translation fault on TLB miss
11558 * Note: This is always 0 on 64-bit EL2 and EL3.
11559 */
3dde962f
PM
11560 goto do_fault;
11561 }
11562
b1a10c86 11563 if (mmu_idx != ARMMMUIdx_Stage2 && mmu_idx != ARMMMUIdx_Stage2_S) {
1853d5a9
EI
11564 /* The starting level depends on the virtual address size (which can
11565 * be up to 48 bits) and the translation granule size. It indicates
11566 * the number of strides (stride bits at a time) needed to
11567 * consume the bits of the input address. In the pseudocode this is:
11568 * level = 4 - RoundUp((inputsize - grainsize) / stride)
11569 * where their 'inputsize' is our 'inputsize', 'grainsize' is
11570 * our 'stride + 3' and 'stride' is our 'stride'.
11571 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
11572 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
11573 * = 4 - (inputsize - 4) / stride;
11574 */
11575 level = 4 - (inputsize - 4) / stride;
11576 } else {
11577 /* For stage 2 translations the starting level is specified by the
11578 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
11579 */
1b4093ea 11580 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
ef56c242 11581 uint32_t sl2 = extract64(tcr->raw_tcr, 33, 1);
1b4093ea 11582 uint32_t startlevel;
1853d5a9
EI
11583 bool ok;
11584
ef56c242
RH
11585 /* SL2 is RES0 unless DS=1 & 4kb granule. */
11586 if (param.ds && stride == 9 && sl2) {
11587 if (sl0 != 0) {
11588 level = 0;
11589 fault_type = ARMFault_Translation;
11590 goto do_fault;
11591 }
11592 startlevel = -1;
11593 } else if (!aarch64 || stride == 9) {
1853d5a9 11594 /* AArch32 or 4KB pages */
1b4093ea 11595 startlevel = 2 - sl0;
c36c65ea
RDC
11596
11597 if (cpu_isar_feature(aa64_st, cpu)) {
11598 startlevel &= 3;
11599 }
1853d5a9
EI
11600 } else {
11601 /* 16KB or 64KB pages */
1b4093ea 11602 startlevel = 3 - sl0;
1853d5a9
EI
11603 }
11604
11605 /* Check that the starting level is valid. */
6e99f762 11606 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
49ba115b 11607 inputsize, stride, outputsize);
1853d5a9 11608 if (!ok) {
da909b2c 11609 fault_type = ARMFault_Translation;
1853d5a9
EI
11610 goto do_fault;
11611 }
1b4093ea 11612 level = startlevel;
1853d5a9 11613 }
3dde962f 11614
d06449f2
RH
11615 indexmask_grainsize = MAKE_64BIT_MASK(0, stride + 3);
11616 indexmask = MAKE_64BIT_MASK(0, inputsize - (stride * (4 - level)));
3dde962f
PM
11617
11618 /* Now we can extract the actual base address from the TTBR */
2c8dd318 11619 descaddr = extract64(ttbr, 0, 48);
f4ecc015
RH
11620
11621 /*
7a928f43
RH
11622 * For FEAT_LPA and PS=6, bits [51:48] of descaddr are in [5:2] of TTBR.
11623 *
11624 * Otherwise, if the base address is out of range, raise AddressSizeFault.
f4ecc015
RH
11625 * In the pseudocode, this is !IsZero(baseregister<47:outputsize>),
11626 * but we've just cleared the bits above 47, so simplify the test.
11627 */
7a928f43
RH
11628 if (outputsize > 48) {
11629 descaddr |= extract64(ttbr, 2, 4) << 48;
11630 } else if (descaddr >> outputsize) {
f4ecc015
RH
11631 level = 0;
11632 fault_type = ARMFault_AddressSize;
11633 goto do_fault;
11634 }
11635
41a4bf1f
PM
11636 /*
11637 * We rely on this masking to clear the RES0 bits at the bottom of the TTBR
11638 * and also to mask out CnP (bit 0) which could validly be non-zero.
11639 */
dddb5223 11640 descaddr &= ~indexmask;
3dde962f 11641
f4ecc015
RH
11642 /*
11643 * For AArch32, the address field in the descriptor goes up to bit 39
11644 * for both v7 and v8. However, for v8 the SBZ bits [47:40] must be 0
11645 * or an AddressSize fault is raised. So for v8 we extract those SBZ
11646 * bits as part of the address, which will be checked via outputsize.
ef56c242
RH
11647 * For AArch64, the address field goes up to bit 47, or 49 with FEAT_LPA2;
11648 * the highest bits of a 52-bit output are placed elsewhere.
6109769a 11649 */
ef56c242
RH
11650 if (param.ds) {
11651 descaddrmask = MAKE_64BIT_MASK(0, 50);
11652 } else if (arm_feature(env, ARM_FEATURE_V8)) {
f4ecc015
RH
11653 descaddrmask = MAKE_64BIT_MASK(0, 48);
11654 } else {
11655 descaddrmask = MAKE_64BIT_MASK(0, 40);
11656 }
11657 descaddrmask &= ~indexmask_grainsize;
6109769a 11658
ebca90e4
PM
11659 /* Secure accesses start with the page table in secure memory and
11660 * can be downgraded to non-secure at any step. Non-secure accesses
11661 * remain non-secure. We implement this by just ORing in the NSTable/NS
11662 * bits at each step.
11663 */
11664 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
11665 for (;;) {
11666 uint64_t descriptor;
ebca90e4 11667 bool nstable;
3dde962f 11668
dddb5223 11669 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 11670 descaddr &= ~7ULL;
ebca90e4 11671 nstable = extract32(tableattrs, 4, 1);
3795a6de 11672 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
3b39d734 11673 if (fi->type != ARMFault_None) {
37785977
EI
11674 goto do_fault;
11675 }
11676
3dde962f
PM
11677 if (!(descriptor & 1) ||
11678 (!(descriptor & 2) && (level == 3))) {
11679 /* Invalid, or the Reserved level 3 encoding */
11680 goto do_fault;
11681 }
f4ecc015 11682
6109769a 11683 descaddr = descriptor & descaddrmask;
7a928f43
RH
11684
11685 /*
11686 * For FEAT_LPA and PS=6, bits [51:48] of descaddr are in [15:12]
ef56c242
RH
11687 * of descriptor. For FEAT_LPA2 and effective DS, bits [51:50] of
11688 * descaddr are in [9:8]. Otherwise, if descaddr is out of range,
11689 * raise AddressSizeFault.
7a928f43
RH
11690 */
11691 if (outputsize > 48) {
ef56c242
RH
11692 if (param.ds) {
11693 descaddr |= extract64(descriptor, 8, 2) << 50;
11694 } else {
11695 descaddr |= extract64(descriptor, 12, 4) << 48;
11696 }
7a928f43 11697 } else if (descaddr >> outputsize) {
f4ecc015
RH
11698 fault_type = ARMFault_AddressSize;
11699 goto do_fault;
11700 }
3dde962f
PM
11701
11702 if ((descriptor & 2) && (level < 3)) {
037c13c5 11703 /* Table entry. The top five bits are attributes which may
3dde962f
PM
11704 * propagate down through lower levels of the table (and
11705 * which are all arranged so that 0 means "no effect", so
11706 * we can gather them up by ORing in the bits at each level).
11707 */
11708 tableattrs |= extract64(descriptor, 59, 5);
11709 level++;
dddb5223 11710 indexmask = indexmask_grainsize;
3dde962f
PM
11711 continue;
11712 }
39a1fd25
PM
11713 /*
11714 * Block entry at level 1 or 2, or page entry at level 3.
3dde962f 11715 * These are basically the same thing, although the number
39a1fd25
PM
11716 * of bits we pull in from the vaddr varies. Note that although
11717 * descaddrmask masks enough of the low bits of the descriptor
11718 * to give a correct page or table address, the address field
11719 * in a block descriptor is smaller; so we need to explicitly
11720 * clear the lower bits here before ORing in the low vaddr bits.
3dde962f 11721 */
973a5434 11722 page_size = (1ULL << ((stride * (4 - level)) + 3));
39a1fd25 11723 descaddr &= ~(page_size - 1);
3dde962f 11724 descaddr |= (address & (page_size - 1));
6ab1a5ee 11725 /* Extract attributes from the descriptor */
d615efac
IC
11726 attrs = extract64(descriptor, 2, 10)
11727 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee 11728
b1a10c86 11729 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
6ab1a5ee
EI
11730 /* Stage 2 table descriptors do not include any attribute fields */
11731 break;
11732 }
11733 /* Merge in attributes from table descriptors */
037c13c5 11734 attrs |= nstable << 3; /* NS */
1bafc2ba 11735 guarded = extract64(descriptor, 50, 1); /* GP */
ba97be9f 11736 if (param.hpd) {
037c13c5
RH
11737 /* HPD disables all the table attributes except NSTable. */
11738 break;
11739 }
11740 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
3dde962f
PM
11741 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
11742 * means "force PL1 access only", which means forcing AP[1] to 0.
11743 */
037c13c5
RH
11744 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
11745 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
3dde962f
PM
11746 break;
11747 }
11748 /* Here descaddr is the final physical address, and attributes
11749 * are all in attrs.
11750 */
da909b2c 11751 fault_type = ARMFault_AccessFlag;
3dde962f
PM
11752 if ((attrs & (1 << 8)) == 0) {
11753 /* Access flag */
11754 goto do_fault;
11755 }
d8e052b3
AJ
11756
11757 ap = extract32(attrs, 4, 2);
d8e052b3 11758
b1a10c86
RDC
11759 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11760 ns = mmu_idx == ARMMMUIdx_Stage2;
ce3125be
PM
11761 xn = extract32(attrs, 11, 2);
11762 *prot = get_S2prot(env, ap, xn, s1_is_el0);
6ab1a5ee
EI
11763 } else {
11764 ns = extract32(attrs, 3, 1);
ce3125be 11765 xn = extract32(attrs, 12, 1);
6ab1a5ee 11766 pxn = extract32(attrs, 11, 1);
6e99f762 11767 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 11768 }
d8e052b3 11769
da909b2c 11770 fault_type = ARMFault_Permission;
d8e052b3 11771 if (!(*prot & (1 << access_type))) {
3dde962f
PM
11772 goto do_fault;
11773 }
3dde962f 11774
8bf5b6a9
PM
11775 if (ns) {
11776 /* The NS bit will (as required by the architecture) have no effect if
11777 * the CPU doesn't support TZ or this is a non-secure translation
11778 * regime, because the attribute will already be non-secure.
11779 */
11780 txattrs->secure = false;
11781 }
1bafc2ba
RH
11782 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
11783 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
149d3b31 11784 arm_tlb_bti_gp(txattrs) = true;
1bafc2ba 11785 }
5b2d261d 11786
b1a10c86 11787 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
7e98e21c
RH
11788 cacheattrs->attrs = convert_stage2_attrs(env, extract32(attrs, 0, 4));
11789 } else {
11790 /* Index into MAIR registers for cache attributes */
11791 uint8_t attrindx = extract32(attrs, 0, 3);
11792 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
11793 assert(attrindx <= 7);
11794 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
5b2d261d 11795 }
ef56c242
RH
11796
11797 /*
11798 * For FEAT_LPA2 and effective DS, the SH field in the attributes
11799 * was re-purposed for output address bits. The SH attribute in
11800 * that case comes from TCR_ELx, which we extracted earlier.
11801 */
11802 if (param.ds) {
11803 cacheattrs->shareability = param.sh;
11804 } else {
11805 cacheattrs->shareability = extract32(attrs, 6, 2);
11806 }
5b2d261d 11807
3dde962f
PM
11808 *phys_ptr = descaddr;
11809 *page_size_ptr = page_size;
b7cc4e82 11810 return false;
3dde962f
PM
11811
11812do_fault:
da909b2c
PM
11813 fi->type = fault_type;
11814 fi->level = level;
37785977 11815 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
b1a10c86
RDC
11816 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_Stage2 ||
11817 mmu_idx == ARMMMUIdx_Stage2_S);
9861248f 11818 fi->s1ns = mmu_idx == ARMMMUIdx_Stage2;
b7cc4e82 11819 return true;
3dde962f
PM
11820}
11821
f6bda88f
PC
11822static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
11823 ARMMMUIdx mmu_idx,
11824 int32_t address, int *prot)
11825{
3a00d560
MD
11826 if (!arm_feature(env, ARM_FEATURE_M)) {
11827 *prot = PAGE_READ | PAGE_WRITE;
11828 switch (address) {
11829 case 0xF0000000 ... 0xFFFFFFFF:
11830 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
11831 /* hivecs execing is ok */
11832 *prot |= PAGE_EXEC;
11833 }
11834 break;
11835 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 11836 *prot |= PAGE_EXEC;
3a00d560
MD
11837 break;
11838 }
11839 } else {
11840 /* Default system address map for M profile cores.
11841 * The architecture specifies which regions are execute-never;
11842 * at the MPU level no other checks are defined.
11843 */
11844 switch (address) {
11845 case 0x00000000 ... 0x1fffffff: /* ROM */
11846 case 0x20000000 ... 0x3fffffff: /* SRAM */
11847 case 0x60000000 ... 0x7fffffff: /* RAM */
11848 case 0x80000000 ... 0x9fffffff: /* RAM */
11849 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11850 break;
11851 case 0x40000000 ... 0x5fffffff: /* Peripheral */
11852 case 0xa0000000 ... 0xbfffffff: /* Device */
11853 case 0xc0000000 ... 0xdfffffff: /* Device */
11854 case 0xe0000000 ... 0xffffffff: /* System */
11855 *prot = PAGE_READ | PAGE_WRITE;
11856 break;
11857 default:
11858 g_assert_not_reached();
f6bda88f 11859 }
f6bda88f 11860 }
f6bda88f
PC
11861}
11862
29c483a5
MD
11863static bool pmsav7_use_background_region(ARMCPU *cpu,
11864 ARMMMUIdx mmu_idx, bool is_user)
11865{
11866 /* Return true if we should use the default memory map as a
11867 * "background" region if there are no hits against any MPU regions.
11868 */
11869 CPUARMState *env = &cpu->env;
11870
11871 if (is_user) {
11872 return false;
11873 }
11874
11875 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
11876 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
11877 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
11878 } else {
11879 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
11880 }
11881}
11882
38aaa60c
PM
11883static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
11884{
11885 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
11886 return arm_feature(env, ARM_FEATURE_M) &&
11887 extract32(address, 20, 12) == 0xe00;
11888}
11889
bf446a11
PM
11890static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
11891{
11892 /* True if address is in the M profile system region
11893 * 0xe0000000 - 0xffffffff
11894 */
11895 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
11896}
11897
f6bda88f 11898static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 11899 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9375ad15 11900 hwaddr *phys_ptr, int *prot,
e5e40999 11901 target_ulong *page_size,
9375ad15 11902 ARMMMUFaultInfo *fi)
f6bda88f 11903{
2fc0cc0e 11904 ARMCPU *cpu = env_archcpu(env);
f6bda88f
PC
11905 int n;
11906 bool is_user = regime_is_user(env, mmu_idx);
11907
11908 *phys_ptr = address;
e5e40999 11909 *page_size = TARGET_PAGE_SIZE;
f6bda88f
PC
11910 *prot = 0;
11911
38aaa60c
PM
11912 if (regime_translation_disabled(env, mmu_idx) ||
11913 m_is_ppb_region(env, address)) {
11914 /* MPU disabled or M profile PPB access: use default memory map.
11915 * The other case which uses the default memory map in the
11916 * v7M ARM ARM pseudocode is exception vector reads from the vector
11917 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
11918 * which always does a direct read using address_space_ldl(), rather
11919 * than going via this function, so we don't need to check that here.
11920 */
f6bda88f
PC
11921 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11922 } else { /* MPU enabled */
11923 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11924 /* region search */
11925 uint32_t base = env->pmsav7.drbar[n];
11926 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
11927 uint32_t rmask;
11928 bool srdis = false;
11929
11930 if (!(env->pmsav7.drsr[n] & 0x1)) {
11931 continue;
11932 }
11933
11934 if (!rsize) {
c9f9f124
MD
11935 qemu_log_mask(LOG_GUEST_ERROR,
11936 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
11937 continue;
11938 }
11939 rsize++;
11940 rmask = (1ull << rsize) - 1;
11941
11942 if (base & rmask) {
c9f9f124
MD
11943 qemu_log_mask(LOG_GUEST_ERROR,
11944 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
11945 "to DRSR region size, mask = 0x%" PRIx32 "\n",
11946 n, base, rmask);
f6bda88f
PC
11947 continue;
11948 }
11949
11950 if (address < base || address > base + rmask) {
9d2b5a58
PM
11951 /*
11952 * Address not in this region. We must check whether the
11953 * region covers addresses in the same page as our address.
11954 * In that case we must not report a size that covers the
11955 * whole page for a subsequent hit against a different MPU
11956 * region or the background region, because it would result in
11957 * incorrect TLB hits for subsequent accesses to addresses that
11958 * are in this MPU region.
11959 */
11960 if (ranges_overlap(base, rmask,
11961 address & TARGET_PAGE_MASK,
11962 TARGET_PAGE_SIZE)) {
11963 *page_size = 1;
11964 }
f6bda88f
PC
11965 continue;
11966 }
11967
11968 /* Region matched */
11969
11970 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
11971 int i, snd;
11972 uint32_t srdis_mask;
11973
11974 rsize -= 3; /* sub region size (power of 2) */
11975 snd = ((address - base) >> rsize) & 0x7;
11976 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
11977
11978 srdis_mask = srdis ? 0x3 : 0x0;
11979 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
11980 /* This will check in groups of 2, 4 and then 8, whether
11981 * the subregion bits are consistent. rsize is incremented
11982 * back up to give the region size, considering consistent
11983 * adjacent subregions as one region. Stop testing if rsize
11984 * is already big enough for an entire QEMU page.
11985 */
11986 int snd_rounded = snd & ~(i - 1);
11987 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
11988 snd_rounded + 8, i);
11989 if (srdis_mask ^ srdis_multi) {
11990 break;
11991 }
11992 srdis_mask = (srdis_mask << i) | srdis_mask;
11993 rsize++;
11994 }
11995 }
f6bda88f
PC
11996 if (srdis) {
11997 continue;
11998 }
e5e40999
PM
11999 if (rsize < TARGET_PAGE_BITS) {
12000 *page_size = 1 << rsize;
12001 }
f6bda88f
PC
12002 break;
12003 }
12004
12005 if (n == -1) { /* no hits */
29c483a5 12006 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f 12007 /* background fault */
9375ad15 12008 fi->type = ARMFault_Background;
f6bda88f
PC
12009 return true;
12010 }
12011 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
12012 } else { /* a MPU hit! */
12013 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
12014 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
12015
12016 if (m_is_system_region(env, address)) {
12017 /* System space is always execute never */
12018 xn = 1;
12019 }
f6bda88f
PC
12020
12021 if (is_user) { /* User mode AP bit decoding */
12022 switch (ap) {
12023 case 0:
12024 case 1:
12025 case 5:
12026 break; /* no access */
12027 case 3:
12028 *prot |= PAGE_WRITE;
12029 /* fall through */
12030 case 2:
12031 case 6:
12032 *prot |= PAGE_READ | PAGE_EXEC;
12033 break;
8638f1ad
PM
12034 case 7:
12035 /* for v7M, same as 6; for R profile a reserved value */
12036 if (arm_feature(env, ARM_FEATURE_M)) {
12037 *prot |= PAGE_READ | PAGE_EXEC;
12038 break;
12039 }
12040 /* fall through */
f6bda88f
PC
12041 default:
12042 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
12043 "DRACR[%d]: Bad value for AP bits: 0x%"
12044 PRIx32 "\n", n, ap);
f6bda88f
PC
12045 }
12046 } else { /* Priv. mode AP bits decoding */
12047 switch (ap) {
12048 case 0:
12049 break; /* no access */
12050 case 1:
12051 case 2:
12052 case 3:
12053 *prot |= PAGE_WRITE;
12054 /* fall through */
12055 case 5:
12056 case 6:
12057 *prot |= PAGE_READ | PAGE_EXEC;
12058 break;
8638f1ad
PM
12059 case 7:
12060 /* for v7M, same as 6; for R profile a reserved value */
12061 if (arm_feature(env, ARM_FEATURE_M)) {
12062 *prot |= PAGE_READ | PAGE_EXEC;
12063 break;
12064 }
12065 /* fall through */
f6bda88f
PC
12066 default:
12067 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
12068 "DRACR[%d]: Bad value for AP bits: 0x%"
12069 PRIx32 "\n", n, ap);
f6bda88f
PC
12070 }
12071 }
12072
12073 /* execute never */
bf446a11 12074 if (xn) {
f6bda88f
PC
12075 *prot &= ~PAGE_EXEC;
12076 }
12077 }
12078 }
12079
9375ad15
PM
12080 fi->type = ARMFault_Permission;
12081 fi->level = 1;
f6bda88f
PC
12082 return !(*prot & (1 << access_type));
12083}
12084
35337cc3
PM
12085static bool v8m_is_sau_exempt(CPUARMState *env,
12086 uint32_t address, MMUAccessType access_type)
12087{
12088 /* The architecture specifies that certain address ranges are
12089 * exempt from v8M SAU/IDAU checks.
12090 */
12091 return
12092 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
12093 (address >= 0xe0000000 && address <= 0xe0002fff) ||
12094 (address >= 0xe000e000 && address <= 0xe000efff) ||
12095 (address >= 0xe002e000 && address <= 0xe002efff) ||
12096 (address >= 0xe0040000 && address <= 0xe0041fff) ||
12097 (address >= 0xe00ff000 && address <= 0xe00fffff);
12098}
12099
787a7e76 12100void v8m_security_lookup(CPUARMState *env, uint32_t address,
35337cc3
PM
12101 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12102 V8M_SAttributes *sattrs)
12103{
12104 /* Look up the security attributes for this address. Compare the
12105 * pseudocode SecurityCheck() function.
12106 * We assume the caller has zero-initialized *sattrs.
12107 */
2fc0cc0e 12108 ARMCPU *cpu = env_archcpu(env);
35337cc3 12109 int r;
181962fd
PM
12110 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
12111 int idau_region = IREGION_NOTVALID;
72042435
PM
12112 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
12113 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
35337cc3 12114
181962fd
PM
12115 if (cpu->idau) {
12116 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
12117 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
12118
12119 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
12120 &idau_nsc);
12121 }
35337cc3
PM
12122
12123 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
12124 /* 0xf0000000..0xffffffff is always S for insn fetches */
12125 return;
12126 }
12127
181962fd 12128 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
35337cc3
PM
12129 sattrs->ns = !regime_is_secure(env, mmu_idx);
12130 return;
12131 }
12132
181962fd
PM
12133 if (idau_region != IREGION_NOTVALID) {
12134 sattrs->irvalid = true;
12135 sattrs->iregion = idau_region;
12136 }
12137
35337cc3
PM
12138 switch (env->sau.ctrl & 3) {
12139 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
12140 break;
12141 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
12142 sattrs->ns = true;
12143 break;
12144 default: /* SAU.ENABLE == 1 */
12145 for (r = 0; r < cpu->sau_sregion; r++) {
12146 if (env->sau.rlar[r] & 1) {
12147 uint32_t base = env->sau.rbar[r] & ~0x1f;
12148 uint32_t limit = env->sau.rlar[r] | 0x1f;
12149
12150 if (base <= address && limit >= address) {
72042435
PM
12151 if (base > addr_page_base || limit < addr_page_limit) {
12152 sattrs->subpage = true;
12153 }
35337cc3
PM
12154 if (sattrs->srvalid) {
12155 /* If we hit in more than one region then we must report
12156 * as Secure, not NS-Callable, with no valid region
12157 * number info.
12158 */
12159 sattrs->ns = false;
12160 sattrs->nsc = false;
12161 sattrs->sregion = 0;
12162 sattrs->srvalid = false;
12163 break;
12164 } else {
12165 if (env->sau.rlar[r] & 2) {
12166 sattrs->nsc = true;
12167 } else {
12168 sattrs->ns = true;
12169 }
12170 sattrs->srvalid = true;
12171 sattrs->sregion = r;
12172 }
9d2b5a58
PM
12173 } else {
12174 /*
12175 * Address not in this region. We must check whether the
12176 * region covers addresses in the same page as our address.
12177 * In that case we must not report a size that covers the
12178 * whole page for a subsequent hit against a different MPU
12179 * region or the background region, because it would result
12180 * in incorrect TLB hits for subsequent accesses to
12181 * addresses that are in this MPU region.
12182 */
12183 if (limit >= base &&
12184 ranges_overlap(base, limit - base + 1,
12185 addr_page_base,
12186 TARGET_PAGE_SIZE)) {
12187 sattrs->subpage = true;
12188 }
35337cc3
PM
12189 }
12190 }
12191 }
7e3f1223
TR
12192 break;
12193 }
35337cc3 12194
7e3f1223
TR
12195 /*
12196 * The IDAU will override the SAU lookup results if it specifies
12197 * higher security than the SAU does.
12198 */
12199 if (!idau_ns) {
12200 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
12201 sattrs->ns = false;
12202 sattrs->nsc = idau_nsc;
181962fd 12203 }
35337cc3
PM
12204 }
12205}
12206
787a7e76 12207bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
54317c0f
PM
12208 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12209 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
12210 int *prot, bool *is_subpage,
12211 ARMMMUFaultInfo *fi, uint32_t *mregion)
54317c0f
PM
12212{
12213 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
12214 * that a full phys-to-virt translation does).
12215 * mregion is (if not NULL) set to the region number which matched,
12216 * or -1 if no region number is returned (MPU off, address did not
12217 * hit a region, address hit in multiple regions).
72042435
PM
12218 * We set is_subpage to true if the region hit doesn't cover the
12219 * entire TARGET_PAGE the address is within.
54317c0f 12220 */
2fc0cc0e 12221 ARMCPU *cpu = env_archcpu(env);
504e3cc3 12222 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 12223 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
12224 int n;
12225 int matchregion = -1;
12226 bool hit = false;
72042435
PM
12227 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
12228 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
504e3cc3 12229
72042435 12230 *is_subpage = false;
504e3cc3
PM
12231 *phys_ptr = address;
12232 *prot = 0;
54317c0f
PM
12233 if (mregion) {
12234 *mregion = -1;
35337cc3
PM
12235 }
12236
504e3cc3
PM
12237 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
12238 * was an exception vector read from the vector table (which is always
12239 * done using the default system address map), because those accesses
12240 * are done in arm_v7m_load_vector(), which always does a direct
12241 * read using address_space_ldl(), rather than going via this function.
12242 */
12243 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
12244 hit = true;
12245 } else if (m_is_ppb_region(env, address)) {
12246 hit = true;
504e3cc3 12247 } else {
cff21316
PM
12248 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
12249 hit = true;
12250 }
12251
504e3cc3
PM
12252 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
12253 /* region search */
12254 /* Note that the base address is bits [31:5] from the register
12255 * with bits [4:0] all zeroes, but the limit address is bits
12256 * [31:5] from the register with bits [4:0] all ones.
12257 */
62c58ee0
PM
12258 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
12259 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 12260
62c58ee0 12261 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
12262 /* Region disabled */
12263 continue;
12264 }
12265
12266 if (address < base || address > limit) {
9d2b5a58
PM
12267 /*
12268 * Address not in this region. We must check whether the
12269 * region covers addresses in the same page as our address.
12270 * In that case we must not report a size that covers the
12271 * whole page for a subsequent hit against a different MPU
12272 * region or the background region, because it would result in
12273 * incorrect TLB hits for subsequent accesses to addresses that
12274 * are in this MPU region.
12275 */
12276 if (limit >= base &&
12277 ranges_overlap(base, limit - base + 1,
12278 addr_page_base,
12279 TARGET_PAGE_SIZE)) {
12280 *is_subpage = true;
12281 }
504e3cc3
PM
12282 continue;
12283 }
12284
72042435
PM
12285 if (base > addr_page_base || limit < addr_page_limit) {
12286 *is_subpage = true;
12287 }
12288
cff21316 12289 if (matchregion != -1) {
504e3cc3
PM
12290 /* Multiple regions match -- always a failure (unlike
12291 * PMSAv7 where highest-numbered-region wins)
12292 */
3f551b5b
PM
12293 fi->type = ARMFault_Permission;
12294 fi->level = 1;
504e3cc3
PM
12295 return true;
12296 }
12297
12298 matchregion = n;
12299 hit = true;
504e3cc3
PM
12300 }
12301 }
12302
12303 if (!hit) {
12304 /* background fault */
3f551b5b 12305 fi->type = ARMFault_Background;
504e3cc3
PM
12306 return true;
12307 }
12308
12309 if (matchregion == -1) {
12310 /* hit using the background region */
12311 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
12312 } else {
62c58ee0
PM
12313 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
12314 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
cad8e2e3
PM
12315 bool pxn = false;
12316
12317 if (arm_feature(env, ARM_FEATURE_V8_1M)) {
12318 pxn = extract32(env->pmsav8.rlar[secure][matchregion], 4, 1);
12319 }
504e3cc3
PM
12320
12321 if (m_is_system_region(env, address)) {
12322 /* System space is always execute never */
12323 xn = 1;
12324 }
12325
12326 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
cad8e2e3 12327 if (*prot && !xn && !(pxn && !is_user)) {
504e3cc3
PM
12328 *prot |= PAGE_EXEC;
12329 }
12330 /* We don't need to look the attribute up in the MAIR0/MAIR1
12331 * registers because that only tells us about cacheability.
12332 */
54317c0f
PM
12333 if (mregion) {
12334 *mregion = matchregion;
12335 }
504e3cc3
PM
12336 }
12337
3f551b5b
PM
12338 fi->type = ARMFault_Permission;
12339 fi->level = 1;
504e3cc3
PM
12340 return !(*prot & (1 << access_type));
12341}
12342
54317c0f
PM
12343
12344static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
12345 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12346 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
12347 int *prot, target_ulong *page_size,
12348 ARMMMUFaultInfo *fi)
54317c0f
PM
12349{
12350 uint32_t secure = regime_is_secure(env, mmu_idx);
12351 V8M_SAttributes sattrs = {};
72042435
PM
12352 bool ret;
12353 bool mpu_is_subpage;
54317c0f
PM
12354
12355 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
12356 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
12357 if (access_type == MMU_INST_FETCH) {
12358 /* Instruction fetches always use the MMU bank and the
12359 * transaction attribute determined by the fetch address,
12360 * regardless of CPU state. This is painful for QEMU
12361 * to handle, because it would mean we need to encode
12362 * into the mmu_idx not just the (user, negpri) information
12363 * for the current security state but also that for the
12364 * other security state, which would balloon the number
12365 * of mmu_idx values needed alarmingly.
12366 * Fortunately we can avoid this because it's not actually
12367 * possible to arbitrarily execute code from memory with
12368 * the wrong security attribute: it will always generate
12369 * an exception of some kind or another, apart from the
12370 * special case of an NS CPU executing an SG instruction
12371 * in S&NSC memory. So we always just fail the translation
12372 * here and sort things out in the exception handler
12373 * (including possibly emulating an SG instruction).
12374 */
12375 if (sattrs.ns != !secure) {
3f551b5b
PM
12376 if (sattrs.nsc) {
12377 fi->type = ARMFault_QEMU_NSCExec;
12378 } else {
12379 fi->type = ARMFault_QEMU_SFault;
12380 }
72042435 12381 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
12382 *phys_ptr = address;
12383 *prot = 0;
12384 return true;
12385 }
12386 } else {
12387 /* For data accesses we always use the MMU bank indicated
12388 * by the current CPU state, but the security attributes
12389 * might downgrade a secure access to nonsecure.
12390 */
12391 if (sattrs.ns) {
12392 txattrs->secure = false;
12393 } else if (!secure) {
12394 /* NS access to S memory must fault.
12395 * Architecturally we should first check whether the
12396 * MPU information for this address indicates that we
12397 * are doing an unaligned access to Device memory, which
12398 * should generate a UsageFault instead. QEMU does not
12399 * currently check for that kind of unaligned access though.
12400 * If we added it we would need to do so as a special case
12401 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
12402 */
3f551b5b 12403 fi->type = ARMFault_QEMU_SFault;
72042435 12404 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
12405 *phys_ptr = address;
12406 *prot = 0;
12407 return true;
12408 }
12409 }
12410 }
12411
72042435
PM
12412 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
12413 txattrs, prot, &mpu_is_subpage, fi, NULL);
72042435
PM
12414 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
12415 return ret;
54317c0f
PM
12416}
12417
13689d43 12418static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 12419 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53a4e5c5
PM
12420 hwaddr *phys_ptr, int *prot,
12421 ARMMMUFaultInfo *fi)
9ee6e8bb
PB
12422{
12423 int n;
12424 uint32_t mask;
12425 uint32_t base;
0480f69a 12426 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 12427
3279adb9
PM
12428 if (regime_translation_disabled(env, mmu_idx)) {
12429 /* MPU disabled. */
12430 *phys_ptr = address;
12431 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
12432 return false;
12433 }
12434
9ee6e8bb
PB
12435 *phys_ptr = address;
12436 for (n = 7; n >= 0; n--) {
554b0b09 12437 base = env->cp15.c6_region[n];
87c3d486 12438 if ((base & 1) == 0) {
554b0b09 12439 continue;
87c3d486 12440 }
554b0b09
PM
12441 mask = 1 << ((base >> 1) & 0x1f);
12442 /* Keep this shift separate from the above to avoid an
12443 (undefined) << 32. */
12444 mask = (mask << 1) - 1;
87c3d486 12445 if (((base ^ address) & ~mask) == 0) {
554b0b09 12446 break;
87c3d486 12447 }
9ee6e8bb 12448 }
87c3d486 12449 if (n < 0) {
53a4e5c5 12450 fi->type = ARMFault_Background;
b7cc4e82 12451 return true;
87c3d486 12452 }
9ee6e8bb 12453
03ae85f8 12454 if (access_type == MMU_INST_FETCH) {
7e09797c 12455 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 12456 } else {
7e09797c 12457 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
12458 }
12459 mask = (mask >> (n * 4)) & 0xf;
12460 switch (mask) {
12461 case 0:
53a4e5c5
PM
12462 fi->type = ARMFault_Permission;
12463 fi->level = 1;
b7cc4e82 12464 return true;
9ee6e8bb 12465 case 1:
87c3d486 12466 if (is_user) {
53a4e5c5
PM
12467 fi->type = ARMFault_Permission;
12468 fi->level = 1;
b7cc4e82 12469 return true;
87c3d486 12470 }
554b0b09
PM
12471 *prot = PAGE_READ | PAGE_WRITE;
12472 break;
9ee6e8bb 12473 case 2:
554b0b09 12474 *prot = PAGE_READ;
87c3d486 12475 if (!is_user) {
554b0b09 12476 *prot |= PAGE_WRITE;
87c3d486 12477 }
554b0b09 12478 break;
9ee6e8bb 12479 case 3:
554b0b09
PM
12480 *prot = PAGE_READ | PAGE_WRITE;
12481 break;
9ee6e8bb 12482 case 5:
87c3d486 12483 if (is_user) {
53a4e5c5
PM
12484 fi->type = ARMFault_Permission;
12485 fi->level = 1;
b7cc4e82 12486 return true;
87c3d486 12487 }
554b0b09
PM
12488 *prot = PAGE_READ;
12489 break;
9ee6e8bb 12490 case 6:
554b0b09
PM
12491 *prot = PAGE_READ;
12492 break;
9ee6e8bb 12493 default:
554b0b09 12494 /* Bad permission. */
53a4e5c5
PM
12495 fi->type = ARMFault_Permission;
12496 fi->level = 1;
b7cc4e82 12497 return true;
9ee6e8bb 12498 }
3ad493fc 12499 *prot |= PAGE_EXEC;
b7cc4e82 12500 return false;
9ee6e8bb
PB
12501}
12502
5b2d261d
AB
12503/* Combine either inner or outer cacheability attributes for normal
12504 * memory, according to table D4-42 and pseudocode procedure
12505 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
12506 *
12507 * NB: only stage 1 includes allocation hints (RW bits), leading to
12508 * some asymmetry.
12509 */
12510static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
12511{
12512 if (s1 == 4 || s2 == 4) {
12513 /* non-cacheable has precedence */
12514 return 4;
12515 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
12516 /* stage 1 write-through takes precedence */
12517 return s1;
12518 } else if (extract32(s2, 2, 2) == 2) {
12519 /* stage 2 write-through takes precedence, but the allocation hint
12520 * is still taken from stage 1
12521 */
12522 return (2 << 2) | extract32(s1, 0, 2);
12523 } else { /* write-back */
12524 return s1;
12525 }
12526}
12527
12528/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
12529 * and CombineS1S2Desc()
12530 *
12531 * @s1: Attributes from stage 1 walk
12532 * @s2: Attributes from stage 2 walk
12533 */
12534static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
12535{
337a03f0 12536 uint8_t s1lo, s2lo, s1hi, s2hi;
5b2d261d 12537 ARMCacheAttrs ret;
337a03f0
RH
12538 bool tagged = false;
12539
12540 if (s1.attrs == 0xf0) {
12541 tagged = true;
12542 s1.attrs = 0xff;
12543 }
12544
12545 s1lo = extract32(s1.attrs, 0, 4);
12546 s2lo = extract32(s2.attrs, 0, 4);
12547 s1hi = extract32(s1.attrs, 4, 4);
12548 s2hi = extract32(s2.attrs, 4, 4);
5b2d261d
AB
12549
12550 /* Combine shareability attributes (table D4-43) */
12551 if (s1.shareability == 2 || s2.shareability == 2) {
12552 /* if either are outer-shareable, the result is outer-shareable */
12553 ret.shareability = 2;
12554 } else if (s1.shareability == 3 || s2.shareability == 3) {
12555 /* if either are inner-shareable, the result is inner-shareable */
12556 ret.shareability = 3;
12557 } else {
12558 /* both non-shareable */
12559 ret.shareability = 0;
12560 }
12561
12562 /* Combine memory type and cacheability attributes */
12563 if (s1hi == 0 || s2hi == 0) {
12564 /* Device has precedence over normal */
12565 if (s1lo == 0 || s2lo == 0) {
12566 /* nGnRnE has precedence over anything */
12567 ret.attrs = 0;
12568 } else if (s1lo == 4 || s2lo == 4) {
12569 /* non-Reordering has precedence over Reordering */
12570 ret.attrs = 4; /* nGnRE */
12571 } else if (s1lo == 8 || s2lo == 8) {
12572 /* non-Gathering has precedence over Gathering */
12573 ret.attrs = 8; /* nGRE */
12574 } else {
12575 ret.attrs = 0xc; /* GRE */
12576 }
12577
12578 /* Any location for which the resultant memory type is any
12579 * type of Device memory is always treated as Outer Shareable.
12580 */
12581 ret.shareability = 2;
12582 } else { /* Normal memory */
12583 /* Outer/inner cacheability combine independently */
12584 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
12585 | combine_cacheattr_nibble(s1lo, s2lo);
12586
12587 if (ret.attrs == 0x44) {
12588 /* Any location for which the resultant memory type is Normal
12589 * Inner Non-cacheable, Outer Non-cacheable is always treated
12590 * as Outer Shareable.
12591 */
12592 ret.shareability = 2;
12593 }
12594 }
12595
337a03f0
RH
12596 /* TODO: CombineS1S2Desc does not consider transient, only WB, RWA. */
12597 if (tagged && ret.attrs == 0xff) {
12598 ret.attrs = 0xf0;
12599 }
12600
5b2d261d
AB
12601 return ret;
12602}
12603
12604
702a9357
PM
12605/* get_phys_addr - get the physical address for this virtual address
12606 *
12607 * Find the physical address corresponding to the given virtual address,
12608 * by doing a translation table walk on MMU based systems or using the
12609 * MPU state on MPU based systems.
12610 *
b7cc4e82
PC
12611 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
12612 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
12613 * information on why the translation aborted, in the format of a
12614 * DFSR/IFSR fault register, with the following caveats:
12615 * * we honour the short vs long DFSR format differences.
12616 * * the WnR bit is never set (the caller must do this).
f6bda88f 12617 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
12618 * value.
12619 *
12620 * @env: CPUARMState
12621 * @address: virtual address to get physical address for
12622 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 12623 * @mmu_idx: MMU index indicating required translation regime
702a9357 12624 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 12625 * @attrs: set to the memory transaction attributes to use
702a9357
PM
12626 * @prot: set to the permissions for the page containing phys_ptr
12627 * @page_size: set to the size of the page containing phys_ptr
5b2d261d
AB
12628 * @fi: set to fault info if the translation fails
12629 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
702a9357 12630 */
ebae861f
PMD
12631bool get_phys_addr(CPUARMState *env, target_ulong address,
12632 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12633 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
12634 target_ulong *page_size,
12635 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9ee6e8bb 12636{
7879460a
RDC
12637 ARMMMUIdx s1_mmu_idx = stage_1_mmu_idx(mmu_idx);
12638
12639 if (mmu_idx != s1_mmu_idx) {
9b539263 12640 /* Call ourselves recursively to do the stage 1 and then stage 2
7879460a 12641 * translations if mmu_idx is a two-stage regime.
0480f69a 12642 */
9b539263
EI
12643 if (arm_feature(env, ARM_FEATURE_EL2)) {
12644 hwaddr ipa;
12645 int s2_prot;
12646 int ret;
6c05a866 12647 bool ipa_secure;
5b2d261d 12648 ARMCacheAttrs cacheattrs2 = {};
b1a10c86
RDC
12649 ARMMMUIdx s2_mmu_idx;
12650 bool is_el0;
9b539263 12651
7879460a
RDC
12652 ret = get_phys_addr(env, address, access_type, s1_mmu_idx, &ipa,
12653 attrs, prot, page_size, fi, cacheattrs);
9b539263
EI
12654
12655 /* If S1 fails or S2 is disabled, return early. */
97fa9350 12656 if (ret || regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
9b539263
EI
12657 *phys_ptr = ipa;
12658 return ret;
12659 }
12660
6c05a866 12661 ipa_secure = attrs->secure;
bcd7a8cf 12662 if (arm_is_secure_below_el3(env)) {
6c05a866 12663 if (ipa_secure) {
bcd7a8cf
IH
12664 attrs->secure = !(env->cp15.vstcr_el2.raw_tcr & VSTCR_SW);
12665 } else {
12666 attrs->secure = !(env->cp15.vtcr_el2.raw_tcr & VTCR_NSW);
12667 }
12668 } else {
6c05a866 12669 assert(!ipa_secure);
bcd7a8cf
IH
12670 }
12671
b1a10c86
RDC
12672 s2_mmu_idx = attrs->secure ? ARMMMUIdx_Stage2_S : ARMMMUIdx_Stage2;
12673 is_el0 = mmu_idx == ARMMMUIdx_E10_0 || mmu_idx == ARMMMUIdx_SE10_0;
12674
9b539263 12675 /* S1 is done. Now do S2 translation. */
b1a10c86 12676 ret = get_phys_addr_lpae(env, ipa, access_type, s2_mmu_idx, is_el0,
9b539263 12677 phys_ptr, attrs, &s2_prot,
7e98e21c 12678 page_size, fi, &cacheattrs2);
9b539263
EI
12679 fi->s2addr = ipa;
12680 /* Combine the S1 and S2 perms. */
12681 *prot &= s2_prot;
5b2d261d 12682
7e98e21c
RH
12683 /* If S2 fails, return early. */
12684 if (ret) {
12685 return ret;
5b2d261d
AB
12686 }
12687
7e98e21c 12688 /* Combine the S1 and S2 cache attributes. */
e04a5752 12689 if (arm_hcr_el2_eff(env) & HCR_DC) {
7e98e21c
RH
12690 /*
12691 * HCR.DC forces the first stage attributes to
12692 * Normal Non-Shareable,
12693 * Inner Write-Back Read-Allocate Write-Allocate,
12694 * Outer Write-Back Read-Allocate Write-Allocate.
337a03f0 12695 * Do not overwrite Tagged within attrs.
7e98e21c 12696 */
337a03f0
RH
12697 if (cacheattrs->attrs != 0xf0) {
12698 cacheattrs->attrs = 0xff;
12699 }
7e98e21c
RH
12700 cacheattrs->shareability = 0;
12701 }
12702 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
b1a10c86
RDC
12703
12704 /* Check if IPA translates to secure or non-secure PA space. */
12705 if (arm_is_secure_below_el3(env)) {
6c05a866 12706 if (ipa_secure) {
b1a10c86
RDC
12707 attrs->secure =
12708 !(env->cp15.vstcr_el2.raw_tcr & (VSTCR_SA | VSTCR_SW));
12709 } else {
12710 attrs->secure =
12711 !((env->cp15.vtcr_el2.raw_tcr & (VTCR_NSA | VTCR_NSW))
d3b2d191 12712 || (env->cp15.vstcr_el2.raw_tcr & (VSTCR_SA | VSTCR_SW)));
b1a10c86
RDC
12713 }
12714 }
7e98e21c 12715 return 0;
9b539263
EI
12716 } else {
12717 /*
12718 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
12719 */
8bd5c820 12720 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 12721 }
0480f69a 12722 }
d3649702 12723
8bf5b6a9
PM
12724 /* The page table entries may downgrade secure to non-secure, but
12725 * cannot upgrade an non-secure translation regime's attributes
12726 * to secure.
12727 */
12728 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 12729 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 12730
0480f69a
PM
12731 /* Fast Context Switch Extension. This doesn't exist at all in v8.
12732 * In v7 and earlier it affects all stage 1 translations.
12733 */
97fa9350 12734 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2
0480f69a
PM
12735 && !arm_feature(env, ARM_FEATURE_V8)) {
12736 if (regime_el(env, mmu_idx) == 3) {
12737 address += env->cp15.fcseidr_s;
12738 } else {
12739 address += env->cp15.fcseidr_ns;
12740 }
54bf36ed 12741 }
9ee6e8bb 12742
3279adb9 12743 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 12744 bool ret;
f6bda88f 12745 *page_size = TARGET_PAGE_SIZE;
3279adb9 12746
504e3cc3
PM
12747 if (arm_feature(env, ARM_FEATURE_V8)) {
12748 /* PMSAv8 */
12749 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
72042435 12750 phys_ptr, attrs, prot, page_size, fi);
504e3cc3 12751 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
12752 /* PMSAv7 */
12753 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
e5e40999 12754 phys_ptr, prot, page_size, fi);
3279adb9
PM
12755 } else {
12756 /* Pre-v7 MPU */
12757 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
53a4e5c5 12758 phys_ptr, prot, fi);
3279adb9
PM
12759 }
12760 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 12761 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
12762 access_type == MMU_DATA_LOAD ? "reading" :
12763 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
12764 (uint32_t)address, mmu_idx,
12765 ret ? "Miss" : "Hit",
12766 *prot & PAGE_READ ? 'r' : '-',
12767 *prot & PAGE_WRITE ? 'w' : '-',
12768 *prot & PAGE_EXEC ? 'x' : '-');
12769
12770 return ret;
f6bda88f
PC
12771 }
12772
3279adb9
PM
12773 /* Definitely a real MMU, not an MPU */
12774
0480f69a 12775 if (regime_translation_disabled(env, mmu_idx)) {
337a03f0
RH
12776 uint64_t hcr;
12777 uint8_t memattr;
12778
cebfb648
RH
12779 /*
12780 * MMU disabled. S1 addresses within aa64 translation regimes are
12781 * still checked for bounds -- see AArch64.TranslateAddressS1Off.
12782 */
b1a10c86 12783 if (mmu_idx != ARMMMUIdx_Stage2 && mmu_idx != ARMMMUIdx_Stage2_S) {
cebfb648
RH
12784 int r_el = regime_el(env, mmu_idx);
12785 if (arm_el_is_aa64(env, r_el)) {
12786 int pamax = arm_pamax(env_archcpu(env));
12787 uint64_t tcr = env->cp15.tcr_el[r_el].raw_tcr;
12788 int addrtop, tbi;
12789
12790 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
12791 if (access_type == MMU_INST_FETCH) {
12792 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
12793 }
12794 tbi = (tbi >> extract64(address, 55, 1)) & 1;
12795 addrtop = (tbi ? 55 : 63);
12796
12797 if (extract64(address, pamax, addrtop - pamax + 1) != 0) {
12798 fi->type = ARMFault_AddressSize;
12799 fi->level = 0;
12800 fi->stage2 = false;
12801 return 1;
12802 }
12803
12804 /*
12805 * When TBI is disabled, we've just validated that all of the
12806 * bits above PAMax are zero, so logically we only need to
12807 * clear the top byte for TBI. But it's clearer to follow
12808 * the pseudocode set of addrdesc.paddress.
12809 */
12810 address = extract64(address, 0, 52);
12811 }
12812 }
9ee6e8bb 12813 *phys_ptr = address;
3ad493fc 12814 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 12815 *page_size = TARGET_PAGE_SIZE;
337a03f0
RH
12816
12817 /* Fill in cacheattr a-la AArch64.TranslateAddressS1Off. */
12818 hcr = arm_hcr_el2_eff(env);
12819 cacheattrs->shareability = 0;
12820 if (hcr & HCR_DC) {
12821 if (hcr & HCR_DCT) {
12822 memattr = 0xf0; /* Tagged, Normal, WB, RWA */
12823 } else {
12824 memattr = 0xff; /* Normal, WB, RWA */
12825 }
12826 } else if (access_type == MMU_INST_FETCH) {
12827 if (regime_sctlr(env, mmu_idx) & SCTLR_I) {
12828 memattr = 0xee; /* Normal, WT, RA, NT */
12829 } else {
12830 memattr = 0x44; /* Normal, NC, No */
12831 }
12832 cacheattrs->shareability = 2; /* outer sharable */
12833 } else {
12834 memattr = 0x00; /* Device, nGnRnE */
12835 }
12836 cacheattrs->attrs = memattr;
9ee6e8bb 12837 return 0;
0480f69a
PM
12838 }
12839
0480f69a 12840 if (regime_using_lpae_format(env, mmu_idx)) {
ff7de2fc 12841 return get_phys_addr_lpae(env, address, access_type, mmu_idx, false,
bc52bfeb
PM
12842 phys_ptr, attrs, prot, page_size,
12843 fi, cacheattrs);
0480f69a 12844 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
bc52bfeb
PM
12845 return get_phys_addr_v6(env, address, access_type, mmu_idx,
12846 phys_ptr, attrs, prot, page_size, fi);
9ee6e8bb 12847 } else {
bc52bfeb 12848 return get_phys_addr_v5(env, address, access_type, mmu_idx,
f989983e 12849 phys_ptr, prot, page_size, fi);
9ee6e8bb
PB
12850 }
12851}
12852
0faea0c7
PM
12853hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
12854 MemTxAttrs *attrs)
b5ff1b31 12855{
00b941e5 12856 ARMCPU *cpu = ARM_CPU(cs);
d3649702 12857 CPUARMState *env = &cpu->env;
a8170e5e 12858 hwaddr phys_addr;
d4c430a8 12859 target_ulong page_size;
b5ff1b31 12860 int prot;
b7cc4e82 12861 bool ret;
e14b5a23 12862 ARMMMUFaultInfo fi = {};
50494a27 12863 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
7e98e21c 12864 ARMCacheAttrs cacheattrs = {};
b5ff1b31 12865
0faea0c7
PM
12866 *attrs = (MemTxAttrs) {};
12867
a9dd161f 12868 ret = get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &phys_addr,
7e98e21c 12869 attrs, &prot, &page_size, &fi, &cacheattrs);
b5ff1b31 12870
b7cc4e82 12871 if (ret) {
b5ff1b31 12872 return -1;
00b941e5 12873 }
b5ff1b31
FB
12874 return phys_addr;
12875}
12876
b5ff1b31 12877#endif
6ddbc6e4
PB
12878
12879/* Note that signed overflow is undefined in C. The following routines are
12880 careful to use unsigned types where modulo arithmetic is required.
12881 Failure to do so _will_ break on newer gcc. */
12882
12883/* Signed saturating arithmetic. */
12884
1654b2d6 12885/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
12886static inline uint16_t add16_sat(uint16_t a, uint16_t b)
12887{
12888 uint16_t res;
12889
12890 res = a + b;
12891 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
12892 if (a & 0x8000)
12893 res = 0x8000;
12894 else
12895 res = 0x7fff;
12896 }
12897 return res;
12898}
12899
1654b2d6 12900/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
12901static inline uint8_t add8_sat(uint8_t a, uint8_t b)
12902{
12903 uint8_t res;
12904
12905 res = a + b;
12906 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
12907 if (a & 0x80)
12908 res = 0x80;
12909 else
12910 res = 0x7f;
12911 }
12912 return res;
12913}
12914
1654b2d6 12915/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
12916static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
12917{
12918 uint16_t res;
12919
12920 res = a - b;
12921 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
12922 if (a & 0x8000)
12923 res = 0x8000;
12924 else
12925 res = 0x7fff;
12926 }
12927 return res;
12928}
12929
1654b2d6 12930/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
12931static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
12932{
12933 uint8_t res;
12934
12935 res = a - b;
12936 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
12937 if (a & 0x80)
12938 res = 0x80;
12939 else
12940 res = 0x7f;
12941 }
12942 return res;
12943}
12944
12945#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
12946#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
12947#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
12948#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
12949#define PFX q
12950
12951#include "op_addsub.h"
12952
12953/* Unsigned saturating arithmetic. */
460a09c1 12954static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
12955{
12956 uint16_t res;
12957 res = a + b;
12958 if (res < a)
12959 res = 0xffff;
12960 return res;
12961}
12962
460a09c1 12963static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 12964{
4c4fd3f8 12965 if (a > b)
6ddbc6e4
PB
12966 return a - b;
12967 else
12968 return 0;
12969}
12970
12971static inline uint8_t add8_usat(uint8_t a, uint8_t b)
12972{
12973 uint8_t res;
12974 res = a + b;
12975 if (res < a)
12976 res = 0xff;
12977 return res;
12978}
12979
12980static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
12981{
4c4fd3f8 12982 if (a > b)
6ddbc6e4
PB
12983 return a - b;
12984 else
12985 return 0;
12986}
12987
12988#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
12989#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
12990#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
12991#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
12992#define PFX uq
12993
12994#include "op_addsub.h"
12995
12996/* Signed modulo arithmetic. */
12997#define SARITH16(a, b, n, op) do { \
12998 int32_t sum; \
db6e2e65 12999 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
13000 RESULT(sum, n, 16); \
13001 if (sum >= 0) \
13002 ge |= 3 << (n * 2); \
13003 } while(0)
13004
13005#define SARITH8(a, b, n, op) do { \
13006 int32_t sum; \
db6e2e65 13007 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
13008 RESULT(sum, n, 8); \
13009 if (sum >= 0) \
13010 ge |= 1 << n; \
13011 } while(0)
13012
13013
13014#define ADD16(a, b, n) SARITH16(a, b, n, +)
13015#define SUB16(a, b, n) SARITH16(a, b, n, -)
13016#define ADD8(a, b, n) SARITH8(a, b, n, +)
13017#define SUB8(a, b, n) SARITH8(a, b, n, -)
13018#define PFX s
13019#define ARITH_GE
13020
13021#include "op_addsub.h"
13022
13023/* Unsigned modulo arithmetic. */
13024#define ADD16(a, b, n) do { \
13025 uint32_t sum; \
13026 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
13027 RESULT(sum, n, 16); \
a87aa10b 13028 if ((sum >> 16) == 1) \
6ddbc6e4
PB
13029 ge |= 3 << (n * 2); \
13030 } while(0)
13031
13032#define ADD8(a, b, n) do { \
13033 uint32_t sum; \
13034 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
13035 RESULT(sum, n, 8); \
a87aa10b
AZ
13036 if ((sum >> 8) == 1) \
13037 ge |= 1 << n; \
6ddbc6e4
PB
13038 } while(0)
13039
13040#define SUB16(a, b, n) do { \
13041 uint32_t sum; \
13042 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
13043 RESULT(sum, n, 16); \
13044 if ((sum >> 16) == 0) \
13045 ge |= 3 << (n * 2); \
13046 } while(0)
13047
13048#define SUB8(a, b, n) do { \
13049 uint32_t sum; \
13050 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
13051 RESULT(sum, n, 8); \
13052 if ((sum >> 8) == 0) \
a87aa10b 13053 ge |= 1 << n; \
6ddbc6e4
PB
13054 } while(0)
13055
13056#define PFX u
13057#define ARITH_GE
13058
13059#include "op_addsub.h"
13060
13061/* Halved signed arithmetic. */
13062#define ADD16(a, b, n) \
13063 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
13064#define SUB16(a, b, n) \
13065 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
13066#define ADD8(a, b, n) \
13067 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
13068#define SUB8(a, b, n) \
13069 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
13070#define PFX sh
13071
13072#include "op_addsub.h"
13073
13074/* Halved unsigned arithmetic. */
13075#define ADD16(a, b, n) \
13076 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
13077#define SUB16(a, b, n) \
13078 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
13079#define ADD8(a, b, n) \
13080 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
13081#define SUB8(a, b, n) \
13082 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
13083#define PFX uh
13084
13085#include "op_addsub.h"
13086
13087static inline uint8_t do_usad(uint8_t a, uint8_t b)
13088{
13089 if (a > b)
13090 return a - b;
13091 else
13092 return b - a;
13093}
13094
13095/* Unsigned sum of absolute byte differences. */
13096uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
13097{
13098 uint32_t sum;
13099 sum = do_usad(a, b);
13100 sum += do_usad(a >> 8, b >> 8);
bdc3b6f5 13101 sum += do_usad(a >> 16, b >> 16);
6ddbc6e4
PB
13102 sum += do_usad(a >> 24, b >> 24);
13103 return sum;
13104}
13105
13106/* For ARMv6 SEL instruction. */
13107uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
13108{
13109 uint32_t mask;
13110
13111 mask = 0;
13112 if (flags & 1)
13113 mask |= 0xff;
13114 if (flags & 2)
13115 mask |= 0xff00;
13116 if (flags & 4)
13117 mask |= 0xff0000;
13118 if (flags & 8)
13119 mask |= 0xff000000;
13120 return (a & mask) | (b & ~mask);
13121}
13122
aa633469
PM
13123/* CRC helpers.
13124 * The upper bytes of val (above the number specified by 'bytes') must have
13125 * been zeroed out by the caller.
13126 */
eb0ecd5a
WN
13127uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
13128{
13129 uint8_t buf[4];
13130
aa633469 13131 stl_le_p(buf, val);
eb0ecd5a
WN
13132
13133 /* zlib crc32 converts the accumulator and output to one's complement. */
13134 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
13135}
13136
13137uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
13138{
13139 uint8_t buf[4];
13140
aa633469 13141 stl_le_p(buf, val);
eb0ecd5a
WN
13142
13143 /* Linux crc32c converts the output to one's complement. */
13144 return crc32c(acc, buf, bytes) ^ 0xffffffff;
13145}
a9e01311
RH
13146
13147/* Return the exception level to which FP-disabled exceptions should
13148 * be taken, or 0 if FP is enabled.
13149 */
ced31551 13150int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 13151{
55faa212 13152#ifndef CONFIG_USER_ONLY
d5a6fa2d
RH
13153 uint64_t hcr_el2;
13154
a9e01311
RH
13155 /* CPACR and the CPTR registers don't exist before v6, so FP is
13156 * always accessible
13157 */
13158 if (!arm_feature(env, ARM_FEATURE_V6)) {
13159 return 0;
13160 }
13161
d87513c0
PM
13162 if (arm_feature(env, ARM_FEATURE_M)) {
13163 /* CPACR can cause a NOCP UsageFault taken to current security state */
13164 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
13165 return 1;
13166 }
13167
13168 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
13169 if (!extract32(env->v7m.nsacr, 10, 1)) {
13170 /* FP insns cause a NOCP UsageFault taken to Secure */
13171 return 3;
13172 }
13173 }
13174
13175 return 0;
13176 }
13177
d5a6fa2d
RH
13178 hcr_el2 = arm_hcr_el2_eff(env);
13179
a9e01311
RH
13180 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
13181 * 0, 2 : trap EL0 and EL1/PL1 accesses
13182 * 1 : trap only EL0 accesses
13183 * 3 : trap no accesses
c2ddb7cf 13184 * This register is ignored if E2H+TGE are both set.
a9e01311 13185 */
d5a6fa2d 13186 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
c2ddb7cf
RH
13187 int fpen = extract32(env->cp15.cpacr_el1, 20, 2);
13188
13189 switch (fpen) {
13190 case 0:
13191 case 2:
13192 if (cur_el == 0 || cur_el == 1) {
13193 /* Trap to PL1, which might be EL1 or EL3 */
13194 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
13195 return 3;
13196 }
13197 return 1;
13198 }
13199 if (cur_el == 3 && !is_a64(env)) {
13200 /* Secure PL1 running at EL3 */
a9e01311
RH
13201 return 3;
13202 }
c2ddb7cf
RH
13203 break;
13204 case 1:
13205 if (cur_el == 0) {
13206 return 1;
13207 }
13208 break;
13209 case 3:
13210 break;
a9e01311 13211 }
a9e01311
RH
13212 }
13213
fc1120a7
PM
13214 /*
13215 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
13216 * to control non-secure access to the FPU. It doesn't have any
13217 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
13218 */
13219 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
13220 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
13221 if (!extract32(env->cp15.nsacr, 10, 1)) {
13222 /* FP insns act as UNDEF */
13223 return cur_el == 2 ? 2 : 1;
13224 }
13225 }
13226
d5a6fa2d
RH
13227 /*
13228 * CPTR_EL2 is present in v7VE or v8, and changes format
13229 * with HCR_EL2.E2H (regardless of TGE).
a9e01311 13230 */
d5a6fa2d
RH
13231 if (cur_el <= 2) {
13232 if (hcr_el2 & HCR_E2H) {
13233 /* Check CPTR_EL2.FPEN. */
13234 switch (extract32(env->cp15.cptr_el[2], 20, 2)) {
13235 case 1:
13236 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
13237 break;
13238 }
13239 /* fall through */
13240 case 0:
13241 case 2:
13242 return 2;
13243 }
13244 } else if (arm_is_el2_enabled(env)) {
13245 if (env->cp15.cptr_el[2] & CPTR_TFP) {
13246 return 2;
13247 }
13248 }
a9e01311
RH
13249 }
13250
13251 /* CPTR_EL3 : present in v8 */
a7b66ada 13252 if (env->cp15.cptr_el[3] & CPTR_TFP) {
a9e01311
RH
13253 /* Trap all FP ops to EL3 */
13254 return 3;
13255 }
55faa212 13256#endif
a9e01311
RH
13257 return 0;
13258}
13259
b9f6033c
RH
13260/* Return the exception level we're running at if this is our mmu_idx */
13261int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
13262{
13263 if (mmu_idx & ARM_MMU_IDX_M) {
13264 return mmu_idx & ARM_MMU_IDX_M_PRIV;
13265 }
13266
13267 switch (mmu_idx) {
13268 case ARMMMUIdx_E10_0:
13269 case ARMMMUIdx_E20_0:
13270 case ARMMMUIdx_SE10_0:
b6ad6062 13271 case ARMMMUIdx_SE20_0:
b9f6033c
RH
13272 return 0;
13273 case ARMMMUIdx_E10_1:
452ef8cb 13274 case ARMMMUIdx_E10_1_PAN:
b9f6033c 13275 case ARMMMUIdx_SE10_1:
452ef8cb 13276 case ARMMMUIdx_SE10_1_PAN:
b9f6033c
RH
13277 return 1;
13278 case ARMMMUIdx_E2:
13279 case ARMMMUIdx_E20_2:
452ef8cb 13280 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
13281 case ARMMMUIdx_SE2:
13282 case ARMMMUIdx_SE20_2:
13283 case ARMMMUIdx_SE20_2_PAN:
b9f6033c
RH
13284 return 2;
13285 case ARMMMUIdx_SE3:
13286 return 3;
13287 default:
13288 g_assert_not_reached();
13289 }
13290}
13291
7aab5a8c 13292#ifndef CONFIG_TCG
65e4655c
RH
13293ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
13294{
7aab5a8c 13295 g_assert_not_reached();
65e4655c 13296}
7aab5a8c 13297#endif
65e4655c 13298
164690b2 13299ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 13300{
b6ad6062
RDC
13301 ARMMMUIdx idx;
13302 uint64_t hcr;
13303
65e4655c 13304 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 13305 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
13306 }
13307
6003d980 13308 /* See ARM pseudo-function ELIsInHost. */
b9f6033c
RH
13309 switch (el) {
13310 case 0:
b6ad6062
RDC
13311 hcr = arm_hcr_el2_eff(env);
13312 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
13313 idx = ARMMMUIdx_E20_0;
13314 } else {
13315 idx = ARMMMUIdx_E10_0;
6003d980 13316 }
b6ad6062 13317 break;
b9f6033c 13318 case 1:
66412260 13319 if (env->pstate & PSTATE_PAN) {
b6ad6062
RDC
13320 idx = ARMMMUIdx_E10_1_PAN;
13321 } else {
13322 idx = ARMMMUIdx_E10_1;
66412260 13323 }
b6ad6062 13324 break;
b9f6033c 13325 case 2:
6003d980 13326 /* Note that TGE does not apply at EL2. */
b6ad6062 13327 if (arm_hcr_el2_eff(env) & HCR_E2H) {
66412260 13328 if (env->pstate & PSTATE_PAN) {
b6ad6062
RDC
13329 idx = ARMMMUIdx_E20_2_PAN;
13330 } else {
13331 idx = ARMMMUIdx_E20_2;
66412260 13332 }
b6ad6062
RDC
13333 } else {
13334 idx = ARMMMUIdx_E2;
6003d980 13335 }
b6ad6062 13336 break;
b9f6033c
RH
13337 case 3:
13338 return ARMMMUIdx_SE3;
13339 default:
13340 g_assert_not_reached();
65e4655c 13341 }
b6ad6062
RDC
13342
13343 if (arm_is_secure_below_el3(env)) {
13344 idx &= ~ARM_MMU_IDX_A_NS;
13345 }
13346
13347 return idx;
50494a27
RH
13348}
13349
164690b2
RH
13350ARMMMUIdx arm_mmu_idx(CPUARMState *env)
13351{
13352 return arm_mmu_idx_el(env, arm_current_el(env));
13353}
13354
64be86ab
RH
13355#ifndef CONFIG_USER_ONLY
13356ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
13357{
13358 return stage_1_mmu_idx(arm_mmu_idx(env));
13359}
13360#endif
13361
3902bfc6
RH
13362static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
13363 ARMMMUIdx mmu_idx,
13364 CPUARMTBFlags flags)
fdd1b228 13365{
a729a46b
RH
13366 DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
13367 DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
fdd1b228 13368
fdd1b228 13369 if (arm_singlestep_active(env)) {
a729a46b 13370 DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
fdd1b228
RH
13371 }
13372 return flags;
13373}
13374
3902bfc6
RH
13375static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
13376 ARMMMUIdx mmu_idx,
13377 CPUARMTBFlags flags)
43eccfb6 13378{
8061a649
RH
13379 bool sctlr_b = arm_sctlr_b(env);
13380
13381 if (sctlr_b) {
a729a46b 13382 DP_TBFLAG_A32(flags, SCTLR__B, 1);
8061a649
RH
13383 }
13384 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
a729a46b 13385 DP_TBFLAG_ANY(flags, BE_DATA, 1);
8061a649 13386 }
a729a46b 13387 DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
43eccfb6
RH
13388
13389 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
13390}
13391
3902bfc6
RH
13392static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
13393 ARMMMUIdx mmu_idx)
6e33ced5 13394{
3902bfc6 13395 CPUARMTBFlags flags = {};
4479ec30
RH
13396 uint32_t ccr = env->v7m.ccr[env->v7m.secure];
13397
13398 /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
13399 if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
13400 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13401 }
6e33ced5
RH
13402
13403 if (arm_v7m_is_handler_mode(env)) {
a729a46b 13404 DP_TBFLAG_M32(flags, HANDLER, 1);
6e33ced5
RH
13405 }
13406
13407 /*
13408 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
13409 * is suppressing them because the requested execution priority
13410 * is less than 0.
13411 */
13412 if (arm_feature(env, ARM_FEATURE_V8) &&
13413 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
4479ec30 13414 (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
a729a46b 13415 DP_TBFLAG_M32(flags, STACKCHECK, 1);
6e33ced5
RH
13416 }
13417
13418 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
13419}
13420
3902bfc6 13421static CPUARMTBFlags rebuild_hflags_aprofile(CPUARMState *env)
83f4baef 13422{
3902bfc6 13423 CPUARMTBFlags flags = {};
83f4baef 13424
a729a46b 13425 DP_TBFLAG_ANY(flags, DEBUG_TARGET_EL, arm_debug_target_el(env));
83f4baef
RH
13426 return flags;
13427}
13428
3902bfc6
RH
13429static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
13430 ARMMMUIdx mmu_idx)
c747224c 13431{
3902bfc6 13432 CPUARMTBFlags flags = rebuild_hflags_aprofile(env);
4479ec30
RH
13433 int el = arm_current_el(env);
13434
13435 if (arm_sctlr(env, el) & SCTLR_A) {
13436 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13437 }
0a54d68e
RH
13438
13439 if (arm_el_is_aa64(env, 1)) {
a729a46b 13440 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 13441 }
5bb0a20b 13442
4479ec30 13443 if (el < 2 && env->cp15.hstr_el2 &&
5bb0a20b 13444 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
a729a46b 13445 DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
5bb0a20b
MZ
13446 }
13447
520d1621
PM
13448 if (env->uncached_cpsr & CPSR_IL) {
13449 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
13450 }
13451
83f4baef 13452 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
c747224c
RH
13453}
13454
3902bfc6
RH
13455static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
13456 ARMMMUIdx mmu_idx)
a9e01311 13457{
3902bfc6 13458 CPUARMTBFlags flags = rebuild_hflags_aprofile(env);
d4d7503a 13459 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
b830a5ee 13460 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
d4d7503a
RH
13461 uint64_t sctlr;
13462 int tbii, tbid;
b9adaa70 13463
a729a46b 13464 DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
cd208a1c 13465
339370b9 13466 /* Get control bits for tagged addresses. */
b830a5ee
RH
13467 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
13468 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
5d8634f5 13469
a729a46b
RH
13470 DP_TBFLAG_A64(flags, TBII, tbii);
13471 DP_TBFLAG_A64(flags, TBID, tbid);
d4d7503a
RH
13472
13473 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
13474 int sve_el = sve_exception_el(env, el);
13475 uint32_t zcr_len;
5d8634f5 13476
d4d7503a
RH
13477 /*
13478 * If SVE is disabled, but FP is enabled,
13479 * then the effective len is 0.
13480 */
13481 if (sve_el != 0 && fp_el == 0) {
13482 zcr_len = 0;
13483 } else {
13484 zcr_len = sve_zcr_len_for_el(env, el);
5d8634f5 13485 }
a729a46b
RH
13486 DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
13487 DP_TBFLAG_A64(flags, ZCR_LEN, zcr_len);
d4d7503a 13488 }
1db5e96c 13489
aaec1432 13490 sctlr = regime_sctlr(env, stage1);
1db5e96c 13491
4479ec30
RH
13492 if (sctlr & SCTLR_A) {
13493 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13494 }
13495
8061a649 13496 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
a729a46b 13497 DP_TBFLAG_ANY(flags, BE_DATA, 1);
8061a649
RH
13498 }
13499
d4d7503a
RH
13500 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
13501 /*
13502 * In order to save space in flags, we record only whether
13503 * pauth is "inactive", meaning all insns are implemented as
13504 * a nop, or "active" when some action must be performed.
13505 * The decision of which action to take is left to a helper.
13506 */
13507 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
a729a46b 13508 DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
1db5e96c 13509 }
d4d7503a 13510 }
0816ef1b 13511
d4d7503a
RH
13512 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
13513 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
13514 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
a729a46b 13515 DP_TBFLAG_A64(flags, BT, 1);
0816ef1b 13516 }
d4d7503a 13517 }
08f1434a 13518
cc28fc30 13519 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
7a8014ab
RH
13520 if (!(env->pstate & PSTATE_UAO)) {
13521 switch (mmu_idx) {
13522 case ARMMMUIdx_E10_1:
13523 case ARMMMUIdx_E10_1_PAN:
13524 case ARMMMUIdx_SE10_1:
13525 case ARMMMUIdx_SE10_1_PAN:
13526 /* TODO: ARMv8.3-NV */
a729a46b 13527 DP_TBFLAG_A64(flags, UNPRIV, 1);
7a8014ab
RH
13528 break;
13529 case ARMMMUIdx_E20_2:
13530 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
13531 case ARMMMUIdx_SE20_2:
13532 case ARMMMUIdx_SE20_2_PAN:
7a8014ab
RH
13533 /*
13534 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
13535 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
13536 */
13537 if (env->cp15.hcr_el2 & HCR_TGE) {
a729a46b 13538 DP_TBFLAG_A64(flags, UNPRIV, 1);
7a8014ab
RH
13539 }
13540 break;
13541 default:
13542 break;
cc28fc30 13543 }
cc28fc30
RH
13544 }
13545
520d1621
PM
13546 if (env->pstate & PSTATE_IL) {
13547 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
13548 }
13549
81ae05fa
RH
13550 if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
13551 /*
13552 * Set MTE_ACTIVE if any access may be Checked, and leave clear
13553 * if all accesses must be Unchecked:
13554 * 1) If no TBI, then there are no tags in the address to check,
13555 * 2) If Tag Check Override, then all accesses are Unchecked,
13556 * 3) If Tag Check Fail == 0, then Checked access have no effect,
13557 * 4) If no Allocation Tag Access, then all accesses are Unchecked.
13558 */
13559 if (allocation_tag_access_enabled(env, el, sctlr)) {
a729a46b 13560 DP_TBFLAG_A64(flags, ATA, 1);
81ae05fa
RH
13561 if (tbid
13562 && !(env->pstate & PSTATE_TCO)
13563 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
a729a46b 13564 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
81ae05fa
RH
13565 }
13566 }
13567 /* And again for unprivileged accesses, if required. */
a729a46b 13568 if (EX_TBFLAG_A64(flags, UNPRIV)
81ae05fa
RH
13569 && tbid
13570 && !(env->pstate & PSTATE_TCO)
2d928adf 13571 && (sctlr & SCTLR_TCF0)
81ae05fa 13572 && allocation_tag_access_enabled(env, 0, sctlr)) {
a729a46b 13573 DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
81ae05fa
RH
13574 }
13575 /* Cache TCMA as well as TBI. */
a729a46b 13576 DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
81ae05fa
RH
13577 }
13578
d4d7503a
RH
13579 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
13580}
13581
3902bfc6 13582static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
3d74e2e9
RH
13583{
13584 int el = arm_current_el(env);
13585 int fp_el = fp_exception_el(env, el);
164690b2 13586 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3d74e2e9
RH
13587
13588 if (is_a64(env)) {
13589 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
13590 } else if (arm_feature(env, ARM_FEATURE_M)) {
13591 return rebuild_hflags_m32(env, fp_el, mmu_idx);
13592 } else {
13593 return rebuild_hflags_a32(env, fp_el, mmu_idx);
13594 }
13595}
13596
13597void arm_rebuild_hflags(CPUARMState *env)
13598{
13599 env->hflags = rebuild_hflags_internal(env);
13600}
13601
19717e9b
PM
13602/*
13603 * If we have triggered a EL state change we can't rely on the
13604 * translator having passed it to us, we need to recompute.
13605 */
13606void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
13607{
13608 int el = arm_current_el(env);
13609 int fp_el = fp_exception_el(env, el);
13610 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3902bfc6 13611
19717e9b
PM
13612 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
13613}
13614
14f3c588
RH
13615void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
13616{
13617 int fp_el = fp_exception_el(env, el);
13618 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13619
13620 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
13621}
13622
f80741d1
AB
13623/*
13624 * If we have triggered a EL state change we can't rely on the
563152e0 13625 * translator having passed it to us, we need to recompute.
f80741d1
AB
13626 */
13627void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
13628{
13629 int el = arm_current_el(env);
13630 int fp_el = fp_exception_el(env, el);
13631 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13632 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
13633}
13634
14f3c588
RH
13635void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
13636{
13637 int fp_el = fp_exception_el(env, el);
13638 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13639
13640 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
13641}
13642
13643void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
13644{
13645 int fp_el = fp_exception_el(env, el);
13646 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13647
13648 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
13649}
13650
0ee8b24a
PMD
13651static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
13652{
13653#ifdef CONFIG_DEBUG_TCG
3902bfc6
RH
13654 CPUARMTBFlags c = env->hflags;
13655 CPUARMTBFlags r = rebuild_hflags_internal(env);
0ee8b24a 13656
a378206a
RH
13657 if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
13658 fprintf(stderr, "TCG hflags mismatch "
13659 "(current:(0x%08x,0x" TARGET_FMT_lx ")"
13660 " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
13661 c.flags, c.flags2, r.flags, r.flags2);
0ee8b24a
PMD
13662 abort();
13663 }
13664#endif
13665}
13666
26702213
PM
13667static bool mve_no_pred(CPUARMState *env)
13668{
13669 /*
13670 * Return true if there is definitely no predication of MVE
13671 * instructions by VPR or LTPSIZE. (Returning false even if there
13672 * isn't any predication is OK; generated code will just be
13673 * a little worse.)
13674 * If the CPU does not implement MVE then this TB flag is always 0.
13675 *
13676 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
13677 * logic in gen_update_fp_context() needs to be updated to match.
13678 *
13679 * We do not include the effect of the ECI bits here -- they are
13680 * tracked in other TB flags. This simplifies the logic for
13681 * "when did we emit code that changes the MVE_NO_PRED TB flag
13682 * and thus need to end the TB?".
13683 */
13684 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
13685 return false;
13686 }
13687 if (env->v7m.vpr) {
13688 return false;
13689 }
13690 if (env->v7m.ltpsize < 4) {
13691 return false;
13692 }
13693 return true;
13694}
13695
d4d7503a
RH
13696void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
13697 target_ulong *cs_base, uint32_t *pflags)
13698{
3902bfc6 13699 CPUARMTBFlags flags;
d4d7503a 13700
0ee8b24a 13701 assert_hflags_rebuild_correctly(env);
3902bfc6 13702 flags = env->hflags;
3d74e2e9 13703
a729a46b 13704 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
d4d7503a 13705 *pc = env->pc;
d4d7503a 13706 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
a729a46b 13707 DP_TBFLAG_A64(flags, BTYPE, env->btype);
08f1434a 13708 }
a9e01311
RH
13709 } else {
13710 *pc = env->regs[15];
6e33ced5
RH
13711
13712 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
13713 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
13714 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
13715 != env->v7m.secure) {
a729a46b 13716 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
9550d1bd
RH
13717 }
13718
13719 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
13720 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
13721 (env->v7m.secure &&
13722 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
13723 /*
13724 * ASPEN is set, but FPCA/SFPA indicate that there is no
13725 * active FP context; we must create a new FP context before
13726 * executing any FP insn.
13727 */
a729a46b 13728 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
9550d1bd
RH
13729 }
13730
13731 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
13732 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
a729a46b 13733 DP_TBFLAG_M32(flags, LSPACT, 1);
9550d1bd 13734 }
26702213
PM
13735
13736 if (mve_no_pred(env)) {
13737 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
13738 }
6e33ced5 13739 } else {
bbad7c62
RH
13740 /*
13741 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
13742 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
13743 */
13744 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
a729a46b 13745 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
bbad7c62 13746 } else {
a729a46b
RH
13747 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
13748 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
bbad7c62 13749 }
0a54d68e 13750 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
a729a46b 13751 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 13752 }
6e33ced5
RH
13753 }
13754
a729a46b
RH
13755 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
13756 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
d4d7503a 13757 }
a9e01311 13758
60e12c37
RH
13759 /*
13760 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
13761 * states defined in the ARM ARM for software singlestep:
13762 * SS_ACTIVE PSTATE.SS State
13763 * 0 x Inactive (the TB flag for SS is always 0)
13764 * 1 0 Active-pending
13765 * 1 1 Active-not-pending
ae6eb1e9 13766 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
a9e01311 13767 */
a729a46b
RH
13768 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
13769 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
a9e01311 13770 }
a9e01311 13771
3902bfc6 13772 *pflags = flags.flags;
a378206a 13773 *cs_base = flags.flags2;
a9e01311 13774}
0ab5953b
RH
13775
13776#ifdef TARGET_AARCH64
13777/*
13778 * The manual says that when SVE is enabled and VQ is widened the
13779 * implementation is allowed to zero the previously inaccessible
13780 * portion of the registers. The corollary to that is that when
13781 * SVE is enabled and VQ is narrowed we are also allowed to zero
13782 * the now inaccessible portion of the registers.
13783 *
13784 * The intent of this is that no predicate bit beyond VQ is ever set.
13785 * Which means that some operations on predicate registers themselves
13786 * may operate on full uint64_t or even unrolled across the maximum
13787 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
13788 * may well be cheaper than conditionals to restrict the operation
13789 * to the relevant portion of a uint16_t[16].
13790 */
13791void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
13792{
13793 int i, j;
13794 uint64_t pmask;
13795
13796 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 13797 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
13798
13799 /* Zap the high bits of the zregs. */
13800 for (i = 0; i < 32; i++) {
13801 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
13802 }
13803
13804 /* Zap the high bits of the pregs and ffr. */
13805 pmask = 0;
13806 if (vq & 3) {
13807 pmask = ~(-1ULL << (16 * (vq & 3)));
13808 }
13809 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
13810 for (i = 0; i < 17; ++i) {
13811 env->vfp.pregs[i].p[j] &= pmask;
13812 }
13813 pmask = 0;
13814 }
13815}
13816
13817/*
13818 * Notice a change in SVE vector size when changing EL.
13819 */
9a05f7b6
RH
13820void aarch64_sve_change_el(CPUARMState *env, int old_el,
13821 int new_el, bool el0_a64)
0ab5953b 13822{
2fc0cc0e 13823 ARMCPU *cpu = env_archcpu(env);
0ab5953b 13824 int old_len, new_len;
9a05f7b6 13825 bool old_a64, new_a64;
0ab5953b
RH
13826
13827 /* Nothing to do if no SVE. */
cd208a1c 13828 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
13829 return;
13830 }
13831
13832 /* Nothing to do if FP is disabled in either EL. */
13833 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
13834 return;
13835 }
13836
13837 /*
13838 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
13839 * at ELx, or not available because the EL is in AArch32 state, then
13840 * for all purposes other than a direct read, the ZCR_ELx.LEN field
13841 * has an effective value of 0".
13842 *
13843 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
13844 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
13845 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
13846 * we already have the correct register contents when encountering the
13847 * vq0->vq0 transition between EL0->EL1.
13848 */
9a05f7b6
RH
13849 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
13850 old_len = (old_a64 && !sve_exception_el(env, old_el)
0ab5953b 13851 ? sve_zcr_len_for_el(env, old_el) : 0);
9a05f7b6
RH
13852 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
13853 new_len = (new_a64 && !sve_exception_el(env, new_el)
0ab5953b
RH
13854 ? sve_zcr_len_for_el(env, new_el) : 0);
13855
13856 /* When changing vector length, clear inaccessible state. */
13857 if (new_len < old_len) {
13858 aarch64_sve_narrow_vq(env, new_len + 1);
13859 }
13860}
13861#endif