]> Git Repo - qemu.git/blame - target/arm/helper.c
Merge tag 'artist-cursor-fix-final-pull-request' of https://github.com/hdeller/qemu...
[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 1757
da3d8b13
RH
1758 if (cpu_isar_feature(aa64_ras, cpu)) {
1759 valid_mask |= SCR_TERR;
1760 }
252e8c69
RH
1761 if (cpu_isar_feature(aa64_lor, cpu)) {
1762 valid_mask |= SCR_TLOR;
1763 }
1764 if (cpu_isar_feature(aa64_pauth, cpu)) {
1765 valid_mask |= SCR_API | SCR_APK;
1766 }
926c1b97
RDC
1767 if (cpu_isar_feature(aa64_sel2, cpu)) {
1768 valid_mask |= SCR_EEL2;
1769 }
8ddb300b
RH
1770 if (cpu_isar_feature(aa64_mte, cpu)) {
1771 valid_mask |= SCR_ATA;
1772 }
7cb1e618
RH
1773 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1774 valid_mask |= SCR_ENSCXT;
1775 }
ea22747c
RH
1776 } else {
1777 valid_mask &= ~(SCR_RW | SCR_ST);
da3d8b13
RH
1778 if (cpu_isar_feature(aa32_ras, cpu)) {
1779 valid_mask |= SCR_TERR;
1780 }
ea22747c 1781 }
64e0e2de
EI
1782
1783 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1784 valid_mask &= ~SCR_HCE;
1785
1786 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1787 * supported if EL2 exists. The bit is UNK/SBZP when
1788 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1789 * when EL2 is unavailable.
4eb27640 1790 * On ARMv8, this bit is always available.
64e0e2de 1791 */
4eb27640
GB
1792 if (arm_feature(env, ARM_FEATURE_V7) &&
1793 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1794 valid_mask &= ~SCR_SMD;
1795 }
1796 }
1797
1798 /* Clear all-context RES0 bits. */
1799 value &= valid_mask;
1800 raw_write(env, ri, value);
1801}
1802
10d0ef3e
MN
1803static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1804{
1805 /*
1806 * scr_write will set the RES1 bits on an AArch64-only CPU.
1807 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1808 */
1809 scr_write(env, ri, 0);
1810}
1811
630fcd4d
MZ
1812static CPAccessResult access_aa64_tid2(CPUARMState *env,
1813 const ARMCPRegInfo *ri,
1814 bool isread)
1815{
1816 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1817 return CP_ACCESS_TRAP_EL2;
1818 }
1819
1820 return CP_ACCESS_OK;
1821}
1822
c4241c7d 1823static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c 1824{
2fc0cc0e 1825 ARMCPU *cpu = env_archcpu(env);
b85a1fd6
FA
1826
1827 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1828 * bank
1829 */
1830 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1831 ri->secure & ARM_CP_SECSTATE_S);
1832
1833 return cpu->ccsidr[index];
776d4e5c
PM
1834}
1835
c4241c7d
PM
1836static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1837 uint64_t value)
776d4e5c 1838{
8d5c773e 1839 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1840}
1841
1090b9c6
PM
1842static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1843{
29a0af61 1844 CPUState *cs = env_cpu(env);
cc974d5c
RDC
1845 bool el1 = arm_current_el(env) == 1;
1846 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1090b9c6
PM
1847 uint64_t ret = 0;
1848
cc974d5c 1849 if (hcr_el2 & HCR_IMO) {
636540e9
PM
1850 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1851 ret |= CPSR_I;
1852 }
1853 } else {
1854 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1855 ret |= CPSR_I;
1856 }
1090b9c6 1857 }
636540e9 1858
cc974d5c 1859 if (hcr_el2 & HCR_FMO) {
636540e9
PM
1860 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1861 ret |= CPSR_F;
1862 }
1863 } else {
1864 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1865 ret |= CPSR_F;
1866 }
1090b9c6 1867 }
636540e9 1868
3c29632f
RH
1869 if (hcr_el2 & HCR_AMO) {
1870 if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1871 ret |= CPSR_A;
1872 }
1873 }
1874
1090b9c6
PM
1875 return ret;
1876}
1877
93fbc983
MZ
1878static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1879 bool isread)
1880{
1881 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1882 return CP_ACCESS_TRAP_EL2;
1883 }
1884
1885 return CP_ACCESS_OK;
1886}
1887
1888static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1889 bool isread)
1890{
1891 if (arm_feature(env, ARM_FEATURE_V8)) {
1892 return access_aa64_tid1(env, ri, isread);
1893 }
1894
1895 return CP_ACCESS_OK;
1896}
1897
e9aa6c21 1898static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
1899 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1900 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1901 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
1902 /* Performance monitors are implementation defined in v7,
1903 * but with an ARM recommended set of registers, which we
ac689a2e 1904 * follow.
200ac0ef
PM
1905 *
1906 * Performance registers fall into three categories:
1907 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1908 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1909 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1910 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1911 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1912 */
1913 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7a0e58fa 1914 .access = PL0_RW, .type = ARM_CP_ALIAS,
8521466b 1915 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1916 .writefn = pmcntenset_write,
1917 .accessfn = pmreg_access,
1918 .raw_writefn = raw_write },
8521466b
AF
1919 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1920 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1921 .access = PL0_RW, .accessfn = pmreg_access,
1922 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1923 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 1924 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
1925 .access = PL0_RW,
1926 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1927 .accessfn = pmreg_access,
1928 .writefn = pmcntenclr_write,
7a0e58fa 1929 .type = ARM_CP_ALIAS },
8521466b
AF
1930 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1931 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1932 .access = PL0_RW, .accessfn = pmreg_access,
7a0e58fa 1933 .type = ARM_CP_ALIAS,
8521466b
AF
1934 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1935 .writefn = pmcntenclr_write },
200ac0ef 1936 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
f4efb4b2 1937 .access = PL0_RW, .type = ARM_CP_IO,
e4e91a21 1938 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
1939 .accessfn = pmreg_access,
1940 .writefn = pmovsr_write,
1941 .raw_writefn = raw_write },
978364f1
AF
1942 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1943 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1944 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 1945 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
1946 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1947 .writefn = pmovsr_write,
1948 .raw_writefn = raw_write },
200ac0ef 1949 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
f4efb4b2
AL
1950 .access = PL0_W, .accessfn = pmreg_access_swinc,
1951 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d
AL
1952 .writefn = pmswinc_write },
1953 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1954 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
f4efb4b2
AL
1955 .access = PL0_W, .accessfn = pmreg_access_swinc,
1956 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d 1957 .writefn = pmswinc_write },
6b040780
WH
1958 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1959 .access = PL0_RW, .type = ARM_CP_ALIAS,
1960 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 1961 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
1962 .raw_writefn = raw_write},
1963 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1964 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 1965 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
1966 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1967 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 1968 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 1969 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
421c7ebd 1970 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 1971 .accessfn = pmreg_access_ccntr },
8521466b
AF
1972 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1973 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 1974 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b 1975 .type = ARM_CP_IO,
980ebe87
AL
1976 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1977 .readfn = pmccntr_read, .writefn = pmccntr_write,
1978 .raw_readfn = raw_read, .raw_writefn = raw_write, },
4b8afa1f
AL
1979 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1980 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
1981 .access = PL0_RW, .accessfn = pmreg_access,
1982 .type = ARM_CP_ALIAS | ARM_CP_IO,
1983 .resetvalue = 0, },
8521466b
AF
1984 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1985 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
980ebe87 1986 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
8521466b
AF
1987 .access = PL0_RW, .accessfn = pmreg_access,
1988 .type = ARM_CP_IO,
1989 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1990 .resetvalue = 0, },
200ac0ef 1991 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
5ecdd3e4
AL
1992 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1993 .accessfn = pmreg_access,
fdb86656
WH
1994 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1995 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1996 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
5ecdd3e4
AL
1997 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1998 .accessfn = pmreg_access,
fdb86656 1999 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
200ac0ef 2000 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
5ecdd3e4
AL
2001 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2002 .accessfn = pmreg_access_xevcntr,
2003 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2004 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2005 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2006 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2007 .accessfn = pmreg_access_xevcntr,
2008 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
200ac0ef 2009 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 2010 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 2011 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 2012 .resetvalue = 0,
d4e6df63 2013 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
2014 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2015 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 2016 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
2017 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2018 .resetvalue = 0,
2019 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 2020 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 2021 .access = PL1_RW, .accessfn = access_tpm,
b7d793ad 2022 .type = ARM_CP_ALIAS | ARM_CP_IO,
e6ec5457 2023 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 2024 .resetvalue = 0,
d4e6df63 2025 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
2026 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2027 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2028 .access = PL1_RW, .accessfn = access_tpm,
2029 .type = ARM_CP_IO,
2030 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2031 .writefn = pmintenset_write, .raw_writefn = raw_write,
2032 .resetvalue = 0x0 },
200ac0ef 2033 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
fc5f6856 2034 .access = PL1_RW, .accessfn = access_tpm,
887c0f15 2035 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
200ac0ef 2036 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 2037 .writefn = pmintenclr_write, },
978364f1
AF
2038 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2039 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
fc5f6856 2040 .access = PL1_RW, .accessfn = access_tpm,
887c0f15 2041 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
978364f1
AF
2042 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2043 .writefn = pmintenclr_write },
7da845b0
PM
2044 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2045 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
630fcd4d
MZ
2046 .access = PL1_R,
2047 .accessfn = access_aa64_tid2,
2048 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
2049 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2050 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
630fcd4d
MZ
2051 .access = PL1_RW,
2052 .accessfn = access_aa64_tid2,
2053 .writefn = csselr_write, .resetvalue = 0,
b85a1fd6
FA
2054 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2055 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
2056 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2057 * just RAZ for all cores:
2058 */
0ff644a7
PM
2059 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2060 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
93fbc983
MZ
2061 .access = PL1_R, .type = ARM_CP_CONST,
2062 .accessfn = access_aa64_tid1,
2063 .resetvalue = 0 },
f32cdad5
PM
2064 /* Auxiliary fault status registers: these also are IMPDEF, and we
2065 * choose to RAZ/WI for all cores.
2066 */
2067 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2068 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
84929218
RH
2069 .access = PL1_RW, .accessfn = access_tvm_trvm,
2070 .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
2071 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2072 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
84929218
RH
2073 .access = PL1_RW, .accessfn = access_tvm_trvm,
2074 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
2075 /* MAIR can just read-as-written because we don't implement caches
2076 * and so don't need to care about memory attributes.
2077 */
2078 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2079 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
84929218
RH
2080 .access = PL1_RW, .accessfn = access_tvm_trvm,
2081 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 2082 .resetvalue = 0 },
4cfb8ad8
PM
2083 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2084 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2085 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2086 .resetvalue = 0 },
b0fe2427
PM
2087 /* For non-long-descriptor page tables these are PRRR and NMRR;
2088 * regardless they still act as reads-as-written for QEMU.
b0fe2427 2089 */
1281f8e3 2090 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
2091 * allows them to assign the correct fieldoffset based on the endianness
2092 * handled in the field definitions.
2093 */
a903c449 2094 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
84929218
RH
2095 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2096 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2097 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2098 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 2099 .resetfn = arm_cp_reset_ignore },
a903c449 2100 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
84929218
RH
2101 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2102 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2103 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2104 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 2105 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
2106 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2107 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 2108 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
2109 /* 32 bit ITLB invalidates */
2110 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
30881b73
RH
2111 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2112 .writefn = tlbiall_write },
995939a6 2113 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
30881b73
RH
2114 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2115 .writefn = tlbimva_write },
995939a6 2116 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
30881b73
RH
2117 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2118 .writefn = tlbiasid_write },
995939a6
PM
2119 /* 32 bit DTLB invalidates */
2120 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
30881b73
RH
2121 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2122 .writefn = tlbiall_write },
995939a6 2123 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
30881b73
RH
2124 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2125 .writefn = tlbimva_write },
995939a6 2126 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
30881b73
RH
2127 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2128 .writefn = tlbiasid_write },
995939a6
PM
2129 /* 32 bit TLB invalidates */
2130 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73
RH
2131 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2132 .writefn = tlbiall_write },
995939a6 2133 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73
RH
2134 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2135 .writefn = tlbimva_write },
995939a6 2136 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73
RH
2137 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2138 .writefn = tlbiasid_write },
995939a6 2139 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73
RH
2140 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2141 .writefn = tlbimvaa_write },
995939a6
PM
2142};
2143
2144static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2145 /* 32 bit TLB invalidates, Inner Shareable */
2146 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
30881b73
RH
2147 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2148 .writefn = tlbiall_is_write },
995939a6 2149 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
30881b73
RH
2150 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2151 .writefn = tlbimva_is_write },
995939a6 2152 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
30881b73 2153 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 2154 .writefn = tlbiasid_is_write },
995939a6 2155 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
30881b73 2156 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 2157 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
2158};
2159
327dd510
AL
2160static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2161 /* PMOVSSET is not implemented in v7 before v7ve */
2162 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2163 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2164 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2165 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2166 .writefn = pmovsset_write,
2167 .raw_writefn = raw_write },
2168 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2169 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2170 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2171 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2172 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2173 .writefn = pmovsset_write,
2174 .raw_writefn = raw_write },
327dd510
AL
2175};
2176
c4241c7d
PM
2177static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2178 uint64_t value)
c326b979
PM
2179{
2180 value &= 1;
2181 env->teecr = value;
c326b979
PM
2182}
2183
cc7613bf
PM
2184static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2185 bool isread)
2186{
2187 /*
2188 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2189 * at all, so we don't need to check whether we're v8A.
2190 */
2191 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2192 (env->cp15.hstr_el2 & HSTR_TTEE)) {
2193 return CP_ACCESS_TRAP_EL2;
2194 }
2195 return CP_ACCESS_OK;
2196}
2197
3f208fd7
PM
2198static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2199 bool isread)
c326b979 2200{
dcbff19b 2201 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 2202 return CP_ACCESS_TRAP;
c326b979 2203 }
cc7613bf 2204 return teecr_access(env, ri, isread);
c326b979
PM
2205}
2206
2207static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2208 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2209 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2210 .resetvalue = 0,
cc7613bf 2211 .writefn = teecr_write, .accessfn = teecr_access },
c326b979
PM
2212 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2213 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 2214 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
2215};
2216
4d31c596 2217static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
2218 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2219 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2220 .access = PL0_RW,
54bf36ed 2221 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
2222 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2223 .access = PL0_RW,
54bf36ed
FA
2224 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2225 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
2226 .resetfn = arm_cp_reset_ignore },
2227 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2228 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2229 .access = PL0_R|PL1_W,
54bf36ed
FA
2230 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2231 .resetvalue = 0},
4d31c596
PM
2232 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2233 .access = PL0_R|PL1_W,
54bf36ed
FA
2234 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2235 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 2236 .resetfn = arm_cp_reset_ignore },
54bf36ed 2237 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 2238 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 2239 .access = PL1_RW,
54bf36ed
FA
2240 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2241 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2242 .access = PL1_RW,
2243 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2244 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2245 .resetvalue = 0 },
4d31c596
PM
2246};
2247
55d284af
PM
2248#ifndef CONFIG_USER_ONLY
2249
3f208fd7
PM
2250static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2251 bool isread)
00108f2d 2252{
75502672
PM
2253 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2254 * Writable only at the highest implemented exception level.
2255 */
2256 int el = arm_current_el(env);
5bc84371
RH
2257 uint64_t hcr;
2258 uint32_t cntkctl;
75502672
PM
2259
2260 switch (el) {
2261 case 0:
5bc84371
RH
2262 hcr = arm_hcr_el2_eff(env);
2263 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2264 cntkctl = env->cp15.cnthctl_el2;
2265 } else {
2266 cntkctl = env->cp15.c14_cntkctl;
2267 }
2268 if (!extract32(cntkctl, 0, 2)) {
75502672
PM
2269 return CP_ACCESS_TRAP;
2270 }
2271 break;
2272 case 1:
2273 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2274 arm_is_secure_below_el3(env)) {
2275 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2276 return CP_ACCESS_TRAP_UNCATEGORIZED;
2277 }
2278 break;
2279 case 2:
2280 case 3:
2281 break;
00108f2d 2282 }
75502672
PM
2283
2284 if (!isread && el < arm_highest_el(env)) {
2285 return CP_ACCESS_TRAP_UNCATEGORIZED;
2286 }
2287
00108f2d
PM
2288 return CP_ACCESS_OK;
2289}
2290
3f208fd7
PM
2291static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2292 bool isread)
00108f2d 2293{
0b6440af 2294 unsigned int cur_el = arm_current_el(env);
e6ef0169 2295 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2296 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2297
5bc84371
RH
2298 switch (cur_el) {
2299 case 0:
2300 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2301 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2302 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2303 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2304 }
0b6440af 2305
5bc84371
RH
2306 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2307 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2308 return CP_ACCESS_TRAP;
2309 }
2310
2311 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2312 if (hcr & HCR_E2H) {
2313 if (timeridx == GTIMER_PHYS &&
2314 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2315 return CP_ACCESS_TRAP_EL2;
2316 }
2317 } else {
2318 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
e6ef0169 2319 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2320 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2321 return CP_ACCESS_TRAP_EL2;
2322 }
2323 }
2324 break;
2325
2326 case 1:
2327 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
e6ef0169 2328 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2329 (hcr & HCR_E2H
2330 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2331 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2332 return CP_ACCESS_TRAP_EL2;
2333 }
2334 break;
0b6440af 2335 }
00108f2d
PM
2336 return CP_ACCESS_OK;
2337}
2338
3f208fd7
PM
2339static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2340 bool isread)
00108f2d 2341{
0b6440af 2342 unsigned int cur_el = arm_current_el(env);
e6ef0169 2343 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2344 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2345
5bc84371
RH
2346 switch (cur_el) {
2347 case 0:
2348 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2349 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2350 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2351 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2352 }
0b6440af 2353
5bc84371
RH
2354 /*
2355 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2356 * EL0 if EL0[PV]TEN is zero.
2357 */
2358 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2359 return CP_ACCESS_TRAP;
2360 }
2361 /* fall through */
2362
2363 case 1:
e6ef0169 2364 if (has_el2 && timeridx == GTIMER_PHYS) {
5bc84371
RH
2365 if (hcr & HCR_E2H) {
2366 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2367 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2368 return CP_ACCESS_TRAP_EL2;
2369 }
2370 } else {
2371 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2372 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2373 return CP_ACCESS_TRAP_EL2;
2374 }
2375 }
2376 }
2377 break;
0b6440af 2378 }
00108f2d
PM
2379 return CP_ACCESS_OK;
2380}
2381
2382static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
2383 const ARMCPRegInfo *ri,
2384 bool isread)
00108f2d 2385{
3f208fd7 2386 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2387}
2388
2389static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
2390 const ARMCPRegInfo *ri,
2391 bool isread)
00108f2d 2392{
3f208fd7 2393 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2394}
2395
3f208fd7
PM
2396static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2397 bool isread)
00108f2d 2398{
3f208fd7 2399 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2400}
2401
3f208fd7
PM
2402static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2403 bool isread)
00108f2d 2404{
3f208fd7 2405 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2406}
2407
b4d3978c 2408static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
2409 const ARMCPRegInfo *ri,
2410 bool isread)
b4d3978c
PM
2411{
2412 /* The AArch64 register view of the secure physical timer is
2413 * always accessible from EL3, and configurably accessible from
2414 * Secure EL1.
2415 */
2416 switch (arm_current_el(env)) {
2417 case 1:
2418 if (!arm_is_secure(env)) {
2419 return CP_ACCESS_TRAP;
2420 }
2421 if (!(env->cp15.scr_el3 & SCR_ST)) {
2422 return CP_ACCESS_TRAP_EL3;
2423 }
2424 return CP_ACCESS_OK;
2425 case 0:
2426 case 2:
2427 return CP_ACCESS_TRAP;
2428 case 3:
2429 return CP_ACCESS_OK;
2430 default:
2431 g_assert_not_reached();
2432 }
2433}
2434
55d284af
PM
2435static uint64_t gt_get_countervalue(CPUARMState *env)
2436{
7def8754
AJ
2437 ARMCPU *cpu = env_archcpu(env);
2438
2439 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
55d284af
PM
2440}
2441
2442static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2443{
2444 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2445
2446 if (gt->ctl & 1) {
2447 /* Timer enabled: calculate and set current ISTATUS, irq, and
2448 * reset timer to when ISTATUS next has to change
2449 */
edac4d8a
EI
2450 uint64_t offset = timeridx == GTIMER_VIRT ?
2451 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
2452 uint64_t count = gt_get_countervalue(&cpu->env);
2453 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 2454 int istatus = count - offset >= gt->cval;
55d284af 2455 uint64_t nexttick;
194cbc49 2456 int irqstate;
55d284af
PM
2457
2458 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
2459
2460 irqstate = (istatus && !(gt->ctl & 2));
2461 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2462
55d284af
PM
2463 if (istatus) {
2464 /* Next transition is when count rolls back over to zero */
2465 nexttick = UINT64_MAX;
2466 } else {
2467 /* Next transition is when we hit cval */
edac4d8a 2468 nexttick = gt->cval + offset;
55d284af
PM
2469 }
2470 /* Note that the desired next expiry time might be beyond the
2471 * signed-64-bit range of a QEMUTimer -- in this case we just
2472 * set the timer for as far in the future as possible. When the
2473 * timer expires we will reset the timer for any remaining period.
2474 */
7def8754 2475 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
4a0245b6
AJ
2476 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2477 } else {
2478 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af 2479 }
194cbc49 2480 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
2481 } else {
2482 /* Timer disabled: ISTATUS and timer output always clear */
2483 gt->ctl &= ~4;
2484 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 2485 timer_del(cpu->gt_timer[timeridx]);
194cbc49 2486 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
2487 }
2488}
2489
0e3eca4c
EI
2490static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2491 int timeridx)
55d284af 2492{
2fc0cc0e 2493 ARMCPU *cpu = env_archcpu(env);
55d284af 2494
bc72ad67 2495 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
2496}
2497
c4241c7d 2498static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 2499{
c4241c7d 2500 return gt_get_countervalue(env);
55d284af
PM
2501}
2502
53d1f856
RH
2503static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2504{
2505 uint64_t hcr;
2506
2507 switch (arm_current_el(env)) {
2508 case 2:
2509 hcr = arm_hcr_el2_eff(env);
2510 if (hcr & HCR_E2H) {
2511 return 0;
2512 }
2513 break;
2514 case 0:
2515 hcr = arm_hcr_el2_eff(env);
2516 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2517 return 0;
2518 }
2519 break;
2520 }
2521
2522 return env->cp15.cntvoff_el2;
2523}
2524
edac4d8a
EI
2525static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2526{
53d1f856 2527 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
edac4d8a
EI
2528}
2529
c4241c7d 2530static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2531 int timeridx,
c4241c7d 2532 uint64_t value)
55d284af 2533{
194cbc49 2534 trace_arm_gt_cval_write(timeridx, value);
55d284af 2535 env->cp15.c14_timer[timeridx].cval = value;
2fc0cc0e 2536 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af 2537}
c4241c7d 2538
0e3eca4c
EI
2539static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2540 int timeridx)
55d284af 2541{
53d1f856
RH
2542 uint64_t offset = 0;
2543
2544 switch (timeridx) {
2545 case GTIMER_VIRT:
8c94b071 2546 case GTIMER_HYPVIRT:
53d1f856
RH
2547 offset = gt_virt_cnt_offset(env);
2548 break;
2549 }
55d284af 2550
c4241c7d 2551 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 2552 (gt_get_countervalue(env) - offset));
55d284af
PM
2553}
2554
c4241c7d 2555static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2556 int timeridx,
c4241c7d 2557 uint64_t value)
55d284af 2558{
53d1f856
RH
2559 uint64_t offset = 0;
2560
2561 switch (timeridx) {
2562 case GTIMER_VIRT:
8c94b071 2563 case GTIMER_HYPVIRT:
53d1f856
RH
2564 offset = gt_virt_cnt_offset(env);
2565 break;
2566 }
55d284af 2567
194cbc49 2568 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 2569 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 2570 sextract64(value, 0, 32);
2fc0cc0e 2571 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af
PM
2572}
2573
c4241c7d 2574static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2575 int timeridx,
c4241c7d 2576 uint64_t value)
55d284af 2577{
2fc0cc0e 2578 ARMCPU *cpu = env_archcpu(env);
55d284af
PM
2579 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2580
194cbc49 2581 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 2582 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
2583 if ((oldval ^ value) & 1) {
2584 /* Enable toggled */
2585 gt_recalc_timer(cpu, timeridx);
d3afacc7 2586 } else if ((oldval ^ value) & 2) {
55d284af
PM
2587 /* IMASK toggled: don't need to recalculate,
2588 * just set the interrupt line based on ISTATUS
2589 */
194cbc49
PM
2590 int irqstate = (oldval & 4) && !(value & 2);
2591
2592 trace_arm_gt_imask_toggle(timeridx, irqstate);
2593 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 2594 }
55d284af
PM
2595}
2596
0e3eca4c
EI
2597static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2598{
2599 gt_timer_reset(env, ri, GTIMER_PHYS);
2600}
2601
2602static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2603 uint64_t value)
2604{
2605 gt_cval_write(env, ri, GTIMER_PHYS, value);
2606}
2607
2608static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2609{
2610 return gt_tval_read(env, ri, GTIMER_PHYS);
2611}
2612
2613static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2614 uint64_t value)
2615{
2616 gt_tval_write(env, ri, GTIMER_PHYS, value);
2617}
2618
2619static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2620 uint64_t value)
2621{
2622 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2623}
2624
bb5972e4
RH
2625static int gt_phys_redir_timeridx(CPUARMState *env)
2626{
2627 switch (arm_mmu_idx(env)) {
2628 case ARMMMUIdx_E20_0:
2629 case ARMMMUIdx_E20_2:
452ef8cb 2630 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
2631 case ARMMMUIdx_SE20_0:
2632 case ARMMMUIdx_SE20_2:
2633 case ARMMMUIdx_SE20_2_PAN:
bb5972e4
RH
2634 return GTIMER_HYP;
2635 default:
2636 return GTIMER_PHYS;
2637 }
2638}
2639
2640static int gt_virt_redir_timeridx(CPUARMState *env)
2641{
2642 switch (arm_mmu_idx(env)) {
2643 case ARMMMUIdx_E20_0:
2644 case ARMMMUIdx_E20_2:
452ef8cb 2645 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
2646 case ARMMMUIdx_SE20_0:
2647 case ARMMMUIdx_SE20_2:
2648 case ARMMMUIdx_SE20_2_PAN:
bb5972e4
RH
2649 return GTIMER_HYPVIRT;
2650 default:
2651 return GTIMER_VIRT;
2652 }
2653}
2654
2655static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2656 const ARMCPRegInfo *ri)
2657{
2658 int timeridx = gt_phys_redir_timeridx(env);
2659 return env->cp15.c14_timer[timeridx].cval;
2660}
2661
2662static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2663 uint64_t value)
2664{
2665 int timeridx = gt_phys_redir_timeridx(env);
2666 gt_cval_write(env, ri, timeridx, value);
2667}
2668
2669static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2670 const ARMCPRegInfo *ri)
2671{
2672 int timeridx = gt_phys_redir_timeridx(env);
2673 return gt_tval_read(env, ri, timeridx);
2674}
2675
2676static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2677 uint64_t value)
2678{
2679 int timeridx = gt_phys_redir_timeridx(env);
2680 gt_tval_write(env, ri, timeridx, value);
2681}
2682
2683static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2684 const ARMCPRegInfo *ri)
2685{
2686 int timeridx = gt_phys_redir_timeridx(env);
2687 return env->cp15.c14_timer[timeridx].ctl;
2688}
2689
2690static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2691 uint64_t value)
2692{
2693 int timeridx = gt_phys_redir_timeridx(env);
2694 gt_ctl_write(env, ri, timeridx, value);
2695}
2696
0e3eca4c
EI
2697static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2698{
2699 gt_timer_reset(env, ri, GTIMER_VIRT);
2700}
2701
2702static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2703 uint64_t value)
2704{
2705 gt_cval_write(env, ri, GTIMER_VIRT, value);
2706}
2707
2708static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2709{
2710 return gt_tval_read(env, ri, GTIMER_VIRT);
2711}
2712
2713static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2714 uint64_t value)
2715{
2716 gt_tval_write(env, ri, GTIMER_VIRT, value);
2717}
2718
2719static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2720 uint64_t value)
2721{
2722 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2723}
2724
edac4d8a
EI
2725static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2726 uint64_t value)
2727{
2fc0cc0e 2728 ARMCPU *cpu = env_archcpu(env);
edac4d8a 2729
194cbc49 2730 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
2731 raw_write(env, ri, value);
2732 gt_recalc_timer(cpu, GTIMER_VIRT);
2733}
2734
bb5972e4
RH
2735static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2736 const ARMCPRegInfo *ri)
2737{
2738 int timeridx = gt_virt_redir_timeridx(env);
2739 return env->cp15.c14_timer[timeridx].cval;
2740}
2741
2742static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2743 uint64_t value)
2744{
2745 int timeridx = gt_virt_redir_timeridx(env);
2746 gt_cval_write(env, ri, timeridx, value);
2747}
2748
2749static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2750 const ARMCPRegInfo *ri)
2751{
2752 int timeridx = gt_virt_redir_timeridx(env);
2753 return gt_tval_read(env, ri, timeridx);
2754}
2755
2756static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2757 uint64_t value)
2758{
2759 int timeridx = gt_virt_redir_timeridx(env);
2760 gt_tval_write(env, ri, timeridx, value);
2761}
2762
2763static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2764 const ARMCPRegInfo *ri)
2765{
2766 int timeridx = gt_virt_redir_timeridx(env);
2767 return env->cp15.c14_timer[timeridx].ctl;
2768}
2769
2770static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2771 uint64_t value)
2772{
2773 int timeridx = gt_virt_redir_timeridx(env);
2774 gt_ctl_write(env, ri, timeridx, value);
2775}
2776
b0e66d95
EI
2777static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2778{
2779 gt_timer_reset(env, ri, GTIMER_HYP);
2780}
2781
2782static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2783 uint64_t value)
2784{
2785 gt_cval_write(env, ri, GTIMER_HYP, value);
2786}
2787
2788static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2789{
2790 return gt_tval_read(env, ri, GTIMER_HYP);
2791}
2792
2793static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2794 uint64_t value)
2795{
2796 gt_tval_write(env, ri, GTIMER_HYP, value);
2797}
2798
2799static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2800 uint64_t value)
2801{
2802 gt_ctl_write(env, ri, GTIMER_HYP, value);
2803}
2804
b4d3978c
PM
2805static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2806{
2807 gt_timer_reset(env, ri, GTIMER_SEC);
2808}
2809
2810static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2811 uint64_t value)
2812{
2813 gt_cval_write(env, ri, GTIMER_SEC, value);
2814}
2815
2816static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2817{
2818 return gt_tval_read(env, ri, GTIMER_SEC);
2819}
2820
2821static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2822 uint64_t value)
2823{
2824 gt_tval_write(env, ri, GTIMER_SEC, value);
2825}
2826
2827static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2828 uint64_t value)
2829{
2830 gt_ctl_write(env, ri, GTIMER_SEC, value);
2831}
2832
8c94b071
RH
2833static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2834{
2835 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2836}
2837
2838static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2839 uint64_t value)
2840{
2841 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2842}
2843
2844static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2845{
2846 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2847}
2848
2849static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2850 uint64_t value)
2851{
2852 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2853}
2854
2855static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2856 uint64_t value)
2857{
2858 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2859}
2860
55d284af
PM
2861void arm_gt_ptimer_cb(void *opaque)
2862{
2863 ARMCPU *cpu = opaque;
2864
2865 gt_recalc_timer(cpu, GTIMER_PHYS);
2866}
2867
2868void arm_gt_vtimer_cb(void *opaque)
2869{
2870 ARMCPU *cpu = opaque;
2871
2872 gt_recalc_timer(cpu, GTIMER_VIRT);
2873}
2874
b0e66d95
EI
2875void arm_gt_htimer_cb(void *opaque)
2876{
2877 ARMCPU *cpu = opaque;
2878
2879 gt_recalc_timer(cpu, GTIMER_HYP);
2880}
2881
b4d3978c
PM
2882void arm_gt_stimer_cb(void *opaque)
2883{
2884 ARMCPU *cpu = opaque;
2885
2886 gt_recalc_timer(cpu, GTIMER_SEC);
2887}
2888
8c94b071
RH
2889void arm_gt_hvtimer_cb(void *opaque)
2890{
2891 ARMCPU *cpu = opaque;
2892
2893 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2894}
2895
96eec6b2
AJ
2896static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2897{
2898 ARMCPU *cpu = env_archcpu(env);
2899
2900 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2901}
2902
55d284af
PM
2903static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2904 /* Note that CNTFRQ is purely reads-as-written for the benefit
2905 * of software; writing it doesn't actually change the timer frequency.
2906 * Our reset value matches the fixed frequency we implement the timer at.
2907 */
2908 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2909 .type = ARM_CP_ALIAS,
a7adc4b7
PM
2910 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2911 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
2912 },
2913 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2914 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2915 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af 2916 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
96eec6b2 2917 .resetfn = arm_gt_cntfrq_reset,
55d284af
PM
2918 },
2919 /* overall control: mostly access permissions */
a7adc4b7
PM
2920 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2921 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
2922 .access = PL1_RW,
2923 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2924 .resetvalue = 0,
2925 },
2926 /* per-timer control */
2927 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 2928 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 2929 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
2930 .accessfn = gt_ptimer_access,
2931 .fieldoffset = offsetoflow32(CPUARMState,
2932 cp15.c14_timer[GTIMER_PHYS].ctl),
bb5972e4
RH
2933 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2934 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7 2935 },
9c513e78 2936 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
2937 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2938 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 2939 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
9ff9dd3c
PM
2940 .accessfn = gt_ptimer_access,
2941 .fieldoffset = offsetoflow32(CPUARMState,
2942 cp15.c14_timer[GTIMER_SEC].ctl),
2943 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2944 },
a7adc4b7
PM
2945 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2946 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
daf1dc5f 2947 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 2948 .accessfn = gt_ptimer_access,
55d284af
PM
2949 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2950 .resetvalue = 0,
bb5972e4
RH
2951 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2952 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2953 },
2954 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
daf1dc5f 2955 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
2956 .accessfn = gt_vtimer_access,
2957 .fieldoffset = offsetoflow32(CPUARMState,
2958 cp15.c14_timer[GTIMER_VIRT].ctl),
bb5972e4
RH
2959 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2960 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
2961 },
2962 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2963 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
daf1dc5f 2964 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 2965 .accessfn = gt_vtimer_access,
55d284af
PM
2966 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2967 .resetvalue = 0,
bb5972e4
RH
2968 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2969 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2970 },
2971 /* TimerValue views: a 32 bit downcounting view of the underlying state */
2972 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 2973 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 2974 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 2975 .accessfn = gt_ptimer_access,
bb5972e4 2976 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
55d284af 2977 },
9c513e78 2978 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
2979 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
2980 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 2981 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
9ff9dd3c
PM
2982 .accessfn = gt_ptimer_access,
2983 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
2984 },
a7adc4b7
PM
2985 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2986 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
daf1dc5f 2987 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 2988 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
bb5972e4 2989 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
a7adc4b7 2990 },
55d284af 2991 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
daf1dc5f 2992 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 2993 .accessfn = gt_vtimer_access,
bb5972e4 2994 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
55d284af 2995 },
a7adc4b7
PM
2996 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
2997 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
daf1dc5f 2998 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 2999 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
bb5972e4 3000 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
a7adc4b7 3001 },
55d284af
PM
3002 /* The counter itself */
3003 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 3004 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3005 .accessfn = gt_pct_access,
a7adc4b7
PM
3006 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3007 },
3008 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3009 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 3010 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3011 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
3012 },
3013 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 3014 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3015 .accessfn = gt_vct_access,
edac4d8a 3016 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
3017 },
3018 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3019 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 3020 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3021 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
3022 },
3023 /* Comparison value, indicating when the timer goes off */
3024 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3025 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3026 .access = PL0_RW,
7a0e58fa 3027 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3028 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 3029 .accessfn = gt_ptimer_access,
bb5972e4
RH
3030 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3031 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7 3032 },
9c513e78 3033 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3034 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3035 .access = PL0_RW,
9ff9dd3c
PM
3036 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3037 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3038 .accessfn = gt_ptimer_access,
3039 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3040 },
a7adc4b7
PM
3041 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3042 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
daf1dc5f 3043 .access = PL0_RW,
a7adc4b7
PM
3044 .type = ARM_CP_IO,
3045 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 3046 .resetvalue = 0, .accessfn = gt_ptimer_access,
bb5972e4
RH
3047 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3048 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
55d284af
PM
3049 },
3050 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
daf1dc5f 3051 .access = PL0_RW,
7a0e58fa 3052 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3053 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 3054 .accessfn = gt_vtimer_access,
bb5972e4
RH
3055 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3056 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
3057 },
3058 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3059 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
daf1dc5f 3060 .access = PL0_RW,
a7adc4b7
PM
3061 .type = ARM_CP_IO,
3062 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3063 .resetvalue = 0, .accessfn = gt_vtimer_access,
bb5972e4
RH
3064 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3065 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
55d284af 3066 },
b4d3978c
PM
3067 /* Secure timer -- this is actually restricted to only EL3
3068 * and configurably Secure-EL1 via the accessfn.
3069 */
3070 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3071 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3072 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3073 .accessfn = gt_stimer_access,
3074 .readfn = gt_sec_tval_read,
3075 .writefn = gt_sec_tval_write,
3076 .resetfn = gt_sec_timer_reset,
3077 },
3078 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3079 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3080 .type = ARM_CP_IO, .access = PL1_RW,
3081 .accessfn = gt_stimer_access,
3082 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3083 .resetvalue = 0,
3084 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3085 },
3086 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3087 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3088 .type = ARM_CP_IO, .access = PL1_RW,
3089 .accessfn = gt_stimer_access,
3090 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3091 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3092 },
55d284af
PM
3093};
3094
bb5972e4
RH
3095static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3096 bool isread)
3097{
3098 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3099 return CP_ACCESS_TRAP;
3100 }
3101 return CP_ACCESS_OK;
3102}
3103
55d284af 3104#else
26c4a83b
AB
3105
3106/* In user-mode most of the generic timer registers are inaccessible
3107 * however modern kernels (4.12+) allow access to cntvct_el0
55d284af 3108 */
26c4a83b
AB
3109
3110static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3111{
7def8754
AJ
3112 ARMCPU *cpu = env_archcpu(env);
3113
26c4a83b
AB
3114 /* Currently we have no support for QEMUTimer in linux-user so we
3115 * can't call gt_get_countervalue(env), instead we directly
3116 * call the lower level functions.
3117 */
7def8754 3118 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
26c4a83b
AB
3119}
3120
6cc7a3ae 3121static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
26c4a83b
AB
3122 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3123 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3124 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3125 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3126 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3127 },
3128 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3129 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3130 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3131 .readfn = gt_virt_cnt_read,
3132 },
6cc7a3ae
PM
3133};
3134
55d284af
PM
3135#endif
3136
c4241c7d 3137static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 3138{
891a2fe7 3139 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 3140 raw_write(env, ri, value);
891a2fe7 3141 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 3142 raw_write(env, ri, value & 0xfffff6ff);
4a501606 3143 } else {
8d5c773e 3144 raw_write(env, ri, value & 0xfffff1ff);
4a501606 3145 }
4a501606
PM
3146}
3147
3148#ifndef CONFIG_USER_ONLY
3149/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 3150
3f208fd7
PM
3151static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3152 bool isread)
92611c00
PM
3153{
3154 if (ri->opc2 & 4) {
926c1b97 3155 /* The ATS12NSO* operations must trap to EL3 or EL2 if executed in
87562e4f
PM
3156 * Secure EL1 (which can only happen if EL3 is AArch64).
3157 * They are simply UNDEF if executed from NS EL1.
3158 * They function normally from EL2 or EL3.
92611c00 3159 */
87562e4f
PM
3160 if (arm_current_el(env) == 1) {
3161 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
3162 if (env->cp15.scr_el3 & SCR_EEL2) {
3163 return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3164 }
87562e4f
PM
3165 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3166 }
3167 return CP_ACCESS_TRAP_UNCATEGORIZED;
3168 }
92611c00
PM
3169 }
3170 return CP_ACCESS_OK;
3171}
3172
9fb005b0 3173#ifdef CONFIG_TCG
060e8a48 3174static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
03ae85f8 3175 MMUAccessType access_type, ARMMMUIdx mmu_idx)
4a501606 3176{
a8170e5e 3177 hwaddr phys_addr;
4a501606
PM
3178 target_ulong page_size;
3179 int prot;
b7cc4e82 3180 bool ret;
01c097f7 3181 uint64_t par64;
1313e2d7 3182 bool format64 = false;
8bf5b6a9 3183 MemTxAttrs attrs = {};
e14b5a23 3184 ARMMMUFaultInfo fi = {};
5b2d261d 3185 ARMCacheAttrs cacheattrs = {};
4a501606 3186
5b2d261d 3187 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs,
bc52bfeb 3188 &prot, &page_size, &fi, &cacheattrs);
1313e2d7 3189
0710b2fa
PM
3190 if (ret) {
3191 /*
3192 * Some kinds of translation fault must cause exceptions rather
3193 * than being reported in the PAR.
3194 */
3195 int current_el = arm_current_el(env);
3196 int target_el;
3197 uint32_t syn, fsr, fsc;
3198 bool take_exc = false;
3199
b1a10c86 3200 if (fi.s1ptw && current_el == 1
fee7aa46 3201 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
0710b2fa
PM
3202 /*
3203 * Synchronous stage 2 fault on an access made as part of the
3204 * translation table walk for AT S1E0* or AT S1E1* insn
3205 * executed from NS EL1. If this is a synchronous external abort
3206 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3207 * to EL3. Otherwise the fault is taken as an exception to EL2,
3208 * and HPFAR_EL2 holds the faulting IPA.
3209 */
3210 if (fi.type == ARMFault_SyncExternalOnWalk &&
3211 (env->cp15.scr_el3 & SCR_EA)) {
3212 target_el = 3;
3213 } else {
3214 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
9861248f
RDC
3215 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3216 env->cp15.hpfar_el2 |= HPFAR_NS;
3217 }
0710b2fa
PM
3218 target_el = 2;
3219 }
3220 take_exc = true;
3221 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3222 /*
3223 * Synchronous external aborts during a translation table walk
3224 * are taken as Data Abort exceptions.
3225 */
3226 if (fi.stage2) {
3227 if (current_el == 3) {
3228 target_el = 3;
3229 } else {
3230 target_el = 2;
3231 }
3232 } else {
3233 target_el = exception_target_el(env);
3234 }
3235 take_exc = true;
3236 }
3237
3238 if (take_exc) {
3239 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3240 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3241 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3242 fsr = arm_fi_to_lfsc(&fi);
3243 fsc = extract32(fsr, 0, 6);
3244 } else {
3245 fsr = arm_fi_to_sfsc(&fi);
3246 fsc = 0x3f;
3247 }
3248 /*
3249 * Report exception with ESR indicating a fault due to a
3250 * translation table walk for a cache maintenance instruction.
3251 */
e24fd076 3252 syn = syn_data_abort_no_iss(current_el == target_el, 0,
0710b2fa
PM
3253 fi.ea, 1, fi.s1ptw, 1, fsc);
3254 env->exception.vaddress = value;
3255 env->exception.fsr = fsr;
3256 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3257 }
3258 }
3259
1313e2d7
EI
3260 if (is_a64(env)) {
3261 format64 = true;
3262 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3263 /*
3264 * ATS1Cxx:
3265 * * TTBCR.EAE determines whether the result is returned using the
3266 * 32-bit or the 64-bit PAR format
3267 * * Instructions executed in Hyp mode always use the 64bit format
3268 *
3269 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3270 * * The Non-secure TTBCR.EAE bit is set to 1
3271 * * The implementation includes EL2, and the value of HCR.VM is 1
3272 *
9d1bab33
PM
3273 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3274 *
23463e0e 3275 * ATS1Hx always uses the 64bit format.
1313e2d7
EI
3276 */
3277 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3278
3279 if (arm_feature(env, ARM_FEATURE_EL2)) {
452ef8cb
RH
3280 if (mmu_idx == ARMMMUIdx_E10_0 ||
3281 mmu_idx == ARMMMUIdx_E10_1 ||
3282 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9d1bab33 3283 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
1313e2d7
EI
3284 } else {
3285 format64 |= arm_current_el(env) == 2;
3286 }
3287 }
3288 }
3289
3290 if (format64) {
5efe9ed4 3291 /* Create a 64-bit PAR */
01c097f7 3292 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 3293 if (!ret) {
702a9357 3294 par64 |= phys_addr & ~0xfffULL;
8bf5b6a9
PM
3295 if (!attrs.secure) {
3296 par64 |= (1 << 9); /* NS */
3297 }
5b2d261d
AB
3298 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */
3299 par64 |= cacheattrs.shareability << 7; /* SH */
4a501606 3300 } else {
5efe9ed4
PM
3301 uint32_t fsr = arm_fi_to_lfsc(&fi);
3302
702a9357 3303 par64 |= 1; /* F */
b7cc4e82 3304 par64 |= (fsr & 0x3f) << 1; /* FS */
0f7b791b
PM
3305 if (fi.stage2) {
3306 par64 |= (1 << 9); /* S */
3307 }
3308 if (fi.s1ptw) {
3309 par64 |= (1 << 8); /* PTW */
3310 }
4a501606
PM
3311 }
3312 } else {
b7cc4e82 3313 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
3314 * translation table format (with WnR always clear).
3315 * Convert it to a 32-bit PAR.
3316 */
b7cc4e82 3317 if (!ret) {
702a9357
PM
3318 /* We do not set any attribute bits in the PAR */
3319 if (page_size == (1 << 24)
3320 && arm_feature(env, ARM_FEATURE_V7)) {
01c097f7 3321 par64 = (phys_addr & 0xff000000) | (1 << 1);
702a9357 3322 } else {
01c097f7 3323 par64 = phys_addr & 0xfffff000;
702a9357 3324 }
8bf5b6a9
PM
3325 if (!attrs.secure) {
3326 par64 |= (1 << 9); /* NS */
3327 }
702a9357 3328 } else {
5efe9ed4
PM
3329 uint32_t fsr = arm_fi_to_sfsc(&fi);
3330
b7cc4e82
PC
3331 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3332 ((fsr & 0xf) << 1) | 1;
702a9357 3333 }
4a501606 3334 }
060e8a48
PM
3335 return par64;
3336}
9fb005b0 3337#endif /* CONFIG_TCG */
060e8a48
PM
3338
3339static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3340{
9fb005b0 3341#ifdef CONFIG_TCG
03ae85f8 3342 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 3343 uint64_t par64;
d3649702
PM
3344 ARMMMUIdx mmu_idx;
3345 int el = arm_current_el(env);
3346 bool secure = arm_is_secure_below_el3(env);
060e8a48 3347
d3649702
PM
3348 switch (ri->opc2 & 6) {
3349 case 0:
04b07d29 3350 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
d3649702
PM
3351 switch (el) {
3352 case 3:
127b2b08 3353 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3354 break;
3355 case 2:
b6ad6062 3356 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
04b07d29 3357 /* fall through */
d3649702 3358 case 1:
04b07d29 3359 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
b1a10c86 3360 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
04b07d29
RH
3361 : ARMMMUIdx_Stage1_E1_PAN);
3362 } else {
b1a10c86 3363 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
04b07d29 3364 }
d3649702
PM
3365 break;
3366 default:
3367 g_assert_not_reached();
3368 }
3369 break;
3370 case 2:
3371 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3372 switch (el) {
3373 case 3:
fba37aed 3374 mmu_idx = ARMMMUIdx_SE10_0;
d3649702
PM
3375 break;
3376 case 2:
b1a10c86 3377 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
2859d7b5 3378 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3379 break;
3380 case 1:
b1a10c86 3381 mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3382 break;
3383 default:
3384 g_assert_not_reached();
3385 }
3386 break;
3387 case 4:
3388 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
01b98b68 3389 mmu_idx = ARMMMUIdx_E10_1;
d3649702
PM
3390 break;
3391 case 6:
3392 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
01b98b68 3393 mmu_idx = ARMMMUIdx_E10_0;
d3649702
PM
3394 break;
3395 default:
3396 g_assert_not_reached();
3397 }
3398
3399 par64 = do_ats_write(env, value, access_type, mmu_idx);
01c097f7
FA
3400
3401 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3402#else
3403 /* Handled by hardware accelerator. */
3404 g_assert_not_reached();
3405#endif /* CONFIG_TCG */
4a501606 3406}
060e8a48 3407
14db7fe0
PM
3408static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3409 uint64_t value)
3410{
9fb005b0 3411#ifdef CONFIG_TCG
03ae85f8 3412 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
3413 uint64_t par64;
3414
e013b741 3415 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2);
14db7fe0
PM
3416
3417 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3418#else
3419 /* Handled by hardware accelerator. */
3420 g_assert_not_reached();
3421#endif /* CONFIG_TCG */
14db7fe0
PM
3422}
3423
3f208fd7
PM
3424static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3425 bool isread)
2a47df95 3426{
926c1b97
RDC
3427 if (arm_current_el(env) == 3 &&
3428 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
2a47df95
PM
3429 return CP_ACCESS_TRAP;
3430 }
3431 return CP_ACCESS_OK;
3432}
3433
060e8a48
PM
3434static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3435 uint64_t value)
3436{
9fb005b0 3437#ifdef CONFIG_TCG
03ae85f8 3438 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
3439 ARMMMUIdx mmu_idx;
3440 int secure = arm_is_secure_below_el3(env);
3441
3442 switch (ri->opc2 & 6) {
3443 case 0:
3444 switch (ri->opc1) {
04b07d29
RH
3445 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3446 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
b1a10c86 3447 mmu_idx = (secure ? ARMMMUIdx_Stage1_SE1_PAN
04b07d29
RH
3448 : ARMMMUIdx_Stage1_E1_PAN);
3449 } else {
b1a10c86 3450 mmu_idx = secure ? ARMMMUIdx_Stage1_SE1 : ARMMMUIdx_Stage1_E1;
04b07d29 3451 }
d3649702
PM
3452 break;
3453 case 4: /* AT S1E2R, AT S1E2W */
b6ad6062 3454 mmu_idx = secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2;
d3649702
PM
3455 break;
3456 case 6: /* AT S1E3R, AT S1E3W */
127b2b08 3457 mmu_idx = ARMMMUIdx_SE3;
d3649702
PM
3458 break;
3459 default:
3460 g_assert_not_reached();
3461 }
3462 break;
3463 case 2: /* AT S1E0R, AT S1E0W */
b1a10c86 3464 mmu_idx = secure ? ARMMMUIdx_Stage1_SE0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3465 break;
3466 case 4: /* AT S12E1R, AT S12E1W */
fba37aed 3467 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1;
d3649702
PM
3468 break;
3469 case 6: /* AT S12E0R, AT S12E0W */
fba37aed 3470 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0;
d3649702
PM
3471 break;
3472 default:
3473 g_assert_not_reached();
3474 }
060e8a48 3475
d3649702 3476 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
9fb005b0
PMD
3477#else
3478 /* Handled by hardware accelerator. */
3479 g_assert_not_reached();
3480#endif /* CONFIG_TCG */
060e8a48 3481}
4a501606
PM
3482#endif
3483
3484static const ARMCPRegInfo vapa_cp_reginfo[] = {
3485 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3486 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
3487 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3488 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
3489 .writefn = par_write },
3490#ifndef CONFIG_USER_ONLY
87562e4f 3491 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 3492 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 3493 .access = PL1_W, .accessfn = ats_access,
0710b2fa 3494 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
4a501606 3495#endif
4a501606
PM
3496};
3497
18032bec
PM
3498/* Return basic MPU access permission bits. */
3499static uint32_t simple_mpu_ap_bits(uint32_t val)
3500{
3501 uint32_t ret;
3502 uint32_t mask;
3503 int i;
3504 ret = 0;
3505 mask = 3;
3506 for (i = 0; i < 16; i += 2) {
3507 ret |= (val >> i) & mask;
3508 mask <<= 2;
3509 }
3510 return ret;
3511}
3512
3513/* Pad basic MPU access permission bits to extended format. */
3514static uint32_t extended_mpu_ap_bits(uint32_t val)
3515{
3516 uint32_t ret;
3517 uint32_t mask;
3518 int i;
3519 ret = 0;
3520 mask = 3;
3521 for (i = 0; i < 16; i += 2) {
3522 ret |= (val & mask) << i;
3523 mask <<= 2;
3524 }
3525 return ret;
3526}
3527
c4241c7d
PM
3528static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3529 uint64_t value)
18032bec 3530{
7e09797c 3531 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
3532}
3533
c4241c7d 3534static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3535{
7e09797c 3536 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
3537}
3538
c4241c7d
PM
3539static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3540 uint64_t value)
18032bec 3541{
7e09797c 3542 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
3543}
3544
c4241c7d 3545static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3546{
7e09797c 3547 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
3548}
3549
6cb0b013
PC
3550static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3551{
3552 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3553
3554 if (!u32p) {
3555 return 0;
3556 }
3557
1bc04a88 3558 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
3559 return *u32p;
3560}
3561
3562static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3563 uint64_t value)
3564{
2fc0cc0e 3565 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3566 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3567
3568 if (!u32p) {
3569 return;
3570 }
3571
1bc04a88 3572 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 3573 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
3574 *u32p = value;
3575}
3576
6cb0b013
PC
3577static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3578 uint64_t value)
3579{
2fc0cc0e 3580 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3581 uint32_t nrgs = cpu->pmsav7_dregion;
3582
3583 if (value >= nrgs) {
3584 qemu_log_mask(LOG_GUEST_ERROR,
3585 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3586 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3587 return;
3588 }
3589
3590 raw_write(env, ri, value);
3591}
3592
3593static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
3594 /* Reset for all these registers is handled in arm_cpu_reset(),
3595 * because the PMSAv7 is also used by M-profile CPUs, which do
3596 * not register cpregs but still need the state to be reset.
3597 */
6cb0b013
PC
3598 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3599 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3600 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
3601 .readfn = pmsav7_read, .writefn = pmsav7_write,
3602 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3603 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3604 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3605 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
3606 .readfn = pmsav7_read, .writefn = pmsav7_write,
3607 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3608 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3609 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3610 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
3611 .readfn = pmsav7_read, .writefn = pmsav7_write,
3612 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3613 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3614 .access = PL1_RW,
1bc04a88 3615 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
3616 .writefn = pmsav7_rgnr_write,
3617 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3618};
3619
18032bec
PM
3620static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3621 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3622 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3623 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
3624 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3625 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 3626 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3627 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
3628 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3629 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3630 .access = PL1_RW,
7e09797c
PM
3631 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3632 .resetvalue = 0, },
18032bec
PM
3633 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3634 .access = PL1_RW,
7e09797c
PM
3635 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3636 .resetvalue = 0, },
ecce5c3c
PM
3637 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3638 .access = PL1_RW,
3639 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3640 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3641 .access = PL1_RW,
3642 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 3643 /* Protection region base and size registers */
e508a92b
PM
3644 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3645 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3646 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3647 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3648 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3649 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3650 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3651 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3652 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3653 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3654 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3655 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3656 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3657 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3658 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3659 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3660 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3661 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3662 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3663 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3664 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3665 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3666 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3667 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
3668};
3669
c4241c7d
PM
3670static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
3671 uint64_t value)
ecce5c3c 3672{
11f136ee 3673 TCR *tcr = raw_ptr(env, ri);
2ebcebe2
PM
3674 int maskshift = extract32(value, 0, 3);
3675
e389be16
FA
3676 if (!arm_feature(env, ARM_FEATURE_V8)) {
3677 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
3678 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3679 * using Long-desciptor translation table format */
3680 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3681 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
3682 /* In an implementation that includes the Security Extensions
3683 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3684 * Short-descriptor translation table format.
3685 */
3686 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3687 } else {
3688 value &= TTBCR_N;
3689 }
e42c4db3 3690 }
e389be16 3691
b6af0975 3692 /* Update the masks corresponding to the TCR bank being written
11f136ee 3693 * Note that we always calculate mask and base_mask, but
e42c4db3 3694 * they are only used for short-descriptor tables (ie if EAE is 0);
11f136ee
FA
3695 * for long-descriptor tables the TCR fields are used differently
3696 * and the mask and base_mask values are meaningless.
e42c4db3 3697 */
11f136ee
FA
3698 tcr->raw_tcr = value;
3699 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
3700 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
ecce5c3c
PM
3701}
3702
c4241c7d
PM
3703static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3704 uint64_t value)
d4e6df63 3705{
2fc0cc0e 3706 ARMCPU *cpu = env_archcpu(env);
ab638a32 3707 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3708
d4e6df63
PM
3709 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3710 /* With LPAE the TTBCR could result in a change of ASID
3711 * via the TTBCR.A1 bit, so do a TLB flush.
3712 */
d10eb08f 3713 tlb_flush(CPU(cpu));
d4e6df63 3714 }
ab638a32
RH
3715 /* Preserve the high half of TCR_EL1, set via TTBCR2. */
3716 value = deposit64(tcr->raw_tcr, 0, 32, value);
c4241c7d 3717 vmsa_ttbcr_raw_write(env, ri, value);
d4e6df63
PM
3718}
3719
ecce5c3c
PM
3720static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3721{
11f136ee
FA
3722 TCR *tcr = raw_ptr(env, ri);
3723
3724 /* Reset both the TCR as well as the masks corresponding to the bank of
3725 * the TCR being reset.
3726 */
3727 tcr->raw_tcr = 0;
3728 tcr->mask = 0;
3729 tcr->base_mask = 0xffffc000u;
ecce5c3c
PM
3730}
3731
d06dc933 3732static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
cb2e37df
PM
3733 uint64_t value)
3734{
2fc0cc0e 3735 ARMCPU *cpu = env_archcpu(env);
11f136ee 3736 TCR *tcr = raw_ptr(env, ri);
00c8cb0a 3737
cb2e37df 3738 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 3739 tlb_flush(CPU(cpu));
11f136ee 3740 tcr->raw_tcr = value;
cb2e37df
PM
3741}
3742
327ed10f
PM
3743static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3744 uint64_t value)
3745{
93f379b0
RH
3746 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3747 if (cpreg_field_is_64bit(ri) &&
3748 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
2fc0cc0e 3749 ARMCPU *cpu = env_archcpu(env);
d10eb08f 3750 tlb_flush(CPU(cpu));
327ed10f
PM
3751 }
3752 raw_write(env, ri, value);
3753}
3754
ed30da8e
RH
3755static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3756 uint64_t value)
3757{
d06dc933
RH
3758 /*
3759 * If we are running with E2&0 regime, then an ASID is active.
3760 * Flush if that might be changing. Note we're not checking
3761 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3762 * holds the active ASID, only checking the field that might.
3763 */
3764 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3765 (arm_hcr_el2_eff(env) & HCR_E2H)) {
b6ad6062
RDC
3766 uint16_t mask = ARMMMUIdxBit_E20_2 |
3767 ARMMMUIdxBit_E20_2_PAN |
3768 ARMMMUIdxBit_E20_0;
3769
3770 if (arm_is_secure_below_el3(env)) {
3771 mask >>= ARM_MMU_IDX_A_NS;
3772 }
3773
3774 tlb_flush_by_mmuidx(env_cpu(env), mask);
d06dc933 3775 }
ed30da8e
RH
3776 raw_write(env, ri, value);
3777}
3778
b698e9cf
EI
3779static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3780 uint64_t value)
3781{
2fc0cc0e 3782 ARMCPU *cpu = env_archcpu(env);
b698e9cf
EI
3783 CPUState *cs = CPU(cpu);
3784
97fa9350
RH
3785 /*
3786 * A change in VMID to the stage2 page table (Stage2) invalidates
3787 * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3788 */
b698e9cf 3789 if (raw_read(env, ri) != value) {
c4f060e8
RDC
3790 uint16_t mask = ARMMMUIdxBit_E10_1 |
3791 ARMMMUIdxBit_E10_1_PAN |
3792 ARMMMUIdxBit_E10_0;
3793
3794 if (arm_is_secure_below_el3(env)) {
3795 mask >>= ARM_MMU_IDX_A_NS;
3796 }
3797
3798 tlb_flush_by_mmuidx(cs, mask);
b698e9cf
EI
3799 raw_write(env, ri, value);
3800 }
3801}
3802
8e5d75c9 3803static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 3804 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218 3805 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4a7e2d73 3806 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 3807 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 3808 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
84929218 3809 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
88ca1c2d
FA
3810 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3811 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9 3812 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
84929218 3813 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
8e5d75c9
PC
3814 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3815 offsetof(CPUARMState, cp15.dfar_ns) } },
3816 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3817 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218
RH
3818 .access = PL1_RW, .accessfn = access_tvm_trvm,
3819 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
8e5d75c9 3820 .resetvalue = 0, },
8e5d75c9
PC
3821};
3822
3823static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
3824 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3825 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
84929218 3826 .access = PL1_RW, .accessfn = access_tvm_trvm,
d81c519c 3827 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 3828 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 3829 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
84929218
RH
3830 .access = PL1_RW, .accessfn = access_tvm_trvm,
3831 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
3832 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3833 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 3834 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 3835 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
84929218
RH
3836 .access = PL1_RW, .accessfn = access_tvm_trvm,
3837 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
3838 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3839 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
3840 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3841 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
3842 .access = PL1_RW, .accessfn = access_tvm_trvm,
3843 .writefn = vmsa_tcr_el12_write,
cb2e37df 3844 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
11f136ee 3845 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 3846 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
3847 .access = PL1_RW, .accessfn = access_tvm_trvm,
3848 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
b061a82b 3849 .raw_writefn = vmsa_ttbcr_raw_write,
d102058e
RH
3850 /* No offsetoflow32 -- pass the entire TCR to writefn/raw_writefn. */
3851 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.tcr_el[3]),
3852 offsetof(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
3853};
3854
ab638a32
RH
3855/* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3856 * qemu tlbs nor adjusting cached masks.
3857 */
3858static const ARMCPRegInfo ttbcr2_reginfo = {
3859 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
84929218
RH
3860 .access = PL1_RW, .accessfn = access_tvm_trvm,
3861 .type = ARM_CP_ALIAS,
d102058e
RH
3862 .bank_fieldoffsets = {
3863 offsetofhigh32(CPUARMState, cp15.tcr_el[3].raw_tcr),
3864 offsetofhigh32(CPUARMState, cp15.tcr_el[1].raw_tcr),
3865 },
ab638a32
RH
3866};
3867
c4241c7d
PM
3868static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3869 uint64_t value)
1047b9d7
PM
3870{
3871 env->cp15.c15_ticonfig = value & 0xe7;
3872 /* The OS_TYPE bit in this register changes the reported CPUID! */
3873 env->cp15.c0_cpuid = (value & (1 << 5)) ?
3874 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
3875}
3876
c4241c7d
PM
3877static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3878 uint64_t value)
1047b9d7
PM
3879{
3880 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
3881}
3882
c4241c7d
PM
3883static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3884 uint64_t value)
1047b9d7
PM
3885{
3886 /* Wait-for-interrupt (deprecated) */
2fc0cc0e 3887 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
1047b9d7
PM
3888}
3889
c4241c7d
PM
3890static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3891 uint64_t value)
c4804214
PM
3892{
3893 /* On OMAP there are registers indicating the max/min index of dcache lines
3894 * containing a dirty line; cache flush operations have to reset these.
3895 */
3896 env->cp15.c15_i_max = 0x000;
3897 env->cp15.c15_i_min = 0xff0;
c4804214
PM
3898}
3899
18032bec
PM
3900static const ARMCPRegInfo omap_cp_reginfo[] = {
3901 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3902 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 3903 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 3904 .resetvalue = 0, },
1047b9d7
PM
3905 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3906 .access = PL1_RW, .type = ARM_CP_NOP },
3907 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3908 .access = PL1_RW,
3909 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3910 .writefn = omap_ticonfig_write },
3911 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3912 .access = PL1_RW,
3913 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3914 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3915 .access = PL1_RW, .resetvalue = 0xff0,
3916 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3917 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3918 .access = PL1_RW,
3919 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3920 .writefn = omap_threadid_write },
3921 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3922 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 3923 .type = ARM_CP_NO_RAW,
1047b9d7
PM
3924 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3925 /* TODO: Peripheral port remap register:
3926 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3927 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3928 * when MMU is off.
3929 */
c4804214 3930 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 3931 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 3932 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 3933 .writefn = omap_cachemaint_write },
34f90529
PM
3934 { .name = "C9", .cp = 15, .crn = 9,
3935 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3936 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
3937};
3938
c4241c7d
PM
3939static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3940 uint64_t value)
1047b9d7 3941{
c0f4af17 3942 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
3943}
3944
3945static const ARMCPRegInfo xscale_cp_reginfo[] = {
3946 { .name = "XSCALE_CPAR",
3947 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3948 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3949 .writefn = xscale_cpar_write, },
2771db27
PM
3950 { .name = "XSCALE_AUXCR",
3951 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3952 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3953 .resetvalue = 0, },
3b771579
PM
3954 /* XScale specific cache-lockdown: since we have no cache we NOP these
3955 * and hope the guest does not really rely on cache behaviour.
3956 */
3957 { .name = "XSCALE_LOCK_ICACHE_LINE",
3958 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3959 .access = PL1_W, .type = ARM_CP_NOP },
3960 { .name = "XSCALE_UNLOCK_ICACHE",
3961 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3962 .access = PL1_W, .type = ARM_CP_NOP },
3963 { .name = "XSCALE_DCACHE_LOCK",
3964 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3965 .access = PL1_RW, .type = ARM_CP_NOP },
3966 { .name = "XSCALE_UNLOCK_DCACHE",
3967 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3968 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
3969};
3970
3971static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3972 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3973 * implementation of this implementation-defined space.
3974 * Ideally this should eventually disappear in favour of actually
3975 * implementing the correct behaviour for all cores.
3976 */
3977 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3978 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 3979 .access = PL1_RW,
7a0e58fa 3980 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 3981 .resetvalue = 0 },
18032bec
PM
3982};
3983
c4804214
PM
3984static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3985 /* Cache status: RAZ because we have no cache so it's always clean */
3986 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 3987 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 3988 .resetvalue = 0 },
c4804214
PM
3989};
3990
3991static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
3992 /* We never have a a block transfer operation in progress */
3993 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 3994 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 3995 .resetvalue = 0 },
30b05bba
PM
3996 /* The cache ops themselves: these all NOP for QEMU */
3997 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
3998 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3999 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4000 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4001 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4002 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4003 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4004 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4005 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4006 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4007 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4008 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
4009};
4010
4011static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4012 /* The cache test-and-clean instructions always return (1 << 30)
4013 * to indicate that there are no dirty cache lines.
4014 */
4015 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 4016 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4017 .resetvalue = (1 << 30) },
c4804214 4018 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 4019 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4020 .resetvalue = (1 << 30) },
c4804214
PM
4021};
4022
34f90529
PM
4023static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4024 /* Ignore ReadBuffer accesses */
4025 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4026 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 4027 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 4028 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
4029};
4030
731de9e6
EI
4031static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4032{
731de9e6 4033 unsigned int cur_el = arm_current_el(env);
731de9e6 4034
e6ef0169 4035 if (arm_is_el2_enabled(env) && cur_el == 1) {
731de9e6
EI
4036 return env->cp15.vpidr_el2;
4037 }
4038 return raw_read(env, ri);
4039}
4040
06a7e647 4041static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 4042{
2fc0cc0e 4043 ARMCPU *cpu = env_archcpu(env);
eb5e1d3c
PF
4044 uint64_t mpidr = cpu->mp_affinity;
4045
81bdde9d 4046 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 4047 mpidr |= (1U << 31);
81bdde9d
PM
4048 /* Cores which are uniprocessor (non-coherent)
4049 * but still implement the MP extensions set
a8e81b31 4050 * bit 30. (For instance, Cortex-R5).
81bdde9d 4051 */
a8e81b31
PC
4052 if (cpu->mp_is_up) {
4053 mpidr |= (1u << 30);
4054 }
81bdde9d 4055 }
c4241c7d 4056 return mpidr;
81bdde9d
PM
4057}
4058
06a7e647
EI
4059static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4060{
f0d574d6 4061 unsigned int cur_el = arm_current_el(env);
f0d574d6 4062
e6ef0169 4063 if (arm_is_el2_enabled(env) && cur_el == 1) {
f0d574d6
EI
4064 return env->cp15.vmpidr_el2;
4065 }
06a7e647
EI
4066 return mpidr_read_val(env);
4067}
4068
7ac681cf 4069static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 4070 /* NOP AMAIR0/1 */
b0fe2427
PM
4071 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4072 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
84929218
RH
4073 .access = PL1_RW, .accessfn = access_tvm_trvm,
4074 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427 4075 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 4076 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
84929218
RH
4077 .access = PL1_RW, .accessfn = access_tvm_trvm,
4078 .type = ARM_CP_CONST, .resetvalue = 0 },
891a2fe7 4079 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
4080 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4081 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4082 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 4083 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
84929218
RH
4084 .access = PL1_RW, .accessfn = access_tvm_trvm,
4085 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4086 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4087 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 4088 .writefn = vmsa_ttbr_write, },
891a2fe7 4089 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
84929218
RH
4090 .access = PL1_RW, .accessfn = access_tvm_trvm,
4091 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4092 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4093 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 4094 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
4095};
4096
c4241c7d 4097static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4098{
c4241c7d 4099 return vfp_get_fpcr(env);
b0d2b7d0
PM
4100}
4101
c4241c7d
PM
4102static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4103 uint64_t value)
b0d2b7d0
PM
4104{
4105 vfp_set_fpcr(env, value);
b0d2b7d0
PM
4106}
4107
c4241c7d 4108static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4109{
c4241c7d 4110 return vfp_get_fpsr(env);
b0d2b7d0
PM
4111}
4112
c4241c7d
PM
4113static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4114 uint64_t value)
b0d2b7d0
PM
4115{
4116 vfp_set_fpsr(env, value);
b0d2b7d0
PM
4117}
4118
3f208fd7
PM
4119static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4120 bool isread)
c2b820fe 4121{
aaec1432 4122 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
c2b820fe
PM
4123 return CP_ACCESS_TRAP;
4124 }
4125 return CP_ACCESS_OK;
4126}
4127
4128static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4129 uint64_t value)
4130{
4131 env->daif = value & PSTATE_DAIF;
4132}
4133
220f508f
RH
4134static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4135{
4136 return env->pstate & PSTATE_PAN;
4137}
4138
4139static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4140 uint64_t value)
4141{
4142 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4143}
4144
4145static const ARMCPRegInfo pan_reginfo = {
4146 .name = "PAN", .state = ARM_CP_STATE_AA64,
4147 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4148 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4149 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4150};
4151
9eeb7a1c
RH
4152static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4153{
4154 return env->pstate & PSTATE_UAO;
4155}
4156
4157static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4158 uint64_t value)
4159{
4160 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4161}
4162
4163static const ARMCPRegInfo uao_reginfo = {
4164 .name = "UAO", .state = ARM_CP_STATE_AA64,
4165 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4166 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4167 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4168};
4169
dc8b1853
RC
4170static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4171{
4172 return env->pstate & PSTATE_DIT;
4173}
4174
4175static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4176 uint64_t value)
4177{
4178 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4179}
4180
4181static const ARMCPRegInfo dit_reginfo = {
4182 .name = "DIT", .state = ARM_CP_STATE_AA64,
4183 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4184 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4185 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4186};
4187
f2f68a78
RC
4188static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4189{
4190 return env->pstate & PSTATE_SSBS;
4191}
4192
4193static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4194 uint64_t value)
4195{
4196 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4197}
4198
4199static const ARMCPRegInfo ssbs_reginfo = {
4200 .name = "SSBS", .state = ARM_CP_STATE_AA64,
4201 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4202 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4203 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4204};
4205
38262d8a
RH
4206static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4207 const ARMCPRegInfo *ri,
4208 bool isread)
8af35c37 4209{
38262d8a
RH
4210 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4211 switch (arm_current_el(env)) {
4212 case 0:
4213 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4214 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4215 return CP_ACCESS_TRAP;
4216 }
4217 /* fall through */
4218 case 1:
4219 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4220 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4221 return CP_ACCESS_TRAP_EL2;
4222 }
4223 break;
8af35c37
PM
4224 }
4225 return CP_ACCESS_OK;
4226}
4227
38262d8a 4228static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
1bed4d2e
RH
4229 const ARMCPRegInfo *ri,
4230 bool isread)
4231{
38262d8a 4232 /* Cache invalidate/clean to Point of Unification... */
1bed4d2e
RH
4233 switch (arm_current_el(env)) {
4234 case 0:
4235 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4236 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4237 return CP_ACCESS_TRAP;
4238 }
4239 /* fall through */
4240 case 1:
38262d8a
RH
4241 /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set. */
4242 if (arm_hcr_el2_eff(env) & HCR_TPU) {
1bed4d2e
RH
4243 return CP_ACCESS_TRAP_EL2;
4244 }
4245 break;
4246 }
4247 return CP_ACCESS_OK;
4248}
4249
dbb1fb27
AB
4250/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4251 * Page D4-1736 (DDI0487A.b)
4252 */
4253
b7e0730d
RH
4254static int vae1_tlbmask(CPUARMState *env)
4255{
e04a5752 4256 uint64_t hcr = arm_hcr_el2_eff(env);
bc944d3a 4257 uint16_t mask;
e04a5752
RDC
4258
4259 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
bc944d3a
RDC
4260 mask = ARMMMUIdxBit_E20_2 |
4261 ARMMMUIdxBit_E20_2_PAN |
4262 ARMMMUIdxBit_E20_0;
b7e0730d 4263 } else {
bc944d3a 4264 mask = ARMMMUIdxBit_E10_1 |
452ef8cb
RH
4265 ARMMMUIdxBit_E10_1_PAN |
4266 ARMMMUIdxBit_E10_0;
b7e0730d 4267 }
bc944d3a
RDC
4268
4269 if (arm_is_secure_below_el3(env)) {
4270 mask >>= ARM_MMU_IDX_A_NS;
4271 }
4272
4273 return mask;
b7e0730d
RH
4274}
4275
ea04dce7
RH
4276/* Return 56 if TBI is enabled, 64 otherwise. */
4277static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4278 uint64_t addr)
4279{
4280 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
4281 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4282 int select = extract64(addr, 55, 1);
4283
4284 return (tbi >> select) & 1 ? 56 : 64;
4285}
4286
4287static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4288{
b6ad6062 4289 uint64_t hcr = arm_hcr_el2_eff(env);
ea04dce7
RH
4290 ARMMMUIdx mmu_idx;
4291
4292 /* Only the regime of the mmu_idx below is significant. */
b6ad6062 4293 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
ea04dce7
RH
4294 mmu_idx = ARMMMUIdx_E20_0;
4295 } else {
4296 mmu_idx = ARMMMUIdx_E10_0;
4297 }
b6ad6062
RDC
4298
4299 if (arm_is_secure_below_el3(env)) {
4300 mmu_idx &= ~ARM_MMU_IDX_A_NS;
4301 }
4302
ea04dce7
RH
4303 return tlbbits_for_regime(env, mmu_idx, addr);
4304}
4305
fd3ed969
PM
4306static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4307 uint64_t value)
168aa23b 4308{
29a0af61 4309 CPUState *cs = env_cpu(env);
b7e0730d 4310 int mask = vae1_tlbmask(env);
dbb1fb27 4311
b7e0730d 4312 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
168aa23b
PM
4313}
4314
b4ab8ce9
PM
4315static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4316 uint64_t value)
4317{
29a0af61 4318 CPUState *cs = env_cpu(env);
b7e0730d 4319 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4320
4321 if (tlb_force_broadcast(env)) {
527db2be
RH
4322 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4323 } else {
4324 tlb_flush_by_mmuidx(cs, mask);
b4ab8ce9 4325 }
b4ab8ce9
PM
4326}
4327
90c19cdf 4328static int alle1_tlbmask(CPUARMState *env)
168aa23b 4329{
90c19cdf
RH
4330 /*
4331 * Note that the 'ALL' scope must invalidate both stage 1 and
fd3ed969
PM
4332 * stage 2 translations, whereas most other scopes only invalidate
4333 * stage 1 translations.
4334 */
fd3ed969 4335 if (arm_is_secure_below_el3(env)) {
452ef8cb
RH
4336 return ARMMMUIdxBit_SE10_1 |
4337 ARMMMUIdxBit_SE10_1_PAN |
4338 ARMMMUIdxBit_SE10_0;
fd3ed969 4339 } else {
452ef8cb
RH
4340 return ARMMMUIdxBit_E10_1 |
4341 ARMMMUIdxBit_E10_1_PAN |
4342 ARMMMUIdxBit_E10_0;
fd3ed969 4343 }
168aa23b
PM
4344}
4345
85d0dc9f
RH
4346static int e2_tlbmask(CPUARMState *env)
4347{
b6ad6062
RDC
4348 if (arm_is_secure_below_el3(env)) {
4349 return ARMMMUIdxBit_SE20_0 |
4350 ARMMMUIdxBit_SE20_2 |
4351 ARMMMUIdxBit_SE20_2_PAN |
4352 ARMMMUIdxBit_SE2;
4353 } else {
4354 return ARMMMUIdxBit_E20_0 |
4355 ARMMMUIdxBit_E20_2 |
4356 ARMMMUIdxBit_E20_2_PAN |
4357 ARMMMUIdxBit_E2;
4358 }
85d0dc9f
RH
4359}
4360
90c19cdf
RH
4361static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4362 uint64_t value)
4363{
4364 CPUState *cs = env_cpu(env);
4365 int mask = alle1_tlbmask(env);
4366
4367 tlb_flush_by_mmuidx(cs, mask);
4368}
4369
fd3ed969 4370static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
4371 uint64_t value)
4372{
85d0dc9f
RH
4373 CPUState *cs = env_cpu(env);
4374 int mask = e2_tlbmask(env);
fd3ed969 4375
85d0dc9f 4376 tlb_flush_by_mmuidx(cs, mask);
fd3ed969
PM
4377}
4378
43efaa33
PM
4379static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4380 uint64_t value)
4381{
2fc0cc0e 4382 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4383 CPUState *cs = CPU(cpu);
4384
127b2b08 4385 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4386}
4387
fd3ed969
PM
4388static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4389 uint64_t value)
4390{
29a0af61 4391 CPUState *cs = env_cpu(env);
90c19cdf
RH
4392 int mask = alle1_tlbmask(env);
4393
4394 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
fa439fc5
PM
4395}
4396
2bfb9d75
PM
4397static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4398 uint64_t value)
4399{
29a0af61 4400 CPUState *cs = env_cpu(env);
85d0dc9f 4401 int mask = e2_tlbmask(env);
2bfb9d75 4402
85d0dc9f 4403 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
2bfb9d75
PM
4404}
4405
43efaa33
PM
4406static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4407 uint64_t value)
4408{
29a0af61 4409 CPUState *cs = env_cpu(env);
43efaa33 4410
127b2b08 4411 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3);
43efaa33
PM
4412}
4413
fd3ed969
PM
4414static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4415 uint64_t value)
fa439fc5 4416{
fd3ed969
PM
4417 /* Invalidate by VA, EL2
4418 * Currently handles both VAE2 and VALE2, since we don't support
4419 * flush-last-level-only.
4420 */
85d0dc9f
RH
4421 CPUState *cs = env_cpu(env);
4422 int mask = e2_tlbmask(env);
fd3ed969
PM
4423 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4424
85d0dc9f 4425 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
fd3ed969
PM
4426}
4427
43efaa33
PM
4428static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4429 uint64_t value)
4430{
4431 /* Invalidate by VA, EL3
4432 * Currently handles both VAE3 and VALE3, since we don't support
4433 * flush-last-level-only.
4434 */
2fc0cc0e 4435 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4436 CPUState *cs = CPU(cpu);
4437 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4438
127b2b08 4439 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3);
43efaa33
PM
4440}
4441
fd3ed969
PM
4442static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4443 uint64_t value)
4444{
90c19cdf
RH
4445 CPUState *cs = env_cpu(env);
4446 int mask = vae1_tlbmask(env);
fa439fc5 4447 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4448 int bits = vae1_tlbbits(env, pageaddr);
fa439fc5 4449
ea04dce7 4450 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
fa439fc5
PM
4451}
4452
b4ab8ce9
PM
4453static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4454 uint64_t value)
4455{
4456 /* Invalidate by VA, EL1&0 (AArch64 version).
4457 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4458 * since we don't support flush-for-specific-ASID-only or
4459 * flush-last-level-only.
4460 */
90c19cdf
RH
4461 CPUState *cs = env_cpu(env);
4462 int mask = vae1_tlbmask(env);
b4ab8ce9 4463 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4464 int bits = vae1_tlbbits(env, pageaddr);
b4ab8ce9
PM
4465
4466 if (tlb_force_broadcast(env)) {
ea04dce7 4467 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
527db2be 4468 } else {
ea04dce7 4469 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
b4ab8ce9 4470 }
b4ab8ce9
PM
4471}
4472
fd3ed969
PM
4473static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4474 uint64_t value)
fa439fc5 4475{
29a0af61 4476 CPUState *cs = env_cpu(env);
fd3ed969 4477 uint64_t pageaddr = sextract64(value << 12, 0, 56);
b6ad6062
RDC
4478 bool secure = arm_is_secure_below_el3(env);
4479 int mask = secure ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2;
eb849d8f 4480 int bits = tlbbits_for_regime(env, secure ? ARMMMUIdx_SE2 : ARMMMUIdx_E2,
b6ad6062 4481 pageaddr);
fa439fc5 4482
b6ad6062 4483 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
fa439fc5
PM
4484}
4485
43efaa33
PM
4486static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4487 uint64_t value)
4488{
29a0af61 4489 CPUState *cs = env_cpu(env);
43efaa33 4490 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4491 int bits = tlbbits_for_regime(env, ARMMMUIdx_SE3, pageaddr);
43efaa33 4492
ea04dce7
RH
4493 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4494 ARMMMUIdxBit_SE3, bits);
43efaa33
PM
4495}
4496
84940ed8 4497#ifdef TARGET_AARCH64
ab1cdb47
RH
4498typedef struct {
4499 uint64_t base;
84940ed8 4500 uint64_t length;
ab1cdb47
RH
4501} TLBIRange;
4502
4503static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4504 uint64_t value)
4505{
4506 unsigned int page_size_granule, page_shift, num, scale, exponent;
3974ff93
RH
4507 /* Extract one bit to represent the va selector in use. */
4508 uint64_t select = sextract64(value, 36, 1);
4509 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
ab1cdb47 4510 TLBIRange ret = { };
84940ed8 4511
84940ed8
RC
4512 page_size_granule = extract64(value, 46, 2);
4513
3974ff93
RH
4514 /* The granule encoded in value must match the granule in use. */
4515 if (page_size_granule != (param.using64k ? 3 : param.using16k ? 2 : 1)) {
4516 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
84940ed8 4517 page_size_granule);
ab1cdb47 4518 return ret;
84940ed8
RC
4519 }
4520
52a9f609 4521 page_shift = (page_size_granule - 1) * 2 + 12;
ab1cdb47
RH
4522 num = extract64(value, 39, 5);
4523 scale = extract64(value, 44, 2);
84940ed8 4524 exponent = (5 * scale) + 1;
84940ed8 4525
ab1cdb47 4526 ret.length = (num + 1) << (exponent + page_shift);
84940ed8 4527
3974ff93 4528 if (param.select) {
d976de21 4529 ret.base = sextract64(value, 0, 37);
84940ed8 4530 } else {
d976de21 4531 ret.base = extract64(value, 0, 37);
84940ed8 4532 }
ef56c242
RH
4533 if (param.ds) {
4534 /*
4535 * With DS=1, BaseADDR is always shifted 16 so that it is able
4536 * to address all 52 va bits. The input address is perforce
4537 * aligned on a 64k boundary regardless of translation granule.
4538 */
4539 page_shift = 16;
4540 }
d976de21 4541 ret.base <<= page_shift;
84940ed8 4542
ab1cdb47 4543 return ret;
84940ed8
RC
4544}
4545
4546static void do_rvae_write(CPUARMState *env, uint64_t value,
4547 int idxmap, bool synced)
4548{
4549 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
ab1cdb47 4550 TLBIRange range;
84940ed8
RC
4551 int bits;
4552
ab1cdb47
RH
4553 range = tlbi_aa64_get_range(env, one_idx, value);
4554 bits = tlbbits_for_regime(env, one_idx, range.base);
84940ed8
RC
4555
4556 if (synced) {
4557 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
ab1cdb47
RH
4558 range.base,
4559 range.length,
84940ed8
RC
4560 idxmap,
4561 bits);
4562 } else {
ab1cdb47
RH
4563 tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4564 range.length, idxmap, bits);
84940ed8
RC
4565 }
4566}
4567
4568static void tlbi_aa64_rvae1_write(CPUARMState *env,
4569 const ARMCPRegInfo *ri,
4570 uint64_t value)
4571{
4572 /*
4573 * Invalidate by VA range, EL1&0.
4574 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4575 * since we don't support flush-for-specific-ASID-only or
4576 * flush-last-level-only.
4577 */
4578
4579 do_rvae_write(env, value, vae1_tlbmask(env),
4580 tlb_force_broadcast(env));
4581}
4582
4583static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4584 const ARMCPRegInfo *ri,
4585 uint64_t value)
4586{
4587 /*
4588 * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4589 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4590 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4591 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4592 * shareable specific flushes.
4593 */
4594
4595 do_rvae_write(env, value, vae1_tlbmask(env), true);
4596}
4597
4598static int vae2_tlbmask(CPUARMState *env)
4599{
4600 return (arm_is_secure_below_el3(env)
4601 ? ARMMMUIdxBit_SE2 : ARMMMUIdxBit_E2);
4602}
4603
4604static void tlbi_aa64_rvae2_write(CPUARMState *env,
4605 const ARMCPRegInfo *ri,
4606 uint64_t value)
4607{
4608 /*
4609 * Invalidate by VA range, EL2.
4610 * Currently handles all of RVAE2 and RVALE2,
4611 * since we don't support flush-for-specific-ASID-only or
4612 * flush-last-level-only.
4613 */
4614
4615 do_rvae_write(env, value, vae2_tlbmask(env),
4616 tlb_force_broadcast(env));
4617
4618
4619}
4620
4621static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4622 const ARMCPRegInfo *ri,
4623 uint64_t value)
4624{
4625 /*
4626 * Invalidate by VA range, Inner/Outer Shareable, EL2.
4627 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4628 * since we don't support flush-for-specific-ASID-only,
4629 * flush-last-level-only or inner/outer shareable specific flushes.
4630 */
4631
4632 do_rvae_write(env, value, vae2_tlbmask(env), true);
4633
4634}
4635
4636static void tlbi_aa64_rvae3_write(CPUARMState *env,
4637 const ARMCPRegInfo *ri,
4638 uint64_t value)
4639{
4640 /*
4641 * Invalidate by VA range, EL3.
4642 * Currently handles all of RVAE3 and RVALE3,
4643 * since we don't support flush-for-specific-ASID-only or
4644 * flush-last-level-only.
4645 */
4646
4647 do_rvae_write(env, value, ARMMMUIdxBit_SE3,
4648 tlb_force_broadcast(env));
4649}
4650
4651static void tlbi_aa64_rvae3is_write(CPUARMState *env,
4652 const ARMCPRegInfo *ri,
4653 uint64_t value)
4654{
4655 /*
4656 * Invalidate by VA range, EL3, Inner/Outer Shareable.
4657 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
4658 * since we don't support flush-for-specific-ASID-only,
4659 * flush-last-level-only or inner/outer specific flushes.
4660 */
4661
4662 do_rvae_write(env, value, ARMMMUIdxBit_SE3, true);
4663}
4664#endif
4665
3f208fd7
PM
4666static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4667 bool isread)
aca3f40b 4668{
4351cb72
RH
4669 int cur_el = arm_current_el(env);
4670
4671 if (cur_el < 2) {
4672 uint64_t hcr = arm_hcr_el2_eff(env);
4673
4674 if (cur_el == 0) {
4675 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4676 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4677 return CP_ACCESS_TRAP_EL2;
4678 }
4679 } else {
4680 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4681 return CP_ACCESS_TRAP;
4682 }
4683 if (hcr & HCR_TDZ) {
4684 return CP_ACCESS_TRAP_EL2;
4685 }
4686 }
4687 } else if (hcr & HCR_TDZ) {
4688 return CP_ACCESS_TRAP_EL2;
4689 }
aca3f40b
PM
4690 }
4691 return CP_ACCESS_OK;
4692}
4693
4694static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4695{
2fc0cc0e 4696 ARMCPU *cpu = env_archcpu(env);
aca3f40b
PM
4697 int dzp_bit = 1 << 4;
4698
4699 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 4700 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
4701 dzp_bit = 0;
4702 }
4703 return cpu->dcz_blocksize | dzp_bit;
4704}
4705
3f208fd7
PM
4706static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4707 bool isread)
f502cfc2 4708{
cdcf1405 4709 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
4710 /* Access to SP_EL0 is undefined if it's being used as
4711 * the stack pointer.
4712 */
4713 return CP_ACCESS_TRAP_UNCATEGORIZED;
4714 }
4715 return CP_ACCESS_OK;
4716}
4717
4718static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4719{
4720 return env->pstate & PSTATE_SP;
4721}
4722
4723static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4724{
4725 update_spsel(env, val);
4726}
4727
137feaa9
FA
4728static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4729 uint64_t value)
4730{
2fc0cc0e 4731 ARMCPU *cpu = env_archcpu(env);
137feaa9 4732
f00faf13
RH
4733 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4734 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4735 value &= ~SCTLR_M;
4736 }
4737
4738 /* ??? Lots of these bits are not implemented. */
4739
4740 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4741 if (ri->opc1 == 6) { /* SCTLR_EL3 */
4742 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4743 } else {
4744 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4745 SCTLR_ATA0 | SCTLR_ATA);
4746 }
4747 }
4748
137feaa9
FA
4749 if (raw_read(env, ri) == value) {
4750 /* Skip the TLB flush if nothing actually changed; Linux likes
4751 * to do a lot of pointless SCTLR writes.
4752 */
4753 return;
4754 }
4755
4756 raw_write(env, ri, value);
f00faf13 4757
137feaa9 4758 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 4759 tlb_flush(CPU(cpu));
2e5dcf36
RH
4760
4761 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4762 /*
4763 * Normally we would always end the TB on an SCTLR write; see the
4764 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4765 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4766 * of hflags from the translator, so do it here.
4767 */
4768 arm_rebuild_hflags(env);
4769 }
137feaa9
FA
4770}
4771
a8d64e73
PM
4772static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4773 uint64_t value)
4774{
4775 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
4776}
4777
b0d2b7d0
PM
4778static const ARMCPRegInfo v8_cp_reginfo[] = {
4779 /* Minimal set of EL0-visible registers. This will need to be expanded
4780 * significantly for system emulation of AArch64 CPUs.
4781 */
4782 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4783 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4784 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
4785 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4786 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 4787 .type = ARM_CP_NO_RAW,
c2b820fe
PM
4788 .access = PL0_RW, .accessfn = aa64_daif_access,
4789 .fieldoffset = offsetof(CPUARMState, daif),
4790 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
4791 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4792 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 4793 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4794 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
4795 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4796 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 4797 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4798 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
4799 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4800 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 4801 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
4802 .readfn = aa64_dczid_read },
4803 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4804 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4805 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4806#ifndef CONFIG_USER_ONLY
4807 /* Avoid overhead of an access check that always passes in user-mode */
4808 .accessfn = aa64_zva_access,
4809#endif
4810 },
0eef9d98
PM
4811 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4812 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4813 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
4814 /* Cache ops: all NOPs since we don't emulate caches */
4815 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4816 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a
RH
4817 .access = PL1_W, .type = ARM_CP_NOP,
4818 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4819 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4820 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a
RH
4821 .access = PL1_W, .type = ARM_CP_NOP,
4822 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4823 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4824 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4825 .access = PL0_W, .type = ARM_CP_NOP,
38262d8a 4826 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4827 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4828 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e
RH
4829 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4830 .type = ARM_CP_NOP },
8af35c37
PM
4831 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4832 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 4833 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
4834 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4835 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4836 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 4837 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
4838 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4839 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 4840 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
4841 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4842 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4843 .access = PL0_W, .type = ARM_CP_NOP,
38262d8a 4844 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4845 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4846 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4847 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 4848 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
4849 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4850 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 4851 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
168aa23b
PM
4852 /* TLBI operations */
4853 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4854 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
30881b73 4855 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4856 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4857 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4858 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
30881b73 4859 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4860 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4861 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4862 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
30881b73 4863 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4864 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4865 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4866 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
30881b73 4867 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4868 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4869 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4870 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
30881b73 4871 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4872 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4873 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4874 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
30881b73 4875 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4876 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4877 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4878 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73 4879 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4880 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4881 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4882 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73 4883 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4884 .writefn = tlbi_aa64_vae1_write },
168aa23b 4885 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4886 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73 4887 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4888 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4889 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4890 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73 4891 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4892 .writefn = tlbi_aa64_vae1_write },
168aa23b 4893 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4894 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73 4895 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4896 .writefn = tlbi_aa64_vae1_write },
168aa23b 4897 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4898 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73 4899 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4900 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
4901 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4902 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
bf05340c 4903 .access = PL2_W, .type = ARM_CP_NOP },
cea66e91
PM
4904 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4905 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
bf05340c 4906 .access = PL2_W, .type = ARM_CP_NOP },
83ddf975
PM
4907 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4908 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4909 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4910 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
4911 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4912 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4913 .access = PL2_W, .type = ARM_CP_NO_RAW,
4914 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
4915 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4916 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
bf05340c 4917 .access = PL2_W, .type = ARM_CP_NOP },
cea66e91
PM
4918 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4919 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
bf05340c 4920 .access = PL2_W, .type = ARM_CP_NOP },
83ddf975
PM
4921 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4922 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4923 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4924 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
4925 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4926 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4927 .access = PL2_W, .type = ARM_CP_NO_RAW,
4928 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
4929#ifndef CONFIG_USER_ONLY
4930 /* 64 bit address translation operations */
4931 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4932 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4933 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4934 .writefn = ats_write64 },
19525524
PM
4935 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4936 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4937 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4938 .writefn = ats_write64 },
19525524
PM
4939 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4940 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
0710b2fa
PM
4941 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4942 .writefn = ats_write64 },
19525524
PM
4943 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4944 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
0710b2fa
PM
4945 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4946 .writefn = ats_write64 },
2a47df95 4947 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 4948 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
0710b2fa
PM
4949 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4950 .writefn = ats_write64 },
2a47df95 4951 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 4952 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
0710b2fa
PM
4953 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4954 .writefn = ats_write64 },
2a47df95 4955 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 4956 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
0710b2fa
PM
4957 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4958 .writefn = ats_write64 },
2a47df95 4959 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 4960 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
0710b2fa
PM
4961 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4962 .writefn = ats_write64 },
2a47df95
PM
4963 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4964 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4965 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4966 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4967 .writefn = ats_write64 },
2a47df95
PM
4968 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4969 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4970 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4971 .writefn = ats_write64 },
c96fc9b5
EI
4972 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4973 .type = ARM_CP_ALIAS,
4974 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4975 .access = PL1_RW, .resetvalue = 0,
4976 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4977 .writefn = par_write },
19525524 4978#endif
995939a6 4979 /* TLB invalidate last level of translation table walk */
9449fdf6 4980 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
30881b73
RH
4981 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4982 .writefn = tlbimva_is_write },
9449fdf6 4983 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
30881b73 4984 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 4985 .writefn = tlbimvaa_is_write },
9449fdf6 4986 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73
RH
4987 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4988 .writefn = tlbimva_write },
9449fdf6 4989 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73
RH
4990 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4991 .writefn = tlbimvaa_write },
541ef8c2
SS
4992 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
4993 .type = ARM_CP_NO_RAW, .access = PL2_W,
4994 .writefn = tlbimva_hyp_write },
4995 { .name = "TLBIMVALHIS",
4996 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
4997 .type = ARM_CP_NO_RAW, .access = PL2_W,
4998 .writefn = tlbimva_hyp_is_write },
4999 { .name = "TLBIIPAS2",
5000 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
bf05340c 5001 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
5002 { .name = "TLBIIPAS2IS",
5003 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
bf05340c 5004 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
5005 { .name = "TLBIIPAS2L",
5006 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
bf05340c 5007 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
5008 { .name = "TLBIIPAS2LIS",
5009 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
bf05340c 5010 .type = ARM_CP_NOP, .access = PL2_W },
9449fdf6
PM
5011 /* 32 bit cache operations */
5012 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a 5013 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6
PM
5014 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5015 .type = ARM_CP_NOP, .access = PL1_W },
5016 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a 5017 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6 5018 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
38262d8a 5019 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6
PM
5020 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5021 .type = ARM_CP_NOP, .access = PL1_W },
5022 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5023 .type = ARM_CP_NOP, .access = PL1_W },
5024 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e 5025 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5026 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 5027 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5028 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
1bed4d2e 5029 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5030 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 5031 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5032 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
38262d8a 5033 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6 5034 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
1bed4d2e 5035 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5036 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 5037 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5038 /* MMU Domain access control / MPU write buffer control */
0c17d68c 5039 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
84929218 5040 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
5041 .writefn = dacr_write, .raw_writefn = raw_write,
5042 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5043 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 5044 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5045 .type = ARM_CP_ALIAS,
a0618a19 5046 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
5047 .access = PL1_RW,
5048 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 5049 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5050 .type = ARM_CP_ALIAS,
a65f1de9 5051 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5052 .access = PL1_RW,
5053 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
5054 /* We rely on the access checks not allowing the guest to write to the
5055 * state field when SPSel indicates that it's being used as the stack
5056 * pointer.
5057 */
5058 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5059 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5060 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 5061 .type = ARM_CP_ALIAS,
f502cfc2 5062 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
5063 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5064 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5065 .access = PL2_RW, .type = ARM_CP_ALIAS,
884b4dee 5066 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
5067 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5068 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 5069 .type = ARM_CP_NO_RAW,
f502cfc2 5070 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
5071 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5072 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
696ba377
RH
5073 .access = PL2_RW,
5074 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
a4c88675 5075 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
6a43e0b6
PM
5076 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5077 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
696ba377 5078 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6a43e0b6
PM
5079 .writefn = dacr_write, .raw_writefn = raw_write,
5080 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5081 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5082 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
696ba377 5083 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6a43e0b6
PM
5084 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5085 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5086 .type = ARM_CP_ALIAS,
5087 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5088 .access = PL2_RW,
5089 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5090 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5091 .type = ARM_CP_ALIAS,
5092 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5093 .access = PL2_RW,
5094 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5095 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5096 .type = ARM_CP_ALIAS,
5097 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5098 .access = PL2_RW,
5099 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5100 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5101 .type = ARM_CP_ALIAS,
5102 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5103 .access = PL2_RW,
5104 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73
PM
5105 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5106 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5107 .resetvalue = 0,
5108 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5109 { .name = "SDCR", .type = ARM_CP_ALIAS,
5110 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5111 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5112 .writefn = sdcr_write,
5113 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
5114};
5115
d1fb4da2 5116static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
f149e3e8 5117{
2fc0cc0e 5118 ARMCPU *cpu = env_archcpu(env);
d1fb4da2
RH
5119
5120 if (arm_feature(env, ARM_FEATURE_V8)) {
5121 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5122 } else {
5123 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5124 }
f149e3e8
EI
5125
5126 if (arm_feature(env, ARM_FEATURE_EL3)) {
5127 valid_mask &= ~HCR_HCD;
77077a83
JK
5128 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5129 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5130 * However, if we're using the SMC PSCI conduit then QEMU is
5131 * effectively acting like EL3 firmware and so the guest at
5132 * EL2 should retain the ability to prevent EL1 from being
5133 * able to make SMC calls into the ersatz firmware, so in
5134 * that case HCR.TSC should be read/write.
5135 */
f149e3e8
EI
5136 valid_mask &= ~HCR_TSC;
5137 }
d1fb4da2
RH
5138
5139 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5140 if (cpu_isar_feature(aa64_vh, cpu)) {
5141 valid_mask |= HCR_E2H;
5142 }
da3d8b13
RH
5143 if (cpu_isar_feature(aa64_ras, cpu)) {
5144 valid_mask |= HCR_TERR | HCR_TEA;
5145 }
d1fb4da2
RH
5146 if (cpu_isar_feature(aa64_lor, cpu)) {
5147 valid_mask |= HCR_TLOR;
5148 }
5149 if (cpu_isar_feature(aa64_pauth, cpu)) {
5150 valid_mask |= HCR_API | HCR_APK;
5151 }
8ddb300b
RH
5152 if (cpu_isar_feature(aa64_mte, cpu)) {
5153 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5154 }
7cb1e618
RH
5155 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5156 valid_mask |= HCR_ENSCXT;
5157 }
ef682cdb 5158 }
f149e3e8
EI
5159
5160 /* Clear RES0 bits. */
5161 value &= valid_mask;
5162
8ddb300b
RH
5163 /*
5164 * These bits change the MMU setup:
f149e3e8
EI
5165 * HCR_VM enables stage 2 translation
5166 * HCR_PTW forbids certain page-table setups
8ddb300b
RH
5167 * HCR_DC disables stage1 and enables stage2 translation
5168 * HCR_DCT enables tagging on (disabled) stage1 translation
f149e3e8 5169 */
8ddb300b 5170 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT)) {
d10eb08f 5171 tlb_flush(CPU(cpu));
f149e3e8 5172 }
ce4afed8 5173 env->cp15.hcr_el2 = value;
89430fc6
PM
5174
5175 /*
5176 * Updates to VI and VF require us to update the status of
5177 * virtual interrupts, which are the logical OR of these bits
5178 * and the state of the input lines from the GIC. (This requires
5179 * that we have the iothread lock, which is done by marking the
5180 * reginfo structs as ARM_CP_IO.)
5181 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5182 * possible for it to be taken immediately, because VIRQ and
5183 * VFIQ are masked unless running at EL0 or EL1, and HCR
5184 * can only be written at EL2.
5185 */
5186 g_assert(qemu_mutex_iothread_locked());
5187 arm_cpu_update_virq(cpu);
5188 arm_cpu_update_vfiq(cpu);
3c29632f 5189 arm_cpu_update_vserr(cpu);
ce4afed8
PM
5190}
5191
d1fb4da2
RH
5192static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5193{
5194 do_hcr_write(env, value, 0);
5195}
5196
ce4afed8
PM
5197static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5198 uint64_t value)
5199{
5200 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5201 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
d1fb4da2 5202 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
ce4afed8
PM
5203}
5204
5205static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5206 uint64_t value)
5207{
5208 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5209 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
d1fb4da2 5210 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
f149e3e8
EI
5211}
5212
f7778444
RH
5213/*
5214 * Return the effective value of HCR_EL2.
5215 * Bits that are not included here:
5216 * RW (read from SCR_EL3.RW as needed)
5217 */
5218uint64_t arm_hcr_el2_eff(CPUARMState *env)
5219{
5220 uint64_t ret = env->cp15.hcr_el2;
5221
e6ef0169 5222 if (!arm_is_el2_enabled(env)) {
f7778444
RH
5223 /*
5224 * "This register has no effect if EL2 is not enabled in the
5225 * current Security state". This is ARMv8.4-SecEL2 speak for
5226 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5227 *
5228 * Prior to that, the language was "In an implementation that
5229 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5230 * as if this field is 0 for all purposes other than a direct
5231 * read or write access of HCR_EL2". With lots of enumeration
5232 * on a per-field basis. In current QEMU, this is condition
5233 * is arm_is_secure_below_el3.
5234 *
5235 * Since the v8.4 language applies to the entire register, and
5236 * appears to be backward compatible, use that.
5237 */
4990e1d3
RH
5238 return 0;
5239 }
5240
5241 /*
5242 * For a cpu that supports both aarch64 and aarch32, we can set bits
5243 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5244 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5245 */
5246 if (!arm_el_is_aa64(env, 2)) {
5247 uint64_t aa32_valid;
5248
5249 /*
5250 * These bits are up-to-date as of ARMv8.6.
5251 * For HCR, it's easiest to list just the 2 bits that are invalid.
5252 * For HCR2, list those that are valid.
5253 */
5254 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5255 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5256 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5257 ret &= aa32_valid;
5258 }
5259
5260 if (ret & HCR_TGE) {
5261 /* These bits are up-to-date as of ARMv8.6. */
f7778444
RH
5262 if (ret & HCR_E2H) {
5263 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5264 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5265 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4990e1d3
RH
5266 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5267 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5268 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
f7778444
RH
5269 } else {
5270 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5271 }
5272 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5273 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5274 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5275 HCR_TLOR);
5276 }
5277
5278 return ret;
5279}
5280
fc1120a7
PM
5281static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5282 uint64_t value)
5283{
5284 /*
5285 * For A-profile AArch32 EL3, if NSACR.CP10
5286 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5287 */
5288 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5289 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5290 value &= ~(0x3 << 10);
5291 value |= env->cp15.cptr_el[2] & (0x3 << 10);
5292 }
5293 env->cp15.cptr_el[2] = value;
5294}
5295
5296static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5297{
5298 /*
5299 * For A-profile AArch32 EL3, if NSACR.CP10
5300 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5301 */
5302 uint64_t value = env->cp15.cptr_el[2];
5303
5304 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5305 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5306 value |= 0x3 << 10;
5307 }
5308 return value;
5309}
5310
4771cd01 5311static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8 5312 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
89430fc6 5313 .type = ARM_CP_IO,
f149e3e8
EI
5314 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5315 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5316 .writefn = hcr_write },
ce4afed8 5317 { .name = "HCR", .state = ARM_CP_STATE_AA32,
89430fc6 5318 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5319 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5320 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5321 .writefn = hcr_writelow },
831a2fca
PM
5322 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5323 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5324 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3b685ba7 5325 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5326 .type = ARM_CP_ALIAS,
3b685ba7
EI
5327 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5328 .access = PL2_RW,
5329 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
68e78e33 5330 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
f2c30f42
EI
5331 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5332 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
cba517c3 5333 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
63b60551
EI
5334 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5335 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
cba517c3
PM
5336 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5337 .type = ARM_CP_ALIAS,
5338 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5339 .access = PL2_RW,
5340 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
3b685ba7 5341 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5342 .type = ARM_CP_ALIAS,
3b685ba7 5343 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5344 .access = PL2_RW,
5345 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d79e0c06 5346 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5347 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5348 .access = PL2_RW, .writefn = vbar_write,
5349 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5350 .resetvalue = 0 },
884b4dee
GB
5351 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5352 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5353 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 5354 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
5355 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5356 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5357 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
fc1120a7
PM
5358 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5359 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
95f949ac
EI
5360 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5361 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5362 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5363 .resetvalue = 0 },
5364 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5365 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac
EI
5366 .access = PL2_RW, .type = ARM_CP_ALIAS,
5367 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
5368 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5369 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5370 .access = PL2_RW, .type = ARM_CP_CONST,
5371 .resetvalue = 0 },
5372 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
55b53c71 5373 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5374 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5375 .access = PL2_RW, .type = ARM_CP_CONST,
5376 .resetvalue = 0 },
37cd6c24
PM
5377 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5378 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5379 .access = PL2_RW, .type = ARM_CP_CONST,
5380 .resetvalue = 0 },
5381 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5382 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5383 .access = PL2_RW, .type = ARM_CP_CONST,
5384 .resetvalue = 0 },
06ec4c8c
EI
5385 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5386 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
d06dc933
RH
5387 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5388 /* no .raw_writefn or .resetfn needed as we never use mask/base_mask */
06ec4c8c 5389 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
5390 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5391 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5392 .type = ARM_CP_ALIAS,
68e9c2fe
EI
5393 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5394 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5395 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5396 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112
PM
5397 .access = PL2_RW,
5398 /* no .writefn needed as this can't cause an ASID change;
5399 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
5400 */
68e9c2fe 5401 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
5402 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5403 .cp = 15, .opc1 = 6, .crm = 2,
5404 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5405 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5406 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5407 .writefn = vttbr_write },
5408 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5409 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5410 .access = PL2_RW, .writefn = vttbr_write,
5411 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
5412 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5413 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5414 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5415 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
5416 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5417 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5418 .access = PL2_RW, .resetvalue = 0,
5419 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
5420 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5421 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
ed30da8e 5422 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
a57633c0
EI
5423 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5424 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5425 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 5426 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
5427 { .name = "TLBIALLNSNH",
5428 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5429 .type = ARM_CP_NO_RAW, .access = PL2_W,
5430 .writefn = tlbiall_nsnh_write },
5431 { .name = "TLBIALLNSNHIS",
5432 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5433 .type = ARM_CP_NO_RAW, .access = PL2_W,
5434 .writefn = tlbiall_nsnh_is_write },
5435 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5436 .type = ARM_CP_NO_RAW, .access = PL2_W,
5437 .writefn = tlbiall_hyp_write },
5438 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5439 .type = ARM_CP_NO_RAW, .access = PL2_W,
5440 .writefn = tlbiall_hyp_is_write },
5441 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5442 .type = ARM_CP_NO_RAW, .access = PL2_W,
5443 .writefn = tlbimva_hyp_write },
5444 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5445 .type = ARM_CP_NO_RAW, .access = PL2_W,
5446 .writefn = tlbimva_hyp_is_write },
51da9014
EI
5447 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5448 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
696ba377 5449 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 5450 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
5451 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5452 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
696ba377 5453 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 5454 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
5455 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5456 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
696ba377 5457 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75
PM
5458 .writefn = tlbi_aa64_vae2_write },
5459 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5460 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
696ba377 5461 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75 5462 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
5463 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5464 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
696ba377 5465 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 5466 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
5467 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5468 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
696ba377 5469 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75 5470 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 5471#ifndef CONFIG_USER_ONLY
2a47df95
PM
5472 /* Unlike the other EL2-related AT operations, these must
5473 * UNDEF from EL3 if EL2 is not implemented, which is why we
5474 * define them here rather than with the rest of the AT ops.
5475 */
5476 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5477 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5478 .access = PL2_W, .accessfn = at_s1e2_access,
696ba377
RH
5479 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5480 .writefn = ats_write64 },
2a47df95
PM
5481 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5482 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5483 .access = PL2_W, .accessfn = at_s1e2_access,
696ba377
RH
5484 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5485 .writefn = ats_write64 },
14db7fe0
PM
5486 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5487 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5488 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5489 * to behave as if SCR.NS was 1.
5490 */
5491 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5492 .access = PL2_W,
0710b2fa 5493 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
14db7fe0
PM
5494 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5495 .access = PL2_W,
0710b2fa 5496 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
0b6440af
EI
5497 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5498 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5499 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5500 * reset values as IMPDEF. We choose to reset to 3 to comply with
5501 * both ARMv7 and ARMv8.
5502 */
5503 .access = PL2_RW, .resetvalue = 3,
5504 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
5505 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5506 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5507 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5508 .writefn = gt_cntvoff_write,
5509 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5510 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5511 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5512 .writefn = gt_cntvoff_write,
5513 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
5514 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5515 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5516 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5517 .type = ARM_CP_IO, .access = PL2_RW,
5518 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5519 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5520 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5521 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5522 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5523 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5524 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 5525 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
5526 .resetfn = gt_hyp_timer_reset,
5527 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5528 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5529 .type = ARM_CP_IO,
5530 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5531 .access = PL2_RW,
5532 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5533 .resetvalue = 0,
5534 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 5535#endif
21c2dd77
PM
5536 /* The only field of MDCR_EL2 that has a defined architectural reset value
5537 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
5538 */
5539 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
5540 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
5541 .access = PL2_RW, .resetvalue = PMCR_NUM_COUNTERS,
5542 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
59e05530
EI
5543 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5544 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5545 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5546 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5547 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5548 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5549 .access = PL2_RW,
5550 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
5551 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5552 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5553 .access = PL2_RW,
5554 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
5555};
5556
ce4afed8
PM
5557static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5558 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
89430fc6 5559 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5560 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5561 .access = PL2_RW,
5562 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5563 .writefn = hcr_writehigh },
ce4afed8
PM
5564};
5565
e9152ee9
RDC
5566static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5567 bool isread)
5568{
5569 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5570 return CP_ACCESS_OK;
5571 }
5572 return CP_ACCESS_TRAP_UNCATEGORIZED;
5573}
5574
5575static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5576 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5577 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5578 .access = PL2_RW, .accessfn = sel2_access,
5579 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5580 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5581 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5582 .access = PL2_RW, .accessfn = sel2_access,
5583 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
e9152ee9
RDC
5584};
5585
2f027fc5
PM
5586static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5587 bool isread)
5588{
5589 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
926c1b97 5590 * At Secure EL1 it traps to EL3 or EL2.
2f027fc5
PM
5591 */
5592 if (arm_current_el(env) == 3) {
5593 return CP_ACCESS_OK;
5594 }
5595 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
5596 if (env->cp15.scr_el3 & SCR_EEL2) {
5597 return CP_ACCESS_TRAP_EL2;
5598 }
2f027fc5
PM
5599 return CP_ACCESS_TRAP_EL3;
5600 }
5601 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5602 if (isread) {
5603 return CP_ACCESS_OK;
5604 }
5605 return CP_ACCESS_TRAP_UNCATEGORIZED;
5606}
5607
60fb1a87
GB
5608static const ARMCPRegInfo el3_cp_reginfo[] = {
5609 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5610 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5611 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
10d0ef3e 5612 .resetfn = scr_reset, .writefn = scr_write },
f80741d1 5613 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
60fb1a87 5614 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
5615 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5616 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 5617 .writefn = scr_write },
60fb1a87
GB
5618 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5619 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5620 .access = PL3_RW, .resetvalue = 0,
5621 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5622 { .name = "SDER",
5623 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5624 .access = PL3_RW, .resetvalue = 0,
5625 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 5626 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
5627 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5628 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 5629 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
5630 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5631 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
f478847f 5632 .access = PL3_RW, .resetvalue = 0,
7dd8c9af 5633 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
5634 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5635 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c
PM
5636 .access = PL3_RW,
5637 /* no .writefn needed as this can't cause an ASID change;
811595a2
PM
5638 * we must provide a .raw_writefn and .resetfn because we handle
5639 * reset and migration for the AArch32 TTBCR(S), which might be
5640 * using mask and base_mask.
6459b94c 5641 */
811595a2 5642 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write,
11f136ee 5643 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 5644 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5645 .type = ARM_CP_ALIAS,
81547d66
EI
5646 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5647 .access = PL3_RW,
5648 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 5649 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
5650 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5651 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
5652 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5653 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5654 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 5655 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5656 .type = ARM_CP_ALIAS,
81547d66 5657 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5658 .access = PL3_RW,
5659 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
5660 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5661 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5662 .access = PL3_RW, .writefn = vbar_write,
5663 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5664 .resetvalue = 0 },
c6f19164
GB
5665 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5666 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5667 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5668 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
5669 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5670 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5671 .access = PL3_RW, .resetvalue = 0,
5672 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
5673 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5674 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5675 .access = PL3_RW, .type = ARM_CP_CONST,
5676 .resetvalue = 0 },
37cd6c24
PM
5677 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5678 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5679 .access = PL3_RW, .type = ARM_CP_CONST,
5680 .resetvalue = 0 },
5681 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5682 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5683 .access = PL3_RW, .type = ARM_CP_CONST,
5684 .resetvalue = 0 },
43efaa33
PM
5685 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5686 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5687 .access = PL3_W, .type = ARM_CP_NO_RAW,
5688 .writefn = tlbi_aa64_alle3is_write },
5689 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5690 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5691 .access = PL3_W, .type = ARM_CP_NO_RAW,
5692 .writefn = tlbi_aa64_vae3is_write },
5693 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5694 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5695 .access = PL3_W, .type = ARM_CP_NO_RAW,
5696 .writefn = tlbi_aa64_vae3is_write },
5697 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5698 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5699 .access = PL3_W, .type = ARM_CP_NO_RAW,
5700 .writefn = tlbi_aa64_alle3_write },
5701 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5702 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5703 .access = PL3_W, .type = ARM_CP_NO_RAW,
5704 .writefn = tlbi_aa64_vae3_write },
5705 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5706 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5707 .access = PL3_W, .type = ARM_CP_NO_RAW,
5708 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
5709};
5710
e2cce18f
RH
5711#ifndef CONFIG_USER_ONLY
5712/* Test if system register redirection is to occur in the current state. */
5713static bool redirect_for_e2h(CPUARMState *env)
5714{
5715 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5716}
5717
5718static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5719{
5720 CPReadFn *readfn;
5721
5722 if (redirect_for_e2h(env)) {
5723 /* Switch to the saved EL2 version of the register. */
5724 ri = ri->opaque;
5725 readfn = ri->readfn;
5726 } else {
5727 readfn = ri->orig_readfn;
5728 }
5729 if (readfn == NULL) {
5730 readfn = raw_read;
5731 }
5732 return readfn(env, ri);
5733}
5734
5735static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5736 uint64_t value)
5737{
5738 CPWriteFn *writefn;
5739
5740 if (redirect_for_e2h(env)) {
5741 /* Switch to the saved EL2 version of the register. */
5742 ri = ri->opaque;
5743 writefn = ri->writefn;
5744 } else {
5745 writefn = ri->orig_writefn;
5746 }
5747 if (writefn == NULL) {
5748 writefn = raw_write;
5749 }
5750 writefn(env, ri, value);
5751}
5752
5753static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5754{
5755 struct E2HAlias {
5756 uint32_t src_key, dst_key, new_key;
5757 const char *src_name, *dst_name, *new_name;
5758 bool (*feature)(const ARMISARegisters *id);
5759 };
5760
5761#define K(op0, op1, crn, crm, op2) \
5762 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5763
5764 static const struct E2HAlias aliases[] = {
5765 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
5766 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5767 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
5768 "CPACR", "CPTR_EL2", "CPACR_EL12" },
5769 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
5770 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5771 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
5772 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5773 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
5774 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5775 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
5776 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5777 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
5778 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5779 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
5780 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5781 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
5782 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5783 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
5784 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5785 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
5786 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5787 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5788 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5789 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5790 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5791 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5792 "VBAR", "VBAR_EL2", "VBAR_EL12" },
5793 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5794 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5795 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5796 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5797
5798 /*
5799 * Note that redirection of ZCR is mentioned in the description
5800 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5801 * not in the summary table.
5802 */
5803 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
5804 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
5805
4b779ceb
RH
5806 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
5807 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5808
7cb1e618
RH
5809 { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
5810 "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
5811 isar_feature_aa64_scxtnum },
5812
e2cce18f
RH
5813 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5814 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5815 };
5816#undef K
5817
5818 size_t i;
5819
5820 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5821 const struct E2HAlias *a = &aliases[i];
9da35a40 5822 ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
9da35a40 5823 bool ok;
e2cce18f
RH
5824
5825 if (a->feature && !a->feature(&cpu->isar)) {
5826 continue;
5827 }
5828
5860362d
RH
5829 src_reg = g_hash_table_lookup(cpu->cp_regs,
5830 (gpointer)(uintptr_t)a->src_key);
5831 dst_reg = g_hash_table_lookup(cpu->cp_regs,
5832 (gpointer)(uintptr_t)a->dst_key);
e2cce18f
RH
5833 g_assert(src_reg != NULL);
5834 g_assert(dst_reg != NULL);
5835
5836 /* Cross-compare names to detect typos in the keys. */
5837 g_assert(strcmp(src_reg->name, a->src_name) == 0);
5838 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5839
5840 /* None of the core system registers use opaque; we will. */
5841 g_assert(src_reg->opaque == NULL);
5842
5843 /* Create alias before redirection so we dup the right data. */
9da35a40 5844 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
9da35a40
RH
5845
5846 new_reg->name = a->new_name;
5847 new_reg->type |= ARM_CP_ALIAS;
5848 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
5849 new_reg->access &= PL2_RW | PL3_RW;
5850
5860362d
RH
5851 ok = g_hash_table_insert(cpu->cp_regs,
5852 (gpointer)(uintptr_t)a->new_key, new_reg);
9da35a40 5853 g_assert(ok);
e2cce18f
RH
5854
5855 src_reg->opaque = dst_reg;
5856 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5857 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5858 if (!src_reg->raw_readfn) {
5859 src_reg->raw_readfn = raw_read;
5860 }
5861 if (!src_reg->raw_writefn) {
5862 src_reg->raw_writefn = raw_write;
5863 }
5864 src_reg->readfn = el2_e2h_read;
5865 src_reg->writefn = el2_e2h_write;
5866 }
5867}
5868#endif
5869
3f208fd7
PM
5870static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5871 bool isread)
7da845b0 5872{
97475a89
RH
5873 int cur_el = arm_current_el(env);
5874
5875 if (cur_el < 2) {
5876 uint64_t hcr = arm_hcr_el2_eff(env);
5877
5878 if (cur_el == 0) {
5879 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5880 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5881 return CP_ACCESS_TRAP_EL2;
5882 }
5883 } else {
5884 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5885 return CP_ACCESS_TRAP;
5886 }
5887 if (hcr & HCR_TID2) {
5888 return CP_ACCESS_TRAP_EL2;
5889 }
5890 }
5891 } else if (hcr & HCR_TID2) {
5892 return CP_ACCESS_TRAP_EL2;
5893 }
7da845b0 5894 }
630fcd4d
MZ
5895
5896 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5897 return CP_ACCESS_TRAP_EL2;
5898 }
5899
7da845b0
PM
5900 return CP_ACCESS_OK;
5901}
5902
1424ca8d
DM
5903static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
5904 uint64_t value)
5905{
5906 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
5907 * read via a bit in OSLSR_EL1.
5908 */
5909 int oslock;
5910
5911 if (ri->state == ARM_CP_STATE_AA32) {
5912 oslock = (value == 0xC5ACCE55);
5913 } else {
5914 oslock = value & 1;
5915 }
5916
5917 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
5918}
5919
50300698 5920static const ARMCPRegInfo debug_cp_reginfo[] = {
50300698 5921 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
10aae104
PM
5922 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
5923 * unlike DBGDRAR it is never accessible from EL0.
5924 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
5925 * accessor.
50300698
PM
5926 */
5927 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
5928 .access = PL0_R, .accessfn = access_tdra,
5929 .type = ARM_CP_CONST, .resetvalue = 0 },
10aae104
PM
5930 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
5931 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
91b0a238
PM
5932 .access = PL1_R, .accessfn = access_tdra,
5933 .type = ARM_CP_CONST, .resetvalue = 0 },
50300698 5934 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
91b0a238
PM
5935 .access = PL0_R, .accessfn = access_tdra,
5936 .type = ARM_CP_CONST, .resetvalue = 0 },
17a9eb53 5937 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
10aae104
PM
5938 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
5939 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
d6c8cf81 5940 .access = PL1_RW, .accessfn = access_tda,
0e5e8935
PM
5941 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
5942 .resetvalue = 0 },
49a6f3bf
NH
5943 /*
5944 * MDCCSR_EL0[30:29] map to EDSCR[30:29]. Simply RAZ as the external
5945 * Debug Communication Channel is not implemented.
5946 */
5947 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_AA64,
5948 .opc0 = 2, .opc1 = 3, .crn = 0, .crm = 1, .opc2 = 0,
5949 .access = PL0_R, .accessfn = access_tda,
5950 .type = ARM_CP_CONST, .resetvalue = 0 },
5951 /*
5952 * DBGDSCRint[15,12,5:2] map to MDSCR_EL1[15,12,5:2]. Map all bits as
5953 * it is unlikely a guest will care.
5e8b12ff
PM
5954 * We don't implement the configurable EL0 access.
5955 */
49a6f3bf
NH
5956 { .name = "DBGDSCRint", .state = ARM_CP_STATE_AA32,
5957 .cp = 14, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7a0e58fa 5958 .type = ARM_CP_ALIAS,
d6c8cf81 5959 .access = PL1_R, .accessfn = access_tda,
b061a82b 5960 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
10aae104
PM
5961 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
5962 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
1424ca8d 5963 .access = PL1_W, .type = ARM_CP_NO_RAW,
187f678d 5964 .accessfn = access_tdosa,
1424ca8d
DM
5965 .writefn = oslar_write },
5966 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
5967 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
5968 .access = PL1_R, .resetvalue = 10,
187f678d 5969 .accessfn = access_tdosa,
1424ca8d 5970 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
5e8b12ff
PM
5971 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
5972 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
5973 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
187f678d
PM
5974 .access = PL1_RW, .accessfn = access_tdosa,
5975 .type = ARM_CP_NOP },
5e8b12ff
PM
5976 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
5977 * implement vector catch debug events yet.
5978 */
5979 { .name = "DBGVCR",
5980 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
d6c8cf81
PM
5981 .access = PL1_RW, .accessfn = access_tda,
5982 .type = ARM_CP_NOP },
4d2ec4da
PM
5983 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
5984 * to save and restore a 32-bit guest's DBGVCR)
5985 */
5986 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64,
5987 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0,
5988 .access = PL2_RW, .accessfn = access_tda,
696ba377 5989 .type = ARM_CP_NOP | ARM_CP_EL3_NO_EL2_KEEP },
5dbdc434
PM
5990 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications
5991 * Channel but Linux may try to access this register. The 32-bit
5992 * alias is DBGDCCINT.
5993 */
5994 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH,
5995 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
5996 .access = PL1_RW, .accessfn = access_tda,
5997 .type = ARM_CP_NOP },
50300698
PM
5998};
5999
6000static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
6001 /* 64 bit access versions of the (dummy) debug registers */
6002 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
6003 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
6004 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
6005 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
50300698
PM
6006};
6007
58e93b48
RH
6008/*
6009 * Check for traps to RAS registers, which are controlled
6010 * by HCR_EL2.TERR and SCR_EL3.TERR.
6011 */
6012static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6013 bool isread)
6014{
6015 int el = arm_current_el(env);
6016
6017 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6018 return CP_ACCESS_TRAP_EL2;
6019 }
6020 if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6021 return CP_ACCESS_TRAP_EL3;
6022 }
6023 return CP_ACCESS_OK;
6024}
6025
6026static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6027{
6028 int el = arm_current_el(env);
6029
6030 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6031 return env->cp15.vdisr_el2;
6032 }
6033 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6034 return 0; /* RAZ/WI */
6035 }
6036 return env->cp15.disr_el1;
6037}
6038
6039static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6040{
6041 int el = arm_current_el(env);
6042
6043 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6044 env->cp15.vdisr_el2 = val;
6045 return;
6046 }
6047 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6048 return; /* RAZ/WI */
6049 }
6050 env->cp15.disr_el1 = val;
6051}
6052
6053/*
6054 * Minimal RAS implementation with no Error Records.
6055 * Which means that all of the Error Record registers:
6056 * ERXADDR_EL1
6057 * ERXCTLR_EL1
6058 * ERXFR_EL1
6059 * ERXMISC0_EL1
6060 * ERXMISC1_EL1
6061 * ERXMISC2_EL1
6062 * ERXMISC3_EL1
6063 * ERXPFGCDN_EL1 (RASv1p1)
6064 * ERXPFGCTL_EL1 (RASv1p1)
6065 * ERXPFGF_EL1 (RASv1p1)
6066 * ERXSTATUS_EL1
6067 * and
6068 * ERRSELR_EL1
6069 * may generate UNDEFINED, which is the effect we get by not
6070 * listing them at all.
6071 */
6072static const ARMCPRegInfo minimal_ras_reginfo[] = {
6073 { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6074 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6075 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6076 .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6077 { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6078 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6079 .access = PL1_R, .accessfn = access_terr,
6080 .type = ARM_CP_CONST, .resetvalue = 0 },
6081 { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6082 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6083 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6084 { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6085 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6086 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6087};
6088
60eed086
RH
6089/* Return the exception level to which exceptions should be taken
6090 * via SVEAccessTrap. If an exception should be routed through
6091 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should
6092 * take care of raising that exception.
6093 * C.f. the ARM pseudocode function CheckSVEEnabled.
5be5e8ed 6094 */
ced31551 6095int sve_exception_el(CPUARMState *env, int el)
5be5e8ed
RH
6096{
6097#ifndef CONFIG_USER_ONLY
c2ddb7cf
RH
6098 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
6099
6100 if (el <= 1 && (hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
7701cee5
RH
6101 /* Check CPACR.ZEN. */
6102 switch (extract32(env->cp15.cpacr_el1, 16, 2)) {
6103 case 1:
6104 if (el != 0) {
6105 break;
6106 }
6107 /* fall through */
6108 case 0:
6109 case 2:
60eed086 6110 /* route_to_el2 */
c2ddb7cf 6111 return hcr_el2 & HCR_TGE ? 2 : 1;
5be5e8ed 6112 }
5be5e8ed 6113
60eed086 6114 /* Check CPACR.FPEN. */
7701cee5
RH
6115 switch (extract32(env->cp15.cpacr_el1, 20, 2)) {
6116 case 1:
6117 if (el != 0) {
6118 break;
6119 }
6120 /* fall through */
6121 case 0:
6122 case 2:
60eed086 6123 return 0;
5be5e8ed 6124 }
5be5e8ed
RH
6125 }
6126
d5a6fa2d
RH
6127 /*
6128 * CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE).
60eed086 6129 */
d5a6fa2d
RH
6130 if (el <= 2) {
6131 if (hcr_el2 & HCR_E2H) {
6132 /* Check CPTR_EL2.ZEN. */
6133 switch (extract32(env->cp15.cptr_el[2], 16, 2)) {
6134 case 1:
6135 if (el != 0 || !(hcr_el2 & HCR_TGE)) {
6136 break;
6137 }
6138 /* fall through */
6139 case 0:
6140 case 2:
6141 return 2;
6142 }
6143
6144 /* Check CPTR_EL2.FPEN. */
6145 switch (extract32(env->cp15.cptr_el[2], 20, 2)) {
6146 case 1:
6147 if (el == 2 || !(hcr_el2 & HCR_TGE)) {
6148 break;
6149 }
6150 /* fall through */
6151 case 0:
6152 case 2:
6153 return 0;
6154 }
6155 } else if (arm_is_el2_enabled(env)) {
6156 if (env->cp15.cptr_el[2] & CPTR_TZ) {
6157 return 2;
6158 }
6159 if (env->cp15.cptr_el[2] & CPTR_TFP) {
6160 return 0;
6161 }
60eed086 6162 }
5be5e8ed
RH
6163 }
6164
60eed086
RH
6165 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6166 if (arm_feature(env, ARM_FEATURE_EL3)
6167 && !(env->cp15.cptr_el[3] & CPTR_EZ)) {
5be5e8ed
RH
6168 return 3;
6169 }
6170#endif
6171 return 0;
6172}
6173
ce440581 6174uint32_t aarch64_sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len)
0df9142d 6175{
6e553f2a 6176 uint32_t end_len;
0df9142d 6177
dc0bc8e7
RH
6178 start_len = MIN(start_len, ARM_MAX_VQ - 1);
6179 end_len = start_len;
6180
6e553f2a
RH
6181 if (!test_bit(start_len, cpu->sve_vq_map)) {
6182 end_len = find_last_bit(cpu->sve_vq_map, start_len);
6183 assert(end_len < start_len);
6184 }
6185 return end_len;
0df9142d
AJ
6186}
6187
0ab5953b
RH
6188/*
6189 * Given that SVE is enabled, return the vector length for EL.
6190 */
ced31551 6191uint32_t sve_zcr_len_for_el(CPUARMState *env, int el)
0ab5953b 6192{
2fc0cc0e 6193 ARMCPU *cpu = env_archcpu(env);
0ab5953b
RH
6194 uint32_t zcr_len = cpu->sve_max_vq - 1;
6195
63888fa7
RH
6196 if (el <= 1 &&
6197 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
0ab5953b
RH
6198 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]);
6199 }
6a02a732 6200 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
0ab5953b
RH
6201 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]);
6202 }
6a02a732 6203 if (arm_feature(env, ARM_FEATURE_EL3)) {
0ab5953b
RH
6204 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]);
6205 }
0df9142d 6206
ce440581 6207 return aarch64_sve_zcr_get_valid_len(cpu, zcr_len);
0ab5953b
RH
6208}
6209
5be5e8ed
RH
6210static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6211 uint64_t value)
6212{
0ab5953b
RH
6213 int cur_el = arm_current_el(env);
6214 int old_len = sve_zcr_len_for_el(env, cur_el);
6215 int new_len;
6216
5be5e8ed 6217 /* Bits other than [3:0] are RAZ/WI. */
7b351d98 6218 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5be5e8ed 6219 raw_write(env, ri, value & 0xf);
0ab5953b
RH
6220
6221 /*
6222 * Because we arrived here, we know both FP and SVE are enabled;
6223 * otherwise we would have trapped access to the ZCR_ELn register.
6224 */
6225 new_len = sve_zcr_len_for_el(env, cur_el);
6226 if (new_len < old_len) {
6227 aarch64_sve_narrow_vq(env, new_len + 1);
6228 }
5be5e8ed
RH
6229}
6230
60360d82
RH
6231static const ARMCPRegInfo zcr_reginfo[] = {
6232 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6233 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6234 .access = PL1_RW, .type = ARM_CP_SVE,
6235 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6236 .writefn = zcr_write, .raw_writefn = raw_write },
6237 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6238 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6239 .access = PL2_RW, .type = ARM_CP_SVE,
6240 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6241 .writefn = zcr_write, .raw_writefn = raw_write },
6242 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6243 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6244 .access = PL3_RW, .type = ARM_CP_SVE,
6245 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6246 .writefn = zcr_write, .raw_writefn = raw_write },
5be5e8ed
RH
6247};
6248
9ee98ce8
PM
6249void hw_watchpoint_update(ARMCPU *cpu, int n)
6250{
6251 CPUARMState *env = &cpu->env;
6252 vaddr len = 0;
6253 vaddr wvr = env->cp15.dbgwvr[n];
6254 uint64_t wcr = env->cp15.dbgwcr[n];
6255 int mask;
6256 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
6257
6258 if (env->cpu_watchpoint[n]) {
6259 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
6260 env->cpu_watchpoint[n] = NULL;
6261 }
6262
8b7a5bbe 6263 if (!FIELD_EX64(wcr, DBGWCR, E)) {
9ee98ce8
PM
6264 /* E bit clear : watchpoint disabled */
6265 return;
6266 }
6267
8b7a5bbe 6268 switch (FIELD_EX64(wcr, DBGWCR, LSC)) {
9ee98ce8
PM
6269 case 0:
6270 /* LSC 00 is reserved and must behave as if the wp is disabled */
6271 return;
6272 case 1:
6273 flags |= BP_MEM_READ;
6274 break;
6275 case 2:
6276 flags |= BP_MEM_WRITE;
6277 break;
6278 case 3:
6279 flags |= BP_MEM_ACCESS;
6280 break;
6281 }
6282
6283 /* Attempts to use both MASK and BAS fields simultaneously are
6284 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
6285 * thus generating a watchpoint for every byte in the masked region.
6286 */
8b7a5bbe 6287 mask = FIELD_EX64(wcr, DBGWCR, MASK);
9ee98ce8
PM
6288 if (mask == 1 || mask == 2) {
6289 /* Reserved values of MASK; we must act as if the mask value was
6290 * some non-reserved value, or as if the watchpoint were disabled.
6291 * We choose the latter.
6292 */
6293 return;
6294 } else if (mask) {
6295 /* Watchpoint covers an aligned area up to 2GB in size */
6296 len = 1ULL << mask;
6297 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
6298 * whether the watchpoint fires when the unmasked bits match; we opt
6299 * to generate the exceptions.
6300 */
6301 wvr &= ~(len - 1);
6302 } else {
6303 /* Watchpoint covers bytes defined by the byte address select bits */
8b7a5bbe 6304 int bas = FIELD_EX64(wcr, DBGWCR, BAS);
9ee98ce8
PM
6305 int basstart;
6306
9ee98ce8
PM
6307 if (extract64(wvr, 2, 1)) {
6308 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
6309 * ignored, and BAS[3:0] define which bytes to watch.
6310 */
6311 bas &= 0xf;
6312 }
ae1111d4
RH
6313
6314 if (bas == 0) {
6315 /* This must act as if the watchpoint is disabled */
6316 return;
6317 }
6318
9ee98ce8
PM
6319 /* The BAS bits are supposed to be programmed to indicate a contiguous
6320 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
6321 * we fire for each byte in the word/doubleword addressed by the WVR.
6322 * We choose to ignore any non-zero bits after the first range of 1s.
6323 */
6324 basstart = ctz32(bas);
6325 len = cto32(bas >> basstart);
6326 wvr += basstart;
6327 }
6328
6329 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
6330 &env->cpu_watchpoint[n]);
6331}
6332
6333void hw_watchpoint_update_all(ARMCPU *cpu)
6334{
6335 int i;
6336 CPUARMState *env = &cpu->env;
6337
6338 /* Completely clear out existing QEMU watchpoints and our array, to
6339 * avoid possible stale entries following migration load.
6340 */
6341 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
6342 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
6343
6344 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
6345 hw_watchpoint_update(cpu, i);
6346 }
6347}
6348
6349static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6350 uint64_t value)
6351{
2fc0cc0e 6352 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6353 int i = ri->crm;
6354
777ab8d8 6355 /*
9ee98ce8 6356 * Bits [1:0] are RES0.
777ab8d8
RH
6357 *
6358 * It is IMPLEMENTATION DEFINED whether [63:49] ([63:53] with FEAT_LVA)
6359 * are hardwired to the value of bit [48] ([52] with FEAT_LVA), or if
6360 * they contain the value written. It is CONSTRAINED UNPREDICTABLE
6361 * whether the RESS bits are ignored when comparing an address.
6362 *
6363 * Therefore we are allowed to compare the entire register, which lets
6364 * us avoid considering whether or not FEAT_LVA is actually enabled.
9ee98ce8 6365 */
777ab8d8 6366 value &= ~3ULL;
9ee98ce8
PM
6367
6368 raw_write(env, ri, value);
6369 hw_watchpoint_update(cpu, i);
6370}
6371
6372static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6373 uint64_t value)
6374{
2fc0cc0e 6375 ARMCPU *cpu = env_archcpu(env);
9ee98ce8
PM
6376 int i = ri->crm;
6377
6378 raw_write(env, ri, value);
6379 hw_watchpoint_update(cpu, i);
6380}
6381
46747d15
PM
6382void hw_breakpoint_update(ARMCPU *cpu, int n)
6383{
6384 CPUARMState *env = &cpu->env;
6385 uint64_t bvr = env->cp15.dbgbvr[n];
6386 uint64_t bcr = env->cp15.dbgbcr[n];
6387 vaddr addr;
6388 int bt;
6389 int flags = BP_CPU;
6390
6391 if (env->cpu_breakpoint[n]) {
6392 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
6393 env->cpu_breakpoint[n] = NULL;
6394 }
6395
6396 if (!extract64(bcr, 0, 1)) {
6397 /* E bit clear : watchpoint disabled */
6398 return;
6399 }
6400
6401 bt = extract64(bcr, 20, 4);
6402
6403 switch (bt) {
6404 case 4: /* unlinked address mismatch (reserved if AArch64) */
6405 case 5: /* linked address mismatch (reserved if AArch64) */
6406 qemu_log_mask(LOG_UNIMP,
0221c8fd 6407 "arm: address mismatch breakpoint types not implemented\n");
46747d15
PM
6408 return;
6409 case 0: /* unlinked address match */
6410 case 1: /* linked address match */
6411 {
777ab8d8
RH
6412 /*
6413 * Bits [1:0] are RES0.
6414 *
6415 * It is IMPLEMENTATION DEFINED whether bits [63:49]
6416 * ([63:53] for FEAT_LVA) are hardwired to a copy of the sign bit
6417 * of the VA field ([48] or [52] for FEAT_LVA), or whether the
6418 * value is read as written. It is CONSTRAINED UNPREDICTABLE
6419 * whether the RESS bits are ignored when comparing an address.
6420 * Therefore we are allowed to compare the entire register, which
6421 * lets us avoid considering whether FEAT_LVA is actually enabled.
6422 *
6423 * The BAS field is used to allow setting breakpoints on 16-bit
6424 * wide instructions; it is CONSTRAINED UNPREDICTABLE whether
46747d15
PM
6425 * a bp will fire if the addresses covered by the bp and the addresses
6426 * covered by the insn overlap but the insn doesn't start at the
6427 * start of the bp address range. We choose to require the insn and
6428 * the bp to have the same address. The constraints on writing to
6429 * BAS enforced in dbgbcr_write mean we have only four cases:
6430 * 0b0000 => no breakpoint
6431 * 0b0011 => breakpoint on addr
6432 * 0b1100 => breakpoint on addr + 2
6433 * 0b1111 => breakpoint on addr
6434 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
6435 */
6436 int bas = extract64(bcr, 5, 4);
777ab8d8 6437 addr = bvr & ~3ULL;
46747d15
PM
6438 if (bas == 0) {
6439 return;
6440 }
6441 if (bas == 0xc) {
6442 addr += 2;
6443 }
6444 break;
6445 }
6446 case 2: /* unlinked context ID match */
6447 case 8: /* unlinked VMID match (reserved if no EL2) */
6448 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
6449 qemu_log_mask(LOG_UNIMP,
0221c8fd 6450 "arm: unlinked context breakpoint types not implemented\n");
46747d15
PM
6451 return;
6452 case 9: /* linked VMID match (reserved if no EL2) */
6453 case 11: /* linked context ID and VMID match (reserved if no EL2) */
6454 case 3: /* linked context ID match */
6455 default:
6456 /* We must generate no events for Linked context matches (unless
6457 * they are linked to by some other bp/wp, which is handled in
6458 * updates for the linking bp/wp). We choose to also generate no events
6459 * for reserved values.
6460 */
6461 return;
6462 }
6463
6464 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
6465}
6466
6467void hw_breakpoint_update_all(ARMCPU *cpu)
6468{
6469 int i;
6470 CPUARMState *env = &cpu->env;
6471
6472 /* Completely clear out existing QEMU breakpoints and our array, to
6473 * avoid possible stale entries following migration load.
6474 */
6475 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
6476 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
6477
6478 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
6479 hw_breakpoint_update(cpu, i);
6480 }
6481}
6482
6483static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6484 uint64_t value)
6485{
2fc0cc0e 6486 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6487 int i = ri->crm;
6488
6489 raw_write(env, ri, value);
6490 hw_breakpoint_update(cpu, i);
6491}
6492
6493static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6494 uint64_t value)
6495{
2fc0cc0e 6496 ARMCPU *cpu = env_archcpu(env);
46747d15
PM
6497 int i = ri->crm;
6498
6499 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
6500 * copy of BAS[0].
6501 */
6502 value = deposit64(value, 6, 1, extract64(value, 5, 1));
6503 value = deposit64(value, 8, 1, extract64(value, 7, 1));
6504
6505 raw_write(env, ri, value);
6506 hw_breakpoint_update(cpu, i);
6507}
6508
50300698 6509static void define_debug_regs(ARMCPU *cpu)
0b45451e 6510{
50300698
PM
6511 /* Define v7 and v8 architectural debug registers.
6512 * These are just dummy implementations for now.
0b45451e
PM
6513 */
6514 int i;
3ff6fc91 6515 int wrps, brps, ctx_cmps;
54a78718
RH
6516
6517 /*
6518 * The Arm ARM says DBGDIDR is optional and deprecated if EL1 cannot
6519 * use AArch32. Given that bit 15 is RES1, if the value is 0 then
6520 * the register must not exist for this cpu.
6521 */
6522 if (cpu->isar.dbgdidr != 0) {
6523 ARMCPRegInfo dbgdidr = {
6524 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0,
6525 .opc1 = 0, .opc2 = 0,
6526 .access = PL0_R, .accessfn = access_tda,
6527 .type = ARM_CP_CONST, .resetvalue = cpu->isar.dbgdidr,
6528 };
6529 define_one_arm_cp_reg(cpu, &dbgdidr);
6530 }
48eb3ae6 6531
3ff6fc91 6532 /* Note that all these register fields hold "number of Xs minus 1". */
88ce6c6e
PM
6533 brps = arm_num_brps(cpu);
6534 wrps = arm_num_wrps(cpu);
6535 ctx_cmps = arm_num_ctx_cmps(cpu);
3ff6fc91
PM
6536
6537 assert(ctx_cmps <= brps);
48eb3ae6 6538
50300698
PM
6539 define_arm_cp_regs(cpu, debug_cp_reginfo);
6540
6541 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
6542 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
6543 }
6544
88ce6c6e 6545 for (i = 0; i < brps; i++) {
0b45451e 6546 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6547 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
6548 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
d6c8cf81 6549 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6550 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
6551 .writefn = dbgbvr_write, .raw_writefn = raw_write
6552 },
10aae104
PM
6553 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
6554 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
d6c8cf81 6555 .access = PL1_RW, .accessfn = access_tda,
46747d15
PM
6556 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
6557 .writefn = dbgbcr_write, .raw_writefn = raw_write
6558 },
48eb3ae6
PM
6559 };
6560 define_arm_cp_regs(cpu, dbgregs);
6561 }
6562
88ce6c6e 6563 for (i = 0; i < wrps; i++) {
48eb3ae6 6564 ARMCPRegInfo dbgregs[] = {
10aae104
PM
6565 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
6566 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
d6c8cf81 6567 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6568 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
6569 .writefn = dbgwvr_write, .raw_writefn = raw_write
6570 },
10aae104
PM
6571 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
6572 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
d6c8cf81 6573 .access = PL1_RW, .accessfn = access_tda,
9ee98ce8
PM
6574 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
6575 .writefn = dbgwcr_write, .raw_writefn = raw_write
6576 },
0b45451e
PM
6577 };
6578 define_arm_cp_regs(cpu, dbgregs);
6579 }
6580}
6581
24183fb6
PM
6582static void define_pmu_regs(ARMCPU *cpu)
6583{
6584 /*
6585 * v7 performance monitor control register: same implementor
6586 * field as main ID register, and we implement four counters in
6587 * addition to the cycle count register.
6588 */
21c2dd77 6589 unsigned int i, pmcrn = PMCR_NUM_COUNTERS;
24183fb6
PM
6590 ARMCPRegInfo pmcr = {
6591 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6592 .access = PL0_RW,
6593 .type = ARM_CP_IO | ARM_CP_ALIAS,
6594 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6595 .accessfn = pmreg_access, .writefn = pmcr_write,
6596 .raw_writefn = raw_write,
6597 };
6598 ARMCPRegInfo pmcr64 = {
6599 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6600 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6601 .access = PL0_RW, .accessfn = pmreg_access,
6602 .type = ARM_CP_IO,
6603 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
21c2dd77
PM
6604 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT) |
6605 PMCRLC,
24183fb6
PM
6606 .writefn = pmcr_write, .raw_writefn = raw_write,
6607 };
6608 define_one_arm_cp_reg(cpu, &pmcr);
6609 define_one_arm_cp_reg(cpu, &pmcr64);
6610 for (i = 0; i < pmcrn; i++) {
6611 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6612 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6613 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6614 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6615 ARMCPRegInfo pmev_regs[] = {
6616 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6617 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6618 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6619 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
99a50d1a 6620 .accessfn = pmreg_access_xevcntr },
24183fb6
PM
6621 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6622 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
99a50d1a 6623 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
24183fb6
PM
6624 .type = ARM_CP_IO,
6625 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6626 .raw_readfn = pmevcntr_rawread,
6627 .raw_writefn = pmevcntr_rawwrite },
6628 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6629 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6630 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6631 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6632 .accessfn = pmreg_access },
6633 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6634 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6635 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6636 .type = ARM_CP_IO,
6637 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6638 .raw_writefn = pmevtyper_rawwrite },
24183fb6
PM
6639 };
6640 define_arm_cp_regs(cpu, pmev_regs);
6641 g_free(pmevcntr_name);
6642 g_free(pmevcntr_el0_name);
6643 g_free(pmevtyper_name);
6644 g_free(pmevtyper_el0_name);
6645 }
a6179538 6646 if (cpu_isar_feature(aa32_pmu_8_1, cpu)) {
24183fb6
PM
6647 ARMCPRegInfo v81_pmu_regs[] = {
6648 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6649 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6650 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6651 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6652 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6653 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6654 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6655 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
24183fb6
PM
6656 };
6657 define_arm_cp_regs(cpu, v81_pmu_regs);
6658 }
15dd1ebd
PM
6659 if (cpu_isar_feature(any_pmu_8_4, cpu)) {
6660 static const ARMCPRegInfo v84_pmmir = {
6661 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6662 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6663 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6664 .resetvalue = 0
6665 };
6666 define_one_arm_cp_reg(cpu, &v84_pmmir);
6667 }
24183fb6
PM
6668}
6669
96a8b92e
PM
6670/* We don't know until after realize whether there's a GICv3
6671 * attached, and that is what registers the gicv3 sysregs.
6672 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6673 * at runtime.
6674 */
6675static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6676{
2fc0cc0e 6677 ARMCPU *cpu = env_archcpu(env);
8a130a7b 6678 uint64_t pfr1 = cpu->isar.id_pfr1;
96a8b92e
PM
6679
6680 if (env->gicv3state) {
6681 pfr1 |= 1 << 28;
6682 }
6683 return pfr1;
6684}
6685
976b99b6 6686#ifndef CONFIG_USER_ONLY
96a8b92e
PM
6687static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6688{
2fc0cc0e 6689 ARMCPU *cpu = env_archcpu(env);
47576b94 6690 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
96a8b92e
PM
6691
6692 if (env->gicv3state) {
6693 pfr0 |= 1 << 24;
6694 }
6695 return pfr0;
6696}
976b99b6 6697#endif
96a8b92e 6698
2d7137c1 6699/* Shared logic between LORID and the rest of the LOR* registers.
9bd268ba 6700 * Secure state exclusion has already been dealt with.
2d7137c1 6701 */
9bd268ba
RDC
6702static CPAccessResult access_lor_ns(CPUARMState *env,
6703 const ARMCPRegInfo *ri, bool isread)
2d7137c1
RH
6704{
6705 int el = arm_current_el(env);
6706
6707 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6708 return CP_ACCESS_TRAP_EL2;
6709 }
6710 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6711 return CP_ACCESS_TRAP_EL3;
6712 }
6713 return CP_ACCESS_OK;
6714}
6715
2d7137c1
RH
6716static CPAccessResult access_lor_other(CPUARMState *env,
6717 const ARMCPRegInfo *ri, bool isread)
6718{
6719 if (arm_is_secure_below_el3(env)) {
6720 /* Access denied in secure mode. */
6721 return CP_ACCESS_TRAP;
6722 }
9bd268ba 6723 return access_lor_ns(env, ri, isread);
2d7137c1
RH
6724}
6725
d8564ee4
RH
6726/*
6727 * A trivial implementation of ARMv8.1-LOR leaves all of these
6728 * registers fixed at 0, which indicates that there are zero
6729 * supported Limited Ordering regions.
6730 */
6731static const ARMCPRegInfo lor_reginfo[] = {
6732 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6733 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6734 .access = PL1_RW, .accessfn = access_lor_other,
6735 .type = ARM_CP_CONST, .resetvalue = 0 },
6736 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6737 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6738 .access = PL1_RW, .accessfn = access_lor_other,
6739 .type = ARM_CP_CONST, .resetvalue = 0 },
6740 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6741 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6742 .access = PL1_RW, .accessfn = access_lor_other,
6743 .type = ARM_CP_CONST, .resetvalue = 0 },
6744 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6745 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6746 .access = PL1_RW, .accessfn = access_lor_other,
6747 .type = ARM_CP_CONST, .resetvalue = 0 },
6748 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6749 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
9bd268ba 6750 .access = PL1_R, .accessfn = access_lor_ns,
d8564ee4 6751 .type = ARM_CP_CONST, .resetvalue = 0 },
d8564ee4
RH
6752};
6753
967aa94f
RH
6754#ifdef TARGET_AARCH64
6755static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6756 bool isread)
6757{
6758 int el = arm_current_el(env);
6759
6760 if (el < 2 &&
6761 arm_feature(env, ARM_FEATURE_EL2) &&
6762 !(arm_hcr_el2_eff(env) & HCR_APK)) {
6763 return CP_ACCESS_TRAP_EL2;
6764 }
6765 if (el < 3 &&
6766 arm_feature(env, ARM_FEATURE_EL3) &&
6767 !(env->cp15.scr_el3 & SCR_APK)) {
6768 return CP_ACCESS_TRAP_EL3;
6769 }
6770 return CP_ACCESS_OK;
6771}
6772
6773static const ARMCPRegInfo pauth_reginfo[] = {
6774 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6775 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6776 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6777 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
967aa94f
RH
6778 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6779 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6780 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6781 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
967aa94f
RH
6782 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6783 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6784 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6785 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
967aa94f
RH
6786 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6787 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6788 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6789 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
967aa94f
RH
6790 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6791 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6792 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6793 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
967aa94f
RH
6794 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6795 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6796 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6797 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
967aa94f
RH
6798 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6799 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6800 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6801 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
967aa94f
RH
6802 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6803 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6804 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6805 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
967aa94f
RH
6806 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6807 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6808 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6809 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
967aa94f
RH
6810 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6811 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6812 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6813 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
967aa94f 6814};
de390645 6815
84940ed8
RC
6816static const ARMCPRegInfo tlbirange_reginfo[] = {
6817 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
6818 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
6819 .access = PL1_W, .type = ARM_CP_NO_RAW,
6820 .writefn = tlbi_aa64_rvae1is_write },
6821 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
6822 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
6823 .access = PL1_W, .type = ARM_CP_NO_RAW,
6824 .writefn = tlbi_aa64_rvae1is_write },
6825 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
6826 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
6827 .access = PL1_W, .type = ARM_CP_NO_RAW,
6828 .writefn = tlbi_aa64_rvae1is_write },
6829 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
6830 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
6831 .access = PL1_W, .type = ARM_CP_NO_RAW,
6832 .writefn = tlbi_aa64_rvae1is_write },
6833 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
6834 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
6835 .access = PL1_W, .type = ARM_CP_NO_RAW,
6836 .writefn = tlbi_aa64_rvae1is_write },
6837 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
6838 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
6839 .access = PL1_W, .type = ARM_CP_NO_RAW,
6840 .writefn = tlbi_aa64_rvae1is_write },
6841 { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
6842 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
6843 .access = PL1_W, .type = ARM_CP_NO_RAW,
6844 .writefn = tlbi_aa64_rvae1is_write },
6845 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
6846 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
6847 .access = PL1_W, .type = ARM_CP_NO_RAW,
6848 .writefn = tlbi_aa64_rvae1is_write },
6849 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
6850 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
6851 .access = PL1_W, .type = ARM_CP_NO_RAW,
6852 .writefn = tlbi_aa64_rvae1_write },
6853 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
6854 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
6855 .access = PL1_W, .type = ARM_CP_NO_RAW,
6856 .writefn = tlbi_aa64_rvae1_write },
6857 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
6858 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
6859 .access = PL1_W, .type = ARM_CP_NO_RAW,
6860 .writefn = tlbi_aa64_rvae1_write },
6861 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
6862 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
6863 .access = PL1_W, .type = ARM_CP_NO_RAW,
6864 .writefn = tlbi_aa64_rvae1_write },
6865 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
6866 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
6867 .access = PL2_W, .type = ARM_CP_NOP },
6868 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
6869 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
6870 .access = PL2_W, .type = ARM_CP_NOP },
6871 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
6872 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
696ba377 6873 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6874 .writefn = tlbi_aa64_rvae2is_write },
6875 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
6876 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
696ba377 6877 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6878 .writefn = tlbi_aa64_rvae2is_write },
6879 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
6880 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
6881 .access = PL2_W, .type = ARM_CP_NOP },
6882 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
6883 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
6884 .access = PL2_W, .type = ARM_CP_NOP },
6885 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
6886 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
696ba377 6887 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6888 .writefn = tlbi_aa64_rvae2is_write },
6889 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
6890 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
696ba377 6891 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6892 .writefn = tlbi_aa64_rvae2is_write },
6893 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
6894 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
696ba377 6895 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6896 .writefn = tlbi_aa64_rvae2_write },
6897 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
6898 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
696ba377 6899 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6900 .writefn = tlbi_aa64_rvae2_write },
6901 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
6902 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
6903 .access = PL3_W, .type = ARM_CP_NO_RAW,
6904 .writefn = tlbi_aa64_rvae3is_write },
6905 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
6906 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
6907 .access = PL3_W, .type = ARM_CP_NO_RAW,
6908 .writefn = tlbi_aa64_rvae3is_write },
6909 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
6910 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
6911 .access = PL3_W, .type = ARM_CP_NO_RAW,
6912 .writefn = tlbi_aa64_rvae3is_write },
6913 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
6914 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
6915 .access = PL3_W, .type = ARM_CP_NO_RAW,
6916 .writefn = tlbi_aa64_rvae3is_write },
6917 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
6918 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
6919 .access = PL3_W, .type = ARM_CP_NO_RAW,
6920 .writefn = tlbi_aa64_rvae3_write },
6921 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
6922 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
6923 .access = PL3_W, .type = ARM_CP_NO_RAW,
6924 .writefn = tlbi_aa64_rvae3_write },
84940ed8
RC
6925};
6926
7113d618
RC
6927static const ARMCPRegInfo tlbios_reginfo[] = {
6928 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
6929 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
6930 .access = PL1_W, .type = ARM_CP_NO_RAW,
6931 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
6932 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
6933 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
6934 .access = PL1_W, .type = ARM_CP_NO_RAW,
6935 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
6936 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
6937 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
6938 .access = PL1_W, .type = ARM_CP_NO_RAW,
6939 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
6940 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
6941 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
6942 .access = PL1_W, .type = ARM_CP_NO_RAW,
6943 .writefn = tlbi_aa64_vae1is_write },
6944 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
6945 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
6946 .access = PL1_W, .type = ARM_CP_NO_RAW,
6947 .writefn = tlbi_aa64_vae1is_write },
6948 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
6949 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
6950 .access = PL1_W, .type = ARM_CP_NO_RAW,
6951 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
6952 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
6953 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
696ba377 6954 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7113d618 6955 .writefn = tlbi_aa64_alle2is_write },
b7469ef9
IH
6956 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
6957 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
696ba377 6958 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
b7469ef9 6959 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
6960 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
6961 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
6962 .access = PL2_W, .type = ARM_CP_NO_RAW,
6963 .writefn = tlbi_aa64_alle1is_write },
b7469ef9
IH
6964 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
6965 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
696ba377 6966 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
b7469ef9 6967 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
6968 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
6969 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
6970 .access = PL2_W, .type = ARM_CP_NO_RAW,
6971 .writefn = tlbi_aa64_alle1is_write },
6972 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
6973 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
6974 .access = PL2_W, .type = ARM_CP_NOP },
6975 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
6976 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
6977 .access = PL2_W, .type = ARM_CP_NOP },
6978 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6979 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
6980 .access = PL2_W, .type = ARM_CP_NOP },
6981 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6982 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
6983 .access = PL2_W, .type = ARM_CP_NOP },
6984 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
6985 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
6986 .access = PL3_W, .type = ARM_CP_NO_RAW,
6987 .writefn = tlbi_aa64_alle3is_write },
b7469ef9
IH
6988 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
6989 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
6990 .access = PL3_W, .type = ARM_CP_NO_RAW,
6991 .writefn = tlbi_aa64_vae3is_write },
6992 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
6993 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
6994 .access = PL3_W, .type = ARM_CP_NO_RAW,
6995 .writefn = tlbi_aa64_vae3is_write },
7113d618
RC
6996};
6997
de390645
RH
6998static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6999{
7000 Error *err = NULL;
7001 uint64_t ret;
7002
7003 /* Success sets NZCV = 0000. */
7004 env->NF = env->CF = env->VF = 0, env->ZF = 1;
7005
7006 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7007 /*
7008 * ??? Failed, for unknown reasons in the crypto subsystem.
7009 * The best we can do is log the reason and return the
7010 * timed-out indication to the guest. There is no reason
7011 * we know to expect this failure to be transitory, so the
7012 * guest may well hang retrying the operation.
7013 */
7014 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7015 ri->name, error_get_pretty(err));
7016 error_free(err);
7017
7018 env->ZF = 0; /* NZCF = 0100 */
7019 return 0;
7020 }
7021 return ret;
7022}
7023
7024/* We do not support re-seeding, so the two registers operate the same. */
7025static const ARMCPRegInfo rndr_reginfo[] = {
7026 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7027 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7028 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7029 .access = PL0_R, .readfn = rndr_readfn },
7030 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7031 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7032 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7033 .access = PL0_R, .readfn = rndr_readfn },
de390645 7034};
0d57b499
BM
7035
7036#ifndef CONFIG_USER_ONLY
7037static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7038 uint64_t value)
7039{
7040 ARMCPU *cpu = env_archcpu(env);
7041 /* CTR_EL0 System register -> DminLine, bits [19:16] */
7042 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7043 uint64_t vaddr_in = (uint64_t) value;
7044 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7045 void *haddr;
7046 int mem_idx = cpu_mmu_index(env, false);
7047
7048 /* This won't be crossing page boundaries */
7049 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7050 if (haddr) {
7051
7052 ram_addr_t offset;
7053 MemoryRegion *mr;
7054
7055 /* RCU lock is already being held */
7056 mr = memory_region_from_host(haddr, &offset);
7057
7058 if (mr) {
4dfe59d1 7059 memory_region_writeback(mr, offset, dline_size);
0d57b499
BM
7060 }
7061 }
7062}
7063
7064static const ARMCPRegInfo dcpop_reg[] = {
7065 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7066 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7067 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 7068 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
7069};
7070
7071static const ARMCPRegInfo dcpodp_reg[] = {
7072 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7073 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7074 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 7075 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
7076};
7077#endif /*CONFIG_USER_ONLY*/
7078
4b779ceb
RH
7079static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7080 bool isread)
7081{
7082 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7083 return CP_ACCESS_TRAP_EL2;
7084 }
7085
7086 return CP_ACCESS_OK;
7087}
7088
7089static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7090 bool isread)
7091{
7092 int el = arm_current_el(env);
7093
0da067f2 7094 if (el < 2 && arm_is_el2_enabled(env)) {
4301acd7
RH
7095 uint64_t hcr = arm_hcr_el2_eff(env);
7096 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7097 return CP_ACCESS_TRAP_EL2;
7098 }
4b779ceb
RH
7099 }
7100 if (el < 3 &&
7101 arm_feature(env, ARM_FEATURE_EL3) &&
7102 !(env->cp15.scr_el3 & SCR_ATA)) {
7103 return CP_ACCESS_TRAP_EL3;
7104 }
7105 return CP_ACCESS_OK;
7106}
7107
7108static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7109{
7110 return env->pstate & PSTATE_TCO;
7111}
7112
7113static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7114{
7115 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7116}
7117
7118static const ARMCPRegInfo mte_reginfo[] = {
7119 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7120 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7121 .access = PL1_RW, .accessfn = access_mte,
7122 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7123 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7124 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7125 .access = PL1_RW, .accessfn = access_mte,
7126 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7127 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7128 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7129 .access = PL2_RW, .accessfn = access_mte,
7130 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7131 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7132 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7133 .access = PL3_RW,
7134 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7135 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7136 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7137 .access = PL1_RW, .accessfn = access_mte,
7138 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7139 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7140 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7141 .access = PL1_RW, .accessfn = access_mte,
7142 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7143 { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7144 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7145 .access = PL1_R, .accessfn = access_aa64_tid5,
7146 .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7147 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7148 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7149 .type = ARM_CP_NO_RAW,
7150 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
5463df16
RH
7151 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7152 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7153 .type = ARM_CP_NOP, .access = PL1_W,
7154 .accessfn = aa64_cacheop_poc_access },
7155 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7156 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7157 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7158 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7159 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7160 .type = ARM_CP_NOP, .access = PL1_W,
7161 .accessfn = aa64_cacheop_poc_access },
7162 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7163 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7164 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7165 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7166 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7167 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7168 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7169 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7170 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7171 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7172 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7173 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7174 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7175 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7176 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
4b779ceb
RH
7177};
7178
7179static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7180 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7181 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7182 .type = ARM_CP_CONST, .access = PL0_RW, },
4b779ceb 7183};
5463df16
RH
7184
7185static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7186 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7187 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7188 .type = ARM_CP_NOP, .access = PL0_W,
7189 .accessfn = aa64_cacheop_poc_access },
7190 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7191 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7192 .type = ARM_CP_NOP, .access = PL0_W,
7193 .accessfn = aa64_cacheop_poc_access },
7194 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7195 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7196 .type = ARM_CP_NOP, .access = PL0_W,
7197 .accessfn = aa64_cacheop_poc_access },
7198 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7199 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7200 .type = ARM_CP_NOP, .access = PL0_W,
7201 .accessfn = aa64_cacheop_poc_access },
7202 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7203 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7204 .type = ARM_CP_NOP, .access = PL0_W,
7205 .accessfn = aa64_cacheop_poc_access },
7206 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7207 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7208 .type = ARM_CP_NOP, .access = PL0_W,
7209 .accessfn = aa64_cacheop_poc_access },
7210 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7211 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7212 .type = ARM_CP_NOP, .access = PL0_W,
7213 .accessfn = aa64_cacheop_poc_access },
7214 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7215 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7216 .type = ARM_CP_NOP, .access = PL0_W,
7217 .accessfn = aa64_cacheop_poc_access },
eb821168
RH
7218 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7219 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7220 .access = PL0_W, .type = ARM_CP_DC_GVA,
7221#ifndef CONFIG_USER_ONLY
7222 /* Avoid overhead of an access check that always passes in user-mode */
7223 .accessfn = aa64_zva_access,
7224#endif
7225 },
7226 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7227 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7228 .access = PL0_W, .type = ARM_CP_DC_GZVA,
7229#ifndef CONFIG_USER_ONLY
7230 /* Avoid overhead of an access check that always passes in user-mode */
7231 .accessfn = aa64_zva_access,
7232#endif
7233 },
5463df16
RH
7234};
7235
7cb1e618
RH
7236static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7237 bool isread)
7238{
7239 uint64_t hcr = arm_hcr_el2_eff(env);
7240 int el = arm_current_el(env);
7241
7242 if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7243 if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7244 if (hcr & HCR_TGE) {
7245 return CP_ACCESS_TRAP_EL2;
7246 }
7247 return CP_ACCESS_TRAP;
7248 }
7249 } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7250 return CP_ACCESS_TRAP_EL2;
7251 }
7252 if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7253 return CP_ACCESS_TRAP_EL2;
7254 }
7255 if (el < 3
7256 && arm_feature(env, ARM_FEATURE_EL3)
7257 && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7258 return CP_ACCESS_TRAP_EL3;
7259 }
7260 return CP_ACCESS_OK;
7261}
7262
7263static const ARMCPRegInfo scxtnum_reginfo[] = {
7264 { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7265 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7266 .access = PL0_RW, .accessfn = access_scxtnum,
7267 .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7268 { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7269 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7270 .access = PL1_RW, .accessfn = access_scxtnum,
7271 .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7272 { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7273 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7274 .access = PL2_RW, .accessfn = access_scxtnum,
7275 .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7276 { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7277 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7278 .access = PL3_RW,
7279 .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7280};
7281#endif /* TARGET_AARCH64 */
967aa94f 7282
cb570bd3
RH
7283static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7284 bool isread)
7285{
7286 int el = arm_current_el(env);
7287
7288 if (el == 0) {
7289 uint64_t sctlr = arm_sctlr(env, el);
7290 if (!(sctlr & SCTLR_EnRCTX)) {
7291 return CP_ACCESS_TRAP;
7292 }
7293 } else if (el == 1) {
7294 uint64_t hcr = arm_hcr_el2_eff(env);
7295 if (hcr & HCR_NV) {
7296 return CP_ACCESS_TRAP_EL2;
7297 }
7298 }
7299 return CP_ACCESS_OK;
7300}
7301
7302static const ARMCPRegInfo predinv_reginfo[] = {
7303 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7304 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7305 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7306 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7307 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7308 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7309 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7310 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7311 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7312 /*
7313 * Note the AArch32 opcodes have a different OPC1.
7314 */
7315 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7316 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7317 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7318 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7319 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7320 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7321 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7322 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7323 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
cb570bd3
RH
7324};
7325
957e6155
PM
7326static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7327{
7328 /* Read the high 32 bits of the current CCSIDR */
7329 return extract64(ccsidr_read(env, ri), 32, 32);
7330}
7331
7332static const ARMCPRegInfo ccsidr2_reginfo[] = {
7333 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7334 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7335 .access = PL1_R,
7336 .accessfn = access_aa64_tid2,
7337 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
957e6155
PM
7338};
7339
6a4ef4e5
MZ
7340static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7341 bool isread)
7342{
7343 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7344 return CP_ACCESS_TRAP_EL2;
7345 }
7346
7347 return CP_ACCESS_OK;
7348}
7349
7350static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7351 bool isread)
7352{
7353 if (arm_feature(env, ARM_FEATURE_V8)) {
7354 return access_aa64_tid3(env, ri, isread);
7355 }
7356
7357 return CP_ACCESS_OK;
7358}
7359
f96f3d5f
MZ
7360static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7361 bool isread)
7362{
7363 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7364 return CP_ACCESS_TRAP_EL2;
7365 }
7366
7367 return CP_ACCESS_OK;
7368}
7369
8e228c9e
PM
7370static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7371 const ARMCPRegInfo *ri, bool isread)
7372{
7373 /*
7374 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7375 * in v7A, not in v8A.
7376 */
7377 if (!arm_feature(env, ARM_FEATURE_V8) &&
7378 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7379 (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7380 return CP_ACCESS_TRAP_EL2;
7381 }
7382 return CP_ACCESS_OK;
7383}
7384
f96f3d5f
MZ
7385static const ARMCPRegInfo jazelle_regs[] = {
7386 { .name = "JIDR",
7387 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7388 .access = PL1_R, .accessfn = access_jazelle,
7389 .type = ARM_CP_CONST, .resetvalue = 0 },
7390 { .name = "JOSCR",
7391 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7392 .accessfn = access_joscr_jmcr,
f96f3d5f
MZ
7393 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7394 { .name = "JMCR",
7395 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7396 .accessfn = access_joscr_jmcr,
f96f3d5f 7397 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
f96f3d5f
MZ
7398};
7399
52d18727
RH
7400static const ARMCPRegInfo contextidr_el2 = {
7401 .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7402 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7403 .access = PL2_RW,
7404 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7405};
7406
e2a1a461 7407static const ARMCPRegInfo vhe_reginfo[] = {
ed30da8e
RH
7408 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7409 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7410 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7411 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8c94b071
RH
7412#ifndef CONFIG_USER_ONLY
7413 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7414 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7415 .fieldoffset =
7416 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7417 .type = ARM_CP_IO, .access = PL2_RW,
7418 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7419 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7420 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7421 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7422 .resetfn = gt_hv_timer_reset,
7423 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7424 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7425 .type = ARM_CP_IO,
7426 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7427 .access = PL2_RW,
7428 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7429 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
bb5972e4
RH
7430 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7431 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7432 .type = ARM_CP_IO | ARM_CP_ALIAS,
7433 .access = PL2_RW, .accessfn = e2h_access,
7434 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7435 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7436 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7437 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7438 .type = ARM_CP_IO | ARM_CP_ALIAS,
7439 .access = PL2_RW, .accessfn = e2h_access,
7440 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7441 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7442 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7443 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7444 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7445 .access = PL2_RW, .accessfn = e2h_access,
7446 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7447 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7448 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7449 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7450 .access = PL2_RW, .accessfn = e2h_access,
7451 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7452 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7453 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7454 .type = ARM_CP_IO | ARM_CP_ALIAS,
7455 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7456 .access = PL2_RW, .accessfn = e2h_access,
7457 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7458 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7459 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7460 .type = ARM_CP_IO | ARM_CP_ALIAS,
7461 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7462 .access = PL2_RW, .accessfn = e2h_access,
7463 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8c94b071 7464#endif
e2a1a461
RH
7465};
7466
04b07d29
RH
7467#ifndef CONFIG_USER_ONLY
7468static const ARMCPRegInfo ats1e1_reginfo[] = {
7469 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7470 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7471 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7472 .writefn = ats_write64 },
7473 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7474 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7475 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7476 .writefn = ats_write64 },
04b07d29
RH
7477};
7478
7479static const ARMCPRegInfo ats1cp_reginfo[] = {
7480 { .name = "ATS1CPRP",
7481 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7482 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7483 .writefn = ats_write },
7484 { .name = "ATS1CPWP",
7485 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7486 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7487 .writefn = ats_write },
04b07d29
RH
7488};
7489#endif
7490
f6287c24
PM
7491/*
7492 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7493 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7494 * is non-zero, which is never for ARMv7, optionally in ARMv8
7495 * and mandatorily for ARMv8.2 and up.
7496 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7497 * implementation is RAZ/WI we can ignore this detail, as we
7498 * do for ACTLR.
7499 */
7500static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7501 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7502 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
99602377
RH
7503 .access = PL1_RW, .accessfn = access_tacr,
7504 .type = ARM_CP_CONST, .resetvalue = 0 },
f6287c24
PM
7505 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7506 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7507 .access = PL2_RW, .type = ARM_CP_CONST,
7508 .resetvalue = 0 },
f6287c24
PM
7509};
7510
2ceb98c0
PM
7511void register_cp_regs_for_features(ARMCPU *cpu)
7512{
7513 /* Register all the coprocessor registers based on feature bits */
7514 CPUARMState *env = &cpu->env;
7515 if (arm_feature(env, ARM_FEATURE_M)) {
7516 /* M profile has no coprocessor registers */
7517 return;
7518 }
7519
e9aa6c21 7520 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
7521 if (!arm_feature(env, ARM_FEATURE_V8)) {
7522 /* Must go early as it is full of wildcards that may be
7523 * overridden by later definitions.
7524 */
7525 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7526 }
7527
7d57f408 7528 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
7529 /* The ID registers all have impdef reset values */
7530 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
7531 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7532 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7533 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7534 .accessfn = access_aa32_tid3,
8a130a7b 7535 .resetvalue = cpu->isar.id_pfr0 },
96a8b92e
PM
7536 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7537 * the value of the GIC field until after we define these regs.
7538 */
0ff644a7
PM
7539 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7540 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e 7541 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 7542 .accessfn = access_aa32_tid3,
96a8b92e
PM
7543 .readfn = id_pfr1_read,
7544 .writefn = arm_cp_write_ignore },
0ff644a7
PM
7545 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7546 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7547 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7548 .accessfn = access_aa32_tid3,
a6179538 7549 .resetvalue = cpu->isar.id_dfr0 },
0ff644a7
PM
7550 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7551 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7552 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7553 .accessfn = access_aa32_tid3,
8515a092 7554 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
7555 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7556 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7557 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7558 .accessfn = access_aa32_tid3,
10054016 7559 .resetvalue = cpu->isar.id_mmfr0 },
0ff644a7
PM
7560 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7561 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7562 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7563 .accessfn = access_aa32_tid3,
10054016 7564 .resetvalue = cpu->isar.id_mmfr1 },
0ff644a7
PM
7565 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7566 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7567 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7568 .accessfn = access_aa32_tid3,
10054016 7569 .resetvalue = cpu->isar.id_mmfr2 },
0ff644a7
PM
7570 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7571 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7572 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7573 .accessfn = access_aa32_tid3,
10054016 7574 .resetvalue = cpu->isar.id_mmfr3 },
0ff644a7
PM
7575 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7576 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7577 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7578 .accessfn = access_aa32_tid3,
47576b94 7579 .resetvalue = cpu->isar.id_isar0 },
0ff644a7
PM
7580 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7581 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7582 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7583 .accessfn = access_aa32_tid3,
47576b94 7584 .resetvalue = cpu->isar.id_isar1 },
0ff644a7
PM
7585 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7586 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7587 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7588 .accessfn = access_aa32_tid3,
47576b94 7589 .resetvalue = cpu->isar.id_isar2 },
0ff644a7
PM
7590 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7591 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7592 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7593 .accessfn = access_aa32_tid3,
47576b94 7594 .resetvalue = cpu->isar.id_isar3 },
0ff644a7
PM
7595 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7596 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7597 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7598 .accessfn = access_aa32_tid3,
47576b94 7599 .resetvalue = cpu->isar.id_isar4 },
0ff644a7
PM
7600 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7601 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7602 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7603 .accessfn = access_aa32_tid3,
47576b94 7604 .resetvalue = cpu->isar.id_isar5 },
e20d84c1
PM
7605 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7606 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7607 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7608 .accessfn = access_aa32_tid3,
10054016 7609 .resetvalue = cpu->isar.id_mmfr4 },
802abf40 7610 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7611 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7612 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7613 .accessfn = access_aa32_tid3,
47576b94 7614 .resetvalue = cpu->isar.id_isar6 },
8515a092
PM
7615 };
7616 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
7617 define_arm_cp_regs(cpu, v6_cp_reginfo);
7618 } else {
7619 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7620 }
4d31c596
PM
7621 if (arm_feature(env, ARM_FEATURE_V6K)) {
7622 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7623 }
5e5cf9e3 7624 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 7625 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
7626 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7627 }
327dd510
AL
7628 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7629 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7630 }
e9aa6c21 7631 if (arm_feature(env, ARM_FEATURE_V7)) {
776d4e5c 7632 ARMCPRegInfo clidr = {
7da845b0
PM
7633 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7634 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
630fcd4d
MZ
7635 .access = PL1_R, .type = ARM_CP_CONST,
7636 .accessfn = access_aa64_tid2,
7637 .resetvalue = cpu->clidr
776d4e5c 7638 };
776d4e5c 7639 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 7640 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 7641 define_debug_regs(cpu);
24183fb6 7642 define_pmu_regs(cpu);
7d57f408
PM
7643 } else {
7644 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 7645 }
b0d2b7d0 7646 if (arm_feature(env, ARM_FEATURE_V8)) {
e20d84c1
PM
7647 /* AArch64 ID registers, which all have impdef reset values.
7648 * Note that within the ID register ranges the unused slots
7649 * must all RAZ, not UNDEF; future architecture versions may
7650 * define new registers here.
7651 */
e60cef86 7652 ARMCPRegInfo v8_idregs[] = {
976b99b6
AB
7653 /*
7654 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7655 * emulation because we don't know the right value for the
7656 * GIC field until after we define these regs.
96a8b92e 7657 */
e60cef86
PM
7658 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7659 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
976b99b6
AB
7660 .access = PL1_R,
7661#ifdef CONFIG_USER_ONLY
7662 .type = ARM_CP_CONST,
7663 .resetvalue = cpu->isar.id_aa64pfr0
7664#else
7665 .type = ARM_CP_NO_RAW,
6a4ef4e5 7666 .accessfn = access_aa64_tid3,
96a8b92e 7667 .readfn = id_aa64pfr0_read,
976b99b6
AB
7668 .writefn = arm_cp_write_ignore
7669#endif
7670 },
e60cef86
PM
7671 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7672 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7673 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7674 .accessfn = access_aa64_tid3,
47576b94 7675 .resetvalue = cpu->isar.id_aa64pfr1},
e20d84c1
PM
7676 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7677 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7678 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7679 .accessfn = access_aa64_tid3,
e20d84c1
PM
7680 .resetvalue = 0 },
7681 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7682 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7683 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7684 .accessfn = access_aa64_tid3,
e20d84c1 7685 .resetvalue = 0 },
9516d772 7686 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7687 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7688 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7689 .accessfn = access_aa64_tid3,
2dc10fa2 7690 .resetvalue = cpu->isar.id_aa64zfr0 },
e20d84c1
PM
7691 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7692 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7693 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7694 .accessfn = access_aa64_tid3,
e20d84c1
PM
7695 .resetvalue = 0 },
7696 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7697 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7698 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7699 .accessfn = access_aa64_tid3,
e20d84c1
PM
7700 .resetvalue = 0 },
7701 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7702 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7703 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7704 .accessfn = access_aa64_tid3,
e20d84c1 7705 .resetvalue = 0 },
e60cef86
PM
7706 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7707 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7708 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7709 .accessfn = access_aa64_tid3,
2a609df8 7710 .resetvalue = cpu->isar.id_aa64dfr0 },
e60cef86
PM
7711 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7712 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7713 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7714 .accessfn = access_aa64_tid3,
2a609df8 7715 .resetvalue = cpu->isar.id_aa64dfr1 },
e20d84c1
PM
7716 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7717 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7718 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7719 .accessfn = access_aa64_tid3,
e20d84c1
PM
7720 .resetvalue = 0 },
7721 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7722 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7723 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7724 .accessfn = access_aa64_tid3,
e20d84c1 7725 .resetvalue = 0 },
e60cef86
PM
7726 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7727 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7728 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7729 .accessfn = access_aa64_tid3,
e60cef86
PM
7730 .resetvalue = cpu->id_aa64afr0 },
7731 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7732 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7733 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7734 .accessfn = access_aa64_tid3,
e60cef86 7735 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
7736 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7737 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7738 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7739 .accessfn = access_aa64_tid3,
e20d84c1
PM
7740 .resetvalue = 0 },
7741 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7742 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7743 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7744 .accessfn = access_aa64_tid3,
e20d84c1 7745 .resetvalue = 0 },
e60cef86
PM
7746 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7747 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7748 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7749 .accessfn = access_aa64_tid3,
47576b94 7750 .resetvalue = cpu->isar.id_aa64isar0 },
e60cef86
PM
7751 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7752 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7753 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7754 .accessfn = access_aa64_tid3,
47576b94 7755 .resetvalue = cpu->isar.id_aa64isar1 },
e20d84c1
PM
7756 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7757 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7758 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7759 .accessfn = access_aa64_tid3,
e20d84c1
PM
7760 .resetvalue = 0 },
7761 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7762 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7763 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7764 .accessfn = access_aa64_tid3,
e20d84c1
PM
7765 .resetvalue = 0 },
7766 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7767 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7768 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7769 .accessfn = access_aa64_tid3,
e20d84c1
PM
7770 .resetvalue = 0 },
7771 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7772 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7773 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7774 .accessfn = access_aa64_tid3,
e20d84c1
PM
7775 .resetvalue = 0 },
7776 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7777 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7778 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7779 .accessfn = access_aa64_tid3,
e20d84c1
PM
7780 .resetvalue = 0 },
7781 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7782 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7783 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7784 .accessfn = access_aa64_tid3,
e20d84c1 7785 .resetvalue = 0 },
e60cef86
PM
7786 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7787 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7788 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7789 .accessfn = access_aa64_tid3,
3dc91ddb 7790 .resetvalue = cpu->isar.id_aa64mmfr0 },
e60cef86
PM
7791 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7792 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7793 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7794 .accessfn = access_aa64_tid3,
3dc91ddb 7795 .resetvalue = cpu->isar.id_aa64mmfr1 },
64761e10 7796 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7797 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7798 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7799 .accessfn = access_aa64_tid3,
64761e10 7800 .resetvalue = cpu->isar.id_aa64mmfr2 },
e20d84c1
PM
7801 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7802 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7803 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7804 .accessfn = access_aa64_tid3,
e20d84c1
PM
7805 .resetvalue = 0 },
7806 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7807 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7808 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7809 .accessfn = access_aa64_tid3,
e20d84c1
PM
7810 .resetvalue = 0 },
7811 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7812 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7813 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7814 .accessfn = access_aa64_tid3,
e20d84c1
PM
7815 .resetvalue = 0 },
7816 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7817 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7818 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7819 .accessfn = access_aa64_tid3,
e20d84c1
PM
7820 .resetvalue = 0 },
7821 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7822 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7823 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7824 .accessfn = access_aa64_tid3,
e20d84c1 7825 .resetvalue = 0 },
a50c0f51
PM
7826 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7827 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7828 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7829 .accessfn = access_aa64_tid3,
47576b94 7830 .resetvalue = cpu->isar.mvfr0 },
a50c0f51
PM
7831 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7832 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7833 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7834 .accessfn = access_aa64_tid3,
47576b94 7835 .resetvalue = cpu->isar.mvfr1 },
a50c0f51
PM
7836 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7837 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7838 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7839 .accessfn = access_aa64_tid3,
47576b94 7840 .resetvalue = cpu->isar.mvfr2 },
e20d84c1
PM
7841 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7842 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7843 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7844 .accessfn = access_aa64_tid3,
e20d84c1 7845 .resetvalue = 0 },
1d51bc96 7846 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7847 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7848 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7849 .accessfn = access_aa64_tid3,
1d51bc96 7850 .resetvalue = cpu->isar.id_pfr2 },
e20d84c1
PM
7851 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7852 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7853 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7854 .accessfn = access_aa64_tid3,
e20d84c1
PM
7855 .resetvalue = 0 },
7856 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7857 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7858 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7859 .accessfn = access_aa64_tid3,
e20d84c1
PM
7860 .resetvalue = 0 },
7861 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7862 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7863 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7864 .accessfn = access_aa64_tid3,
e20d84c1 7865 .resetvalue = 0 },
4054bfa9
AF
7866 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7867 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7868 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7869 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
4054bfa9
AF
7870 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7871 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7872 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7873 .resetvalue = cpu->pmceid0 },
7874 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7875 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7876 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7877 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
4054bfa9
AF
7878 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7879 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7880 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7881 .resetvalue = cpu->pmceid1 },
e60cef86 7882 };
6c5c0fec 7883#ifdef CONFIG_USER_ONLY
10b0220e 7884 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
6c5c0fec
AB
7885 { .name = "ID_AA64PFR0_EL1",
7886 .exported_bits = 0x000f000f00ff0000,
7887 .fixed_bits = 0x0000000000000011 },
7888 { .name = "ID_AA64PFR1_EL1",
7889 .exported_bits = 0x00000000000000f0 },
d040242e
AB
7890 { .name = "ID_AA64PFR*_EL1_RESERVED",
7891 .is_glob = true },
6c5c0fec
AB
7892 { .name = "ID_AA64ZFR0_EL1" },
7893 { .name = "ID_AA64MMFR0_EL1",
7894 .fixed_bits = 0x00000000ff000000 },
7895 { .name = "ID_AA64MMFR1_EL1" },
d040242e
AB
7896 { .name = "ID_AA64MMFR*_EL1_RESERVED",
7897 .is_glob = true },
6c5c0fec
AB
7898 { .name = "ID_AA64DFR0_EL1",
7899 .fixed_bits = 0x0000000000000006 },
7900 { .name = "ID_AA64DFR1_EL1" },
d040242e
AB
7901 { .name = "ID_AA64DFR*_EL1_RESERVED",
7902 .is_glob = true },
7903 { .name = "ID_AA64AFR*",
7904 .is_glob = true },
6c5c0fec
AB
7905 { .name = "ID_AA64ISAR0_EL1",
7906 .exported_bits = 0x00fffffff0fffff0 },
7907 { .name = "ID_AA64ISAR1_EL1",
7908 .exported_bits = 0x000000f0ffffffff },
d040242e
AB
7909 { .name = "ID_AA64ISAR*_EL1_RESERVED",
7910 .is_glob = true },
6c5c0fec
AB
7911 };
7912 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7913#endif
be8e8128
GB
7914 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7915 if (!arm_feature(env, ARM_FEATURE_EL3) &&
7916 !arm_feature(env, ARM_FEATURE_EL2)) {
7917 ARMCPRegInfo rvbar = {
7918 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7919 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
7920 .access = PL1_R,
7921 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
be8e8128
GB
7922 };
7923 define_one_arm_cp_reg(cpu, &rvbar);
7924 }
e60cef86 7925 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0
PM
7926 define_arm_cp_regs(cpu, v8_cp_reginfo);
7927 }
99a90811
RH
7928
7929 /*
7930 * Register the base EL2 cpregs.
7931 * Pre v8, these registers are implemented only as part of the
7932 * Virtualization Extensions (EL2 present). Beginning with v8,
7933 * if EL2 is missing but EL3 is enabled, mostly these become
7934 * RES0 from EL3, with some specific exceptions.
7935 */
7936 if (arm_feature(env, ARM_FEATURE_EL2)
7937 || (arm_feature(env, ARM_FEATURE_EL3)
7938 && arm_feature(env, ARM_FEATURE_V8))) {
f0d574d6 7939 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
7940 ARMCPRegInfo vpidr_regs[] = {
7941 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7942 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7943 .access = PL2_RW, .accessfn = access_el3_aa32ns,
696ba377
RH
7944 .resetvalue = cpu->midr,
7945 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
36476562 7946 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
7947 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7948 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7949 .access = PL2_RW, .resetvalue = cpu->midr,
696ba377 7950 .type = ARM_CP_EL3_NO_EL2_C_NZ,
731de9e6 7951 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7952 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7953 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7954 .access = PL2_RW, .accessfn = access_el3_aa32ns,
696ba377
RH
7955 .resetvalue = vmpidr_def,
7956 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
36476562 7957 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
7958 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7959 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
696ba377
RH
7960 .access = PL2_RW, .resetvalue = vmpidr_def,
7961 .type = ARM_CP_EL3_NO_EL2_C_NZ,
f0d574d6 7962 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6
EI
7963 };
7964 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 7965 define_arm_cp_regs(cpu, el2_cp_reginfo);
ce4afed8
PM
7966 if (arm_feature(env, ARM_FEATURE_V8)) {
7967 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7968 }
e9152ee9
RDC
7969 if (cpu_isar_feature(aa64_sel2, cpu)) {
7970 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
7971 }
be8e8128
GB
7972 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7973 if (!arm_feature(env, ARM_FEATURE_EL3)) {
7974 ARMCPRegInfo rvbar = {
7975 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7976 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
7977 .access = PL2_R,
7978 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
be8e8128
GB
7979 };
7980 define_one_arm_cp_reg(cpu, &rvbar);
7981 }
3b685ba7 7982 }
99a90811
RH
7983
7984 /* Register the base EL3 cpregs. */
81547d66 7985 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 7986 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
7987 ARMCPRegInfo el3_regs[] = {
7988 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7989 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
7990 .access = PL3_R,
7991 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7992 },
e24fdd23
PM
7993 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7994 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7995 .access = PL3_RW,
7996 .raw_writefn = raw_write, .writefn = sctlr_write,
7997 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7998 .resetvalue = cpu->reset_sctlr },
be8e8128 7999 };
e24fdd23
PM
8000
8001 define_arm_cp_regs(cpu, el3_regs);
81547d66 8002 }
2f027fc5
PM
8003 /* The behaviour of NSACR is sufficiently various that we don't
8004 * try to describe it in a single reginfo:
8005 * if EL3 is 64 bit, then trap to EL3 from S EL1,
8006 * reads as constant 0xc00 from NS EL1 and NS EL2
8007 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8008 * if v7 without EL3, register doesn't exist
8009 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8010 */
8011 if (arm_feature(env, ARM_FEATURE_EL3)) {
8012 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
10b0220e 8013 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8014 .name = "NSACR", .type = ARM_CP_CONST,
8015 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8016 .access = PL1_RW, .accessfn = nsacr_access,
8017 .resetvalue = 0xc00
8018 };
8019 define_one_arm_cp_reg(cpu, &nsacr);
8020 } else {
10b0220e 8021 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8022 .name = "NSACR",
8023 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8024 .access = PL3_RW | PL1_R,
8025 .resetvalue = 0,
8026 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8027 };
8028 define_one_arm_cp_reg(cpu, &nsacr);
8029 }
8030 } else {
8031 if (arm_feature(env, ARM_FEATURE_V8)) {
10b0220e 8032 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8033 .name = "NSACR", .type = ARM_CP_CONST,
8034 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8035 .access = PL1_R,
8036 .resetvalue = 0xc00
8037 };
8038 define_one_arm_cp_reg(cpu, &nsacr);
8039 }
8040 }
8041
452a0955 8042 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
8043 if (arm_feature(env, ARM_FEATURE_V6)) {
8044 /* PMSAv6 not implemented */
8045 assert(arm_feature(env, ARM_FEATURE_V7));
8046 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8047 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8048 } else {
8049 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8050 }
18032bec 8051 } else {
8e5d75c9 8052 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec 8053 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4036b7d1
PM
8054 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
8055 if (cpu_isar_feature(aa32_hpd, cpu)) {
ab638a32
RH
8056 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8057 }
18032bec 8058 }
c326b979
PM
8059 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8060 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8061 }
6cc7a3ae
PM
8062 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8063 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8064 }
4a501606
PM
8065 if (arm_feature(env, ARM_FEATURE_VAPA)) {
8066 define_arm_cp_regs(cpu, vapa_cp_reginfo);
8067 }
c4804214
PM
8068 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8069 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8070 }
8071 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8072 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8073 }
8074 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8075 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8076 }
18032bec
PM
8077 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8078 define_arm_cp_regs(cpu, omap_cp_reginfo);
8079 }
34f90529
PM
8080 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8081 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8082 }
1047b9d7
PM
8083 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8084 define_arm_cp_regs(cpu, xscale_cp_reginfo);
8085 }
8086 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8087 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8088 }
7ac681cf
PM
8089 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8090 define_arm_cp_regs(cpu, lpae_cp_reginfo);
8091 }
873b73c0 8092 if (cpu_isar_feature(aa32_jazelle, cpu)) {
f96f3d5f
MZ
8093 define_arm_cp_regs(cpu, jazelle_regs);
8094 }
7884849c
PM
8095 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
8096 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8097 * be read-only (ie write causes UNDEF exception).
8098 */
8099 {
00a29f3d
PM
8100 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8101 /* Pre-v8 MIDR space.
8102 * Note that the MIDR isn't a simple constant register because
7884849c
PM
8103 * of the TI925 behaviour where writes to another register can
8104 * cause the MIDR value to change.
97ce8d61
PC
8105 *
8106 * Unimplemented registers in the c15 0 0 0 space default to
8107 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8108 * and friends override accordingly.
7884849c
PM
8109 */
8110 { .name = "MIDR",
97ce8d61 8111 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 8112 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 8113 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 8114 .readfn = midr_read,
97ce8d61
PC
8115 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8116 .type = ARM_CP_OVERRIDE },
7884849c
PM
8117 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8118 { .name = "DUMMY",
8119 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8120 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8121 { .name = "DUMMY",
8122 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8123 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8124 { .name = "DUMMY",
8125 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8126 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8127 { .name = "DUMMY",
8128 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8129 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8130 { .name = "DUMMY",
8131 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8132 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7884849c 8133 };
00a29f3d 8134 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
8135 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8136 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
8137 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8138 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8139 .readfn = midr_read },
ac00c79f
SF
8140 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
8141 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8142 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8143 .access = PL1_R, .resetvalue = cpu->midr },
8144 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8145 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8146 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
8147 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8148 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
93fbc983
MZ
8149 .access = PL1_R,
8150 .accessfn = access_aa64_tid1,
8151 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
8152 };
8153 ARMCPRegInfo id_cp_reginfo[] = {
8154 /* These are common to v8 and pre-v8 */
8155 { .name = "CTR",
8156 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
630fcd4d
MZ
8157 .access = PL1_R, .accessfn = ctr_el0_access,
8158 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
00a29f3d
PM
8159 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8160 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8161 .access = PL0_R, .accessfn = ctr_el0_access,
8162 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8163 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8164 { .name = "TCMTR",
8165 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
93fbc983
MZ
8166 .access = PL1_R,
8167 .accessfn = access_aa32_tid1,
8168 .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d 8169 };
8085ce63
PC
8170 /* TLBTR is specific to VMSA */
8171 ARMCPRegInfo id_tlbtr_reginfo = {
8172 .name = "TLBTR",
8173 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
93fbc983
MZ
8174 .access = PL1_R,
8175 .accessfn = access_aa32_tid1,
8176 .type = ARM_CP_CONST, .resetvalue = 0,
8085ce63 8177 };
3281af81
PC
8178 /* MPUIR is specific to PMSA V6+ */
8179 ARMCPRegInfo id_mpuir_reginfo = {
8180 .name = "MPUIR",
8181 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8182 .access = PL1_R, .type = ARM_CP_CONST,
8183 .resetvalue = cpu->pmsav7_dregion << 8
8184 };
10b0220e 8185 static const ARMCPRegInfo crn0_wi_reginfo = {
7884849c
PM
8186 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8187 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8188 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8189 };
6c5c0fec 8190#ifdef CONFIG_USER_ONLY
10b0220e 8191 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
6c5c0fec
AB
8192 { .name = "MIDR_EL1",
8193 .exported_bits = 0x00000000ffffffff },
8194 { .name = "REVIDR_EL1" },
6c5c0fec
AB
8195 };
8196 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8197#endif
7884849c
PM
8198 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8199 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5809ac57 8200 size_t i;
7884849c 8201 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
8202 * whole space. Then update the specific ID registers to allow write
8203 * access, so that they ignore writes rather than causing them to
8204 * UNDEF.
7884849c
PM
8205 */
8206 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
5809ac57
RH
8207 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8208 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
00a29f3d 8209 }
5809ac57
RH
8210 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8211 id_cp_reginfo[i].access = PL1_RW;
7884849c 8212 }
10006112 8213 id_mpuir_reginfo.access = PL1_RW;
3281af81 8214 id_tlbtr_reginfo.access = PL1_RW;
7884849c 8215 }
00a29f3d
PM
8216 if (arm_feature(env, ARM_FEATURE_V8)) {
8217 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8218 } else {
8219 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8220 }
a703eda1 8221 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 8222 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 8223 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
8224 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8225 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 8226 }
7884849c
PM
8227 }
8228
97ce8d61 8229 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
52264166
AB
8230 ARMCPRegInfo mpidr_cp_reginfo[] = {
8231 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8232 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8233 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
52264166
AB
8234 };
8235#ifdef CONFIG_USER_ONLY
10b0220e 8236 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
52264166
AB
8237 { .name = "MPIDR_EL1",
8238 .fixed_bits = 0x0000000080000000 },
52264166
AB
8239 };
8240 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8241#endif
97ce8d61
PC
8242 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8243 }
8244
2771db27 8245 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
8246 ARMCPRegInfo auxcr_reginfo[] = {
8247 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8248 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
99602377
RH
8249 .access = PL1_RW, .accessfn = access_tacr,
8250 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
834a6c69
PM
8251 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8252 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8253 .access = PL2_RW, .type = ARM_CP_CONST,
8254 .resetvalue = 0 },
8255 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8256 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8257 .access = PL3_RW, .type = ARM_CP_CONST,
8258 .resetvalue = 0 },
2771db27 8259 };
834a6c69 8260 define_arm_cp_regs(cpu, auxcr_reginfo);
f6287c24
PM
8261 if (cpu_isar_feature(aa32_ac2, cpu)) {
8262 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
0e0456ab 8263 }
2771db27
PM
8264 }
8265
d8ba780b 8266 if (arm_feature(env, ARM_FEATURE_CBAR)) {
d56974af
LM
8267 /*
8268 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8269 * There are two flavours:
8270 * (1) older 32-bit only cores have a simple 32-bit CBAR
8271 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8272 * 32-bit register visible to AArch32 at a different encoding
8273 * to the "flavour 1" register and with the bits rearranged to
8274 * be able to squash a 64-bit address into the 32-bit view.
8275 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8276 * in future if we support AArch32-only configs of some of the
8277 * AArch64 cores we might need to add a specific feature flag
8278 * to indicate cores with "flavour 2" CBAR.
8279 */
f318cec6
PM
8280 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8281 /* 32 bit view is [31:18] 0...0 [43:32]. */
8282 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8283 | extract64(cpu->reset_cbar, 32, 12);
8284 ARMCPRegInfo cbar_reginfo[] = {
8285 { .name = "CBAR",
8286 .type = ARM_CP_CONST,
d56974af
LM
8287 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8288 .access = PL1_R, .resetvalue = cbar32 },
f318cec6
PM
8289 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8290 .type = ARM_CP_CONST,
8291 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
d56974af 8292 .access = PL1_R, .resetvalue = cpu->reset_cbar },
f318cec6
PM
8293 };
8294 /* We don't implement a r/w 64 bit CBAR currently */
8295 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8296 define_arm_cp_regs(cpu, cbar_reginfo);
8297 } else {
8298 ARMCPRegInfo cbar = {
8299 .name = "CBAR",
8300 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8301 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8302 .fieldoffset = offsetof(CPUARMState,
8303 cp15.c15_config_base_address)
8304 };
8305 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8306 cbar.access = PL1_R;
8307 cbar.fieldoffset = 0;
8308 cbar.type = ARM_CP_CONST;
8309 }
8310 define_one_arm_cp_reg(cpu, &cbar);
8311 }
d8ba780b
PC
8312 }
8313
91db4642 8314 if (arm_feature(env, ARM_FEATURE_VBAR)) {
10b0220e 8315 static const ARMCPRegInfo vbar_cp_reginfo[] = {
91db4642
CLG
8316 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8317 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8318 .access = PL1_RW, .writefn = vbar_write,
8319 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8320 offsetof(CPUARMState, cp15.vbar_ns) },
8321 .resetvalue = 0 },
91db4642
CLG
8322 };
8323 define_arm_cp_regs(cpu, vbar_cp_reginfo);
8324 }
8325
2771db27
PM
8326 /* Generic registers whose values depend on the implementation */
8327 {
8328 ARMCPRegInfo sctlr = {
5ebafdf3 8329 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9 8330 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
84929218 8331 .access = PL1_RW, .accessfn = access_tvm_trvm,
137feaa9
FA
8332 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8333 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
8334 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8335 .raw_writefn = raw_write,
2771db27
PM
8336 };
8337 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8338 /* Normally we would always end the TB on an SCTLR write, but Linux
8339 * arch/arm/mach-pxa/sleep.S expects two instructions following
8340 * an MMU enable to execute from cache. Imitate this behaviour.
8341 */
8342 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8343 }
8344 define_one_arm_cp_reg(cpu, &sctlr);
8345 }
5be5e8ed 8346
2d7137c1 8347 if (cpu_isar_feature(aa64_lor, cpu)) {
2d7137c1
RH
8348 define_arm_cp_regs(cpu, lor_reginfo);
8349 }
220f508f
RH
8350 if (cpu_isar_feature(aa64_pan, cpu)) {
8351 define_one_arm_cp_reg(cpu, &pan_reginfo);
8352 }
04b07d29
RH
8353#ifndef CONFIG_USER_ONLY
8354 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8355 define_arm_cp_regs(cpu, ats1e1_reginfo);
8356 }
8357 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8358 define_arm_cp_regs(cpu, ats1cp_reginfo);
8359 }
8360#endif
9eeb7a1c
RH
8361 if (cpu_isar_feature(aa64_uao, cpu)) {
8362 define_one_arm_cp_reg(cpu, &uao_reginfo);
8363 }
2d7137c1 8364
dc8b1853
RC
8365 if (cpu_isar_feature(aa64_dit, cpu)) {
8366 define_one_arm_cp_reg(cpu, &dit_reginfo);
8367 }
f2f68a78
RC
8368 if (cpu_isar_feature(aa64_ssbs, cpu)) {
8369 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8370 }
58e93b48
RH
8371 if (cpu_isar_feature(any_ras, cpu)) {
8372 define_arm_cp_regs(cpu, minimal_ras_reginfo);
8373 }
dc8b1853 8374
52d18727
RH
8375 if (cpu_isar_feature(aa64_vh, cpu) ||
8376 cpu_isar_feature(aa64_debugv8p2, cpu)) {
8377 define_one_arm_cp_reg(cpu, &contextidr_el2);
8378 }
e2a1a461
RH
8379 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8380 define_arm_cp_regs(cpu, vhe_reginfo);
8381 }
8382
cd208a1c 8383 if (cpu_isar_feature(aa64_sve, cpu)) {
60360d82 8384 define_arm_cp_regs(cpu, zcr_reginfo);
5be5e8ed 8385 }
967aa94f
RH
8386
8387#ifdef TARGET_AARCH64
8388 if (cpu_isar_feature(aa64_pauth, cpu)) {
8389 define_arm_cp_regs(cpu, pauth_reginfo);
8390 }
de390645
RH
8391 if (cpu_isar_feature(aa64_rndr, cpu)) {
8392 define_arm_cp_regs(cpu, rndr_reginfo);
8393 }
84940ed8
RC
8394 if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8395 define_arm_cp_regs(cpu, tlbirange_reginfo);
8396 }
7113d618
RC
8397 if (cpu_isar_feature(aa64_tlbios, cpu)) {
8398 define_arm_cp_regs(cpu, tlbios_reginfo);
8399 }
0d57b499
BM
8400#ifndef CONFIG_USER_ONLY
8401 /* Data Cache clean instructions up to PoP */
8402 if (cpu_isar_feature(aa64_dcpop, cpu)) {
8403 define_one_arm_cp_reg(cpu, dcpop_reg);
8404
8405 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8406 define_one_arm_cp_reg(cpu, dcpodp_reg);
8407 }
8408 }
8409#endif /*CONFIG_USER_ONLY*/
4b779ceb
RH
8410
8411 /*
8412 * If full MTE is enabled, add all of the system registers.
8413 * If only "instructions available at EL0" are enabled,
8414 * then define only a RAZ/WI version of PSTATE.TCO.
8415 */
8416 if (cpu_isar_feature(aa64_mte, cpu)) {
8417 define_arm_cp_regs(cpu, mte_reginfo);
5463df16 8418 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb
RH
8419 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8420 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
5463df16 8421 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb 8422 }
7cb1e618
RH
8423
8424 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8425 define_arm_cp_regs(cpu, scxtnum_reginfo);
8426 }
967aa94f 8427#endif
cb570bd3 8428
22e57073 8429 if (cpu_isar_feature(any_predinv, cpu)) {
cb570bd3
RH
8430 define_arm_cp_regs(cpu, predinv_reginfo);
8431 }
e2cce18f 8432
957e6155
PM
8433 if (cpu_isar_feature(any_ccidx, cpu)) {
8434 define_arm_cp_regs(cpu, ccsidr2_reginfo);
8435 }
8436
e2cce18f
RH
8437#ifndef CONFIG_USER_ONLY
8438 /*
8439 * Register redirections and aliases must be done last,
8440 * after the registers from the other extensions have been defined.
8441 */
8442 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8443 define_arm_vh_e2h_redirects_aliases(cpu);
8444 }
8445#endif
2ceb98c0
PM
8446}
8447
777dc784
PM
8448/* Sort alphabetically by type name, except for "any". */
8449static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 8450{
777dc784
PM
8451 ObjectClass *class_a = (ObjectClass *)a;
8452 ObjectClass *class_b = (ObjectClass *)b;
8453 const char *name_a, *name_b;
5adb4839 8454
777dc784
PM
8455 name_a = object_class_get_name(class_a);
8456 name_b = object_class_get_name(class_b);
51492fd1 8457 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 8458 return 1;
51492fd1 8459 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
8460 return -1;
8461 } else {
8462 return strcmp(name_a, name_b);
5adb4839
PB
8463 }
8464}
8465
777dc784 8466static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 8467{
777dc784 8468 ObjectClass *oc = data;
51492fd1
AF
8469 const char *typename;
8470 char *name;
3371d272 8471
51492fd1
AF
8472 typename = object_class_get_name(oc);
8473 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
0442428a 8474 qemu_printf(" %s\n", name);
51492fd1 8475 g_free(name);
777dc784
PM
8476}
8477
0442428a 8478void arm_cpu_list(void)
777dc784 8479{
777dc784
PM
8480 GSList *list;
8481
8482 list = object_class_get_list(TYPE_ARM_CPU, false);
8483 list = g_slist_sort(list, arm_cpu_list_compare);
0442428a
MA
8484 qemu_printf("Available CPUs:\n");
8485 g_slist_foreach(list, arm_cpu_list_entry, NULL);
777dc784 8486 g_slist_free(list);
40f137e1
PB
8487}
8488
78027bb6
CR
8489static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8490{
8491 ObjectClass *oc = data;
8492 CpuDefinitionInfoList **cpu_list = user_data;
78027bb6
CR
8493 CpuDefinitionInfo *info;
8494 const char *typename;
8495
8496 typename = object_class_get_name(oc);
8497 info = g_malloc0(sizeof(*info));
8498 info->name = g_strndup(typename,
8499 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 8500 info->q_typename = g_strdup(typename);
78027bb6 8501
54aa3de7 8502 QAPI_LIST_PREPEND(*cpu_list, info);
78027bb6
CR
8503}
8504
25a9d6ca 8505CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
78027bb6
CR
8506{
8507 CpuDefinitionInfoList *cpu_list = NULL;
8508 GSList *list;
8509
8510 list = object_class_get_list(TYPE_ARM_CPU, false);
8511 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8512 g_slist_free(list);
8513
8514 return cpu_list;
8515}
8516
1859f8c3
RH
8517/*
8518 * Private utility function for define_one_arm_cp_reg_with_opaque():
8519 * add a single reginfo struct to the hash table.
8520 */
6e6efd61 8521static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
cbe64585
RH
8522 void *opaque, CPState state,
8523 CPSecureState secstate,
9c513e78
AB
8524 int crm, int opc1, int opc2,
8525 const char *name)
6e6efd61 8526{
696ba377 8527 CPUARMState *env = &cpu->env;
5860362d 8528 uint32_t key;
c27f5d3a 8529 ARMCPRegInfo *r2;
4c8c4541
RH
8530 bool is64 = r->type & ARM_CP_64BIT;
8531 bool ns = secstate & ARM_CP_SECSTATE_NS;
cac65299 8532 int cp = r->cp;
c27f5d3a 8533 size_t name_len;
696ba377 8534 bool make_const;
c27f5d3a 8535
cac65299
RH
8536 switch (state) {
8537 case ARM_CP_STATE_AA32:
8538 /* We assume it is a cp15 register if the .cp field is left unset. */
8539 if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
8540 cp = 15;
8541 }
8542 key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
8543 break;
8544 case ARM_CP_STATE_AA64:
8545 /*
8546 * To allow abbreviation of ARMCPRegInfo definitions, we treat
8547 * cp == 0 as equivalent to the value for "standard guest-visible
8548 * sysreg". STATE_BOTH definitions are also always "standard sysreg"
8549 * in their AArch64 view (the .cp value may be non-zero for the
8550 * benefit of the AArch32 view).
8551 */
8552 if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8553 cp = CP_REG_ARM64_SYSREG_CP;
8554 }
8555 key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
8556 break;
8557 default:
8558 g_assert_not_reached();
8559 }
8560
dc44545b
RH
8561 /* Overriding of an existing definition must be explicitly requested. */
8562 if (!(r->type & ARM_CP_OVERRIDE)) {
8563 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
8564 if (oldreg) {
8565 assert(oldreg->type & ARM_CP_OVERRIDE);
8566 }
8567 }
8568
696ba377
RH
8569 /*
8570 * Eliminate registers that are not present because the EL is missing.
8571 * Doing this here makes it easier to put all registers for a given
8572 * feature into the same ARMCPRegInfo array and define them all at once.
8573 */
8574 make_const = false;
8575 if (arm_feature(env, ARM_FEATURE_EL3)) {
8576 /*
8577 * An EL2 register without EL2 but with EL3 is (usually) RES0.
8578 * See rule RJFFP in section D1.1.3 of DDI0487H.a.
8579 */
8580 int min_el = ctz32(r->access) / 2;
8581 if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
8582 if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
8583 return;
8584 }
8585 make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
8586 }
8587 } else {
8588 CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
8589 ? PL2_RW : PL1_RW);
8590 if ((r->access & max_el) == 0) {
8591 return;
8592 }
8593 }
8594
c27f5d3a
RH
8595 /* Combine cpreg and name into one allocation. */
8596 name_len = strlen(name) + 1;
8597 r2 = g_malloc(sizeof(*r2) + name_len);
8598 *r2 = *r;
8599 r2->name = memcpy(r2 + 1, name, name_len);
3f3c82a5 8600
cc946d96
RH
8601 /*
8602 * Update fields to match the instantiation, overwiting wildcards
8603 * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
3f3c82a5 8604 */
cc946d96
RH
8605 r2->cp = cp;
8606 r2->crm = crm;
8607 r2->opc1 = opc1;
8608 r2->opc2 = opc2;
8609 r2->state = state;
3f3c82a5 8610 r2->secure = secstate;
cc946d96
RH
8611 if (opaque) {
8612 r2->opaque = opaque;
8613 }
3f3c82a5 8614
696ba377
RH
8615 if (make_const) {
8616 /* This should not have been a very special register to begin. */
8617 int old_special = r2->type & ARM_CP_SPECIAL_MASK;
8618 assert(old_special == 0 || old_special == ARM_CP_NOP);
1859f8c3 8619 /*
696ba377
RH
8620 * Set the special function to CONST, retaining the other flags.
8621 * This is important for e.g. ARM_CP_SVE so that we still
8622 * take the SVE trap if CPTR_EL3.EZ == 0.
f5a0a5a5 8623 */
696ba377
RH
8624 r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
8625 /*
8626 * Usually, these registers become RES0, but there are a few
8627 * special cases like VPIDR_EL2 which have a constant non-zero
8628 * value with writes ignored.
8629 */
8630 if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
8631 r2->resetvalue = 0;
8632 }
8633 /*
8634 * ARM_CP_CONST has precedence, so removing the callbacks and
8635 * offsets are not strictly necessary, but it is potentially
8636 * less confusing to debug later.
8637 */
8638 r2->readfn = NULL;
8639 r2->writefn = NULL;
8640 r2->raw_readfn = NULL;
8641 r2->raw_writefn = NULL;
8642 r2->resetfn = NULL;
8643 r2->fieldoffset = 0;
8644 r2->bank_fieldoffsets[0] = 0;
8645 r2->bank_fieldoffsets[1] = 0;
8646 } else {
8647 bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
3f3c82a5 8648
10748a96 8649 if (isbanked) {
1859f8c3 8650 /*
696ba377
RH
8651 * Register is banked (using both entries in array).
8652 * Overwriting fieldoffset as the array is only used to define
8653 * banked registers but later only fieldoffset is used.
3f3c82a5 8654 */
696ba377
RH
8655 r2->fieldoffset = r->bank_fieldoffsets[ns];
8656 }
8657 if (state == ARM_CP_STATE_AA32) {
8658 if (isbanked) {
8659 /*
8660 * If the register is banked then we don't need to migrate or
8661 * reset the 32-bit instance in certain cases:
8662 *
8663 * 1) If the register has both 32-bit and 64-bit instances
8664 * then we can count on the 64-bit instance taking care
8665 * of the non-secure bank.
8666 * 2) If ARMv8 is enabled then we can count on a 64-bit
8667 * version taking care of the secure bank. This requires
8668 * that separate 32 and 64-bit definitions are provided.
8669 */
8670 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8671 (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
8672 r2->type |= ARM_CP_ALIAS;
8673 }
8674 } else if ((secstate != r->secure) && !ns) {
8675 /*
8676 * The register is not banked so we only want to allow
8677 * migration of the non-secure instance.
8678 */
7a0e58fa 8679 r2->type |= ARM_CP_ALIAS;
3f3c82a5 8680 }
3f3c82a5 8681
696ba377
RH
8682 if (HOST_BIG_ENDIAN &&
8683 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
8684 r2->fieldoffset += sizeof(uint32_t);
8685 }
3f3c82a5 8686 }
f5a0a5a5 8687 }
cc946d96 8688
1859f8c3
RH
8689 /*
8690 * By convention, for wildcarded registers only the first
6e6efd61 8691 * entry is used for migration; the others are marked as
7a0e58fa 8692 * ALIAS so we don't try to transfer the register
6e6efd61 8693 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 8694 * never migratable and not even raw-accessible.
6e6efd61 8695 */
696ba377 8696 if (r2->type & ARM_CP_SPECIAL_MASK) {
7a0e58fa
PM
8697 r2->type |= ARM_CP_NO_RAW;
8698 }
8699 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
8700 ((r->opc1 == CP_ANY) && opc1 != 0) ||
8701 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 8702 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
8703 }
8704
1859f8c3
RH
8705 /*
8706 * Check that raw accesses are either forbidden or handled. Note that
375421cc
PM
8707 * we can't assert this earlier because the setup of fieldoffset for
8708 * banked registers has to be done first.
8709 */
8710 if (!(r2->type & ARM_CP_NO_RAW)) {
8711 assert(!raw_accessors_invalid(r2));
8712 }
8713
5860362d 8714 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
6e6efd61
PM
8715}
8716
8717
4b6a83fb
PM
8718void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8719 const ARMCPRegInfo *r, void *opaque)
8720{
8721 /* Define implementations of coprocessor registers.
8722 * We store these in a hashtable because typically
8723 * there are less than 150 registers in a space which
8724 * is 16*16*16*8*8 = 262144 in size.
8725 * Wildcarding is supported for the crm, opc1 and opc2 fields.
8726 * If a register is defined twice then the second definition is
8727 * used, so this can be used to define some generic registers and
8728 * then override them with implementation specific variations.
8729 * At least one of the original and the second definition should
8730 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8731 * against accidental use.
f5a0a5a5
PM
8732 *
8733 * The state field defines whether the register is to be
8734 * visible in the AArch32 or AArch64 execution state. If the
8735 * state is set to ARM_CP_STATE_BOTH then we synthesise a
8736 * reginfo structure for the AArch32 view, which sees the lower
8737 * 32 bits of the 64 bit register.
8738 *
8739 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8740 * be wildcarded. AArch64 registers are always considered to be 64
8741 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8742 * the register, if any.
4b6a83fb 8743 */
d95101d6 8744 int crm, opc1, opc2;
4b6a83fb
PM
8745 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8746 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8747 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8748 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8749 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8750 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
d95101d6
RH
8751 CPState state;
8752
4b6a83fb
PM
8753 /* 64 bit registers have only CRm and Opc1 fields */
8754 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
8755 /* op0 only exists in the AArch64 encodings */
8756 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8757 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8758 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
cd8be50e
PM
8759 /*
8760 * This API is only for Arm's system coprocessors (14 and 15) or
8761 * (M-profile or v7A-and-earlier only) for implementation defined
8762 * coprocessors in the range 0..7. Our decode assumes this, since
8763 * 8..13 can be used for other insns including VFP and Neon. See
8764 * valid_cp() in translate.c. Assert here that we haven't tried
8765 * to use an invalid coprocessor number.
8766 */
8767 switch (r->state) {
8768 case ARM_CP_STATE_BOTH:
8769 /* 0 has a special meaning, but otherwise the same rules as AA32. */
8770 if (r->cp == 0) {
8771 break;
8772 }
8773 /* fall through */
8774 case ARM_CP_STATE_AA32:
8775 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8776 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8777 assert(r->cp >= 14 && r->cp <= 15);
8778 } else {
8779 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8780 }
8781 break;
8782 case ARM_CP_STATE_AA64:
8783 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8784 break;
8785 default:
8786 g_assert_not_reached();
8787 }
f5a0a5a5
PM
8788 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8789 * encodes a minimum access level for the register. We roll this
8790 * runtime check into our general permission check code, so check
8791 * here that the reginfo's specified permissions are strict enough
8792 * to encompass the generic architectural permission check.
8793 */
8794 if (r->state != ARM_CP_STATE_AA32) {
39107337 8795 CPAccessRights mask;
f5a0a5a5 8796 switch (r->opc1) {
b5bd7440
AB
8797 case 0:
8798 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8799 mask = PL0U_R | PL1_RW;
8800 break;
8801 case 1: case 2:
f5a0a5a5
PM
8802 /* min_EL EL1 */
8803 mask = PL1_RW;
8804 break;
8805 case 3:
8806 /* min_EL EL0 */
8807 mask = PL0_RW;
8808 break;
8809 case 4:
b4ecf60f 8810 case 5:
f5a0a5a5
PM
8811 /* min_EL EL2 */
8812 mask = PL2_RW;
8813 break;
f5a0a5a5
PM
8814 case 6:
8815 /* min_EL EL3 */
8816 mask = PL3_RW;
8817 break;
8818 case 7:
8819 /* min_EL EL1, secure mode only (we don't check the latter) */
8820 mask = PL1_RW;
8821 break;
8822 default:
8823 /* broken reginfo with out-of-range opc1 */
d385a605 8824 g_assert_not_reached();
f5a0a5a5
PM
8825 }
8826 /* assert our permissions are not too lax (stricter is fine) */
8827 assert((r->access & ~mask) == 0);
8828 }
8829
4b6a83fb
PM
8830 /* Check that the register definition has enough info to handle
8831 * reads and writes if they are permitted.
8832 */
87c3f0f2 8833 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
4b6a83fb 8834 if (r->access & PL3_R) {
3f3c82a5
FA
8835 assert((r->fieldoffset ||
8836 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8837 r->readfn);
4b6a83fb
PM
8838 }
8839 if (r->access & PL3_W) {
3f3c82a5
FA
8840 assert((r->fieldoffset ||
8841 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8842 r->writefn);
4b6a83fb
PM
8843 }
8844 }
5809ac57 8845
4b6a83fb
PM
8846 for (crm = crmmin; crm <= crmmax; crm++) {
8847 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8848 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
8849 for (state = ARM_CP_STATE_AA32;
8850 state <= ARM_CP_STATE_AA64; state++) {
8851 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8852 continue;
8853 }
3f3c82a5
FA
8854 if (state == ARM_CP_STATE_AA32) {
8855 /* Under AArch32 CP registers can be common
8856 * (same for secure and non-secure world) or banked.
8857 */
9c513e78
AB
8858 char *name;
8859
3f3c82a5
FA
8860 switch (r->secure) {
8861 case ARM_CP_SECSTATE_S:
8862 case ARM_CP_SECSTATE_NS:
8863 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
8864 r->secure, crm, opc1, opc2,
8865 r->name);
3f3c82a5 8866 break;
cbe64585 8867 case ARM_CP_SECSTATE_BOTH:
9c513e78 8868 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
8869 add_cpreg_to_hashtable(cpu, r, opaque, state,
8870 ARM_CP_SECSTATE_S,
9c513e78
AB
8871 crm, opc1, opc2, name);
8872 g_free(name);
3f3c82a5
FA
8873 add_cpreg_to_hashtable(cpu, r, opaque, state,
8874 ARM_CP_SECSTATE_NS,
9c513e78 8875 crm, opc1, opc2, r->name);
3f3c82a5 8876 break;
cbe64585
RH
8877 default:
8878 g_assert_not_reached();
3f3c82a5
FA
8879 }
8880 } else {
8881 /* AArch64 registers get mapped to non-secure instance
8882 * of AArch32 */
8883 add_cpreg_to_hashtable(cpu, r, opaque, state,
8884 ARM_CP_SECSTATE_NS,
9c513e78 8885 crm, opc1, opc2, r->name);
3f3c82a5 8886 }
f5a0a5a5 8887 }
4b6a83fb
PM
8888 }
8889 }
8890 }
8891}
8892
5809ac57
RH
8893/* Define a whole list of registers */
8894void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
8895 void *opaque, size_t len)
4b6a83fb 8896{
5809ac57
RH
8897 size_t i;
8898 for (i = 0; i < len; ++i) {
8899 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
4b6a83fb
PM
8900 }
8901}
8902
6c5c0fec
AB
8903/*
8904 * Modify ARMCPRegInfo for access from userspace.
8905 *
8906 * This is a data driven modification directed by
8907 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8908 * user-space cannot alter any values and dynamic values pertaining to
8909 * execution state are hidden from user space view anyway.
8910 */
5809ac57
RH
8911void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
8912 const ARMCPRegUserSpaceInfo *mods,
8913 size_t mods_len)
6c5c0fec 8914{
5809ac57
RH
8915 for (size_t mi = 0; mi < mods_len; ++mi) {
8916 const ARMCPRegUserSpaceInfo *m = mods + mi;
d040242e 8917 GPatternSpec *pat = NULL;
5809ac57 8918
d040242e
AB
8919 if (m->is_glob) {
8920 pat = g_pattern_spec_new(m->name);
8921 }
5809ac57
RH
8922 for (size_t ri = 0; ri < regs_len; ++ri) {
8923 ARMCPRegInfo *r = regs + ri;
8924
d040242e
AB
8925 if (pat && g_pattern_match_string(pat, r->name)) {
8926 r->type = ARM_CP_CONST;
8927 r->access = PL0U_R;
8928 r->resetvalue = 0;
8929 /* continue */
8930 } else if (strcmp(r->name, m->name) == 0) {
6c5c0fec
AB
8931 r->type = ARM_CP_CONST;
8932 r->access = PL0U_R;
8933 r->resetvalue &= m->exported_bits;
8934 r->resetvalue |= m->fixed_bits;
8935 break;
8936 }
8937 }
d040242e
AB
8938 if (pat) {
8939 g_pattern_spec_free(pat);
8940 }
6c5c0fec
AB
8941 }
8942}
8943
60322b39 8944const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 8945{
5860362d 8946 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
4b6a83fb
PM
8947}
8948
c4241c7d
PM
8949void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8950 uint64_t value)
4b6a83fb
PM
8951{
8952 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
8953}
8954
c4241c7d 8955uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
8956{
8957 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
8958 return 0;
8959}
8960
f5a0a5a5
PM
8961void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8962{
8963 /* Helper coprocessor reset function for do-nothing-on-reset registers */
8964}
8965
af393ffc 8966static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
8967{
8968 /* Return true if it is not valid for us to switch to
8969 * this CPU mode (ie all the UNPREDICTABLE cases in
8970 * the ARM ARM CPSRWriteByInstr pseudocode).
8971 */
af393ffc
PM
8972
8973 /* Changes to or from Hyp via MSR and CPS are illegal. */
8974 if (write_type == CPSRWriteByInstr &&
8975 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8976 mode == ARM_CPU_MODE_HYP)) {
8977 return 1;
8978 }
8979
37064a8b
PM
8980 switch (mode) {
8981 case ARM_CPU_MODE_USR:
10eacda7 8982 return 0;
37064a8b
PM
8983 case ARM_CPU_MODE_SYS:
8984 case ARM_CPU_MODE_SVC:
8985 case ARM_CPU_MODE_ABT:
8986 case ARM_CPU_MODE_UND:
8987 case ARM_CPU_MODE_IRQ:
8988 case ARM_CPU_MODE_FIQ:
52ff951b
PM
8989 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8990 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8991 */
10eacda7
PM
8992 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8993 * and CPS are treated as illegal mode changes.
8994 */
8995 if (write_type == CPSRWriteByInstr &&
10eacda7 8996 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7c208e0f 8997 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10eacda7
PM
8998 return 1;
8999 }
37064a8b 9000 return 0;
e6c8fc07 9001 case ARM_CPU_MODE_HYP:
e6ef0169 9002 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
027fc527 9003 case ARM_CPU_MODE_MON:
58ae2d1f 9004 return arm_current_el(env) < 3;
37064a8b
PM
9005 default:
9006 return 1;
9007 }
9008}
9009
2f4a40e5
AZ
9010uint32_t cpsr_read(CPUARMState *env)
9011{
9012 int ZF;
6fbe23d5
PB
9013 ZF = (env->ZF == 0);
9014 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
9015 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9016 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9017 | ((env->condexec_bits & 0xfc) << 8)
af519934 9018 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
9019}
9020
50866ba5
PM
9021void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9022 CPSRWriteType write_type)
2f4a40e5 9023{
6e8801f9 9024 uint32_t changed_daif;
e784807c
PM
9025 bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9026 (mask & (CPSR_M | CPSR_E | CPSR_IL));
6e8801f9 9027
2f4a40e5 9028 if (mask & CPSR_NZCV) {
6fbe23d5
PB
9029 env->ZF = (~val) & CPSR_Z;
9030 env->NF = val;
2f4a40e5
AZ
9031 env->CF = (val >> 29) & 1;
9032 env->VF = (val << 3) & 0x80000000;
9033 }
9034 if (mask & CPSR_Q)
9035 env->QF = ((val & CPSR_Q) != 0);
9036 if (mask & CPSR_T)
9037 env->thumb = ((val & CPSR_T) != 0);
9038 if (mask & CPSR_IT_0_1) {
9039 env->condexec_bits &= ~3;
9040 env->condexec_bits |= (val >> 25) & 3;
9041 }
9042 if (mask & CPSR_IT_2_7) {
9043 env->condexec_bits &= 3;
9044 env->condexec_bits |= (val >> 8) & 0xfc;
9045 }
9046 if (mask & CPSR_GE) {
9047 env->GE = (val >> 16) & 0xf;
9048 }
9049
6e8801f9
FA
9050 /* In a V7 implementation that includes the security extensions but does
9051 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9052 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9053 * bits respectively.
9054 *
9055 * In a V8 implementation, it is permitted for privileged software to
9056 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9057 */
f8c88bbc 9058 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
9059 arm_feature(env, ARM_FEATURE_EL3) &&
9060 !arm_feature(env, ARM_FEATURE_EL2) &&
9061 !arm_is_secure(env)) {
9062
9063 changed_daif = (env->daif ^ val) & mask;
9064
9065 if (changed_daif & CPSR_A) {
9066 /* Check to see if we are allowed to change the masking of async
9067 * abort exceptions from a non-secure state.
9068 */
9069 if (!(env->cp15.scr_el3 & SCR_AW)) {
9070 qemu_log_mask(LOG_GUEST_ERROR,
9071 "Ignoring attempt to switch CPSR_A flag from "
9072 "non-secure world with SCR.AW bit clear\n");
9073 mask &= ~CPSR_A;
9074 }
9075 }
9076
9077 if (changed_daif & CPSR_F) {
9078 /* Check to see if we are allowed to change the masking of FIQ
9079 * exceptions from a non-secure state.
9080 */
9081 if (!(env->cp15.scr_el3 & SCR_FW)) {
9082 qemu_log_mask(LOG_GUEST_ERROR,
9083 "Ignoring attempt to switch CPSR_F flag from "
9084 "non-secure world with SCR.FW bit clear\n");
9085 mask &= ~CPSR_F;
9086 }
9087
9088 /* Check whether non-maskable FIQ (NMFI) support is enabled.
9089 * If this bit is set software is not allowed to mask
9090 * FIQs, but is allowed to set CPSR_F to 0.
9091 */
9092 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9093 (val & CPSR_F)) {
9094 qemu_log_mask(LOG_GUEST_ERROR,
9095 "Ignoring attempt to enable CPSR_F flag "
9096 "(non-maskable FIQ [NMFI] support enabled)\n");
9097 mask &= ~CPSR_F;
9098 }
9099 }
9100 }
9101
4cc35614
PM
9102 env->daif &= ~(CPSR_AIF & mask);
9103 env->daif |= val & CPSR_AIF & mask;
9104
f8c88bbc
PM
9105 if (write_type != CPSRWriteRaw &&
9106 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
9107 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9108 /* Note that we can only get here in USR mode if this is a
9109 * gdb stub write; for this case we follow the architectural
9110 * behaviour for guest writes in USR mode of ignoring an attempt
9111 * to switch mode. (Those are caught by translate.c for writes
9112 * triggered by guest instructions.)
9113 */
9114 mask &= ~CPSR_M;
9115 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
9116 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9117 * v7, and has defined behaviour in v8:
9118 * + leave CPSR.M untouched
9119 * + allow changes to the other CPSR fields
9120 * + set PSTATE.IL
9121 * For user changes via the GDB stub, we don't set PSTATE.IL,
9122 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
9123 */
9124 mask &= ~CPSR_M;
81907a58
PM
9125 if (write_type != CPSRWriteByGDBStub &&
9126 arm_feature(env, ARM_FEATURE_V8)) {
9127 mask |= CPSR_IL;
9128 val |= CPSR_IL;
9129 }
81e37284
PM
9130 qemu_log_mask(LOG_GUEST_ERROR,
9131 "Illegal AArch32 mode switch attempt from %s to %s\n",
9132 aarch32_mode_name(env->uncached_cpsr),
9133 aarch32_mode_name(val));
37064a8b 9134 } else {
81e37284
PM
9135 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9136 write_type == CPSRWriteExceptionReturn ?
9137 "Exception return from AArch32" :
9138 "AArch32 mode switch from",
9139 aarch32_mode_name(env->uncached_cpsr),
9140 aarch32_mode_name(val), env->regs[15]);
37064a8b
PM
9141 switch_mode(env, val & CPSR_M);
9142 }
2f4a40e5
AZ
9143 }
9144 mask &= ~CACHED_CPSR_BITS;
9145 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
e784807c
PM
9146 if (rebuild_hflags) {
9147 arm_rebuild_hflags(env);
9148 }
2f4a40e5
AZ
9149}
9150
b26eefb6
PB
9151/* Sign/zero extend */
9152uint32_t HELPER(sxtb16)(uint32_t x)
9153{
9154 uint32_t res;
9155 res = (uint16_t)(int8_t)x;
9156 res |= (uint32_t)(int8_t)(x >> 16) << 16;
9157 return res;
9158}
9159
e5346292
PM
9160static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9161{
9162 /*
9163 * Take a division-by-zero exception if necessary; otherwise return
9164 * to get the usual non-trapping division behaviour (result of 0)
9165 */
9166 if (arm_feature(env, ARM_FEATURE_M)
9167 && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9168 raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9169 }
9170}
9171
b26eefb6
PB
9172uint32_t HELPER(uxtb16)(uint32_t x)
9173{
9174 uint32_t res;
9175 res = (uint16_t)(uint8_t)x;
9176 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9177 return res;
9178}
9179
e5346292 9180int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
3670669c 9181{
fc7a5038 9182 if (den == 0) {
e5346292 9183 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9184 return 0;
9185 }
9186 if (num == INT_MIN && den == -1) {
9187 return INT_MIN;
9188 }
3670669c
PB
9189 return num / den;
9190}
9191
e5346292 9192uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
3670669c 9193{
fc7a5038 9194 if (den == 0) {
e5346292 9195 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9196 return 0;
9197 }
3670669c
PB
9198 return num / den;
9199}
9200
9201uint32_t HELPER(rbit)(uint32_t x)
9202{
42fedbca 9203 return revbit32(x);
3670669c
PB
9204}
9205
c47eaf9f 9206#ifdef CONFIG_USER_ONLY
b5ff1b31 9207
affdb64d 9208static void switch_mode(CPUARMState *env, int mode)
b5ff1b31 9209{
2fc0cc0e 9210 ARMCPU *cpu = env_archcpu(env);
a47dddd7
AF
9211
9212 if (mode != ARM_CPU_MODE_USR) {
9213 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9214 }
b5ff1b31
FB
9215}
9216
012a906b
GB
9217uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9218 uint32_t cur_el, bool secure)
9e729b57
EI
9219{
9220 return 1;
9221}
9222
ce02049d
GB
9223void aarch64_sync_64_to_32(CPUARMState *env)
9224{
9225 g_assert_not_reached();
9226}
9227
b5ff1b31
FB
9228#else
9229
affdb64d 9230static void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
9231{
9232 int old_mode;
9233 int i;
9234
9235 old_mode = env->uncached_cpsr & CPSR_M;
9236 if (mode == old_mode)
9237 return;
9238
9239 if (old_mode == ARM_CPU_MODE_FIQ) {
9240 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 9241 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
9242 } else if (mode == ARM_CPU_MODE_FIQ) {
9243 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 9244 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
9245 }
9246
f5206413 9247 i = bank_number(old_mode);
b5ff1b31 9248 env->banked_r13[i] = env->regs[13];
b5ff1b31
FB
9249 env->banked_spsr[i] = env->spsr;
9250
f5206413 9251 i = bank_number(mode);
b5ff1b31 9252 env->regs[13] = env->banked_r13[i];
b5ff1b31 9253 env->spsr = env->banked_spsr[i];
593cfa2b
PM
9254
9255 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9256 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
b5ff1b31
FB
9257}
9258
0eeb17d6
GB
9259/* Physical Interrupt Target EL Lookup Table
9260 *
9261 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9262 *
9263 * The below multi-dimensional table is used for looking up the target
9264 * exception level given numerous condition criteria. Specifically, the
9265 * target EL is based on SCR and HCR routing controls as well as the
9266 * currently executing EL and secure state.
9267 *
9268 * Dimensions:
9269 * target_el_table[2][2][2][2][2][4]
9270 * | | | | | +--- Current EL
9271 * | | | | +------ Non-secure(0)/Secure(1)
9272 * | | | +--------- HCR mask override
9273 * | | +------------ SCR exec state control
9274 * | +--------------- SCR mask override
9275 * +------------------ 32-bit(0)/64-bit(1) EL3
9276 *
9277 * The table values are as such:
9278 * 0-3 = EL0-EL3
9279 * -1 = Cannot occur
9280 *
9281 * The ARM ARM target EL table includes entries indicating that an "exception
9282 * is not taken". The two cases where this is applicable are:
9283 * 1) An exception is taken from EL3 but the SCR does not have the exception
9284 * routed to EL3.
9285 * 2) An exception is taken from EL2 but the HCR does not have the exception
9286 * routed to EL2.
9287 * In these two cases, the below table contain a target of EL1. This value is
9288 * returned as it is expected that the consumer of the table data will check
9289 * for "target EL >= current EL" to ensure the exception is not taken.
9290 *
9291 * SCR HCR
9292 * 64 EA AMO From
9293 * BIT IRQ IMO Non-secure Secure
9294 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
9295 */
82c39f6a 9296static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
9297 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9298 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
9299 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9300 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
9301 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9302 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
9303 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9304 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
9305 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6c85f906
RDC
9306 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
9307 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
9308 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
0eeb17d6
GB
9309 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
9310 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6c85f906
RDC
9311 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
9312 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
0eeb17d6
GB
9313};
9314
9315/*
9316 * Determine the target EL for physical exceptions
9317 */
012a906b
GB
9318uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9319 uint32_t cur_el, bool secure)
0eeb17d6
GB
9320{
9321 CPUARMState *env = cs->env_ptr;
f7778444
RH
9322 bool rw;
9323 bool scr;
9324 bool hcr;
0eeb17d6 9325 int target_el;
2cde031f 9326 /* Is the highest EL AArch64? */
f7778444
RH
9327 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9328 uint64_t hcr_el2;
2cde031f
SS
9329
9330 if (arm_feature(env, ARM_FEATURE_EL3)) {
9331 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9332 } else {
9333 /* Either EL2 is the highest EL (and so the EL2 register width
9334 * is given by is64); or there is no EL2 or EL3, in which case
9335 * the value of 'rw' does not affect the table lookup anyway.
9336 */
9337 rw = is64;
9338 }
0eeb17d6 9339
f7778444 9340 hcr_el2 = arm_hcr_el2_eff(env);
0eeb17d6
GB
9341 switch (excp_idx) {
9342 case EXCP_IRQ:
9343 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
f7778444 9344 hcr = hcr_el2 & HCR_IMO;
0eeb17d6
GB
9345 break;
9346 case EXCP_FIQ:
9347 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
f7778444 9348 hcr = hcr_el2 & HCR_FMO;
0eeb17d6
GB
9349 break;
9350 default:
9351 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
f7778444 9352 hcr = hcr_el2 & HCR_AMO;
0eeb17d6
GB
9353 break;
9354 };
9355
d1b31428
RH
9356 /*
9357 * For these purposes, TGE and AMO/IMO/FMO both force the
9358 * interrupt to EL2. Fold TGE into the bit extracted above.
9359 */
9360 hcr |= (hcr_el2 & HCR_TGE) != 0;
9361
0eeb17d6
GB
9362 /* Perform a table-lookup for the target EL given the current state */
9363 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9364
9365 assert(target_el > 0);
9366
9367 return target_el;
9368}
9369
fc6177af 9370void arm_log_exception(CPUState *cs)
b59f479b 9371{
fc6177af
PM
9372 int idx = cs->exception_index;
9373
b59f479b
PMD
9374 if (qemu_loglevel_mask(CPU_LOG_INT)) {
9375 const char *exc = NULL;
9376 static const char * const excnames[] = {
9377 [EXCP_UDEF] = "Undefined Instruction",
9378 [EXCP_SWI] = "SVC",
9379 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9380 [EXCP_DATA_ABORT] = "Data Abort",
9381 [EXCP_IRQ] = "IRQ",
9382 [EXCP_FIQ] = "FIQ",
9383 [EXCP_BKPT] = "Breakpoint",
9384 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9385 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9386 [EXCP_HVC] = "Hypervisor Call",
9387 [EXCP_HYP_TRAP] = "Hypervisor Trap",
9388 [EXCP_SMC] = "Secure Monitor Call",
9389 [EXCP_VIRQ] = "Virtual IRQ",
9390 [EXCP_VFIQ] = "Virtual FIQ",
9391 [EXCP_SEMIHOST] = "Semihosting call",
9392 [EXCP_NOCP] = "v7M NOCP UsageFault",
9393 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9394 [EXCP_STKOF] = "v8M STKOF UsageFault",
9395 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9396 [EXCP_LSERR] = "v8M LSERR UsageFault",
9397 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
e5346292 9398 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
3c29632f 9399 [EXCP_VSERR] = "Virtual SERR",
b59f479b
PMD
9400 };
9401
9402 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9403 exc = excnames[idx];
9404 }
9405 if (!exc) {
9406 exc = "unknown";
9407 }
fc6177af
PM
9408 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9409 idx, exc, cs->cpu_index);
b59f479b
PMD
9410 }
9411}
9412
a356dacf 9413/*
7aab5a8c
PMD
9414 * Function used to synchronize QEMU's AArch64 register set with AArch32
9415 * register set. This is necessary when switching between AArch32 and AArch64
9416 * execution state.
a356dacf 9417 */
7aab5a8c 9418void aarch64_sync_32_to_64(CPUARMState *env)
9ee6e8bb 9419{
7aab5a8c
PMD
9420 int i;
9421 uint32_t mode = env->uncached_cpsr & CPSR_M;
9422
9423 /* We can blanket copy R[0:7] to X[0:7] */
9424 for (i = 0; i < 8; i++) {
9425 env->xregs[i] = env->regs[i];
fd592d89 9426 }
70d74660 9427
9a223097 9428 /*
7aab5a8c
PMD
9429 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9430 * Otherwise, they come from the banked user regs.
fd592d89 9431 */
7aab5a8c
PMD
9432 if (mode == ARM_CPU_MODE_FIQ) {
9433 for (i = 8; i < 13; i++) {
9434 env->xregs[i] = env->usr_regs[i - 8];
9435 }
9436 } else {
9437 for (i = 8; i < 13; i++) {
9438 env->xregs[i] = env->regs[i];
9439 }
fd592d89 9440 }
9ee6e8bb 9441
7aab5a8c
PMD
9442 /*
9443 * Registers x13-x23 are the various mode SP and FP registers. Registers
9444 * r13 and r14 are only copied if we are in that mode, otherwise we copy
9445 * from the mode banked register.
9446 */
9447 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9448 env->xregs[13] = env->regs[13];
9449 env->xregs[14] = env->regs[14];
9450 } else {
9451 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9452 /* HYP is an exception in that it is copied from r14 */
9453 if (mode == ARM_CPU_MODE_HYP) {
9454 env->xregs[14] = env->regs[14];
95695eff 9455 } else {
7aab5a8c 9456 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
95695eff 9457 }
95695eff
PM
9458 }
9459
7aab5a8c
PMD
9460 if (mode == ARM_CPU_MODE_HYP) {
9461 env->xregs[15] = env->regs[13];
9462 } else {
9463 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
95695eff
PM
9464 }
9465
7aab5a8c
PMD
9466 if (mode == ARM_CPU_MODE_IRQ) {
9467 env->xregs[16] = env->regs[14];
9468 env->xregs[17] = env->regs[13];
9469 } else {
9470 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9471 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9472 }
95695eff 9473
7aab5a8c
PMD
9474 if (mode == ARM_CPU_MODE_SVC) {
9475 env->xregs[18] = env->regs[14];
9476 env->xregs[19] = env->regs[13];
9477 } else {
9478 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9479 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9480 }
95695eff 9481
7aab5a8c
PMD
9482 if (mode == ARM_CPU_MODE_ABT) {
9483 env->xregs[20] = env->regs[14];
9484 env->xregs[21] = env->regs[13];
9485 } else {
9486 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9487 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9488 }
e33cf0f8 9489
7aab5a8c
PMD
9490 if (mode == ARM_CPU_MODE_UND) {
9491 env->xregs[22] = env->regs[14];
9492 env->xregs[23] = env->regs[13];
9493 } else {
9494 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9495 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
e33cf0f8
PM
9496 }
9497
9498 /*
7aab5a8c
PMD
9499 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9500 * mode, then we can copy from r8-r14. Otherwise, we copy from the
9501 * FIQ bank for r8-r14.
e33cf0f8 9502 */
7aab5a8c
PMD
9503 if (mode == ARM_CPU_MODE_FIQ) {
9504 for (i = 24; i < 31; i++) {
9505 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
9506 }
9507 } else {
9508 for (i = 24; i < 29; i++) {
9509 env->xregs[i] = env->fiq_regs[i - 24];
e33cf0f8 9510 }
7aab5a8c
PMD
9511 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9512 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
e33cf0f8 9513 }
7aab5a8c
PMD
9514
9515 env->pc = env->regs[15];
e33cf0f8
PM
9516}
9517
9a223097 9518/*
7aab5a8c
PMD
9519 * Function used to synchronize QEMU's AArch32 register set with AArch64
9520 * register set. This is necessary when switching between AArch32 and AArch64
9521 * execution state.
de2db7ec 9522 */
7aab5a8c 9523void aarch64_sync_64_to_32(CPUARMState *env)
9ee6e8bb 9524{
7aab5a8c
PMD
9525 int i;
9526 uint32_t mode = env->uncached_cpsr & CPSR_M;
abc24d86 9527
7aab5a8c
PMD
9528 /* We can blanket copy X[0:7] to R[0:7] */
9529 for (i = 0; i < 8; i++) {
9530 env->regs[i] = env->xregs[i];
de2db7ec 9531 }
3f0cddee 9532
9a223097 9533 /*
7aab5a8c
PMD
9534 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9535 * Otherwise, we copy x8-x12 into the banked user regs.
de2db7ec 9536 */
7aab5a8c
PMD
9537 if (mode == ARM_CPU_MODE_FIQ) {
9538 for (i = 8; i < 13; i++) {
9539 env->usr_regs[i - 8] = env->xregs[i];
9540 }
9541 } else {
9542 for (i = 8; i < 13; i++) {
9543 env->regs[i] = env->xregs[i];
9544 }
fb602cb7
PM
9545 }
9546
9a223097 9547 /*
7aab5a8c
PMD
9548 * Registers r13 & r14 depend on the current mode.
9549 * If we are in a given mode, we copy the corresponding x registers to r13
9550 * and r14. Otherwise, we copy the x register to the banked r13 and r14
9551 * for the mode.
fb602cb7 9552 */
7aab5a8c
PMD
9553 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9554 env->regs[13] = env->xregs[13];
9555 env->regs[14] = env->xregs[14];
fb602cb7 9556 } else {
7aab5a8c 9557 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
fb602cb7 9558
7aab5a8c
PMD
9559 /*
9560 * HYP is an exception in that it does not have its own banked r14 but
9561 * shares the USR r14
9562 */
9563 if (mode == ARM_CPU_MODE_HYP) {
9564 env->regs[14] = env->xregs[14];
9565 } else {
9566 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9567 }
9568 }
fb602cb7 9569
7aab5a8c
PMD
9570 if (mode == ARM_CPU_MODE_HYP) {
9571 env->regs[13] = env->xregs[15];
fb602cb7 9572 } else {
7aab5a8c 9573 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
fb602cb7 9574 }
d02a8698 9575
7aab5a8c
PMD
9576 if (mode == ARM_CPU_MODE_IRQ) {
9577 env->regs[14] = env->xregs[16];
9578 env->regs[13] = env->xregs[17];
d02a8698 9579 } else {
7aab5a8c
PMD
9580 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9581 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
d02a8698
PM
9582 }
9583
7aab5a8c
PMD
9584 if (mode == ARM_CPU_MODE_SVC) {
9585 env->regs[14] = env->xregs[18];
9586 env->regs[13] = env->xregs[19];
9587 } else {
9588 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9589 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
fb602cb7
PM
9590 }
9591
7aab5a8c
PMD
9592 if (mode == ARM_CPU_MODE_ABT) {
9593 env->regs[14] = env->xregs[20];
9594 env->regs[13] = env->xregs[21];
9595 } else {
9596 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9597 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
9598 }
9599
9600 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
9601 env->regs[14] = env->xregs[22];
9602 env->regs[13] = env->xregs[23];
ce02049d 9603 } else {
593cfa2b 9604 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
3a9148d0 9605 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
9606 }
9607
9608 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9609 * mode, then we can copy to r8-r14. Otherwise, we copy to the
9610 * FIQ bank for r8-r14.
9611 */
9612 if (mode == ARM_CPU_MODE_FIQ) {
9613 for (i = 24; i < 31; i++) {
9614 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
9615 }
9616 } else {
9617 for (i = 24; i < 29; i++) {
9618 env->fiq_regs[i - 24] = env->xregs[i];
9619 }
9620 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
593cfa2b 9621 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
ce02049d
GB
9622 }
9623
9624 env->regs[15] = env->pc;
9625}
9626
dea8378b
PM
9627static void take_aarch32_exception(CPUARMState *env, int new_mode,
9628 uint32_t mask, uint32_t offset,
9629 uint32_t newpc)
9630{
4a2696c0
RH
9631 int new_el;
9632
dea8378b
PM
9633 /* Change the CPU state so as to actually take the exception. */
9634 switch_mode(env, new_mode);
4a2696c0 9635
dea8378b
PM
9636 /*
9637 * For exceptions taken to AArch32 we must clear the SS bit in both
9638 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9639 */
f944a854 9640 env->pstate &= ~PSTATE_SS;
dea8378b
PM
9641 env->spsr = cpsr_read(env);
9642 /* Clear IT bits. */
9643 env->condexec_bits = 0;
9644 /* Switch to the new mode, and to the correct instruction set. */
9645 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
88828bf1
CD
9646
9647 /* This must be after mode switching. */
9648 new_el = arm_current_el(env);
9649
dea8378b
PM
9650 /* Set new mode endianness */
9651 env->uncached_cpsr &= ~CPSR_E;
4a2696c0 9652 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
dea8378b
PM
9653 env->uncached_cpsr |= CPSR_E;
9654 }
829f9fd3
PM
9655 /* J and IL must always be cleared for exception entry */
9656 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
dea8378b
PM
9657 env->daif |= mask;
9658
f2f68a78
RC
9659 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9660 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9661 env->uncached_cpsr |= CPSR_SSBS;
9662 } else {
9663 env->uncached_cpsr &= ~CPSR_SSBS;
9664 }
9665 }
9666
dea8378b
PM
9667 if (new_mode == ARM_CPU_MODE_HYP) {
9668 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9669 env->elr_el[2] = env->regs[15];
9670 } else {
4a2696c0 9671 /* CPSR.PAN is normally preserved preserved unless... */
f8af1143 9672 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
4a2696c0
RH
9673 switch (new_el) {
9674 case 3:
9675 if (!arm_is_secure_below_el3(env)) {
9676 /* ... the target is EL3, from non-secure state. */
9677 env->uncached_cpsr &= ~CPSR_PAN;
9678 break;
9679 }
9680 /* ... the target is EL3, from secure state ... */
9681 /* fall through */
9682 case 1:
9683 /* ... the target is EL1 and SCTLR.SPAN is 0. */
9684 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9685 env->uncached_cpsr |= CPSR_PAN;
9686 }
9687 break;
9688 }
9689 }
dea8378b
PM
9690 /*
9691 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9692 * and we should just guard the thumb mode on V4
9693 */
9694 if (arm_feature(env, ARM_FEATURE_V4T)) {
9695 env->thumb =
9696 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9697 }
9698 env->regs[14] = env->regs[15] + offset;
9699 }
9700 env->regs[15] = newpc;
a8a79c7a 9701 arm_rebuild_hflags(env);
dea8378b
PM
9702}
9703
b9bc21ff
PM
9704static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9705{
9706 /*
9707 * Handle exception entry to Hyp mode; this is sufficiently
9708 * different to entry to other AArch32 modes that we handle it
9709 * separately here.
9710 *
9711 * The vector table entry used is always the 0x14 Hyp mode entry point,
2c023d36 9712 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
b9bc21ff
PM
9713 * The offset applied to the preferred return address is always zero
9714 * (see DDI0487C.a section G1.12.3).
9715 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9716 */
9717 uint32_t addr, mask;
9718 ARMCPU *cpu = ARM_CPU(cs);
9719 CPUARMState *env = &cpu->env;
9720
9721 switch (cs->exception_index) {
9722 case EXCP_UDEF:
9723 addr = 0x04;
9724 break;
9725 case EXCP_SWI:
2c023d36 9726 addr = 0x08;
b9bc21ff
PM
9727 break;
9728 case EXCP_BKPT:
9729 /* Fall through to prefetch abort. */
9730 case EXCP_PREFETCH_ABORT:
9731 env->cp15.ifar_s = env->exception.vaddress;
9732 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9733 (uint32_t)env->exception.vaddress);
9734 addr = 0x0c;
9735 break;
9736 case EXCP_DATA_ABORT:
9737 env->cp15.dfar_s = env->exception.vaddress;
9738 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9739 (uint32_t)env->exception.vaddress);
9740 addr = 0x10;
9741 break;
9742 case EXCP_IRQ:
9743 addr = 0x18;
9744 break;
9745 case EXCP_FIQ:
9746 addr = 0x1c;
9747 break;
9748 case EXCP_HVC:
9749 addr = 0x08;
9750 break;
9751 case EXCP_HYP_TRAP:
9752 addr = 0x14;
9bbb4ef9 9753 break;
b9bc21ff
PM
9754 default:
9755 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9756 }
9757
9758 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
2ed08180
PM
9759 if (!arm_feature(env, ARM_FEATURE_V8)) {
9760 /*
9761 * QEMU syndrome values are v8-style. v7 has the IL bit
9762 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9763 * If this is a v7 CPU, squash the IL bit in those cases.
9764 */
9765 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9766 (cs->exception_index == EXCP_DATA_ABORT &&
9767 !(env->exception.syndrome & ARM_EL_ISV)) ||
9768 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9769 env->exception.syndrome &= ~ARM_EL_IL;
9770 }
9771 }
b9bc21ff
PM
9772 env->cp15.esr_el[2] = env->exception.syndrome;
9773 }
9774
9775 if (arm_current_el(env) != 2 && addr < 0x14) {
9776 addr = 0x14;
9777 }
9778
9779 mask = 0;
9780 if (!(env->cp15.scr_el3 & SCR_EA)) {
9781 mask |= CPSR_A;
9782 }
9783 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9784 mask |= CPSR_I;
9785 }
9786 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9787 mask |= CPSR_F;
9788 }
9789
9790 addr += env->cp15.hvbar;
9791
9792 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9793}
9794
966f758c 9795static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 9796{
97a8ea5a
AF
9797 ARMCPU *cpu = ARM_CPU(cs);
9798 CPUARMState *env = &cpu->env;
b5ff1b31
FB
9799 uint32_t addr;
9800 uint32_t mask;
9801 int new_mode;
9802 uint32_t offset;
16a906fd 9803 uint32_t moe;
b5ff1b31 9804
16a906fd 9805 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
64b91e3f 9806 switch (syn_get_ec(env->exception.syndrome)) {
16a906fd
PM
9807 case EC_BREAKPOINT:
9808 case EC_BREAKPOINT_SAME_EL:
9809 moe = 1;
9810 break;
9811 case EC_WATCHPOINT:
9812 case EC_WATCHPOINT_SAME_EL:
9813 moe = 10;
9814 break;
9815 case EC_AA32_BKPT:
9816 moe = 3;
9817 break;
9818 case EC_VECTORCATCH:
9819 moe = 5;
9820 break;
9821 default:
9822 moe = 0;
9823 break;
9824 }
9825
9826 if (moe) {
9827 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9828 }
9829
b9bc21ff
PM
9830 if (env->exception.target_el == 2) {
9831 arm_cpu_do_interrupt_aarch32_hyp(cs);
9832 return;
9833 }
9834
27103424 9835 switch (cs->exception_index) {
b5ff1b31
FB
9836 case EXCP_UDEF:
9837 new_mode = ARM_CPU_MODE_UND;
9838 addr = 0x04;
9839 mask = CPSR_I;
9840 if (env->thumb)
9841 offset = 2;
9842 else
9843 offset = 4;
9844 break;
9845 case EXCP_SWI:
9846 new_mode = ARM_CPU_MODE_SVC;
9847 addr = 0x08;
9848 mask = CPSR_I;
601d70b9 9849 /* The PC already points to the next instruction. */
b5ff1b31
FB
9850 offset = 0;
9851 break;
06c949e6 9852 case EXCP_BKPT:
9ee6e8bb
PB
9853 /* Fall through to prefetch abort. */
9854 case EXCP_PREFETCH_ABORT:
88ca1c2d 9855 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 9856 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 9857 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 9858 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9859 new_mode = ARM_CPU_MODE_ABT;
9860 addr = 0x0c;
9861 mask = CPSR_A | CPSR_I;
9862 offset = 4;
9863 break;
9864 case EXCP_DATA_ABORT:
4a7e2d73 9865 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 9866 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 9867 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 9868 env->exception.fsr,
6cd8a264 9869 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9870 new_mode = ARM_CPU_MODE_ABT;
9871 addr = 0x10;
9872 mask = CPSR_A | CPSR_I;
9873 offset = 8;
9874 break;
9875 case EXCP_IRQ:
9876 new_mode = ARM_CPU_MODE_IRQ;
9877 addr = 0x18;
9878 /* Disable IRQ and imprecise data aborts. */
9879 mask = CPSR_A | CPSR_I;
9880 offset = 4;
de38d23b
FA
9881 if (env->cp15.scr_el3 & SCR_IRQ) {
9882 /* IRQ routed to monitor mode */
9883 new_mode = ARM_CPU_MODE_MON;
9884 mask |= CPSR_F;
9885 }
b5ff1b31
FB
9886 break;
9887 case EXCP_FIQ:
9888 new_mode = ARM_CPU_MODE_FIQ;
9889 addr = 0x1c;
9890 /* Disable FIQ, IRQ and imprecise data aborts. */
9891 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
9892 if (env->cp15.scr_el3 & SCR_FIQ) {
9893 /* FIQ routed to monitor mode */
9894 new_mode = ARM_CPU_MODE_MON;
9895 }
b5ff1b31
FB
9896 offset = 4;
9897 break;
87a4b270
PM
9898 case EXCP_VIRQ:
9899 new_mode = ARM_CPU_MODE_IRQ;
9900 addr = 0x18;
9901 /* Disable IRQ and imprecise data aborts. */
9902 mask = CPSR_A | CPSR_I;
9903 offset = 4;
9904 break;
9905 case EXCP_VFIQ:
9906 new_mode = ARM_CPU_MODE_FIQ;
9907 addr = 0x1c;
9908 /* Disable FIQ, IRQ and imprecise data aborts. */
9909 mask = CPSR_A | CPSR_I | CPSR_F;
9910 offset = 4;
9911 break;
3c29632f
RH
9912 case EXCP_VSERR:
9913 {
9914 /*
9915 * Note that this is reported as a data abort, but the DFAR
9916 * has an UNKNOWN value. Construct the SError syndrome from
9917 * AET and ExT fields.
9918 */
9919 ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
9920
9921 if (extended_addresses_enabled(env)) {
9922 env->exception.fsr = arm_fi_to_lfsc(&fi);
9923 } else {
9924 env->exception.fsr = arm_fi_to_sfsc(&fi);
9925 }
9926 env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
9927 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9928 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
9929 env->exception.fsr);
9930
9931 new_mode = ARM_CPU_MODE_ABT;
9932 addr = 0x10;
9933 mask = CPSR_A | CPSR_I;
9934 offset = 8;
9935 }
9936 break;
dbe9d163
FA
9937 case EXCP_SMC:
9938 new_mode = ARM_CPU_MODE_MON;
9939 addr = 0x08;
9940 mask = CPSR_A | CPSR_I | CPSR_F;
9941 offset = 0;
9942 break;
b5ff1b31 9943 default:
a47dddd7 9944 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
9945 return; /* Never happens. Keep compiler happy. */
9946 }
e89e51a1
FA
9947
9948 if (new_mode == ARM_CPU_MODE_MON) {
9949 addr += env->cp15.mvbar;
137feaa9 9950 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 9951 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 9952 addr += 0xffff0000;
8641136c
NR
9953 } else {
9954 /* ARM v7 architectures provide a vector base address register to remap
9955 * the interrupt vector table.
e89e51a1 9956 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
9957 * Note: only bits 31:5 are valid.
9958 */
fb6c91ba 9959 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 9960 }
dbe9d163
FA
9961
9962 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9963 env->cp15.scr_el3 &= ~SCR_NS;
9964 }
9965
dea8378b 9966 take_aarch32_exception(env, new_mode, mask, offset, addr);
b5ff1b31
FB
9967}
9968
a65dabf7
PM
9969static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
9970{
9971 /*
9972 * Return the register number of the AArch64 view of the AArch32
9973 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
9974 * be that of the AArch32 mode the exception came from.
9975 */
9976 int mode = env->uncached_cpsr & CPSR_M;
9977
9978 switch (aarch32_reg) {
9979 case 0 ... 7:
9980 return aarch32_reg;
9981 case 8 ... 12:
9982 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
9983 case 13:
9984 switch (mode) {
9985 case ARM_CPU_MODE_USR:
9986 case ARM_CPU_MODE_SYS:
9987 return 13;
9988 case ARM_CPU_MODE_HYP:
9989 return 15;
9990 case ARM_CPU_MODE_IRQ:
9991 return 17;
9992 case ARM_CPU_MODE_SVC:
9993 return 19;
9994 case ARM_CPU_MODE_ABT:
9995 return 21;
9996 case ARM_CPU_MODE_UND:
9997 return 23;
9998 case ARM_CPU_MODE_FIQ:
9999 return 29;
10000 default:
10001 g_assert_not_reached();
10002 }
10003 case 14:
10004 switch (mode) {
10005 case ARM_CPU_MODE_USR:
10006 case ARM_CPU_MODE_SYS:
10007 case ARM_CPU_MODE_HYP:
10008 return 14;
10009 case ARM_CPU_MODE_IRQ:
10010 return 16;
10011 case ARM_CPU_MODE_SVC:
10012 return 18;
10013 case ARM_CPU_MODE_ABT:
10014 return 20;
10015 case ARM_CPU_MODE_UND:
10016 return 22;
10017 case ARM_CPU_MODE_FIQ:
10018 return 30;
10019 default:
10020 g_assert_not_reached();
10021 }
10022 case 15:
10023 return 31;
10024 default:
10025 g_assert_not_reached();
10026 }
10027}
10028
f944a854
RC
10029static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10030{
10031 uint32_t ret = cpsr_read(env);
10032
10033 /* Move DIT to the correct location for SPSR_ELx */
10034 if (ret & CPSR_DIT) {
10035 ret &= ~CPSR_DIT;
10036 ret |= PSTATE_DIT;
10037 }
10038 /* Merge PSTATE.SS into SPSR_ELx */
10039 ret |= env->pstate & PSTATE_SS;
10040
10041 return ret;
10042}
10043
966f758c
PM
10044/* Handle exception entry to a target EL which is using AArch64 */
10045static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
10046{
10047 ARMCPU *cpu = ARM_CPU(cs);
10048 CPUARMState *env = &cpu->env;
10049 unsigned int new_el = env->exception.target_el;
10050 target_ulong addr = env->cp15.vbar_el[new_el];
10051 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
4a2696c0 10052 unsigned int old_mode;
0ab5953b 10053 unsigned int cur_el = arm_current_el(env);
a65dabf7 10054 int rt;
0ab5953b 10055
9a05f7b6
RH
10056 /*
10057 * Note that new_el can never be 0. If cur_el is 0, then
10058 * el0_a64 is is_a64(), else el0_a64 is ignored.
10059 */
10060 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
f3a9b694 10061
0ab5953b 10062 if (cur_el < new_el) {
3d6f7617
PM
10063 /* Entry vector offset depends on whether the implemented EL
10064 * immediately lower than the target level is using AArch32 or AArch64
10065 */
10066 bool is_aa64;
cb092fbb 10067 uint64_t hcr;
3d6f7617
PM
10068
10069 switch (new_el) {
10070 case 3:
10071 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10072 break;
10073 case 2:
cb092fbb
RH
10074 hcr = arm_hcr_el2_eff(env);
10075 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10076 is_aa64 = (hcr & HCR_RW) != 0;
10077 break;
10078 }
10079 /* fall through */
3d6f7617
PM
10080 case 1:
10081 is_aa64 = is_a64(env);
10082 break;
10083 default:
10084 g_assert_not_reached();
10085 }
10086
10087 if (is_aa64) {
f3a9b694
PM
10088 addr += 0x400;
10089 } else {
10090 addr += 0x600;
10091 }
10092 } else if (pstate_read(env) & PSTATE_SP) {
10093 addr += 0x200;
10094 }
10095
f3a9b694
PM
10096 switch (cs->exception_index) {
10097 case EXCP_PREFETCH_ABORT:
10098 case EXCP_DATA_ABORT:
10099 env->cp15.far_el[new_el] = env->exception.vaddress;
10100 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10101 env->cp15.far_el[new_el]);
10102 /* fall through */
10103 case EXCP_BKPT:
10104 case EXCP_UDEF:
10105 case EXCP_SWI:
10106 case EXCP_HVC:
10107 case EXCP_HYP_TRAP:
10108 case EXCP_SMC:
a65dabf7
PM
10109 switch (syn_get_ec(env->exception.syndrome)) {
10110 case EC_ADVSIMDFPACCESSTRAP:
4be42f40
PM
10111 /*
10112 * QEMU internal FP/SIMD syndromes from AArch32 include the
10113 * TA and coproc fields which are only exposed if the exception
10114 * is taken to AArch32 Hyp mode. Mask them out to get a valid
10115 * AArch64 format syndrome.
10116 */
10117 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
a65dabf7
PM
10118 break;
10119 case EC_CP14RTTRAP:
10120 case EC_CP15RTTRAP:
10121 case EC_CP14DTTRAP:
10122 /*
10123 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10124 * the raw register field from the insn; when taking this to
10125 * AArch64 we must convert it to the AArch64 view of the register
10126 * number. Notice that we read a 4-bit AArch32 register number and
10127 * write back a 5-bit AArch64 one.
10128 */
10129 rt = extract32(env->exception.syndrome, 5, 4);
10130 rt = aarch64_regnum(env, rt);
10131 env->exception.syndrome = deposit32(env->exception.syndrome,
10132 5, 5, rt);
10133 break;
10134 case EC_CP15RRTTRAP:
10135 case EC_CP14RRTTRAP:
10136 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10137 rt = extract32(env->exception.syndrome, 5, 4);
10138 rt = aarch64_regnum(env, rt);
10139 env->exception.syndrome = deposit32(env->exception.syndrome,
10140 5, 5, rt);
10141 rt = extract32(env->exception.syndrome, 10, 4);
10142 rt = aarch64_regnum(env, rt);
10143 env->exception.syndrome = deposit32(env->exception.syndrome,
10144 10, 5, rt);
10145 break;
4be42f40 10146 }
f3a9b694
PM
10147 env->cp15.esr_el[new_el] = env->exception.syndrome;
10148 break;
10149 case EXCP_IRQ:
10150 case EXCP_VIRQ:
10151 addr += 0x80;
10152 break;
10153 case EXCP_FIQ:
10154 case EXCP_VFIQ:
10155 addr += 0x100;
10156 break;
3c29632f
RH
10157 case EXCP_VSERR:
10158 addr += 0x180;
10159 /* Construct the SError syndrome from IDS and ISS fields. */
10160 env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10161 env->cp15.esr_el[new_el] = env->exception.syndrome;
10162 break;
f3a9b694
PM
10163 default:
10164 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10165 }
10166
10167 if (is_a64(env)) {
4a2696c0 10168 old_mode = pstate_read(env);
f3a9b694
PM
10169 aarch64_save_sp(env, arm_current_el(env));
10170 env->elr_el[new_el] = env->pc;
10171 } else {
f944a854 10172 old_mode = cpsr_read_for_spsr_elx(env);
f3a9b694
PM
10173 env->elr_el[new_el] = env->regs[15];
10174
10175 aarch64_sync_32_to_64(env);
10176
10177 env->condexec_bits = 0;
10178 }
4a2696c0
RH
10179 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10180
f3a9b694
PM
10181 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10182 env->elr_el[new_el]);
10183
4a2696c0
RH
10184 if (cpu_isar_feature(aa64_pan, cpu)) {
10185 /* The value of PSTATE.PAN is normally preserved, except when ... */
10186 new_mode |= old_mode & PSTATE_PAN;
10187 switch (new_el) {
10188 case 2:
10189 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
10190 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10191 != (HCR_E2H | HCR_TGE)) {
10192 break;
10193 }
10194 /* fall through */
10195 case 1:
10196 /* ... the target is EL1 ... */
10197 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
10198 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10199 new_mode |= PSTATE_PAN;
10200 }
10201 break;
10202 }
10203 }
34669338
RH
10204 if (cpu_isar_feature(aa64_mte, cpu)) {
10205 new_mode |= PSTATE_TCO;
10206 }
4a2696c0 10207
f2f68a78
RC
10208 if (cpu_isar_feature(aa64_ssbs, cpu)) {
10209 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10210 new_mode |= PSTATE_SSBS;
10211 } else {
10212 new_mode &= ~PSTATE_SSBS;
10213 }
10214 }
10215
f3a9b694 10216 pstate_write(env, PSTATE_DAIF | new_mode);
53221552 10217 env->aarch64 = true;
f3a9b694 10218 aarch64_restore_sp(env, new_el);
a8a79c7a 10219 helper_rebuild_hflags_a64(env, new_el);
f3a9b694
PM
10220
10221 env->pc = addr;
10222
10223 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10224 new_el, env->pc, pstate_read(env));
966f758c
PM
10225}
10226
ed6e6ba9
AB
10227/*
10228 * Do semihosting call and set the appropriate return value. All the
10229 * permission and validity checks have been done at translate time.
10230 *
10231 * We only see semihosting exceptions in TCG only as they are not
10232 * trapped to the hypervisor in KVM.
10233 */
91f78c58 10234#ifdef CONFIG_TCG
ed6e6ba9
AB
10235static void handle_semihosting(CPUState *cs)
10236{
904c04de
PM
10237 ARMCPU *cpu = ARM_CPU(cs);
10238 CPUARMState *env = &cpu->env;
10239
10240 if (is_a64(env)) {
ed6e6ba9
AB
10241 qemu_log_mask(CPU_LOG_INT,
10242 "...handling as semihosting call 0x%" PRIx64 "\n",
10243 env->xregs[0]);
0bb446d8 10244 env->xregs[0] = do_common_semihosting(cs);
4ff5ef9e 10245 env->pc += 4;
904c04de 10246 } else {
904c04de
PM
10247 qemu_log_mask(CPU_LOG_INT,
10248 "...handling as semihosting call 0x%x\n",
10249 env->regs[0]);
0bb446d8 10250 env->regs[0] = do_common_semihosting(cs);
4ff5ef9e 10251 env->regs[15] += env->thumb ? 2 : 4;
904c04de
PM
10252 }
10253}
ed6e6ba9 10254#endif
904c04de 10255
966f758c
PM
10256/* Handle a CPU exception for A and R profile CPUs.
10257 * Do any appropriate logging, handle PSCI calls, and then hand off
10258 * to the AArch64-entry or AArch32-entry function depending on the
10259 * target exception level's register width.
853bfef4
CF
10260 *
10261 * Note: this is used for both TCG (as the do_interrupt tcg op),
10262 * and KVM to re-inject guest debug exceptions, and to
10263 * inject a Synchronous-External-Abort.
966f758c
PM
10264 */
10265void arm_cpu_do_interrupt(CPUState *cs)
10266{
10267 ARMCPU *cpu = ARM_CPU(cs);
10268 CPUARMState *env = &cpu->env;
10269 unsigned int new_el = env->exception.target_el;
10270
531c60a9 10271 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c 10272
fc6177af 10273 arm_log_exception(cs);
966f758c
PM
10274 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10275 new_el);
10276 if (qemu_loglevel_mask(CPU_LOG_INT)
10277 && !excp_is_internal(cs->exception_index)) {
6568da45 10278 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
64b91e3f 10279 syn_get_ec(env->exception.syndrome),
966f758c
PM
10280 env->exception.syndrome);
10281 }
10282
10283 if (arm_is_psci_call(cpu, cs->exception_index)) {
10284 arm_handle_psci_call(cpu);
10285 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10286 return;
10287 }
10288
ed6e6ba9
AB
10289 /*
10290 * Semihosting semantics depend on the register width of the code
10291 * that caused the exception, not the target exception level, so
10292 * must be handled here.
966f758c 10293 */
ed6e6ba9
AB
10294#ifdef CONFIG_TCG
10295 if (cs->exception_index == EXCP_SEMIHOST) {
10296 handle_semihosting(cs);
904c04de
PM
10297 return;
10298 }
ed6e6ba9 10299#endif
904c04de 10300
b5c53d1b
AL
10301 /* Hooks may change global state so BQL should be held, also the
10302 * BQL needs to be held for any modification of
10303 * cs->interrupt_request.
10304 */
10305 g_assert(qemu_mutex_iothread_locked());
10306
10307 arm_call_pre_el_change_hook(cpu);
10308
904c04de
PM
10309 assert(!excp_is_internal(cs->exception_index));
10310 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
10311 arm_cpu_do_interrupt_aarch64(cs);
10312 } else {
10313 arm_cpu_do_interrupt_aarch32(cs);
10314 }
f3a9b694 10315
bd7d00fc
PM
10316 arm_call_el_change_hook(cpu);
10317
f3a9b694
PM
10318 if (!kvm_enabled()) {
10319 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10320 }
10321}
c47eaf9f 10322#endif /* !CONFIG_USER_ONLY */
0480f69a 10323
aaec1432
RH
10324uint64_t arm_sctlr(CPUARMState *env, int el)
10325{
10326 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10327 if (el == 0) {
10328 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
b6ad6062
RDC
10329 el = (mmu_idx == ARMMMUIdx_E20_0 || mmu_idx == ARMMMUIdx_SE20_0)
10330 ? 2 : 1;
aaec1432
RH
10331 }
10332 return env->cp15.sctlr_el[el];
10333}
c47eaf9f 10334
0480f69a 10335/* Return the SCTLR value which controls this address translation regime */
aaec1432 10336static inline uint64_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
0480f69a
PM
10337{
10338 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
10339}
10340
aaec1432
RH
10341#ifndef CONFIG_USER_ONLY
10342
0480f69a
PM
10343/* Return true if the specified stage of address translation is disabled */
10344static inline bool regime_translation_disabled(CPUARMState *env,
10345 ARMMMUIdx mmu_idx)
10346{
e04a5752
RDC
10347 uint64_t hcr_el2;
10348
29c483a5 10349 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea 10350 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] &
3bef7012
PM
10351 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) {
10352 case R_V7M_MPU_CTRL_ENABLE_MASK:
10353 /* Enabled, but not for HardFault and NMI */
62593718 10354 return mmu_idx & ARM_MMU_IDX_M_NEGPRI;
3bef7012
PM
10355 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK:
10356 /* Enabled for all cases */
10357 return false;
10358 case 0:
10359 default:
10360 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but
10361 * we warned about that in armv7m_nvic.c when the guest set it.
10362 */
10363 return true;
10364 }
29c483a5
MD
10365 }
10366
e04a5752
RDC
10367 hcr_el2 = arm_hcr_el2_eff(env);
10368
b1a10c86 10369 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
9d1bab33 10370 /* HCR.DC means HCR.VM behaves as 1 */
e04a5752 10371 return (hcr_el2 & (HCR_DC | HCR_VM)) == 0;
0480f69a 10372 }
3d0e3080 10373
e04a5752 10374 if (hcr_el2 & HCR_TGE) {
3d0e3080
PM
10375 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */
10376 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) {
10377 return true;
10378 }
10379 }
10380
e04a5752 10381 if ((hcr_el2 & HCR_DC) && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
9d1bab33
PM
10382 /* HCR.DC means SCTLR_EL1.M behaves as 0 */
10383 return true;
10384 }
10385
0480f69a
PM
10386 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
10387}
10388
73462ddd
PC
10389static inline bool regime_translation_big_endian(CPUARMState *env,
10390 ARMMMUIdx mmu_idx)
10391{
10392 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
10393}
10394
c47eaf9f
PM
10395/* Return the TTBR associated with this translation regime */
10396static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
10397 int ttbrn)
10398{
97fa9350 10399 if (mmu_idx == ARMMMUIdx_Stage2) {
c47eaf9f
PM
10400 return env->cp15.vttbr_el2;
10401 }
b1a10c86
RDC
10402 if (mmu_idx == ARMMMUIdx_Stage2_S) {
10403 return env->cp15.vsttbr_el2;
10404 }
c47eaf9f
PM
10405 if (ttbrn == 0) {
10406 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
10407 } else {
10408 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
10409 }
10410}
10411
10412#endif /* !CONFIG_USER_ONLY */
10413
8bd5c820
PM
10414/* Convert a possible stage1+2 MMU index into the appropriate
10415 * stage 1 MMU index
10416 */
10417static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx)
10418{
b9f6033c 10419 switch (mmu_idx) {
b1a10c86
RDC
10420 case ARMMMUIdx_SE10_0:
10421 return ARMMMUIdx_Stage1_SE0;
10422 case ARMMMUIdx_SE10_1:
10423 return ARMMMUIdx_Stage1_SE1;
10424 case ARMMMUIdx_SE10_1_PAN:
10425 return ARMMMUIdx_Stage1_SE1_PAN;
b9f6033c
RH
10426 case ARMMMUIdx_E10_0:
10427 return ARMMMUIdx_Stage1_E0;
10428 case ARMMMUIdx_E10_1:
10429 return ARMMMUIdx_Stage1_E1;
452ef8cb
RH
10430 case ARMMMUIdx_E10_1_PAN:
10431 return ARMMMUIdx_Stage1_E1_PAN;
b9f6033c
RH
10432 default:
10433 return mmu_idx;
8bd5c820 10434 }
8bd5c820
PM
10435}
10436
0480f69a
PM
10437/* Return true if the translation regime is using LPAE format page tables */
10438static inline bool regime_using_lpae_format(CPUARMState *env,
10439 ARMMMUIdx mmu_idx)
10440{
10441 int el = regime_el(env, mmu_idx);
10442 if (el == 2 || arm_el_is_aa64(env, el)) {
10443 return true;
10444 }
10445 if (arm_feature(env, ARM_FEATURE_LPAE)
10446 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
10447 return true;
10448 }
10449 return false;
10450}
10451
deb2db99
AR
10452/* Returns true if the stage 1 translation regime is using LPAE format page
10453 * tables. Used when raising alignment exceptions, whose FSR changes depending
10454 * on whether the long or short descriptor format is in use. */
10455bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
30901475 10456{
8bd5c820 10457 mmu_idx = stage_1_mmu_idx(mmu_idx);
deb2db99 10458
30901475
AB
10459 return regime_using_lpae_format(env, mmu_idx);
10460}
10461
c47eaf9f 10462#ifndef CONFIG_USER_ONLY
0480f69a
PM
10463static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
10464{
10465 switch (mmu_idx) {
fba37aed 10466 case ARMMMUIdx_SE10_0:
b9f6033c 10467 case ARMMMUIdx_E20_0:
b6ad6062 10468 case ARMMMUIdx_SE20_0:
2859d7b5 10469 case ARMMMUIdx_Stage1_E0:
b1a10c86 10470 case ARMMMUIdx_Stage1_SE0:
e7b921c2 10471 case ARMMMUIdx_MUser:
871bec7c 10472 case ARMMMUIdx_MSUser:
62593718
PM
10473 case ARMMMUIdx_MUserNegPri:
10474 case ARMMMUIdx_MSUserNegPri:
0480f69a
PM
10475 return true;
10476 default:
10477 return false;
01b98b68
RH
10478 case ARMMMUIdx_E10_0:
10479 case ARMMMUIdx_E10_1:
452ef8cb 10480 case ARMMMUIdx_E10_1_PAN:
0480f69a
PM
10481 g_assert_not_reached();
10482 }
10483}
10484
0fbf5238
AJ
10485/* Translate section/page access permissions to page
10486 * R/W protection flags
d76951b6
AJ
10487 *
10488 * @env: CPUARMState
10489 * @mmu_idx: MMU index indicating required translation regime
10490 * @ap: The 3-bit access permissions (AP[2:0])
10491 * @domain_prot: The 2-bit domain access permissions
0fbf5238
AJ
10492 */
10493static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
10494 int ap, int domain_prot)
10495{
554b0b09
PM
10496 bool is_user = regime_is_user(env, mmu_idx);
10497
10498 if (domain_prot == 3) {
10499 return PAGE_READ | PAGE_WRITE;
10500 }
10501
554b0b09
PM
10502 switch (ap) {
10503 case 0:
10504 if (arm_feature(env, ARM_FEATURE_V7)) {
10505 return 0;
10506 }
554b0b09
PM
10507 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
10508 case SCTLR_S:
10509 return is_user ? 0 : PAGE_READ;
10510 case SCTLR_R:
10511 return PAGE_READ;
10512 default:
10513 return 0;
10514 }
10515 case 1:
10516 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10517 case 2:
87c3d486 10518 if (is_user) {
0fbf5238 10519 return PAGE_READ;
87c3d486 10520 } else {
554b0b09 10521 return PAGE_READ | PAGE_WRITE;
87c3d486 10522 }
554b0b09
PM
10523 case 3:
10524 return PAGE_READ | PAGE_WRITE;
10525 case 4: /* Reserved. */
10526 return 0;
10527 case 5:
0fbf5238 10528 return is_user ? 0 : PAGE_READ;
554b0b09 10529 case 6:
0fbf5238 10530 return PAGE_READ;
554b0b09 10531 case 7:
87c3d486 10532 if (!arm_feature(env, ARM_FEATURE_V6K)) {
554b0b09 10533 return 0;
87c3d486 10534 }
0fbf5238 10535 return PAGE_READ;
554b0b09 10536 default:
0fbf5238 10537 g_assert_not_reached();
554b0b09 10538 }
b5ff1b31
FB
10539}
10540
d76951b6
AJ
10541/* Translate section/page access permissions to page
10542 * R/W protection flags.
10543 *
d76951b6 10544 * @ap: The 2-bit simple AP (AP[2:1])
d8e052b3 10545 * @is_user: TRUE if accessing from PL0
d76951b6 10546 */
d8e052b3 10547static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
d76951b6 10548{
d76951b6
AJ
10549 switch (ap) {
10550 case 0:
10551 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
10552 case 1:
10553 return PAGE_READ | PAGE_WRITE;
10554 case 2:
10555 return is_user ? 0 : PAGE_READ;
10556 case 3:
10557 return PAGE_READ;
10558 default:
10559 g_assert_not_reached();
10560 }
10561}
10562
d8e052b3
AJ
10563static inline int
10564simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
10565{
10566 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
10567}
10568
6ab1a5ee
EI
10569/* Translate S2 section/page access permissions to protection flags
10570 *
10571 * @env: CPUARMState
10572 * @s2ap: The 2-bit stage2 access permissions (S2AP)
ce3125be
PM
10573 * @xn: XN (execute-never) bits
10574 * @s1_is_el0: true if this is S2 of an S1+2 walk for EL0
6ab1a5ee 10575 */
ce3125be 10576static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0)
6ab1a5ee
EI
10577{
10578 int prot = 0;
10579
10580 if (s2ap & 1) {
10581 prot |= PAGE_READ;
10582 }
10583 if (s2ap & 2) {
10584 prot |= PAGE_WRITE;
10585 }
ce3125be
PM
10586
10587 if (cpu_isar_feature(any_tts2uxn, env_archcpu(env))) {
10588 switch (xn) {
10589 case 0:
dfda6837 10590 prot |= PAGE_EXEC;
ce3125be
PM
10591 break;
10592 case 1:
10593 if (s1_is_el0) {
10594 prot |= PAGE_EXEC;
10595 }
10596 break;
10597 case 2:
10598 break;
10599 case 3:
10600 if (!s1_is_el0) {
10601 prot |= PAGE_EXEC;
10602 }
10603 break;
10604 default:
10605 g_assert_not_reached();
10606 }
10607 } else {
10608 if (!extract32(xn, 1, 1)) {
10609 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
10610 prot |= PAGE_EXEC;
10611 }
dfda6837 10612 }
6ab1a5ee
EI
10613 }
10614 return prot;
10615}
10616
d8e052b3
AJ
10617/* Translate section/page access permissions to protection flags
10618 *
10619 * @env: CPUARMState
10620 * @mmu_idx: MMU index indicating required translation regime
10621 * @is_aa64: TRUE if AArch64
10622 * @ap: The 2-bit simple AP (AP[2:1])
10623 * @ns: NS (non-secure) bit
10624 * @xn: XN (execute-never) bit
10625 * @pxn: PXN (privileged execute-never) bit
10626 */
10627static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
10628 int ap, int ns, int xn, int pxn)
10629{
10630 bool is_user = regime_is_user(env, mmu_idx);
10631 int prot_rw, user_rw;
10632 bool have_wxn;
10633 int wxn = 0;
10634
97fa9350 10635 assert(mmu_idx != ARMMMUIdx_Stage2);
b1a10c86 10636 assert(mmu_idx != ARMMMUIdx_Stage2_S);
d8e052b3
AJ
10637
10638 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
10639 if (is_user) {
10640 prot_rw = user_rw;
10641 } else {
81636b70 10642 if (user_rw && regime_is_pan(env, mmu_idx)) {
f4e1dbc5
PM
10643 /* PAN forbids data accesses but doesn't affect insn fetch */
10644 prot_rw = 0;
10645 } else {
10646 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
81636b70 10647 }
d8e052b3
AJ
10648 }
10649
10650 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
10651 return prot_rw;
10652 }
10653
10654 /* TODO have_wxn should be replaced with
10655 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
10656 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
10657 * compatible processors have EL2, which is required for [U]WXN.
10658 */
10659 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
10660
10661 if (have_wxn) {
10662 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
10663 }
10664
10665 if (is_aa64) {
339370b9
RH
10666 if (regime_has_2_ranges(mmu_idx) && !is_user) {
10667 xn = pxn || (user_rw & PAGE_WRITE);
d8e052b3
AJ
10668 }
10669 } else if (arm_feature(env, ARM_FEATURE_V7)) {
10670 switch (regime_el(env, mmu_idx)) {
10671 case 1:
10672 case 3:
10673 if (is_user) {
10674 xn = xn || !(user_rw & PAGE_READ);
10675 } else {
10676 int uwxn = 0;
10677 if (have_wxn) {
10678 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
10679 }
10680 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
10681 (uwxn && (user_rw & PAGE_WRITE));
10682 }
10683 break;
10684 case 2:
10685 break;
10686 }
10687 } else {
10688 xn = wxn = 0;
10689 }
10690
10691 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
10692 return prot_rw;
10693 }
10694 return prot_rw | PAGE_EXEC;
10695}
10696
0480f69a
PM
10697static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
10698 uint32_t *table, uint32_t address)
b2fa1797 10699{
0480f69a 10700 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
0480f69a 10701 TCR *tcr = regime_tcr(env, mmu_idx);
11f136ee 10702
11f136ee
FA
10703 if (address & tcr->mask) {
10704 if (tcr->raw_tcr & TTBCR_PD1) {
e389be16
FA
10705 /* Translation table walk disabled for TTBR1 */
10706 return false;
10707 }
aef878be 10708 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
e389be16 10709 } else {
11f136ee 10710 if (tcr->raw_tcr & TTBCR_PD0) {
e389be16
FA
10711 /* Translation table walk disabled for TTBR0 */
10712 return false;
10713 }
aef878be 10714 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
e389be16
FA
10715 }
10716 *table |= (address >> 18) & 0x3ffc;
10717 return true;
b2fa1797
PB
10718}
10719
37785977
EI
10720/* Translate a S1 pagetable walk through S2 if needed. */
10721static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
3d4bd397 10722 hwaddr addr, bool *is_secure,
37785977
EI
10723 ARMMMUFaultInfo *fi)
10724{
fee7aa46 10725 if (arm_mmu_idx_is_stage1_of_2(mmu_idx) &&
97fa9350 10726 !regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
37785977
EI
10727 target_ulong s2size;
10728 hwaddr s2pa;
10729 int s2prot;
10730 int ret;
b1a10c86
RDC
10731 ARMMMUIdx s2_mmu_idx = *is_secure ? ARMMMUIdx_Stage2_S
10732 : ARMMMUIdx_Stage2;
eadb2feb 10733 ARMCacheAttrs cacheattrs = {};
3d4bd397
RDC
10734 MemTxAttrs txattrs = {};
10735
b1a10c86 10736 ret = get_phys_addr_lpae(env, addr, MMU_DATA_LOAD, s2_mmu_idx, false,
59dff859 10737 &s2pa, &txattrs, &s2prot, &s2size, fi,
a6d6f37a 10738 &cacheattrs);
37785977 10739 if (ret) {
3b39d734 10740 assert(fi->type != ARMFault_None);
37785977
EI
10741 fi->s2addr = addr;
10742 fi->stage2 = true;
10743 fi->s1ptw = true;
9861248f 10744 fi->s1ns = !*is_secure;
37785977
EI
10745 return ~0;
10746 }
e04a5752
RDC
10747 if ((arm_hcr_el2_eff(env) & HCR_PTW) &&
10748 (cacheattrs.attrs & 0xf0) == 0) {
a6d6f37a
RH
10749 /*
10750 * PTW set and S1 walk touched S2 Device memory:
10751 * generate Permission fault.
10752 */
eadb2feb
PM
10753 fi->type = ARMFault_Permission;
10754 fi->s2addr = addr;
10755 fi->stage2 = true;
10756 fi->s1ptw = true;
9861248f 10757 fi->s1ns = !*is_secure;
eadb2feb
PM
10758 return ~0;
10759 }
588c6dd1
RDC
10760
10761 if (arm_is_secure_below_el3(env)) {
10762 /* Check if page table walk is to secure or non-secure PA space. */
10763 if (*is_secure) {
10764 *is_secure = !(env->cp15.vstcr_el2.raw_tcr & VSTCR_SW);
10765 } else {
10766 *is_secure = !(env->cp15.vtcr_el2.raw_tcr & VTCR_NSW);
10767 }
10768 } else {
10769 assert(!*is_secure);
10770 }
10771
37785977
EI
10772 addr = s2pa;
10773 }
10774 return addr;
10775}
10776
14577270 10777/* All loads done in the course of a page table walk go through here. */
a614e698 10778static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 10779 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 10780{
a614e698
EI
10781 ARMCPU *cpu = ARM_CPU(cs);
10782 CPUARMState *env = &cpu->env;
ebca90e4 10783 MemTxAttrs attrs = {};
3b39d734 10784 MemTxResult result = MEMTX_OK;
5ce4ff65 10785 AddressSpace *as;
3b39d734 10786 uint32_t data;
ebca90e4 10787
3d4bd397 10788 addr = S1_ptw_translate(env, mmu_idx, addr, &is_secure, fi);
ebca90e4 10789 attrs.secure = is_secure;
5ce4ff65 10790 as = arm_addressspace(cs, attrs);
a614e698
EI
10791 if (fi->s1ptw) {
10792 return 0;
10793 }
73462ddd 10794 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 10795 data = address_space_ldl_be(as, addr, attrs, &result);
73462ddd 10796 } else {
3b39d734 10797 data = address_space_ldl_le(as, addr, attrs, &result);
73462ddd 10798 }
3b39d734
PM
10799 if (result == MEMTX_OK) {
10800 return data;
10801 }
10802 fi->type = ARMFault_SyncExternalOnWalk;
10803 fi->ea = arm_extabort_type(result);
10804 return 0;
ebca90e4
PM
10805}
10806
37785977 10807static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
3795a6de 10808 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi)
ebca90e4 10809{
37785977
EI
10810 ARMCPU *cpu = ARM_CPU(cs);
10811 CPUARMState *env = &cpu->env;
ebca90e4 10812 MemTxAttrs attrs = {};
3b39d734 10813 MemTxResult result = MEMTX_OK;
5ce4ff65 10814 AddressSpace *as;
9aea1ea3 10815 uint64_t data;
ebca90e4 10816
3d4bd397 10817 addr = S1_ptw_translate(env, mmu_idx, addr, &is_secure, fi);
ebca90e4 10818 attrs.secure = is_secure;
5ce4ff65 10819 as = arm_addressspace(cs, attrs);
37785977
EI
10820 if (fi->s1ptw) {
10821 return 0;
10822 }
73462ddd 10823 if (regime_translation_big_endian(env, mmu_idx)) {
3b39d734 10824 data = address_space_ldq_be(as, addr, attrs, &result);
73462ddd 10825 } else {
3b39d734
PM
10826 data = address_space_ldq_le(as, addr, attrs, &result);
10827 }
10828 if (result == MEMTX_OK) {
10829 return data;
73462ddd 10830 }
3b39d734
PM
10831 fi->type = ARMFault_SyncExternalOnWalk;
10832 fi->ea = arm_extabort_type(result);
10833 return 0;
ebca90e4
PM
10834}
10835
b7cc4e82 10836static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
03ae85f8 10837 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10838 hwaddr *phys_ptr, int *prot,
f989983e 10839 target_ulong *page_size,
e14b5a23 10840 ARMMMUFaultInfo *fi)
b5ff1b31 10841{
2fc0cc0e 10842 CPUState *cs = env_cpu(env);
f989983e 10843 int level = 1;
b5ff1b31
FB
10844 uint32_t table;
10845 uint32_t desc;
10846 int type;
10847 int ap;
e389be16 10848 int domain = 0;
dd4ebc2e 10849 int domain_prot;
a8170e5e 10850 hwaddr phys_addr;
0480f69a 10851 uint32_t dacr;
b5ff1b31 10852
9ee6e8bb
PB
10853 /* Pagetable walk. */
10854 /* Lookup l1 descriptor. */
0480f69a 10855 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10856 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f989983e 10857 fi->type = ARMFault_Translation;
e389be16
FA
10858 goto do_fault;
10859 }
a614e698 10860 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10861 mmu_idx, fi);
3b39d734
PM
10862 if (fi->type != ARMFault_None) {
10863 goto do_fault;
10864 }
9ee6e8bb 10865 type = (desc & 3);
dd4ebc2e 10866 domain = (desc >> 5) & 0x0f;
0480f69a
PM
10867 if (regime_el(env, mmu_idx) == 1) {
10868 dacr = env->cp15.dacr_ns;
10869 } else {
10870 dacr = env->cp15.dacr_s;
10871 }
10872 domain_prot = (dacr >> (domain * 2)) & 3;
9ee6e8bb 10873 if (type == 0) {
601d70b9 10874 /* Section translation fault. */
f989983e 10875 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10876 goto do_fault;
10877 }
f989983e
PM
10878 if (type != 2) {
10879 level = 2;
10880 }
dd4ebc2e 10881 if (domain_prot == 0 || domain_prot == 2) {
f989983e 10882 fi->type = ARMFault_Domain;
9ee6e8bb
PB
10883 goto do_fault;
10884 }
10885 if (type == 2) {
10886 /* 1Mb section. */
10887 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
10888 ap = (desc >> 10) & 3;
d4c430a8 10889 *page_size = 1024 * 1024;
9ee6e8bb
PB
10890 } else {
10891 /* Lookup l2 entry. */
554b0b09
PM
10892 if (type == 1) {
10893 /* Coarse pagetable. */
10894 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
10895 } else {
10896 /* Fine pagetable. */
10897 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
10898 }
a614e698 10899 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10900 mmu_idx, fi);
3b39d734
PM
10901 if (fi->type != ARMFault_None) {
10902 goto do_fault;
10903 }
9ee6e8bb
PB
10904 switch (desc & 3) {
10905 case 0: /* Page translation fault. */
f989983e 10906 fi->type = ARMFault_Translation;
9ee6e8bb
PB
10907 goto do_fault;
10908 case 1: /* 64k page. */
10909 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
10910 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
d4c430a8 10911 *page_size = 0x10000;
ce819861 10912 break;
9ee6e8bb
PB
10913 case 2: /* 4k page. */
10914 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
c10f7fc3 10915 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
d4c430a8 10916 *page_size = 0x1000;
ce819861 10917 break;
fc1891c7 10918 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
554b0b09 10919 if (type == 1) {
fc1891c7
PM
10920 /* ARMv6/XScale extended small page format */
10921 if (arm_feature(env, ARM_FEATURE_XSCALE)
10922 || arm_feature(env, ARM_FEATURE_V6)) {
554b0b09 10923 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
fc1891c7 10924 *page_size = 0x1000;
554b0b09 10925 } else {
fc1891c7
PM
10926 /* UNPREDICTABLE in ARMv5; we choose to take a
10927 * page translation fault.
10928 */
f989983e 10929 fi->type = ARMFault_Translation;
554b0b09
PM
10930 goto do_fault;
10931 }
10932 } else {
10933 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
fc1891c7 10934 *page_size = 0x400;
554b0b09 10935 }
9ee6e8bb 10936 ap = (desc >> 4) & 3;
ce819861
PB
10937 break;
10938 default:
9ee6e8bb 10939 /* Never happens, but compiler isn't smart enough to tell. */
d385a605 10940 g_assert_not_reached();
ce819861 10941 }
9ee6e8bb 10942 }
0fbf5238
AJ
10943 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
10944 *prot |= *prot ? PAGE_EXEC : 0;
10945 if (!(*prot & (1 << access_type))) {
9ee6e8bb 10946 /* Access permission fault. */
f989983e 10947 fi->type = ARMFault_Permission;
9ee6e8bb
PB
10948 goto do_fault;
10949 }
10950 *phys_ptr = phys_addr;
b7cc4e82 10951 return false;
9ee6e8bb 10952do_fault:
f989983e
PM
10953 fi->domain = domain;
10954 fi->level = level;
b7cc4e82 10955 return true;
9ee6e8bb
PB
10956}
10957
b7cc4e82 10958static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
03ae85f8 10959 MMUAccessType access_type, ARMMMUIdx mmu_idx,
b7cc4e82 10960 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
f06cf243 10961 target_ulong *page_size, ARMMMUFaultInfo *fi)
9ee6e8bb 10962{
2fc0cc0e 10963 CPUState *cs = env_cpu(env);
0ae0326b 10964 ARMCPU *cpu = env_archcpu(env);
f06cf243 10965 int level = 1;
9ee6e8bb
PB
10966 uint32_t table;
10967 uint32_t desc;
10968 uint32_t xn;
de9b05b8 10969 uint32_t pxn = 0;
9ee6e8bb
PB
10970 int type;
10971 int ap;
de9b05b8 10972 int domain = 0;
dd4ebc2e 10973 int domain_prot;
a8170e5e 10974 hwaddr phys_addr;
0480f69a 10975 uint32_t dacr;
8bf5b6a9 10976 bool ns;
9ee6e8bb
PB
10977
10978 /* Pagetable walk. */
10979 /* Lookup l1 descriptor. */
0480f69a 10980 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
e389be16 10981 /* Section translation fault if page walk is disabled by PD0 or PD1 */
f06cf243 10982 fi->type = ARMFault_Translation;
e389be16
FA
10983 goto do_fault;
10984 }
a614e698 10985 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 10986 mmu_idx, fi);
3b39d734
PM
10987 if (fi->type != ARMFault_None) {
10988 goto do_fault;
10989 }
9ee6e8bb 10990 type = (desc & 3);
0ae0326b 10991 if (type == 0 || (type == 3 && !cpu_isar_feature(aa32_pxn, cpu))) {
de9b05b8
PM
10992 /* Section translation fault, or attempt to use the encoding
10993 * which is Reserved on implementations without PXN.
10994 */
f06cf243 10995 fi->type = ARMFault_Translation;
9ee6e8bb 10996 goto do_fault;
de9b05b8
PM
10997 }
10998 if ((type == 1) || !(desc & (1 << 18))) {
10999 /* Page or Section. */
dd4ebc2e 11000 domain = (desc >> 5) & 0x0f;
9ee6e8bb 11001 }
0480f69a
PM
11002 if (regime_el(env, mmu_idx) == 1) {
11003 dacr = env->cp15.dacr_ns;
11004 } else {
11005 dacr = env->cp15.dacr_s;
11006 }
f06cf243
PM
11007 if (type == 1) {
11008 level = 2;
11009 }
0480f69a 11010 domain_prot = (dacr >> (domain * 2)) & 3;
dd4ebc2e 11011 if (domain_prot == 0 || domain_prot == 2) {
f06cf243
PM
11012 /* Section or Page domain fault */
11013 fi->type = ARMFault_Domain;
9ee6e8bb
PB
11014 goto do_fault;
11015 }
de9b05b8 11016 if (type != 1) {
9ee6e8bb
PB
11017 if (desc & (1 << 18)) {
11018 /* Supersection. */
11019 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
4e42a6ca
SF
11020 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
11021 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
d4c430a8 11022 *page_size = 0x1000000;
b5ff1b31 11023 } else {
9ee6e8bb
PB
11024 /* Section. */
11025 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
d4c430a8 11026 *page_size = 0x100000;
b5ff1b31 11027 }
9ee6e8bb
PB
11028 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
11029 xn = desc & (1 << 4);
de9b05b8 11030 pxn = desc & 1;
8bf5b6a9 11031 ns = extract32(desc, 19, 1);
9ee6e8bb 11032 } else {
0ae0326b 11033 if (cpu_isar_feature(aa32_pxn, cpu)) {
de9b05b8
PM
11034 pxn = (desc >> 2) & 1;
11035 }
8bf5b6a9 11036 ns = extract32(desc, 3, 1);
9ee6e8bb
PB
11037 /* Lookup l2 entry. */
11038 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
a614e698 11039 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
3795a6de 11040 mmu_idx, fi);
3b39d734
PM
11041 if (fi->type != ARMFault_None) {
11042 goto do_fault;
11043 }
9ee6e8bb
PB
11044 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
11045 switch (desc & 3) {
11046 case 0: /* Page translation fault. */
f06cf243 11047 fi->type = ARMFault_Translation;
b5ff1b31 11048 goto do_fault;
9ee6e8bb
PB
11049 case 1: /* 64k page. */
11050 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
11051 xn = desc & (1 << 15);
d4c430a8 11052 *page_size = 0x10000;
9ee6e8bb
PB
11053 break;
11054 case 2: case 3: /* 4k page. */
11055 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
11056 xn = desc & 1;
d4c430a8 11057 *page_size = 0x1000;
9ee6e8bb
PB
11058 break;
11059 default:
11060 /* Never happens, but compiler isn't smart enough to tell. */
d385a605 11061 g_assert_not_reached();
b5ff1b31 11062 }
9ee6e8bb 11063 }
dd4ebc2e 11064 if (domain_prot == 3) {
c0034328
JR
11065 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11066 } else {
0480f69a 11067 if (pxn && !regime_is_user(env, mmu_idx)) {
de9b05b8
PM
11068 xn = 1;
11069 }
f06cf243
PM
11070 if (xn && access_type == MMU_INST_FETCH) {
11071 fi->type = ARMFault_Permission;
c0034328 11072 goto do_fault;
f06cf243 11073 }
9ee6e8bb 11074
d76951b6
AJ
11075 if (arm_feature(env, ARM_FEATURE_V6K) &&
11076 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
11077 /* The simplified model uses AP[0] as an access control bit. */
11078 if ((ap & 1) == 0) {
11079 /* Access flag fault. */
f06cf243 11080 fi->type = ARMFault_AccessFlag;
d76951b6
AJ
11081 goto do_fault;
11082 }
11083 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
11084 } else {
11085 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
c0034328 11086 }
0fbf5238
AJ
11087 if (*prot && !xn) {
11088 *prot |= PAGE_EXEC;
11089 }
11090 if (!(*prot & (1 << access_type))) {
c0034328 11091 /* Access permission fault. */
f06cf243 11092 fi->type = ARMFault_Permission;
c0034328
JR
11093 goto do_fault;
11094 }
3ad493fc 11095 }
8bf5b6a9
PM
11096 if (ns) {
11097 /* The NS bit will (as required by the architecture) have no effect if
11098 * the CPU doesn't support TZ or this is a non-secure translation
11099 * regime, because the attribute will already be non-secure.
11100 */
11101 attrs->secure = false;
11102 }
9ee6e8bb 11103 *phys_ptr = phys_addr;
b7cc4e82 11104 return false;
b5ff1b31 11105do_fault:
f06cf243
PM
11106 fi->domain = domain;
11107 fi->level = level;
b7cc4e82 11108 return true;
b5ff1b31
FB
11109}
11110
1853d5a9 11111/*
a0e966c9 11112 * check_s2_mmu_setup
1853d5a9
EI
11113 * @cpu: ARMCPU
11114 * @is_aa64: True if the translation regime is in AArch64 state
11115 * @startlevel: Suggested starting level
11116 * @inputsize: Bitsize of IPAs
11117 * @stride: Page-table stride (See the ARM ARM)
11118 *
a0e966c9
EI
11119 * Returns true if the suggested S2 translation parameters are OK and
11120 * false otherwise.
1853d5a9 11121 */
a0e966c9 11122static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
49ba115b 11123 int inputsize, int stride, int outputsize)
1853d5a9 11124{
98d68ec2
EI
11125 const int grainsize = stride + 3;
11126 int startsizecheck;
11127
ef56c242
RH
11128 /*
11129 * Negative levels are usually not allowed...
11130 * Except for FEAT_LPA2, 4k page table, 52-bit address space, which
11131 * begins with level -1. Note that previous feature tests will have
11132 * eliminated this combination if it is not enabled.
11133 */
11134 if (level < (inputsize == 52 && stride == 9 ? -1 : 0)) {
1853d5a9
EI
11135 return false;
11136 }
11137
98d68ec2
EI
11138 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
11139 if (startsizecheck < 1 || startsizecheck > stride + 4) {
11140 return false;
11141 }
11142
1853d5a9 11143 if (is_aa64) {
1853d5a9
EI
11144 switch (stride) {
11145 case 13: /* 64KB Pages. */
49ba115b 11146 if (level == 0 || (level == 1 && outputsize <= 42)) {
1853d5a9
EI
11147 return false;
11148 }
11149 break;
11150 case 11: /* 16KB Pages. */
49ba115b 11151 if (level == 0 || (level == 1 && outputsize <= 40)) {
1853d5a9
EI
11152 return false;
11153 }
11154 break;
11155 case 9: /* 4KB Pages. */
49ba115b 11156 if (level == 0 && outputsize <= 42) {
1853d5a9
EI
11157 return false;
11158 }
11159 break;
11160 default:
11161 g_assert_not_reached();
11162 }
3526423e
EI
11163
11164 /* Inputsize checks. */
49ba115b
RH
11165 if (inputsize > outputsize &&
11166 (arm_el_is_aa64(&cpu->env, 1) || inputsize > 40)) {
3526423e
EI
11167 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
11168 return false;
11169 }
1853d5a9 11170 } else {
1853d5a9
EI
11171 /* AArch32 only supports 4KB pages. Assert on that. */
11172 assert(stride == 9);
11173
11174 if (level == 0) {
11175 return false;
11176 }
1853d5a9
EI
11177 }
11178 return true;
11179}
11180
5b2d261d
AB
11181/* Translate from the 4-bit stage 2 representation of
11182 * memory attributes (without cache-allocation hints) to
11183 * the 8-bit representation of the stage 1 MAIR registers
11184 * (which includes allocation hints).
11185 *
11186 * ref: shared/translation/attrs/S2AttrDecode()
11187 * .../S2ConvertAttrsHints()
11188 */
11189static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs)
11190{
11191 uint8_t hiattr = extract32(s2attrs, 2, 2);
11192 uint8_t loattr = extract32(s2attrs, 0, 2);
11193 uint8_t hihint = 0, lohint = 0;
11194
11195 if (hiattr != 0) { /* normal memory */
e04a5752 11196 if (arm_hcr_el2_eff(env) & HCR_CD) { /* cache disabled */
5b2d261d
AB
11197 hiattr = loattr = 1; /* non-cacheable */
11198 } else {
11199 if (hiattr != 1) { /* Write-through or write-back */
11200 hihint = 3; /* RW allocate */
11201 }
11202 if (loattr != 1) { /* Write-through or write-back */
11203 lohint = 3; /* RW allocate */
11204 }
11205 }
11206 }
11207
11208 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint;
11209}
c47eaf9f 11210#endif /* !CONFIG_USER_ONLY */
5b2d261d 11211
f4ecc015
RH
11212/* This mapping is common between ID_AA64MMFR0.PARANGE and TCR_ELx.{I}PS. */
11213static const uint8_t pamax_map[] = {
11214 [0] = 32,
11215 [1] = 36,
11216 [2] = 40,
11217 [3] = 42,
11218 [4] = 44,
11219 [5] = 48,
7a928f43 11220 [6] = 52,
f4ecc015
RH
11221};
11222
71a77257
RH
11223/* The cpu-specific constant value of PAMax; also used by hw/arm/virt. */
11224unsigned int arm_pamax(ARMCPU *cpu)
11225{
71a77257
RH
11226 unsigned int parange =
11227 FIELD_EX64(cpu->isar.id_aa64mmfr0, ID_AA64MMFR0, PARANGE);
11228
11229 /*
11230 * id_aa64mmfr0 is a read-only register so values outside of the
11231 * supported mappings can be considered an implementation error.
11232 */
11233 assert(parange < ARRAY_SIZE(pamax_map));
11234 return pamax_map[parange];
11235}
11236
b830a5ee
RH
11237static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
11238{
11239 if (regime_has_2_ranges(mmu_idx)) {
11240 return extract64(tcr, 37, 2);
b1a10c86 11241 } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
b830a5ee
RH
11242 return 0; /* VTCR_EL2 */
11243 } else {
3e270f67
RH
11244 /* Replicate the single TBI bit so we always have 2 bits. */
11245 return extract32(tcr, 20, 1) * 3;
b830a5ee
RH
11246 }
11247}
11248
11249static int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
11250{
11251 if (regime_has_2_ranges(mmu_idx)) {
11252 return extract64(tcr, 51, 2);
b1a10c86 11253 } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
b830a5ee
RH
11254 return 0; /* VTCR_EL2 */
11255 } else {
3e270f67
RH
11256 /* Replicate the single TBID bit so we always have 2 bits. */
11257 return extract32(tcr, 29, 1) * 3;
b830a5ee
RH
11258 }
11259}
11260
81ae05fa
RH
11261static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
11262{
11263 if (regime_has_2_ranges(mmu_idx)) {
11264 return extract64(tcr, 57, 2);
11265 } else {
11266 /* Replicate the single TCMA bit so we always have 2 bits. */
11267 return extract32(tcr, 30, 1) * 3;
11268 }
11269}
11270
b830a5ee
RH
11271ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11272 ARMMMUIdx mmu_idx, bool data)
ba97be9f
RH
11273{
11274 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
ef56c242
RH
11275 bool epd, hpd, using16k, using64k, tsz_oob, ds;
11276 int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11277 ARMCPU *cpu = env_archcpu(env);
ba97be9f 11278
339370b9 11279 if (!regime_has_2_ranges(mmu_idx)) {
71d18164 11280 select = 0;
ba97be9f
RH
11281 tsz = extract32(tcr, 0, 6);
11282 using64k = extract32(tcr, 14, 1);
11283 using16k = extract32(tcr, 15, 1);
b1a10c86 11284 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
ba97be9f 11285 /* VTCR_EL2 */
b830a5ee 11286 hpd = false;
ba97be9f 11287 } else {
ba97be9f
RH
11288 hpd = extract32(tcr, 24, 1);
11289 }
11290 epd = false;
ef56c242 11291 sh = extract32(tcr, 12, 2);
f4ecc015 11292 ps = extract32(tcr, 16, 3);
ef56c242 11293 ds = extract64(tcr, 32, 1);
ba97be9f 11294 } else {
71d18164
RH
11295 /*
11296 * Bit 55 is always between the two regions, and is canonical for
11297 * determining if address tagging is enabled.
11298 */
11299 select = extract64(va, 55, 1);
11300 if (!select) {
11301 tsz = extract32(tcr, 0, 6);
11302 epd = extract32(tcr, 7, 1);
ef56c242 11303 sh = extract32(tcr, 12, 2);
71d18164
RH
11304 using64k = extract32(tcr, 14, 1);
11305 using16k = extract32(tcr, 15, 1);
71d18164 11306 hpd = extract64(tcr, 41, 1);
71d18164
RH
11307 } else {
11308 int tg = extract32(tcr, 30, 2);
11309 using16k = tg == 1;
11310 using64k = tg == 3;
11311 tsz = extract32(tcr, 16, 6);
11312 epd = extract32(tcr, 23, 1);
ef56c242 11313 sh = extract32(tcr, 28, 2);
71d18164 11314 hpd = extract64(tcr, 42, 1);
71d18164 11315 }
f4ecc015 11316 ps = extract64(tcr, 32, 3);
ef56c242 11317 ds = extract64(tcr, 59, 1);
ba97be9f 11318 }
c36c65ea 11319
ef56c242 11320 if (cpu_isar_feature(aa64_st, cpu)) {
c36c65ea
RDC
11321 max_tsz = 48 - using64k;
11322 } else {
11323 max_tsz = 39;
11324 }
0af312b6 11325
ef56c242
RH
11326 /*
11327 * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11328 * adjust the effective value of DS, as documented.
11329 */
0af312b6
RH
11330 min_tsz = 16;
11331 if (using64k) {
ef56c242
RH
11332 if (cpu_isar_feature(aa64_lva, cpu)) {
11333 min_tsz = 12;
11334 }
11335 ds = false;
11336 } else if (ds) {
11337 switch (mmu_idx) {
11338 case ARMMMUIdx_Stage2:
11339 case ARMMMUIdx_Stage2_S:
11340 if (using16k) {
11341 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11342 } else {
11343 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11344 }
11345 break;
11346 default:
11347 if (using16k) {
11348 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11349 } else {
11350 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11351 }
11352 break;
11353 }
11354 if (ds) {
0af312b6
RH
11355 min_tsz = 12;
11356 }
11357 }
c36c65ea 11358
ebf93ce7
RH
11359 if (tsz > max_tsz) {
11360 tsz = max_tsz;
11361 tsz_oob = true;
11362 } else if (tsz < min_tsz) {
11363 tsz = min_tsz;
11364 tsz_oob = true;
11365 } else {
11366 tsz_oob = false;
11367 }
ba97be9f 11368
b830a5ee
RH
11369 /* Present TBI as a composite with TBID. */
11370 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11371 if (!data) {
11372 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11373 }
11374 tbi = (tbi >> select) & 1;
11375
ba97be9f
RH
11376 return (ARMVAParameters) {
11377 .tsz = tsz,
f4ecc015 11378 .ps = ps,
ef56c242 11379 .sh = sh,
ba97be9f
RH
11380 .select = select,
11381 .tbi = tbi,
11382 .epd = epd,
11383 .hpd = hpd,
11384 .using16k = using16k,
11385 .using64k = using64k,
ebf93ce7 11386 .tsz_oob = tsz_oob,
ef56c242 11387 .ds = ds,
ba97be9f
RH
11388 };
11389}
11390
c47eaf9f 11391#ifndef CONFIG_USER_ONLY
ba97be9f
RH
11392static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va,
11393 ARMMMUIdx mmu_idx)
11394{
11395 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
11396 uint32_t el = regime_el(env, mmu_idx);
11397 int select, tsz;
11398 bool epd, hpd;
11399
b1a10c86
RDC
11400 assert(mmu_idx != ARMMMUIdx_Stage2_S);
11401
97fa9350 11402 if (mmu_idx == ARMMMUIdx_Stage2) {
ba97be9f
RH
11403 /* VTCR */
11404 bool sext = extract32(tcr, 4, 1);
11405 bool sign = extract32(tcr, 3, 1);
11406
11407 /*
11408 * If the sign-extend bit is not the same as t0sz[3], the result
11409 * is unpredictable. Flag this as a guest error.
11410 */
11411 if (sign != sext) {
11412 qemu_log_mask(LOG_GUEST_ERROR,
11413 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n");
11414 }
11415 tsz = sextract32(tcr, 0, 4) + 8;
11416 select = 0;
11417 hpd = false;
11418 epd = false;
11419 } else if (el == 2) {
11420 /* HTCR */
11421 tsz = extract32(tcr, 0, 3);
11422 select = 0;
11423 hpd = extract64(tcr, 24, 1);
11424 epd = false;
11425 } else {
11426 int t0sz = extract32(tcr, 0, 3);
11427 int t1sz = extract32(tcr, 16, 3);
11428
11429 if (t1sz == 0) {
11430 select = va > (0xffffffffu >> t0sz);
11431 } else {
11432 /* Note that we will detect errors later. */
11433 select = va >= ~(0xffffffffu >> t1sz);
11434 }
11435 if (!select) {
11436 tsz = t0sz;
11437 epd = extract32(tcr, 7, 1);
11438 hpd = extract64(tcr, 41, 1);
11439 } else {
11440 tsz = t1sz;
11441 epd = extract32(tcr, 23, 1);
11442 hpd = extract64(tcr, 42, 1);
11443 }
11444 /* For aarch32, hpd0 is not enabled without t2e as well. */
11445 hpd &= extract32(tcr, 6, 1);
11446 }
11447
11448 return (ARMVAParameters) {
11449 .tsz = tsz,
11450 .select = select,
11451 .epd = epd,
11452 .hpd = hpd,
11453 };
11454}
11455
ff7de2fc
PM
11456/**
11457 * get_phys_addr_lpae: perform one stage of page table walk, LPAE format
11458 *
11459 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
11460 * prot and page_size may not be filled in, and the populated fsr value provides
11461 * information on why the translation aborted, in the format of a long-format
11462 * DFSR/IFSR fault register, with the following caveats:
11463 * * the WnR bit is never set (the caller must do this).
11464 *
11465 * @env: CPUARMState
11466 * @address: virtual address to get physical address for
11467 * @access_type: MMU_DATA_LOAD, MMU_DATA_STORE or MMU_INST_FETCH
11468 * @mmu_idx: MMU index indicating required translation regime
11469 * @s1_is_el0: if @mmu_idx is ARMMMUIdx_Stage2 (so this is a stage 2 page table
11470 * walk), must be true if this is stage 2 of a stage 1+2 walk for an
11471 * EL0 access). If @mmu_idx is anything else, @s1_is_el0 is ignored.
11472 * @phys_ptr: set to the physical address corresponding to the virtual address
11473 * @attrs: set to the memory transaction attributes to use
11474 * @prot: set to the permissions for the page containing phys_ptr
11475 * @page_size_ptr: set to the size of the page containing phys_ptr
11476 * @fi: set to fault info if the translation fails
11477 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
11478 */
98e87797 11479static bool get_phys_addr_lpae(CPUARMState *env, uint64_t address,
03ae85f8 11480 MMUAccessType access_type, ARMMMUIdx mmu_idx,
ff7de2fc 11481 bool s1_is_el0,
b7cc4e82 11482 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
da909b2c 11483 target_ulong *page_size_ptr,
5b2d261d 11484 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
3dde962f 11485{
2fc0cc0e 11486 ARMCPU *cpu = env_archcpu(env);
1853d5a9 11487 CPUState *cs = CPU(cpu);
3dde962f 11488 /* Read an LPAE long-descriptor translation table. */
da909b2c 11489 ARMFaultType fault_type = ARMFault_Translation;
1b4093ea 11490 uint32_t level;
ba97be9f 11491 ARMVAParameters param;
3dde962f 11492 uint64_t ttbr;
dddb5223 11493 hwaddr descaddr, indexmask, indexmask_grainsize;
3dde962f 11494 uint32_t tableattrs;
36d820af 11495 target_ulong page_size;
3dde962f 11496 uint32_t attrs;
ba97be9f 11497 int32_t stride;
49ba115b 11498 int addrsize, inputsize, outputsize;
0480f69a 11499 TCR *tcr = regime_tcr(env, mmu_idx);
d8e052b3 11500 int ap, ns, xn, pxn;
88e8add8 11501 uint32_t el = regime_el(env, mmu_idx);
6109769a 11502 uint64_t descaddrmask;
6e99f762 11503 bool aarch64 = arm_el_is_aa64(env, el);
1bafc2ba 11504 bool guarded = false;
0480f69a 11505
07d1be3b 11506 /* TODO: This code does not support shareability levels. */
6e99f762 11507 if (aarch64) {
f4ecc015
RH
11508 int ps;
11509
ba97be9f
RH
11510 param = aa64_va_parameters(env, address, mmu_idx,
11511 access_type != MMU_INST_FETCH);
1b4093ea 11512 level = 0;
ebf93ce7
RH
11513
11514 /*
11515 * If TxSZ is programmed to a value larger than the maximum,
11516 * or smaller than the effective minimum, it is IMPLEMENTATION
11517 * DEFINED whether we behave as if the field were programmed
11518 * within bounds, or if a level 0 Translation fault is generated.
11519 *
11520 * With FEAT_LVA, fault on less than minimum becomes required,
11521 * so our choice is to always raise the fault.
11522 */
11523 if (param.tsz_oob) {
11524 fault_type = ARMFault_Translation;
11525 goto do_fault;
11526 }
11527
ba97be9f
RH
11528 addrsize = 64 - 8 * param.tbi;
11529 inputsize = 64 - param.tsz;
f4ecc015
RH
11530
11531 /*
11532 * Bound PS by PARANGE to find the effective output address size.
11533 * ID_AA64MMFR0 is a read-only register so values outside of the
11534 * supported mappings can be considered an implementation error.
11535 */
11536 ps = FIELD_EX64(cpu->isar.id_aa64mmfr0, ID_AA64MMFR0, PARANGE);
11537 ps = MIN(ps, param.ps);
11538 assert(ps < ARRAY_SIZE(pamax_map));
11539 outputsize = pamax_map[ps];
d0a2cbce 11540 } else {
ba97be9f 11541 param = aa32_va_parameters(env, address, mmu_idx);
1b4093ea 11542 level = 1;
97fa9350 11543 addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32);
ba97be9f 11544 inputsize = addrsize - param.tsz;
49ba115b 11545 outputsize = 40;
2c8dd318 11546 }
3dde962f 11547
ba97be9f
RH
11548 /*
11549 * We determined the region when collecting the parameters, but we
11550 * have not yet validated that the address is valid for the region.
11551 * Extract the top bits and verify that they all match select.
36d820af
RH
11552 *
11553 * For aa32, if inputsize == addrsize, then we have selected the
11554 * region by exclusion in aa32_va_parameters and there is no more
11555 * validation to do here.
11556 */
11557 if (inputsize < addrsize) {
11558 target_ulong top_bits = sextract64(address, inputsize,
11559 addrsize - inputsize);
03f27724 11560 if (-top_bits != param.select) {
36d820af
RH
11561 /* The gap between the two regions is a Translation fault */
11562 fault_type = ARMFault_Translation;
11563 goto do_fault;
11564 }
3dde962f
PM
11565 }
11566
ba97be9f
RH
11567 if (param.using64k) {
11568 stride = 13;
11569 } else if (param.using16k) {
11570 stride = 11;
11571 } else {
11572 stride = 9;
11573 }
11574
3dde962f
PM
11575 /* Note that QEMU ignores shareability and cacheability attributes,
11576 * so we don't need to do anything with the SH, ORGN, IRGN fields
11577 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
11578 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
11579 * implement any ASID-like capability so we can ignore it (instead
11580 * we will always flush the TLB any time the ASID is changed).
11581 */
ba97be9f 11582 ttbr = regime_ttbr(env, mmu_idx, param.select);
3dde962f 11583
0480f69a 11584 /* Here we should have set up all the parameters for the translation:
6e99f762 11585 * inputsize, ttbr, epd, stride, tbi
0480f69a
PM
11586 */
11587
ba97be9f 11588 if (param.epd) {
88e8add8
GB
11589 /* Translation table walk disabled => Translation fault on TLB miss
11590 * Note: This is always 0 on 64-bit EL2 and EL3.
11591 */
3dde962f
PM
11592 goto do_fault;
11593 }
11594
b1a10c86 11595 if (mmu_idx != ARMMMUIdx_Stage2 && mmu_idx != ARMMMUIdx_Stage2_S) {
1853d5a9
EI
11596 /* The starting level depends on the virtual address size (which can
11597 * be up to 48 bits) and the translation granule size. It indicates
11598 * the number of strides (stride bits at a time) needed to
11599 * consume the bits of the input address. In the pseudocode this is:
11600 * level = 4 - RoundUp((inputsize - grainsize) / stride)
11601 * where their 'inputsize' is our 'inputsize', 'grainsize' is
11602 * our 'stride + 3' and 'stride' is our 'stride'.
11603 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
11604 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
11605 * = 4 - (inputsize - 4) / stride;
11606 */
11607 level = 4 - (inputsize - 4) / stride;
11608 } else {
11609 /* For stage 2 translations the starting level is specified by the
11610 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
11611 */
1b4093ea 11612 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
ef56c242 11613 uint32_t sl2 = extract64(tcr->raw_tcr, 33, 1);
1b4093ea 11614 uint32_t startlevel;
1853d5a9
EI
11615 bool ok;
11616
ef56c242
RH
11617 /* SL2 is RES0 unless DS=1 & 4kb granule. */
11618 if (param.ds && stride == 9 && sl2) {
11619 if (sl0 != 0) {
11620 level = 0;
11621 fault_type = ARMFault_Translation;
11622 goto do_fault;
11623 }
11624 startlevel = -1;
11625 } else if (!aarch64 || stride == 9) {
1853d5a9 11626 /* AArch32 or 4KB pages */
1b4093ea 11627 startlevel = 2 - sl0;
c36c65ea
RDC
11628
11629 if (cpu_isar_feature(aa64_st, cpu)) {
11630 startlevel &= 3;
11631 }
1853d5a9
EI
11632 } else {
11633 /* 16KB or 64KB pages */
1b4093ea 11634 startlevel = 3 - sl0;
1853d5a9
EI
11635 }
11636
11637 /* Check that the starting level is valid. */
6e99f762 11638 ok = check_s2_mmu_setup(cpu, aarch64, startlevel,
49ba115b 11639 inputsize, stride, outputsize);
1853d5a9 11640 if (!ok) {
da909b2c 11641 fault_type = ARMFault_Translation;
1853d5a9
EI
11642 goto do_fault;
11643 }
1b4093ea 11644 level = startlevel;
1853d5a9 11645 }
3dde962f 11646
d06449f2
RH
11647 indexmask_grainsize = MAKE_64BIT_MASK(0, stride + 3);
11648 indexmask = MAKE_64BIT_MASK(0, inputsize - (stride * (4 - level)));
3dde962f
PM
11649
11650 /* Now we can extract the actual base address from the TTBR */
2c8dd318 11651 descaddr = extract64(ttbr, 0, 48);
f4ecc015
RH
11652
11653 /*
7a928f43
RH
11654 * For FEAT_LPA and PS=6, bits [51:48] of descaddr are in [5:2] of TTBR.
11655 *
11656 * Otherwise, if the base address is out of range, raise AddressSizeFault.
f4ecc015
RH
11657 * In the pseudocode, this is !IsZero(baseregister<47:outputsize>),
11658 * but we've just cleared the bits above 47, so simplify the test.
11659 */
7a928f43
RH
11660 if (outputsize > 48) {
11661 descaddr |= extract64(ttbr, 2, 4) << 48;
11662 } else if (descaddr >> outputsize) {
f4ecc015
RH
11663 level = 0;
11664 fault_type = ARMFault_AddressSize;
11665 goto do_fault;
11666 }
11667
41a4bf1f
PM
11668 /*
11669 * We rely on this masking to clear the RES0 bits at the bottom of the TTBR
11670 * and also to mask out CnP (bit 0) which could validly be non-zero.
11671 */
dddb5223 11672 descaddr &= ~indexmask;
3dde962f 11673
f4ecc015
RH
11674 /*
11675 * For AArch32, the address field in the descriptor goes up to bit 39
11676 * for both v7 and v8. However, for v8 the SBZ bits [47:40] must be 0
11677 * or an AddressSize fault is raised. So for v8 we extract those SBZ
11678 * bits as part of the address, which will be checked via outputsize.
ef56c242
RH
11679 * For AArch64, the address field goes up to bit 47, or 49 with FEAT_LPA2;
11680 * the highest bits of a 52-bit output are placed elsewhere.
6109769a 11681 */
ef56c242
RH
11682 if (param.ds) {
11683 descaddrmask = MAKE_64BIT_MASK(0, 50);
11684 } else if (arm_feature(env, ARM_FEATURE_V8)) {
f4ecc015
RH
11685 descaddrmask = MAKE_64BIT_MASK(0, 48);
11686 } else {
11687 descaddrmask = MAKE_64BIT_MASK(0, 40);
11688 }
11689 descaddrmask &= ~indexmask_grainsize;
6109769a 11690
ebca90e4
PM
11691 /* Secure accesses start with the page table in secure memory and
11692 * can be downgraded to non-secure at any step. Non-secure accesses
11693 * remain non-secure. We implement this by just ORing in the NSTable/NS
11694 * bits at each step.
11695 */
11696 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
3dde962f
PM
11697 for (;;) {
11698 uint64_t descriptor;
ebca90e4 11699 bool nstable;
3dde962f 11700
dddb5223 11701 descaddr |= (address >> (stride * (4 - level))) & indexmask;
2c8dd318 11702 descaddr &= ~7ULL;
ebca90e4 11703 nstable = extract32(tableattrs, 4, 1);
3795a6de 11704 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi);
3b39d734 11705 if (fi->type != ARMFault_None) {
37785977
EI
11706 goto do_fault;
11707 }
11708
3dde962f
PM
11709 if (!(descriptor & 1) ||
11710 (!(descriptor & 2) && (level == 3))) {
11711 /* Invalid, or the Reserved level 3 encoding */
11712 goto do_fault;
11713 }
f4ecc015 11714
6109769a 11715 descaddr = descriptor & descaddrmask;
7a928f43
RH
11716
11717 /*
11718 * For FEAT_LPA and PS=6, bits [51:48] of descaddr are in [15:12]
ef56c242
RH
11719 * of descriptor. For FEAT_LPA2 and effective DS, bits [51:50] of
11720 * descaddr are in [9:8]. Otherwise, if descaddr is out of range,
11721 * raise AddressSizeFault.
7a928f43
RH
11722 */
11723 if (outputsize > 48) {
ef56c242
RH
11724 if (param.ds) {
11725 descaddr |= extract64(descriptor, 8, 2) << 50;
11726 } else {
11727 descaddr |= extract64(descriptor, 12, 4) << 48;
11728 }
7a928f43 11729 } else if (descaddr >> outputsize) {
f4ecc015
RH
11730 fault_type = ARMFault_AddressSize;
11731 goto do_fault;
11732 }
3dde962f
PM
11733
11734 if ((descriptor & 2) && (level < 3)) {
037c13c5 11735 /* Table entry. The top five bits are attributes which may
3dde962f
PM
11736 * propagate down through lower levels of the table (and
11737 * which are all arranged so that 0 means "no effect", so
11738 * we can gather them up by ORing in the bits at each level).
11739 */
11740 tableattrs |= extract64(descriptor, 59, 5);
11741 level++;
dddb5223 11742 indexmask = indexmask_grainsize;
3dde962f
PM
11743 continue;
11744 }
39a1fd25
PM
11745 /*
11746 * Block entry at level 1 or 2, or page entry at level 3.
3dde962f 11747 * These are basically the same thing, although the number
39a1fd25
PM
11748 * of bits we pull in from the vaddr varies. Note that although
11749 * descaddrmask masks enough of the low bits of the descriptor
11750 * to give a correct page or table address, the address field
11751 * in a block descriptor is smaller; so we need to explicitly
11752 * clear the lower bits here before ORing in the low vaddr bits.
3dde962f 11753 */
973a5434 11754 page_size = (1ULL << ((stride * (4 - level)) + 3));
39a1fd25 11755 descaddr &= ~(page_size - 1);
3dde962f 11756 descaddr |= (address & (page_size - 1));
6ab1a5ee 11757 /* Extract attributes from the descriptor */
d615efac
IC
11758 attrs = extract64(descriptor, 2, 10)
11759 | (extract64(descriptor, 52, 12) << 10);
6ab1a5ee 11760
b1a10c86 11761 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
6ab1a5ee
EI
11762 /* Stage 2 table descriptors do not include any attribute fields */
11763 break;
11764 }
11765 /* Merge in attributes from table descriptors */
037c13c5 11766 attrs |= nstable << 3; /* NS */
1bafc2ba 11767 guarded = extract64(descriptor, 50, 1); /* GP */
ba97be9f 11768 if (param.hpd) {
037c13c5
RH
11769 /* HPD disables all the table attributes except NSTable. */
11770 break;
11771 }
11772 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
3dde962f
PM
11773 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
11774 * means "force PL1 access only", which means forcing AP[1] to 0.
11775 */
037c13c5
RH
11776 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */
11777 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */
3dde962f
PM
11778 break;
11779 }
11780 /* Here descaddr is the final physical address, and attributes
11781 * are all in attrs.
11782 */
da909b2c 11783 fault_type = ARMFault_AccessFlag;
3dde962f
PM
11784 if ((attrs & (1 << 8)) == 0) {
11785 /* Access flag */
11786 goto do_fault;
11787 }
d8e052b3
AJ
11788
11789 ap = extract32(attrs, 4, 2);
d8e052b3 11790
b1a10c86
RDC
11791 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
11792 ns = mmu_idx == ARMMMUIdx_Stage2;
ce3125be
PM
11793 xn = extract32(attrs, 11, 2);
11794 *prot = get_S2prot(env, ap, xn, s1_is_el0);
6ab1a5ee
EI
11795 } else {
11796 ns = extract32(attrs, 3, 1);
ce3125be 11797 xn = extract32(attrs, 12, 1);
6ab1a5ee 11798 pxn = extract32(attrs, 11, 1);
6e99f762 11799 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn);
6ab1a5ee 11800 }
d8e052b3 11801
da909b2c 11802 fault_type = ARMFault_Permission;
d8e052b3 11803 if (!(*prot & (1 << access_type))) {
3dde962f
PM
11804 goto do_fault;
11805 }
3dde962f 11806
8bf5b6a9
PM
11807 if (ns) {
11808 /* The NS bit will (as required by the architecture) have no effect if
11809 * the CPU doesn't support TZ or this is a non-secure translation
11810 * regime, because the attribute will already be non-secure.
11811 */
11812 txattrs->secure = false;
11813 }
1bafc2ba
RH
11814 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */
11815 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) {
149d3b31 11816 arm_tlb_bti_gp(txattrs) = true;
1bafc2ba 11817 }
5b2d261d 11818
b1a10c86 11819 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
7e98e21c
RH
11820 cacheattrs->attrs = convert_stage2_attrs(env, extract32(attrs, 0, 4));
11821 } else {
11822 /* Index into MAIR registers for cache attributes */
11823 uint8_t attrindx = extract32(attrs, 0, 3);
11824 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)];
11825 assert(attrindx <= 7);
11826 cacheattrs->attrs = extract64(mair, attrindx * 8, 8);
5b2d261d 11827 }
ef56c242
RH
11828
11829 /*
11830 * For FEAT_LPA2 and effective DS, the SH field in the attributes
11831 * was re-purposed for output address bits. The SH attribute in
11832 * that case comes from TCR_ELx, which we extracted earlier.
11833 */
11834 if (param.ds) {
11835 cacheattrs->shareability = param.sh;
11836 } else {
11837 cacheattrs->shareability = extract32(attrs, 6, 2);
11838 }
5b2d261d 11839
3dde962f
PM
11840 *phys_ptr = descaddr;
11841 *page_size_ptr = page_size;
b7cc4e82 11842 return false;
3dde962f
PM
11843
11844do_fault:
da909b2c
PM
11845 fi->type = fault_type;
11846 fi->level = level;
37785977 11847 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
b1a10c86
RDC
11848 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_Stage2 ||
11849 mmu_idx == ARMMMUIdx_Stage2_S);
9861248f 11850 fi->s1ns = mmu_idx == ARMMMUIdx_Stage2;
b7cc4e82 11851 return true;
3dde962f
PM
11852}
11853
f6bda88f
PC
11854static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
11855 ARMMMUIdx mmu_idx,
11856 int32_t address, int *prot)
11857{
3a00d560
MD
11858 if (!arm_feature(env, ARM_FEATURE_M)) {
11859 *prot = PAGE_READ | PAGE_WRITE;
11860 switch (address) {
11861 case 0xF0000000 ... 0xFFFFFFFF:
11862 if (regime_sctlr(env, mmu_idx) & SCTLR_V) {
11863 /* hivecs execing is ok */
11864 *prot |= PAGE_EXEC;
11865 }
11866 break;
11867 case 0x00000000 ... 0x7FFFFFFF:
f6bda88f 11868 *prot |= PAGE_EXEC;
3a00d560
MD
11869 break;
11870 }
11871 } else {
11872 /* Default system address map for M profile cores.
11873 * The architecture specifies which regions are execute-never;
11874 * at the MPU level no other checks are defined.
11875 */
11876 switch (address) {
11877 case 0x00000000 ... 0x1fffffff: /* ROM */
11878 case 0x20000000 ... 0x3fffffff: /* SRAM */
11879 case 0x60000000 ... 0x7fffffff: /* RAM */
11880 case 0x80000000 ... 0x9fffffff: /* RAM */
11881 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
11882 break;
11883 case 0x40000000 ... 0x5fffffff: /* Peripheral */
11884 case 0xa0000000 ... 0xbfffffff: /* Device */
11885 case 0xc0000000 ... 0xdfffffff: /* Device */
11886 case 0xe0000000 ... 0xffffffff: /* System */
11887 *prot = PAGE_READ | PAGE_WRITE;
11888 break;
11889 default:
11890 g_assert_not_reached();
f6bda88f 11891 }
f6bda88f 11892 }
f6bda88f
PC
11893}
11894
29c483a5
MD
11895static bool pmsav7_use_background_region(ARMCPU *cpu,
11896 ARMMMUIdx mmu_idx, bool is_user)
11897{
11898 /* Return true if we should use the default memory map as a
11899 * "background" region if there are no hits against any MPU regions.
11900 */
11901 CPUARMState *env = &cpu->env;
11902
11903 if (is_user) {
11904 return false;
11905 }
11906
11907 if (arm_feature(env, ARM_FEATURE_M)) {
ecf5e8ea
PM
11908 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)]
11909 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK;
29c483a5
MD
11910 } else {
11911 return regime_sctlr(env, mmu_idx) & SCTLR_BR;
11912 }
11913}
11914
38aaa60c
PM
11915static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address)
11916{
11917 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */
11918 return arm_feature(env, ARM_FEATURE_M) &&
11919 extract32(address, 20, 12) == 0xe00;
11920}
11921
bf446a11
PM
11922static inline bool m_is_system_region(CPUARMState *env, uint32_t address)
11923{
11924 /* True if address is in the M profile system region
11925 * 0xe0000000 - 0xffffffff
11926 */
11927 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7;
11928}
11929
f6bda88f 11930static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
03ae85f8 11931 MMUAccessType access_type, ARMMMUIdx mmu_idx,
9375ad15 11932 hwaddr *phys_ptr, int *prot,
e5e40999 11933 target_ulong *page_size,
9375ad15 11934 ARMMMUFaultInfo *fi)
f6bda88f 11935{
2fc0cc0e 11936 ARMCPU *cpu = env_archcpu(env);
f6bda88f
PC
11937 int n;
11938 bool is_user = regime_is_user(env, mmu_idx);
11939
11940 *phys_ptr = address;
e5e40999 11941 *page_size = TARGET_PAGE_SIZE;
f6bda88f
PC
11942 *prot = 0;
11943
38aaa60c
PM
11944 if (regime_translation_disabled(env, mmu_idx) ||
11945 m_is_ppb_region(env, address)) {
11946 /* MPU disabled or M profile PPB access: use default memory map.
11947 * The other case which uses the default memory map in the
11948 * v7M ARM ARM pseudocode is exception vector reads from the vector
11949 * table. In QEMU those accesses are done in arm_v7m_load_vector(),
11950 * which always does a direct read using address_space_ldl(), rather
11951 * than going via this function, so we don't need to check that here.
11952 */
f6bda88f
PC
11953 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
11954 } else { /* MPU enabled */
11955 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
11956 /* region search */
11957 uint32_t base = env->pmsav7.drbar[n];
11958 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
11959 uint32_t rmask;
11960 bool srdis = false;
11961
11962 if (!(env->pmsav7.drsr[n] & 0x1)) {
11963 continue;
11964 }
11965
11966 if (!rsize) {
c9f9f124
MD
11967 qemu_log_mask(LOG_GUEST_ERROR,
11968 "DRSR[%d]: Rsize field cannot be 0\n", n);
f6bda88f
PC
11969 continue;
11970 }
11971 rsize++;
11972 rmask = (1ull << rsize) - 1;
11973
11974 if (base & rmask) {
c9f9f124
MD
11975 qemu_log_mask(LOG_GUEST_ERROR,
11976 "DRBAR[%d]: 0x%" PRIx32 " misaligned "
11977 "to DRSR region size, mask = 0x%" PRIx32 "\n",
11978 n, base, rmask);
f6bda88f
PC
11979 continue;
11980 }
11981
11982 if (address < base || address > base + rmask) {
9d2b5a58
PM
11983 /*
11984 * Address not in this region. We must check whether the
11985 * region covers addresses in the same page as our address.
11986 * In that case we must not report a size that covers the
11987 * whole page for a subsequent hit against a different MPU
11988 * region or the background region, because it would result in
11989 * incorrect TLB hits for subsequent accesses to addresses that
11990 * are in this MPU region.
11991 */
11992 if (ranges_overlap(base, rmask,
11993 address & TARGET_PAGE_MASK,
11994 TARGET_PAGE_SIZE)) {
11995 *page_size = 1;
11996 }
f6bda88f
PC
11997 continue;
11998 }
11999
12000 /* Region matched */
12001
12002 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
12003 int i, snd;
12004 uint32_t srdis_mask;
12005
12006 rsize -= 3; /* sub region size (power of 2) */
12007 snd = ((address - base) >> rsize) & 0x7;
12008 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
12009
12010 srdis_mask = srdis ? 0x3 : 0x0;
12011 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
12012 /* This will check in groups of 2, 4 and then 8, whether
12013 * the subregion bits are consistent. rsize is incremented
12014 * back up to give the region size, considering consistent
12015 * adjacent subregions as one region. Stop testing if rsize
12016 * is already big enough for an entire QEMU page.
12017 */
12018 int snd_rounded = snd & ~(i - 1);
12019 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
12020 snd_rounded + 8, i);
12021 if (srdis_mask ^ srdis_multi) {
12022 break;
12023 }
12024 srdis_mask = (srdis_mask << i) | srdis_mask;
12025 rsize++;
12026 }
12027 }
f6bda88f
PC
12028 if (srdis) {
12029 continue;
12030 }
e5e40999
PM
12031 if (rsize < TARGET_PAGE_BITS) {
12032 *page_size = 1 << rsize;
12033 }
f6bda88f
PC
12034 break;
12035 }
12036
12037 if (n == -1) { /* no hits */
29c483a5 12038 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
f6bda88f 12039 /* background fault */
9375ad15 12040 fi->type = ARMFault_Background;
f6bda88f
PC
12041 return true;
12042 }
12043 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
12044 } else { /* a MPU hit! */
12045 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
bf446a11
PM
12046 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1);
12047
12048 if (m_is_system_region(env, address)) {
12049 /* System space is always execute never */
12050 xn = 1;
12051 }
f6bda88f
PC
12052
12053 if (is_user) { /* User mode AP bit decoding */
12054 switch (ap) {
12055 case 0:
12056 case 1:
12057 case 5:
12058 break; /* no access */
12059 case 3:
12060 *prot |= PAGE_WRITE;
12061 /* fall through */
12062 case 2:
12063 case 6:
12064 *prot |= PAGE_READ | PAGE_EXEC;
12065 break;
8638f1ad
PM
12066 case 7:
12067 /* for v7M, same as 6; for R profile a reserved value */
12068 if (arm_feature(env, ARM_FEATURE_M)) {
12069 *prot |= PAGE_READ | PAGE_EXEC;
12070 break;
12071 }
12072 /* fall through */
f6bda88f
PC
12073 default:
12074 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
12075 "DRACR[%d]: Bad value for AP bits: 0x%"
12076 PRIx32 "\n", n, ap);
f6bda88f
PC
12077 }
12078 } else { /* Priv. mode AP bits decoding */
12079 switch (ap) {
12080 case 0:
12081 break; /* no access */
12082 case 1:
12083 case 2:
12084 case 3:
12085 *prot |= PAGE_WRITE;
12086 /* fall through */
12087 case 5:
12088 case 6:
12089 *prot |= PAGE_READ | PAGE_EXEC;
12090 break;
8638f1ad
PM
12091 case 7:
12092 /* for v7M, same as 6; for R profile a reserved value */
12093 if (arm_feature(env, ARM_FEATURE_M)) {
12094 *prot |= PAGE_READ | PAGE_EXEC;
12095 break;
12096 }
12097 /* fall through */
f6bda88f
PC
12098 default:
12099 qemu_log_mask(LOG_GUEST_ERROR,
c9f9f124
MD
12100 "DRACR[%d]: Bad value for AP bits: 0x%"
12101 PRIx32 "\n", n, ap);
f6bda88f
PC
12102 }
12103 }
12104
12105 /* execute never */
bf446a11 12106 if (xn) {
f6bda88f
PC
12107 *prot &= ~PAGE_EXEC;
12108 }
12109 }
12110 }
12111
9375ad15
PM
12112 fi->type = ARMFault_Permission;
12113 fi->level = 1;
f6bda88f
PC
12114 return !(*prot & (1 << access_type));
12115}
12116
35337cc3
PM
12117static bool v8m_is_sau_exempt(CPUARMState *env,
12118 uint32_t address, MMUAccessType access_type)
12119{
12120 /* The architecture specifies that certain address ranges are
12121 * exempt from v8M SAU/IDAU checks.
12122 */
12123 return
12124 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) ||
12125 (address >= 0xe0000000 && address <= 0xe0002fff) ||
12126 (address >= 0xe000e000 && address <= 0xe000efff) ||
12127 (address >= 0xe002e000 && address <= 0xe002efff) ||
12128 (address >= 0xe0040000 && address <= 0xe0041fff) ||
12129 (address >= 0xe00ff000 && address <= 0xe00fffff);
12130}
12131
787a7e76 12132void v8m_security_lookup(CPUARMState *env, uint32_t address,
35337cc3
PM
12133 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12134 V8M_SAttributes *sattrs)
12135{
12136 /* Look up the security attributes for this address. Compare the
12137 * pseudocode SecurityCheck() function.
12138 * We assume the caller has zero-initialized *sattrs.
12139 */
2fc0cc0e 12140 ARMCPU *cpu = env_archcpu(env);
35337cc3 12141 int r;
181962fd
PM
12142 bool idau_exempt = false, idau_ns = true, idau_nsc = true;
12143 int idau_region = IREGION_NOTVALID;
72042435
PM
12144 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
12145 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
35337cc3 12146
181962fd
PM
12147 if (cpu->idau) {
12148 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau);
12149 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau);
12150
12151 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns,
12152 &idau_nsc);
12153 }
35337cc3
PM
12154
12155 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) {
12156 /* 0xf0000000..0xffffffff is always S for insn fetches */
12157 return;
12158 }
12159
181962fd 12160 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) {
35337cc3
PM
12161 sattrs->ns = !regime_is_secure(env, mmu_idx);
12162 return;
12163 }
12164
181962fd
PM
12165 if (idau_region != IREGION_NOTVALID) {
12166 sattrs->irvalid = true;
12167 sattrs->iregion = idau_region;
12168 }
12169
35337cc3
PM
12170 switch (env->sau.ctrl & 3) {
12171 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */
12172 break;
12173 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */
12174 sattrs->ns = true;
12175 break;
12176 default: /* SAU.ENABLE == 1 */
12177 for (r = 0; r < cpu->sau_sregion; r++) {
12178 if (env->sau.rlar[r] & 1) {
12179 uint32_t base = env->sau.rbar[r] & ~0x1f;
12180 uint32_t limit = env->sau.rlar[r] | 0x1f;
12181
12182 if (base <= address && limit >= address) {
72042435
PM
12183 if (base > addr_page_base || limit < addr_page_limit) {
12184 sattrs->subpage = true;
12185 }
35337cc3
PM
12186 if (sattrs->srvalid) {
12187 /* If we hit in more than one region then we must report
12188 * as Secure, not NS-Callable, with no valid region
12189 * number info.
12190 */
12191 sattrs->ns = false;
12192 sattrs->nsc = false;
12193 sattrs->sregion = 0;
12194 sattrs->srvalid = false;
12195 break;
12196 } else {
12197 if (env->sau.rlar[r] & 2) {
12198 sattrs->nsc = true;
12199 } else {
12200 sattrs->ns = true;
12201 }
12202 sattrs->srvalid = true;
12203 sattrs->sregion = r;
12204 }
9d2b5a58
PM
12205 } else {
12206 /*
12207 * Address not in this region. We must check whether the
12208 * region covers addresses in the same page as our address.
12209 * In that case we must not report a size that covers the
12210 * whole page for a subsequent hit against a different MPU
12211 * region or the background region, because it would result
12212 * in incorrect TLB hits for subsequent accesses to
12213 * addresses that are in this MPU region.
12214 */
12215 if (limit >= base &&
12216 ranges_overlap(base, limit - base + 1,
12217 addr_page_base,
12218 TARGET_PAGE_SIZE)) {
12219 sattrs->subpage = true;
12220 }
35337cc3
PM
12221 }
12222 }
12223 }
7e3f1223
TR
12224 break;
12225 }
35337cc3 12226
7e3f1223
TR
12227 /*
12228 * The IDAU will override the SAU lookup results if it specifies
12229 * higher security than the SAU does.
12230 */
12231 if (!idau_ns) {
12232 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) {
12233 sattrs->ns = false;
12234 sattrs->nsc = idau_nsc;
181962fd 12235 }
35337cc3
PM
12236 }
12237}
12238
787a7e76 12239bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address,
54317c0f
PM
12240 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12241 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
12242 int *prot, bool *is_subpage,
12243 ARMMMUFaultInfo *fi, uint32_t *mregion)
54317c0f
PM
12244{
12245 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check
12246 * that a full phys-to-virt translation does).
12247 * mregion is (if not NULL) set to the region number which matched,
12248 * or -1 if no region number is returned (MPU off, address did not
12249 * hit a region, address hit in multiple regions).
72042435
PM
12250 * We set is_subpage to true if the region hit doesn't cover the
12251 * entire TARGET_PAGE the address is within.
54317c0f 12252 */
2fc0cc0e 12253 ARMCPU *cpu = env_archcpu(env);
504e3cc3 12254 bool is_user = regime_is_user(env, mmu_idx);
62c58ee0 12255 uint32_t secure = regime_is_secure(env, mmu_idx);
504e3cc3
PM
12256 int n;
12257 int matchregion = -1;
12258 bool hit = false;
72042435
PM
12259 uint32_t addr_page_base = address & TARGET_PAGE_MASK;
12260 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1);
504e3cc3 12261
72042435 12262 *is_subpage = false;
504e3cc3
PM
12263 *phys_ptr = address;
12264 *prot = 0;
54317c0f
PM
12265 if (mregion) {
12266 *mregion = -1;
35337cc3
PM
12267 }
12268
504e3cc3
PM
12269 /* Unlike the ARM ARM pseudocode, we don't need to check whether this
12270 * was an exception vector read from the vector table (which is always
12271 * done using the default system address map), because those accesses
12272 * are done in arm_v7m_load_vector(), which always does a direct
12273 * read using address_space_ldl(), rather than going via this function.
12274 */
12275 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
12276 hit = true;
12277 } else if (m_is_ppb_region(env, address)) {
12278 hit = true;
504e3cc3 12279 } else {
cff21316
PM
12280 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) {
12281 hit = true;
12282 }
12283
504e3cc3
PM
12284 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
12285 /* region search */
12286 /* Note that the base address is bits [31:5] from the register
12287 * with bits [4:0] all zeroes, but the limit address is bits
12288 * [31:5] from the register with bits [4:0] all ones.
12289 */
62c58ee0
PM
12290 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f;
12291 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f;
504e3cc3 12292
62c58ee0 12293 if (!(env->pmsav8.rlar[secure][n] & 0x1)) {
504e3cc3
PM
12294 /* Region disabled */
12295 continue;
12296 }
12297
12298 if (address < base || address > limit) {
9d2b5a58
PM
12299 /*
12300 * Address not in this region. We must check whether the
12301 * region covers addresses in the same page as our address.
12302 * In that case we must not report a size that covers the
12303 * whole page for a subsequent hit against a different MPU
12304 * region or the background region, because it would result in
12305 * incorrect TLB hits for subsequent accesses to addresses that
12306 * are in this MPU region.
12307 */
12308 if (limit >= base &&
12309 ranges_overlap(base, limit - base + 1,
12310 addr_page_base,
12311 TARGET_PAGE_SIZE)) {
12312 *is_subpage = true;
12313 }
504e3cc3
PM
12314 continue;
12315 }
12316
72042435
PM
12317 if (base > addr_page_base || limit < addr_page_limit) {
12318 *is_subpage = true;
12319 }
12320
cff21316 12321 if (matchregion != -1) {
504e3cc3
PM
12322 /* Multiple regions match -- always a failure (unlike
12323 * PMSAv7 where highest-numbered-region wins)
12324 */
3f551b5b
PM
12325 fi->type = ARMFault_Permission;
12326 fi->level = 1;
504e3cc3
PM
12327 return true;
12328 }
12329
12330 matchregion = n;
12331 hit = true;
504e3cc3
PM
12332 }
12333 }
12334
12335 if (!hit) {
12336 /* background fault */
3f551b5b 12337 fi->type = ARMFault_Background;
504e3cc3
PM
12338 return true;
12339 }
12340
12341 if (matchregion == -1) {
12342 /* hit using the background region */
12343 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
12344 } else {
62c58ee0
PM
12345 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2);
12346 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1);
cad8e2e3
PM
12347 bool pxn = false;
12348
12349 if (arm_feature(env, ARM_FEATURE_V8_1M)) {
12350 pxn = extract32(env->pmsav8.rlar[secure][matchregion], 4, 1);
12351 }
504e3cc3
PM
12352
12353 if (m_is_system_region(env, address)) {
12354 /* System space is always execute never */
12355 xn = 1;
12356 }
12357
12358 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap);
cad8e2e3 12359 if (*prot && !xn && !(pxn && !is_user)) {
504e3cc3
PM
12360 *prot |= PAGE_EXEC;
12361 }
12362 /* We don't need to look the attribute up in the MAIR0/MAIR1
12363 * registers because that only tells us about cacheability.
12364 */
54317c0f
PM
12365 if (mregion) {
12366 *mregion = matchregion;
12367 }
504e3cc3
PM
12368 }
12369
3f551b5b
PM
12370 fi->type = ARMFault_Permission;
12371 fi->level = 1;
504e3cc3
PM
12372 return !(*prot & (1 << access_type));
12373}
12374
54317c0f
PM
12375
12376static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address,
12377 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12378 hwaddr *phys_ptr, MemTxAttrs *txattrs,
72042435
PM
12379 int *prot, target_ulong *page_size,
12380 ARMMMUFaultInfo *fi)
54317c0f
PM
12381{
12382 uint32_t secure = regime_is_secure(env, mmu_idx);
12383 V8M_SAttributes sattrs = {};
72042435
PM
12384 bool ret;
12385 bool mpu_is_subpage;
54317c0f
PM
12386
12387 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
12388 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs);
12389 if (access_type == MMU_INST_FETCH) {
12390 /* Instruction fetches always use the MMU bank and the
12391 * transaction attribute determined by the fetch address,
12392 * regardless of CPU state. This is painful for QEMU
12393 * to handle, because it would mean we need to encode
12394 * into the mmu_idx not just the (user, negpri) information
12395 * for the current security state but also that for the
12396 * other security state, which would balloon the number
12397 * of mmu_idx values needed alarmingly.
12398 * Fortunately we can avoid this because it's not actually
12399 * possible to arbitrarily execute code from memory with
12400 * the wrong security attribute: it will always generate
12401 * an exception of some kind or another, apart from the
12402 * special case of an NS CPU executing an SG instruction
12403 * in S&NSC memory. So we always just fail the translation
12404 * here and sort things out in the exception handler
12405 * (including possibly emulating an SG instruction).
12406 */
12407 if (sattrs.ns != !secure) {
3f551b5b
PM
12408 if (sattrs.nsc) {
12409 fi->type = ARMFault_QEMU_NSCExec;
12410 } else {
12411 fi->type = ARMFault_QEMU_SFault;
12412 }
72042435 12413 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
12414 *phys_ptr = address;
12415 *prot = 0;
12416 return true;
12417 }
12418 } else {
12419 /* For data accesses we always use the MMU bank indicated
12420 * by the current CPU state, but the security attributes
12421 * might downgrade a secure access to nonsecure.
12422 */
12423 if (sattrs.ns) {
12424 txattrs->secure = false;
12425 } else if (!secure) {
12426 /* NS access to S memory must fault.
12427 * Architecturally we should first check whether the
12428 * MPU information for this address indicates that we
12429 * are doing an unaligned access to Device memory, which
12430 * should generate a UsageFault instead. QEMU does not
12431 * currently check for that kind of unaligned access though.
12432 * If we added it we would need to do so as a special case
12433 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt().
12434 */
3f551b5b 12435 fi->type = ARMFault_QEMU_SFault;
72042435 12436 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE;
54317c0f
PM
12437 *phys_ptr = address;
12438 *prot = 0;
12439 return true;
12440 }
12441 }
12442 }
12443
72042435
PM
12444 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr,
12445 txattrs, prot, &mpu_is_subpage, fi, NULL);
72042435
PM
12446 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE;
12447 return ret;
54317c0f
PM
12448}
12449
13689d43 12450static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
03ae85f8 12451 MMUAccessType access_type, ARMMMUIdx mmu_idx,
53a4e5c5
PM
12452 hwaddr *phys_ptr, int *prot,
12453 ARMMMUFaultInfo *fi)
9ee6e8bb
PB
12454{
12455 int n;
12456 uint32_t mask;
12457 uint32_t base;
0480f69a 12458 bool is_user = regime_is_user(env, mmu_idx);
9ee6e8bb 12459
3279adb9
PM
12460 if (regime_translation_disabled(env, mmu_idx)) {
12461 /* MPU disabled. */
12462 *phys_ptr = address;
12463 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
12464 return false;
12465 }
12466
9ee6e8bb
PB
12467 *phys_ptr = address;
12468 for (n = 7; n >= 0; n--) {
554b0b09 12469 base = env->cp15.c6_region[n];
87c3d486 12470 if ((base & 1) == 0) {
554b0b09 12471 continue;
87c3d486 12472 }
554b0b09
PM
12473 mask = 1 << ((base >> 1) & 0x1f);
12474 /* Keep this shift separate from the above to avoid an
12475 (undefined) << 32. */
12476 mask = (mask << 1) - 1;
87c3d486 12477 if (((base ^ address) & ~mask) == 0) {
554b0b09 12478 break;
87c3d486 12479 }
9ee6e8bb 12480 }
87c3d486 12481 if (n < 0) {
53a4e5c5 12482 fi->type = ARMFault_Background;
b7cc4e82 12483 return true;
87c3d486 12484 }
9ee6e8bb 12485
03ae85f8 12486 if (access_type == MMU_INST_FETCH) {
7e09797c 12487 mask = env->cp15.pmsav5_insn_ap;
9ee6e8bb 12488 } else {
7e09797c 12489 mask = env->cp15.pmsav5_data_ap;
9ee6e8bb
PB
12490 }
12491 mask = (mask >> (n * 4)) & 0xf;
12492 switch (mask) {
12493 case 0:
53a4e5c5
PM
12494 fi->type = ARMFault_Permission;
12495 fi->level = 1;
b7cc4e82 12496 return true;
9ee6e8bb 12497 case 1:
87c3d486 12498 if (is_user) {
53a4e5c5
PM
12499 fi->type = ARMFault_Permission;
12500 fi->level = 1;
b7cc4e82 12501 return true;
87c3d486 12502 }
554b0b09
PM
12503 *prot = PAGE_READ | PAGE_WRITE;
12504 break;
9ee6e8bb 12505 case 2:
554b0b09 12506 *prot = PAGE_READ;
87c3d486 12507 if (!is_user) {
554b0b09 12508 *prot |= PAGE_WRITE;
87c3d486 12509 }
554b0b09 12510 break;
9ee6e8bb 12511 case 3:
554b0b09
PM
12512 *prot = PAGE_READ | PAGE_WRITE;
12513 break;
9ee6e8bb 12514 case 5:
87c3d486 12515 if (is_user) {
53a4e5c5
PM
12516 fi->type = ARMFault_Permission;
12517 fi->level = 1;
b7cc4e82 12518 return true;
87c3d486 12519 }
554b0b09
PM
12520 *prot = PAGE_READ;
12521 break;
9ee6e8bb 12522 case 6:
554b0b09
PM
12523 *prot = PAGE_READ;
12524 break;
9ee6e8bb 12525 default:
554b0b09 12526 /* Bad permission. */
53a4e5c5
PM
12527 fi->type = ARMFault_Permission;
12528 fi->level = 1;
b7cc4e82 12529 return true;
9ee6e8bb 12530 }
3ad493fc 12531 *prot |= PAGE_EXEC;
b7cc4e82 12532 return false;
9ee6e8bb
PB
12533}
12534
5b2d261d
AB
12535/* Combine either inner or outer cacheability attributes for normal
12536 * memory, according to table D4-42 and pseudocode procedure
12537 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM).
12538 *
12539 * NB: only stage 1 includes allocation hints (RW bits), leading to
12540 * some asymmetry.
12541 */
12542static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2)
12543{
12544 if (s1 == 4 || s2 == 4) {
12545 /* non-cacheable has precedence */
12546 return 4;
12547 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) {
12548 /* stage 1 write-through takes precedence */
12549 return s1;
12550 } else if (extract32(s2, 2, 2) == 2) {
12551 /* stage 2 write-through takes precedence, but the allocation hint
12552 * is still taken from stage 1
12553 */
12554 return (2 << 2) | extract32(s1, 0, 2);
12555 } else { /* write-back */
12556 return s1;
12557 }
12558}
12559
12560/* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4
12561 * and CombineS1S2Desc()
12562 *
12563 * @s1: Attributes from stage 1 walk
12564 * @s2: Attributes from stage 2 walk
12565 */
12566static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2)
12567{
337a03f0 12568 uint8_t s1lo, s2lo, s1hi, s2hi;
5b2d261d 12569 ARMCacheAttrs ret;
337a03f0
RH
12570 bool tagged = false;
12571
12572 if (s1.attrs == 0xf0) {
12573 tagged = true;
12574 s1.attrs = 0xff;
12575 }
12576
12577 s1lo = extract32(s1.attrs, 0, 4);
12578 s2lo = extract32(s2.attrs, 0, 4);
12579 s1hi = extract32(s1.attrs, 4, 4);
12580 s2hi = extract32(s2.attrs, 4, 4);
5b2d261d
AB
12581
12582 /* Combine shareability attributes (table D4-43) */
12583 if (s1.shareability == 2 || s2.shareability == 2) {
12584 /* if either are outer-shareable, the result is outer-shareable */
12585 ret.shareability = 2;
12586 } else if (s1.shareability == 3 || s2.shareability == 3) {
12587 /* if either are inner-shareable, the result is inner-shareable */
12588 ret.shareability = 3;
12589 } else {
12590 /* both non-shareable */
12591 ret.shareability = 0;
12592 }
12593
12594 /* Combine memory type and cacheability attributes */
12595 if (s1hi == 0 || s2hi == 0) {
12596 /* Device has precedence over normal */
12597 if (s1lo == 0 || s2lo == 0) {
12598 /* nGnRnE has precedence over anything */
12599 ret.attrs = 0;
12600 } else if (s1lo == 4 || s2lo == 4) {
12601 /* non-Reordering has precedence over Reordering */
12602 ret.attrs = 4; /* nGnRE */
12603 } else if (s1lo == 8 || s2lo == 8) {
12604 /* non-Gathering has precedence over Gathering */
12605 ret.attrs = 8; /* nGRE */
12606 } else {
12607 ret.attrs = 0xc; /* GRE */
12608 }
12609
12610 /* Any location for which the resultant memory type is any
12611 * type of Device memory is always treated as Outer Shareable.
12612 */
12613 ret.shareability = 2;
12614 } else { /* Normal memory */
12615 /* Outer/inner cacheability combine independently */
12616 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4
12617 | combine_cacheattr_nibble(s1lo, s2lo);
12618
12619 if (ret.attrs == 0x44) {
12620 /* Any location for which the resultant memory type is Normal
12621 * Inner Non-cacheable, Outer Non-cacheable is always treated
12622 * as Outer Shareable.
12623 */
12624 ret.shareability = 2;
12625 }
12626 }
12627
337a03f0
RH
12628 /* TODO: CombineS1S2Desc does not consider transient, only WB, RWA. */
12629 if (tagged && ret.attrs == 0xff) {
12630 ret.attrs = 0xf0;
12631 }
12632
5b2d261d
AB
12633 return ret;
12634}
12635
12636
702a9357
PM
12637/* get_phys_addr - get the physical address for this virtual address
12638 *
12639 * Find the physical address corresponding to the given virtual address,
12640 * by doing a translation table walk on MMU based systems or using the
12641 * MPU state on MPU based systems.
12642 *
b7cc4e82
PC
12643 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
12644 * prot and page_size may not be filled in, and the populated fsr value provides
702a9357
PM
12645 * information on why the translation aborted, in the format of a
12646 * DFSR/IFSR fault register, with the following caveats:
12647 * * we honour the short vs long DFSR format differences.
12648 * * the WnR bit is never set (the caller must do this).
f6bda88f 12649 * * for PSMAv5 based systems we don't bother to return a full FSR format
702a9357
PM
12650 * value.
12651 *
12652 * @env: CPUARMState
12653 * @address: virtual address to get physical address for
12654 * @access_type: 0 for read, 1 for write, 2 for execute
d3649702 12655 * @mmu_idx: MMU index indicating required translation regime
702a9357 12656 * @phys_ptr: set to the physical address corresponding to the virtual address
8bf5b6a9 12657 * @attrs: set to the memory transaction attributes to use
702a9357
PM
12658 * @prot: set to the permissions for the page containing phys_ptr
12659 * @page_size: set to the size of the page containing phys_ptr
5b2d261d
AB
12660 * @fi: set to fault info if the translation fails
12661 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes
702a9357 12662 */
ebae861f
PMD
12663bool get_phys_addr(CPUARMState *env, target_ulong address,
12664 MMUAccessType access_type, ARMMMUIdx mmu_idx,
12665 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
12666 target_ulong *page_size,
12667 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs)
9ee6e8bb 12668{
7879460a
RDC
12669 ARMMMUIdx s1_mmu_idx = stage_1_mmu_idx(mmu_idx);
12670
12671 if (mmu_idx != s1_mmu_idx) {
9b539263 12672 /* Call ourselves recursively to do the stage 1 and then stage 2
7879460a 12673 * translations if mmu_idx is a two-stage regime.
0480f69a 12674 */
9b539263
EI
12675 if (arm_feature(env, ARM_FEATURE_EL2)) {
12676 hwaddr ipa;
12677 int s2_prot;
12678 int ret;
6c05a866 12679 bool ipa_secure;
5b2d261d 12680 ARMCacheAttrs cacheattrs2 = {};
b1a10c86
RDC
12681 ARMMMUIdx s2_mmu_idx;
12682 bool is_el0;
9b539263 12683
7879460a
RDC
12684 ret = get_phys_addr(env, address, access_type, s1_mmu_idx, &ipa,
12685 attrs, prot, page_size, fi, cacheattrs);
9b539263
EI
12686
12687 /* If S1 fails or S2 is disabled, return early. */
97fa9350 12688 if (ret || regime_translation_disabled(env, ARMMMUIdx_Stage2)) {
9b539263
EI
12689 *phys_ptr = ipa;
12690 return ret;
12691 }
12692
6c05a866 12693 ipa_secure = attrs->secure;
bcd7a8cf 12694 if (arm_is_secure_below_el3(env)) {
6c05a866 12695 if (ipa_secure) {
bcd7a8cf
IH
12696 attrs->secure = !(env->cp15.vstcr_el2.raw_tcr & VSTCR_SW);
12697 } else {
12698 attrs->secure = !(env->cp15.vtcr_el2.raw_tcr & VTCR_NSW);
12699 }
12700 } else {
6c05a866 12701 assert(!ipa_secure);
bcd7a8cf
IH
12702 }
12703
b1a10c86
RDC
12704 s2_mmu_idx = attrs->secure ? ARMMMUIdx_Stage2_S : ARMMMUIdx_Stage2;
12705 is_el0 = mmu_idx == ARMMMUIdx_E10_0 || mmu_idx == ARMMMUIdx_SE10_0;
12706
9b539263 12707 /* S1 is done. Now do S2 translation. */
b1a10c86 12708 ret = get_phys_addr_lpae(env, ipa, access_type, s2_mmu_idx, is_el0,
9b539263 12709 phys_ptr, attrs, &s2_prot,
7e98e21c 12710 page_size, fi, &cacheattrs2);
9b539263
EI
12711 fi->s2addr = ipa;
12712 /* Combine the S1 and S2 perms. */
12713 *prot &= s2_prot;
5b2d261d 12714
7e98e21c
RH
12715 /* If S2 fails, return early. */
12716 if (ret) {
12717 return ret;
5b2d261d
AB
12718 }
12719
7e98e21c 12720 /* Combine the S1 and S2 cache attributes. */
e04a5752 12721 if (arm_hcr_el2_eff(env) & HCR_DC) {
7e98e21c
RH
12722 /*
12723 * HCR.DC forces the first stage attributes to
12724 * Normal Non-Shareable,
12725 * Inner Write-Back Read-Allocate Write-Allocate,
12726 * Outer Write-Back Read-Allocate Write-Allocate.
337a03f0 12727 * Do not overwrite Tagged within attrs.
7e98e21c 12728 */
337a03f0
RH
12729 if (cacheattrs->attrs != 0xf0) {
12730 cacheattrs->attrs = 0xff;
12731 }
7e98e21c
RH
12732 cacheattrs->shareability = 0;
12733 }
12734 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2);
b1a10c86
RDC
12735
12736 /* Check if IPA translates to secure or non-secure PA space. */
12737 if (arm_is_secure_below_el3(env)) {
6c05a866 12738 if (ipa_secure) {
b1a10c86
RDC
12739 attrs->secure =
12740 !(env->cp15.vstcr_el2.raw_tcr & (VSTCR_SA | VSTCR_SW));
12741 } else {
12742 attrs->secure =
12743 !((env->cp15.vtcr_el2.raw_tcr & (VTCR_NSA | VTCR_NSW))
d3b2d191 12744 || (env->cp15.vstcr_el2.raw_tcr & (VSTCR_SA | VSTCR_SW)));
b1a10c86
RDC
12745 }
12746 }
7e98e21c 12747 return 0;
9b539263
EI
12748 } else {
12749 /*
12750 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
12751 */
8bd5c820 12752 mmu_idx = stage_1_mmu_idx(mmu_idx);
9b539263 12753 }
0480f69a 12754 }
d3649702 12755
8bf5b6a9
PM
12756 /* The page table entries may downgrade secure to non-secure, but
12757 * cannot upgrade an non-secure translation regime's attributes
12758 * to secure.
12759 */
12760 attrs->secure = regime_is_secure(env, mmu_idx);
0995bf8c 12761 attrs->user = regime_is_user(env, mmu_idx);
8bf5b6a9 12762
0480f69a
PM
12763 /* Fast Context Switch Extension. This doesn't exist at all in v8.
12764 * In v7 and earlier it affects all stage 1 translations.
12765 */
97fa9350 12766 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2
0480f69a
PM
12767 && !arm_feature(env, ARM_FEATURE_V8)) {
12768 if (regime_el(env, mmu_idx) == 3) {
12769 address += env->cp15.fcseidr_s;
12770 } else {
12771 address += env->cp15.fcseidr_ns;
12772 }
54bf36ed 12773 }
9ee6e8bb 12774
3279adb9 12775 if (arm_feature(env, ARM_FEATURE_PMSA)) {
c9f9f124 12776 bool ret;
f6bda88f 12777 *page_size = TARGET_PAGE_SIZE;
3279adb9 12778
504e3cc3
PM
12779 if (arm_feature(env, ARM_FEATURE_V8)) {
12780 /* PMSAv8 */
12781 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx,
72042435 12782 phys_ptr, attrs, prot, page_size, fi);
504e3cc3 12783 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3279adb9
PM
12784 /* PMSAv7 */
12785 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
e5e40999 12786 phys_ptr, prot, page_size, fi);
3279adb9
PM
12787 } else {
12788 /* Pre-v7 MPU */
12789 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
53a4e5c5 12790 phys_ptr, prot, fi);
3279adb9
PM
12791 }
12792 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32
c9f9f124 12793 " mmu_idx %u -> %s (prot %c%c%c)\n",
709e4407
PM
12794 access_type == MMU_DATA_LOAD ? "reading" :
12795 (access_type == MMU_DATA_STORE ? "writing" : "execute"),
c9f9f124
MD
12796 (uint32_t)address, mmu_idx,
12797 ret ? "Miss" : "Hit",
12798 *prot & PAGE_READ ? 'r' : '-',
12799 *prot & PAGE_WRITE ? 'w' : '-',
12800 *prot & PAGE_EXEC ? 'x' : '-');
12801
12802 return ret;
f6bda88f
PC
12803 }
12804
3279adb9
PM
12805 /* Definitely a real MMU, not an MPU */
12806
0480f69a 12807 if (regime_translation_disabled(env, mmu_idx)) {
337a03f0
RH
12808 uint64_t hcr;
12809 uint8_t memattr;
12810
cebfb648
RH
12811 /*
12812 * MMU disabled. S1 addresses within aa64 translation regimes are
12813 * still checked for bounds -- see AArch64.TranslateAddressS1Off.
12814 */
b1a10c86 12815 if (mmu_idx != ARMMMUIdx_Stage2 && mmu_idx != ARMMMUIdx_Stage2_S) {
cebfb648
RH
12816 int r_el = regime_el(env, mmu_idx);
12817 if (arm_el_is_aa64(env, r_el)) {
12818 int pamax = arm_pamax(env_archcpu(env));
12819 uint64_t tcr = env->cp15.tcr_el[r_el].raw_tcr;
12820 int addrtop, tbi;
12821
12822 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
12823 if (access_type == MMU_INST_FETCH) {
12824 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
12825 }
12826 tbi = (tbi >> extract64(address, 55, 1)) & 1;
12827 addrtop = (tbi ? 55 : 63);
12828
12829 if (extract64(address, pamax, addrtop - pamax + 1) != 0) {
12830 fi->type = ARMFault_AddressSize;
12831 fi->level = 0;
12832 fi->stage2 = false;
12833 return 1;
12834 }
12835
12836 /*
12837 * When TBI is disabled, we've just validated that all of the
12838 * bits above PAMax are zero, so logically we only need to
12839 * clear the top byte for TBI. But it's clearer to follow
12840 * the pseudocode set of addrdesc.paddress.
12841 */
12842 address = extract64(address, 0, 52);
12843 }
12844 }
9ee6e8bb 12845 *phys_ptr = address;
3ad493fc 12846 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
d4c430a8 12847 *page_size = TARGET_PAGE_SIZE;
337a03f0
RH
12848
12849 /* Fill in cacheattr a-la AArch64.TranslateAddressS1Off. */
12850 hcr = arm_hcr_el2_eff(env);
12851 cacheattrs->shareability = 0;
12852 if (hcr & HCR_DC) {
12853 if (hcr & HCR_DCT) {
12854 memattr = 0xf0; /* Tagged, Normal, WB, RWA */
12855 } else {
12856 memattr = 0xff; /* Normal, WB, RWA */
12857 }
12858 } else if (access_type == MMU_INST_FETCH) {
12859 if (regime_sctlr(env, mmu_idx) & SCTLR_I) {
12860 memattr = 0xee; /* Normal, WT, RA, NT */
12861 } else {
12862 memattr = 0x44; /* Normal, NC, No */
12863 }
12864 cacheattrs->shareability = 2; /* outer sharable */
12865 } else {
12866 memattr = 0x00; /* Device, nGnRnE */
12867 }
12868 cacheattrs->attrs = memattr;
9ee6e8bb 12869 return 0;
0480f69a
PM
12870 }
12871
0480f69a 12872 if (regime_using_lpae_format(env, mmu_idx)) {
ff7de2fc 12873 return get_phys_addr_lpae(env, address, access_type, mmu_idx, false,
bc52bfeb
PM
12874 phys_ptr, attrs, prot, page_size,
12875 fi, cacheattrs);
0480f69a 12876 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
bc52bfeb
PM
12877 return get_phys_addr_v6(env, address, access_type, mmu_idx,
12878 phys_ptr, attrs, prot, page_size, fi);
9ee6e8bb 12879 } else {
bc52bfeb 12880 return get_phys_addr_v5(env, address, access_type, mmu_idx,
f989983e 12881 phys_ptr, prot, page_size, fi);
9ee6e8bb
PB
12882 }
12883}
12884
0faea0c7
PM
12885hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
12886 MemTxAttrs *attrs)
b5ff1b31 12887{
00b941e5 12888 ARMCPU *cpu = ARM_CPU(cs);
d3649702 12889 CPUARMState *env = &cpu->env;
a8170e5e 12890 hwaddr phys_addr;
d4c430a8 12891 target_ulong page_size;
b5ff1b31 12892 int prot;
b7cc4e82 12893 bool ret;
e14b5a23 12894 ARMMMUFaultInfo fi = {};
50494a27 12895 ARMMMUIdx mmu_idx = arm_mmu_idx(env);
7e98e21c 12896 ARMCacheAttrs cacheattrs = {};
b5ff1b31 12897
0faea0c7
PM
12898 *attrs = (MemTxAttrs) {};
12899
a9dd161f 12900 ret = get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &phys_addr,
7e98e21c 12901 attrs, &prot, &page_size, &fi, &cacheattrs);
b5ff1b31 12902
b7cc4e82 12903 if (ret) {
b5ff1b31 12904 return -1;
00b941e5 12905 }
b5ff1b31
FB
12906 return phys_addr;
12907}
12908
b5ff1b31 12909#endif
6ddbc6e4
PB
12910
12911/* Note that signed overflow is undefined in C. The following routines are
12912 careful to use unsigned types where modulo arithmetic is required.
12913 Failure to do so _will_ break on newer gcc. */
12914
12915/* Signed saturating arithmetic. */
12916
1654b2d6 12917/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
12918static inline uint16_t add16_sat(uint16_t a, uint16_t b)
12919{
12920 uint16_t res;
12921
12922 res = a + b;
12923 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
12924 if (a & 0x8000)
12925 res = 0x8000;
12926 else
12927 res = 0x7fff;
12928 }
12929 return res;
12930}
12931
1654b2d6 12932/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
12933static inline uint8_t add8_sat(uint8_t a, uint8_t b)
12934{
12935 uint8_t res;
12936
12937 res = a + b;
12938 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
12939 if (a & 0x80)
12940 res = 0x80;
12941 else
12942 res = 0x7f;
12943 }
12944 return res;
12945}
12946
1654b2d6 12947/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
12948static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
12949{
12950 uint16_t res;
12951
12952 res = a - b;
12953 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
12954 if (a & 0x8000)
12955 res = 0x8000;
12956 else
12957 res = 0x7fff;
12958 }
12959 return res;
12960}
12961
1654b2d6 12962/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
12963static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
12964{
12965 uint8_t res;
12966
12967 res = a - b;
12968 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
12969 if (a & 0x80)
12970 res = 0x80;
12971 else
12972 res = 0x7f;
12973 }
12974 return res;
12975}
12976
12977#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
12978#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
12979#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
12980#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
12981#define PFX q
12982
12983#include "op_addsub.h"
12984
12985/* Unsigned saturating arithmetic. */
460a09c1 12986static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
12987{
12988 uint16_t res;
12989 res = a + b;
12990 if (res < a)
12991 res = 0xffff;
12992 return res;
12993}
12994
460a09c1 12995static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 12996{
4c4fd3f8 12997 if (a > b)
6ddbc6e4
PB
12998 return a - b;
12999 else
13000 return 0;
13001}
13002
13003static inline uint8_t add8_usat(uint8_t a, uint8_t b)
13004{
13005 uint8_t res;
13006 res = a + b;
13007 if (res < a)
13008 res = 0xff;
13009 return res;
13010}
13011
13012static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
13013{
4c4fd3f8 13014 if (a > b)
6ddbc6e4
PB
13015 return a - b;
13016 else
13017 return 0;
13018}
13019
13020#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
13021#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
13022#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
13023#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
13024#define PFX uq
13025
13026#include "op_addsub.h"
13027
13028/* Signed modulo arithmetic. */
13029#define SARITH16(a, b, n, op) do { \
13030 int32_t sum; \
db6e2e65 13031 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
13032 RESULT(sum, n, 16); \
13033 if (sum >= 0) \
13034 ge |= 3 << (n * 2); \
13035 } while(0)
13036
13037#define SARITH8(a, b, n, op) do { \
13038 int32_t sum; \
db6e2e65 13039 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
13040 RESULT(sum, n, 8); \
13041 if (sum >= 0) \
13042 ge |= 1 << n; \
13043 } while(0)
13044
13045
13046#define ADD16(a, b, n) SARITH16(a, b, n, +)
13047#define SUB16(a, b, n) SARITH16(a, b, n, -)
13048#define ADD8(a, b, n) SARITH8(a, b, n, +)
13049#define SUB8(a, b, n) SARITH8(a, b, n, -)
13050#define PFX s
13051#define ARITH_GE
13052
13053#include "op_addsub.h"
13054
13055/* Unsigned modulo arithmetic. */
13056#define ADD16(a, b, n) do { \
13057 uint32_t sum; \
13058 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
13059 RESULT(sum, n, 16); \
a87aa10b 13060 if ((sum >> 16) == 1) \
6ddbc6e4
PB
13061 ge |= 3 << (n * 2); \
13062 } while(0)
13063
13064#define ADD8(a, b, n) do { \
13065 uint32_t sum; \
13066 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
13067 RESULT(sum, n, 8); \
a87aa10b
AZ
13068 if ((sum >> 8) == 1) \
13069 ge |= 1 << n; \
6ddbc6e4
PB
13070 } while(0)
13071
13072#define SUB16(a, b, n) do { \
13073 uint32_t sum; \
13074 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
13075 RESULT(sum, n, 16); \
13076 if ((sum >> 16) == 0) \
13077 ge |= 3 << (n * 2); \
13078 } while(0)
13079
13080#define SUB8(a, b, n) do { \
13081 uint32_t sum; \
13082 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
13083 RESULT(sum, n, 8); \
13084 if ((sum >> 8) == 0) \
a87aa10b 13085 ge |= 1 << n; \
6ddbc6e4
PB
13086 } while(0)
13087
13088#define PFX u
13089#define ARITH_GE
13090
13091#include "op_addsub.h"
13092
13093/* Halved signed arithmetic. */
13094#define ADD16(a, b, n) \
13095 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
13096#define SUB16(a, b, n) \
13097 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
13098#define ADD8(a, b, n) \
13099 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
13100#define SUB8(a, b, n) \
13101 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
13102#define PFX sh
13103
13104#include "op_addsub.h"
13105
13106/* Halved unsigned arithmetic. */
13107#define ADD16(a, b, n) \
13108 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
13109#define SUB16(a, b, n) \
13110 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
13111#define ADD8(a, b, n) \
13112 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
13113#define SUB8(a, b, n) \
13114 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
13115#define PFX uh
13116
13117#include "op_addsub.h"
13118
13119static inline uint8_t do_usad(uint8_t a, uint8_t b)
13120{
13121 if (a > b)
13122 return a - b;
13123 else
13124 return b - a;
13125}
13126
13127/* Unsigned sum of absolute byte differences. */
13128uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
13129{
13130 uint32_t sum;
13131 sum = do_usad(a, b);
13132 sum += do_usad(a >> 8, b >> 8);
bdc3b6f5 13133 sum += do_usad(a >> 16, b >> 16);
6ddbc6e4
PB
13134 sum += do_usad(a >> 24, b >> 24);
13135 return sum;
13136}
13137
13138/* For ARMv6 SEL instruction. */
13139uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
13140{
13141 uint32_t mask;
13142
13143 mask = 0;
13144 if (flags & 1)
13145 mask |= 0xff;
13146 if (flags & 2)
13147 mask |= 0xff00;
13148 if (flags & 4)
13149 mask |= 0xff0000;
13150 if (flags & 8)
13151 mask |= 0xff000000;
13152 return (a & mask) | (b & ~mask);
13153}
13154
aa633469
PM
13155/* CRC helpers.
13156 * The upper bytes of val (above the number specified by 'bytes') must have
13157 * been zeroed out by the caller.
13158 */
eb0ecd5a
WN
13159uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
13160{
13161 uint8_t buf[4];
13162
aa633469 13163 stl_le_p(buf, val);
eb0ecd5a
WN
13164
13165 /* zlib crc32 converts the accumulator and output to one's complement. */
13166 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
13167}
13168
13169uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
13170{
13171 uint8_t buf[4];
13172
aa633469 13173 stl_le_p(buf, val);
eb0ecd5a
WN
13174
13175 /* Linux crc32c converts the output to one's complement. */
13176 return crc32c(acc, buf, bytes) ^ 0xffffffff;
13177}
a9e01311
RH
13178
13179/* Return the exception level to which FP-disabled exceptions should
13180 * be taken, or 0 if FP is enabled.
13181 */
ced31551 13182int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 13183{
55faa212 13184#ifndef CONFIG_USER_ONLY
d5a6fa2d
RH
13185 uint64_t hcr_el2;
13186
a9e01311
RH
13187 /* CPACR and the CPTR registers don't exist before v6, so FP is
13188 * always accessible
13189 */
13190 if (!arm_feature(env, ARM_FEATURE_V6)) {
13191 return 0;
13192 }
13193
d87513c0
PM
13194 if (arm_feature(env, ARM_FEATURE_M)) {
13195 /* CPACR can cause a NOCP UsageFault taken to current security state */
13196 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
13197 return 1;
13198 }
13199
13200 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
13201 if (!extract32(env->v7m.nsacr, 10, 1)) {
13202 /* FP insns cause a NOCP UsageFault taken to Secure */
13203 return 3;
13204 }
13205 }
13206
13207 return 0;
13208 }
13209
d5a6fa2d
RH
13210 hcr_el2 = arm_hcr_el2_eff(env);
13211
a9e01311
RH
13212 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
13213 * 0, 2 : trap EL0 and EL1/PL1 accesses
13214 * 1 : trap only EL0 accesses
13215 * 3 : trap no accesses
c2ddb7cf 13216 * This register is ignored if E2H+TGE are both set.
a9e01311 13217 */
d5a6fa2d 13218 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
c2ddb7cf
RH
13219 int fpen = extract32(env->cp15.cpacr_el1, 20, 2);
13220
13221 switch (fpen) {
13222 case 0:
13223 case 2:
13224 if (cur_el == 0 || cur_el == 1) {
13225 /* Trap to PL1, which might be EL1 or EL3 */
13226 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) {
13227 return 3;
13228 }
13229 return 1;
13230 }
13231 if (cur_el == 3 && !is_a64(env)) {
13232 /* Secure PL1 running at EL3 */
a9e01311
RH
13233 return 3;
13234 }
c2ddb7cf
RH
13235 break;
13236 case 1:
13237 if (cur_el == 0) {
13238 return 1;
13239 }
13240 break;
13241 case 3:
13242 break;
a9e01311 13243 }
a9e01311
RH
13244 }
13245
fc1120a7
PM
13246 /*
13247 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
13248 * to control non-secure access to the FPU. It doesn't have any
13249 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
13250 */
13251 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
13252 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
13253 if (!extract32(env->cp15.nsacr, 10, 1)) {
13254 /* FP insns act as UNDEF */
13255 return cur_el == 2 ? 2 : 1;
13256 }
13257 }
13258
d5a6fa2d
RH
13259 /*
13260 * CPTR_EL2 is present in v7VE or v8, and changes format
13261 * with HCR_EL2.E2H (regardless of TGE).
a9e01311 13262 */
d5a6fa2d
RH
13263 if (cur_el <= 2) {
13264 if (hcr_el2 & HCR_E2H) {
13265 /* Check CPTR_EL2.FPEN. */
13266 switch (extract32(env->cp15.cptr_el[2], 20, 2)) {
13267 case 1:
13268 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
13269 break;
13270 }
13271 /* fall through */
13272 case 0:
13273 case 2:
13274 return 2;
13275 }
13276 } else if (arm_is_el2_enabled(env)) {
13277 if (env->cp15.cptr_el[2] & CPTR_TFP) {
13278 return 2;
13279 }
13280 }
a9e01311
RH
13281 }
13282
13283 /* CPTR_EL3 : present in v8 */
a7b66ada 13284 if (env->cp15.cptr_el[3] & CPTR_TFP) {
a9e01311
RH
13285 /* Trap all FP ops to EL3 */
13286 return 3;
13287 }
55faa212 13288#endif
a9e01311
RH
13289 return 0;
13290}
13291
b9f6033c
RH
13292/* Return the exception level we're running at if this is our mmu_idx */
13293int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
13294{
13295 if (mmu_idx & ARM_MMU_IDX_M) {
13296 return mmu_idx & ARM_MMU_IDX_M_PRIV;
13297 }
13298
13299 switch (mmu_idx) {
13300 case ARMMMUIdx_E10_0:
13301 case ARMMMUIdx_E20_0:
13302 case ARMMMUIdx_SE10_0:
b6ad6062 13303 case ARMMMUIdx_SE20_0:
b9f6033c
RH
13304 return 0;
13305 case ARMMMUIdx_E10_1:
452ef8cb 13306 case ARMMMUIdx_E10_1_PAN:
b9f6033c 13307 case ARMMMUIdx_SE10_1:
452ef8cb 13308 case ARMMMUIdx_SE10_1_PAN:
b9f6033c
RH
13309 return 1;
13310 case ARMMMUIdx_E2:
13311 case ARMMMUIdx_E20_2:
452ef8cb 13312 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
13313 case ARMMMUIdx_SE2:
13314 case ARMMMUIdx_SE20_2:
13315 case ARMMMUIdx_SE20_2_PAN:
b9f6033c
RH
13316 return 2;
13317 case ARMMMUIdx_SE3:
13318 return 3;
13319 default:
13320 g_assert_not_reached();
13321 }
13322}
13323
7aab5a8c 13324#ifndef CONFIG_TCG
65e4655c
RH
13325ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
13326{
7aab5a8c 13327 g_assert_not_reached();
65e4655c 13328}
7aab5a8c 13329#endif
65e4655c 13330
164690b2 13331ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 13332{
b6ad6062
RDC
13333 ARMMMUIdx idx;
13334 uint64_t hcr;
13335
65e4655c 13336 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 13337 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
13338 }
13339
6003d980 13340 /* See ARM pseudo-function ELIsInHost. */
b9f6033c
RH
13341 switch (el) {
13342 case 0:
b6ad6062
RDC
13343 hcr = arm_hcr_el2_eff(env);
13344 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
13345 idx = ARMMMUIdx_E20_0;
13346 } else {
13347 idx = ARMMMUIdx_E10_0;
6003d980 13348 }
b6ad6062 13349 break;
b9f6033c 13350 case 1:
66412260 13351 if (env->pstate & PSTATE_PAN) {
b6ad6062
RDC
13352 idx = ARMMMUIdx_E10_1_PAN;
13353 } else {
13354 idx = ARMMMUIdx_E10_1;
66412260 13355 }
b6ad6062 13356 break;
b9f6033c 13357 case 2:
6003d980 13358 /* Note that TGE does not apply at EL2. */
b6ad6062 13359 if (arm_hcr_el2_eff(env) & HCR_E2H) {
66412260 13360 if (env->pstate & PSTATE_PAN) {
b6ad6062
RDC
13361 idx = ARMMMUIdx_E20_2_PAN;
13362 } else {
13363 idx = ARMMMUIdx_E20_2;
66412260 13364 }
b6ad6062
RDC
13365 } else {
13366 idx = ARMMMUIdx_E2;
6003d980 13367 }
b6ad6062 13368 break;
b9f6033c
RH
13369 case 3:
13370 return ARMMMUIdx_SE3;
13371 default:
13372 g_assert_not_reached();
65e4655c 13373 }
b6ad6062
RDC
13374
13375 if (arm_is_secure_below_el3(env)) {
13376 idx &= ~ARM_MMU_IDX_A_NS;
13377 }
13378
13379 return idx;
50494a27
RH
13380}
13381
164690b2
RH
13382ARMMMUIdx arm_mmu_idx(CPUARMState *env)
13383{
13384 return arm_mmu_idx_el(env, arm_current_el(env));
13385}
13386
64be86ab
RH
13387#ifndef CONFIG_USER_ONLY
13388ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env)
13389{
13390 return stage_1_mmu_idx(arm_mmu_idx(env));
13391}
13392#endif
13393
3902bfc6
RH
13394static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
13395 ARMMMUIdx mmu_idx,
13396 CPUARMTBFlags flags)
fdd1b228 13397{
a729a46b
RH
13398 DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
13399 DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
fdd1b228 13400
fdd1b228 13401 if (arm_singlestep_active(env)) {
a729a46b 13402 DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
fdd1b228
RH
13403 }
13404 return flags;
13405}
13406
3902bfc6
RH
13407static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
13408 ARMMMUIdx mmu_idx,
13409 CPUARMTBFlags flags)
43eccfb6 13410{
8061a649
RH
13411 bool sctlr_b = arm_sctlr_b(env);
13412
13413 if (sctlr_b) {
a729a46b 13414 DP_TBFLAG_A32(flags, SCTLR__B, 1);
8061a649
RH
13415 }
13416 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
a729a46b 13417 DP_TBFLAG_ANY(flags, BE_DATA, 1);
8061a649 13418 }
a729a46b 13419 DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
43eccfb6
RH
13420
13421 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
13422}
13423
3902bfc6
RH
13424static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
13425 ARMMMUIdx mmu_idx)
6e33ced5 13426{
3902bfc6 13427 CPUARMTBFlags flags = {};
4479ec30
RH
13428 uint32_t ccr = env->v7m.ccr[env->v7m.secure];
13429
13430 /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
13431 if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
13432 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13433 }
6e33ced5
RH
13434
13435 if (arm_v7m_is_handler_mode(env)) {
a729a46b 13436 DP_TBFLAG_M32(flags, HANDLER, 1);
6e33ced5
RH
13437 }
13438
13439 /*
13440 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
13441 * is suppressing them because the requested execution priority
13442 * is less than 0.
13443 */
13444 if (arm_feature(env, ARM_FEATURE_V8) &&
13445 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
4479ec30 13446 (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
a729a46b 13447 DP_TBFLAG_M32(flags, STACKCHECK, 1);
6e33ced5
RH
13448 }
13449
13450 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
13451}
13452
3902bfc6 13453static CPUARMTBFlags rebuild_hflags_aprofile(CPUARMState *env)
83f4baef 13454{
3902bfc6 13455 CPUARMTBFlags flags = {};
83f4baef 13456
a729a46b 13457 DP_TBFLAG_ANY(flags, DEBUG_TARGET_EL, arm_debug_target_el(env));
83f4baef
RH
13458 return flags;
13459}
13460
3902bfc6
RH
13461static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
13462 ARMMMUIdx mmu_idx)
c747224c 13463{
3902bfc6 13464 CPUARMTBFlags flags = rebuild_hflags_aprofile(env);
4479ec30
RH
13465 int el = arm_current_el(env);
13466
13467 if (arm_sctlr(env, el) & SCTLR_A) {
13468 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13469 }
0a54d68e
RH
13470
13471 if (arm_el_is_aa64(env, 1)) {
a729a46b 13472 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 13473 }
5bb0a20b 13474
4479ec30 13475 if (el < 2 && env->cp15.hstr_el2 &&
5bb0a20b 13476 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
a729a46b 13477 DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
5bb0a20b
MZ
13478 }
13479
520d1621
PM
13480 if (env->uncached_cpsr & CPSR_IL) {
13481 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
13482 }
13483
83f4baef 13484 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
c747224c
RH
13485}
13486
3902bfc6
RH
13487static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
13488 ARMMMUIdx mmu_idx)
a9e01311 13489{
3902bfc6 13490 CPUARMTBFlags flags = rebuild_hflags_aprofile(env);
d4d7503a 13491 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
b830a5ee 13492 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr;
d4d7503a
RH
13493 uint64_t sctlr;
13494 int tbii, tbid;
b9adaa70 13495
a729a46b 13496 DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
cd208a1c 13497
339370b9 13498 /* Get control bits for tagged addresses. */
b830a5ee
RH
13499 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
13500 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
5d8634f5 13501
a729a46b
RH
13502 DP_TBFLAG_A64(flags, TBII, tbii);
13503 DP_TBFLAG_A64(flags, TBID, tbid);
d4d7503a
RH
13504
13505 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
13506 int sve_el = sve_exception_el(env, el);
13507 uint32_t zcr_len;
5d8634f5 13508
d4d7503a
RH
13509 /*
13510 * If SVE is disabled, but FP is enabled,
13511 * then the effective len is 0.
13512 */
13513 if (sve_el != 0 && fp_el == 0) {
13514 zcr_len = 0;
13515 } else {
13516 zcr_len = sve_zcr_len_for_el(env, el);
5d8634f5 13517 }
a729a46b
RH
13518 DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
13519 DP_TBFLAG_A64(flags, ZCR_LEN, zcr_len);
d4d7503a 13520 }
1db5e96c 13521
aaec1432 13522 sctlr = regime_sctlr(env, stage1);
1db5e96c 13523
4479ec30
RH
13524 if (sctlr & SCTLR_A) {
13525 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
13526 }
13527
8061a649 13528 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
a729a46b 13529 DP_TBFLAG_ANY(flags, BE_DATA, 1);
8061a649
RH
13530 }
13531
d4d7503a
RH
13532 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
13533 /*
13534 * In order to save space in flags, we record only whether
13535 * pauth is "inactive", meaning all insns are implemented as
13536 * a nop, or "active" when some action must be performed.
13537 * The decision of which action to take is left to a helper.
13538 */
13539 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
a729a46b 13540 DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
1db5e96c 13541 }
d4d7503a 13542 }
0816ef1b 13543
d4d7503a
RH
13544 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
13545 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
13546 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
a729a46b 13547 DP_TBFLAG_A64(flags, BT, 1);
0816ef1b 13548 }
d4d7503a 13549 }
08f1434a 13550
cc28fc30 13551 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
7a8014ab
RH
13552 if (!(env->pstate & PSTATE_UAO)) {
13553 switch (mmu_idx) {
13554 case ARMMMUIdx_E10_1:
13555 case ARMMMUIdx_E10_1_PAN:
13556 case ARMMMUIdx_SE10_1:
13557 case ARMMMUIdx_SE10_1_PAN:
13558 /* TODO: ARMv8.3-NV */
a729a46b 13559 DP_TBFLAG_A64(flags, UNPRIV, 1);
7a8014ab
RH
13560 break;
13561 case ARMMMUIdx_E20_2:
13562 case ARMMMUIdx_E20_2_PAN:
b6ad6062
RDC
13563 case ARMMMUIdx_SE20_2:
13564 case ARMMMUIdx_SE20_2_PAN:
7a8014ab
RH
13565 /*
13566 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
13567 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
13568 */
13569 if (env->cp15.hcr_el2 & HCR_TGE) {
a729a46b 13570 DP_TBFLAG_A64(flags, UNPRIV, 1);
7a8014ab
RH
13571 }
13572 break;
13573 default:
13574 break;
cc28fc30 13575 }
cc28fc30
RH
13576 }
13577
520d1621
PM
13578 if (env->pstate & PSTATE_IL) {
13579 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
13580 }
13581
81ae05fa
RH
13582 if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
13583 /*
13584 * Set MTE_ACTIVE if any access may be Checked, and leave clear
13585 * if all accesses must be Unchecked:
13586 * 1) If no TBI, then there are no tags in the address to check,
13587 * 2) If Tag Check Override, then all accesses are Unchecked,
13588 * 3) If Tag Check Fail == 0, then Checked access have no effect,
13589 * 4) If no Allocation Tag Access, then all accesses are Unchecked.
13590 */
13591 if (allocation_tag_access_enabled(env, el, sctlr)) {
a729a46b 13592 DP_TBFLAG_A64(flags, ATA, 1);
81ae05fa
RH
13593 if (tbid
13594 && !(env->pstate & PSTATE_TCO)
13595 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
a729a46b 13596 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
81ae05fa
RH
13597 }
13598 }
13599 /* And again for unprivileged accesses, if required. */
a729a46b 13600 if (EX_TBFLAG_A64(flags, UNPRIV)
81ae05fa
RH
13601 && tbid
13602 && !(env->pstate & PSTATE_TCO)
2d928adf 13603 && (sctlr & SCTLR_TCF0)
81ae05fa 13604 && allocation_tag_access_enabled(env, 0, sctlr)) {
a729a46b 13605 DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
81ae05fa
RH
13606 }
13607 /* Cache TCMA as well as TBI. */
a729a46b 13608 DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
81ae05fa
RH
13609 }
13610
d4d7503a
RH
13611 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
13612}
13613
3902bfc6 13614static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
3d74e2e9
RH
13615{
13616 int el = arm_current_el(env);
13617 int fp_el = fp_exception_el(env, el);
164690b2 13618 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3d74e2e9
RH
13619
13620 if (is_a64(env)) {
13621 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
13622 } else if (arm_feature(env, ARM_FEATURE_M)) {
13623 return rebuild_hflags_m32(env, fp_el, mmu_idx);
13624 } else {
13625 return rebuild_hflags_a32(env, fp_el, mmu_idx);
13626 }
13627}
13628
13629void arm_rebuild_hflags(CPUARMState *env)
13630{
13631 env->hflags = rebuild_hflags_internal(env);
13632}
13633
19717e9b
PM
13634/*
13635 * If we have triggered a EL state change we can't rely on the
13636 * translator having passed it to us, we need to recompute.
13637 */
13638void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
13639{
13640 int el = arm_current_el(env);
13641 int fp_el = fp_exception_el(env, el);
13642 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3902bfc6 13643
19717e9b
PM
13644 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
13645}
13646
14f3c588
RH
13647void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
13648{
13649 int fp_el = fp_exception_el(env, el);
13650 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13651
13652 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
13653}
13654
f80741d1
AB
13655/*
13656 * If we have triggered a EL state change we can't rely on the
563152e0 13657 * translator having passed it to us, we need to recompute.
f80741d1
AB
13658 */
13659void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
13660{
13661 int el = arm_current_el(env);
13662 int fp_el = fp_exception_el(env, el);
13663 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13664 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
13665}
13666
14f3c588
RH
13667void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
13668{
13669 int fp_el = fp_exception_el(env, el);
13670 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13671
13672 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
13673}
13674
13675void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
13676{
13677 int fp_el = fp_exception_el(env, el);
13678 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
13679
13680 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
13681}
13682
0ee8b24a
PMD
13683static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
13684{
13685#ifdef CONFIG_DEBUG_TCG
3902bfc6
RH
13686 CPUARMTBFlags c = env->hflags;
13687 CPUARMTBFlags r = rebuild_hflags_internal(env);
0ee8b24a 13688
a378206a
RH
13689 if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
13690 fprintf(stderr, "TCG hflags mismatch "
13691 "(current:(0x%08x,0x" TARGET_FMT_lx ")"
13692 " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
13693 c.flags, c.flags2, r.flags, r.flags2);
0ee8b24a
PMD
13694 abort();
13695 }
13696#endif
13697}
13698
26702213
PM
13699static bool mve_no_pred(CPUARMState *env)
13700{
13701 /*
13702 * Return true if there is definitely no predication of MVE
13703 * instructions by VPR or LTPSIZE. (Returning false even if there
13704 * isn't any predication is OK; generated code will just be
13705 * a little worse.)
13706 * If the CPU does not implement MVE then this TB flag is always 0.
13707 *
13708 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
13709 * logic in gen_update_fp_context() needs to be updated to match.
13710 *
13711 * We do not include the effect of the ECI bits here -- they are
13712 * tracked in other TB flags. This simplifies the logic for
13713 * "when did we emit code that changes the MVE_NO_PRED TB flag
13714 * and thus need to end the TB?".
13715 */
13716 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
13717 return false;
13718 }
13719 if (env->v7m.vpr) {
13720 return false;
13721 }
13722 if (env->v7m.ltpsize < 4) {
13723 return false;
13724 }
13725 return true;
13726}
13727
d4d7503a
RH
13728void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
13729 target_ulong *cs_base, uint32_t *pflags)
13730{
3902bfc6 13731 CPUARMTBFlags flags;
d4d7503a 13732
0ee8b24a 13733 assert_hflags_rebuild_correctly(env);
3902bfc6 13734 flags = env->hflags;
3d74e2e9 13735
a729a46b 13736 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
d4d7503a 13737 *pc = env->pc;
d4d7503a 13738 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
a729a46b 13739 DP_TBFLAG_A64(flags, BTYPE, env->btype);
08f1434a 13740 }
a9e01311
RH
13741 } else {
13742 *pc = env->regs[15];
6e33ced5
RH
13743
13744 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
13745 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
13746 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
13747 != env->v7m.secure) {
a729a46b 13748 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
9550d1bd
RH
13749 }
13750
13751 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
13752 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
13753 (env->v7m.secure &&
13754 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
13755 /*
13756 * ASPEN is set, but FPCA/SFPA indicate that there is no
13757 * active FP context; we must create a new FP context before
13758 * executing any FP insn.
13759 */
a729a46b 13760 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
9550d1bd
RH
13761 }
13762
13763 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
13764 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
a729a46b 13765 DP_TBFLAG_M32(flags, LSPACT, 1);
9550d1bd 13766 }
26702213
PM
13767
13768 if (mve_no_pred(env)) {
13769 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
13770 }
6e33ced5 13771 } else {
bbad7c62
RH
13772 /*
13773 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
13774 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
13775 */
13776 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
a729a46b 13777 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
bbad7c62 13778 } else {
a729a46b
RH
13779 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
13780 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
bbad7c62 13781 }
0a54d68e 13782 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
a729a46b 13783 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 13784 }
6e33ced5
RH
13785 }
13786
a729a46b
RH
13787 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
13788 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
d4d7503a 13789 }
a9e01311 13790
60e12c37
RH
13791 /*
13792 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
13793 * states defined in the ARM ARM for software singlestep:
13794 * SS_ACTIVE PSTATE.SS State
13795 * 0 x Inactive (the TB flag for SS is always 0)
13796 * 1 0 Active-pending
13797 * 1 1 Active-not-pending
ae6eb1e9 13798 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
a9e01311 13799 */
a729a46b
RH
13800 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
13801 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
a9e01311 13802 }
a9e01311 13803
3902bfc6 13804 *pflags = flags.flags;
a378206a 13805 *cs_base = flags.flags2;
a9e01311 13806}
0ab5953b
RH
13807
13808#ifdef TARGET_AARCH64
13809/*
13810 * The manual says that when SVE is enabled and VQ is widened the
13811 * implementation is allowed to zero the previously inaccessible
13812 * portion of the registers. The corollary to that is that when
13813 * SVE is enabled and VQ is narrowed we are also allowed to zero
13814 * the now inaccessible portion of the registers.
13815 *
13816 * The intent of this is that no predicate bit beyond VQ is ever set.
13817 * Which means that some operations on predicate registers themselves
13818 * may operate on full uint64_t or even unrolled across the maximum
13819 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
13820 * may well be cheaper than conditionals to restrict the operation
13821 * to the relevant portion of a uint16_t[16].
13822 */
13823void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
13824{
13825 int i, j;
13826 uint64_t pmask;
13827
13828 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 13829 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
13830
13831 /* Zap the high bits of the zregs. */
13832 for (i = 0; i < 32; i++) {
13833 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
13834 }
13835
13836 /* Zap the high bits of the pregs and ffr. */
13837 pmask = 0;
13838 if (vq & 3) {
13839 pmask = ~(-1ULL << (16 * (vq & 3)));
13840 }
13841 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
13842 for (i = 0; i < 17; ++i) {
13843 env->vfp.pregs[i].p[j] &= pmask;
13844 }
13845 pmask = 0;
13846 }
13847}
13848
13849/*
13850 * Notice a change in SVE vector size when changing EL.
13851 */
9a05f7b6
RH
13852void aarch64_sve_change_el(CPUARMState *env, int old_el,
13853 int new_el, bool el0_a64)
0ab5953b 13854{
2fc0cc0e 13855 ARMCPU *cpu = env_archcpu(env);
0ab5953b 13856 int old_len, new_len;
9a05f7b6 13857 bool old_a64, new_a64;
0ab5953b
RH
13858
13859 /* Nothing to do if no SVE. */
cd208a1c 13860 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
13861 return;
13862 }
13863
13864 /* Nothing to do if FP is disabled in either EL. */
13865 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
13866 return;
13867 }
13868
13869 /*
13870 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
13871 * at ELx, or not available because the EL is in AArch32 state, then
13872 * for all purposes other than a direct read, the ZCR_ELx.LEN field
13873 * has an effective value of 0".
13874 *
13875 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
13876 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
13877 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
13878 * we already have the correct register contents when encountering the
13879 * vq0->vq0 transition between EL0->EL1.
13880 */
9a05f7b6
RH
13881 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
13882 old_len = (old_a64 && !sve_exception_el(env, old_el)
0ab5953b 13883 ? sve_zcr_len_for_el(env, old_el) : 0);
9a05f7b6
RH
13884 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
13885 new_len = (new_a64 && !sve_exception_el(env, new_el)
0ab5953b
RH
13886 ? sve_zcr_len_for_el(env, new_el) : 0);
13887
13888 /* When changing vector length, clear inaccessible state. */
13889 if (new_len < old_len) {
13890 aarch64_sve_narrow_vq(env, new_len + 1);
13891 }
13892}
13893#endif
This page took 4.547528 seconds and 4 git commands to generate.