]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/helper.c
target/arm: Drop secure check for HCR.TGE vs SCTLR_EL1.M
[mirror_qemu.git] / target / arm / helper.c
CommitLineData
ed3baad1
PMD
1/*
2 * ARM generic helpers.
3 *
4 * This code is licensed under the GNU GPL v2 or later.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
db725815 8
74c21bd0 9#include "qemu/osdep.h"
63159601 10#include "qemu/units.h"
cd617484 11#include "qemu/log.h"
194cbc49 12#include "trace.h"
b5ff1b31 13#include "cpu.h"
ccd38087 14#include "internals.h"
2ef6175a 15#include "exec/helper-proto.h"
1de7afc9 16#include "qemu/host-utils.h"
db725815 17#include "qemu/main-loop.h"
b8012ecf 18#include "qemu/timer.h"
1de7afc9 19#include "qemu/bitops.h"
eb0ecd5a 20#include "qemu/crc32c.h"
0442428a 21#include "qemu/qemu-print.h"
63c91552 22#include "exec/exec-all.h"
eb0ecd5a 23#include <zlib.h> /* For crc32 */
64552b6b 24#include "hw/irq.h"
6b5fe137 25#include "semihosting/semihost.h"
b2e23725 26#include "sysemu/cpus.h"
740b1759 27#include "sysemu/cpu-timers.h"
f3a9b694 28#include "sysemu/kvm.h"
9d2b5a58 29#include "qemu/range.h"
7f7b4e7a 30#include "qapi/qapi-commands-machine-target.h"
de390645
RH
31#include "qapi/error.h"
32#include "qemu/guest-random.h"
91f78c58
PMD
33#ifdef CONFIG_TCG
34#include "arm_ldst.h"
7aab5a8c 35#include "exec/cpu_ldst.h"
6b5fe137 36#include "semihosting/common-semi.h"
91f78c58 37#endif
cf7c6d10 38#include "cpregs.h"
0b03bdfc 39
352c98e5
LV
40#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
41
affdb64d
PM
42static void switch_mode(CPUARMState *env, int mode);
43
c4241c7d 44static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 45{
375421cc 46 assert(ri->fieldoffset);
67ed771d 47 if (cpreg_field_is_64bit(ri)) {
c4241c7d 48 return CPREG_FIELD64(env, ri);
22d9e1a9 49 } else {
c4241c7d 50 return CPREG_FIELD32(env, ri);
22d9e1a9 51 }
d4e6df63
PM
52}
53
f43ee493 54void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
d4e6df63 55{
375421cc 56 assert(ri->fieldoffset);
67ed771d 57 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
58 CPREG_FIELD64(env, ri) = value;
59 } else {
60 CPREG_FIELD32(env, ri) = value;
61 }
d4e6df63
PM
62}
63
11f136ee
FA
64static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
65{
66 return (char *)env + ri->fieldoffset;
67}
68
49a66191 69uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 70{
59a1c327 71 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 72 if (ri->type & ARM_CP_CONST) {
59a1c327 73 return ri->resetvalue;
721fae12 74 } else if (ri->raw_readfn) {
59a1c327 75 return ri->raw_readfn(env, ri);
721fae12 76 } else if (ri->readfn) {
59a1c327 77 return ri->readfn(env, ri);
721fae12 78 } else {
59a1c327 79 return raw_read(env, ri);
721fae12 80 }
721fae12
PM
81}
82
59a1c327 83static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 84 uint64_t v)
721fae12
PM
85{
86 /* Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
87 * Note that constant registers are treated as write-ignored; the
88 * caller should check for success by whether a readback gives the
89 * value written.
90 */
91 if (ri->type & ARM_CP_CONST) {
59a1c327 92 return;
721fae12 93 } else if (ri->raw_writefn) {
c4241c7d 94 ri->raw_writefn(env, ri, v);
721fae12 95 } else if (ri->writefn) {
c4241c7d 96 ri->writefn(env, ri, v);
721fae12 97 } else {
afb2530f 98 raw_write(env, ri, v);
721fae12 99 }
721fae12
PM
100}
101
375421cc
PM
102static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
103{
104 /* Return true if the regdef would cause an assertion if you called
105 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
106 * program bug for it not to have the NO_RAW flag).
107 * NB that returning false here doesn't necessarily mean that calling
108 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
109 * read/write access functions which are safe for raw use" from "has
110 * read/write access functions which have side effects but has forgotten
111 * to provide raw access functions".
112 * The tests here line up with the conditions in read/write_raw_cp_reg()
113 * and assertions in raw_read()/raw_write().
114 */
115 if ((ri->type & ARM_CP_CONST) ||
116 ri->fieldoffset ||
117 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
118 return false;
119 }
120 return true;
121}
122
b698e4ee 123bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
721fae12
PM
124{
125 /* Write the coprocessor state from cpu->env to the (index,value) list. */
126 int i;
127 bool ok = true;
128
129 for (i = 0; i < cpu->cpreg_array_len; i++) {
130 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
131 const ARMCPRegInfo *ri;
b698e4ee 132 uint64_t newval;
59a1c327 133
60322b39 134 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
135 if (!ri) {
136 ok = false;
137 continue;
138 }
7a0e58fa 139 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
140 continue;
141 }
b698e4ee
PM
142
143 newval = read_raw_cp_reg(&cpu->env, ri);
144 if (kvm_sync) {
145 /*
146 * Only sync if the previous list->cpustate sync succeeded.
147 * Rather than tracking the success/failure state for every
148 * item in the list, we just recheck "does the raw write we must
149 * have made in write_list_to_cpustate() read back OK" here.
150 */
151 uint64_t oldval = cpu->cpreg_values[i];
152
153 if (oldval == newval) {
154 continue;
155 }
156
157 write_raw_cp_reg(&cpu->env, ri, oldval);
158 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
159 continue;
160 }
161
162 write_raw_cp_reg(&cpu->env, ri, newval);
163 }
164 cpu->cpreg_values[i] = newval;
721fae12
PM
165 }
166 return ok;
167}
168
169bool write_list_to_cpustate(ARMCPU *cpu)
170{
171 int i;
172 bool ok = true;
173
174 for (i = 0; i < cpu->cpreg_array_len; i++) {
175 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
176 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
177 const ARMCPRegInfo *ri;
178
60322b39 179 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
180 if (!ri) {
181 ok = false;
182 continue;
183 }
7a0e58fa 184 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
185 continue;
186 }
187 /* Write value and confirm it reads back as written
188 * (to catch read-only registers and partially read-only
189 * registers where the incoming migration value doesn't match)
190 */
59a1c327
PM
191 write_raw_cp_reg(&cpu->env, ri, v);
192 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
193 ok = false;
194 }
195 }
196 return ok;
197}
198
199static void add_cpreg_to_list(gpointer key, gpointer opaque)
200{
201 ARMCPU *cpu = opaque;
5860362d
RH
202 uint32_t regidx = (uintptr_t)key;
203 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 204
7a0e58fa 205 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
206 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
207 /* The value array need not be initialized at this point */
208 cpu->cpreg_array_len++;
209 }
210}
211
212static void count_cpreg(gpointer key, gpointer opaque)
213{
214 ARMCPU *cpu = opaque;
721fae12
PM
215 const ARMCPRegInfo *ri;
216
5860362d 217 ri = g_hash_table_lookup(cpu->cp_regs, key);
721fae12 218
7a0e58fa 219 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
721fae12
PM
220 cpu->cpreg_array_len++;
221 }
222}
223
224static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
225{
5860362d
RH
226 uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
227 uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
721fae12 228
cbf239b7
AR
229 if (aidx > bidx) {
230 return 1;
231 }
232 if (aidx < bidx) {
233 return -1;
234 }
235 return 0;
721fae12
PM
236}
237
238void init_cpreg_list(ARMCPU *cpu)
239{
240 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
241 * Note that we require cpreg_tuples[] to be sorted by key ID.
242 */
57b6d95e 243 GList *keys;
721fae12
PM
244 int arraylen;
245
57b6d95e 246 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
247 keys = g_list_sort(keys, cpreg_key_compare);
248
249 cpu->cpreg_array_len = 0;
250
251 g_list_foreach(keys, count_cpreg, cpu);
252
253 arraylen = cpu->cpreg_array_len;
254 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
255 cpu->cpreg_values = g_new(uint64_t, arraylen);
256 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
257 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
258 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
259 cpu->cpreg_array_len = 0;
260
261 g_list_foreach(keys, add_cpreg_to_list, cpu);
262
263 assert(cpu->cpreg_array_len == arraylen);
264
265 g_list_free(keys);
266}
267
68e9c2fe 268/*
93dd1e61 269 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
68e9c2fe
EI
270 */
271static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
272 const ARMCPRegInfo *ri,
273 bool isread)
68e9c2fe 274{
93dd1e61
EI
275 if (!is_a64(env) && arm_current_el(env) == 3 &&
276 arm_is_secure_below_el3(env)) {
68e9c2fe
EI
277 return CP_ACCESS_TRAP_UNCATEGORIZED;
278 }
279 return CP_ACCESS_OK;
280}
281
5513c3ab
PM
282/* Some secure-only AArch32 registers trap to EL3 if used from
283 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
284 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
285 * We assume that the .access field is set to PL1_RW.
286 */
287static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
288 const ARMCPRegInfo *ri,
289 bool isread)
5513c3ab
PM
290{
291 if (arm_current_el(env) == 3) {
292 return CP_ACCESS_OK;
293 }
294 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
295 if (env->cp15.scr_el3 & SCR_EEL2) {
296 return CP_ACCESS_TRAP_EL2;
297 }
5513c3ab
PM
298 return CP_ACCESS_TRAP_EL3;
299 }
300 /* This will be EL1 NS and EL2 NS, which just UNDEF */
301 return CP_ACCESS_TRAP_UNCATEGORIZED;
302}
303
1fce1ba9
PM
304/* Check for traps to performance monitor registers, which are controlled
305 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
306 */
307static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
308 bool isread)
309{
310 int el = arm_current_el(env);
59dd089c 311 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1fce1ba9 312
59dd089c 313 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1fce1ba9
PM
314 return CP_ACCESS_TRAP_EL2;
315 }
316 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
317 return CP_ACCESS_TRAP_EL3;
318 }
319 return CP_ACCESS_OK;
320}
321
84929218
RH
322/* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
323static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
324 bool isread)
325{
326 if (arm_current_el(env) == 1) {
327 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
328 if (arm_hcr_el2_eff(env) & trap) {
329 return CP_ACCESS_TRAP_EL2;
330 }
331 }
332 return CP_ACCESS_OK;
333}
334
1803d271
RH
335/* Check for traps from EL1 due to HCR_EL2.TSW. */
336static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
337 bool isread)
338{
339 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
340 return CP_ACCESS_TRAP_EL2;
341 }
342 return CP_ACCESS_OK;
343}
344
99602377
RH
345/* Check for traps from EL1 due to HCR_EL2.TACR. */
346static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
347 bool isread)
348{
349 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
350 return CP_ACCESS_TRAP_EL2;
351 }
352 return CP_ACCESS_OK;
353}
354
30881b73
RH
355/* Check for traps from EL1 due to HCR_EL2.TTLB. */
356static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
357 bool isread)
358{
359 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
360 return CP_ACCESS_TRAP_EL2;
361 }
362 return CP_ACCESS_OK;
363}
364
c4241c7d 365static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 366{
2fc0cc0e 367 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 368
8d5c773e 369 raw_write(env, ri, value);
d10eb08f 370 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
371}
372
c4241c7d 373static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 374{
2fc0cc0e 375 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 376
8d5c773e 377 if (raw_read(env, ri) != value) {
08de207b
PM
378 /* Unlike real hardware the qemu TLB uses virtual addresses,
379 * not modified virtual addresses, so this causes a TLB flush.
380 */
d10eb08f 381 tlb_flush(CPU(cpu));
8d5c773e 382 raw_write(env, ri, value);
08de207b 383 }
08de207b 384}
c4241c7d
PM
385
386static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
387 uint64_t value)
08de207b 388{
2fc0cc0e 389 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 390
452a0955 391 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 392 && !extended_addresses_enabled(env)) {
08de207b
PM
393 /* For VMSA (when not using the LPAE long descriptor page table
394 * format) this register includes the ASID, so do a TLB flush.
395 * For PMSA it is purely a process ID and no action is needed.
396 */
d10eb08f 397 tlb_flush(CPU(cpu));
08de207b 398 }
8d5c773e 399 raw_write(env, ri, value);
08de207b
PM
400}
401
b4ab8ce9
PM
402/* IS variants of TLB operations must affect all cores */
403static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
404 uint64_t value)
405{
29a0af61 406 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
407
408 tlb_flush_all_cpus_synced(cs);
409}
410
411static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
412 uint64_t value)
413{
29a0af61 414 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
415
416 tlb_flush_all_cpus_synced(cs);
417}
418
419static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
420 uint64_t value)
421{
29a0af61 422 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
423
424 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
425}
426
427static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
428 uint64_t value)
429{
29a0af61 430 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
431
432 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
433}
434
435/*
436 * Non-IS variants of TLB operations are upgraded to
373e7ffd 437 * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
b4ab8ce9
PM
438 * force broadcast of these operations.
439 */
440static bool tlb_force_broadcast(CPUARMState *env)
441{
373e7ffd 442 return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
b4ab8ce9
PM
443}
444
c4241c7d
PM
445static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
446 uint64_t value)
d929823f
PM
447{
448 /* Invalidate all (TLBIALL) */
527db2be 449 CPUState *cs = env_cpu(env);
00c8cb0a 450
b4ab8ce9 451 if (tlb_force_broadcast(env)) {
527db2be
RH
452 tlb_flush_all_cpus_synced(cs);
453 } else {
454 tlb_flush(cs);
b4ab8ce9 455 }
d929823f
PM
456}
457
c4241c7d
PM
458static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
459 uint64_t value)
d929823f
PM
460{
461 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
527db2be 462 CPUState *cs = env_cpu(env);
31b030d4 463
527db2be 464 value &= TARGET_PAGE_MASK;
b4ab8ce9 465 if (tlb_force_broadcast(env)) {
527db2be
RH
466 tlb_flush_page_all_cpus_synced(cs, value);
467 } else {
468 tlb_flush_page(cs, value);
b4ab8ce9 469 }
d929823f
PM
470}
471
c4241c7d
PM
472static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
473 uint64_t value)
d929823f
PM
474{
475 /* Invalidate by ASID (TLBIASID) */
527db2be 476 CPUState *cs = env_cpu(env);
00c8cb0a 477
b4ab8ce9 478 if (tlb_force_broadcast(env)) {
527db2be
RH
479 tlb_flush_all_cpus_synced(cs);
480 } else {
481 tlb_flush(cs);
b4ab8ce9 482 }
d929823f
PM
483}
484
c4241c7d
PM
485static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
486 uint64_t value)
d929823f
PM
487{
488 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
527db2be 489 CPUState *cs = env_cpu(env);
31b030d4 490
527db2be 491 value &= TARGET_PAGE_MASK;
b4ab8ce9 492 if (tlb_force_broadcast(env)) {
527db2be
RH
493 tlb_flush_page_all_cpus_synced(cs, value);
494 } else {
495 tlb_flush_page(cs, value);
b4ab8ce9 496 }
fa439fc5
PM
497}
498
541ef8c2
SS
499static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
500 uint64_t value)
501{
29a0af61 502 CPUState *cs = env_cpu(env);
541ef8c2 503
0336cbf8 504 tlb_flush_by_mmuidx(cs,
01b98b68 505 ARMMMUIdxBit_E10_1 |
452ef8cb 506 ARMMMUIdxBit_E10_1_PAN |
bf05340c 507 ARMMMUIdxBit_E10_0);
541ef8c2
SS
508}
509
510static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
511 uint64_t value)
512{
29a0af61 513 CPUState *cs = env_cpu(env);
541ef8c2 514
a67cf277 515 tlb_flush_by_mmuidx_all_cpus_synced(cs,
01b98b68 516 ARMMMUIdxBit_E10_1 |
452ef8cb 517 ARMMMUIdxBit_E10_1_PAN |
bf05340c 518 ARMMMUIdxBit_E10_0);
541ef8c2
SS
519}
520
541ef8c2
SS
521
522static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
523 uint64_t value)
524{
29a0af61 525 CPUState *cs = env_cpu(env);
541ef8c2 526
e013b741 527 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
528}
529
530static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
531 uint64_t value)
532{
29a0af61 533 CPUState *cs = env_cpu(env);
541ef8c2 534
e013b741 535 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
536}
537
538static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
539 uint64_t value)
540{
29a0af61 541 CPUState *cs = env_cpu(env);
541ef8c2
SS
542 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
543
e013b741 544 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
541ef8c2
SS
545}
546
547static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
548 uint64_t value)
549{
29a0af61 550 CPUState *cs = env_cpu(env);
541ef8c2
SS
551 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
552
a67cf277 553 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 554 ARMMMUIdxBit_E2);
541ef8c2
SS
555}
556
e9aa6c21 557static const ARMCPRegInfo cp_reginfo[] = {
54bf36ed
FA
558 /* Define the secure and non-secure FCSE identifier CP registers
559 * separately because there is no secure bank in V8 (no _EL3). This allows
560 * the secure register to be properly reset and migrated. There is also no
561 * v8 EL1 version of the register so the non-secure instance stands alone.
562 */
9c513e78 563 { .name = "FCSEIDR",
54bf36ed
FA
564 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
565 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
566 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
567 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9c513e78 568 { .name = "FCSEIDR_S",
54bf36ed
FA
569 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
570 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
571 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 572 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
54bf36ed
FA
573 /* Define the secure and non-secure context identifier CP registers
574 * separately because there is no secure bank in V8 (no _EL3). This allows
575 * the secure register to be properly reset and migrated. In the
576 * non-secure case, the 32-bit register will have reset and migration
577 * disabled during registration as it is handled by the 64-bit instance.
578 */
579 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 580 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
581 .access = PL1_RW, .accessfn = access_tvm_trvm,
582 .secure = ARM_CP_SECSTATE_NS,
54bf36ed
FA
583 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
584 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9c513e78 585 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
54bf36ed 586 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
587 .access = PL1_RW, .accessfn = access_tvm_trvm,
588 .secure = ARM_CP_SECSTATE_S,
54bf36ed 589 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 590 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
591};
592
593static const ARMCPRegInfo not_v8_cp_reginfo[] = {
594 /* NB: Some of these registers exist in v8 but with more precise
595 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
596 */
597 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
598 { .name = "DACR",
599 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
84929218 600 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
601 .writefn = dacr_write, .raw_writefn = raw_write,
602 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
603 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a903c449
EI
604 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
605 * For v6 and v5, these mappings are overly broad.
4fdd17dd 606 */
a903c449
EI
607 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
608 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
609 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
610 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
611 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
612 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
613 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 614 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
615 /* Cache maintenance ops; some of this space may be overridden later. */
616 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
617 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
618 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
619};
620
7d57f408
PM
621static const ARMCPRegInfo not_v6_cp_reginfo[] = {
622 /* Not all pre-v6 cores implemented this WFI, so this is slightly
623 * over-broad.
624 */
625 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
626 .access = PL1_W, .type = ARM_CP_WFI },
7d57f408
PM
627};
628
629static const ARMCPRegInfo not_v7_cp_reginfo[] = {
630 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
631 * is UNPREDICTABLE; we choose to NOP as most implementations do).
632 */
633 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
634 .access = PL1_W, .type = ARM_CP_WFI },
34f90529
PM
635 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
636 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
637 * OMAPCP will override this space.
638 */
639 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
640 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
641 .resetvalue = 0 },
642 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
643 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
644 .resetvalue = 0 },
776d4e5c
PM
645 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
646 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 647 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 648 .resetvalue = 0 },
50300698
PM
649 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
650 * implementing it as RAZ means the "debug architecture version" bits
651 * will read as a reserved value, which should cause Linux to not try
652 * to use the debug hardware.
653 */
654 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
655 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
995939a6
PM
656 /* MMU TLB control. Note that the wildcarding means we cover not just
657 * the unified TLB ops but also the dside/iside/inner-shareable variants.
658 */
659 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
660 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 661 .type = ARM_CP_NO_RAW },
995939a6
PM
662 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
663 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 664 .type = ARM_CP_NO_RAW },
995939a6
PM
665 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
666 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 667 .type = ARM_CP_NO_RAW },
995939a6
PM
668 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
669 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 670 .type = ARM_CP_NO_RAW },
a903c449
EI
671 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
672 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
673 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
674 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
675};
676
c4241c7d
PM
677static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
678 uint64_t value)
2771db27 679{
f0aff255
FA
680 uint32_t mask = 0;
681
682 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
683 if (!arm_feature(env, ARM_FEATURE_V8)) {
684 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
685 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
686 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
687 */
7fbc6a40 688 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
f0aff255 689 /* VFP coprocessor: cp10 & cp11 [23:20] */
fab8ad39
RH
690 mask |= R_CPACR_ASEDIS_MASK |
691 R_CPACR_D32DIS_MASK |
692 R_CPACR_CP11_MASK |
693 R_CPACR_CP10_MASK;
f0aff255
FA
694
695 if (!arm_feature(env, ARM_FEATURE_NEON)) {
696 /* ASEDIS [31] bit is RAO/WI */
fab8ad39 697 value |= R_CPACR_ASEDIS_MASK;
f0aff255
FA
698 }
699
700 /* VFPv3 and upwards with NEON implement 32 double precision
701 * registers (D0-D31).
702 */
a6627f5f 703 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
f0aff255 704 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
fab8ad39 705 value |= R_CPACR_D32DIS_MASK;
f0aff255
FA
706 }
707 }
708 value &= mask;
2771db27 709 }
fc1120a7
PM
710
711 /*
712 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
713 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
714 */
715 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
716 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39
RH
717 mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
718 value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
fc1120a7
PM
719 }
720
7ebd5f2e 721 env->cp15.cpacr_el1 = value;
2771db27
PM
722}
723
fc1120a7
PM
724static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
725{
726 /*
727 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
728 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
729 */
730 uint64_t value = env->cp15.cpacr_el1;
731
732 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
733 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39 734 value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
fc1120a7
PM
735 }
736 return value;
737}
738
739
5deac39c
PM
740static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
741{
742 /* Call cpacr_write() so that we reset with the correct RAO bits set
743 * for our CPU features.
744 */
745 cpacr_write(env, ri, 0);
746}
747
3f208fd7
PM
748static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
749 bool isread)
c6f19164
GB
750{
751 if (arm_feature(env, ARM_FEATURE_V8)) {
752 /* Check if CPACR accesses are to be trapped to EL2 */
e6ef0169 753 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
fab8ad39 754 FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
c6f19164
GB
755 return CP_ACCESS_TRAP_EL2;
756 /* Check if CPACR accesses are to be trapped to EL3 */
757 } else if (arm_current_el(env) < 3 &&
fab8ad39 758 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
c6f19164
GB
759 return CP_ACCESS_TRAP_EL3;
760 }
761 }
762
763 return CP_ACCESS_OK;
764}
765
3f208fd7
PM
766static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
767 bool isread)
c6f19164
GB
768{
769 /* Check if CPTR accesses are set to trap to EL3 */
fab8ad39
RH
770 if (arm_current_el(env) == 2 &&
771 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
c6f19164
GB
772 return CP_ACCESS_TRAP_EL3;
773 }
774
775 return CP_ACCESS_OK;
776}
777
7d57f408
PM
778static const ARMCPRegInfo v6_cp_reginfo[] = {
779 /* prefetch by MVA in v6, NOP in v7 */
780 { .name = "MVA_prefetch",
781 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
782 .access = PL1_W, .type = ARM_CP_NOP },
6df99dec
SS
783 /* We need to break the TB after ISB to execute self-modifying code
784 * correctly and also to take any pending interrupts immediately.
785 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
786 */
7d57f408 787 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 788 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 789 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 790 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 791 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 792 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 793 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218 794 .access = PL1_RW, .accessfn = access_tvm_trvm,
b848ce2b
FA
795 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
796 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31
PM
797 .resetvalue = 0, },
798 /* Watchpoint Fault Address Register : should actually only be present
799 * for 1136, 1176, 11MPCore.
800 */
801 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
802 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 803 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 804 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
7ebd5f2e 805 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
fc1120a7 806 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
7d57f408
PM
807};
808
57a4a11b
AL
809typedef struct pm_event {
810 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
811 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
812 bool (*supported)(CPUARMState *);
813 /*
814 * Retrieve the current count of the underlying event. The programmed
815 * counters hold a difference from the return value from this function
816 */
817 uint64_t (*get_count)(CPUARMState *);
4e7beb0c
AL
818 /*
819 * Return how many nanoseconds it will take (at a minimum) for count events
820 * to occur. A negative value indicates the counter will never overflow, or
821 * that the counter has otherwise arranged for the overflow bit to be set
822 * and the PMU interrupt to be raised on overflow.
823 */
824 int64_t (*ns_per_count)(uint64_t);
57a4a11b
AL
825} pm_event;
826
b2e23725
AL
827static bool event_always_supported(CPUARMState *env)
828{
829 return true;
830}
831
0d4bfd7d
AL
832static uint64_t swinc_get_count(CPUARMState *env)
833{
834 /*
835 * SW_INCR events are written directly to the pmevcntr's by writes to
836 * PMSWINC, so there is no underlying count maintained by the PMU itself
837 */
838 return 0;
839}
840
4e7beb0c
AL
841static int64_t swinc_ns_per(uint64_t ignored)
842{
843 return -1;
844}
845
b2e23725
AL
846/*
847 * Return the underlying cycle count for the PMU cycle counters. If we're in
848 * usermode, simply return 0.
849 */
850static uint64_t cycles_get_count(CPUARMState *env)
851{
852#ifndef CONFIG_USER_ONLY
853 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
854 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
855#else
856 return cpu_get_host_ticks();
857#endif
858}
859
860#ifndef CONFIG_USER_ONLY
4e7beb0c
AL
861static int64_t cycles_ns_per(uint64_t cycles)
862{
863 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
864}
865
b2e23725
AL
866static bool instructions_supported(CPUARMState *env)
867{
740b1759 868 return icount_enabled() == 1; /* Precise instruction counting */
b2e23725
AL
869}
870
871static uint64_t instructions_get_count(CPUARMState *env)
872{
8191d368 873 return (uint64_t)icount_get_raw();
b2e23725 874}
4e7beb0c
AL
875
876static int64_t instructions_ns_per(uint64_t icount)
877{
8191d368 878 return icount_to_ns((int64_t)icount);
4e7beb0c 879}
b2e23725
AL
880#endif
881
a793bcd0 882static bool pmuv3p1_events_supported(CPUARMState *env)
0727f63b
PM
883{
884 /* For events which are supported in any v8.1 PMU */
a793bcd0 885 return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
0727f63b
PM
886}
887
a793bcd0 888static bool pmuv3p4_events_supported(CPUARMState *env)
15dd1ebd
PM
889{
890 /* For events which are supported in any v8.1 PMU */
a793bcd0 891 return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
15dd1ebd
PM
892}
893
0727f63b
PM
894static uint64_t zero_event_get_count(CPUARMState *env)
895{
896 /* For events which on QEMU never fire, so their count is always zero */
897 return 0;
898}
899
900static int64_t zero_event_ns_per(uint64_t cycles)
901{
902 /* An event which never fires can never overflow */
903 return -1;
904}
905
57a4a11b 906static const pm_event pm_events[] = {
0d4bfd7d
AL
907 { .number = 0x000, /* SW_INCR */
908 .supported = event_always_supported,
909 .get_count = swinc_get_count,
4e7beb0c 910 .ns_per_count = swinc_ns_per,
0d4bfd7d 911 },
b2e23725
AL
912#ifndef CONFIG_USER_ONLY
913 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
914 .supported = instructions_supported,
915 .get_count = instructions_get_count,
4e7beb0c 916 .ns_per_count = instructions_ns_per,
b2e23725
AL
917 },
918 { .number = 0x011, /* CPU_CYCLES, Cycle */
919 .supported = event_always_supported,
920 .get_count = cycles_get_count,
4e7beb0c 921 .ns_per_count = cycles_ns_per,
0727f63b 922 },
b2e23725 923#endif
0727f63b 924 { .number = 0x023, /* STALL_FRONTEND */
a793bcd0 925 .supported = pmuv3p1_events_supported,
0727f63b
PM
926 .get_count = zero_event_get_count,
927 .ns_per_count = zero_event_ns_per,
928 },
929 { .number = 0x024, /* STALL_BACKEND */
a793bcd0 930 .supported = pmuv3p1_events_supported,
0727f63b
PM
931 .get_count = zero_event_get_count,
932 .ns_per_count = zero_event_ns_per,
933 },
15dd1ebd 934 { .number = 0x03c, /* STALL */
a793bcd0 935 .supported = pmuv3p4_events_supported,
15dd1ebd
PM
936 .get_count = zero_event_get_count,
937 .ns_per_count = zero_event_ns_per,
938 },
57a4a11b
AL
939};
940
941/*
942 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
943 * events (i.e. the statistical profiling extension), this implementation
944 * should first be updated to something sparse instead of the current
945 * supported_event_map[] array.
946 */
15dd1ebd 947#define MAX_EVENT_ID 0x3c
57a4a11b
AL
948#define UNSUPPORTED_EVENT UINT16_MAX
949static uint16_t supported_event_map[MAX_EVENT_ID + 1];
950
951/*
bf8d0969
AL
952 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
953 * of ARM event numbers to indices in our pm_events array.
57a4a11b
AL
954 *
955 * Note: Events in the 0x40XX range are not currently supported.
956 */
bf8d0969 957void pmu_init(ARMCPU *cpu)
57a4a11b 958{
57a4a11b
AL
959 unsigned int i;
960
bf8d0969
AL
961 /*
962 * Empty supported_event_map and cpu->pmceid[01] before adding supported
963 * events to them
964 */
57a4a11b
AL
965 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
966 supported_event_map[i] = UNSUPPORTED_EVENT;
967 }
bf8d0969
AL
968 cpu->pmceid0 = 0;
969 cpu->pmceid1 = 0;
57a4a11b
AL
970
971 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
972 const pm_event *cnt = &pm_events[i];
973 assert(cnt->number <= MAX_EVENT_ID);
974 /* We do not currently support events in the 0x40xx range */
975 assert(cnt->number <= 0x3f);
976
bf8d0969 977 if (cnt->supported(&cpu->env)) {
57a4a11b 978 supported_event_map[cnt->number] = i;
67da43d6 979 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
bf8d0969
AL
980 if (cnt->number & 0x20) {
981 cpu->pmceid1 |= event_mask;
982 } else {
983 cpu->pmceid0 |= event_mask;
984 }
57a4a11b
AL
985 }
986 }
57a4a11b
AL
987}
988
5ecdd3e4
AL
989/*
990 * Check at runtime whether a PMU event is supported for the current machine
991 */
992static bool event_supported(uint16_t number)
993{
994 if (number > MAX_EVENT_ID) {
995 return false;
996 }
997 return supported_event_map[number] != UNSUPPORTED_EVENT;
998}
999
3f208fd7
PM
1000static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1001 bool isread)
200ac0ef 1002{
3b163b01 1003 /* Performance monitor registers user accessibility is controlled
1fce1ba9
PM
1004 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1005 * trapping to EL2 or EL3 for other accesses.
200ac0ef 1006 */
1fce1ba9 1007 int el = arm_current_el(env);
59dd089c 1008 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1fce1ba9 1009
6ecd0b6b 1010 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 1011 return CP_ACCESS_TRAP;
200ac0ef 1012 }
59dd089c 1013 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1fce1ba9
PM
1014 return CP_ACCESS_TRAP_EL2;
1015 }
1016 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1017 return CP_ACCESS_TRAP_EL3;
1018 }
1019
fcd25206 1020 return CP_ACCESS_OK;
200ac0ef
PM
1021}
1022
6ecd0b6b
AB
1023static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1024 const ARMCPRegInfo *ri,
1025 bool isread)
1026{
1027 /* ER: event counter read trap control */
1028 if (arm_feature(env, ARM_FEATURE_V8)
1029 && arm_current_el(env) == 0
1030 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1031 && isread) {
1032 return CP_ACCESS_OK;
1033 }
1034
1035 return pmreg_access(env, ri, isread);
1036}
1037
1038static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1039 const ARMCPRegInfo *ri,
1040 bool isread)
1041{
1042 /* SW: software increment write trap control */
1043 if (arm_feature(env, ARM_FEATURE_V8)
1044 && arm_current_el(env) == 0
1045 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1046 && !isread) {
1047 return CP_ACCESS_OK;
1048 }
1049
1050 return pmreg_access(env, ri, isread);
1051}
1052
6ecd0b6b
AB
1053static CPAccessResult pmreg_access_selr(CPUARMState *env,
1054 const ARMCPRegInfo *ri,
1055 bool isread)
1056{
1057 /* ER: event counter read trap control */
1058 if (arm_feature(env, ARM_FEATURE_V8)
1059 && arm_current_el(env) == 0
1060 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1061 return CP_ACCESS_OK;
1062 }
1063
1064 return pmreg_access(env, ri, isread);
1065}
1066
1067static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1068 const ARMCPRegInfo *ri,
1069 bool isread)
1070{
1071 /* CR: cycle counter read trap control */
1072 if (arm_feature(env, ARM_FEATURE_V8)
1073 && arm_current_el(env) == 0
1074 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1075 && isread) {
1076 return CP_ACCESS_OK;
1077 }
1078
1079 return pmreg_access(env, ri, isread);
1080}
1081
01765386
PM
1082/*
1083 * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1084 * We use these to decide whether we need to wrap a write to MDCR_EL2
1085 * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1086 */
47b385da
PM
1087#define MDCR_EL2_PMU_ENABLE_BITS \
1088 (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
0b42f4fa 1089#define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
01765386 1090
033614c4
AL
1091/* Returns true if the counter (pass 31 for PMCCNTR) should count events using
1092 * the current EL, security state, and register configuration.
1093 */
1094static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
87124fde 1095{
033614c4
AL
1096 uint64_t filter;
1097 bool e, p, u, nsk, nsu, nsh, m;
872d2034 1098 bool enabled, prohibited = false, filtered;
033614c4
AL
1099 bool secure = arm_is_secure(env);
1100 int el = arm_current_el(env);
59dd089c
RDC
1101 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1102 uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
87124fde 1103
cbbb3041
AJ
1104 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1105 return false;
1106 }
1107
033614c4
AL
1108 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1109 (counter < hpmn || counter == 31)) {
1110 e = env->cp15.c9_pmcr & PMCRE;
1111 } else {
59dd089c 1112 e = mdcr_el2 & MDCR_HPME;
87124fde 1113 }
033614c4 1114 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
87124fde 1115
872d2034
PM
1116 /* Is event counting prohibited? */
1117 if (el == 2 && (counter < hpmn || counter == 31)) {
1118 prohibited = mdcr_el2 & MDCR_HPMD;
1119 }
1120 if (secure) {
1121 prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
033614c4
AL
1122 }
1123
0b42f4fa
PM
1124 if (counter == 31) {
1125 /*
1126 * The cycle counter defaults to running. PMCR.DP says "disable
1127 * the cycle counter when event counting is prohibited".
1128 * Some MDCR bits disable the cycle counter specifically.
1129 */
1130 prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1131 if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1132 if (secure) {
1133 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1134 }
1135 if (el == 2) {
1136 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1137 }
1138 }
033614c4
AL
1139 }
1140
5ecdd3e4
AL
1141 if (counter == 31) {
1142 filter = env->cp15.pmccfiltr_el0;
1143 } else {
1144 filter = env->cp15.c14_pmevtyper[counter];
1145 }
033614c4
AL
1146
1147 p = filter & PMXEVTYPER_P;
1148 u = filter & PMXEVTYPER_U;
1149 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1150 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1151 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1152 m = arm_el_is_aa64(env, 1) &&
1153 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1154
1155 if (el == 0) {
1156 filtered = secure ? u : u != nsu;
1157 } else if (el == 1) {
1158 filtered = secure ? p : p != nsk;
1159 } else if (el == 2) {
1160 filtered = !nsh;
1161 } else { /* EL3 */
1162 filtered = m != p;
1163 }
1164
5ecdd3e4
AL
1165 if (counter != 31) {
1166 /*
1167 * If not checking PMCCNTR, ensure the counter is setup to an event we
1168 * support
1169 */
1170 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1171 if (!event_supported(event)) {
1172 return false;
1173 }
1174 }
1175
033614c4 1176 return enabled && !prohibited && !filtered;
87124fde 1177}
033614c4 1178
f4efb4b2
AL
1179static void pmu_update_irq(CPUARMState *env)
1180{
2fc0cc0e 1181 ARMCPU *cpu = env_archcpu(env);
f4efb4b2
AL
1182 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1183 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1184}
1185
b57aa7bd
PM
1186static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1187{
1188 /*
1189 * Return true if the clock divider is enabled and the cycle counter
1190 * is supposed to tick only once every 64 clock cycles. This is
1191 * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1192 * (64-bit) cycle counter PMCR.D has no effect.
1193 */
1194 return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1195}
1196
47b385da
PM
1197static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1198{
1199 /* Return true if the specified event counter is configured to be 64 bit */
1200
1201 /* This isn't intended to be used with the cycle counter */
1202 assert(counter < 31);
1203
1204 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1205 return false;
1206 }
1207
1208 if (arm_feature(env, ARM_FEATURE_EL2)) {
1209 /*
1210 * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1211 * current security state, so we don't use arm_mdcr_el2_eff() here.
1212 */
1213 bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1214 int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1215
1216 if (hpmn != 0 && counter >= hpmn) {
1217 return hlp;
1218 }
1219 }
1220 return env->cp15.c9_pmcr & PMCRLP;
1221}
1222
5d05b9d4
AL
1223/*
1224 * Ensure c15_ccnt is the guest-visible count so that operations such as
1225 * enabling/disabling the counter or filtering, modifying the count itself,
1226 * etc. can be done logically. This is essentially a no-op if the counter is
1227 * not enabled at the time of the call.
1228 */
f2b2f53f 1229static void pmccntr_op_start(CPUARMState *env)
ec7b4ce4 1230{
b2e23725 1231 uint64_t cycles = cycles_get_count(env);
ec7b4ce4 1232
033614c4 1233 if (pmu_counter_enabled(env, 31)) {
5d05b9d4 1234 uint64_t eff_cycles = cycles;
b57aa7bd 1235 if (pmccntr_clockdiv_enabled(env)) {
5d05b9d4
AL
1236 eff_cycles /= 64;
1237 }
1238
f4efb4b2
AL
1239 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1240
1241 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1242 1ull << 63 : 1ull << 31;
1243 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
76e25d41 1244 env->cp15.c9_pmovsr |= (1ULL << 31);
f4efb4b2
AL
1245 pmu_update_irq(env);
1246 }
1247
1248 env->cp15.c15_ccnt = new_pmccntr;
ec7b4ce4 1249 }
5d05b9d4
AL
1250 env->cp15.c15_ccnt_delta = cycles;
1251}
ec7b4ce4 1252
5d05b9d4
AL
1253/*
1254 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1255 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1256 * pmccntr_op_start.
1257 */
f2b2f53f 1258static void pmccntr_op_finish(CPUARMState *env)
5d05b9d4 1259{
033614c4 1260 if (pmu_counter_enabled(env, 31)) {
4e7beb0c
AL
1261#ifndef CONFIG_USER_ONLY
1262 /* Calculate when the counter will next overflow */
1263 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1264 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1265 remaining_cycles = (uint32_t)remaining_cycles;
1266 }
1267 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1268
1269 if (overflow_in > 0) {
f1dd2506
PM
1270 int64_t overflow_at;
1271
1272 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1273 overflow_in, &overflow_at)) {
1274 ARMCPU *cpu = env_archcpu(env);
1275 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1276 }
4e7beb0c
AL
1277 }
1278#endif
5d05b9d4 1279
4e7beb0c 1280 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
b57aa7bd 1281 if (pmccntr_clockdiv_enabled(env)) {
5d05b9d4
AL
1282 prev_cycles /= 64;
1283 }
5d05b9d4 1284 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
ec7b4ce4
AF
1285 }
1286}
1287
5ecdd3e4
AL
1288static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1289{
1290
1291 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1292 uint64_t count = 0;
1293 if (event_supported(event)) {
1294 uint16_t event_idx = supported_event_map[event];
1295 count = pm_events[event_idx].get_count(env);
1296 }
1297
1298 if (pmu_counter_enabled(env, counter)) {
47b385da
PM
1299 uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1300 uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1301 1ULL << 63 : 1ULL << 31;
f4efb4b2 1302
47b385da 1303 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
f4efb4b2
AL
1304 env->cp15.c9_pmovsr |= (1 << counter);
1305 pmu_update_irq(env);
1306 }
1307 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
5ecdd3e4
AL
1308 }
1309 env->cp15.c14_pmevcntr_delta[counter] = count;
1310}
1311
1312static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1313{
1314 if (pmu_counter_enabled(env, counter)) {
4e7beb0c
AL
1315#ifndef CONFIG_USER_ONLY
1316 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1317 uint16_t event_idx = supported_event_map[event];
47b385da
PM
1318 uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1319 int64_t overflow_in;
1320
1321 if (!pmevcntr_is_64_bit(env, counter)) {
1322 delta = (uint32_t)delta;
1323 }
1324 overflow_in = pm_events[event_idx].ns_per_count(delta);
4e7beb0c
AL
1325
1326 if (overflow_in > 0) {
f1dd2506
PM
1327 int64_t overflow_at;
1328
1329 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1330 overflow_in, &overflow_at)) {
1331 ARMCPU *cpu = env_archcpu(env);
1332 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1333 }
4e7beb0c
AL
1334 }
1335#endif
1336
5ecdd3e4
AL
1337 env->cp15.c14_pmevcntr_delta[counter] -=
1338 env->cp15.c14_pmevcntr[counter];
1339 }
1340}
1341
5d05b9d4
AL
1342void pmu_op_start(CPUARMState *env)
1343{
5ecdd3e4 1344 unsigned int i;
5d05b9d4 1345 pmccntr_op_start(env);
5ecdd3e4
AL
1346 for (i = 0; i < pmu_num_counters(env); i++) {
1347 pmevcntr_op_start(env, i);
1348 }
5d05b9d4
AL
1349}
1350
1351void pmu_op_finish(CPUARMState *env)
1352{
5ecdd3e4 1353 unsigned int i;
5d05b9d4 1354 pmccntr_op_finish(env);
5ecdd3e4
AL
1355 for (i = 0; i < pmu_num_counters(env); i++) {
1356 pmevcntr_op_finish(env, i);
1357 }
5d05b9d4
AL
1358}
1359
033614c4
AL
1360void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1361{
1362 pmu_op_start(&cpu->env);
1363}
1364
1365void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1366{
1367 pmu_op_finish(&cpu->env);
1368}
1369
4e7beb0c
AL
1370void arm_pmu_timer_cb(void *opaque)
1371{
1372 ARMCPU *cpu = opaque;
1373
1374 /*
1375 * Update all the counter values based on the current underlying counts,
1376 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1377 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1378 * counter may expire.
1379 */
1380 pmu_op_start(&cpu->env);
1381 pmu_op_finish(&cpu->env);
1382}
1383
c4241c7d
PM
1384static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1385 uint64_t value)
200ac0ef 1386{
5d05b9d4 1387 pmu_op_start(env);
7c2cb42b
AF
1388
1389 if (value & PMCRC) {
1390 /* The counter has been reset */
1391 env->cp15.c15_ccnt = 0;
1392 }
1393
5ecdd3e4
AL
1394 if (value & PMCRP) {
1395 unsigned int i;
1396 for (i = 0; i < pmu_num_counters(env); i++) {
1397 env->cp15.c14_pmevcntr[i] = 0;
1398 }
1399 }
1400
9323e79f
PM
1401 env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1402 env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
7c2cb42b 1403
5d05b9d4 1404 pmu_op_finish(env);
7c2cb42b
AF
1405}
1406
0d4bfd7d
AL
1407static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1408 uint64_t value)
1409{
1410 unsigned int i;
47b385da
PM
1411 uint64_t overflow_mask, new_pmswinc;
1412
0d4bfd7d
AL
1413 for (i = 0; i < pmu_num_counters(env); i++) {
1414 /* Increment a counter's count iff: */
1415 if ((value & (1 << i)) && /* counter's bit is set */
1416 /* counter is enabled and not filtered */
1417 pmu_counter_enabled(env, i) &&
1418 /* counter is SW_INCR */
1419 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1420 pmevcntr_op_start(env, i);
f4efb4b2
AL
1421
1422 /*
1423 * Detect if this write causes an overflow since we can't predict
1424 * PMSWINC overflows like we can for other events
1425 */
47b385da
PM
1426 new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1427
1428 overflow_mask = pmevcntr_is_64_bit(env, i) ?
1429 1ULL << 63 : 1ULL << 31;
f4efb4b2 1430
47b385da 1431 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
f4efb4b2
AL
1432 env->cp15.c9_pmovsr |= (1 << i);
1433 pmu_update_irq(env);
1434 }
1435
1436 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1437
0d4bfd7d
AL
1438 pmevcntr_op_finish(env, i);
1439 }
1440 }
1441}
1442
7c2cb42b
AF
1443static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1444{
5d05b9d4
AL
1445 uint64_t ret;
1446 pmccntr_op_start(env);
1447 ret = env->cp15.c15_ccnt;
1448 pmccntr_op_finish(env);
1449 return ret;
7c2cb42b
AF
1450}
1451
6b040780
WH
1452static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1453 uint64_t value)
1454{
1455 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1456 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1457 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1458 * accessed.
1459 */
1460 env->cp15.c9_pmselr = value & 0x1f;
1461}
1462
7c2cb42b
AF
1463static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1464 uint64_t value)
1465{
5d05b9d4
AL
1466 pmccntr_op_start(env);
1467 env->cp15.c15_ccnt = value;
1468 pmccntr_op_finish(env);
200ac0ef 1469}
421c7ebd
PC
1470
1471static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1472 uint64_t value)
1473{
1474 uint64_t cur_val = pmccntr_read(env, NULL);
1475
1476 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1477}
1478
0614601c
AF
1479static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1480 uint64_t value)
1481{
5d05b9d4 1482 pmccntr_op_start(env);
4b8afa1f
AL
1483 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1484 pmccntr_op_finish(env);
1485}
1486
1487static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1488 uint64_t value)
1489{
1490 pmccntr_op_start(env);
1491 /* M is not accessible from AArch32 */
1492 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1493 (value & PMCCFILTR);
5d05b9d4 1494 pmccntr_op_finish(env);
0614601c
AF
1495}
1496
4b8afa1f
AL
1497static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1498{
1499 /* M is not visible in AArch32 */
1500 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1501}
1502
c4241c7d 1503static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1504 uint64_t value)
1505{
01765386 1506 pmu_op_start(env);
7ece99b1 1507 value &= pmu_counter_mask(env);
200ac0ef 1508 env->cp15.c9_pmcnten |= value;
01765386 1509 pmu_op_finish(env);
200ac0ef
PM
1510}
1511
c4241c7d
PM
1512static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1513 uint64_t value)
200ac0ef 1514{
01765386 1515 pmu_op_start(env);
7ece99b1 1516 value &= pmu_counter_mask(env);
200ac0ef 1517 env->cp15.c9_pmcnten &= ~value;
01765386 1518 pmu_op_finish(env);
200ac0ef
PM
1519}
1520
c4241c7d
PM
1521static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1522 uint64_t value)
200ac0ef 1523{
599b71e2 1524 value &= pmu_counter_mask(env);
200ac0ef 1525 env->cp15.c9_pmovsr &= ~value;
f4efb4b2 1526 pmu_update_irq(env);
200ac0ef
PM
1527}
1528
327dd510
AL
1529static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1530 uint64_t value)
1531{
1532 value &= pmu_counter_mask(env);
1533 env->cp15.c9_pmovsr |= value;
f4efb4b2 1534 pmu_update_irq(env);
327dd510
AL
1535}
1536
5ecdd3e4
AL
1537static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1538 uint64_t value, const uint8_t counter)
200ac0ef 1539{
5ecdd3e4
AL
1540 if (counter == 31) {
1541 pmccfiltr_write(env, ri, value);
1542 } else if (counter < pmu_num_counters(env)) {
1543 pmevcntr_op_start(env, counter);
1544
1545 /*
1546 * If this counter's event type is changing, store the current
1547 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1548 * pmevcntr_op_finish has the correct baseline when it converts back to
1549 * a delta.
1550 */
1551 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1552 PMXEVTYPER_EVTCOUNT;
1553 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1554 if (old_event != new_event) {
1555 uint64_t count = 0;
1556 if (event_supported(new_event)) {
1557 uint16_t event_idx = supported_event_map[new_event];
1558 count = pm_events[event_idx].get_count(env);
1559 }
1560 env->cp15.c14_pmevcntr_delta[counter] = count;
1561 }
1562
1563 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1564 pmevcntr_op_finish(env, counter);
1565 }
fdb86656
WH
1566 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1567 * PMSELR value is equal to or greater than the number of implemented
1568 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1569 */
5ecdd3e4
AL
1570}
1571
1572static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1573 const uint8_t counter)
1574{
1575 if (counter == 31) {
1576 return env->cp15.pmccfiltr_el0;
1577 } else if (counter < pmu_num_counters(env)) {
1578 return env->cp15.c14_pmevtyper[counter];
1579 } else {
1580 /*
1581 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1582 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1583 */
1584 return 0;
1585 }
1586}
1587
1588static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1589 uint64_t value)
1590{
1591 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1592 pmevtyper_write(env, ri, value, counter);
1593}
1594
1595static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1596 uint64_t value)
1597{
1598 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1599 env->cp15.c14_pmevtyper[counter] = value;
1600
1601 /*
1602 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1603 * pmu_op_finish calls when loading saved state for a migration. Because
1604 * we're potentially updating the type of event here, the value written to
1605 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1606 * different counter type. Therefore, we need to set this value to the
1607 * current count for the counter type we're writing so that pmu_op_finish
1608 * has the correct count for its calculation.
1609 */
1610 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1611 if (event_supported(event)) {
1612 uint16_t event_idx = supported_event_map[event];
1613 env->cp15.c14_pmevcntr_delta[counter] =
1614 pm_events[event_idx].get_count(env);
fdb86656
WH
1615 }
1616}
1617
5ecdd3e4
AL
1618static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1619{
1620 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1621 return pmevtyper_read(env, ri, counter);
1622}
1623
1624static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1625 uint64_t value)
1626{
1627 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1628}
1629
fdb86656
WH
1630static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1631{
5ecdd3e4
AL
1632 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1633}
1634
1635static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1636 uint64_t value, uint8_t counter)
1637{
47b385da
PM
1638 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1639 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1640 value &= MAKE_64BIT_MASK(0, 32);
1641 }
5ecdd3e4
AL
1642 if (counter < pmu_num_counters(env)) {
1643 pmevcntr_op_start(env, counter);
1644 env->cp15.c14_pmevcntr[counter] = value;
1645 pmevcntr_op_finish(env, counter);
1646 }
1647 /*
1648 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1649 * are CONSTRAINED UNPREDICTABLE.
fdb86656 1650 */
5ecdd3e4
AL
1651}
1652
1653static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1654 uint8_t counter)
1655{
1656 if (counter < pmu_num_counters(env)) {
1657 uint64_t ret;
1658 pmevcntr_op_start(env, counter);
1659 ret = env->cp15.c14_pmevcntr[counter];
1660 pmevcntr_op_finish(env, counter);
47b385da
PM
1661 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1662 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1663 ret &= MAKE_64BIT_MASK(0, 32);
1664 }
5ecdd3e4 1665 return ret;
fdb86656 1666 } else {
5ecdd3e4
AL
1667 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1668 * are CONSTRAINED UNPREDICTABLE. */
fdb86656
WH
1669 return 0;
1670 }
200ac0ef
PM
1671}
1672
5ecdd3e4
AL
1673static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1674 uint64_t value)
1675{
1676 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1677 pmevcntr_write(env, ri, value, counter);
1678}
1679
1680static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1681{
1682 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1683 return pmevcntr_read(env, ri, counter);
1684}
1685
1686static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1687 uint64_t value)
1688{
1689 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1690 assert(counter < pmu_num_counters(env));
1691 env->cp15.c14_pmevcntr[counter] = value;
1692 pmevcntr_write(env, ri, value, counter);
1693}
1694
1695static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1696{
1697 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1698 assert(counter < pmu_num_counters(env));
1699 return env->cp15.c14_pmevcntr[counter];
1700}
1701
1702static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1703 uint64_t value)
1704{
1705 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1706}
1707
1708static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1709{
1710 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1711}
1712
c4241c7d 1713static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1714 uint64_t value)
1715{
6ecd0b6b
AB
1716 if (arm_feature(env, ARM_FEATURE_V8)) {
1717 env->cp15.c9_pmuserenr = value & 0xf;
1718 } else {
1719 env->cp15.c9_pmuserenr = value & 1;
1720 }
200ac0ef
PM
1721}
1722
c4241c7d
PM
1723static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1724 uint64_t value)
200ac0ef
PM
1725{
1726 /* We have no event counters so only the C bit can be changed */
7ece99b1 1727 value &= pmu_counter_mask(env);
200ac0ef 1728 env->cp15.c9_pminten |= value;
f4efb4b2 1729 pmu_update_irq(env);
200ac0ef
PM
1730}
1731
c4241c7d
PM
1732static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1733 uint64_t value)
200ac0ef 1734{
7ece99b1 1735 value &= pmu_counter_mask(env);
200ac0ef 1736 env->cp15.c9_pminten &= ~value;
f4efb4b2 1737 pmu_update_irq(env);
200ac0ef
PM
1738}
1739
c4241c7d
PM
1740static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1741 uint64_t value)
8641136c 1742{
a505d7fe
PM
1743 /* Note that even though the AArch64 view of this register has bits
1744 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1745 * architectural requirements for bits which are RES0 only in some
1746 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1747 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1748 */
855ea66d 1749 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1750}
1751
64e0e2de
EI
1752static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1753{
ea22747c 1754 /* Begin with base v8.0 state. */
06f2adcc 1755 uint64_t valid_mask = 0x3fff;
2fc0cc0e 1756 ARMCPU *cpu = env_archcpu(env);
d902ae75 1757 uint64_t changed;
ea22747c 1758
bfe43e3d
RH
1759 /*
1760 * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1761 * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1762 * Instead, choose the format based on the mode of EL3.
1763 */
1764 if (arm_el_is_aa64(env, 3)) {
1765 value |= SCR_FW | SCR_AW; /* RES1 */
1766 valid_mask &= ~SCR_NET; /* RES0 */
252e8c69 1767
6bcbb07a
RH
1768 if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1769 !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1770 value |= SCR_RW; /* RAO/WI */
1771 }
da3d8b13
RH
1772 if (cpu_isar_feature(aa64_ras, cpu)) {
1773 valid_mask |= SCR_TERR;
1774 }
252e8c69
RH
1775 if (cpu_isar_feature(aa64_lor, cpu)) {
1776 valid_mask |= SCR_TLOR;
1777 }
1778 if (cpu_isar_feature(aa64_pauth, cpu)) {
1779 valid_mask |= SCR_API | SCR_APK;
1780 }
926c1b97
RDC
1781 if (cpu_isar_feature(aa64_sel2, cpu)) {
1782 valid_mask |= SCR_EEL2;
1783 }
8ddb300b
RH
1784 if (cpu_isar_feature(aa64_mte, cpu)) {
1785 valid_mask |= SCR_ATA;
1786 }
7cb1e618
RH
1787 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1788 valid_mask |= SCR_ENSCXT;
1789 }
7ac61020
PM
1790 if (cpu_isar_feature(aa64_doublefault, cpu)) {
1791 valid_mask |= SCR_EASE | SCR_NMEA;
1792 }
06f2adcc
JF
1793 if (cpu_isar_feature(aa64_sme, cpu)) {
1794 valid_mask |= SCR_ENTP2;
1795 }
ea22747c
RH
1796 } else {
1797 valid_mask &= ~(SCR_RW | SCR_ST);
da3d8b13
RH
1798 if (cpu_isar_feature(aa32_ras, cpu)) {
1799 valid_mask |= SCR_TERR;
1800 }
ea22747c 1801 }
64e0e2de
EI
1802
1803 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1804 valid_mask &= ~SCR_HCE;
1805
1806 /* On ARMv7, SMD (or SCD as it is called in v7) is only
1807 * supported if EL2 exists. The bit is UNK/SBZP when
1808 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1809 * when EL2 is unavailable.
4eb27640 1810 * On ARMv8, this bit is always available.
64e0e2de 1811 */
4eb27640
GB
1812 if (arm_feature(env, ARM_FEATURE_V7) &&
1813 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1814 valid_mask &= ~SCR_SMD;
1815 }
1816 }
1817
1818 /* Clear all-context RES0 bits. */
1819 value &= valid_mask;
d902ae75
RH
1820 changed = env->cp15.scr_el3 ^ value;
1821 env->cp15.scr_el3 = value;
1822
1823 /*
1824 * If SCR_EL3.NS changes, i.e. arm_is_secure_below_el3, then
1825 * we must invalidate all TLBs below EL3.
1826 */
1827 if (changed & SCR_NS) {
1828 tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1829 ARMMMUIdxBit_E20_0 |
1830 ARMMMUIdxBit_E10_1 |
1831 ARMMMUIdxBit_E20_2 |
1832 ARMMMUIdxBit_E10_1_PAN |
1833 ARMMMUIdxBit_E20_2_PAN |
1834 ARMMMUIdxBit_E2));
1835 }
64e0e2de
EI
1836}
1837
10d0ef3e
MN
1838static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1839{
1840 /*
1841 * scr_write will set the RES1 bits on an AArch64-only CPU.
1842 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1843 */
1844 scr_write(env, ri, 0);
1845}
1846
630fcd4d
MZ
1847static CPAccessResult access_aa64_tid2(CPUARMState *env,
1848 const ARMCPRegInfo *ri,
1849 bool isread)
1850{
1851 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) {
1852 return CP_ACCESS_TRAP_EL2;
1853 }
1854
1855 return CP_ACCESS_OK;
1856}
1857
c4241c7d 1858static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c 1859{
2fc0cc0e 1860 ARMCPU *cpu = env_archcpu(env);
b85a1fd6
FA
1861
1862 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1863 * bank
1864 */
1865 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1866 ri->secure & ARM_CP_SECSTATE_S);
1867
1868 return cpu->ccsidr[index];
776d4e5c
PM
1869}
1870
c4241c7d
PM
1871static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1872 uint64_t value)
776d4e5c 1873{
8d5c773e 1874 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1875}
1876
1090b9c6
PM
1877static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1878{
29a0af61 1879 CPUState *cs = env_cpu(env);
cc974d5c
RDC
1880 bool el1 = arm_current_el(env) == 1;
1881 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1090b9c6
PM
1882 uint64_t ret = 0;
1883
cc974d5c 1884 if (hcr_el2 & HCR_IMO) {
636540e9
PM
1885 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1886 ret |= CPSR_I;
1887 }
1888 } else {
1889 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1890 ret |= CPSR_I;
1891 }
1090b9c6 1892 }
636540e9 1893
cc974d5c 1894 if (hcr_el2 & HCR_FMO) {
636540e9
PM
1895 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1896 ret |= CPSR_F;
1897 }
1898 } else {
1899 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1900 ret |= CPSR_F;
1901 }
1090b9c6 1902 }
636540e9 1903
3c29632f
RH
1904 if (hcr_el2 & HCR_AMO) {
1905 if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1906 ret |= CPSR_A;
1907 }
1908 }
1909
1090b9c6
PM
1910 return ret;
1911}
1912
93fbc983
MZ
1913static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1914 bool isread)
1915{
1916 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1917 return CP_ACCESS_TRAP_EL2;
1918 }
1919
1920 return CP_ACCESS_OK;
1921}
1922
1923static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1924 bool isread)
1925{
1926 if (arm_feature(env, ARM_FEATURE_V8)) {
1927 return access_aa64_tid1(env, ri, isread);
1928 }
1929
1930 return CP_ACCESS_OK;
1931}
1932
e9aa6c21 1933static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
1934 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1935 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1936 .access = PL1_W, .type = ARM_CP_NOP },
200ac0ef
PM
1937 /* Performance monitors are implementation defined in v7,
1938 * but with an ARM recommended set of registers, which we
ac689a2e 1939 * follow.
200ac0ef
PM
1940 *
1941 * Performance registers fall into three categories:
1942 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1943 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1944 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1945 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1946 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1947 */
1948 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7f4fbfb5 1949 .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
8521466b 1950 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1951 .writefn = pmcntenset_write,
1952 .accessfn = pmreg_access,
1953 .raw_writefn = raw_write },
7f4fbfb5 1954 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
8521466b
AF
1955 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1956 .access = PL0_RW, .accessfn = pmreg_access,
1957 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1958 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 1959 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
1960 .access = PL0_RW,
1961 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
1962 .accessfn = pmreg_access,
1963 .writefn = pmcntenclr_write,
7f4fbfb5 1964 .type = ARM_CP_ALIAS | ARM_CP_IO },
8521466b
AF
1965 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1966 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1967 .access = PL0_RW, .accessfn = pmreg_access,
7f4fbfb5 1968 .type = ARM_CP_ALIAS | ARM_CP_IO,
8521466b
AF
1969 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1970 .writefn = pmcntenclr_write },
200ac0ef 1971 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
f4efb4b2 1972 .access = PL0_RW, .type = ARM_CP_IO,
e4e91a21 1973 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206
PM
1974 .accessfn = pmreg_access,
1975 .writefn = pmovsr_write,
1976 .raw_writefn = raw_write },
978364f1
AF
1977 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1978 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1979 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 1980 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
1981 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1982 .writefn = pmovsr_write,
1983 .raw_writefn = raw_write },
200ac0ef 1984 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
f4efb4b2
AL
1985 .access = PL0_W, .accessfn = pmreg_access_swinc,
1986 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d
AL
1987 .writefn = pmswinc_write },
1988 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1989 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
f4efb4b2
AL
1990 .access = PL0_W, .accessfn = pmreg_access_swinc,
1991 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d 1992 .writefn = pmswinc_write },
6b040780
WH
1993 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1994 .access = PL0_RW, .type = ARM_CP_ALIAS,
1995 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 1996 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
1997 .raw_writefn = raw_write},
1998 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1999 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 2000 .access = PL0_RW, .accessfn = pmreg_access_selr,
6b040780
WH
2001 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2002 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 2003 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 2004 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
421c7ebd 2005 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 2006 .accessfn = pmreg_access_ccntr },
8521466b
AF
2007 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2008 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 2009 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
8521466b 2010 .type = ARM_CP_IO,
980ebe87
AL
2011 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2012 .readfn = pmccntr_read, .writefn = pmccntr_write,
2013 .raw_readfn = raw_read, .raw_writefn = raw_write, },
4b8afa1f
AL
2014 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2015 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2016 .access = PL0_RW, .accessfn = pmreg_access,
2017 .type = ARM_CP_ALIAS | ARM_CP_IO,
2018 .resetvalue = 0, },
8521466b
AF
2019 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2020 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
980ebe87 2021 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
8521466b
AF
2022 .access = PL0_RW, .accessfn = pmreg_access,
2023 .type = ARM_CP_IO,
2024 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2025 .resetvalue = 0, },
200ac0ef 2026 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
5ecdd3e4
AL
2027 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2028 .accessfn = pmreg_access,
fdb86656
WH
2029 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2030 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2031 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
5ecdd3e4
AL
2032 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2033 .accessfn = pmreg_access,
fdb86656 2034 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
200ac0ef 2035 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
5ecdd3e4
AL
2036 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2037 .accessfn = pmreg_access_xevcntr,
2038 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2039 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2040 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2041 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2042 .accessfn = pmreg_access_xevcntr,
2043 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
200ac0ef 2044 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 2045 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 2046 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 2047 .resetvalue = 0,
d4e6df63 2048 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
2049 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2050 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 2051 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
2052 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2053 .resetvalue = 0,
2054 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 2055 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 2056 .access = PL1_RW, .accessfn = access_tpm,
b7d793ad 2057 .type = ARM_CP_ALIAS | ARM_CP_IO,
e6ec5457 2058 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 2059 .resetvalue = 0,
d4e6df63 2060 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
2061 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2062 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2063 .access = PL1_RW, .accessfn = access_tpm,
2064 .type = ARM_CP_IO,
2065 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2066 .writefn = pmintenset_write, .raw_writefn = raw_write,
2067 .resetvalue = 0x0 },
200ac0ef 2068 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
fc5f6856 2069 .access = PL1_RW, .accessfn = access_tpm,
887c0f15 2070 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
200ac0ef 2071 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 2072 .writefn = pmintenclr_write, },
978364f1
AF
2073 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2074 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
fc5f6856 2075 .access = PL1_RW, .accessfn = access_tpm,
887c0f15 2076 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
978364f1
AF
2077 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2078 .writefn = pmintenclr_write },
7da845b0
PM
2079 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2080 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
630fcd4d
MZ
2081 .access = PL1_R,
2082 .accessfn = access_aa64_tid2,
2083 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
2084 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2085 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
630fcd4d
MZ
2086 .access = PL1_RW,
2087 .accessfn = access_aa64_tid2,
2088 .writefn = csselr_write, .resetvalue = 0,
b85a1fd6
FA
2089 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2090 offsetof(CPUARMState, cp15.csselr_ns) } },
776d4e5c
PM
2091 /* Auxiliary ID register: this actually has an IMPDEF value but for now
2092 * just RAZ for all cores:
2093 */
0ff644a7
PM
2094 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2095 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
93fbc983
MZ
2096 .access = PL1_R, .type = ARM_CP_CONST,
2097 .accessfn = access_aa64_tid1,
2098 .resetvalue = 0 },
f32cdad5
PM
2099 /* Auxiliary fault status registers: these also are IMPDEF, and we
2100 * choose to RAZ/WI for all cores.
2101 */
2102 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2103 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
84929218
RH
2104 .access = PL1_RW, .accessfn = access_tvm_trvm,
2105 .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
2106 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2107 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
84929218
RH
2108 .access = PL1_RW, .accessfn = access_tvm_trvm,
2109 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427
PM
2110 /* MAIR can just read-as-written because we don't implement caches
2111 * and so don't need to care about memory attributes.
2112 */
2113 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2114 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
84929218
RH
2115 .access = PL1_RW, .accessfn = access_tvm_trvm,
2116 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 2117 .resetvalue = 0 },
4cfb8ad8
PM
2118 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2119 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2120 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2121 .resetvalue = 0 },
b0fe2427
PM
2122 /* For non-long-descriptor page tables these are PRRR and NMRR;
2123 * regardless they still act as reads-as-written for QEMU.
b0fe2427 2124 */
1281f8e3 2125 /* MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
2126 * allows them to assign the correct fieldoffset based on the endianness
2127 * handled in the field definitions.
2128 */
a903c449 2129 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
84929218
RH
2130 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2131 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2132 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2133 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 2134 .resetfn = arm_cp_reset_ignore },
a903c449 2135 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
84929218
RH
2136 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2137 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2138 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2139 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 2140 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
2141 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2142 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
7a0e58fa 2143 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
2144 /* 32 bit ITLB invalidates */
2145 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
30881b73
RH
2146 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2147 .writefn = tlbiall_write },
995939a6 2148 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
30881b73
RH
2149 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2150 .writefn = tlbimva_write },
995939a6 2151 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
30881b73
RH
2152 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2153 .writefn = tlbiasid_write },
995939a6
PM
2154 /* 32 bit DTLB invalidates */
2155 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
30881b73
RH
2156 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2157 .writefn = tlbiall_write },
995939a6 2158 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
30881b73
RH
2159 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2160 .writefn = tlbimva_write },
995939a6 2161 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
30881b73
RH
2162 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2163 .writefn = tlbiasid_write },
995939a6
PM
2164 /* 32 bit TLB invalidates */
2165 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73
RH
2166 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2167 .writefn = tlbiall_write },
995939a6 2168 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73
RH
2169 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2170 .writefn = tlbimva_write },
995939a6 2171 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73
RH
2172 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2173 .writefn = tlbiasid_write },
995939a6 2174 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73
RH
2175 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2176 .writefn = tlbimvaa_write },
995939a6
PM
2177};
2178
2179static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2180 /* 32 bit TLB invalidates, Inner Shareable */
2181 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
30881b73
RH
2182 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2183 .writefn = tlbiall_is_write },
995939a6 2184 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
30881b73
RH
2185 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2186 .writefn = tlbimva_is_write },
995939a6 2187 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
30881b73 2188 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 2189 .writefn = tlbiasid_is_write },
995939a6 2190 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
30881b73 2191 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 2192 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
2193};
2194
327dd510
AL
2195static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2196 /* PMOVSSET is not implemented in v7 before v7ve */
2197 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2198 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2199 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2200 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2201 .writefn = pmovsset_write,
2202 .raw_writefn = raw_write },
2203 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2204 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2205 .access = PL0_RW, .accessfn = pmreg_access,
f4efb4b2 2206 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2207 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2208 .writefn = pmovsset_write,
2209 .raw_writefn = raw_write },
327dd510
AL
2210};
2211
c4241c7d
PM
2212static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2213 uint64_t value)
c326b979
PM
2214{
2215 value &= 1;
2216 env->teecr = value;
c326b979
PM
2217}
2218
cc7613bf
PM
2219static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2220 bool isread)
2221{
2222 /*
2223 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2224 * at all, so we don't need to check whether we're v8A.
2225 */
2226 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2227 (env->cp15.hstr_el2 & HSTR_TTEE)) {
2228 return CP_ACCESS_TRAP_EL2;
2229 }
2230 return CP_ACCESS_OK;
2231}
2232
3f208fd7
PM
2233static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2234 bool isread)
c326b979 2235{
dcbff19b 2236 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 2237 return CP_ACCESS_TRAP;
c326b979 2238 }
cc7613bf 2239 return teecr_access(env, ri, isread);
c326b979
PM
2240}
2241
2242static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2243 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2244 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2245 .resetvalue = 0,
cc7613bf 2246 .writefn = teecr_write, .accessfn = teecr_access },
c326b979
PM
2247 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2248 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 2249 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
2250};
2251
4d31c596 2252static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
2253 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2254 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2255 .access = PL0_RW,
54bf36ed 2256 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
2257 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2258 .access = PL0_RW,
54bf36ed
FA
2259 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2260 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
2261 .resetfn = arm_cp_reset_ignore },
2262 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2263 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2264 .access = PL0_R|PL1_W,
54bf36ed
FA
2265 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2266 .resetvalue = 0},
4d31c596
PM
2267 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2268 .access = PL0_R|PL1_W,
54bf36ed
FA
2269 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2270 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 2271 .resetfn = arm_cp_reset_ignore },
54bf36ed 2272 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 2273 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 2274 .access = PL1_RW,
54bf36ed
FA
2275 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2276 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2277 .access = PL1_RW,
2278 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2279 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2280 .resetvalue = 0 },
4d31c596
PM
2281};
2282
55d284af
PM
2283#ifndef CONFIG_USER_ONLY
2284
3f208fd7
PM
2285static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2286 bool isread)
00108f2d 2287{
75502672
PM
2288 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2289 * Writable only at the highest implemented exception level.
2290 */
2291 int el = arm_current_el(env);
5bc84371
RH
2292 uint64_t hcr;
2293 uint32_t cntkctl;
75502672
PM
2294
2295 switch (el) {
2296 case 0:
5bc84371
RH
2297 hcr = arm_hcr_el2_eff(env);
2298 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2299 cntkctl = env->cp15.cnthctl_el2;
2300 } else {
2301 cntkctl = env->cp15.c14_cntkctl;
2302 }
2303 if (!extract32(cntkctl, 0, 2)) {
75502672
PM
2304 return CP_ACCESS_TRAP;
2305 }
2306 break;
2307 case 1:
2308 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2309 arm_is_secure_below_el3(env)) {
2310 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2311 return CP_ACCESS_TRAP_UNCATEGORIZED;
2312 }
2313 break;
2314 case 2:
2315 case 3:
2316 break;
00108f2d 2317 }
75502672
PM
2318
2319 if (!isread && el < arm_highest_el(env)) {
2320 return CP_ACCESS_TRAP_UNCATEGORIZED;
2321 }
2322
00108f2d
PM
2323 return CP_ACCESS_OK;
2324}
2325
3f208fd7
PM
2326static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2327 bool isread)
00108f2d 2328{
0b6440af 2329 unsigned int cur_el = arm_current_el(env);
e6ef0169 2330 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2331 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2332
5bc84371
RH
2333 switch (cur_el) {
2334 case 0:
2335 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2336 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2337 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2338 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2339 }
0b6440af 2340
5bc84371
RH
2341 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2342 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2343 return CP_ACCESS_TRAP;
2344 }
2345
2346 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2347 if (hcr & HCR_E2H) {
2348 if (timeridx == GTIMER_PHYS &&
2349 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2350 return CP_ACCESS_TRAP_EL2;
2351 }
2352 } else {
2353 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
e6ef0169 2354 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2355 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2356 return CP_ACCESS_TRAP_EL2;
2357 }
2358 }
2359 break;
2360
2361 case 1:
2362 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
e6ef0169 2363 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2364 (hcr & HCR_E2H
2365 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2366 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2367 return CP_ACCESS_TRAP_EL2;
2368 }
2369 break;
0b6440af 2370 }
00108f2d
PM
2371 return CP_ACCESS_OK;
2372}
2373
3f208fd7
PM
2374static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2375 bool isread)
00108f2d 2376{
0b6440af 2377 unsigned int cur_el = arm_current_el(env);
e6ef0169 2378 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2379 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2380
5bc84371
RH
2381 switch (cur_el) {
2382 case 0:
2383 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2384 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2385 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2386 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2387 }
0b6440af 2388
5bc84371
RH
2389 /*
2390 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2391 * EL0 if EL0[PV]TEN is zero.
2392 */
2393 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2394 return CP_ACCESS_TRAP;
2395 }
2396 /* fall through */
2397
2398 case 1:
e6ef0169 2399 if (has_el2 && timeridx == GTIMER_PHYS) {
5bc84371
RH
2400 if (hcr & HCR_E2H) {
2401 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2402 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2403 return CP_ACCESS_TRAP_EL2;
2404 }
2405 } else {
2406 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2407 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2408 return CP_ACCESS_TRAP_EL2;
2409 }
2410 }
2411 }
2412 break;
0b6440af 2413 }
00108f2d
PM
2414 return CP_ACCESS_OK;
2415}
2416
2417static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
2418 const ARMCPRegInfo *ri,
2419 bool isread)
00108f2d 2420{
3f208fd7 2421 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2422}
2423
2424static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
2425 const ARMCPRegInfo *ri,
2426 bool isread)
00108f2d 2427{
3f208fd7 2428 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2429}
2430
3f208fd7
PM
2431static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2432 bool isread)
00108f2d 2433{
3f208fd7 2434 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2435}
2436
3f208fd7
PM
2437static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2438 bool isread)
00108f2d 2439{
3f208fd7 2440 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2441}
2442
b4d3978c 2443static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
2444 const ARMCPRegInfo *ri,
2445 bool isread)
b4d3978c
PM
2446{
2447 /* The AArch64 register view of the secure physical timer is
2448 * always accessible from EL3, and configurably accessible from
2449 * Secure EL1.
2450 */
2451 switch (arm_current_el(env)) {
2452 case 1:
2453 if (!arm_is_secure(env)) {
2454 return CP_ACCESS_TRAP;
2455 }
2456 if (!(env->cp15.scr_el3 & SCR_ST)) {
2457 return CP_ACCESS_TRAP_EL3;
2458 }
2459 return CP_ACCESS_OK;
2460 case 0:
2461 case 2:
2462 return CP_ACCESS_TRAP;
2463 case 3:
2464 return CP_ACCESS_OK;
2465 default:
2466 g_assert_not_reached();
2467 }
2468}
2469
55d284af
PM
2470static uint64_t gt_get_countervalue(CPUARMState *env)
2471{
7def8754
AJ
2472 ARMCPU *cpu = env_archcpu(env);
2473
2474 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
55d284af
PM
2475}
2476
2477static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2478{
2479 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2480
2481 if (gt->ctl & 1) {
2482 /* Timer enabled: calculate and set current ISTATUS, irq, and
2483 * reset timer to when ISTATUS next has to change
2484 */
edac4d8a
EI
2485 uint64_t offset = timeridx == GTIMER_VIRT ?
2486 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
2487 uint64_t count = gt_get_countervalue(&cpu->env);
2488 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 2489 int istatus = count - offset >= gt->cval;
55d284af 2490 uint64_t nexttick;
194cbc49 2491 int irqstate;
55d284af
PM
2492
2493 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
2494
2495 irqstate = (istatus && !(gt->ctl & 2));
2496 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2497
55d284af
PM
2498 if (istatus) {
2499 /* Next transition is when count rolls back over to zero */
2500 nexttick = UINT64_MAX;
2501 } else {
2502 /* Next transition is when we hit cval */
edac4d8a 2503 nexttick = gt->cval + offset;
55d284af
PM
2504 }
2505 /* Note that the desired next expiry time might be beyond the
2506 * signed-64-bit range of a QEMUTimer -- in this case we just
2507 * set the timer for as far in the future as possible. When the
2508 * timer expires we will reset the timer for any remaining period.
2509 */
7def8754 2510 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
4a0245b6
AJ
2511 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2512 } else {
2513 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af 2514 }
194cbc49 2515 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
2516 } else {
2517 /* Timer disabled: ISTATUS and timer output always clear */
2518 gt->ctl &= ~4;
2519 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 2520 timer_del(cpu->gt_timer[timeridx]);
194cbc49 2521 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
2522 }
2523}
2524
0e3eca4c
EI
2525static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2526 int timeridx)
55d284af 2527{
2fc0cc0e 2528 ARMCPU *cpu = env_archcpu(env);
55d284af 2529
bc72ad67 2530 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
2531}
2532
c4241c7d 2533static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 2534{
c4241c7d 2535 return gt_get_countervalue(env);
55d284af
PM
2536}
2537
53d1f856
RH
2538static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2539{
2540 uint64_t hcr;
2541
2542 switch (arm_current_el(env)) {
2543 case 2:
2544 hcr = arm_hcr_el2_eff(env);
2545 if (hcr & HCR_E2H) {
2546 return 0;
2547 }
2548 break;
2549 case 0:
2550 hcr = arm_hcr_el2_eff(env);
2551 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2552 return 0;
2553 }
2554 break;
2555 }
2556
2557 return env->cp15.cntvoff_el2;
2558}
2559
edac4d8a
EI
2560static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2561{
53d1f856 2562 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
edac4d8a
EI
2563}
2564
c4241c7d 2565static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2566 int timeridx,
c4241c7d 2567 uint64_t value)
55d284af 2568{
194cbc49 2569 trace_arm_gt_cval_write(timeridx, value);
55d284af 2570 env->cp15.c14_timer[timeridx].cval = value;
2fc0cc0e 2571 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af 2572}
c4241c7d 2573
0e3eca4c
EI
2574static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2575 int timeridx)
55d284af 2576{
53d1f856
RH
2577 uint64_t offset = 0;
2578
2579 switch (timeridx) {
2580 case GTIMER_VIRT:
8c94b071 2581 case GTIMER_HYPVIRT:
53d1f856
RH
2582 offset = gt_virt_cnt_offset(env);
2583 break;
2584 }
55d284af 2585
c4241c7d 2586 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 2587 (gt_get_countervalue(env) - offset));
55d284af
PM
2588}
2589
c4241c7d 2590static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2591 int timeridx,
c4241c7d 2592 uint64_t value)
55d284af 2593{
53d1f856
RH
2594 uint64_t offset = 0;
2595
2596 switch (timeridx) {
2597 case GTIMER_VIRT:
8c94b071 2598 case GTIMER_HYPVIRT:
53d1f856
RH
2599 offset = gt_virt_cnt_offset(env);
2600 break;
2601 }
55d284af 2602
194cbc49 2603 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 2604 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 2605 sextract64(value, 0, 32);
2fc0cc0e 2606 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af
PM
2607}
2608
c4241c7d 2609static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2610 int timeridx,
c4241c7d 2611 uint64_t value)
55d284af 2612{
2fc0cc0e 2613 ARMCPU *cpu = env_archcpu(env);
55d284af
PM
2614 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2615
194cbc49 2616 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 2617 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
2618 if ((oldval ^ value) & 1) {
2619 /* Enable toggled */
2620 gt_recalc_timer(cpu, timeridx);
d3afacc7 2621 } else if ((oldval ^ value) & 2) {
55d284af
PM
2622 /* IMASK toggled: don't need to recalculate,
2623 * just set the interrupt line based on ISTATUS
2624 */
194cbc49
PM
2625 int irqstate = (oldval & 4) && !(value & 2);
2626
2627 trace_arm_gt_imask_toggle(timeridx, irqstate);
2628 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 2629 }
55d284af
PM
2630}
2631
0e3eca4c
EI
2632static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2633{
2634 gt_timer_reset(env, ri, GTIMER_PHYS);
2635}
2636
2637static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2638 uint64_t value)
2639{
2640 gt_cval_write(env, ri, GTIMER_PHYS, value);
2641}
2642
2643static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2644{
2645 return gt_tval_read(env, ri, GTIMER_PHYS);
2646}
2647
2648static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2649 uint64_t value)
2650{
2651 gt_tval_write(env, ri, GTIMER_PHYS, value);
2652}
2653
2654static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2655 uint64_t value)
2656{
2657 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2658}
2659
bb5972e4
RH
2660static int gt_phys_redir_timeridx(CPUARMState *env)
2661{
2662 switch (arm_mmu_idx(env)) {
2663 case ARMMMUIdx_E20_0:
2664 case ARMMMUIdx_E20_2:
452ef8cb 2665 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2666 return GTIMER_HYP;
2667 default:
2668 return GTIMER_PHYS;
2669 }
2670}
2671
2672static int gt_virt_redir_timeridx(CPUARMState *env)
2673{
2674 switch (arm_mmu_idx(env)) {
2675 case ARMMMUIdx_E20_0:
2676 case ARMMMUIdx_E20_2:
452ef8cb 2677 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2678 return GTIMER_HYPVIRT;
2679 default:
2680 return GTIMER_VIRT;
2681 }
2682}
2683
2684static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2685 const ARMCPRegInfo *ri)
2686{
2687 int timeridx = gt_phys_redir_timeridx(env);
2688 return env->cp15.c14_timer[timeridx].cval;
2689}
2690
2691static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2692 uint64_t value)
2693{
2694 int timeridx = gt_phys_redir_timeridx(env);
2695 gt_cval_write(env, ri, timeridx, value);
2696}
2697
2698static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2699 const ARMCPRegInfo *ri)
2700{
2701 int timeridx = gt_phys_redir_timeridx(env);
2702 return gt_tval_read(env, ri, timeridx);
2703}
2704
2705static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2706 uint64_t value)
2707{
2708 int timeridx = gt_phys_redir_timeridx(env);
2709 gt_tval_write(env, ri, timeridx, value);
2710}
2711
2712static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2713 const ARMCPRegInfo *ri)
2714{
2715 int timeridx = gt_phys_redir_timeridx(env);
2716 return env->cp15.c14_timer[timeridx].ctl;
2717}
2718
2719static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2720 uint64_t value)
2721{
2722 int timeridx = gt_phys_redir_timeridx(env);
2723 gt_ctl_write(env, ri, timeridx, value);
2724}
2725
0e3eca4c
EI
2726static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2727{
2728 gt_timer_reset(env, ri, GTIMER_VIRT);
2729}
2730
2731static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2732 uint64_t value)
2733{
2734 gt_cval_write(env, ri, GTIMER_VIRT, value);
2735}
2736
2737static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2738{
2739 return gt_tval_read(env, ri, GTIMER_VIRT);
2740}
2741
2742static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2743 uint64_t value)
2744{
2745 gt_tval_write(env, ri, GTIMER_VIRT, value);
2746}
2747
2748static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2749 uint64_t value)
2750{
2751 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2752}
2753
edac4d8a
EI
2754static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2755 uint64_t value)
2756{
2fc0cc0e 2757 ARMCPU *cpu = env_archcpu(env);
edac4d8a 2758
194cbc49 2759 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
2760 raw_write(env, ri, value);
2761 gt_recalc_timer(cpu, GTIMER_VIRT);
2762}
2763
bb5972e4
RH
2764static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2765 const ARMCPRegInfo *ri)
2766{
2767 int timeridx = gt_virt_redir_timeridx(env);
2768 return env->cp15.c14_timer[timeridx].cval;
2769}
2770
2771static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2772 uint64_t value)
2773{
2774 int timeridx = gt_virt_redir_timeridx(env);
2775 gt_cval_write(env, ri, timeridx, value);
2776}
2777
2778static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2779 const ARMCPRegInfo *ri)
2780{
2781 int timeridx = gt_virt_redir_timeridx(env);
2782 return gt_tval_read(env, ri, timeridx);
2783}
2784
2785static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2786 uint64_t value)
2787{
2788 int timeridx = gt_virt_redir_timeridx(env);
2789 gt_tval_write(env, ri, timeridx, value);
2790}
2791
2792static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2793 const ARMCPRegInfo *ri)
2794{
2795 int timeridx = gt_virt_redir_timeridx(env);
2796 return env->cp15.c14_timer[timeridx].ctl;
2797}
2798
2799static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2800 uint64_t value)
2801{
2802 int timeridx = gt_virt_redir_timeridx(env);
2803 gt_ctl_write(env, ri, timeridx, value);
2804}
2805
b0e66d95
EI
2806static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2807{
2808 gt_timer_reset(env, ri, GTIMER_HYP);
2809}
2810
2811static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2812 uint64_t value)
2813{
2814 gt_cval_write(env, ri, GTIMER_HYP, value);
2815}
2816
2817static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2818{
2819 return gt_tval_read(env, ri, GTIMER_HYP);
2820}
2821
2822static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2823 uint64_t value)
2824{
2825 gt_tval_write(env, ri, GTIMER_HYP, value);
2826}
2827
2828static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2829 uint64_t value)
2830{
2831 gt_ctl_write(env, ri, GTIMER_HYP, value);
2832}
2833
b4d3978c
PM
2834static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2835{
2836 gt_timer_reset(env, ri, GTIMER_SEC);
2837}
2838
2839static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2840 uint64_t value)
2841{
2842 gt_cval_write(env, ri, GTIMER_SEC, value);
2843}
2844
2845static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2846{
2847 return gt_tval_read(env, ri, GTIMER_SEC);
2848}
2849
2850static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2851 uint64_t value)
2852{
2853 gt_tval_write(env, ri, GTIMER_SEC, value);
2854}
2855
2856static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2857 uint64_t value)
2858{
2859 gt_ctl_write(env, ri, GTIMER_SEC, value);
2860}
2861
8c94b071
RH
2862static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2863{
2864 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2865}
2866
2867static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2868 uint64_t value)
2869{
2870 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
2871}
2872
2873static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2874{
2875 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
2876}
2877
2878static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2879 uint64_t value)
2880{
2881 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
2882}
2883
2884static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2885 uint64_t value)
2886{
2887 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
2888}
2889
55d284af
PM
2890void arm_gt_ptimer_cb(void *opaque)
2891{
2892 ARMCPU *cpu = opaque;
2893
2894 gt_recalc_timer(cpu, GTIMER_PHYS);
2895}
2896
2897void arm_gt_vtimer_cb(void *opaque)
2898{
2899 ARMCPU *cpu = opaque;
2900
2901 gt_recalc_timer(cpu, GTIMER_VIRT);
2902}
2903
b0e66d95
EI
2904void arm_gt_htimer_cb(void *opaque)
2905{
2906 ARMCPU *cpu = opaque;
2907
2908 gt_recalc_timer(cpu, GTIMER_HYP);
2909}
2910
b4d3978c
PM
2911void arm_gt_stimer_cb(void *opaque)
2912{
2913 ARMCPU *cpu = opaque;
2914
2915 gt_recalc_timer(cpu, GTIMER_SEC);
2916}
2917
8c94b071
RH
2918void arm_gt_hvtimer_cb(void *opaque)
2919{
2920 ARMCPU *cpu = opaque;
2921
2922 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
2923}
2924
96eec6b2
AJ
2925static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2926{
2927 ARMCPU *cpu = env_archcpu(env);
2928
2929 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2930}
2931
55d284af
PM
2932static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
2933 /* Note that CNTFRQ is purely reads-as-written for the benefit
2934 * of software; writing it doesn't actually change the timer frequency.
2935 * Our reset value matches the fixed frequency we implement the timer at.
2936 */
2937 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 2938 .type = ARM_CP_ALIAS,
a7adc4b7
PM
2939 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
2940 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
2941 },
2942 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
2943 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
2944 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af 2945 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
96eec6b2 2946 .resetfn = arm_gt_cntfrq_reset,
55d284af
PM
2947 },
2948 /* overall control: mostly access permissions */
a7adc4b7
PM
2949 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
2950 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
2951 .access = PL1_RW,
2952 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
2953 .resetvalue = 0,
2954 },
2955 /* per-timer control */
2956 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 2957 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 2958 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
2959 .accessfn = gt_ptimer_access,
2960 .fieldoffset = offsetoflow32(CPUARMState,
2961 cp15.c14_timer[GTIMER_PHYS].ctl),
bb5972e4
RH
2962 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2963 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7 2964 },
9c513e78 2965 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
2966 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
2967 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 2968 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
9ff9dd3c
PM
2969 .accessfn = gt_ptimer_access,
2970 .fieldoffset = offsetoflow32(CPUARMState,
2971 cp15.c14_timer[GTIMER_SEC].ctl),
2972 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
2973 },
a7adc4b7
PM
2974 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
2975 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
daf1dc5f 2976 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 2977 .accessfn = gt_ptimer_access,
55d284af
PM
2978 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
2979 .resetvalue = 0,
bb5972e4
RH
2980 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
2981 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2982 },
2983 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
daf1dc5f 2984 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
2985 .accessfn = gt_vtimer_access,
2986 .fieldoffset = offsetoflow32(CPUARMState,
2987 cp15.c14_timer[GTIMER_VIRT].ctl),
bb5972e4
RH
2988 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2989 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
2990 },
2991 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
2992 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
daf1dc5f 2993 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 2994 .accessfn = gt_vtimer_access,
55d284af
PM
2995 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
2996 .resetvalue = 0,
bb5972e4
RH
2997 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
2998 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
2999 },
3000 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3001 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 3002 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3003 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3004 .accessfn = gt_ptimer_access,
bb5972e4 3005 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
55d284af 3006 },
9c513e78 3007 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
3008 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3009 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3010 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
9ff9dd3c
PM
3011 .accessfn = gt_ptimer_access,
3012 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3013 },
a7adc4b7
PM
3014 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3015 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
daf1dc5f 3016 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3017 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
bb5972e4 3018 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
a7adc4b7 3019 },
55d284af 3020 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
daf1dc5f 3021 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3022 .accessfn = gt_vtimer_access,
bb5972e4 3023 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
55d284af 3024 },
a7adc4b7
PM
3025 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3026 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
daf1dc5f 3027 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3028 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
bb5972e4 3029 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
a7adc4b7 3030 },
55d284af
PM
3031 /* The counter itself */
3032 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 3033 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3034 .accessfn = gt_pct_access,
a7adc4b7
PM
3035 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3036 },
3037 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3038 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 3039 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3040 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
3041 },
3042 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 3043 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3044 .accessfn = gt_vct_access,
edac4d8a 3045 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
3046 },
3047 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3048 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 3049 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3050 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
3051 },
3052 /* Comparison value, indicating when the timer goes off */
3053 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3054 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3055 .access = PL0_RW,
7a0e58fa 3056 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3057 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 3058 .accessfn = gt_ptimer_access,
bb5972e4
RH
3059 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3060 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7 3061 },
9c513e78 3062 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3063 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3064 .access = PL0_RW,
9ff9dd3c
PM
3065 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3066 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3067 .accessfn = gt_ptimer_access,
3068 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3069 },
a7adc4b7
PM
3070 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3071 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
daf1dc5f 3072 .access = PL0_RW,
a7adc4b7
PM
3073 .type = ARM_CP_IO,
3074 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 3075 .resetvalue = 0, .accessfn = gt_ptimer_access,
bb5972e4
RH
3076 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3077 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
55d284af
PM
3078 },
3079 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
daf1dc5f 3080 .access = PL0_RW,
7a0e58fa 3081 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3082 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 3083 .accessfn = gt_vtimer_access,
bb5972e4
RH
3084 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3085 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
3086 },
3087 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3088 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
daf1dc5f 3089 .access = PL0_RW,
a7adc4b7
PM
3090 .type = ARM_CP_IO,
3091 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3092 .resetvalue = 0, .accessfn = gt_vtimer_access,
bb5972e4
RH
3093 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3094 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
55d284af 3095 },
b4d3978c
PM
3096 /* Secure timer -- this is actually restricted to only EL3
3097 * and configurably Secure-EL1 via the accessfn.
3098 */
3099 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3100 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3101 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3102 .accessfn = gt_stimer_access,
3103 .readfn = gt_sec_tval_read,
3104 .writefn = gt_sec_tval_write,
3105 .resetfn = gt_sec_timer_reset,
3106 },
3107 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3108 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3109 .type = ARM_CP_IO, .access = PL1_RW,
3110 .accessfn = gt_stimer_access,
3111 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3112 .resetvalue = 0,
3113 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3114 },
3115 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3116 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3117 .type = ARM_CP_IO, .access = PL1_RW,
3118 .accessfn = gt_stimer_access,
3119 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3120 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3121 },
55d284af
PM
3122};
3123
bb5972e4
RH
3124static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3125 bool isread)
3126{
3127 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3128 return CP_ACCESS_TRAP;
3129 }
3130 return CP_ACCESS_OK;
3131}
3132
55d284af 3133#else
26c4a83b
AB
3134
3135/* In user-mode most of the generic timer registers are inaccessible
3136 * however modern kernels (4.12+) allow access to cntvct_el0
55d284af 3137 */
26c4a83b
AB
3138
3139static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3140{
7def8754
AJ
3141 ARMCPU *cpu = env_archcpu(env);
3142
26c4a83b
AB
3143 /* Currently we have no support for QEMUTimer in linux-user so we
3144 * can't call gt_get_countervalue(env), instead we directly
3145 * call the lower level functions.
3146 */
7def8754 3147 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
26c4a83b
AB
3148}
3149
6cc7a3ae 3150static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
26c4a83b
AB
3151 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3152 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3153 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3154 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3155 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3156 },
3157 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3158 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3159 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3160 .readfn = gt_virt_cnt_read,
3161 },
6cc7a3ae
PM
3162};
3163
55d284af
PM
3164#endif
3165
c4241c7d 3166static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 3167{
891a2fe7 3168 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 3169 raw_write(env, ri, value);
891a2fe7 3170 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 3171 raw_write(env, ri, value & 0xfffff6ff);
4a501606 3172 } else {
8d5c773e 3173 raw_write(env, ri, value & 0xfffff1ff);
4a501606 3174 }
4a501606
PM
3175}
3176
3177#ifndef CONFIG_USER_ONLY
3178/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 3179
3f208fd7
PM
3180static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3181 bool isread)
92611c00
PM
3182{
3183 if (ri->opc2 & 4) {
926c1b97 3184 /* The ATS12NSO* operations must trap to EL3 or EL2 if executed in
87562e4f
PM
3185 * Secure EL1 (which can only happen if EL3 is AArch64).
3186 * They are simply UNDEF if executed from NS EL1.
3187 * They function normally from EL2 or EL3.
92611c00 3188 */
87562e4f
PM
3189 if (arm_current_el(env) == 1) {
3190 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
3191 if (env->cp15.scr_el3 & SCR_EEL2) {
3192 return CP_ACCESS_TRAP_UNCATEGORIZED_EL2;
3193 }
87562e4f
PM
3194 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
3195 }
3196 return CP_ACCESS_TRAP_UNCATEGORIZED;
3197 }
92611c00
PM
3198 }
3199 return CP_ACCESS_OK;
3200}
3201
9fb005b0 3202#ifdef CONFIG_TCG
060e8a48 3203static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
7aee3cb9
RH
3204 MMUAccessType access_type, ARMMMUIdx mmu_idx,
3205 bool is_secure)
4a501606 3206{
b7cc4e82 3207 bool ret;
01c097f7 3208 uint64_t par64;
1313e2d7 3209 bool format64 = false;
e14b5a23 3210 ARMMMUFaultInfo fi = {};
de05a709 3211 GetPhysAddrResult res = {};
4a501606 3212
7aee3cb9
RH
3213 ret = get_phys_addr_with_secure(env, value, access_type, mmu_idx,
3214 is_secure, &res, &fi);
1313e2d7 3215
9f225e60
PM
3216 /*
3217 * ATS operations only do S1 or S1+S2 translations, so we never
3218 * have to deal with the ARMCacheAttrs format for S2 only.
3219 */
de05a709 3220 assert(!res.cacheattrs.is_s2_format);
9f225e60 3221
0710b2fa
PM
3222 if (ret) {
3223 /*
3224 * Some kinds of translation fault must cause exceptions rather
3225 * than being reported in the PAR.
3226 */
3227 int current_el = arm_current_el(env);
3228 int target_el;
3229 uint32_t syn, fsr, fsc;
3230 bool take_exc = false;
3231
b1a10c86 3232 if (fi.s1ptw && current_el == 1
fee7aa46 3233 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
0710b2fa
PM
3234 /*
3235 * Synchronous stage 2 fault on an access made as part of the
3236 * translation table walk for AT S1E0* or AT S1E1* insn
3237 * executed from NS EL1. If this is a synchronous external abort
3238 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3239 * to EL3. Otherwise the fault is taken as an exception to EL2,
3240 * and HPFAR_EL2 holds the faulting IPA.
3241 */
3242 if (fi.type == ARMFault_SyncExternalOnWalk &&
3243 (env->cp15.scr_el3 & SCR_EA)) {
3244 target_el = 3;
3245 } else {
3246 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
9861248f
RDC
3247 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3248 env->cp15.hpfar_el2 |= HPFAR_NS;
3249 }
0710b2fa
PM
3250 target_el = 2;
3251 }
3252 take_exc = true;
3253 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3254 /*
3255 * Synchronous external aborts during a translation table walk
3256 * are taken as Data Abort exceptions.
3257 */
3258 if (fi.stage2) {
3259 if (current_el == 3) {
3260 target_el = 3;
3261 } else {
3262 target_el = 2;
3263 }
3264 } else {
3265 target_el = exception_target_el(env);
3266 }
3267 take_exc = true;
3268 }
3269
3270 if (take_exc) {
3271 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3272 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3273 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3274 fsr = arm_fi_to_lfsc(&fi);
3275 fsc = extract32(fsr, 0, 6);
3276 } else {
3277 fsr = arm_fi_to_sfsc(&fi);
3278 fsc = 0x3f;
3279 }
3280 /*
3281 * Report exception with ESR indicating a fault due to a
3282 * translation table walk for a cache maintenance instruction.
3283 */
e24fd076 3284 syn = syn_data_abort_no_iss(current_el == target_el, 0,
0710b2fa
PM
3285 fi.ea, 1, fi.s1ptw, 1, fsc);
3286 env->exception.vaddress = value;
3287 env->exception.fsr = fsr;
3288 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3289 }
3290 }
3291
1313e2d7
EI
3292 if (is_a64(env)) {
3293 format64 = true;
3294 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3295 /*
3296 * ATS1Cxx:
3297 * * TTBCR.EAE determines whether the result is returned using the
3298 * 32-bit or the 64-bit PAR format
3299 * * Instructions executed in Hyp mode always use the 64bit format
3300 *
3301 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3302 * * The Non-secure TTBCR.EAE bit is set to 1
3303 * * The implementation includes EL2, and the value of HCR.VM is 1
3304 *
9d1bab33
PM
3305 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3306 *
23463e0e 3307 * ATS1Hx always uses the 64bit format.
1313e2d7
EI
3308 */
3309 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3310
3311 if (arm_feature(env, ARM_FEATURE_EL2)) {
452ef8cb
RH
3312 if (mmu_idx == ARMMMUIdx_E10_0 ||
3313 mmu_idx == ARMMMUIdx_E10_1 ||
3314 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9d1bab33 3315 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
1313e2d7
EI
3316 } else {
3317 format64 |= arm_current_el(env) == 2;
3318 }
3319 }
3320 }
3321
3322 if (format64) {
5efe9ed4 3323 /* Create a 64-bit PAR */
01c097f7 3324 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 3325 if (!ret) {
de05a709
RH
3326 par64 |= res.phys & ~0xfffULL;
3327 if (!res.attrs.secure) {
8bf5b6a9
PM
3328 par64 |= (1 << 9); /* NS */
3329 }
de05a709
RH
3330 par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3331 par64 |= res.cacheattrs.shareability << 7; /* SH */
4a501606 3332 } else {
5efe9ed4
PM
3333 uint32_t fsr = arm_fi_to_lfsc(&fi);
3334
702a9357 3335 par64 |= 1; /* F */
b7cc4e82 3336 par64 |= (fsr & 0x3f) << 1; /* FS */
0f7b791b
PM
3337 if (fi.stage2) {
3338 par64 |= (1 << 9); /* S */
3339 }
3340 if (fi.s1ptw) {
3341 par64 |= (1 << 8); /* PTW */
3342 }
4a501606
PM
3343 }
3344 } else {
b7cc4e82 3345 /* fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
3346 * translation table format (with WnR always clear).
3347 * Convert it to a 32-bit PAR.
3348 */
b7cc4e82 3349 if (!ret) {
702a9357 3350 /* We do not set any attribute bits in the PAR */
de05a709 3351 if (res.page_size == (1 << 24)
702a9357 3352 && arm_feature(env, ARM_FEATURE_V7)) {
de05a709 3353 par64 = (res.phys & 0xff000000) | (1 << 1);
702a9357 3354 } else {
de05a709 3355 par64 = res.phys & 0xfffff000;
702a9357 3356 }
de05a709 3357 if (!res.attrs.secure) {
8bf5b6a9
PM
3358 par64 |= (1 << 9); /* NS */
3359 }
702a9357 3360 } else {
5efe9ed4
PM
3361 uint32_t fsr = arm_fi_to_sfsc(&fi);
3362
b7cc4e82
PC
3363 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3364 ((fsr & 0xf) << 1) | 1;
702a9357 3365 }
4a501606 3366 }
060e8a48
PM
3367 return par64;
3368}
9fb005b0 3369#endif /* CONFIG_TCG */
060e8a48
PM
3370
3371static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3372{
9fb005b0 3373#ifdef CONFIG_TCG
03ae85f8 3374 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 3375 uint64_t par64;
d3649702
PM
3376 ARMMMUIdx mmu_idx;
3377 int el = arm_current_el(env);
3378 bool secure = arm_is_secure_below_el3(env);
060e8a48 3379
d3649702
PM
3380 switch (ri->opc2 & 6) {
3381 case 0:
04b07d29 3382 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
d3649702
PM
3383 switch (el) {
3384 case 3:
d902ae75 3385 mmu_idx = ARMMMUIdx_E3;
7aee3cb9 3386 secure = true;
d3649702
PM
3387 break;
3388 case 2:
b6ad6062 3389 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
04b07d29 3390 /* fall through */
d3649702 3391 case 1:
04b07d29 3392 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
d902ae75 3393 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
04b07d29 3394 } else {
d902ae75 3395 mmu_idx = ARMMMUIdx_Stage1_E1;
04b07d29 3396 }
d3649702
PM
3397 break;
3398 default:
3399 g_assert_not_reached();
3400 }
3401 break;
3402 case 2:
3403 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3404 switch (el) {
3405 case 3:
d902ae75 3406 mmu_idx = ARMMMUIdx_E10_0;
7aee3cb9 3407 secure = true;
d3649702
PM
3408 break;
3409 case 2:
b1a10c86 3410 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
2859d7b5 3411 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3412 break;
3413 case 1:
d902ae75 3414 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3415 break;
3416 default:
3417 g_assert_not_reached();
3418 }
3419 break;
3420 case 4:
3421 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
01b98b68 3422 mmu_idx = ARMMMUIdx_E10_1;
7aee3cb9 3423 secure = false;
d3649702
PM
3424 break;
3425 case 6:
3426 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
01b98b68 3427 mmu_idx = ARMMMUIdx_E10_0;
7aee3cb9 3428 secure = false;
d3649702
PM
3429 break;
3430 default:
3431 g_assert_not_reached();
3432 }
3433
7aee3cb9 3434 par64 = do_ats_write(env, value, access_type, mmu_idx, secure);
01c097f7
FA
3435
3436 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3437#else
3438 /* Handled by hardware accelerator. */
3439 g_assert_not_reached();
3440#endif /* CONFIG_TCG */
4a501606 3441}
060e8a48 3442
14db7fe0
PM
3443static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3444 uint64_t value)
3445{
9fb005b0 3446#ifdef CONFIG_TCG
03ae85f8 3447 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
3448 uint64_t par64;
3449
7aee3cb9
RH
3450 /* There is no SecureEL2 for AArch32. */
3451 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2, false);
14db7fe0
PM
3452
3453 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3454#else
3455 /* Handled by hardware accelerator. */
3456 g_assert_not_reached();
3457#endif /* CONFIG_TCG */
14db7fe0
PM
3458}
3459
3f208fd7
PM
3460static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3461 bool isread)
2a47df95 3462{
926c1b97
RDC
3463 if (arm_current_el(env) == 3 &&
3464 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
2a47df95
PM
3465 return CP_ACCESS_TRAP;
3466 }
3467 return CP_ACCESS_OK;
3468}
3469
060e8a48
PM
3470static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3471 uint64_t value)
3472{
9fb005b0 3473#ifdef CONFIG_TCG
03ae85f8 3474 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
3475 ARMMMUIdx mmu_idx;
3476 int secure = arm_is_secure_below_el3(env);
3477
3478 switch (ri->opc2 & 6) {
3479 case 0:
3480 switch (ri->opc1) {
04b07d29
RH
3481 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3482 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
d902ae75 3483 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
04b07d29 3484 } else {
d902ae75 3485 mmu_idx = ARMMMUIdx_Stage1_E1;
04b07d29 3486 }
d3649702
PM
3487 break;
3488 case 4: /* AT S1E2R, AT S1E2W */
d902ae75 3489 mmu_idx = ARMMMUIdx_E2;
d3649702
PM
3490 break;
3491 case 6: /* AT S1E3R, AT S1E3W */
d902ae75 3492 mmu_idx = ARMMMUIdx_E3;
7aee3cb9 3493 secure = true;
d3649702
PM
3494 break;
3495 default:
3496 g_assert_not_reached();
3497 }
3498 break;
3499 case 2: /* AT S1E0R, AT S1E0W */
d902ae75 3500 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3501 break;
3502 case 4: /* AT S12E1R, AT S12E1W */
d902ae75 3503 mmu_idx = ARMMMUIdx_E10_1;
d3649702
PM
3504 break;
3505 case 6: /* AT S12E0R, AT S12E0W */
d902ae75 3506 mmu_idx = ARMMMUIdx_E10_0;
d3649702
PM
3507 break;
3508 default:
3509 g_assert_not_reached();
3510 }
060e8a48 3511
7aee3cb9
RH
3512 env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3513 mmu_idx, secure);
9fb005b0
PMD
3514#else
3515 /* Handled by hardware accelerator. */
3516 g_assert_not_reached();
3517#endif /* CONFIG_TCG */
060e8a48 3518}
4a501606
PM
3519#endif
3520
3521static const ARMCPRegInfo vapa_cp_reginfo[] = {
3522 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3523 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
3524 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3525 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
3526 .writefn = par_write },
3527#ifndef CONFIG_USER_ONLY
87562e4f 3528 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 3529 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 3530 .access = PL1_W, .accessfn = ats_access,
0710b2fa 3531 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
4a501606 3532#endif
4a501606
PM
3533};
3534
18032bec
PM
3535/* Return basic MPU access permission bits. */
3536static uint32_t simple_mpu_ap_bits(uint32_t val)
3537{
3538 uint32_t ret;
3539 uint32_t mask;
3540 int i;
3541 ret = 0;
3542 mask = 3;
3543 for (i = 0; i < 16; i += 2) {
3544 ret |= (val >> i) & mask;
3545 mask <<= 2;
3546 }
3547 return ret;
3548}
3549
3550/* Pad basic MPU access permission bits to extended format. */
3551static uint32_t extended_mpu_ap_bits(uint32_t val)
3552{
3553 uint32_t ret;
3554 uint32_t mask;
3555 int i;
3556 ret = 0;
3557 mask = 3;
3558 for (i = 0; i < 16; i += 2) {
3559 ret |= (val & mask) << i;
3560 mask <<= 2;
3561 }
3562 return ret;
3563}
3564
c4241c7d
PM
3565static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3566 uint64_t value)
18032bec 3567{
7e09797c 3568 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
3569}
3570
c4241c7d 3571static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3572{
7e09797c 3573 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
3574}
3575
c4241c7d
PM
3576static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3577 uint64_t value)
18032bec 3578{
7e09797c 3579 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
3580}
3581
c4241c7d 3582static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3583{
7e09797c 3584 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
3585}
3586
6cb0b013
PC
3587static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3588{
3589 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3590
3591 if (!u32p) {
3592 return 0;
3593 }
3594
1bc04a88 3595 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
3596 return *u32p;
3597}
3598
3599static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3600 uint64_t value)
3601{
2fc0cc0e 3602 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3603 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3604
3605 if (!u32p) {
3606 return;
3607 }
3608
1bc04a88 3609 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 3610 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
3611 *u32p = value;
3612}
3613
6cb0b013
PC
3614static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3615 uint64_t value)
3616{
2fc0cc0e 3617 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3618 uint32_t nrgs = cpu->pmsav7_dregion;
3619
3620 if (value >= nrgs) {
3621 qemu_log_mask(LOG_GUEST_ERROR,
3622 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3623 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3624 return;
3625 }
3626
3627 raw_write(env, ri, value);
3628}
3629
3630static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
69ceea64
PM
3631 /* Reset for all these registers is handled in arm_cpu_reset(),
3632 * because the PMSAv7 is also used by M-profile CPUs, which do
3633 * not register cpregs but still need the state to be reset.
3634 */
6cb0b013
PC
3635 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3636 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3637 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
3638 .readfn = pmsav7_read, .writefn = pmsav7_write,
3639 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3640 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3641 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3642 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
3643 .readfn = pmsav7_read, .writefn = pmsav7_write,
3644 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3645 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
3646 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3647 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
3648 .readfn = pmsav7_read, .writefn = pmsav7_write,
3649 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3650 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
3651 .access = PL1_RW,
1bc04a88 3652 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
3653 .writefn = pmsav7_rgnr_write,
3654 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3655};
3656
18032bec
PM
3657static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
3658 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3659 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3660 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
3661 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
3662 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 3663 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 3664 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
3665 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
3666 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
3667 .access = PL1_RW,
7e09797c
PM
3668 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
3669 .resetvalue = 0, },
18032bec
PM
3670 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
3671 .access = PL1_RW,
7e09797c
PM
3672 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
3673 .resetvalue = 0, },
ecce5c3c
PM
3674 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
3675 .access = PL1_RW,
3676 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
3677 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
3678 .access = PL1_RW,
3679 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 3680 /* Protection region base and size registers */
e508a92b
PM
3681 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
3682 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3683 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
3684 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
3685 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3686 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
3687 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
3688 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3689 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
3690 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
3691 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3692 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
3693 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
3694 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3695 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
3696 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
3697 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3698 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
3699 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
3700 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3701 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
3702 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
3703 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
3704 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
3705};
3706
cb4a0a34
PM
3707static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3708 uint64_t value)
ecce5c3c 3709{
cb4a0a34 3710 ARMCPU *cpu = env_archcpu(env);
2ebcebe2 3711
e389be16
FA
3712 if (!arm_feature(env, ARM_FEATURE_V8)) {
3713 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
cb4a0a34
PM
3714 /*
3715 * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
3716 * using Long-descriptor translation table format
3717 */
e389be16
FA
3718 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
3719 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
cb4a0a34
PM
3720 /*
3721 * In an implementation that includes the Security Extensions
e389be16
FA
3722 * TTBCR has additional fields PD0 [4] and PD1 [5] for
3723 * Short-descriptor translation table format.
3724 */
3725 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
3726 } else {
3727 value &= TTBCR_N;
3728 }
e42c4db3 3729 }
e389be16 3730
d4e6df63
PM
3731 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3732 /* With LPAE the TTBCR could result in a change of ASID
3733 * via the TTBCR.A1 bit, so do a TLB flush.
3734 */
d10eb08f 3735 tlb_flush(CPU(cpu));
d4e6df63 3736 }
cb4a0a34 3737 raw_write(env, ri, value);
ecce5c3c
PM
3738}
3739
d06dc933 3740static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
cb2e37df
PM
3741 uint64_t value)
3742{
2fc0cc0e 3743 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 3744
cb2e37df 3745 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 3746 tlb_flush(CPU(cpu));
cb4a0a34 3747 raw_write(env, ri, value);
cb2e37df
PM
3748}
3749
327ed10f
PM
3750static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3751 uint64_t value)
3752{
93f379b0
RH
3753 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
3754 if (cpreg_field_is_64bit(ri) &&
3755 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
2fc0cc0e 3756 ARMCPU *cpu = env_archcpu(env);
d10eb08f 3757 tlb_flush(CPU(cpu));
327ed10f
PM
3758 }
3759 raw_write(env, ri, value);
3760}
3761
ed30da8e
RH
3762static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
3763 uint64_t value)
3764{
d06dc933
RH
3765 /*
3766 * If we are running with E2&0 regime, then an ASID is active.
3767 * Flush if that might be changing. Note we're not checking
3768 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
3769 * holds the active ASID, only checking the field that might.
3770 */
3771 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
3772 (arm_hcr_el2_eff(env) & HCR_E2H)) {
b6ad6062
RDC
3773 uint16_t mask = ARMMMUIdxBit_E20_2 |
3774 ARMMMUIdxBit_E20_2_PAN |
3775 ARMMMUIdxBit_E20_0;
b6ad6062 3776 tlb_flush_by_mmuidx(env_cpu(env), mask);
d06dc933 3777 }
ed30da8e
RH
3778 raw_write(env, ri, value);
3779}
3780
b698e9cf
EI
3781static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3782 uint64_t value)
3783{
2fc0cc0e 3784 ARMCPU *cpu = env_archcpu(env);
b698e9cf
EI
3785 CPUState *cs = CPU(cpu);
3786
97fa9350
RH
3787 /*
3788 * A change in VMID to the stage2 page table (Stage2) invalidates
3789 * the combined stage 1&2 tlbs (EL10_1 and EL10_0).
3790 */
b698e9cf 3791 if (raw_read(env, ri) != value) {
c4f060e8
RDC
3792 uint16_t mask = ARMMMUIdxBit_E10_1 |
3793 ARMMMUIdxBit_E10_1_PAN |
3794 ARMMMUIdxBit_E10_0;
c4f060e8 3795 tlb_flush_by_mmuidx(cs, mask);
b698e9cf
EI
3796 raw_write(env, ri, value);
3797 }
3798}
3799
8e5d75c9 3800static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 3801 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218 3802 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4a7e2d73 3803 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 3804 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 3805 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
84929218 3806 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
88ca1c2d
FA
3807 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
3808 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9 3809 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
84929218 3810 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
8e5d75c9
PC
3811 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
3812 offsetof(CPUARMState, cp15.dfar_ns) } },
3813 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
3814 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218
RH
3815 .access = PL1_RW, .accessfn = access_tvm_trvm,
3816 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
8e5d75c9 3817 .resetvalue = 0, },
8e5d75c9
PC
3818};
3819
3820static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
3821 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
3822 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
84929218 3823 .access = PL1_RW, .accessfn = access_tvm_trvm,
d81c519c 3824 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 3825 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 3826 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
84929218
RH
3827 .access = PL1_RW, .accessfn = access_tvm_trvm,
3828 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
3829 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
3830 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 3831 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 3832 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
84929218
RH
3833 .access = PL1_RW, .accessfn = access_tvm_trvm,
3834 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
3835 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
3836 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
3837 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
3838 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
3839 .access = PL1_RW, .accessfn = access_tvm_trvm,
3840 .writefn = vmsa_tcr_el12_write,
cb4a0a34
PM
3841 .raw_writefn = raw_write,
3842 .resetvalue = 0,
11f136ee 3843 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 3844 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
3845 .access = PL1_RW, .accessfn = access_tvm_trvm,
3846 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
cb4a0a34
PM
3847 .raw_writefn = raw_write,
3848 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
3849 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
3850};
3851
ab638a32
RH
3852/* Note that unlike TTBCR, writing to TTBCR2 does not require flushing
3853 * qemu tlbs nor adjusting cached masks.
3854 */
3855static const ARMCPRegInfo ttbcr2_reginfo = {
3856 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
84929218
RH
3857 .access = PL1_RW, .accessfn = access_tvm_trvm,
3858 .type = ARM_CP_ALIAS,
d102058e 3859 .bank_fieldoffsets = {
cb4a0a34
PM
3860 offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
3861 offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
d102058e 3862 },
ab638a32
RH
3863};
3864
c4241c7d
PM
3865static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
3866 uint64_t value)
1047b9d7
PM
3867{
3868 env->cp15.c15_ticonfig = value & 0xe7;
3869 /* The OS_TYPE bit in this register changes the reported CPUID! */
3870 env->cp15.c0_cpuid = (value & (1 << 5)) ?
3871 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
3872}
3873
c4241c7d
PM
3874static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
3875 uint64_t value)
1047b9d7
PM
3876{
3877 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
3878}
3879
c4241c7d
PM
3880static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
3881 uint64_t value)
1047b9d7
PM
3882{
3883 /* Wait-for-interrupt (deprecated) */
2fc0cc0e 3884 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
1047b9d7
PM
3885}
3886
c4241c7d
PM
3887static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
3888 uint64_t value)
c4804214
PM
3889{
3890 /* On OMAP there are registers indicating the max/min index of dcache lines
3891 * containing a dirty line; cache flush operations have to reset these.
3892 */
3893 env->cp15.c15_i_max = 0x000;
3894 env->cp15.c15_i_min = 0xff0;
c4804214
PM
3895}
3896
18032bec
PM
3897static const ARMCPRegInfo omap_cp_reginfo[] = {
3898 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
3899 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 3900 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 3901 .resetvalue = 0, },
1047b9d7
PM
3902 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
3903 .access = PL1_RW, .type = ARM_CP_NOP },
3904 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
3905 .access = PL1_RW,
3906 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
3907 .writefn = omap_ticonfig_write },
3908 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
3909 .access = PL1_RW,
3910 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
3911 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
3912 .access = PL1_RW, .resetvalue = 0xff0,
3913 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
3914 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
3915 .access = PL1_RW,
3916 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
3917 .writefn = omap_threadid_write },
3918 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
3919 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 3920 .type = ARM_CP_NO_RAW,
1047b9d7
PM
3921 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
3922 /* TODO: Peripheral port remap register:
3923 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
3924 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
3925 * when MMU is off.
3926 */
c4804214 3927 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 3928 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 3929 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 3930 .writefn = omap_cachemaint_write },
34f90529
PM
3931 { .name = "C9", .cp = 15, .crn = 9,
3932 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
3933 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
3934};
3935
c4241c7d
PM
3936static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3937 uint64_t value)
1047b9d7 3938{
c0f4af17 3939 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
3940}
3941
3942static const ARMCPRegInfo xscale_cp_reginfo[] = {
3943 { .name = "XSCALE_CPAR",
3944 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
3945 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
3946 .writefn = xscale_cpar_write, },
2771db27
PM
3947 { .name = "XSCALE_AUXCR",
3948 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
3949 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
3950 .resetvalue = 0, },
3b771579
PM
3951 /* XScale specific cache-lockdown: since we have no cache we NOP these
3952 * and hope the guest does not really rely on cache behaviour.
3953 */
3954 { .name = "XSCALE_LOCK_ICACHE_LINE",
3955 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
3956 .access = PL1_W, .type = ARM_CP_NOP },
3957 { .name = "XSCALE_UNLOCK_ICACHE",
3958 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
3959 .access = PL1_W, .type = ARM_CP_NOP },
3960 { .name = "XSCALE_DCACHE_LOCK",
3961 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
3962 .access = PL1_RW, .type = ARM_CP_NOP },
3963 { .name = "XSCALE_UNLOCK_DCACHE",
3964 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
3965 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
3966};
3967
3968static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
3969 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
3970 * implementation of this implementation-defined space.
3971 * Ideally this should eventually disappear in favour of actually
3972 * implementing the correct behaviour for all cores.
3973 */
3974 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
3975 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 3976 .access = PL1_RW,
7a0e58fa 3977 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 3978 .resetvalue = 0 },
18032bec
PM
3979};
3980
c4804214
PM
3981static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
3982 /* Cache status: RAZ because we have no cache so it's always clean */
3983 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 3984 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 3985 .resetvalue = 0 },
c4804214
PM
3986};
3987
3988static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
a07d9df0 3989 /* We never have a block transfer operation in progress */
c4804214 3990 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 3991 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 3992 .resetvalue = 0 },
30b05bba
PM
3993 /* The cache ops themselves: these all NOP for QEMU */
3994 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
3995 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3996 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
3997 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
3998 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
3999 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4000 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4001 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4002 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4003 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
4004 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4005 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
c4804214
PM
4006};
4007
4008static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4009 /* The cache test-and-clean instructions always return (1 << 30)
4010 * to indicate that there are no dirty cache lines.
4011 */
4012 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 4013 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4014 .resetvalue = (1 << 30) },
c4804214 4015 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 4016 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4017 .resetvalue = (1 << 30) },
c4804214
PM
4018};
4019
34f90529
PM
4020static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4021 /* Ignore ReadBuffer accesses */
4022 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4023 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 4024 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 4025 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
4026};
4027
731de9e6
EI
4028static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4029{
731de9e6 4030 unsigned int cur_el = arm_current_el(env);
731de9e6 4031
e6ef0169 4032 if (arm_is_el2_enabled(env) && cur_el == 1) {
731de9e6
EI
4033 return env->cp15.vpidr_el2;
4034 }
4035 return raw_read(env, ri);
4036}
4037
06a7e647 4038static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 4039{
2fc0cc0e 4040 ARMCPU *cpu = env_archcpu(env);
eb5e1d3c
PF
4041 uint64_t mpidr = cpu->mp_affinity;
4042
81bdde9d 4043 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 4044 mpidr |= (1U << 31);
81bdde9d
PM
4045 /* Cores which are uniprocessor (non-coherent)
4046 * but still implement the MP extensions set
a8e81b31 4047 * bit 30. (For instance, Cortex-R5).
81bdde9d 4048 */
a8e81b31
PC
4049 if (cpu->mp_is_up) {
4050 mpidr |= (1u << 30);
4051 }
81bdde9d 4052 }
c4241c7d 4053 return mpidr;
81bdde9d
PM
4054}
4055
06a7e647
EI
4056static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4057{
f0d574d6 4058 unsigned int cur_el = arm_current_el(env);
f0d574d6 4059
e6ef0169 4060 if (arm_is_el2_enabled(env) && cur_el == 1) {
f0d574d6
EI
4061 return env->cp15.vmpidr_el2;
4062 }
06a7e647
EI
4063 return mpidr_read_val(env);
4064}
4065
7ac681cf 4066static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 4067 /* NOP AMAIR0/1 */
b0fe2427
PM
4068 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4069 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
84929218
RH
4070 .access = PL1_RW, .accessfn = access_tvm_trvm,
4071 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427 4072 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 4073 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
84929218
RH
4074 .access = PL1_RW, .accessfn = access_tvm_trvm,
4075 .type = ARM_CP_CONST, .resetvalue = 0 },
891a2fe7 4076 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
4077 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4078 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4079 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 4080 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
84929218
RH
4081 .access = PL1_RW, .accessfn = access_tvm_trvm,
4082 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4083 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4084 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 4085 .writefn = vmsa_ttbr_write, },
891a2fe7 4086 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
84929218
RH
4087 .access = PL1_RW, .accessfn = access_tvm_trvm,
4088 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4089 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4090 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 4091 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
4092};
4093
c4241c7d 4094static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4095{
c4241c7d 4096 return vfp_get_fpcr(env);
b0d2b7d0
PM
4097}
4098
c4241c7d
PM
4099static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4100 uint64_t value)
b0d2b7d0
PM
4101{
4102 vfp_set_fpcr(env, value);
b0d2b7d0
PM
4103}
4104
c4241c7d 4105static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4106{
c4241c7d 4107 return vfp_get_fpsr(env);
b0d2b7d0
PM
4108}
4109
c4241c7d
PM
4110static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4111 uint64_t value)
b0d2b7d0
PM
4112{
4113 vfp_set_fpsr(env, value);
b0d2b7d0
PM
4114}
4115
3f208fd7
PM
4116static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4117 bool isread)
c2b820fe 4118{
aaec1432 4119 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
c2b820fe
PM
4120 return CP_ACCESS_TRAP;
4121 }
4122 return CP_ACCESS_OK;
4123}
4124
4125static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4126 uint64_t value)
4127{
4128 env->daif = value & PSTATE_DAIF;
4129}
4130
220f508f
RH
4131static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4132{
4133 return env->pstate & PSTATE_PAN;
4134}
4135
4136static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4137 uint64_t value)
4138{
4139 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4140}
4141
4142static const ARMCPRegInfo pan_reginfo = {
4143 .name = "PAN", .state = ARM_CP_STATE_AA64,
4144 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4145 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4146 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4147};
4148
9eeb7a1c
RH
4149static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4150{
4151 return env->pstate & PSTATE_UAO;
4152}
4153
4154static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4155 uint64_t value)
4156{
4157 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4158}
4159
4160static const ARMCPRegInfo uao_reginfo = {
4161 .name = "UAO", .state = ARM_CP_STATE_AA64,
4162 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4163 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4164 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4165};
4166
dc8b1853
RC
4167static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4168{
4169 return env->pstate & PSTATE_DIT;
4170}
4171
4172static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4173 uint64_t value)
4174{
4175 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4176}
4177
4178static const ARMCPRegInfo dit_reginfo = {
4179 .name = "DIT", .state = ARM_CP_STATE_AA64,
4180 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4181 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4182 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4183};
4184
f2f68a78
RC
4185static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4186{
4187 return env->pstate & PSTATE_SSBS;
4188}
4189
4190static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4191 uint64_t value)
4192{
4193 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4194}
4195
4196static const ARMCPRegInfo ssbs_reginfo = {
4197 .name = "SSBS", .state = ARM_CP_STATE_AA64,
4198 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4199 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4200 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4201};
4202
38262d8a
RH
4203static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4204 const ARMCPRegInfo *ri,
4205 bool isread)
8af35c37 4206{
38262d8a
RH
4207 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4208 switch (arm_current_el(env)) {
4209 case 0:
4210 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4211 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4212 return CP_ACCESS_TRAP;
4213 }
4214 /* fall through */
4215 case 1:
4216 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4217 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4218 return CP_ACCESS_TRAP_EL2;
4219 }
4220 break;
8af35c37
PM
4221 }
4222 return CP_ACCESS_OK;
4223}
4224
38262d8a 4225static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env,
1bed4d2e
RH
4226 const ARMCPRegInfo *ri,
4227 bool isread)
4228{
38262d8a 4229 /* Cache invalidate/clean to Point of Unification... */
1bed4d2e
RH
4230 switch (arm_current_el(env)) {
4231 case 0:
4232 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4233 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4234 return CP_ACCESS_TRAP;
4235 }
4236 /* fall through */
4237 case 1:
38262d8a
RH
4238 /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set. */
4239 if (arm_hcr_el2_eff(env) & HCR_TPU) {
1bed4d2e
RH
4240 return CP_ACCESS_TRAP_EL2;
4241 }
4242 break;
4243 }
4244 return CP_ACCESS_OK;
4245}
4246
dbb1fb27
AB
4247/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4248 * Page D4-1736 (DDI0487A.b)
4249 */
4250
b7e0730d
RH
4251static int vae1_tlbmask(CPUARMState *env)
4252{
e04a5752 4253 uint64_t hcr = arm_hcr_el2_eff(env);
bc944d3a 4254 uint16_t mask;
e04a5752
RDC
4255
4256 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
bc944d3a
RDC
4257 mask = ARMMMUIdxBit_E20_2 |
4258 ARMMMUIdxBit_E20_2_PAN |
4259 ARMMMUIdxBit_E20_0;
b7e0730d 4260 } else {
bc944d3a 4261 mask = ARMMMUIdxBit_E10_1 |
452ef8cb
RH
4262 ARMMMUIdxBit_E10_1_PAN |
4263 ARMMMUIdxBit_E10_0;
b7e0730d 4264 }
bc944d3a 4265 return mask;
b7e0730d
RH
4266}
4267
ea04dce7
RH
4268/* Return 56 if TBI is enabled, 64 otherwise. */
4269static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4270 uint64_t addr)
4271{
c1547bba 4272 uint64_t tcr = regime_tcr(env, mmu_idx);
ea04dce7
RH
4273 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4274 int select = extract64(addr, 55, 1);
4275
4276 return (tbi >> select) & 1 ? 56 : 64;
4277}
4278
4279static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4280{
b6ad6062 4281 uint64_t hcr = arm_hcr_el2_eff(env);
ea04dce7
RH
4282 ARMMMUIdx mmu_idx;
4283
4284 /* Only the regime of the mmu_idx below is significant. */
b6ad6062 4285 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
ea04dce7
RH
4286 mmu_idx = ARMMMUIdx_E20_0;
4287 } else {
4288 mmu_idx = ARMMMUIdx_E10_0;
4289 }
b6ad6062 4290
ea04dce7
RH
4291 return tlbbits_for_regime(env, mmu_idx, addr);
4292}
4293
fd3ed969
PM
4294static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4295 uint64_t value)
168aa23b 4296{
29a0af61 4297 CPUState *cs = env_cpu(env);
b7e0730d 4298 int mask = vae1_tlbmask(env);
dbb1fb27 4299
b7e0730d 4300 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
168aa23b
PM
4301}
4302
b4ab8ce9
PM
4303static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4304 uint64_t value)
4305{
29a0af61 4306 CPUState *cs = env_cpu(env);
b7e0730d 4307 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4308
4309 if (tlb_force_broadcast(env)) {
527db2be
RH
4310 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4311 } else {
4312 tlb_flush_by_mmuidx(cs, mask);
b4ab8ce9 4313 }
b4ab8ce9
PM
4314}
4315
90c19cdf 4316static int alle1_tlbmask(CPUARMState *env)
168aa23b 4317{
90c19cdf
RH
4318 /*
4319 * Note that the 'ALL' scope must invalidate both stage 1 and
fd3ed969
PM
4320 * stage 2 translations, whereas most other scopes only invalidate
4321 * stage 1 translations.
4322 */
d902ae75
RH
4323 return (ARMMMUIdxBit_E10_1 |
4324 ARMMMUIdxBit_E10_1_PAN |
4325 ARMMMUIdxBit_E10_0);
168aa23b
PM
4326}
4327
85d0dc9f
RH
4328static int e2_tlbmask(CPUARMState *env)
4329{
d902ae75
RH
4330 return (ARMMMUIdxBit_E20_0 |
4331 ARMMMUIdxBit_E20_2 |
4332 ARMMMUIdxBit_E20_2_PAN |
4333 ARMMMUIdxBit_E2);
85d0dc9f
RH
4334}
4335
90c19cdf
RH
4336static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4337 uint64_t value)
4338{
4339 CPUState *cs = env_cpu(env);
4340 int mask = alle1_tlbmask(env);
4341
4342 tlb_flush_by_mmuidx(cs, mask);
4343}
4344
fd3ed969 4345static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
4346 uint64_t value)
4347{
85d0dc9f
RH
4348 CPUState *cs = env_cpu(env);
4349 int mask = e2_tlbmask(env);
fd3ed969 4350
85d0dc9f 4351 tlb_flush_by_mmuidx(cs, mask);
fd3ed969
PM
4352}
4353
43efaa33
PM
4354static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4355 uint64_t value)
4356{
2fc0cc0e 4357 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4358 CPUState *cs = CPU(cpu);
4359
d902ae75 4360 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
43efaa33
PM
4361}
4362
fd3ed969
PM
4363static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4364 uint64_t value)
4365{
29a0af61 4366 CPUState *cs = env_cpu(env);
90c19cdf
RH
4367 int mask = alle1_tlbmask(env);
4368
4369 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
fa439fc5
PM
4370}
4371
2bfb9d75
PM
4372static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4373 uint64_t value)
4374{
29a0af61 4375 CPUState *cs = env_cpu(env);
85d0dc9f 4376 int mask = e2_tlbmask(env);
2bfb9d75 4377
85d0dc9f 4378 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
2bfb9d75
PM
4379}
4380
43efaa33
PM
4381static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4382 uint64_t value)
4383{
29a0af61 4384 CPUState *cs = env_cpu(env);
43efaa33 4385
d902ae75 4386 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
43efaa33
PM
4387}
4388
fd3ed969
PM
4389static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4390 uint64_t value)
fa439fc5 4391{
fd3ed969
PM
4392 /* Invalidate by VA, EL2
4393 * Currently handles both VAE2 and VALE2, since we don't support
4394 * flush-last-level-only.
4395 */
85d0dc9f
RH
4396 CPUState *cs = env_cpu(env);
4397 int mask = e2_tlbmask(env);
fd3ed969
PM
4398 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4399
85d0dc9f 4400 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
fd3ed969
PM
4401}
4402
43efaa33
PM
4403static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4404 uint64_t value)
4405{
4406 /* Invalidate by VA, EL3
4407 * Currently handles both VAE3 and VALE3, since we don't support
4408 * flush-last-level-only.
4409 */
2fc0cc0e 4410 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4411 CPUState *cs = CPU(cpu);
4412 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4413
d902ae75 4414 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
43efaa33
PM
4415}
4416
fd3ed969
PM
4417static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4418 uint64_t value)
4419{
90c19cdf
RH
4420 CPUState *cs = env_cpu(env);
4421 int mask = vae1_tlbmask(env);
fa439fc5 4422 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4423 int bits = vae1_tlbbits(env, pageaddr);
fa439fc5 4424
ea04dce7 4425 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
fa439fc5
PM
4426}
4427
b4ab8ce9
PM
4428static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4429 uint64_t value)
4430{
4431 /* Invalidate by VA, EL1&0 (AArch64 version).
4432 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4433 * since we don't support flush-for-specific-ASID-only or
4434 * flush-last-level-only.
4435 */
90c19cdf
RH
4436 CPUState *cs = env_cpu(env);
4437 int mask = vae1_tlbmask(env);
b4ab8ce9 4438 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4439 int bits = vae1_tlbbits(env, pageaddr);
b4ab8ce9
PM
4440
4441 if (tlb_force_broadcast(env)) {
ea04dce7 4442 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
527db2be 4443 } else {
ea04dce7 4444 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
b4ab8ce9 4445 }
b4ab8ce9
PM
4446}
4447
fd3ed969
PM
4448static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4449 uint64_t value)
fa439fc5 4450{
29a0af61 4451 CPUState *cs = env_cpu(env);
fd3ed969 4452 uint64_t pageaddr = sextract64(value << 12, 0, 56);
d902ae75 4453 int bits = tlbbits_for_regime(env, ARMMMUIdx_E2, pageaddr);
fa439fc5 4454
d902ae75
RH
4455 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4456 ARMMMUIdxBit_E2, bits);
fa439fc5
PM
4457}
4458
43efaa33
PM
4459static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4460 uint64_t value)
4461{
29a0af61 4462 CPUState *cs = env_cpu(env);
43efaa33 4463 uint64_t pageaddr = sextract64(value << 12, 0, 56);
d902ae75 4464 int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
43efaa33 4465
ea04dce7 4466 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
d902ae75 4467 ARMMMUIdxBit_E3, bits);
43efaa33
PM
4468}
4469
84940ed8 4470#ifdef TARGET_AARCH64
ab1cdb47
RH
4471typedef struct {
4472 uint64_t base;
84940ed8 4473 uint64_t length;
ab1cdb47
RH
4474} TLBIRange;
4475
4476static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4477 uint64_t value)
4478{
4479 unsigned int page_size_granule, page_shift, num, scale, exponent;
3974ff93
RH
4480 /* Extract one bit to represent the va selector in use. */
4481 uint64_t select = sextract64(value, 36, 1);
4482 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
ab1cdb47 4483 TLBIRange ret = { };
84940ed8 4484
84940ed8
RC
4485 page_size_granule = extract64(value, 46, 2);
4486
3974ff93
RH
4487 /* The granule encoded in value must match the granule in use. */
4488 if (page_size_granule != (param.using64k ? 3 : param.using16k ? 2 : 1)) {
4489 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
84940ed8 4490 page_size_granule);
ab1cdb47 4491 return ret;
84940ed8
RC
4492 }
4493
52a9f609 4494 page_shift = (page_size_granule - 1) * 2 + 12;
ab1cdb47
RH
4495 num = extract64(value, 39, 5);
4496 scale = extract64(value, 44, 2);
84940ed8 4497 exponent = (5 * scale) + 1;
84940ed8 4498
ab1cdb47 4499 ret.length = (num + 1) << (exponent + page_shift);
84940ed8 4500
3974ff93 4501 if (param.select) {
d976de21 4502 ret.base = sextract64(value, 0, 37);
84940ed8 4503 } else {
d976de21 4504 ret.base = extract64(value, 0, 37);
84940ed8 4505 }
ef56c242
RH
4506 if (param.ds) {
4507 /*
4508 * With DS=1, BaseADDR is always shifted 16 so that it is able
4509 * to address all 52 va bits. The input address is perforce
4510 * aligned on a 64k boundary regardless of translation granule.
4511 */
4512 page_shift = 16;
4513 }
d976de21 4514 ret.base <<= page_shift;
84940ed8 4515
ab1cdb47 4516 return ret;
84940ed8
RC
4517}
4518
4519static void do_rvae_write(CPUARMState *env, uint64_t value,
4520 int idxmap, bool synced)
4521{
4522 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
ab1cdb47 4523 TLBIRange range;
84940ed8
RC
4524 int bits;
4525
ab1cdb47
RH
4526 range = tlbi_aa64_get_range(env, one_idx, value);
4527 bits = tlbbits_for_regime(env, one_idx, range.base);
84940ed8
RC
4528
4529 if (synced) {
4530 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
ab1cdb47
RH
4531 range.base,
4532 range.length,
84940ed8
RC
4533 idxmap,
4534 bits);
4535 } else {
ab1cdb47
RH
4536 tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4537 range.length, idxmap, bits);
84940ed8
RC
4538 }
4539}
4540
4541static void tlbi_aa64_rvae1_write(CPUARMState *env,
4542 const ARMCPRegInfo *ri,
4543 uint64_t value)
4544{
4545 /*
4546 * Invalidate by VA range, EL1&0.
4547 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4548 * since we don't support flush-for-specific-ASID-only or
4549 * flush-last-level-only.
4550 */
4551
4552 do_rvae_write(env, value, vae1_tlbmask(env),
4553 tlb_force_broadcast(env));
4554}
4555
4556static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4557 const ARMCPRegInfo *ri,
4558 uint64_t value)
4559{
4560 /*
4561 * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4562 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4563 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4564 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4565 * shareable specific flushes.
4566 */
4567
4568 do_rvae_write(env, value, vae1_tlbmask(env), true);
4569}
4570
4571static int vae2_tlbmask(CPUARMState *env)
4572{
d902ae75 4573 return ARMMMUIdxBit_E2;
84940ed8
RC
4574}
4575
4576static void tlbi_aa64_rvae2_write(CPUARMState *env,
4577 const ARMCPRegInfo *ri,
4578 uint64_t value)
4579{
4580 /*
4581 * Invalidate by VA range, EL2.
4582 * Currently handles all of RVAE2 and RVALE2,
4583 * since we don't support flush-for-specific-ASID-only or
4584 * flush-last-level-only.
4585 */
4586
4587 do_rvae_write(env, value, vae2_tlbmask(env),
4588 tlb_force_broadcast(env));
4589
4590
4591}
4592
4593static void tlbi_aa64_rvae2is_write(CPUARMState *env,
4594 const ARMCPRegInfo *ri,
4595 uint64_t value)
4596{
4597 /*
4598 * Invalidate by VA range, Inner/Outer Shareable, EL2.
4599 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
4600 * since we don't support flush-for-specific-ASID-only,
4601 * flush-last-level-only or inner/outer shareable specific flushes.
4602 */
4603
4604 do_rvae_write(env, value, vae2_tlbmask(env), true);
4605
4606}
4607
4608static void tlbi_aa64_rvae3_write(CPUARMState *env,
4609 const ARMCPRegInfo *ri,
4610 uint64_t value)
4611{
4612 /*
4613 * Invalidate by VA range, EL3.
4614 * Currently handles all of RVAE3 and RVALE3,
4615 * since we don't support flush-for-specific-ASID-only or
4616 * flush-last-level-only.
4617 */
4618
d902ae75 4619 do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
84940ed8
RC
4620}
4621
4622static void tlbi_aa64_rvae3is_write(CPUARMState *env,
4623 const ARMCPRegInfo *ri,
4624 uint64_t value)
4625{
4626 /*
4627 * Invalidate by VA range, EL3, Inner/Outer Shareable.
4628 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
4629 * since we don't support flush-for-specific-ASID-only,
4630 * flush-last-level-only or inner/outer specific flushes.
4631 */
4632
d902ae75 4633 do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
84940ed8
RC
4634}
4635#endif
4636
3f208fd7
PM
4637static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4638 bool isread)
aca3f40b 4639{
4351cb72
RH
4640 int cur_el = arm_current_el(env);
4641
4642 if (cur_el < 2) {
4643 uint64_t hcr = arm_hcr_el2_eff(env);
4644
4645 if (cur_el == 0) {
4646 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4647 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4648 return CP_ACCESS_TRAP_EL2;
4649 }
4650 } else {
4651 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4652 return CP_ACCESS_TRAP;
4653 }
4654 if (hcr & HCR_TDZ) {
4655 return CP_ACCESS_TRAP_EL2;
4656 }
4657 }
4658 } else if (hcr & HCR_TDZ) {
4659 return CP_ACCESS_TRAP_EL2;
4660 }
aca3f40b
PM
4661 }
4662 return CP_ACCESS_OK;
4663}
4664
4665static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4666{
2fc0cc0e 4667 ARMCPU *cpu = env_archcpu(env);
aca3f40b
PM
4668 int dzp_bit = 1 << 4;
4669
4670 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 4671 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
4672 dzp_bit = 0;
4673 }
4674 return cpu->dcz_blocksize | dzp_bit;
4675}
4676
3f208fd7
PM
4677static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4678 bool isread)
f502cfc2 4679{
cdcf1405 4680 if (!(env->pstate & PSTATE_SP)) {
f502cfc2
PM
4681 /* Access to SP_EL0 is undefined if it's being used as
4682 * the stack pointer.
4683 */
4684 return CP_ACCESS_TRAP_UNCATEGORIZED;
4685 }
4686 return CP_ACCESS_OK;
4687}
4688
4689static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4690{
4691 return env->pstate & PSTATE_SP;
4692}
4693
4694static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4695{
4696 update_spsel(env, val);
4697}
4698
137feaa9
FA
4699static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4700 uint64_t value)
4701{
2fc0cc0e 4702 ARMCPU *cpu = env_archcpu(env);
137feaa9 4703
f00faf13
RH
4704 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4705 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4706 value &= ~SCTLR_M;
4707 }
4708
4709 /* ??? Lots of these bits are not implemented. */
4710
4711 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4712 if (ri->opc1 == 6) { /* SCTLR_EL3 */
4713 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4714 } else {
4715 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4716 SCTLR_ATA0 | SCTLR_ATA);
4717 }
4718 }
4719
137feaa9
FA
4720 if (raw_read(env, ri) == value) {
4721 /* Skip the TLB flush if nothing actually changed; Linux likes
4722 * to do a lot of pointless SCTLR writes.
4723 */
4724 return;
4725 }
4726
4727 raw_write(env, ri, value);
f00faf13 4728
137feaa9 4729 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 4730 tlb_flush(CPU(cpu));
2e5dcf36
RH
4731
4732 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
4733 /*
4734 * Normally we would always end the TB on an SCTLR write; see the
4735 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4736 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4737 * of hflags from the translator, so do it here.
4738 */
4739 arm_rebuild_hflags(env);
4740 }
137feaa9
FA
4741}
4742
80d2b43b
PM
4743static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4744 uint64_t value)
a8d64e73 4745{
01765386
PM
4746 /*
4747 * Some MDCR_EL3 bits affect whether PMU counters are running:
4748 * if we are trying to change any of those then we must
4749 * bracket this update with PMU start/finish calls.
4750 */
4751 bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
4752
4753 if (pmu_op) {
4754 pmu_op_start(env);
4755 }
80d2b43b 4756 env->cp15.mdcr_el3 = value;
01765386
PM
4757 if (pmu_op) {
4758 pmu_op_finish(env);
4759 }
4760}
4761
80d2b43b
PM
4762static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4763 uint64_t value)
4764{
4765 /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
4766 mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
4767}
4768
01765386
PM
4769static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4770 uint64_t value)
4771{
4772 /*
4773 * Some MDCR_EL2 bits affect whether PMU counters are running:
4774 * if we are trying to change any of those then we must
4775 * bracket this update with PMU start/finish calls.
4776 */
4777 bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
4778
4779 if (pmu_op) {
4780 pmu_op_start(env);
4781 }
4782 env->cp15.mdcr_el2 = value;
4783 if (pmu_op) {
4784 pmu_op_finish(env);
4785 }
a8d64e73
PM
4786}
4787
b0d2b7d0
PM
4788static const ARMCPRegInfo v8_cp_reginfo[] = {
4789 /* Minimal set of EL0-visible registers. This will need to be expanded
4790 * significantly for system emulation of AArch64 CPUs.
4791 */
4792 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
4793 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
4794 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
4795 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
4796 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 4797 .type = ARM_CP_NO_RAW,
c2b820fe
PM
4798 .access = PL0_RW, .accessfn = aa64_daif_access,
4799 .fieldoffset = offsetof(CPUARMState, daif),
4800 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
4801 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
4802 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 4803 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4804 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
4805 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
4806 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 4807 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 4808 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
4809 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
4810 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 4811 .access = PL0_R, .type = ARM_CP_NO_RAW,
aca3f40b
PM
4812 .readfn = aa64_dczid_read },
4813 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
4814 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
4815 .access = PL0_W, .type = ARM_CP_DC_ZVA,
4816#ifndef CONFIG_USER_ONLY
4817 /* Avoid overhead of an access check that always passes in user-mode */
4818 .accessfn = aa64_zva_access,
4819#endif
4820 },
0eef9d98
PM
4821 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
4822 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
4823 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
4824 /* Cache ops: all NOPs since we don't emulate caches */
4825 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
4826 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a
RH
4827 .access = PL1_W, .type = ARM_CP_NOP,
4828 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4829 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
4830 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a
RH
4831 .access = PL1_W, .type = ARM_CP_NOP,
4832 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4833 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
4834 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
4835 .access = PL0_W, .type = ARM_CP_NOP,
38262d8a 4836 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4837 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
4838 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e
RH
4839 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
4840 .type = ARM_CP_NOP },
8af35c37
PM
4841 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
4842 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 4843 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
4844 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
4845 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
4846 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 4847 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
4848 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
4849 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 4850 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
4851 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
4852 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
4853 .access = PL0_W, .type = ARM_CP_NOP,
38262d8a 4854 .accessfn = aa64_cacheop_pou_access },
8af35c37
PM
4855 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
4856 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
4857 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 4858 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
4859 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
4860 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 4861 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
168aa23b
PM
4862 /* TLBI operations */
4863 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4864 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
30881b73 4865 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4866 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4867 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4868 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
30881b73 4869 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4870 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4871 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4872 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
30881b73 4873 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4874 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 4875 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4876 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
30881b73 4877 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4878 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4879 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4880 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
30881b73 4881 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4882 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4883 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 4884 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
30881b73 4885 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4886 .writefn = tlbi_aa64_vae1is_write },
168aa23b 4887 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4888 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73 4889 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4890 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4891 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4892 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73 4893 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4894 .writefn = tlbi_aa64_vae1_write },
168aa23b 4895 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4896 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73 4897 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4898 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 4899 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4900 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73 4901 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4902 .writefn = tlbi_aa64_vae1_write },
168aa23b 4903 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4904 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73 4905 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4906 .writefn = tlbi_aa64_vae1_write },
168aa23b 4907 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 4908 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73 4909 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 4910 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
4911 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
4912 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
bf05340c 4913 .access = PL2_W, .type = ARM_CP_NOP },
cea66e91
PM
4914 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
4915 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
bf05340c 4916 .access = PL2_W, .type = ARM_CP_NOP },
83ddf975
PM
4917 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
4918 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
4919 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4920 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
4921 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
4922 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
4923 .access = PL2_W, .type = ARM_CP_NO_RAW,
4924 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
4925 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
4926 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
bf05340c 4927 .access = PL2_W, .type = ARM_CP_NOP },
cea66e91
PM
4928 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
4929 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
bf05340c 4930 .access = PL2_W, .type = ARM_CP_NOP },
83ddf975
PM
4931 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
4932 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
4933 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 4934 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
4935 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
4936 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
4937 .access = PL2_W, .type = ARM_CP_NO_RAW,
4938 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
4939#ifndef CONFIG_USER_ONLY
4940 /* 64 bit address translation operations */
4941 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
4942 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4943 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4944 .writefn = ats_write64 },
19525524
PM
4945 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
4946 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4947 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4948 .writefn = ats_write64 },
19525524
PM
4949 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
4950 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
0710b2fa
PM
4951 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4952 .writefn = ats_write64 },
19525524
PM
4953 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
4954 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
0710b2fa
PM
4955 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4956 .writefn = ats_write64 },
2a47df95 4957 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 4958 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
0710b2fa
PM
4959 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4960 .writefn = ats_write64 },
2a47df95 4961 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 4962 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
0710b2fa
PM
4963 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4964 .writefn = ats_write64 },
2a47df95 4965 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 4966 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
0710b2fa
PM
4967 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4968 .writefn = ats_write64 },
2a47df95 4969 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 4970 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
0710b2fa
PM
4971 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4972 .writefn = ats_write64 },
2a47df95
PM
4973 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
4974 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
4975 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
4976 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4977 .writefn = ats_write64 },
2a47df95
PM
4978 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
4979 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
4980 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
4981 .writefn = ats_write64 },
c96fc9b5
EI
4982 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
4983 .type = ARM_CP_ALIAS,
4984 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
4985 .access = PL1_RW, .resetvalue = 0,
4986 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
4987 .writefn = par_write },
19525524 4988#endif
995939a6 4989 /* TLB invalidate last level of translation table walk */
9449fdf6 4990 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
30881b73
RH
4991 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4992 .writefn = tlbimva_is_write },
9449fdf6 4993 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
30881b73 4994 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
fa439fc5 4995 .writefn = tlbimvaa_is_write },
9449fdf6 4996 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73
RH
4997 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
4998 .writefn = tlbimva_write },
9449fdf6 4999 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73
RH
5000 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5001 .writefn = tlbimvaa_write },
541ef8c2
SS
5002 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5003 .type = ARM_CP_NO_RAW, .access = PL2_W,
5004 .writefn = tlbimva_hyp_write },
5005 { .name = "TLBIMVALHIS",
5006 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5007 .type = ARM_CP_NO_RAW, .access = PL2_W,
5008 .writefn = tlbimva_hyp_is_write },
5009 { .name = "TLBIIPAS2",
5010 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
bf05340c 5011 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
5012 { .name = "TLBIIPAS2IS",
5013 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
bf05340c 5014 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
5015 { .name = "TLBIIPAS2L",
5016 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
bf05340c 5017 .type = ARM_CP_NOP, .access = PL2_W },
541ef8c2
SS
5018 { .name = "TLBIIPAS2LIS",
5019 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
bf05340c 5020 .type = ARM_CP_NOP, .access = PL2_W },
9449fdf6
PM
5021 /* 32 bit cache operations */
5022 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a 5023 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6
PM
5024 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5025 .type = ARM_CP_NOP, .access = PL1_W },
5026 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a 5027 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6 5028 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
38262d8a 5029 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6
PM
5030 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5031 .type = ARM_CP_NOP, .access = PL1_W },
5032 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5033 .type = ARM_CP_NOP, .access = PL1_W },
5034 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e 5035 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5036 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 5037 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5038 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
1bed4d2e 5039 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5040 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 5041 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5042 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
38262d8a 5043 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access },
9449fdf6 5044 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
1bed4d2e 5045 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5046 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 5047 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5048 /* MMU Domain access control / MPU write buffer control */
0c17d68c 5049 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
84929218 5050 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
5051 .writefn = dacr_write, .raw_writefn = raw_write,
5052 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5053 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 5054 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5055 .type = ARM_CP_ALIAS,
a0618a19 5056 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
5057 .access = PL1_RW,
5058 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 5059 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5060 .type = ARM_CP_ALIAS,
a65f1de9 5061 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5062 .access = PL1_RW,
5063 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
f502cfc2
PM
5064 /* We rely on the access checks not allowing the guest to write to the
5065 * state field when SPSel indicates that it's being used as the stack
5066 * pointer.
5067 */
5068 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5069 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5070 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 5071 .type = ARM_CP_ALIAS,
f502cfc2 5072 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
5073 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5074 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
beeec926 5075 .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
884b4dee 5076 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
5077 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5078 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 5079 .type = ARM_CP_NO_RAW,
f502cfc2 5080 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
5081 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5082 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
696ba377
RH
5083 .access = PL2_RW,
5084 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
a4c88675 5085 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
6a43e0b6
PM
5086 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5087 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
696ba377 5088 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6a43e0b6
PM
5089 .writefn = dacr_write, .raw_writefn = raw_write,
5090 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5091 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5092 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
696ba377 5093 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6a43e0b6
PM
5094 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5095 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5096 .type = ARM_CP_ALIAS,
5097 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5098 .access = PL2_RW,
5099 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5100 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5101 .type = ARM_CP_ALIAS,
5102 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5103 .access = PL2_RW,
5104 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5105 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5106 .type = ARM_CP_ALIAS,
5107 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5108 .access = PL2_RW,
5109 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5110 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5111 .type = ARM_CP_ALIAS,
5112 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5113 .access = PL2_RW,
5114 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73 5115 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
80d2b43b 5116 .type = ARM_CP_IO,
a8d64e73
PM
5117 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5118 .resetvalue = 0,
80d2b43b
PM
5119 .access = PL3_RW,
5120 .writefn = mdcr_el3_write,
5121 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
7f4fbfb5 5122 { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
a8d64e73
PM
5123 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5124 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5125 .writefn = sdcr_write,
5126 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
5127};
5128
d1fb4da2 5129static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
f149e3e8 5130{
2fc0cc0e 5131 ARMCPU *cpu = env_archcpu(env);
d1fb4da2
RH
5132
5133 if (arm_feature(env, ARM_FEATURE_V8)) {
5134 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5135 } else {
5136 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5137 }
f149e3e8
EI
5138
5139 if (arm_feature(env, ARM_FEATURE_EL3)) {
5140 valid_mask &= ~HCR_HCD;
77077a83
JK
5141 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5142 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5143 * However, if we're using the SMC PSCI conduit then QEMU is
5144 * effectively acting like EL3 firmware and so the guest at
5145 * EL2 should retain the ability to prevent EL1 from being
5146 * able to make SMC calls into the ersatz firmware, so in
5147 * that case HCR.TSC should be read/write.
5148 */
f149e3e8
EI
5149 valid_mask &= ~HCR_TSC;
5150 }
d1fb4da2
RH
5151
5152 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5153 if (cpu_isar_feature(aa64_vh, cpu)) {
5154 valid_mask |= HCR_E2H;
5155 }
da3d8b13
RH
5156 if (cpu_isar_feature(aa64_ras, cpu)) {
5157 valid_mask |= HCR_TERR | HCR_TEA;
5158 }
d1fb4da2
RH
5159 if (cpu_isar_feature(aa64_lor, cpu)) {
5160 valid_mask |= HCR_TLOR;
5161 }
5162 if (cpu_isar_feature(aa64_pauth, cpu)) {
5163 valid_mask |= HCR_API | HCR_APK;
5164 }
8ddb300b
RH
5165 if (cpu_isar_feature(aa64_mte, cpu)) {
5166 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5167 }
7cb1e618
RH
5168 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5169 valid_mask |= HCR_ENSCXT;
5170 }
8c7e17ef
PM
5171 if (cpu_isar_feature(aa64_fwb, cpu)) {
5172 valid_mask |= HCR_FWB;
5173 }
ef682cdb 5174 }
f149e3e8
EI
5175
5176 /* Clear RES0 bits. */
5177 value &= valid_mask;
5178
8ddb300b
RH
5179 /*
5180 * These bits change the MMU setup:
f149e3e8
EI
5181 * HCR_VM enables stage 2 translation
5182 * HCR_PTW forbids certain page-table setups
8ddb300b
RH
5183 * HCR_DC disables stage1 and enables stage2 translation
5184 * HCR_DCT enables tagging on (disabled) stage1 translation
8c7e17ef 5185 * HCR_FWB changes the interpretation of stage2 descriptor bits
f149e3e8 5186 */
8c7e17ef
PM
5187 if ((env->cp15.hcr_el2 ^ value) &
5188 (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
d10eb08f 5189 tlb_flush(CPU(cpu));
f149e3e8 5190 }
ce4afed8 5191 env->cp15.hcr_el2 = value;
89430fc6
PM
5192
5193 /*
5194 * Updates to VI and VF require us to update the status of
5195 * virtual interrupts, which are the logical OR of these bits
5196 * and the state of the input lines from the GIC. (This requires
5197 * that we have the iothread lock, which is done by marking the
5198 * reginfo structs as ARM_CP_IO.)
5199 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5200 * possible for it to be taken immediately, because VIRQ and
5201 * VFIQ are masked unless running at EL0 or EL1, and HCR
5202 * can only be written at EL2.
5203 */
5204 g_assert(qemu_mutex_iothread_locked());
5205 arm_cpu_update_virq(cpu);
5206 arm_cpu_update_vfiq(cpu);
3c29632f 5207 arm_cpu_update_vserr(cpu);
ce4afed8
PM
5208}
5209
d1fb4da2
RH
5210static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5211{
5212 do_hcr_write(env, value, 0);
5213}
5214
ce4afed8
PM
5215static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5216 uint64_t value)
5217{
5218 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5219 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
d1fb4da2 5220 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
ce4afed8
PM
5221}
5222
5223static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5224 uint64_t value)
5225{
5226 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5227 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
d1fb4da2 5228 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
f149e3e8
EI
5229}
5230
f7778444
RH
5231/*
5232 * Return the effective value of HCR_EL2.
5233 * Bits that are not included here:
5234 * RW (read from SCR_EL3.RW as needed)
5235 */
5236uint64_t arm_hcr_el2_eff(CPUARMState *env)
5237{
5238 uint64_t ret = env->cp15.hcr_el2;
5239
e6ef0169 5240 if (!arm_is_el2_enabled(env)) {
f7778444
RH
5241 /*
5242 * "This register has no effect if EL2 is not enabled in the
5243 * current Security state". This is ARMv8.4-SecEL2 speak for
5244 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5245 *
5246 * Prior to that, the language was "In an implementation that
5247 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5248 * as if this field is 0 for all purposes other than a direct
5249 * read or write access of HCR_EL2". With lots of enumeration
5250 * on a per-field basis. In current QEMU, this is condition
5251 * is arm_is_secure_below_el3.
5252 *
5253 * Since the v8.4 language applies to the entire register, and
5254 * appears to be backward compatible, use that.
5255 */
4990e1d3
RH
5256 return 0;
5257 }
5258
5259 /*
5260 * For a cpu that supports both aarch64 and aarch32, we can set bits
5261 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5262 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5263 */
5264 if (!arm_el_is_aa64(env, 2)) {
5265 uint64_t aa32_valid;
5266
5267 /*
5268 * These bits are up-to-date as of ARMv8.6.
5269 * For HCR, it's easiest to list just the 2 bits that are invalid.
5270 * For HCR2, list those that are valid.
5271 */
5272 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5273 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5274 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5275 ret &= aa32_valid;
5276 }
5277
5278 if (ret & HCR_TGE) {
5279 /* These bits are up-to-date as of ARMv8.6. */
f7778444
RH
5280 if (ret & HCR_E2H) {
5281 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5282 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5283 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4990e1d3
RH
5284 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5285 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5286 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
f7778444
RH
5287 } else {
5288 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5289 }
5290 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5291 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5292 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5293 HCR_TLOR);
5294 }
5295
5296 return ret;
5297}
5298
19668718
RH
5299/*
5300 * Corresponds to ARM pseudocode function ELIsInHost().
5301 */
5302bool el_is_in_host(CPUARMState *env, int el)
5303{
5304 uint64_t mask;
5305
5306 /*
5307 * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5308 * Perform the simplest bit tests first, and validate EL2 afterward.
5309 */
5310 if (el & 1) {
5311 return false; /* EL1 or EL3 */
5312 }
5313
5314 /*
5315 * Note that hcr_write() checks isar_feature_aa64_vh(),
5316 * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5317 */
5318 mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5319 if ((env->cp15.hcr_el2 & mask) != mask) {
5320 return false;
5321 }
5322
5323 /* TGE and/or E2H set: double check those bits are currently legal. */
5324 return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5325}
5326
5814d587
RH
5327static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5328 uint64_t value)
5329{
5330 uint64_t valid_mask = 0;
5331
5332 /* No features adding bits to HCRX are implemented. */
5333
5334 /* Clear RES0 bits. */
5335 env->cp15.hcrx_el2 = value & valid_mask;
5336}
5337
5338static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5339 bool isread)
5340{
5341 if (arm_current_el(env) < 3
5342 && arm_feature(env, ARM_FEATURE_EL3)
5343 && !(env->cp15.scr_el3 & SCR_HXEN)) {
5344 return CP_ACCESS_TRAP_EL3;
5345 }
5346 return CP_ACCESS_OK;
5347}
5348
5349static const ARMCPRegInfo hcrx_el2_reginfo = {
5350 .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5351 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5352 .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5353 .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5354};
5355
5356/* Return the effective value of HCRX_EL2. */
5357uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5358{
5359 /*
5360 * The bits in this register behave as 0 for all purposes other than
5361 * direct reads of the register if:
5362 * - EL2 is not enabled in the current security state,
5363 * - SCR_EL3.HXEn is 0.
5364 */
5365 if (!arm_is_el2_enabled(env)
5366 || (arm_feature(env, ARM_FEATURE_EL3)
5367 && !(env->cp15.scr_el3 & SCR_HXEN))) {
5368 return 0;
5369 }
5370 return env->cp15.hcrx_el2;
5371}
5372
fc1120a7
PM
5373static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5374 uint64_t value)
5375{
5376 /*
5377 * For A-profile AArch32 EL3, if NSACR.CP10
5378 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5379 */
5380 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5381 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39
RH
5382 uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5383 value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
fc1120a7
PM
5384 }
5385 env->cp15.cptr_el[2] = value;
5386}
5387
5388static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5389{
5390 /*
5391 * For A-profile AArch32 EL3, if NSACR.CP10
5392 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5393 */
5394 uint64_t value = env->cp15.cptr_el[2];
5395
5396 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5397 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39 5398 value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
fc1120a7
PM
5399 }
5400 return value;
5401}
5402
4771cd01 5403static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8 5404 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
89430fc6 5405 .type = ARM_CP_IO,
f149e3e8
EI
5406 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5407 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5408 .writefn = hcr_write },
ce4afed8 5409 { .name = "HCR", .state = ARM_CP_STATE_AA32,
89430fc6 5410 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5411 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5412 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5413 .writefn = hcr_writelow },
831a2fca
PM
5414 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5415 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5416 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3b685ba7 5417 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5418 .type = ARM_CP_ALIAS,
3b685ba7
EI
5419 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5420 .access = PL2_RW,
5421 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
68e78e33 5422 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
f2c30f42
EI
5423 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5424 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
cba517c3 5425 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
63b60551
EI
5426 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5427 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
cba517c3
PM
5428 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5429 .type = ARM_CP_ALIAS,
5430 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5431 .access = PL2_RW,
5432 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
3b685ba7 5433 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5434 .type = ARM_CP_ALIAS,
3b685ba7 5435 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5436 .access = PL2_RW,
5437 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d79e0c06 5438 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5439 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5440 .access = PL2_RW, .writefn = vbar_write,
5441 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5442 .resetvalue = 0 },
884b4dee
GB
5443 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5444 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5445 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 5446 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
5447 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5448 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5449 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
fc1120a7
PM
5450 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5451 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
95f949ac
EI
5452 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5453 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5454 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5455 .resetvalue = 0 },
5456 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5457 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac
EI
5458 .access = PL2_RW, .type = ARM_CP_ALIAS,
5459 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
5460 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5461 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5462 .access = PL2_RW, .type = ARM_CP_CONST,
5463 .resetvalue = 0 },
5464 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
55b53c71 5465 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5466 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5467 .access = PL2_RW, .type = ARM_CP_CONST,
5468 .resetvalue = 0 },
37cd6c24
PM
5469 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5470 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5471 .access = PL2_RW, .type = ARM_CP_CONST,
5472 .resetvalue = 0 },
5473 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5474 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5475 .access = PL2_RW, .type = ARM_CP_CONST,
5476 .resetvalue = 0 },
06ec4c8c
EI
5477 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5478 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
d06dc933 5479 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
06ec4c8c 5480 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
5481 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5482 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5483 .type = ARM_CP_ALIAS,
68e9c2fe 5484 .access = PL2_RW, .accessfn = access_el3_aa32ns,
afbb181c 5485 .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
68e9c2fe
EI
5486 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5487 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5488 .access = PL2_RW,
988cc190 5489 /* no .writefn needed as this can't cause an ASID change */
68e9c2fe 5490 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
5491 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5492 .cp = 15, .opc1 = 6, .crm = 2,
5493 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5494 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5495 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5496 .writefn = vttbr_write },
5497 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5498 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5499 .access = PL2_RW, .writefn = vttbr_write,
5500 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
5501 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5502 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5503 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5504 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
5505 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5506 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5507 .access = PL2_RW, .resetvalue = 0,
5508 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
5509 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5510 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
ed30da8e 5511 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
a57633c0
EI
5512 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5513 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5514 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 5515 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
5516 { .name = "TLBIALLNSNH",
5517 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5518 .type = ARM_CP_NO_RAW, .access = PL2_W,
5519 .writefn = tlbiall_nsnh_write },
5520 { .name = "TLBIALLNSNHIS",
5521 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5522 .type = ARM_CP_NO_RAW, .access = PL2_W,
5523 .writefn = tlbiall_nsnh_is_write },
5524 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
5525 .type = ARM_CP_NO_RAW, .access = PL2_W,
5526 .writefn = tlbiall_hyp_write },
5527 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
5528 .type = ARM_CP_NO_RAW, .access = PL2_W,
5529 .writefn = tlbiall_hyp_is_write },
5530 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
5531 .type = ARM_CP_NO_RAW, .access = PL2_W,
5532 .writefn = tlbimva_hyp_write },
5533 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
5534 .type = ARM_CP_NO_RAW, .access = PL2_W,
5535 .writefn = tlbimva_hyp_is_write },
51da9014
EI
5536 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
5537 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
696ba377 5538 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 5539 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
5540 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
5541 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
696ba377 5542 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 5543 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
5544 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
5545 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
696ba377 5546 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75
PM
5547 .writefn = tlbi_aa64_vae2_write },
5548 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
5549 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
696ba377 5550 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75 5551 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
5552 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
5553 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
696ba377 5554 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 5555 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
5556 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
5557 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
696ba377 5558 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75 5559 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 5560#ifndef CONFIG_USER_ONLY
2a47df95
PM
5561 /* Unlike the other EL2-related AT operations, these must
5562 * UNDEF from EL3 if EL2 is not implemented, which is why we
5563 * define them here rather than with the rest of the AT ops.
5564 */
5565 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5566 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5567 .access = PL2_W, .accessfn = at_s1e2_access,
696ba377
RH
5568 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5569 .writefn = ats_write64 },
2a47df95
PM
5570 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5571 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5572 .access = PL2_W, .accessfn = at_s1e2_access,
696ba377
RH
5573 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5574 .writefn = ats_write64 },
14db7fe0
PM
5575 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5576 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5577 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5578 * to behave as if SCR.NS was 1.
5579 */
5580 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5581 .access = PL2_W,
0710b2fa 5582 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
14db7fe0
PM
5583 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5584 .access = PL2_W,
0710b2fa 5585 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
0b6440af
EI
5586 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5587 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5588 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5589 * reset values as IMPDEF. We choose to reset to 3 to comply with
5590 * both ARMv7 and ARMv8.
5591 */
5592 .access = PL2_RW, .resetvalue = 3,
5593 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
5594 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5595 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5596 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5597 .writefn = gt_cntvoff_write,
5598 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5599 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5600 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5601 .writefn = gt_cntvoff_write,
5602 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
5603 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5604 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5605 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5606 .type = ARM_CP_IO, .access = PL2_RW,
5607 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5608 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5609 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5610 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5611 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5612 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5613 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 5614 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
5615 .resetfn = gt_hyp_timer_reset,
5616 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5617 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5618 .type = ARM_CP_IO,
5619 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5620 .access = PL2_RW,
5621 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5622 .resetvalue = 0,
5623 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 5624#endif
59e05530
EI
5625 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5626 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5627 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5628 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5629 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5630 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5631 .access = PL2_RW,
5632 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
5633 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5634 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5635 .access = PL2_RW,
5636 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
5637};
5638
ce4afed8
PM
5639static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5640 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
89430fc6 5641 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5642 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5643 .access = PL2_RW,
5644 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5645 .writefn = hcr_writehigh },
ce4afed8
PM
5646};
5647
e9152ee9
RDC
5648static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5649 bool isread)
5650{
5651 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5652 return CP_ACCESS_OK;
5653 }
5654 return CP_ACCESS_TRAP_UNCATEGORIZED;
5655}
5656
5657static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5658 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5659 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5660 .access = PL2_RW, .accessfn = sel2_access,
5661 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5662 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5663 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5664 .access = PL2_RW, .accessfn = sel2_access,
5665 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
e9152ee9
RDC
5666};
5667
2f027fc5
PM
5668static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5669 bool isread)
5670{
5671 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
926c1b97 5672 * At Secure EL1 it traps to EL3 or EL2.
2f027fc5
PM
5673 */
5674 if (arm_current_el(env) == 3) {
5675 return CP_ACCESS_OK;
5676 }
5677 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
5678 if (env->cp15.scr_el3 & SCR_EEL2) {
5679 return CP_ACCESS_TRAP_EL2;
5680 }
2f027fc5
PM
5681 return CP_ACCESS_TRAP_EL3;
5682 }
5683 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5684 if (isread) {
5685 return CP_ACCESS_OK;
5686 }
5687 return CP_ACCESS_TRAP_UNCATEGORIZED;
5688}
5689
60fb1a87
GB
5690static const ARMCPRegInfo el3_cp_reginfo[] = {
5691 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5692 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5693 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
10d0ef3e 5694 .resetfn = scr_reset, .writefn = scr_write },
f80741d1 5695 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
60fb1a87 5696 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
5697 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5698 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 5699 .writefn = scr_write },
60fb1a87
GB
5700 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5701 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5702 .access = PL3_RW, .resetvalue = 0,
5703 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5704 { .name = "SDER",
5705 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5706 .access = PL3_RW, .resetvalue = 0,
5707 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 5708 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
5709 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5710 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 5711 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
5712 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5713 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
f478847f 5714 .access = PL3_RW, .resetvalue = 0,
7dd8c9af 5715 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
5716 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5717 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c 5718 .access = PL3_RW,
cb4a0a34
PM
5719 /* no .writefn needed as this can't cause an ASID change */
5720 .resetvalue = 0,
11f136ee 5721 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 5722 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5723 .type = ARM_CP_ALIAS,
81547d66
EI
5724 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5725 .access = PL3_RW,
5726 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 5727 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
5728 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5729 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
5730 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5731 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5732 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 5733 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 5734 .type = ARM_CP_ALIAS,
81547d66 5735 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5736 .access = PL3_RW,
5737 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
5738 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5739 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5740 .access = PL3_RW, .writefn = vbar_write,
5741 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5742 .resetvalue = 0 },
c6f19164
GB
5743 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5744 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5745 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5746 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
5747 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5748 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5749 .access = PL3_RW, .resetvalue = 0,
5750 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
5751 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5752 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
5753 .access = PL3_RW, .type = ARM_CP_CONST,
5754 .resetvalue = 0 },
37cd6c24
PM
5755 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
5756 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
5757 .access = PL3_RW, .type = ARM_CP_CONST,
5758 .resetvalue = 0 },
5759 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
5760 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
5761 .access = PL3_RW, .type = ARM_CP_CONST,
5762 .resetvalue = 0 },
43efaa33
PM
5763 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
5764 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
5765 .access = PL3_W, .type = ARM_CP_NO_RAW,
5766 .writefn = tlbi_aa64_alle3is_write },
5767 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
5768 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
5769 .access = PL3_W, .type = ARM_CP_NO_RAW,
5770 .writefn = tlbi_aa64_vae3is_write },
5771 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
5772 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
5773 .access = PL3_W, .type = ARM_CP_NO_RAW,
5774 .writefn = tlbi_aa64_vae3is_write },
5775 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
5776 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
5777 .access = PL3_W, .type = ARM_CP_NO_RAW,
5778 .writefn = tlbi_aa64_alle3_write },
5779 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
5780 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
5781 .access = PL3_W, .type = ARM_CP_NO_RAW,
5782 .writefn = tlbi_aa64_vae3_write },
5783 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
5784 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
5785 .access = PL3_W, .type = ARM_CP_NO_RAW,
5786 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
5787};
5788
e2cce18f
RH
5789#ifndef CONFIG_USER_ONLY
5790/* Test if system register redirection is to occur in the current state. */
5791static bool redirect_for_e2h(CPUARMState *env)
5792{
5793 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
5794}
5795
5796static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
5797{
5798 CPReadFn *readfn;
5799
5800 if (redirect_for_e2h(env)) {
5801 /* Switch to the saved EL2 version of the register. */
5802 ri = ri->opaque;
5803 readfn = ri->readfn;
5804 } else {
5805 readfn = ri->orig_readfn;
5806 }
5807 if (readfn == NULL) {
5808 readfn = raw_read;
5809 }
5810 return readfn(env, ri);
5811}
5812
5813static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
5814 uint64_t value)
5815{
5816 CPWriteFn *writefn;
5817
5818 if (redirect_for_e2h(env)) {
5819 /* Switch to the saved EL2 version of the register. */
5820 ri = ri->opaque;
5821 writefn = ri->writefn;
5822 } else {
5823 writefn = ri->orig_writefn;
5824 }
5825 if (writefn == NULL) {
5826 writefn = raw_write;
5827 }
5828 writefn(env, ri, value);
5829}
5830
5831static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
5832{
5833 struct E2HAlias {
5834 uint32_t src_key, dst_key, new_key;
5835 const char *src_name, *dst_name, *new_name;
5836 bool (*feature)(const ARMISARegisters *id);
5837 };
5838
5839#define K(op0, op1, crn, crm, op2) \
5840 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
5841
5842 static const struct E2HAlias aliases[] = {
5843 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
5844 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
5845 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
5846 "CPACR", "CPTR_EL2", "CPACR_EL12" },
5847 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
5848 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
5849 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
5850 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
5851 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
5852 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
5853 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
5854 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
5855 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
5856 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
5857 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
5858 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
5859 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
5860 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
5861 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
5862 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
5863 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
5864 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
5865 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
5866 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
5867 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
5868 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
5869 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
5870 "VBAR", "VBAR_EL2", "VBAR_EL12" },
5871 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
5872 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
5873 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
5874 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
5875
5876 /*
5877 * Note that redirection of ZCR is mentioned in the description
5878 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
5879 * not in the summary table.
5880 */
5881 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
5882 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
de561988
RH
5883 { K(3, 0, 1, 2, 6), K(3, 4, 1, 2, 6), K(3, 5, 1, 2, 6),
5884 "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
e2cce18f 5885
4b779ceb
RH
5886 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
5887 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
5888
7cb1e618
RH
5889 { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
5890 "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
5891 isar_feature_aa64_scxtnum },
5892
e2cce18f
RH
5893 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
5894 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
5895 };
5896#undef K
5897
5898 size_t i;
5899
5900 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
5901 const struct E2HAlias *a = &aliases[i];
9da35a40 5902 ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
9da35a40 5903 bool ok;
e2cce18f
RH
5904
5905 if (a->feature && !a->feature(&cpu->isar)) {
5906 continue;
5907 }
5908
5860362d
RH
5909 src_reg = g_hash_table_lookup(cpu->cp_regs,
5910 (gpointer)(uintptr_t)a->src_key);
5911 dst_reg = g_hash_table_lookup(cpu->cp_regs,
5912 (gpointer)(uintptr_t)a->dst_key);
e2cce18f
RH
5913 g_assert(src_reg != NULL);
5914 g_assert(dst_reg != NULL);
5915
5916 /* Cross-compare names to detect typos in the keys. */
5917 g_assert(strcmp(src_reg->name, a->src_name) == 0);
5918 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
5919
5920 /* None of the core system registers use opaque; we will. */
5921 g_assert(src_reg->opaque == NULL);
5922
5923 /* Create alias before redirection so we dup the right data. */
9da35a40 5924 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
9da35a40
RH
5925
5926 new_reg->name = a->new_name;
5927 new_reg->type |= ARM_CP_ALIAS;
5928 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
5929 new_reg->access &= PL2_RW | PL3_RW;
5930
5860362d
RH
5931 ok = g_hash_table_insert(cpu->cp_regs,
5932 (gpointer)(uintptr_t)a->new_key, new_reg);
9da35a40 5933 g_assert(ok);
e2cce18f
RH
5934
5935 src_reg->opaque = dst_reg;
5936 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
5937 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
5938 if (!src_reg->raw_readfn) {
5939 src_reg->raw_readfn = raw_read;
5940 }
5941 if (!src_reg->raw_writefn) {
5942 src_reg->raw_writefn = raw_write;
5943 }
5944 src_reg->readfn = el2_e2h_read;
5945 src_reg->writefn = el2_e2h_write;
5946 }
5947}
5948#endif
5949
3f208fd7
PM
5950static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5951 bool isread)
7da845b0 5952{
97475a89
RH
5953 int cur_el = arm_current_el(env);
5954
5955 if (cur_el < 2) {
5956 uint64_t hcr = arm_hcr_el2_eff(env);
5957
5958 if (cur_el == 0) {
5959 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5960 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
5961 return CP_ACCESS_TRAP_EL2;
5962 }
5963 } else {
5964 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
5965 return CP_ACCESS_TRAP;
5966 }
5967 if (hcr & HCR_TID2) {
5968 return CP_ACCESS_TRAP_EL2;
5969 }
5970 }
5971 } else if (hcr & HCR_TID2) {
5972 return CP_ACCESS_TRAP_EL2;
5973 }
7da845b0 5974 }
630fcd4d
MZ
5975
5976 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
5977 return CP_ACCESS_TRAP_EL2;
5978 }
5979
7da845b0
PM
5980 return CP_ACCESS_OK;
5981}
5982
58e93b48
RH
5983/*
5984 * Check for traps to RAS registers, which are controlled
5985 * by HCR_EL2.TERR and SCR_EL3.TERR.
5986 */
5987static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
5988 bool isread)
5989{
5990 int el = arm_current_el(env);
5991
5992 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
5993 return CP_ACCESS_TRAP_EL2;
5994 }
5995 if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
5996 return CP_ACCESS_TRAP_EL3;
5997 }
5998 return CP_ACCESS_OK;
5999}
6000
6001static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6002{
6003 int el = arm_current_el(env);
6004
6005 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6006 return env->cp15.vdisr_el2;
6007 }
6008 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6009 return 0; /* RAZ/WI */
6010 }
6011 return env->cp15.disr_el1;
6012}
6013
6014static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6015{
6016 int el = arm_current_el(env);
6017
6018 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6019 env->cp15.vdisr_el2 = val;
6020 return;
6021 }
6022 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6023 return; /* RAZ/WI */
6024 }
6025 env->cp15.disr_el1 = val;
6026}
6027
6028/*
6029 * Minimal RAS implementation with no Error Records.
6030 * Which means that all of the Error Record registers:
6031 * ERXADDR_EL1
6032 * ERXCTLR_EL1
6033 * ERXFR_EL1
6034 * ERXMISC0_EL1
6035 * ERXMISC1_EL1
6036 * ERXMISC2_EL1
6037 * ERXMISC3_EL1
6038 * ERXPFGCDN_EL1 (RASv1p1)
6039 * ERXPFGCTL_EL1 (RASv1p1)
6040 * ERXPFGF_EL1 (RASv1p1)
6041 * ERXSTATUS_EL1
6042 * and
6043 * ERRSELR_EL1
6044 * may generate UNDEFINED, which is the effect we get by not
6045 * listing them at all.
6046 */
6047static const ARMCPRegInfo minimal_ras_reginfo[] = {
6048 { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6049 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6050 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6051 .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6052 { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6053 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6054 .access = PL1_R, .accessfn = access_terr,
6055 .type = ARM_CP_CONST, .resetvalue = 0 },
6056 { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6057 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6058 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6059 { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6060 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6061 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6062};
6063
397d922c
RH
6064/*
6065 * Return the exception level to which exceptions should be taken
6066 * via SVEAccessTrap. This excludes the check for whether the exception
6067 * should be routed through AArch64.AdvSIMDFPAccessTrap. That can easily
6068 * be found by testing 0 < fp_exception_el < sve_exception_el.
6069 *
6070 * C.f. the ARM pseudocode function CheckSVEEnabled. Note that the
6071 * pseudocode does *not* separate out the FP trap checks, but has them
6072 * all in one function.
5be5e8ed 6073 */
ced31551 6074int sve_exception_el(CPUARMState *env, int el)
5be5e8ed
RH
6075{
6076#ifndef CONFIG_USER_ONLY
aa4451b6 6077 if (el <= 1 && !el_is_in_host(env, el)) {
fab8ad39 6078 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
7701cee5
RH
6079 case 1:
6080 if (el != 0) {
6081 break;
6082 }
6083 /* fall through */
6084 case 0:
6085 case 2:
61a8c23a 6086 return 1;
5be5e8ed 6087 }
5be5e8ed
RH
6088 }
6089
7d38cb92
RH
6090 if (el <= 2 && arm_is_el2_enabled(env)) {
6091 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6092 if (env->cp15.hcr_el2 & HCR_E2H) {
fab8ad39 6093 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
d5a6fa2d 6094 case 1:
7d38cb92 6095 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
d5a6fa2d
RH
6096 break;
6097 }
6098 /* fall through */
6099 case 0:
6100 case 2:
6101 return 2;
6102 }
7d38cb92 6103 } else {
fab8ad39 6104 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
d5a6fa2d
RH
6105 return 2;
6106 }
60eed086 6107 }
5be5e8ed
RH
6108 }
6109
60eed086
RH
6110 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6111 if (arm_feature(env, ARM_FEATURE_EL3)
fab8ad39 6112 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
5be5e8ed
RH
6113 return 3;
6114 }
6115#endif
6116 return 0;
6117}
6118
6b2ca83e
RH
6119/*
6120 * Return the exception level to which exceptions should be taken for SME.
6121 * C.f. the ARM pseudocode function CheckSMEAccess.
6122 */
6123int sme_exception_el(CPUARMState *env, int el)
6124{
6125#ifndef CONFIG_USER_ONLY
6126 if (el <= 1 && !el_is_in_host(env, el)) {
6127 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6128 case 1:
6129 if (el != 0) {
6130 break;
6131 }
6132 /* fall through */
6133 case 0:
6134 case 2:
6135 return 1;
6136 }
6137 }
6138
6139 if (el <= 2 && arm_is_el2_enabled(env)) {
6140 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6141 if (env->cp15.hcr_el2 & HCR_E2H) {
6142 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6143 case 1:
6144 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6145 break;
6146 }
6147 /* fall through */
6148 case 0:
6149 case 2:
6150 return 2;
6151 }
6152 } else {
6153 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6154 return 2;
6155 }
6156 }
6157 }
6158
6159 /* CPTR_EL3. Since ESM is negative we must check for EL3. */
6160 if (arm_feature(env, ARM_FEATURE_EL3)
6161 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6162 return 3;
6163 }
6164#endif
6165 return 0;
6166}
6167
75fe8356
RH
6168/* This corresponds to the ARM pseudocode function IsFullA64Enabled(). */
6169static bool sme_fa64(CPUARMState *env, int el)
6170{
6171 if (!cpu_isar_feature(aa64_sme_fa64, env_archcpu(env))) {
6172 return false;
6173 }
6174
6175 if (el <= 1 && !el_is_in_host(env, el)) {
6176 if (!FIELD_EX64(env->vfp.smcr_el[1], SMCR, FA64)) {
6177 return false;
6178 }
6179 }
6180 if (el <= 2 && arm_is_el2_enabled(env)) {
6181 if (!FIELD_EX64(env->vfp.smcr_el[2], SMCR, FA64)) {
6182 return false;
6183 }
6184 }
6185 if (arm_feature(env, ARM_FEATURE_EL3)) {
6186 if (!FIELD_EX64(env->vfp.smcr_el[3], SMCR, FA64)) {
6187 return false;
6188 }
6189 }
6190
6191 return true;
6192}
6193
0ab5953b
RH
6194/*
6195 * Given that SVE is enabled, return the vector length for EL.
6196 */
6ca54aa9 6197uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
0ab5953b 6198{
2fc0cc0e 6199 ARMCPU *cpu = env_archcpu(env);
6ca54aa9
RH
6200 uint64_t *cr = env->vfp.zcr_el;
6201 uint32_t map = cpu->sve_vq.map;
6202 uint32_t len = ARM_MAX_VQ - 1;
6203
6204 if (sm) {
6205 cr = env->vfp.smcr_el;
6206 map = cpu->sme_vq.map;
6207 }
0ab5953b 6208
c6225beb 6209 if (el <= 1 && !el_is_in_host(env, el)) {
6ca54aa9 6210 len = MIN(len, 0xf & (uint32_t)cr[1]);
0ab5953b 6211 }
6a02a732 6212 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6ca54aa9 6213 len = MIN(len, 0xf & (uint32_t)cr[2]);
0ab5953b 6214 }
6a02a732 6215 if (arm_feature(env, ARM_FEATURE_EL3)) {
6ca54aa9
RH
6216 len = MIN(len, 0xf & (uint32_t)cr[3]);
6217 }
6218
6219 map &= MAKE_64BIT_MASK(0, len + 1);
6220 if (map != 0) {
6221 return 31 - clz32(map);
0ab5953b 6222 }
0df9142d 6223
6ca54aa9
RH
6224 /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6225 assert(sm);
6226 return ctz32(cpu->sme_vq.map);
6227}
6228
6229uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6230{
6231 return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
0ab5953b
RH
6232}
6233
5be5e8ed
RH
6234static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6235 uint64_t value)
6236{
0ab5953b 6237 int cur_el = arm_current_el(env);
5ef3cc56 6238 int old_len = sve_vqm1_for_el(env, cur_el);
0ab5953b
RH
6239 int new_len;
6240
5be5e8ed 6241 /* Bits other than [3:0] are RAZ/WI. */
7b351d98 6242 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5be5e8ed 6243 raw_write(env, ri, value & 0xf);
0ab5953b
RH
6244
6245 /*
6246 * Because we arrived here, we know both FP and SVE are enabled;
6247 * otherwise we would have trapped access to the ZCR_ELn register.
6248 */
5ef3cc56 6249 new_len = sve_vqm1_for_el(env, cur_el);
0ab5953b
RH
6250 if (new_len < old_len) {
6251 aarch64_sve_narrow_vq(env, new_len + 1);
6252 }
5be5e8ed
RH
6253}
6254
60360d82
RH
6255static const ARMCPRegInfo zcr_reginfo[] = {
6256 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6257 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6258 .access = PL1_RW, .type = ARM_CP_SVE,
6259 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6260 .writefn = zcr_write, .raw_writefn = raw_write },
6261 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6262 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6263 .access = PL2_RW, .type = ARM_CP_SVE,
6264 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6265 .writefn = zcr_write, .raw_writefn = raw_write },
6266 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6267 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6268 .access = PL3_RW, .type = ARM_CP_SVE,
6269 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6270 .writefn = zcr_write, .raw_writefn = raw_write },
5be5e8ed
RH
6271};
6272
9e5ec745
RH
6273#ifdef TARGET_AARCH64
6274static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6275 bool isread)
6276{
6277 int el = arm_current_el(env);
6278
6279 if (el == 0) {
6280 uint64_t sctlr = arm_sctlr(env, el);
6281 if (!(sctlr & SCTLR_EnTP2)) {
6282 return CP_ACCESS_TRAP;
6283 }
6284 }
6285 /* TODO: FEAT_FGT */
6286 if (el < 3
6287 && arm_feature(env, ARM_FEATURE_EL3)
6288 && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6289 return CP_ACCESS_TRAP_EL3;
6290 }
6291 return CP_ACCESS_OK;
6292}
6293
d5b1223a
RH
6294static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6295 bool isread)
6296{
6297 /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6298 if (arm_current_el(env) < 3
6299 && arm_feature(env, ARM_FEATURE_EL3)
6300 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6301 return CP_ACCESS_TRAP_EL3;
6302 }
6303 return CP_ACCESS_OK;
6304}
6305
c37e6ac9
RH
6306static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6307 uint64_t value)
6308{
f84734b8
RH
6309 helper_set_pstate_sm(env, FIELD_EX64(value, SVCR, SM));
6310 helper_set_pstate_za(env, FIELD_EX64(value, SVCR, ZA));
6311 arm_rebuild_hflags(env);
c37e6ac9
RH
6312}
6313
de561988
RH
6314static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6315 uint64_t value)
6316{
6317 int cur_el = arm_current_el(env);
6318 int old_len = sve_vqm1_for_el(env, cur_el);
6319 int new_len;
6320
6321 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6322 value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6323 raw_write(env, ri, value);
6324
6325 /*
6326 * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6327 * when SVL is widened (old values kept, or zeros). Choose to keep the
6328 * current values for simplicity. But for QEMU internals, we must still
6329 * apply the narrower SVL to the Zregs and Pregs -- see the comment
6330 * above aarch64_sve_narrow_vq.
6331 */
6332 new_len = sve_vqm1_for_el(env, cur_el);
6333 if (new_len < old_len) {
6334 aarch64_sve_narrow_vq(env, new_len + 1);
6335 }
6336}
6337
9e5ec745
RH
6338static const ARMCPRegInfo sme_reginfo[] = {
6339 { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6340 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6341 .access = PL0_RW, .accessfn = access_tpidr2,
6342 .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
c37e6ac9
RH
6343 { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6344 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6345 .access = PL0_RW, .type = ARM_CP_SME,
6346 .fieldoffset = offsetof(CPUARMState, svcr),
6347 .writefn = svcr_write, .raw_writefn = raw_write },
de561988
RH
6348 { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6349 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6350 .access = PL1_RW, .type = ARM_CP_SME,
6351 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6352 .writefn = smcr_write, .raw_writefn = raw_write },
6353 { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6354 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6355 .access = PL2_RW, .type = ARM_CP_SME,
6356 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6357 .writefn = smcr_write, .raw_writefn = raw_write },
6358 { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6359 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6360 .access = PL3_RW, .type = ARM_CP_SME,
6361 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6362 .writefn = smcr_write, .raw_writefn = raw_write },
d5b1223a
RH
6363 { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6364 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6365 .access = PL1_R, .accessfn = access_aa64_tid1,
6366 /*
6367 * IMPLEMENTOR = 0 (software)
6368 * REVISION = 0 (implementation defined)
6369 * SMPS = 0 (no streaming execution priority in QEMU)
6370 * AFFINITY = 0 (streaming sve mode not shared with other PEs)
6371 */
6372 .type = ARM_CP_CONST, .resetvalue = 0, },
6373 /*
6374 * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6375 */
6376 { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6377 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6378 .access = PL1_RW, .accessfn = access_esm,
6379 .type = ARM_CP_CONST, .resetvalue = 0 },
6380 { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6381 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6382 .access = PL2_RW, .accessfn = access_esm,
6383 .type = ARM_CP_CONST, .resetvalue = 0 },
9e5ec745
RH
6384};
6385#endif /* TARGET_AARCH64 */
6386
24183fb6
PM
6387static void define_pmu_regs(ARMCPU *cpu)
6388{
6389 /*
6390 * v7 performance monitor control register: same implementor
6391 * field as main ID register, and we implement four counters in
6392 * addition to the cycle count register.
6393 */
24526bb9 6394 unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
24183fb6
PM
6395 ARMCPRegInfo pmcr = {
6396 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6397 .access = PL0_RW,
6398 .type = ARM_CP_IO | ARM_CP_ALIAS,
6399 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6400 .accessfn = pmreg_access, .writefn = pmcr_write,
6401 .raw_writefn = raw_write,
6402 };
6403 ARMCPRegInfo pmcr64 = {
6404 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6405 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6406 .access = PL0_RW, .accessfn = pmreg_access,
6407 .type = ARM_CP_IO,
6408 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
24526bb9 6409 .resetvalue = cpu->isar.reset_pmcr_el0,
24183fb6
PM
6410 .writefn = pmcr_write, .raw_writefn = raw_write,
6411 };
24526bb9 6412
24183fb6
PM
6413 define_one_arm_cp_reg(cpu, &pmcr);
6414 define_one_arm_cp_reg(cpu, &pmcr64);
6415 for (i = 0; i < pmcrn; i++) {
6416 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6417 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6418 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6419 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6420 ARMCPRegInfo pmev_regs[] = {
6421 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6422 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6423 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6424 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
99a50d1a 6425 .accessfn = pmreg_access_xevcntr },
24183fb6
PM
6426 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6427 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
99a50d1a 6428 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
24183fb6
PM
6429 .type = ARM_CP_IO,
6430 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6431 .raw_readfn = pmevcntr_rawread,
6432 .raw_writefn = pmevcntr_rawwrite },
6433 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6434 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6435 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6436 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6437 .accessfn = pmreg_access },
6438 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6439 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6440 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6441 .type = ARM_CP_IO,
6442 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6443 .raw_writefn = pmevtyper_rawwrite },
24183fb6
PM
6444 };
6445 define_arm_cp_regs(cpu, pmev_regs);
6446 g_free(pmevcntr_name);
6447 g_free(pmevcntr_el0_name);
6448 g_free(pmevtyper_name);
6449 g_free(pmevtyper_el0_name);
6450 }
a793bcd0 6451 if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
24183fb6
PM
6452 ARMCPRegInfo v81_pmu_regs[] = {
6453 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6454 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6455 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6456 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6457 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6458 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6459 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6460 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
24183fb6
PM
6461 };
6462 define_arm_cp_regs(cpu, v81_pmu_regs);
6463 }
a793bcd0 6464 if (cpu_isar_feature(any_pmuv3p4, cpu)) {
15dd1ebd
PM
6465 static const ARMCPRegInfo v84_pmmir = {
6466 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6467 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6468 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6469 .resetvalue = 0
6470 };
6471 define_one_arm_cp_reg(cpu, &v84_pmmir);
6472 }
24183fb6
PM
6473}
6474
96a8b92e
PM
6475/* We don't know until after realize whether there's a GICv3
6476 * attached, and that is what registers the gicv3 sysregs.
6477 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6478 * at runtime.
6479 */
6480static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6481{
2fc0cc0e 6482 ARMCPU *cpu = env_archcpu(env);
8a130a7b 6483 uint64_t pfr1 = cpu->isar.id_pfr1;
96a8b92e
PM
6484
6485 if (env->gicv3state) {
6486 pfr1 |= 1 << 28;
6487 }
6488 return pfr1;
6489}
6490
976b99b6 6491#ifndef CONFIG_USER_ONLY
96a8b92e
PM
6492static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6493{
2fc0cc0e 6494 ARMCPU *cpu = env_archcpu(env);
47576b94 6495 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
96a8b92e
PM
6496
6497 if (env->gicv3state) {
6498 pfr0 |= 1 << 24;
6499 }
6500 return pfr0;
6501}
976b99b6 6502#endif
96a8b92e 6503
2d7137c1 6504/* Shared logic between LORID and the rest of the LOR* registers.
9bd268ba 6505 * Secure state exclusion has already been dealt with.
2d7137c1 6506 */
9bd268ba
RDC
6507static CPAccessResult access_lor_ns(CPUARMState *env,
6508 const ARMCPRegInfo *ri, bool isread)
2d7137c1
RH
6509{
6510 int el = arm_current_el(env);
6511
6512 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6513 return CP_ACCESS_TRAP_EL2;
6514 }
6515 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6516 return CP_ACCESS_TRAP_EL3;
6517 }
6518 return CP_ACCESS_OK;
6519}
6520
2d7137c1
RH
6521static CPAccessResult access_lor_other(CPUARMState *env,
6522 const ARMCPRegInfo *ri, bool isread)
6523{
6524 if (arm_is_secure_below_el3(env)) {
6525 /* Access denied in secure mode. */
6526 return CP_ACCESS_TRAP;
6527 }
9bd268ba 6528 return access_lor_ns(env, ri, isread);
2d7137c1
RH
6529}
6530
d8564ee4
RH
6531/*
6532 * A trivial implementation of ARMv8.1-LOR leaves all of these
6533 * registers fixed at 0, which indicates that there are zero
6534 * supported Limited Ordering regions.
6535 */
6536static const ARMCPRegInfo lor_reginfo[] = {
6537 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6538 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6539 .access = PL1_RW, .accessfn = access_lor_other,
6540 .type = ARM_CP_CONST, .resetvalue = 0 },
6541 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6542 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6543 .access = PL1_RW, .accessfn = access_lor_other,
6544 .type = ARM_CP_CONST, .resetvalue = 0 },
6545 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6546 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6547 .access = PL1_RW, .accessfn = access_lor_other,
6548 .type = ARM_CP_CONST, .resetvalue = 0 },
6549 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
6550 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
6551 .access = PL1_RW, .accessfn = access_lor_other,
6552 .type = ARM_CP_CONST, .resetvalue = 0 },
6553 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
6554 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
9bd268ba 6555 .access = PL1_R, .accessfn = access_lor_ns,
d8564ee4 6556 .type = ARM_CP_CONST, .resetvalue = 0 },
d8564ee4
RH
6557};
6558
967aa94f
RH
6559#ifdef TARGET_AARCH64
6560static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
6561 bool isread)
6562{
6563 int el = arm_current_el(env);
6564
6565 if (el < 2 &&
07b034ea 6566 arm_is_el2_enabled(env) &&
967aa94f
RH
6567 !(arm_hcr_el2_eff(env) & HCR_APK)) {
6568 return CP_ACCESS_TRAP_EL2;
6569 }
6570 if (el < 3 &&
6571 arm_feature(env, ARM_FEATURE_EL3) &&
6572 !(env->cp15.scr_el3 & SCR_APK)) {
6573 return CP_ACCESS_TRAP_EL3;
6574 }
6575 return CP_ACCESS_OK;
6576}
6577
6578static const ARMCPRegInfo pauth_reginfo[] = {
6579 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6580 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
6581 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6582 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
967aa94f
RH
6583 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6584 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
6585 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6586 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
967aa94f
RH
6587 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6588 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
6589 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6590 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
967aa94f
RH
6591 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6592 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
6593 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6594 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
967aa94f
RH
6595 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6596 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
6597 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6598 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
967aa94f
RH
6599 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6600 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
6601 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6602 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
967aa94f
RH
6603 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6604 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
6605 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6606 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
967aa94f
RH
6607 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6608 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
6609 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6610 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
967aa94f
RH
6611 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
6612 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
6613 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6614 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
967aa94f
RH
6615 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
6616 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
6617 .access = PL1_RW, .accessfn = access_pauth,
108b3ba8 6618 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
967aa94f 6619};
de390645 6620
84940ed8
RC
6621static const ARMCPRegInfo tlbirange_reginfo[] = {
6622 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
6623 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
6624 .access = PL1_W, .type = ARM_CP_NO_RAW,
6625 .writefn = tlbi_aa64_rvae1is_write },
6626 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
6627 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
6628 .access = PL1_W, .type = ARM_CP_NO_RAW,
6629 .writefn = tlbi_aa64_rvae1is_write },
6630 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
6631 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
6632 .access = PL1_W, .type = ARM_CP_NO_RAW,
6633 .writefn = tlbi_aa64_rvae1is_write },
6634 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
6635 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
6636 .access = PL1_W, .type = ARM_CP_NO_RAW,
6637 .writefn = tlbi_aa64_rvae1is_write },
6638 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
6639 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
6640 .access = PL1_W, .type = ARM_CP_NO_RAW,
6641 .writefn = tlbi_aa64_rvae1is_write },
6642 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
6643 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
6644 .access = PL1_W, .type = ARM_CP_NO_RAW,
6645 .writefn = tlbi_aa64_rvae1is_write },
6646 { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
6647 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
6648 .access = PL1_W, .type = ARM_CP_NO_RAW,
6649 .writefn = tlbi_aa64_rvae1is_write },
6650 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
6651 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
6652 .access = PL1_W, .type = ARM_CP_NO_RAW,
6653 .writefn = tlbi_aa64_rvae1is_write },
6654 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
6655 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
6656 .access = PL1_W, .type = ARM_CP_NO_RAW,
6657 .writefn = tlbi_aa64_rvae1_write },
6658 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
6659 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
6660 .access = PL1_W, .type = ARM_CP_NO_RAW,
6661 .writefn = tlbi_aa64_rvae1_write },
6662 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
6663 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
6664 .access = PL1_W, .type = ARM_CP_NO_RAW,
6665 .writefn = tlbi_aa64_rvae1_write },
6666 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
6667 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
6668 .access = PL1_W, .type = ARM_CP_NO_RAW,
6669 .writefn = tlbi_aa64_rvae1_write },
6670 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
6671 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
6672 .access = PL2_W, .type = ARM_CP_NOP },
6673 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
6674 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
6675 .access = PL2_W, .type = ARM_CP_NOP },
6676 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
6677 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
696ba377 6678 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6679 .writefn = tlbi_aa64_rvae2is_write },
6680 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
6681 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
696ba377 6682 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6683 .writefn = tlbi_aa64_rvae2is_write },
6684 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
6685 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
6686 .access = PL2_W, .type = ARM_CP_NOP },
6687 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
6688 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
6689 .access = PL2_W, .type = ARM_CP_NOP },
6690 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
6691 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
696ba377 6692 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6693 .writefn = tlbi_aa64_rvae2is_write },
6694 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
6695 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
696ba377 6696 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6697 .writefn = tlbi_aa64_rvae2is_write },
6698 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
6699 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
696ba377 6700 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6701 .writefn = tlbi_aa64_rvae2_write },
6702 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
6703 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
696ba377 6704 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
6705 .writefn = tlbi_aa64_rvae2_write },
6706 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
6707 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
6708 .access = PL3_W, .type = ARM_CP_NO_RAW,
6709 .writefn = tlbi_aa64_rvae3is_write },
6710 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
6711 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
6712 .access = PL3_W, .type = ARM_CP_NO_RAW,
6713 .writefn = tlbi_aa64_rvae3is_write },
6714 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
6715 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
6716 .access = PL3_W, .type = ARM_CP_NO_RAW,
6717 .writefn = tlbi_aa64_rvae3is_write },
6718 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
6719 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
6720 .access = PL3_W, .type = ARM_CP_NO_RAW,
6721 .writefn = tlbi_aa64_rvae3is_write },
6722 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
6723 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
6724 .access = PL3_W, .type = ARM_CP_NO_RAW,
6725 .writefn = tlbi_aa64_rvae3_write },
6726 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
6727 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
6728 .access = PL3_W, .type = ARM_CP_NO_RAW,
6729 .writefn = tlbi_aa64_rvae3_write },
84940ed8
RC
6730};
6731
7113d618
RC
6732static const ARMCPRegInfo tlbios_reginfo[] = {
6733 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
6734 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
6735 .access = PL1_W, .type = ARM_CP_NO_RAW,
6736 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
6737 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
6738 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
6739 .access = PL1_W, .type = ARM_CP_NO_RAW,
6740 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
6741 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
6742 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
6743 .access = PL1_W, .type = ARM_CP_NO_RAW,
6744 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
6745 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
6746 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
6747 .access = PL1_W, .type = ARM_CP_NO_RAW,
6748 .writefn = tlbi_aa64_vae1is_write },
6749 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
6750 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
6751 .access = PL1_W, .type = ARM_CP_NO_RAW,
6752 .writefn = tlbi_aa64_vae1is_write },
6753 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
6754 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
6755 .access = PL1_W, .type = ARM_CP_NO_RAW,
6756 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
6757 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
6758 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
696ba377 6759 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7113d618 6760 .writefn = tlbi_aa64_alle2is_write },
b7469ef9
IH
6761 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
6762 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
696ba377 6763 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
b7469ef9 6764 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
6765 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
6766 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
6767 .access = PL2_W, .type = ARM_CP_NO_RAW,
6768 .writefn = tlbi_aa64_alle1is_write },
b7469ef9
IH
6769 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
6770 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
696ba377 6771 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
b7469ef9 6772 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
6773 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
6774 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
6775 .access = PL2_W, .type = ARM_CP_NO_RAW,
6776 .writefn = tlbi_aa64_alle1is_write },
6777 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
6778 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
6779 .access = PL2_W, .type = ARM_CP_NOP },
6780 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
6781 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
6782 .access = PL2_W, .type = ARM_CP_NOP },
6783 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6784 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
6785 .access = PL2_W, .type = ARM_CP_NOP },
6786 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
6787 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
6788 .access = PL2_W, .type = ARM_CP_NOP },
6789 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
6790 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
6791 .access = PL3_W, .type = ARM_CP_NO_RAW,
6792 .writefn = tlbi_aa64_alle3is_write },
b7469ef9
IH
6793 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
6794 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
6795 .access = PL3_W, .type = ARM_CP_NO_RAW,
6796 .writefn = tlbi_aa64_vae3is_write },
6797 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
6798 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
6799 .access = PL3_W, .type = ARM_CP_NO_RAW,
6800 .writefn = tlbi_aa64_vae3is_write },
7113d618
RC
6801};
6802
de390645
RH
6803static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
6804{
6805 Error *err = NULL;
6806 uint64_t ret;
6807
6808 /* Success sets NZCV = 0000. */
6809 env->NF = env->CF = env->VF = 0, env->ZF = 1;
6810
6811 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
6812 /*
6813 * ??? Failed, for unknown reasons in the crypto subsystem.
6814 * The best we can do is log the reason and return the
6815 * timed-out indication to the guest. There is no reason
6816 * we know to expect this failure to be transitory, so the
6817 * guest may well hang retrying the operation.
6818 */
6819 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
6820 ri->name, error_get_pretty(err));
6821 error_free(err);
6822
6823 env->ZF = 0; /* NZCF = 0100 */
6824 return 0;
6825 }
6826 return ret;
6827}
6828
6829/* We do not support re-seeding, so the two registers operate the same. */
6830static const ARMCPRegInfo rndr_reginfo[] = {
6831 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
6832 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6833 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
6834 .access = PL0_R, .readfn = rndr_readfn },
6835 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
6836 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
6837 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
6838 .access = PL0_R, .readfn = rndr_readfn },
de390645 6839};
0d57b499
BM
6840
6841#ifndef CONFIG_USER_ONLY
6842static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
6843 uint64_t value)
6844{
6845 ARMCPU *cpu = env_archcpu(env);
6846 /* CTR_EL0 System register -> DminLine, bits [19:16] */
6847 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
6848 uint64_t vaddr_in = (uint64_t) value;
6849 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
6850 void *haddr;
6851 int mem_idx = cpu_mmu_index(env, false);
6852
6853 /* This won't be crossing page boundaries */
6854 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
6855 if (haddr) {
6856
6857 ram_addr_t offset;
6858 MemoryRegion *mr;
6859
6860 /* RCU lock is already being held */
6861 mr = memory_region_from_host(haddr, &offset);
6862
6863 if (mr) {
4dfe59d1 6864 memory_region_writeback(mr, offset, dline_size);
0d57b499
BM
6865 }
6866 }
6867}
6868
6869static const ARMCPRegInfo dcpop_reg[] = {
6870 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
6871 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
6872 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 6873 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
6874};
6875
6876static const ARMCPRegInfo dcpodp_reg[] = {
6877 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
6878 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
6879 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
1bed4d2e 6880 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
6881};
6882#endif /*CONFIG_USER_ONLY*/
6883
4b779ceb
RH
6884static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
6885 bool isread)
6886{
6887 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
6888 return CP_ACCESS_TRAP_EL2;
6889 }
6890
6891 return CP_ACCESS_OK;
6892}
6893
6894static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
6895 bool isread)
6896{
6897 int el = arm_current_el(env);
6898
0da067f2 6899 if (el < 2 && arm_is_el2_enabled(env)) {
4301acd7
RH
6900 uint64_t hcr = arm_hcr_el2_eff(env);
6901 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
6902 return CP_ACCESS_TRAP_EL2;
6903 }
4b779ceb
RH
6904 }
6905 if (el < 3 &&
6906 arm_feature(env, ARM_FEATURE_EL3) &&
6907 !(env->cp15.scr_el3 & SCR_ATA)) {
6908 return CP_ACCESS_TRAP_EL3;
6909 }
6910 return CP_ACCESS_OK;
6911}
6912
6913static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
6914{
6915 return env->pstate & PSTATE_TCO;
6916}
6917
6918static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6919{
6920 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
6921}
6922
6923static const ARMCPRegInfo mte_reginfo[] = {
6924 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
6925 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
6926 .access = PL1_RW, .accessfn = access_mte,
6927 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
6928 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
6929 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
6930 .access = PL1_RW, .accessfn = access_mte,
6931 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
6932 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
6933 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
6934 .access = PL2_RW, .accessfn = access_mte,
6935 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
6936 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
6937 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
6938 .access = PL3_RW,
6939 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
6940 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
6941 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
6942 .access = PL1_RW, .accessfn = access_mte,
6943 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
6944 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
6945 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
6946 .access = PL1_RW, .accessfn = access_mte,
6947 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
6948 { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
6949 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
6950 .access = PL1_R, .accessfn = access_aa64_tid5,
6951 .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
6952 { .name = "TCO", .state = ARM_CP_STATE_AA64,
6953 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
6954 .type = ARM_CP_NO_RAW,
6955 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
5463df16
RH
6956 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
6957 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
6958 .type = ARM_CP_NOP, .access = PL1_W,
6959 .accessfn = aa64_cacheop_poc_access },
6960 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
6961 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
6962 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6963 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
6964 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
6965 .type = ARM_CP_NOP, .access = PL1_W,
6966 .accessfn = aa64_cacheop_poc_access },
6967 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
6968 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
6969 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6970 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
6971 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
6972 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6973 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
6974 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
6975 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6976 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
6977 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
6978 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6979 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
6980 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
6981 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
4b779ceb
RH
6982};
6983
6984static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
6985 { .name = "TCO", .state = ARM_CP_STATE_AA64,
6986 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
6987 .type = ARM_CP_CONST, .access = PL0_RW, },
4b779ceb 6988};
5463df16
RH
6989
6990static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
6991 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
6992 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
6993 .type = ARM_CP_NOP, .access = PL0_W,
6994 .accessfn = aa64_cacheop_poc_access },
6995 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
6996 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
6997 .type = ARM_CP_NOP, .access = PL0_W,
6998 .accessfn = aa64_cacheop_poc_access },
6999 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7000 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7001 .type = ARM_CP_NOP, .access = PL0_W,
7002 .accessfn = aa64_cacheop_poc_access },
7003 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7004 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7005 .type = ARM_CP_NOP, .access = PL0_W,
7006 .accessfn = aa64_cacheop_poc_access },
7007 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7008 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7009 .type = ARM_CP_NOP, .access = PL0_W,
7010 .accessfn = aa64_cacheop_poc_access },
7011 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7012 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7013 .type = ARM_CP_NOP, .access = PL0_W,
7014 .accessfn = aa64_cacheop_poc_access },
7015 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7016 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7017 .type = ARM_CP_NOP, .access = PL0_W,
7018 .accessfn = aa64_cacheop_poc_access },
7019 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7020 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7021 .type = ARM_CP_NOP, .access = PL0_W,
7022 .accessfn = aa64_cacheop_poc_access },
eb821168
RH
7023 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7024 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7025 .access = PL0_W, .type = ARM_CP_DC_GVA,
7026#ifndef CONFIG_USER_ONLY
7027 /* Avoid overhead of an access check that always passes in user-mode */
7028 .accessfn = aa64_zva_access,
7029#endif
7030 },
7031 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7032 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7033 .access = PL0_W, .type = ARM_CP_DC_GZVA,
7034#ifndef CONFIG_USER_ONLY
7035 /* Avoid overhead of an access check that always passes in user-mode */
7036 .accessfn = aa64_zva_access,
7037#endif
7038 },
5463df16
RH
7039};
7040
7cb1e618
RH
7041static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7042 bool isread)
7043{
7044 uint64_t hcr = arm_hcr_el2_eff(env);
7045 int el = arm_current_el(env);
7046
7047 if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7048 if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7049 if (hcr & HCR_TGE) {
7050 return CP_ACCESS_TRAP_EL2;
7051 }
7052 return CP_ACCESS_TRAP;
7053 }
7054 } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7055 return CP_ACCESS_TRAP_EL2;
7056 }
7057 if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7058 return CP_ACCESS_TRAP_EL2;
7059 }
7060 if (el < 3
7061 && arm_feature(env, ARM_FEATURE_EL3)
7062 && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7063 return CP_ACCESS_TRAP_EL3;
7064 }
7065 return CP_ACCESS_OK;
7066}
7067
7068static const ARMCPRegInfo scxtnum_reginfo[] = {
7069 { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7070 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7071 .access = PL0_RW, .accessfn = access_scxtnum,
7072 .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7073 { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7074 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7075 .access = PL1_RW, .accessfn = access_scxtnum,
7076 .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7077 { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7078 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7079 .access = PL2_RW, .accessfn = access_scxtnum,
7080 .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7081 { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7082 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7083 .access = PL3_RW,
7084 .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7085};
7086#endif /* TARGET_AARCH64 */
967aa94f 7087
cb570bd3
RH
7088static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7089 bool isread)
7090{
7091 int el = arm_current_el(env);
7092
7093 if (el == 0) {
7094 uint64_t sctlr = arm_sctlr(env, el);
7095 if (!(sctlr & SCTLR_EnRCTX)) {
7096 return CP_ACCESS_TRAP;
7097 }
7098 } else if (el == 1) {
7099 uint64_t hcr = arm_hcr_el2_eff(env);
7100 if (hcr & HCR_NV) {
7101 return CP_ACCESS_TRAP_EL2;
7102 }
7103 }
7104 return CP_ACCESS_OK;
7105}
7106
7107static const ARMCPRegInfo predinv_reginfo[] = {
7108 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7109 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7110 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7111 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7112 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7113 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7114 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7115 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7116 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7117 /*
7118 * Note the AArch32 opcodes have a different OPC1.
7119 */
7120 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7121 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7122 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7123 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7124 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7125 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7126 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7127 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7128 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
cb570bd3
RH
7129};
7130
957e6155
PM
7131static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7132{
7133 /* Read the high 32 bits of the current CCSIDR */
7134 return extract64(ccsidr_read(env, ri), 32, 32);
7135}
7136
7137static const ARMCPRegInfo ccsidr2_reginfo[] = {
7138 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7139 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7140 .access = PL1_R,
7141 .accessfn = access_aa64_tid2,
7142 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
957e6155
PM
7143};
7144
6a4ef4e5
MZ
7145static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7146 bool isread)
7147{
7148 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7149 return CP_ACCESS_TRAP_EL2;
7150 }
7151
7152 return CP_ACCESS_OK;
7153}
7154
7155static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7156 bool isread)
7157{
7158 if (arm_feature(env, ARM_FEATURE_V8)) {
7159 return access_aa64_tid3(env, ri, isread);
7160 }
7161
7162 return CP_ACCESS_OK;
7163}
7164
f96f3d5f
MZ
7165static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7166 bool isread)
7167{
7168 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7169 return CP_ACCESS_TRAP_EL2;
7170 }
7171
7172 return CP_ACCESS_OK;
7173}
7174
8e228c9e
PM
7175static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7176 const ARMCPRegInfo *ri, bool isread)
7177{
7178 /*
7179 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7180 * in v7A, not in v8A.
7181 */
7182 if (!arm_feature(env, ARM_FEATURE_V8) &&
7183 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7184 (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7185 return CP_ACCESS_TRAP_EL2;
7186 }
7187 return CP_ACCESS_OK;
7188}
7189
f96f3d5f
MZ
7190static const ARMCPRegInfo jazelle_regs[] = {
7191 { .name = "JIDR",
7192 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7193 .access = PL1_R, .accessfn = access_jazelle,
7194 .type = ARM_CP_CONST, .resetvalue = 0 },
7195 { .name = "JOSCR",
7196 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7197 .accessfn = access_joscr_jmcr,
f96f3d5f
MZ
7198 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7199 { .name = "JMCR",
7200 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7201 .accessfn = access_joscr_jmcr,
f96f3d5f 7202 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
f96f3d5f
MZ
7203};
7204
52d18727
RH
7205static const ARMCPRegInfo contextidr_el2 = {
7206 .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7207 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7208 .access = PL2_RW,
7209 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7210};
7211
e2a1a461 7212static const ARMCPRegInfo vhe_reginfo[] = {
ed30da8e
RH
7213 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7214 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7215 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7216 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8c94b071
RH
7217#ifndef CONFIG_USER_ONLY
7218 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7219 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7220 .fieldoffset =
7221 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7222 .type = ARM_CP_IO, .access = PL2_RW,
7223 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7224 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7225 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7226 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7227 .resetfn = gt_hv_timer_reset,
7228 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7229 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7230 .type = ARM_CP_IO,
7231 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7232 .access = PL2_RW,
7233 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7234 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
bb5972e4
RH
7235 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7236 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7237 .type = ARM_CP_IO | ARM_CP_ALIAS,
7238 .access = PL2_RW, .accessfn = e2h_access,
7239 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7240 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7241 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7242 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7243 .type = ARM_CP_IO | ARM_CP_ALIAS,
7244 .access = PL2_RW, .accessfn = e2h_access,
7245 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7246 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7247 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7248 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7249 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7250 .access = PL2_RW, .accessfn = e2h_access,
7251 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7252 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7253 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7254 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7255 .access = PL2_RW, .accessfn = e2h_access,
7256 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7257 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7258 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7259 .type = ARM_CP_IO | ARM_CP_ALIAS,
7260 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7261 .access = PL2_RW, .accessfn = e2h_access,
7262 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7263 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7264 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7265 .type = ARM_CP_IO | ARM_CP_ALIAS,
7266 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7267 .access = PL2_RW, .accessfn = e2h_access,
7268 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8c94b071 7269#endif
e2a1a461
RH
7270};
7271
04b07d29
RH
7272#ifndef CONFIG_USER_ONLY
7273static const ARMCPRegInfo ats1e1_reginfo[] = {
7274 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
7275 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7276 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7277 .writefn = ats_write64 },
7278 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
7279 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7280 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7281 .writefn = ats_write64 },
04b07d29
RH
7282};
7283
7284static const ARMCPRegInfo ats1cp_reginfo[] = {
7285 { .name = "ATS1CPRP",
7286 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7287 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7288 .writefn = ats_write },
7289 { .name = "ATS1CPWP",
7290 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7291 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7292 .writefn = ats_write },
04b07d29
RH
7293};
7294#endif
7295
f6287c24
PM
7296/*
7297 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7298 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7299 * is non-zero, which is never for ARMv7, optionally in ARMv8
7300 * and mandatorily for ARMv8.2 and up.
7301 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7302 * implementation is RAZ/WI we can ignore this detail, as we
7303 * do for ACTLR.
7304 */
7305static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7306 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7307 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
99602377
RH
7308 .access = PL1_RW, .accessfn = access_tacr,
7309 .type = ARM_CP_CONST, .resetvalue = 0 },
f6287c24
PM
7310 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7311 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7312 .access = PL2_RW, .type = ARM_CP_CONST,
7313 .resetvalue = 0 },
f6287c24
PM
7314};
7315
2ceb98c0
PM
7316void register_cp_regs_for_features(ARMCPU *cpu)
7317{
7318 /* Register all the coprocessor registers based on feature bits */
7319 CPUARMState *env = &cpu->env;
7320 if (arm_feature(env, ARM_FEATURE_M)) {
7321 /* M profile has no coprocessor registers */
7322 return;
7323 }
7324
e9aa6c21 7325 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6
PM
7326 if (!arm_feature(env, ARM_FEATURE_V8)) {
7327 /* Must go early as it is full of wildcards that may be
7328 * overridden by later definitions.
7329 */
7330 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7331 }
7332
7d57f408 7333 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
7334 /* The ID registers all have impdef reset values */
7335 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
7336 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7337 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7338 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7339 .accessfn = access_aa32_tid3,
8a130a7b 7340 .resetvalue = cpu->isar.id_pfr0 },
96a8b92e
PM
7341 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7342 * the value of the GIC field until after we define these regs.
7343 */
0ff644a7
PM
7344 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7345 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e 7346 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 7347 .accessfn = access_aa32_tid3,
96a8b92e
PM
7348 .readfn = id_pfr1_read,
7349 .writefn = arm_cp_write_ignore },
0ff644a7
PM
7350 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7351 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7352 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7353 .accessfn = access_aa32_tid3,
a6179538 7354 .resetvalue = cpu->isar.id_dfr0 },
0ff644a7
PM
7355 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7356 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7357 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7358 .accessfn = access_aa32_tid3,
8515a092 7359 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
7360 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7361 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7362 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7363 .accessfn = access_aa32_tid3,
10054016 7364 .resetvalue = cpu->isar.id_mmfr0 },
0ff644a7
PM
7365 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7366 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7367 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7368 .accessfn = access_aa32_tid3,
10054016 7369 .resetvalue = cpu->isar.id_mmfr1 },
0ff644a7
PM
7370 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7371 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7372 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7373 .accessfn = access_aa32_tid3,
10054016 7374 .resetvalue = cpu->isar.id_mmfr2 },
0ff644a7
PM
7375 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7376 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7377 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7378 .accessfn = access_aa32_tid3,
10054016 7379 .resetvalue = cpu->isar.id_mmfr3 },
0ff644a7
PM
7380 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7381 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7382 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7383 .accessfn = access_aa32_tid3,
47576b94 7384 .resetvalue = cpu->isar.id_isar0 },
0ff644a7
PM
7385 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7386 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7387 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7388 .accessfn = access_aa32_tid3,
47576b94 7389 .resetvalue = cpu->isar.id_isar1 },
0ff644a7
PM
7390 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7391 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7392 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7393 .accessfn = access_aa32_tid3,
47576b94 7394 .resetvalue = cpu->isar.id_isar2 },
0ff644a7
PM
7395 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7396 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7397 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7398 .accessfn = access_aa32_tid3,
47576b94 7399 .resetvalue = cpu->isar.id_isar3 },
0ff644a7
PM
7400 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7401 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7402 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7403 .accessfn = access_aa32_tid3,
47576b94 7404 .resetvalue = cpu->isar.id_isar4 },
0ff644a7
PM
7405 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7406 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7407 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7408 .accessfn = access_aa32_tid3,
47576b94 7409 .resetvalue = cpu->isar.id_isar5 },
e20d84c1
PM
7410 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7411 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7412 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7413 .accessfn = access_aa32_tid3,
10054016 7414 .resetvalue = cpu->isar.id_mmfr4 },
802abf40 7415 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7416 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7417 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7418 .accessfn = access_aa32_tid3,
47576b94 7419 .resetvalue = cpu->isar.id_isar6 },
8515a092
PM
7420 };
7421 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
7422 define_arm_cp_regs(cpu, v6_cp_reginfo);
7423 } else {
7424 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7425 }
4d31c596
PM
7426 if (arm_feature(env, ARM_FEATURE_V6K)) {
7427 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7428 }
5e5cf9e3 7429 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 7430 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
7431 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
7432 }
327dd510
AL
7433 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7434 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7435 }
e9aa6c21 7436 if (arm_feature(env, ARM_FEATURE_V7)) {
776d4e5c 7437 ARMCPRegInfo clidr = {
7da845b0
PM
7438 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7439 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
630fcd4d
MZ
7440 .access = PL1_R, .type = ARM_CP_CONST,
7441 .accessfn = access_aa64_tid2,
7442 .resetvalue = cpu->clidr
776d4e5c 7443 };
776d4e5c 7444 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 7445 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 7446 define_debug_regs(cpu);
24183fb6 7447 define_pmu_regs(cpu);
7d57f408
PM
7448 } else {
7449 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 7450 }
b0d2b7d0 7451 if (arm_feature(env, ARM_FEATURE_V8)) {
dde4d028
PM
7452 /*
7453 * v8 ID registers, which all have impdef reset values.
e20d84c1
PM
7454 * Note that within the ID register ranges the unused slots
7455 * must all RAZ, not UNDEF; future architecture versions may
7456 * define new registers here.
dde4d028
PM
7457 * ID registers which are AArch64 views of the AArch32 ID registers
7458 * which already existed in v6 and v7 are handled elsewhere,
7459 * in v6_idregs[].
e20d84c1 7460 */
dde4d028 7461 int i;
e60cef86 7462 ARMCPRegInfo v8_idregs[] = {
976b99b6
AB
7463 /*
7464 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7465 * emulation because we don't know the right value for the
7466 * GIC field until after we define these regs.
96a8b92e 7467 */
e60cef86
PM
7468 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7469 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
976b99b6
AB
7470 .access = PL1_R,
7471#ifdef CONFIG_USER_ONLY
7472 .type = ARM_CP_CONST,
7473 .resetvalue = cpu->isar.id_aa64pfr0
7474#else
7475 .type = ARM_CP_NO_RAW,
6a4ef4e5 7476 .accessfn = access_aa64_tid3,
96a8b92e 7477 .readfn = id_aa64pfr0_read,
976b99b6
AB
7478 .writefn = arm_cp_write_ignore
7479#endif
7480 },
e60cef86
PM
7481 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7482 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7483 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7484 .accessfn = access_aa64_tid3,
47576b94 7485 .resetvalue = cpu->isar.id_aa64pfr1},
e20d84c1
PM
7486 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7487 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7488 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7489 .accessfn = access_aa64_tid3,
e20d84c1
PM
7490 .resetvalue = 0 },
7491 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7492 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7493 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7494 .accessfn = access_aa64_tid3,
e20d84c1 7495 .resetvalue = 0 },
9516d772 7496 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7497 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7498 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7499 .accessfn = access_aa64_tid3,
2dc10fa2 7500 .resetvalue = cpu->isar.id_aa64zfr0 },
414c54d5 7501 { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7502 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7503 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7504 .accessfn = access_aa64_tid3,
414c54d5 7505 .resetvalue = cpu->isar.id_aa64smfr0 },
e20d84c1
PM
7506 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7507 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7508 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7509 .accessfn = access_aa64_tid3,
e20d84c1
PM
7510 .resetvalue = 0 },
7511 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7512 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7513 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7514 .accessfn = access_aa64_tid3,
e20d84c1 7515 .resetvalue = 0 },
e60cef86
PM
7516 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7517 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7518 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7519 .accessfn = access_aa64_tid3,
2a609df8 7520 .resetvalue = cpu->isar.id_aa64dfr0 },
e60cef86
PM
7521 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7522 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7523 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7524 .accessfn = access_aa64_tid3,
2a609df8 7525 .resetvalue = cpu->isar.id_aa64dfr1 },
e20d84c1
PM
7526 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7527 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7528 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7529 .accessfn = access_aa64_tid3,
e20d84c1
PM
7530 .resetvalue = 0 },
7531 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7532 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7533 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7534 .accessfn = access_aa64_tid3,
e20d84c1 7535 .resetvalue = 0 },
e60cef86
PM
7536 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7537 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7538 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7539 .accessfn = access_aa64_tid3,
e60cef86
PM
7540 .resetvalue = cpu->id_aa64afr0 },
7541 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7542 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7543 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7544 .accessfn = access_aa64_tid3,
e60cef86 7545 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
7546 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7547 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7548 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7549 .accessfn = access_aa64_tid3,
e20d84c1
PM
7550 .resetvalue = 0 },
7551 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7552 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7553 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7554 .accessfn = access_aa64_tid3,
e20d84c1 7555 .resetvalue = 0 },
e60cef86
PM
7556 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7557 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
7558 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7559 .accessfn = access_aa64_tid3,
47576b94 7560 .resetvalue = cpu->isar.id_aa64isar0 },
e60cef86
PM
7561 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
7562 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
7563 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7564 .accessfn = access_aa64_tid3,
47576b94 7565 .resetvalue = cpu->isar.id_aa64isar1 },
e20d84c1
PM
7566 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7567 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
7568 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7569 .accessfn = access_aa64_tid3,
e20d84c1
PM
7570 .resetvalue = 0 },
7571 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7572 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
7573 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7574 .accessfn = access_aa64_tid3,
e20d84c1
PM
7575 .resetvalue = 0 },
7576 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7577 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
7578 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7579 .accessfn = access_aa64_tid3,
e20d84c1
PM
7580 .resetvalue = 0 },
7581 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7582 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
7583 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7584 .accessfn = access_aa64_tid3,
e20d84c1
PM
7585 .resetvalue = 0 },
7586 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7587 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
7588 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7589 .accessfn = access_aa64_tid3,
e20d84c1
PM
7590 .resetvalue = 0 },
7591 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7592 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
7593 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7594 .accessfn = access_aa64_tid3,
e20d84c1 7595 .resetvalue = 0 },
e60cef86
PM
7596 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
7597 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
7598 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7599 .accessfn = access_aa64_tid3,
3dc91ddb 7600 .resetvalue = cpu->isar.id_aa64mmfr0 },
e60cef86
PM
7601 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
7602 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
7603 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7604 .accessfn = access_aa64_tid3,
3dc91ddb 7605 .resetvalue = cpu->isar.id_aa64mmfr1 },
64761e10 7606 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
7607 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
7608 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7609 .accessfn = access_aa64_tid3,
64761e10 7610 .resetvalue = cpu->isar.id_aa64mmfr2 },
e20d84c1
PM
7611 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7612 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
7613 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7614 .accessfn = access_aa64_tid3,
e20d84c1
PM
7615 .resetvalue = 0 },
7616 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7617 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
7618 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7619 .accessfn = access_aa64_tid3,
e20d84c1
PM
7620 .resetvalue = 0 },
7621 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7622 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
7623 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7624 .accessfn = access_aa64_tid3,
e20d84c1
PM
7625 .resetvalue = 0 },
7626 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7627 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
7628 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7629 .accessfn = access_aa64_tid3,
e20d84c1
PM
7630 .resetvalue = 0 },
7631 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7632 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
7633 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7634 .accessfn = access_aa64_tid3,
e20d84c1 7635 .resetvalue = 0 },
a50c0f51
PM
7636 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
7637 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7638 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7639 .accessfn = access_aa64_tid3,
47576b94 7640 .resetvalue = cpu->isar.mvfr0 },
a50c0f51
PM
7641 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
7642 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7643 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7644 .accessfn = access_aa64_tid3,
47576b94 7645 .resetvalue = cpu->isar.mvfr1 },
a50c0f51
PM
7646 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
7647 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7648 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7649 .accessfn = access_aa64_tid3,
47576b94 7650 .resetvalue = cpu->isar.mvfr2 },
dde4d028
PM
7651 /*
7652 * "0, c0, c3, {0,1,2}" are the encodings corresponding to
7653 * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
7654 * as RAZ, since it is in the "reserved for future ID
7655 * registers, RAZ" part of the AArch32 encoding space.
7656 */
7657 { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
7658 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
7659 .access = PL1_R, .type = ARM_CP_CONST,
7660 .accessfn = access_aa64_tid3,
7661 .resetvalue = 0 },
7662 { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
7663 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
7664 .access = PL1_R, .type = ARM_CP_CONST,
7665 .accessfn = access_aa64_tid3,
7666 .resetvalue = 0 },
7667 { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
7668 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
7669 .access = PL1_R, .type = ARM_CP_CONST,
7670 .accessfn = access_aa64_tid3,
7671 .resetvalue = 0 },
7672 /*
7673 * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
7674 * they're also RAZ for AArch64, and in v8 are gradually
7675 * being filled with AArch64-view-of-AArch32-ID-register
7676 * for new ID registers.
7677 */
7678 { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7679 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
7680 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7681 .accessfn = access_aa64_tid3,
e20d84c1 7682 .resetvalue = 0 },
1d51bc96 7683 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7684 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
7685 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7686 .accessfn = access_aa64_tid3,
1d51bc96 7687 .resetvalue = cpu->isar.id_pfr2 },
d22c5649 7688 { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7689 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
7690 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7691 .accessfn = access_aa64_tid3,
d22c5649 7692 .resetvalue = cpu->isar.id_dfr1 },
32957aad 7693 { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7694 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
7695 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7696 .accessfn = access_aa64_tid3,
32957aad 7697 .resetvalue = cpu->isar.id_mmfr5 },
dde4d028 7698 { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
7699 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
7700 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7701 .accessfn = access_aa64_tid3,
e20d84c1 7702 .resetvalue = 0 },
4054bfa9
AF
7703 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
7704 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
7705 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7706 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
4054bfa9
AF
7707 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
7708 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
7709 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7710 .resetvalue = cpu->pmceid0 },
7711 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
7712 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
7713 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
cad86737 7714 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
4054bfa9
AF
7715 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
7716 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
7717 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7718 .resetvalue = cpu->pmceid1 },
e60cef86 7719 };
6c5c0fec 7720#ifdef CONFIG_USER_ONLY
10b0220e 7721 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
6c5c0fec
AB
7722 { .name = "ID_AA64PFR0_EL1",
7723 .exported_bits = 0x000f000f00ff0000,
7724 .fixed_bits = 0x0000000000000011 },
7725 { .name = "ID_AA64PFR1_EL1",
7726 .exported_bits = 0x00000000000000f0 },
d040242e
AB
7727 { .name = "ID_AA64PFR*_EL1_RESERVED",
7728 .is_glob = true },
6c5c0fec
AB
7729 { .name = "ID_AA64ZFR0_EL1" },
7730 { .name = "ID_AA64MMFR0_EL1",
7731 .fixed_bits = 0x00000000ff000000 },
7732 { .name = "ID_AA64MMFR1_EL1" },
d040242e
AB
7733 { .name = "ID_AA64MMFR*_EL1_RESERVED",
7734 .is_glob = true },
6c5c0fec
AB
7735 { .name = "ID_AA64DFR0_EL1",
7736 .fixed_bits = 0x0000000000000006 },
7737 { .name = "ID_AA64DFR1_EL1" },
d040242e
AB
7738 { .name = "ID_AA64DFR*_EL1_RESERVED",
7739 .is_glob = true },
7740 { .name = "ID_AA64AFR*",
7741 .is_glob = true },
6c5c0fec
AB
7742 { .name = "ID_AA64ISAR0_EL1",
7743 .exported_bits = 0x00fffffff0fffff0 },
7744 { .name = "ID_AA64ISAR1_EL1",
7745 .exported_bits = 0x000000f0ffffffff },
d040242e
AB
7746 { .name = "ID_AA64ISAR*_EL1_RESERVED",
7747 .is_glob = true },
6c5c0fec
AB
7748 };
7749 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
7750#endif
be8e8128
GB
7751 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
7752 if (!arm_feature(env, ARM_FEATURE_EL3) &&
7753 !arm_feature(env, ARM_FEATURE_EL2)) {
7754 ARMCPRegInfo rvbar = {
7755 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
7756 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
7757 .access = PL1_R,
7758 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
be8e8128
GB
7759 };
7760 define_one_arm_cp_reg(cpu, &rvbar);
7761 }
e60cef86 7762 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0 7763 define_arm_cp_regs(cpu, v8_cp_reginfo);
dde4d028
PM
7764
7765 for (i = 4; i < 16; i++) {
7766 /*
7767 * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
7768 * For pre-v8 cores there are RAZ patterns for these in
7769 * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
7770 * v8 extends the "must RAZ" part of the ID register space
7771 * to also cover c0, 0, c{8-15}, {0-7}.
7772 * These are STATE_AA32 because in the AArch64 sysreg space
7773 * c4-c7 is where the AArch64 ID registers live (and we've
7774 * already defined those in v8_idregs[]), and c8-c15 are not
7775 * "must RAZ" for AArch64.
7776 */
7777 g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
7778 ARMCPRegInfo v8_aa32_raz_idregs = {
7779 .name = name,
7780 .state = ARM_CP_STATE_AA32,
7781 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
7782 .access = PL1_R, .type = ARM_CP_CONST,
7783 .accessfn = access_aa64_tid3,
7784 .resetvalue = 0 };
7785 define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
7786 }
b0d2b7d0 7787 }
99a90811
RH
7788
7789 /*
7790 * Register the base EL2 cpregs.
7791 * Pre v8, these registers are implemented only as part of the
7792 * Virtualization Extensions (EL2 present). Beginning with v8,
7793 * if EL2 is missing but EL3 is enabled, mostly these become
7794 * RES0 from EL3, with some specific exceptions.
7795 */
7796 if (arm_feature(env, ARM_FEATURE_EL2)
7797 || (arm_feature(env, ARM_FEATURE_EL3)
7798 && arm_feature(env, ARM_FEATURE_V8))) {
f0d574d6 7799 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
7800 ARMCPRegInfo vpidr_regs[] = {
7801 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
7802 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7803 .access = PL2_RW, .accessfn = access_el3_aa32ns,
696ba377
RH
7804 .resetvalue = cpu->midr,
7805 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
36476562 7806 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
7807 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
7808 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
7809 .access = PL2_RW, .resetvalue = cpu->midr,
696ba377 7810 .type = ARM_CP_EL3_NO_EL2_C_NZ,
731de9e6 7811 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
7812 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
7813 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
7814 .access = PL2_RW, .accessfn = access_el3_aa32ns,
696ba377
RH
7815 .resetvalue = vmpidr_def,
7816 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
36476562 7817 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
7818 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
7819 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
696ba377
RH
7820 .access = PL2_RW, .resetvalue = vmpidr_def,
7821 .type = ARM_CP_EL3_NO_EL2_C_NZ,
f0d574d6 7822 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6 7823 };
24526bb9
PM
7824 /*
7825 * The only field of MDCR_EL2 that has a defined architectural reset
7826 * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
7827 */
7828 ARMCPRegInfo mdcr_el2 = {
7f4fbfb5 7829 .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
24526bb9 7830 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
01765386 7831 .writefn = mdcr_el2_write,
24526bb9
PM
7832 .access = PL2_RW, .resetvalue = pmu_num_counters(env),
7833 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
7834 };
7835 define_one_arm_cp_reg(cpu, &mdcr_el2);
731de9e6 7836 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 7837 define_arm_cp_regs(cpu, el2_cp_reginfo);
ce4afed8
PM
7838 if (arm_feature(env, ARM_FEATURE_V8)) {
7839 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
7840 }
e9152ee9
RDC
7841 if (cpu_isar_feature(aa64_sel2, cpu)) {
7842 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
7843 }
be8e8128
GB
7844 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
7845 if (!arm_feature(env, ARM_FEATURE_EL3)) {
7846 ARMCPRegInfo rvbar = {
7847 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
7848 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
7849 .access = PL2_R,
7850 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
be8e8128
GB
7851 };
7852 define_one_arm_cp_reg(cpu, &rvbar);
7853 }
3b685ba7 7854 }
99a90811
RH
7855
7856 /* Register the base EL3 cpregs. */
81547d66 7857 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 7858 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
7859 ARMCPRegInfo el3_regs[] = {
7860 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
7861 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
7862 .access = PL3_R,
7863 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
7864 },
e24fdd23
PM
7865 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
7866 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
7867 .access = PL3_RW,
7868 .raw_writefn = raw_write, .writefn = sctlr_write,
7869 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
7870 .resetvalue = cpu->reset_sctlr },
be8e8128 7871 };
e24fdd23
PM
7872
7873 define_arm_cp_regs(cpu, el3_regs);
81547d66 7874 }
2f027fc5
PM
7875 /* The behaviour of NSACR is sufficiently various that we don't
7876 * try to describe it in a single reginfo:
7877 * if EL3 is 64 bit, then trap to EL3 from S EL1,
7878 * reads as constant 0xc00 from NS EL1 and NS EL2
7879 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
7880 * if v7 without EL3, register doesn't exist
7881 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
7882 */
7883 if (arm_feature(env, ARM_FEATURE_EL3)) {
7884 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
10b0220e 7885 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
7886 .name = "NSACR", .type = ARM_CP_CONST,
7887 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7888 .access = PL1_RW, .accessfn = nsacr_access,
7889 .resetvalue = 0xc00
7890 };
7891 define_one_arm_cp_reg(cpu, &nsacr);
7892 } else {
10b0220e 7893 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
7894 .name = "NSACR",
7895 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7896 .access = PL3_RW | PL1_R,
7897 .resetvalue = 0,
7898 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
7899 };
7900 define_one_arm_cp_reg(cpu, &nsacr);
7901 }
7902 } else {
7903 if (arm_feature(env, ARM_FEATURE_V8)) {
10b0220e 7904 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
7905 .name = "NSACR", .type = ARM_CP_CONST,
7906 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
7907 .access = PL1_R,
7908 .resetvalue = 0xc00
7909 };
7910 define_one_arm_cp_reg(cpu, &nsacr);
7911 }
7912 }
7913
452a0955 7914 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
7915 if (arm_feature(env, ARM_FEATURE_V6)) {
7916 /* PMSAv6 not implemented */
7917 assert(arm_feature(env, ARM_FEATURE_V7));
7918 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
7919 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
7920 } else {
7921 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
7922 }
18032bec 7923 } else {
8e5d75c9 7924 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec 7925 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4036b7d1
PM
7926 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
7927 if (cpu_isar_feature(aa32_hpd, cpu)) {
ab638a32
RH
7928 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
7929 }
18032bec 7930 }
c326b979
PM
7931 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
7932 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
7933 }
6cc7a3ae
PM
7934 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
7935 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
7936 }
4a501606
PM
7937 if (arm_feature(env, ARM_FEATURE_VAPA)) {
7938 define_arm_cp_regs(cpu, vapa_cp_reginfo);
7939 }
c4804214
PM
7940 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
7941 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
7942 }
7943 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
7944 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
7945 }
7946 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
7947 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
7948 }
18032bec
PM
7949 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
7950 define_arm_cp_regs(cpu, omap_cp_reginfo);
7951 }
34f90529
PM
7952 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
7953 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
7954 }
1047b9d7
PM
7955 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
7956 define_arm_cp_regs(cpu, xscale_cp_reginfo);
7957 }
7958 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
7959 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
7960 }
7ac681cf
PM
7961 if (arm_feature(env, ARM_FEATURE_LPAE)) {
7962 define_arm_cp_regs(cpu, lpae_cp_reginfo);
7963 }
873b73c0 7964 if (cpu_isar_feature(aa32_jazelle, cpu)) {
f96f3d5f
MZ
7965 define_arm_cp_regs(cpu, jazelle_regs);
7966 }
7884849c
PM
7967 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
7968 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
7969 * be read-only (ie write causes UNDEF exception).
7970 */
7971 {
00a29f3d
PM
7972 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
7973 /* Pre-v8 MIDR space.
7974 * Note that the MIDR isn't a simple constant register because
7884849c
PM
7975 * of the TI925 behaviour where writes to another register can
7976 * cause the MIDR value to change.
97ce8d61
PC
7977 *
7978 * Unimplemented registers in the c15 0 0 0 space default to
7979 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
7980 * and friends override accordingly.
7884849c
PM
7981 */
7982 { .name = "MIDR",
97ce8d61 7983 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 7984 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 7985 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 7986 .readfn = midr_read,
97ce8d61
PC
7987 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
7988 .type = ARM_CP_OVERRIDE },
7884849c
PM
7989 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
7990 { .name = "DUMMY",
7991 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
7992 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7993 { .name = "DUMMY",
7994 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
7995 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7996 { .name = "DUMMY",
7997 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
7998 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7999 { .name = "DUMMY",
8000 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8001 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8002 { .name = "DUMMY",
8003 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8004 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7884849c 8005 };
00a29f3d 8006 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
8007 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8008 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6
EI
8009 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8010 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8011 .readfn = midr_read },
ac00c79f
SF
8012 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
8013 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8014 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8015 .access = PL1_R, .resetvalue = cpu->midr },
8016 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8017 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8018 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
8019 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8020 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
93fbc983
MZ
8021 .access = PL1_R,
8022 .accessfn = access_aa64_tid1,
8023 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d
PM
8024 };
8025 ARMCPRegInfo id_cp_reginfo[] = {
8026 /* These are common to v8 and pre-v8 */
8027 { .name = "CTR",
8028 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
630fcd4d
MZ
8029 .access = PL1_R, .accessfn = ctr_el0_access,
8030 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
00a29f3d
PM
8031 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8032 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8033 .access = PL0_R, .accessfn = ctr_el0_access,
8034 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8035 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8036 { .name = "TCMTR",
8037 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
93fbc983
MZ
8038 .access = PL1_R,
8039 .accessfn = access_aa32_tid1,
8040 .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d 8041 };
8085ce63
PC
8042 /* TLBTR is specific to VMSA */
8043 ARMCPRegInfo id_tlbtr_reginfo = {
8044 .name = "TLBTR",
8045 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
93fbc983
MZ
8046 .access = PL1_R,
8047 .accessfn = access_aa32_tid1,
8048 .type = ARM_CP_CONST, .resetvalue = 0,
8085ce63 8049 };
3281af81
PC
8050 /* MPUIR is specific to PMSA V6+ */
8051 ARMCPRegInfo id_mpuir_reginfo = {
8052 .name = "MPUIR",
8053 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8054 .access = PL1_R, .type = ARM_CP_CONST,
8055 .resetvalue = cpu->pmsav7_dregion << 8
8056 };
10b0220e 8057 static const ARMCPRegInfo crn0_wi_reginfo = {
7884849c
PM
8058 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8059 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8060 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8061 };
6c5c0fec 8062#ifdef CONFIG_USER_ONLY
10b0220e 8063 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
6c5c0fec
AB
8064 { .name = "MIDR_EL1",
8065 .exported_bits = 0x00000000ffffffff },
8066 { .name = "REVIDR_EL1" },
6c5c0fec
AB
8067 };
8068 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8069#endif
7884849c
PM
8070 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8071 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5809ac57 8072 size_t i;
7884849c 8073 /* Register the blanket "writes ignored" value first to cover the
a703eda1
PC
8074 * whole space. Then update the specific ID registers to allow write
8075 * access, so that they ignore writes rather than causing them to
8076 * UNDEF.
7884849c
PM
8077 */
8078 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
5809ac57
RH
8079 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8080 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
00a29f3d 8081 }
5809ac57
RH
8082 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8083 id_cp_reginfo[i].access = PL1_RW;
7884849c 8084 }
10006112 8085 id_mpuir_reginfo.access = PL1_RW;
3281af81 8086 id_tlbtr_reginfo.access = PL1_RW;
7884849c 8087 }
00a29f3d
PM
8088 if (arm_feature(env, ARM_FEATURE_V8)) {
8089 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8090 } else {
8091 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8092 }
a703eda1 8093 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 8094 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 8095 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
3281af81
PC
8096 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8097 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 8098 }
7884849c
PM
8099 }
8100
97ce8d61 8101 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
52264166
AB
8102 ARMCPRegInfo mpidr_cp_reginfo[] = {
8103 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8104 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8105 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
52264166
AB
8106 };
8107#ifdef CONFIG_USER_ONLY
10b0220e 8108 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
52264166
AB
8109 { .name = "MPIDR_EL1",
8110 .fixed_bits = 0x0000000080000000 },
52264166
AB
8111 };
8112 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8113#endif
97ce8d61
PC
8114 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8115 }
8116
2771db27 8117 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
8118 ARMCPRegInfo auxcr_reginfo[] = {
8119 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8120 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
99602377
RH
8121 .access = PL1_RW, .accessfn = access_tacr,
8122 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
834a6c69
PM
8123 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8124 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8125 .access = PL2_RW, .type = ARM_CP_CONST,
8126 .resetvalue = 0 },
8127 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8128 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8129 .access = PL3_RW, .type = ARM_CP_CONST,
8130 .resetvalue = 0 },
2771db27 8131 };
834a6c69 8132 define_arm_cp_regs(cpu, auxcr_reginfo);
f6287c24
PM
8133 if (cpu_isar_feature(aa32_ac2, cpu)) {
8134 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
0e0456ab 8135 }
2771db27
PM
8136 }
8137
d8ba780b 8138 if (arm_feature(env, ARM_FEATURE_CBAR)) {
d56974af
LM
8139 /*
8140 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8141 * There are two flavours:
8142 * (1) older 32-bit only cores have a simple 32-bit CBAR
8143 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8144 * 32-bit register visible to AArch32 at a different encoding
8145 * to the "flavour 1" register and with the bits rearranged to
8146 * be able to squash a 64-bit address into the 32-bit view.
8147 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8148 * in future if we support AArch32-only configs of some of the
8149 * AArch64 cores we might need to add a specific feature flag
8150 * to indicate cores with "flavour 2" CBAR.
8151 */
f318cec6
PM
8152 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8153 /* 32 bit view is [31:18] 0...0 [43:32]. */
8154 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8155 | extract64(cpu->reset_cbar, 32, 12);
8156 ARMCPRegInfo cbar_reginfo[] = {
8157 { .name = "CBAR",
8158 .type = ARM_CP_CONST,
d56974af
LM
8159 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8160 .access = PL1_R, .resetvalue = cbar32 },
f318cec6
PM
8161 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8162 .type = ARM_CP_CONST,
8163 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
d56974af 8164 .access = PL1_R, .resetvalue = cpu->reset_cbar },
f318cec6
PM
8165 };
8166 /* We don't implement a r/w 64 bit CBAR currently */
8167 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8168 define_arm_cp_regs(cpu, cbar_reginfo);
8169 } else {
8170 ARMCPRegInfo cbar = {
8171 .name = "CBAR",
8172 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8173 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
8174 .fieldoffset = offsetof(CPUARMState,
8175 cp15.c15_config_base_address)
8176 };
8177 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8178 cbar.access = PL1_R;
8179 cbar.fieldoffset = 0;
8180 cbar.type = ARM_CP_CONST;
8181 }
8182 define_one_arm_cp_reg(cpu, &cbar);
8183 }
d8ba780b
PC
8184 }
8185
91db4642 8186 if (arm_feature(env, ARM_FEATURE_VBAR)) {
10b0220e 8187 static const ARMCPRegInfo vbar_cp_reginfo[] = {
91db4642
CLG
8188 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8189 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8190 .access = PL1_RW, .writefn = vbar_write,
8191 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8192 offsetof(CPUARMState, cp15.vbar_ns) },
8193 .resetvalue = 0 },
91db4642
CLG
8194 };
8195 define_arm_cp_regs(cpu, vbar_cp_reginfo);
8196 }
8197
2771db27
PM
8198 /* Generic registers whose values depend on the implementation */
8199 {
8200 ARMCPRegInfo sctlr = {
5ebafdf3 8201 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9 8202 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
84929218 8203 .access = PL1_RW, .accessfn = access_tvm_trvm,
137feaa9
FA
8204 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8205 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
8206 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8207 .raw_writefn = raw_write,
2771db27
PM
8208 };
8209 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8210 /* Normally we would always end the TB on an SCTLR write, but Linux
8211 * arch/arm/mach-pxa/sleep.S expects two instructions following
8212 * an MMU enable to execute from cache. Imitate this behaviour.
8213 */
8214 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8215 }
8216 define_one_arm_cp_reg(cpu, &sctlr);
8217 }
5be5e8ed 8218
2d7137c1 8219 if (cpu_isar_feature(aa64_lor, cpu)) {
2d7137c1
RH
8220 define_arm_cp_regs(cpu, lor_reginfo);
8221 }
220f508f
RH
8222 if (cpu_isar_feature(aa64_pan, cpu)) {
8223 define_one_arm_cp_reg(cpu, &pan_reginfo);
8224 }
04b07d29
RH
8225#ifndef CONFIG_USER_ONLY
8226 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8227 define_arm_cp_regs(cpu, ats1e1_reginfo);
8228 }
8229 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8230 define_arm_cp_regs(cpu, ats1cp_reginfo);
8231 }
8232#endif
9eeb7a1c
RH
8233 if (cpu_isar_feature(aa64_uao, cpu)) {
8234 define_one_arm_cp_reg(cpu, &uao_reginfo);
8235 }
2d7137c1 8236
dc8b1853
RC
8237 if (cpu_isar_feature(aa64_dit, cpu)) {
8238 define_one_arm_cp_reg(cpu, &dit_reginfo);
8239 }
f2f68a78
RC
8240 if (cpu_isar_feature(aa64_ssbs, cpu)) {
8241 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8242 }
58e93b48
RH
8243 if (cpu_isar_feature(any_ras, cpu)) {
8244 define_arm_cp_regs(cpu, minimal_ras_reginfo);
8245 }
dc8b1853 8246
52d18727
RH
8247 if (cpu_isar_feature(aa64_vh, cpu) ||
8248 cpu_isar_feature(aa64_debugv8p2, cpu)) {
8249 define_one_arm_cp_reg(cpu, &contextidr_el2);
8250 }
e2a1a461
RH
8251 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8252 define_arm_cp_regs(cpu, vhe_reginfo);
8253 }
8254
cd208a1c 8255 if (cpu_isar_feature(aa64_sve, cpu)) {
60360d82 8256 define_arm_cp_regs(cpu, zcr_reginfo);
5be5e8ed 8257 }
967aa94f 8258
5814d587
RH
8259 if (cpu_isar_feature(aa64_hcx, cpu)) {
8260 define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8261 }
8262
967aa94f 8263#ifdef TARGET_AARCH64
9e5ec745
RH
8264 if (cpu_isar_feature(aa64_sme, cpu)) {
8265 define_arm_cp_regs(cpu, sme_reginfo);
8266 }
967aa94f
RH
8267 if (cpu_isar_feature(aa64_pauth, cpu)) {
8268 define_arm_cp_regs(cpu, pauth_reginfo);
8269 }
de390645
RH
8270 if (cpu_isar_feature(aa64_rndr, cpu)) {
8271 define_arm_cp_regs(cpu, rndr_reginfo);
8272 }
84940ed8
RC
8273 if (cpu_isar_feature(aa64_tlbirange, cpu)) {
8274 define_arm_cp_regs(cpu, tlbirange_reginfo);
8275 }
7113d618
RC
8276 if (cpu_isar_feature(aa64_tlbios, cpu)) {
8277 define_arm_cp_regs(cpu, tlbios_reginfo);
8278 }
0d57b499
BM
8279#ifndef CONFIG_USER_ONLY
8280 /* Data Cache clean instructions up to PoP */
8281 if (cpu_isar_feature(aa64_dcpop, cpu)) {
8282 define_one_arm_cp_reg(cpu, dcpop_reg);
8283
8284 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8285 define_one_arm_cp_reg(cpu, dcpodp_reg);
8286 }
8287 }
8288#endif /*CONFIG_USER_ONLY*/
4b779ceb
RH
8289
8290 /*
8291 * If full MTE is enabled, add all of the system registers.
8292 * If only "instructions available at EL0" are enabled,
8293 * then define only a RAZ/WI version of PSTATE.TCO.
8294 */
8295 if (cpu_isar_feature(aa64_mte, cpu)) {
8296 define_arm_cp_regs(cpu, mte_reginfo);
5463df16 8297 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb
RH
8298 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8299 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
5463df16 8300 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb 8301 }
7cb1e618
RH
8302
8303 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8304 define_arm_cp_regs(cpu, scxtnum_reginfo);
8305 }
967aa94f 8306#endif
cb570bd3 8307
22e57073 8308 if (cpu_isar_feature(any_predinv, cpu)) {
cb570bd3
RH
8309 define_arm_cp_regs(cpu, predinv_reginfo);
8310 }
e2cce18f 8311
957e6155
PM
8312 if (cpu_isar_feature(any_ccidx, cpu)) {
8313 define_arm_cp_regs(cpu, ccsidr2_reginfo);
8314 }
8315
e2cce18f
RH
8316#ifndef CONFIG_USER_ONLY
8317 /*
8318 * Register redirections and aliases must be done last,
8319 * after the registers from the other extensions have been defined.
8320 */
8321 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8322 define_arm_vh_e2h_redirects_aliases(cpu);
8323 }
8324#endif
2ceb98c0
PM
8325}
8326
777dc784
PM
8327/* Sort alphabetically by type name, except for "any". */
8328static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 8329{
777dc784
PM
8330 ObjectClass *class_a = (ObjectClass *)a;
8331 ObjectClass *class_b = (ObjectClass *)b;
8332 const char *name_a, *name_b;
5adb4839 8333
777dc784
PM
8334 name_a = object_class_get_name(class_a);
8335 name_b = object_class_get_name(class_b);
51492fd1 8336 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 8337 return 1;
51492fd1 8338 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
8339 return -1;
8340 } else {
8341 return strcmp(name_a, name_b);
5adb4839
PB
8342 }
8343}
8344
777dc784 8345static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 8346{
777dc784 8347 ObjectClass *oc = data;
977c33ba 8348 CPUClass *cc = CPU_CLASS(oc);
51492fd1
AF
8349 const char *typename;
8350 char *name;
3371d272 8351
51492fd1
AF
8352 typename = object_class_get_name(oc);
8353 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
977c33ba
DB
8354 if (cc->deprecation_note) {
8355 qemu_printf(" %s (deprecated)\n", name);
8356 } else {
8357 qemu_printf(" %s\n", name);
8358 }
51492fd1 8359 g_free(name);
777dc784
PM
8360}
8361
0442428a 8362void arm_cpu_list(void)
777dc784 8363{
777dc784
PM
8364 GSList *list;
8365
8366 list = object_class_get_list(TYPE_ARM_CPU, false);
8367 list = g_slist_sort(list, arm_cpu_list_compare);
0442428a
MA
8368 qemu_printf("Available CPUs:\n");
8369 g_slist_foreach(list, arm_cpu_list_entry, NULL);
777dc784 8370 g_slist_free(list);
40f137e1
PB
8371}
8372
78027bb6
CR
8373static void arm_cpu_add_definition(gpointer data, gpointer user_data)
8374{
8375 ObjectClass *oc = data;
8376 CpuDefinitionInfoList **cpu_list = user_data;
78027bb6
CR
8377 CpuDefinitionInfo *info;
8378 const char *typename;
8379
8380 typename = object_class_get_name(oc);
8381 info = g_malloc0(sizeof(*info));
8382 info->name = g_strndup(typename,
8383 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 8384 info->q_typename = g_strdup(typename);
78027bb6 8385
54aa3de7 8386 QAPI_LIST_PREPEND(*cpu_list, info);
78027bb6
CR
8387}
8388
25a9d6ca 8389CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
78027bb6
CR
8390{
8391 CpuDefinitionInfoList *cpu_list = NULL;
8392 GSList *list;
8393
8394 list = object_class_get_list(TYPE_ARM_CPU, false);
8395 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
8396 g_slist_free(list);
8397
8398 return cpu_list;
8399}
8400
1859f8c3
RH
8401/*
8402 * Private utility function for define_one_arm_cp_reg_with_opaque():
8403 * add a single reginfo struct to the hash table.
8404 */
6e6efd61 8405static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
cbe64585
RH
8406 void *opaque, CPState state,
8407 CPSecureState secstate,
9c513e78
AB
8408 int crm, int opc1, int opc2,
8409 const char *name)
6e6efd61 8410{
696ba377 8411 CPUARMState *env = &cpu->env;
5860362d 8412 uint32_t key;
c27f5d3a 8413 ARMCPRegInfo *r2;
4c8c4541
RH
8414 bool is64 = r->type & ARM_CP_64BIT;
8415 bool ns = secstate & ARM_CP_SECSTATE_NS;
cac65299 8416 int cp = r->cp;
c27f5d3a 8417 size_t name_len;
696ba377 8418 bool make_const;
c27f5d3a 8419
cac65299
RH
8420 switch (state) {
8421 case ARM_CP_STATE_AA32:
8422 /* We assume it is a cp15 register if the .cp field is left unset. */
8423 if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
8424 cp = 15;
8425 }
8426 key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
8427 break;
8428 case ARM_CP_STATE_AA64:
8429 /*
8430 * To allow abbreviation of ARMCPRegInfo definitions, we treat
8431 * cp == 0 as equivalent to the value for "standard guest-visible
8432 * sysreg". STATE_BOTH definitions are also always "standard sysreg"
8433 * in their AArch64 view (the .cp value may be non-zero for the
8434 * benefit of the AArch32 view).
8435 */
8436 if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
8437 cp = CP_REG_ARM64_SYSREG_CP;
8438 }
8439 key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
8440 break;
8441 default:
8442 g_assert_not_reached();
8443 }
8444
dc44545b
RH
8445 /* Overriding of an existing definition must be explicitly requested. */
8446 if (!(r->type & ARM_CP_OVERRIDE)) {
8447 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
8448 if (oldreg) {
8449 assert(oldreg->type & ARM_CP_OVERRIDE);
8450 }
8451 }
8452
696ba377
RH
8453 /*
8454 * Eliminate registers that are not present because the EL is missing.
8455 * Doing this here makes it easier to put all registers for a given
8456 * feature into the same ARMCPRegInfo array and define them all at once.
8457 */
8458 make_const = false;
8459 if (arm_feature(env, ARM_FEATURE_EL3)) {
8460 /*
8461 * An EL2 register without EL2 but with EL3 is (usually) RES0.
8462 * See rule RJFFP in section D1.1.3 of DDI0487H.a.
8463 */
8464 int min_el = ctz32(r->access) / 2;
8465 if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
8466 if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
8467 return;
8468 }
8469 make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
8470 }
8471 } else {
8472 CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
8473 ? PL2_RW : PL1_RW);
8474 if ((r->access & max_el) == 0) {
8475 return;
8476 }
8477 }
8478
c27f5d3a
RH
8479 /* Combine cpreg and name into one allocation. */
8480 name_len = strlen(name) + 1;
8481 r2 = g_malloc(sizeof(*r2) + name_len);
8482 *r2 = *r;
8483 r2->name = memcpy(r2 + 1, name, name_len);
3f3c82a5 8484
cc946d96
RH
8485 /*
8486 * Update fields to match the instantiation, overwiting wildcards
8487 * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
3f3c82a5 8488 */
cc946d96
RH
8489 r2->cp = cp;
8490 r2->crm = crm;
8491 r2->opc1 = opc1;
8492 r2->opc2 = opc2;
8493 r2->state = state;
3f3c82a5 8494 r2->secure = secstate;
cc946d96
RH
8495 if (opaque) {
8496 r2->opaque = opaque;
8497 }
3f3c82a5 8498
696ba377
RH
8499 if (make_const) {
8500 /* This should not have been a very special register to begin. */
8501 int old_special = r2->type & ARM_CP_SPECIAL_MASK;
8502 assert(old_special == 0 || old_special == ARM_CP_NOP);
1859f8c3 8503 /*
696ba377
RH
8504 * Set the special function to CONST, retaining the other flags.
8505 * This is important for e.g. ARM_CP_SVE so that we still
8506 * take the SVE trap if CPTR_EL3.EZ == 0.
f5a0a5a5 8507 */
696ba377
RH
8508 r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
8509 /*
8510 * Usually, these registers become RES0, but there are a few
8511 * special cases like VPIDR_EL2 which have a constant non-zero
8512 * value with writes ignored.
8513 */
8514 if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
8515 r2->resetvalue = 0;
8516 }
8517 /*
8518 * ARM_CP_CONST has precedence, so removing the callbacks and
8519 * offsets are not strictly necessary, but it is potentially
8520 * less confusing to debug later.
8521 */
8522 r2->readfn = NULL;
8523 r2->writefn = NULL;
8524 r2->raw_readfn = NULL;
8525 r2->raw_writefn = NULL;
8526 r2->resetfn = NULL;
8527 r2->fieldoffset = 0;
8528 r2->bank_fieldoffsets[0] = 0;
8529 r2->bank_fieldoffsets[1] = 0;
8530 } else {
8531 bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
3f3c82a5 8532
10748a96 8533 if (isbanked) {
1859f8c3 8534 /*
696ba377
RH
8535 * Register is banked (using both entries in array).
8536 * Overwriting fieldoffset as the array is only used to define
8537 * banked registers but later only fieldoffset is used.
3f3c82a5 8538 */
696ba377
RH
8539 r2->fieldoffset = r->bank_fieldoffsets[ns];
8540 }
8541 if (state == ARM_CP_STATE_AA32) {
8542 if (isbanked) {
8543 /*
8544 * If the register is banked then we don't need to migrate or
8545 * reset the 32-bit instance in certain cases:
8546 *
8547 * 1) If the register has both 32-bit and 64-bit instances
8548 * then we can count on the 64-bit instance taking care
8549 * of the non-secure bank.
8550 * 2) If ARMv8 is enabled then we can count on a 64-bit
8551 * version taking care of the secure bank. This requires
8552 * that separate 32 and 64-bit definitions are provided.
8553 */
8554 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
8555 (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
8556 r2->type |= ARM_CP_ALIAS;
8557 }
8558 } else if ((secstate != r->secure) && !ns) {
8559 /*
8560 * The register is not banked so we only want to allow
8561 * migration of the non-secure instance.
8562 */
7a0e58fa 8563 r2->type |= ARM_CP_ALIAS;
3f3c82a5 8564 }
3f3c82a5 8565
696ba377
RH
8566 if (HOST_BIG_ENDIAN &&
8567 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
8568 r2->fieldoffset += sizeof(uint32_t);
8569 }
3f3c82a5 8570 }
f5a0a5a5 8571 }
cc946d96 8572
1859f8c3
RH
8573 /*
8574 * By convention, for wildcarded registers only the first
6e6efd61 8575 * entry is used for migration; the others are marked as
7a0e58fa 8576 * ALIAS so we don't try to transfer the register
6e6efd61 8577 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 8578 * never migratable and not even raw-accessible.
6e6efd61 8579 */
696ba377 8580 if (r2->type & ARM_CP_SPECIAL_MASK) {
7a0e58fa
PM
8581 r2->type |= ARM_CP_NO_RAW;
8582 }
8583 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
8584 ((r->opc1 == CP_ANY) && opc1 != 0) ||
8585 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 8586 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
8587 }
8588
1859f8c3
RH
8589 /*
8590 * Check that raw accesses are either forbidden or handled. Note that
375421cc
PM
8591 * we can't assert this earlier because the setup of fieldoffset for
8592 * banked registers has to be done first.
8593 */
8594 if (!(r2->type & ARM_CP_NO_RAW)) {
8595 assert(!raw_accessors_invalid(r2));
8596 }
8597
5860362d 8598 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
6e6efd61
PM
8599}
8600
8601
4b6a83fb
PM
8602void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
8603 const ARMCPRegInfo *r, void *opaque)
8604{
8605 /* Define implementations of coprocessor registers.
8606 * We store these in a hashtable because typically
8607 * there are less than 150 registers in a space which
8608 * is 16*16*16*8*8 = 262144 in size.
8609 * Wildcarding is supported for the crm, opc1 and opc2 fields.
8610 * If a register is defined twice then the second definition is
8611 * used, so this can be used to define some generic registers and
8612 * then override them with implementation specific variations.
8613 * At least one of the original and the second definition should
8614 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
8615 * against accidental use.
f5a0a5a5
PM
8616 *
8617 * The state field defines whether the register is to be
8618 * visible in the AArch32 or AArch64 execution state. If the
8619 * state is set to ARM_CP_STATE_BOTH then we synthesise a
8620 * reginfo structure for the AArch32 view, which sees the lower
8621 * 32 bits of the 64 bit register.
8622 *
8623 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
8624 * be wildcarded. AArch64 registers are always considered to be 64
8625 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
8626 * the register, if any.
4b6a83fb 8627 */
d95101d6 8628 int crm, opc1, opc2;
4b6a83fb
PM
8629 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
8630 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
8631 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
8632 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
8633 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
8634 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
d95101d6
RH
8635 CPState state;
8636
4b6a83fb
PM
8637 /* 64 bit registers have only CRm and Opc1 fields */
8638 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
8639 /* op0 only exists in the AArch64 encodings */
8640 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
8641 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
8642 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
cd8be50e
PM
8643 /*
8644 * This API is only for Arm's system coprocessors (14 and 15) or
8645 * (M-profile or v7A-and-earlier only) for implementation defined
8646 * coprocessors in the range 0..7. Our decode assumes this, since
8647 * 8..13 can be used for other insns including VFP and Neon. See
8648 * valid_cp() in translate.c. Assert here that we haven't tried
8649 * to use an invalid coprocessor number.
8650 */
8651 switch (r->state) {
8652 case ARM_CP_STATE_BOTH:
8653 /* 0 has a special meaning, but otherwise the same rules as AA32. */
8654 if (r->cp == 0) {
8655 break;
8656 }
8657 /* fall through */
8658 case ARM_CP_STATE_AA32:
8659 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
8660 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
8661 assert(r->cp >= 14 && r->cp <= 15);
8662 } else {
8663 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
8664 }
8665 break;
8666 case ARM_CP_STATE_AA64:
8667 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
8668 break;
8669 default:
8670 g_assert_not_reached();
8671 }
f5a0a5a5
PM
8672 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
8673 * encodes a minimum access level for the register. We roll this
8674 * runtime check into our general permission check code, so check
8675 * here that the reginfo's specified permissions are strict enough
8676 * to encompass the generic architectural permission check.
8677 */
8678 if (r->state != ARM_CP_STATE_AA32) {
39107337 8679 CPAccessRights mask;
f5a0a5a5 8680 switch (r->opc1) {
b5bd7440
AB
8681 case 0:
8682 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
8683 mask = PL0U_R | PL1_RW;
8684 break;
8685 case 1: case 2:
f5a0a5a5
PM
8686 /* min_EL EL1 */
8687 mask = PL1_RW;
8688 break;
8689 case 3:
8690 /* min_EL EL0 */
8691 mask = PL0_RW;
8692 break;
8693 case 4:
b4ecf60f 8694 case 5:
f5a0a5a5
PM
8695 /* min_EL EL2 */
8696 mask = PL2_RW;
8697 break;
f5a0a5a5
PM
8698 case 6:
8699 /* min_EL EL3 */
8700 mask = PL3_RW;
8701 break;
8702 case 7:
8703 /* min_EL EL1, secure mode only (we don't check the latter) */
8704 mask = PL1_RW;
8705 break;
8706 default:
8707 /* broken reginfo with out-of-range opc1 */
d385a605 8708 g_assert_not_reached();
f5a0a5a5
PM
8709 }
8710 /* assert our permissions are not too lax (stricter is fine) */
8711 assert((r->access & ~mask) == 0);
8712 }
8713
4b6a83fb
PM
8714 /* Check that the register definition has enough info to handle
8715 * reads and writes if they are permitted.
8716 */
87c3f0f2 8717 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
4b6a83fb 8718 if (r->access & PL3_R) {
3f3c82a5
FA
8719 assert((r->fieldoffset ||
8720 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8721 r->readfn);
4b6a83fb
PM
8722 }
8723 if (r->access & PL3_W) {
3f3c82a5
FA
8724 assert((r->fieldoffset ||
8725 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
8726 r->writefn);
4b6a83fb
PM
8727 }
8728 }
5809ac57 8729
4b6a83fb
PM
8730 for (crm = crmmin; crm <= crmmax; crm++) {
8731 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
8732 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
8733 for (state = ARM_CP_STATE_AA32;
8734 state <= ARM_CP_STATE_AA64; state++) {
8735 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
8736 continue;
8737 }
3f3c82a5
FA
8738 if (state == ARM_CP_STATE_AA32) {
8739 /* Under AArch32 CP registers can be common
8740 * (same for secure and non-secure world) or banked.
8741 */
9c513e78
AB
8742 char *name;
8743
3f3c82a5
FA
8744 switch (r->secure) {
8745 case ARM_CP_SECSTATE_S:
8746 case ARM_CP_SECSTATE_NS:
8747 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
8748 r->secure, crm, opc1, opc2,
8749 r->name);
3f3c82a5 8750 break;
cbe64585 8751 case ARM_CP_SECSTATE_BOTH:
9c513e78 8752 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
8753 add_cpreg_to_hashtable(cpu, r, opaque, state,
8754 ARM_CP_SECSTATE_S,
9c513e78
AB
8755 crm, opc1, opc2, name);
8756 g_free(name);
3f3c82a5
FA
8757 add_cpreg_to_hashtable(cpu, r, opaque, state,
8758 ARM_CP_SECSTATE_NS,
9c513e78 8759 crm, opc1, opc2, r->name);
3f3c82a5 8760 break;
cbe64585
RH
8761 default:
8762 g_assert_not_reached();
3f3c82a5
FA
8763 }
8764 } else {
8765 /* AArch64 registers get mapped to non-secure instance
8766 * of AArch32 */
8767 add_cpreg_to_hashtable(cpu, r, opaque, state,
8768 ARM_CP_SECSTATE_NS,
9c513e78 8769 crm, opc1, opc2, r->name);
3f3c82a5 8770 }
f5a0a5a5 8771 }
4b6a83fb
PM
8772 }
8773 }
8774 }
8775}
8776
5809ac57
RH
8777/* Define a whole list of registers */
8778void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
8779 void *opaque, size_t len)
4b6a83fb 8780{
5809ac57
RH
8781 size_t i;
8782 for (i = 0; i < len; ++i) {
8783 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
4b6a83fb
PM
8784 }
8785}
8786
6c5c0fec
AB
8787/*
8788 * Modify ARMCPRegInfo for access from userspace.
8789 *
8790 * This is a data driven modification directed by
8791 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
8792 * user-space cannot alter any values and dynamic values pertaining to
8793 * execution state are hidden from user space view anyway.
8794 */
5809ac57
RH
8795void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
8796 const ARMCPRegUserSpaceInfo *mods,
8797 size_t mods_len)
6c5c0fec 8798{
5809ac57
RH
8799 for (size_t mi = 0; mi < mods_len; ++mi) {
8800 const ARMCPRegUserSpaceInfo *m = mods + mi;
d040242e 8801 GPatternSpec *pat = NULL;
5809ac57 8802
d040242e
AB
8803 if (m->is_glob) {
8804 pat = g_pattern_spec_new(m->name);
8805 }
5809ac57
RH
8806 for (size_t ri = 0; ri < regs_len; ++ri) {
8807 ARMCPRegInfo *r = regs + ri;
8808
d040242e
AB
8809 if (pat && g_pattern_match_string(pat, r->name)) {
8810 r->type = ARM_CP_CONST;
8811 r->access = PL0U_R;
8812 r->resetvalue = 0;
8813 /* continue */
8814 } else if (strcmp(r->name, m->name) == 0) {
6c5c0fec
AB
8815 r->type = ARM_CP_CONST;
8816 r->access = PL0U_R;
8817 r->resetvalue &= m->exported_bits;
8818 r->resetvalue |= m->fixed_bits;
8819 break;
8820 }
8821 }
d040242e
AB
8822 if (pat) {
8823 g_pattern_spec_free(pat);
8824 }
6c5c0fec
AB
8825 }
8826}
8827
60322b39 8828const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 8829{
5860362d 8830 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
4b6a83fb
PM
8831}
8832
c4241c7d
PM
8833void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
8834 uint64_t value)
4b6a83fb
PM
8835{
8836 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
8837}
8838
c4241c7d 8839uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
8840{
8841 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
8842 return 0;
8843}
8844
f5a0a5a5
PM
8845void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
8846{
8847 /* Helper coprocessor reset function for do-nothing-on-reset registers */
8848}
8849
af393ffc 8850static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b
PM
8851{
8852 /* Return true if it is not valid for us to switch to
8853 * this CPU mode (ie all the UNPREDICTABLE cases in
8854 * the ARM ARM CPSRWriteByInstr pseudocode).
8855 */
af393ffc
PM
8856
8857 /* Changes to or from Hyp via MSR and CPS are illegal. */
8858 if (write_type == CPSRWriteByInstr &&
8859 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
8860 mode == ARM_CPU_MODE_HYP)) {
8861 return 1;
8862 }
8863
37064a8b
PM
8864 switch (mode) {
8865 case ARM_CPU_MODE_USR:
10eacda7 8866 return 0;
37064a8b
PM
8867 case ARM_CPU_MODE_SYS:
8868 case ARM_CPU_MODE_SVC:
8869 case ARM_CPU_MODE_ABT:
8870 case ARM_CPU_MODE_UND:
8871 case ARM_CPU_MODE_IRQ:
8872 case ARM_CPU_MODE_FIQ:
52ff951b
PM
8873 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
8874 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
8875 */
10eacda7
PM
8876 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
8877 * and CPS are treated as illegal mode changes.
8878 */
8879 if (write_type == CPSRWriteByInstr &&
10eacda7 8880 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7c208e0f 8881 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10eacda7
PM
8882 return 1;
8883 }
37064a8b 8884 return 0;
e6c8fc07 8885 case ARM_CPU_MODE_HYP:
e6ef0169 8886 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
027fc527 8887 case ARM_CPU_MODE_MON:
58ae2d1f 8888 return arm_current_el(env) < 3;
37064a8b
PM
8889 default:
8890 return 1;
8891 }
8892}
8893
2f4a40e5
AZ
8894uint32_t cpsr_read(CPUARMState *env)
8895{
8896 int ZF;
6fbe23d5
PB
8897 ZF = (env->ZF == 0);
8898 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
8899 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
8900 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
8901 | ((env->condexec_bits & 0xfc) << 8)
af519934 8902 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
8903}
8904
50866ba5
PM
8905void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
8906 CPSRWriteType write_type)
2f4a40e5 8907{
6e8801f9 8908 uint32_t changed_daif;
e784807c
PM
8909 bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
8910 (mask & (CPSR_M | CPSR_E | CPSR_IL));
6e8801f9 8911
2f4a40e5 8912 if (mask & CPSR_NZCV) {
6fbe23d5
PB
8913 env->ZF = (~val) & CPSR_Z;
8914 env->NF = val;
2f4a40e5
AZ
8915 env->CF = (val >> 29) & 1;
8916 env->VF = (val << 3) & 0x80000000;
8917 }
8918 if (mask & CPSR_Q)
8919 env->QF = ((val & CPSR_Q) != 0);
8920 if (mask & CPSR_T)
8921 env->thumb = ((val & CPSR_T) != 0);
8922 if (mask & CPSR_IT_0_1) {
8923 env->condexec_bits &= ~3;
8924 env->condexec_bits |= (val >> 25) & 3;
8925 }
8926 if (mask & CPSR_IT_2_7) {
8927 env->condexec_bits &= 3;
8928 env->condexec_bits |= (val >> 8) & 0xfc;
8929 }
8930 if (mask & CPSR_GE) {
8931 env->GE = (val >> 16) & 0xf;
8932 }
8933
6e8801f9
FA
8934 /* In a V7 implementation that includes the security extensions but does
8935 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
8936 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
8937 * bits respectively.
8938 *
8939 * In a V8 implementation, it is permitted for privileged software to
8940 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
8941 */
f8c88bbc 8942 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
8943 arm_feature(env, ARM_FEATURE_EL3) &&
8944 !arm_feature(env, ARM_FEATURE_EL2) &&
8945 !arm_is_secure(env)) {
8946
8947 changed_daif = (env->daif ^ val) & mask;
8948
8949 if (changed_daif & CPSR_A) {
8950 /* Check to see if we are allowed to change the masking of async
8951 * abort exceptions from a non-secure state.
8952 */
8953 if (!(env->cp15.scr_el3 & SCR_AW)) {
8954 qemu_log_mask(LOG_GUEST_ERROR,
8955 "Ignoring attempt to switch CPSR_A flag from "
8956 "non-secure world with SCR.AW bit clear\n");
8957 mask &= ~CPSR_A;
8958 }
8959 }
8960
8961 if (changed_daif & CPSR_F) {
8962 /* Check to see if we are allowed to change the masking of FIQ
8963 * exceptions from a non-secure state.
8964 */
8965 if (!(env->cp15.scr_el3 & SCR_FW)) {
8966 qemu_log_mask(LOG_GUEST_ERROR,
8967 "Ignoring attempt to switch CPSR_F flag from "
8968 "non-secure world with SCR.FW bit clear\n");
8969 mask &= ~CPSR_F;
8970 }
8971
8972 /* Check whether non-maskable FIQ (NMFI) support is enabled.
8973 * If this bit is set software is not allowed to mask
8974 * FIQs, but is allowed to set CPSR_F to 0.
8975 */
8976 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
8977 (val & CPSR_F)) {
8978 qemu_log_mask(LOG_GUEST_ERROR,
8979 "Ignoring attempt to enable CPSR_F flag "
8980 "(non-maskable FIQ [NMFI] support enabled)\n");
8981 mask &= ~CPSR_F;
8982 }
8983 }
8984 }
8985
4cc35614
PM
8986 env->daif &= ~(CPSR_AIF & mask);
8987 env->daif |= val & CPSR_AIF & mask;
8988
f8c88bbc
PM
8989 if (write_type != CPSRWriteRaw &&
8990 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9
PM
8991 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
8992 /* Note that we can only get here in USR mode if this is a
8993 * gdb stub write; for this case we follow the architectural
8994 * behaviour for guest writes in USR mode of ignoring an attempt
8995 * to switch mode. (Those are caught by translate.c for writes
8996 * triggered by guest instructions.)
8997 */
8998 mask &= ~CPSR_M;
8999 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
81907a58
PM
9000 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9001 * v7, and has defined behaviour in v8:
9002 * + leave CPSR.M untouched
9003 * + allow changes to the other CPSR fields
9004 * + set PSTATE.IL
9005 * For user changes via the GDB stub, we don't set PSTATE.IL,
9006 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
9007 */
9008 mask &= ~CPSR_M;
81907a58
PM
9009 if (write_type != CPSRWriteByGDBStub &&
9010 arm_feature(env, ARM_FEATURE_V8)) {
9011 mask |= CPSR_IL;
9012 val |= CPSR_IL;
9013 }
81e37284
PM
9014 qemu_log_mask(LOG_GUEST_ERROR,
9015 "Illegal AArch32 mode switch attempt from %s to %s\n",
9016 aarch32_mode_name(env->uncached_cpsr),
9017 aarch32_mode_name(val));
37064a8b 9018 } else {
81e37284
PM
9019 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9020 write_type == CPSRWriteExceptionReturn ?
9021 "Exception return from AArch32" :
9022 "AArch32 mode switch from",
9023 aarch32_mode_name(env->uncached_cpsr),
9024 aarch32_mode_name(val), env->regs[15]);
37064a8b
PM
9025 switch_mode(env, val & CPSR_M);
9026 }
2f4a40e5
AZ
9027 }
9028 mask &= ~CACHED_CPSR_BITS;
9029 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
e784807c
PM
9030 if (rebuild_hflags) {
9031 arm_rebuild_hflags(env);
9032 }
2f4a40e5
AZ
9033}
9034
b26eefb6
PB
9035/* Sign/zero extend */
9036uint32_t HELPER(sxtb16)(uint32_t x)
9037{
9038 uint32_t res;
9039 res = (uint16_t)(int8_t)x;
9040 res |= (uint32_t)(int8_t)(x >> 16) << 16;
9041 return res;
9042}
9043
e5346292
PM
9044static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9045{
9046 /*
9047 * Take a division-by-zero exception if necessary; otherwise return
9048 * to get the usual non-trapping division behaviour (result of 0)
9049 */
9050 if (arm_feature(env, ARM_FEATURE_M)
9051 && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9052 raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9053 }
9054}
9055
b26eefb6
PB
9056uint32_t HELPER(uxtb16)(uint32_t x)
9057{
9058 uint32_t res;
9059 res = (uint16_t)(uint8_t)x;
9060 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9061 return res;
9062}
9063
e5346292 9064int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
3670669c 9065{
fc7a5038 9066 if (den == 0) {
e5346292 9067 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9068 return 0;
9069 }
9070 if (num == INT_MIN && den == -1) {
9071 return INT_MIN;
9072 }
3670669c
PB
9073 return num / den;
9074}
9075
e5346292 9076uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
3670669c 9077{
fc7a5038 9078 if (den == 0) {
e5346292 9079 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9080 return 0;
9081 }
3670669c
PB
9082 return num / den;
9083}
9084
9085uint32_t HELPER(rbit)(uint32_t x)
9086{
42fedbca 9087 return revbit32(x);
3670669c
PB
9088}
9089
c47eaf9f 9090#ifdef CONFIG_USER_ONLY
b5ff1b31 9091
affdb64d 9092static void switch_mode(CPUARMState *env, int mode)
b5ff1b31 9093{
2fc0cc0e 9094 ARMCPU *cpu = env_archcpu(env);
a47dddd7
AF
9095
9096 if (mode != ARM_CPU_MODE_USR) {
9097 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9098 }
b5ff1b31
FB
9099}
9100
012a906b
GB
9101uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9102 uint32_t cur_el, bool secure)
9e729b57
EI
9103{
9104 return 1;
9105}
9106
ce02049d
GB
9107void aarch64_sync_64_to_32(CPUARMState *env)
9108{
9109 g_assert_not_reached();
9110}
9111
b5ff1b31
FB
9112#else
9113
affdb64d 9114static void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
9115{
9116 int old_mode;
9117 int i;
9118
9119 old_mode = env->uncached_cpsr & CPSR_M;
9120 if (mode == old_mode)
9121 return;
9122
9123 if (old_mode == ARM_CPU_MODE_FIQ) {
9124 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 9125 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
9126 } else if (mode == ARM_CPU_MODE_FIQ) {
9127 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
8637c67f 9128 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
9129 }
9130
f5206413 9131 i = bank_number(old_mode);
b5ff1b31 9132 env->banked_r13[i] = env->regs[13];
b5ff1b31
FB
9133 env->banked_spsr[i] = env->spsr;
9134
f5206413 9135 i = bank_number(mode);
b5ff1b31 9136 env->regs[13] = env->banked_r13[i];
b5ff1b31 9137 env->spsr = env->banked_spsr[i];
593cfa2b
PM
9138
9139 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9140 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
b5ff1b31
FB
9141}
9142
0eeb17d6
GB
9143/* Physical Interrupt Target EL Lookup Table
9144 *
9145 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9146 *
9147 * The below multi-dimensional table is used for looking up the target
9148 * exception level given numerous condition criteria. Specifically, the
9149 * target EL is based on SCR and HCR routing controls as well as the
9150 * currently executing EL and secure state.
9151 *
9152 * Dimensions:
9153 * target_el_table[2][2][2][2][2][4]
9154 * | | | | | +--- Current EL
9155 * | | | | +------ Non-secure(0)/Secure(1)
9156 * | | | +--------- HCR mask override
9157 * | | +------------ SCR exec state control
9158 * | +--------------- SCR mask override
9159 * +------------------ 32-bit(0)/64-bit(1) EL3
9160 *
9161 * The table values are as such:
9162 * 0-3 = EL0-EL3
9163 * -1 = Cannot occur
9164 *
9165 * The ARM ARM target EL table includes entries indicating that an "exception
9166 * is not taken". The two cases where this is applicable are:
9167 * 1) An exception is taken from EL3 but the SCR does not have the exception
9168 * routed to EL3.
9169 * 2) An exception is taken from EL2 but the HCR does not have the exception
9170 * routed to EL2.
9171 * In these two cases, the below table contain a target of EL1. This value is
9172 * returned as it is expected that the consumer of the table data will check
9173 * for "target EL >= current EL" to ensure the exception is not taken.
9174 *
9175 * SCR HCR
9176 * 64 EA AMO From
9177 * BIT IRQ IMO Non-secure Secure
9178 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
9179 */
82c39f6a 9180static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
9181 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9182 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
9183 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9184 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
9185 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9186 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
9187 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9188 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
9189 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6c85f906
RDC
9190 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
9191 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
9192 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
0eeb17d6
GB
9193 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
9194 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6c85f906
RDC
9195 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
9196 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
0eeb17d6
GB
9197};
9198
9199/*
9200 * Determine the target EL for physical exceptions
9201 */
012a906b
GB
9202uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9203 uint32_t cur_el, bool secure)
0eeb17d6
GB
9204{
9205 CPUARMState *env = cs->env_ptr;
f7778444
RH
9206 bool rw;
9207 bool scr;
9208 bool hcr;
0eeb17d6 9209 int target_el;
2cde031f 9210 /* Is the highest EL AArch64? */
f7778444
RH
9211 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9212 uint64_t hcr_el2;
2cde031f
SS
9213
9214 if (arm_feature(env, ARM_FEATURE_EL3)) {
9215 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
9216 } else {
9217 /* Either EL2 is the highest EL (and so the EL2 register width
9218 * is given by is64); or there is no EL2 or EL3, in which case
9219 * the value of 'rw' does not affect the table lookup anyway.
9220 */
9221 rw = is64;
9222 }
0eeb17d6 9223
f7778444 9224 hcr_el2 = arm_hcr_el2_eff(env);
0eeb17d6
GB
9225 switch (excp_idx) {
9226 case EXCP_IRQ:
9227 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
f7778444 9228 hcr = hcr_el2 & HCR_IMO;
0eeb17d6
GB
9229 break;
9230 case EXCP_FIQ:
9231 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
f7778444 9232 hcr = hcr_el2 & HCR_FMO;
0eeb17d6
GB
9233 break;
9234 default:
9235 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
f7778444 9236 hcr = hcr_el2 & HCR_AMO;
0eeb17d6
GB
9237 break;
9238 };
9239
d1b31428
RH
9240 /*
9241 * For these purposes, TGE and AMO/IMO/FMO both force the
9242 * interrupt to EL2. Fold TGE into the bit extracted above.
9243 */
9244 hcr |= (hcr_el2 & HCR_TGE) != 0;
9245
0eeb17d6
GB
9246 /* Perform a table-lookup for the target EL given the current state */
9247 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9248
9249 assert(target_el > 0);
9250
9251 return target_el;
9252}
9253
fc6177af 9254void arm_log_exception(CPUState *cs)
b59f479b 9255{
fc6177af
PM
9256 int idx = cs->exception_index;
9257
b59f479b
PMD
9258 if (qemu_loglevel_mask(CPU_LOG_INT)) {
9259 const char *exc = NULL;
9260 static const char * const excnames[] = {
9261 [EXCP_UDEF] = "Undefined Instruction",
9262 [EXCP_SWI] = "SVC",
9263 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9264 [EXCP_DATA_ABORT] = "Data Abort",
9265 [EXCP_IRQ] = "IRQ",
9266 [EXCP_FIQ] = "FIQ",
9267 [EXCP_BKPT] = "Breakpoint",
9268 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9269 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9270 [EXCP_HVC] = "Hypervisor Call",
9271 [EXCP_HYP_TRAP] = "Hypervisor Trap",
9272 [EXCP_SMC] = "Secure Monitor Call",
9273 [EXCP_VIRQ] = "Virtual IRQ",
9274 [EXCP_VFIQ] = "Virtual FIQ",
9275 [EXCP_SEMIHOST] = "Semihosting call",
9276 [EXCP_NOCP] = "v7M NOCP UsageFault",
9277 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9278 [EXCP_STKOF] = "v8M STKOF UsageFault",
9279 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9280 [EXCP_LSERR] = "v8M LSERR UsageFault",
9281 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
e5346292 9282 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
3c29632f 9283 [EXCP_VSERR] = "Virtual SERR",
b59f479b
PMD
9284 };
9285
9286 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9287 exc = excnames[idx];
9288 }
9289 if (!exc) {
9290 exc = "unknown";
9291 }
fc6177af
PM
9292 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9293 idx, exc, cs->cpu_index);
b59f479b
PMD
9294 }
9295}
9296
a356dacf 9297/*
7aab5a8c
PMD
9298 * Function used to synchronize QEMU's AArch64 register set with AArch32
9299 * register set. This is necessary when switching between AArch32 and AArch64
9300 * execution state.
a356dacf 9301 */
7aab5a8c 9302void aarch64_sync_32_to_64(CPUARMState *env)
9ee6e8bb 9303{
7aab5a8c
PMD
9304 int i;
9305 uint32_t mode = env->uncached_cpsr & CPSR_M;
9306
9307 /* We can blanket copy R[0:7] to X[0:7] */
9308 for (i = 0; i < 8; i++) {
9309 env->xregs[i] = env->regs[i];
fd592d89 9310 }
70d74660 9311
9a223097 9312 /*
7aab5a8c
PMD
9313 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9314 * Otherwise, they come from the banked user regs.
fd592d89 9315 */
7aab5a8c
PMD
9316 if (mode == ARM_CPU_MODE_FIQ) {
9317 for (i = 8; i < 13; i++) {
9318 env->xregs[i] = env->usr_regs[i - 8];
9319 }
9320 } else {
9321 for (i = 8; i < 13; i++) {
9322 env->xregs[i] = env->regs[i];
9323 }
fd592d89 9324 }
9ee6e8bb 9325
7aab5a8c
PMD
9326 /*
9327 * Registers x13-x23 are the various mode SP and FP registers. Registers
9328 * r13 and r14 are only copied if we are in that mode, otherwise we copy
9329 * from the mode banked register.
9330 */
9331 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9332 env->xregs[13] = env->regs[13];
9333 env->xregs[14] = env->regs[14];
9334 } else {
9335 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9336 /* HYP is an exception in that it is copied from r14 */
9337 if (mode == ARM_CPU_MODE_HYP) {
9338 env->xregs[14] = env->regs[14];
95695eff 9339 } else {
7aab5a8c 9340 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
95695eff 9341 }
95695eff
PM
9342 }
9343
7aab5a8c
PMD
9344 if (mode == ARM_CPU_MODE_HYP) {
9345 env->xregs[15] = env->regs[13];
9346 } else {
9347 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
95695eff
PM
9348 }
9349
7aab5a8c
PMD
9350 if (mode == ARM_CPU_MODE_IRQ) {
9351 env->xregs[16] = env->regs[14];
9352 env->xregs[17] = env->regs[13];
9353 } else {
9354 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9355 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9356 }
95695eff 9357
7aab5a8c
PMD
9358 if (mode == ARM_CPU_MODE_SVC) {
9359 env->xregs[18] = env->regs[14];
9360 env->xregs[19] = env->regs[13];
9361 } else {
9362 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9363 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9364 }
95695eff 9365
7aab5a8c
PMD
9366 if (mode == ARM_CPU_MODE_ABT) {
9367 env->xregs[20] = env->regs[14];
9368 env->xregs[21] = env->regs[13];
9369 } else {
9370 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9371 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9372 }
e33cf0f8 9373
7aab5a8c
PMD
9374 if (mode == ARM_CPU_MODE_UND) {
9375 env->xregs[22] = env->regs[14];
9376 env->xregs[23] = env->regs[13];
9377 } else {
9378 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
9379 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
e33cf0f8
PM
9380 }
9381
9382 /*
7aab5a8c
PMD
9383 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9384 * mode, then we can copy from r8-r14. Otherwise, we copy from the
9385 * FIQ bank for r8-r14.
e33cf0f8 9386 */
7aab5a8c
PMD
9387 if (mode == ARM_CPU_MODE_FIQ) {
9388 for (i = 24; i < 31; i++) {
9389 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
9390 }
9391 } else {
9392 for (i = 24; i < 29; i++) {
9393 env->xregs[i] = env->fiq_regs[i - 24];
e33cf0f8 9394 }
7aab5a8c
PMD
9395 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
9396 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
e33cf0f8 9397 }
7aab5a8c
PMD
9398
9399 env->pc = env->regs[15];
e33cf0f8
PM
9400}
9401
9a223097 9402/*
7aab5a8c
PMD
9403 * Function used to synchronize QEMU's AArch32 register set with AArch64
9404 * register set. This is necessary when switching between AArch32 and AArch64
9405 * execution state.
de2db7ec 9406 */
7aab5a8c 9407void aarch64_sync_64_to_32(CPUARMState *env)
9ee6e8bb 9408{
7aab5a8c
PMD
9409 int i;
9410 uint32_t mode = env->uncached_cpsr & CPSR_M;
abc24d86 9411
7aab5a8c
PMD
9412 /* We can blanket copy X[0:7] to R[0:7] */
9413 for (i = 0; i < 8; i++) {
9414 env->regs[i] = env->xregs[i];
de2db7ec 9415 }
3f0cddee 9416
9a223097 9417 /*
7aab5a8c
PMD
9418 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
9419 * Otherwise, we copy x8-x12 into the banked user regs.
de2db7ec 9420 */
7aab5a8c
PMD
9421 if (mode == ARM_CPU_MODE_FIQ) {
9422 for (i = 8; i < 13; i++) {
9423 env->usr_regs[i - 8] = env->xregs[i];
9424 }
9425 } else {
9426 for (i = 8; i < 13; i++) {
9427 env->regs[i] = env->xregs[i];
9428 }
fb602cb7
PM
9429 }
9430
9a223097 9431 /*
7aab5a8c
PMD
9432 * Registers r13 & r14 depend on the current mode.
9433 * If we are in a given mode, we copy the corresponding x registers to r13
9434 * and r14. Otherwise, we copy the x register to the banked r13 and r14
9435 * for the mode.
fb602cb7 9436 */
7aab5a8c
PMD
9437 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9438 env->regs[13] = env->xregs[13];
9439 env->regs[14] = env->xregs[14];
fb602cb7 9440 } else {
7aab5a8c 9441 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
fb602cb7 9442
7aab5a8c
PMD
9443 /*
9444 * HYP is an exception in that it does not have its own banked r14 but
9445 * shares the USR r14
9446 */
9447 if (mode == ARM_CPU_MODE_HYP) {
9448 env->regs[14] = env->xregs[14];
9449 } else {
9450 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
9451 }
9452 }
fb602cb7 9453
7aab5a8c
PMD
9454 if (mode == ARM_CPU_MODE_HYP) {
9455 env->regs[13] = env->xregs[15];
fb602cb7 9456 } else {
7aab5a8c 9457 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
fb602cb7 9458 }
d02a8698 9459
7aab5a8c
PMD
9460 if (mode == ARM_CPU_MODE_IRQ) {
9461 env->regs[14] = env->xregs[16];
9462 env->regs[13] = env->xregs[17];
d02a8698 9463 } else {
7aab5a8c
PMD
9464 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
9465 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
d02a8698
PM
9466 }
9467
7aab5a8c
PMD
9468 if (mode == ARM_CPU_MODE_SVC) {
9469 env->regs[14] = env->xregs[18];
9470 env->regs[13] = env->xregs[19];
9471 } else {
9472 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
9473 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
fb602cb7
PM
9474 }
9475
7aab5a8c
PMD
9476 if (mode == ARM_CPU_MODE_ABT) {
9477 env->regs[14] = env->xregs[20];
9478 env->regs[13] = env->xregs[21];
9479 } else {
9480 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
9481 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
9482 }
9483
9484 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
9485 env->regs[14] = env->xregs[22];
9486 env->regs[13] = env->xregs[23];
ce02049d 9487 } else {
593cfa2b 9488 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
3a9148d0 9489 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
9490 }
9491
9492 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
9493 * mode, then we can copy to r8-r14. Otherwise, we copy to the
9494 * FIQ bank for r8-r14.
9495 */
9496 if (mode == ARM_CPU_MODE_FIQ) {
9497 for (i = 24; i < 31; i++) {
9498 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
9499 }
9500 } else {
9501 for (i = 24; i < 29; i++) {
9502 env->fiq_regs[i - 24] = env->xregs[i];
9503 }
9504 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
593cfa2b 9505 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
ce02049d
GB
9506 }
9507
9508 env->regs[15] = env->pc;
9509}
9510
dea8378b
PM
9511static void take_aarch32_exception(CPUARMState *env, int new_mode,
9512 uint32_t mask, uint32_t offset,
9513 uint32_t newpc)
9514{
4a2696c0
RH
9515 int new_el;
9516
dea8378b
PM
9517 /* Change the CPU state so as to actually take the exception. */
9518 switch_mode(env, new_mode);
4a2696c0 9519
dea8378b
PM
9520 /*
9521 * For exceptions taken to AArch32 we must clear the SS bit in both
9522 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
9523 */
f944a854 9524 env->pstate &= ~PSTATE_SS;
dea8378b
PM
9525 env->spsr = cpsr_read(env);
9526 /* Clear IT bits. */
9527 env->condexec_bits = 0;
9528 /* Switch to the new mode, and to the correct instruction set. */
9529 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
88828bf1
CD
9530
9531 /* This must be after mode switching. */
9532 new_el = arm_current_el(env);
9533
dea8378b
PM
9534 /* Set new mode endianness */
9535 env->uncached_cpsr &= ~CPSR_E;
4a2696c0 9536 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
dea8378b
PM
9537 env->uncached_cpsr |= CPSR_E;
9538 }
829f9fd3
PM
9539 /* J and IL must always be cleared for exception entry */
9540 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
dea8378b
PM
9541 env->daif |= mask;
9542
f2f68a78
RC
9543 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
9544 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
9545 env->uncached_cpsr |= CPSR_SSBS;
9546 } else {
9547 env->uncached_cpsr &= ~CPSR_SSBS;
9548 }
9549 }
9550
dea8378b
PM
9551 if (new_mode == ARM_CPU_MODE_HYP) {
9552 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
9553 env->elr_el[2] = env->regs[15];
9554 } else {
4a2696c0 9555 /* CPSR.PAN is normally preserved preserved unless... */
f8af1143 9556 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
4a2696c0
RH
9557 switch (new_el) {
9558 case 3:
9559 if (!arm_is_secure_below_el3(env)) {
9560 /* ... the target is EL3, from non-secure state. */
9561 env->uncached_cpsr &= ~CPSR_PAN;
9562 break;
9563 }
9564 /* ... the target is EL3, from secure state ... */
9565 /* fall through */
9566 case 1:
9567 /* ... the target is EL1 and SCTLR.SPAN is 0. */
9568 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
9569 env->uncached_cpsr |= CPSR_PAN;
9570 }
9571 break;
9572 }
9573 }
dea8378b
PM
9574 /*
9575 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
9576 * and we should just guard the thumb mode on V4
9577 */
9578 if (arm_feature(env, ARM_FEATURE_V4T)) {
9579 env->thumb =
9580 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
9581 }
9582 env->regs[14] = env->regs[15] + offset;
9583 }
9584 env->regs[15] = newpc;
a8a79c7a 9585 arm_rebuild_hflags(env);
dea8378b
PM
9586}
9587
b9bc21ff
PM
9588static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
9589{
9590 /*
9591 * Handle exception entry to Hyp mode; this is sufficiently
9592 * different to entry to other AArch32 modes that we handle it
9593 * separately here.
9594 *
9595 * The vector table entry used is always the 0x14 Hyp mode entry point,
2c023d36 9596 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
b9bc21ff
PM
9597 * The offset applied to the preferred return address is always zero
9598 * (see DDI0487C.a section G1.12.3).
9599 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
9600 */
9601 uint32_t addr, mask;
9602 ARMCPU *cpu = ARM_CPU(cs);
9603 CPUARMState *env = &cpu->env;
9604
9605 switch (cs->exception_index) {
9606 case EXCP_UDEF:
9607 addr = 0x04;
9608 break;
9609 case EXCP_SWI:
2c023d36 9610 addr = 0x08;
b9bc21ff
PM
9611 break;
9612 case EXCP_BKPT:
9613 /* Fall through to prefetch abort. */
9614 case EXCP_PREFETCH_ABORT:
9615 env->cp15.ifar_s = env->exception.vaddress;
9616 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
9617 (uint32_t)env->exception.vaddress);
9618 addr = 0x0c;
9619 break;
9620 case EXCP_DATA_ABORT:
9621 env->cp15.dfar_s = env->exception.vaddress;
9622 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
9623 (uint32_t)env->exception.vaddress);
9624 addr = 0x10;
9625 break;
9626 case EXCP_IRQ:
9627 addr = 0x18;
9628 break;
9629 case EXCP_FIQ:
9630 addr = 0x1c;
9631 break;
9632 case EXCP_HVC:
9633 addr = 0x08;
9634 break;
9635 case EXCP_HYP_TRAP:
9636 addr = 0x14;
9bbb4ef9 9637 break;
b9bc21ff
PM
9638 default:
9639 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
9640 }
9641
9642 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
2ed08180
PM
9643 if (!arm_feature(env, ARM_FEATURE_V8)) {
9644 /*
9645 * QEMU syndrome values are v8-style. v7 has the IL bit
9646 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
9647 * If this is a v7 CPU, squash the IL bit in those cases.
9648 */
9649 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
9650 (cs->exception_index == EXCP_DATA_ABORT &&
9651 !(env->exception.syndrome & ARM_EL_ISV)) ||
9652 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
9653 env->exception.syndrome &= ~ARM_EL_IL;
9654 }
9655 }
b9bc21ff
PM
9656 env->cp15.esr_el[2] = env->exception.syndrome;
9657 }
9658
9659 if (arm_current_el(env) != 2 && addr < 0x14) {
9660 addr = 0x14;
9661 }
9662
9663 mask = 0;
9664 if (!(env->cp15.scr_el3 & SCR_EA)) {
9665 mask |= CPSR_A;
9666 }
9667 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
9668 mask |= CPSR_I;
9669 }
9670 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
9671 mask |= CPSR_F;
9672 }
9673
9674 addr += env->cp15.hvbar;
9675
9676 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
9677}
9678
966f758c 9679static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 9680{
97a8ea5a
AF
9681 ARMCPU *cpu = ARM_CPU(cs);
9682 CPUARMState *env = &cpu->env;
b5ff1b31
FB
9683 uint32_t addr;
9684 uint32_t mask;
9685 int new_mode;
9686 uint32_t offset;
16a906fd 9687 uint32_t moe;
b5ff1b31 9688
16a906fd 9689 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
64b91e3f 9690 switch (syn_get_ec(env->exception.syndrome)) {
16a906fd
PM
9691 case EC_BREAKPOINT:
9692 case EC_BREAKPOINT_SAME_EL:
9693 moe = 1;
9694 break;
9695 case EC_WATCHPOINT:
9696 case EC_WATCHPOINT_SAME_EL:
9697 moe = 10;
9698 break;
9699 case EC_AA32_BKPT:
9700 moe = 3;
9701 break;
9702 case EC_VECTORCATCH:
9703 moe = 5;
9704 break;
9705 default:
9706 moe = 0;
9707 break;
9708 }
9709
9710 if (moe) {
9711 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
9712 }
9713
b9bc21ff
PM
9714 if (env->exception.target_el == 2) {
9715 arm_cpu_do_interrupt_aarch32_hyp(cs);
9716 return;
9717 }
9718
27103424 9719 switch (cs->exception_index) {
b5ff1b31
FB
9720 case EXCP_UDEF:
9721 new_mode = ARM_CPU_MODE_UND;
9722 addr = 0x04;
9723 mask = CPSR_I;
9724 if (env->thumb)
9725 offset = 2;
9726 else
9727 offset = 4;
9728 break;
9729 case EXCP_SWI:
9730 new_mode = ARM_CPU_MODE_SVC;
9731 addr = 0x08;
9732 mask = CPSR_I;
601d70b9 9733 /* The PC already points to the next instruction. */
b5ff1b31
FB
9734 offset = 0;
9735 break;
06c949e6 9736 case EXCP_BKPT:
9ee6e8bb
PB
9737 /* Fall through to prefetch abort. */
9738 case EXCP_PREFETCH_ABORT:
88ca1c2d 9739 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 9740 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 9741 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 9742 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9743 new_mode = ARM_CPU_MODE_ABT;
9744 addr = 0x0c;
9745 mask = CPSR_A | CPSR_I;
9746 offset = 4;
9747 break;
9748 case EXCP_DATA_ABORT:
4a7e2d73 9749 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 9750 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 9751 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 9752 env->exception.fsr,
6cd8a264 9753 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
9754 new_mode = ARM_CPU_MODE_ABT;
9755 addr = 0x10;
9756 mask = CPSR_A | CPSR_I;
9757 offset = 8;
9758 break;
9759 case EXCP_IRQ:
9760 new_mode = ARM_CPU_MODE_IRQ;
9761 addr = 0x18;
9762 /* Disable IRQ and imprecise data aborts. */
9763 mask = CPSR_A | CPSR_I;
9764 offset = 4;
de38d23b
FA
9765 if (env->cp15.scr_el3 & SCR_IRQ) {
9766 /* IRQ routed to monitor mode */
9767 new_mode = ARM_CPU_MODE_MON;
9768 mask |= CPSR_F;
9769 }
b5ff1b31
FB
9770 break;
9771 case EXCP_FIQ:
9772 new_mode = ARM_CPU_MODE_FIQ;
9773 addr = 0x1c;
9774 /* Disable FIQ, IRQ and imprecise data aborts. */
9775 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
9776 if (env->cp15.scr_el3 & SCR_FIQ) {
9777 /* FIQ routed to monitor mode */
9778 new_mode = ARM_CPU_MODE_MON;
9779 }
b5ff1b31
FB
9780 offset = 4;
9781 break;
87a4b270
PM
9782 case EXCP_VIRQ:
9783 new_mode = ARM_CPU_MODE_IRQ;
9784 addr = 0x18;
9785 /* Disable IRQ and imprecise data aborts. */
9786 mask = CPSR_A | CPSR_I;
9787 offset = 4;
9788 break;
9789 case EXCP_VFIQ:
9790 new_mode = ARM_CPU_MODE_FIQ;
9791 addr = 0x1c;
9792 /* Disable FIQ, IRQ and imprecise data aborts. */
9793 mask = CPSR_A | CPSR_I | CPSR_F;
9794 offset = 4;
9795 break;
3c29632f
RH
9796 case EXCP_VSERR:
9797 {
9798 /*
9799 * Note that this is reported as a data abort, but the DFAR
9800 * has an UNKNOWN value. Construct the SError syndrome from
9801 * AET and ExT fields.
9802 */
9803 ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
9804
9805 if (extended_addresses_enabled(env)) {
9806 env->exception.fsr = arm_fi_to_lfsc(&fi);
9807 } else {
9808 env->exception.fsr = arm_fi_to_sfsc(&fi);
9809 }
9810 env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
9811 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
9812 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
9813 env->exception.fsr);
9814
9815 new_mode = ARM_CPU_MODE_ABT;
9816 addr = 0x10;
9817 mask = CPSR_A | CPSR_I;
9818 offset = 8;
9819 }
9820 break;
dbe9d163
FA
9821 case EXCP_SMC:
9822 new_mode = ARM_CPU_MODE_MON;
9823 addr = 0x08;
9824 mask = CPSR_A | CPSR_I | CPSR_F;
9825 offset = 0;
9826 break;
b5ff1b31 9827 default:
a47dddd7 9828 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
9829 return; /* Never happens. Keep compiler happy. */
9830 }
e89e51a1
FA
9831
9832 if (new_mode == ARM_CPU_MODE_MON) {
9833 addr += env->cp15.mvbar;
137feaa9 9834 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 9835 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 9836 addr += 0xffff0000;
8641136c
NR
9837 } else {
9838 /* ARM v7 architectures provide a vector base address register to remap
9839 * the interrupt vector table.
e89e51a1 9840 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
9841 * Note: only bits 31:5 are valid.
9842 */
fb6c91ba 9843 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 9844 }
dbe9d163
FA
9845
9846 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
9847 env->cp15.scr_el3 &= ~SCR_NS;
9848 }
9849
dea8378b 9850 take_aarch32_exception(env, new_mode, mask, offset, addr);
b5ff1b31
FB
9851}
9852
a65dabf7
PM
9853static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
9854{
9855 /*
9856 * Return the register number of the AArch64 view of the AArch32
9857 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
9858 * be that of the AArch32 mode the exception came from.
9859 */
9860 int mode = env->uncached_cpsr & CPSR_M;
9861
9862 switch (aarch32_reg) {
9863 case 0 ... 7:
9864 return aarch32_reg;
9865 case 8 ... 12:
9866 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
9867 case 13:
9868 switch (mode) {
9869 case ARM_CPU_MODE_USR:
9870 case ARM_CPU_MODE_SYS:
9871 return 13;
9872 case ARM_CPU_MODE_HYP:
9873 return 15;
9874 case ARM_CPU_MODE_IRQ:
9875 return 17;
9876 case ARM_CPU_MODE_SVC:
9877 return 19;
9878 case ARM_CPU_MODE_ABT:
9879 return 21;
9880 case ARM_CPU_MODE_UND:
9881 return 23;
9882 case ARM_CPU_MODE_FIQ:
9883 return 29;
9884 default:
9885 g_assert_not_reached();
9886 }
9887 case 14:
9888 switch (mode) {
9889 case ARM_CPU_MODE_USR:
9890 case ARM_CPU_MODE_SYS:
9891 case ARM_CPU_MODE_HYP:
9892 return 14;
9893 case ARM_CPU_MODE_IRQ:
9894 return 16;
9895 case ARM_CPU_MODE_SVC:
9896 return 18;
9897 case ARM_CPU_MODE_ABT:
9898 return 20;
9899 case ARM_CPU_MODE_UND:
9900 return 22;
9901 case ARM_CPU_MODE_FIQ:
9902 return 30;
9903 default:
9904 g_assert_not_reached();
9905 }
9906 case 15:
9907 return 31;
9908 default:
9909 g_assert_not_reached();
9910 }
9911}
9912
f944a854
RC
9913static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
9914{
9915 uint32_t ret = cpsr_read(env);
9916
9917 /* Move DIT to the correct location for SPSR_ELx */
9918 if (ret & CPSR_DIT) {
9919 ret &= ~CPSR_DIT;
9920 ret |= PSTATE_DIT;
9921 }
9922 /* Merge PSTATE.SS into SPSR_ELx */
9923 ret |= env->pstate & PSTATE_SS;
9924
9925 return ret;
9926}
9927
7ac61020
PM
9928static bool syndrome_is_sync_extabt(uint32_t syndrome)
9929{
9930 /* Return true if this syndrome value is a synchronous external abort */
9931 switch (syn_get_ec(syndrome)) {
9932 case EC_INSNABORT:
9933 case EC_INSNABORT_SAME_EL:
9934 case EC_DATAABORT:
9935 case EC_DATAABORT_SAME_EL:
9936 /* Look at fault status code for all the synchronous ext abort cases */
9937 switch (syndrome & 0x3f) {
9938 case 0x10:
9939 case 0x13:
9940 case 0x14:
9941 case 0x15:
9942 case 0x16:
9943 case 0x17:
9944 return true;
9945 default:
9946 return false;
9947 }
9948 default:
9949 return false;
9950 }
9951}
9952
966f758c
PM
9953/* Handle exception entry to a target EL which is using AArch64 */
9954static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
9955{
9956 ARMCPU *cpu = ARM_CPU(cs);
9957 CPUARMState *env = &cpu->env;
9958 unsigned int new_el = env->exception.target_el;
9959 target_ulong addr = env->cp15.vbar_el[new_el];
9960 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
4a2696c0 9961 unsigned int old_mode;
0ab5953b 9962 unsigned int cur_el = arm_current_el(env);
a65dabf7 9963 int rt;
0ab5953b 9964
9a05f7b6
RH
9965 /*
9966 * Note that new_el can never be 0. If cur_el is 0, then
9967 * el0_a64 is is_a64(), else el0_a64 is ignored.
9968 */
9969 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
f3a9b694 9970
0ab5953b 9971 if (cur_el < new_el) {
3d6f7617
PM
9972 /* Entry vector offset depends on whether the implemented EL
9973 * immediately lower than the target level is using AArch32 or AArch64
9974 */
9975 bool is_aa64;
cb092fbb 9976 uint64_t hcr;
3d6f7617
PM
9977
9978 switch (new_el) {
9979 case 3:
9980 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
9981 break;
9982 case 2:
cb092fbb
RH
9983 hcr = arm_hcr_el2_eff(env);
9984 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
9985 is_aa64 = (hcr & HCR_RW) != 0;
9986 break;
9987 }
9988 /* fall through */
3d6f7617
PM
9989 case 1:
9990 is_aa64 = is_a64(env);
9991 break;
9992 default:
9993 g_assert_not_reached();
9994 }
9995
9996 if (is_aa64) {
f3a9b694
PM
9997 addr += 0x400;
9998 } else {
9999 addr += 0x600;
10000 }
10001 } else if (pstate_read(env) & PSTATE_SP) {
10002 addr += 0x200;
10003 }
10004
f3a9b694
PM
10005 switch (cs->exception_index) {
10006 case EXCP_PREFETCH_ABORT:
10007 case EXCP_DATA_ABORT:
7ac61020
PM
10008 /*
10009 * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10010 * to be taken to the SError vector entrypoint.
10011 */
10012 if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10013 syndrome_is_sync_extabt(env->exception.syndrome)) {
10014 addr += 0x180;
10015 }
f3a9b694
PM
10016 env->cp15.far_el[new_el] = env->exception.vaddress;
10017 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10018 env->cp15.far_el[new_el]);
10019 /* fall through */
10020 case EXCP_BKPT:
10021 case EXCP_UDEF:
10022 case EXCP_SWI:
10023 case EXCP_HVC:
10024 case EXCP_HYP_TRAP:
10025 case EXCP_SMC:
a65dabf7
PM
10026 switch (syn_get_ec(env->exception.syndrome)) {
10027 case EC_ADVSIMDFPACCESSTRAP:
4be42f40
PM
10028 /*
10029 * QEMU internal FP/SIMD syndromes from AArch32 include the
10030 * TA and coproc fields which are only exposed if the exception
10031 * is taken to AArch32 Hyp mode. Mask them out to get a valid
10032 * AArch64 format syndrome.
10033 */
10034 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
a65dabf7
PM
10035 break;
10036 case EC_CP14RTTRAP:
10037 case EC_CP15RTTRAP:
10038 case EC_CP14DTTRAP:
10039 /*
10040 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10041 * the raw register field from the insn; when taking this to
10042 * AArch64 we must convert it to the AArch64 view of the register
10043 * number. Notice that we read a 4-bit AArch32 register number and
10044 * write back a 5-bit AArch64 one.
10045 */
10046 rt = extract32(env->exception.syndrome, 5, 4);
10047 rt = aarch64_regnum(env, rt);
10048 env->exception.syndrome = deposit32(env->exception.syndrome,
10049 5, 5, rt);
10050 break;
10051 case EC_CP15RRTTRAP:
10052 case EC_CP14RRTTRAP:
10053 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10054 rt = extract32(env->exception.syndrome, 5, 4);
10055 rt = aarch64_regnum(env, rt);
10056 env->exception.syndrome = deposit32(env->exception.syndrome,
10057 5, 5, rt);
10058 rt = extract32(env->exception.syndrome, 10, 4);
10059 rt = aarch64_regnum(env, rt);
10060 env->exception.syndrome = deposit32(env->exception.syndrome,
10061 10, 5, rt);
10062 break;
4be42f40 10063 }
f3a9b694
PM
10064 env->cp15.esr_el[new_el] = env->exception.syndrome;
10065 break;
10066 case EXCP_IRQ:
10067 case EXCP_VIRQ:
10068 addr += 0x80;
10069 break;
10070 case EXCP_FIQ:
10071 case EXCP_VFIQ:
10072 addr += 0x100;
10073 break;
3c29632f
RH
10074 case EXCP_VSERR:
10075 addr += 0x180;
10076 /* Construct the SError syndrome from IDS and ISS fields. */
10077 env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10078 env->cp15.esr_el[new_el] = env->exception.syndrome;
10079 break;
f3a9b694
PM
10080 default:
10081 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10082 }
10083
10084 if (is_a64(env)) {
4a2696c0 10085 old_mode = pstate_read(env);
f3a9b694
PM
10086 aarch64_save_sp(env, arm_current_el(env));
10087 env->elr_el[new_el] = env->pc;
10088 } else {
f944a854 10089 old_mode = cpsr_read_for_spsr_elx(env);
f3a9b694
PM
10090 env->elr_el[new_el] = env->regs[15];
10091
10092 aarch64_sync_32_to_64(env);
10093
10094 env->condexec_bits = 0;
10095 }
4a2696c0
RH
10096 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10097
f3a9b694
PM
10098 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10099 env->elr_el[new_el]);
10100
4a2696c0
RH
10101 if (cpu_isar_feature(aa64_pan, cpu)) {
10102 /* The value of PSTATE.PAN is normally preserved, except when ... */
10103 new_mode |= old_mode & PSTATE_PAN;
10104 switch (new_el) {
10105 case 2:
10106 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
10107 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10108 != (HCR_E2H | HCR_TGE)) {
10109 break;
10110 }
10111 /* fall through */
10112 case 1:
10113 /* ... the target is EL1 ... */
10114 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
10115 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10116 new_mode |= PSTATE_PAN;
10117 }
10118 break;
10119 }
10120 }
34669338
RH
10121 if (cpu_isar_feature(aa64_mte, cpu)) {
10122 new_mode |= PSTATE_TCO;
10123 }
4a2696c0 10124
f2f68a78
RC
10125 if (cpu_isar_feature(aa64_ssbs, cpu)) {
10126 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10127 new_mode |= PSTATE_SSBS;
10128 } else {
10129 new_mode &= ~PSTATE_SSBS;
10130 }
10131 }
10132
f3a9b694 10133 pstate_write(env, PSTATE_DAIF | new_mode);
53221552 10134 env->aarch64 = true;
f3a9b694 10135 aarch64_restore_sp(env, new_el);
a8a79c7a 10136 helper_rebuild_hflags_a64(env, new_el);
f3a9b694
PM
10137
10138 env->pc = addr;
10139
10140 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10141 new_el, env->pc, pstate_read(env));
966f758c
PM
10142}
10143
ed6e6ba9
AB
10144/*
10145 * Do semihosting call and set the appropriate return value. All the
10146 * permission and validity checks have been done at translate time.
10147 *
10148 * We only see semihosting exceptions in TCG only as they are not
10149 * trapped to the hypervisor in KVM.
10150 */
91f78c58 10151#ifdef CONFIG_TCG
ed6e6ba9
AB
10152static void handle_semihosting(CPUState *cs)
10153{
904c04de
PM
10154 ARMCPU *cpu = ARM_CPU(cs);
10155 CPUARMState *env = &cpu->env;
10156
10157 if (is_a64(env)) {
ed6e6ba9
AB
10158 qemu_log_mask(CPU_LOG_INT,
10159 "...handling as semihosting call 0x%" PRIx64 "\n",
10160 env->xregs[0]);
ed3a06b1 10161 do_common_semihosting(cs);
4ff5ef9e 10162 env->pc += 4;
904c04de 10163 } else {
904c04de
PM
10164 qemu_log_mask(CPU_LOG_INT,
10165 "...handling as semihosting call 0x%x\n",
10166 env->regs[0]);
ed3a06b1 10167 do_common_semihosting(cs);
4ff5ef9e 10168 env->regs[15] += env->thumb ? 2 : 4;
904c04de
PM
10169 }
10170}
ed6e6ba9 10171#endif
904c04de 10172
966f758c
PM
10173/* Handle a CPU exception for A and R profile CPUs.
10174 * Do any appropriate logging, handle PSCI calls, and then hand off
10175 * to the AArch64-entry or AArch32-entry function depending on the
10176 * target exception level's register width.
853bfef4
CF
10177 *
10178 * Note: this is used for both TCG (as the do_interrupt tcg op),
10179 * and KVM to re-inject guest debug exceptions, and to
10180 * inject a Synchronous-External-Abort.
966f758c
PM
10181 */
10182void arm_cpu_do_interrupt(CPUState *cs)
10183{
10184 ARMCPU *cpu = ARM_CPU(cs);
10185 CPUARMState *env = &cpu->env;
10186 unsigned int new_el = env->exception.target_el;
10187
531c60a9 10188 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c 10189
fc6177af 10190 arm_log_exception(cs);
966f758c
PM
10191 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10192 new_el);
10193 if (qemu_loglevel_mask(CPU_LOG_INT)
10194 && !excp_is_internal(cs->exception_index)) {
6568da45 10195 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
64b91e3f 10196 syn_get_ec(env->exception.syndrome),
966f758c
PM
10197 env->exception.syndrome);
10198 }
10199
10200 if (arm_is_psci_call(cpu, cs->exception_index)) {
10201 arm_handle_psci_call(cpu);
10202 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10203 return;
10204 }
10205
ed6e6ba9
AB
10206 /*
10207 * Semihosting semantics depend on the register width of the code
10208 * that caused the exception, not the target exception level, so
10209 * must be handled here.
966f758c 10210 */
ed6e6ba9
AB
10211#ifdef CONFIG_TCG
10212 if (cs->exception_index == EXCP_SEMIHOST) {
10213 handle_semihosting(cs);
904c04de
PM
10214 return;
10215 }
ed6e6ba9 10216#endif
904c04de 10217
b5c53d1b
AL
10218 /* Hooks may change global state so BQL should be held, also the
10219 * BQL needs to be held for any modification of
10220 * cs->interrupt_request.
10221 */
10222 g_assert(qemu_mutex_iothread_locked());
10223
10224 arm_call_pre_el_change_hook(cpu);
10225
904c04de
PM
10226 assert(!excp_is_internal(cs->exception_index));
10227 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
10228 arm_cpu_do_interrupt_aarch64(cs);
10229 } else {
10230 arm_cpu_do_interrupt_aarch32(cs);
10231 }
f3a9b694 10232
bd7d00fc
PM
10233 arm_call_el_change_hook(cpu);
10234
f3a9b694
PM
10235 if (!kvm_enabled()) {
10236 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10237 }
10238}
c47eaf9f 10239#endif /* !CONFIG_USER_ONLY */
0480f69a 10240
aaec1432
RH
10241uint64_t arm_sctlr(CPUARMState *env, int el)
10242{
10243 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
10244 if (el == 0) {
10245 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
d902ae75 10246 el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
aaec1432
RH
10247 }
10248 return env->cp15.sctlr_el[el];
10249}
c47eaf9f 10250
8ae08860 10251int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
b830a5ee
RH
10252{
10253 if (regime_has_2_ranges(mmu_idx)) {
10254 return extract64(tcr, 37, 2);
b1a10c86 10255 } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
b830a5ee
RH
10256 return 0; /* VTCR_EL2 */
10257 } else {
3e270f67
RH
10258 /* Replicate the single TBI bit so we always have 2 bits. */
10259 return extract32(tcr, 20, 1) * 3;
b830a5ee
RH
10260 }
10261}
10262
8ae08860 10263int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
b830a5ee
RH
10264{
10265 if (regime_has_2_ranges(mmu_idx)) {
10266 return extract64(tcr, 51, 2);
b1a10c86 10267 } else if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
b830a5ee
RH
10268 return 0; /* VTCR_EL2 */
10269 } else {
3e270f67
RH
10270 /* Replicate the single TBID bit so we always have 2 bits. */
10271 return extract32(tcr, 29, 1) * 3;
b830a5ee
RH
10272 }
10273}
10274
81ae05fa
RH
10275static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10276{
10277 if (regime_has_2_ranges(mmu_idx)) {
10278 return extract64(tcr, 57, 2);
10279 } else {
10280 /* Replicate the single TCMA bit so we always have 2 bits. */
10281 return extract32(tcr, 30, 1) * 3;
10282 }
10283}
10284
b830a5ee
RH
10285ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
10286 ARMMMUIdx mmu_idx, bool data)
ba97be9f 10287{
c1547bba 10288 uint64_t tcr = regime_tcr(env, mmu_idx);
ef56c242
RH
10289 bool epd, hpd, using16k, using64k, tsz_oob, ds;
10290 int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
10291 ARMCPU *cpu = env_archcpu(env);
ba97be9f 10292
339370b9 10293 if (!regime_has_2_ranges(mmu_idx)) {
71d18164 10294 select = 0;
ba97be9f
RH
10295 tsz = extract32(tcr, 0, 6);
10296 using64k = extract32(tcr, 14, 1);
10297 using16k = extract32(tcr, 15, 1);
b1a10c86 10298 if (mmu_idx == ARMMMUIdx_Stage2 || mmu_idx == ARMMMUIdx_Stage2_S) {
ba97be9f 10299 /* VTCR_EL2 */
b830a5ee 10300 hpd = false;
ba97be9f 10301 } else {
ba97be9f
RH
10302 hpd = extract32(tcr, 24, 1);
10303 }
10304 epd = false;
ef56c242 10305 sh = extract32(tcr, 12, 2);
f4ecc015 10306 ps = extract32(tcr, 16, 3);
ef56c242 10307 ds = extract64(tcr, 32, 1);
ba97be9f 10308 } else {
71d18164
RH
10309 /*
10310 * Bit 55 is always between the two regions, and is canonical for
10311 * determining if address tagging is enabled.
10312 */
10313 select = extract64(va, 55, 1);
10314 if (!select) {
10315 tsz = extract32(tcr, 0, 6);
10316 epd = extract32(tcr, 7, 1);
ef56c242 10317 sh = extract32(tcr, 12, 2);
71d18164
RH
10318 using64k = extract32(tcr, 14, 1);
10319 using16k = extract32(tcr, 15, 1);
71d18164 10320 hpd = extract64(tcr, 41, 1);
71d18164
RH
10321 } else {
10322 int tg = extract32(tcr, 30, 2);
10323 using16k = tg == 1;
10324 using64k = tg == 3;
10325 tsz = extract32(tcr, 16, 6);
10326 epd = extract32(tcr, 23, 1);
ef56c242 10327 sh = extract32(tcr, 28, 2);
71d18164 10328 hpd = extract64(tcr, 42, 1);
71d18164 10329 }
f4ecc015 10330 ps = extract64(tcr, 32, 3);
ef56c242 10331 ds = extract64(tcr, 59, 1);
ba97be9f 10332 }
c36c65ea 10333
ef56c242 10334 if (cpu_isar_feature(aa64_st, cpu)) {
c36c65ea
RDC
10335 max_tsz = 48 - using64k;
10336 } else {
10337 max_tsz = 39;
10338 }
0af312b6 10339
ef56c242
RH
10340 /*
10341 * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
10342 * adjust the effective value of DS, as documented.
10343 */
0af312b6
RH
10344 min_tsz = 16;
10345 if (using64k) {
ef56c242
RH
10346 if (cpu_isar_feature(aa64_lva, cpu)) {
10347 min_tsz = 12;
10348 }
10349 ds = false;
10350 } else if (ds) {
10351 switch (mmu_idx) {
10352 case ARMMMUIdx_Stage2:
10353 case ARMMMUIdx_Stage2_S:
10354 if (using16k) {
10355 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
10356 } else {
10357 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
10358 }
10359 break;
10360 default:
10361 if (using16k) {
10362 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
10363 } else {
10364 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
10365 }
10366 break;
10367 }
10368 if (ds) {
0af312b6
RH
10369 min_tsz = 12;
10370 }
10371 }
c36c65ea 10372
ebf93ce7
RH
10373 if (tsz > max_tsz) {
10374 tsz = max_tsz;
10375 tsz_oob = true;
10376 } else if (tsz < min_tsz) {
10377 tsz = min_tsz;
10378 tsz_oob = true;
10379 } else {
10380 tsz_oob = false;
10381 }
ba97be9f 10382
b830a5ee
RH
10383 /* Present TBI as a composite with TBID. */
10384 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
10385 if (!data) {
10386 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
10387 }
10388 tbi = (tbi >> select) & 1;
10389
ba97be9f
RH
10390 return (ARMVAParameters) {
10391 .tsz = tsz,
f4ecc015 10392 .ps = ps,
ef56c242 10393 .sh = sh,
ba97be9f
RH
10394 .select = select,
10395 .tbi = tbi,
10396 .epd = epd,
10397 .hpd = hpd,
10398 .using16k = using16k,
10399 .using64k = using64k,
ebf93ce7 10400 .tsz_oob = tsz_oob,
ef56c242 10401 .ds = ds,
ba97be9f
RH
10402 };
10403}
10404
6ddbc6e4
PB
10405/* Note that signed overflow is undefined in C. The following routines are
10406 careful to use unsigned types where modulo arithmetic is required.
10407 Failure to do so _will_ break on newer gcc. */
10408
10409/* Signed saturating arithmetic. */
10410
1654b2d6 10411/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
10412static inline uint16_t add16_sat(uint16_t a, uint16_t b)
10413{
10414 uint16_t res;
10415
10416 res = a + b;
10417 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
10418 if (a & 0x8000)
10419 res = 0x8000;
10420 else
10421 res = 0x7fff;
10422 }
10423 return res;
10424}
10425
1654b2d6 10426/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
10427static inline uint8_t add8_sat(uint8_t a, uint8_t b)
10428{
10429 uint8_t res;
10430
10431 res = a + b;
10432 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
10433 if (a & 0x80)
10434 res = 0x80;
10435 else
10436 res = 0x7f;
10437 }
10438 return res;
10439}
10440
1654b2d6 10441/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
10442static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
10443{
10444 uint16_t res;
10445
10446 res = a - b;
10447 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
10448 if (a & 0x8000)
10449 res = 0x8000;
10450 else
10451 res = 0x7fff;
10452 }
10453 return res;
10454}
10455
1654b2d6 10456/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
10457static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
10458{
10459 uint8_t res;
10460
10461 res = a - b;
10462 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
10463 if (a & 0x80)
10464 res = 0x80;
10465 else
10466 res = 0x7f;
10467 }
10468 return res;
10469}
10470
10471#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
10472#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
10473#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
10474#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
10475#define PFX q
10476
10477#include "op_addsub.h"
10478
10479/* Unsigned saturating arithmetic. */
460a09c1 10480static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
10481{
10482 uint16_t res;
10483 res = a + b;
10484 if (res < a)
10485 res = 0xffff;
10486 return res;
10487}
10488
460a09c1 10489static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 10490{
4c4fd3f8 10491 if (a > b)
6ddbc6e4
PB
10492 return a - b;
10493 else
10494 return 0;
10495}
10496
10497static inline uint8_t add8_usat(uint8_t a, uint8_t b)
10498{
10499 uint8_t res;
10500 res = a + b;
10501 if (res < a)
10502 res = 0xff;
10503 return res;
10504}
10505
10506static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
10507{
4c4fd3f8 10508 if (a > b)
6ddbc6e4
PB
10509 return a - b;
10510 else
10511 return 0;
10512}
10513
10514#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
10515#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
10516#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
10517#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
10518#define PFX uq
10519
10520#include "op_addsub.h"
10521
10522/* Signed modulo arithmetic. */
10523#define SARITH16(a, b, n, op) do { \
10524 int32_t sum; \
db6e2e65 10525 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
10526 RESULT(sum, n, 16); \
10527 if (sum >= 0) \
10528 ge |= 3 << (n * 2); \
10529 } while(0)
10530
10531#define SARITH8(a, b, n, op) do { \
10532 int32_t sum; \
db6e2e65 10533 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
10534 RESULT(sum, n, 8); \
10535 if (sum >= 0) \
10536 ge |= 1 << n; \
10537 } while(0)
10538
10539
10540#define ADD16(a, b, n) SARITH16(a, b, n, +)
10541#define SUB16(a, b, n) SARITH16(a, b, n, -)
10542#define ADD8(a, b, n) SARITH8(a, b, n, +)
10543#define SUB8(a, b, n) SARITH8(a, b, n, -)
10544#define PFX s
10545#define ARITH_GE
10546
10547#include "op_addsub.h"
10548
10549/* Unsigned modulo arithmetic. */
10550#define ADD16(a, b, n) do { \
10551 uint32_t sum; \
10552 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
10553 RESULT(sum, n, 16); \
a87aa10b 10554 if ((sum >> 16) == 1) \
6ddbc6e4
PB
10555 ge |= 3 << (n * 2); \
10556 } while(0)
10557
10558#define ADD8(a, b, n) do { \
10559 uint32_t sum; \
10560 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
10561 RESULT(sum, n, 8); \
a87aa10b
AZ
10562 if ((sum >> 8) == 1) \
10563 ge |= 1 << n; \
6ddbc6e4
PB
10564 } while(0)
10565
10566#define SUB16(a, b, n) do { \
10567 uint32_t sum; \
10568 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
10569 RESULT(sum, n, 16); \
10570 if ((sum >> 16) == 0) \
10571 ge |= 3 << (n * 2); \
10572 } while(0)
10573
10574#define SUB8(a, b, n) do { \
10575 uint32_t sum; \
10576 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
10577 RESULT(sum, n, 8); \
10578 if ((sum >> 8) == 0) \
a87aa10b 10579 ge |= 1 << n; \
6ddbc6e4
PB
10580 } while(0)
10581
10582#define PFX u
10583#define ARITH_GE
10584
10585#include "op_addsub.h"
10586
10587/* Halved signed arithmetic. */
10588#define ADD16(a, b, n) \
10589 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
10590#define SUB16(a, b, n) \
10591 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
10592#define ADD8(a, b, n) \
10593 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
10594#define SUB8(a, b, n) \
10595 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
10596#define PFX sh
10597
10598#include "op_addsub.h"
10599
10600/* Halved unsigned arithmetic. */
10601#define ADD16(a, b, n) \
10602 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10603#define SUB16(a, b, n) \
10604 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
10605#define ADD8(a, b, n) \
10606 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10607#define SUB8(a, b, n) \
10608 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
10609#define PFX uh
10610
10611#include "op_addsub.h"
10612
10613static inline uint8_t do_usad(uint8_t a, uint8_t b)
10614{
10615 if (a > b)
10616 return a - b;
10617 else
10618 return b - a;
10619}
10620
10621/* Unsigned sum of absolute byte differences. */
10622uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
10623{
10624 uint32_t sum;
10625 sum = do_usad(a, b);
10626 sum += do_usad(a >> 8, b >> 8);
bdc3b6f5 10627 sum += do_usad(a >> 16, b >> 16);
6ddbc6e4
PB
10628 sum += do_usad(a >> 24, b >> 24);
10629 return sum;
10630}
10631
10632/* For ARMv6 SEL instruction. */
10633uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
10634{
10635 uint32_t mask;
10636
10637 mask = 0;
10638 if (flags & 1)
10639 mask |= 0xff;
10640 if (flags & 2)
10641 mask |= 0xff00;
10642 if (flags & 4)
10643 mask |= 0xff0000;
10644 if (flags & 8)
10645 mask |= 0xff000000;
10646 return (a & mask) | (b & ~mask);
10647}
10648
aa633469
PM
10649/* CRC helpers.
10650 * The upper bytes of val (above the number specified by 'bytes') must have
10651 * been zeroed out by the caller.
10652 */
eb0ecd5a
WN
10653uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
10654{
10655 uint8_t buf[4];
10656
aa633469 10657 stl_le_p(buf, val);
eb0ecd5a
WN
10658
10659 /* zlib crc32 converts the accumulator and output to one's complement. */
10660 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
10661}
10662
10663uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
10664{
10665 uint8_t buf[4];
10666
aa633469 10667 stl_le_p(buf, val);
eb0ecd5a
WN
10668
10669 /* Linux crc32c converts the output to one's complement. */
10670 return crc32c(acc, buf, bytes) ^ 0xffffffff;
10671}
a9e01311
RH
10672
10673/* Return the exception level to which FP-disabled exceptions should
10674 * be taken, or 0 if FP is enabled.
10675 */
ced31551 10676int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 10677{
55faa212 10678#ifndef CONFIG_USER_ONLY
d5a6fa2d
RH
10679 uint64_t hcr_el2;
10680
a9e01311
RH
10681 /* CPACR and the CPTR registers don't exist before v6, so FP is
10682 * always accessible
10683 */
10684 if (!arm_feature(env, ARM_FEATURE_V6)) {
10685 return 0;
10686 }
10687
d87513c0
PM
10688 if (arm_feature(env, ARM_FEATURE_M)) {
10689 /* CPACR can cause a NOCP UsageFault taken to current security state */
10690 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
10691 return 1;
10692 }
10693
10694 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
10695 if (!extract32(env->v7m.nsacr, 10, 1)) {
10696 /* FP insns cause a NOCP UsageFault taken to Secure */
10697 return 3;
10698 }
10699 }
10700
10701 return 0;
10702 }
10703
d5a6fa2d
RH
10704 hcr_el2 = arm_hcr_el2_eff(env);
10705
a9e01311
RH
10706 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit:
10707 * 0, 2 : trap EL0 and EL1/PL1 accesses
10708 * 1 : trap only EL0 accesses
10709 * 3 : trap no accesses
c2ddb7cf 10710 * This register is ignored if E2H+TGE are both set.
a9e01311 10711 */
d5a6fa2d 10712 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
fab8ad39 10713 int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
c2ddb7cf
RH
10714
10715 switch (fpen) {
02e1de14
RH
10716 case 1:
10717 if (cur_el != 0) {
10718 break;
10719 }
10720 /* fall through */
c2ddb7cf
RH
10721 case 0:
10722 case 2:
02e1de14
RH
10723 /* Trap from Secure PL0 or PL1 to Secure PL1. */
10724 if (!arm_el_is_aa64(env, 3)
10725 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
a9e01311
RH
10726 return 3;
10727 }
02e1de14 10728 if (cur_el <= 1) {
c2ddb7cf
RH
10729 return 1;
10730 }
10731 break;
a9e01311 10732 }
a9e01311
RH
10733 }
10734
fc1120a7
PM
10735 /*
10736 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
10737 * to control non-secure access to the FPU. It doesn't have any
10738 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
10739 */
10740 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
10741 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
10742 if (!extract32(env->cp15.nsacr, 10, 1)) {
10743 /* FP insns act as UNDEF */
10744 return cur_el == 2 ? 2 : 1;
10745 }
10746 }
10747
d5a6fa2d
RH
10748 /*
10749 * CPTR_EL2 is present in v7VE or v8, and changes format
10750 * with HCR_EL2.E2H (regardless of TGE).
a9e01311 10751 */
d5a6fa2d
RH
10752 if (cur_el <= 2) {
10753 if (hcr_el2 & HCR_E2H) {
fab8ad39 10754 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
d5a6fa2d
RH
10755 case 1:
10756 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
10757 break;
10758 }
10759 /* fall through */
10760 case 0:
10761 case 2:
10762 return 2;
10763 }
10764 } else if (arm_is_el2_enabled(env)) {
fab8ad39 10765 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
d5a6fa2d
RH
10766 return 2;
10767 }
10768 }
a9e01311
RH
10769 }
10770
10771 /* CPTR_EL3 : present in v8 */
fab8ad39 10772 if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
a9e01311
RH
10773 /* Trap all FP ops to EL3 */
10774 return 3;
10775 }
55faa212 10776#endif
a9e01311
RH
10777 return 0;
10778}
10779
b9f6033c
RH
10780/* Return the exception level we're running at if this is our mmu_idx */
10781int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
10782{
10783 if (mmu_idx & ARM_MMU_IDX_M) {
10784 return mmu_idx & ARM_MMU_IDX_M_PRIV;
10785 }
10786
10787 switch (mmu_idx) {
10788 case ARMMMUIdx_E10_0:
10789 case ARMMMUIdx_E20_0:
b9f6033c
RH
10790 return 0;
10791 case ARMMMUIdx_E10_1:
452ef8cb 10792 case ARMMMUIdx_E10_1_PAN:
b9f6033c
RH
10793 return 1;
10794 case ARMMMUIdx_E2:
10795 case ARMMMUIdx_E20_2:
452ef8cb 10796 case ARMMMUIdx_E20_2_PAN:
b9f6033c 10797 return 2;
d902ae75 10798 case ARMMMUIdx_E3:
b9f6033c
RH
10799 return 3;
10800 default:
10801 g_assert_not_reached();
10802 }
10803}
10804
7aab5a8c 10805#ifndef CONFIG_TCG
65e4655c
RH
10806ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
10807{
7aab5a8c 10808 g_assert_not_reached();
65e4655c 10809}
7aab5a8c 10810#endif
65e4655c 10811
164690b2 10812ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 10813{
b6ad6062
RDC
10814 ARMMMUIdx idx;
10815 uint64_t hcr;
10816
65e4655c 10817 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 10818 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
10819 }
10820
6003d980 10821 /* See ARM pseudo-function ELIsInHost. */
b9f6033c
RH
10822 switch (el) {
10823 case 0:
b6ad6062
RDC
10824 hcr = arm_hcr_el2_eff(env);
10825 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
10826 idx = ARMMMUIdx_E20_0;
10827 } else {
10828 idx = ARMMMUIdx_E10_0;
6003d980 10829 }
b6ad6062 10830 break;
b9f6033c 10831 case 1:
66412260 10832 if (env->pstate & PSTATE_PAN) {
b6ad6062
RDC
10833 idx = ARMMMUIdx_E10_1_PAN;
10834 } else {
10835 idx = ARMMMUIdx_E10_1;
66412260 10836 }
b6ad6062 10837 break;
b9f6033c 10838 case 2:
6003d980 10839 /* Note that TGE does not apply at EL2. */
b6ad6062 10840 if (arm_hcr_el2_eff(env) & HCR_E2H) {
66412260 10841 if (env->pstate & PSTATE_PAN) {
b6ad6062
RDC
10842 idx = ARMMMUIdx_E20_2_PAN;
10843 } else {
10844 idx = ARMMMUIdx_E20_2;
66412260 10845 }
b6ad6062
RDC
10846 } else {
10847 idx = ARMMMUIdx_E2;
6003d980 10848 }
b6ad6062 10849 break;
b9f6033c 10850 case 3:
d902ae75 10851 return ARMMMUIdx_E3;
b9f6033c
RH
10852 default:
10853 g_assert_not_reached();
65e4655c 10854 }
b6ad6062 10855
b6ad6062 10856 return idx;
50494a27
RH
10857}
10858
164690b2
RH
10859ARMMMUIdx arm_mmu_idx(CPUARMState *env)
10860{
10861 return arm_mmu_idx_el(env, arm_current_el(env));
10862}
10863
3902bfc6
RH
10864static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
10865 ARMMMUIdx mmu_idx,
10866 CPUARMTBFlags flags)
fdd1b228 10867{
a729a46b
RH
10868 DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
10869 DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
fdd1b228 10870
fdd1b228 10871 if (arm_singlestep_active(env)) {
a729a46b 10872 DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
fdd1b228
RH
10873 }
10874 return flags;
10875}
10876
3902bfc6
RH
10877static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
10878 ARMMMUIdx mmu_idx,
10879 CPUARMTBFlags flags)
43eccfb6 10880{
8061a649
RH
10881 bool sctlr_b = arm_sctlr_b(env);
10882
10883 if (sctlr_b) {
a729a46b 10884 DP_TBFLAG_A32(flags, SCTLR__B, 1);
8061a649
RH
10885 }
10886 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
a729a46b 10887 DP_TBFLAG_ANY(flags, BE_DATA, 1);
8061a649 10888 }
a729a46b 10889 DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
43eccfb6
RH
10890
10891 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
10892}
10893
3902bfc6
RH
10894static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
10895 ARMMMUIdx mmu_idx)
6e33ced5 10896{
3902bfc6 10897 CPUARMTBFlags flags = {};
4479ec30
RH
10898 uint32_t ccr = env->v7m.ccr[env->v7m.secure];
10899
10900 /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
10901 if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
10902 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
10903 }
6e33ced5
RH
10904
10905 if (arm_v7m_is_handler_mode(env)) {
a729a46b 10906 DP_TBFLAG_M32(flags, HANDLER, 1);
6e33ced5
RH
10907 }
10908
10909 /*
10910 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
10911 * is suppressing them because the requested execution priority
10912 * is less than 0.
10913 */
10914 if (arm_feature(env, ARM_FEATURE_V8) &&
10915 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
4479ec30 10916 (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
a729a46b 10917 DP_TBFLAG_M32(flags, STACKCHECK, 1);
6e33ced5
RH
10918 }
10919
a393dee0
RH
10920 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && env->v7m.secure) {
10921 DP_TBFLAG_M32(flags, SECURE, 1);
10922 }
10923
6e33ced5
RH
10924 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
10925}
10926
3902bfc6
RH
10927static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
10928 ARMMMUIdx mmu_idx)
c747224c 10929{
8480e933 10930 CPUARMTBFlags flags = {};
4479ec30
RH
10931 int el = arm_current_el(env);
10932
10933 if (arm_sctlr(env, el) & SCTLR_A) {
10934 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
10935 }
0a54d68e
RH
10936
10937 if (arm_el_is_aa64(env, 1)) {
a729a46b 10938 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 10939 }
5bb0a20b 10940
4479ec30 10941 if (el < 2 && env->cp15.hstr_el2 &&
5bb0a20b 10942 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
a729a46b 10943 DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
5bb0a20b
MZ
10944 }
10945
520d1621
PM
10946 if (env->uncached_cpsr & CPSR_IL) {
10947 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
10948 }
10949
75fe8356
RH
10950 /*
10951 * The SME exception we are testing for is raised via
10952 * AArch64.CheckFPAdvSIMDEnabled(), as called from
10953 * AArch32.CheckAdvSIMDOrFPEnabled().
10954 */
10955 if (el == 0
10956 && FIELD_EX64(env->svcr, SVCR, SM)
10957 && (!arm_is_el2_enabled(env)
10958 || (arm_el_is_aa64(env, 2) && !(env->cp15.hcr_el2 & HCR_TGE)))
10959 && arm_el_is_aa64(env, 1)
10960 && !sme_fa64(env, el)) {
10961 DP_TBFLAG_A32(flags, SME_TRAP_NONSTREAMING, 1);
10962 }
10963
83f4baef 10964 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
c747224c
RH
10965}
10966
3902bfc6
RH
10967static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
10968 ARMMMUIdx mmu_idx)
a9e01311 10969{
8480e933 10970 CPUARMTBFlags flags = {};
d4d7503a 10971 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
c1547bba 10972 uint64_t tcr = regime_tcr(env, mmu_idx);
d4d7503a
RH
10973 uint64_t sctlr;
10974 int tbii, tbid;
b9adaa70 10975
a729a46b 10976 DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
cd208a1c 10977
339370b9 10978 /* Get control bits for tagged addresses. */
b830a5ee
RH
10979 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
10980 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
5d8634f5 10981
a729a46b
RH
10982 DP_TBFLAG_A64(flags, TBII, tbii);
10983 DP_TBFLAG_A64(flags, TBID, tbid);
d4d7503a
RH
10984
10985 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
10986 int sve_el = sve_exception_el(env, el);
5d8634f5 10987
d4d7503a 10988 /*
397d922c
RH
10989 * If either FP or SVE are disabled, translator does not need len.
10990 * If SVE EL > FP EL, FP exception has precedence, and translator
10991 * does not need SVE EL. Save potential re-translations by forcing
10992 * the unneeded data to zero.
d4d7503a 10993 */
397d922c
RH
10994 if (fp_el != 0) {
10995 if (sve_el > fp_el) {
10996 sve_el = 0;
10997 }
10998 } else if (sve_el == 0) {
5ef3cc56 10999 DP_TBFLAG_A64(flags, VL, sve_vqm1_for_el(env, el));
5d8634f5 11000 }
a729a46b 11001 DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
d4d7503a 11002 }
6b2ca83e 11003 if (cpu_isar_feature(aa64_sme, env_archcpu(env))) {
5d7953ad 11004 int sme_el = sme_exception_el(env, el);
62151133 11005 bool sm = FIELD_EX64(env->svcr, SVCR, SM);
5d7953ad
RH
11006
11007 DP_TBFLAG_A64(flags, SMEEXC_EL, sme_el);
11008 if (sme_el == 0) {
11009 /* Similarly, do not compute SVL if SME is disabled. */
62151133
RH
11010 int svl = sve_vqm1_for_el_sm(env, el, true);
11011 DP_TBFLAG_A64(flags, SVL, svl);
11012 if (sm) {
11013 /* If SVE is disabled, we will not have set VL above. */
11014 DP_TBFLAG_A64(flags, VL, svl);
11015 }
5d7953ad 11016 }
62151133 11017 if (sm) {
a3637e88 11018 DP_TBFLAG_A64(flags, PSTATE_SM, 1);
75fe8356 11019 DP_TBFLAG_A64(flags, SME_TRAP_NONSTREAMING, !sme_fa64(env, el));
a3637e88
RH
11020 }
11021 DP_TBFLAG_A64(flags, PSTATE_ZA, FIELD_EX64(env->svcr, SVCR, ZA));
6b2ca83e 11022 }
1db5e96c 11023
aaec1432 11024 sctlr = regime_sctlr(env, stage1);
1db5e96c 11025
4479ec30
RH
11026 if (sctlr & SCTLR_A) {
11027 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11028 }
11029
8061a649 11030 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
a729a46b 11031 DP_TBFLAG_ANY(flags, BE_DATA, 1);
8061a649
RH
11032 }
11033
d4d7503a
RH
11034 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11035 /*
11036 * In order to save space in flags, we record only whether
11037 * pauth is "inactive", meaning all insns are implemented as
11038 * a nop, or "active" when some action must be performed.
11039 * The decision of which action to take is left to a helper.
11040 */
11041 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
a729a46b 11042 DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
1db5e96c 11043 }
d4d7503a 11044 }
0816ef1b 11045
d4d7503a
RH
11046 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11047 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
11048 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
a729a46b 11049 DP_TBFLAG_A64(flags, BT, 1);
0816ef1b 11050 }
d4d7503a 11051 }
08f1434a 11052
cc28fc30 11053 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
7a8014ab
RH
11054 if (!(env->pstate & PSTATE_UAO)) {
11055 switch (mmu_idx) {
11056 case ARMMMUIdx_E10_1:
11057 case ARMMMUIdx_E10_1_PAN:
7a8014ab 11058 /* TODO: ARMv8.3-NV */
a729a46b 11059 DP_TBFLAG_A64(flags, UNPRIV, 1);
7a8014ab
RH
11060 break;
11061 case ARMMMUIdx_E20_2:
11062 case ARMMMUIdx_E20_2_PAN:
7a8014ab
RH
11063 /*
11064 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
11065 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
11066 */
11067 if (env->cp15.hcr_el2 & HCR_TGE) {
a729a46b 11068 DP_TBFLAG_A64(flags, UNPRIV, 1);
7a8014ab
RH
11069 }
11070 break;
11071 default:
11072 break;
cc28fc30 11073 }
cc28fc30
RH
11074 }
11075
520d1621
PM
11076 if (env->pstate & PSTATE_IL) {
11077 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11078 }
11079
81ae05fa
RH
11080 if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
11081 /*
11082 * Set MTE_ACTIVE if any access may be Checked, and leave clear
11083 * if all accesses must be Unchecked:
11084 * 1) If no TBI, then there are no tags in the address to check,
11085 * 2) If Tag Check Override, then all accesses are Unchecked,
11086 * 3) If Tag Check Fail == 0, then Checked access have no effect,
11087 * 4) If no Allocation Tag Access, then all accesses are Unchecked.
11088 */
11089 if (allocation_tag_access_enabled(env, el, sctlr)) {
a729a46b 11090 DP_TBFLAG_A64(flags, ATA, 1);
81ae05fa
RH
11091 if (tbid
11092 && !(env->pstate & PSTATE_TCO)
11093 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
a729a46b 11094 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
81ae05fa
RH
11095 }
11096 }
11097 /* And again for unprivileged accesses, if required. */
a729a46b 11098 if (EX_TBFLAG_A64(flags, UNPRIV)
81ae05fa
RH
11099 && tbid
11100 && !(env->pstate & PSTATE_TCO)
2d928adf 11101 && (sctlr & SCTLR_TCF0)
81ae05fa 11102 && allocation_tag_access_enabled(env, 0, sctlr)) {
a729a46b 11103 DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
81ae05fa
RH
11104 }
11105 /* Cache TCMA as well as TBI. */
a729a46b 11106 DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
81ae05fa
RH
11107 }
11108
d4d7503a
RH
11109 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11110}
11111
3902bfc6 11112static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
3d74e2e9
RH
11113{
11114 int el = arm_current_el(env);
11115 int fp_el = fp_exception_el(env, el);
164690b2 11116 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3d74e2e9
RH
11117
11118 if (is_a64(env)) {
11119 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11120 } else if (arm_feature(env, ARM_FEATURE_M)) {
11121 return rebuild_hflags_m32(env, fp_el, mmu_idx);
11122 } else {
11123 return rebuild_hflags_a32(env, fp_el, mmu_idx);
11124 }
11125}
11126
11127void arm_rebuild_hflags(CPUARMState *env)
11128{
11129 env->hflags = rebuild_hflags_internal(env);
11130}
11131
19717e9b
PM
11132/*
11133 * If we have triggered a EL state change we can't rely on the
11134 * translator having passed it to us, we need to recompute.
11135 */
11136void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
11137{
11138 int el = arm_current_el(env);
11139 int fp_el = fp_exception_el(env, el);
11140 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3902bfc6 11141
19717e9b
PM
11142 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11143}
11144
14f3c588
RH
11145void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
11146{
11147 int fp_el = fp_exception_el(env, el);
11148 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11149
11150 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
11151}
11152
f80741d1
AB
11153/*
11154 * If we have triggered a EL state change we can't rely on the
563152e0 11155 * translator having passed it to us, we need to recompute.
f80741d1
AB
11156 */
11157void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
11158{
11159 int el = arm_current_el(env);
11160 int fp_el = fp_exception_el(env, el);
11161 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11162 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11163}
11164
14f3c588
RH
11165void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
11166{
11167 int fp_el = fp_exception_el(env, el);
11168 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11169
11170 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
11171}
11172
11173void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
11174{
11175 int fp_el = fp_exception_el(env, el);
11176 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
11177
11178 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
11179}
11180
0ee8b24a
PMD
11181static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
11182{
11183#ifdef CONFIG_DEBUG_TCG
3902bfc6
RH
11184 CPUARMTBFlags c = env->hflags;
11185 CPUARMTBFlags r = rebuild_hflags_internal(env);
0ee8b24a 11186
a378206a
RH
11187 if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
11188 fprintf(stderr, "TCG hflags mismatch "
11189 "(current:(0x%08x,0x" TARGET_FMT_lx ")"
11190 " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
11191 c.flags, c.flags2, r.flags, r.flags2);
0ee8b24a
PMD
11192 abort();
11193 }
11194#endif
11195}
11196
26702213
PM
11197static bool mve_no_pred(CPUARMState *env)
11198{
11199 /*
11200 * Return true if there is definitely no predication of MVE
11201 * instructions by VPR or LTPSIZE. (Returning false even if there
11202 * isn't any predication is OK; generated code will just be
11203 * a little worse.)
11204 * If the CPU does not implement MVE then this TB flag is always 0.
11205 *
11206 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11207 * logic in gen_update_fp_context() needs to be updated to match.
11208 *
11209 * We do not include the effect of the ECI bits here -- they are
11210 * tracked in other TB flags. This simplifies the logic for
11211 * "when did we emit code that changes the MVE_NO_PRED TB flag
11212 * and thus need to end the TB?".
11213 */
11214 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11215 return false;
11216 }
11217 if (env->v7m.vpr) {
11218 return false;
11219 }
11220 if (env->v7m.ltpsize < 4) {
11221 return false;
11222 }
11223 return true;
11224}
11225
d4d7503a
RH
11226void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
11227 target_ulong *cs_base, uint32_t *pflags)
11228{
3902bfc6 11229 CPUARMTBFlags flags;
d4d7503a 11230
0ee8b24a 11231 assert_hflags_rebuild_correctly(env);
3902bfc6 11232 flags = env->hflags;
3d74e2e9 11233
a729a46b 11234 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
d4d7503a 11235 *pc = env->pc;
d4d7503a 11236 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
a729a46b 11237 DP_TBFLAG_A64(flags, BTYPE, env->btype);
08f1434a 11238 }
a9e01311
RH
11239 } else {
11240 *pc = env->regs[15];
6e33ced5
RH
11241
11242 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
11243 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11244 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11245 != env->v7m.secure) {
a729a46b 11246 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
9550d1bd
RH
11247 }
11248
11249 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11250 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11251 (env->v7m.secure &&
11252 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11253 /*
11254 * ASPEN is set, but FPCA/SFPA indicate that there is no
11255 * active FP context; we must create a new FP context before
11256 * executing any FP insn.
11257 */
a729a46b 11258 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
9550d1bd
RH
11259 }
11260
11261 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11262 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
a729a46b 11263 DP_TBFLAG_M32(flags, LSPACT, 1);
9550d1bd 11264 }
26702213
PM
11265
11266 if (mve_no_pred(env)) {
11267 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11268 }
6e33ced5 11269 } else {
bbad7c62
RH
11270 /*
11271 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11272 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11273 */
11274 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
a729a46b 11275 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
bbad7c62 11276 } else {
a729a46b
RH
11277 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11278 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
bbad7c62 11279 }
0a54d68e 11280 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
a729a46b 11281 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 11282 }
6e33ced5
RH
11283 }
11284
a729a46b
RH
11285 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11286 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
d4d7503a 11287 }
a9e01311 11288
60e12c37
RH
11289 /*
11290 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
11291 * states defined in the ARM ARM for software singlestep:
11292 * SS_ACTIVE PSTATE.SS State
11293 * 0 x Inactive (the TB flag for SS is always 0)
11294 * 1 0 Active-pending
11295 * 1 1 Active-not-pending
ae6eb1e9 11296 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
a9e01311 11297 */
a729a46b
RH
11298 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11299 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
a9e01311 11300 }
a9e01311 11301
3902bfc6 11302 *pflags = flags.flags;
a378206a 11303 *cs_base = flags.flags2;
a9e01311 11304}
0ab5953b
RH
11305
11306#ifdef TARGET_AARCH64
11307/*
11308 * The manual says that when SVE is enabled and VQ is widened the
11309 * implementation is allowed to zero the previously inaccessible
11310 * portion of the registers. The corollary to that is that when
11311 * SVE is enabled and VQ is narrowed we are also allowed to zero
11312 * the now inaccessible portion of the registers.
11313 *
11314 * The intent of this is that no predicate bit beyond VQ is ever set.
11315 * Which means that some operations on predicate registers themselves
11316 * may operate on full uint64_t or even unrolled across the maximum
11317 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
11318 * may well be cheaper than conditionals to restrict the operation
11319 * to the relevant portion of a uint16_t[16].
11320 */
11321void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11322{
11323 int i, j;
11324 uint64_t pmask;
11325
11326 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 11327 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
11328
11329 /* Zap the high bits of the zregs. */
11330 for (i = 0; i < 32; i++) {
11331 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11332 }
11333
11334 /* Zap the high bits of the pregs and ffr. */
11335 pmask = 0;
11336 if (vq & 3) {
11337 pmask = ~(-1ULL << (16 * (vq & 3)));
11338 }
11339 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11340 for (i = 0; i < 17; ++i) {
11341 env->vfp.pregs[i].p[j] &= pmask;
11342 }
11343 pmask = 0;
11344 }
11345}
11346
6a775fd6
RH
11347static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
11348{
11349 int exc_el;
11350
11351 if (sm) {
11352 exc_el = sme_exception_el(env, el);
11353 } else {
11354 exc_el = sve_exception_el(env, el);
11355 }
11356 if (exc_el) {
11357 return 0; /* disabled */
11358 }
11359 return sve_vqm1_for_el_sm(env, el, sm);
11360}
11361
0ab5953b
RH
11362/*
11363 * Notice a change in SVE vector size when changing EL.
11364 */
9a05f7b6
RH
11365void aarch64_sve_change_el(CPUARMState *env, int old_el,
11366 int new_el, bool el0_a64)
0ab5953b 11367{
2fc0cc0e 11368 ARMCPU *cpu = env_archcpu(env);
0ab5953b 11369 int old_len, new_len;
6a775fd6 11370 bool old_a64, new_a64, sm;
0ab5953b
RH
11371
11372 /* Nothing to do if no SVE. */
cd208a1c 11373 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
11374 return;
11375 }
11376
11377 /* Nothing to do if FP is disabled in either EL. */
11378 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11379 return;
11380 }
11381
04fbce76
RH
11382 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11383 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11384
11385 /*
11386 * Both AArch64.TakeException and AArch64.ExceptionReturn
11387 * invoke ResetSVEState when taking an exception from, or
11388 * returning to, AArch32 state when PSTATE.SM is enabled.
11389 */
6a775fd6
RH
11390 sm = FIELD_EX64(env->svcr, SVCR, SM);
11391 if (old_a64 != new_a64 && sm) {
04fbce76
RH
11392 arm_reset_sve_state(env);
11393 return;
11394 }
11395
0ab5953b
RH
11396 /*
11397 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11398 * at ELx, or not available because the EL is in AArch32 state, then
11399 * for all purposes other than a direct read, the ZCR_ELx.LEN field
11400 * has an effective value of 0".
11401 *
11402 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11403 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11404 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
11405 * we already have the correct register contents when encountering the
11406 * vq0->vq0 transition between EL0->EL1.
11407 */
6a775fd6
RH
11408 old_len = new_len = 0;
11409 if (old_a64) {
11410 old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
11411 }
11412 if (new_a64) {
11413 new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
11414 }
0ab5953b
RH
11415
11416 /* When changing vector length, clear inaccessible state. */
11417 if (new_len < old_len) {
11418 aarch64_sve_narrow_vq(env, new_len + 1);
11419 }
11420}
11421#endif