]> Git Repo - qemu.git/blame - target/arm/helper.c
target/arm: Merge allocation of the cpreg and its name
[qemu.git] / target / arm / helper.c
CommitLineData
ed3baad1
PMD
1/*
2 * ARM generic helpers.
3 *
4 * This code is licensed under the GNU GPL v2 or later.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
db725815 8
74c21bd0 9#include "qemu/osdep.h"
63159601 10#include "qemu/units.h"
cd617484 11#include "qemu/log.h"
181962fd 12#include "target/arm/idau.h"
194cbc49 13#include "trace.h"
b5ff1b31 14#include "cpu.h"
ccd38087 15#include "internals.h"
2ef6175a 16#include "exec/helper-proto.h"
1de7afc9 17#include "qemu/host-utils.h"
db725815 18#include "qemu/main-loop.h"
b8012ecf 19#include "qemu/timer.h"
1de7afc9 20#include "qemu/bitops.h"
eb0ecd5a 21#include "qemu/crc32c.h"
0442428a 22#include "qemu/qemu-print.h"
63c91552 23#include "exec/exec-all.h"
eb0ecd5a 24#include <zlib.h> /* For crc32 */
64552b6b 25#include "hw/irq.h"
6b5fe137 26#include "semihosting/semihost.h"
b2e23725 27#include "sysemu/cpus.h"
740b1759 28#include "sysemu/cpu-timers.h"
f3a9b694 29#include "sysemu/kvm.h"
9d2b5a58 30#include "qemu/range.h"
7f7b4e7a 31#include "qapi/qapi-commands-machine-target.h"
de390645
RH
32#include "qapi/error.h"
33#include "qemu/guest-random.h"
91f78c58
PMD
34#ifdef CONFIG_TCG
35#include "arm_ldst.h"
7aab5a8c 36#include "exec/cpu_ldst.h"
6b5fe137 37#include "semihosting/common-semi.h"
91f78c58 38#endif
cf7c6d10 39#include "cpregs.h"
0b03bdfc 40
352c98e5 41#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
21c2dd77 42#define PMCR_NUM_COUNTERS 4 /* QEMU IMPDEF choice */
352c98e5 43
4a501606 44#ifndef CONFIG_USER_ONLY
7c2cb42b 45
98e87797 46static bool get_phys_addr_lpae(CPUARMState *env, uint64_t address,
03ae85f8 47 MMUAccessType access_type, ARMMMUIdx mmu_idx,
ff7de2fc 48 bool s1_is_el0,
37785977 49 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 50 target_ulong *page_size_ptr,
7e98e21c
RH
51 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
52 __attribute__((nonnull));
4a501606
PM
53#endif
54
affdb64d 55static void switch_mode(CPUARMState *env, int mode);
ea04dce7 56static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx);
affdb64d 57
c4241c7d 58static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 59{
375421cc 60 assert(ri->fieldoffset);
67ed771d 61 if (cpreg_field_is_64bit(ri)) {
c4241c7d 62 return CPREG_FIELD64(env, ri);
22d9e1a9 63 } else {
c4241c7d 64 return CPREG_FIELD32(env, ri);
22d9e1a9 65 }
d4e6df63
PM
66}
67
c4241c7d
PM
68static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
69 uint64_t value)
d4e6df63 70{
375421cc 71 assert(ri->fieldoffset);
67ed771d 72 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
73 CPREG_FIELD64(env, ri) = value;
74 } else {
75 CPREG_FIELD32(env, ri) = value;
76 }
d4e6df63
PM
77}
78
11f136ee
FA
79static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
80{
81 return (char *)env + ri->fieldoffset;
82}
83
49a66191 84uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 85{
59a1c327 86 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 87 if (ri->type & ARM_CP_CONST) {
59a1c327 88 return ri->resetvalue;
721fae12 89 } else if (ri->raw_readfn) {
59a1c327 90 return ri->raw_readfn(env, ri);
721fae12 91 } else if (ri->readfn) {
59a1c327 92 return ri->readfn(env, ri);
721fae12 93 } else {
59a1c327 94 return raw_read(env, ri);
721fae12 95 }
721fae12
PM
96}
97
59a1c327 98static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 99 uint64_t v)
721fae12
PM
100{
101 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
102 * Note that constant registers are treated as write-ignored; the
103 * caller should check for success by whether a readback gives the
104 * value written.
105 */
106 if (ri->type & ARM_CP_CONST) {
59a1c327 107 return;
721fae12 108 } else if (ri->raw_writefn) {
c4241c7d 109 ri->raw_writefn(env, ri, v);
721fae12 110 } else if (ri->writefn) {
c4241c7d 111 ri->writefn(env, ri, v);
721fae12 112 } else {
afb2530f 113 raw_write(env, ri, v);
721fae12 114 }
721fae12
PM
115}
116
375421cc
PM
117static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
118{
119 /* Return true if the regdef would cause an assertion if you called
120 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
121 * program bug for it not to have the NO_RAW flag).
122 * NB that returning false here doesn't necessarily mean that calling
123 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
124 * read/write access functions which are safe for raw use" from "has
125 * read/write access functions which have side effects but has forgotten
126 * to provide raw access functions".
127 * The tests here line up with the conditions in read/write_raw_cp_reg()
128 * and assertions in raw_read()/raw_write().
129 */
130 if ((ri->type & ARM_CP_CONST) ||
131 ri->fieldoffset ||
132 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
133 return false;
134 }
135 return true;
136}
137
b698e4ee 138bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
721fae12
PM
139{
140 /* Write the coprocessor state from cpu->env to the (index,value) list. */
141 int i;
142 bool ok = true;
143
144 for (i = 0; i < cpu->cpreg_array_len; i++) {
145 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
146 const ARMCPRegInfo *ri;
b698e4ee 147 uint64_t newval;
59a1c327 148
60322b39 149 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
150 if (!ri) {
151 ok = false;
152 continue;
153 }
7a0e58fa 154 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
155 continue;
156 }
b698e4ee
PM
157
158 newval = read_raw_cp_reg(&cpu->env, ri);
159 if (kvm_sync) {
160 /*
161 * Only sync if the previous list->cpustate sync succeeded.
162 * Rather than tracking the success/failure state for every
163 * item in the list, we just recheck "does the raw write we must
164 * have made in write_list_to_cpustate() read back OK" here.
165 */
166 uint64_t oldval = cpu->cpreg_values[i];
167
168 if (oldval == newval) {
169 continue;
170 }
171
172 write_raw_cp_reg(&cpu->env, ri, oldval);
173 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
174 continue;
175 }
176
177 write_raw_cp_reg(&cpu->env, ri, newval);
178 }
179 cpu->cpreg_values[i] = newval;
721fae12
PM
180 }
181 return ok;
182}
183
184bool write_list_to_cpustate(ARMCPU *cpu)
185{
186 int i;
187 bool ok = true;
188
189 for (i = 0; i < cpu->cpreg_array_len; i++) {
190 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
191 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
192 const ARMCPRegInfo *ri;
193
60322b39 194 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
195 if (!ri) {
196 ok = false;
197 continue;
198 }
7a0e58fa 199 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
200 continue;
201 }
202 /* Write value and confirm it reads back as written
203 * (to catch read-only registers and partially read-only
204 * registers where the incoming migration value doesn't match)
205 */
59a1c327
PM
206 write_raw_cp_reg(&cpu->env, ri, v);
207 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
208 ok = false;
209 }
210 }
211 return ok;
212}
213
214static void add_cpreg_to_list(gpointer key, gpointer opaque)
215{
216 ARMCPU *cpu = opaque;
5860362d
RH
217 uint32_t regidx = (uintptr_t)key;
218 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 219
7a0e58fa 220 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
221 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
222 /* The value array need not be initialized at this point */
223 cpu->cpreg_array_len++;
224 }
225}
226
227static void count_cpreg(gpointer key, gpointer opaque)
228{
229 ARMCPU *cpu = opaque;
721fae12
PM
230 const ARMCPRegInfo *ri;
231
5860362d 232 ri = g_hash_table_lookup(cpu->cp_regs, key);
721fae12 233
7a0e58fa 234 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
235 cpu->cpreg_array_len++;
236 }
237}
238
239static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
240{
5860362d
RH
241 uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
242 uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
721fae12 243
cbf239b7
AR
244 if (aidx > bidx) {
245 return 1;
246 }
247 if (aidx < bidx) {
248 return -1;
249 }
250 return 0;
721fae12
PM
251}
252
253void init_cpreg_list(ARMCPU *cpu)
254{
255 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
256 * Note that we require cpreg_tuples[] to be sorted by key ID.
257 */
57b6d95e 258 GList *keys;
721fae12
PM
259 int arraylen;
260
57b6d95e 261 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
262 keys = g_list_sort(keys, cpreg_key_compare);
263
264 cpu->cpreg_array_len = 0;
265
266 g_list_foreach(keys, count_cpreg, cpu);
267
268 arraylen = cpu->cpreg_array_len;
269 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
270 cpu->cpreg_values = g_new(uint64_t, arraylen);
271 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
272 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
273 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
274 cpu->cpreg_array_len = 0;
275
276 g_list_foreach(keys, add_cpreg_to_list, cpu);
277
278 assert(cpu->cpreg_array_len == arraylen);
279
280 g_list_free(keys);
281}
282
68e9c2fe 283/*
93dd1e61 284 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
68e9c2fe
EI
285 */
286static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
287 const ARMCPRegInfo *ri,
288 bool isread)
68e9c2fe 289{
93dd1e61
EI
290 if (!is_a64(env) && arm_current_el(env) == 3 &&
291 arm_is_secure_below_el3(env)) {
68e9c2fe
EI
292 return CP_ACCESS_TRAP_UNCATEGORIZED;
293 }
294 return CP_ACCESS_OK;
295}
296
5513c3ab
PM
297/* Some secure-only AArch32 registers trap to EL3 if used from
298 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
299 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
300 * We assume that the .access field is set to PL1_RW.
301 */
302static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
303 const ARMCPRegInfo *ri,
304 bool isread)
5513c3ab
PM
305{
306 if (arm_current_el(env) == 3) {
307 return CP_ACCESS_OK;
308 }
309 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
310 if (env->cp15.scr_el3 & SCR_EEL2) {
311 return CP_ACCESS_TRAP_EL2;
312 }
5513c3ab
PM
313 return CP_ACCESS_TRAP_EL3;
314 }
315 /* This will be EL1 NS and EL2 NS, which just UNDEF */
316 return CP_ACCESS_TRAP_UNCATEGORIZED;
317}
318
59dd089c
RDC
319static uint64_t arm_mdcr_el2_eff(CPUARMState *env)
320{
321 return arm_is_el2_enabled(env) ? env->cp15.mdcr_el2 : 0;
322}
323
187f678d
PM
324/* Check for traps to "powerdown debug" registers, which are controlled
325 * by MDCR.TDOSA
326 */
327static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
328 bool isread)
329{
330 int el = arm_current_el(env);
59dd089c
RDC
331 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
332 bool mdcr_el2_tdosa = (mdcr_el2 & MDCR_TDOSA) || (mdcr_el2 & MDCR_TDE) ||
7c208e0f 333 (arm_hcr_el2_eff(env) & HCR_TGE);
187f678d 334
59dd089c 335 if (el < 2 && mdcr_el2_tdosa) {
187f678d
PM
336 return CP_ACCESS_TRAP_EL2;
337 }
338 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
339 return CP_ACCESS_TRAP_EL3;
340 }
341 return CP_ACCESS_OK;
342}
343
91b0a238
PM
344/* Check for traps to "debug ROM" registers, which are controlled
345 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
346 */
347static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
348 bool isread)
349{
350 int el = arm_current_el(env);
59dd089c
RDC
351 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
352 bool mdcr_el2_tdra = (mdcr_el2 & MDCR_TDRA) || (mdcr_el2 & MDCR_TDE) ||
7c208e0f 353 (arm_hcr_el2_eff(env) & HCR_TGE);
91b0a238 354
59dd089c 355 if (el < 2 && mdcr_el2_tdra) {
91b0a238
PM
356 return CP_ACCESS_TRAP_EL2;
357 }
358 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
359 return CP_ACCESS_TRAP_EL3;
360 }
361 return CP_ACCESS_OK;
362}
363
d6c8cf81
PM
364/* Check for traps to general debug registers, which are controlled
365 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
366 */
367static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
368 bool isread)
369{
370 int el = arm_current_el(env);
59dd089c
RDC
371 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
372 bool mdcr_el2_tda = (mdcr_el2 & MDCR_TDA) || (mdcr_el2 & MDCR_TDE) ||
7c208e0f 373 (arm_hcr_el2_eff(env) & HCR_TGE);
d6c8cf81 374
59dd089c 375 if (el < 2 && mdcr_el2_tda) {
d6c8cf81
PM
376 return CP_ACCESS_TRAP_EL2;
377 }
378 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
379 return CP_ACCESS_TRAP_EL3;
380 }
381 return CP_ACCESS_OK;
382}
383
1fce1ba9
PM
384/* Check for traps to performance monitor registers, which are controlled
385 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
386 */
387static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
388 bool isread)
389{
390 int el = arm_current_el(env);
59dd089c 391 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1fce1ba9 392
59dd089c 393 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1fce1ba9
PM
394 return CP_ACCESS_TRAP_EL2;
395 }
396 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
397 return CP_ACCESS_TRAP_EL3;
398 }
399 return CP_ACCESS_OK;
400}
401
84929218
RH
402/* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
403static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
404 bool isread)
405{
406 if (arm_current_el(env) == 1) {
407 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
408 if (arm_hcr_el2_eff(env) & trap) {
409 return CP_ACCESS_TRAP_EL2;
410 }
411 }
412 return CP_ACCESS_OK;
413}
414
1803d271
RH
415/* Check for traps from EL1 due to HCR_EL2.TSW. */
416static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
417 bool isread)
418{
419 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
420 return CP_ACCESS_TRAP_EL2;
421 }
422 return CP_ACCESS_OK;
423}
424
99602377
RH
425/* Check for traps from EL1 due to HCR_EL2.TACR. */
426static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
427 bool isread)
428{
429 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
430 return CP_ACCESS_TRAP_EL2;
431 }
432 return CP_ACCESS_OK;
433}
434
30881b73
RH
435/* Check for traps from EL1 due to HCR_EL2.TTLB. */
436static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
437 bool isread)
438{
439 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
440 return CP_ACCESS_TRAP_EL2;
441 }
442 return CP_ACCESS_OK;
443}
444
c4241c7d 445static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 446{
2fc0cc0e 447 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 448
8d5c773e 449 raw_write(env, ri, value);
d10eb08f 450 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
451}
452
c4241c7d 453static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 454{
2fc0cc0e 455 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 456
8d5c773e 457 if (raw_read(env, ri) != value) {
08de207b
PM
458 /* Unlike real hardware the qemu TLB uses virtual addresses,
459 * not modified virtual addresses, so this causes a TLB flush.
460 */
d10eb08f 461 tlb_flush(CPU(cpu));
8d5c773e 462 raw_write(env, ri, value);
08de207b 463 }
08de207b 464}
c4241c7d
PM
465
466static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
467 uint64_t value)
08de207b 468{
2fc0cc0e 469 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 470
452a0955 471 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 472 && !extended_addresses_enabled(env)) {
08de207b
PM
473 /* For VMSA (when not using the LPAE long descriptor page table
474 * format) this register includes the ASID, so do a TLB flush.
475 * For PMSA it is purely a process ID and no action is needed.
476 */
d10eb08f 477 tlb_flush(CPU(cpu));
08de207b 478 }
8d5c773e 479 raw_write(env, ri, value);
08de207b
PM
480}
481
b4ab8ce9
PM
482/* IS variants of TLB operations must affect all cores */
483static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
484 uint64_t value)
485{
29a0af61 486 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
487
488 tlb_flush_all_cpus_synced(cs);
489}
490
491static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
492 uint64_t value)
493{
29a0af61 494 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
495
496 tlb_flush_all_cpus_synced(cs);
497}
498
499static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
500 uint64_t value)
501{
29a0af61 502 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
503
504 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
505}
506
507static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
508 uint64_t value)
509{
29a0af61 510 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
511
512 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
513}
514
515/*
516 * Non-IS variants of TLB operations are upgraded to
373e7ffd 517 * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
b4ab8ce9
PM
518 * force broadcast of these operations.
519 */
520static bool tlb_force_broadcast(CPUARMState *env)
521{
373e7ffd 522 return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
b4ab8ce9
PM
523}
524
c4241c7d
PM
525static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
526 uint64_t value)
d929823f
PM
527{
528 /* Invalidate all (TLBIALL) */
527db2be 529 CPUState *cs = env_cpu(env);
00c8cb0a 530
b4ab8ce9 531 if (tlb_force_broadcast(env)) {
527db2be
RH
532 tlb_flush_all_cpus_synced(cs);
533 } else {
534 tlb_flush(cs);
b4ab8ce9 535 }
d929823f
PM
536}
537
c4241c7d
PM
538static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
539 uint64_t value)
d929823f
PM
540{
541 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
527db2be 542 CPUState *cs = env_cpu(env);
31b030d4 543
527db2be 544 value &= TARGET_PAGE_MASK;
b4ab8ce9 545 if (tlb_force_broadcast(env)) {
527db2be
RH
546 tlb_flush_page_all_cpus_synced(cs, value);
547 } else {
548 tlb_flush_page(cs, value);
b4ab8ce9 549 }
d929823f
PM
550}
551
c4241c7d
PM
552static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
553 uint64_t value)
d929823f
PM
554{
555 /* Invalidate by ASID (TLBIASID) */
527db2be 556 CPUState *cs = env_cpu(env);
00c8cb0a 557
b4ab8ce9 558 if (tlb_force_broadcast(env)) {
527db2be
RH
559 tlb_flush_all_cpus_synced(cs);
560 } else {
561 tlb_flush(cs);
b4ab8ce9 562 }
d929823f
PM
563}
564
c4241c7d
PM
565static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
566 uint64_t value)
d929823f
PM
567{
568 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
527db2be 569 CPUState *cs = env_cpu(env);
31b030d4 570
527db2be 571 value &= TARGET_PAGE_MASK;
b4ab8ce9 572 if (tlb_force_broadcast(env)) {
527db2be
RH
573 tlb_flush_page_all_cpus_synced(cs, value);
574 } else {
575 tlb_flush_page(cs, value);
b4ab8ce9 576 }
fa439fc5
PM
577}
578
541ef8c2
SS
579static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
580 uint64_t value)
581{
29a0af61 582 CPUState *cs = env_cpu(env);
541ef8c2 583
0336cbf8 584 tlb_flush_by_mmuidx(cs,
01b98b68 585 ARMMMUIdxBit_E10_1 |
452ef8cb 586 ARMMMUIdxBit_E10_1_PAN |
bf05340c 587 ARMMMUIdxBit_E10_0);
541ef8c2
SS
588}
589
590static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
591 uint64_t value)
592{
29a0af61 593 CPUState *cs = env_cpu(env);
541ef8c2 594
a67cf277 595 tlb_flush_by_mmuidx_all_cpus_synced(cs,
01b98b68 596 ARMMMUIdxBit_E10_1 |
452ef8cb 597 ARMMMUIdxBit_E10_1_PAN |
bf05340c 598 ARMMMUIdxBit_E10_0);
541ef8c2
SS
599}
600
541ef8c2
SS
601
602static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
603 uint64_t value)
604{
29a0af61 605 CPUState *cs = env_cpu(env);
541ef8c2 606
e013b741 607 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
608}
609
610static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
611 uint64_t value)
612{
29a0af61 613 CPUState *cs = env_cpu(env);
541ef8c2 614
e013b741 615 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
616}
617
618static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
619 uint64_t value)
620{
29a0af61 621 CPUState *cs = env_cpu(env);
541ef8c2
SS
622 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
623
e013b741 624 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
541ef8c2
SS
625}
626
627static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
628 uint64_t value)
629{
29a0af61 630 CPUState *cs = env_cpu(env);
541ef8c2
SS
631 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
632
a67cf277 633 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 634 ARMMMUIdxBit_E2);
541ef8c2
SS
635}
636
e9aa6c21 637static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
638 /* Define the secure and non-secure FCSE identifier CP registers
639 * separately because there is no secure bank in V8 (no _EL3). This allows
640 * the secure register to be properly reset and migrated. There is also no
641 * v8 EL1 version of the register so the non-secure instance stands alone.
642 */
9c513e78 643 { .name = "FCSEIDR",
54bf36ed
FA
644 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
645 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
646 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
647 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9c513e78 648 { .name = "FCSEIDR_S",
54bf36ed
FA
649 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
650 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
651 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 652 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
653 /* Define the secure and non-secure context identifier CP registers
654 * separately because there is no secure bank in V8 (no _EL3). This allows
655 * the secure register to be properly reset and migrated. In the
656 * non-secure case, the 32-bit register will have reset and migration
657 * disabled during registration as it is handled by the 64-bit instance.
658 */
659 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 660 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
661 .access = PL1_RW, .accessfn = access_tvm_trvm,
662 .secure = ARM_CP_SECSTATE_NS,
54bf36ed
FA
663 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
664 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9c513e78 665 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
54bf36ed 666 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
667 .access = PL1_RW, .accessfn = access_tvm_trvm,
668 .secure = ARM_CP_SECSTATE_S,
54bf36ed 669 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 670 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
671};
672
673static const ARMCPRegInfo not_v8_cp_reginfo[] = {
674 /* NB: Some of these registers exist in v8 but with more precise
675 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
676 */
677 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
678 { .name = "DACR",
679 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
84929218 680 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
681 .writefn = dacr_write, .raw_writefn = raw_write,
682 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
683 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
684 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
685 * For v6 and v5, these mappings are overly broad.
4fdd17dd 686 */
a903c449
EI
687 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
688 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
689 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
690 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
691 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
692 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
693 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 694 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
695 /* Cache maintenance ops; some of this space may be overridden later. */
696 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
697 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
698 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
699};
700
7d57f408
PM
701static const ARMCPRegInfo not_v6_cp_reginfo[] = {
702 /* Not all pre-v6 cores implemented this WFI, so this is slightly
703 * over-broad.
704 */
705 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
706 .access = PL1_W, .type = ARM_CP_WFI },
7d57f408
PM
707};
708
709static const ARMCPRegInfo not_v7_cp_reginfo[] = {
710 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
711 * is UNPREDICTABLE; we choose to NOP as most implementations do).
712 */
713 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
714 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
715 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
716 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
717 * OMAPCP will override this space.
718 */
719 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
720 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
721 .resetvalue = 0 },
722 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
723 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
724 .resetvalue = 0 },
776d4e5c
PM
725 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
726 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 727 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 728 .resetvalue = 0 },
50300698
PM
729 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
730 * implementing it as RAZ means the "debug architecture version" bits
731 * will read as a reserved value, which should cause Linux to not try
732 * to use the debug hardware.
733 */
734 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
735 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
736 /* MMU TLB control. Note that the wildcarding means we cover not just
737 * the unified TLB ops but also the dside/iside/inner-shareable variants.
738 */
739 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
740 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 741 .type = ARM_CP_NO_RAW },
995939a6
PM
742 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
743 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 744 .type = ARM_CP_NO_RAW },
995939a6
PM
745 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
746 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 747 .type = ARM_CP_NO_RAW },
995939a6
PM
748 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
749 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 750 .type = ARM_CP_NO_RAW },
a903c449
EI
751 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
752 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
753 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
754 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
755};
756
c4241c7d
PM
757static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
758 uint64_t value)
2771db27 759{
f0aff255
FA
760 uint32_t mask = 0;
761
762 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
763 if (!arm_feature(env, ARM_FEATURE_V8)) {
764 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
765 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
766 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
767 */
7fbc6a40 768 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
f0aff255
FA
769 /* VFP coprocessor: cp10 & cp11 [23:20] */
770 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
771
772 if (!arm_feature(env, ARM_FEATURE_NEON)) {
773 /* ASEDIS [31] bit is RAO/WI */
774 value |= (1 << 31);
775 }
776
777 /* VFPv3 and upwards with NEON implement 32 double precision
778 * registers (D0-D31).
779 */
a6627f5f 780 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
f0aff255
FA
781 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
782 value |= (1 << 30);
783 }
784 }
785 value &= mask;
2771db27 786 }
fc1120a7
PM
787
788 /*
789 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
790 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
791 */
792 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
793 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
794 value &= ~(0xf << 20);
795 value |= env->cp15.cpacr_el1 & (0xf << 20);
796 }
797
7ebd5f2e 798 env->cp15.cpacr_el1 = value;
2771db27
PM
799}
800
fc1120a7
PM
801static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
802{
803 /*
804 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
805 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
806 */
807 uint64_t value = env->cp15.cpacr_el1;
808
809 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
810 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
811 value &= ~(0xf << 20);
812 }
813 return value;
814}
815
816
5deac39c
PM
817static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
818{
819 /* Call cpacr_write() so that we reset with the correct RAO bits set
820 * for our CPU features.
821 */
822 cpacr_write(env, ri, 0);
823}
824
3f208fd7
PM
825static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
826 bool isread)
c6f19164
GB
827{
828 if (arm_feature(env, ARM_FEATURE_V8)) {
829 /* Check if CPACR accesses are to be trapped to EL2 */
e6ef0169
RDC
830 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
831 (env->cp15.cptr_el[2] & CPTR_TCPAC)) {
c6f19164
GB
832 return CP_ACCESS_TRAP_EL2;
833 /* Check if CPACR accesses are to be trapped to EL3 */
834 } else if (arm_current_el(env) < 3 &&
835 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
836 return CP_ACCESS_TRAP_EL3;
837 }
838 }
839
840 return CP_ACCESS_OK;
841}
842
3f208fd7
PM
843static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
844 bool isread)
c6f19164
GB
845{
846 /* Check if CPTR accesses are set to trap to EL3 */
847 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
848 return CP_ACCESS_TRAP_EL3;
849 }
850
851 return CP_ACCESS_OK;
852}
853
7d57f408
PM
854static const ARMCPRegInfo v6_cp_reginfo[] = {
855 /* prefetch by MVA in v6, NOP in v7 */
856 { .name = "MVA_prefetch",
857 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
858 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
859 /* We need to break the TB after ISB to execute self-modifying code
860 * correctly and also to take any pending interrupts immediately.
861 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
862 */
7d57f408 863 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 864 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 865 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 866 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 867 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 868 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 869 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218 870 .access = PL1_RW, .accessfn = access_tvm_trvm,
b848ce2b
FA
871 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
872 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
873 .resetvalue = 0, },
874 /* Watchpoint Fault Address Register : should actually only be present
875 * for 1136, 1176, 11MPCore.
876 */
877 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
878 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 879 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 880 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 881 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
fc1120a7 882 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
7d57f408
PM
883};
884
57a4a11b
AL
885typedef struct pm_event {
886 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
887 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
888 bool (*supported)(CPUARMState *);
889 /*
890 * Retrieve the current count of the underlying event. The programmed
891 * counters hold a difference from the return value from this function
892 */
893 uint64_t (*get_count)(CPUARMState *);
4e7beb0c
AL
894 /*
895 * Return how many nanoseconds it will take (at a minimum) for count events
896 * to occur. A negative value indicates the counter will never overflow, or
897 * that the counter has otherwise arranged for the overflow bit to be set
898 * and the PMU interrupt to be raised on overflow.
899 */
900 int64_t (*ns_per_count)(uint64_t);
57a4a11b
AL
901} pm_event;
902
b2e23725
AL
903static bool event_always_supported(CPUARMState *env)
904{
905 return true;
906}
907
0d4bfd7d
AL
908static uint64_t swinc_get_count(CPUARMState *env)
909{
910 /*
911 * SW_INCR events are written directly to the pmevcntr's by writes to
912 * PMSWINC, so there is no underlying count maintained by the PMU itself
913 */
914 return 0;
915}
916
4e7beb0c
AL
917static int64_t swinc_ns_per(uint64_t ignored)
918{
919 return -1;
920}
921
b2e23725
AL
922/*
923 * Return the underlying cycle count for the PMU cycle counters. If we're in
924 * usermode, simply return 0.
925 */
926static uint64_t cycles_get_count(CPUARMState *env)
927{
928#ifndef CONFIG_USER_ONLY
929 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
930 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
931#else
932 return cpu_get_host_ticks();
933#endif
934}
935
936#ifndef CONFIG_USER_ONLY
4e7beb0c
AL
937static int64_t cycles_ns_per(uint64_t cycles)
938{
939 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
940}
941
b2e23725
AL
942static bool instructions_supported(CPUARMState *env)
943{
740b1759 944 return icount_enabled() == 1; /* Precise instruction counting */
b2e23725
AL
945}
946
947static uint64_t instructions_get_count(CPUARMState *env)
948{
8191d368 949 return (uint64_t)icount_get_raw();
b2e23725 950}
4e7beb0c
AL
951
952static int64_t instructions_ns_per(uint64_t icount)
953{
8191d368 954 return icount_to_ns((int64_t)icount);
4e7beb0c 955}
b2e23725
AL
956#endif
957
0727f63b
PM
958static bool pmu_8_1_events_supported(CPUARMState *env)
959{
960 /* For events which are supported in any v8.1 PMU */
961 return cpu_isar_feature(any_pmu_8_1, env_archcpu(env));
962}
963
15dd1ebd
PM
964static bool pmu_8_4_events_supported(CPUARMState *env)
965{
966 /* For events which are supported in any v8.1 PMU */
967 return cpu_isar_feature(any_pmu_8_4, env_archcpu(env));
968}
969
0727f63b
PM
970static uint64_t zero_event_get_count(CPUARMState *env)
971{
972 /* For events which on QEMU never fire, so their count is always zero */
973 return 0;
974}
975
976static int64_t zero_event_ns_per(uint64_t cycles)
977{
978 /* An event which never fires can never overflow */
979 return -1;
980}
981
57a4a11b 982static const pm_event pm_events[] = {
0d4bfd7d
AL
983 { .number = 0x000, /* SW_INCR */
984 .supported = event_always_supported,
985 .get_count = swinc_get_count,
4e7beb0c 986 .ns_per_count = swinc_ns_per,
0d4bfd7d 987 },
b2e23725
AL
988#ifndef CONFIG_USER_ONLY
989 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
990 .supported = instructions_supported,
991 .get_count = instructions_get_count,
4e7beb0c 992 .ns_per_count = instructions_ns_per,
b2e23725
AL
993 },
994 { .number = 0x011, /* CPU_CYCLES, Cycle */
995 .supported = event_always_supported,
996 .get_count = cycles_get_count,
4e7beb0c 997 .ns_per_count = cycles_ns_per,
0727f63b 998 },
b2e23725 999#endif
0727f63b
PM
1000 { .number = 0x023, /* STALL_FRONTEND */
1001 .supported = pmu_8_1_events_supported,
1002 .get_count = zero_event_get_count,
1003 .ns_per_count = zero_event_ns_per,
1004 },
1005 { .number = 0x024, /* STALL_BACKEND */
1006 .supported = pmu_8_1_events_supported,
1007 .get_count = zero_event_get_count,
1008 .ns_per_count = zero_event_ns_per,
1009 },
15dd1ebd
PM
1010 { .number = 0x03c, /* STALL */
1011 .supported = pmu_8_4_events_supported,
1012 .get_count = zero_event_get_count,
1013 .ns_per_count = zero_event_ns_per,
1014 },
57a4a11b
AL
1015};
1016
1017/*
1018 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1019 * events (i.e. the statistical profiling extension), this implementation
1020 * should first be updated to something sparse instead of the current
1021 * supported_event_map[] array.
1022 */
15dd1ebd 1023#define MAX_EVENT_ID 0x3c
57a4a11b
AL
1024#define UNSUPPORTED_EVENT UINT16_MAX
1025static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1026
1027/*
bf8d0969
AL
1028 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1029 * of ARM event numbers to indices in our pm_events array.
57a4a11b
AL
1030 *
1031 * Note: Events in the 0x40XX range are not currently supported.
1032 */
bf8d0969 1033void pmu_init(ARMCPU *cpu)
57a4a11b 1034{
57a4a11b
AL
1035 unsigned int i;
1036
bf8d0969
AL
1037 /*
1038 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1039 * events to them
1040 */
57a4a11b
AL
1041 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1042 supported_event_map[i] = UNSUPPORTED_EVENT;
1043 }
bf8d0969
AL
1044 cpu->pmceid0 = 0;
1045 cpu->pmceid1 = 0;
57a4a11b
AL
1046
1047 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1048 const pm_event *cnt = &pm_events[i];
1049 assert(cnt->number <= MAX_EVENT_ID);
1050 /* We do not currently support events in the 0x40xx range */
1051 assert(cnt->number <= 0x3f);
1052
bf8d0969 1053 if (cnt->supported(&cpu->env)) {
57a4a11b 1054 supported_event_map[cnt->number] = i;
67da43d6 1055 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
bf8d0969
AL
1056 if (cnt->number & 0x20) {
1057 cpu->pmceid1 |= event_mask;
1058 } else {
1059 cpu->pmceid0 |= event_mask;
1060 }
57a4a11b
AL
1061 }
1062 }
57a4a11b
AL
1063}
1064
5ecdd3e4
AL
1065/*
1066 * Check at runtime whether a PMU event is supported for the current machine
1067 */
1068static bool event_supported(uint16_t number)
1069{
1070 if (number > MAX_EVENT_ID) {
1071 return false;
1072 }
1073 return supported_event_map[number] != UNSUPPORTED_EVENT;
1074}
1075
3f208fd7
PM
1076static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1077 bool isread)
200ac0ef 1078{
3b163b01 1079 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
1080 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1081 * trapping to EL2 or EL3 for other accesses.
200ac0ef 1082 */
1fce1ba9 1083 int el = arm_current_el(env);
59dd089c 1084 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1fce1ba9 1085
6ecd0b6b 1086 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 1087 return CP_ACCESS_TRAP;
200ac0ef 1088 }
59dd089c 1089 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1fce1ba9
PM
1090 return CP_ACCESS_TRAP_EL2;
1091 }
1092 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1093 return CP_ACCESS_TRAP_EL3;
1094 }
1095
fcd25206 1096 return CP_ACCESS_OK;
200ac0ef
PM
1097}
1098
6ecd0b6b
AB
1099static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1100 const ARMCPRegInfo *ri,
1101 bool isread)
1102{
1103 /* ER: event counter read trap control */
1104 if (arm_feature(env, ARM_FEATURE_V8)
1105 && arm_current_el(env) == 0
1106 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1107 && isread) {
1108 return CP_ACCESS_OK;
1109 }
1110
1111 return pmreg_access(env, ri, isread);
1112}
1113
1114static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1115 const ARMCPRegInfo *ri,
1116 bool isread)
1117{
1118 /* SW: software increment write trap control */
1119 if (arm_feature(env, ARM_FEATURE_V8)
1120 && arm_current_el(env) == 0
1121 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1122 && !isread) {
1123 return CP_ACCESS_OK;
1124 }
1125
1126 return pmreg_access(env, ri, isread);
1127}
1128
6ecd0b6b
AB
1129static CPAccessResult pmreg_access_selr(CPUARMState *env,
1130 const ARMCPRegInfo *ri,
1131 bool isread)
1132{
1133 /* ER: event counter read trap control */
1134 if (arm_feature(env, ARM_FEATURE_V8)
1135 && arm_current_el(env) == 0
1136 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1137 return CP_ACCESS_OK;
1138 }
1139
1140 return pmreg_access(env, ri, isread);
1141}
1142
1143static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1144 const ARMCPRegInfo *ri,
1145 bool isread)
1146{
1147 /* CR: cycle counter read trap control */
1148 if (arm_feature(env, ARM_FEATURE_V8)
1149 && arm_current_el(env) == 0
1150 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1151 && isread) {
1152 return CP_ACCESS_OK;
1153 }
1154
1155 return pmreg_access(env, ri, isread);
1156}
1157
033614c4
AL
1158/* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1159 * the current EL, security state, and register configuration.
1160 */
1161static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
87124fde 1162{
033614c4
AL
1163 uint64_t filter;
1164 bool e, p, u, nsk, nsu, nsh, m;
1165 bool enabled, prohibited, filtered;
1166 bool secure = arm_is_secure(env);
1167 int el = arm_current_el(env);
59dd089c
RDC
1168 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1169 uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
87124fde 1170
cbbb3041
AJ
1171 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1172 return false;
1173 }
1174
033614c4
AL
1175 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1176 (counter < hpmn || counter == 31)) {
1177 e = env->cp15.c9_pmcr & PMCRE;
1178 } else {
59dd089c 1179 e = mdcr_el2 & MDCR_HPME;
87124fde 1180 }
033614c4 1181 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
87124fde 1182
033614c4
AL
1183 if (!secure) {
1184 if (el == 2 && (counter < hpmn || counter == 31)) {
59dd089c 1185 prohibited = mdcr_el2 & MDCR_HPMD;
033614c4
AL
1186 } else {
1187 prohibited = false;
1188 }
1189 } else {
1190 prohibited = arm_feature(env, ARM_FEATURE_EL3) &&
db1f3afb 1191 !(env->cp15.mdcr_el3 & MDCR_SPME);
033614c4
AL
1192 }
1193
1194 if (prohibited && counter == 31) {
1195 prohibited = env->cp15.c9_pmcr & PMCRDP;
1196 }
1197
5ecdd3e4
AL
1198 if (counter == 31) {
1199 filter = env->cp15.pmccfiltr_el0;
1200 } else {
1201 filter = env->cp15.c14_pmevtyper[counter];
1202 }
033614c4
AL
1203
1204 p = filter & PMXEVTYPER_P;
1205 u = filter & PMXEVTYPER_U;
1206 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1207 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1208 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1209 m = arm_el_is_aa64(env, 1) &&
1210 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1211
1212 if (el == 0) {
1213 filtered = secure ? u : u != nsu;
1214 } else if (el == 1) {
1215 filtered = secure ? p : p != nsk;
1216 } else if (el == 2) {
1217 filtered = !nsh;
1218 } else { /* EL3 */
1219 filtered = m != p;
1220 }
1221
5ecdd3e4
AL
1222 if (counter != 31) {
1223 /*
1224 * If not checking PMCCNTR, ensure the counter is setup to an event we
1225 * support
1226 */
1227 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1228 if (!event_supported(event)) {
1229 return false;
1230 }
1231 }
1232
033614c4 1233 return enabled && !prohibited && !filtered;
87124fde 1234}
033614c4 1235
f4efb4b2
AL
1236static void pmu_update_irq(CPUARMState *env)
1237{
2fc0cc0e 1238 ARMCPU *cpu = env_archcpu(env);
f4efb4b2
AL
1239 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1240 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1241}
1242
5d05b9d4
AL
1243/*
1244 * Ensure c15_ccnt is the guest-visible count so that operations such as
1245 * enabling/disabling the counter or filtering, modifying the count itself,
1246 * etc. can be done logically. This is essentially a no-op if the counter is
1247 * not enabled at the time of the call.
1248 */
f2b2f53f 1249static void pmccntr_op_start(CPUARMState *env)
ec7b4ce4 1250{
b2e23725 1251 uint64_t cycles = cycles_get_count(env);
ec7b4ce4 1252
033614c4 1253 if (pmu_counter_enabled(env, 31)) {
5d05b9d4
AL
1254 uint64_t eff_cycles = cycles;
1255 if (env->cp15.c9_pmcr & PMCRD) {
1256 /* Increment once every 64 processor clock cycles */
1257 eff_cycles /= 64;
1258 }
1259
f4efb4b2
AL
1260 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1261
1262 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1263 1ull << 63 : 1ull << 31;
1264 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1265 env->cp15.c9_pmovsr |= (1 << 31);
1266 pmu_update_irq(env);
1267 }
1268
1269 env->cp15.c15_ccnt = new_pmccntr;
ec7b4ce4 1270 }
5d05b9d4
AL
1271 env->cp15.c15_ccnt_delta = cycles;
1272}
ec7b4ce4 1273
5d05b9d4
AL
1274/*
1275 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1276 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1277 * pmccntr_op_start.
1278 */
f2b2f53f 1279static void pmccntr_op_finish(CPUARMState *env)
5d05b9d4 1280{
033614c4 1281 if (pmu_counter_enabled(env, 31)) {
4e7beb0c
AL
1282#ifndef CONFIG_USER_ONLY
1283 /* Calculate when the counter will next overflow */
1284 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1285 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1286 remaining_cycles = (uint32_t)remaining_cycles;
1287 }
1288 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1289
1290 if (overflow_in > 0) {
1291 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1292 overflow_in;
2fc0cc0e 1293 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1294 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1295 }
1296#endif
5d05b9d4 1297
4e7beb0c 1298 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
5d05b9d4
AL
1299 if (env->cp15.c9_pmcr & PMCRD) {
1300 /* Increment once every 64 processor clock cycles */
1301 prev_cycles /= 64;
1302 }
5d05b9d4 1303 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
ec7b4ce4
AF
1304 }
1305}
1306
5ecdd3e4
AL
1307static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1308{
1309
1310 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1311 uint64_t count = 0;
1312 if (event_supported(event)) {
1313 uint16_t event_idx = supported_event_map[event];
1314 count = pm_events[event_idx].get_count(env);
1315 }
1316
1317 if (pmu_counter_enabled(env, counter)) {
f4efb4b2
AL
1318 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1319
1320 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) {
1321 env->cp15.c9_pmovsr |= (1 << counter);
1322 pmu_update_irq(env);
1323 }
1324 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
5ecdd3e4
AL
1325 }
1326 env->cp15.c14_pmevcntr_delta[counter] = count;
1327}
1328
1329static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1330{
1331 if (pmu_counter_enabled(env, counter)) {
4e7beb0c
AL
1332#ifndef CONFIG_USER_ONLY
1333 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1334 uint16_t event_idx = supported_event_map[event];
1335 uint64_t delta = UINT32_MAX -
1336 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1;
1337 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta);
1338
1339 if (overflow_in > 0) {
1340 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
1341 overflow_in;
2fc0cc0e 1342 ARMCPU *cpu = env_archcpu(env);
4e7beb0c
AL
1343 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1344 }
1345#endif
1346
5ecdd3e4
AL
1347 env->cp15.c14_pmevcntr_delta[counter] -=
1348 env->cp15.c14_pmevcntr[counter];
1349 }
1350}
1351
5d05b9d4
AL
1352void pmu_op_start(CPUARMState *env)
1353{
5ecdd3e4 1354 unsigned int i;
5d05b9d4 1355 pmccntr_op_start(env);
5ecdd3e4
AL
1356 for (i = 0; i < pmu_num_counters(env); i++) {
1357 pmevcntr_op_start(env, i);
1358 }
5d05b9d4
AL
1359}
1360
1361void pmu_op_finish(CPUARMState *env)
1362{
5ecdd3e4 1363 unsigned int i;
5d05b9d4 1364 pmccntr_op_finish(env);
5ecdd3e4
AL
1365 for (i = 0; i < pmu_num_counters(env); i++) {
1366 pmevcntr_op_finish(env, i);
1367 }
5d05b9d4
AL
1368}
1369
033614c4
AL
1370void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1371{
1372 pmu_op_start(&cpu->env);
1373}
1374
1375void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1376{
1377 pmu_op_finish(&cpu->env);
1378}
1379
4e7beb0c
AL
1380void arm_pmu_timer_cb(void *opaque)
1381{
1382 ARMCPU *cpu = opaque;
1383
1384 /*
1385 * Update all the counter values based on the current underlying counts,
1386 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1387 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1388 * counter may expire.
1389 */
1390 pmu_op_start(&cpu->env);
1391 pmu_op_finish(&cpu->env);
1392}
1393
c4241c7d
PM
1394static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1395 uint64_t value)
200ac0ef 1396{
5d05b9d4 1397 pmu_op_start(env);
7c2cb42b
AF
1398
1399 if (value & PMCRC) {
1400 /* The counter has been reset */
1401 env->cp15.c15_ccnt = 0;
1402 }
1403
5ecdd3e4
AL
1404 if (value & PMCRP) {
1405 unsigned int i;
1406 for (i = 0; i < pmu_num_counters(env); i++) {
1407 env->cp15.c14_pmevcntr[i] = 0;
1408 }
1409 }
1410
62d96ff4
PM
1411 env->cp15.c9_pmcr &= ~PMCR_WRITEABLE_MASK;
1412 env->cp15.c9_pmcr |= (value & PMCR_WRITEABLE_MASK);
7c2cb42b 1413
5d05b9d4 1414 pmu_op_finish(env);
7c2cb42b
AF
1415}
1416
0d4bfd7d
AL
1417static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1418 uint64_t value)
1419{
1420 unsigned int i;
1421 for (i = 0; i < pmu_num_counters(env); i++) {
1422 /* Increment a counter's count iff: */
1423 if ((value & (1 << i)) && /* counter's bit is set */
1424 /* counter is enabled and not filtered */
1425 pmu_counter_enabled(env, i) &&
1426 /* counter is SW_INCR */
1427 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1428 pmevcntr_op_start(env, i);
f4efb4b2
AL
1429
1430 /*
1431 * Detect if this write causes an overflow since we can't predict
1432 * PMSWINC overflows like we can for other events
1433 */
1434 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1435
1436 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) {
1437 env->cp15.c9_pmovsr |= (1 << i);
1438 pmu_update_irq(env);
1439 }
1440
1441 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1442
0d4bfd7d
AL
1443 pmevcntr_op_finish(env, i);
1444 }
1445 }
1446}
1447
7c2cb42b
AF
1448static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1449{
5d05b9d4
AL
1450 uint64_t ret;
1451 pmccntr_op_start(env);
1452 ret = env->cp15.c15_ccnt;
1453 pmccntr_op_finish(env);
1454 return ret;
7c2cb42b
AF
1455}
1456
6b040780
WH
1457static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1458 uint64_t value)
1459{
1460 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1461 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1462 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1463 * accessed.
1464 */
1465 env->cp15.c9_pmselr = value & 0x1f;
1466}
1467
7c2cb42b
AF
1468static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1469 uint64_t value)
1470{
5d05b9d4
AL
1471 pmccntr_op_start(env);
1472 env->cp15.c15_ccnt = value;
1473 pmccntr_op_finish(env);
200ac0ef 1474}
421c7ebd
PC
1475
1476static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1477 uint64_t value)
1478{
1479 uint64_t cur_val = pmccntr_read(env, NULL);
1480
1481 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1482}
1483
0614601c
AF
1484static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1485 uint64_t value)
1486{
5d05b9d4 1487 pmccntr_op_start(env);
4b8afa1f
AL
1488 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1489 pmccntr_op_finish(env);
1490}
1491
1492static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1493 uint64_t value)
1494{
1495 pmccntr_op_start(env);
1496 /* M is not accessible from AArch32 */
1497 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1498 (value & PMCCFILTR);
5d05b9d4 1499 pmccntr_op_finish(env);
0614601c
AF
1500}
1501
4b8afa1f
AL
1502static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1503{
1504 /* M is not visible in AArch32 */
1505 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1506}
1507
c4241c7d 1508static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1509 uint64_t value)
1510{
7ece99b1 1511 value &= pmu_counter_mask(env);
200ac0ef 1512 env->cp15.c9_pmcnten |= value;
200ac0ef
PM
1513}
1514
c4241c7d
PM
1515static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1516 uint64_t value)
200ac0ef 1517{
7ece99b1 1518 value &= pmu_counter_mask(env);
200ac0ef 1519 env->cp15.c9_pmcnten &= ~value;
200ac0ef
PM
1520}
1521
c4241c7d
PM
1522static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1523 uint64_t value)
200ac0ef 1524{
599b71e2 1525 value &= pmu_counter_mask(env);
200ac0ef 1526 env->cp15.c9_pmovsr &= ~value;
f4efb4b2 1527 pmu_update_irq(env);
200ac0ef
PM
1528}
1529
327dd510
AL
1530static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1531 uint64_t value)
1532{
1533 value &= pmu_counter_mask(env);
1534 env->cp15.c9_pmovsr |= value;
f4efb4b2 1535 pmu_update_irq(env);
327dd510
AL
1536}
1537
5ecdd3e4
AL
1538static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1539 uint64_t value, const uint8_t counter)
200ac0ef 1540{
5ecdd3e4
AL
1541 if (counter == 31) {
1542 pmccfiltr_write(env, ri, value);
1543 } else if (counter < pmu_num_counters(env)) {
1544 pmevcntr_op_start(env, counter);
1545
1546 /*
1547 * If this counter's event type is changing, store the current
1548 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1549 * pmevcntr_op_finish has the correct baseline when it converts back to
1550 * a delta.
1551 */
1552 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1553 PMXEVTYPER_EVTCOUNT;
1554 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1555 if (old_event != new_event) {
1556 uint64_t count = 0;
1557 if (event_supported(new_event)) {
1558 uint16_t event_idx = supported_event_map[new_event];
1559 count = pm_events[event_idx].get_count(env);
1560 }
1561 env->cp15.c14_pmevcntr_delta[counter] = count;
1562 }
1563
1564 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1565 pmevcntr_op_finish(env, counter);
1566 }
fdb86656
WH
1567 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1568 * PMSELR value is equal to or greater than the number of implemented
1569 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1570 */
5ecdd3e4
AL
1571}
1572
1573static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1574 const uint8_t counter)
1575{
1576 if (counter == 31) {
1577 return env->cp15.pmccfiltr_el0;
1578 } else if (counter < pmu_num_counters(env)) {
1579 return env->cp15.c14_pmevtyper[counter];
1580 } else {
1581 /*
1582 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1583 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1584 */
1585 return 0;
1586 }
1587}
1588
1589static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1590 uint64_t value)
1591{
1592 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1593 pmevtyper_write(env, ri, value, counter);
1594}
1595
1596static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1597 uint64_t value)
1598{
1599 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1600 env->cp15.c14_pmevtyper[counter] = value;
1601
1602 /*
1603 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1604 * pmu_op_finish calls when loading saved state for a migration. Because
1605 * we're potentially updating the type of event here, the value written to
1606 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1607 * different counter type. Therefore, we need to set this value to the
1608 * current count for the counter type we're writing so that pmu_op_finish
1609 * has the correct count for its calculation.
1610 */
1611 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1612 if (event_supported(event)) {
1613 uint16_t event_idx = supported_event_map[event];
1614 env->cp15.c14_pmevcntr_delta[counter] =
1615 pm_events[event_idx].get_count(env);
fdb86656
WH
1616 }
1617}
1618
5ecdd3e4
AL
1619static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1620{
1621 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1622 return pmevtyper_read(env, ri, counter);
1623}
1624
1625static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1626 uint64_t value)
1627{
1628 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1629}
1630
fdb86656
WH
1631static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1632{
5ecdd3e4
AL
1633 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1634}
1635
1636static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1637 uint64_t value, uint8_t counter)
1638{
1639 if (counter < pmu_num_counters(env)) {
1640 pmevcntr_op_start(env, counter);
1641 env->cp15.c14_pmevcntr[counter] = value;
1642 pmevcntr_op_finish(env, counter);
1643 }
1644 /*
1645 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1646 * are CONSTRAINED UNPREDICTABLE.
fdb86656 1647 */
5ecdd3e4
AL
1648}
1649
1650static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1651 uint8_t counter)
1652{
1653 if (counter < pmu_num_counters(env)) {
1654 uint64_t ret;
1655 pmevcntr_op_start(env, counter);
1656 ret = env->cp15.c14_pmevcntr[counter];
1657 pmevcntr_op_finish(env, counter);
1658 return ret;
fdb86656 1659 } else {
5ecdd3e4
AL
1660 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1661 * are CONSTRAINED UNPREDICTABLE. */
fdb86656
WH
1662 return 0;
1663 }
200ac0ef
PM
1664}
1665
5ecdd3e4
AL
1666static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1667 uint64_t value)
1668{
1669 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1670 pmevcntr_write(env, ri, value, counter);
1671}
1672
1673static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1674{
1675 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1676 return pmevcntr_read(env, ri, counter);
1677}
1678
1679static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1680 uint64_t value)
1681{
1682 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1683 assert(counter < pmu_num_counters(env));
1684 env->cp15.c14_pmevcntr[counter] = value;
1685 pmevcntr_write(env, ri, value, counter);
1686}
1687
1688static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1689{
1690 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1691 assert(counter < pmu_num_counters(env));
1692 return env->cp15.c14_pmevcntr[counter];
1693}
1694
1695static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1696 uint64_t value)
1697{
1698 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1699}
1700
1701static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1702{
1703 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1704}
1705
c4241c7d 1706static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1707 uint64_t value)
1708{
6ecd0b6b
AB
1709 if (arm_feature(env, ARM_FEATURE_V8)) {
1710 env->cp15.c9_pmuserenr = value & 0xf;
1711 } else {
1712 env->cp15.c9_pmuserenr = value & 1;
1713 }
200ac0ef
PM
1714}
1715
c4241c7d
PM
1716static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1717 uint64_t value)
200ac0ef
PM
1718{
1719 /* We have no event counters so only the C bit can be changed */
7ece99b1 1720 value &= pmu_counter_mask(env);
200ac0ef 1721 env->cp15.c9_pminten |= value;
f4efb4b2 1722 pmu_update_irq(env);
200ac0ef
PM
1723}
1724
c4241c7d
PM
1725static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1726 uint64_t value)
200ac0ef 1727{
7ece99b1 1728 value &= pmu_counter_mask(env);
200ac0ef 1729 env->cp15.c9_pminten &= ~value;
f4efb4b2 1730 pmu_update_irq(env);
200ac0ef
PM
1731}
1732
c4241c7d
PM
1733static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1734 uint64_t value)
8641136c 1735{
a505d7fe
PM
1736 /* Note that even though the AArch64 view of this register has bits
1737 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1738 * architectural requirements for bits which are RES0 only in some
1739 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1740 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1741 */
855ea66d 1742 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1743}
1744
64e0e2de
EI
1745static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1746{
ea22747c
RH
1747 /* Begin with base v8.0 state. */
1748 uint32_t valid_mask = 0x3fff;
2fc0cc0e 1749 ARMCPU *cpu = env_archcpu(env);
ea22747c 1750
252e8c69 1751 if (ri->state == ARM_CP_STATE_AA64) {
10d0ef3e
MN
1752 if (arm_feature(env, ARM_FEATURE_AARCH64) &&
1753 !cpu_isar_feature(aa64_aa32_el1, cpu)) {
1754 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */
1755 }
ea22747c 1756 valid_mask &= ~SCR_NET;
252e8c69
RH
1757
1758 if (cpu_isar_feature(aa64_lor, cpu)) {
1759 valid_mask |= SCR_TLOR;
1760 }
1761 if (cpu_isar_feature(aa64_pauth, cpu)) {
1762 valid_mask |= SCR_API | SCR_APK;
1763 }
926c1b97
RDC
1764 if (cpu_isar_feature(aa64_sel2, cpu)) {
1765 valid_mask |= SCR_EEL2;
1766 }
8ddb300b
RH
1767 if (cpu_isar_feature(aa64_mte, cpu)) {
1768 valid_mask |= SCR_ATA;
1769 }
ea22747c
RH
1770 } else {
1771 valid_mask &= ~(SCR_RW | SCR_ST);
1772 }
64e0e2de
EI
1773
1774 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1775 valid_mask &= ~SCR_HCE;
1776
1777 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1778 * supported if EL2 exists. The bit is UNK/SBZP when
1779 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1780 * when EL2 is unavailable.
4eb27640 1781 * On ARMv8, this bit is always available.
64e0e2de 1782 */
4eb27640
GB
1783 if (arm_feature(env, ARM_FEATURE_V7) &&
1784 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1785 valid_mask &= ~SCR_SMD;
1786 }
1787 }
1788
1789 /* Clear all-context RES0 bits. */
1790 value &= valid_mask;
1791 raw_write(env, ri, value);
1792}
1793
10d0ef3e
MN
1794static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1795{
1796 /*
1797 * scr_write will set the RES1 bits on an AArch64-only CPU.
1798 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1799 */
1800 scr_write(env, ri, 0);
1801}
1802
630fcd4d
MZ
1803static CPAccessResult access_aa64_tid2(CPUARMState *env,
1804 const ARMCPRegInfo *ri,
1805 bool isread)
1806{
1807 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1808 return CP_ACCESS_TRAP_EL2;
1809 }
1810
1811 return CP_ACCESS_OK;
1812}
1813
c4241c7d 1814static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c 1815{
2fc0cc0e 1816 ARMCPU *cpu = env_archcpu(env);
b85a1fd6
FA
1817
1818 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1819 * bank
1820 */
1821 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1822 ri->secure & ARM_CP_SECSTATE_S);
1823
1824 return cpu->ccsidr[index];
776d4e5c
PM
1825}
1826
c4241c7d
PM
1827static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1828 uint64_t value)
776d4e5c 1829{
8d5c773e 1830 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1831}
1832
1090b9c6
PM
1833static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1834{
29a0af61 1835 CPUState *cs = env_cpu(env);
cc974d5c
RDC
1836 bool el1 = arm_current_el(env) == 1;
1837 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1090b9c6
PM
1838 uint64_t ret = 0;
1839
cc974d5c 1840 if (hcr_el2 & HCR_IMO) {
636540e9
PM
1841 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1842 ret |= CPSR_I;
1843 }
1844 } else {
1845 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1846 ret |= CPSR_I;
1847 }
1090b9c6 1848 }
636540e9 1849
cc974d5c 1850 if (hcr_el2 & HCR_FMO) {
636540e9
PM
1851 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1852 ret |= CPSR_F;
1853 }
1854 } else {
1855 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1856 ret |= CPSR_F;
1857 }
1090b9c6 1858 }
636540e9 1859
1090b9c6
PM
1860 /* External aborts are not possible in QEMU so A bit is always clear */
1861 return ret;
1862}
1863
93fbc983
MZ
1864static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1865 bool isread)
1866{
1867 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1868 return CP_ACCESS_TRAP_EL2;
1869 }
1870
1871 return CP_ACCESS_OK;
1872}
1873
1874static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1875 bool isread)
1876{
1877 if (arm_feature(env, ARM_FEATURE_V8)) {
1878 return access_aa64_tid1(env, ri, isread);
1879 }
1880
1881 return CP_ACCESS_OK;
1882}
1883
e9aa6c21 1884static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
1885 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1886 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1887 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
1888 /* Performance monitors are implementation defined in v7,
1889 * but with an ARM recommended set of registers, which we
ac689a2e 1890 * follow.
200ac0ef
PM
1891 *
1892 * Performance registers fall into three categories:
1893 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1894 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1895 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1896 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1897 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1898 */
1899 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 1900 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 1901 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1902 .writefn = pmcntenset_write,
1903 .accessfn = pmreg_access,
1904 .raw_writefn = raw_write },
8521466b
AF
1905 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1906 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1907 .access = PL0_RW, .accessfn = pmreg_access,
1908 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1909 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 1910 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
1911 .access = PL0_RW,
1912 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1913 .accessfn = pmreg_access,
1914 .writefn = pmcntenclr_write,
7a0e58fa 1915 .type = ARM_CP_ALIAS },
8521466b
AF
1916 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1917 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1918 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 1919 .type = ARM_CP_ALIAS,
8521466b
AF
1920 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1921 .writefn = pmcntenclr_write },
200ac0ef 1922 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
f4efb4b2 1923 .access = PL0_RW, .type = ARM_CP_IO,
e4e91a21 1924 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
1925 .accessfn = pmreg_access,
1926 .writefn = pmovsr_write,
1927 .raw_writefn = raw_write },
978364f1
AF
1928 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1929 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1930 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 1931 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
1932 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1933 .writefn = pmovsr_write,
1934 .raw_writefn = raw_write },
200ac0ef 1935 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
f4efb4b2
AL
1936 .access = PL0_W, .accessfn = pmreg_access_swinc,
1937 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d
AL
1938 .writefn = pmswinc_write },
1939 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1940 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
f4efb4b2
AL
1941 .access = PL0_W, .accessfn = pmreg_access_swinc,
1942 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d 1943 .writefn = pmswinc_write },
6b040780
WH
1944 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1945 .access = PL0_RW, .type = ARM_CP_ALIAS,
1946 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 1947 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
1948 .raw_writefn = raw_write},
1949 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1950 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 1951 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
1952 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1953 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 1954 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 1955 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
421c7ebd 1956 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 1957 .accessfn = pmreg_access_ccntr },
8521466b
AF
1958 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1959 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 1960 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b 1961 .type = ARM_CP_IO,
980ebe87
AL
1962 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1963 .readfn = pmccntr_read, .writefn = pmccntr_write,
1964 .raw_readfn = raw_read, .raw_writefn = raw_write, },
4b8afa1f
AL
1965 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1966 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
1967 .access = PL0_RW, .accessfn = pmreg_access,
1968 .type = ARM_CP_ALIAS | ARM_CP_IO,
1969 .resetvalue = 0, },
8521466b
AF
1970 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1971 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
980ebe87 1972 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
8521466b
AF
1973 .access = PL0_RW, .accessfn = pmreg_access,
1974 .type = ARM_CP_IO,
1975 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1976 .resetvalue = 0, },
200ac0ef 1977 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
5ecdd3e4
AL
1978 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1979 .accessfn = pmreg_access,
fdb86656
WH
1980 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1981 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1982 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
5ecdd3e4
AL
1983 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1984 .accessfn = pmreg_access,
fdb86656 1985 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
200ac0ef 1986 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
5ecdd3e4
AL
1987 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1988 .accessfn = pmreg_access_xevcntr,
1989 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
1990 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
1991 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
1992 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1993 .accessfn = pmreg_access_xevcntr,
1994 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
200ac0ef 1995 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 1996 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 1997 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 1998 .resetvalue = 0,
d4e6df63 1999 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
2000 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2001 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 2002 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
2003 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2004 .resetvalue = 0,
2005 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 2006 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 2007 .access = PL1_RW, .accessfn = access_tpm,
b7d793ad 2008 .type = ARM_CP_ALIAS | ARM_CP_IO,
e6ec5457 2009 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 2010 .resetvalue = 0,
d4e6df63 2011 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
2012 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2013 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2014 .access = PL1_RW, .accessfn = access_tpm,
2015 .type = ARM_CP_IO,
2016 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2017 .writefn = pmintenset_write, .raw_writefn = raw_write,
2018 .resetvalue = 0x0 },
200ac0ef 2019 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
fc5f6856 2020 .access = PL1_RW, .accessfn = access_tpm,
887c0f15 2021 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
200ac0ef 2022 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 2023 .writefn = pmintenclr_write, },
978364f1
AF
2024 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2025 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
fc5f6856 2026 .access = PL1_RW, .accessfn = access_tpm,
887c0f15 2027 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
978364f1
AF
2028 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2029 .writefn = pmintenclr_write },
7da845b0
PM
2030 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2031 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
630fcd4d
MZ
2032 .access = PL1_R,
2033 .accessfn = access_aa64_tid2,
2034 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
2035 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2036 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
630fcd4d
MZ
2037 .access = PL1_RW,
2038 .accessfn = access_aa64_tid2,
2039 .writefn = csselr_write, .resetvalue = 0,
b85a1fd6
FA
2040 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2041 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
2042 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2043 * just RAZ for all cores:
2044 */
0ff644a7
PM
2045 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2046 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
93fbc983
MZ
2047 .access = PL1_R, .type = ARM_CP_CONST,
2048 .accessfn = access_aa64_tid1,
2049 .resetvalue = 0 },
f32cdad5
PM
2050 /* Auxiliary fault status registers: these also are IMPDEF, and we
2051 * choose to RAZ/WI for all cores.
2052 */
2053 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2054 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
84929218
RH
2055 .access = PL1_RW, .accessfn = access_tvm_trvm,
2056 .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
2057 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2058 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
84929218
RH
2059 .access = PL1_RW, .accessfn = access_tvm_trvm,
2060 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
2061 /* MAIR can just read-as-written because we don't implement caches
2062 * and so don't need to care about memory attributes.
2063 */
2064 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2065 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
84929218
RH
2066 .access = PL1_RW, .accessfn = access_tvm_trvm,
2067 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 2068 .resetvalue = 0 },
4cfb8ad8
PM
2069 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2070 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2071 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2072 .resetvalue = 0 },
b0fe2427
PM
2073 /* For non-long-descriptor page tables these are PRRR and NMRR;
2074 * regardless they still act as reads-as-written for QEMU.
b0fe2427 2075 */
1281f8e3 2076 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
2077 * allows them to assign the correct fieldoffset based on the endianness
2078 * handled in the field definitions.
2079 */
a903c449 2080 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
84929218
RH
2081 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2082 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2083 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2084 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 2085 .resetfn = arm_cp_reset_ignore },
a903c449 2086 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
84929218
RH
2087 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2088 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2089 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2090 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 2091 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
2092 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2093 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 2094 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
2095 /* 32 bit ITLB invalidates */
2096 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
30881b73
RH
2097 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2098 .writefn = tlbiall_write },
995939a6 2099 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
30881b73
RH
2100 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2101 .writefn = tlbimva_write },
995939a6 2102 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
30881b73
RH
2103 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2104 .writefn = tlbiasid_write },
995939a6
PM
2105 /* 32 bit DTLB invalidates */
2106 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
30881b73
RH
2107 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2108 .writefn = tlbiall_write },
995939a6 2109 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
30881b73
RH
2110 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2111 .writefn = tlbimva_write },
995939a6 2112 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
30881b73
RH
2113 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2114 .writefn = tlbiasid_write },
995939a6
PM
2115 /* 32 bit TLB invalidates */
2116 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73
RH
2117 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2118 .writefn = tlbiall_write },
995939a6 2119 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73
RH
2120 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2121 .writefn = tlbimva_write },
995939a6 2122 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73
RH
2123 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2124 .writefn = tlbiasid_write },
995939a6 2125 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73
RH
2126 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2127 .writefn = tlbimvaa_write },
995939a6
PM
2128};
2129
2130static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2131 /* 32 bit TLB invalidates, Inner Shareable */
2132 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
30881b73
RH
2133 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2134 .writefn = tlbiall_is_write },
995939a6 2135 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
30881b73
RH
2136 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2137 .writefn = tlbimva_is_write },
995939a6 2138 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
30881b73 2139 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 2140 .writefn = tlbiasid_is_write },
995939a6 2141 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
30881b73 2142 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 2143 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
2144};
2145
327dd510
AL
2146static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2147 /* PMOVSSET is not implemented in v7 before v7ve */
2148 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2149 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2150 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2151 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2152 .writefn = pmovsset_write,
2153 .raw_writefn = raw_write },
2154 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2155 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2156 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2157 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2158 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2159 .writefn = pmovsset_write,
2160 .raw_writefn = raw_write },
327dd510
AL
2161};
2162
c4241c7d
PM
2163static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2164 uint64_t value)
c326b979
PM
2165{
2166 value &= 1;
2167 env->teecr = value;
c326b979
PM
2168}
2169
cc7613bf
PM
2170static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2171 bool isread)
2172{
2173 /*
2174 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2175 * at all, so we don't need to check whether we're v8A.
2176 */
2177 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2178 (env->cp15.hstr_el2 & HSTR_TTEE)) {
2179 return CP_ACCESS_TRAP_EL2;
2180 }
2181 return CP_ACCESS_OK;
2182}
2183
3f208fd7
PM
2184static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2185 bool isread)
c326b979 2186{
dcbff19b 2187 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 2188 return CP_ACCESS_TRAP;
c326b979 2189 }
cc7613bf 2190 return teecr_access(env, ri, isread);
c326b979
PM
2191}
2192
2193static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2194 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2195 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2196 .resetvalue = 0,
cc7613bf 2197 .writefn = teecr_write, .accessfn = teecr_access },
c326b979
PM
2198 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2199 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 2200 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
2201};
2202
4d31c596 2203static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
2204 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2205 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2206 .access = PL0_RW,
54bf36ed 2207 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
2208 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2209 .access = PL0_RW,
54bf36ed
FA
2210 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2211 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
2212 .resetfn = arm_cp_reset_ignore },
2213 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2214 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2215 .access = PL0_R|PL1_W,
54bf36ed
FA
2216 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2217 .resetvalue = 0},
4d31c596
PM
2218 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2219 .access = PL0_R|PL1_W,
54bf36ed
FA
2220 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2221 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 2222 .resetfn = arm_cp_reset_ignore },
54bf36ed 2223 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 2224 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 2225 .access = PL1_RW,
54bf36ed
FA
2226 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2227 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2228 .access = PL1_RW,
2229 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2230 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2231 .resetvalue = 0 },
4d31c596
PM
2232};
2233
55d284af
PM
2234#ifndef CONFIG_USER_ONLY
2235
3f208fd7
PM
2236static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2237 bool isread)
00108f2d 2238{
75502672
PM
2239 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2240 * Writable only at the highest implemented exception level.
2241 */
2242 int el = arm_current_el(env);
5bc84371
RH
2243 uint64_t hcr;
2244 uint32_t cntkctl;
75502672
PM
2245
2246 switch (el) {
2247 case 0:
5bc84371
RH
2248 hcr = arm_hcr_el2_eff(env);
2249 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2250 cntkctl = env->cp15.cnthctl_el2;
2251 } else {
2252 cntkctl = env->cp15.c14_cntkctl;
2253 }
2254 if (!extract32(cntkctl, 0, 2)) {
75502672
PM
2255 return CP_ACCESS_TRAP;
2256 }
2257 break;
2258 case 1:
2259 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2260 arm_is_secure_below_el3(env)) {
2261 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2262 return CP_ACCESS_TRAP_UNCATEGORIZED;
2263 }
2264 break;
2265 case 2:
2266 case 3:
2267 break;
00108f2d 2268 }
75502672
PM
2269
2270 if (!isread && el < arm_highest_el(env)) {
2271 return CP_ACCESS_TRAP_UNCATEGORIZED;
2272 }
2273
00108f2d
PM
2274 return CP_ACCESS_OK;
2275}
2276
3f208fd7
PM
2277static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2278 bool isread)
00108f2d 2279{
0b6440af 2280 unsigned int cur_el = arm_current_el(env);
e6ef0169 2281 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2282 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2283
5bc84371
RH
2284 switch (cur_el) {
2285 case 0:
2286 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2287 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2288 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2289 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2290 }
0b6440af 2291
5bc84371
RH
2292 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2293 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2294 return CP_ACCESS_TRAP;
2295 }
2296
2297 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2298 if (hcr & HCR_E2H) {
2299 if (timeridx == GTIMER_PHYS &&
2300 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2301 return CP_ACCESS_TRAP_EL2;
2302 }
2303 } else {
2304 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
e6ef0169 2305 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2306 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2307 return CP_ACCESS_TRAP_EL2;
2308 }
2309 }
2310 break;
2311
2312 case 1:
2313 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
e6ef0169 2314 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2315 (hcr & HCR_E2H
2316 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2317 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2318 return CP_ACCESS_TRAP_EL2;
2319 }
2320 break;
0b6440af 2321 }
00108f2d
PM
2322 return CP_ACCESS_OK;
2323}
2324
3f208fd7
PM
2325static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2326 bool isread)
00108f2d 2327{
0b6440af 2328 unsigned int cur_el = arm_current_el(env);
e6ef0169 2329 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2330 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2331
5bc84371
RH
2332 switch (cur_el) {
2333 case 0:
2334 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2335 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2336 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2337 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2338 }
0b6440af 2339
5bc84371
RH
2340 /*
2341 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2342 * EL0 if EL0[PV]TEN is zero.
2343 */
2344 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2345 return CP_ACCESS_TRAP;
2346 }
2347 /* fall through */
2348
2349 case 1:
e6ef0169 2350 if (has_el2 && timeridx == GTIMER_PHYS) {
5bc84371
RH
2351 if (hcr & HCR_E2H) {
2352 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2353 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2354 return CP_ACCESS_TRAP_EL2;
2355 }
2356 } else {
2357 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2358 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2359 return CP_ACCESS_TRAP_EL2;
2360 }
2361 }
2362 }
2363 break;
0b6440af 2364 }
00108f2d
PM
2365 return CP_ACCESS_OK;
2366}
2367
2368static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
2369 const ARMCPRegInfo *ri,
2370 bool isread)
00108f2d 2371{
3f208fd7 2372 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2373}
2374
2375static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
2376 const ARMCPRegInfo *ri,
2377 bool isread)
00108f2d 2378{
3f208fd7 2379 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2380}
2381
3f208fd7
PM
2382static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2383 bool isread)
00108f2d 2384{
3f208fd7 2385 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2386}
2387
3f208fd7
PM
2388static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2389 bool isread)
00108f2d 2390{
3f208fd7 2391 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2392}
2393
b4d3978c 2394static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
2395 const ARMCPRegInfo *ri,
2396 bool isread)
b4d3978c
PM
2397{
2398 /* The AArch64 register view of the secure physical timer is
2399 * always accessible from EL3, and configurably accessible from
2400 * Secure EL1.
2401 */
2402 switch (arm_current_el(env)) {
2403 case 1:
2404 if (!arm_is_secure(env)) {
2405 return CP_ACCESS_TRAP;
2406 }
2407 if (!(env->cp15.scr_el3 & SCR_ST)) {
2408 return CP_ACCESS_TRAP_EL3;
2409 }
2410 return CP_ACCESS_OK;
2411 case 0:
2412 case 2:
2413 return CP_ACCESS_TRAP;
2414 case 3:
2415 return CP_ACCESS_OK;
2416 default:
2417 g_assert_not_reached();
2418 }
2419}
2420
55d284af
PM
2421static uint64_t gt_get_countervalue(CPUARMState *env)
2422{
7def8754
AJ
2423 ARMCPU *cpu = env_archcpu(env);
2424
2425 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
55d284af
PM
2426}
2427
2428static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2429{
2430 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2431
2432 if (gt->ctl & 1) {
2433 /* Timer enabled: calculate and set current ISTATUS, irq, and
2434 * reset timer to when ISTATUS next has to change
2435 */
edac4d8a
EI
2436 uint64_t offset = timeridx == GTIMER_VIRT ?
2437 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
2438 uint64_t count = gt_get_countervalue(&cpu->env);
2439 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 2440 int istatus = count - offset >= gt->cval;
55d284af 2441 uint64_t nexttick;
194cbc49 2442 int irqstate;
55d284af
PM
2443
2444 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
2445
2446 irqstate = (istatus && !(gt->ctl & 2));
2447 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2448
55d284af
PM
2449 if (istatus) {
2450 /* Next transition is when count rolls back over to zero */
2451 nexttick = UINT64_MAX;
2452 } else {
2453 /* Next transition is when we hit cval */
edac4d8a 2454 nexttick = gt->cval + offset;
55d284af
PM
2455 }
2456 /* Note that the desired next expiry time might be beyond the
2457 * signed-64-bit range of a QEMUTimer -- in this case we just
2458 * set the timer for as far in the future as possible. When the
2459 * timer expires we will reset the timer for any remaining period.
2460 */
7def8754 2461 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
4a0245b6
AJ
2462 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2463 } else {
2464 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af 2465 }
194cbc49 2466 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
2467 } else {
2468 /* Timer disabled: ISTATUS and timer output always clear */
2469 gt->ctl &= ~4;
2470 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 2471 timer_del(cpu->gt_timer[timeridx]);
194cbc49 2472 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
2473 }
2474}
2475
0e3eca4c
EI
2476static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2477 int timeridx)
55d284af 2478{
2fc0cc0e 2479 ARMCPU *cpu = env_archcpu(env);
55d284af 2480
bc72ad67 2481 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
2482}
2483
c4241c7d 2484static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 2485{
c4241c7d 2486 return gt_get_countervalue(env);
55d284af
PM
2487}
2488
53d1f856
RH
2489static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2490{
2491 uint64_t hcr;
2492
2493 switch (arm_current_el(env)) {
2494 case 2:
2495 hcr = arm_hcr_el2_eff(env);
2496 if (hcr & HCR_E2H) {
2497 return 0;
2498 }
2499 break;
2500 case 0:
2501 hcr = arm_hcr_el2_eff(env);
2502 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2503 return 0;
2504 }
2505 break;
2506 }
2507
2508 return env->cp15.cntvoff_el2;
2509}
2510
edac4d8a
EI
2511static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2512{
53d1f856 2513 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
edac4d8a
EI
2514}
2515
c4241c7d 2516static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2517 int timeridx,
c4241c7d 2518 uint64_t value)
55d284af 2519{
194cbc49 2520 trace_arm_gt_cval_write(timeridx, value);
55d284af 2521 env->cp15.c14_timer[timeridx].cval = value;
2fc0cc0e 2522 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af 2523}
c4241c7d 2524
0e3eca4c
EI
2525static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2526 int timeridx)
55d284af 2527{
53d1f856
RH
2528 uint64_t offset = 0;
2529
2530 switch (timeridx) {
2531 case GTIMER_VIRT:
8c94b071 2532 case GTIMER_HYPVIRT:
53d1f856
RH
2533 offset = gt_virt_cnt_offset(env);
2534 break;
2535 }
55d284af 2536
c4241c7d 2537 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 2538 (gt_get_countervalue(env) - offset));
55d284af
PM
2539}
2540
c4241c7d 2541static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2542 int timeridx,
c4241c7d 2543 uint64_t value)
55d284af 2544{
53d1f856
RH
2545 uint64_t offset = 0;
2546
2547 switch (timeridx) {
2548 case GTIMER_VIRT:
8c94b071 2549 case GTIMER_HYPVIRT:
53d1f856
RH
2550 offset = gt_virt_cnt_offset(env);
2551 break;
2552 }
55d284af 2553
194cbc49 2554 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 2555 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 2556 sextract64(value, 0, 32);
2fc0cc0e 2557 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af
PM
2558}
2559
c4241c7d 2560static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2561 int timeridx,
c4241c7d 2562 uint64_t value)
55d284af 2563{
2fc0cc0e 2564 ARMCPU *cpu = env_archcpu(env);
55d284af
PM
2565 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2566
194cbc49 2567 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 2568 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
2569 if ((oldval ^ value) & 1) {
2570 /* Enable toggled */
2571 gt_recalc_timer(cpu, timeridx);
d3afacc7 2572 } else if ((oldval ^ value) & 2) {
55d284af
PM
2573 /* IMASK toggled: don't need to recalculate,
2574 * just set the interrupt line based on ISTATUS
2575 */
194cbc49
PM
2576 int irqstate = (oldval & 4) && !(value & 2);
2577
2578 trace_arm_gt_imask_toggle(timeridx, irqstate);
2579 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 2580 }
55d284af
PM
2581}
2582
0e3eca4c
EI
2583static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2584{
2585 gt_timer_reset(env, ri, GTIMER_PHYS);
2586}
2587
2588static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2589 uint64_t value)
2590{
2591 gt_cval_write(env, ri, GTIMER_PHYS, value);
2592}
2593
2594static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2595{
2596 return gt_tval_read(env, ri, GTIMER_PHYS);
2597}
2598
2599static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2600 uint64_t value)
2601{
2602 gt_tval_write(env, ri, GTIMER_PHYS, value);
2603}
2604
2605static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2606 uint64_t value)
2607{
2608 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2609}
2610
bb5972e4
RH
2611static int gt_phys_redir_timeridx(CPUARMState *env)
2612{
2613 switch (arm_mmu_idx(env)) {
2614 case ARMMMUIdx_E20_0:
2615 case ARMMMUIdx_E20_2:
452ef8cb 2616 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
2617 case ARMMMUIdx_SE20_0:
2618 case ARMMMUIdx_SE20_2:
2619 case ARMMMUIdx_SE20_2_PAN:
bb5972e4
RH
2620 return GTIMER_HYP;
2621 default:
2622 return GTIMER_PHYS;
2623 }
2624}
2625
2626static int gt_virt_redir_timeridx(CPUARMState *env)
2627{
2628 switch (arm_mmu_idx(env)) {
2629 case ARMMMUIdx_E20_0:
2630 case ARMMMUIdx_E20_2:
452ef8cb 2631 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
2632 case ARMMMUIdx_SE20_0:
2633 case ARMMMUIdx_SE20_2:
2634 case ARMMMUIdx_SE20_2_PAN:
bb5972e4
RH
2635 return GTIMER_HYPVIRT;
2636 default:
2637 return GTIMER_VIRT;
2638 }
2639}
2640
2641static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2642 const ARMCPRegInfo *ri)
2643{
2644 int timeridx = gt_phys_redir_timeridx(env);
2645 return env->cp15.c14_timer[timeridx].cval;
2646}
2647
2648static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2649 uint64_t value)
2650{
2651 int timeridx = gt_phys_redir_timeridx(env);
2652 gt_cval_write(env, ri, timeridx, value);
2653}
2654
2655static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2656 const ARMCPRegInfo *ri)
2657{
2658 int timeridx = gt_phys_redir_timeridx(env);
2659 return gt_tval_read(env, ri, timeridx);
2660}
2661
2662static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2663 uint64_t value)
2664{
2665 int timeridx = gt_phys_redir_timeridx(env);
2666 gt_tval_write(env, ri, timeridx, value);
2667}
2668
2669static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2670 const ARMCPRegInfo *ri)
2671{
2672 int timeridx = gt_phys_redir_timeridx(env);
2673 return env->cp15.c14_timer[timeridx].ctl;
2674}
2675
2676static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2677 uint64_t value)
2678{
2679 int timeridx = gt_phys_redir_timeridx(env);
2680 gt_ctl_write(env, ri, timeridx, value);
2681}
2682
0e3eca4c
EI
2683static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2684{
2685 gt_timer_reset(env, ri, GTIMER_VIRT);
2686}
2687
2688static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2689 uint64_t value)
2690{
2691 gt_cval_write(env, ri, GTIMER_VIRT, value);
2692}
2693
2694static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2695{
2696 return gt_tval_read(env, ri, GTIMER_VIRT);
2697}
2698
2699static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2700 uint64_t value)
2701{
2702 gt_tval_write(env, ri, GTIMER_VIRT, value);
2703}
2704
2705static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2706 uint64_t value)
2707{
2708 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2709}
2710
edac4d8a
EI
2711static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2712 uint64_t value)
2713{
2fc0cc0e 2714 ARMCPU *cpu = env_archcpu(env);
edac4d8a 2715
194cbc49 2716 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
2717 raw_write(env, ri, value);
2718 gt_recalc_timer(cpu, GTIMER_VIRT);
2719}
2720
bb5972e4
RH
2721static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2722 const ARMCPRegInfo *ri)
2723{
2724 int timeridx = gt_virt_redir_timeridx(env);
2725 return env->cp15.c14_timer[timeridx].cval;
2726}
2727
2728static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2729 uint64_t value)
2730{
2731 int timeridx = gt_virt_redir_timeridx(env);
2732 gt_cval_write(env, ri, timeridx, value);
2733}
2734
2735static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2736 const ARMCPRegInfo *ri)
2737{
2738 int timeridx = gt_virt_redir_timeridx(env);
2739 return gt_tval_read(env, ri, timeridx);
2740}
2741
2742static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2743 uint64_t value)
2744{
2745 int timeridx = gt_virt_redir_timeridx(env);
2746 gt_tval_write(env, ri, timeridx, value);
2747}
2748
2749static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2750 const ARMCPRegInfo *ri)
2751{
2752 int timeridx = gt_virt_redir_timeridx(env);
2753 return env->cp15.c14_timer[timeridx].ctl;
2754}
2755
2756static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2757 uint64_t value)
2758{
2759 int timeridx = gt_virt_redir_timeridx(env);
2760 gt_ctl_write(env, ri, timeridx, value);
2761}
2762
b0e66d95
EI
2763static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2764{
2765 gt_timer_reset(env, ri, GTIMER_HYP);
2766}
2767
2768static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2769 uint64_t value)
2770{
2771 gt_cval_write(env, ri, GTIMER_HYP, value);
2772}
2773
2774static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2775{
2776 return gt_tval_read(env, ri, GTIMER_HYP);
2777}
2778
2779static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2780 uint64_t value)
2781{
2782 gt_tval_write(env, ri, GTIMER_HYP, value);
2783}
2784
2785static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2786 uint64_t value)
2787{
2788 gt_ctl_write(env, ri, GTIMER_HYP, value);
2789}
2790
b4d3978c
PM
2791static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2792{
2793 gt_timer_reset(env, ri, GTIMER_SEC);
2794}
2795
2796static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2797 uint64_t value)
2798{
2799 gt_cval_write(env, ri, GTIMER_SEC, value);
2800}
2801
2802static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2803{
2804 return gt_tval_read(env, ri, GTIMER_SEC);
2805}
2806
2807static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2808 uint64_t value)
2809{
2810 gt_tval_write(env, ri, GTIMER_SEC, value);
2811}
2812
2813static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2814 uint64_t value)
2815{
2816 gt_ctl_write(env, ri, GTIMER_SEC, value);
2817}
2818
8c94b071
RH
2819static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2820{
2821 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2822}
2823
2824static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2825 uint64_t value)
2826{
2827 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2828}
2829
2830static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2831{
2832 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2833}
2834
2835static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2836 uint64_t value)
2837{
2838 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2839}
2840
2841static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2842 uint64_t value)
2843{
2844 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2845}
2846
55d284af
PM
2847void arm_gt_ptimer_cb(void *opaque)
2848{
2849 ARMCPU *cpu = opaque;
2850
2851 gt_recalc_timer(cpu, GTIMER_PHYS);
2852}
2853
2854void arm_gt_vtimer_cb(void *opaque)
2855{
2856 ARMCPU *cpu = opaque;
2857
2858 gt_recalc_timer(cpu, GTIMER_VIRT);
2859}
2860
b0e66d95
EI
2861void arm_gt_htimer_cb(void *opaque)
2862{
2863 ARMCPU *cpu = opaque;
2864
2865 gt_recalc_timer(cpu, GTIMER_HYP);
2866}
2867
b4d3978c
PM
2868void arm_gt_stimer_cb(void *opaque)
2869{
2870 ARMCPU *cpu = opaque;
2871
2872 gt_recalc_timer(cpu, GTIMER_SEC);
2873}
2874
8c94b071
RH
2875void arm_gt_hvtimer_cb(void *opaque)
2876{
2877 ARMCPU *cpu = opaque;
2878
2879 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2880}
2881
96eec6b2
AJ
2882static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2883{
2884 ARMCPU *cpu = env_archcpu(env);
2885
2886 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2887}
2888
55d284af
PM
2889static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2890 /* Note that CNTFRQ is purely reads-as-written for the benefit
2891 * of software; writing it doesn't actually change the timer frequency.
2892 * Our reset value matches the fixed frequency we implement the timer at.
2893 */
2894 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2895 .type = ARM_CP_ALIAS,
a7adc4b7
PM
2896 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2897 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
2898 },
2899 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2900 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2901 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af 2902 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
96eec6b2 2903 .resetfn = arm_gt_cntfrq_reset,
55d284af
PM
2904 },
2905 /* overall control: mostly access permissions */
a7adc4b7
PM
2906 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2907 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
2908 .access = PL1_RW,
2909 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2910 .resetvalue = 0,
2911 },
2912 /* per-timer control */
2913 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 2914 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 2915 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
2916 .accessfn = gt_ptimer_access,
2917 .fieldoffset = offsetoflow32(CPUARMState,
2918 cp15.c14_timer[GTIMER_PHYS].ctl),
bb5972e4
RH
2919 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2920 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7 2921 },
9c513e78 2922 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
2923 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2924 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 2925 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
9ff9dd3c
PM
2926 .accessfn = gt_ptimer_access,
2927 .fieldoffset = offsetoflow32(CPUARMState,
2928 cp15.c14_timer[GTIMER_SEC].ctl),
2929 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2930 },
a7adc4b7
PM
2931 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2932 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
daf1dc5f 2933 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 2934 .accessfn = gt_ptimer_access,
55d284af
PM
2935 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2936 .resetvalue = 0,
bb5972e4
RH
2937 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2938 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2939 },
2940 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
daf1dc5f 2941 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
2942 .accessfn = gt_vtimer_access,
2943 .fieldoffset = offsetoflow32(CPUARMState,
2944 cp15.c14_timer[GTIMER_VIRT].ctl),
bb5972e4
RH
2945 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2946 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
2947 },
2948 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2949 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
daf1dc5f 2950 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 2951 .accessfn = gt_vtimer_access,
55d284af
PM
2952 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2953 .resetvalue = 0,
bb5972e4
RH
2954 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2955 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2956 },
2957 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2958 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 2959 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 2960 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 2961 .accessfn = gt_ptimer_access,
bb5972e4 2962 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
55d284af 2963 },
9c513e78 2964 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
2965 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2966 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 2967 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
9ff9dd3c
PM
2968 .accessfn = gt_ptimer_access,
2969 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2970 },
a7adc4b7
PM
2971 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2972 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
daf1dc5f 2973 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 2974 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
bb5972e4 2975 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
a7adc4b7 2976 },
55d284af 2977 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
daf1dc5f 2978 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 2979 .accessfn = gt_vtimer_access,
bb5972e4 2980 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
55d284af 2981 },
a7adc4b7
PM
2982 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2983 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
daf1dc5f 2984 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 2985 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
bb5972e4 2986 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
a7adc4b7 2987 },
55d284af
PM
2988 /* The counter itself */
2989 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 2990 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 2991 .accessfn = gt_pct_access,
a7adc4b7
PM
2992 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
2993 },
2994 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
2995 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 2996 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 2997 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
2998 },
2999 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 3000 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3001 .accessfn = gt_vct_access,
edac4d8a 3002 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
3003 },
3004 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3005 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 3006 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3007 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
3008 },
3009 /* Comparison value, indicating when the timer goes off */
3010 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3011 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3012 .access = PL0_RW,
7a0e58fa 3013 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3014 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 3015 .accessfn = gt_ptimer_access,
bb5972e4
RH
3016 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3017 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7 3018 },
9c513e78 3019 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3020 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3021 .access = PL0_RW,
9ff9dd3c
PM
3022 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3023 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3024 .accessfn = gt_ptimer_access,
3025 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3026 },
a7adc4b7
PM
3027 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3028 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
daf1dc5f 3029 .access = PL0_RW,
a7adc4b7
PM
3030 .type = ARM_CP_IO,
3031 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 3032 .resetvalue = 0, .accessfn = gt_ptimer_access,
bb5972e4
RH
3033 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3034 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
55d284af
PM
3035 },
3036 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
daf1dc5f 3037 .access = PL0_RW,
7a0e58fa 3038 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3039 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 3040 .accessfn = gt_vtimer_access,
bb5972e4
RH
3041 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3042 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
3043 },
3044 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3045 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
daf1dc5f 3046 .access = PL0_RW,
a7adc4b7
PM
3047 .type = ARM_CP_IO,
3048 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3049 .resetvalue = 0, .accessfn = gt_vtimer_access,
bb5972e4
RH
3050 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3051 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
55d284af 3052 },
b4d3978c
PM
3053 /* Secure timer -- this is actually restricted to only EL3
3054 * and configurably Secure-EL1 via the accessfn.
3055 */
3056 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3057 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3058 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3059 .accessfn = gt_stimer_access,
3060 .readfn = gt_sec_tval_read,
3061 .writefn = gt_sec_tval_write,
3062 .resetfn = gt_sec_timer_reset,
3063 },
3064 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3065 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3066 .type = ARM_CP_IO, .access = PL1_RW,
3067 .accessfn = gt_stimer_access,
3068 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3069 .resetvalue = 0,
3070 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3071 },
3072 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3073 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3074 .type = ARM_CP_IO, .access = PL1_RW,
3075 .accessfn = gt_stimer_access,
3076 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3077 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3078 },
55d284af
PM
3079};
3080
bb5972e4
RH
3081static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3082 bool isread)
3083{
3084 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3085 return CP_ACCESS_TRAP;
3086 }
3087 return CP_ACCESS_OK;
3088}
3089
55d284af 3090#else
26c4a83b
AB
3091
3092/* In user-mode most of the generic timer registers are inaccessible
3093 * however modern kernels (4.12+) allow access to cntvct_el0
55d284af 3094 */
26c4a83b
AB
3095
3096static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3097{
7def8754
AJ
3098 ARMCPU *cpu = env_archcpu(env);
3099
26c4a83b
AB
3100 /* Currently we have no support for QEMUTimer in linux-user so we
3101 * can't call gt_get_countervalue(env), instead we directly
3102 * call the lower level functions.
3103 */
7def8754 3104 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
26c4a83b
AB
3105}
3106
6cc7a3ae 3107static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
26c4a83b
AB
3108 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3109 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3110 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3111 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3112 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3113 },
3114 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3115 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3116 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3117 .readfn = gt_virt_cnt_read,
3118 },
6cc7a3ae
PM
3119};
3120
55d284af
PM
3121#endif
3122
c4241c7d 3123static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 3124{
891a2fe7 3125 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 3126 raw_write(env, ri, value);
891a2fe7 3127 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 3128 raw_write(env, ri, value & 0xfffff6ff);
4a501606 3129 } else {
8d5c773e 3130 raw_write(env, ri, value & 0xfffff1ff);
4a501606 3131 }
4a501606
PM
3132}
3133
3134#ifndef CONFIG_USER_ONLY
3135/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 3136
3f208fd7
PM
3137static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3138 bool isread)
92611c00
PM
3139{
3140 if (ri->opc2 & 4) {
926c1b97 3141 /* The ATS12NSO* operations must trap to EL3 or EL2 if executed in
87562e4f
PM
3142 * Secure EL1 (which can only happen if EL3 is AArch64).
3143 * They are simply UNDEF if executed from NS EL1.
3144 * They function normally from EL2 or EL3.
92611c00 3145 */
87562e4f
PM
3146 if (arm_current_el(env) == 1) {
3147 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
3148 if (env->cp15.scr_el3 & SCR_EEL2) {
3149 return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3150 }
87562e4f
PM
3151 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3152 }
3153 return CP_ACCESS_TRAP_UNCATEGORIZED;
3154 }
92611c00
PM
3155 }
3156 return CP_ACCESS_OK;
3157}
3158
9fb005b0 3159#ifdef CONFIG_TCG
060e8a48 3160static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
03ae85f8 3161 MMUAccessType access_type, ARMMMUIdx mmu_idx)
4a501606 3162{
a8170e5e 3163 hwaddr phys_addr;
4a501606
PM
3164 target_ulong page_size;
3165 int prot;
b7cc4e82 3166 bool ret;
01c097f7 3167 uint64_t par64;
1313e2d7 3168 bool format64 = false;
8bf5b6a9 3169 MemTxAttrs attrs = {};
e14b5a23 3170 ARMMMUFaultInfo fi = {};
5b2d261d 3171 ARMCacheAttrs cacheattrs = {};
4a501606 3172
5b2d261d 3173 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
bc52bfeb 3174 &prot, &page_size, &fi, &cacheattrs);
1313e2d7 3175
0710b2fa
PM
3176 if (ret) {
3177 /*
3178 * Some kinds of translation fault must cause exceptions rather
3179 * than being reported in the PAR.
3180 */
3181 int current_el = arm_current_el(env);
3182 int target_el;
3183 uint32_t syn, fsr, fsc;
3184 bool take_exc = false;
3185
b1a10c86 3186 if (fi.s1ptw && current_el == 1
fee7aa46 3187 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
0710b2fa
PM
3188 /*
3189 * Synchronous stage 2 fault on an access made as part of the
3190 * translation table walk for AT S1E0* or AT S1E1* insn
3191 * executed from NS EL1. If this is a synchronous external abort
3192 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3193 * to EL3. Otherwise the fault is taken as an exception to EL2,
3194 * and HPFAR_EL2 holds the faulting IPA.
3195 */
3196 if (fi.type == ARMFault_SyncExternalOnWalk &&
3197 (env->cp15.scr_el3 & SCR_EA)) {
3198 target_el = 3;
3199 } else {
3200 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
9861248f
RDC
3201 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3202 env->cp15.hpfar_el2 |= HPFAR_NS;
3203 }
0710b2fa
PM
3204 target_el = 2;
3205 }
3206 take_exc = true;
3207 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3208 /*
3209 * Synchronous external aborts during a translation table walk
3210 * are taken as Data Abort exceptions.
3211 */
3212 if (fi.stage2) {
3213 if (current_el == 3) {
3214 target_el = 3;
3215 } else {
3216 target_el = 2;
3217 }
3218 } else {
3219 target_el = exception_target_el(env);
3220 }
3221 take_exc = true;
3222 }
3223
3224 if (take_exc) {
3225 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3226 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3227 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3228 fsr = arm_fi_to_lfsc(&fi);
3229 fsc = extract32(fsr, 0, 6);
3230 } else {
3231 fsr = arm_fi_to_sfsc(&fi);
3232 fsc = 0x3f;
3233 }
3234 /*
3235 * Report exception with ESR indicating a fault due to a
3236 * translation table walk for a cache maintenance instruction.
3237 */
e24fd076 3238 syn = syn_data_abort_no_iss(current_el == target_el, 0,
0710b2fa
PM
3239 fi.ea, 1, fi.s1ptw, 1, fsc);
3240 env->exception.vaddress = value;
3241 env->exception.fsr = fsr;
3242 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3243 }
3244 }
3245
1313e2d7
EI
3246 if (is_a64(env)) {
3247 format64 = true;
3248 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3249 /*
3250 * ATS1Cxx:
3251 * * TTBCR.EAE determines whether the result is returned using the
3252 * 32-bit or the 64-bit PAR format
3253 * * Instructions executed in Hyp mode always use the 64bit format
3254 *
3255 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3256 * * The Non-secure TTBCR.EAE bit is set to 1
3257 * * The implementation includes EL2, and the value of HCR.VM is 1
3258 *
9d1bab33
PM
3259 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3260 *
23463e0e 3261 * ATS1Hx always uses the 64bit format.
1313e2d7
EI
3262 */
3263 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3264
3265 if (arm_feature(env, ARM_FEATURE_EL2)) {
452ef8cb
RH
3266 if (mmu_idx == ARMMMUIdx_E10_0 ||
3267 mmu_idx == ARMMMUIdx_E10_1 ||
3268 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9d1bab33 3269 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
1313e2d7
EI
3270 } else {
3271 format64 |= arm_current_el(env) == 2;
3272 }
3273 }
3274 }
3275
3276 if (format64) {
5efe9ed4 3277 /* Create a 64-bit PAR */
01c097f7 3278 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 3279 if (!ret) {
702a9357 3280 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
3281 if (!attrs.secure) {
3282 par64 |= (1 << 9); /* NS */
3283 }
5b2d261d
AB
3284 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3285 par64 |= cacheattrs.shareability << 7; /* SH */
4a501606 3286 } else {
5efe9ed4
PM
3287 uint32_t fsr = arm_fi_to_lfsc(&fi);
3288
702a9357 3289 par64 |= 1; /* F */
b7cc4e82 3290 par64 |= (fsr & 0x3f) << 1; /* FS */
0f7b791b
PM
3291 if (fi.stage2) {
3292 par64 |= (1 << 9); /* S */
3293 }
3294 if (fi.s1ptw) {
3295 par64 |= (1 << 8); /* PTW */
3296 }
4a501606
PM
3297 }
3298 } else {
b7cc4e82 3299 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
3300 * translation table format (with WnR always clear).
3301 * Convert it to a 32-bit PAR.
3302 */
b7cc4e82 3303 if (!ret) {
702a9357
PM
3304 /* We do not set any attribute bits in the PAR */
3305 if (page_size == (1 << 24)
3306 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 3307 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 3308 } else {
01c097f7 3309 par64 = phys_addr & 0xfffff000;
702a9357 3310 }
8bf5b6a9
PM
3311 if (!attrs.secure) {
3312 par64 |= (1 << 9); /* NS */
3313 }
702a9357 3314 } else {
5efe9ed4
PM
3315 uint32_t fsr = arm_fi_to_sfsc(&fi);
3316
b7cc4e82
PC
3317 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3318 ((fsr & 0xf) << 1) | 1;
702a9357 3319 }
4a501606 3320 }
060e8a48
PM
3321 return par64;
3322}
9fb005b0 3323#endif /* CONFIG_TCG */
060e8a48
PM
3324
3325static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3326{
9fb005b0 3327#ifdef CONFIG_TCG
03ae85f8 3328 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 3329 uint64_t par64;
d3649702
PM
3330 ARMMMUIdx mmu_idx;
3331 int el = arm_current_el(env);
3332 bool secure = arm_is_secure_below_el3(env);
060e8a48 3333
d3649702
PM
3334 switch (ri->opc2 & 6) {
3335 case 0:
04b07d29 3336 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
d3649702
PM
3337 switch (el) {
3338 case 3:
127b2b08 3339 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3340 break;
3341 case 2:
b6ad6062 3342 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
04b07d29 3343 /* fall through */
d3649702 3344 case 1:
04b07d29 3345 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
b1a10c86 3346 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
04b07d29
RH
3347 : ARMMMUIdx_Stage1_E1_PAN);
3348 } else {
b1a10c86 3349 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
04b07d29 3350 }
d3649702
PM
3351 break;
3352 default:
3353 g_assert_not_reached();
3354 }
3355 break;
3356 case 2:
3357 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3358 switch (el) {
3359 case 3:
fba37aed 3360 mmu_idx = ARMMMUIdx_SE10_0;
d3649702
PM
3361 break;
3362 case 2:
b1a10c86 3363 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
2859d7b5 3364 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3365 break;
3366 case 1:
b1a10c86 3367 mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3368 break;
3369 default:
3370 g_assert_not_reached();
3371 }
3372 break;
3373 case 4:
3374 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
01b98b68 3375 mmu_idx = ARMMMUIdx_E10_1;
d3649702
PM
3376 break;
3377 case 6:
3378 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
01b98b68 3379 mmu_idx = ARMMMUIdx_E10_0;
d3649702
PM
3380 break;
3381 default:
3382 g_assert_not_reached();
3383 }
3384
3385 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
3386
3387 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3388#else
3389 /* Handled by hardware accelerator. */
3390 g_assert_not_reached();
3391#endif /* CONFIG_TCG */
4a501606 3392}
060e8a48 3393
14db7fe0
PM
3394static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3395 uint64_t value)
3396{
9fb005b0 3397#ifdef CONFIG_TCG
03ae85f8 3398 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
3399 uint64_t par64;
3400
e013b741 3401 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
14db7fe0
PM
3402
3403 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3404#else
3405 /* Handled by hardware accelerator. */
3406 g_assert_not_reached();
3407#endif /* CONFIG_TCG */
14db7fe0
PM
3408}
3409
3f208fd7
PM
3410static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3411 bool isread)
2a47df95 3412{
926c1b97
RDC
3413 if (arm_current_el(env) == 3 &&
3414 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
2a47df95
PM
3415 return CP_ACCESS_TRAP;
3416 }
3417 return CP_ACCESS_OK;
3418}
3419
060e8a48
PM
3420static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3421 uint64_t value)
3422{
9fb005b0 3423#ifdef CONFIG_TCG
03ae85f8 3424 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
3425 ARMMMUIdx mmu_idx;
3426 int secure = arm_is_secure_below_el3(env);
3427
3428 switch (ri->opc2 & 6) {
3429 case 0:
3430 switch (ri->opc1) {
04b07d29
RH
3431 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3432 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
b1a10c86 3433 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
04b07d29
RH
3434 : ARMMMUIdx_Stage1_E1_PAN);
3435 } else {
b1a10c86 3436 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
04b07d29 3437 }
d3649702
PM
3438 break;
3439 case 4: /* AT S1E2R, AT S1E2W */
b6ad6062 3440 mmu_idx = secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2;
d3649702
PM
3441 break;
3442 case 6: /* AT S1E3R, AT S1E3W */
127b2b08 3443 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3444 break;
3445 default:
3446 g_assert_not_reached();
3447 }
3448 break;
3449 case 2: /* AT S1E0R, AT S1E0W */
b1a10c86 3450 mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3451 break;
3452 case 4: /* AT S12E1R, AT S12E1W */
fba37aed 3453 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
d3649702
PM
3454 break;
3455 case 6: /* AT S12E0R, AT S12E0W */
fba37aed 3456 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
d3649702
PM
3457 break;
3458 default:
3459 g_assert_not_reached();
3460 }
060e8a48 3461
d3649702 3462 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
9fb005b0
PMD
3463#else
3464 /* Handled by hardware accelerator. */
3465 g_assert_not_reached();
3466#endif /* CONFIG_TCG */
060e8a48 3467}
4a501606
PM
3468#endif
3469
3470static const ARMCPRegInfo vapa_cp_reginfo[] = {
3471 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3472 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
3473 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3474 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
3475 .writefn = par_write },
3476#ifndef CONFIG_USER_ONLY
87562e4f 3477 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 3478 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 3479 .access = PL1_W, .accessfn = ats_access,
0710b2fa 3480 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
4a501606 3481#endif
4a501606
PM
3482};
3483
18032bec
PM
3484/* Return basic MPU access permission bits. */
3485static uint32_t simple_mpu_ap_bits(uint32_t val)
3486{
3487 uint32_t ret;
3488 uint32_t mask;
3489 int i;
3490 ret = 0;
3491 mask = 3;
3492 for (i = 0; i < 16; i += 2) {
3493 ret |= (val >> i) & mask;
3494 mask <<= 2;
3495 }
3496 return ret;
3497}
3498
3499/* Pad basic MPU access permission bits to extended format. */
3500static uint32_t extended_mpu_ap_bits(uint32_t val)
3501{
3502 uint32_t ret;
3503 uint32_t mask;
3504 int i;
3505 ret = 0;
3506 mask = 3;
3507 for (i = 0; i < 16; i += 2) {
3508 ret |= (val & mask) << i;
3509 mask <<= 2;
3510 }
3511 return ret;
3512}
3513
c4241c7d
PM
3514static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3515 uint64_t value)
18032bec 3516{
7e09797c 3517 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
3518}
3519
c4241c7d 3520static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3521{
7e09797c 3522 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
3523}
3524
c4241c7d
PM
3525static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3526 uint64_t value)
18032bec 3527{
7e09797c 3528 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
3529}
3530
c4241c7d 3531static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3532{
7e09797c 3533 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
3534}
3535
6cb0b013
PC
3536static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3537{
3538 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3539
3540 if (!u32p) {
3541 return 0;
3542 }
3543
1bc04a88 3544 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
3545 return *u32p;
3546}
3547
3548static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3549 uint64_t value)
3550{
2fc0cc0e 3551 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3552 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3553
3554 if (!u32p) {
3555 return;
3556 }
3557
1bc04a88 3558 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 3559 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
3560 *u32p = value;
3561}
3562
6cb0b013
PC
3563static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3564 uint64_t value)
3565{
2fc0cc0e 3566 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3567 uint32_t nrgs = cpu->pmsav7_dregion;
3568
3569 if (value >= nrgs) {
3570 qemu_log_mask(LOG_GUEST_ERROR,
3571 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3572 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3573 return;
3574 }
3575
3576 raw_write(env, ri, value);
3577}
3578
3579static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
3580 /* Reset for all these registers is handled in arm_cpu_reset(),
3581 * because the PMSAv7 is also used by M-profile CPUs, which do
3582 * not register cpregs but still need the state to be reset.
3583 */
6cb0b013
PC
3584 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3585 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3586 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
3587 .readfn = pmsav7_read, .writefn = pmsav7_write,
3588 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3589 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3590 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3591 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
3592 .readfn = pmsav7_read, .writefn = pmsav7_write,
3593 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3594 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3595 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3596 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
3597 .readfn = pmsav7_read, .writefn = pmsav7_write,
3598 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3599 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3600 .access = PL1_RW,
1bc04a88 3601 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
3602 .writefn = pmsav7_rgnr_write,
3603 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3604};
3605
18032bec
PM
3606static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3607 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3608 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3609 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
3610 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3611 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 3612 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3613 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
3614 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3615 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3616 .access = PL1_RW,
7e09797c
PM
3617 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3618 .resetvalue = 0, },
18032bec
PM
3619 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3620 .access = PL1_RW,
7e09797c
PM
3621 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3622 .resetvalue = 0, },
ecce5c3c
PM
3623 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3624 .access = PL1_RW,
3625 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3626 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3627 .access = PL1_RW,
3628 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 3629 /* Protection region base and size registers */
e508a92b
PM
3630 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3631 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3632 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3633 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3634 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3635 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3636 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3637 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3638 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3639 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3640 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3641 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3642 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3643 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3644 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3645 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3646 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3647 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3648 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3649 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3650 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3651 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3652 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3653 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
3654};
3655
c4241c7d
PM
3656static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3657 uint64_t value)
ecce5c3c 3658{
11f136ee 3659 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
3660 int maskshift = extract32(value, 0, 3);
3661
e389be16
FA
3662 if (!arm_feature(env, ARM_FEATURE_V8)) {
3663 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3664 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3665 * using Long-desciptor translation table format */
3666 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3667 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3668 /* In an implementation that includes the Security Extensions
3669 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3670 * Short-descriptor translation table format.
3671 */
3672 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3673 } else {
3674 value &= TTBCR_N;
3675 }
e42c4db3 3676 }
e389be16 3677
b6af0975 3678 /* Update the masks corresponding to the TCR bank being written
11f136ee 3679 * Note that we always calculate mask and base_mask, but
e42c4db3 3680 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
3681 * for long-descriptor tables the TCR fields are used differently
3682 * and the mask and base_mask values are meaningless.
e42c4db3 3683 */
11f136ee
FA
3684 tcr->raw_tcr = value;
3685 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3686 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
3687}
3688
c4241c7d
PM
3689static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3690 uint64_t value)
d4e6df63 3691{
2fc0cc0e 3692 ARMCPU *cpu = env_archcpu(env);
ab638a32 3693 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3694
d4e6df63
PM
3695 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3696 /* With LPAE the TTBCR could result in a change of ASID
3697 * via the TTBCR.A1 bit, so do a TLB flush.
3698 */
d10eb08f 3699 tlb_flush(CPU(cpu));
d4e6df63 3700 }
ab638a32
RH
3701 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3702 value = deposit64(tcr->raw_tcr, 0, 32, value);
c4241c7d 3703 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
3704}
3705
ecce5c3c
PM
3706static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3707{
11f136ee
FA
3708 TCR *tcr = raw_ptr(env, ri);
3709
3710 /* Reset both the TCR as well as the masks corresponding to the bank of
3711 * the TCR being reset.
3712 */
3713 tcr->raw_tcr = 0;
3714 tcr->mask = 0;
3715 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
3716}
3717
d06dc933 3718static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
cb2e37df
PM
3719 uint64_t value)
3720{
2fc0cc0e 3721 ARMCPU *cpu = env_archcpu(env);
11f136ee 3722 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3723
cb2e37df 3724 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 3725 tlb_flush(CPU(cpu));
11f136ee 3726 tcr->raw_tcr = value;
cb2e37df
PM
3727}
3728
327ed10f
PM
3729static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3730 uint64_t value)
3731{
93f379b0
RH
3732 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3733 if (cpreg_field_is_64bit(ri) &&
3734 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
2fc0cc0e 3735 ARMCPU *cpu = env_archcpu(env);
d10eb08f 3736 tlb_flush(CPU(cpu));
327ed10f
PM
3737 }
3738 raw_write(env, ri, value);
3739}
3740
ed30da8e
RH
3741static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3742 uint64_t value)
3743{
d06dc933
RH
3744 /*
3745 * If we are running with E2&0 regime, then an ASID is active.
3746 * Flush if that might be changing. Note we're not checking
3747 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3748 * holds the active ASID, only checking the field that might.
3749 */
3750 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3751 (arm_hcr_el2_eff(env) & HCR_E2H)) {
b6ad6062
RDC
3752 uint16_t mask = ARMMMUIdxBit_E20_2 |
3753 ARMMMUIdxBit_E20_2_PAN |
3754 ARMMMUIdxBit_E20_0;
3755
3756 if (arm_is_secure_below_el3(env)) {
3757 mask >>= ARM_MMU_IDX_A_NS;
3758 }
3759
3760 tlb_flush_by_mmuidx(env_cpu(env), mask);
d06dc933 3761 }
ed30da8e
RH
3762 raw_write(env, ri, value);
3763}
3764
b698e9cf
EI
3765static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3766 uint64_t value)
3767{
2fc0cc0e 3768 ARMCPU *cpu = env_archcpu(env);
b698e9cf
EI
3769 CPUState *cs = CPU(cpu);
3770
97fa9350
RH
3771 /*
3772 * A change in VMID to the stage2 page table (Stage2) invalidates
3773 * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3774 */
b698e9cf 3775 if (raw_read(env, ri) != value) {
c4f060e8
RDC
3776 uint16_t mask = ARMMMUIdxBit_E10_1 |
3777 ARMMMUIdxBit_E10_1_PAN |
3778 ARMMMUIdxBit_E10_0;
3779
3780 if (arm_is_secure_below_el3(env)) {
3781 mask >>= ARM_MMU_IDX_A_NS;
3782 }
3783
3784 tlb_flush_by_mmuidx(cs, mask);
b698e9cf
EI
3785 raw_write(env, ri, value);
3786 }
3787}
3788
8e5d75c9 3789static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 3790 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218 3791 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4a7e2d73 3792 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 3793 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 3794 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
84929218 3795 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
88ca1c2d
FA
3796 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3797 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9 3798 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
84929218 3799 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
8e5d75c9
PC
3800 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3801 offsetof(CPUARMState, cp15.dfar_ns) } },
3802 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3803 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218
RH
3804 .access = PL1_RW, .accessfn = access_tvm_trvm,
3805 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
8e5d75c9 3806 .resetvalue = 0, },
8e5d75c9
PC
3807};
3808
3809static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
3810 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3811 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
84929218 3812 .access = PL1_RW, .accessfn = access_tvm_trvm,
d81c519c 3813 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 3814 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 3815 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
84929218
RH
3816 .access = PL1_RW, .accessfn = access_tvm_trvm,
3817 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
3818 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3819 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 3820 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 3821 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
84929218
RH
3822 .access = PL1_RW, .accessfn = access_tvm_trvm,
3823 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
3824 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3825 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
3826 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3827 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
3828 .access = PL1_RW, .accessfn = access_tvm_trvm,
3829 .writefn = vmsa_tcr_el12_write,
cb2e37df 3830 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 3831 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 3832 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
3833 .access = PL1_RW, .accessfn = access_tvm_trvm,
3834 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 3835 .raw_writefn = vmsa_ttbcr_raw_write,
d102058e
RH
3836 /* No offsetoflow32 -- pass the entire TCR to writefn/raw_writefn. */
3837 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.tcr_el[3]),
3838 offsetof(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
3839};
3840
ab638a32
RH
3841/* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3842 * qemu tlbs nor adjusting cached masks.
3843 */
3844static const ARMCPRegInfo ttbcr2_reginfo = {
3845 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
84929218
RH
3846 .access = PL1_RW, .accessfn = access_tvm_trvm,
3847 .type = ARM_CP_ALIAS,
d102058e
RH
3848 .bank_fieldoffsets = {
3849 offsetofhigh32(CPUARMState, cp15.tcr_el[3].raw_tcr),
3850 offsetofhigh32(CPUARMState, cp15.tcr_el[1].raw_tcr),
3851 },
ab638a32
RH
3852};
3853
c4241c7d
PM
3854static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3855 uint64_t value)
1047b9d7
PM
3856{
3857 env->cp15.c15_ticonfig = value & 0xe7;
3858 /* The OS_TYPE bit in this register changes the reported CPUID! */
3859 env->cp15.c0_cpuid = (value & (1 << 5)) ?
3860 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
3861}
3862
c4241c7d
PM
3863static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3864 uint64_t value)
1047b9d7
PM
3865{
3866 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
3867}
3868
c4241c7d
PM
3869static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3870 uint64_t value)
1047b9d7
PM
3871{
3872 /* Wait-for-interrupt (deprecated) */
2fc0cc0e 3873 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
1047b9d7
PM
3874}
3875
c4241c7d
PM
3876static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3877 uint64_t value)
c4804214
PM
3878{
3879 /* On OMAP there are registers indicating the max/min index of dcache lines
3880 * containing a dirty line; cache flush operations have to reset these.
3881 */
3882 env->cp15.c15_i_max = 0x000;
3883 env->cp15.c15_i_min = 0xff0;
c4804214
PM
3884}
3885
18032bec
PM
3886static const ARMCPRegInfo omap_cp_reginfo[] = {
3887 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3888 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 3889 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 3890 .resetvalue = 0, },
1047b9d7
PM
3891 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3892 .access = PL1_RW, .type = ARM_CP_NOP },
3893 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3894 .access = PL1_RW,
3895 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3896 .writefn = omap_ticonfig_write },
3897 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3898 .access = PL1_RW,
3899 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3900 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3901 .access = PL1_RW, .resetvalue = 0xff0,
3902 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3903 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3904 .access = PL1_RW,
3905 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3906 .writefn = omap_threadid_write },
3907 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3908 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 3909 .type = ARM_CP_NO_RAW,
1047b9d7
PM
3910 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3911 /* TODO: Peripheral port remap register:
3912 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3913 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3914 * when MMU is off.
3915 */
c4804214 3916 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 3917 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 3918 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 3919 .writefn = omap_cachemaint_write },
34f90529
PM
3920 { .name = "C9", .cp = 15, .crn = 9,
3921 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3922 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
3923};
3924
c4241c7d
PM
3925static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3926 uint64_t value)
1047b9d7 3927{
c0f4af17 3928 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
3929}
3930
3931static const ARMCPRegInfo xscale_cp_reginfo[] = {
3932 { .name = "XSCALE_CPAR",
3933 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3934 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3935 .writefn = xscale_cpar_write, },
2771db27
PM
3936 { .name = "XSCALE_AUXCR",
3937 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3938 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3939 .resetvalue = 0, },
3b771579
PM
3940 /* XScale specific cache-lockdown: since we have no cache we NOP these
3941 * and hope the guest does not really rely on cache behaviour.
3942 */
3943 { .name = "XSCALE_LOCK_ICACHE_LINE",
3944 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3945 .access = PL1_W, .type = ARM_CP_NOP },
3946 { .name = "XSCALE_UNLOCK_ICACHE",
3947 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3948 .access = PL1_W, .type = ARM_CP_NOP },
3949 { .name = "XSCALE_DCACHE_LOCK",
3950 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3951 .access = PL1_RW, .type = ARM_CP_NOP },
3952 { .name = "XSCALE_UNLOCK_DCACHE",
3953 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3954 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
3955};
3956
3957static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3958 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3959 * implementation of this implementation-defined space.
3960 * Ideally this should eventually disappear in favour of actually
3961 * implementing the correct behaviour for all cores.
3962 */
3963 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3964 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 3965 .access = PL1_RW,
7a0e58fa 3966 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 3967 .resetvalue = 0 },
18032bec
PM
3968};
3969
c4804214
PM
3970static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3971 /* Cache status: RAZ because we have no cache so it's always clean */
3972 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 3973 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 3974 .resetvalue = 0 },
c4804214
PM
3975};
3976
3977static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
3978 /* We never have a a block transfer operation in progress */
3979 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 3980 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 3981 .resetvalue = 0 },
30b05bba
PM
3982 /* The cache ops themselves: these all NOP for QEMU */
3983 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
3984 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3985 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
3986 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3987 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
3988 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3989 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
3990 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3991 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
3992 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3993 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
3994 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
3995};
3996
3997static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
3998 /* The cache test-and-clean instructions always return (1 << 30)
3999 * to indicate that there are no dirty cache lines.
4000 */
4001 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 4002 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4003 .resetvalue = (1 << 30) },
c4804214 4004 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 4005 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4006 .resetvalue = (1 << 30) },
c4804214
PM
4007};
4008
34f90529
PM
4009static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4010 /* Ignore ReadBuffer accesses */
4011 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4012 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 4013 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 4014 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
4015};
4016
731de9e6
EI
4017static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4018{
731de9e6 4019 unsigned int cur_el = arm_current_el(env);
731de9e6 4020
e6ef0169 4021 if (arm_is_el2_enabled(env) && cur_el == 1) {
731de9e6
EI
4022 return env->cp15.vpidr_el2;
4023 }
4024 return raw_read(env, ri);
4025}
4026
06a7e647 4027static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 4028{
2fc0cc0e 4029 ARMCPU *cpu = env_archcpu(env);
eb5e1d3c
PF
4030 uint64_t mpidr = cpu->mp_affinity;
4031
81bdde9d 4032 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 4033 mpidr |= (1U << 31);
81bdde9d
PM
4034 /* Cores which are uniprocessor (non-coherent)
4035 * but still implement the MP extensions set
a8e81b31 4036 * bit 30. (For instance, Cortex-R5).
81bdde9d 4037 */
a8e81b31
PC
4038 if (cpu->mp_is_up) {
4039 mpidr |= (1u << 30);
4040 }
81bdde9d 4041 }
c4241c7d 4042 return mpidr;
81bdde9d
PM
4043}
4044
06a7e647
EI
4045static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4046{
f0d574d6 4047 unsigned int cur_el = arm_current_el(env);
f0d574d6 4048
e6ef0169 4049 if (arm_is_el2_enabled(env) && cur_el == 1) {
f0d574d6
EI
4050 return env->cp15.vmpidr_el2;
4051 }
06a7e647
EI
4052 return mpidr_read_val(env);
4053}
4054
7ac681cf 4055static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 4056 /* NOP AMAIR0/1 */
b0fe2427
PM
4057 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4058 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
84929218
RH
4059 .access = PL1_RW, .accessfn = access_tvm_trvm,
4060 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427 4061 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 4062 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
84929218
RH
4063 .access = PL1_RW, .accessfn = access_tvm_trvm,
4064 .type = ARM_CP_CONST, .resetvalue = 0 },
891a2fe7 4065 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
4066 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4067 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4068 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 4069 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
84929218
RH
4070 .access = PL1_RW, .accessfn = access_tvm_trvm,
4071 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4072 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4073 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 4074 .writefn = vmsa_ttbr_write, },
891a2fe7 4075 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
84929218
RH
4076 .access = PL1_RW, .accessfn = access_tvm_trvm,
4077 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4078 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4079 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 4080 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
4081};
4082
c4241c7d 4083static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4084{
c4241c7d 4085 return vfp_get_fpcr(env);
b0d2b7d0
PM
4086}
4087
c4241c7d
PM
4088static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4089 uint64_t value)
b0d2b7d0
PM
4090{
4091 vfp_set_fpcr(env, value);
b0d2b7d0
PM
4092}
4093
c4241c7d 4094static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4095{
c4241c7d 4096 return vfp_get_fpsr(env);
b0d2b7d0
PM
4097}
4098
c4241c7d
PM
4099static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4100 uint64_t value)
b0d2b7d0
PM
4101{
4102 vfp_set_fpsr(env, value);
b0d2b7d0
PM
4103}
4104
3f208fd7
PM
4105static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4106 bool isread)
c2b820fe 4107{
aaec1432 4108 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
c2b820fe
PM
4109 return CP_ACCESS_TRAP;
4110 }
4111 return CP_ACCESS_OK;
4112}
4113
4114static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4115 uint64_t value)
4116{
4117 env->daif = value & PSTATE_DAIF;
4118}
4119
220f508f
RH
4120static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4121{
4122 return env->pstate & PSTATE_PAN;
4123}
4124
4125static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4126 uint64_t value)
4127{
4128 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4129}
4130
4131static const ARMCPRegInfo pan_reginfo = {
4132 .name = "PAN", .state = ARM_CP_STATE_AA64,
4133 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4134 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4135 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4136};
4137
9eeb7a1c
RH
4138static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4139{
4140 return env->pstate & PSTATE_UAO;
4141}
4142
4143static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4144 uint64_t value)
4145{
4146 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4147}
4148
4149static const ARMCPRegInfo uao_reginfo = {
4150 .name = "UAO", .state = ARM_CP_STATE_AA64,
4151 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4152 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4153 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4154};
4155
dc8b1853
RC
4156static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4157{
4158 return env->pstate & PSTATE_DIT;
4159}
4160
4161static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4162 uint64_t value)
4163{
4164 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4165}
4166
4167static const ARMCPRegInfo dit_reginfo = {
4168 .name = "DIT", .state = ARM_CP_STATE_AA64,
4169 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4170 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4171 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4172};
4173
f2f68a78
RC
4174static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4175{
4176 return env->pstate & PSTATE_SSBS;
4177}
4178
4179static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4180 uint64_t value)
4181{
4182 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4183}
4184
4185static const ARMCPRegInfo ssbs_reginfo = {
4186 .name = "SSBS", .state = ARM_CP_STATE_AA64,
4187 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4188 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4189 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4190};
4191
38262d8a
RH
4192static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4193 const ARMCPRegInfo *ri,
4194 bool isread)
8af35c37 4195{
38262d8a
RH
4196 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4197 switch (arm_current_el(env)) {
4198 case 0:
4199 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4200 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4201 return CP_ACCESS_TRAP;
4202 }
4203 /* fall through */
4204 case 1:
4205 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4206 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4207 return CP_ACCESS_TRAP_EL2;
4208 }
4209 break;
8af35c37
PM
4210 }
4211 return CP_ACCESS_OK;
4212}
4213
38262d8a 4214static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
1bed4d2e
RH
4215 const ARMCPRegInfo *ri,
4216 bool isread)
4217{
38262d8a 4218 /* Cache invalidate/clean to Point of Unification... */
1bed4d2e
RH
4219 switch (arm_current_el(env)) {
4220 case 0:
4221 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4222 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4223 return CP_ACCESS_TRAP;
4224 }
4225 /* fall through */
4226 case 1:
38262d8a
RH
4227 /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set. */
4228 if (arm_hcr_el2_eff(env) & HCR_TPU) {
1bed4d2e
RH
4229 return CP_ACCESS_TRAP_EL2;
4230 }
4231 break;
4232 }
4233 return CP_ACCESS_OK;
4234}
4235
dbb1fb27
AB
4236/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4237 * Page D4-1736 (DDI0487A.b)
4238 */
4239
b7e0730d
RH
4240static int vae1_tlbmask(CPUARMState *env)
4241{
e04a5752 4242 uint64_t hcr = arm_hcr_el2_eff(env);
bc944d3a 4243 uint16_t mask;
e04a5752
RDC
4244
4245 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
bc944d3a
RDC
4246 mask = ARMMMUIdxBit_E20_2 |
4247 ARMMMUIdxBit_E20_2_PAN |
4248 ARMMMUIdxBit_E20_0;
b7e0730d 4249 } else {
bc944d3a 4250 mask = ARMMMUIdxBit_E10_1 |
452ef8cb
RH
4251 ARMMMUIdxBit_E10_1_PAN |
4252 ARMMMUIdxBit_E10_0;
b7e0730d 4253 }
bc944d3a
RDC
4254
4255 if (arm_is_secure_below_el3(env)) {
4256 mask >>= ARM_MMU_IDX_A_NS;
4257 }
4258
4259 return mask;
b7e0730d
RH
4260}
4261
ea04dce7
RH
4262/* Return 56 if TBI is enabled, 64 otherwise. */
4263static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4264 uint64_t addr)
4265{
4266 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
4267 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4268 int select = extract64(addr, 55, 1);
4269
4270 return (tbi >> select) & 1 ? 56 : 64;
4271}
4272
4273static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4274{
b6ad6062 4275 uint64_t hcr = arm_hcr_el2_eff(env);
ea04dce7
RH
4276 ARMMMUIdx mmu_idx;
4277
4278 /* Only the regime of the mmu_idx below is significant. */
b6ad6062 4279 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
ea04dce7
RH
4280 mmu_idx = ARMMMUIdx_E20_0;
4281 } else {
4282 mmu_idx = ARMMMUIdx_E10_0;
4283 }
b6ad6062
RDC
4284
4285 if (arm_is_secure_below_el3(env)) {
4286 mmu_idx &= ~ARM_MMU_IDX_A_NS;
4287 }
4288
ea04dce7
RH
4289 return tlbbits_for_regime(env, mmu_idx, addr);
4290}
4291
fd3ed969
PM
4292static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4293 uint64_t value)
168aa23b 4294{
29a0af61 4295 CPUState *cs = env_cpu(env);
b7e0730d 4296 int mask = vae1_tlbmask(env);
dbb1fb27 4297
b7e0730d 4298 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
168aa23b
PM
4299}
4300
b4ab8ce9
PM
4301static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4302 uint64_t value)
4303{
29a0af61 4304 CPUState *cs = env_cpu(env);
b7e0730d 4305 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4306
4307 if (tlb_force_broadcast(env)) {
527db2be
RH
4308 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4309 } else {
4310 tlb_flush_by_mmuidx(cs, mask);
b4ab8ce9 4311 }
b4ab8ce9
PM
4312}
4313
90c19cdf 4314static int alle1_tlbmask(CPUARMState *env)
168aa23b 4315{
90c19cdf
RH
4316 /*
4317 * Note that the 'ALL' scope must invalidate both stage 1 and
fd3ed969
PM
4318 * stage 2 translations, whereas most other scopes only invalidate
4319 * stage 1 translations.
4320 */
fd3ed969 4321 if (arm_is_secure_below_el3(env)) {
452ef8cb
RH
4322 return ARMMMUIdxBit_SE10_1 |
4323 ARMMMUIdxBit_SE10_1_PAN |
4324 ARMMMUIdxBit_SE10_0;
fd3ed969 4325 } else {
452ef8cb
RH
4326 return ARMMMUIdxBit_E10_1 |
4327 ARMMMUIdxBit_E10_1_PAN |
4328 ARMMMUIdxBit_E10_0;
fd3ed969 4329 }
168aa23b
PM
4330}
4331
85d0dc9f
RH
4332static int e2_tlbmask(CPUARMState *env)
4333{
b6ad6062
RDC
4334 if (arm_is_secure_below_el3(env)) {
4335 return ARMMMUIdxBit_SE20_0 |
4336 ARMMMUIdxBit_SE20_2 |
4337 ARMMMUIdxBit_SE20_2_PAN |
4338 ARMMMUIdxBit_SE2;
4339 } else {
4340 return ARMMMUIdxBit_E20_0 |
4341 ARMMMUIdxBit_E20_2 |
4342 ARMMMUIdxBit_E20_2_PAN |
4343 ARMMMUIdxBit_E2;
4344 }
85d0dc9f
RH
4345}
4346
90c19cdf
RH
4347static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4348 uint64_t value)
4349{
4350 CPUState *cs = env_cpu(env);
4351 int mask = alle1_tlbmask(env);
4352
4353 tlb_flush_by_mmuidx(cs, mask);
4354}
4355
fd3ed969 4356static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
4357 uint64_t value)
4358{
85d0dc9f
RH
4359 CPUState *cs = env_cpu(env);
4360 int mask = e2_tlbmask(env);
fd3ed969 4361
85d0dc9f 4362 tlb_flush_by_mmuidx(cs, mask);
fd3ed969
PM
4363}
4364
43efaa33
PM
4365static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4366 uint64_t value)
4367{
2fc0cc0e 4368 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4369 CPUState *cs = CPU(cpu);
4370
127b2b08 4371 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4372}
4373
fd3ed969
PM
4374static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4375 uint64_t value)
4376{
29a0af61 4377 CPUState *cs = env_cpu(env);
90c19cdf
RH
4378 int mask = alle1_tlbmask(env);
4379
4380 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
fa439fc5
PM
4381}
4382
2bfb9d75
PM
4383static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4384 uint64_t value)
4385{
29a0af61 4386 CPUState *cs = env_cpu(env);
85d0dc9f 4387 int mask = e2_tlbmask(env);
2bfb9d75 4388
85d0dc9f 4389 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
2bfb9d75
PM
4390}
4391
43efaa33
PM
4392static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4393 uint64_t value)
4394{
29a0af61 4395 CPUState *cs = env_cpu(env);
43efaa33 4396
127b2b08 4397 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4398}
4399
fd3ed969
PM
4400static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4401 uint64_t value)
fa439fc5 4402{
fd3ed969
PM
4403 /* Invalidate by VA, EL2
4404 * Currently handles both VAE2 and VALE2, since we don't support
4405 * flush-last-level-only.
4406 */
85d0dc9f
RH
4407 CPUState *cs = env_cpu(env);
4408 int mask = e2_tlbmask(env);
fd3ed969
PM
4409 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4410
85d0dc9f 4411 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
fd3ed969
PM
4412}
4413
43efaa33
PM
4414static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4415 uint64_t value)
4416{
4417 /* Invalidate by VA, EL3
4418 * Currently handles both VAE3 and VALE3, since we don't support
4419 * flush-last-level-only.
4420 */
2fc0cc0e 4421 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4422 CPUState *cs = CPU(cpu);
4423 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4424
127b2b08 4425 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
43efaa33
PM
4426}
4427
fd3ed969
PM
4428static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4429 uint64_t value)
4430{
90c19cdf
RH
4431 CPUState *cs = env_cpu(env);
4432 int mask = vae1_tlbmask(env);
fa439fc5 4433 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4434 int bits = vae1_tlbbits(env, pageaddr);
fa439fc5 4435
ea04dce7 4436 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
fa439fc5
PM
4437}
4438
b4ab8ce9
PM
4439static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4440 uint64_t value)
4441{
4442 /* Invalidate by VA, EL1&0 (AArch64 version).
4443 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4444 * since we don't support flush-for-specific-ASID-only or
4445 * flush-last-level-only.
4446 */
90c19cdf
RH
4447 CPUState *cs = env_cpu(env);
4448 int mask = vae1_tlbmask(env);
b4ab8ce9 4449 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4450 int bits = vae1_tlbbits(env, pageaddr);
b4ab8ce9
PM
4451
4452 if (tlb_force_broadcast(env)) {
ea04dce7 4453 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
527db2be 4454 } else {
ea04dce7 4455 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
b4ab8ce9 4456 }
b4ab8ce9
PM
4457}
4458
fd3ed969
PM
4459static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4460 uint64_t value)
fa439fc5 4461{
29a0af61 4462 CPUState *cs = env_cpu(env);
fd3ed969 4463 uint64_t pageaddr = sextract64(value << 12, 0, 56);
b6ad6062
RDC
4464 bool secure = arm_is_secure_below_el3(env);
4465 int mask = secure ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2;
eb849d8f 4466 int bits = tlbbits_for_regime(env, secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2,
b6ad6062 4467 pageaddr);
fa439fc5 4468
b6ad6062 4469 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
fa439fc5
PM
4470}
4471
43efaa33
PM
4472static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4473 uint64_t value)
4474{
29a0af61 4475 CPUState *cs = env_cpu(env);
43efaa33 4476 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4477 int bits = tlbbits_for_regime(env, ARMMMUIdx_SE3, pageaddr);
43efaa33 4478
ea04dce7
RH
4479 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4480 ARMMMUIdxBit_SE3, bits);
43efaa33
PM
4481}
4482
84940ed8 4483#ifdef TARGET_AARCH64
ab1cdb47
RH
4484typedef struct {
4485 uint64_t base;
84940ed8 4486 uint64_t length;
ab1cdb47
RH
4487} TLBIRange;
4488
4489static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4490 uint64_t value)
4491{
4492 unsigned int page_size_granule, page_shift, num, scale, exponent;
3974ff93
RH
4493 /* Extract one bit to represent the va selector in use. */
4494 uint64_t select = sextract64(value, 36, 1);
4495 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
ab1cdb47 4496 TLBIRange ret = { };
84940ed8 4497
84940ed8
RC
4498 page_size_granule = extract64(value, 46, 2);
4499
3974ff93
RH
4500 /* The granule encoded in value must match the granule in use. */
4501 if (page_size_granule != (param.using64k ? 3 : param.using16k ? 2 : 1)) {
4502 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
84940ed8 4503 page_size_granule);
ab1cdb47 4504 return ret;
84940ed8
RC
4505 }
4506
52a9f609 4507 page_shift = (page_size_granule - 1) * 2 + 12;
ab1cdb47
RH
4508 num = extract64(value, 39, 5);
4509 scale = extract64(value, 44, 2);
84940ed8 4510 exponent = (5 * scale) + 1;
84940ed8 4511
ab1cdb47 4512 ret.length = (num + 1) << (exponent + page_shift);
84940ed8 4513
3974ff93 4514 if (param.select) {
d976de21 4515 ret.base = sextract64(value, 0, 37);
84940ed8 4516 } else {
d976de21 4517 ret.base = extract64(value, 0, 37);
84940ed8 4518 }
ef56c242
RH
4519 if (param.ds) {
4520 /*
4521 * With DS=1, BaseADDR is always shifted 16 so that it is able
4522 * to address all 52 va bits. The input address is perforce
4523 * aligned on a 64k boundary regardless of translation granule.
4524 */
4525 page_shift = 16;
4526 }
d976de21 4527 ret.base <<= page_shift;
84940ed8 4528
ab1cdb47 4529 return ret;
84940ed8
RC
4530}
4531
4532static void do_rvae_write(CPUARMState *env, uint64_t value,
4533 int idxmap, bool synced)
4534{
4535 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
ab1cdb47 4536 TLBIRange range;
84940ed8
RC
4537 int bits;
4538
ab1cdb47
RH
4539 range = tlbi_aa64_get_range(env, one_idx, value);
4540 bits = tlbbits_for_regime(env, one_idx, range.base);
84940ed8
RC
4541
4542 if (synced) {
4543 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
ab1cdb47
RH
4544 range.base,
4545 range.length,
84940ed8
RC
4546 idxmap,
4547 bits);
4548 } else {
ab1cdb47
RH
4549 tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4550 range.length, idxmap, bits);
84940ed8
RC
4551 }
4552}
4553
4554static void tlbi_aa64_rvae1_write(CPUARMState *env,
4555 const ARMCPRegInfo *ri,
4556 uint64_t value)
4557{
4558 /*
4559 * Invalidate by VA range, EL1&0.
4560 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4561 * since we don't support flush-for-specific-ASID-only or
4562 * flush-last-level-only.
4563 */
4564
4565 do_rvae_write(env, value, vae1_tlbmask(env),
4566 tlb_force_broadcast(env));
4567}
4568
4569static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4570 const ARMCPRegInfo *ri,
4571 uint64_t value)
4572{
4573 /*
4574 * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4575 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4576 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4577 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4578 * shareable specific flushes.
4579 */
4580
4581 do_rvae_write(env, value, vae1_tlbmask(env), true);
4582}
4583
4584static int vae2_tlbmask(CPUARMState *env)
4585{
4586 return (arm_is_secure_below_el3(env)
4587 ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2);
4588}
4589
4590static void tlbi_aa64_rvae2_write(CPUARMState *env,
4591 const ARMCPRegInfo *ri,
4592 uint64_t value)
4593{
4594 /*
4595 * Invalidate by VA range, EL2.
4596 * Currently handles all of RVAE2 and RVALE2,
4597 * since we don't support flush-for-specific-ASID-only or
4598 * flush-last-level-only.
4599 */
4600
4601 do_rvae_write(env, value, vae2_tlbmask(env),
4602 tlb_force_broadcast(env));
4603
4604
4605}
4606
4607static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4608 const ARMCPRegInfo *ri,
4609 uint64_t value)
4610{
4611 /*
4612 * Invalidate by VA range, Inner/Outer Shareable, EL2.
4613 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4614 * since we don't support flush-for-specific-ASID-only,
4615 * flush-last-level-only or inner/outer shareable specific flushes.
4616 */
4617
4618 do_rvae_write(env, value, vae2_tlbmask(env), true);
4619
4620}
4621
4622static void tlbi_aa64_rvae3_write(CPUARMState *env,
4623 const ARMCPRegInfo *ri,
4624 uint64_t value)
4625{
4626 /*
4627 * Invalidate by VA range, EL3.
4628 * Currently handles all of RVAE3 and RVALE3,
4629 * since we don't support flush-for-specific-ASID-only or
4630 * flush-last-level-only.
4631 */
4632
4633 do_rvae_write(env, value, ARMMMUIdxBit_SE3,
4634 tlb_force_broadcast(env));
4635}
4636
4637static void tlbi_aa64_rvae3is_write(CPUARMState *env,
4638 const ARMCPRegInfo *ri,
4639 uint64_t value)
4640{
4641 /*
4642 * Invalidate by VA range, EL3, Inner/Outer Shareable.
4643 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
4644 * since we don't support flush-for-specific-ASID-only,
4645 * flush-last-level-only or inner/outer specific flushes.
4646 */
4647
4648 do_rvae_write(env, value, ARMMMUIdxBit_SE3, true);
4649}
4650#endif
4651
3f208fd7
PM
4652static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4653 bool isread)
aca3f40b 4654{
4351cb72
RH
4655 int cur_el = arm_current_el(env);
4656
4657 if (cur_el < 2) {
4658 uint64_t hcr = arm_hcr_el2_eff(env);
4659
4660 if (cur_el == 0) {
4661 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4662 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4663 return CP_ACCESS_TRAP_EL2;
4664 }
4665 } else {
4666 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4667 return CP_ACCESS_TRAP;
4668 }
4669 if (hcr & HCR_TDZ) {
4670 return CP_ACCESS_TRAP_EL2;
4671 }
4672 }
4673 } else if (hcr & HCR_TDZ) {
4674 return CP_ACCESS_TRAP_EL2;
4675 }
aca3f40b
PM
4676 }
4677 return CP_ACCESS_OK;
4678}
4679
4680static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4681{
2fc0cc0e 4682 ARMCPU *cpu = env_archcpu(env);
aca3f40b
PM
4683 int dzp_bit = 1 << 4;
4684
4685 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 4686 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
4687 dzp_bit = 0;
4688 }
4689 return cpu->dcz_blocksize | dzp_bit;
4690}
4691
3f208fd7
PM
4692static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4693 bool isread)
f502cfc2 4694{
cdcf1405 4695 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
4696 /* Access to SP_EL0 is undefined if it's being used as
4697 * the stack pointer.
4698 */
4699 return CP_ACCESS_TRAP_UNCATEGORIZED;
4700 }
4701 return CP_ACCESS_OK;
4702}
4703
4704static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4705{
4706 return env->pstate & PSTATE_SP;
4707}
4708
4709static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4710{
4711 update_spsel(env, val);
4712}
4713
137feaa9
FA
4714static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4715 uint64_t value)
4716{
2fc0cc0e 4717 ARMCPU *cpu = env_archcpu(env);
137feaa9 4718
f00faf13
RH
4719 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4720 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4721 value &= ~SCTLR_M;
4722 }
4723
4724 /* ??? Lots of these bits are not implemented. */
4725
4726 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4727 if (ri->opc1 == 6) { /* SCTLR_EL3 */
4728 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4729 } else {
4730 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4731 SCTLR_ATA0 | SCTLR_ATA);
4732 }
4733 }
4734
137feaa9
FA
4735 if (raw_read(env, ri) == value) {
4736 /* Skip the TLB flush if nothing actually changed; Linux likes
4737 * to do a lot of pointless SCTLR writes.
4738 */
4739 return;
4740 }
4741
4742 raw_write(env, ri, value);
f00faf13 4743
137feaa9 4744 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 4745 tlb_flush(CPU(cpu));
2e5dcf36
RH
4746
4747 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4748 /*
4749 * Normally we would always end the TB on an SCTLR write; see the
4750 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4751 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4752 * of hflags from the translator, so do it here.
4753 */
4754 arm_rebuild_hflags(env);
4755 }
137feaa9
FA
4756}
4757
a8d64e73
PM
4758static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4759 uint64_t value)
4760{
4761 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4762}
4763
b0d2b7d0
PM
4764static const ARMCPRegInfo v8_cp_reginfo[] = {
4765 /* Minimal set of EL0-visible registers. This will need to be expanded
4766 * significantly for system emulation of AArch64 CPUs.
4767 */
4768 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4769 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4770 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
4771 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4772 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 4773 .type = ARM_CP_NO_RAW,
c2b820fe
PM
4774 .access = PL0_RW, .accessfn = aa64_daif_access,
4775 .fieldoffset = offsetof(CPUARMState, daif),
4776 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
4777 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4778 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 4779 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4780 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
4781 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4782 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 4783 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4784 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
4785 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4786 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 4787 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
4788 .readfn = aa64_dczid_read },
4789 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4790 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4791 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4792#ifndef CONFIG_USER_ONLY
4793 /* Avoid overhead of an access check that always passes in user-mode */
4794 .accessfn = aa64_zva_access,
4795#endif
4796 },
0eef9d98
PM
4797 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4798 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4799 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
4800 /* Cache ops: all NOPs since we don't emulate caches */
4801 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4802 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a
RH
4803 .access = PL1_W, .type = ARM_CP_NOP,
4804 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4805 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4806 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a
RH
4807 .access = PL1_W, .type = ARM_CP_NOP,
4808 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4809 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4810 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4811 .access = PL0_W, .type = ARM_CP_NOP,
38262d8a 4812 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4813 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4814 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e
RH
4815 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4816 .type = ARM_CP_NOP },
8af35c37
PM
4817 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4818 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 4819 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
4820 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4821 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4822 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 4823 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
4824 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4825 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 4826 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
4827 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4828 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4829 .access = PL0_W, .type = ARM_CP_NOP,
38262d8a 4830 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4831 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4832 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4833 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 4834 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
4835 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4836 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 4837 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
168aa23b
PM
4838 /* TLBI operations */
4839 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4840 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
30881b73 4841 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4842 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4843 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4844 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
30881b73 4845 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4846 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4847 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4848 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
30881b73 4849 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4850 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4851 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4852 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
30881b73 4853 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4854 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4855 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4856 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
30881b73 4857 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4858 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4859 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4860 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
30881b73 4861 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4862 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4863 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4864 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73 4865 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4866 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4867 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4868 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73 4869 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4870 .writefn = tlbi_aa64_vae1_write },
168aa23b 4871 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4872 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73 4873 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4874 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4875 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4876 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73 4877 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4878 .writefn = tlbi_aa64_vae1_write },
168aa23b 4879 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4880 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73 4881 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4882 .writefn = tlbi_aa64_vae1_write },
168aa23b 4883 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4884 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73 4885 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4886 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
4887 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4888 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
bf05340c 4889 .access = PL2_W, .type = ARM_CP_NOP },
cea66e91
PM
4890 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4891 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
bf05340c 4892 .access = PL2_W, .type = ARM_CP_NOP },
83ddf975
PM
4893 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4894 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4895 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4896 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
4897 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4898 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4899 .access = PL2_W, .type = ARM_CP_NO_RAW,
4900 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
4901 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4902 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
bf05340c 4903 .access = PL2_W, .type = ARM_CP_NOP },
cea66e91
PM
4904 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4905 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
bf05340c 4906 .access = PL2_W, .type = ARM_CP_NOP },
83ddf975
PM
4907 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4908 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4909 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4910 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
4911 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4912 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4913 .access = PL2_W, .type = ARM_CP_NO_RAW,
4914 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
4915#ifndef CONFIG_USER_ONLY
4916 /* 64 bit address translation operations */
4917 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4918 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4919 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4920 .writefn = ats_write64 },
19525524
PM
4921 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4922 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4923 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4924 .writefn = ats_write64 },
19525524
PM
4925 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4926 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
0710b2fa
PM
4927 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4928 .writefn = ats_write64 },
19525524
PM
4929 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4930 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
0710b2fa
PM
4931 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4932 .writefn = ats_write64 },
2a47df95 4933 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 4934 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
0710b2fa
PM
4935 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4936 .writefn = ats_write64 },
2a47df95 4937 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 4938 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
0710b2fa
PM
4939 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4940 .writefn = ats_write64 },
2a47df95 4941 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 4942 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
0710b2fa
PM
4943 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4944 .writefn = ats_write64 },
2a47df95 4945 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 4946 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
0710b2fa
PM
4947 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4948 .writefn = ats_write64 },
2a47df95
PM
4949 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4950 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4951 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4952 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4953 .writefn = ats_write64 },
2a47df95
PM
4954 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4955 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4956 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4957 .writefn = ats_write64 },
c96fc9b5
EI
4958 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4959 .type = ARM_CP_ALIAS,
4960 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4961 .access = PL1_RW, .resetvalue = 0,
4962 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4963 .writefn = par_write },
19525524 4964#endif
995939a6 4965 /* TLB invalidate last level of translation table walk */
9449fdf6 4966 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
30881b73
RH
4967 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4968 .writefn = tlbimva_is_write },
9449fdf6 4969 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
30881b73 4970 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 4971 .writefn = tlbimvaa_is_write },
9449fdf6 4972 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73
RH
4973 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4974 .writefn = tlbimva_write },
9449fdf6 4975 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73
RH
4976 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4977 .writefn = tlbimvaa_write },
541ef8c2
SS
4978 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4979 .type = ARM_CP_NO_RAW, .access = PL2_W,
4980 .writefn = tlbimva_hyp_write },
4981 { .name = "TLBIMVALHIS",
4982 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4983 .type = ARM_CP_NO_RAW, .access = PL2_W,
4984 .writefn = tlbimva_hyp_is_write },
4985 { .name = "TLBIIPAS2",
4986 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
bf05340c 4987 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
4988 { .name = "TLBIIPAS2IS",
4989 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
bf05340c 4990 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
4991 { .name = "TLBIIPAS2L",
4992 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
bf05340c 4993 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
4994 { .name = "TLBIIPAS2LIS",
4995 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
bf05340c 4996 .type = ARM_CP_NOP, .access = PL2_W },
9449fdf6
PM
4997 /* 32 bit cache operations */
4998 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a 4999 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6
PM
5000 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5001 .type = ARM_CP_NOP, .access = PL1_W },
5002 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a 5003 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6 5004 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
38262d8a 5005 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6
PM
5006 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5007 .type = ARM_CP_NOP, .access = PL1_W },
5008 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5009 .type = ARM_CP_NOP, .access = PL1_W },
5010 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e 5011 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5012 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 5013 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5014 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
1bed4d2e 5015 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5016 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 5017 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5018 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
38262d8a 5019 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6 5020 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
1bed4d2e 5021 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5022 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 5023 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5024 /* MMU Domain access control / MPU write buffer control */
0c17d68c 5025 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
84929218 5026 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
5027 .writefn = dacr_write, .raw_writefn = raw_write,
5028 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5029 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 5030 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5031 .type = ARM_CP_ALIAS,
a0618a19 5032 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
5033 .access = PL1_RW,
5034 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 5035 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5036 .type = ARM_CP_ALIAS,
a65f1de9 5037 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5038 .access = PL1_RW,
5039 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
5040 /* We rely on the access checks not allowing the guest to write to the
5041 * state field when SPSel indicates that it's being used as the stack
5042 * pointer.
5043 */
5044 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5045 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5046 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 5047 .type = ARM_CP_ALIAS,
f502cfc2 5048 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
5049 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5050 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5051 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 5052 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
5053 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5054 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 5055 .type = ARM_CP_NO_RAW,
f502cfc2 5056 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
5057 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5058 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
a4c88675
RH
5059 .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_FPU,
5060 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
6a43e0b6
PM
5061 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5062 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5063 .access = PL2_RW, .resetvalue = 0,
5064 .writefn = dacr_write, .raw_writefn = raw_write,
5065 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5066 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5067 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5068 .access = PL2_RW, .resetvalue = 0,
5069 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5070 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5071 .type = ARM_CP_ALIAS,
5072 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5073 .access = PL2_RW,
5074 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5075 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5076 .type = ARM_CP_ALIAS,
5077 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5078 .access = PL2_RW,
5079 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5080 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5081 .type = ARM_CP_ALIAS,
5082 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5083 .access = PL2_RW,
5084 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5085 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5086 .type = ARM_CP_ALIAS,
5087 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5088 .access = PL2_RW,
5089 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73
PM
5090 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5091 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5092 .resetvalue = 0,
5093 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5094 { .name = "SDCR", .type = ARM_CP_ALIAS,
5095 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5096 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5097 .writefn = sdcr_write,
5098 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
5099};
5100
d42e3c26 5101/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
4771cd01 5102static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
d79e0c06 5103 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5104 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5105 .access = PL2_RW,
5106 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
ce4afed8 5107 { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH,
f149e3e8
EI
5108 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5109 .access = PL2_RW,
ce4afed8 5110 .type = ARM_CP_CONST, .resetvalue = 0 },
831a2fca
PM
5111 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5112 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5113 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e78e33
PM
5114 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5115 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5116 .access = PL2_RW,
5117 .type = ARM_CP_CONST, .resetvalue = 0 },
c6f19164
GB
5118 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5119 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5120 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
95f949ac
EI
5121 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5122 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5123 .access = PL2_RW, .type = ARM_CP_CONST,
5124 .resetvalue = 0 },
5125 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5126 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac 5127 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
2179ef95
PM
5128 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5129 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5130 .access = PL2_RW, .type = ARM_CP_CONST,
5131 .resetvalue = 0 },
55b53c71 5132 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5133 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5134 .access = PL2_RW, .type = ARM_CP_CONST,
5135 .resetvalue = 0 },
37cd6c24
PM
5136 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5137 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5138 .access = PL2_RW, .type = ARM_CP_CONST,
5139 .resetvalue = 0 },
5140 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5141 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5142 .access = PL2_RW, .type = ARM_CP_CONST,
5143 .resetvalue = 0 },
06ec4c8c
EI
5144 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5145 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5146 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
68e9c2fe
EI
5147 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
5148 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
93dd1e61 5149 .access = PL2_RW, .accessfn = access_el3_aa32ns,
68e9c2fe 5150 .type = ARM_CP_CONST, .resetvalue = 0 },
b698e9cf
EI
5151 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5152 .cp = 15, .opc1 = 6, .crm = 2,
5153 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5154 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
5155 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5156 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5157 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
b9cb5323
EI
5158 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5159 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5160 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
ff05f37b
EI
5161 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5162 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5163 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
a57633c0
EI
5164 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5165 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5166 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5167 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5168 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5169 .resetvalue = 0 },
0b6440af
EI
5170 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5171 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5172 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
edac4d8a
EI
5173 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5174 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5175 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5176 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5177 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5178 .resetvalue = 0 },
b0e66d95
EI
5179 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5180 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5181 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5182 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5183 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
5184 .resetvalue = 0 },
5185 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5186 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5187 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5188 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5189 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5190 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
14cc7b54
SF
5191 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5192 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
d6c8cf81
PM
5193 .access = PL2_RW, .accessfn = access_tda,
5194 .type = ARM_CP_CONST, .resetvalue = 0 },
59e05530
EI
5195 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
5196 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
93dd1e61 5197 .access = PL2_RW, .accessfn = access_el3_aa32ns,
59e05530 5198 .type = ARM_CP_CONST, .resetvalue = 0 },
2a5a9abd
AF
5199 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5200 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5201 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
cba517c3
PM
5202 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5203 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5204 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5205 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5206 .type = ARM_CP_CONST,
5207 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5208 .access = PL2_RW, .resetvalue = 0 },
d42e3c26
EI
5209};
5210
ce4afed8
PM
5211/* Ditto, but for registers which exist in ARMv8 but not v7 */
5212static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = {
5213 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5214 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5215 .access = PL2_RW,
5216 .type = ARM_CP_CONST, .resetvalue = 0 },
ce4afed8
PM
5217};
5218
d1fb4da2 5219static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
f149e3e8 5220{
2fc0cc0e 5221 ARMCPU *cpu = env_archcpu(env);
d1fb4da2
RH
5222
5223 if (arm_feature(env, ARM_FEATURE_V8)) {
5224 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5225 } else {
5226 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5227 }
f149e3e8
EI
5228
5229 if (arm_feature(env, ARM_FEATURE_EL3)) {
5230 valid_mask &= ~HCR_HCD;
77077a83
JK
5231 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5232 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5233 * However, if we're using the SMC PSCI conduit then QEMU is
5234 * effectively acting like EL3 firmware and so the guest at
5235 * EL2 should retain the ability to prevent EL1 from being
5236 * able to make SMC calls into the ersatz firmware, so in
5237 * that case HCR.TSC should be read/write.
5238 */
f149e3e8
EI
5239 valid_mask &= ~HCR_TSC;
5240 }
d1fb4da2
RH
5241
5242 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5243 if (cpu_isar_feature(aa64_vh, cpu)) {
5244 valid_mask |= HCR_E2H;
5245 }
5246 if (cpu_isar_feature(aa64_lor, cpu)) {
5247 valid_mask |= HCR_TLOR;
5248 }
5249 if (cpu_isar_feature(aa64_pauth, cpu)) {
5250 valid_mask |= HCR_API | HCR_APK;
5251 }
8ddb300b
RH
5252 if (cpu_isar_feature(aa64_mte, cpu)) {
5253 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5254 }
ef682cdb 5255 }
f149e3e8
EI
5256
5257 /* Clear RES0 bits. */
5258 value &= valid_mask;
5259
8ddb300b
RH
5260 /*
5261 * These bits change the MMU setup:
f149e3e8
EI
5262 * HCR_VM enables stage 2 translation
5263 * HCR_PTW forbids certain page-table setups
8ddb300b
RH
5264 * HCR_DC disables stage1 and enables stage2 translation
5265 * HCR_DCT enables tagging on (disabled) stage1 translation
f149e3e8 5266 */
8ddb300b 5267 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT)) {
d10eb08f 5268 tlb_flush(CPU(cpu));
f149e3e8 5269 }
ce4afed8 5270 env->cp15.hcr_el2 = value;
89430fc6
PM
5271
5272 /*
5273 * Updates to VI and VF require us to update the status of
5274 * virtual interrupts, which are the logical OR of these bits
5275 * and the state of the input lines from the GIC. (This requires
5276 * that we have the iothread lock, which is done by marking the
5277 * reginfo structs as ARM_CP_IO.)
5278 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5279 * possible for it to be taken immediately, because VIRQ and
5280 * VFIQ are masked unless running at EL0 or EL1, and HCR
5281 * can only be written at EL2.
5282 */
5283 g_assert(qemu_mutex_iothread_locked());
5284 arm_cpu_update_virq(cpu);
5285 arm_cpu_update_vfiq(cpu);
ce4afed8
PM
5286}
5287
d1fb4da2
RH
5288static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5289{
5290 do_hcr_write(env, value, 0);
5291}
5292
ce4afed8
PM
5293static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5294 uint64_t value)
5295{
5296 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5297 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
d1fb4da2 5298 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
ce4afed8
PM
5299}
5300
5301static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5302 uint64_t value)
5303{
5304 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5305 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
d1fb4da2 5306 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
f149e3e8
EI
5307}
5308
f7778444
RH
5309/*
5310 * Return the effective value of HCR_EL2.
5311 * Bits that are not included here:
5312 * RW (read from SCR_EL3.RW as needed)
5313 */
5314uint64_t arm_hcr_el2_eff(CPUARMState *env)
5315{
5316 uint64_t ret = env->cp15.hcr_el2;
5317
e6ef0169 5318 if (!arm_is_el2_enabled(env)) {
f7778444
RH
5319 /*
5320 * "This register has no effect if EL2 is not enabled in the
5321 * current Security state". This is ARMv8.4-SecEL2 speak for
5322 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5323 *
5324 * Prior to that, the language was "In an implementation that
5325 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5326 * as if this field is 0 for all purposes other than a direct
5327 * read or write access of HCR_EL2". With lots of enumeration
5328 * on a per-field basis. In current QEMU, this is condition
5329 * is arm_is_secure_below_el3.
5330 *
5331 * Since the v8.4 language applies to the entire register, and
5332 * appears to be backward compatible, use that.
5333 */
4990e1d3
RH
5334 return 0;
5335 }
5336
5337 /*
5338 * For a cpu that supports both aarch64 and aarch32, we can set bits
5339 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5340 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5341 */
5342 if (!arm_el_is_aa64(env, 2)) {
5343 uint64_t aa32_valid;
5344
5345 /*
5346 * These bits are up-to-date as of ARMv8.6.
5347 * For HCR, it's easiest to list just the 2 bits that are invalid.
5348 * For HCR2, list those that are valid.
5349 */
5350 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5351 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5352 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5353 ret &= aa32_valid;
5354 }
5355
5356 if (ret & HCR_TGE) {
5357 /* These bits are up-to-date as of ARMv8.6. */
f7778444
RH
5358 if (ret & HCR_E2H) {
5359 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5360 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5361 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4990e1d3
RH
5362 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5363 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5364 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
f7778444
RH
5365 } else {
5366 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5367 }
5368 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5369 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5370 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5371 HCR_TLOR);
5372 }
5373
5374 return ret;
5375}
5376
fc1120a7
PM
5377static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5378 uint64_t value)
5379{
5380 /*
5381 * For A-profile AArch32 EL3, if NSACR.CP10
5382 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5383 */
5384 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5385 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5386 value &= ~(0x3 << 10);
5387 value |= env->cp15.cptr_el[2] & (0x3 << 10);
5388 }
5389 env->cp15.cptr_el[2] = value;
5390}
5391
5392static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5393{
5394 /*
5395 * For A-profile AArch32 EL3, if NSACR.CP10
5396 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5397 */
5398 uint64_t value = env->cp15.cptr_el[2];
5399
5400 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5401 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5402 value |= 0x3 << 10;
5403 }
5404 return value;
5405}
5406
4771cd01 5407static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8 5408 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
89430fc6 5409 .type = ARM_CP_IO,
f149e3e8
EI
5410 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5411 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5412 .writefn = hcr_write },
ce4afed8 5413 { .name = "HCR", .state = ARM_CP_STATE_AA32,
89430fc6 5414 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5415 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5416 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5417 .writefn = hcr_writelow },
831a2fca
PM
5418 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5419 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5420 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3b685ba7 5421 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5422 .type = ARM_CP_ALIAS,
3b685ba7
EI
5423 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5424 .access = PL2_RW,
5425 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
68e78e33 5426 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
f2c30f42
EI
5427 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5428 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
cba517c3 5429 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
63b60551
EI
5430 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5431 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
cba517c3
PM
5432 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5433 .type = ARM_CP_ALIAS,
5434 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5435 .access = PL2_RW,
5436 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
3b685ba7 5437 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5438 .type = ARM_CP_ALIAS,
3b685ba7 5439 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5440 .access = PL2_RW,
5441 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d79e0c06 5442 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5443 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5444 .access = PL2_RW, .writefn = vbar_write,
5445 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5446 .resetvalue = 0 },
884b4dee
GB
5447 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5448 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5449 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 5450 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
5451 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5452 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5453 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
fc1120a7
PM
5454 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5455 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
95f949ac
EI
5456 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5457 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5458 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5459 .resetvalue = 0 },
5460 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5461 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac
EI
5462 .access = PL2_RW, .type = ARM_CP_ALIAS,
5463 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
5464 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5465 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5466 .access = PL2_RW, .type = ARM_CP_CONST,
5467 .resetvalue = 0 },
5468 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
55b53c71 5469 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5470 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5471 .access = PL2_RW, .type = ARM_CP_CONST,
5472 .resetvalue = 0 },
37cd6c24
PM
5473 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5474 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5475 .access = PL2_RW, .type = ARM_CP_CONST,
5476 .resetvalue = 0 },
5477 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5478 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5479 .access = PL2_RW, .type = ARM_CP_CONST,
5480 .resetvalue = 0 },
06ec4c8c
EI
5481 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5482 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
d06dc933
RH
5483 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5484 /* no .raw_writefn or .resetfn needed as we never use mask/base_mask */
06ec4c8c 5485 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
5486 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5487 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5488 .type = ARM_CP_ALIAS,
68e9c2fe
EI
5489 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5490 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5491 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5492 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112
PM
5493 .access = PL2_RW,
5494 /* no .writefn needed as this can't cause an ASID change;
5495 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
5496 */
68e9c2fe 5497 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
5498 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5499 .cp = 15, .opc1 = 6, .crm = 2,
5500 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5501 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5502 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5503 .writefn = vttbr_write },
5504 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5505 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5506 .access = PL2_RW, .writefn = vttbr_write,
5507 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
5508 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5509 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5510 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5511 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
5512 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5513 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5514 .access = PL2_RW, .resetvalue = 0,
5515 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
5516 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5517 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
ed30da8e 5518 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
a57633c0
EI
5519 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5520 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5521 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 5522 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
5523 { .name = "TLBIALLNSNH",
5524 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5525 .type = ARM_CP_NO_RAW, .access = PL2_W,
5526 .writefn = tlbiall_nsnh_write },
5527 { .name = "TLBIALLNSNHIS",
5528 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5529 .type = ARM_CP_NO_RAW, .access = PL2_W,
5530 .writefn = tlbiall_nsnh_is_write },
5531 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5532 .type = ARM_CP_NO_RAW, .access = PL2_W,
5533 .writefn = tlbiall_hyp_write },
5534 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5535 .type = ARM_CP_NO_RAW, .access = PL2_W,
5536 .writefn = tlbiall_hyp_is_write },
5537 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5538 .type = ARM_CP_NO_RAW, .access = PL2_W,
5539 .writefn = tlbimva_hyp_write },
5540 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5541 .type = ARM_CP_NO_RAW, .access = PL2_W,
5542 .writefn = tlbimva_hyp_is_write },
51da9014
EI
5543 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5544 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5545 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5546 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
5547 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5548 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5549 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5550 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
5551 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5552 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5553 .access = PL2_W, .type = ARM_CP_NO_RAW,
5554 .writefn = tlbi_aa64_vae2_write },
5555 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5556 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5557 .access = PL2_W, .type = ARM_CP_NO_RAW,
5558 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
5559 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5560 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5561 .type = ARM_CP_NO_RAW, .access = PL2_W,
fd3ed969 5562 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
5563 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5564 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5565 .access = PL2_W, .type = ARM_CP_NO_RAW,
5566 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 5567#ifndef CONFIG_USER_ONLY
2a47df95
PM
5568 /* Unlike the other EL2-related AT operations, these must
5569 * UNDEF from EL3 if EL2 is not implemented, which is why we
5570 * define them here rather than with the rest of the AT ops.
5571 */
5572 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5573 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5574 .access = PL2_W, .accessfn = at_s1e2_access,
0710b2fa 5575 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
2a47df95
PM
5576 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5577 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5578 .access = PL2_W, .accessfn = at_s1e2_access,
0710b2fa 5579 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 },
14db7fe0
PM
5580 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5581 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5582 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5583 * to behave as if SCR.NS was 1.
5584 */
5585 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5586 .access = PL2_W,
0710b2fa 5587 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
14db7fe0
PM
5588 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5589 .access = PL2_W,
0710b2fa 5590 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
0b6440af
EI
5591 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5592 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5593 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5594 * reset values as IMPDEF. We choose to reset to 3 to comply with
5595 * both ARMv7 and ARMv8.
5596 */
5597 .access = PL2_RW, .resetvalue = 3,
5598 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
5599 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5600 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5601 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5602 .writefn = gt_cntvoff_write,
5603 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5604 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5605 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5606 .writefn = gt_cntvoff_write,
5607 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
5608 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5609 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5610 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5611 .type = ARM_CP_IO, .access = PL2_RW,
5612 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5613 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5614 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5615 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5616 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5617 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5618 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 5619 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
5620 .resetfn = gt_hyp_timer_reset,
5621 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5622 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5623 .type = ARM_CP_IO,
5624 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5625 .access = PL2_RW,
5626 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5627 .resetvalue = 0,
5628 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 5629#endif
21c2dd77
PM
5630 /* The only field of MDCR_EL2 that has a defined architectural reset value
5631 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
5632 */
5633 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5634 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
5635 .access = PL2_RW, .resetvalue = PMCR_NUM_COUNTERS,
5636 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
5637 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5638 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5639 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5640 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5641 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5642 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5643 .access = PL2_RW,
5644 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
5645 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5646 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5647 .access = PL2_RW,
5648 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
5649};
5650
ce4afed8
PM
5651static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5652 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
89430fc6 5653 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5654 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5655 .access = PL2_RW,
5656 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5657 .writefn = hcr_writehigh },
ce4afed8
PM
5658};
5659
e9152ee9
RDC
5660static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5661 bool isread)
5662{
5663 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5664 return CP_ACCESS_OK;
5665 }
5666 return CP_ACCESS_TRAP_UNCATEGORIZED;
5667}
5668
5669static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5670 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5671 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5672 .access = PL2_RW, .accessfn = sel2_access,
5673 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5674 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5675 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5676 .access = PL2_RW, .accessfn = sel2_access,
5677 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
e9152ee9
RDC
5678};
5679
2f027fc5
PM
5680static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5681 bool isread)
5682{
5683 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
926c1b97 5684 * At Secure EL1 it traps to EL3 or EL2.
2f027fc5
PM
5685 */
5686 if (arm_current_el(env) == 3) {
5687 return CP_ACCESS_OK;
5688 }
5689 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
5690 if (env->cp15.scr_el3 & SCR_EEL2) {
5691 return CP_ACCESS_TRAP_EL2;
5692 }
2f027fc5
PM
5693 return CP_ACCESS_TRAP_EL3;
5694 }
5695 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5696 if (isread) {
5697 return CP_ACCESS_OK;
5698 }
5699 return CP_ACCESS_TRAP_UNCATEGORIZED;
5700}
5701
60fb1a87
GB
5702static const ARMCPRegInfo el3_cp_reginfo[] = {
5703 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5704 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5705 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
10d0ef3e 5706 .resetfn = scr_reset, .writefn = scr_write },
f80741d1 5707 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
60fb1a87 5708 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
5709 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5710 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 5711 .writefn = scr_write },
60fb1a87
GB
5712 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5713 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5714 .access = PL3_RW, .resetvalue = 0,
5715 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5716 { .name = "SDER",
5717 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5718 .access = PL3_RW, .resetvalue = 0,
5719 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 5720 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
5721 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5722 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 5723 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
5724 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5725 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
f478847f 5726 .access = PL3_RW, .resetvalue = 0,
7dd8c9af 5727 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
5728 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5729 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
5730 .access = PL3_RW,
5731 /* no .writefn needed as this can't cause an ASID change;
811595a2
PM
5732 * we must provide a .raw_writefn and .resetfn because we handle
5733 * reset and migration for the AArch32 TTBCR(S), which might be
5734 * using mask and base_mask.
6459b94c 5735 */
811595a2 5736 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee 5737 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 5738 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5739 .type = ARM_CP_ALIAS,
81547d66
EI
5740 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5741 .access = PL3_RW,
5742 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 5743 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
5744 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5745 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
5746 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5747 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5748 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 5749 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5750 .type = ARM_CP_ALIAS,
81547d66 5751 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5752 .access = PL3_RW,
5753 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
5754 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5755 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5756 .access = PL3_RW, .writefn = vbar_write,
5757 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5758 .resetvalue = 0 },
c6f19164
GB
5759 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5760 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5761 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5762 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
5763 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5764 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5765 .access = PL3_RW, .resetvalue = 0,
5766 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
5767 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5768 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5769 .access = PL3_RW, .type = ARM_CP_CONST,
5770 .resetvalue = 0 },
37cd6c24
PM
5771 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5772 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5773 .access = PL3_RW, .type = ARM_CP_CONST,
5774 .resetvalue = 0 },
5775 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5776 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5777 .access = PL3_RW, .type = ARM_CP_CONST,
5778 .resetvalue = 0 },
43efaa33
PM
5779 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5780 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5781 .access = PL3_W, .type = ARM_CP_NO_RAW,
5782 .writefn = tlbi_aa64_alle3is_write },
5783 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5784 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5785 .access = PL3_W, .type = ARM_CP_NO_RAW,
5786 .writefn = tlbi_aa64_vae3is_write },
5787 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5788 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5789 .access = PL3_W, .type = ARM_CP_NO_RAW,
5790 .writefn = tlbi_aa64_vae3is_write },
5791 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5792 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5793 .access = PL3_W, .type = ARM_CP_NO_RAW,
5794 .writefn = tlbi_aa64_alle3_write },
5795 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5796 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5797 .access = PL3_W, .type = ARM_CP_NO_RAW,
5798 .writefn = tlbi_aa64_vae3_write },
5799 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5800 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5801 .access = PL3_W, .type = ARM_CP_NO_RAW,
5802 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
5803};
5804
e2cce18f
RH
5805#ifndef CONFIG_USER_ONLY
5806/* Test if system register redirection is to occur in the current state. */
5807static bool redirect_for_e2h(CPUARMState *env)
5808{
5809 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5810}
5811
5812static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5813{
5814 CPReadFn *readfn;
5815
5816 if (redirect_for_e2h(env)) {
5817 /* Switch to the saved EL2 version of the register. */
5818 ri = ri->opaque;
5819 readfn = ri->readfn;
5820 } else {
5821 readfn = ri->orig_readfn;
5822 }
5823 if (readfn == NULL) {
5824 readfn = raw_read;
5825 }
5826 return readfn(env, ri);
5827}
5828
5829static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5830 uint64_t value)
5831{
5832 CPWriteFn *writefn;
5833
5834 if (redirect_for_e2h(env)) {
5835 /* Switch to the saved EL2 version of the register. */
5836 ri = ri->opaque;
5837 writefn = ri->writefn;
5838 } else {
5839 writefn = ri->orig_writefn;
5840 }
5841 if (writefn == NULL) {
5842 writefn = raw_write;
5843 }
5844 writefn(env, ri, value);
5845}
5846
5847static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5848{
5849 struct E2HAlias {
5850 uint32_t src_key, dst_key, new_key;
5851 const char *src_name, *dst_name, *new_name;
5852 bool (*feature)(const ARMISARegisters *id);
5853 };
5854
5855#define K(op0, op1, crn, crm, op2) \
5856 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5857
5858 static const struct E2HAlias aliases[] = {
5859 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
5860 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5861 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
5862 "CPACR", "CPTR_EL2", "CPACR_EL12" },
5863 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
5864 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5865 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
5866 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5867 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
5868 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5869 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
5870 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5871 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
5872 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5873 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
5874 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5875 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
5876 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5877 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
5878 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5879 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
5880 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5881 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5882 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5883 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5884 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5885 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5886 "VBAR", "VBAR_EL2", "VBAR_EL12" },
5887 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5888 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5889 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5890 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5891
5892 /*
5893 * Note that redirection of ZCR is mentioned in the description
5894 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5895 * not in the summary table.
5896 */
5897 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
5898 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5899
4b779ceb
RH
5900 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
5901 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5902
e2cce18f
RH
5903 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5904 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5905 };
5906#undef K
5907
5908 size_t i;
5909
5910 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5911 const struct E2HAlias *a = &aliases[i];
9da35a40 5912 ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
9da35a40 5913 bool ok;
e2cce18f
RH
5914
5915 if (a->feature && !a->feature(&cpu->isar)) {
5916 continue;
5917 }
5918
5860362d
RH
5919 src_reg = g_hash_table_lookup(cpu->cp_regs,
5920 (gpointer)(uintptr_t)a->src_key);
5921 dst_reg = g_hash_table_lookup(cpu->cp_regs,
5922 (gpointer)(uintptr_t)a->dst_key);
e2cce18f
RH
5923 g_assert(src_reg != NULL);
5924 g_assert(dst_reg != NULL);
5925
5926 /* Cross-compare names to detect typos in the keys. */
5927 g_assert(strcmp(src_reg->name, a->src_name) == 0);
5928 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5929
5930 /* None of the core system registers use opaque; we will. */
5931 g_assert(src_reg->opaque == NULL);
5932
5933 /* Create alias before redirection so we dup the right data. */
9da35a40 5934 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
9da35a40
RH
5935
5936 new_reg->name = a->new_name;
5937 new_reg->type |= ARM_CP_ALIAS;
5938 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
5939 new_reg->access &= PL2_RW | PL3_RW;
5940
5860362d
RH
5941 ok = g_hash_table_insert(cpu->cp_regs,
5942 (gpointer)(uintptr_t)a->new_key, new_reg);
9da35a40 5943 g_assert(ok);
e2cce18f
RH
5944
5945 src_reg->opaque = dst_reg;
5946 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5947 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5948 if (!src_reg->raw_readfn) {
5949 src_reg->raw_readfn = raw_read;
5950 }
5951 if (!src_reg->raw_writefn) {
5952 src_reg->raw_writefn = raw_write;
5953 }
5954 src_reg->readfn = el2_e2h_read;
5955 src_reg->writefn = el2_e2h_write;
5956 }
5957}
5958#endif
5959
3f208fd7
PM
5960static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5961 bool isread)
7da845b0 5962{
97475a89
RH
5963 int cur_el = arm_current_el(env);
5964
5965 if (cur_el < 2) {
5966 uint64_t hcr = arm_hcr_el2_eff(env);
5967
5968 if (cur_el == 0) {
5969 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5970 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5971 return CP_ACCESS_TRAP_EL2;
5972 }
5973 } else {
5974 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5975 return CP_ACCESS_TRAP;
5976 }
5977 if (hcr & HCR_TID2) {
5978 return CP_ACCESS_TRAP_EL2;
5979 }
5980 }
5981 } else if (hcr & HCR_TID2) {
5982 return CP_ACCESS_TRAP_EL2;
5983 }
7da845b0 5984 }
630fcd4d
MZ
5985
5986 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5987 return CP_ACCESS_TRAP_EL2;
5988 }
5989
7da845b0
PM
5990 return CP_ACCESS_OK;
5991}
5992
1424ca8d
DM
5993static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
5994 uint64_t value)
5995{
5996 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
5997 * read via a bit in OSLSR_EL1.
5998 */
5999 int oslock;
6000
6001 if (ri->state == ARM_CP_STATE_AA32) {
6002 oslock = (value == 0xC5ACCE55);
6003 } else {
6004 oslock = value & 1;
6005 }
6006
6007 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
6008}
6009
50300698 6010static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 6011 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
6012 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
6013 * unlike DBGDRAR it is never accessible from EL0.
6014 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
6015 * accessor.
50300698
PM
6016 */
6017 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
6018 .access = PL0_R, .accessfn = access_tdra,
6019 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
6020 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
6021 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
6022 .access = PL1_R, .accessfn = access_tdra,
6023 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 6024 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
6025 .access = PL0_R, .accessfn = access_tdra,
6026 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 6027 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
6028 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
6029 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 6030 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
6031 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
6032 .resetvalue = 0 },
49a6f3bf
NH
6033 /*
6034 * MDCCSR_EL0[30:29] map to EDSCR[30:29]. Simply RAZ as the external
6035 * Debug Communication Channel is not implemented.
6036 */
6037 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_AA64,
6038 .opc0 = 2, .opc1 = 3, .crn = 0, .crm = 1, .opc2 = 0,
6039 .access = PL0_R, .accessfn = access_tda,
6040 .type = ARM_CP_CONST, .resetvalue = 0 },
6041 /*
6042 * DBGDSCRint[15,12,5:2] map to MDSCR_EL1[15,12,5:2]. Map all bits as
6043 * it is unlikely a guest will care.
5e8b12ff
PM
6044 * We don't implement the configurable EL0 access.
6045 */
49a6f3bf
NH
6046 { .name = "DBGDSCRint", .state = ARM_CP_STATE_AA32,
6047 .cp = 14, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 6048 .type = ARM_CP_ALIAS,
d6c8cf81 6049 .access = PL1_R, .accessfn = access_tda,
b061a82b 6050 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
6051 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
6052 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 6053 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 6054 .accessfn = access_tdosa,
1424ca8d
DM
6055 .writefn = oslar_write },
6056 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
6057 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
6058 .access = PL1_R, .resetvalue = 10,
187f678d 6059 .accessfn = access_tdosa,
1424ca8d 6060 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
6061 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
6062 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
6063 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
6064 .access = PL1_RW, .accessfn = access_tdosa,
6065 .type = ARM_CP_NOP },
5e8b12ff
PM
6066 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
6067 * implement vector catch debug events yet.
6068 */
6069 { .name = "DBGVCR",
6070 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
6071 .access = PL1_RW, .accessfn = access_tda,
6072 .type = ARM_CP_NOP },
4d2ec4da
PM
6073 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
6074 * to save and restore a 32-bit guest's DBGVCR)
6075 */
6076 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
6077 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
6078 .access = PL2_RW, .accessfn = access_tda,
6079 .type = ARM_CP_NOP },
5dbdc434
PM
6080 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
6081 * Channel but Linux may try to access this register. The 32-bit
6082 * alias is DBGDCCINT.
6083 */
6084 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
6085 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
6086 .access = PL1_RW, .accessfn = access_tda,
6087 .type = ARM_CP_NOP },
50300698
PM
6088};
6089
6090static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
6091 /* 64 bit access versions of the (dummy) debug registers */
6092 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
6093 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6094 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
6095 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
50300698
PM
6096};
6097
60eed086
RH
6098/* Return the exception level to which exceptions should be taken
6099 * via SVEAccessTrap. If an exception should be routed through
6100 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
6101 * take care of raising that exception.
6102 * C.f. the ARM pseudocode function CheckSVEEnabled.
5be5e8ed 6103 */
ced31551 6104int sve_exception_el(CPUARMState *env, int el)
5be5e8ed
RH
6105{
6106#ifndef CONFIG_USER_ONLY
c2ddb7cf
RH
6107 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
6108
6109 if (el <= 1 && (hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
7701cee5
RH
6110 /* Check CPACR.ZEN. */
6111 switch (extract32(env->cp15.cpacr_el1, 16, 2)) {
6112 case 1:
6113 if (el != 0) {
6114 break;
6115 }
6116 /* fall through */
6117 case 0:
6118 case 2:
60eed086 6119 /* route_to_el2 */
c2ddb7cf 6120 return hcr_el2 & HCR_TGE ? 2 : 1;
5be5e8ed 6121 }
5be5e8ed 6122
60eed086 6123 /* Check CPACR.FPEN. */
7701cee5
RH
6124 switch (extract32(env->cp15.cpacr_el1, 20, 2)) {
6125 case 1:
6126 if (el != 0) {
6127 break;
6128 }
6129 /* fall through */
6130 case 0:
6131 case 2:
60eed086 6132 return 0;
5be5e8ed 6133 }
5be5e8ed
RH
6134 }
6135
d5a6fa2d
RH
6136 /*
6137 * CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE).
60eed086 6138 */
d5a6fa2d
RH
6139 if (el <= 2) {
6140 if (hcr_el2 & HCR_E2H) {
6141 /* Check CPTR_EL2.ZEN. */
6142 switch (extract32(env->cp15.cptr_el[2], 16, 2)) {
6143 case 1:
6144 if (el != 0 || !(hcr_el2 & HCR_TGE)) {
6145 break;
6146 }
6147 /* fall through */
6148 case 0:
6149 case 2:
6150 return 2;
6151 }
6152
6153 /* Check CPTR_EL2.FPEN. */
6154 switch (extract32(env->cp15.cptr_el[2], 20, 2)) {
6155 case 1:
6156 if (el == 2 || !(hcr_el2 & HCR_TGE)) {
6157 break;
6158 }
6159 /* fall through */
6160 case 0:
6161 case 2:
6162 return 0;
6163 }
6164 } else if (arm_is_el2_enabled(env)) {
6165 if (env->cp15.cptr_el[2] & CPTR_TZ) {
6166 return 2;
6167 }
6168 if (env->cp15.cptr_el[2] & CPTR_TFP) {
6169 return 0;
6170 }
60eed086 6171 }
5be5e8ed
RH
6172 }
6173
60eed086
RH
6174 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6175 if (arm_feature(env, ARM_FEATURE_EL3)
6176 && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
5be5e8ed
RH
6177 return 3;
6178 }
6179#endif
6180 return 0;
6181}
6182
ce440581 6183uint32_t aarch64_sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len)
0df9142d 6184{
6e553f2a 6185 uint32_t end_len;
0df9142d 6186
dc0bc8e7
RH
6187 start_len = MIN(start_len, ARM_MAX_VQ - 1);
6188 end_len = start_len;
6189
6e553f2a
RH
6190 if (!test_bit(start_len, cpu->sve_vq_map)) {
6191 end_len = find_last_bit(cpu->sve_vq_map, start_len);
6192 assert(end_len < start_len);
6193 }
6194 return end_len;
0df9142d
AJ
6195}
6196
0ab5953b
RH
6197/*
6198 * Given that SVE is enabled, return the vector length for EL.
6199 */
ced31551 6200uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
0ab5953b 6201{
2fc0cc0e 6202 ARMCPU *cpu = env_archcpu(env);
0ab5953b
RH
6203 uint32_t zcr_len = cpu->sve_max_vq - 1;
6204
63888fa7
RH
6205 if (el <= 1 &&
6206 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
0ab5953b
RH
6207 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
6208 }
6a02a732 6209 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
0ab5953b
RH
6210 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
6211 }
6a02a732 6212 if (arm_feature(env, ARM_FEATURE_EL3)) {
0ab5953b
RH
6213 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
6214 }
0df9142d 6215
ce440581 6216 return aarch64_sve_zcr_get_valid_len(cpu, zcr_len);
0ab5953b
RH
6217}
6218
5be5e8ed
RH
6219static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6220 uint64_t value)
6221{
0ab5953b
RH
6222 int cur_el = arm_current_el(env);
6223 int old_len = sve_zcr_len_for_el(env, cur_el);
6224 int new_len;
6225
5be5e8ed 6226 /* Bits other than [3:0] are RAZ/WI. */
7b351d98 6227 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5be5e8ed 6228 raw_write(env, ri, value & 0xf);
0ab5953b
RH
6229
6230 /*
6231 * Because we arrived here, we know both FP and SVE are enabled;
6232 * otherwise we would have trapped access to the ZCR_ELn register.
6233 */
6234 new_len = sve_zcr_len_for_el(env, cur_el);
6235 if (new_len < old_len) {
6236 aarch64_sve_narrow_vq(env, new_len + 1);
6237 }
5be5e8ed
RH
6238}
6239
6240static const ARMCPRegInfo zcr_el1_reginfo = {
6241 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6242 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6243 .access = PL1_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6244 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6245 .writefn = zcr_write, .raw_writefn = raw_write
6246};
6247
6248static const ARMCPRegInfo zcr_el2_reginfo = {
6249 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6250 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6251 .access = PL2_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6252 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6253 .writefn = zcr_write, .raw_writefn = raw_write
6254};
6255
6256static const ARMCPRegInfo zcr_no_el2_reginfo = {
6257 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6258 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6259 .access = PL2_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6260 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore
6261};
6262
6263static const ARMCPRegInfo zcr_el3_reginfo = {
6264 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6265 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
11d7870b 6266 .access = PL3_RW, .type = ARM_CP_SVE,
5be5e8ed
RH
6267 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6268 .writefn = zcr_write, .raw_writefn = raw_write
6269};
6270
9ee98ce8
PM
6271void hw_watchpoint_update(ARMCPU *cpu, int n)
6272{
6273 CPUARMState *env = &cpu->env;
6274 vaddr len = 0;
6275 vaddr wvr = env->cp15.dbgwvr[n];
6276 uint64_t wcr = env->cp15.dbgwcr[n];
6277 int mask;
6278 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
6279
6280 if (env->cpu_watchpoint[n]) {
6281 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
6282 env->cpu_watchpoint[n] = NULL;
6283 }
6284
8b7a5bbe 6285 if (!FIELD_EX64(wcr, DBGWCR, E)) {
9ee98ce8
PM
6286 /* E bit clear : watchpoint disabled */
6287 return;
6288 }
6289
8b7a5bbe 6290 switch (FIELD_EX64(wcr, DBGWCR, LSC)) {
9ee98ce8
PM
6291 case 0:
6292 /* LSC 00 is reserved and must behave as if the wp is disabled */
6293 return;
6294 case 1:
6295 flags |= BP_MEM_READ;
6296 break;
6297 case 2:
6298 flags |= BP_MEM_WRITE;
6299 break;
6300 case 3:
6301 flags |= BP_MEM_ACCESS;
6302 break;
6303 }
6304
6305 /* Attempts to use both MASK and BAS fields simultaneously are
6306 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
6307 * thus generating a watchpoint for every byte in the masked region.
6308 */
8b7a5bbe 6309 mask = FIELD_EX64(wcr, DBGWCR, MASK);
9ee98ce8
PM
6310 if (mask == 1 || mask == 2) {
6311 /* Reserved values of MASK; we must act as if the mask value was
6312 * some non-reserved value, or as if the watchpoint were disabled.
6313 * We choose the latter.
6314 */
6315 return;
6316 } else if (mask) {
6317 /* Watchpoint covers an aligned area up to 2GB in size */
6318 len = 1ULL << mask;
6319 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
6320 * whether the watchpoint fires when the unmasked bits match; we opt
6321 * to generate the exceptions.
6322 */
6323 wvr &= ~(len - 1);
6324 } else {
6325 /* Watchpoint covers bytes defined by the byte address select bits */
8b7a5bbe 6326 int bas = FIELD_EX64(wcr, DBGWCR, BAS);
9ee98ce8
PM
6327 int basstart;
6328
9ee98ce8
PM
6329 if (extract64(wvr, 2, 1)) {
6330 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
6331 * ignored, and BAS[3:0] define which bytes to watch.
6332 */
6333 bas &= 0xf;
6334 }
ae1111d4
RH
6335
6336 if (bas == 0) {
6337 /* This must act as if the watchpoint is disabled */
6338 return;
6339 }
6340
9ee98ce8
PM
6341 /* The BAS bits are supposed to be programmed to indicate a contiguous
6342 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
6343 * we fire for each byte in the word/doubleword addressed by the WVR.
6344 * We choose to ignore any non-zero bits after the first range of 1s.
6345 */
6346 basstart = ctz32(bas);
6347 len = cto32(bas >> basstart);
6348 wvr += basstart;
6349 }
6350
6351 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
6352 &env->cpu_watchpoint[n]);
6353}
6354
6355void hw_watchpoint_update_all(ARMCPU *cpu)
6356{
6357 int i;
6358 CPUARMState *env = &cpu->env;
6359
6360 /* Completely clear out existing QEMU watchpoints and our array, to
6361 * avoid possible stale entries following migration load.
6362 */
6363 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
6364 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
6365
6366 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
6367 hw_watchpoint_update(cpu, i);
6368 }
6369}
6370
6371static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6372 uint64_t value)
6373{
2fc0cc0e 6374 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6375 int i = ri->crm;
6376
777ab8d8 6377 /*
9ee98ce8 6378 * Bits [1:0] are RES0.
777ab8d8
RH
6379 *
6380 * It is IMPLEMENTATION DEFINED whether [63:49] ([63:53] with FEAT_LVA)
6381 * are hardwired to the value of bit [48] ([52] with FEAT_LVA), or if
6382 * they contain the value written. It is CONSTRAINED UNPREDICTABLE
6383 * whether the RESS bits are ignored when comparing an address.
6384 *
6385 * Therefore we are allowed to compare the entire register, which lets
6386 * us avoid considering whether or not FEAT_LVA is actually enabled.
9ee98ce8 6387 */
777ab8d8 6388 value &= ~3ULL;
9ee98ce8
PM
6389
6390 raw_write(env, ri, value);
6391 hw_watchpoint_update(cpu, i);
6392}
6393
6394static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6395 uint64_t value)
6396{
2fc0cc0e 6397 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6398 int i = ri->crm;
6399
6400 raw_write(env, ri, value);
6401 hw_watchpoint_update(cpu, i);
6402}
6403
46747d15
PM
6404void hw_breakpoint_update(ARMCPU *cpu, int n)
6405{
6406 CPUARMState *env = &cpu->env;
6407 uint64_t bvr = env->cp15.dbgbvr[n];
6408 uint64_t bcr = env->cp15.dbgbcr[n];
6409 vaddr addr;
6410 int bt;
6411 int flags = BP_CPU;
6412
6413 if (env->cpu_breakpoint[n]) {
6414 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
6415 env->cpu_breakpoint[n] = NULL;
6416 }
6417
6418 if (!extract64(bcr, 0, 1)) {
6419 /* E bit clear : watchpoint disabled */
6420 return;
6421 }
6422
6423 bt = extract64(bcr, 20, 4);
6424
6425 switch (bt) {
6426 case 4: /* unlinked address mismatch (reserved if AArch64) */
6427 case 5: /* linked address mismatch (reserved if AArch64) */
6428 qemu_log_mask(LOG_UNIMP,
0221c8fd 6429 "arm: address mismatch breakpoint types not implemented\n");
46747d15
PM
6430 return;
6431 case 0: /* unlinked address match */
6432 case 1: /* linked address match */
6433 {
777ab8d8
RH
6434 /*
6435 * Bits [1:0] are RES0.
6436 *
6437 * It is IMPLEMENTATION DEFINED whether bits [63:49]
6438 * ([63:53] for FEAT_LVA) are hardwired to a copy of the sign bit
6439 * of the VA field ([48] or [52] for FEAT_LVA), or whether the
6440 * value is read as written. It is CONSTRAINED UNPREDICTABLE
6441 * whether the RESS bits are ignored when comparing an address.
6442 * Therefore we are allowed to compare the entire register, which
6443 * lets us avoid considering whether FEAT_LVA is actually enabled.
6444 *
6445 * The BAS field is used to allow setting breakpoints on 16-bit
6446 * wide instructions; it is CONSTRAINED UNPREDICTABLE whether
46747d15
PM
6447 * a bp will fire if the addresses covered by the bp and the addresses
6448 * covered by the insn overlap but the insn doesn't start at the
6449 * start of the bp address range. We choose to require the insn and
6450 * the bp to have the same address. The constraints on writing to
6451 * BAS enforced in dbgbcr_write mean we have only four cases:
6452 * 0b0000 => no breakpoint
6453 * 0b0011 => breakpoint on addr
6454 * 0b1100 => breakpoint on addr + 2
6455 * 0b1111 => breakpoint on addr
6456 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
6457 */
6458 int bas = extract64(bcr, 5, 4);
777ab8d8 6459 addr = bvr & ~3ULL;
46747d15
PM
6460 if (bas == 0) {
6461 return;
6462 }
6463 if (bas == 0xc) {
6464 addr += 2;
6465 }
6466 break;
6467 }
6468 case 2: /* unlinked context ID match */
6469 case 8: /* unlinked VMID match (reserved if no EL2) */
6470 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
6471 qemu_log_mask(LOG_UNIMP,
0221c8fd 6472 "arm: unlinked context breakpoint types not implemented\n");
46747d15
PM
6473 return;
6474 case 9: /* linked VMID match (reserved if no EL2) */
6475 case 11: /* linked context ID and VMID match (reserved if no EL2) */
6476 case 3: /* linked context ID match */
6477 default:
6478 /* We must generate no events for Linked context matches (unless
6479 * they are linked to by some other bp/wp, which is handled in
6480 * updates for the linking bp/wp). We choose to also generate no events
6481 * for reserved values.
6482 */
6483 return;
6484 }
6485
6486 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
6487}
6488
6489void hw_breakpoint_update_all(ARMCPU *cpu)
6490{
6491 int i;
6492 CPUARMState *env = &cpu->env;
6493
6494 /* Completely clear out existing QEMU breakpoints and our array, to
6495 * avoid possible stale entries following migration load.
6496 */
6497 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
6498 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
6499
6500 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
6501 hw_breakpoint_update(cpu, i);
6502 }
6503}
6504
6505static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6506 uint64_t value)
6507{
2fc0cc0e 6508 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6509 int i = ri->crm;
6510
6511 raw_write(env, ri, value);
6512 hw_breakpoint_update(cpu, i);
6513}
6514
6515static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6516 uint64_t value)
6517{
2fc0cc0e 6518 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6519 int i = ri->crm;
6520
6521 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
6522 * copy of BAS[0].
6523 */
6524 value = deposit64(value, 6, 1, extract64(value, 5, 1));
6525 value = deposit64(value, 8, 1, extract64(value, 7, 1));
6526
6527 raw_write(env, ri, value);
6528 hw_breakpoint_update(cpu, i);
6529}
6530
50300698 6531static void define_debug_regs(ARMCPU *cpu)
0b45451e 6532{
50300698
PM
6533 /* Define v7 and v8 architectural debug registers.
6534 * These are just dummy implementations for now.
0b45451e
PM
6535 */
6536 int i;
3ff6fc91 6537 int wrps, brps, ctx_cmps;
54a78718
RH
6538
6539 /*
6540 * The Arm ARM says DBGDIDR is optional and deprecated if EL1 cannot
6541 * use AArch32. Given that bit 15 is RES1, if the value is 0 then
6542 * the register must not exist for this cpu.
6543 */
6544 if (cpu->isar.dbgdidr != 0) {
6545 ARMCPRegInfo dbgdidr = {
6546 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0,
6547 .opc1 = 0, .opc2 = 0,
6548 .access = PL0_R, .accessfn = access_tda,
6549 .type = ARM_CP_CONST, .resetvalue = cpu->isar.dbgdidr,
6550 };
6551 define_one_arm_cp_reg(cpu, &dbgdidr);
6552 }
48eb3ae6 6553
3ff6fc91 6554 /* Note that all these register fields hold "number of Xs minus 1". */
88ce6c6e
PM
6555 brps = arm_num_brps(cpu);
6556 wrps = arm_num_wrps(cpu);
6557 ctx_cmps = arm_num_ctx_cmps(cpu);
3ff6fc91
PM
6558
6559 assert(ctx_cmps <= brps);
48eb3ae6 6560
50300698
PM
6561 define_arm_cp_regs(cpu, debug_cp_reginfo);
6562
6563 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
6564 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
6565 }
6566
88ce6c6e 6567 for (i = 0; i < brps; i++) {
0b45451e 6568 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6569 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
6570 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 6571 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6572 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
6573 .writefn = dbgbvr_write, .raw_writefn = raw_write
6574 },
10aae104
PM
6575 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
6576 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 6577 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6578 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
6579 .writefn = dbgbcr_write, .raw_writefn = raw_write
6580 },
48eb3ae6
PM
6581 };
6582 define_arm_cp_regs(cpu, dbgregs);
6583 }
6584
88ce6c6e 6585 for (i = 0; i < wrps; i++) {
48eb3ae6 6586 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6587 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
6588 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 6589 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6590 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
6591 .writefn = dbgwvr_write, .raw_writefn = raw_write
6592 },
10aae104
PM
6593 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
6594 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 6595 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6596 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
6597 .writefn = dbgwcr_write, .raw_writefn = raw_write
6598 },
0b45451e
PM
6599 };
6600 define_arm_cp_regs(cpu, dbgregs);
6601 }
6602}
6603
24183fb6
PM
6604static void define_pmu_regs(ARMCPU *cpu)
6605{
6606 /*
6607 * v7 performance monitor control register: same implementor
6608 * field as main ID register, and we implement four counters in
6609 * addition to the cycle count register.
6610 */
21c2dd77 6611 unsigned int i, pmcrn = PMCR_NUM_COUNTERS;
24183fb6
PM
6612 ARMCPRegInfo pmcr = {
6613 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6614 .access = PL0_RW,
6615 .type = ARM_CP_IO | ARM_CP_ALIAS,
6616 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6617 .accessfn = pmreg_access, .writefn = pmcr_write,
6618 .raw_writefn = raw_write,
6619 };
6620 ARMCPRegInfo pmcr64 = {
6621 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6622 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6623 .access = PL0_RW, .accessfn = pmreg_access,
6624 .type = ARM_CP_IO,
6625 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
21c2dd77
PM
6626 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT) |
6627 PMCRLC,
24183fb6
PM
6628 .writefn = pmcr_write, .raw_writefn = raw_write,
6629 };
6630 define_one_arm_cp_reg(cpu, &pmcr);
6631 define_one_arm_cp_reg(cpu, &pmcr64);
6632 for (i = 0; i < pmcrn; i++) {
6633 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6634 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6635 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6636 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6637 ARMCPRegInfo pmev_regs[] = {
6638 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6639 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6640 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6641 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6642 .accessfn = pmreg_access },
6643 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6644 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6645 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6646 .type = ARM_CP_IO,
6647 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6648 .raw_readfn = pmevcntr_rawread,
6649 .raw_writefn = pmevcntr_rawwrite },
6650 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6651 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6652 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6653 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6654 .accessfn = pmreg_access },
6655 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6656 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6657 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6658 .type = ARM_CP_IO,
6659 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6660 .raw_writefn = pmevtyper_rawwrite },
24183fb6
PM
6661 };
6662 define_arm_cp_regs(cpu, pmev_regs);
6663 g_free(pmevcntr_name);
6664 g_free(pmevcntr_el0_name);
6665 g_free(pmevtyper_name);
6666 g_free(pmevtyper_el0_name);
6667 }
a6179538 6668 if (cpu_isar_feature(aa32_pmu_8_1, cpu)) {
24183fb6
PM
6669 ARMCPRegInfo v81_pmu_regs[] = {
6670 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6671 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6672 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6673 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6674 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6675 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6676 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6677 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
24183fb6
PM
6678 };
6679 define_arm_cp_regs(cpu, v81_pmu_regs);
6680 }
15dd1ebd
PM
6681 if (cpu_isar_feature(any_pmu_8_4, cpu)) {
6682 static const ARMCPRegInfo v84_pmmir = {
6683 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6684 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6685 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6686 .resetvalue = 0
6687 };
6688 define_one_arm_cp_reg(cpu, &v84_pmmir);
6689 }
24183fb6
PM
6690}
6691
96a8b92e
PM
6692/* We don't know until after realize whether there's a GICv3
6693 * attached, and that is what registers the gicv3 sysregs.
6694 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6695 * at runtime.
6696 */
6697static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6698{
2fc0cc0e 6699 ARMCPU *cpu = env_archcpu(env);
8a130a7b 6700 uint64_t pfr1 = cpu->isar.id_pfr1;
96a8b92e
PM
6701
6702 if (env->gicv3state) {
6703 pfr1 |= 1 << 28;
6704 }
6705 return pfr1;
6706}
6707
976b99b6 6708#ifndef CONFIG_USER_ONLY
96a8b92e
PM
6709static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6710{
2fc0cc0e 6711 ARMCPU *cpu = env_archcpu(env);
47576b94 6712 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
96a8b92e
PM
6713
6714 if (env->gicv3state) {
6715 pfr0 |= 1 << 24;
6716 }
6717 return pfr0;
6718}
976b99b6 6719#endif
96a8b92e 6720
2d7137c1 6721/* Shared logic between LORID and the rest of the LOR* registers.
9bd268ba 6722 * Secure state exclusion has already been dealt with.
2d7137c1 6723 */
9bd268ba
RDC
6724static CPAccessResult access_lor_ns(CPUARMState *env,
6725 const ARMCPRegInfo *ri, bool isread)
2d7137c1
RH
6726{
6727 int el = arm_current_el(env);
6728
6729 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6730 return CP_ACCESS_TRAP_EL2;
6731 }
6732 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6733 return CP_ACCESS_TRAP_EL3;
6734 }
6735 return CP_ACCESS_OK;
6736}
6737
2d7137c1
RH
6738static CPAccessResult access_lor_other(CPUARMState *env,
6739 const ARMCPRegInfo *ri, bool isread)
6740{
6741 if (arm_is_secure_below_el3(env)) {
6742 /* Access denied in secure mode. */
6743 return CP_ACCESS_TRAP;
6744 }
9bd268ba 6745 return access_lor_ns(env, ri, isread);
2d7137c1
RH
6746}
6747
d8564ee4
RH
6748/*
6749 * A trivial implementation of ARMv8.1-LOR leaves all of these
6750 * registers fixed at 0, which indicates that there are zero
6751 * supported Limited Ordering regions.
6752 */
6753static const ARMCPRegInfo lor_reginfo[] = {
6754 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6755 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6756 .access = PL1_RW, .accessfn = access_lor_other,
6757 .type = ARM_CP_CONST, .resetvalue = 0 },
6758 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6759 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6760 .access = PL1_RW, .accessfn = access_lor_other,
6761 .type = ARM_CP_CONST, .resetvalue = 0 },
6762 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6763 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6764 .access = PL1_RW, .accessfn = access_lor_other,
6765 .type = ARM_CP_CONST, .resetvalue = 0 },
6766 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6767 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6768 .access = PL1_RW, .accessfn = access_lor_other,
6769 .type = ARM_CP_CONST, .resetvalue = 0 },
6770 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6771 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
9bd268ba 6772 .access = PL1_R, .accessfn = access_lor_ns,
d8564ee4 6773 .type = ARM_CP_CONST, .resetvalue = 0 },
d8564ee4
RH
6774};
6775
967aa94f
RH
6776#ifdef TARGET_AARCH64
6777static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6778 bool isread)
6779{
6780 int el = arm_current_el(env);
6781
6782 if (el < 2 &&
6783 arm_feature(env, ARM_FEATURE_EL2) &&
6784 !(arm_hcr_el2_eff(env) & HCR_APK)) {
6785 return CP_ACCESS_TRAP_EL2;
6786 }
6787 if (el < 3 &&
6788 arm_feature(env, ARM_FEATURE_EL3) &&
6789 !(env->cp15.scr_el3 & SCR_APK)) {
6790 return CP_ACCESS_TRAP_EL3;
6791 }
6792 return CP_ACCESS_OK;
6793}
6794
6795static const ARMCPRegInfo pauth_reginfo[] = {
6796 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6797 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6798 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6799 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
967aa94f
RH
6800 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6801 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6802 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6803 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
967aa94f
RH
6804 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6805 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6806 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6807 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
967aa94f
RH
6808 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6809 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6810 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6811 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
967aa94f
RH
6812 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6813 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6814 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6815 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
967aa94f
RH
6816 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6817 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6818 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6819 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
967aa94f
RH
6820 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6821 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6822 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6823 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
967aa94f
RH
6824 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6825 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6826 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6827 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
967aa94f
RH
6828 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6829 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6830 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6831 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
967aa94f
RH
6832 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6833 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6834 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6835 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
967aa94f 6836};
de390645 6837
84940ed8
RC
6838static const ARMCPRegInfo tlbirange_reginfo[] = {
6839 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
6840 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
6841 .access = PL1_W, .type = ARM_CP_NO_RAW,
6842 .writefn = tlbi_aa64_rvae1is_write },
6843 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
6844 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
6845 .access = PL1_W, .type = ARM_CP_NO_RAW,
6846 .writefn = tlbi_aa64_rvae1is_write },
6847 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
6848 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
6849 .access = PL1_W, .type = ARM_CP_NO_RAW,
6850 .writefn = tlbi_aa64_rvae1is_write },
6851 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
6852 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
6853 .access = PL1_W, .type = ARM_CP_NO_RAW,
6854 .writefn = tlbi_aa64_rvae1is_write },
6855 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
6856 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
6857 .access = PL1_W, .type = ARM_CP_NO_RAW,
6858 .writefn = tlbi_aa64_rvae1is_write },
6859 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
6860 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
6861 .access = PL1_W, .type = ARM_CP_NO_RAW,
6862 .writefn = tlbi_aa64_rvae1is_write },
6863 { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
6864 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
6865 .access = PL1_W, .type = ARM_CP_NO_RAW,
6866 .writefn = tlbi_aa64_rvae1is_write },
6867 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
6868 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
6869 .access = PL1_W, .type = ARM_CP_NO_RAW,
6870 .writefn = tlbi_aa64_rvae1is_write },
6871 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
6872 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
6873 .access = PL1_W, .type = ARM_CP_NO_RAW,
6874 .writefn = tlbi_aa64_rvae1_write },
6875 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
6876 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
6877 .access = PL1_W, .type = ARM_CP_NO_RAW,
6878 .writefn = tlbi_aa64_rvae1_write },
6879 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
6880 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
6881 .access = PL1_W, .type = ARM_CP_NO_RAW,
6882 .writefn = tlbi_aa64_rvae1_write },
6883 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
6884 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
6885 .access = PL1_W, .type = ARM_CP_NO_RAW,
6886 .writefn = tlbi_aa64_rvae1_write },
6887 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
6888 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
6889 .access = PL2_W, .type = ARM_CP_NOP },
6890 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
6891 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
6892 .access = PL2_W, .type = ARM_CP_NOP },
6893 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
6894 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
6895 .access = PL2_W, .type = ARM_CP_NO_RAW,
6896 .writefn = tlbi_aa64_rvae2is_write },
6897 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
6898 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
6899 .access = PL2_W, .type = ARM_CP_NO_RAW,
6900 .writefn = tlbi_aa64_rvae2is_write },
6901 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
6902 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
6903 .access = PL2_W, .type = ARM_CP_NOP },
6904 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
6905 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
6906 .access = PL2_W, .type = ARM_CP_NOP },
6907 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
6908 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
6909 .access = PL2_W, .type = ARM_CP_NO_RAW,
6910 .writefn = tlbi_aa64_rvae2is_write },
6911 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
6912 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
6913 .access = PL2_W, .type = ARM_CP_NO_RAW,
6914 .writefn = tlbi_aa64_rvae2is_write },
6915 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
6916 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
6917 .access = PL2_W, .type = ARM_CP_NO_RAW,
6918 .writefn = tlbi_aa64_rvae2_write },
6919 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
6920 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
6921 .access = PL2_W, .type = ARM_CP_NO_RAW,
6922 .writefn = tlbi_aa64_rvae2_write },
6923 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
6924 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
6925 .access = PL3_W, .type = ARM_CP_NO_RAW,
6926 .writefn = tlbi_aa64_rvae3is_write },
6927 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
6928 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
6929 .access = PL3_W, .type = ARM_CP_NO_RAW,
6930 .writefn = tlbi_aa64_rvae3is_write },
6931 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
6932 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
6933 .access = PL3_W, .type = ARM_CP_NO_RAW,
6934 .writefn = tlbi_aa64_rvae3is_write },
6935 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
6936 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
6937 .access = PL3_W, .type = ARM_CP_NO_RAW,
6938 .writefn = tlbi_aa64_rvae3is_write },
6939 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
6940 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
6941 .access = PL3_W, .type = ARM_CP_NO_RAW,
6942 .writefn = tlbi_aa64_rvae3_write },
6943 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
6944 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
6945 .access = PL3_W, .type = ARM_CP_NO_RAW,
6946 .writefn = tlbi_aa64_rvae3_write },
84940ed8
RC
6947};
6948
7113d618
RC
6949static const ARMCPRegInfo tlbios_reginfo[] = {
6950 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
6951 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
6952 .access = PL1_W, .type = ARM_CP_NO_RAW,
6953 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
6954 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
6955 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
6956 .access = PL1_W, .type = ARM_CP_NO_RAW,
6957 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
6958 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
6959 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
6960 .access = PL1_W, .type = ARM_CP_NO_RAW,
6961 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
6962 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
6963 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
6964 .access = PL1_W, .type = ARM_CP_NO_RAW,
6965 .writefn = tlbi_aa64_vae1is_write },
6966 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
6967 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
6968 .access = PL1_W, .type = ARM_CP_NO_RAW,
6969 .writefn = tlbi_aa64_vae1is_write },
6970 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
6971 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
6972 .access = PL1_W, .type = ARM_CP_NO_RAW,
6973 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
6974 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
6975 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
6976 .access = PL2_W, .type = ARM_CP_NO_RAW,
6977 .writefn = tlbi_aa64_alle2is_write },
b7469ef9
IH
6978 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
6979 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
6980 .access = PL2_W, .type = ARM_CP_NO_RAW,
6981 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
6982 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
6983 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
6984 .access = PL2_W, .type = ARM_CP_NO_RAW,
6985 .writefn = tlbi_aa64_alle1is_write },
b7469ef9
IH
6986 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
6987 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
6988 .access = PL2_W, .type = ARM_CP_NO_RAW,
6989 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
6990 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
6991 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
6992 .access = PL2_W, .type = ARM_CP_NO_RAW,
6993 .writefn = tlbi_aa64_alle1is_write },
6994 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
6995 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
6996 .access = PL2_W, .type = ARM_CP_NOP },
6997 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
6998 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
6999 .access = PL2_W, .type = ARM_CP_NOP },
7000 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7001 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7002 .access = PL2_W, .type = ARM_CP_NOP },
7003 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7004 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7005 .access = PL2_W, .type = ARM_CP_NOP },
7006 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7007 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7008 .access = PL3_W, .type = ARM_CP_NO_RAW,
7009 .writefn = tlbi_aa64_alle3is_write },
b7469ef9
IH
7010 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7011 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7012 .access = PL3_W, .type = ARM_CP_NO_RAW,
7013 .writefn = tlbi_aa64_vae3is_write },
7014 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7015 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7016 .access = PL3_W, .type = ARM_CP_NO_RAW,
7017 .writefn = tlbi_aa64_vae3is_write },
7113d618
RC
7018};
7019
de390645
RH
7020static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7021{
7022 Error *err = NULL;
7023 uint64_t ret;
7024
7025 /* Success sets NZCV = 0000. */
7026 env->NF = env->CF = env->VF = 0, env->ZF = 1;
7027
7028 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7029 /*
7030 * ??? Failed, for unknown reasons in the crypto subsystem.
7031 * The best we can do is log the reason and return the
7032 * timed-out indication to the guest. There is no reason
7033 * we know to expect this failure to be transitory, so the
7034 * guest may well hang retrying the operation.
7035 */
7036 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7037 ri->name, error_get_pretty(err));
7038 error_free(err);
7039
7040 env->ZF = 0; /* NZCF = 0100 */
7041 return 0;
7042 }
7043 return ret;
7044}
7045
7046/* We do not support re-seeding, so the two registers operate the same. */
7047static const ARMCPRegInfo rndr_reginfo[] = {
7048 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7049 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7050 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7051 .access = PL0_R, .readfn = rndr_readfn },
7052 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7053 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7054 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7055 .access = PL0_R, .readfn = rndr_readfn },
de390645 7056};
0d57b499
BM
7057
7058#ifndef CONFIG_USER_ONLY
7059static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7060 uint64_t value)
7061{
7062 ARMCPU *cpu = env_archcpu(env);
7063 /* CTR_EL0 System register -> DminLine, bits [19:16] */
7064 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7065 uint64_t vaddr_in = (uint64_t) value;
7066 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7067 void *haddr;
7068 int mem_idx = cpu_mmu_index(env, false);
7069
7070 /* This won't be crossing page boundaries */
7071 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7072 if (haddr) {
7073
7074 ram_addr_t offset;
7075 MemoryRegion *mr;
7076
7077 /* RCU lock is already being held */
7078 mr = memory_region_from_host(haddr, &offset);
7079
7080 if (mr) {
4dfe59d1 7081 memory_region_writeback(mr, offset, dline_size);
0d57b499
BM
7082 }
7083 }
7084}
7085
7086static const ARMCPRegInfo dcpop_reg[] = {
7087 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7088 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7089 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 7090 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
7091};
7092
7093static const ARMCPRegInfo dcpodp_reg[] = {
7094 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7095 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7096 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 7097 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
7098};
7099#endif /*CONFIG_USER_ONLY*/
7100
4b779ceb
RH
7101static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7102 bool isread)
7103{
7104 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7105 return CP_ACCESS_TRAP_EL2;
7106 }
7107
7108 return CP_ACCESS_OK;
7109}
7110
7111static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7112 bool isread)
7113{
7114 int el = arm_current_el(env);
7115
0da067f2 7116 if (el < 2 && arm_is_el2_enabled(env)) {
4301acd7
RH
7117 uint64_t hcr = arm_hcr_el2_eff(env);
7118 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7119 return CP_ACCESS_TRAP_EL2;
7120 }
4b779ceb
RH
7121 }
7122 if (el < 3 &&
7123 arm_feature(env, ARM_FEATURE_EL3) &&
7124 !(env->cp15.scr_el3 & SCR_ATA)) {
7125 return CP_ACCESS_TRAP_EL3;
7126 }
7127 return CP_ACCESS_OK;
7128}
7129
7130static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7131{
7132 return env->pstate & PSTATE_TCO;
7133}
7134
7135static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7136{
7137 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7138}
7139
7140static const ARMCPRegInfo mte_reginfo[] = {
7141 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7142 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7143 .access = PL1_RW, .accessfn = access_mte,
7144 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7145 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7146 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7147 .access = PL1_RW, .accessfn = access_mte,
7148 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7149 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7150 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7151 .access = PL2_RW, .accessfn = access_mte,
7152 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7153 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7154 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7155 .access = PL3_RW,
7156 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7157 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7158 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7159 .access = PL1_RW, .accessfn = access_mte,
7160 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7161 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7162 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7163 .access = PL1_RW, .accessfn = access_mte,
7164 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7165 { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7166 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7167 .access = PL1_R, .accessfn = access_aa64_tid5,
7168 .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7169 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7170 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7171 .type = ARM_CP_NO_RAW,
7172 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
5463df16
RH
7173 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7174 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7175 .type = ARM_CP_NOP, .access = PL1_W,
7176 .accessfn = aa64_cacheop_poc_access },
7177 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7178 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7179 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7180 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7181 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7182 .type = ARM_CP_NOP, .access = PL1_W,
7183 .accessfn = aa64_cacheop_poc_access },
7184 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7185 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7186 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7187 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7188 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7189 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7190 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7191 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7192 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7193 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7194 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7195 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7196 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7197 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7198 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
4b779ceb
RH
7199};
7200
7201static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7202 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7203 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7204 .type = ARM_CP_CONST, .access = PL0_RW, },
4b779ceb 7205};
5463df16
RH
7206
7207static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7208 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7209 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7210 .type = ARM_CP_NOP, .access = PL0_W,
7211 .accessfn = aa64_cacheop_poc_access },
7212 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7213 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7214 .type = ARM_CP_NOP, .access = PL0_W,
7215 .accessfn = aa64_cacheop_poc_access },
7216 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7217 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7218 .type = ARM_CP_NOP, .access = PL0_W,
7219 .accessfn = aa64_cacheop_poc_access },
7220 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7221 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7222 .type = ARM_CP_NOP, .access = PL0_W,
7223 .accessfn = aa64_cacheop_poc_access },
7224 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7225 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7226 .type = ARM_CP_NOP, .access = PL0_W,
7227 .accessfn = aa64_cacheop_poc_access },
7228 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7229 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7230 .type = ARM_CP_NOP, .access = PL0_W,
7231 .accessfn = aa64_cacheop_poc_access },
7232 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7233 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7234 .type = ARM_CP_NOP, .access = PL0_W,
7235 .accessfn = aa64_cacheop_poc_access },
7236 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7237 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7238 .type = ARM_CP_NOP, .access = PL0_W,
7239 .accessfn = aa64_cacheop_poc_access },
eb821168
RH
7240 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7241 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7242 .access = PL0_W, .type = ARM_CP_DC_GVA,
7243#ifndef CONFIG_USER_ONLY
7244 /* Avoid overhead of an access check that always passes in user-mode */
7245 .accessfn = aa64_zva_access,
7246#endif
7247 },
7248 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7249 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7250 .access = PL0_W, .type = ARM_CP_DC_GZVA,
7251#ifndef CONFIG_USER_ONLY
7252 /* Avoid overhead of an access check that always passes in user-mode */
7253 .accessfn = aa64_zva_access,
7254#endif
7255 },
5463df16
RH
7256};
7257
967aa94f
RH
7258#endif
7259
cb570bd3
RH
7260static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7261 bool isread)
7262{
7263 int el = arm_current_el(env);
7264
7265 if (el == 0) {
7266 uint64_t sctlr = arm_sctlr(env, el);
7267 if (!(sctlr & SCTLR_EnRCTX)) {
7268 return CP_ACCESS_TRAP;
7269 }
7270 } else if (el == 1) {
7271 uint64_t hcr = arm_hcr_el2_eff(env);
7272 if (hcr & HCR_NV) {
7273 return CP_ACCESS_TRAP_EL2;
7274 }
7275 }
7276 return CP_ACCESS_OK;
7277}
7278
7279static const ARMCPRegInfo predinv_reginfo[] = {
7280 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7281 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7282 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7283 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7284 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7285 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7286 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7287 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7288 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7289 /*
7290 * Note the AArch32 opcodes have a different OPC1.
7291 */
7292 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7293 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7294 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7295 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7296 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7297 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7298 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7299 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7300 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
cb570bd3
RH
7301};
7302
957e6155
PM
7303static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7304{
7305 /* Read the high 32 bits of the current CCSIDR */
7306 return extract64(ccsidr_read(env, ri), 32, 32);
7307}
7308
7309static const ARMCPRegInfo ccsidr2_reginfo[] = {
7310 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7311 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7312 .access = PL1_R,
7313 .accessfn = access_aa64_tid2,
7314 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
957e6155
PM
7315};
7316
6a4ef4e5
MZ
7317static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7318 bool isread)
7319{
7320 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7321 return CP_ACCESS_TRAP_EL2;
7322 }
7323
7324 return CP_ACCESS_OK;
7325}
7326
7327static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7328 bool isread)
7329{
7330 if (arm_feature(env, ARM_FEATURE_V8)) {
7331 return access_aa64_tid3(env, ri, isread);
7332 }
7333
7334 return CP_ACCESS_OK;
7335}
7336
f96f3d5f
MZ
7337static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7338 bool isread)
7339{
7340 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7341 return CP_ACCESS_TRAP_EL2;
7342 }
7343
7344 return CP_ACCESS_OK;
7345}
7346
8e228c9e
PM
7347static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7348 const ARMCPRegInfo *ri, bool isread)
7349{
7350 /*
7351 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7352 * in v7A, not in v8A.
7353 */
7354 if (!arm_feature(env, ARM_FEATURE_V8) &&
7355 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7356 (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7357 return CP_ACCESS_TRAP_EL2;
7358 }
7359 return CP_ACCESS_OK;
7360}
7361
f96f3d5f
MZ
7362static const ARMCPRegInfo jazelle_regs[] = {
7363 { .name = "JIDR",
7364 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7365 .access = PL1_R, .accessfn = access_jazelle,
7366 .type = ARM_CP_CONST, .resetvalue = 0 },
7367 { .name = "JOSCR",
7368 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7369 .accessfn = access_joscr_jmcr,
f96f3d5f
MZ
7370 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7371 { .name = "JMCR",
7372 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7373 .accessfn = access_joscr_jmcr,
f96f3d5f 7374 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
f96f3d5f
MZ
7375};
7376
e2a1a461
RH
7377static const ARMCPRegInfo vhe_reginfo[] = {
7378 { .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7379 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7380 .access = PL2_RW,
7381 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2]) },
ed30da8e
RH
7382 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7383 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7384 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7385 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8c94b071
RH
7386#ifndef CONFIG_USER_ONLY
7387 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7388 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7389 .fieldoffset =
7390 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7391 .type = ARM_CP_IO, .access = PL2_RW,
7392 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7393 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7394 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7395 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7396 .resetfn = gt_hv_timer_reset,
7397 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7398 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7399 .type = ARM_CP_IO,
7400 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7401 .access = PL2_RW,
7402 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7403 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
bb5972e4
RH
7404 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7405 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7406 .type = ARM_CP_IO | ARM_CP_ALIAS,
7407 .access = PL2_RW, .accessfn = e2h_access,
7408 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7409 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7410 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7411 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7412 .type = ARM_CP_IO | ARM_CP_ALIAS,
7413 .access = PL2_RW, .accessfn = e2h_access,
7414 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7415 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7416 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7417 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7418 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7419 .access = PL2_RW, .accessfn = e2h_access,
7420 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7421 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7422 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7423 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7424 .access = PL2_RW, .accessfn = e2h_access,
7425 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7426 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7427 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7428 .type = ARM_CP_IO | ARM_CP_ALIAS,
7429 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7430 .access = PL2_RW, .accessfn = e2h_access,
7431 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7432 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7433 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7434 .type = ARM_CP_IO | ARM_CP_ALIAS,
7435 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7436 .access = PL2_RW, .accessfn = e2h_access,
7437 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8c94b071 7438#endif
e2a1a461
RH
7439};
7440
04b07d29
RH
7441#ifndef CONFIG_USER_ONLY
7442static const ARMCPRegInfo ats1e1_reginfo[] = {
7443 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7444 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7445 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7446 .writefn = ats_write64 },
7447 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7448 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7449 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7450 .writefn = ats_write64 },
04b07d29
RH
7451};
7452
7453static const ARMCPRegInfo ats1cp_reginfo[] = {
7454 { .name = "ATS1CPRP",
7455 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7456 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7457 .writefn = ats_write },
7458 { .name = "ATS1CPWP",
7459 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7460 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7461 .writefn = ats_write },
04b07d29
RH
7462};
7463#endif
7464
f6287c24
PM
7465/*
7466 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7467 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7468 * is non-zero, which is never for ARMv7, optionally in ARMv8
7469 * and mandatorily for ARMv8.2 and up.
7470 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7471 * implementation is RAZ/WI we can ignore this detail, as we
7472 * do for ACTLR.
7473 */
7474static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7475 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7476 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
99602377
RH
7477 .access = PL1_RW, .accessfn = access_tacr,
7478 .type = ARM_CP_CONST, .resetvalue = 0 },
f6287c24
PM
7479 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7480 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7481 .access = PL2_RW, .type = ARM_CP_CONST,
7482 .resetvalue = 0 },
f6287c24
PM
7483};
7484
2ceb98c0
PM
7485void register_cp_regs_for_features(ARMCPU *cpu)
7486{
7487 /* Register all the coprocessor registers based on feature bits */
7488 CPUARMState *env = &cpu->env;
7489 if (arm_feature(env, ARM_FEATURE_M)) {
7490 /* M profile has no coprocessor registers */
7491 return;
7492 }
7493
e9aa6c21 7494 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
7495 if (!arm_feature(env, ARM_FEATURE_V8)) {
7496 /* Must go early as it is full of wildcards that may be
7497 * overridden by later definitions.
7498 */
7499 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7500 }
7501
7d57f408 7502 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
7503 /* The ID registers all have impdef reset values */
7504 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
7505 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7506 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7507 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7508 .accessfn = access_aa32_tid3,
8a130a7b 7509 .resetvalue = cpu->isar.id_pfr0 },
96a8b92e
PM
7510 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7511 * the value of the GIC field until after we define these regs.
7512 */
0ff644a7
PM
7513 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7514 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e 7515 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 7516 .accessfn = access_aa32_tid3,
96a8b92e
PM
7517 .readfn = id_pfr1_read,
7518 .writefn = arm_cp_write_ignore },
0ff644a7
PM
7519 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7520 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7521 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7522 .accessfn = access_aa32_tid3,
a6179538 7523 .resetvalue = cpu->isar.id_dfr0 },
0ff644a7
PM
7524 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7525 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7526 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7527 .accessfn = access_aa32_tid3,
8515a092 7528 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
7529 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7530 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7531 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7532 .accessfn = access_aa32_tid3,
10054016 7533 .resetvalue = cpu->isar.id_mmfr0 },
0ff644a7
PM
7534 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7535 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7536 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7537 .accessfn = access_aa32_tid3,
10054016 7538 .resetvalue = cpu->isar.id_mmfr1 },
0ff644a7
PM
7539 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7540 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7541 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7542 .accessfn = access_aa32_tid3,
10054016 7543 .resetvalue = cpu->isar.id_mmfr2 },
0ff644a7
PM
7544 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7545 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7546 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7547 .accessfn = access_aa32_tid3,
10054016 7548 .resetvalue = cpu->isar.id_mmfr3 },
0ff644a7
PM
7549 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7550 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7551 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7552 .accessfn = access_aa32_tid3,
47576b94 7553 .resetvalue = cpu->isar.id_isar0 },
0ff644a7
PM
7554 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7555 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7556 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7557 .accessfn = access_aa32_tid3,
47576b94 7558 .resetvalue = cpu->isar.id_isar1 },
0ff644a7
PM
7559 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7560 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7561 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7562 .accessfn = access_aa32_tid3,
47576b94 7563 .resetvalue = cpu->isar.id_isar2 },
0ff644a7
PM
7564 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7565 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7566 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7567 .accessfn = access_aa32_tid3,
47576b94 7568 .resetvalue = cpu->isar.id_isar3 },
0ff644a7
PM
7569 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7570 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7571 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7572 .accessfn = access_aa32_tid3,
47576b94 7573 .resetvalue = cpu->isar.id_isar4 },
0ff644a7
PM
7574 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7575 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7576 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7577 .accessfn = access_aa32_tid3,
47576b94 7578 .resetvalue = cpu->isar.id_isar5 },
e20d84c1
PM
7579 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7580 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7581 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7582 .accessfn = access_aa32_tid3,
10054016 7583 .resetvalue = cpu->isar.id_mmfr4 },
802abf40 7584 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7585 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7586 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7587 .accessfn = access_aa32_tid3,
47576b94 7588 .resetvalue = cpu->isar.id_isar6 },
8515a092
PM
7589 };
7590 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
7591 define_arm_cp_regs(cpu, v6_cp_reginfo);
7592 } else {
7593 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7594 }
4d31c596
PM
7595 if (arm_feature(env, ARM_FEATURE_V6K)) {
7596 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7597 }
5e5cf9e3 7598 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 7599 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
7600 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7601 }
327dd510
AL
7602 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7603 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7604 }
e9aa6c21 7605 if (arm_feature(env, ARM_FEATURE_V7)) {
776d4e5c 7606 ARMCPRegInfo clidr = {
7da845b0
PM
7607 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7608 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
630fcd4d
MZ
7609 .access = PL1_R, .type = ARM_CP_CONST,
7610 .accessfn = access_aa64_tid2,
7611 .resetvalue = cpu->clidr
776d4e5c 7612 };
776d4e5c 7613 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 7614 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 7615 define_debug_regs(cpu);
24183fb6 7616 define_pmu_regs(cpu);
7d57f408
PM
7617 } else {
7618 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 7619 }
b0d2b7d0 7620 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
7621 /* AArch64 ID registers, which all have impdef reset values.
7622 * Note that within the ID register ranges the unused slots
7623 * must all RAZ, not UNDEF; future architecture versions may
7624 * define new registers here.
7625 */
e60cef86 7626 ARMCPRegInfo v8_idregs[] = {
976b99b6
AB
7627 /*
7628 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7629 * emulation because we don't know the right value for the
7630 * GIC field until after we define these regs.
96a8b92e 7631 */
e60cef86
PM
7632 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7633 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
976b99b6
AB
7634 .access = PL1_R,
7635#ifdef CONFIG_USER_ONLY
7636 .type = ARM_CP_CONST,
7637 .resetvalue = cpu->isar.id_aa64pfr0
7638#else
7639 .type = ARM_CP_NO_RAW,
6a4ef4e5 7640 .accessfn = access_aa64_tid3,
96a8b92e 7641 .readfn = id_aa64pfr0_read,
976b99b6
AB
7642 .writefn = arm_cp_write_ignore
7643#endif
7644 },
e60cef86
PM
7645 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7646 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7647 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7648 .accessfn = access_aa64_tid3,
47576b94 7649 .resetvalue = cpu->isar.id_aa64pfr1},
e20d84c1
PM
7650 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7651 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7652 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7653 .accessfn = access_aa64_tid3,
e20d84c1
PM
7654 .resetvalue = 0 },
7655 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7656 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7657 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7658 .accessfn = access_aa64_tid3,
e20d84c1 7659 .resetvalue = 0 },
9516d772 7660 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7661 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7662 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7663 .accessfn = access_aa64_tid3,
2dc10fa2 7664 .resetvalue = cpu->isar.id_aa64zfr0 },
e20d84c1
PM
7665 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7666 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7667 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7668 .accessfn = access_aa64_tid3,
e20d84c1
PM
7669 .resetvalue = 0 },
7670 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7671 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7672 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7673 .accessfn = access_aa64_tid3,
e20d84c1
PM
7674 .resetvalue = 0 },
7675 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7676 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7677 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7678 .accessfn = access_aa64_tid3,
e20d84c1 7679 .resetvalue = 0 },
e60cef86
PM
7680 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7681 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7682 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7683 .accessfn = access_aa64_tid3,
2a609df8 7684 .resetvalue = cpu->isar.id_aa64dfr0 },
e60cef86
PM
7685 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7686 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7687 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7688 .accessfn = access_aa64_tid3,
2a609df8 7689 .resetvalue = cpu->isar.id_aa64dfr1 },
e20d84c1
PM
7690 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7691 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7692 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7693 .accessfn = access_aa64_tid3,
e20d84c1
PM
7694 .resetvalue = 0 },
7695 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7696 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7697 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7698 .accessfn = access_aa64_tid3,
e20d84c1 7699 .resetvalue = 0 },
e60cef86
PM
7700 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7701 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7702 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7703 .accessfn = access_aa64_tid3,
e60cef86
PM
7704 .resetvalue = cpu->id_aa64afr0 },
7705 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7706 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7707 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7708 .accessfn = access_aa64_tid3,
e60cef86 7709 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
7710 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7711 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7712 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7713 .accessfn = access_aa64_tid3,
e20d84c1
PM
7714 .resetvalue = 0 },
7715 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7716 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7717 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7718 .accessfn = access_aa64_tid3,
e20d84c1 7719 .resetvalue = 0 },
e60cef86
PM
7720 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7721 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7722 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7723 .accessfn = access_aa64_tid3,
47576b94 7724 .resetvalue = cpu->isar.id_aa64isar0 },
e60cef86
PM
7725 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7726 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7727 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7728 .accessfn = access_aa64_tid3,
47576b94 7729 .resetvalue = cpu->isar.id_aa64isar1 },
e20d84c1
PM
7730 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7731 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7732 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7733 .accessfn = access_aa64_tid3,
e20d84c1
PM
7734 .resetvalue = 0 },
7735 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7736 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7737 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7738 .accessfn = access_aa64_tid3,
e20d84c1
PM
7739 .resetvalue = 0 },
7740 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7741 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7742 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7743 .accessfn = access_aa64_tid3,
e20d84c1
PM
7744 .resetvalue = 0 },
7745 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7746 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7747 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7748 .accessfn = access_aa64_tid3,
e20d84c1
PM
7749 .resetvalue = 0 },
7750 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7751 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7752 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7753 .accessfn = access_aa64_tid3,
e20d84c1
PM
7754 .resetvalue = 0 },
7755 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7756 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7757 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7758 .accessfn = access_aa64_tid3,
e20d84c1 7759 .resetvalue = 0 },
e60cef86
PM
7760 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7761 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7762 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7763 .accessfn = access_aa64_tid3,
3dc91ddb 7764 .resetvalue = cpu->isar.id_aa64mmfr0 },
e60cef86
PM
7765 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7766 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7767 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7768 .accessfn = access_aa64_tid3,
3dc91ddb 7769 .resetvalue = cpu->isar.id_aa64mmfr1 },
64761e10 7770 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7771 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7772 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7773 .accessfn = access_aa64_tid3,
64761e10 7774 .resetvalue = cpu->isar.id_aa64mmfr2 },
e20d84c1
PM
7775 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7776 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7777 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7778 .accessfn = access_aa64_tid3,
e20d84c1
PM
7779 .resetvalue = 0 },
7780 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7781 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7782 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7783 .accessfn = access_aa64_tid3,
e20d84c1
PM
7784 .resetvalue = 0 },
7785 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7786 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7787 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7788 .accessfn = access_aa64_tid3,
e20d84c1
PM
7789 .resetvalue = 0 },
7790 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7791 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7792 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7793 .accessfn = access_aa64_tid3,
e20d84c1
PM
7794 .resetvalue = 0 },
7795 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7796 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7797 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7798 .accessfn = access_aa64_tid3,
e20d84c1 7799 .resetvalue = 0 },
a50c0f51
PM
7800 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7801 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7802 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7803 .accessfn = access_aa64_tid3,
47576b94 7804 .resetvalue = cpu->isar.mvfr0 },
a50c0f51
PM
7805 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7806 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7807 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7808 .accessfn = access_aa64_tid3,
47576b94 7809 .resetvalue = cpu->isar.mvfr1 },
a50c0f51
PM
7810 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7811 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7812 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7813 .accessfn = access_aa64_tid3,
47576b94 7814 .resetvalue = cpu->isar.mvfr2 },
e20d84c1
PM
7815 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7816 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7817 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7818 .accessfn = access_aa64_tid3,
e20d84c1 7819 .resetvalue = 0 },
1d51bc96 7820 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7821 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7822 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7823 .accessfn = access_aa64_tid3,
1d51bc96 7824 .resetvalue = cpu->isar.id_pfr2 },
e20d84c1
PM
7825 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7826 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7827 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7828 .accessfn = access_aa64_tid3,
e20d84c1
PM
7829 .resetvalue = 0 },
7830 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7831 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7832 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7833 .accessfn = access_aa64_tid3,
e20d84c1
PM
7834 .resetvalue = 0 },
7835 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7836 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7837 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7838 .accessfn = access_aa64_tid3,
e20d84c1 7839 .resetvalue = 0 },
4054bfa9
AF
7840 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7841 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7842 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7843 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
4054bfa9
AF
7844 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7845 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7846 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7847 .resetvalue = cpu->pmceid0 },
7848 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7849 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7850 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7851 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
4054bfa9
AF
7852 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7853 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7854 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7855 .resetvalue = cpu->pmceid1 },
e60cef86 7856 };
6c5c0fec 7857#ifdef CONFIG_USER_ONLY
10b0220e 7858 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
6c5c0fec
AB
7859 { .name = "ID_AA64PFR0_EL1",
7860 .exported_bits = 0x000f000f00ff0000,
7861 .fixed_bits = 0x0000000000000011 },
7862 { .name = "ID_AA64PFR1_EL1",
7863 .exported_bits = 0x00000000000000f0 },
d040242e
AB
7864 { .name = "ID_AA64PFR*_EL1_RESERVED",
7865 .is_glob = true },
6c5c0fec
AB
7866 { .name = "ID_AA64ZFR0_EL1" },
7867 { .name = "ID_AA64MMFR0_EL1",
7868 .fixed_bits = 0x00000000ff000000 },
7869 { .name = "ID_AA64MMFR1_EL1" },
d040242e
AB
7870 { .name = "ID_AA64MMFR*_EL1_RESERVED",
7871 .is_glob = true },
6c5c0fec
AB
7872 { .name = "ID_AA64DFR0_EL1",
7873 .fixed_bits = 0x0000000000000006 },
7874 { .name = "ID_AA64DFR1_EL1" },
d040242e
AB
7875 { .name = "ID_AA64DFR*_EL1_RESERVED",
7876 .is_glob = true },
7877 { .name = "ID_AA64AFR*",
7878 .is_glob = true },
6c5c0fec
AB
7879 { .name = "ID_AA64ISAR0_EL1",
7880 .exported_bits = 0x00fffffff0fffff0 },
7881 { .name = "ID_AA64ISAR1_EL1",
7882 .exported_bits = 0x000000f0ffffffff },
d040242e
AB
7883 { .name = "ID_AA64ISAR*_EL1_RESERVED",
7884 .is_glob = true },
6c5c0fec
AB
7885 };
7886 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7887#endif
be8e8128
GB
7888 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7889 if (!arm_feature(env, ARM_FEATURE_EL3) &&
7890 !arm_feature(env, ARM_FEATURE_EL2)) {
7891 ARMCPRegInfo rvbar = {
7892 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7893 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
7894 .access = PL1_R,
7895 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
be8e8128
GB
7896 };
7897 define_one_arm_cp_reg(cpu, &rvbar);
7898 }
e60cef86 7899 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
7900 define_arm_cp_regs(cpu, v8_cp_reginfo);
7901 }
3b685ba7 7902 if (arm_feature(env, ARM_FEATURE_EL2)) {
f0d574d6 7903 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
7904 ARMCPRegInfo vpidr_regs[] = {
7905 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7906 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7907 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
7908 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS,
7909 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
7910 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7911 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7912 .access = PL2_RW, .resetvalue = cpu->midr,
7913 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7914 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7915 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7916 .access = PL2_RW, .accessfn = access_el3_aa32ns,
36476562
PM
7917 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS,
7918 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
7919 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7920 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7921 .access = PL2_RW,
7922 .resetvalue = vmpidr_def,
7923 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
7924 };
7925 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 7926 define_arm_cp_regs(cpu, el2_cp_reginfo);
ce4afed8
PM
7927 if (arm_feature(env, ARM_FEATURE_V8)) {
7928 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7929 }
e9152ee9
RDC
7930 if (cpu_isar_feature(aa64_sel2, cpu)) {
7931 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
7932 }
be8e8128
GB
7933 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7934 if (!arm_feature(env, ARM_FEATURE_EL3)) {
7935 ARMCPRegInfo rvbar = {
7936 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7937 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
7938 .access = PL2_R,
7939 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
be8e8128
GB
7940 };
7941 define_one_arm_cp_reg(cpu, &rvbar);
7942 }
d42e3c26
EI
7943 } else {
7944 /* If EL2 is missing but higher ELs are enabled, we need to
7945 * register the no_el2 reginfos.
7946 */
7947 if (arm_feature(env, ARM_FEATURE_EL3)) {
f0d574d6
EI
7948 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
7949 * of MIDR_EL1 and MPIDR_EL1.
731de9e6
EI
7950 */
7951 ARMCPRegInfo vpidr_regs[] = {
7952 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7953 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
93dd1e61 7954 .access = PL2_RW, .accessfn = access_el3_aa32ns,
731de9e6
EI
7955 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
7956 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7957 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
7958 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
93dd1e61 7959 .access = PL2_RW, .accessfn = access_el3_aa32ns,
f0d574d6
EI
7960 .type = ARM_CP_NO_RAW,
7961 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
731de9e6
EI
7962 };
7963 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 7964 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
ce4afed8
PM
7965 if (arm_feature(env, ARM_FEATURE_V8)) {
7966 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo);
7967 }
d42e3c26 7968 }
3b685ba7 7969 }
81547d66 7970 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 7971 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
7972 ARMCPRegInfo el3_regs[] = {
7973 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7974 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
7975 .access = PL3_R,
7976 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7977 },
e24fdd23
PM
7978 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7979 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7980 .access = PL3_RW,
7981 .raw_writefn = raw_write, .writefn = sctlr_write,
7982 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7983 .resetvalue = cpu->reset_sctlr },
be8e8128 7984 };
e24fdd23
PM
7985
7986 define_arm_cp_regs(cpu, el3_regs);
81547d66 7987 }
2f027fc5
PM
7988 /* The behaviour of NSACR is sufficiently various that we don't
7989 * try to describe it in a single reginfo:
7990 * if EL3 is 64 bit, then trap to EL3 from S EL1,
7991 * reads as constant 0xc00 from NS EL1 and NS EL2
7992 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7993 * if v7 without EL3, register doesn't exist
7994 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7995 */
7996 if (arm_feature(env, ARM_FEATURE_EL3)) {
7997 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
10b0220e 7998 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
7999 .name = "NSACR", .type = ARM_CP_CONST,
8000 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8001 .access = PL1_RW, .accessfn = nsacr_access,
8002 .resetvalue = 0xc00
8003 };
8004 define_one_arm_cp_reg(cpu, &nsacr);
8005 } else {
10b0220e 8006 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8007 .name = "NSACR",
8008 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8009 .access = PL3_RW | PL1_R,
8010 .resetvalue = 0,
8011 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8012 };
8013 define_one_arm_cp_reg(cpu, &nsacr);
8014 }
8015 } else {
8016 if (arm_feature(env, ARM_FEATURE_V8)) {
10b0220e 8017 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8018 .name = "NSACR", .type = ARM_CP_CONST,
8019 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8020 .access = PL1_R,
8021 .resetvalue = 0xc00
8022 };
8023 define_one_arm_cp_reg(cpu, &nsacr);
8024 }
8025 }
8026
452a0955 8027 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
8028 if (arm_feature(env, ARM_FEATURE_V6)) {
8029 /* PMSAv6 not implemented */
8030 assert(arm_feature(env, ARM_FEATURE_V7));
8031 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8032 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8033 } else {
8034 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8035 }
18032bec 8036 } else {
8e5d75c9 8037 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec 8038 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4036b7d1
PM
8039 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
8040 if (cpu_isar_feature(aa32_hpd, cpu)) {
ab638a32
RH
8041 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8042 }
18032bec 8043 }
c326b979
PM
8044 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8045 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8046 }
6cc7a3ae
PM
8047 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8048 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8049 }
4a501606
PM
8050 if (arm_feature(env, ARM_FEATURE_VAPA)) {
8051 define_arm_cp_regs(cpu, vapa_cp_reginfo);
8052 }
c4804214
PM
8053 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8054 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8055 }
8056 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8057 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8058 }
8059 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8060 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8061 }
18032bec
PM
8062 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8063 define_arm_cp_regs(cpu, omap_cp_reginfo);
8064 }
34f90529
PM
8065 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8066 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8067 }
1047b9d7
PM
8068 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8069 define_arm_cp_regs(cpu, xscale_cp_reginfo);
8070 }
8071 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8072 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8073 }
7ac681cf
PM
8074 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8075 define_arm_cp_regs(cpu, lpae_cp_reginfo);
8076 }
873b73c0 8077 if (cpu_isar_feature(aa32_jazelle, cpu)) {
f96f3d5f
MZ
8078 define_arm_cp_regs(cpu, jazelle_regs);
8079 }
7884849c
PM
8080 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
8081 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8082 * be read-only (ie write causes UNDEF exception).
8083 */
8084 {
00a29f3d
PM
8085 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8086 /* Pre-v8 MIDR space.
8087 * Note that the MIDR isn't a simple constant register because
7884849c
PM
8088 * of the TI925 behaviour where writes to another register can
8089 * cause the MIDR value to change.
97ce8d61
PC
8090 *
8091 * Unimplemented registers in the c15 0 0 0 space default to
8092 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8093 * and friends override accordingly.
7884849c
PM
8094 */
8095 { .name = "MIDR",
97ce8d61 8096 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 8097 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 8098 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 8099 .readfn = midr_read,
97ce8d61
PC
8100 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8101 .type = ARM_CP_OVERRIDE },
7884849c
PM
8102 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8103 { .name = "DUMMY",
8104 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8105 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8106 { .name = "DUMMY",
8107 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8108 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8109 { .name = "DUMMY",
8110 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8111 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8112 { .name = "DUMMY",
8113 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8114 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8115 { .name = "DUMMY",
8116 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8117 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7884849c 8118 };
00a29f3d 8119 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
8120 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8121 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
8122 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8123 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8124 .readfn = midr_read },
ac00c79f
SF
8125 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
8126 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8127 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8128 .access = PL1_R, .resetvalue = cpu->midr },
8129 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8130 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8131 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
8132 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8133 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
93fbc983
MZ
8134 .access = PL1_R,
8135 .accessfn = access_aa64_tid1,
8136 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
8137 };
8138 ARMCPRegInfo id_cp_reginfo[] = {
8139 /* These are common to v8 and pre-v8 */
8140 { .name = "CTR",
8141 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
630fcd4d
MZ
8142 .access = PL1_R, .accessfn = ctr_el0_access,
8143 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
00a29f3d
PM
8144 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8145 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8146 .access = PL0_R, .accessfn = ctr_el0_access,
8147 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8148 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8149 { .name = "TCMTR",
8150 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
93fbc983
MZ
8151 .access = PL1_R,
8152 .accessfn = access_aa32_tid1,
8153 .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d 8154 };
8085ce63
PC
8155 /* TLBTR is specific to VMSA */
8156 ARMCPRegInfo id_tlbtr_reginfo = {
8157 .name = "TLBTR",
8158 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
93fbc983
MZ
8159 .access = PL1_R,
8160 .accessfn = access_aa32_tid1,
8161 .type = ARM_CP_CONST, .resetvalue = 0,
8085ce63 8162 };
3281af81
PC
8163 /* MPUIR is specific to PMSA V6+ */
8164 ARMCPRegInfo id_mpuir_reginfo = {
8165 .name = "MPUIR",
8166 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8167 .access = PL1_R, .type = ARM_CP_CONST,
8168 .resetvalue = cpu->pmsav7_dregion << 8
8169 };
10b0220e 8170 static const ARMCPRegInfo crn0_wi_reginfo = {
7884849c
PM
8171 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8172 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8173 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8174 };
6c5c0fec 8175#ifdef CONFIG_USER_ONLY
10b0220e 8176 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
6c5c0fec
AB
8177 { .name = "MIDR_EL1",
8178 .exported_bits = 0x00000000ffffffff },
8179 { .name = "REVIDR_EL1" },
6c5c0fec
AB
8180 };
8181 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8182#endif
7884849c
PM
8183 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8184 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5809ac57 8185 size_t i;
7884849c 8186 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
8187 * whole space. Then update the specific ID registers to allow write
8188 * access, so that they ignore writes rather than causing them to
8189 * UNDEF.
7884849c
PM
8190 */
8191 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
5809ac57
RH
8192 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8193 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
00a29f3d 8194 }
5809ac57
RH
8195 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8196 id_cp_reginfo[i].access = PL1_RW;
7884849c 8197 }
10006112 8198 id_mpuir_reginfo.access = PL1_RW;
3281af81 8199 id_tlbtr_reginfo.access = PL1_RW;
7884849c 8200 }
00a29f3d
PM
8201 if (arm_feature(env, ARM_FEATURE_V8)) {
8202 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8203 } else {
8204 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8205 }
a703eda1 8206 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 8207 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 8208 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
8209 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8210 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 8211 }
7884849c
PM
8212 }
8213
97ce8d61 8214 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
52264166
AB
8215 ARMCPRegInfo mpidr_cp_reginfo[] = {
8216 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8217 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8218 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
52264166
AB
8219 };
8220#ifdef CONFIG_USER_ONLY
10b0220e 8221 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
52264166
AB
8222 { .name = "MPIDR_EL1",
8223 .fixed_bits = 0x0000000080000000 },
52264166
AB
8224 };
8225 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8226#endif
97ce8d61
PC
8227 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8228 }
8229
2771db27 8230 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
8231 ARMCPRegInfo auxcr_reginfo[] = {
8232 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8233 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
99602377
RH
8234 .access = PL1_RW, .accessfn = access_tacr,
8235 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
834a6c69
PM
8236 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8237 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8238 .access = PL2_RW, .type = ARM_CP_CONST,
8239 .resetvalue = 0 },
8240 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8241 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8242 .access = PL3_RW, .type = ARM_CP_CONST,
8243 .resetvalue = 0 },
2771db27 8244 };
834a6c69 8245 define_arm_cp_regs(cpu, auxcr_reginfo);
f6287c24
PM
8246 if (cpu_isar_feature(aa32_ac2, cpu)) {
8247 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
0e0456ab 8248 }
2771db27
PM
8249 }
8250
d8ba780b 8251 if (arm_feature(env, ARM_FEATURE_CBAR)) {
d56974af
LM
8252 /*
8253 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8254 * There are two flavours:
8255 * (1) older 32-bit only cores have a simple 32-bit CBAR
8256 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8257 * 32-bit register visible to AArch32 at a different encoding
8258 * to the "flavour 1" register and with the bits rearranged to
8259 * be able to squash a 64-bit address into the 32-bit view.
8260 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8261 * in future if we support AArch32-only configs of some of the
8262 * AArch64 cores we might need to add a specific feature flag
8263 * to indicate cores with "flavour 2" CBAR.
8264 */
f318cec6
PM
8265 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8266 /* 32 bit view is [31:18] 0...0 [43:32]. */
8267 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8268 | extract64(cpu->reset_cbar, 32, 12);
8269 ARMCPRegInfo cbar_reginfo[] = {
8270 { .name = "CBAR",
8271 .type = ARM_CP_CONST,
d56974af
LM
8272 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8273 .access = PL1_R, .resetvalue = cbar32 },
f318cec6
PM
8274 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8275 .type = ARM_CP_CONST,
8276 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
d56974af 8277 .access = PL1_R, .resetvalue = cpu->reset_cbar },
f318cec6
PM
8278 };
8279 /* We don't implement a r/w 64 bit CBAR currently */
8280 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8281 define_arm_cp_regs(cpu, cbar_reginfo);
8282 } else {
8283 ARMCPRegInfo cbar = {
8284 .name = "CBAR",
8285 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8286 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8287 .fieldoffset = offsetof(CPUARMState,
8288 cp15.c15_config_base_address)
8289 };
8290 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8291 cbar.access = PL1_R;
8292 cbar.fieldoffset = 0;
8293 cbar.type = ARM_CP_CONST;
8294 }
8295 define_one_arm_cp_reg(cpu, &cbar);
8296 }
d8ba780b
PC
8297 }
8298
91db4642 8299 if (arm_feature(env, ARM_FEATURE_VBAR)) {
10b0220e 8300 static const ARMCPRegInfo vbar_cp_reginfo[] = {
91db4642
CLG
8301 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8302 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8303 .access = PL1_RW, .writefn = vbar_write,
8304 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8305 offsetof(CPUARMState, cp15.vbar_ns) },
8306 .resetvalue = 0 },
91db4642
CLG
8307 };
8308 define_arm_cp_regs(cpu, vbar_cp_reginfo);
8309 }
8310
2771db27
PM
8311 /* Generic registers whose values depend on the implementation */
8312 {
8313 ARMCPRegInfo sctlr = {
5ebafdf3 8314 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9 8315 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
84929218 8316 .access = PL1_RW, .accessfn = access_tvm_trvm,
137feaa9
FA
8317 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8318 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
8319 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8320 .raw_writefn = raw_write,
2771db27
PM
8321 };
8322 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8323 /* Normally we would always end the TB on an SCTLR write, but Linux
8324 * arch/arm/mach-pxa/sleep.S expects two instructions following
8325 * an MMU enable to execute from cache. Imitate this behaviour.
8326 */
8327 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8328 }
8329 define_one_arm_cp_reg(cpu, &sctlr);
8330 }
5be5e8ed 8331
2d7137c1 8332 if (cpu_isar_feature(aa64_lor, cpu)) {
2d7137c1
RH
8333 define_arm_cp_regs(cpu, lor_reginfo);
8334 }
220f508f
RH
8335 if (cpu_isar_feature(aa64_pan, cpu)) {
8336 define_one_arm_cp_reg(cpu, &pan_reginfo);
8337 }
04b07d29
RH
8338#ifndef CONFIG_USER_ONLY
8339 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8340 define_arm_cp_regs(cpu, ats1e1_reginfo);
8341 }
8342 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8343 define_arm_cp_regs(cpu, ats1cp_reginfo);
8344 }
8345#endif
9eeb7a1c
RH
8346 if (cpu_isar_feature(aa64_uao, cpu)) {
8347 define_one_arm_cp_reg(cpu, &uao_reginfo);
8348 }
2d7137c1 8349
dc8b1853
RC
8350 if (cpu_isar_feature(aa64_dit, cpu)) {
8351 define_one_arm_cp_reg(cpu, &dit_reginfo);
8352 }
f2f68a78
RC
8353 if (cpu_isar_feature(aa64_ssbs, cpu)) {
8354 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8355 }
dc8b1853 8356
e2a1a461
RH
8357 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8358 define_arm_cp_regs(cpu, vhe_reginfo);
8359 }
8360
cd208a1c 8361 if (cpu_isar_feature(aa64_sve, cpu)) {
5be5e8ed
RH
8362 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo);
8363 if (arm_feature(env, ARM_FEATURE_EL2)) {
8364 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo);
8365 } else {
8366 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo);
8367 }
8368 if (arm_feature(env, ARM_FEATURE_EL3)) {
8369 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo);
8370 }
8371 }
967aa94f
RH
8372
8373#ifdef TARGET_AARCH64
8374 if (cpu_isar_feature(aa64_pauth, cpu)) {
8375 define_arm_cp_regs(cpu, pauth_reginfo);
8376 }
de390645
RH
8377 if (cpu_isar_feature(aa64_rndr, cpu)) {
8378 define_arm_cp_regs(cpu, rndr_reginfo);
8379 }
84940ed8
RC
8380 if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8381 define_arm_cp_regs(cpu, tlbirange_reginfo);
8382 }
7113d618
RC
8383 if (cpu_isar_feature(aa64_tlbios, cpu)) {
8384 define_arm_cp_regs(cpu, tlbios_reginfo);
8385 }
0d57b499
BM
8386#ifndef CONFIG_USER_ONLY
8387 /* Data Cache clean instructions up to PoP */
8388 if (cpu_isar_feature(aa64_dcpop, cpu)) {
8389 define_one_arm_cp_reg(cpu, dcpop_reg);
8390
8391 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8392 define_one_arm_cp_reg(cpu, dcpodp_reg);
8393 }
8394 }
8395#endif /*CONFIG_USER_ONLY*/
4b779ceb
RH
8396
8397 /*
8398 * If full MTE is enabled, add all of the system registers.
8399 * If only "instructions available at EL0" are enabled,
8400 * then define only a RAZ/WI version of PSTATE.TCO.
8401 */
8402 if (cpu_isar_feature(aa64_mte, cpu)) {
8403 define_arm_cp_regs(cpu, mte_reginfo);
5463df16 8404 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb
RH
8405 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8406 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
5463df16 8407 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb 8408 }
967aa94f 8409#endif
cb570bd3 8410
22e57073 8411 if (cpu_isar_feature(any_predinv, cpu)) {
cb570bd3
RH
8412 define_arm_cp_regs(cpu, predinv_reginfo);
8413 }
e2cce18f 8414
957e6155
PM
8415 if (cpu_isar_feature(any_ccidx, cpu)) {
8416 define_arm_cp_regs(cpu, ccsidr2_reginfo);
8417 }
8418
e2cce18f
RH
8419#ifndef CONFIG_USER_ONLY
8420 /*
8421 * Register redirections and aliases must be done last,
8422 * after the registers from the other extensions have been defined.
8423 */
8424 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8425 define_arm_vh_e2h_redirects_aliases(cpu);
8426 }
8427#endif
2ceb98c0
PM
8428}
8429
777dc784
PM
8430/* Sort alphabetically by type name, except for "any". */
8431static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 8432{
777dc784
PM
8433 ObjectClass *class_a = (ObjectClass *)a;
8434 ObjectClass *class_b = (ObjectClass *)b;
8435 const char *name_a, *name_b;
5adb4839 8436
777dc784
PM
8437 name_a = object_class_get_name(class_a);
8438 name_b = object_class_get_name(class_b);
51492fd1 8439 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 8440 return 1;
51492fd1 8441 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
8442 return -1;
8443 } else {
8444 return strcmp(name_a, name_b);
5adb4839
PB
8445 }
8446}
8447
777dc784 8448static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 8449{
777dc784 8450 ObjectClass *oc = data;
51492fd1
AF
8451 const char *typename;
8452 char *name;
3371d272 8453
51492fd1
AF
8454 typename = object_class_get_name(oc);
8455 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
0442428a 8456 qemu_printf(" %s\n", name);
51492fd1 8457 g_free(name);
777dc784
PM
8458}
8459
0442428a 8460void arm_cpu_list(void)
777dc784 8461{
777dc784
PM
8462 GSList *list;
8463
8464 list = object_class_get_list(TYPE_ARM_CPU, false);
8465 list = g_slist_sort(list, arm_cpu_list_compare);
0442428a
MA
8466 qemu_printf("Available CPUs:\n");
8467 g_slist_foreach(list, arm_cpu_list_entry, NULL);
777dc784 8468 g_slist_free(list);
40f137e1
PB
8469}
8470
78027bb6
CR
8471static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8472{
8473 ObjectClass *oc = data;
8474 CpuDefinitionInfoList **cpu_list = user_data;
78027bb6
CR
8475 CpuDefinitionInfo *info;
8476 const char *typename;
8477
8478 typename = object_class_get_name(oc);
8479 info = g_malloc0(sizeof(*info));
8480 info->name = g_strndup(typename,
8481 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 8482 info->q_typename = g_strdup(typename);
78027bb6 8483
54aa3de7 8484 QAPI_LIST_PREPEND(*cpu_list, info);
78027bb6
CR
8485}
8486
25a9d6ca 8487CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
78027bb6
CR
8488{
8489 CpuDefinitionInfoList *cpu_list = NULL;
8490 GSList *list;
8491
8492 list = object_class_get_list(TYPE_ARM_CPU, false);
8493 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8494 g_slist_free(list);
8495
8496 return cpu_list;
8497}
8498
6e6efd61 8499static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
cbe64585
RH
8500 void *opaque, CPState state,
8501 CPSecureState secstate,
9c513e78
AB
8502 int crm, int opc1, int opc2,
8503 const char *name)
6e6efd61
PM
8504{
8505 /* Private utility function for define_one_arm_cp_reg_with_opaque():
8506 * add a single reginfo struct to the hash table.
8507 */
5860362d 8508 uint32_t key;
c27f5d3a 8509 ARMCPRegInfo *r2;
6e6efd61 8510 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
3f3c82a5 8511 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
c27f5d3a
RH
8512 size_t name_len;
8513
8514 /* Combine cpreg and name into one allocation. */
8515 name_len = strlen(name) + 1;
8516 r2 = g_malloc(sizeof(*r2) + name_len);
8517 *r2 = *r;
8518 r2->name = memcpy(r2 + 1, name, name_len);
3f3c82a5
FA
8519
8520 /* Reset the secure state to the specific incoming state. This is
8521 * necessary as the register may have been defined with both states.
8522 */
8523 r2->secure = secstate;
8524
8525 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8526 /* Register is banked (using both entries in array).
8527 * Overwriting fieldoffset as the array is only used to define
8528 * banked registers but later only fieldoffset is used.
f5a0a5a5 8529 */
3f3c82a5
FA
8530 r2->fieldoffset = r->bank_fieldoffsets[ns];
8531 }
8532
8533 if (state == ARM_CP_STATE_AA32) {
8534 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
8535 /* If the register is banked then we don't need to migrate or
8536 * reset the 32-bit instance in certain cases:
8537 *
8538 * 1) If the register has both 32-bit and 64-bit instances then we
8539 * can count on the 64-bit instance taking care of the
8540 * non-secure bank.
8541 * 2) If ARMv8 is enabled then we can count on a 64-bit version
8542 * taking care of the secure bank. This requires that separate
8543 * 32 and 64-bit definitions are provided.
8544 */
8545 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8546 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
7a0e58fa 8547 r2->type |= ARM_CP_ALIAS;
3f3c82a5
FA
8548 }
8549 } else if ((secstate != r->secure) && !ns) {
8550 /* The register is not banked so we only want to allow migration of
8551 * the non-secure instance.
8552 */
7a0e58fa 8553 r2->type |= ARM_CP_ALIAS;
58a1d8ce 8554 }
3f3c82a5
FA
8555
8556 if (r->state == ARM_CP_STATE_BOTH) {
8557 /* We assume it is a cp15 register if the .cp field is left unset.
8558 */
8559 if (r2->cp == 0) {
8560 r2->cp = 15;
8561 }
8562
e03b5686 8563#if HOST_BIG_ENDIAN
3f3c82a5
FA
8564 if (r2->fieldoffset) {
8565 r2->fieldoffset += sizeof(uint32_t);
8566 }
f5a0a5a5 8567#endif
3f3c82a5 8568 }
f5a0a5a5
PM
8569 }
8570 if (state == ARM_CP_STATE_AA64) {
8571 /* To allow abbreviation of ARMCPRegInfo
8572 * definitions, we treat cp == 0 as equivalent to
8573 * the value for "standard guest-visible sysreg".
58a1d8ce
PM
8574 * STATE_BOTH definitions are also always "standard
8575 * sysreg" in their AArch64 view (the .cp value may
8576 * be non-zero for the benefit of the AArch32 view).
f5a0a5a5 8577 */
58a1d8ce 8578 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
f5a0a5a5
PM
8579 r2->cp = CP_REG_ARM64_SYSREG_CP;
8580 }
5860362d
RH
8581 key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
8582 r2->opc0, opc1, opc2);
f5a0a5a5 8583 } else {
5860362d 8584 key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
f5a0a5a5 8585 }
6e6efd61
PM
8586 if (opaque) {
8587 r2->opaque = opaque;
8588 }
67ed771d
PM
8589 /* reginfo passed to helpers is correct for the actual access,
8590 * and is never ARM_CP_STATE_BOTH:
8591 */
8592 r2->state = state;
6e6efd61
PM
8593 /* Make sure reginfo passed to helpers for wildcarded regs
8594 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
8595 */
8596 r2->crm = crm;
8597 r2->opc1 = opc1;
8598 r2->opc2 = opc2;
8599 /* By convention, for wildcarded registers only the first
8600 * entry is used for migration; the others are marked as
7a0e58fa 8601 * ALIAS so we don't try to transfer the register
6e6efd61 8602 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 8603 * never migratable and not even raw-accessible.
6e6efd61 8604 */
87c3f0f2 8605 if (r->type & ARM_CP_SPECIAL_MASK) {
7a0e58fa
PM
8606 r2->type |= ARM_CP_NO_RAW;
8607 }
8608 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
8609 ((r->opc1 == CP_ANY) && opc1 != 0) ||
8610 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 8611 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
8612 }
8613
375421cc
PM
8614 /* Check that raw accesses are either forbidden or handled. Note that
8615 * we can't assert this earlier because the setup of fieldoffset for
8616 * banked registers has to be done first.
8617 */
8618 if (!(r2->type & ARM_CP_NO_RAW)) {
8619 assert(!raw_accessors_invalid(r2));
8620 }
8621
6e6efd61
PM
8622 /* Overriding of an existing definition must be explicitly
8623 * requested.
8624 */
8625 if (!(r->type & ARM_CP_OVERRIDE)) {
5860362d 8626 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
6e6efd61
PM
8627 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
8628 fprintf(stderr, "Register redefined: cp=%d %d bit "
8629 "crn=%d crm=%d opc1=%d opc2=%d, "
8630 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
8631 r2->crn, r2->crm, r2->opc1, r2->opc2,
8632 oldreg->name, r2->name);
8633 g_assert_not_reached();
8634 }
8635 }
5860362d 8636 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
6e6efd61
PM
8637}
8638
8639
4b6a83fb
PM
8640void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8641 const ARMCPRegInfo *r, void *opaque)
8642{
8643 /* Define implementations of coprocessor registers.
8644 * We store these in a hashtable because typically
8645 * there are less than 150 registers in a space which
8646 * is 16*16*16*8*8 = 262144 in size.
8647 * Wildcarding is supported for the crm, opc1 and opc2 fields.
8648 * If a register is defined twice then the second definition is
8649 * used, so this can be used to define some generic registers and
8650 * then override them with implementation specific variations.
8651 * At least one of the original and the second definition should
8652 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8653 * against accidental use.
f5a0a5a5
PM
8654 *
8655 * The state field defines whether the register is to be
8656 * visible in the AArch32 or AArch64 execution state. If the
8657 * state is set to ARM_CP_STATE_BOTH then we synthesise a
8658 * reginfo structure for the AArch32 view, which sees the lower
8659 * 32 bits of the 64 bit register.
8660 *
8661 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8662 * be wildcarded. AArch64 registers are always considered to be 64
8663 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8664 * the register, if any.
4b6a83fb 8665 */
d95101d6 8666 int crm, opc1, opc2;
4b6a83fb
PM
8667 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8668 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8669 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8670 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8671 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8672 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
d95101d6
RH
8673 CPState state;
8674
4b6a83fb
PM
8675 /* 64 bit registers have only CRm and Opc1 fields */
8676 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
8677 /* op0 only exists in the AArch64 encodings */
8678 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8679 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8680 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
cd8be50e
PM
8681 /*
8682 * This API is only for Arm's system coprocessors (14 and 15) or
8683 * (M-profile or v7A-and-earlier only) for implementation defined
8684 * coprocessors in the range 0..7. Our decode assumes this, since
8685 * 8..13 can be used for other insns including VFP and Neon. See
8686 * valid_cp() in translate.c. Assert here that we haven't tried
8687 * to use an invalid coprocessor number.
8688 */
8689 switch (r->state) {
8690 case ARM_CP_STATE_BOTH:
8691 /* 0 has a special meaning, but otherwise the same rules as AA32. */
8692 if (r->cp == 0) {
8693 break;
8694 }
8695 /* fall through */
8696 case ARM_CP_STATE_AA32:
8697 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8698 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8699 assert(r->cp >= 14 && r->cp <= 15);
8700 } else {
8701 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8702 }
8703 break;
8704 case ARM_CP_STATE_AA64:
8705 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8706 break;
8707 default:
8708 g_assert_not_reached();
8709 }
f5a0a5a5
PM
8710 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8711 * encodes a minimum access level for the register. We roll this
8712 * runtime check into our general permission check code, so check
8713 * here that the reginfo's specified permissions are strict enough
8714 * to encompass the generic architectural permission check.
8715 */
8716 if (r->state != ARM_CP_STATE_AA32) {
39107337 8717 CPAccessRights mask;
f5a0a5a5 8718 switch (r->opc1) {
b5bd7440
AB
8719 case 0:
8720 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8721 mask = PL0U_R | PL1_RW;
8722 break;
8723 case 1: case 2:
f5a0a5a5
PM
8724 /* min_EL EL1 */
8725 mask = PL1_RW;
8726 break;
8727 case 3:
8728 /* min_EL EL0 */
8729 mask = PL0_RW;
8730 break;
8731 case 4:
b4ecf60f 8732 case 5:
f5a0a5a5
PM
8733 /* min_EL EL2 */
8734 mask = PL2_RW;
8735 break;
f5a0a5a5
PM
8736 case 6:
8737 /* min_EL EL3 */
8738 mask = PL3_RW;
8739 break;
8740 case 7:
8741 /* min_EL EL1, secure mode only (we don't check the latter) */
8742 mask = PL1_RW;
8743 break;
8744 default:
8745 /* broken reginfo with out-of-range opc1 */
d385a605 8746 g_assert_not_reached();
f5a0a5a5
PM
8747 }
8748 /* assert our permissions are not too lax (stricter is fine) */
8749 assert((r->access & ~mask) == 0);
8750 }
8751
4b6a83fb
PM
8752 /* Check that the register definition has enough info to handle
8753 * reads and writes if they are permitted.
8754 */
87c3f0f2 8755 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
4b6a83fb 8756 if (r->access & PL3_R) {
3f3c82a5
FA
8757 assert((r->fieldoffset ||
8758 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8759 r->readfn);
4b6a83fb
PM
8760 }
8761 if (r->access & PL3_W) {
3f3c82a5
FA
8762 assert((r->fieldoffset ||
8763 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8764 r->writefn);
4b6a83fb
PM
8765 }
8766 }
5809ac57 8767
4b6a83fb
PM
8768 for (crm = crmmin; crm <= crmmax; crm++) {
8769 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8770 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
8771 for (state = ARM_CP_STATE_AA32;
8772 state <= ARM_CP_STATE_AA64; state++) {
8773 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8774 continue;
8775 }
3f3c82a5
FA
8776 if (state == ARM_CP_STATE_AA32) {
8777 /* Under AArch32 CP registers can be common
8778 * (same for secure and non-secure world) or banked.
8779 */
9c513e78
AB
8780 char *name;
8781
3f3c82a5
FA
8782 switch (r->secure) {
8783 case ARM_CP_SECSTATE_S:
8784 case ARM_CP_SECSTATE_NS:
8785 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
8786 r->secure, crm, opc1, opc2,
8787 r->name);
3f3c82a5 8788 break;
cbe64585 8789 case ARM_CP_SECSTATE_BOTH:
9c513e78 8790 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
8791 add_cpreg_to_hashtable(cpu, r, opaque, state,
8792 ARM_CP_SECSTATE_S,
9c513e78
AB
8793 crm, opc1, opc2, name);
8794 g_free(name);
3f3c82a5
FA
8795 add_cpreg_to_hashtable(cpu, r, opaque, state,
8796 ARM_CP_SECSTATE_NS,
9c513e78 8797 crm, opc1, opc2, r->name);
3f3c82a5 8798 break;
cbe64585
RH
8799 default:
8800 g_assert_not_reached();
3f3c82a5
FA
8801 }
8802 } else {
8803 /* AArch64 registers get mapped to non-secure instance
8804 * of AArch32 */
8805 add_cpreg_to_hashtable(cpu, r, opaque, state,
8806 ARM_CP_SECSTATE_NS,
9c513e78 8807 crm, opc1, opc2, r->name);
3f3c82a5 8808 }
f5a0a5a5 8809 }
4b6a83fb
PM
8810 }
8811 }
8812 }
8813}
8814
5809ac57
RH
8815/* Define a whole list of registers */
8816void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
8817 void *opaque, size_t len)
4b6a83fb 8818{
5809ac57
RH
8819 size_t i;
8820 for (i = 0; i < len; ++i) {
8821 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
4b6a83fb
PM
8822 }
8823}
8824
6c5c0fec
AB
8825/*
8826 * Modify ARMCPRegInfo for access from userspace.
8827 *
8828 * This is a data driven modification directed by
8829 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8830 * user-space cannot alter any values and dynamic values pertaining to
8831 * execution state are hidden from user space view anyway.
8832 */
5809ac57
RH
8833void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
8834 const ARMCPRegUserSpaceInfo *mods,
8835 size_t mods_len)
6c5c0fec 8836{
5809ac57
RH
8837 for (size_t mi = 0; mi < mods_len; ++mi) {
8838 const ARMCPRegUserSpaceInfo *m = mods + mi;
d040242e 8839 GPatternSpec *pat = NULL;
5809ac57 8840
d040242e
AB
8841 if (m->is_glob) {
8842 pat = g_pattern_spec_new(m->name);
8843 }
5809ac57
RH
8844 for (size_t ri = 0; ri < regs_len; ++ri) {
8845 ARMCPRegInfo *r = regs + ri;
8846
d040242e
AB
8847 if (pat && g_pattern_match_string(pat, r->name)) {
8848 r->type = ARM_CP_CONST;
8849 r->access = PL0U_R;
8850 r->resetvalue = 0;
8851 /* continue */
8852 } else if (strcmp(r->name, m->name) == 0) {
6c5c0fec
AB
8853 r->type = ARM_CP_CONST;
8854 r->access = PL0U_R;
8855 r->resetvalue &= m->exported_bits;
8856 r->resetvalue |= m->fixed_bits;
8857 break;
8858 }
8859 }
d040242e
AB
8860 if (pat) {
8861 g_pattern_spec_free(pat);
8862 }
6c5c0fec
AB
8863 }
8864}
8865
60322b39 8866const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 8867{
5860362d 8868 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
4b6a83fb
PM
8869}
8870
c4241c7d
PM
8871void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8872 uint64_t value)
4b6a83fb
PM
8873{
8874 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
8875}
8876
c4241c7d 8877uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
8878{
8879 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
8880 return 0;
8881}
8882
f5a0a5a5
PM
8883void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8884{
8885 /* Helper coprocessor reset function for do-nothing-on-reset registers */
8886}
8887
af393ffc 8888static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
8889{
8890 /* Return true if it is not valid for us to switch to
8891 * this CPU mode (ie all the UNPREDICTABLE cases in
8892 * the ARM ARM CPSRWriteByInstr pseudocode).
8893 */
af393ffc
PM
8894
8895 /* Changes to or from Hyp via MSR and CPS are illegal. */
8896 if (write_type == CPSRWriteByInstr &&
8897 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8898 mode == ARM_CPU_MODE_HYP)) {
8899 return 1;
8900 }
8901
37064a8b
PM
8902 switch (mode) {
8903 case ARM_CPU_MODE_USR:
10eacda7 8904 return 0;
37064a8b
PM
8905 case ARM_CPU_MODE_SYS:
8906 case ARM_CPU_MODE_SVC:
8907 case ARM_CPU_MODE_ABT:
8908 case ARM_CPU_MODE_UND:
8909 case ARM_CPU_MODE_IRQ:
8910 case ARM_CPU_MODE_FIQ:
52ff951b
PM
8911 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8912 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8913 */
10eacda7
PM
8914 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8915 * and CPS are treated as illegal mode changes.
8916 */
8917 if (write_type == CPSRWriteByInstr &&
10eacda7 8918 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7c208e0f 8919 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10eacda7
PM
8920 return 1;
8921 }
37064a8b 8922 return 0;
e6c8fc07 8923 case ARM_CPU_MODE_HYP:
e6ef0169 8924 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
027fc527 8925 case ARM_CPU_MODE_MON:
58ae2d1f 8926 return arm_current_el(env) < 3;
37064a8b
PM
8927 default:
8928 return 1;
8929 }
8930}
8931
2f4a40e5
AZ
8932uint32_t cpsr_read(CPUARMState *env)
8933{
8934 int ZF;
6fbe23d5
PB
8935 ZF = (env->ZF == 0);
8936 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
8937 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
8938 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
8939 | ((env->condexec_bits & 0xfc) << 8)
af519934 8940 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
8941}
8942
50866ba5
PM
8943void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
8944 CPSRWriteType write_type)
2f4a40e5 8945{
6e8801f9 8946 uint32_t changed_daif;
e784807c
PM
8947 bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
8948 (mask & (CPSR_M | CPSR_E | CPSR_IL));
6e8801f9 8949
2f4a40e5 8950 if (mask & CPSR_NZCV) {
6fbe23d5
PB
8951 env->ZF = (~val) & CPSR_Z;
8952 env->NF = val;
2f4a40e5
AZ
8953 env->CF = (val >> 29) & 1;
8954 env->VF = (val << 3) & 0x80000000;
8955 }
8956 if (mask & CPSR_Q)
8957 env->QF = ((val & CPSR_Q) != 0);
8958 if (mask & CPSR_T)
8959 env->thumb = ((val & CPSR_T) != 0);
8960 if (mask & CPSR_IT_0_1) {
8961 env->condexec_bits &= ~3;
8962 env->condexec_bits |= (val >> 25) & 3;
8963 }
8964 if (mask & CPSR_IT_2_7) {
8965 env->condexec_bits &= 3;
8966 env->condexec_bits |= (val >> 8) & 0xfc;
8967 }
8968 if (mask & CPSR_GE) {
8969 env->GE = (val >> 16) & 0xf;
8970 }
8971
6e8801f9
FA
8972 /* In a V7 implementation that includes the security extensions but does
8973 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
8974 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
8975 * bits respectively.
8976 *
8977 * In a V8 implementation, it is permitted for privileged software to
8978 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
8979 */
f8c88bbc 8980 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
8981 arm_feature(env, ARM_FEATURE_EL3) &&
8982 !arm_feature(env, ARM_FEATURE_EL2) &&
8983 !arm_is_secure(env)) {
8984
8985 changed_daif = (env->daif ^ val) & mask;
8986
8987 if (changed_daif & CPSR_A) {
8988 /* Check to see if we are allowed to change the masking of async
8989 * abort exceptions from a non-secure state.
8990 */
8991 if (!(env->cp15.scr_el3 & SCR_AW)) {
8992 qemu_log_mask(LOG_GUEST_ERROR,
8993 "Ignoring attempt to switch CPSR_A flag from "
8994 "non-secure world with SCR.AW bit clear\n");
8995 mask &= ~CPSR_A;
8996 }
8997 }
8998
8999 if (changed_daif & CPSR_F) {
9000 /* Check to see if we are allowed to change the masking of FIQ
9001 * exceptions from a non-secure state.
9002 */
9003 if (!(env->cp15.scr_el3 & SCR_FW)) {
9004 qemu_log_mask(LOG_GUEST_ERROR,
9005 "Ignoring attempt to switch CPSR_F flag from "
9006 "non-secure world with SCR.FW bit clear\n");
9007 mask &= ~CPSR_F;
9008 }
9009
9010 /* Check whether non-maskable FIQ (NMFI) support is enabled.
9011 * If this bit is set software is not allowed to mask
9012 * FIQs, but is allowed to set CPSR_F to 0.
9013 */
9014 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9015 (val & CPSR_F)) {
9016 qemu_log_mask(LOG_GUEST_ERROR,
9017 "Ignoring attempt to enable CPSR_F flag "
9018 "(non-maskable FIQ [NMFI] support enabled)\n");
9019 mask &= ~CPSR_F;
9020 }
9021 }
9022 }
9023
4cc35614
PM
9024 env->daif &= ~(CPSR_AIF & mask);
9025 env->daif |= val & CPSR_AIF & mask;
9026
f8c88bbc
PM
9027 if (write_type != CPSRWriteRaw &&
9028 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
9029 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9030 /* Note that we can only get here in USR mode if this is a
9031 * gdb stub write; for this case we follow the architectural
9032 * behaviour for guest writes in USR mode of ignoring an attempt
9033 * to switch mode. (Those are caught by translate.c for writes
9034 * triggered by guest instructions.)
9035 */
9036 mask &= ~CPSR_M;
9037 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
9038 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9039 * v7, and has defined behaviour in v8:
9040 * + leave CPSR.M untouched
9041 * + allow changes to the other CPSR fields
9042 * + set PSTATE.IL
9043 * For user changes via the GDB stub, we don't set PSTATE.IL,
9044 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
9045 */
9046 mask &= ~CPSR_M;
81907a58
PM
9047 if (write_type != CPSRWriteByGDBStub &&
9048 arm_feature(env, ARM_FEATURE_V8)) {
9049 mask |= CPSR_IL;
9050 val |= CPSR_IL;
9051 }
81e37284
PM
9052 qemu_log_mask(LOG_GUEST_ERROR,
9053 "Illegal AArch32 mode switch attempt from %s to %s\n",
9054 aarch32_mode_name(env->uncached_cpsr),
9055 aarch32_mode_name(val));
37064a8b 9056 } else {
81e37284
PM
9057 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9058 write_type == CPSRWriteExceptionReturn ?
9059 "Exception return from AArch32" :
9060 "AArch32 mode switch from",
9061 aarch32_mode_name(env->uncached_cpsr),
9062 aarch32_mode_name(val), env->regs[15]);
37064a8b
PM
9063 switch_mode(env, val & CPSR_M);
9064 }
2f4a40e5
AZ
9065 }
9066 mask &= ~CACHED_CPSR_BITS;
9067 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
e784807c
PM
9068 if (rebuild_hflags) {
9069 arm_rebuild_hflags(env);
9070 }
2f4a40e5
AZ
9071}
9072
b26eefb6
PB
9073/* Sign/zero extend */
9074uint32_t HELPER(sxtb16)(uint32_t x)
9075{
9076 uint32_t res;
9077 res = (uint16_t)(int8_t)x;
9078 res |= (uint32_t)(int8_t)(x >> 16) << 16;
9079 return res;
9080}
9081
e5346292
PM
9082static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9083{
9084 /*
9085 * Take a division-by-zero exception if necessary; otherwise return
9086 * to get the usual non-trapping division behaviour (result of 0)
9087 */
9088 if (arm_feature(env, ARM_FEATURE_M)
9089 && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9090 raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9091 }
9092}
9093
b26eefb6
PB
9094uint32_t HELPER(uxtb16)(uint32_t x)
9095{
9096 uint32_t res;
9097 res = (uint16_t)(uint8_t)x;
9098 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9099 return res;
9100}
9101
e5346292 9102int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
3670669c 9103{
fc7a5038 9104 if (den == 0) {
e5346292 9105 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9106 return 0;
9107 }
9108 if (num == INT_MIN && den == -1) {
9109 return INT_MIN;
9110 }
3670669c
PB
9111 return num / den;
9112}
9113
e5346292 9114uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
3670669c 9115{
fc7a5038 9116 if (den == 0) {
e5346292 9117 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9118 return 0;
9119 }
3670669c
PB
9120 return num / den;
9121}
9122
9123uint32_t HELPER(rbit)(uint32_t x)
9124{
42fedbca 9125 return revbit32(x);
3670669c
PB
9126}
9127
c47eaf9f 9128#ifdef CONFIG_USER_ONLY
b5ff1b31 9129
affdb64d 9130static void switch_mode(CPUARMState *env, int mode)
b5ff1b31 9131{
2fc0cc0e 9132 ARMCPU *cpu = env_archcpu(env);
a47dddd7
AF
9133
9134 if (mode != ARM_CPU_MODE_USR) {
9135 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9136 }
b5ff1b31
FB
9137}
9138
012a906b
GB
9139uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9140 uint32_t cur_el, bool secure)
9e729b57
EI
9141{
9142 return 1;
9143}
9144
ce02049d
GB
9145void aarch64_sync_64_to_32(CPUARMState *env)
9146{
9147 g_assert_not_reached();
9148}
9149
b5ff1b31
FB
9150#else
9151
affdb64d 9152static void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
9153{
9154 int old_mode;
9155 int i;
9156
9157 old_mode = env->uncached_cpsr & CPSR_M;
9158 if (mode == old_mode)
9159 return;
9160
9161 if (old_mode == ARM_CPU_MODE_FIQ) {
9162 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 9163 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
9164 } else if (mode == ARM_CPU_MODE_FIQ) {
9165 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 9166 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
9167 }
9168
f5206413 9169 i = bank_number(old_mode);
b5ff1b31 9170 env->banked_r13[i] = env->regs[13];
b5ff1b31
FB
9171 env->banked_spsr[i] = env->spsr;
9172
f5206413 9173 i = bank_number(mode);
b5ff1b31 9174 env->regs[13] = env->banked_r13[i];
b5ff1b31 9175 env->spsr = env->banked_spsr[i];
593cfa2b
PM
9176
9177 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9178 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
b5ff1b31
FB
9179}
9180
0eeb17d6
GB
9181/* Physical Interrupt Target EL Lookup Table
9182 *
9183 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9184 *
9185 * The below multi-dimensional table is used for looking up the target
9186 * exception level given numerous condition criteria. Specifically, the
9187 * target EL is based on SCR and HCR routing controls as well as the
9188 * currently executing EL and secure state.
9189 *
9190 * Dimensions:
9191 * target_el_table[2][2][2][2][2][4]
9192 * | | | | | +--- Current EL
9193 * | | | | +------ Non-secure(0)/Secure(1)
9194 * | | | +--------- HCR mask override
9195 * | | +------------ SCR exec state control
9196 * | +--------------- SCR mask override
9197 * +------------------ 32-bit(0)/64-bit(1) EL3
9198 *
9199 * The table values are as such:
9200 * 0-3 = EL0-EL3
9201 * -1 = Cannot occur
9202 *
9203 * The ARM ARM target EL table includes entries indicating that an "exception
9204 * is not taken". The two cases where this is applicable are:
9205 * 1) An exception is taken from EL3 but the SCR does not have the exception
9206 * routed to EL3.
9207 * 2) An exception is taken from EL2 but the HCR does not have the exception
9208 * routed to EL2.
9209 * In these two cases, the below table contain a target of EL1. This value is
9210 * returned as it is expected that the consumer of the table data will check
9211 * for "target EL >= current EL" to ensure the exception is not taken.
9212 *
9213 * SCR HCR
9214 * 64 EA AMO From
9215 * BIT IRQ IMO Non-secure Secure
9216 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
9217 */
82c39f6a 9218static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
9219 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9220 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
9221 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9222 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
9223 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9224 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
9225 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9226 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
9227 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6c85f906
RDC
9228 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
9229 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
9230 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
0eeb17d6
GB
9231 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
9232 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6c85f906
RDC
9233 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
9234 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
0eeb17d6
GB
9235};
9236
9237/*
9238 * Determine the target EL for physical exceptions
9239 */
012a906b
GB
9240uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9241 uint32_t cur_el, bool secure)
0eeb17d6
GB
9242{
9243 CPUARMState *env = cs->env_ptr;
f7778444
RH
9244 bool rw;
9245 bool scr;
9246 bool hcr;
0eeb17d6 9247 int target_el;
2cde031f 9248 /* Is the highest EL AArch64? */
f7778444
RH
9249 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9250 uint64_t hcr_el2;
2cde031f
SS
9251
9252 if (arm_feature(env, ARM_FEATURE_EL3)) {
9253 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9254 } else {
9255 /* Either EL2 is the highest EL (and so the EL2 register width
9256 * is given by is64); or there is no EL2 or EL3, in which case
9257 * the value of 'rw' does not affect the table lookup anyway.
9258 */
9259 rw = is64;
9260 }
0eeb17d6 9261
f7778444 9262 hcr_el2 = arm_hcr_el2_eff(env);
0eeb17d6
GB
9263 switch (excp_idx) {
9264 case EXCP_IRQ:
9265 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
f7778444 9266 hcr = hcr_el2 & HCR_IMO;
0eeb17d6
GB
9267 break;
9268 case EXCP_FIQ:
9269 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
f7778444 9270 hcr = hcr_el2 & HCR_FMO;
0eeb17d6
GB
9271 break;
9272 default:
9273 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
f7778444 9274 hcr = hcr_el2 & HCR_AMO;
0eeb17d6
GB
9275 break;
9276 };
9277
d1b31428
RH
9278 /*
9279 * For these purposes, TGE and AMO/IMO/FMO both force the
9280 * interrupt to EL2. Fold TGE into the bit extracted above.
9281 */
9282 hcr |= (hcr_el2 & HCR_TGE) != 0;
9283
0eeb17d6
GB
9284 /* Perform a table-lookup for the target EL given the current state */
9285 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9286
9287 assert(target_el > 0);
9288
9289 return target_el;
9290}
9291
fc6177af 9292void arm_log_exception(CPUState *cs)
b59f479b 9293{
fc6177af
PM
9294 int idx = cs->exception_index;
9295
b59f479b
PMD
9296 if (qemu_loglevel_mask(CPU_LOG_INT)) {
9297 const char *exc = NULL;
9298 static const char * const excnames[] = {
9299 [EXCP_UDEF] = "Undefined Instruction",
9300 [EXCP_SWI] = "SVC",
9301 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9302 [EXCP_DATA_ABORT] = "Data Abort",
9303 [EXCP_IRQ] = "IRQ",
9304 [EXCP_FIQ] = "FIQ",
9305 [EXCP_BKPT] = "Breakpoint",
9306 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9307 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9308 [EXCP_HVC] = "Hypervisor Call",
9309 [EXCP_HYP_TRAP] = "Hypervisor Trap",
9310 [EXCP_SMC] = "Secure Monitor Call",
9311 [EXCP_VIRQ] = "Virtual IRQ",
9312 [EXCP_VFIQ] = "Virtual FIQ",
9313 [EXCP_SEMIHOST] = "Semihosting call",
9314 [EXCP_NOCP] = "v7M NOCP UsageFault",
9315 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9316 [EXCP_STKOF] = "v8M STKOF UsageFault",
9317 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9318 [EXCP_LSERR] = "v8M LSERR UsageFault",
9319 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
e5346292 9320 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
b59f479b
PMD
9321 };
9322
9323 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9324 exc = excnames[idx];
9325 }
9326 if (!exc) {
9327 exc = "unknown";
9328 }
fc6177af
PM
9329 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9330 idx, exc, cs->cpu_index);
b59f479b
PMD
9331 }
9332}
9333
a356dacf 9334/*
7aab5a8c
PMD
9335 * Function used to synchronize QEMU's AArch64 register set with AArch32
9336 * register set. This is necessary when switching between AArch32 and AArch64
9337 * execution state.
a356dacf 9338 */
7aab5a8c 9339void aarch64_sync_32_to_64(CPUARMState *env)
9ee6e8bb 9340{
7aab5a8c
PMD
9341 int i;
9342 uint32_t mode = env->uncached_cpsr & CPSR_M;
9343
9344 /* We can blanket copy R[0:7] to X[0:7] */
9345 for (i = 0; i < 8; i++) {
9346 env->xregs[i] = env->regs[i];
fd592d89 9347 }
70d74660 9348
9a223097 9349 /*
7aab5a8c
PMD
9350 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9351 * Otherwise, they come from the banked user regs.
fd592d89 9352 */
7aab5a8c
PMD
9353 if (mode == ARM_CPU_MODE_FIQ) {
9354 for (i = 8; i < 13; i++) {
9355 env->xregs[i] = env->usr_regs[i - 8];
9356 }
9357 } else {
9358 for (i = 8; i < 13; i++) {
9359 env->xregs[i] = env->regs[i];
9360 }
fd592d89 9361 }
9ee6e8bb 9362
7aab5a8c
PMD
9363 /*
9364 * Registers x13-x23 are the various mode SP and FP registers. Registers
9365 * r13 and r14 are only copied if we are in that mode, otherwise we copy
9366 * from the mode banked register.
9367 */
9368 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9369 env->xregs[13] = env->regs[13];
9370 env->xregs[14] = env->regs[14];
9371 } else {
9372 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9373 /* HYP is an exception in that it is copied from r14 */
9374 if (mode == ARM_CPU_MODE_HYP) {
9375 env->xregs[14] = env->regs[14];
95695eff 9376 } else {
7aab5a8c 9377 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
95695eff 9378 }
95695eff
PM
9379 }
9380
7aab5a8c
PMD
9381 if (mode == ARM_CPU_MODE_HYP) {
9382 env->xregs[15] = env->regs[13];
9383 } else {
9384 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
95695eff
PM
9385 }
9386
7aab5a8c
PMD
9387 if (mode == ARM_CPU_MODE_IRQ) {
9388 env->xregs[16] = env->regs[14];
9389 env->xregs[17] = env->regs[13];
9390 } else {
9391 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9392 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9393 }
95695eff 9394
7aab5a8c
PMD
9395 if (mode == ARM_CPU_MODE_SVC) {
9396 env->xregs[18] = env->regs[14];
9397 env->xregs[19] = env->regs[13];
9398 } else {
9399 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9400 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9401 }
95695eff 9402
7aab5a8c
PMD
9403 if (mode == ARM_CPU_MODE_ABT) {
9404 env->xregs[20] = env->regs[14];
9405 env->xregs[21] = env->regs[13];
9406 } else {
9407 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9408 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9409 }
e33cf0f8 9410
7aab5a8c
PMD
9411 if (mode == ARM_CPU_MODE_UND) {
9412 env->xregs[22] = env->regs[14];
9413 env->xregs[23] = env->regs[13];
9414 } else {
9415 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9416 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
e33cf0f8
PM
9417 }
9418
9419 /*
7aab5a8c
PMD
9420 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9421 * mode, then we can copy from r8-r14. Otherwise, we copy from the
9422 * FIQ bank for r8-r14.
e33cf0f8 9423 */
7aab5a8c
PMD
9424 if (mode == ARM_CPU_MODE_FIQ) {
9425 for (i = 24; i < 31; i++) {
9426 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
9427 }
9428 } else {
9429 for (i = 24; i < 29; i++) {
9430 env->xregs[i] = env->fiq_regs[i - 24];
e33cf0f8 9431 }
7aab5a8c
PMD
9432 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9433 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
e33cf0f8 9434 }
7aab5a8c
PMD
9435
9436 env->pc = env->regs[15];
e33cf0f8
PM
9437}
9438
9a223097 9439/*
7aab5a8c
PMD
9440 * Function used to synchronize QEMU's AArch32 register set with AArch64
9441 * register set. This is necessary when switching between AArch32 and AArch64
9442 * execution state.
de2db7ec 9443 */
7aab5a8c 9444void aarch64_sync_64_to_32(CPUARMState *env)
9ee6e8bb 9445{
7aab5a8c
PMD
9446 int i;
9447 uint32_t mode = env->uncached_cpsr & CPSR_M;
abc24d86 9448
7aab5a8c
PMD
9449 /* We can blanket copy X[0:7] to R[0:7] */
9450 for (i = 0; i < 8; i++) {
9451 env->regs[i] = env->xregs[i];
de2db7ec 9452 }
3f0cddee 9453
9a223097 9454 /*
7aab5a8c
PMD
9455 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9456 * Otherwise, we copy x8-x12 into the banked user regs.
de2db7ec 9457 */
7aab5a8c
PMD
9458 if (mode == ARM_CPU_MODE_FIQ) {
9459 for (i = 8; i < 13; i++) {
9460 env->usr_regs[i - 8] = env->xregs[i];
9461 }
9462 } else {
9463 for (i = 8; i < 13; i++) {
9464 env->regs[i] = env->xregs[i];
9465 }
fb602cb7
PM
9466 }
9467
9a223097 9468 /*
7aab5a8c
PMD
9469 * Registers r13 & r14 depend on the current mode.
9470 * If we are in a given mode, we copy the corresponding x registers to r13
9471 * and r14. Otherwise, we copy the x register to the banked r13 and r14
9472 * for the mode.
fb602cb7 9473 */
7aab5a8c
PMD
9474 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9475 env->regs[13] = env->xregs[13];
9476 env->regs[14] = env->xregs[14];
fb602cb7 9477 } else {
7aab5a8c 9478 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
fb602cb7 9479
7aab5a8c
PMD
9480 /*
9481 * HYP is an exception in that it does not have its own banked r14 but
9482 * shares the USR r14
9483 */
9484 if (mode == ARM_CPU_MODE_HYP) {
9485 env->regs[14] = env->xregs[14];
9486 } else {
9487 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9488 }
9489 }
fb602cb7 9490
7aab5a8c
PMD
9491 if (mode == ARM_CPU_MODE_HYP) {
9492 env->regs[13] = env->xregs[15];
fb602cb7 9493 } else {
7aab5a8c 9494 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
fb602cb7 9495 }
d02a8698 9496
7aab5a8c
PMD
9497 if (mode == ARM_CPU_MODE_IRQ) {
9498 env->regs[14] = env->xregs[16];
9499 env->regs[13] = env->xregs[17];
d02a8698 9500 } else {
7aab5a8c
PMD
9501 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9502 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
d02a8698
PM
9503 }
9504
7aab5a8c
PMD
9505 if (mode == ARM_CPU_MODE_SVC) {
9506 env->regs[14] = env->xregs[18];
9507 env->regs[13] = env->xregs[19];
9508 } else {
9509 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9510 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
fb602cb7
PM
9511 }
9512
7aab5a8c
PMD
9513 if (mode == ARM_CPU_MODE_ABT) {
9514 env->regs[14] = env->xregs[20];
9515 env->regs[13] = env->xregs[21];
9516 } else {
9517 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9518 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
9519 }
9520
9521 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
9522 env->regs[14] = env->xregs[22];
9523 env->regs[13] = env->xregs[23];
ce02049d 9524 } else {
593cfa2b 9525 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
3a9148d0 9526 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
9527 }
9528
9529 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9530 * mode, then we can copy to r8-r14. Otherwise, we copy to the
9531 * FIQ bank for r8-r14.
9532 */
9533 if (mode == ARM_CPU_MODE_FIQ) {
9534 for (i = 24; i < 31; i++) {
9535 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
9536 }
9537 } else {
9538 for (i = 24; i < 29; i++) {
9539 env->fiq_regs[i - 24] = env->xregs[i];
9540 }
9541 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
593cfa2b 9542 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
ce02049d
GB
9543 }
9544
9545 env->regs[15] = env->pc;
9546}
9547
dea8378b
PM
9548static void take_aarch32_exception(CPUARMState *env, int new_mode,
9549 uint32_t mask, uint32_t offset,
9550 uint32_t newpc)
9551{
4a2696c0
RH
9552 int new_el;
9553
dea8378b
PM
9554 /* Change the CPU state so as to actually take the exception. */
9555 switch_mode(env, new_mode);
4a2696c0 9556
dea8378b
PM
9557 /*
9558 * For exceptions taken to AArch32 we must clear the SS bit in both
9559 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9560 */
f944a854 9561 env->pstate &= ~PSTATE_SS;
dea8378b
PM
9562 env->spsr = cpsr_read(env);
9563 /* Clear IT bits. */
9564 env->condexec_bits = 0;
9565 /* Switch to the new mode, and to the correct instruction set. */
9566 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
88828bf1
CD
9567
9568 /* This must be after mode switching. */
9569 new_el = arm_current_el(env);
9570
dea8378b
PM
9571 /* Set new mode endianness */
9572 env->uncached_cpsr &= ~CPSR_E;
4a2696c0 9573 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
dea8378b
PM
9574 env->uncached_cpsr |= CPSR_E;
9575 }
829f9fd3
PM
9576 /* J and IL must always be cleared for exception entry */
9577 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
dea8378b
PM
9578 env->daif |= mask;
9579
f2f68a78
RC
9580 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9581 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9582 env->uncached_cpsr |= CPSR_SSBS;
9583 } else {
9584 env->uncached_cpsr &= ~CPSR_SSBS;
9585 }
9586 }
9587
dea8378b
PM
9588 if (new_mode == ARM_CPU_MODE_HYP) {
9589 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9590 env->elr_el[2] = env->regs[15];
9591 } else {
4a2696c0 9592 /* CPSR.PAN is normally preserved preserved unless... */
f8af1143 9593 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
4a2696c0
RH
9594 switch (new_el) {
9595 case 3:
9596 if (!arm_is_secure_below_el3(env)) {
9597 /* ... the target is EL3, from non-secure state. */
9598 env->uncached_cpsr &= ~CPSR_PAN;
9599 break;
9600 }
9601 /* ... the target is EL3, from secure state ... */
9602 /* fall through */
9603 case 1:
9604 /* ... the target is EL1 and SCTLR.SPAN is 0. */
9605 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9606 env->uncached_cpsr |= CPSR_PAN;
9607 }
9608 break;
9609 }
9610 }
dea8378b
PM
9611 /*
9612 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9613 * and we should just guard the thumb mode on V4
9614 */
9615 if (arm_feature(env, ARM_FEATURE_V4T)) {
9616 env->thumb =
9617 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9618 }
9619 env->regs[14] = env->regs[15] + offset;
9620 }
9621 env->regs[15] = newpc;
a8a79c7a 9622 arm_rebuild_hflags(env);
dea8378b
PM
9623}
9624
b9bc21ff
PM
9625static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9626{
9627 /*
9628 * Handle exception entry to Hyp mode; this is sufficiently
9629 * different to entry to other AArch32 modes that we handle it
9630 * separately here.
9631 *
9632 * The vector table entry used is always the 0x14 Hyp mode entry point,
2c023d36 9633 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
b9bc21ff
PM
9634 * The offset applied to the preferred return address is always zero
9635 * (see DDI0487C.a section G1.12.3).
9636 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9637 */
9638 uint32_t addr, mask;
9639 ARMCPU *cpu = ARM_CPU(cs);
9640 CPUARMState *env = &cpu->env;
9641
9642 switch (cs->exception_index) {
9643 case EXCP_UDEF:
9644 addr = 0x04;
9645 break;
9646 case EXCP_SWI:
2c023d36 9647 addr = 0x08;
b9bc21ff
PM
9648 break;
9649 case EXCP_BKPT:
9650 /* Fall through to prefetch abort. */
9651 case EXCP_PREFETCH_ABORT:
9652 env->cp15.ifar_s = env->exception.vaddress;
9653 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9654 (uint32_t)env->exception.vaddress);
9655 addr = 0x0c;
9656 break;
9657 case EXCP_DATA_ABORT:
9658 env->cp15.dfar_s = env->exception.vaddress;
9659 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9660 (uint32_t)env->exception.vaddress);
9661 addr = 0x10;
9662 break;
9663 case EXCP_IRQ:
9664 addr = 0x18;
9665 break;
9666 case EXCP_FIQ:
9667 addr = 0x1c;
9668 break;
9669 case EXCP_HVC:
9670 addr = 0x08;
9671 break;
9672 case EXCP_HYP_TRAP:
9673 addr = 0x14;
9bbb4ef9 9674 break;
b9bc21ff
PM
9675 default:
9676 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9677 }
9678
9679 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
2ed08180
PM
9680 if (!arm_feature(env, ARM_FEATURE_V8)) {
9681 /*
9682 * QEMU syndrome values are v8-style. v7 has the IL bit
9683 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9684 * If this is a v7 CPU, squash the IL bit in those cases.
9685 */
9686 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9687 (cs->exception_index == EXCP_DATA_ABORT &&
9688 !(env->exception.syndrome & ARM_EL_ISV)) ||
9689 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9690 env->exception.syndrome &= ~ARM_EL_IL;
9691 }
9692 }
b9bc21ff
PM
9693 env->cp15.esr_el[2] = env->exception.syndrome;
9694 }
9695
9696 if (arm_current_el(env) != 2 && addr < 0x14) {
9697 addr = 0x14;
9698 }
9699
9700 mask = 0;
9701 if (!(env->cp15.scr_el3 & SCR_EA)) {
9702 mask |= CPSR_A;
9703 }
9704 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9705 mask |= CPSR_I;
9706 }
9707 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9708 mask |= CPSR_F;
9709 }
9710
9711 addr += env->cp15.hvbar;
9712
9713 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9714}
9715
966f758c 9716static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 9717{
97a8ea5a
AF
9718 ARMCPU *cpu = ARM_CPU(cs);
9719 CPUARMState *env = &cpu->env;
b5ff1b31
FB
9720 uint32_t addr;
9721 uint32_t mask;
9722 int new_mode;
9723 uint32_t offset;
16a906fd 9724 uint32_t moe;
b5ff1b31 9725
16a906fd 9726 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
64b91e3f 9727 switch (syn_get_ec(env->exception.syndrome)) {
16a906fd
PM
9728 case EC_BREAKPOINT:
9729 case EC_BREAKPOINT_SAME_EL:
9730 moe = 1;
9731 break;
9732 case EC_WATCHPOINT:
9733 case EC_WATCHPOINT_SAME_EL:
9734 moe = 10;
9735 break;
9736 case EC_AA32_BKPT:
9737 moe = 3;
9738 break;
9739 case EC_VECTORCATCH:
9740 moe = 5;
9741 break;
9742 default:
9743 moe = 0;
9744 break;
9745 }
9746
9747 if (moe) {
9748 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9749 }
9750
b9bc21ff
PM
9751 if (env->exception.target_el == 2) {
9752 arm_cpu_do_interrupt_aarch32_hyp(cs);
9753 return;
9754 }
9755
27103424 9756 switch (cs->exception_index) {
b5ff1b31
FB
9757 case EXCP_UDEF:
9758 new_mode = ARM_CPU_MODE_UND;
9759 addr = 0x04;
9760 mask = CPSR_I;
9761 if (env->thumb)
9762 offset = 2;
9763 else
9764 offset = 4;
9765 break;
9766 case EXCP_SWI:
9767 new_mode = ARM_CPU_MODE_SVC;
9768 addr = 0x08;
9769 mask = CPSR_I;
601d70b9 9770 /* The PC already points to the next instruction. */
b5ff1b31
FB
9771 offset = 0;
9772 break;
06c949e6 9773 case EXCP_BKPT:
9ee6e8bb
PB
9774 /* Fall through to prefetch abort. */
9775 case EXCP_PREFETCH_ABORT:
88ca1c2d 9776 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 9777 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 9778 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 9779 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9780 new_mode = ARM_CPU_MODE_ABT;
9781 addr = 0x0c;
9782 mask = CPSR_A | CPSR_I;
9783 offset = 4;
9784 break;
9785 case EXCP_DATA_ABORT:
4a7e2d73 9786 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 9787 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 9788 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 9789 env->exception.fsr,
6cd8a264 9790 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9791 new_mode = ARM_CPU_MODE_ABT;
9792 addr = 0x10;
9793 mask = CPSR_A | CPSR_I;
9794 offset = 8;
9795 break;
9796 case EXCP_IRQ:
9797 new_mode = ARM_CPU_MODE_IRQ;
9798 addr = 0x18;
9799 /* Disable IRQ and imprecise data aborts. */
9800 mask = CPSR_A | CPSR_I;
9801 offset = 4;
de38d23b
FA
9802 if (env->cp15.scr_el3 & SCR_IRQ) {
9803 /* IRQ routed to monitor mode */
9804 new_mode = ARM_CPU_MODE_MON;
9805 mask |= CPSR_F;
9806 }
b5ff1b31
FB
9807 break;
9808 case EXCP_FIQ:
9809 new_mode = ARM_CPU_MODE_FIQ;
9810 addr = 0x1c;
9811 /* Disable FIQ, IRQ and imprecise data aborts. */
9812 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
9813 if (env->cp15.scr_el3 & SCR_FIQ) {
9814 /* FIQ routed to monitor mode */
9815 new_mode = ARM_CPU_MODE_MON;
9816 }
b5ff1b31
FB
9817 offset = 4;
9818 break;
87a4b270
PM
9819 case EXCP_VIRQ:
9820 new_mode = ARM_CPU_MODE_IRQ;
9821 addr = 0x18;
9822 /* Disable IRQ and imprecise data aborts. */
9823 mask = CPSR_A | CPSR_I;
9824 offset = 4;
9825 break;
9826 case EXCP_VFIQ:
9827 new_mode = ARM_CPU_MODE_FIQ;
9828 addr = 0x1c;
9829 /* Disable FIQ, IRQ and imprecise data aborts. */
9830 mask = CPSR_A | CPSR_I | CPSR_F;
9831 offset = 4;
9832 break;
dbe9d163
FA
9833 case EXCP_SMC:
9834 new_mode = ARM_CPU_MODE_MON;
9835 addr = 0x08;
9836 mask = CPSR_A | CPSR_I | CPSR_F;
9837 offset = 0;
9838 break;
b5ff1b31 9839 default:
a47dddd7 9840 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
9841 return; /* Never happens. Keep compiler happy. */
9842 }
e89e51a1
FA
9843
9844 if (new_mode == ARM_CPU_MODE_MON) {
9845 addr += env->cp15.mvbar;
137feaa9 9846 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 9847 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 9848 addr += 0xffff0000;
8641136c
NR
9849 } else {
9850 /* ARM v7 architectures provide a vector base address register to remap
9851 * the interrupt vector table.
e89e51a1 9852 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
9853 * Note: only bits 31:5 are valid.
9854 */
fb6c91ba 9855 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 9856 }
dbe9d163
FA
9857
9858 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9859 env->cp15.scr_el3 &= ~SCR_NS;
9860 }
9861
dea8378b 9862 take_aarch32_exception(env, new_mode, mask, offset, addr);
b5ff1b31
FB
9863}
9864
a65dabf7
PM
9865static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
9866{
9867 /*
9868 * Return the register number of the AArch64 view of the AArch32
9869 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
9870 * be that of the AArch32 mode the exception came from.
9871 */
9872 int mode = env->uncached_cpsr & CPSR_M;
9873
9874 switch (aarch32_reg) {
9875 case 0 ... 7:
9876 return aarch32_reg;
9877 case 8 ... 12:
9878 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
9879 case 13:
9880 switch (mode) {
9881 case ARM_CPU_MODE_USR:
9882 case ARM_CPU_MODE_SYS:
9883 return 13;
9884 case ARM_CPU_MODE_HYP:
9885 return 15;
9886 case ARM_CPU_MODE_IRQ:
9887 return 17;
9888 case ARM_CPU_MODE_SVC:
9889 return 19;
9890 case ARM_CPU_MODE_ABT:
9891 return 21;
9892 case ARM_CPU_MODE_UND:
9893 return 23;
9894 case ARM_CPU_MODE_FIQ:
9895 return 29;
9896 default:
9897 g_assert_not_reached();
9898 }
9899 case 14:
9900 switch (mode) {
9901 case ARM_CPU_MODE_USR:
9902 case ARM_CPU_MODE_SYS:
9903 case ARM_CPU_MODE_HYP:
9904 return 14;
9905 case ARM_CPU_MODE_IRQ:
9906 return 16;
9907 case ARM_CPU_MODE_SVC:
9908 return 18;
9909 case ARM_CPU_MODE_ABT:
9910 return 20;
9911 case ARM_CPU_MODE_UND:
9912 return 22;
9913 case ARM_CPU_MODE_FIQ:
9914 return 30;
9915 default:
9916 g_assert_not_reached();
9917 }
9918 case 15:
9919 return 31;
9920 default:
9921 g_assert_not_reached();
9922 }
9923}
9924
f944a854
RC
9925static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
9926{
9927 uint32_t ret = cpsr_read(env);
9928
9929 /* Move DIT to the correct location for SPSR_ELx */
9930 if (ret & CPSR_DIT) {
9931 ret &= ~CPSR_DIT;
9932 ret |= PSTATE_DIT;
9933 }
9934 /* Merge PSTATE.SS into SPSR_ELx */
9935 ret |= env->pstate & PSTATE_SS;
9936
9937 return ret;
9938}
9939
966f758c
PM
9940/* Handle exception entry to a target EL which is using AArch64 */
9941static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
9942{
9943 ARMCPU *cpu = ARM_CPU(cs);
9944 CPUARMState *env = &cpu->env;
9945 unsigned int new_el = env->exception.target_el;
9946 target_ulong addr = env->cp15.vbar_el[new_el];
9947 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
4a2696c0 9948 unsigned int old_mode;
0ab5953b 9949 unsigned int cur_el = arm_current_el(env);
a65dabf7 9950 int rt;
0ab5953b 9951
9a05f7b6
RH
9952 /*
9953 * Note that new_el can never be 0. If cur_el is 0, then
9954 * el0_a64 is is_a64(), else el0_a64 is ignored.
9955 */
9956 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
f3a9b694 9957
0ab5953b 9958 if (cur_el < new_el) {
3d6f7617
PM
9959 /* Entry vector offset depends on whether the implemented EL
9960 * immediately lower than the target level is using AArch32 or AArch64
9961 */
9962 bool is_aa64;
cb092fbb 9963 uint64_t hcr;
3d6f7617
PM
9964
9965 switch (new_el) {
9966 case 3:
9967 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
9968 break;
9969 case 2:
cb092fbb
RH
9970 hcr = arm_hcr_el2_eff(env);
9971 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
9972 is_aa64 = (hcr & HCR_RW) != 0;
9973 break;
9974 }
9975 /* fall through */
3d6f7617
PM
9976 case 1:
9977 is_aa64 = is_a64(env);
9978 break;
9979 default:
9980 g_assert_not_reached();
9981 }
9982
9983 if (is_aa64) {
f3a9b694
PM
9984 addr += 0x400;
9985 } else {
9986 addr += 0x600;
9987 }
9988 } else if (pstate_read(env) & PSTATE_SP) {
9989 addr += 0x200;
9990 }
9991
f3a9b694
PM
9992 switch (cs->exception_index) {
9993 case EXCP_PREFETCH_ABORT:
9994 case EXCP_DATA_ABORT:
9995 env->cp15.far_el[new_el] = env->exception.vaddress;
9996 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
9997 env->cp15.far_el[new_el]);
9998 /* fall through */
9999 case EXCP_BKPT:
10000 case EXCP_UDEF:
10001 case EXCP_SWI:
10002 case EXCP_HVC:
10003 case EXCP_HYP_TRAP:
10004 case EXCP_SMC:
a65dabf7
PM
10005 switch (syn_get_ec(env->exception.syndrome)) {
10006 case EC_ADVSIMDFPACCESSTRAP:
4be42f40
PM
10007 /*
10008 * QEMU internal FP/SIMD syndromes from AArch32 include the
10009 * TA and coproc fields which are only exposed if the exception
10010 * is taken to AArch32 Hyp mode. Mask them out to get a valid
10011 * AArch64 format syndrome.
10012 */
10013 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
a65dabf7
PM
10014 break;
10015 case EC_CP14RTTRAP:
10016 case EC_CP15RTTRAP:
10017 case EC_CP14DTTRAP:
10018 /*
10019 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10020 * the raw register field from the insn; when taking this to
10021 * AArch64 we must convert it to the AArch64 view of the register
10022 * number. Notice that we read a 4-bit AArch32 register number and
10023 * write back a 5-bit AArch64 one.
10024 */
10025 rt = extract32(env->exception.syndrome, 5, 4);
10026 rt = aarch64_regnum(env, rt);
10027 env->exception.syndrome = deposit32(env->exception.syndrome,
10028 5, 5, rt);
10029 break;
10030 case EC_CP15RRTTRAP:
10031 case EC_CP14RRTTRAP:
10032 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10033 rt = extract32(env->exception.syndrome, 5, 4);
10034 rt = aarch64_regnum(env, rt);
10035 env->exception.syndrome = deposit32(env->exception.syndrome,
10036 5, 5, rt);
10037 rt = extract32(env->exception.syndrome, 10, 4);
10038 rt = aarch64_regnum(env, rt);
10039 env->exception.syndrome = deposit32(env->exception.syndrome,
10040 10, 5, rt);
10041 break;
4be42f40 10042 }
f3a9b694
PM
10043 env->cp15.esr_el[new_el] = env->exception.syndrome;
10044 break;
10045 case EXCP_IRQ:
10046 case EXCP_VIRQ:
10047 addr += 0x80;
10048 break;
10049 case EXCP_FIQ:
10050 case EXCP_VFIQ:
10051 addr += 0x100;
10052 break;
f3a9b694
PM
10053 default:
10054 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10055 }
10056
10057 if (is_a64(env)) {
4a2696c0 10058 old_mode = pstate_read(env);
f3a9b694
PM
10059 aarch64_save_sp(env, arm_current_el(env));
10060 env->elr_el[new_el] = env->pc;
10061 } else {
f944a854 10062 old_mode = cpsr_read_for_spsr_elx(env);
f3a9b694
PM
10063 env->elr_el[new_el] = env->regs[15];
10064
10065 aarch64_sync_32_to_64(env);
10066
10067 env->condexec_bits = 0;
10068 }
4a2696c0
RH
10069 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10070
f3a9b694
PM
10071 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10072 env->elr_el[new_el]);
10073
4a2696c0
RH
10074 if (cpu_isar_feature(aa64_pan, cpu)) {
10075 /* The value of PSTATE.PAN is normally preserved, except when ... */
10076 new_mode |= old_mode & PSTATE_PAN;
10077 switch (new_el) {
10078 case 2:
10079 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
10080 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10081 != (HCR_E2H | HCR_TGE)) {
10082 break;
10083 }
10084 /* fall through */
10085 case 1:
10086 /* ... the target is EL1 ... */
10087 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
10088 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10089 new_mode |= PSTATE_PAN;
10090 }
10091 break;
10092 }
10093 }
34669338
RH
10094 if (cpu_isar_feature(aa64_mte, cpu)) {
10095 new_mode |= PSTATE_TCO;
10096 }
4a2696c0 10097
f2f68a78
RC
10098 if (cpu_isar_feature(aa64_ssbs, cpu)) {
10099 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10100 new_mode |= PSTATE_SSBS;
10101 } else {
10102 new_mode &= ~PSTATE_SSBS;
10103 }
10104 }
10105
f3a9b694 10106 pstate_write(env, PSTATE_DAIF | new_mode);
53221552 10107 env->aarch64 = true;
f3a9b694 10108 aarch64_restore_sp(env, new_el);
a8a79c7a 10109 helper_rebuild_hflags_a64(env, new_el);
f3a9b694
PM
10110
10111 env->pc = addr;
10112
10113 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10114 new_el, env->pc, pstate_read(env));
966f758c
PM
10115}
10116
ed6e6ba9
AB
10117/*
10118 * Do semihosting call and set the appropriate return value. All the
10119 * permission and validity checks have been done at translate time.
10120 *
10121 * We only see semihosting exceptions in TCG only as they are not
10122 * trapped to the hypervisor in KVM.
10123 */
91f78c58 10124#ifdef CONFIG_TCG
ed6e6ba9
AB
10125static void handle_semihosting(CPUState *cs)
10126{
904c04de
PM
10127 ARMCPU *cpu = ARM_CPU(cs);
10128 CPUARMState *env = &cpu->env;
10129
10130 if (is_a64(env)) {
ed6e6ba9
AB
10131 qemu_log_mask(CPU_LOG_INT,
10132 "...handling as semihosting call 0x%" PRIx64 "\n",
10133 env->xregs[0]);
0bb446d8 10134 env->xregs[0] = do_common_semihosting(cs);
4ff5ef9e 10135 env->pc += 4;
904c04de 10136 } else {
904c04de
PM
10137 qemu_log_mask(CPU_LOG_INT,
10138 "...handling as semihosting call 0x%x\n",
10139 env->regs[0]);
0bb446d8 10140 env->regs[0] = do_common_semihosting(cs);
4ff5ef9e 10141 env->regs[15] += env->thumb ? 2 : 4;
904c04de
PM
10142 }
10143}
ed6e6ba9 10144#endif
904c04de 10145
966f758c
PM
10146/* Handle a CPU exception for A and R profile CPUs.
10147 * Do any appropriate logging, handle PSCI calls, and then hand off
10148 * to the AArch64-entry or AArch32-entry function depending on the
10149 * target exception level's register width.
853bfef4
CF
10150 *
10151 * Note: this is used for both TCG (as the do_interrupt tcg op),
10152 * and KVM to re-inject guest debug exceptions, and to
10153 * inject a Synchronous-External-Abort.
966f758c
PM
10154 */
10155void arm_cpu_do_interrupt(CPUState *cs)
10156{
10157 ARMCPU *cpu = ARM_CPU(cs);
10158 CPUARMState *env = &cpu->env;
10159 unsigned int new_el = env->exception.target_el;
10160
531c60a9 10161 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c 10162
fc6177af 10163 arm_log_exception(cs);
966f758c
PM
10164 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10165 new_el);
10166 if (qemu_loglevel_mask(CPU_LOG_INT)
10167 && !excp_is_internal(cs->exception_index)) {
6568da45 10168 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
64b91e3f 10169 syn_get_ec(env->exception.syndrome),
966f758c
PM
10170 env->exception.syndrome);
10171 }
10172
10173 if (arm_is_psci_call(cpu, cs->exception_index)) {
10174 arm_handle_psci_call(cpu);
10175 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10176 return;
10177 }
10178
ed6e6ba9
AB
10179 /*
10180 * Semihosting semantics depend on the register width of the code
10181 * that caused the exception, not the target exception level, so
10182 * must be handled here.
966f758c 10183 */
ed6e6ba9
AB
10184#ifdef CONFIG_TCG
10185 if (cs->exception_index == EXCP_SEMIHOST) {
10186 handle_semihosting(cs);
904c04de
PM
10187 return;
10188 }
ed6e6ba9 10189#endif
904c04de 10190
b5c53d1b
AL
10191 /* Hooks may change global state so BQL should be held, also the
10192 * BQL needs to be held for any modification of
10193 * cs->interrupt_request.
10194 */
10195 g_assert(qemu_mutex_iothread_locked());
10196
10197 arm_call_pre_el_change_hook(cpu);
10198
904c04de
PM
10199 assert(!excp_is_internal(cs->exception_index));
10200 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
10201 arm_cpu_do_interrupt_aarch64(cs);
10202 } else {
10203 arm_cpu_do_interrupt_aarch32(cs);
10204 }
f3a9b694 10205
bd7d00fc
PM
10206 arm_call_el_change_hook(cpu);
10207
f3a9b694
PM
10208 if (!kvm_enabled()) {
10209 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10210 }
10211}
c47eaf9f 10212#endif /* !CONFIG_USER_ONLY */
0480f69a 10213
aaec1432
RH
10214uint64_t arm_sctlr(CPUARMState *env, int el)
10215{
10216 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10217 if (el == 0) {
10218 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
b6ad6062
RDC
10219 el = (mmu_idx == ARMMMUIdx_E20_0 || mmu_idx == ARMMMUIdx_SE20_0)
10220 ? 2 : 1;
aaec1432
RH
10221 }
10222 return env->cp15.sctlr_el[el];
10223}
c47eaf9f 10224
0480f69a 10225/* Return the SCTLR value which controls this address translation regime */
aaec1432 10226static inline uint64_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
0480f69a
PM
10227{
10228 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
10229}
10230
aaec1432
RH
10231#ifndef CONFIG_USER_ONLY
10232
0480f69a
PM
10233/* Return true if the specified stage of address translation is disabled */
10234static inline bool regime_translation_disabled(CPUARMState *env,
10235 ARMMMUIdx mmu_idx)
10236{
e04a5752
RDC
10237 uint64_t hcr_el2;
10238
29c483a5 10239 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 10240 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
10241 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
10242 case R_V7M_MPU_CTRL_ENABLE_MASK:
10243 /* Enabled, but not for HardFault and NMI */
62593718 10244 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
3bef7012
PM
10245 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
10246 /* Enabled for all cases */
10247 return false;
10248 case 0:
10249 default:
10250 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
10251 * we warned about that in armv7m_nvic.c when the guest set it.
10252 */
10253 return true;
10254 }
29c483a5
MD
10255 }
10256
e04a5752
RDC
10257 hcr_el2 = arm_hcr_el2_eff(env);
10258
b1a10c86 10259 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
9d1bab33 10260 /* HCR.DC means HCR.VM behaves as 1 */
e04a5752 10261 return (hcr_el2 & (HCR_DC | HCR_VM)) == 0;
0480f69a 10262 }
3d0e3080 10263
e04a5752 10264 if (hcr_el2 & HCR_TGE) {
3d0e3080
PM
10265 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
10266 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
10267 return true;
10268 }
10269 }
10270
e04a5752 10271 if ((hcr_el2 & HCR_DC) && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
9d1bab33
PM
10272 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
10273 return true;
10274 }
10275
0480f69a
PM
10276 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
10277}
10278
73462ddd
PC
10279static inline bool regime_translation_big_endian(CPUARMState *env,
10280 ARMMMUIdx mmu_idx)
10281{
10282 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
10283}
10284
c47eaf9f
PM
10285/* Return the TTBR associated with this translation regime */
10286static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
10287 int ttbrn)
10288{
97fa9350 10289 if (mmu_idx == ARMMMUIdx_Stage2) {
c47eaf9f
PM
10290 return env->cp15.vttbr_el2;
10291 }
b1a10c86
RDC
10292 if (mmu_idx == ARMMMUIdx_Stage2_S) {
10293 return env->cp15.vsttbr_el2;
10294 }
c47eaf9f
PM
10295 if (ttbrn == 0) {
10296 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
10297 } else {
10298 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
10299 }
10300}
10301
10302#endif /* !CONFIG_USER_ONLY */
10303
8bd5c820
PM
10304/* Convert a possible stage1+2 MMU index into the appropriate
10305 * stage 1 MMU index
10306 */
10307static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
10308{
b9f6033c 10309 switch (mmu_idx) {
b1a10c86
RDC
10310 case ARMMMUIdx_SE10_0:
10311 return ARMMMUIdx_Stage1_SE0;
10312 case ARMMMUIdx_SE10_1:
10313 return ARMMMUIdx_Stage1_SE1;
10314 case ARMMMUIdx_SE10_1_PAN:
10315 return ARMMMUIdx_Stage1_SE1_PAN;
b9f6033c
RH
10316 case ARMMMUIdx_E10_0:
10317 return ARMMMUIdx_Stage1_E0;
10318 case ARMMMUIdx_E10_1:
10319 return ARMMMUIdx_Stage1_E1;
452ef8cb
RH
10320 case ARMMMUIdx_E10_1_PAN:
10321 return ARMMMUIdx_Stage1_E1_PAN;
b9f6033c
RH
10322 default:
10323 return mmu_idx;
8bd5c820 10324 }
8bd5c820
PM
10325}
10326
0480f69a
PM
10327/* Return true if the translation regime is using LPAE format page tables */
10328static inline bool regime_using_lpae_format(CPUARMState *env,
10329 ARMMMUIdx mmu_idx)
10330{
10331 int el = regime_el(env, mmu_idx);
10332 if (el == 2 || arm_el_is_aa64(env, el)) {
10333 return true;
10334 }
10335 if (arm_feature(env, ARM_FEATURE_LPAE)
10336 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
10337 return true;
10338 }
10339 return false;
10340}
10341
deb2db99
AR
10342/* Returns true if the stage 1 translation regime is using LPAE format page
10343 * tables. Used when raising alignment exceptions, whose FSR changes depending
10344 * on whether the long or short descriptor format is in use. */
10345bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 10346{
8bd5c820 10347 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 10348
30901475
AB
10349 return regime_using_lpae_format(env, mmu_idx);
10350}
10351
c47eaf9f 10352#ifndef CONFIG_USER_ONLY
0480f69a
PM
10353static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
10354{
10355 switch (mmu_idx) {
fba37aed 10356 case ARMMMUIdx_SE10_0:
b9f6033c 10357 case ARMMMUIdx_E20_0:
b6ad6062 10358 case ARMMMUIdx_SE20_0:
2859d7b5 10359 case ARMMMUIdx_Stage1_E0:
b1a10c86 10360 case ARMMMUIdx_Stage1_SE0:
e7b921c2 10361 case ARMMMUIdx_MUser:
871bec7c 10362 case ARMMMUIdx_MSUser:
62593718
PM
10363 case ARMMMUIdx_MUserNegPri:
10364 case ARMMMUIdx_MSUserNegPri:
0480f69a
PM
10365 return true;
10366 default:
10367 return false;
01b98b68
RH
10368 case ARMMMUIdx_E10_0:
10369 case ARMMMUIdx_E10_1:
452ef8cb 10370 case ARMMMUIdx_E10_1_PAN:
0480f69a
PM
10371 g_assert_not_reached();
10372 }
10373}
10374
0fbf5238
AJ
10375/* Translate section/page access permissions to page
10376 * R/W protection flags
d76951b6
AJ
10377 *
10378 * @env: CPUARMState
10379 * @mmu_idx: MMU index indicating required translation regime
10380 * @ap: The 3-bit access permissions (AP[2:0])
10381 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
10382 */
10383static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
10384 int ap, int domain_prot)
10385{
554b0b09
PM
10386 bool is_user = regime_is_user(env, mmu_idx);
10387
10388 if (domain_prot == 3) {
10389 return PAGE_READ | PAGE_WRITE;
10390 }
10391
554b0b09
PM
10392 switch (ap) {
10393 case 0:
10394 if (arm_feature(env, ARM_FEATURE_V7)) {
10395 return 0;
10396 }
554b0b09
PM
10397 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
10398 case SCTLR_S:
10399 return is_user ? 0 : PAGE_READ;
10400 case SCTLR_R:
10401 return PAGE_READ;
10402 default:
10403 return 0;
10404 }
10405 case 1:
10406 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10407 case 2:
87c3d486 10408 if (is_user) {
0fbf5238 10409 return PAGE_READ;
87c3d486 10410 } else {
554b0b09 10411 return PAGE_READ | PAGE_WRITE;
87c3d486 10412 }
554b0b09
PM
10413 case 3:
10414 return PAGE_READ | PAGE_WRITE;
10415 case 4: /* Reserved. */
10416 return 0;
10417 case 5:
0fbf5238 10418 return is_user ? 0 : PAGE_READ;
554b0b09 10419 case 6:
0fbf5238 10420 return PAGE_READ;
554b0b09 10421 case 7:
87c3d486 10422 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 10423 return 0;
87c3d486 10424 }
0fbf5238 10425 return PAGE_READ;
554b0b09 10426 default:
0fbf5238 10427 g_assert_not_reached();
554b0b09 10428 }
b5ff1b31
FB
10429}
10430
d76951b6
AJ
10431/* Translate section/page access permissions to page
10432 * R/W protection flags.
10433 *
d76951b6 10434 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 10435 * @is_user: TRUE if accessing from PL0
d76951b6 10436 */
d8e052b3 10437static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 10438{
d76951b6
AJ
10439 switch (ap) {
10440 case 0:
10441 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10442 case 1:
10443 return PAGE_READ | PAGE_WRITE;
10444 case 2:
10445 return is_user ? 0 : PAGE_READ;
10446 case 3:
10447 return PAGE_READ;
10448 default:
10449 g_assert_not_reached();
10450 }
10451}
10452
d8e052b3
AJ
10453static inline int
10454simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
10455{
10456 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
10457}
10458
6ab1a5ee
EI
10459/* Translate S2 section/page access permissions to protection flags
10460 *
10461 * @env: CPUARMState
10462 * @s2ap: The 2-bit stage2 access permissions (S2AP)
ce3125be
PM
10463 * @xn: XN (execute-never) bits
10464 * @s1_is_el0: true if this is S2 of an S1+2 walk for EL0
6ab1a5ee 10465 */
ce3125be 10466static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0)
6ab1a5ee
EI
10467{
10468 int prot = 0;
10469
10470 if (s2ap & 1) {
10471 prot |= PAGE_READ;
10472 }
10473 if (s2ap & 2) {
10474 prot |= PAGE_WRITE;
10475 }
ce3125be
PM
10476
10477 if (cpu_isar_feature(any_tts2uxn, env_archcpu(env))) {
10478 switch (xn) {
10479 case 0:
dfda6837 10480 prot |= PAGE_EXEC;
ce3125be
PM
10481 break;
10482 case 1:
10483 if (s1_is_el0) {
10484 prot |= PAGE_EXEC;
10485 }
10486 break;
10487 case 2:
10488 break;
10489 case 3:
10490 if (!s1_is_el0) {
10491 prot |= PAGE_EXEC;
10492 }
10493 break;
10494 default:
10495 g_assert_not_reached();
10496 }
10497 } else {
10498 if (!extract32(xn, 1, 1)) {
10499 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
10500 prot |= PAGE_EXEC;
10501 }
dfda6837 10502 }
6ab1a5ee
EI
10503 }
10504 return prot;
10505}
10506
d8e052b3
AJ
10507/* Translate section/page access permissions to protection flags
10508 *
10509 * @env: CPUARMState
10510 * @mmu_idx: MMU index indicating required translation regime
10511 * @is_aa64: TRUE if AArch64
10512 * @ap: The 2-bit simple AP (AP[2:1])
10513 * @ns: NS (non-secure) bit
10514 * @xn: XN (execute-never) bit
10515 * @pxn: PXN (privileged execute-never) bit
10516 */
10517static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
10518 int ap, int ns, int xn, int pxn)
10519{
10520 bool is_user = regime_is_user(env, mmu_idx);
10521 int prot_rw, user_rw;
10522 bool have_wxn;
10523 int wxn = 0;
10524
97fa9350 10525 assert(mmu_idx != ARMMMUIdx_Stage2);
b1a10c86 10526 assert(mmu_idx != ARMMMUIdx_Stage2_S);
d8e052b3
AJ
10527
10528 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
10529 if (is_user) {
10530 prot_rw = user_rw;
10531 } else {
81636b70 10532 if (user_rw && regime_is_pan(env, mmu_idx)) {
f4e1dbc5
PM
10533 /* PAN forbids data accesses but doesn't affect insn fetch */
10534 prot_rw = 0;
10535 } else {
10536 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
81636b70 10537 }
d8e052b3
AJ
10538 }
10539
10540 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
10541 return prot_rw;
10542 }
10543
10544 /* TODO have_wxn should be replaced with
10545 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
10546 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
10547 * compatible processors have EL2, which is required for [U]WXN.
10548 */
10549 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
10550
10551 if (have_wxn) {
10552 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
10553 }
10554
10555 if (is_aa64) {
339370b9
RH
10556 if (regime_has_2_ranges(mmu_idx) && !is_user) {
10557 xn = pxn || (user_rw & PAGE_WRITE);
d8e052b3
AJ
10558 }
10559 } else if (arm_feature(env, ARM_FEATURE_V7)) {
10560 switch (regime_el(env, mmu_idx)) {
10561 case 1:
10562 case 3:
10563 if (is_user) {
10564 xn = xn || !(user_rw & PAGE_READ);
10565 } else {
10566 int uwxn = 0;
10567 if (have_wxn) {
10568 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
10569 }
10570 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
10571 (uwxn && (user_rw & PAGE_WRITE));
10572 }
10573 break;
10574 case 2:
10575 break;
10576 }
10577 } else {
10578 xn = wxn = 0;
10579 }
10580
10581 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
10582 return prot_rw;
10583 }
10584 return prot_rw | PAGE_EXEC;
10585}
10586
0480f69a
PM
10587static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
10588 uint32_t *table, uint32_t address)
b2fa1797 10589{
0480f69a 10590 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 10591 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 10592
11f136ee
FA
10593 if (address & tcr->mask) {
10594 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
10595 /* Translation table walk disabled for TTBR1 */
10596 return false;
10597 }
aef878be 10598 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 10599 } else {
11f136ee 10600 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
10601 /* Translation table walk disabled for TTBR0 */
10602 return false;
10603 }
aef878be 10604 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
10605 }
10606 *table |= (address >> 18) & 0x3ffc;
10607 return true;
b2fa1797
PB
10608}
10609
37785977
EI
10610/* Translate a S1 pagetable walk through S2 if needed. */
10611static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
3d4bd397 10612 hwaddr addr, bool *is_secure,
37785977
EI
10613 ARMMMUFaultInfo *fi)
10614{
fee7aa46 10615 if (arm_mmu_idx_is_stage1_of_2(mmu_idx) &&
97fa9350 10616 !regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
37785977
EI
10617 target_ulong s2size;
10618 hwaddr s2pa;
10619 int s2prot;
10620 int ret;
b1a10c86
RDC
10621 ARMMMUIdx s2_mmu_idx = *is_secure ? ARMMMUIdx_Stage2_S
10622 : ARMMMUIdx_Stage2;
eadb2feb 10623 ARMCacheAttrs cacheattrs = {};
3d4bd397
RDC
10624 MemTxAttrs txattrs = {};
10625
b1a10c86 10626 ret = get_phys_addr_lpae(env, addr, MMU_DATA_LOAD, s2_mmu_idx, false,
59dff859 10627 &s2pa, &txattrs, &s2prot, &s2size, fi,
a6d6f37a 10628 &cacheattrs);
37785977 10629 if (ret) {
3b39d734 10630 assert(fi->type != ARMFault_None);
37785977
EI
10631 fi->s2addr = addr;
10632 fi->stage2 = true;
10633 fi->s1ptw = true;
9861248f 10634 fi->s1ns = !*is_secure;
37785977
EI
10635 return ~0;
10636 }
e04a5752
RDC
10637 if ((arm_hcr_el2_eff(env) & HCR_PTW) &&
10638 (cacheattrs.attrs & 0xf0) == 0) {
a6d6f37a
RH
10639 /*
10640 * PTW set and S1 walk touched S2 Device memory:
10641 * generate Permission fault.
10642 */
eadb2feb
PM
10643 fi->type = ARMFault_Permission;
10644 fi->s2addr = addr;
10645 fi->stage2 = true;
10646 fi->s1ptw = true;
9861248f 10647 fi->s1ns = !*is_secure;
eadb2feb
PM
10648 return ~0;
10649 }
588c6dd1
RDC
10650
10651 if (arm_is_secure_below_el3(env)) {
10652 /* Check if page table walk is to secure or non-secure PA space. */
10653 if (*is_secure) {
10654 *is_secure = !(env->cp15.vstcr_el2.raw_tcr & VSTCR_SW);
10655 } else {
10656 *is_secure = !(env->cp15.vtcr_el2.raw_tcr & VTCR_NSW);
10657 }
10658 } else {
10659 assert(!*is_secure);
10660 }
10661
37785977
EI
10662 addr = s2pa;
10663 }
10664 return addr;
10665}
10666
14577270 10667/* All loads done in the course of a page table walk go through here. */
a614e698 10668static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 10669 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 10670{
a614e698
EI
10671 ARMCPU *cpu = ARM_CPU(cs);
10672 CPUARMState *env = &cpu->env;
ebca90e4 10673 MemTxAttrs attrs = {};
3b39d734 10674 MemTxResult result = MEMTX_OK;
5ce4ff65 10675 AddressSpace *as;
3b39d734 10676 uint32_t data;
ebca90e4 10677
3d4bd397 10678 addr = S1_ptw_translate(env, mmu_idx, addr, &is_secure, fi);
ebca90e4 10679 attrs.secure = is_secure;
5ce4ff65 10680 as = arm_addressspace(cs, attrs);
a614e698
EI
10681 if (fi->s1ptw) {
10682 return 0;
10683 }
73462ddd 10684 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 10685 data = address_space_ldl_be(as, addr, attrs, &result);
73462ddd 10686 } else {
3b39d734 10687 data = address_space_ldl_le(as, addr, attrs, &result);
73462ddd 10688 }
3b39d734
PM
10689 if (result == MEMTX_OK) {
10690 return data;
10691 }
10692 fi->type = ARMFault_SyncExternalOnWalk;
10693 fi->ea = arm_extabort_type(result);
10694 return 0;
ebca90e4
PM
10695}
10696
37785977 10697static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 10698 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 10699{
37785977
EI
10700 ARMCPU *cpu = ARM_CPU(cs);
10701 CPUARMState *env = &cpu->env;
ebca90e4 10702 MemTxAttrs attrs = {};
3b39d734 10703 MemTxResult result = MEMTX_OK;
5ce4ff65 10704 AddressSpace *as;
9aea1ea3 10705 uint64_t data;
ebca90e4 10706
3d4bd397 10707 addr = S1_ptw_translate(env, mmu_idx, addr, &is_secure, fi);
ebca90e4 10708 attrs.secure = is_secure;
5ce4ff65 10709 as = arm_addressspace(cs, attrs);
37785977
EI
10710 if (fi->s1ptw) {
10711 return 0;
10712 }
73462ddd 10713 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 10714 data = address_space_ldq_be(as, addr, attrs, &result);
73462ddd 10715 } else {
3b39d734
PM
10716 data = address_space_ldq_le(as, addr, attrs, &result);
10717 }
10718 if (result == MEMTX_OK) {
10719 return data;
73462ddd 10720 }
3b39d734
PM
10721 fi->type = ARMFault_SyncExternalOnWalk;
10722 fi->ea = arm_extabort_type(result);
10723 return 0;
ebca90e4
PM
10724}
10725
b7cc4e82 10726static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 10727 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10728 hwaddr *phys_ptr, int *prot,
f989983e 10729 target_ulong *page_size,
e14b5a23 10730 ARMMMUFaultInfo *fi)
b5ff1b31 10731{
2fc0cc0e 10732 CPUState *cs = env_cpu(env);
f989983e 10733 int level = 1;
b5ff1b31
FB
10734 uint32_t table;
10735 uint32_t desc;
10736 int type;
10737 int ap;
e389be16 10738 int domain = 0;
dd4ebc2e 10739 int domain_prot;
a8170e5e 10740 hwaddr phys_addr;
0480f69a 10741 uint32_t dacr;
b5ff1b31 10742
9ee6e8bb
PB
10743 /* Pagetable walk. */
10744 /* Lookup l1 descriptor. */
0480f69a 10745 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10746 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f989983e 10747 fi->type = ARMFault_Translation;
e389be16
FA
10748 goto do_fault;
10749 }
a614e698 10750 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10751 mmu_idx, fi);
3b39d734
PM
10752 if (fi->type != ARMFault_None) {
10753 goto do_fault;
10754 }
9ee6e8bb 10755 type = (desc & 3);
dd4ebc2e 10756 domain = (desc >> 5) & 0x0f;
0480f69a
PM
10757 if (regime_el(env, mmu_idx) == 1) {
10758 dacr = env->cp15.dacr_ns;
10759 } else {
10760 dacr = env->cp15.dacr_s;
10761 }
10762 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 10763 if (type == 0) {
601d70b9 10764 /* Section translation fault. */
f989983e 10765 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10766 goto do_fault;
10767 }
f989983e
PM
10768 if (type != 2) {
10769 level = 2;
10770 }
dd4ebc2e 10771 if (domain_prot == 0 || domain_prot == 2) {
f989983e 10772 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10773 goto do_fault;
10774 }
10775 if (type == 2) {
10776 /* 1Mb section. */
10777 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
10778 ap = (desc >> 10) & 3;
d4c430a8 10779 *page_size = 1024 * 1024;
9ee6e8bb
PB
10780 } else {
10781 /* Lookup l2 entry. */
554b0b09
PM
10782 if (type == 1) {
10783 /* Coarse pagetable. */
10784 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
10785 } else {
10786 /* Fine pagetable. */
10787 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
10788 }
a614e698 10789 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10790 mmu_idx, fi);
3b39d734
PM
10791 if (fi->type != ARMFault_None) {
10792 goto do_fault;
10793 }
9ee6e8bb
PB
10794 switch (desc & 3) {
10795 case 0: /* Page translation fault. */
f989983e 10796 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10797 goto do_fault;
10798 case 1: /* 64k page. */
10799 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10800 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 10801 *page_size = 0x10000;
ce819861 10802 break;
9ee6e8bb
PB
10803 case 2: /* 4k page. */
10804 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 10805 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 10806 *page_size = 0x1000;
ce819861 10807 break;
fc1891c7 10808 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 10809 if (type == 1) {
fc1891c7
PM
10810 /* ARMv6/XScale extended small page format */
10811 if (arm_feature(env, ARM_FEATURE_XSCALE)
10812 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 10813 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 10814 *page_size = 0x1000;
554b0b09 10815 } else {
fc1891c7
PM
10816 /* UNPREDICTABLE in ARMv5; we choose to take a
10817 * page translation fault.
10818 */
f989983e 10819 fi->type = ARMFault_Translation;
554b0b09
PM
10820 goto do_fault;
10821 }
10822 } else {
10823 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 10824 *page_size = 0x400;
554b0b09 10825 }
9ee6e8bb 10826 ap = (desc >> 4) & 3;
ce819861
PB
10827 break;
10828 default:
9ee6e8bb 10829 /* Never happens, but compiler isn't smart enough to tell. */
d385a605 10830 g_assert_not_reached();
ce819861 10831 }
9ee6e8bb 10832 }
0fbf5238
AJ
10833 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
10834 *prot |= *prot ? PAGE_EXEC : 0;
10835 if (!(*prot & (1 << access_type))) {
9ee6e8bb 10836 /* Access permission fault. */
f989983e 10837 fi->type = ARMFault_Permission;
9ee6e8bb
PB
10838 goto do_fault;
10839 }
10840 *phys_ptr = phys_addr;
b7cc4e82 10841 return false;
9ee6e8bb 10842do_fault:
f989983e
PM
10843 fi->domain = domain;
10844 fi->level = level;
b7cc4e82 10845 return true;
9ee6e8bb
PB
10846}
10847
b7cc4e82 10848static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 10849 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10850 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
f06cf243 10851 target_ulong *page_size, ARMMMUFaultInfo *fi)
9ee6e8bb 10852{
2fc0cc0e 10853 CPUState *cs = env_cpu(env);
0ae0326b 10854 ARMCPU *cpu = env_archcpu(env);
f06cf243 10855 int level = 1;
9ee6e8bb
PB
10856 uint32_t table;
10857 uint32_t desc;
10858 uint32_t xn;
de9b05b8 10859 uint32_t pxn = 0;
9ee6e8bb
PB
10860 int type;
10861 int ap;
de9b05b8 10862 int domain = 0;
dd4ebc2e 10863 int domain_prot;
a8170e5e 10864 hwaddr phys_addr;
0480f69a 10865 uint32_t dacr;
8bf5b6a9 10866 bool ns;
9ee6e8bb
PB
10867
10868 /* Pagetable walk. */
10869 /* Lookup l1 descriptor. */
0480f69a 10870 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10871 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f06cf243 10872 fi->type = ARMFault_Translation;
e389be16
FA
10873 goto do_fault;
10874 }
a614e698 10875 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10876 mmu_idx, fi);
3b39d734
PM
10877 if (fi->type != ARMFault_None) {
10878 goto do_fault;
10879 }
9ee6e8bb 10880 type = (desc & 3);
0ae0326b 10881 if (type == 0 || (type == 3 && !cpu_isar_feature(aa32_pxn, cpu))) {
de9b05b8
PM
10882 /* Section translation fault, or attempt to use the encoding
10883 * which is Reserved on implementations without PXN.
10884 */
f06cf243 10885 fi->type = ARMFault_Translation;
9ee6e8bb 10886 goto do_fault;
de9b05b8
PM
10887 }
10888 if ((type == 1) || !(desc & (1 << 18))) {
10889 /* Page or Section. */
dd4ebc2e 10890 domain = (desc >> 5) & 0x0f;
9ee6e8bb 10891 }
0480f69a
PM
10892 if (regime_el(env, mmu_idx) == 1) {
10893 dacr = env->cp15.dacr_ns;
10894 } else {
10895 dacr = env->cp15.dacr_s;
10896 }
f06cf243
PM
10897 if (type == 1) {
10898 level = 2;
10899 }
0480f69a 10900 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 10901 if (domain_prot == 0 || domain_prot == 2) {
f06cf243
PM
10902 /* Section or Page domain fault */
10903 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10904 goto do_fault;
10905 }
de9b05b8 10906 if (type != 1) {
9ee6e8bb
PB
10907 if (desc & (1 << 18)) {
10908 /* Supersection. */
10909 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
10910 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
10911 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 10912 *page_size = 0x1000000;
b5ff1b31 10913 } else {
9ee6e8bb
PB
10914 /* Section. */
10915 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 10916 *page_size = 0x100000;
b5ff1b31 10917 }
9ee6e8bb
PB
10918 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
10919 xn = desc & (1 << 4);
de9b05b8 10920 pxn = desc & 1;
8bf5b6a9 10921 ns = extract32(desc, 19, 1);
9ee6e8bb 10922 } else {
0ae0326b 10923 if (cpu_isar_feature(aa32_pxn, cpu)) {
de9b05b8
PM
10924 pxn = (desc >> 2) & 1;
10925 }
8bf5b6a9 10926 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
10927 /* Lookup l2 entry. */
10928 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698 10929 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10930 mmu_idx, fi);
3b39d734
PM
10931 if (fi->type != ARMFault_None) {
10932 goto do_fault;
10933 }
9ee6e8bb
PB
10934 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
10935 switch (desc & 3) {
10936 case 0: /* Page translation fault. */
f06cf243 10937 fi->type = ARMFault_Translation;
b5ff1b31 10938 goto do_fault;
9ee6e8bb
PB
10939 case 1: /* 64k page. */
10940 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10941 xn = desc & (1 << 15);
d4c430a8 10942 *page_size = 0x10000;
9ee6e8bb
PB
10943 break;
10944 case 2: case 3: /* 4k page. */
10945 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
10946 xn = desc & 1;
d4c430a8 10947 *page_size = 0x1000;
9ee6e8bb
PB
10948 break;
10949 default:
10950 /* Never happens, but compiler isn't smart enough to tell. */
d385a605 10951 g_assert_not_reached();
b5ff1b31 10952 }
9ee6e8bb 10953 }
dd4ebc2e 10954 if (domain_prot == 3) {
c0034328
JR
10955 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
10956 } else {
0480f69a 10957 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
10958 xn = 1;
10959 }
f06cf243
PM
10960 if (xn && access_type == MMU_INST_FETCH) {
10961 fi->type = ARMFault_Permission;
c0034328 10962 goto do_fault;
f06cf243 10963 }
9ee6e8bb 10964
d76951b6
AJ
10965 if (arm_feature(env, ARM_FEATURE_V6K) &&
10966 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
10967 /* The simplified model uses AP[0] as an access control bit. */
10968 if ((ap & 1) == 0) {
10969 /* Access flag fault. */
f06cf243 10970 fi->type = ARMFault_AccessFlag;
d76951b6
AJ
10971 goto do_fault;
10972 }
10973 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
10974 } else {
10975 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 10976 }
0fbf5238
AJ
10977 if (*prot && !xn) {
10978 *prot |= PAGE_EXEC;
10979 }
10980 if (!(*prot & (1 << access_type))) {
c0034328 10981 /* Access permission fault. */
f06cf243 10982 fi->type = ARMFault_Permission;
c0034328
JR
10983 goto do_fault;
10984 }
3ad493fc 10985 }
8bf5b6a9
PM
10986 if (ns) {
10987 /* The NS bit will (as required by the architecture) have no effect if
10988 * the CPU doesn't support TZ or this is a non-secure translation
10989 * regime, because the attribute will already be non-secure.
10990 */
10991 attrs->secure = false;
10992 }
9ee6e8bb 10993 *phys_ptr = phys_addr;
b7cc4e82 10994 return false;
b5ff1b31 10995do_fault:
f06cf243
PM
10996 fi->domain = domain;
10997 fi->level = level;
b7cc4e82 10998 return true;
b5ff1b31
FB
10999}
11000
1853d5a9 11001/*
a0e966c9 11002 * check_s2_mmu_setup
1853d5a9
EI
11003 * @cpu: ARMCPU
11004 * @is_aa64: True if the translation regime is in AArch64 state
11005 * @startlevel: Suggested starting level
11006 * @inputsize: Bitsize of IPAs
11007 * @stride: Page-table stride (See the ARM ARM)
11008 *
a0e966c9
EI
11009 * Returns true if the suggested S2 translation parameters are OK and
11010 * false otherwise.
1853d5a9 11011 */
a0e966c9 11012static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
49ba115b 11013 int inputsize, int stride, int outputsize)
1853d5a9 11014{
98d68ec2
EI
11015 const int grainsize = stride + 3;
11016 int startsizecheck;
11017
ef56c242
RH
11018 /*
11019 * Negative levels are usually not allowed...
11020 * Except for FEAT_LPA2, 4k page table, 52-bit address space, which
11021 * begins with level -1. Note that previous feature tests will have
11022 * eliminated this combination if it is not enabled.
11023 */
11024 if (level < (inputsize == 52 && stride == 9 ? -1 : 0)) {
1853d5a9
EI
11025 return false;
11026 }
11027
98d68ec2
EI
11028 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
11029 if (startsizecheck < 1 || startsizecheck > stride + 4) {
11030 return false;
11031 }
11032
1853d5a9 11033 if (is_aa64) {
1853d5a9
EI
11034 switch (stride) {
11035 case 13: /* 64KB Pages. */
49ba115b 11036 if (level == 0 || (level == 1 && outputsize <= 42)) {
1853d5a9
EI
11037 return false;
11038 }
11039 break;
11040 case 11: /* 16KB Pages. */
49ba115b 11041 if (level == 0 || (level == 1 && outputsize <= 40)) {
1853d5a9
EI
11042 return false;
11043 }
11044 break;
11045 case 9: /* 4KB Pages. */
49ba115b 11046 if (level == 0 && outputsize <= 42) {
1853d5a9
EI
11047 return false;
11048 }
11049 break;
11050 default:
11051 g_assert_not_reached();
11052 }
3526423e
EI
11053
11054 /* Inputsize checks. */
49ba115b
RH
11055 if (inputsize > outputsize &&
11056 (arm_el_is_aa64(&cpu->env, 1) || inputsize > 40)) {
3526423e
EI
11057 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
11058 return false;
11059 }
1853d5a9 11060 } else {
1853d5a9
EI
11061 /* AArch32 only supports 4KB pages. Assert on that. */
11062 assert(stride == 9);
11063
11064 if (level == 0) {
11065 return false;
11066 }
1853d5a9
EI
11067 }
11068 return true;
11069}
11070
5b2d261d
AB
11071/* Translate from the 4-bit stage 2 representation of
11072 * memory attributes (without cache-allocation hints) to
11073 * the 8-bit representation of the stage 1 MAIR registers
11074 * (which includes allocation hints).
11075 *
11076 * ref: shared/translation/attrs/S2AttrDecode()
11077 * .../S2ConvertAttrsHints()
11078 */
11079static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
11080{
11081 uint8_t hiattr = extract32(s2attrs, 2, 2);
11082 uint8_t loattr = extract32(s2attrs, 0, 2);
11083 uint8_t hihint = 0, lohint = 0;
11084
11085 if (hiattr != 0) { /* normal memory */
e04a5752 11086 if (arm_hcr_el2_eff(env) & HCR_CD) { /* cache disabled */
5b2d261d
AB
11087 hiattr = loattr = 1; /* non-cacheable */
11088 } else {
11089 if (hiattr != 1) { /* Write-through or write-back */
11090 hihint = 3; /* RW allocate */
11091 }
11092 if (loattr != 1) { /* Write-through or write-back */
11093 lohint = 3; /* RW allocate */
11094 }
11095 }
11096 }
11097
11098 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
11099}
c47eaf9f 11100#endif /* !CONFIG_USER_ONLY */
5b2d261d 11101
f4ecc015
RH
11102/* This mapping is common between ID_AA64MMFR0.PARANGE and TCR_ELx.{I}PS. */
11103static const uint8_t pamax_map[] = {
11104 [0] = 32,
11105 [1] = 36,
11106 [2] = 40,
11107 [3] = 42,
11108 [4] = 44,
11109 [5] = 48,
7a928f43 11110 [6] = 52,
f4ecc015
RH
11111};
11112
71a77257
RH
11113/* The cpu-specific constant value of PAMax; also used by hw/arm/virt. */
11114unsigned int arm_pamax(ARMCPU *cpu)
11115{
71a77257
RH
11116 unsigned int parange =
11117 FIELD_EX64(cpu->isar.id_aa64mmfr0, ID_AA64MMFR0, PARANGE);
11118
11119 /*
11120 * id_aa64mmfr0 is a read-only register so values outside of the
11121 * supported mappings can be considered an implementation error.
11122 */
11123 assert(parange < ARRAY_SIZE(pamax_map));
11124 return pamax_map[parange];
11125}
11126
b830a5ee
RH
11127static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
11128{
11129 if (regime_has_2_ranges(mmu_idx)) {
11130 return extract64(tcr, 37, 2);
b1a10c86 11131 } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
b830a5ee
RH
11132 return 0; /* VTCR_EL2 */
11133 } else {
3e270f67
RH
11134 /* Replicate the single TBI bit so we always have 2 bits. */
11135 return extract32(tcr, 20, 1) * 3;
b830a5ee
RH
11136 }
11137}
11138
11139static int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
11140{
11141 if (regime_has_2_ranges(mmu_idx)) {
11142 return extract64(tcr, 51, 2);
b1a10c86 11143 } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
b830a5ee
RH
11144 return 0; /* VTCR_EL2 */
11145 } else {
3e270f67
RH
11146 /* Replicate the single TBID bit so we always have 2 bits. */
11147 return extract32(tcr, 29, 1) * 3;
b830a5ee
RH
11148 }
11149}
11150
81ae05fa
RH
11151static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
11152{
11153 if (regime_has_2_ranges(mmu_idx)) {
11154 return extract64(tcr, 57, 2);
11155 } else {
11156 /* Replicate the single TCMA bit so we always have 2 bits. */
11157 return extract32(tcr, 30, 1) * 3;
11158 }
11159}
11160
b830a5ee
RH
11161ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11162 ARMMMUIdx mmu_idx, bool data)
ba97be9f
RH
11163{
11164 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
ef56c242
RH
11165 bool epd, hpd, using16k, using64k, tsz_oob, ds;
11166 int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11167 ARMCPU *cpu = env_archcpu(env);
ba97be9f 11168
339370b9 11169 if (!regime_has_2_ranges(mmu_idx)) {
71d18164 11170 select = 0;
ba97be9f
RH
11171 tsz = extract32(tcr, 0, 6);
11172 using64k = extract32(tcr, 14, 1);
11173 using16k = extract32(tcr, 15, 1);
b1a10c86 11174 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
ba97be9f 11175 /* VTCR_EL2 */
b830a5ee 11176 hpd = false;
ba97be9f 11177 } else {
ba97be9f
RH
11178 hpd = extract32(tcr, 24, 1);
11179 }
11180 epd = false;
ef56c242 11181 sh = extract32(tcr, 12, 2);
f4ecc015 11182 ps = extract32(tcr, 16, 3);
ef56c242 11183 ds = extract64(tcr, 32, 1);
ba97be9f 11184 } else {
71d18164
RH
11185 /*
11186 * Bit 55 is always between the two regions, and is canonical for
11187 * determining if address tagging is enabled.
11188 */
11189 select = extract64(va, 55, 1);
11190 if (!select) {
11191 tsz = extract32(tcr, 0, 6);
11192 epd = extract32(tcr, 7, 1);
ef56c242 11193 sh = extract32(tcr, 12, 2);
71d18164
RH
11194 using64k = extract32(tcr, 14, 1);
11195 using16k = extract32(tcr, 15, 1);
71d18164 11196 hpd = extract64(tcr, 41, 1);
71d18164
RH
11197 } else {
11198 int tg = extract32(tcr, 30, 2);
11199 using16k = tg == 1;
11200 using64k = tg == 3;
11201 tsz = extract32(tcr, 16, 6);
11202 epd = extract32(tcr, 23, 1);
ef56c242 11203 sh = extract32(tcr, 28, 2);
71d18164 11204 hpd = extract64(tcr, 42, 1);
71d18164 11205 }
f4ecc015 11206 ps = extract64(tcr, 32, 3);
ef56c242 11207 ds = extract64(tcr, 59, 1);
ba97be9f 11208 }
c36c65ea 11209
ef56c242 11210 if (cpu_isar_feature(aa64_st, cpu)) {
c36c65ea
RDC
11211 max_tsz = 48 - using64k;
11212 } else {
11213 max_tsz = 39;
11214 }
0af312b6 11215
ef56c242
RH
11216 /*
11217 * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11218 * adjust the effective value of DS, as documented.
11219 */
0af312b6
RH
11220 min_tsz = 16;
11221 if (using64k) {
ef56c242
RH
11222 if (cpu_isar_feature(aa64_lva, cpu)) {
11223 min_tsz = 12;
11224 }
11225 ds = false;
11226 } else if (ds) {
11227 switch (mmu_idx) {
11228 case ARMMMUIdx_Stage2:
11229 case ARMMMUIdx_Stage2_S:
11230 if (using16k) {
11231 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11232 } else {
11233 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11234 }
11235 break;
11236 default:
11237 if (using16k) {
11238 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11239 } else {
11240 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11241 }
11242 break;
11243 }
11244 if (ds) {
0af312b6
RH
11245 min_tsz = 12;
11246 }
11247 }
c36c65ea 11248
ebf93ce7
RH
11249 if (tsz > max_tsz) {
11250 tsz = max_tsz;
11251 tsz_oob = true;
11252 } else if (tsz < min_tsz) {
11253 tsz = min_tsz;
11254 tsz_oob = true;
11255 } else {
11256 tsz_oob = false;
11257 }
ba97be9f 11258
b830a5ee
RH
11259 /* Present TBI as a composite with TBID. */
11260 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11261 if (!data) {
11262 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11263 }
11264 tbi = (tbi >> select) & 1;
11265
ba97be9f
RH
11266 return (ARMVAParameters) {
11267 .tsz = tsz,
f4ecc015 11268 .ps = ps,
ef56c242 11269 .sh = sh,
ba97be9f
RH
11270 .select = select,
11271 .tbi = tbi,
11272 .epd = epd,
11273 .hpd = hpd,
11274 .using16k = using16k,
11275 .using64k = using64k,
ebf93ce7 11276 .tsz_oob = tsz_oob,
ef56c242 11277 .ds = ds,
ba97be9f
RH
11278 };
11279}
11280
c47eaf9f 11281#ifndef CONFIG_USER_ONLY
ba97be9f
RH
11282static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
11283 ARMMMUIdx mmu_idx)
11284{
11285 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
11286 uint32_t el = regime_el(env, mmu_idx);
11287 int select, tsz;
11288 bool epd, hpd;
11289
b1a10c86
RDC
11290 assert(mmu_idx != ARMMMUIdx_Stage2_S);
11291
97fa9350 11292 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f
RH
11293 /* VTCR */
11294 bool sext = extract32(tcr, 4, 1);
11295 bool sign = extract32(tcr, 3, 1);
11296
11297 /*
11298 * If the sign-extend bit is not the same as t0sz[3], the result
11299 * is unpredictable. Flag this as a guest error.
11300 */
11301 if (sign != sext) {
11302 qemu_log_mask(LOG_GUEST_ERROR,
11303 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
11304 }
11305 tsz = sextract32(tcr, 0, 4) + 8;
11306 select = 0;
11307 hpd = false;
11308 epd = false;
11309 } else if (el == 2) {
11310 /* HTCR */
11311 tsz = extract32(tcr, 0, 3);
11312 select = 0;
11313 hpd = extract64(tcr, 24, 1);
11314 epd = false;
11315 } else {
11316 int t0sz = extract32(tcr, 0, 3);
11317 int t1sz = extract32(tcr, 16, 3);
11318
11319 if (t1sz == 0) {
11320 select = va > (0xffffffffu >> t0sz);
11321 } else {
11322 /* Note that we will detect errors later. */
11323 select = va >= ~(0xffffffffu >> t1sz);
11324 }
11325 if (!select) {
11326 tsz = t0sz;
11327 epd = extract32(tcr, 7, 1);
11328 hpd = extract64(tcr, 41, 1);
11329 } else {
11330 tsz = t1sz;
11331 epd = extract32(tcr, 23, 1);
11332 hpd = extract64(tcr, 42, 1);
11333 }
11334 /* For aarch32, hpd0 is not enabled without t2e as well. */
11335 hpd &= extract32(tcr, 6, 1);
11336 }
11337
11338 return (ARMVAParameters) {
11339 .tsz = tsz,
11340 .select = select,
11341 .epd = epd,
11342 .hpd = hpd,
11343 };
11344}
11345
ff7de2fc
PM
11346/**
11347 * get_phys_addr_lpae: perform one stage of page table walk, LPAE format
11348 *
11349 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
11350 * prot and page_size may not be filled in, and the populated fsr value provides
11351 * information on why the translation aborted, in the format of a long-format
11352 * DFSR/IFSR fault register, with the following caveats:
11353 * * the WnR bit is never set (the caller must do this).
11354 *
11355 * @env: CPUARMState
11356 * @address: virtual address to get physical address for
11357 * @access_type: MMU_DATA_LOAD, MMU_DATA_STORE or MMU_INST_FETCH
11358 * @mmu_idx: MMU index indicating required translation regime
11359 * @s1_is_el0: if @mmu_idx is ARMMMUIdx_Stage2 (so this is a stage 2 page table
11360 * walk), must be true if this is stage 2 of a stage 1+2 walk for an
11361 * EL0 access). If @mmu_idx is anything else, @s1_is_el0 is ignored.
11362 * @phys_ptr: set to the physical address corresponding to the virtual address
11363 * @attrs: set to the memory transaction attributes to use
11364 * @prot: set to the permissions for the page containing phys_ptr
11365 * @page_size_ptr: set to the size of the page containing phys_ptr
11366 * @fi: set to fault info if the translation fails
11367 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
11368 */
98e87797 11369static bool get_phys_addr_lpae(CPUARMState *env, uint64_t address,
03ae85f8 11370 MMUAccessType access_type, ARMMMUIdx mmu_idx,
ff7de2fc 11371 bool s1_is_el0,
b7cc4e82 11372 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 11373 target_ulong *page_size_ptr,
5b2d261d 11374 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
3dde962f 11375{
2fc0cc0e 11376 ARMCPU *cpu = env_archcpu(env);
1853d5a9 11377 CPUState *cs = CPU(cpu);
3dde962f 11378 /* Read an LPAE long-descriptor translation table. */
da909b2c 11379 ARMFaultType fault_type = ARMFault_Translation;
1b4093ea 11380 uint32_t level;
ba97be9f 11381 ARMVAParameters param;
3dde962f 11382 uint64_t ttbr;
dddb5223 11383 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f 11384 uint32_t tableattrs;
36d820af 11385 target_ulong page_size;
3dde962f 11386 uint32_t attrs;
ba97be9f 11387 int32_t stride;
49ba115b 11388 int addrsize, inputsize, outputsize;
0480f69a 11389 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 11390 int ap, ns, xn, pxn;
88e8add8 11391 uint32_t el = regime_el(env, mmu_idx);
6109769a 11392 uint64_t descaddrmask;
6e99f762 11393 bool aarch64 = arm_el_is_aa64(env, el);
1bafc2ba 11394 bool guarded = false;
0480f69a 11395
07d1be3b 11396 /* TODO: This code does not support shareability levels. */
6e99f762 11397 if (aarch64) {
f4ecc015
RH
11398 int ps;
11399
ba97be9f
RH
11400 param = aa64_va_parameters(env, address, mmu_idx,
11401 access_type != MMU_INST_FETCH);
1b4093ea 11402 level = 0;
ebf93ce7
RH
11403
11404 /*
11405 * If TxSZ is programmed to a value larger than the maximum,
11406 * or smaller than the effective minimum, it is IMPLEMENTATION
11407 * DEFINED whether we behave as if the field were programmed
11408 * within bounds, or if a level 0 Translation fault is generated.
11409 *
11410 * With FEAT_LVA, fault on less than minimum becomes required,
11411 * so our choice is to always raise the fault.
11412 */
11413 if (param.tsz_oob) {
11414 fault_type = ARMFault_Translation;
11415 goto do_fault;
11416 }
11417
ba97be9f
RH
11418 addrsize = 64 - 8 * param.tbi;
11419 inputsize = 64 - param.tsz;
f4ecc015
RH
11420
11421 /*
11422 * Bound PS by PARANGE to find the effective output address size.
11423 * ID_AA64MMFR0 is a read-only register so values outside of the
11424 * supported mappings can be considered an implementation error.
11425 */
11426 ps = FIELD_EX64(cpu->isar.id_aa64mmfr0, ID_AA64MMFR0, PARANGE);
11427 ps = MIN(ps, param.ps);
11428 assert(ps < ARRAY_SIZE(pamax_map));
11429 outputsize = pamax_map[ps];
d0a2cbce 11430 } else {
ba97be9f 11431 param = aa32_va_parameters(env, address, mmu_idx);
1b4093ea 11432 level = 1;
97fa9350 11433 addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32);
ba97be9f 11434 inputsize = addrsize - param.tsz;
49ba115b 11435 outputsize = 40;
2c8dd318 11436 }
3dde962f 11437
ba97be9f
RH
11438 /*
11439 * We determined the region when collecting the parameters, but we
11440 * have not yet validated that the address is valid for the region.
11441 * Extract the top bits and verify that they all match select.
36d820af
RH
11442 *
11443 * For aa32, if inputsize == addrsize, then we have selected the
11444 * region by exclusion in aa32_va_parameters and there is no more
11445 * validation to do here.
11446 */
11447 if (inputsize < addrsize) {
11448 target_ulong top_bits = sextract64(address, inputsize,
11449 addrsize - inputsize);
03f27724 11450 if (-top_bits != param.select) {
36d820af
RH
11451 /* The gap between the two regions is a Translation fault */
11452 fault_type = ARMFault_Translation;
11453 goto do_fault;
11454 }
3dde962f
PM
11455 }
11456
ba97be9f
RH
11457 if (param.using64k) {
11458 stride = 13;
11459 } else if (param.using16k) {
11460 stride = 11;
11461 } else {
11462 stride = 9;
11463 }
11464
3dde962f
PM
11465 /* Note that QEMU ignores shareability and cacheability attributes,
11466 * so we don't need to do anything with the SH, ORGN, IRGN fields
11467 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
11468 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
11469 * implement any ASID-like capability so we can ignore it (instead
11470 * we will always flush the TLB any time the ASID is changed).
11471 */
ba97be9f 11472 ttbr = regime_ttbr(env, mmu_idx, param.select);
3dde962f 11473
0480f69a 11474 /* Here we should have set up all the parameters for the translation:
6e99f762 11475 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
11476 */
11477
ba97be9f 11478 if (param.epd) {
88e8add8
GB
11479 /* Translation table walk disabled => Translation fault on TLB miss
11480 * Note: This is always 0 on 64-bit EL2 and EL3.
11481 */
3dde962f
PM
11482 goto do_fault;
11483 }
11484
b1a10c86 11485 if (mmu_idx != ARMMMUIdx_Stage2 && mmu_idx != ARMMMUIdx_Stage2_S) {
1853d5a9
EI
11486 /* The starting level depends on the virtual address size (which can
11487 * be up to 48 bits) and the translation granule size. It indicates
11488 * the number of strides (stride bits at a time) needed to
11489 * consume the bits of the input address. In the pseudocode this is:
11490 * level = 4 - RoundUp((inputsize - grainsize) / stride)
11491 * where their 'inputsize' is our 'inputsize', 'grainsize' is
11492 * our 'stride + 3' and 'stride' is our 'stride'.
11493 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
11494 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
11495 * = 4 - (inputsize - 4) / stride;
11496 */
11497 level = 4 - (inputsize - 4) / stride;
11498 } else {
11499 /* For stage 2 translations the starting level is specified by the
11500 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
11501 */
1b4093ea 11502 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
ef56c242 11503 uint32_t sl2 = extract64(tcr->raw_tcr, 33, 1);
1b4093ea 11504 uint32_t startlevel;
1853d5a9
EI
11505 bool ok;
11506
ef56c242
RH
11507 /* SL2 is RES0 unless DS=1 & 4kb granule. */
11508 if (param.ds && stride == 9 && sl2) {
11509 if (sl0 != 0) {
11510 level = 0;
11511 fault_type = ARMFault_Translation;
11512 goto do_fault;
11513 }
11514 startlevel = -1;
11515 } else if (!aarch64 || stride == 9) {
1853d5a9 11516 /* AArch32 or 4KB pages */
1b4093ea 11517 startlevel = 2 - sl0;
c36c65ea
RDC
11518
11519 if (cpu_isar_feature(aa64_st, cpu)) {
11520 startlevel &= 3;
11521 }
1853d5a9
EI
11522 } else {
11523 /* 16KB or 64KB pages */
1b4093ea 11524 startlevel = 3 - sl0;
1853d5a9
EI
11525 }
11526
11527 /* Check that the starting level is valid. */
6e99f762 11528 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
49ba115b 11529 inputsize, stride, outputsize);
1853d5a9 11530 if (!ok) {
da909b2c 11531 fault_type = ARMFault_Translation;
1853d5a9
EI
11532 goto do_fault;
11533 }
1b4093ea 11534 level = startlevel;
1853d5a9 11535 }
3dde962f 11536
d06449f2
RH
11537 indexmask_grainsize = MAKE_64BIT_MASK(0, stride + 3);
11538 indexmask = MAKE_64BIT_MASK(0, inputsize - (stride * (4 - level)));
3dde962f
PM
11539
11540 /* Now we can extract the actual base address from the TTBR */
2c8dd318 11541 descaddr = extract64(ttbr, 0, 48);
f4ecc015
RH
11542
11543 /*
7a928f43
RH
11544 * For FEAT_LPA and PS=6, bits [51:48] of descaddr are in [5:2] of TTBR.
11545 *
11546 * Otherwise, if the base address is out of range, raise AddressSizeFault.
f4ecc015
RH
11547 * In the pseudocode, this is !IsZero(baseregister<47:outputsize>),
11548 * but we've just cleared the bits above 47, so simplify the test.
11549 */
7a928f43
RH
11550 if (outputsize > 48) {
11551 descaddr |= extract64(ttbr, 2, 4) << 48;
11552 } else if (descaddr >> outputsize) {
f4ecc015
RH
11553 level = 0;
11554 fault_type = ARMFault_AddressSize;
11555 goto do_fault;
11556 }
11557
41a4bf1f
PM
11558 /*
11559 * We rely on this masking to clear the RES0 bits at the bottom of the TTBR
11560 * and also to mask out CnP (bit 0) which could validly be non-zero.
11561 */
dddb5223 11562 descaddr &= ~indexmask;
3dde962f 11563
f4ecc015
RH
11564 /*
11565 * For AArch32, the address field in the descriptor goes up to bit 39
11566 * for both v7 and v8. However, for v8 the SBZ bits [47:40] must be 0
11567 * or an AddressSize fault is raised. So for v8 we extract those SBZ
11568 * bits as part of the address, which will be checked via outputsize.
ef56c242
RH
11569 * For AArch64, the address field goes up to bit 47, or 49 with FEAT_LPA2;
11570 * the highest bits of a 52-bit output are placed elsewhere.
6109769a 11571 */
ef56c242
RH
11572 if (param.ds) {
11573 descaddrmask = MAKE_64BIT_MASK(0, 50);
11574 } else if (arm_feature(env, ARM_FEATURE_V8)) {
f4ecc015
RH
11575 descaddrmask = MAKE_64BIT_MASK(0, 48);
11576 } else {
11577 descaddrmask = MAKE_64BIT_MASK(0, 40);
11578 }
11579 descaddrmask &= ~indexmask_grainsize;
6109769a 11580
ebca90e4
PM
11581 /* Secure accesses start with the page table in secure memory and
11582 * can be downgraded to non-secure at any step. Non-secure accesses
11583 * remain non-secure. We implement this by just ORing in the NSTable/NS
11584 * bits at each step.
11585 */
11586 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
11587 for (;;) {
11588 uint64_t descriptor;
ebca90e4 11589 bool nstable;
3dde962f 11590
dddb5223 11591 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 11592 descaddr &= ~7ULL;
ebca90e4 11593 nstable = extract32(tableattrs, 4, 1);
3795a6de 11594 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
3b39d734 11595 if (fi->type != ARMFault_None) {
37785977
EI
11596 goto do_fault;
11597 }
11598
3dde962f
PM
11599 if (!(descriptor & 1) ||
11600 (!(descriptor & 2) && (level == 3))) {
11601 /* Invalid, or the Reserved level 3 encoding */
11602 goto do_fault;
11603 }
f4ecc015 11604
6109769a 11605 descaddr = descriptor & descaddrmask;
7a928f43
RH
11606
11607 /*
11608 * For FEAT_LPA and PS=6, bits [51:48] of descaddr are in [15:12]
ef56c242
RH
11609 * of descriptor. For FEAT_LPA2 and effective DS, bits [51:50] of
11610 * descaddr are in [9:8]. Otherwise, if descaddr is out of range,
11611 * raise AddressSizeFault.
7a928f43
RH
11612 */
11613 if (outputsize > 48) {
ef56c242
RH
11614 if (param.ds) {
11615 descaddr |= extract64(descriptor, 8, 2) << 50;
11616 } else {
11617 descaddr |= extract64(descriptor, 12, 4) << 48;
11618 }
7a928f43 11619 } else if (descaddr >> outputsize) {
f4ecc015
RH
11620 fault_type = ARMFault_AddressSize;
11621 goto do_fault;
11622 }
3dde962f
PM
11623
11624 if ((descriptor & 2) && (level < 3)) {
037c13c5 11625 /* Table entry. The top five bits are attributes which may
3dde962f
PM
11626 * propagate down through lower levels of the table (and
11627 * which are all arranged so that 0 means "no effect", so
11628 * we can gather them up by ORing in the bits at each level).
11629 */
11630 tableattrs |= extract64(descriptor, 59, 5);
11631 level++;
dddb5223 11632 indexmask = indexmask_grainsize;
3dde962f
PM
11633 continue;
11634 }
39a1fd25
PM
11635 /*
11636 * Block entry at level 1 or 2, or page entry at level 3.
3dde962f 11637 * These are basically the same thing, although the number
39a1fd25
PM
11638 * of bits we pull in from the vaddr varies. Note that although
11639 * descaddrmask masks enough of the low bits of the descriptor
11640 * to give a correct page or table address, the address field
11641 * in a block descriptor is smaller; so we need to explicitly
11642 * clear the lower bits here before ORing in the low vaddr bits.
3dde962f 11643 */
973a5434 11644 page_size = (1ULL << ((stride * (4 - level)) + 3));
39a1fd25 11645 descaddr &= ~(page_size - 1);
3dde962f 11646 descaddr |= (address & (page_size - 1));
6ab1a5ee 11647 /* Extract attributes from the descriptor */
d615efac
IC
11648 attrs = extract64(descriptor, 2, 10)
11649 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee 11650
b1a10c86 11651 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
6ab1a5ee
EI
11652 /* Stage 2 table descriptors do not include any attribute fields */
11653 break;
11654 }
11655 /* Merge in attributes from table descriptors */
037c13c5 11656 attrs |= nstable << 3; /* NS */
1bafc2ba 11657 guarded = extract64(descriptor, 50, 1); /* GP */
ba97be9f 11658 if (param.hpd) {
037c13c5
RH
11659 /* HPD disables all the table attributes except NSTable. */
11660 break;
11661 }
11662 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
3dde962f
PM
11663 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
11664 * means "force PL1 access only", which means forcing AP[1] to 0.
11665 */
037c13c5
RH
11666 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
11667 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
3dde962f
PM
11668 break;
11669 }
11670 /* Here descaddr is the final physical address, and attributes
11671 * are all in attrs.
11672 */
da909b2c 11673 fault_type = ARMFault_AccessFlag;
3dde962f
PM
11674 if ((attrs & (1 << 8)) == 0) {
11675 /* Access flag */
11676 goto do_fault;
11677 }
d8e052b3
AJ
11678
11679 ap = extract32(attrs, 4, 2);
d8e052b3 11680
b1a10c86
RDC
11681 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11682 ns = mmu_idx == ARMMMUIdx_Stage2;
ce3125be
PM
11683 xn = extract32(attrs, 11, 2);
11684 *prot = get_S2prot(env, ap, xn, s1_is_el0);
6ab1a5ee
EI
11685 } else {
11686 ns = extract32(attrs, 3, 1);
ce3125be 11687 xn = extract32(attrs, 12, 1);
6ab1a5ee 11688 pxn = extract32(attrs, 11, 1);
6e99f762 11689 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 11690 }
d8e052b3 11691
da909b2c 11692 fault_type = ARMFault_Permission;
d8e052b3 11693 if (!(*prot & (1 << access_type))) {
3dde962f
PM
11694 goto do_fault;
11695 }
3dde962f 11696
8bf5b6a9
PM
11697 if (ns) {
11698 /* The NS bit will (as required by the architecture) have no effect if
11699 * the CPU doesn't support TZ or this is a non-secure translation
11700 * regime, because the attribute will already be non-secure.
11701 */
11702 txattrs->secure = false;
11703 }
1bafc2ba
RH
11704 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
11705 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
149d3b31 11706 arm_tlb_bti_gp(txattrs) = true;
1bafc2ba 11707 }
5b2d261d 11708
b1a10c86 11709 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
7e98e21c
RH
11710 cacheattrs->attrs = convert_stage2_attrs(env, extract32(attrs, 0, 4));
11711 } else {
11712 /* Index into MAIR registers for cache attributes */
11713 uint8_t attrindx = extract32(attrs, 0, 3);
11714 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
11715 assert(attrindx <= 7);
11716 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
5b2d261d 11717 }
ef56c242
RH
11718
11719 /*
11720 * For FEAT_LPA2 and effective DS, the SH field in the attributes
11721 * was re-purposed for output address bits. The SH attribute in
11722 * that case comes from TCR_ELx, which we extracted earlier.
11723 */
11724 if (param.ds) {
11725 cacheattrs->shareability = param.sh;
11726 } else {
11727 cacheattrs->shareability = extract32(attrs, 6, 2);
11728 }
5b2d261d 11729
3dde962f
PM
11730 *phys_ptr = descaddr;
11731 *page_size_ptr = page_size;
b7cc4e82 11732 return false;
3dde962f
PM
11733
11734do_fault:
da909b2c
PM
11735 fi->type = fault_type;
11736 fi->level = level;
37785977 11737 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
b1a10c86
RDC
11738 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_Stage2 ||
11739 mmu_idx == ARMMMUIdx_Stage2_S);
9861248f 11740 fi->s1ns = mmu_idx == ARMMMUIdx_Stage2;
b7cc4e82 11741 return true;
3dde962f
PM
11742}
11743
f6bda88f
PC
11744static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
11745 ARMMMUIdx mmu_idx,
11746 int32_t address, int *prot)
11747{
3a00d560
MD
11748 if (!arm_feature(env, ARM_FEATURE_M)) {
11749 *prot = PAGE_READ | PAGE_WRITE;
11750 switch (address) {
11751 case 0xF0000000 ... 0xFFFFFFFF:
11752 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
11753 /* hivecs execing is ok */
11754 *prot |= PAGE_EXEC;
11755 }
11756 break;
11757 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 11758 *prot |= PAGE_EXEC;
3a00d560
MD
11759 break;
11760 }
11761 } else {
11762 /* Default system address map for M profile cores.
11763 * The architecture specifies which regions are execute-never;
11764 * at the MPU level no other checks are defined.
11765 */
11766 switch (address) {
11767 case 0x00000000 ... 0x1fffffff: /* ROM */
11768 case 0x20000000 ... 0x3fffffff: /* SRAM */
11769 case 0x60000000 ... 0x7fffffff: /* RAM */
11770 case 0x80000000 ... 0x9fffffff: /* RAM */
11771 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11772 break;
11773 case 0x40000000 ... 0x5fffffff: /* Peripheral */
11774 case 0xa0000000 ... 0xbfffffff: /* Device */
11775 case 0xc0000000 ... 0xdfffffff: /* Device */
11776 case 0xe0000000 ... 0xffffffff: /* System */
11777 *prot = PAGE_READ | PAGE_WRITE;
11778 break;
11779 default:
11780 g_assert_not_reached();
f6bda88f 11781 }
f6bda88f 11782 }
f6bda88f
PC
11783}
11784
29c483a5
MD
11785static bool pmsav7_use_background_region(ARMCPU *cpu,
11786 ARMMMUIdx mmu_idx, bool is_user)
11787{
11788 /* Return true if we should use the default memory map as a
11789 * "background" region if there are no hits against any MPU regions.
11790 */
11791 CPUARMState *env = &cpu->env;
11792
11793 if (is_user) {
11794 return false;
11795 }
11796
11797 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
11798 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
11799 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
11800 } else {
11801 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
11802 }
11803}
11804
38aaa60c
PM
11805static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
11806{
11807 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
11808 return arm_feature(env, ARM_FEATURE_M) &&
11809 extract32(address, 20, 12) == 0xe00;
11810}
11811
bf446a11
PM
11812static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
11813{
11814 /* True if address is in the M profile system region
11815 * 0xe0000000 - 0xffffffff
11816 */
11817 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
11818}
11819
f6bda88f 11820static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 11821 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9375ad15 11822 hwaddr *phys_ptr, int *prot,
e5e40999 11823 target_ulong *page_size,
9375ad15 11824 ARMMMUFaultInfo *fi)
f6bda88f 11825{
2fc0cc0e 11826 ARMCPU *cpu = env_archcpu(env);
f6bda88f
PC
11827 int n;
11828 bool is_user = regime_is_user(env, mmu_idx);
11829
11830 *phys_ptr = address;
e5e40999 11831 *page_size = TARGET_PAGE_SIZE;
f6bda88f
PC
11832 *prot = 0;
11833
38aaa60c
PM
11834 if (regime_translation_disabled(env, mmu_idx) ||
11835 m_is_ppb_region(env, address)) {
11836 /* MPU disabled or M profile PPB access: use default memory map.
11837 * The other case which uses the default memory map in the
11838 * v7M ARM ARM pseudocode is exception vector reads from the vector
11839 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
11840 * which always does a direct read using address_space_ldl(), rather
11841 * than going via this function, so we don't need to check that here.
11842 */
f6bda88f
PC
11843 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11844 } else { /* MPU enabled */
11845 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11846 /* region search */
11847 uint32_t base = env->pmsav7.drbar[n];
11848 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
11849 uint32_t rmask;
11850 bool srdis = false;
11851
11852 if (!(env->pmsav7.drsr[n] & 0x1)) {
11853 continue;
11854 }
11855
11856 if (!rsize) {
c9f9f124
MD
11857 qemu_log_mask(LOG_GUEST_ERROR,
11858 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
11859 continue;
11860 }
11861 rsize++;
11862 rmask = (1ull << rsize) - 1;
11863
11864 if (base & rmask) {
c9f9f124
MD
11865 qemu_log_mask(LOG_GUEST_ERROR,
11866 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
11867 "to DRSR region size, mask = 0x%" PRIx32 "\n",
11868 n, base, rmask);
f6bda88f
PC
11869 continue;
11870 }
11871
11872 if (address < base || address > base + rmask) {
9d2b5a58
PM
11873 /*
11874 * Address not in this region. We must check whether the
11875 * region covers addresses in the same page as our address.
11876 * In that case we must not report a size that covers the
11877 * whole page for a subsequent hit against a different MPU
11878 * region or the background region, because it would result in
11879 * incorrect TLB hits for subsequent accesses to addresses that
11880 * are in this MPU region.
11881 */
11882 if (ranges_overlap(base, rmask,
11883 address & TARGET_PAGE_MASK,
11884 TARGET_PAGE_SIZE)) {
11885 *page_size = 1;
11886 }
f6bda88f
PC
11887 continue;
11888 }
11889
11890 /* Region matched */
11891
11892 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
11893 int i, snd;
11894 uint32_t srdis_mask;
11895
11896 rsize -= 3; /* sub region size (power of 2) */
11897 snd = ((address - base) >> rsize) & 0x7;
11898 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
11899
11900 srdis_mask = srdis ? 0x3 : 0x0;
11901 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
11902 /* This will check in groups of 2, 4 and then 8, whether
11903 * the subregion bits are consistent. rsize is incremented
11904 * back up to give the region size, considering consistent
11905 * adjacent subregions as one region. Stop testing if rsize
11906 * is already big enough for an entire QEMU page.
11907 */
11908 int snd_rounded = snd & ~(i - 1);
11909 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
11910 snd_rounded + 8, i);
11911 if (srdis_mask ^ srdis_multi) {
11912 break;
11913 }
11914 srdis_mask = (srdis_mask << i) | srdis_mask;
11915 rsize++;
11916 }
11917 }
f6bda88f
PC
11918 if (srdis) {
11919 continue;
11920 }
e5e40999
PM
11921 if (rsize < TARGET_PAGE_BITS) {
11922 *page_size = 1 << rsize;
11923 }
f6bda88f
PC
11924 break;
11925 }
11926
11927 if (n == -1) { /* no hits */
29c483a5 11928 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f 11929 /* background fault */
9375ad15 11930 fi->type = ARMFault_Background;
f6bda88f
PC
11931 return true;
11932 }
11933 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11934 } else { /* a MPU hit! */
11935 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
11936 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
11937
11938 if (m_is_system_region(env, address)) {
11939 /* System space is always execute never */
11940 xn = 1;
11941 }
f6bda88f
PC
11942
11943 if (is_user) { /* User mode AP bit decoding */
11944 switch (ap) {
11945 case 0:
11946 case 1:
11947 case 5:
11948 break; /* no access */
11949 case 3:
11950 *prot |= PAGE_WRITE;
11951 /* fall through */
11952 case 2:
11953 case 6:
11954 *prot |= PAGE_READ | PAGE_EXEC;
11955 break;
8638f1ad
PM
11956 case 7:
11957 /* for v7M, same as 6; for R profile a reserved value */
11958 if (arm_feature(env, ARM_FEATURE_M)) {
11959 *prot |= PAGE_READ | PAGE_EXEC;
11960 break;
11961 }
11962 /* fall through */
f6bda88f
PC
11963 default:
11964 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
11965 "DRACR[%d]: Bad value for AP bits: 0x%"
11966 PRIx32 "\n", n, ap);
f6bda88f
PC
11967 }
11968 } else { /* Priv. mode AP bits decoding */
11969 switch (ap) {
11970 case 0:
11971 break; /* no access */
11972 case 1:
11973 case 2:
11974 case 3:
11975 *prot |= PAGE_WRITE;
11976 /* fall through */
11977 case 5:
11978 case 6:
11979 *prot |= PAGE_READ | PAGE_EXEC;
11980 break;
8638f1ad
PM
11981 case 7:
11982 /* for v7M, same as 6; for R profile a reserved value */
11983 if (arm_feature(env, ARM_FEATURE_M)) {
11984 *prot |= PAGE_READ | PAGE_EXEC;
11985 break;
11986 }
11987 /* fall through */
f6bda88f
PC
11988 default:
11989 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
11990 "DRACR[%d]: Bad value for AP bits: 0x%"
11991 PRIx32 "\n", n, ap);
f6bda88f
PC
11992 }
11993 }
11994
11995 /* execute never */
bf446a11 11996 if (xn) {
f6bda88f
PC
11997 *prot &= ~PAGE_EXEC;
11998 }
11999 }
12000 }
12001
9375ad15
PM
12002 fi->type = ARMFault_Permission;
12003 fi->level = 1;
f6bda88f
PC
12004 return !(*prot & (1 << access_type));
12005}
12006
35337cc3
PM
12007static bool v8m_is_sau_exempt(CPUARMState *env,
12008 uint32_t address, MMUAccessType access_type)
12009{
12010 /* The architecture specifies that certain address ranges are
12011 * exempt from v8M SAU/IDAU checks.
12012 */
12013 return
12014 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
12015 (address >= 0xe0000000 && address <= 0xe0002fff) ||
12016 (address >= 0xe000e000 && address <= 0xe000efff) ||
12017 (address >= 0xe002e000 && address <= 0xe002efff) ||
12018 (address >= 0xe0040000 && address <= 0xe0041fff) ||
12019 (address >= 0xe00ff000 && address <= 0xe00fffff);
12020}
12021
787a7e76 12022void v8m_security_lookup(CPUARMState *env, uint32_t address,
35337cc3
PM
12023 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12024 V8M_SAttributes *sattrs)
12025{
12026 /* Look up the security attributes for this address. Compare the
12027 * pseudocode SecurityCheck() function.
12028 * We assume the caller has zero-initialized *sattrs.
12029 */
2fc0cc0e 12030 ARMCPU *cpu = env_archcpu(env);
35337cc3 12031 int r;
181962fd
PM
12032 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
12033 int idau_region = IREGION_NOTVALID;
72042435
PM
12034 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
12035 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
35337cc3 12036
181962fd
PM
12037 if (cpu->idau) {
12038 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
12039 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
12040
12041 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
12042 &idau_nsc);
12043 }
35337cc3
PM
12044
12045 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
12046 /* 0xf0000000..0xffffffff is always S for insn fetches */
12047 return;
12048 }
12049
181962fd 12050 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
35337cc3
PM
12051 sattrs->ns = !regime_is_secure(env, mmu_idx);
12052 return;
12053 }
12054
181962fd
PM
12055 if (idau_region != IREGION_NOTVALID) {
12056 sattrs->irvalid = true;
12057 sattrs->iregion = idau_region;
12058 }
12059
35337cc3
PM
12060 switch (env->sau.ctrl & 3) {
12061 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
12062 break;
12063 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
12064 sattrs->ns = true;
12065 break;
12066 default: /* SAU.ENABLE == 1 */
12067 for (r = 0; r < cpu->sau_sregion; r++) {
12068 if (env->sau.rlar[r] & 1) {
12069 uint32_t base = env->sau.rbar[r] & ~0x1f;
12070 uint32_t limit = env->sau.rlar[r] | 0x1f;
12071
12072 if (base <= address && limit >= address) {
72042435
PM
12073 if (base > addr_page_base || limit < addr_page_limit) {
12074 sattrs->subpage = true;
12075 }
35337cc3
PM
12076 if (sattrs->srvalid) {
12077 /* If we hit in more than one region then we must report
12078 * as Secure, not NS-Callable, with no valid region
12079 * number info.
12080 */
12081 sattrs->ns = false;
12082 sattrs->nsc = false;
12083 sattrs->sregion = 0;
12084 sattrs->srvalid = false;
12085 break;
12086 } else {
12087 if (env->sau.rlar[r] & 2) {
12088 sattrs->nsc = true;
12089 } else {
12090 sattrs->ns = true;
12091 }
12092 sattrs->srvalid = true;
12093 sattrs->sregion = r;
12094 }
9d2b5a58
PM
12095 } else {
12096 /*
12097 * Address not in this region. We must check whether the
12098 * region covers addresses in the same page as our address.
12099 * In that case we must not report a size that covers the
12100 * whole page for a subsequent hit against a different MPU
12101 * region or the background region, because it would result
12102 * in incorrect TLB hits for subsequent accesses to
12103 * addresses that are in this MPU region.
12104 */
12105 if (limit >= base &&
12106 ranges_overlap(base, limit - base + 1,
12107 addr_page_base,
12108 TARGET_PAGE_SIZE)) {
12109 sattrs->subpage = true;
12110 }
35337cc3
PM
12111 }
12112 }
12113 }
7e3f1223
TR
12114 break;
12115 }
35337cc3 12116
7e3f1223
TR
12117 /*
12118 * The IDAU will override the SAU lookup results if it specifies
12119 * higher security than the SAU does.
12120 */
12121 if (!idau_ns) {
12122 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
12123 sattrs->ns = false;
12124 sattrs->nsc = idau_nsc;
181962fd 12125 }
35337cc3
PM
12126 }
12127}
12128
787a7e76 12129bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
54317c0f
PM
12130 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12131 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
12132 int *prot, bool *is_subpage,
12133 ARMMMUFaultInfo *fi, uint32_t *mregion)
54317c0f
PM
12134{
12135 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
12136 * that a full phys-to-virt translation does).
12137 * mregion is (if not NULL) set to the region number which matched,
12138 * or -1 if no region number is returned (MPU off, address did not
12139 * hit a region, address hit in multiple regions).
72042435
PM
12140 * We set is_subpage to true if the region hit doesn't cover the
12141 * entire TARGET_PAGE the address is within.
54317c0f 12142 */
2fc0cc0e 12143 ARMCPU *cpu = env_archcpu(env);
504e3cc3 12144 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 12145 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
12146 int n;
12147 int matchregion = -1;
12148 bool hit = false;
72042435
PM
12149 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
12150 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
504e3cc3 12151
72042435 12152 *is_subpage = false;
504e3cc3
PM
12153 *phys_ptr = address;
12154 *prot = 0;
54317c0f
PM
12155 if (mregion) {
12156 *mregion = -1;
35337cc3
PM
12157 }
12158
504e3cc3
PM
12159 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
12160 * was an exception vector read from the vector table (which is always
12161 * done using the default system address map), because those accesses
12162 * are done in arm_v7m_load_vector(), which always does a direct
12163 * read using address_space_ldl(), rather than going via this function.
12164 */
12165 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
12166 hit = true;
12167 } else if (m_is_ppb_region(env, address)) {
12168 hit = true;
504e3cc3 12169 } else {
cff21316
PM
12170 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
12171 hit = true;
12172 }
12173
504e3cc3
PM
12174 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
12175 /* region search */
12176 /* Note that the base address is bits [31:5] from the register
12177 * with bits [4:0] all zeroes, but the limit address is bits
12178 * [31:5] from the register with bits [4:0] all ones.
12179 */
62c58ee0
PM
12180 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
12181 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 12182
62c58ee0 12183 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
12184 /* Region disabled */
12185 continue;
12186 }
12187
12188 if (address < base || address > limit) {
9d2b5a58
PM
12189 /*
12190 * Address not in this region. We must check whether the
12191 * region covers addresses in the same page as our address.
12192 * In that case we must not report a size that covers the
12193 * whole page for a subsequent hit against a different MPU
12194 * region or the background region, because it would result in
12195 * incorrect TLB hits for subsequent accesses to addresses that
12196 * are in this MPU region.
12197 */
12198 if (limit >= base &&
12199 ranges_overlap(base, limit - base + 1,
12200 addr_page_base,
12201 TARGET_PAGE_SIZE)) {
12202 *is_subpage = true;
12203 }
504e3cc3
PM
12204 continue;
12205 }
12206
72042435
PM
12207 if (base > addr_page_base || limit < addr_page_limit) {
12208 *is_subpage = true;
12209 }
12210
cff21316 12211 if (matchregion != -1) {
504e3cc3
PM
12212 /* Multiple regions match -- always a failure (unlike
12213 * PMSAv7 where highest-numbered-region wins)
12214 */
3f551b5b
PM
12215 fi->type = ARMFault_Permission;
12216 fi->level = 1;
504e3cc3
PM
12217 return true;
12218 }
12219
12220 matchregion = n;
12221 hit = true;
504e3cc3
PM
12222 }
12223 }
12224
12225 if (!hit) {
12226 /* background fault */
3f551b5b 12227 fi->type = ARMFault_Background;
504e3cc3
PM
12228 return true;
12229 }
12230
12231 if (matchregion == -1) {
12232 /* hit using the background region */
12233 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
12234 } else {
62c58ee0
PM
12235 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
12236 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
cad8e2e3
PM
12237 bool pxn = false;
12238
12239 if (arm_feature(env, ARM_FEATURE_V8_1M)) {
12240 pxn = extract32(env->pmsav8.rlar[secure][matchregion], 4, 1);
12241 }
504e3cc3
PM
12242
12243 if (m_is_system_region(env, address)) {
12244 /* System space is always execute never */
12245 xn = 1;
12246 }
12247
12248 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
cad8e2e3 12249 if (*prot && !xn && !(pxn && !is_user)) {
504e3cc3
PM
12250 *prot |= PAGE_EXEC;
12251 }
12252 /* We don't need to look the attribute up in the MAIR0/MAIR1
12253 * registers because that only tells us about cacheability.
12254 */
54317c0f
PM
12255 if (mregion) {
12256 *mregion = matchregion;
12257 }
504e3cc3
PM
12258 }
12259
3f551b5b
PM
12260 fi->type = ARMFault_Permission;
12261 fi->level = 1;
504e3cc3
PM
12262 return !(*prot & (1 << access_type));
12263}
12264
54317c0f
PM
12265
12266static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
12267 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12268 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
12269 int *prot, target_ulong *page_size,
12270 ARMMMUFaultInfo *fi)
54317c0f
PM
12271{
12272 uint32_t secure = regime_is_secure(env, mmu_idx);
12273 V8M_SAttributes sattrs = {};
72042435
PM
12274 bool ret;
12275 bool mpu_is_subpage;
54317c0f
PM
12276
12277 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
12278 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
12279 if (access_type == MMU_INST_FETCH) {
12280 /* Instruction fetches always use the MMU bank and the
12281 * transaction attribute determined by the fetch address,
12282 * regardless of CPU state. This is painful for QEMU
12283 * to handle, because it would mean we need to encode
12284 * into the mmu_idx not just the (user, negpri) information
12285 * for the current security state but also that for the
12286 * other security state, which would balloon the number
12287 * of mmu_idx values needed alarmingly.
12288 * Fortunately we can avoid this because it's not actually
12289 * possible to arbitrarily execute code from memory with
12290 * the wrong security attribute: it will always generate
12291 * an exception of some kind or another, apart from the
12292 * special case of an NS CPU executing an SG instruction
12293 * in S&NSC memory. So we always just fail the translation
12294 * here and sort things out in the exception handler
12295 * (including possibly emulating an SG instruction).
12296 */
12297 if (sattrs.ns != !secure) {
3f551b5b
PM
12298 if (sattrs.nsc) {
12299 fi->type = ARMFault_QEMU_NSCExec;
12300 } else {
12301 fi->type = ARMFault_QEMU_SFault;
12302 }
72042435 12303 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
12304 *phys_ptr = address;
12305 *prot = 0;
12306 return true;
12307 }
12308 } else {
12309 /* For data accesses we always use the MMU bank indicated
12310 * by the current CPU state, but the security attributes
12311 * might downgrade a secure access to nonsecure.
12312 */
12313 if (sattrs.ns) {
12314 txattrs->secure = false;
12315 } else if (!secure) {
12316 /* NS access to S memory must fault.
12317 * Architecturally we should first check whether the
12318 * MPU information for this address indicates that we
12319 * are doing an unaligned access to Device memory, which
12320 * should generate a UsageFault instead. QEMU does not
12321 * currently check for that kind of unaligned access though.
12322 * If we added it we would need to do so as a special case
12323 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
12324 */
3f551b5b 12325 fi->type = ARMFault_QEMU_SFault;
72042435 12326 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
12327 *phys_ptr = address;
12328 *prot = 0;
12329 return true;
12330 }
12331 }
12332 }
12333
72042435
PM
12334 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
12335 txattrs, prot, &mpu_is_subpage, fi, NULL);
72042435
PM
12336 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
12337 return ret;
54317c0f
PM
12338}
12339
13689d43 12340static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 12341 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53a4e5c5
PM
12342 hwaddr *phys_ptr, int *prot,
12343 ARMMMUFaultInfo *fi)
9ee6e8bb
PB
12344{
12345 int n;
12346 uint32_t mask;
12347 uint32_t base;
0480f69a 12348 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 12349
3279adb9
PM
12350 if (regime_translation_disabled(env, mmu_idx)) {
12351 /* MPU disabled. */
12352 *phys_ptr = address;
12353 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
12354 return false;
12355 }
12356
9ee6e8bb
PB
12357 *phys_ptr = address;
12358 for (n = 7; n >= 0; n--) {
554b0b09 12359 base = env->cp15.c6_region[n];
87c3d486 12360 if ((base & 1) == 0) {
554b0b09 12361 continue;
87c3d486 12362 }
554b0b09
PM
12363 mask = 1 << ((base >> 1) & 0x1f);
12364 /* Keep this shift separate from the above to avoid an
12365 (undefined) << 32. */
12366 mask = (mask << 1) - 1;
87c3d486 12367 if (((base ^ address) & ~mask) == 0) {
554b0b09 12368 break;
87c3d486 12369 }
9ee6e8bb 12370 }
87c3d486 12371 if (n < 0) {
53a4e5c5 12372 fi->type = ARMFault_Background;
b7cc4e82 12373 return true;
87c3d486 12374 }
9ee6e8bb 12375
03ae85f8 12376 if (access_type == MMU_INST_FETCH) {
7e09797c 12377 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 12378 } else {
7e09797c 12379 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
12380 }
12381 mask = (mask >> (n * 4)) & 0xf;
12382 switch (mask) {
12383 case 0:
53a4e5c5
PM
12384 fi->type = ARMFault_Permission;
12385 fi->level = 1;
b7cc4e82 12386 return true;
9ee6e8bb 12387 case 1:
87c3d486 12388 if (is_user) {
53a4e5c5
PM
12389 fi->type = ARMFault_Permission;
12390 fi->level = 1;
b7cc4e82 12391 return true;
87c3d486 12392 }
554b0b09
PM
12393 *prot = PAGE_READ | PAGE_WRITE;
12394 break;
9ee6e8bb 12395 case 2:
554b0b09 12396 *prot = PAGE_READ;
87c3d486 12397 if (!is_user) {
554b0b09 12398 *prot |= PAGE_WRITE;
87c3d486 12399 }
554b0b09 12400 break;
9ee6e8bb 12401 case 3:
554b0b09
PM
12402 *prot = PAGE_READ | PAGE_WRITE;
12403 break;
9ee6e8bb 12404 case 5:
87c3d486 12405 if (is_user) {
53a4e5c5
PM
12406 fi->type = ARMFault_Permission;
12407 fi->level = 1;
b7cc4e82 12408 return true;
87c3d486 12409 }
554b0b09
PM
12410 *prot = PAGE_READ;
12411 break;
9ee6e8bb 12412 case 6:
554b0b09
PM
12413 *prot = PAGE_READ;
12414 break;
9ee6e8bb 12415 default:
554b0b09 12416 /* Bad permission. */
53a4e5c5
PM
12417 fi->type = ARMFault_Permission;
12418 fi->level = 1;
b7cc4e82 12419 return true;
9ee6e8bb 12420 }
3ad493fc 12421 *prot |= PAGE_EXEC;
b7cc4e82 12422 return false;
9ee6e8bb
PB
12423}
12424
5b2d261d
AB
12425/* Combine either inner or outer cacheability attributes for normal
12426 * memory, according to table D4-42 and pseudocode procedure
12427 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
12428 *
12429 * NB: only stage 1 includes allocation hints (RW bits), leading to
12430 * some asymmetry.
12431 */
12432static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
12433{
12434 if (s1 == 4 || s2 == 4) {
12435 /* non-cacheable has precedence */
12436 return 4;
12437 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
12438 /* stage 1 write-through takes precedence */
12439 return s1;
12440 } else if (extract32(s2, 2, 2) == 2) {
12441 /* stage 2 write-through takes precedence, but the allocation hint
12442 * is still taken from stage 1
12443 */
12444 return (2 << 2) | extract32(s1, 0, 2);
12445 } else { /* write-back */
12446 return s1;
12447 }
12448}
12449
12450/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
12451 * and CombineS1S2Desc()
12452 *
12453 * @s1: Attributes from stage 1 walk
12454 * @s2: Attributes from stage 2 walk
12455 */
12456static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
12457{
337a03f0 12458 uint8_t s1lo, s2lo, s1hi, s2hi;
5b2d261d 12459 ARMCacheAttrs ret;
337a03f0
RH
12460 bool tagged = false;
12461
12462 if (s1.attrs == 0xf0) {
12463 tagged = true;
12464 s1.attrs = 0xff;
12465 }
12466
12467 s1lo = extract32(s1.attrs, 0, 4);
12468 s2lo = extract32(s2.attrs, 0, 4);
12469 s1hi = extract32(s1.attrs, 4, 4);
12470 s2hi = extract32(s2.attrs, 4, 4);
5b2d261d
AB
12471
12472 /* Combine shareability attributes (table D4-43) */
12473 if (s1.shareability == 2 || s2.shareability == 2) {
12474 /* if either are outer-shareable, the result is outer-shareable */
12475 ret.shareability = 2;
12476 } else if (s1.shareability == 3 || s2.shareability == 3) {
12477 /* if either are inner-shareable, the result is inner-shareable */
12478 ret.shareability = 3;
12479 } else {
12480 /* both non-shareable */
12481 ret.shareability = 0;
12482 }
12483
12484 /* Combine memory type and cacheability attributes */
12485 if (s1hi == 0 || s2hi == 0) {
12486 /* Device has precedence over normal */
12487 if (s1lo == 0 || s2lo == 0) {
12488 /* nGnRnE has precedence over anything */
12489 ret.attrs = 0;
12490 } else if (s1lo == 4 || s2lo == 4) {
12491 /* non-Reordering has precedence over Reordering */
12492 ret.attrs = 4; /* nGnRE */
12493 } else if (s1lo == 8 || s2lo == 8) {
12494 /* non-Gathering has precedence over Gathering */
12495 ret.attrs = 8; /* nGRE */
12496 } else {
12497 ret.attrs = 0xc; /* GRE */
12498 }
12499
12500 /* Any location for which the resultant memory type is any
12501 * type of Device memory is always treated as Outer Shareable.
12502 */
12503 ret.shareability = 2;
12504 } else { /* Normal memory */
12505 /* Outer/inner cacheability combine independently */
12506 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
12507 | combine_cacheattr_nibble(s1lo, s2lo);
12508
12509 if (ret.attrs == 0x44) {
12510 /* Any location for which the resultant memory type is Normal
12511 * Inner Non-cacheable, Outer Non-cacheable is always treated
12512 * as Outer Shareable.
12513 */
12514 ret.shareability = 2;
12515 }
12516 }
12517
337a03f0
RH
12518 /* TODO: CombineS1S2Desc does not consider transient, only WB, RWA. */
12519 if (tagged && ret.attrs == 0xff) {
12520 ret.attrs = 0xf0;
12521 }
12522
5b2d261d
AB
12523 return ret;
12524}
12525
12526
702a9357
PM
12527/* get_phys_addr - get the physical address for this virtual address
12528 *
12529 * Find the physical address corresponding to the given virtual address,
12530 * by doing a translation table walk on MMU based systems or using the
12531 * MPU state on MPU based systems.
12532 *
b7cc4e82
PC
12533 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
12534 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
12535 * information on why the translation aborted, in the format of a
12536 * DFSR/IFSR fault register, with the following caveats:
12537 * * we honour the short vs long DFSR format differences.
12538 * * the WnR bit is never set (the caller must do this).
f6bda88f 12539 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
12540 * value.
12541 *
12542 * @env: CPUARMState
12543 * @address: virtual address to get physical address for
12544 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 12545 * @mmu_idx: MMU index indicating required translation regime
702a9357 12546 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 12547 * @attrs: set to the memory transaction attributes to use
702a9357
PM
12548 * @prot: set to the permissions for the page containing phys_ptr
12549 * @page_size: set to the size of the page containing phys_ptr
5b2d261d
AB
12550 * @fi: set to fault info if the translation fails
12551 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
702a9357 12552 */
ebae861f
PMD
12553bool get_phys_addr(CPUARMState *env, target_ulong address,
12554 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12555 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
12556 target_ulong *page_size,
12557 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9ee6e8bb 12558{
7879460a
RDC
12559 ARMMMUIdx s1_mmu_idx = stage_1_mmu_idx(mmu_idx);
12560
12561 if (mmu_idx != s1_mmu_idx) {
9b539263 12562 /* Call ourselves recursively to do the stage 1 and then stage 2
7879460a 12563 * translations if mmu_idx is a two-stage regime.
0480f69a 12564 */
9b539263
EI
12565 if (arm_feature(env, ARM_FEATURE_EL2)) {
12566 hwaddr ipa;
12567 int s2_prot;
12568 int ret;
6c05a866 12569 bool ipa_secure;
5b2d261d 12570 ARMCacheAttrs cacheattrs2 = {};
b1a10c86
RDC
12571 ARMMMUIdx s2_mmu_idx;
12572 bool is_el0;
9b539263 12573
7879460a
RDC
12574 ret = get_phys_addr(env, address, access_type, s1_mmu_idx, &ipa,
12575 attrs, prot, page_size, fi, cacheattrs);
9b539263
EI
12576
12577 /* If S1 fails or S2 is disabled, return early. */
97fa9350 12578 if (ret || regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
9b539263
EI
12579 *phys_ptr = ipa;
12580 return ret;
12581 }
12582
6c05a866 12583 ipa_secure = attrs->secure;
bcd7a8cf 12584 if (arm_is_secure_below_el3(env)) {
6c05a866 12585 if (ipa_secure) {
bcd7a8cf
IH
12586 attrs->secure = !(env->cp15.vstcr_el2.raw_tcr & VSTCR_SW);
12587 } else {
12588 attrs->secure = !(env->cp15.vtcr_el2.raw_tcr & VTCR_NSW);
12589 }
12590 } else {
6c05a866 12591 assert(!ipa_secure);
bcd7a8cf
IH
12592 }
12593
b1a10c86
RDC
12594 s2_mmu_idx = attrs->secure ? ARMMMUIdx_Stage2_S : ARMMMUIdx_Stage2;
12595 is_el0 = mmu_idx == ARMMMUIdx_E10_0 || mmu_idx == ARMMMUIdx_SE10_0;
12596
9b539263 12597 /* S1 is done. Now do S2 translation. */
b1a10c86 12598 ret = get_phys_addr_lpae(env, ipa, access_type, s2_mmu_idx, is_el0,
9b539263 12599 phys_ptr, attrs, &s2_prot,
7e98e21c 12600 page_size, fi, &cacheattrs2);
9b539263
EI
12601 fi->s2addr = ipa;
12602 /* Combine the S1 and S2 perms. */
12603 *prot &= s2_prot;
5b2d261d 12604
7e98e21c
RH
12605 /* If S2 fails, return early. */
12606 if (ret) {
12607 return ret;
5b2d261d
AB
12608 }
12609
7e98e21c 12610 /* Combine the S1 and S2 cache attributes. */
e04a5752 12611 if (arm_hcr_el2_eff(env) & HCR_DC) {
7e98e21c
RH
12612 /*
12613 * HCR.DC forces the first stage attributes to
12614 * Normal Non-Shareable,
12615 * Inner Write-Back Read-Allocate Write-Allocate,
12616 * Outer Write-Back Read-Allocate Write-Allocate.
337a03f0 12617 * Do not overwrite Tagged within attrs.
7e98e21c 12618 */
337a03f0
RH
12619 if (cacheattrs->attrs != 0xf0) {
12620 cacheattrs->attrs = 0xff;
12621 }
7e98e21c
RH
12622 cacheattrs->shareability = 0;
12623 }
12624 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
b1a10c86
RDC
12625
12626 /* Check if IPA translates to secure or non-secure PA space. */
12627 if (arm_is_secure_below_el3(env)) {
6c05a866 12628 if (ipa_secure) {
b1a10c86
RDC
12629 attrs->secure =
12630 !(env->cp15.vstcr_el2.raw_tcr & (VSTCR_SA | VSTCR_SW));
12631 } else {
12632 attrs->secure =
12633 !((env->cp15.vtcr_el2.raw_tcr & (VTCR_NSA | VTCR_NSW))
d3b2d191 12634 || (env->cp15.vstcr_el2.raw_tcr & (VSTCR_SA | VSTCR_SW)));
b1a10c86
RDC
12635 }
12636 }
7e98e21c 12637 return 0;
9b539263
EI
12638 } else {
12639 /*
12640 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
12641 */
8bd5c820 12642 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 12643 }
0480f69a 12644 }
d3649702 12645
8bf5b6a9
PM
12646 /* The page table entries may downgrade secure to non-secure, but
12647 * cannot upgrade an non-secure translation regime's attributes
12648 * to secure.
12649 */
12650 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 12651 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 12652
0480f69a
PM
12653 /* Fast Context Switch Extension. This doesn't exist at all in v8.
12654 * In v7 and earlier it affects all stage 1 translations.
12655 */
97fa9350 12656 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2
0480f69a
PM
12657 && !arm_feature(env, ARM_FEATURE_V8)) {
12658 if (regime_el(env, mmu_idx) == 3) {
12659 address += env->cp15.fcseidr_s;
12660 } else {
12661 address += env->cp15.fcseidr_ns;
12662 }
54bf36ed 12663 }
9ee6e8bb 12664
3279adb9 12665 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 12666 bool ret;
f6bda88f 12667 *page_size = TARGET_PAGE_SIZE;
3279adb9 12668
504e3cc3
PM
12669 if (arm_feature(env, ARM_FEATURE_V8)) {
12670 /* PMSAv8 */
12671 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
72042435 12672 phys_ptr, attrs, prot, page_size, fi);
504e3cc3 12673 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
12674 /* PMSAv7 */
12675 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
e5e40999 12676 phys_ptr, prot, page_size, fi);
3279adb9
PM
12677 } else {
12678 /* Pre-v7 MPU */
12679 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
53a4e5c5 12680 phys_ptr, prot, fi);
3279adb9
PM
12681 }
12682 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 12683 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
12684 access_type == MMU_DATA_LOAD ? "reading" :
12685 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
12686 (uint32_t)address, mmu_idx,
12687 ret ? "Miss" : "Hit",
12688 *prot & PAGE_READ ? 'r' : '-',
12689 *prot & PAGE_WRITE ? 'w' : '-',
12690 *prot & PAGE_EXEC ? 'x' : '-');
12691
12692 return ret;
f6bda88f
PC
12693 }
12694
3279adb9
PM
12695 /* Definitely a real MMU, not an MPU */
12696
0480f69a 12697 if (regime_translation_disabled(env, mmu_idx)) {
337a03f0
RH
12698 uint64_t hcr;
12699 uint8_t memattr;
12700
cebfb648
RH
12701 /*
12702 * MMU disabled. S1 addresses within aa64 translation regimes are
12703 * still checked for bounds -- see AArch64.TranslateAddressS1Off.
12704 */
b1a10c86 12705 if (mmu_idx != ARMMMUIdx_Stage2 && mmu_idx != ARMMMUIdx_Stage2_S) {
cebfb648
RH
12706 int r_el = regime_el(env, mmu_idx);
12707 if (arm_el_is_aa64(env, r_el)) {
12708 int pamax = arm_pamax(env_archcpu(env));
12709 uint64_t tcr = env->cp15.tcr_el[r_el].raw_tcr;
12710 int addrtop, tbi;
12711
12712 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
12713 if (access_type == MMU_INST_FETCH) {
12714 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
12715 }
12716 tbi = (tbi >> extract64(address, 55, 1)) & 1;
12717 addrtop = (tbi ? 55 : 63);
12718
12719 if (extract64(address, pamax, addrtop - pamax + 1) != 0) {
12720 fi->type = ARMFault_AddressSize;
12721 fi->level = 0;
12722 fi->stage2 = false;
12723 return 1;
12724 }
12725
12726 /*
12727 * When TBI is disabled, we've just validated that all of the
12728 * bits above PAMax are zero, so logically we only need to
12729 * clear the top byte for TBI. But it's clearer to follow
12730 * the pseudocode set of addrdesc.paddress.
12731 */
12732 address = extract64(address, 0, 52);
12733 }
12734 }
9ee6e8bb 12735 *phys_ptr = address;
3ad493fc 12736 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 12737 *page_size = TARGET_PAGE_SIZE;
337a03f0
RH
12738
12739 /* Fill in cacheattr a-la AArch64.TranslateAddressS1Off. */
12740 hcr = arm_hcr_el2_eff(env);
12741 cacheattrs->shareability = 0;
12742 if (hcr & HCR_DC) {
12743 if (hcr & HCR_DCT) {
12744 memattr = 0xf0; /* Tagged, Normal, WB, RWA */
12745 } else {
12746 memattr = 0xff; /* Normal, WB, RWA */
12747 }
12748 } else if (access_type == MMU_INST_FETCH) {
12749 if (regime_sctlr(env, mmu_idx) & SCTLR_I) {
12750 memattr = 0xee; /* Normal, WT, RA, NT */
12751 } else {
12752 memattr = 0x44; /* Normal, NC, No */
12753 }
12754 cacheattrs->shareability = 2; /* outer sharable */
12755 } else {
12756 memattr = 0x00; /* Device, nGnRnE */
12757 }
12758 cacheattrs->attrs = memattr;
9ee6e8bb 12759 return 0;
0480f69a
PM
12760 }
12761
0480f69a 12762 if (regime_using_lpae_format(env, mmu_idx)) {
ff7de2fc 12763 return get_phys_addr_lpae(env, address, access_type, mmu_idx, false,
bc52bfeb
PM
12764 phys_ptr, attrs, prot, page_size,
12765 fi, cacheattrs);
0480f69a 12766 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
bc52bfeb
PM
12767 return get_phys_addr_v6(env, address, access_type, mmu_idx,
12768 phys_ptr, attrs, prot, page_size, fi);
9ee6e8bb 12769 } else {
bc52bfeb 12770 return get_phys_addr_v5(env, address, access_type, mmu_idx,
f989983e 12771 phys_ptr, prot, page_size, fi);
9ee6e8bb
PB
12772 }
12773}
12774
0faea0c7
PM
12775hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
12776 MemTxAttrs *attrs)
b5ff1b31 12777{
00b941e5 12778 ARMCPU *cpu = ARM_CPU(cs);
d3649702 12779 CPUARMState *env = &cpu->env;
a8170e5e 12780 hwaddr phys_addr;
d4c430a8 12781 target_ulong page_size;
b5ff1b31 12782 int prot;
b7cc4e82 12783 bool ret;
e14b5a23 12784 ARMMMUFaultInfo fi = {};
50494a27 12785 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
7e98e21c 12786 ARMCacheAttrs cacheattrs = {};
b5ff1b31 12787
0faea0c7
PM
12788 *attrs = (MemTxAttrs) {};
12789
a9dd161f 12790 ret = get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &phys_addr,
7e98e21c 12791 attrs, &prot, &page_size, &fi, &cacheattrs);
b5ff1b31 12792
b7cc4e82 12793 if (ret) {
b5ff1b31 12794 return -1;
00b941e5 12795 }
b5ff1b31
FB
12796 return phys_addr;
12797}
12798
b5ff1b31 12799#endif
6ddbc6e4
PB
12800
12801/* Note that signed overflow is undefined in C. The following routines are
12802 careful to use unsigned types where modulo arithmetic is required.
12803 Failure to do so _will_ break on newer gcc. */
12804
12805/* Signed saturating arithmetic. */
12806
1654b2d6 12807/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
12808static inline uint16_t add16_sat(uint16_t a, uint16_t b)
12809{
12810 uint16_t res;
12811
12812 res = a + b;
12813 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
12814 if (a & 0x8000)
12815 res = 0x8000;
12816 else
12817 res = 0x7fff;
12818 }
12819 return res;
12820}
12821
1654b2d6 12822/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
12823static inline uint8_t add8_sat(uint8_t a, uint8_t b)
12824{
12825 uint8_t res;
12826
12827 res = a + b;
12828 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
12829 if (a & 0x80)
12830 res = 0x80;
12831 else
12832 res = 0x7f;
12833 }
12834 return res;
12835}
12836
1654b2d6 12837/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
12838static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
12839{
12840 uint16_t res;
12841
12842 res = a - b;
12843 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
12844 if (a & 0x8000)
12845 res = 0x8000;
12846 else
12847 res = 0x7fff;
12848 }
12849 return res;
12850}
12851
1654b2d6 12852/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
12853static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
12854{
12855 uint8_t res;
12856
12857 res = a - b;
12858 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
12859 if (a & 0x80)
12860 res = 0x80;
12861 else
12862 res = 0x7f;
12863 }
12864 return res;
12865}
12866
12867#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
12868#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
12869#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
12870#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
12871#define PFX q
12872
12873#include "op_addsub.h"
12874
12875/* Unsigned saturating arithmetic. */
460a09c1 12876static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
12877{
12878 uint16_t res;
12879 res = a + b;
12880 if (res < a)
12881 res = 0xffff;
12882 return res;
12883}
12884
460a09c1 12885static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 12886{
4c4fd3f8 12887 if (a > b)
6ddbc6e4
PB
12888 return a - b;
12889 else
12890 return 0;
12891}
12892
12893static inline uint8_t add8_usat(uint8_t a, uint8_t b)
12894{
12895 uint8_t res;
12896 res = a + b;
12897 if (res < a)
12898 res = 0xff;
12899 return res;
12900}
12901
12902static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
12903{
4c4fd3f8 12904 if (a > b)
6ddbc6e4
PB
12905 return a - b;
12906 else
12907 return 0;
12908}
12909
12910#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
12911#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
12912#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
12913#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
12914#define PFX uq
12915
12916#include "op_addsub.h"
12917
12918/* Signed modulo arithmetic. */
12919#define SARITH16(a, b, n, op) do { \
12920 int32_t sum; \
db6e2e65 12921 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
12922 RESULT(sum, n, 16); \
12923 if (sum >= 0) \
12924 ge |= 3 << (n * 2); \
12925 } while(0)
12926
12927#define SARITH8(a, b, n, op) do { \
12928 int32_t sum; \
db6e2e65 12929 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
12930 RESULT(sum, n, 8); \
12931 if (sum >= 0) \
12932 ge |= 1 << n; \
12933 } while(0)
12934
12935
12936#define ADD16(a, b, n) SARITH16(a, b, n, +)
12937#define SUB16(a, b, n) SARITH16(a, b, n, -)
12938#define ADD8(a, b, n) SARITH8(a, b, n, +)
12939#define SUB8(a, b, n) SARITH8(a, b, n, -)
12940#define PFX s
12941#define ARITH_GE
12942
12943#include "op_addsub.h"
12944
12945/* Unsigned modulo arithmetic. */
12946#define ADD16(a, b, n) do { \
12947 uint32_t sum; \
12948 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
12949 RESULT(sum, n, 16); \
a87aa10b 12950 if ((sum >> 16) == 1) \
6ddbc6e4
PB
12951 ge |= 3 << (n * 2); \
12952 } while(0)
12953
12954#define ADD8(a, b, n) do { \
12955 uint32_t sum; \
12956 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
12957 RESULT(sum, n, 8); \
a87aa10b
AZ
12958 if ((sum >> 8) == 1) \
12959 ge |= 1 << n; \
6ddbc6e4
PB
12960 } while(0)
12961
12962#define SUB16(a, b, n) do { \
12963 uint32_t sum; \
12964 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
12965 RESULT(sum, n, 16); \
12966 if ((sum >> 16) == 0) \
12967 ge |= 3 << (n * 2); \
12968 } while(0)
12969
12970#define SUB8(a, b, n) do { \
12971 uint32_t sum; \
12972 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
12973 RESULT(sum, n, 8); \
12974 if ((sum >> 8) == 0) \
a87aa10b 12975 ge |= 1 << n; \
6ddbc6e4
PB
12976 } while(0)
12977
12978#define PFX u
12979#define ARITH_GE
12980
12981#include "op_addsub.h"
12982
12983/* Halved signed arithmetic. */
12984#define ADD16(a, b, n) \
12985 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
12986#define SUB16(a, b, n) \
12987 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
12988#define ADD8(a, b, n) \
12989 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
12990#define SUB8(a, b, n) \
12991 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
12992#define PFX sh
12993
12994#include "op_addsub.h"
12995
12996/* Halved unsigned arithmetic. */
12997#define ADD16(a, b, n) \
12998 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12999#define SUB16(a, b, n) \
13000 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
13001#define ADD8(a, b, n) \
13002 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
13003#define SUB8(a, b, n) \
13004 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
13005#define PFX uh
13006
13007#include "op_addsub.h"
13008
13009static inline uint8_t do_usad(uint8_t a, uint8_t b)
13010{
13011 if (a > b)
13012 return a - b;
13013 else
13014 return b - a;
13015}
13016
13017/* Unsigned sum of absolute byte differences. */
13018uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
13019{
13020 uint32_t sum;
13021 sum = do_usad(a, b);
13022 sum += do_usad(a >> 8, b >> 8);
bdc3b6f5 13023 sum += do_usad(a >> 16, b >> 16);
6ddbc6e4
PB
13024 sum += do_usad(a >> 24, b >> 24);
13025 return sum;
13026}
13027
13028/* For ARMv6 SEL instruction. */
13029uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
13030{
13031 uint32_t mask;
13032
13033 mask = 0;
13034 if (flags & 1)
13035 mask |= 0xff;
13036 if (flags & 2)
13037 mask |= 0xff00;
13038 if (flags & 4)
13039 mask |= 0xff0000;
13040 if (flags & 8)
13041 mask |= 0xff000000;
13042 return (a & mask) | (b & ~mask);
13043}
13044
aa633469
PM
13045/* CRC helpers.
13046 * The upper bytes of val (above the number specified by 'bytes') must have
13047 * been zeroed out by the caller.
13048 */
eb0ecd5a
WN
13049uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
13050{
13051 uint8_t buf[4];
13052
aa633469 13053 stl_le_p(buf, val);
eb0ecd5a
WN
13054
13055 /* zlib crc32 converts the accumulator and output to one's complement. */
13056 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
13057}
13058
13059uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
13060{
13061 uint8_t buf[4];
13062
aa633469 13063 stl_le_p(buf, val);
eb0ecd5a
WN
13064
13065 /* Linux crc32c converts the output to one's complement. */
13066 return crc32c(acc, buf, bytes) ^ 0xffffffff;
13067}
a9e01311
RH
13068
13069/* Return the exception level to which FP-disabled exceptions should
13070 * be taken, or 0 if FP is enabled.
13071 */
ced31551 13072int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 13073{
55faa212 13074#ifndef CONFIG_USER_ONLY
d5a6fa2d
RH
13075 uint64_t hcr_el2;
13076
a9e01311
RH
13077 /* CPACR and the CPTR registers don't exist before v6, so FP is
13078 * always accessible
13079 */
13080 if (!arm_feature(env, ARM_FEATURE_V6)) {
13081 return 0;
13082 }
13083
d87513c0
PM
13084 if (arm_feature(env, ARM_FEATURE_M)) {
13085 /* CPACR can cause a NOCP UsageFault taken to current security state */
13086 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
13087 return 1;
13088 }
13089
13090 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
13091 if (!extract32(env->v7m.nsacr, 10, 1)) {
13092 /* FP insns cause a NOCP UsageFault taken to Secure */
13093 return 3;
13094 }
13095 }
13096
13097 return 0;
13098 }
13099
d5a6fa2d
RH
13100 hcr_el2 = arm_hcr_el2_eff(env);
13101
a9e01311
RH
13102 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
13103 * 0, 2 : trap EL0 and EL1/PL1 accesses
13104 * 1 : trap only EL0 accesses
13105 * 3 : trap no accesses
c2ddb7cf 13106 * This register is ignored if E2H+TGE are both set.
a9e01311 13107 */
d5a6fa2d 13108 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
c2ddb7cf
RH
13109 int fpen = extract32(env->cp15.cpacr_el1, 20, 2);
13110
13111 switch (fpen) {
13112 case 0:
13113 case 2:
13114 if (cur_el == 0 || cur_el == 1) {
13115 /* Trap to PL1, which might be EL1 or EL3 */
13116 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
13117 return 3;
13118 }
13119 return 1;
13120 }
13121 if (cur_el == 3 && !is_a64(env)) {
13122 /* Secure PL1 running at EL3 */
a9e01311
RH
13123 return 3;
13124 }
c2ddb7cf
RH
13125 break;
13126 case 1:
13127 if (cur_el == 0) {
13128 return 1;
13129 }
13130 break;
13131 case 3:
13132 break;
a9e01311 13133 }
a9e01311
RH
13134 }
13135
fc1120a7
PM
13136 /*
13137 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
13138 * to control non-secure access to the FPU. It doesn't have any
13139 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
13140 */
13141 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
13142 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
13143 if (!extract32(env->cp15.nsacr, 10, 1)) {
13144 /* FP insns act as UNDEF */
13145 return cur_el == 2 ? 2 : 1;
13146 }
13147 }
13148
d5a6fa2d
RH
13149 /*
13150 * CPTR_EL2 is present in v7VE or v8, and changes format
13151 * with HCR_EL2.E2H (regardless of TGE).
a9e01311 13152 */
d5a6fa2d
RH
13153 if (cur_el <= 2) {
13154 if (hcr_el2 & HCR_E2H) {
13155 /* Check CPTR_EL2.FPEN. */
13156 switch (extract32(env->cp15.cptr_el[2], 20, 2)) {
13157 case 1:
13158 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
13159 break;
13160 }
13161 /* fall through */
13162 case 0:
13163 case 2:
13164 return 2;
13165 }
13166 } else if (arm_is_el2_enabled(env)) {
13167 if (env->cp15.cptr_el[2] & CPTR_TFP) {
13168 return 2;
13169 }
13170 }
a9e01311
RH
13171 }
13172
13173 /* CPTR_EL3 : present in v8 */
a7b66ada 13174 if (env->cp15.cptr_el[3] & CPTR_TFP) {
a9e01311
RH
13175 /* Trap all FP ops to EL3 */
13176 return 3;
13177 }
55faa212 13178#endif
a9e01311
RH
13179 return 0;
13180}
13181
b9f6033c
RH
13182/* Return the exception level we're running at if this is our mmu_idx */
13183int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
13184{
13185 if (mmu_idx & ARM_MMU_IDX_M) {
13186 return mmu_idx & ARM_MMU_IDX_M_PRIV;
13187 }
13188
13189 switch (mmu_idx) {
13190 case ARMMMUIdx_E10_0:
13191 case ARMMMUIdx_E20_0:
13192 case ARMMMUIdx_SE10_0:
b6ad6062 13193 case ARMMMUIdx_SE20_0:
b9f6033c
RH
13194 return 0;
13195 case ARMMMUIdx_E10_1:
452ef8cb 13196 case ARMMMUIdx_E10_1_PAN:
b9f6033c 13197 case ARMMMUIdx_SE10_1:
452ef8cb 13198 case ARMMMUIdx_SE10_1_PAN:
b9f6033c
RH
13199 return 1;
13200 case ARMMMUIdx_E2:
13201 case ARMMMUIdx_E20_2:
452ef8cb 13202 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
13203 case ARMMMUIdx_SE2:
13204 case ARMMMUIdx_SE20_2:
13205 case ARMMMUIdx_SE20_2_PAN:
b9f6033c
RH
13206 return 2;
13207 case ARMMMUIdx_SE3:
13208 return 3;
13209 default:
13210 g_assert_not_reached();
13211 }
13212}
13213
7aab5a8c 13214#ifndef CONFIG_TCG
65e4655c
RH
13215ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
13216{
7aab5a8c 13217 g_assert_not_reached();
65e4655c 13218}
7aab5a8c 13219#endif
65e4655c 13220
164690b2 13221ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 13222{
b6ad6062
RDC
13223 ARMMMUIdx idx;
13224 uint64_t hcr;
13225
65e4655c 13226 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 13227 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
13228 }
13229
6003d980 13230 /* See ARM pseudo-function ELIsInHost. */
b9f6033c
RH
13231 switch (el) {
13232 case 0:
b6ad6062
RDC
13233 hcr = arm_hcr_el2_eff(env);
13234 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
13235 idx = ARMMMUIdx_E20_0;
13236 } else {
13237 idx = ARMMMUIdx_E10_0;
6003d980 13238 }
b6ad6062 13239 break;
b9f6033c 13240 case 1:
66412260 13241 if (env->pstate & PSTATE_PAN) {
b6ad6062
RDC
13242 idx = ARMMMUIdx_E10_1_PAN;
13243 } else {
13244 idx = ARMMMUIdx_E10_1;
66412260 13245 }
b6ad6062 13246 break;
b9f6033c 13247 case 2:
6003d980 13248 /* Note that TGE does not apply at EL2. */
b6ad6062 13249 if (arm_hcr_el2_eff(env) & HCR_E2H) {
66412260 13250 if (env->pstate & PSTATE_PAN) {
b6ad6062
RDC
13251 idx = ARMMMUIdx_E20_2_PAN;
13252 } else {
13253 idx = ARMMMUIdx_E20_2;
66412260 13254 }
b6ad6062
RDC
13255 } else {
13256 idx = ARMMMUIdx_E2;
6003d980 13257 }
b6ad6062 13258 break;
b9f6033c
RH
13259 case 3:
13260 return ARMMMUIdx_SE3;
13261 default:
13262 g_assert_not_reached();
65e4655c 13263 }
b6ad6062
RDC
13264
13265 if (arm_is_secure_below_el3(env)) {
13266 idx &= ~ARM_MMU_IDX_A_NS;
13267 }
13268
13269 return idx;
50494a27
RH
13270}
13271
164690b2
RH
13272ARMMMUIdx arm_mmu_idx(CPUARMState *env)
13273{
13274 return arm_mmu_idx_el(env, arm_current_el(env));
13275}
13276
64be86ab
RH
13277#ifndef CONFIG_USER_ONLY
13278ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
13279{
13280 return stage_1_mmu_idx(arm_mmu_idx(env));
13281}
13282#endif
13283
3902bfc6
RH
13284static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
13285 ARMMMUIdx mmu_idx,
13286 CPUARMTBFlags flags)
fdd1b228 13287{
a729a46b
RH
13288 DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
13289 DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
fdd1b228 13290
fdd1b228 13291 if (arm_singlestep_active(env)) {
a729a46b 13292 DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
fdd1b228
RH
13293 }
13294 return flags;
13295}
13296
3902bfc6
RH
13297static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
13298 ARMMMUIdx mmu_idx,
13299 CPUARMTBFlags flags)
43eccfb6 13300{
8061a649
RH
13301 bool sctlr_b = arm_sctlr_b(env);
13302
13303 if (sctlr_b) {
a729a46b 13304 DP_TBFLAG_A32(flags, SCTLR__B, 1);
8061a649
RH
13305 }
13306 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
a729a46b 13307 DP_TBFLAG_ANY(flags, BE_DATA, 1);
8061a649 13308 }
a729a46b 13309 DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
43eccfb6
RH
13310
13311 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
13312}
13313
3902bfc6
RH
13314static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
13315 ARMMMUIdx mmu_idx)
6e33ced5 13316{
3902bfc6 13317 CPUARMTBFlags flags = {};
4479ec30
RH
13318 uint32_t ccr = env->v7m.ccr[env->v7m.secure];
13319
13320 /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
13321 if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
13322 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13323 }
6e33ced5
RH
13324
13325 if (arm_v7m_is_handler_mode(env)) {
a729a46b 13326 DP_TBFLAG_M32(flags, HANDLER, 1);
6e33ced5
RH
13327 }
13328
13329 /*
13330 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
13331 * is suppressing them because the requested execution priority
13332 * is less than 0.
13333 */
13334 if (arm_feature(env, ARM_FEATURE_V8) &&
13335 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
4479ec30 13336 (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
a729a46b 13337 DP_TBFLAG_M32(flags, STACKCHECK, 1);
6e33ced5
RH
13338 }
13339
13340 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
13341}
13342
3902bfc6 13343static CPUARMTBFlags rebuild_hflags_aprofile(CPUARMState *env)
83f4baef 13344{
3902bfc6 13345 CPUARMTBFlags flags = {};
83f4baef 13346
a729a46b 13347 DP_TBFLAG_ANY(flags, DEBUG_TARGET_EL, arm_debug_target_el(env));
83f4baef
RH
13348 return flags;
13349}
13350
3902bfc6
RH
13351static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
13352 ARMMMUIdx mmu_idx)
c747224c 13353{
3902bfc6 13354 CPUARMTBFlags flags = rebuild_hflags_aprofile(env);
4479ec30
RH
13355 int el = arm_current_el(env);
13356
13357 if (arm_sctlr(env, el) & SCTLR_A) {
13358 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13359 }
0a54d68e
RH
13360
13361 if (arm_el_is_aa64(env, 1)) {
a729a46b 13362 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 13363 }
5bb0a20b 13364
4479ec30 13365 if (el < 2 && env->cp15.hstr_el2 &&
5bb0a20b 13366 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
a729a46b 13367 DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
5bb0a20b
MZ
13368 }
13369
520d1621
PM
13370 if (env->uncached_cpsr & CPSR_IL) {
13371 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
13372 }
13373
83f4baef 13374 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
c747224c
RH
13375}
13376
3902bfc6
RH
13377static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
13378 ARMMMUIdx mmu_idx)
a9e01311 13379{
3902bfc6 13380 CPUARMTBFlags flags = rebuild_hflags_aprofile(env);
d4d7503a 13381 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
b830a5ee 13382 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
d4d7503a
RH
13383 uint64_t sctlr;
13384 int tbii, tbid;
b9adaa70 13385
a729a46b 13386 DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
cd208a1c 13387
339370b9 13388 /* Get control bits for tagged addresses. */
b830a5ee
RH
13389 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
13390 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
5d8634f5 13391
a729a46b
RH
13392 DP_TBFLAG_A64(flags, TBII, tbii);
13393 DP_TBFLAG_A64(flags, TBID, tbid);
d4d7503a
RH
13394
13395 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
13396 int sve_el = sve_exception_el(env, el);
13397 uint32_t zcr_len;
5d8634f5 13398
d4d7503a
RH
13399 /*
13400 * If SVE is disabled, but FP is enabled,
13401 * then the effective len is 0.
13402 */
13403 if (sve_el != 0 && fp_el == 0) {
13404 zcr_len = 0;
13405 } else {
13406 zcr_len = sve_zcr_len_for_el(env, el);
5d8634f5 13407 }
a729a46b
RH
13408 DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
13409 DP_TBFLAG_A64(flags, ZCR_LEN, zcr_len);
d4d7503a 13410 }
1db5e96c 13411
aaec1432 13412 sctlr = regime_sctlr(env, stage1);
1db5e96c 13413
4479ec30
RH
13414 if (sctlr & SCTLR_A) {
13415 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13416 }
13417
8061a649 13418 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
a729a46b 13419 DP_TBFLAG_ANY(flags, BE_DATA, 1);
8061a649
RH
13420 }
13421
d4d7503a
RH
13422 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
13423 /*
13424 * In order to save space in flags, we record only whether
13425 * pauth is "inactive", meaning all insns are implemented as
13426 * a nop, or "active" when some action must be performed.
13427 * The decision of which action to take is left to a helper.
13428 */
13429 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
a729a46b 13430 DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
1db5e96c 13431 }
d4d7503a 13432 }
0816ef1b 13433
d4d7503a
RH
13434 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
13435 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
13436 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
a729a46b 13437 DP_TBFLAG_A64(flags, BT, 1);
0816ef1b 13438 }
d4d7503a 13439 }
08f1434a 13440
cc28fc30 13441 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
7a8014ab
RH
13442 if (!(env->pstate & PSTATE_UAO)) {
13443 switch (mmu_idx) {
13444 case ARMMMUIdx_E10_1:
13445 case ARMMMUIdx_E10_1_PAN:
13446 case ARMMMUIdx_SE10_1:
13447 case ARMMMUIdx_SE10_1_PAN:
13448 /* TODO: ARMv8.3-NV */
a729a46b 13449 DP_TBFLAG_A64(flags, UNPRIV, 1);
7a8014ab
RH
13450 break;
13451 case ARMMMUIdx_E20_2:
13452 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
13453 case ARMMMUIdx_SE20_2:
13454 case ARMMMUIdx_SE20_2_PAN:
7a8014ab
RH
13455 /*
13456 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
13457 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
13458 */
13459 if (env->cp15.hcr_el2 & HCR_TGE) {
a729a46b 13460 DP_TBFLAG_A64(flags, UNPRIV, 1);
7a8014ab
RH
13461 }
13462 break;
13463 default:
13464 break;
cc28fc30 13465 }
cc28fc30
RH
13466 }
13467
520d1621
PM
13468 if (env->pstate & PSTATE_IL) {
13469 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
13470 }
13471
81ae05fa
RH
13472 if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
13473 /*
13474 * Set MTE_ACTIVE if any access may be Checked, and leave clear
13475 * if all accesses must be Unchecked:
13476 * 1) If no TBI, then there are no tags in the address to check,
13477 * 2) If Tag Check Override, then all accesses are Unchecked,
13478 * 3) If Tag Check Fail == 0, then Checked access have no effect,
13479 * 4) If no Allocation Tag Access, then all accesses are Unchecked.
13480 */
13481 if (allocation_tag_access_enabled(env, el, sctlr)) {
a729a46b 13482 DP_TBFLAG_A64(flags, ATA, 1);
81ae05fa
RH
13483 if (tbid
13484 && !(env->pstate & PSTATE_TCO)
13485 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
a729a46b 13486 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
81ae05fa
RH
13487 }
13488 }
13489 /* And again for unprivileged accesses, if required. */
a729a46b 13490 if (EX_TBFLAG_A64(flags, UNPRIV)
81ae05fa
RH
13491 && tbid
13492 && !(env->pstate & PSTATE_TCO)
2d928adf 13493 && (sctlr & SCTLR_TCF0)
81ae05fa 13494 && allocation_tag_access_enabled(env, 0, sctlr)) {
a729a46b 13495 DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
81ae05fa
RH
13496 }
13497 /* Cache TCMA as well as TBI. */
a729a46b 13498 DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
81ae05fa
RH
13499 }
13500
d4d7503a
RH
13501 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
13502}
13503
3902bfc6 13504static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
3d74e2e9
RH
13505{
13506 int el = arm_current_el(env);
13507 int fp_el = fp_exception_el(env, el);
164690b2 13508 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3d74e2e9
RH
13509
13510 if (is_a64(env)) {
13511 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
13512 } else if (arm_feature(env, ARM_FEATURE_M)) {
13513 return rebuild_hflags_m32(env, fp_el, mmu_idx);
13514 } else {
13515 return rebuild_hflags_a32(env, fp_el, mmu_idx);
13516 }
13517}
13518
13519void arm_rebuild_hflags(CPUARMState *env)
13520{
13521 env->hflags = rebuild_hflags_internal(env);
13522}
13523
19717e9b
PM
13524/*
13525 * If we have triggered a EL state change we can't rely on the
13526 * translator having passed it to us, we need to recompute.
13527 */
13528void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
13529{
13530 int el = arm_current_el(env);
13531 int fp_el = fp_exception_el(env, el);
13532 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3902bfc6 13533
19717e9b
PM
13534 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
13535}
13536
14f3c588
RH
13537void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
13538{
13539 int fp_el = fp_exception_el(env, el);
13540 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13541
13542 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
13543}
13544
f80741d1
AB
13545/*
13546 * If we have triggered a EL state change we can't rely on the
563152e0 13547 * translator having passed it to us, we need to recompute.
f80741d1
AB
13548 */
13549void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
13550{
13551 int el = arm_current_el(env);
13552 int fp_el = fp_exception_el(env, el);
13553 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13554 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
13555}
13556
14f3c588
RH
13557void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
13558{
13559 int fp_el = fp_exception_el(env, el);
13560 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13561
13562 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
13563}
13564
13565void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
13566{
13567 int fp_el = fp_exception_el(env, el);
13568 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13569
13570 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
13571}
13572
0ee8b24a
PMD
13573static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
13574{
13575#ifdef CONFIG_DEBUG_TCG
3902bfc6
RH
13576 CPUARMTBFlags c = env->hflags;
13577 CPUARMTBFlags r = rebuild_hflags_internal(env);
0ee8b24a 13578
a378206a
RH
13579 if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
13580 fprintf(stderr, "TCG hflags mismatch "
13581 "(current:(0x%08x,0x" TARGET_FMT_lx ")"
13582 " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
13583 c.flags, c.flags2, r.flags, r.flags2);
0ee8b24a
PMD
13584 abort();
13585 }
13586#endif
13587}
13588
26702213
PM
13589static bool mve_no_pred(CPUARMState *env)
13590{
13591 /*
13592 * Return true if there is definitely no predication of MVE
13593 * instructions by VPR or LTPSIZE. (Returning false even if there
13594 * isn't any predication is OK; generated code will just be
13595 * a little worse.)
13596 * If the CPU does not implement MVE then this TB flag is always 0.
13597 *
13598 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
13599 * logic in gen_update_fp_context() needs to be updated to match.
13600 *
13601 * We do not include the effect of the ECI bits here -- they are
13602 * tracked in other TB flags. This simplifies the logic for
13603 * "when did we emit code that changes the MVE_NO_PRED TB flag
13604 * and thus need to end the TB?".
13605 */
13606 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
13607 return false;
13608 }
13609 if (env->v7m.vpr) {
13610 return false;
13611 }
13612 if (env->v7m.ltpsize < 4) {
13613 return false;
13614 }
13615 return true;
13616}
13617
d4d7503a
RH
13618void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
13619 target_ulong *cs_base, uint32_t *pflags)
13620{
3902bfc6 13621 CPUARMTBFlags flags;
d4d7503a 13622
0ee8b24a 13623 assert_hflags_rebuild_correctly(env);
3902bfc6 13624 flags = env->hflags;
3d74e2e9 13625
a729a46b 13626 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
d4d7503a 13627 *pc = env->pc;
d4d7503a 13628 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
a729a46b 13629 DP_TBFLAG_A64(flags, BTYPE, env->btype);
08f1434a 13630 }
a9e01311
RH
13631 } else {
13632 *pc = env->regs[15];
6e33ced5
RH
13633
13634 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
13635 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
13636 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
13637 != env->v7m.secure) {
a729a46b 13638 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
9550d1bd
RH
13639 }
13640
13641 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
13642 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
13643 (env->v7m.secure &&
13644 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
13645 /*
13646 * ASPEN is set, but FPCA/SFPA indicate that there is no
13647 * active FP context; we must create a new FP context before
13648 * executing any FP insn.
13649 */
a729a46b 13650 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
9550d1bd
RH
13651 }
13652
13653 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
13654 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
a729a46b 13655 DP_TBFLAG_M32(flags, LSPACT, 1);
9550d1bd 13656 }
26702213
PM
13657
13658 if (mve_no_pred(env)) {
13659 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
13660 }
6e33ced5 13661 } else {
bbad7c62
RH
13662 /*
13663 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
13664 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
13665 */
13666 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
a729a46b 13667 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
bbad7c62 13668 } else {
a729a46b
RH
13669 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
13670 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
bbad7c62 13671 }
0a54d68e 13672 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
a729a46b 13673 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 13674 }
6e33ced5
RH
13675 }
13676
a729a46b
RH
13677 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
13678 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
d4d7503a 13679 }
a9e01311 13680
60e12c37
RH
13681 /*
13682 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
13683 * states defined in the ARM ARM for software singlestep:
13684 * SS_ACTIVE PSTATE.SS State
13685 * 0 x Inactive (the TB flag for SS is always 0)
13686 * 1 0 Active-pending
13687 * 1 1 Active-not-pending
ae6eb1e9 13688 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
a9e01311 13689 */
a729a46b
RH
13690 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
13691 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
a9e01311 13692 }
a9e01311 13693
3902bfc6 13694 *pflags = flags.flags;
a378206a 13695 *cs_base = flags.flags2;
a9e01311 13696}
0ab5953b
RH
13697
13698#ifdef TARGET_AARCH64
13699/*
13700 * The manual says that when SVE is enabled and VQ is widened the
13701 * implementation is allowed to zero the previously inaccessible
13702 * portion of the registers. The corollary to that is that when
13703 * SVE is enabled and VQ is narrowed we are also allowed to zero
13704 * the now inaccessible portion of the registers.
13705 *
13706 * The intent of this is that no predicate bit beyond VQ is ever set.
13707 * Which means that some operations on predicate registers themselves
13708 * may operate on full uint64_t or even unrolled across the maximum
13709 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
13710 * may well be cheaper than conditionals to restrict the operation
13711 * to the relevant portion of a uint16_t[16].
13712 */
13713void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
13714{
13715 int i, j;
13716 uint64_t pmask;
13717
13718 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 13719 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
13720
13721 /* Zap the high bits of the zregs. */
13722 for (i = 0; i < 32; i++) {
13723 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
13724 }
13725
13726 /* Zap the high bits of the pregs and ffr. */
13727 pmask = 0;
13728 if (vq & 3) {
13729 pmask = ~(-1ULL << (16 * (vq & 3)));
13730 }
13731 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
13732 for (i = 0; i < 17; ++i) {
13733 env->vfp.pregs[i].p[j] &= pmask;
13734 }
13735 pmask = 0;
13736 }
13737}
13738
13739/*
13740 * Notice a change in SVE vector size when changing EL.
13741 */
9a05f7b6
RH
13742void aarch64_sve_change_el(CPUARMState *env, int old_el,
13743 int new_el, bool el0_a64)
0ab5953b 13744{
2fc0cc0e 13745 ARMCPU *cpu = env_archcpu(env);
0ab5953b 13746 int old_len, new_len;
9a05f7b6 13747 bool old_a64, new_a64;
0ab5953b
RH
13748
13749 /* Nothing to do if no SVE. */
cd208a1c 13750 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
13751 return;
13752 }
13753
13754 /* Nothing to do if FP is disabled in either EL. */
13755 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
13756 return;
13757 }
13758
13759 /*
13760 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
13761 * at ELx, or not available because the EL is in AArch32 state, then
13762 * for all purposes other than a direct read, the ZCR_ELx.LEN field
13763 * has an effective value of 0".
13764 *
13765 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
13766 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
13767 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
13768 * we already have the correct register contents when encountering the
13769 * vq0->vq0 transition between EL0->EL1.
13770 */
9a05f7b6
RH
13771 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
13772 old_len = (old_a64 && !sve_exception_el(env, old_el)
0ab5953b 13773 ? sve_zcr_len_for_el(env, old_el) : 0);
9a05f7b6
RH
13774 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
13775 new_len = (new_a64 && !sve_exception_el(env, new_el)
0ab5953b
RH
13776 ? sve_zcr_len_for_el(env, new_el) : 0);
13777
13778 /* When changing vector length, clear inaccessible state. */
13779 if (new_len < old_len) {
13780 aarch64_sve_narrow_vq(env, new_len + 1);
13781 }
13782}
13783#endif
This page took 4.562499 seconds and 4 git commands to generate.