]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/helper.c
target/arm: Set IL bit for pauth, SVE access, BTI trap syndromes
[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"
cd617484 10#include "qemu/log.h"
194cbc49 11#include "trace.h"
b5ff1b31 12#include "cpu.h"
ccd38087 13#include "internals.h"
5a534314 14#include "cpu-features.h"
2ef6175a 15#include "exec/helper-proto.h"
db725815 16#include "qemu/main-loop.h"
b8012ecf 17#include "qemu/timer.h"
1de7afc9 18#include "qemu/bitops.h"
eb0ecd5a 19#include "qemu/crc32c.h"
0442428a 20#include "qemu/qemu-print.h"
63c91552 21#include "exec/exec-all.h"
eb0ecd5a 22#include <zlib.h> /* For crc32 */
64552b6b 23#include "hw/irq.h"
740b1759 24#include "sysemu/cpu-timers.h"
f3a9b694 25#include "sysemu/kvm.h"
0c1aaa66 26#include "sysemu/tcg.h"
de390645
RH
27#include "qapi/error.h"
28#include "qemu/guest-random.h"
91f78c58 29#ifdef CONFIG_TCG
6b5fe137 30#include "semihosting/common-semi.h"
91f78c58 31#endif
cf7c6d10 32#include "cpregs.h"
0b03bdfc 33
352c98e5
LV
34#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
35
affdb64d
PM
36static void switch_mode(CPUARMState *env, int mode);
37
c4241c7d 38static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 39{
375421cc 40 assert(ri->fieldoffset);
67ed771d 41 if (cpreg_field_is_64bit(ri)) {
c4241c7d 42 return CPREG_FIELD64(env, ri);
22d9e1a9 43 } else {
c4241c7d 44 return CPREG_FIELD32(env, ri);
22d9e1a9 45 }
d4e6df63
PM
46}
47
f43ee493 48void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
d4e6df63 49{
375421cc 50 assert(ri->fieldoffset);
67ed771d 51 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
52 CPREG_FIELD64(env, ri) = value;
53 } else {
54 CPREG_FIELD32(env, ri) = value;
55 }
d4e6df63
PM
56}
57
11f136ee
FA
58static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
59{
60 return (char *)env + ri->fieldoffset;
61}
62
49a66191 63uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 64{
59a1c327 65 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 66 if (ri->type & ARM_CP_CONST) {
59a1c327 67 return ri->resetvalue;
721fae12 68 } else if (ri->raw_readfn) {
59a1c327 69 return ri->raw_readfn(env, ri);
721fae12 70 } else if (ri->readfn) {
59a1c327 71 return ri->readfn(env, ri);
721fae12 72 } else {
59a1c327 73 return raw_read(env, ri);
721fae12 74 }
721fae12
PM
75}
76
59a1c327 77static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 78 uint64_t v)
721fae12 79{
9b37a28c
FR
80 /*
81 * Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
82 * Note that constant registers are treated as write-ignored; the
83 * caller should check for success by whether a readback gives the
84 * value written.
85 */
86 if (ri->type & ARM_CP_CONST) {
59a1c327 87 return;
721fae12 88 } else if (ri->raw_writefn) {
c4241c7d 89 ri->raw_writefn(env, ri, v);
721fae12 90 } else if (ri->writefn) {
c4241c7d 91 ri->writefn(env, ri, v);
721fae12 92 } else {
afb2530f 93 raw_write(env, ri, v);
721fae12 94 }
721fae12
PM
95}
96
375421cc
PM
97static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
98{
9b37a28c
FR
99 /*
100 * Return true if the regdef would cause an assertion if you called
375421cc
PM
101 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
102 * program bug for it not to have the NO_RAW flag).
103 * NB that returning false here doesn't necessarily mean that calling
104 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
105 * read/write access functions which are safe for raw use" from "has
106 * read/write access functions which have side effects but has forgotten
107 * to provide raw access functions".
108 * The tests here line up with the conditions in read/write_raw_cp_reg()
109 * and assertions in raw_read()/raw_write().
110 */
111 if ((ri->type & ARM_CP_CONST) ||
112 ri->fieldoffset ||
113 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
114 return false;
115 }
116 return true;
117}
118
b698e4ee 119bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
721fae12
PM
120{
121 /* Write the coprocessor state from cpu->env to the (index,value) list. */
122 int i;
123 bool ok = true;
124
125 for (i = 0; i < cpu->cpreg_array_len; i++) {
126 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
127 const ARMCPRegInfo *ri;
b698e4ee 128 uint64_t newval;
59a1c327 129
60322b39 130 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
131 if (!ri) {
132 ok = false;
133 continue;
134 }
7a0e58fa 135 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
136 continue;
137 }
b698e4ee
PM
138
139 newval = read_raw_cp_reg(&cpu->env, ri);
140 if (kvm_sync) {
141 /*
142 * Only sync if the previous list->cpustate sync succeeded.
143 * Rather than tracking the success/failure state for every
144 * item in the list, we just recheck "does the raw write we must
145 * have made in write_list_to_cpustate() read back OK" here.
146 */
147 uint64_t oldval = cpu->cpreg_values[i];
148
149 if (oldval == newval) {
150 continue;
151 }
152
153 write_raw_cp_reg(&cpu->env, ri, oldval);
154 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
155 continue;
156 }
157
158 write_raw_cp_reg(&cpu->env, ri, newval);
159 }
160 cpu->cpreg_values[i] = newval;
721fae12
PM
161 }
162 return ok;
163}
164
165bool write_list_to_cpustate(ARMCPU *cpu)
166{
167 int i;
168 bool ok = true;
169
170 for (i = 0; i < cpu->cpreg_array_len; i++) {
171 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
172 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
173 const ARMCPRegInfo *ri;
174
60322b39 175 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
176 if (!ri) {
177 ok = false;
178 continue;
179 }
7a0e58fa 180 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
181 continue;
182 }
9b37a28c
FR
183 /*
184 * Write value and confirm it reads back as written
721fae12
PM
185 * (to catch read-only registers and partially read-only
186 * registers where the incoming migration value doesn't match)
187 */
59a1c327
PM
188 write_raw_cp_reg(&cpu->env, ri, v);
189 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
190 ok = false;
191 }
192 }
193 return ok;
194}
195
196static void add_cpreg_to_list(gpointer key, gpointer opaque)
197{
198 ARMCPU *cpu = opaque;
5860362d
RH
199 uint32_t regidx = (uintptr_t)key;
200 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 201
04215eb1 202 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
721fae12
PM
203 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
204 /* The value array need not be initialized at this point */
205 cpu->cpreg_array_len++;
206 }
207}
208
209static void count_cpreg(gpointer key, gpointer opaque)
210{
211 ARMCPU *cpu = opaque;
721fae12
PM
212 const ARMCPRegInfo *ri;
213
5860362d 214 ri = g_hash_table_lookup(cpu->cp_regs, key);
721fae12 215
04215eb1 216 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
721fae12
PM
217 cpu->cpreg_array_len++;
218 }
219}
220
221static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
222{
5860362d
RH
223 uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
224 uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
721fae12 225
cbf239b7
AR
226 if (aidx > bidx) {
227 return 1;
228 }
229 if (aidx < bidx) {
230 return -1;
231 }
232 return 0;
721fae12
PM
233}
234
235void init_cpreg_list(ARMCPU *cpu)
236{
9b37a28c
FR
237 /*
238 * Initialise the cpreg_tuples[] array based on the cp_regs hash.
721fae12
PM
239 * Note that we require cpreg_tuples[] to be sorted by key ID.
240 */
57b6d95e 241 GList *keys;
721fae12
PM
242 int arraylen;
243
57b6d95e 244 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
245 keys = g_list_sort(keys, cpreg_key_compare);
246
247 cpu->cpreg_array_len = 0;
248
249 g_list_foreach(keys, count_cpreg, cpu);
250
251 arraylen = cpu->cpreg_array_len;
252 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
253 cpu->cpreg_values = g_new(uint64_t, arraylen);
254 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
255 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
256 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
257 cpu->cpreg_array_len = 0;
258
259 g_list_foreach(keys, add_cpreg_to_list, cpu);
260
261 assert(cpu->cpreg_array_len == arraylen);
262
263 g_list_free(keys);
264}
265
68e9c2fe 266/*
93dd1e61 267 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
68e9c2fe
EI
268 */
269static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
270 const ARMCPRegInfo *ri,
271 bool isread)
68e9c2fe 272{
93dd1e61
EI
273 if (!is_a64(env) && arm_current_el(env) == 3 &&
274 arm_is_secure_below_el3(env)) {
68e9c2fe
EI
275 return CP_ACCESS_TRAP_UNCATEGORIZED;
276 }
277 return CP_ACCESS_OK;
278}
279
9b37a28c
FR
280/*
281 * Some secure-only AArch32 registers trap to EL3 if used from
5513c3ab
PM
282 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
283 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
284 * We assume that the .access field is set to PL1_RW.
285 */
286static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
287 const ARMCPRegInfo *ri,
288 bool isread)
5513c3ab
PM
289{
290 if (arm_current_el(env) == 3) {
291 return CP_ACCESS_OK;
292 }
293 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
294 if (env->cp15.scr_el3 & SCR_EEL2) {
295 return CP_ACCESS_TRAP_EL2;
296 }
5513c3ab
PM
297 return CP_ACCESS_TRAP_EL3;
298 }
299 /* This will be EL1 NS and EL2 NS, which just UNDEF */
300 return CP_ACCESS_TRAP_UNCATEGORIZED;
301}
302
9b37a28c
FR
303/*
304 * Check for traps to performance monitor registers, which are controlled
1fce1ba9
PM
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 322/* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
6d482423
RH
323CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
324 bool isread)
84929218
RH
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
0f66d223
PM
365/* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBIS. */
366static CPAccessResult access_ttlbis(CPUARMState *env, const ARMCPRegInfo *ri,
367 bool isread)
368{
369 if (arm_current_el(env) == 1 &&
370 (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBIS))) {
371 return CP_ACCESS_TRAP_EL2;
372 }
373 return CP_ACCESS_OK;
374}
375
fe3ca86c
PM
376#ifdef TARGET_AARCH64
377/* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBOS. */
378static CPAccessResult access_ttlbos(CPUARMState *env, const ARMCPRegInfo *ri,
379 bool isread)
380{
381 if (arm_current_el(env) == 1 &&
382 (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBOS))) {
383 return CP_ACCESS_TRAP_EL2;
384 }
385 return CP_ACCESS_OK;
386}
387#endif
388
c4241c7d 389static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 390{
2fc0cc0e 391 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 392
8d5c773e 393 raw_write(env, ri, value);
d10eb08f 394 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
395}
396
c4241c7d 397static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 398{
2fc0cc0e 399 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 400
8d5c773e 401 if (raw_read(env, ri) != value) {
9b37a28c
FR
402 /*
403 * Unlike real hardware the qemu TLB uses virtual addresses,
08de207b
PM
404 * not modified virtual addresses, so this causes a TLB flush.
405 */
d10eb08f 406 tlb_flush(CPU(cpu));
8d5c773e 407 raw_write(env, ri, value);
08de207b 408 }
08de207b 409}
c4241c7d
PM
410
411static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
412 uint64_t value)
08de207b 413{
2fc0cc0e 414 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 415
452a0955 416 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 417 && !extended_addresses_enabled(env)) {
9b37a28c
FR
418 /*
419 * For VMSA (when not using the LPAE long descriptor page table
08de207b
PM
420 * format) this register includes the ASID, so do a TLB flush.
421 * For PMSA it is purely a process ID and no action is needed.
422 */
d10eb08f 423 tlb_flush(CPU(cpu));
08de207b 424 }
8d5c773e 425 raw_write(env, ri, value);
08de207b
PM
426}
427
575a94af
RH
428static int alle1_tlbmask(CPUARMState *env)
429{
430 /*
431 * Note that the 'ALL' scope must invalidate both stage 1 and
432 * stage 2 translations, whereas most other scopes only invalidate
433 * stage 1 translations.
434 */
435 return (ARMMMUIdxBit_E10_1 |
436 ARMMMUIdxBit_E10_1_PAN |
437 ARMMMUIdxBit_E10_0 |
438 ARMMMUIdxBit_Stage2 |
439 ARMMMUIdxBit_Stage2_S);
440}
441
442
b4ab8ce9
PM
443/* IS variants of TLB operations must affect all cores */
444static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
445 uint64_t value)
446{
29a0af61 447 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
448
449 tlb_flush_all_cpus_synced(cs);
450}
451
452static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
453 uint64_t value)
454{
29a0af61 455 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
456
457 tlb_flush_all_cpus_synced(cs);
458}
459
460static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
461 uint64_t value)
462{
29a0af61 463 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
464
465 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
466}
467
468static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
469 uint64_t value)
470{
29a0af61 471 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
472
473 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
474}
475
476/*
477 * Non-IS variants of TLB operations are upgraded to
373e7ffd 478 * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
b4ab8ce9
PM
479 * force broadcast of these operations.
480 */
481static bool tlb_force_broadcast(CPUARMState *env)
482{
373e7ffd 483 return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
b4ab8ce9
PM
484}
485
c4241c7d
PM
486static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
487 uint64_t value)
d929823f
PM
488{
489 /* Invalidate all (TLBIALL) */
527db2be 490 CPUState *cs = env_cpu(env);
00c8cb0a 491
b4ab8ce9 492 if (tlb_force_broadcast(env)) {
527db2be
RH
493 tlb_flush_all_cpus_synced(cs);
494 } else {
495 tlb_flush(cs);
b4ab8ce9 496 }
d929823f
PM
497}
498
c4241c7d
PM
499static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
500 uint64_t value)
d929823f
PM
501{
502 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
527db2be 503 CPUState *cs = env_cpu(env);
31b030d4 504
527db2be 505 value &= TARGET_PAGE_MASK;
b4ab8ce9 506 if (tlb_force_broadcast(env)) {
527db2be
RH
507 tlb_flush_page_all_cpus_synced(cs, value);
508 } else {
509 tlb_flush_page(cs, value);
b4ab8ce9 510 }
d929823f
PM
511}
512
c4241c7d
PM
513static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
514 uint64_t value)
d929823f
PM
515{
516 /* Invalidate by ASID (TLBIASID) */
527db2be 517 CPUState *cs = env_cpu(env);
00c8cb0a 518
b4ab8ce9 519 if (tlb_force_broadcast(env)) {
527db2be
RH
520 tlb_flush_all_cpus_synced(cs);
521 } else {
522 tlb_flush(cs);
b4ab8ce9 523 }
d929823f
PM
524}
525
c4241c7d
PM
526static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
527 uint64_t value)
d929823f
PM
528{
529 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
527db2be 530 CPUState *cs = env_cpu(env);
31b030d4 531
527db2be 532 value &= TARGET_PAGE_MASK;
b4ab8ce9 533 if (tlb_force_broadcast(env)) {
527db2be
RH
534 tlb_flush_page_all_cpus_synced(cs, value);
535 } else {
536 tlb_flush_page(cs, value);
b4ab8ce9 537 }
fa439fc5
PM
538}
539
541ef8c2
SS
540static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
541 uint64_t value)
542{
29a0af61 543 CPUState *cs = env_cpu(env);
541ef8c2 544
575a94af 545 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
541ef8c2
SS
546}
547
548static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
549 uint64_t value)
550{
29a0af61 551 CPUState *cs = env_cpu(env);
541ef8c2 552
575a94af 553 tlb_flush_by_mmuidx_all_cpus_synced(cs, alle1_tlbmask(env));
541ef8c2
SS
554}
555
541ef8c2
SS
556
557static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
558 uint64_t value)
559{
29a0af61 560 CPUState *cs = env_cpu(env);
541ef8c2 561
e013b741 562 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
563}
564
565static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
566 uint64_t value)
567{
29a0af61 568 CPUState *cs = env_cpu(env);
541ef8c2 569
e013b741 570 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
571}
572
573static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
574 uint64_t value)
575{
29a0af61 576 CPUState *cs = env_cpu(env);
541ef8c2
SS
577 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
578
e013b741 579 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
541ef8c2
SS
580}
581
582static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
583 uint64_t value)
584{
29a0af61 585 CPUState *cs = env_cpu(env);
541ef8c2
SS
586 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
587
a67cf277 588 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 589 ARMMMUIdxBit_E2);
541ef8c2
SS
590}
591
575a94af
RH
592static void tlbiipas2_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
593 uint64_t value)
594{
595 CPUState *cs = env_cpu(env);
596 uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
597
598 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
599}
600
601static void tlbiipas2is_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
602 uint64_t value)
603{
604 CPUState *cs = env_cpu(env);
605 uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
606
607 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, ARMMMUIdxBit_Stage2);
608}
609
e9aa6c21 610static const ARMCPRegInfo cp_reginfo[] = {
9b37a28c
FR
611 /*
612 * Define the secure and non-secure FCSE identifier CP registers
54bf36ed
FA
613 * separately because there is no secure bank in V8 (no _EL3). This allows
614 * the secure register to be properly reset and migrated. There is also no
615 * v8 EL1 version of the register so the non-secure instance stands alone.
616 */
9c513e78 617 { .name = "FCSEIDR",
54bf36ed
FA
618 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
619 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
620 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
621 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9c513e78 622 { .name = "FCSEIDR_S",
54bf36ed
FA
623 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
624 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
625 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 626 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9b37a28c
FR
627 /*
628 * Define the secure and non-secure context identifier CP registers
54bf36ed
FA
629 * separately because there is no secure bank in V8 (no _EL3). This allows
630 * the secure register to be properly reset and migrated. In the
631 * non-secure case, the 32-bit register will have reset and migration
632 * disabled during registration as it is handled by the 64-bit instance.
633 */
634 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 635 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218 636 .access = PL1_RW, .accessfn = access_tvm_trvm,
158c276c 637 .fgt = FGT_CONTEXTIDR_EL1,
84929218 638 .secure = ARM_CP_SECSTATE_NS,
54bf36ed
FA
639 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
640 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9c513e78 641 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
54bf36ed 642 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
643 .access = PL1_RW, .accessfn = access_tvm_trvm,
644 .secure = ARM_CP_SECSTATE_S,
54bf36ed 645 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 646 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
647};
648
649static const ARMCPRegInfo not_v8_cp_reginfo[] = {
9b37a28c
FR
650 /*
651 * NB: Some of these registers exist in v8 but with more precise
9449fdf6
PM
652 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
653 */
654 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
655 { .name = "DACR",
656 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
84929218 657 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
658 .writefn = dacr_write, .raw_writefn = raw_write,
659 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
660 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
9b37a28c
FR
661 /*
662 * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
a903c449 663 * For v6 and v5, these mappings are overly broad.
4fdd17dd 664 */
a903c449
EI
665 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
666 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
667 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
668 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
669 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
670 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
671 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 672 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
673 /* Cache maintenance ops; some of this space may be overridden later. */
674 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
675 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
676 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
677};
678
7d57f408 679static const ARMCPRegInfo not_v6_cp_reginfo[] = {
9b37a28c
FR
680 /*
681 * Not all pre-v6 cores implemented this WFI, so this is slightly
7d57f408
PM
682 * over-broad.
683 */
684 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
685 .access = PL1_W, .type = ARM_CP_WFI },
7d57f408
PM
686};
687
688static const ARMCPRegInfo not_v7_cp_reginfo[] = {
9b37a28c
FR
689 /*
690 * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
7d57f408
PM
691 * is UNPREDICTABLE; we choose to NOP as most implementations do).
692 */
693 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
694 .access = PL1_W, .type = ARM_CP_WFI },
9b37a28c
FR
695 /*
696 * L1 cache lockdown. Not architectural in v6 and earlier but in practice
34f90529
PM
697 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
698 * OMAPCP will override this space.
699 */
700 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
701 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
702 .resetvalue = 0 },
703 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
704 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
705 .resetvalue = 0 },
776d4e5c
PM
706 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
707 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 708 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 709 .resetvalue = 0 },
9b37a28c
FR
710 /*
711 * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
50300698
PM
712 * implementing it as RAZ means the "debug architecture version" bits
713 * will read as a reserved value, which should cause Linux to not try
714 * to use the debug hardware.
715 */
716 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
717 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9b37a28c
FR
718 /*
719 * MMU TLB control. Note that the wildcarding means we cover not just
995939a6
PM
720 * the unified TLB ops but also the dside/iside/inner-shareable variants.
721 */
722 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
723 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 724 .type = ARM_CP_NO_RAW },
995939a6
PM
725 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
726 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 727 .type = ARM_CP_NO_RAW },
995939a6
PM
728 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
729 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 730 .type = ARM_CP_NO_RAW },
995939a6
PM
731 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
732 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 733 .type = ARM_CP_NO_RAW },
a903c449
EI
734 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
735 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
736 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
737 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
738};
739
c4241c7d
PM
740static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
741 uint64_t value)
2771db27 742{
f0aff255
FA
743 uint32_t mask = 0;
744
745 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
746 if (!arm_feature(env, ARM_FEATURE_V8)) {
9b37a28c
FR
747 /*
748 * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
f0aff255
FA
749 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
750 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
751 */
7fbc6a40 752 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
f0aff255 753 /* VFP coprocessor: cp10 & cp11 [23:20] */
fab8ad39
RH
754 mask |= R_CPACR_ASEDIS_MASK |
755 R_CPACR_D32DIS_MASK |
756 R_CPACR_CP11_MASK |
757 R_CPACR_CP10_MASK;
f0aff255
FA
758
759 if (!arm_feature(env, ARM_FEATURE_NEON)) {
760 /* ASEDIS [31] bit is RAO/WI */
fab8ad39 761 value |= R_CPACR_ASEDIS_MASK;
f0aff255
FA
762 }
763
9b37a28c
FR
764 /*
765 * VFPv3 and upwards with NEON implement 32 double precision
f0aff255
FA
766 * registers (D0-D31).
767 */
a6627f5f 768 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
f0aff255 769 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
fab8ad39 770 value |= R_CPACR_D32DIS_MASK;
f0aff255
FA
771 }
772 }
773 value &= mask;
2771db27 774 }
fc1120a7
PM
775
776 /*
777 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
778 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
779 */
780 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
781 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39
RH
782 mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
783 value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
fc1120a7
PM
784 }
785
7ebd5f2e 786 env->cp15.cpacr_el1 = value;
2771db27
PM
787}
788
fc1120a7
PM
789static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
790{
791 /*
792 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
793 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
794 */
795 uint64_t value = env->cp15.cpacr_el1;
796
797 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
798 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39 799 value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
fc1120a7
PM
800 }
801 return value;
802}
803
804
5deac39c
PM
805static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
806{
9b37a28c
FR
807 /*
808 * Call cpacr_write() so that we reset with the correct RAO bits set
5deac39c
PM
809 * for our CPU features.
810 */
811 cpacr_write(env, ri, 0);
812}
813
3f208fd7
PM
814static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
815 bool isread)
c6f19164
GB
816{
817 if (arm_feature(env, ARM_FEATURE_V8)) {
818 /* Check if CPACR accesses are to be trapped to EL2 */
e6ef0169 819 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
fab8ad39 820 FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
c6f19164
GB
821 return CP_ACCESS_TRAP_EL2;
822 /* Check if CPACR accesses are to be trapped to EL3 */
823 } else if (arm_current_el(env) < 3 &&
fab8ad39 824 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
c6f19164
GB
825 return CP_ACCESS_TRAP_EL3;
826 }
827 }
828
829 return CP_ACCESS_OK;
830}
831
3f208fd7
PM
832static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
833 bool isread)
c6f19164
GB
834{
835 /* Check if CPTR accesses are set to trap to EL3 */
fab8ad39
RH
836 if (arm_current_el(env) == 2 &&
837 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
c6f19164
GB
838 return CP_ACCESS_TRAP_EL3;
839 }
840
841 return CP_ACCESS_OK;
842}
843
7d57f408
PM
844static const ARMCPRegInfo v6_cp_reginfo[] = {
845 /* prefetch by MVA in v6, NOP in v7 */
846 { .name = "MVA_prefetch",
847 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
848 .access = PL1_W, .type = ARM_CP_NOP },
9b37a28c
FR
849 /*
850 * We need to break the TB after ISB to execute self-modifying code
6df99dec
SS
851 * correctly and also to take any pending interrupts immediately.
852 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
853 */
7d57f408 854 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 855 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 856 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 857 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 858 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 859 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 860 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218 861 .access = PL1_RW, .accessfn = access_tvm_trvm,
b848ce2b
FA
862 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
863 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31 864 .resetvalue = 0, },
9b37a28c
FR
865 /*
866 * Watchpoint Fault Address Register : should actually only be present
06d76f31
PM
867 * for 1136, 1176, 11MPCore.
868 */
869 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
870 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 871 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 872 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
b19ed03c 873 .fgt = FGT_CPACR_EL1,
7ebd5f2e 874 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
fc1120a7 875 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
7d57f408
PM
876};
877
57a4a11b
AL
878typedef struct pm_event {
879 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
880 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
881 bool (*supported)(CPUARMState *);
882 /*
883 * Retrieve the current count of the underlying event. The programmed
884 * counters hold a difference from the return value from this function
885 */
886 uint64_t (*get_count)(CPUARMState *);
4e7beb0c
AL
887 /*
888 * Return how many nanoseconds it will take (at a minimum) for count events
889 * to occur. A negative value indicates the counter will never overflow, or
890 * that the counter has otherwise arranged for the overflow bit to be set
891 * and the PMU interrupt to be raised on overflow.
892 */
893 int64_t (*ns_per_count)(uint64_t);
57a4a11b
AL
894} pm_event;
895
b2e23725
AL
896static bool event_always_supported(CPUARMState *env)
897{
898 return true;
899}
900
0d4bfd7d
AL
901static uint64_t swinc_get_count(CPUARMState *env)
902{
903 /*
904 * SW_INCR events are written directly to the pmevcntr's by writes to
905 * PMSWINC, so there is no underlying count maintained by the PMU itself
906 */
907 return 0;
908}
909
4e7beb0c
AL
910static int64_t swinc_ns_per(uint64_t ignored)
911{
912 return -1;
913}
914
b2e23725
AL
915/*
916 * Return the underlying cycle count for the PMU cycle counters. If we're in
917 * usermode, simply return 0.
918 */
919static uint64_t cycles_get_count(CPUARMState *env)
920{
921#ifndef CONFIG_USER_ONLY
922 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
923 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
924#else
925 return cpu_get_host_ticks();
926#endif
927}
928
929#ifndef CONFIG_USER_ONLY
4e7beb0c
AL
930static int64_t cycles_ns_per(uint64_t cycles)
931{
932 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
933}
934
b2e23725
AL
935static bool instructions_supported(CPUARMState *env)
936{
740b1759 937 return icount_enabled() == 1; /* Precise instruction counting */
b2e23725
AL
938}
939
940static uint64_t instructions_get_count(CPUARMState *env)
941{
8191d368 942 return (uint64_t)icount_get_raw();
b2e23725 943}
4e7beb0c
AL
944
945static int64_t instructions_ns_per(uint64_t icount)
946{
8191d368 947 return icount_to_ns((int64_t)icount);
4e7beb0c 948}
b2e23725
AL
949#endif
950
a793bcd0 951static bool pmuv3p1_events_supported(CPUARMState *env)
0727f63b
PM
952{
953 /* For events which are supported in any v8.1 PMU */
a793bcd0 954 return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
0727f63b
PM
955}
956
a793bcd0 957static bool pmuv3p4_events_supported(CPUARMState *env)
15dd1ebd
PM
958{
959 /* For events which are supported in any v8.1 PMU */
a793bcd0 960 return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
15dd1ebd
PM
961}
962
0727f63b
PM
963static uint64_t zero_event_get_count(CPUARMState *env)
964{
965 /* For events which on QEMU never fire, so their count is always zero */
966 return 0;
967}
968
969static int64_t zero_event_ns_per(uint64_t cycles)
970{
971 /* An event which never fires can never overflow */
972 return -1;
973}
974
57a4a11b 975static const pm_event pm_events[] = {
0d4bfd7d
AL
976 { .number = 0x000, /* SW_INCR */
977 .supported = event_always_supported,
978 .get_count = swinc_get_count,
4e7beb0c 979 .ns_per_count = swinc_ns_per,
0d4bfd7d 980 },
b2e23725
AL
981#ifndef CONFIG_USER_ONLY
982 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
983 .supported = instructions_supported,
984 .get_count = instructions_get_count,
4e7beb0c 985 .ns_per_count = instructions_ns_per,
b2e23725
AL
986 },
987 { .number = 0x011, /* CPU_CYCLES, Cycle */
988 .supported = event_always_supported,
989 .get_count = cycles_get_count,
4e7beb0c 990 .ns_per_count = cycles_ns_per,
0727f63b 991 },
b2e23725 992#endif
0727f63b 993 { .number = 0x023, /* STALL_FRONTEND */
a793bcd0 994 .supported = pmuv3p1_events_supported,
0727f63b
PM
995 .get_count = zero_event_get_count,
996 .ns_per_count = zero_event_ns_per,
997 },
998 { .number = 0x024, /* STALL_BACKEND */
a793bcd0 999 .supported = pmuv3p1_events_supported,
0727f63b
PM
1000 .get_count = zero_event_get_count,
1001 .ns_per_count = zero_event_ns_per,
1002 },
15dd1ebd 1003 { .number = 0x03c, /* STALL */
a793bcd0 1004 .supported = pmuv3p4_events_supported,
15dd1ebd
PM
1005 .get_count = zero_event_get_count,
1006 .ns_per_count = zero_event_ns_per,
1007 },
57a4a11b
AL
1008};
1009
1010/*
1011 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1012 * events (i.e. the statistical profiling extension), this implementation
1013 * should first be updated to something sparse instead of the current
1014 * supported_event_map[] array.
1015 */
15dd1ebd 1016#define MAX_EVENT_ID 0x3c
57a4a11b
AL
1017#define UNSUPPORTED_EVENT UINT16_MAX
1018static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1019
1020/*
bf8d0969
AL
1021 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1022 * of ARM event numbers to indices in our pm_events array.
57a4a11b
AL
1023 *
1024 * Note: Events in the 0x40XX range are not currently supported.
1025 */
bf8d0969 1026void pmu_init(ARMCPU *cpu)
57a4a11b 1027{
57a4a11b
AL
1028 unsigned int i;
1029
bf8d0969
AL
1030 /*
1031 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1032 * events to them
1033 */
57a4a11b
AL
1034 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1035 supported_event_map[i] = UNSUPPORTED_EVENT;
1036 }
bf8d0969
AL
1037 cpu->pmceid0 = 0;
1038 cpu->pmceid1 = 0;
57a4a11b
AL
1039
1040 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1041 const pm_event *cnt = &pm_events[i];
1042 assert(cnt->number <= MAX_EVENT_ID);
1043 /* We do not currently support events in the 0x40xx range */
1044 assert(cnt->number <= 0x3f);
1045
bf8d0969 1046 if (cnt->supported(&cpu->env)) {
57a4a11b 1047 supported_event_map[cnt->number] = i;
67da43d6 1048 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
bf8d0969
AL
1049 if (cnt->number & 0x20) {
1050 cpu->pmceid1 |= event_mask;
1051 } else {
1052 cpu->pmceid0 |= event_mask;
1053 }
57a4a11b
AL
1054 }
1055 }
57a4a11b
AL
1056}
1057
5ecdd3e4
AL
1058/*
1059 * Check at runtime whether a PMU event is supported for the current machine
1060 */
1061static bool event_supported(uint16_t number)
1062{
1063 if (number > MAX_EVENT_ID) {
1064 return false;
1065 }
1066 return supported_event_map[number] != UNSUPPORTED_EVENT;
1067}
1068
3f208fd7
PM
1069static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1070 bool isread)
200ac0ef 1071{
9b37a28c
FR
1072 /*
1073 * Performance monitor registers user accessibility is controlled
1fce1ba9
PM
1074 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1075 * trapping to EL2 or EL3 for other accesses.
200ac0ef 1076 */
1fce1ba9 1077 int el = arm_current_el(env);
59dd089c 1078 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1fce1ba9 1079
6ecd0b6b 1080 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 1081 return CP_ACCESS_TRAP;
200ac0ef 1082 }
59dd089c 1083 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1fce1ba9
PM
1084 return CP_ACCESS_TRAP_EL2;
1085 }
1086 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1087 return CP_ACCESS_TRAP_EL3;
1088 }
1089
fcd25206 1090 return CP_ACCESS_OK;
200ac0ef
PM
1091}
1092
6ecd0b6b
AB
1093static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1094 const ARMCPRegInfo *ri,
1095 bool isread)
1096{
1097 /* ER: event counter read trap control */
1098 if (arm_feature(env, ARM_FEATURE_V8)
1099 && arm_current_el(env) == 0
1100 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1101 && isread) {
1102 return CP_ACCESS_OK;
1103 }
1104
1105 return pmreg_access(env, ri, isread);
1106}
1107
1108static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1109 const ARMCPRegInfo *ri,
1110 bool isread)
1111{
1112 /* SW: software increment write trap control */
1113 if (arm_feature(env, ARM_FEATURE_V8)
1114 && arm_current_el(env) == 0
1115 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1116 && !isread) {
1117 return CP_ACCESS_OK;
1118 }
1119
1120 return pmreg_access(env, ri, isread);
1121}
1122
6ecd0b6b
AB
1123static CPAccessResult pmreg_access_selr(CPUARMState *env,
1124 const ARMCPRegInfo *ri,
1125 bool isread)
1126{
1127 /* ER: event counter read trap control */
1128 if (arm_feature(env, ARM_FEATURE_V8)
1129 && arm_current_el(env) == 0
1130 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1131 return CP_ACCESS_OK;
1132 }
1133
1134 return pmreg_access(env, ri, isread);
1135}
1136
1137static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1138 const ARMCPRegInfo *ri,
1139 bool isread)
1140{
1141 /* CR: cycle counter read trap control */
1142 if (arm_feature(env, ARM_FEATURE_V8)
1143 && arm_current_el(env) == 0
1144 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1145 && isread) {
1146 return CP_ACCESS_OK;
1147 }
1148
1149 return pmreg_access(env, ri, isread);
1150}
1151
01765386
PM
1152/*
1153 * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1154 * We use these to decide whether we need to wrap a write to MDCR_EL2
1155 * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1156 */
47b385da
PM
1157#define MDCR_EL2_PMU_ENABLE_BITS \
1158 (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
0b42f4fa 1159#define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
01765386 1160
9b37a28c
FR
1161/*
1162 * Returns true if the counter (pass 31 for PMCCNTR) should count events using
033614c4
AL
1163 * the current EL, security state, and register configuration.
1164 */
1165static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
87124fde 1166{
033614c4
AL
1167 uint64_t filter;
1168 bool e, p, u, nsk, nsu, nsh, m;
872d2034 1169 bool enabled, prohibited = false, filtered;
033614c4
AL
1170 bool secure = arm_is_secure(env);
1171 int el = arm_current_el(env);
59dd089c
RDC
1172 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1173 uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
87124fde 1174
cbbb3041
AJ
1175 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1176 return false;
1177 }
1178
033614c4
AL
1179 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1180 (counter < hpmn || counter == 31)) {
1181 e = env->cp15.c9_pmcr & PMCRE;
1182 } else {
59dd089c 1183 e = mdcr_el2 & MDCR_HPME;
87124fde 1184 }
033614c4 1185 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
87124fde 1186
872d2034
PM
1187 /* Is event counting prohibited? */
1188 if (el == 2 && (counter < hpmn || counter == 31)) {
1189 prohibited = mdcr_el2 & MDCR_HPMD;
1190 }
1191 if (secure) {
1192 prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
033614c4
AL
1193 }
1194
0b42f4fa
PM
1195 if (counter == 31) {
1196 /*
1197 * The cycle counter defaults to running. PMCR.DP says "disable
1198 * the cycle counter when event counting is prohibited".
1199 * Some MDCR bits disable the cycle counter specifically.
1200 */
1201 prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1202 if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1203 if (secure) {
1204 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1205 }
1206 if (el == 2) {
1207 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1208 }
1209 }
033614c4
AL
1210 }
1211
5ecdd3e4
AL
1212 if (counter == 31) {
1213 filter = env->cp15.pmccfiltr_el0;
1214 } else {
1215 filter = env->cp15.c14_pmevtyper[counter];
1216 }
033614c4
AL
1217
1218 p = filter & PMXEVTYPER_P;
1219 u = filter & PMXEVTYPER_U;
1220 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1221 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1222 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1223 m = arm_el_is_aa64(env, 1) &&
1224 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1225
1226 if (el == 0) {
1227 filtered = secure ? u : u != nsu;
1228 } else if (el == 1) {
1229 filtered = secure ? p : p != nsk;
1230 } else if (el == 2) {
1231 filtered = !nsh;
1232 } else { /* EL3 */
1233 filtered = m != p;
1234 }
1235
5ecdd3e4
AL
1236 if (counter != 31) {
1237 /*
1238 * If not checking PMCCNTR, ensure the counter is setup to an event we
1239 * support
1240 */
1241 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1242 if (!event_supported(event)) {
1243 return false;
1244 }
1245 }
1246
033614c4 1247 return enabled && !prohibited && !filtered;
87124fde 1248}
033614c4 1249
f4efb4b2
AL
1250static void pmu_update_irq(CPUARMState *env)
1251{
2fc0cc0e 1252 ARMCPU *cpu = env_archcpu(env);
f4efb4b2
AL
1253 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1254 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1255}
1256
b57aa7bd
PM
1257static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1258{
1259 /*
1260 * Return true if the clock divider is enabled and the cycle counter
1261 * is supposed to tick only once every 64 clock cycles. This is
1262 * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1263 * (64-bit) cycle counter PMCR.D has no effect.
1264 */
1265 return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1266}
1267
47b385da
PM
1268static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1269{
1270 /* Return true if the specified event counter is configured to be 64 bit */
1271
1272 /* This isn't intended to be used with the cycle counter */
1273 assert(counter < 31);
1274
1275 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1276 return false;
1277 }
1278
1279 if (arm_feature(env, ARM_FEATURE_EL2)) {
1280 /*
1281 * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1282 * current security state, so we don't use arm_mdcr_el2_eff() here.
1283 */
1284 bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1285 int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1286
3d80bbf1 1287 if (counter >= hpmn) {
47b385da
PM
1288 return hlp;
1289 }
1290 }
1291 return env->cp15.c9_pmcr & PMCRLP;
1292}
1293
5d05b9d4
AL
1294/*
1295 * Ensure c15_ccnt is the guest-visible count so that operations such as
1296 * enabling/disabling the counter or filtering, modifying the count itself,
1297 * etc. can be done logically. This is essentially a no-op if the counter is
1298 * not enabled at the time of the call.
1299 */
f2b2f53f 1300static void pmccntr_op_start(CPUARMState *env)
ec7b4ce4 1301{
b2e23725 1302 uint64_t cycles = cycles_get_count(env);
ec7b4ce4 1303
033614c4 1304 if (pmu_counter_enabled(env, 31)) {
5d05b9d4 1305 uint64_t eff_cycles = cycles;
b57aa7bd 1306 if (pmccntr_clockdiv_enabled(env)) {
5d05b9d4
AL
1307 eff_cycles /= 64;
1308 }
1309
f4efb4b2
AL
1310 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1311
1312 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1313 1ull << 63 : 1ull << 31;
1314 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
76e25d41 1315 env->cp15.c9_pmovsr |= (1ULL << 31);
f4efb4b2
AL
1316 pmu_update_irq(env);
1317 }
1318
1319 env->cp15.c15_ccnt = new_pmccntr;
ec7b4ce4 1320 }
5d05b9d4
AL
1321 env->cp15.c15_ccnt_delta = cycles;
1322}
ec7b4ce4 1323
5d05b9d4
AL
1324/*
1325 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1326 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1327 * pmccntr_op_start.
1328 */
f2b2f53f 1329static void pmccntr_op_finish(CPUARMState *env)
5d05b9d4 1330{
033614c4 1331 if (pmu_counter_enabled(env, 31)) {
4e7beb0c
AL
1332#ifndef CONFIG_USER_ONLY
1333 /* Calculate when the counter will next overflow */
1334 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1335 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1336 remaining_cycles = (uint32_t)remaining_cycles;
1337 }
1338 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1339
1340 if (overflow_in > 0) {
f1dd2506
PM
1341 int64_t overflow_at;
1342
1343 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1344 overflow_in, &overflow_at)) {
1345 ARMCPU *cpu = env_archcpu(env);
1346 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1347 }
4e7beb0c
AL
1348 }
1349#endif
5d05b9d4 1350
4e7beb0c 1351 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
b57aa7bd 1352 if (pmccntr_clockdiv_enabled(env)) {
5d05b9d4
AL
1353 prev_cycles /= 64;
1354 }
5d05b9d4 1355 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
ec7b4ce4
AF
1356 }
1357}
1358
5ecdd3e4
AL
1359static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1360{
1361
1362 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1363 uint64_t count = 0;
1364 if (event_supported(event)) {
1365 uint16_t event_idx = supported_event_map[event];
1366 count = pm_events[event_idx].get_count(env);
1367 }
1368
1369 if (pmu_counter_enabled(env, counter)) {
47b385da
PM
1370 uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1371 uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1372 1ULL << 63 : 1ULL << 31;
f4efb4b2 1373
47b385da 1374 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
f4efb4b2
AL
1375 env->cp15.c9_pmovsr |= (1 << counter);
1376 pmu_update_irq(env);
1377 }
1378 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
5ecdd3e4
AL
1379 }
1380 env->cp15.c14_pmevcntr_delta[counter] = count;
1381}
1382
1383static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1384{
1385 if (pmu_counter_enabled(env, counter)) {
4e7beb0c
AL
1386#ifndef CONFIG_USER_ONLY
1387 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1388 uint16_t event_idx = supported_event_map[event];
47b385da
PM
1389 uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1390 int64_t overflow_in;
1391
1392 if (!pmevcntr_is_64_bit(env, counter)) {
1393 delta = (uint32_t)delta;
1394 }
1395 overflow_in = pm_events[event_idx].ns_per_count(delta);
4e7beb0c
AL
1396
1397 if (overflow_in > 0) {
f1dd2506
PM
1398 int64_t overflow_at;
1399
1400 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1401 overflow_in, &overflow_at)) {
1402 ARMCPU *cpu = env_archcpu(env);
1403 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1404 }
4e7beb0c
AL
1405 }
1406#endif
1407
5ecdd3e4
AL
1408 env->cp15.c14_pmevcntr_delta[counter] -=
1409 env->cp15.c14_pmevcntr[counter];
1410 }
1411}
1412
5d05b9d4
AL
1413void pmu_op_start(CPUARMState *env)
1414{
5ecdd3e4 1415 unsigned int i;
5d05b9d4 1416 pmccntr_op_start(env);
5ecdd3e4
AL
1417 for (i = 0; i < pmu_num_counters(env); i++) {
1418 pmevcntr_op_start(env, i);
1419 }
5d05b9d4
AL
1420}
1421
1422void pmu_op_finish(CPUARMState *env)
1423{
5ecdd3e4 1424 unsigned int i;
5d05b9d4 1425 pmccntr_op_finish(env);
5ecdd3e4
AL
1426 for (i = 0; i < pmu_num_counters(env); i++) {
1427 pmevcntr_op_finish(env, i);
1428 }
5d05b9d4
AL
1429}
1430
033614c4
AL
1431void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1432{
1433 pmu_op_start(&cpu->env);
1434}
1435
1436void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1437{
1438 pmu_op_finish(&cpu->env);
1439}
1440
4e7beb0c
AL
1441void arm_pmu_timer_cb(void *opaque)
1442{
1443 ARMCPU *cpu = opaque;
1444
1445 /*
1446 * Update all the counter values based on the current underlying counts,
1447 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1448 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1449 * counter may expire.
1450 */
1451 pmu_op_start(&cpu->env);
1452 pmu_op_finish(&cpu->env);
1453}
1454
c4241c7d
PM
1455static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1456 uint64_t value)
200ac0ef 1457{
5d05b9d4 1458 pmu_op_start(env);
7c2cb42b
AF
1459
1460 if (value & PMCRC) {
1461 /* The counter has been reset */
1462 env->cp15.c15_ccnt = 0;
1463 }
1464
5ecdd3e4
AL
1465 if (value & PMCRP) {
1466 unsigned int i;
1467 for (i = 0; i < pmu_num_counters(env); i++) {
1468 env->cp15.c14_pmevcntr[i] = 0;
1469 }
1470 }
1471
9323e79f
PM
1472 env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1473 env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
7c2cb42b 1474
5d05b9d4 1475 pmu_op_finish(env);
7c2cb42b
AF
1476}
1477
0d4bfd7d
AL
1478static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1479 uint64_t value)
1480{
1481 unsigned int i;
47b385da
PM
1482 uint64_t overflow_mask, new_pmswinc;
1483
0d4bfd7d
AL
1484 for (i = 0; i < pmu_num_counters(env); i++) {
1485 /* Increment a counter's count iff: */
1486 if ((value & (1 << i)) && /* counter's bit is set */
1487 /* counter is enabled and not filtered */
1488 pmu_counter_enabled(env, i) &&
1489 /* counter is SW_INCR */
1490 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1491 pmevcntr_op_start(env, i);
f4efb4b2
AL
1492
1493 /*
1494 * Detect if this write causes an overflow since we can't predict
1495 * PMSWINC overflows like we can for other events
1496 */
47b385da
PM
1497 new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1498
1499 overflow_mask = pmevcntr_is_64_bit(env, i) ?
1500 1ULL << 63 : 1ULL << 31;
f4efb4b2 1501
47b385da 1502 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
f4efb4b2
AL
1503 env->cp15.c9_pmovsr |= (1 << i);
1504 pmu_update_irq(env);
1505 }
1506
1507 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1508
0d4bfd7d
AL
1509 pmevcntr_op_finish(env, i);
1510 }
1511 }
1512}
1513
7c2cb42b
AF
1514static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1515{
5d05b9d4
AL
1516 uint64_t ret;
1517 pmccntr_op_start(env);
1518 ret = env->cp15.c15_ccnt;
1519 pmccntr_op_finish(env);
1520 return ret;
7c2cb42b
AF
1521}
1522
6b040780
WH
1523static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1524 uint64_t value)
1525{
9b37a28c
FR
1526 /*
1527 * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
6b040780
WH
1528 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1529 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1530 * accessed.
1531 */
1532 env->cp15.c9_pmselr = value & 0x1f;
1533}
1534
7c2cb42b
AF
1535static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1536 uint64_t value)
1537{
5d05b9d4
AL
1538 pmccntr_op_start(env);
1539 env->cp15.c15_ccnt = value;
1540 pmccntr_op_finish(env);
200ac0ef 1541}
421c7ebd
PC
1542
1543static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1544 uint64_t value)
1545{
1546 uint64_t cur_val = pmccntr_read(env, NULL);
1547
1548 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1549}
1550
0614601c
AF
1551static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1552 uint64_t value)
1553{
5d05b9d4 1554 pmccntr_op_start(env);
4b8afa1f
AL
1555 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1556 pmccntr_op_finish(env);
1557}
1558
1559static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1560 uint64_t value)
1561{
1562 pmccntr_op_start(env);
1563 /* M is not accessible from AArch32 */
1564 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1565 (value & PMCCFILTR);
5d05b9d4 1566 pmccntr_op_finish(env);
0614601c
AF
1567}
1568
4b8afa1f
AL
1569static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1570{
1571 /* M is not visible in AArch32 */
1572 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1573}
1574
c4241c7d 1575static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1576 uint64_t value)
1577{
01765386 1578 pmu_op_start(env);
7ece99b1 1579 value &= pmu_counter_mask(env);
200ac0ef 1580 env->cp15.c9_pmcnten |= value;
01765386 1581 pmu_op_finish(env);
200ac0ef
PM
1582}
1583
c4241c7d
PM
1584static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1585 uint64_t value)
200ac0ef 1586{
01765386 1587 pmu_op_start(env);
7ece99b1 1588 value &= pmu_counter_mask(env);
200ac0ef 1589 env->cp15.c9_pmcnten &= ~value;
01765386 1590 pmu_op_finish(env);
200ac0ef
PM
1591}
1592
c4241c7d
PM
1593static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1594 uint64_t value)
200ac0ef 1595{
599b71e2 1596 value &= pmu_counter_mask(env);
200ac0ef 1597 env->cp15.c9_pmovsr &= ~value;
f4efb4b2 1598 pmu_update_irq(env);
200ac0ef
PM
1599}
1600
327dd510
AL
1601static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1602 uint64_t value)
1603{
1604 value &= pmu_counter_mask(env);
1605 env->cp15.c9_pmovsr |= value;
f4efb4b2 1606 pmu_update_irq(env);
327dd510
AL
1607}
1608
5ecdd3e4
AL
1609static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1610 uint64_t value, const uint8_t counter)
200ac0ef 1611{
5ecdd3e4
AL
1612 if (counter == 31) {
1613 pmccfiltr_write(env, ri, value);
1614 } else if (counter < pmu_num_counters(env)) {
1615 pmevcntr_op_start(env, counter);
1616
1617 /*
1618 * If this counter's event type is changing, store the current
1619 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1620 * pmevcntr_op_finish has the correct baseline when it converts back to
1621 * a delta.
1622 */
1623 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1624 PMXEVTYPER_EVTCOUNT;
1625 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1626 if (old_event != new_event) {
1627 uint64_t count = 0;
1628 if (event_supported(new_event)) {
1629 uint16_t event_idx = supported_event_map[new_event];
1630 count = pm_events[event_idx].get_count(env);
1631 }
1632 env->cp15.c14_pmevcntr_delta[counter] = count;
1633 }
1634
1635 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1636 pmevcntr_op_finish(env, counter);
1637 }
9b37a28c
FR
1638 /*
1639 * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
fdb86656
WH
1640 * PMSELR value is equal to or greater than the number of implemented
1641 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1642 */
5ecdd3e4
AL
1643}
1644
1645static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1646 const uint8_t counter)
1647{
1648 if (counter == 31) {
1649 return env->cp15.pmccfiltr_el0;
1650 } else if (counter < pmu_num_counters(env)) {
1651 return env->cp15.c14_pmevtyper[counter];
1652 } else {
1653 /*
1654 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1655 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1656 */
1657 return 0;
1658 }
1659}
1660
1661static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1662 uint64_t value)
1663{
1664 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1665 pmevtyper_write(env, ri, value, counter);
1666}
1667
1668static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1669 uint64_t value)
1670{
1671 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1672 env->cp15.c14_pmevtyper[counter] = value;
1673
1674 /*
1675 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1676 * pmu_op_finish calls when loading saved state for a migration. Because
1677 * we're potentially updating the type of event here, the value written to
673d8215 1678 * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a
5ecdd3e4
AL
1679 * different counter type. Therefore, we need to set this value to the
1680 * current count for the counter type we're writing so that pmu_op_finish
1681 * has the correct count for its calculation.
1682 */
1683 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1684 if (event_supported(event)) {
1685 uint16_t event_idx = supported_event_map[event];
1686 env->cp15.c14_pmevcntr_delta[counter] =
1687 pm_events[event_idx].get_count(env);
fdb86656
WH
1688 }
1689}
1690
5ecdd3e4
AL
1691static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1692{
1693 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1694 return pmevtyper_read(env, ri, counter);
1695}
1696
1697static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1698 uint64_t value)
1699{
1700 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1701}
1702
fdb86656
WH
1703static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1704{
5ecdd3e4
AL
1705 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1706}
1707
1708static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1709 uint64_t value, uint8_t counter)
1710{
47b385da
PM
1711 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1712 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1713 value &= MAKE_64BIT_MASK(0, 32);
1714 }
5ecdd3e4
AL
1715 if (counter < pmu_num_counters(env)) {
1716 pmevcntr_op_start(env, counter);
1717 env->cp15.c14_pmevcntr[counter] = value;
1718 pmevcntr_op_finish(env, counter);
1719 }
1720 /*
1721 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1722 * are CONSTRAINED UNPREDICTABLE.
fdb86656 1723 */
5ecdd3e4
AL
1724}
1725
1726static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1727 uint8_t counter)
1728{
1729 if (counter < pmu_num_counters(env)) {
1730 uint64_t ret;
1731 pmevcntr_op_start(env, counter);
1732 ret = env->cp15.c14_pmevcntr[counter];
1733 pmevcntr_op_finish(env, counter);
47b385da
PM
1734 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1735 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1736 ret &= MAKE_64BIT_MASK(0, 32);
1737 }
5ecdd3e4 1738 return ret;
fdb86656 1739 } else {
9b37a28c
FR
1740 /*
1741 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1742 * are CONSTRAINED UNPREDICTABLE.
1743 */
fdb86656
WH
1744 return 0;
1745 }
200ac0ef
PM
1746}
1747
5ecdd3e4
AL
1748static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1749 uint64_t value)
1750{
1751 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1752 pmevcntr_write(env, ri, value, counter);
1753}
1754
1755static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1756{
1757 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1758 return pmevcntr_read(env, ri, counter);
1759}
1760
1761static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1762 uint64_t value)
1763{
1764 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1765 assert(counter < pmu_num_counters(env));
1766 env->cp15.c14_pmevcntr[counter] = value;
1767 pmevcntr_write(env, ri, value, counter);
1768}
1769
1770static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1771{
1772 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1773 assert(counter < pmu_num_counters(env));
1774 return env->cp15.c14_pmevcntr[counter];
1775}
1776
1777static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1778 uint64_t value)
1779{
1780 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1781}
1782
1783static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1784{
1785 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1786}
1787
c4241c7d 1788static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1789 uint64_t value)
1790{
6ecd0b6b
AB
1791 if (arm_feature(env, ARM_FEATURE_V8)) {
1792 env->cp15.c9_pmuserenr = value & 0xf;
1793 } else {
1794 env->cp15.c9_pmuserenr = value & 1;
1795 }
200ac0ef
PM
1796}
1797
c4241c7d
PM
1798static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1799 uint64_t value)
200ac0ef
PM
1800{
1801 /* We have no event counters so only the C bit can be changed */
7ece99b1 1802 value &= pmu_counter_mask(env);
200ac0ef 1803 env->cp15.c9_pminten |= value;
f4efb4b2 1804 pmu_update_irq(env);
200ac0ef
PM
1805}
1806
c4241c7d
PM
1807static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1808 uint64_t value)
200ac0ef 1809{
7ece99b1 1810 value &= pmu_counter_mask(env);
200ac0ef 1811 env->cp15.c9_pminten &= ~value;
f4efb4b2 1812 pmu_update_irq(env);
200ac0ef
PM
1813}
1814
c4241c7d
PM
1815static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1816 uint64_t value)
8641136c 1817{
9b37a28c
FR
1818 /*
1819 * Note that even though the AArch64 view of this register has bits
a505d7fe
PM
1820 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1821 * architectural requirements for bits which are RES0 only in some
1822 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1823 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1824 */
855ea66d 1825 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1826}
1827
64e0e2de
EI
1828static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1829{
ea22747c 1830 /* Begin with base v8.0 state. */
06f2adcc 1831 uint64_t valid_mask = 0x3fff;
2fc0cc0e 1832 ARMCPU *cpu = env_archcpu(env);
d902ae75 1833 uint64_t changed;
ea22747c 1834
bfe43e3d
RH
1835 /*
1836 * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1837 * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1838 * Instead, choose the format based on the mode of EL3.
1839 */
1840 if (arm_el_is_aa64(env, 3)) {
1841 value |= SCR_FW | SCR_AW; /* RES1 */
1842 valid_mask &= ~SCR_NET; /* RES0 */
252e8c69 1843
6bcbb07a
RH
1844 if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1845 !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1846 value |= SCR_RW; /* RAO/WI */
1847 }
da3d8b13
RH
1848 if (cpu_isar_feature(aa64_ras, cpu)) {
1849 valid_mask |= SCR_TERR;
1850 }
252e8c69
RH
1851 if (cpu_isar_feature(aa64_lor, cpu)) {
1852 valid_mask |= SCR_TLOR;
1853 }
1854 if (cpu_isar_feature(aa64_pauth, cpu)) {
1855 valid_mask |= SCR_API | SCR_APK;
1856 }
926c1b97
RDC
1857 if (cpu_isar_feature(aa64_sel2, cpu)) {
1858 valid_mask |= SCR_EEL2;
87bfbfe7
RH
1859 } else if (cpu_isar_feature(aa64_rme, cpu)) {
1860 /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */
1861 value |= SCR_NS;
926c1b97 1862 }
8ddb300b
RH
1863 if (cpu_isar_feature(aa64_mte, cpu)) {
1864 valid_mask |= SCR_ATA;
1865 }
7cb1e618
RH
1866 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1867 valid_mask |= SCR_ENSCXT;
1868 }
7ac61020
PM
1869 if (cpu_isar_feature(aa64_doublefault, cpu)) {
1870 valid_mask |= SCR_EASE | SCR_NMEA;
1871 }
06f2adcc
JF
1872 if (cpu_isar_feature(aa64_sme, cpu)) {
1873 valid_mask |= SCR_ENTP2;
1874 }
08899b5c
EI
1875 if (cpu_isar_feature(aa64_hcx, cpu)) {
1876 valid_mask |= SCR_HXEN;
1877 }
15126d9c
PM
1878 if (cpu_isar_feature(aa64_fgt, cpu)) {
1879 valid_mask |= SCR_FGTEN;
1880 }
aa3cc42c
RH
1881 if (cpu_isar_feature(aa64_rme, cpu)) {
1882 valid_mask |= SCR_NSE | SCR_GPF;
1883 }
ea22747c
RH
1884 } else {
1885 valid_mask &= ~(SCR_RW | SCR_ST);
da3d8b13
RH
1886 if (cpu_isar_feature(aa32_ras, cpu)) {
1887 valid_mask |= SCR_TERR;
1888 }
ea22747c 1889 }
64e0e2de
EI
1890
1891 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1892 valid_mask &= ~SCR_HCE;
1893
9b37a28c
FR
1894 /*
1895 * On ARMv7, SMD (or SCD as it is called in v7) is only
64e0e2de
EI
1896 * supported if EL2 exists. The bit is UNK/SBZP when
1897 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1898 * when EL2 is unavailable.
4eb27640 1899 * On ARMv8, this bit is always available.
64e0e2de 1900 */
4eb27640
GB
1901 if (arm_feature(env, ARM_FEATURE_V7) &&
1902 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1903 valid_mask &= ~SCR_SMD;
1904 }
1905 }
1906
1907 /* Clear all-context RES0 bits. */
1908 value &= valid_mask;
d902ae75
RH
1909 changed = env->cp15.scr_el3 ^ value;
1910 env->cp15.scr_el3 = value;
1911
1912 /*
aa3cc42c 1913 * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
d902ae75
RH
1914 * we must invalidate all TLBs below EL3.
1915 */
aa3cc42c 1916 if (changed & (SCR_NS | SCR_NSE)) {
d902ae75
RH
1917 tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1918 ARMMMUIdxBit_E20_0 |
1919 ARMMMUIdxBit_E10_1 |
1920 ARMMMUIdxBit_E20_2 |
1921 ARMMMUIdxBit_E10_1_PAN |
1922 ARMMMUIdxBit_E20_2_PAN |
1923 ARMMMUIdxBit_E2));
1924 }
64e0e2de
EI
1925}
1926
10d0ef3e
MN
1927static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1928{
1929 /*
1930 * scr_write will set the RES1 bits on an AArch64-only CPU.
1931 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1932 */
1933 scr_write(env, ri, 0);
1934}
1935
e2ce5fcd
PM
1936static CPAccessResult access_tid4(CPUARMState *env,
1937 const ARMCPRegInfo *ri,
1938 bool isread)
630fcd4d 1939{
e2ce5fcd
PM
1940 if (arm_current_el(env) == 1 &&
1941 (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
630fcd4d
MZ
1942 return CP_ACCESS_TRAP_EL2;
1943 }
1944
1945 return CP_ACCESS_OK;
1946}
1947
c4241c7d 1948static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c 1949{
2fc0cc0e 1950 ARMCPU *cpu = env_archcpu(env);
b85a1fd6 1951
9b37a28c
FR
1952 /*
1953 * Acquire the CSSELR index from the bank corresponding to the CCSIDR
b85a1fd6
FA
1954 * bank
1955 */
1956 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1957 ri->secure & ARM_CP_SECSTATE_S);
1958
1959 return cpu->ccsidr[index];
776d4e5c
PM
1960}
1961
c4241c7d
PM
1962static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1963 uint64_t value)
776d4e5c 1964{
8d5c773e 1965 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1966}
1967
1090b9c6
PM
1968static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1969{
29a0af61 1970 CPUState *cs = env_cpu(env);
cc974d5c
RDC
1971 bool el1 = arm_current_el(env) == 1;
1972 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1090b9c6
PM
1973 uint64_t ret = 0;
1974
cc974d5c 1975 if (hcr_el2 & HCR_IMO) {
636540e9
PM
1976 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1977 ret |= CPSR_I;
1978 }
1979 } else {
1980 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1981 ret |= CPSR_I;
1982 }
1090b9c6 1983 }
636540e9 1984
cc974d5c 1985 if (hcr_el2 & HCR_FMO) {
636540e9
PM
1986 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1987 ret |= CPSR_F;
1988 }
1989 } else {
1990 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1991 ret |= CPSR_F;
1992 }
1090b9c6 1993 }
636540e9 1994
3c29632f
RH
1995 if (hcr_el2 & HCR_AMO) {
1996 if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1997 ret |= CPSR_A;
1998 }
1999 }
2000
1090b9c6
PM
2001 return ret;
2002}
2003
93fbc983
MZ
2004static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2005 bool isread)
2006{
2007 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2008 return CP_ACCESS_TRAP_EL2;
2009 }
2010
2011 return CP_ACCESS_OK;
2012}
2013
2014static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2015 bool isread)
2016{
2017 if (arm_feature(env, ARM_FEATURE_V8)) {
2018 return access_aa64_tid1(env, ri, isread);
2019 }
2020
2021 return CP_ACCESS_OK;
2022}
2023
e9aa6c21 2024static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
2025 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2026 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2027 .access = PL1_W, .type = ARM_CP_NOP },
9b37a28c
FR
2028 /*
2029 * Performance monitors are implementation defined in v7,
200ac0ef 2030 * but with an ARM recommended set of registers, which we
ac689a2e 2031 * follow.
200ac0ef
PM
2032 *
2033 * Performance registers fall into three categories:
2034 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2035 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2036 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2037 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2038 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2039 */
2040 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7f4fbfb5 2041 .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
8521466b 2042 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2043 .writefn = pmcntenset_write,
2044 .accessfn = pmreg_access,
dc780233 2045 .fgt = FGT_PMCNTEN,
fcd25206 2046 .raw_writefn = raw_write },
7f4fbfb5 2047 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
8521466b
AF
2048 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2049 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2050 .fgt = FGT_PMCNTEN,
8521466b
AF
2051 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2052 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 2053 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
2054 .access = PL0_RW,
2055 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206 2056 .accessfn = pmreg_access,
dc780233 2057 .fgt = FGT_PMCNTEN,
fcd25206 2058 .writefn = pmcntenclr_write,
7f4fbfb5 2059 .type = ARM_CP_ALIAS | ARM_CP_IO },
8521466b
AF
2060 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2061 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2062 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2063 .fgt = FGT_PMCNTEN,
7f4fbfb5 2064 .type = ARM_CP_ALIAS | ARM_CP_IO,
8521466b
AF
2065 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2066 .writefn = pmcntenclr_write },
200ac0ef 2067 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
f4efb4b2 2068 .access = PL0_RW, .type = ARM_CP_IO,
e4e91a21 2069 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206 2070 .accessfn = pmreg_access,
dc780233 2071 .fgt = FGT_PMOVS,
fcd25206
PM
2072 .writefn = pmovsr_write,
2073 .raw_writefn = raw_write },
978364f1
AF
2074 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2075 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2076 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2077 .fgt = FGT_PMOVS,
f4efb4b2 2078 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2079 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2080 .writefn = pmovsr_write,
2081 .raw_writefn = raw_write },
200ac0ef 2082 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
f4efb4b2 2083 .access = PL0_W, .accessfn = pmreg_access_swinc,
dc780233 2084 .fgt = FGT_PMSWINC_EL0,
f4efb4b2 2085 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d
AL
2086 .writefn = pmswinc_write },
2087 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2088 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
f4efb4b2 2089 .access = PL0_W, .accessfn = pmreg_access_swinc,
dc780233 2090 .fgt = FGT_PMSWINC_EL0,
f4efb4b2 2091 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d 2092 .writefn = pmswinc_write },
6b040780
WH
2093 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2094 .access = PL0_RW, .type = ARM_CP_ALIAS,
dc780233 2095 .fgt = FGT_PMSELR_EL0,
6b040780 2096 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 2097 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
2098 .raw_writefn = raw_write},
2099 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2100 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 2101 .access = PL0_RW, .accessfn = pmreg_access_selr,
dc780233 2102 .fgt = FGT_PMSELR_EL0,
6b040780
WH
2103 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2104 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 2105 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 2106 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
dc780233 2107 .fgt = FGT_PMCCNTR_EL0,
421c7ebd 2108 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 2109 .accessfn = pmreg_access_ccntr },
8521466b
AF
2110 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2111 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 2112 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
dc780233 2113 .fgt = FGT_PMCCNTR_EL0,
8521466b 2114 .type = ARM_CP_IO,
980ebe87
AL
2115 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2116 .readfn = pmccntr_read, .writefn = pmccntr_write,
2117 .raw_readfn = raw_read, .raw_writefn = raw_write, },
4b8afa1f
AL
2118 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2119 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2120 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2121 .fgt = FGT_PMCCFILTR_EL0,
4b8afa1f
AL
2122 .type = ARM_CP_ALIAS | ARM_CP_IO,
2123 .resetvalue = 0, },
8521466b
AF
2124 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2125 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
980ebe87 2126 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
8521466b 2127 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2128 .fgt = FGT_PMCCFILTR_EL0,
8521466b
AF
2129 .type = ARM_CP_IO,
2130 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2131 .resetvalue = 0, },
200ac0ef 2132 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
5ecdd3e4
AL
2133 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2134 .accessfn = pmreg_access,
dc780233 2135 .fgt = FGT_PMEVTYPERN_EL0,
fdb86656
WH
2136 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2137 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2138 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
5ecdd3e4
AL
2139 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2140 .accessfn = pmreg_access,
dc780233 2141 .fgt = FGT_PMEVTYPERN_EL0,
fdb86656 2142 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
200ac0ef 2143 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
5ecdd3e4
AL
2144 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2145 .accessfn = pmreg_access_xevcntr,
dc780233 2146 .fgt = FGT_PMEVCNTRN_EL0,
5ecdd3e4
AL
2147 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2148 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2149 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2150 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2151 .accessfn = pmreg_access_xevcntr,
dc780233 2152 .fgt = FGT_PMEVCNTRN_EL0,
5ecdd3e4 2153 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
200ac0ef 2154 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 2155 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 2156 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 2157 .resetvalue = 0,
d4e6df63 2158 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
2159 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2160 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 2161 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
2162 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2163 .resetvalue = 0,
2164 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 2165 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 2166 .access = PL1_RW, .accessfn = access_tpm,
dc780233 2167 .fgt = FGT_PMINTEN,
b7d793ad 2168 .type = ARM_CP_ALIAS | ARM_CP_IO,
e6ec5457 2169 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 2170 .resetvalue = 0,
d4e6df63 2171 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
2172 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2173 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2174 .access = PL1_RW, .accessfn = access_tpm,
dc780233 2175 .fgt = FGT_PMINTEN,
e6ec5457
WH
2176 .type = ARM_CP_IO,
2177 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2178 .writefn = pmintenset_write, .raw_writefn = raw_write,
2179 .resetvalue = 0x0 },
200ac0ef 2180 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
fc5f6856 2181 .access = PL1_RW, .accessfn = access_tpm,
dc780233 2182 .fgt = FGT_PMINTEN,
887c0f15 2183 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
200ac0ef 2184 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 2185 .writefn = pmintenclr_write, },
978364f1
AF
2186 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2187 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
fc5f6856 2188 .access = PL1_RW, .accessfn = access_tpm,
dc780233 2189 .fgt = FGT_PMINTEN,
887c0f15 2190 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
978364f1
AF
2191 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2192 .writefn = pmintenclr_write },
7da845b0
PM
2193 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2194 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
630fcd4d 2195 .access = PL1_R,
e2ce5fcd 2196 .accessfn = access_tid4,
158c276c 2197 .fgt = FGT_CCSIDR_EL1,
630fcd4d 2198 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
2199 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2200 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
630fcd4d 2201 .access = PL1_RW,
e2ce5fcd 2202 .accessfn = access_tid4,
b19ed03c 2203 .fgt = FGT_CSSELR_EL1,
630fcd4d 2204 .writefn = csselr_write, .resetvalue = 0,
b85a1fd6
FA
2205 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2206 offsetof(CPUARMState, cp15.csselr_ns) } },
9b37a28c
FR
2207 /*
2208 * Auxiliary ID register: this actually has an IMPDEF value but for now
776d4e5c
PM
2209 * just RAZ for all cores:
2210 */
0ff644a7
PM
2211 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2212 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
93fbc983
MZ
2213 .access = PL1_R, .type = ARM_CP_CONST,
2214 .accessfn = access_aa64_tid1,
158c276c 2215 .fgt = FGT_AIDR_EL1,
93fbc983 2216 .resetvalue = 0 },
9b37a28c
FR
2217 /*
2218 * Auxiliary fault status registers: these also are IMPDEF, and we
f32cdad5
PM
2219 * choose to RAZ/WI for all cores.
2220 */
2221 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2222 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
84929218 2223 .access = PL1_RW, .accessfn = access_tvm_trvm,
158c276c 2224 .fgt = FGT_AFSR0_EL1,
84929218 2225 .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
2226 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2227 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
84929218 2228 .access = PL1_RW, .accessfn = access_tvm_trvm,
158c276c 2229 .fgt = FGT_AFSR1_EL1,
84929218 2230 .type = ARM_CP_CONST, .resetvalue = 0 },
9b37a28c
FR
2231 /*
2232 * MAIR can just read-as-written because we don't implement caches
b0fe2427
PM
2233 * and so don't need to care about memory attributes.
2234 */
2235 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2236 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
84929218 2237 .access = PL1_RW, .accessfn = access_tvm_trvm,
67dd8030 2238 .fgt = FGT_MAIR_EL1,
84929218 2239 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 2240 .resetvalue = 0 },
4cfb8ad8
PM
2241 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2242 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2243 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2244 .resetvalue = 0 },
9b37a28c
FR
2245 /*
2246 * For non-long-descriptor page tables these are PRRR and NMRR;
b0fe2427 2247 * regardless they still act as reads-as-written for QEMU.
b0fe2427 2248 */
9b37a28c
FR
2249 /*
2250 * MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
2251 * allows them to assign the correct fieldoffset based on the endianness
2252 * handled in the field definitions.
2253 */
a903c449 2254 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
84929218
RH
2255 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2256 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2257 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2258 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 2259 .resetfn = arm_cp_reset_ignore },
a903c449 2260 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
84929218
RH
2261 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2262 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2263 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2264 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 2265 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
2266 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2267 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
b19ed03c 2268 .fgt = FGT_ISR_EL1,
7a0e58fa 2269 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
2270 /* 32 bit ITLB invalidates */
2271 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
30881b73
RH
2272 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2273 .writefn = tlbiall_write },
995939a6 2274 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
30881b73
RH
2275 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2276 .writefn = tlbimva_write },
995939a6 2277 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
30881b73
RH
2278 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2279 .writefn = tlbiasid_write },
995939a6
PM
2280 /* 32 bit DTLB invalidates */
2281 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
30881b73
RH
2282 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2283 .writefn = tlbiall_write },
995939a6 2284 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
30881b73
RH
2285 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2286 .writefn = tlbimva_write },
995939a6 2287 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
30881b73
RH
2288 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2289 .writefn = tlbiasid_write },
995939a6
PM
2290 /* 32 bit TLB invalidates */
2291 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73
RH
2292 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2293 .writefn = tlbiall_write },
995939a6 2294 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73
RH
2295 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2296 .writefn = tlbimva_write },
995939a6 2297 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73
RH
2298 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2299 .writefn = tlbiasid_write },
995939a6 2300 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73
RH
2301 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2302 .writefn = tlbimvaa_write },
995939a6
PM
2303};
2304
2305static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2306 /* 32 bit TLB invalidates, Inner Shareable */
2307 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
0f66d223 2308 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
30881b73 2309 .writefn = tlbiall_is_write },
995939a6 2310 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
0f66d223 2311 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
30881b73 2312 .writefn = tlbimva_is_write },
995939a6 2313 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
0f66d223 2314 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
fa439fc5 2315 .writefn = tlbiasid_is_write },
995939a6 2316 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
0f66d223 2317 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
fa439fc5 2318 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
2319};
2320
327dd510
AL
2321static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2322 /* PMOVSSET is not implemented in v7 before v7ve */
2323 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2324 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2325 .fgt = FGT_PMOVS,
f4efb4b2 2326 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2327 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2328 .writefn = pmovsset_write,
2329 .raw_writefn = raw_write },
2330 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2331 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2332 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2333 .fgt = FGT_PMOVS,
f4efb4b2 2334 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2335 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2336 .writefn = pmovsset_write,
2337 .raw_writefn = raw_write },
327dd510
AL
2338};
2339
c4241c7d
PM
2340static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2341 uint64_t value)
c326b979
PM
2342{
2343 value &= 1;
2344 env->teecr = value;
c326b979
PM
2345}
2346
cc7613bf
PM
2347static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2348 bool isread)
2349{
2350 /*
2351 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2352 * at all, so we don't need to check whether we're v8A.
2353 */
2354 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2355 (env->cp15.hstr_el2 & HSTR_TTEE)) {
2356 return CP_ACCESS_TRAP_EL2;
2357 }
2358 return CP_ACCESS_OK;
2359}
2360
3f208fd7
PM
2361static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2362 bool isread)
c326b979 2363{
dcbff19b 2364 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 2365 return CP_ACCESS_TRAP;
c326b979 2366 }
cc7613bf 2367 return teecr_access(env, ri, isread);
c326b979
PM
2368}
2369
2370static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2371 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2372 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2373 .resetvalue = 0,
cc7613bf 2374 .writefn = teecr_write, .accessfn = teecr_access },
c326b979
PM
2375 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2376 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 2377 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
2378};
2379
4d31c596 2380static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
2381 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2382 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2383 .access = PL0_RW,
67dd8030 2384 .fgt = FGT_TPIDR_EL0,
54bf36ed 2385 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
2386 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2387 .access = PL0_RW,
67dd8030 2388 .fgt = FGT_TPIDR_EL0,
54bf36ed
FA
2389 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2390 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
2391 .resetfn = arm_cp_reset_ignore },
2392 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2393 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
04215eb1 2394 .access = PL0_R | PL1_W,
67dd8030 2395 .fgt = FGT_TPIDRRO_EL0,
54bf36ed
FA
2396 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2397 .resetvalue = 0},
4d31c596 2398 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
04215eb1 2399 .access = PL0_R | PL1_W,
67dd8030 2400 .fgt = FGT_TPIDRRO_EL0,
54bf36ed
FA
2401 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2402 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 2403 .resetfn = arm_cp_reset_ignore },
54bf36ed 2404 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 2405 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 2406 .access = PL1_RW,
67dd8030 2407 .fgt = FGT_TPIDR_EL1,
54bf36ed
FA
2408 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2409 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2410 .access = PL1_RW,
2411 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2412 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2413 .resetvalue = 0 },
4d31c596
PM
2414};
2415
55d284af
PM
2416#ifndef CONFIG_USER_ONLY
2417
3f208fd7
PM
2418static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2419 bool isread)
00108f2d 2420{
9b37a28c
FR
2421 /*
2422 * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
75502672
PM
2423 * Writable only at the highest implemented exception level.
2424 */
2425 int el = arm_current_el(env);
5bc84371
RH
2426 uint64_t hcr;
2427 uint32_t cntkctl;
75502672
PM
2428
2429 switch (el) {
2430 case 0:
5bc84371
RH
2431 hcr = arm_hcr_el2_eff(env);
2432 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2433 cntkctl = env->cp15.cnthctl_el2;
2434 } else {
2435 cntkctl = env->cp15.c14_cntkctl;
2436 }
2437 if (!extract32(cntkctl, 0, 2)) {
75502672
PM
2438 return CP_ACCESS_TRAP;
2439 }
2440 break;
2441 case 1:
2442 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2443 arm_is_secure_below_el3(env)) {
2444 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2445 return CP_ACCESS_TRAP_UNCATEGORIZED;
2446 }
2447 break;
2448 case 2:
2449 case 3:
2450 break;
00108f2d 2451 }
75502672
PM
2452
2453 if (!isread && el < arm_highest_el(env)) {
2454 return CP_ACCESS_TRAP_UNCATEGORIZED;
2455 }
2456
00108f2d
PM
2457 return CP_ACCESS_OK;
2458}
2459
3f208fd7
PM
2460static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2461 bool isread)
00108f2d 2462{
0b6440af 2463 unsigned int cur_el = arm_current_el(env);
e6ef0169 2464 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2465 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2466
5bc84371
RH
2467 switch (cur_el) {
2468 case 0:
2469 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2470 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2471 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2472 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2473 }
0b6440af 2474
5bc84371
RH
2475 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2476 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2477 return CP_ACCESS_TRAP;
2478 }
d01448c7 2479 /* fall through */
5bc84371
RH
2480 case 1:
2481 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
e6ef0169 2482 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2483 (hcr & HCR_E2H
2484 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2485 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2486 return CP_ACCESS_TRAP_EL2;
2487 }
2488 break;
0b6440af 2489 }
00108f2d
PM
2490 return CP_ACCESS_OK;
2491}
2492
3f208fd7
PM
2493static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2494 bool isread)
00108f2d 2495{
0b6440af 2496 unsigned int cur_el = arm_current_el(env);
e6ef0169 2497 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2498 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2499
5bc84371
RH
2500 switch (cur_el) {
2501 case 0:
2502 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2503 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2504 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2505 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2506 }
0b6440af 2507
5bc84371
RH
2508 /*
2509 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2510 * EL0 if EL0[PV]TEN is zero.
2511 */
2512 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2513 return CP_ACCESS_TRAP;
2514 }
2515 /* fall through */
2516
2517 case 1:
e6ef0169 2518 if (has_el2 && timeridx == GTIMER_PHYS) {
5bc84371
RH
2519 if (hcr & HCR_E2H) {
2520 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2521 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2522 return CP_ACCESS_TRAP_EL2;
2523 }
2524 } else {
2525 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2526 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2527 return CP_ACCESS_TRAP_EL2;
2528 }
2529 }
2530 }
2531 break;
0b6440af 2532 }
00108f2d
PM
2533 return CP_ACCESS_OK;
2534}
2535
2536static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
2537 const ARMCPRegInfo *ri,
2538 bool isread)
00108f2d 2539{
3f208fd7 2540 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2541}
2542
2543static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
2544 const ARMCPRegInfo *ri,
2545 bool isread)
00108f2d 2546{
3f208fd7 2547 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2548}
2549
3f208fd7
PM
2550static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2551 bool isread)
00108f2d 2552{
3f208fd7 2553 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2554}
2555
3f208fd7
PM
2556static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2557 bool isread)
00108f2d 2558{
3f208fd7 2559 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2560}
2561
b4d3978c 2562static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
2563 const ARMCPRegInfo *ri,
2564 bool isread)
b4d3978c 2565{
9b37a28c
FR
2566 /*
2567 * The AArch64 register view of the secure physical timer is
b4d3978c
PM
2568 * always accessible from EL3, and configurably accessible from
2569 * Secure EL1.
2570 */
2571 switch (arm_current_el(env)) {
2572 case 1:
2573 if (!arm_is_secure(env)) {
2574 return CP_ACCESS_TRAP;
2575 }
2576 if (!(env->cp15.scr_el3 & SCR_ST)) {
2577 return CP_ACCESS_TRAP_EL3;
2578 }
2579 return CP_ACCESS_OK;
2580 case 0:
2581 case 2:
2582 return CP_ACCESS_TRAP;
2583 case 3:
2584 return CP_ACCESS_OK;
2585 default:
2586 g_assert_not_reached();
2587 }
2588}
2589
55d284af
PM
2590static uint64_t gt_get_countervalue(CPUARMState *env)
2591{
7def8754
AJ
2592 ARMCPU *cpu = env_archcpu(env);
2593
2594 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
55d284af
PM
2595}
2596
f6fc36de
JPB
2597static void gt_update_irq(ARMCPU *cpu, int timeridx)
2598{
2599 CPUARMState *env = &cpu->env;
2600 uint64_t cnthctl = env->cp15.cnthctl_el2;
2601 ARMSecuritySpace ss = arm_security_space(env);
2602 /* ISTATUS && !IMASK */
2603 int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2604
2605 /*
2606 * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2607 * It is RES0 in Secure and NonSecure state.
2608 */
2609 if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2610 ((timeridx == GTIMER_VIRT && (cnthctl & CNTHCTL_CNTVMASK)) ||
2611 (timeridx == GTIMER_PHYS && (cnthctl & CNTHCTL_CNTPMASK)))) {
2612 irqstate = 0;
2613 }
2614
2615 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2616 trace_arm_gt_update_irq(timeridx, irqstate);
2617}
2618
2619void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2620{
2621 /*
2622 * Changing security state between Root and Secure/NonSecure, which may
2623 * happen when switching EL, can change the effective value of CNTHCTL_EL2
2624 * mask bits. Update the IRQ state accordingly.
2625 */
2626 gt_update_irq(cpu, GTIMER_VIRT);
2627 gt_update_irq(cpu, GTIMER_PHYS);
2628}
2629
55d284af
PM
2630static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2631{
2632 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2633
2634 if (gt->ctl & 1) {
9b37a28c
FR
2635 /*
2636 * Timer enabled: calculate and set current ISTATUS, irq, and
55d284af
PM
2637 * reset timer to when ISTATUS next has to change
2638 */
edac4d8a
EI
2639 uint64_t offset = timeridx == GTIMER_VIRT ?
2640 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
2641 uint64_t count = gt_get_countervalue(&cpu->env);
2642 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 2643 int istatus = count - offset >= gt->cval;
55d284af
PM
2644 uint64_t nexttick;
2645
2646 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49 2647
55d284af
PM
2648 if (istatus) {
2649 /* Next transition is when count rolls back over to zero */
2650 nexttick = UINT64_MAX;
2651 } else {
2652 /* Next transition is when we hit cval */
edac4d8a 2653 nexttick = gt->cval + offset;
55d284af 2654 }
9b37a28c
FR
2655 /*
2656 * Note that the desired next expiry time might be beyond the
55d284af
PM
2657 * signed-64-bit range of a QEMUTimer -- in this case we just
2658 * set the timer for as far in the future as possible. When the
2659 * timer expires we will reset the timer for any remaining period.
2660 */
7def8754 2661 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
4a0245b6
AJ
2662 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2663 } else {
2664 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af 2665 }
f6fc36de 2666 trace_arm_gt_recalc(timeridx, nexttick);
55d284af
PM
2667 } else {
2668 /* Timer disabled: ISTATUS and timer output always clear */
2669 gt->ctl &= ~4;
bc72ad67 2670 timer_del(cpu->gt_timer[timeridx]);
194cbc49 2671 trace_arm_gt_recalc_disabled(timeridx);
55d284af 2672 }
f6fc36de 2673 gt_update_irq(cpu, timeridx);
55d284af
PM
2674}
2675
0e3eca4c
EI
2676static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2677 int timeridx)
55d284af 2678{
2fc0cc0e 2679 ARMCPU *cpu = env_archcpu(env);
55d284af 2680
bc72ad67 2681 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
2682}
2683
c4241c7d 2684static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 2685{
c4241c7d 2686 return gt_get_countervalue(env);
55d284af
PM
2687}
2688
53d1f856
RH
2689static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2690{
2691 uint64_t hcr;
2692
2693 switch (arm_current_el(env)) {
2694 case 2:
2695 hcr = arm_hcr_el2_eff(env);
2696 if (hcr & HCR_E2H) {
2697 return 0;
2698 }
2699 break;
2700 case 0:
2701 hcr = arm_hcr_el2_eff(env);
2702 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2703 return 0;
2704 }
2705 break;
2706 }
2707
2708 return env->cp15.cntvoff_el2;
2709}
2710
edac4d8a
EI
2711static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2712{
53d1f856 2713 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
edac4d8a
EI
2714}
2715
c4241c7d 2716static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2717 int timeridx,
c4241c7d 2718 uint64_t value)
55d284af 2719{
194cbc49 2720 trace_arm_gt_cval_write(timeridx, value);
55d284af 2721 env->cp15.c14_timer[timeridx].cval = value;
2fc0cc0e 2722 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af 2723}
c4241c7d 2724
0e3eca4c
EI
2725static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2726 int timeridx)
55d284af 2727{
53d1f856
RH
2728 uint64_t offset = 0;
2729
2730 switch (timeridx) {
2731 case GTIMER_VIRT:
8c94b071 2732 case GTIMER_HYPVIRT:
53d1f856
RH
2733 offset = gt_virt_cnt_offset(env);
2734 break;
2735 }
55d284af 2736
c4241c7d 2737 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 2738 (gt_get_countervalue(env) - offset));
55d284af
PM
2739}
2740
c4241c7d 2741static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2742 int timeridx,
c4241c7d 2743 uint64_t value)
55d284af 2744{
53d1f856
RH
2745 uint64_t offset = 0;
2746
2747 switch (timeridx) {
2748 case GTIMER_VIRT:
8c94b071 2749 case GTIMER_HYPVIRT:
53d1f856
RH
2750 offset = gt_virt_cnt_offset(env);
2751 break;
2752 }
55d284af 2753
194cbc49 2754 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 2755 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 2756 sextract64(value, 0, 32);
2fc0cc0e 2757 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af
PM
2758}
2759
c4241c7d 2760static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2761 int timeridx,
c4241c7d 2762 uint64_t value)
55d284af 2763{
2fc0cc0e 2764 ARMCPU *cpu = env_archcpu(env);
55d284af
PM
2765 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2766
194cbc49 2767 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 2768 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
2769 if ((oldval ^ value) & 1) {
2770 /* Enable toggled */
2771 gt_recalc_timer(cpu, timeridx);
d3afacc7 2772 } else if ((oldval ^ value) & 2) {
9b37a28c
FR
2773 /*
2774 * IMASK toggled: don't need to recalculate,
55d284af
PM
2775 * just set the interrupt line based on ISTATUS
2776 */
f6fc36de
JPB
2777 trace_arm_gt_imask_toggle(timeridx);
2778 gt_update_irq(cpu, timeridx);
55d284af 2779 }
55d284af
PM
2780}
2781
0e3eca4c
EI
2782static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2783{
2784 gt_timer_reset(env, ri, GTIMER_PHYS);
2785}
2786
2787static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2788 uint64_t value)
2789{
2790 gt_cval_write(env, ri, GTIMER_PHYS, value);
2791}
2792
2793static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2794{
2795 return gt_tval_read(env, ri, GTIMER_PHYS);
2796}
2797
2798static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2799 uint64_t value)
2800{
2801 gt_tval_write(env, ri, GTIMER_PHYS, value);
2802}
2803
2804static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2805 uint64_t value)
2806{
2807 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2808}
2809
bb5972e4
RH
2810static int gt_phys_redir_timeridx(CPUARMState *env)
2811{
2812 switch (arm_mmu_idx(env)) {
2813 case ARMMMUIdx_E20_0:
2814 case ARMMMUIdx_E20_2:
452ef8cb 2815 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2816 return GTIMER_HYP;
2817 default:
2818 return GTIMER_PHYS;
2819 }
2820}
2821
2822static int gt_virt_redir_timeridx(CPUARMState *env)
2823{
2824 switch (arm_mmu_idx(env)) {
2825 case ARMMMUIdx_E20_0:
2826 case ARMMMUIdx_E20_2:
452ef8cb 2827 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2828 return GTIMER_HYPVIRT;
2829 default:
2830 return GTIMER_VIRT;
2831 }
2832}
2833
2834static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2835 const ARMCPRegInfo *ri)
2836{
2837 int timeridx = gt_phys_redir_timeridx(env);
2838 return env->cp15.c14_timer[timeridx].cval;
2839}
2840
2841static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2842 uint64_t value)
2843{
2844 int timeridx = gt_phys_redir_timeridx(env);
2845 gt_cval_write(env, ri, timeridx, value);
2846}
2847
2848static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2849 const ARMCPRegInfo *ri)
2850{
2851 int timeridx = gt_phys_redir_timeridx(env);
2852 return gt_tval_read(env, ri, timeridx);
2853}
2854
2855static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2856 uint64_t value)
2857{
2858 int timeridx = gt_phys_redir_timeridx(env);
2859 gt_tval_write(env, ri, timeridx, value);
2860}
2861
2862static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2863 const ARMCPRegInfo *ri)
2864{
2865 int timeridx = gt_phys_redir_timeridx(env);
2866 return env->cp15.c14_timer[timeridx].ctl;
2867}
2868
2869static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2870 uint64_t value)
2871{
2872 int timeridx = gt_phys_redir_timeridx(env);
2873 gt_ctl_write(env, ri, timeridx, value);
2874}
2875
0e3eca4c
EI
2876static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2877{
2878 gt_timer_reset(env, ri, GTIMER_VIRT);
2879}
2880
2881static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2882 uint64_t value)
2883{
2884 gt_cval_write(env, ri, GTIMER_VIRT, value);
2885}
2886
2887static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2888{
2889 return gt_tval_read(env, ri, GTIMER_VIRT);
2890}
2891
2892static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2893 uint64_t value)
2894{
2895 gt_tval_write(env, ri, GTIMER_VIRT, value);
2896}
2897
2898static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2899 uint64_t value)
2900{
2901 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2902}
2903
f6fc36de
JPB
2904static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2905 uint64_t value)
2906{
2907 ARMCPU *cpu = env_archcpu(env);
2908 uint32_t oldval = env->cp15.cnthctl_el2;
2909
2910 raw_write(env, ri, value);
2911
2912 if ((oldval ^ value) & CNTHCTL_CNTVMASK) {
2913 gt_update_irq(cpu, GTIMER_VIRT);
2914 } else if ((oldval ^ value) & CNTHCTL_CNTPMASK) {
2915 gt_update_irq(cpu, GTIMER_PHYS);
2916 }
2917}
2918
edac4d8a
EI
2919static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2920 uint64_t value)
2921{
2fc0cc0e 2922 ARMCPU *cpu = env_archcpu(env);
edac4d8a 2923
194cbc49 2924 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
2925 raw_write(env, ri, value);
2926 gt_recalc_timer(cpu, GTIMER_VIRT);
2927}
2928
bb5972e4
RH
2929static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2930 const ARMCPRegInfo *ri)
2931{
2932 int timeridx = gt_virt_redir_timeridx(env);
2933 return env->cp15.c14_timer[timeridx].cval;
2934}
2935
2936static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2937 uint64_t value)
2938{
2939 int timeridx = gt_virt_redir_timeridx(env);
2940 gt_cval_write(env, ri, timeridx, value);
2941}
2942
2943static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2944 const ARMCPRegInfo *ri)
2945{
2946 int timeridx = gt_virt_redir_timeridx(env);
2947 return gt_tval_read(env, ri, timeridx);
2948}
2949
2950static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2951 uint64_t value)
2952{
2953 int timeridx = gt_virt_redir_timeridx(env);
2954 gt_tval_write(env, ri, timeridx, value);
2955}
2956
2957static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2958 const ARMCPRegInfo *ri)
2959{
2960 int timeridx = gt_virt_redir_timeridx(env);
2961 return env->cp15.c14_timer[timeridx].ctl;
2962}
2963
2964static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2965 uint64_t value)
2966{
2967 int timeridx = gt_virt_redir_timeridx(env);
2968 gt_ctl_write(env, ri, timeridx, value);
2969}
2970
b0e66d95
EI
2971static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2972{
2973 gt_timer_reset(env, ri, GTIMER_HYP);
2974}
2975
2976static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2977 uint64_t value)
2978{
2979 gt_cval_write(env, ri, GTIMER_HYP, value);
2980}
2981
2982static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2983{
2984 return gt_tval_read(env, ri, GTIMER_HYP);
2985}
2986
2987static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2988 uint64_t value)
2989{
2990 gt_tval_write(env, ri, GTIMER_HYP, value);
2991}
2992
2993static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2994 uint64_t value)
2995{
2996 gt_ctl_write(env, ri, GTIMER_HYP, value);
2997}
2998
b4d3978c
PM
2999static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3000{
3001 gt_timer_reset(env, ri, GTIMER_SEC);
3002}
3003
3004static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3005 uint64_t value)
3006{
3007 gt_cval_write(env, ri, GTIMER_SEC, value);
3008}
3009
3010static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3011{
3012 return gt_tval_read(env, ri, GTIMER_SEC);
3013}
3014
3015static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3016 uint64_t value)
3017{
3018 gt_tval_write(env, ri, GTIMER_SEC, value);
3019}
3020
3021static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3022 uint64_t value)
3023{
3024 gt_ctl_write(env, ri, GTIMER_SEC, value);
3025}
3026
8c94b071
RH
3027static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3028{
3029 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3030}
3031
3032static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3033 uint64_t value)
3034{
3035 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3036}
3037
3038static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3039{
3040 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3041}
3042
3043static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3044 uint64_t value)
3045{
3046 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3047}
3048
3049static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3050 uint64_t value)
3051{
3052 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3053}
3054
55d284af
PM
3055void arm_gt_ptimer_cb(void *opaque)
3056{
3057 ARMCPU *cpu = opaque;
3058
3059 gt_recalc_timer(cpu, GTIMER_PHYS);
3060}
3061
3062void arm_gt_vtimer_cb(void *opaque)
3063{
3064 ARMCPU *cpu = opaque;
3065
3066 gt_recalc_timer(cpu, GTIMER_VIRT);
3067}
3068
b0e66d95
EI
3069void arm_gt_htimer_cb(void *opaque)
3070{
3071 ARMCPU *cpu = opaque;
3072
3073 gt_recalc_timer(cpu, GTIMER_HYP);
3074}
3075
b4d3978c
PM
3076void arm_gt_stimer_cb(void *opaque)
3077{
3078 ARMCPU *cpu = opaque;
3079
3080 gt_recalc_timer(cpu, GTIMER_SEC);
3081}
3082
8c94b071
RH
3083void arm_gt_hvtimer_cb(void *opaque)
3084{
3085 ARMCPU *cpu = opaque;
3086
3087 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3088}
3089
96eec6b2
AJ
3090static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3091{
3092 ARMCPU *cpu = env_archcpu(env);
3093
3094 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3095}
3096
55d284af 3097static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
9b37a28c
FR
3098 /*
3099 * Note that CNTFRQ is purely reads-as-written for the benefit
55d284af
PM
3100 * of software; writing it doesn't actually change the timer frequency.
3101 * Our reset value matches the fixed frequency we implement the timer at.
3102 */
3103 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3104 .type = ARM_CP_ALIAS,
a7adc4b7
PM
3105 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3106 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
3107 },
3108 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3109 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3110 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af 3111 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
96eec6b2 3112 .resetfn = arm_gt_cntfrq_reset,
55d284af
PM
3113 },
3114 /* overall control: mostly access permissions */
a7adc4b7
PM
3115 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3116 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
3117 .access = PL1_RW,
3118 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3119 .resetvalue = 0,
3120 },
3121 /* per-timer control */
3122 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 3123 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3124 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3125 .accessfn = gt_ptimer_access,
3126 .fieldoffset = offsetoflow32(CPUARMState,
3127 cp15.c14_timer[GTIMER_PHYS].ctl),
bb5972e4
RH
3128 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3129 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7 3130 },
9c513e78 3131 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
3132 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3133 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3134 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
9ff9dd3c
PM
3135 .accessfn = gt_ptimer_access,
3136 .fieldoffset = offsetoflow32(CPUARMState,
3137 cp15.c14_timer[GTIMER_SEC].ctl),
3138 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3139 },
a7adc4b7
PM
3140 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3141 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
daf1dc5f 3142 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3143 .accessfn = gt_ptimer_access,
55d284af
PM
3144 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3145 .resetvalue = 0,
bb5972e4
RH
3146 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3147 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3148 },
3149 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
daf1dc5f 3150 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3151 .accessfn = gt_vtimer_access,
3152 .fieldoffset = offsetoflow32(CPUARMState,
3153 cp15.c14_timer[GTIMER_VIRT].ctl),
bb5972e4
RH
3154 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3155 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
3156 },
3157 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3158 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
daf1dc5f 3159 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3160 .accessfn = gt_vtimer_access,
55d284af
PM
3161 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3162 .resetvalue = 0,
bb5972e4
RH
3163 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3164 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3165 },
3166 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3167 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 3168 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3169 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3170 .accessfn = gt_ptimer_access,
bb5972e4 3171 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
55d284af 3172 },
9c513e78 3173 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
3174 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3175 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3176 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
9ff9dd3c
PM
3177 .accessfn = gt_ptimer_access,
3178 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3179 },
a7adc4b7
PM
3180 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3181 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
daf1dc5f 3182 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3183 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
bb5972e4 3184 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
a7adc4b7 3185 },
55d284af 3186 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
daf1dc5f 3187 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3188 .accessfn = gt_vtimer_access,
bb5972e4 3189 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
55d284af 3190 },
a7adc4b7
PM
3191 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3192 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
daf1dc5f 3193 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3194 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
bb5972e4 3195 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
a7adc4b7 3196 },
55d284af
PM
3197 /* The counter itself */
3198 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 3199 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3200 .accessfn = gt_pct_access,
a7adc4b7
PM
3201 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3202 },
3203 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3204 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 3205 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3206 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
3207 },
3208 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 3209 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3210 .accessfn = gt_vct_access,
edac4d8a 3211 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
3212 },
3213 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3214 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 3215 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3216 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
3217 },
3218 /* Comparison value, indicating when the timer goes off */
3219 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3220 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3221 .access = PL0_RW,
7a0e58fa 3222 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3223 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 3224 .accessfn = gt_ptimer_access,
bb5972e4
RH
3225 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3226 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7 3227 },
9c513e78 3228 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3229 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3230 .access = PL0_RW,
9ff9dd3c
PM
3231 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3232 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3233 .accessfn = gt_ptimer_access,
3234 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3235 },
a7adc4b7
PM
3236 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3237 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
daf1dc5f 3238 .access = PL0_RW,
a7adc4b7
PM
3239 .type = ARM_CP_IO,
3240 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 3241 .resetvalue = 0, .accessfn = gt_ptimer_access,
bb5972e4
RH
3242 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3243 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
55d284af
PM
3244 },
3245 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
daf1dc5f 3246 .access = PL0_RW,
7a0e58fa 3247 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3248 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 3249 .accessfn = gt_vtimer_access,
bb5972e4
RH
3250 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3251 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
3252 },
3253 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3254 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
daf1dc5f 3255 .access = PL0_RW,
a7adc4b7
PM
3256 .type = ARM_CP_IO,
3257 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3258 .resetvalue = 0, .accessfn = gt_vtimer_access,
bb5972e4
RH
3259 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3260 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
55d284af 3261 },
9b37a28c
FR
3262 /*
3263 * Secure timer -- this is actually restricted to only EL3
b4d3978c
PM
3264 * and configurably Secure-EL1 via the accessfn.
3265 */
3266 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3267 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3268 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3269 .accessfn = gt_stimer_access,
3270 .readfn = gt_sec_tval_read,
3271 .writefn = gt_sec_tval_write,
3272 .resetfn = gt_sec_timer_reset,
3273 },
3274 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3275 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3276 .type = ARM_CP_IO, .access = PL1_RW,
3277 .accessfn = gt_stimer_access,
3278 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3279 .resetvalue = 0,
3280 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3281 },
3282 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3283 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3284 .type = ARM_CP_IO, .access = PL1_RW,
3285 .accessfn = gt_stimer_access,
3286 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3287 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3288 },
55d284af
PM
3289};
3290
bb5972e4
RH
3291static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3292 bool isread)
3293{
3294 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3295 return CP_ACCESS_TRAP;
3296 }
3297 return CP_ACCESS_OK;
3298}
3299
55d284af 3300#else
26c4a83b 3301
9b37a28c
FR
3302/*
3303 * In user-mode most of the generic timer registers are inaccessible
26c4a83b 3304 * however modern kernels (4.12+) allow access to cntvct_el0
55d284af 3305 */
26c4a83b
AB
3306
3307static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3308{
7def8754
AJ
3309 ARMCPU *cpu = env_archcpu(env);
3310
9b37a28c
FR
3311 /*
3312 * Currently we have no support for QEMUTimer in linux-user so we
26c4a83b
AB
3313 * can't call gt_get_countervalue(env), instead we directly
3314 * call the lower level functions.
3315 */
7def8754 3316 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
26c4a83b
AB
3317}
3318
6cc7a3ae 3319static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
26c4a83b
AB
3320 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3321 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3322 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3323 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3324 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3325 },
3326 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3327 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3328 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3329 .readfn = gt_virt_cnt_read,
3330 },
6cc7a3ae
PM
3331};
3332
55d284af
PM
3333#endif
3334
c4241c7d 3335static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 3336{
891a2fe7 3337 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 3338 raw_write(env, ri, value);
891a2fe7 3339 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 3340 raw_write(env, ri, value & 0xfffff6ff);
4a501606 3341 } else {
8d5c773e 3342 raw_write(env, ri, value & 0xfffff1ff);
4a501606 3343 }
4a501606
PM
3344}
3345
3346#ifndef CONFIG_USER_ONLY
3347/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 3348
3f208fd7
PM
3349static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3350 bool isread)
92611c00
PM
3351{
3352 if (ri->opc2 & 4) {
9b37a28c
FR
3353 /*
3354 * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
87562e4f
PM
3355 * Secure EL1 (which can only happen if EL3 is AArch64).
3356 * They are simply UNDEF if executed from NS EL1.
3357 * They function normally from EL2 or EL3.
92611c00 3358 */
87562e4f
PM
3359 if (arm_current_el(env) == 1) {
3360 if (arm_is_secure_below_el3(env)) {
926c1b97 3361 if (env->cp15.scr_el3 & SCR_EEL2) {
ce9a8863 3362 return CP_ACCESS_TRAP_EL2;
926c1b97 3363 }
ce9a8863 3364 return CP_ACCESS_TRAP_EL3;
87562e4f
PM
3365 }
3366 return CP_ACCESS_TRAP_UNCATEGORIZED;
3367 }
92611c00
PM
3368 }
3369 return CP_ACCESS_OK;
3370}
3371
9fb005b0 3372#ifdef CONFIG_TCG
b17d86eb
PM
3373static int par_el1_shareability(GetPhysAddrResult *res)
3374{
3375 /*
3376 * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3377 * memory -- see pseudocode PAREncodeShareability().
3378 */
3379 if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3380 res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3381 return 2;
3382 }
3383 return res->cacheattrs.shareability;
3384}
3385
060e8a48 3386static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
7aee3cb9 3387 MMUAccessType access_type, ARMMMUIdx mmu_idx,
e1ee56ec 3388 ARMSecuritySpace ss)
4a501606 3389{
b7cc4e82 3390 bool ret;
01c097f7 3391 uint64_t par64;
1313e2d7 3392 bool format64 = false;
e14b5a23 3393 ARMMMUFaultInfo fi = {};
de05a709 3394 GetPhysAddrResult res = {};
4a501606 3395
f1269a98
JPB
3396 /*
3397 * I_MXTJT: Granule protection checks are not performed on the final address
3398 * of a successful translation.
3399 */
e1ee56ec
JPB
3400 ret = get_phys_addr_with_space_nogpc(env, value, access_type, mmu_idx, ss,
3401 &res, &fi);
1313e2d7 3402
9f225e60
PM
3403 /*
3404 * ATS operations only do S1 or S1+S2 translations, so we never
3405 * have to deal with the ARMCacheAttrs format for S2 only.
3406 */
de05a709 3407 assert(!res.cacheattrs.is_s2_format);
9f225e60 3408
0710b2fa
PM
3409 if (ret) {
3410 /*
3411 * Some kinds of translation fault must cause exceptions rather
3412 * than being reported in the PAR.
3413 */
3414 int current_el = arm_current_el(env);
3415 int target_el;
3416 uint32_t syn, fsr, fsc;
3417 bool take_exc = false;
3418
b1a10c86 3419 if (fi.s1ptw && current_el == 1
fee7aa46 3420 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
0710b2fa
PM
3421 /*
3422 * Synchronous stage 2 fault on an access made as part of the
3423 * translation table walk for AT S1E0* or AT S1E1* insn
3424 * executed from NS EL1. If this is a synchronous external abort
3425 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3426 * to EL3. Otherwise the fault is taken as an exception to EL2,
3427 * and HPFAR_EL2 holds the faulting IPA.
3428 */
3429 if (fi.type == ARMFault_SyncExternalOnWalk &&
3430 (env->cp15.scr_el3 & SCR_EA)) {
3431 target_el = 3;
3432 } else {
3433 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
9861248f
RDC
3434 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3435 env->cp15.hpfar_el2 |= HPFAR_NS;
3436 }
0710b2fa
PM
3437 target_el = 2;
3438 }
3439 take_exc = true;
3440 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3441 /*
3442 * Synchronous external aborts during a translation table walk
3443 * are taken as Data Abort exceptions.
3444 */
3445 if (fi.stage2) {
3446 if (current_el == 3) {
3447 target_el = 3;
3448 } else {
3449 target_el = 2;
3450 }
3451 } else {
3452 target_el = exception_target_el(env);
3453 }
3454 take_exc = true;
3455 }
3456
3457 if (take_exc) {
3458 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3459 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3460 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3461 fsr = arm_fi_to_lfsc(&fi);
3462 fsc = extract32(fsr, 0, 6);
3463 } else {
3464 fsr = arm_fi_to_sfsc(&fi);
3465 fsc = 0x3f;
3466 }
3467 /*
3468 * Report exception with ESR indicating a fault due to a
3469 * translation table walk for a cache maintenance instruction.
3470 */
e24fd076 3471 syn = syn_data_abort_no_iss(current_el == target_el, 0,
0710b2fa
PM
3472 fi.ea, 1, fi.s1ptw, 1, fsc);
3473 env->exception.vaddress = value;
3474 env->exception.fsr = fsr;
3475 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3476 }
3477 }
3478
1313e2d7
EI
3479 if (is_a64(env)) {
3480 format64 = true;
3481 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3482 /*
3483 * ATS1Cxx:
3484 * * TTBCR.EAE determines whether the result is returned using the
3485 * 32-bit or the 64-bit PAR format
3486 * * Instructions executed in Hyp mode always use the 64bit format
3487 *
3488 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3489 * * The Non-secure TTBCR.EAE bit is set to 1
3490 * * The implementation includes EL2, and the value of HCR.VM is 1
3491 *
9d1bab33
PM
3492 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3493 *
23463e0e 3494 * ATS1Hx always uses the 64bit format.
1313e2d7
EI
3495 */
3496 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3497
3498 if (arm_feature(env, ARM_FEATURE_EL2)) {
452ef8cb
RH
3499 if (mmu_idx == ARMMMUIdx_E10_0 ||
3500 mmu_idx == ARMMMUIdx_E10_1 ||
3501 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9d1bab33 3502 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
1313e2d7
EI
3503 } else {
3504 format64 |= arm_current_el(env) == 2;
3505 }
3506 }
3507 }
3508
3509 if (format64) {
5efe9ed4 3510 /* Create a 64-bit PAR */
01c097f7 3511 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 3512 if (!ret) {
7fa7ea8f
RH
3513 par64 |= res.f.phys_addr & ~0xfffULL;
3514 if (!res.f.attrs.secure) {
8bf5b6a9
PM
3515 par64 |= (1 << 9); /* NS */
3516 }
de05a709 3517 par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
b17d86eb 3518 par64 |= par_el1_shareability(&res) << 7; /* SH */
4a501606 3519 } else {
5efe9ed4
PM
3520 uint32_t fsr = arm_fi_to_lfsc(&fi);
3521
702a9357 3522 par64 |= 1; /* F */
b7cc4e82 3523 par64 |= (fsr & 0x3f) << 1; /* FS */
0f7b791b
PM
3524 if (fi.stage2) {
3525 par64 |= (1 << 9); /* S */
3526 }
3527 if (fi.s1ptw) {
3528 par64 |= (1 << 8); /* PTW */
3529 }
4a501606
PM
3530 }
3531 } else {
9b37a28c
FR
3532 /*
3533 * fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
3534 * translation table format (with WnR always clear).
3535 * Convert it to a 32-bit PAR.
3536 */
b7cc4e82 3537 if (!ret) {
702a9357 3538 /* We do not set any attribute bits in the PAR */
7fa7ea8f 3539 if (res.f.lg_page_size == 24
702a9357 3540 && arm_feature(env, ARM_FEATURE_V7)) {
7fa7ea8f 3541 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
702a9357 3542 } else {
7fa7ea8f 3543 par64 = res.f.phys_addr & 0xfffff000;
702a9357 3544 }
7fa7ea8f 3545 if (!res.f.attrs.secure) {
8bf5b6a9
PM
3546 par64 |= (1 << 9); /* NS */
3547 }
702a9357 3548 } else {
5efe9ed4
PM
3549 uint32_t fsr = arm_fi_to_sfsc(&fi);
3550
b7cc4e82
PC
3551 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3552 ((fsr & 0xf) << 1) | 1;
702a9357 3553 }
4a501606 3554 }
060e8a48
PM
3555 return par64;
3556}
9fb005b0 3557#endif /* CONFIG_TCG */
060e8a48
PM
3558
3559static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3560{
9fb005b0 3561#ifdef CONFIG_TCG
03ae85f8 3562 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 3563 uint64_t par64;
d3649702
PM
3564 ARMMMUIdx mmu_idx;
3565 int el = arm_current_el(env);
e1ee56ec 3566 ARMSecuritySpace ss = arm_security_space(env);
060e8a48 3567
d3649702
PM
3568 switch (ri->opc2 & 6) {
3569 case 0:
04b07d29 3570 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
d3649702
PM
3571 switch (el) {
3572 case 3:
d902ae75 3573 mmu_idx = ARMMMUIdx_E3;
d3649702
PM
3574 break;
3575 case 2:
e1ee56ec 3576 g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */
04b07d29 3577 /* fall through */
d3649702 3578 case 1:
04b07d29 3579 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
d902ae75 3580 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
04b07d29 3581 } else {
d902ae75 3582 mmu_idx = ARMMMUIdx_Stage1_E1;
04b07d29 3583 }
d3649702
PM
3584 break;
3585 default:
3586 g_assert_not_reached();
3587 }
3588 break;
3589 case 2:
3590 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3591 switch (el) {
3592 case 3:
d902ae75 3593 mmu_idx = ARMMMUIdx_E10_0;
d3649702
PM
3594 break;
3595 case 2:
e1ee56ec 3596 g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */
2859d7b5 3597 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3598 break;
3599 case 1:
d902ae75 3600 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3601 break;
3602 default:
3603 g_assert_not_reached();
3604 }
3605 break;
3606 case 4:
3607 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
01b98b68 3608 mmu_idx = ARMMMUIdx_E10_1;
e1ee56ec 3609 ss = ARMSS_NonSecure;
d3649702
PM
3610 break;
3611 case 6:
3612 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
01b98b68 3613 mmu_idx = ARMMMUIdx_E10_0;
e1ee56ec 3614 ss = ARMSS_NonSecure;
d3649702
PM
3615 break;
3616 default:
3617 g_assert_not_reached();
3618 }
3619
e1ee56ec 3620 par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
01c097f7
FA
3621
3622 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3623#else
3624 /* Handled by hardware accelerator. */
3625 g_assert_not_reached();
3626#endif /* CONFIG_TCG */
4a501606 3627}
060e8a48 3628
14db7fe0
PM
3629static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3630 uint64_t value)
3631{
9fb005b0 3632#ifdef CONFIG_TCG
03ae85f8 3633 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
3634 uint64_t par64;
3635
7aee3cb9 3636 /* There is no SecureEL2 for AArch32. */
e1ee56ec
JPB
3637 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
3638 ARMSS_NonSecure);
14db7fe0
PM
3639
3640 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3641#else
3642 /* Handled by hardware accelerator. */
3643 g_assert_not_reached();
3644#endif /* CONFIG_TCG */
14db7fe0
PM
3645}
3646
1acd00ef
JPB
3647static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
3648 bool isread)
3649{
3650 /*
3651 * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
3652 * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
3653 * only happen when executing at EL3 because that combination also causes an
3654 * illegal exception return. We don't need to check FEAT_RME either, because
3655 * scr_write() ensures that the NSE bit is not set otherwise.
3656 */
3657 if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
3658 return CP_ACCESS_TRAP;
3659 }
3660 return CP_ACCESS_OK;
3661}
3662
3f208fd7
PM
3663static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3664 bool isread)
2a47df95 3665{
926c1b97
RDC
3666 if (arm_current_el(env) == 3 &&
3667 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
2a47df95
PM
3668 return CP_ACCESS_TRAP;
3669 }
1acd00ef 3670 return at_e012_access(env, ri, isread);
2a47df95
PM
3671}
3672
060e8a48
PM
3673static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3674 uint64_t value)
3675{
9fb005b0 3676#ifdef CONFIG_TCG
03ae85f8 3677 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702 3678 ARMMMUIdx mmu_idx;
638d5dbd
AK
3679 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3680 bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
d3649702
PM
3681
3682 switch (ri->opc2 & 6) {
3683 case 0:
3684 switch (ri->opc1) {
04b07d29
RH
3685 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3686 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
638d5dbd
AK
3687 mmu_idx = regime_e20 ?
3688 ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
04b07d29 3689 } else {
638d5dbd 3690 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
04b07d29 3691 }
d3649702
PM
3692 break;
3693 case 4: /* AT S1E2R, AT S1E2W */
638d5dbd 3694 mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
d3649702
PM
3695 break;
3696 case 6: /* AT S1E3R, AT S1E3W */
d902ae75 3697 mmu_idx = ARMMMUIdx_E3;
d3649702
PM
3698 break;
3699 default:
3700 g_assert_not_reached();
3701 }
3702 break;
3703 case 2: /* AT S1E0R, AT S1E0W */
638d5dbd 3704 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3705 break;
3706 case 4: /* AT S12E1R, AT S12E1W */
638d5dbd 3707 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
d3649702
PM
3708 break;
3709 case 6: /* AT S12E0R, AT S12E0W */
638d5dbd 3710 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
d3649702
PM
3711 break;
3712 default:
3713 g_assert_not_reached();
3714 }
060e8a48 3715
7aee3cb9 3716 env->cp15.par_el[1] = do_ats_write(env, value, access_type,
e1ee56ec 3717 mmu_idx, arm_security_space(env));
9fb005b0
PMD
3718#else
3719 /* Handled by hardware accelerator. */
3720 g_assert_not_reached();
3721#endif /* CONFIG_TCG */
060e8a48 3722}
4a501606
PM
3723#endif
3724
18032bec
PM
3725/* Return basic MPU access permission bits. */
3726static uint32_t simple_mpu_ap_bits(uint32_t val)
3727{
3728 uint32_t ret;
3729 uint32_t mask;
3730 int i;
3731 ret = 0;
3732 mask = 3;
3733 for (i = 0; i < 16; i += 2) {
3734 ret |= (val >> i) & mask;
3735 mask <<= 2;
3736 }
3737 return ret;
3738}
3739
3740/* Pad basic MPU access permission bits to extended format. */
3741static uint32_t extended_mpu_ap_bits(uint32_t val)
3742{
3743 uint32_t ret;
3744 uint32_t mask;
3745 int i;
3746 ret = 0;
3747 mask = 3;
3748 for (i = 0; i < 16; i += 2) {
3749 ret |= (val & mask) << i;
3750 mask <<= 2;
3751 }
3752 return ret;
3753}
3754
c4241c7d
PM
3755static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3756 uint64_t value)
18032bec 3757{
7e09797c 3758 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
3759}
3760
c4241c7d 3761static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3762{
7e09797c 3763 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
3764}
3765
c4241c7d
PM
3766static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3767 uint64_t value)
18032bec 3768{
7e09797c 3769 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
3770}
3771
c4241c7d 3772static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3773{
7e09797c 3774 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
3775}
3776
6cb0b013
PC
3777static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3778{
3779 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3780
3781 if (!u32p) {
3782 return 0;
3783 }
3784
1bc04a88 3785 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
3786 return *u32p;
3787}
3788
3789static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3790 uint64_t value)
3791{
2fc0cc0e 3792 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3793 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3794
3795 if (!u32p) {
3796 return;
3797 }
3798
1bc04a88 3799 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 3800 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
3801 *u32p = value;
3802}
3803
6cb0b013
PC
3804static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3805 uint64_t value)
3806{
2fc0cc0e 3807 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3808 uint32_t nrgs = cpu->pmsav7_dregion;
3809
3810 if (value >= nrgs) {
3811 qemu_log_mask(LOG_GUEST_ERROR,
3812 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3813 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3814 return;
3815 }
3816
3817 raw_write(env, ri, value);
3818}
3819
761c4642
TR
3820static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3821 uint64_t value)
3822{
3823 ARMCPU *cpu = env_archcpu(env);
3824
3825 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3826 env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3827}
3828
3829static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3830{
3831 return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3832}
3833
3834static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3835 uint64_t value)
3836{
3837 ARMCPU *cpu = env_archcpu(env);
3838
3839 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3840 env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3841}
3842
3843static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3844{
3845 return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3846}
3847
3848static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3849 uint64_t value)
3850{
3851 ARMCPU *cpu = env_archcpu(env);
3852
3853 /*
3854 * Ignore writes that would select not implemented region.
3855 * This is architecturally UNPREDICTABLE.
3856 */
3857 if (value >= cpu->pmsav7_dregion) {
3858 return;
3859 }
3860
3861 env->pmsav7.rnr[M_REG_NS] = value;
3862}
3863
3864static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3865 uint64_t value)
3866{
3867 ARMCPU *cpu = env_archcpu(env);
3868
3869 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3870 env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3871}
3872
3873static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3874{
3875 return env->pmsav8.hprbar[env->pmsav8.hprselr];
3876}
3877
3878static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3879 uint64_t value)
3880{
3881 ARMCPU *cpu = env_archcpu(env);
3882
3883 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3884 env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
3885}
3886
3887static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3888{
3889 return env->pmsav8.hprlar[env->pmsav8.hprselr];
3890}
3891
3892static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3893 uint64_t value)
3894{
3895 uint32_t n;
3896 uint32_t bit;
3897 ARMCPU *cpu = env_archcpu(env);
3898
3899 /* Ignore writes to unimplemented regions */
3900 int rmax = MIN(cpu->pmsav8r_hdregion, 32);
3901 value &= MAKE_64BIT_MASK(0, rmax);
3902
3903 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3904
3905 /* Register alias is only valid for first 32 indexes */
3906 for (n = 0; n < rmax; ++n) {
3907 bit = extract32(value, n, 1);
3908 env->pmsav8.hprlar[n] = deposit32(
3909 env->pmsav8.hprlar[n], 0, 1, bit);
3910 }
3911}
3912
3913static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3914{
3915 uint32_t n;
3916 uint32_t result = 0x0;
3917 ARMCPU *cpu = env_archcpu(env);
3918
3919 /* Register alias is only valid for first 32 indexes */
3920 for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
3921 if (env->pmsav8.hprlar[n] & 0x1) {
3922 result |= (0x1 << n);
3923 }
3924 }
3925 return result;
3926}
3927
3928static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3929 uint64_t value)
3930{
3931 ARMCPU *cpu = env_archcpu(env);
3932
3933 /*
3934 * Ignore writes that would select not implemented region.
3935 * This is architecturally UNPREDICTABLE.
3936 */
3937 if (value >= cpu->pmsav8r_hdregion) {
3938 return;
3939 }
3940
3941 env->pmsav8.hprselr = value;
3942}
3943
3944static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
3945 uint64_t value)
3946{
3947 ARMCPU *cpu = env_archcpu(env);
3948 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3949 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3950
3951 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3952
3953 if (ri->opc1 & 4) {
3954 if (index >= cpu->pmsav8r_hdregion) {
3955 return;
3956 }
3957 if (ri->opc2 & 0x1) {
3958 env->pmsav8.hprlar[index] = value;
3959 } else {
3960 env->pmsav8.hprbar[index] = value;
3961 }
3962 } else {
3963 if (index >= cpu->pmsav7_dregion) {
3964 return;
3965 }
3966 if (ri->opc2 & 0x1) {
3967 env->pmsav8.rlar[M_REG_NS][index] = value;
3968 } else {
3969 env->pmsav8.rbar[M_REG_NS][index] = value;
3970 }
3971 }
3972}
3973
3974static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
3975{
3976 ARMCPU *cpu = env_archcpu(env);
3977 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3978 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3979
3980 if (ri->opc1 & 4) {
3981 if (index >= cpu->pmsav8r_hdregion) {
3982 return 0x0;
3983 }
3984 if (ri->opc2 & 0x1) {
3985 return env->pmsav8.hprlar[index];
3986 } else {
3987 return env->pmsav8.hprbar[index];
3988 }
3989 } else {
3990 if (index >= cpu->pmsav7_dregion) {
3991 return 0x0;
3992 }
3993 if (ri->opc2 & 0x1) {
3994 return env->pmsav8.rlar[M_REG_NS][index];
3995 } else {
3996 return env->pmsav8.rbar[M_REG_NS][index];
3997 }
3998 }
3999}
4000
4001static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
4002 { .name = "PRBAR",
4003 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
4004 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4005 .accessfn = access_tvm_trvm,
4006 .readfn = prbar_read, .writefn = prbar_write },
4007 { .name = "PRLAR",
4008 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
4009 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4010 .accessfn = access_tvm_trvm,
4011 .readfn = prlar_read, .writefn = prlar_write },
4012 { .name = "PRSELR", .resetvalue = 0,
4013 .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
4014 .access = PL1_RW, .accessfn = access_tvm_trvm,
4015 .writefn = prselr_write,
4016 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
4017 { .name = "HPRBAR", .resetvalue = 0,
4018 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
4019 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4020 .readfn = hprbar_read, .writefn = hprbar_write },
4021 { .name = "HPRLAR",
4022 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
4023 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4024 .readfn = hprlar_read, .writefn = hprlar_write },
4025 { .name = "HPRSELR", .resetvalue = 0,
4026 .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
4027 .access = PL2_RW,
4028 .writefn = hprselr_write,
4029 .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
4030 { .name = "HPRENR",
4031 .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
4032 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4033 .readfn = hprenr_read, .writefn = hprenr_write },
4034};
4035
6cb0b013 4036static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
9b37a28c
FR
4037 /*
4038 * Reset for all these registers is handled in arm_cpu_reset(),
69ceea64
PM
4039 * because the PMSAv7 is also used by M-profile CPUs, which do
4040 * not register cpregs but still need the state to be reset.
4041 */
6cb0b013
PC
4042 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4043 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4044 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
4045 .readfn = pmsav7_read, .writefn = pmsav7_write,
4046 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
4047 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4048 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4049 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
4050 .readfn = pmsav7_read, .writefn = pmsav7_write,
4051 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
4052 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4053 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4054 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
4055 .readfn = pmsav7_read, .writefn = pmsav7_write,
4056 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
4057 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4058 .access = PL1_RW,
1bc04a88 4059 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
4060 .writefn = pmsav7_rgnr_write,
4061 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
4062};
4063
18032bec
PM
4064static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4065 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 4066 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 4067 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
4068 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4069 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 4070 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 4071 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
4072 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4073 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4074 .access = PL1_RW,
7e09797c
PM
4075 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4076 .resetvalue = 0, },
18032bec
PM
4077 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4078 .access = PL1_RW,
7e09797c
PM
4079 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4080 .resetvalue = 0, },
ecce5c3c
PM
4081 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4082 .access = PL1_RW,
4083 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4084 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4085 .access = PL1_RW,
4086 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 4087 /* Protection region base and size registers */
e508a92b
PM
4088 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4089 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4090 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4091 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4092 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4093 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4094 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4095 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4096 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4097 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4098 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4099 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4100 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4101 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4102 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4103 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4104 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4105 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4106 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4107 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4108 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4109 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4110 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4111 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
4112};
4113
cb4a0a34
PM
4114static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4115 uint64_t value)
ecce5c3c 4116{
cb4a0a34 4117 ARMCPU *cpu = env_archcpu(env);
2ebcebe2 4118
e389be16
FA
4119 if (!arm_feature(env, ARM_FEATURE_V8)) {
4120 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
cb4a0a34
PM
4121 /*
4122 * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4123 * using Long-descriptor translation table format
4124 */
e389be16
FA
4125 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4126 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
cb4a0a34
PM
4127 /*
4128 * In an implementation that includes the Security Extensions
e389be16
FA
4129 * TTBCR has additional fields PD0 [4] and PD1 [5] for
4130 * Short-descriptor translation table format.
4131 */
4132 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4133 } else {
4134 value &= TTBCR_N;
4135 }
e42c4db3 4136 }
e389be16 4137
d4e6df63 4138 if (arm_feature(env, ARM_FEATURE_LPAE)) {
9b37a28c
FR
4139 /*
4140 * With LPAE the TTBCR could result in a change of ASID
d4e6df63
PM
4141 * via the TTBCR.A1 bit, so do a TLB flush.
4142 */
d10eb08f 4143 tlb_flush(CPU(cpu));
d4e6df63 4144 }
cb4a0a34 4145 raw_write(env, ri, value);
ecce5c3c
PM
4146}
4147
d06dc933 4148static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
cb2e37df
PM
4149 uint64_t value)
4150{
2fc0cc0e 4151 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 4152
cb2e37df 4153 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 4154 tlb_flush(CPU(cpu));
cb4a0a34 4155 raw_write(env, ri, value);
cb2e37df
PM
4156}
4157
327ed10f
PM
4158static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4159 uint64_t value)
4160{
93f379b0
RH
4161 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
4162 if (cpreg_field_is_64bit(ri) &&
4163 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
2fc0cc0e 4164 ARMCPU *cpu = env_archcpu(env);
d10eb08f 4165 tlb_flush(CPU(cpu));
327ed10f
PM
4166 }
4167 raw_write(env, ri, value);
4168}
4169
ed30da8e
RH
4170static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4171 uint64_t value)
4172{
d06dc933
RH
4173 /*
4174 * If we are running with E2&0 regime, then an ASID is active.
4175 * Flush if that might be changing. Note we're not checking
4176 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4177 * holds the active ASID, only checking the field that might.
4178 */
4179 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4180 (arm_hcr_el2_eff(env) & HCR_E2H)) {
b6ad6062
RDC
4181 uint16_t mask = ARMMMUIdxBit_E20_2 |
4182 ARMMMUIdxBit_E20_2_PAN |
4183 ARMMMUIdxBit_E20_0;
b6ad6062 4184 tlb_flush_by_mmuidx(env_cpu(env), mask);
d06dc933 4185 }
ed30da8e
RH
4186 raw_write(env, ri, value);
4187}
4188
b698e9cf
EI
4189static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4190 uint64_t value)
4191{
2fc0cc0e 4192 ARMCPU *cpu = env_archcpu(env);
b698e9cf
EI
4193 CPUState *cs = CPU(cpu);
4194
97fa9350
RH
4195 /*
4196 * A change in VMID to the stage2 page table (Stage2) invalidates
575a94af 4197 * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
97fa9350 4198 */
00b20ee4 4199 if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
575a94af 4200 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
b698e9cf 4201 }
00b20ee4 4202 raw_write(env, ri, value);
b698e9cf
EI
4203}
4204
8e5d75c9 4205static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 4206 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218 4207 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4a7e2d73 4208 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 4209 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 4210 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
84929218 4211 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
88ca1c2d
FA
4212 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4213 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9 4214 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
84929218 4215 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
8e5d75c9
PC
4216 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4217 offsetof(CPUARMState, cp15.dfar_ns) } },
4218 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4219 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218 4220 .access = PL1_RW, .accessfn = access_tvm_trvm,
b19ed03c 4221 .fgt = FGT_FAR_EL1,
84929218 4222 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
8e5d75c9 4223 .resetvalue = 0, },
8e5d75c9
PC
4224};
4225
4226static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
4227 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4228 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
84929218 4229 .access = PL1_RW, .accessfn = access_tvm_trvm,
b19ed03c 4230 .fgt = FGT_ESR_EL1,
d81c519c 4231 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 4232 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 4233 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
84929218 4234 .access = PL1_RW, .accessfn = access_tvm_trvm,
bd8db7d9 4235 .fgt = FGT_TTBR0_EL1,
587f8b33 4236 .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
7dd8c9af
FA
4237 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4238 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 4239 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 4240 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
84929218 4241 .access = PL1_RW, .accessfn = access_tvm_trvm,
bd8db7d9 4242 .fgt = FGT_TTBR1_EL1,
587f8b33 4243 .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
7dd8c9af
FA
4244 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4245 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
4246 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4247 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218 4248 .access = PL1_RW, .accessfn = access_tvm_trvm,
67dd8030 4249 .fgt = FGT_TCR_EL1,
84929218 4250 .writefn = vmsa_tcr_el12_write,
cb4a0a34
PM
4251 .raw_writefn = raw_write,
4252 .resetvalue = 0,
11f136ee 4253 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 4254 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
4255 .access = PL1_RW, .accessfn = access_tvm_trvm,
4256 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
cb4a0a34
PM
4257 .raw_writefn = raw_write,
4258 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4259 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
4260};
4261
9b37a28c
FR
4262/*
4263 * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
ab638a32
RH
4264 * qemu tlbs nor adjusting cached masks.
4265 */
4266static const ARMCPRegInfo ttbcr2_reginfo = {
4267 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
84929218
RH
4268 .access = PL1_RW, .accessfn = access_tvm_trvm,
4269 .type = ARM_CP_ALIAS,
d102058e 4270 .bank_fieldoffsets = {
cb4a0a34
PM
4271 offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4272 offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
d102058e 4273 },
ab638a32
RH
4274};
4275
c4241c7d
PM
4276static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4277 uint64_t value)
1047b9d7
PM
4278{
4279 env->cp15.c15_ticonfig = value & 0xe7;
4280 /* The OS_TYPE bit in this register changes the reported CPUID! */
4281 env->cp15.c0_cpuid = (value & (1 << 5)) ?
4282 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
4283}
4284
c4241c7d
PM
4285static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4286 uint64_t value)
1047b9d7
PM
4287{
4288 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
4289}
4290
c4241c7d
PM
4291static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4292 uint64_t value)
1047b9d7
PM
4293{
4294 /* Wait-for-interrupt (deprecated) */
2fc0cc0e 4295 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
1047b9d7
PM
4296}
4297
c4241c7d
PM
4298static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4299 uint64_t value)
c4804214 4300{
9b37a28c
FR
4301 /*
4302 * On OMAP there are registers indicating the max/min index of dcache lines
c4804214
PM
4303 * containing a dirty line; cache flush operations have to reset these.
4304 */
4305 env->cp15.c15_i_max = 0x000;
4306 env->cp15.c15_i_min = 0xff0;
c4804214
PM
4307}
4308
18032bec
PM
4309static const ARMCPRegInfo omap_cp_reginfo[] = {
4310 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4311 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 4312 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 4313 .resetvalue = 0, },
1047b9d7
PM
4314 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4315 .access = PL1_RW, .type = ARM_CP_NOP },
4316 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4317 .access = PL1_RW,
4318 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4319 .writefn = omap_ticonfig_write },
4320 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4321 .access = PL1_RW,
4322 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4323 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4324 .access = PL1_RW, .resetvalue = 0xff0,
4325 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4326 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4327 .access = PL1_RW,
4328 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4329 .writefn = omap_threadid_write },
4330 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4331 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 4332 .type = ARM_CP_NO_RAW,
1047b9d7 4333 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
9b37a28c
FR
4334 /*
4335 * TODO: Peripheral port remap register:
1047b9d7
PM
4336 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4337 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4338 * when MMU is off.
4339 */
c4804214 4340 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 4341 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 4342 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 4343 .writefn = omap_cachemaint_write },
34f90529
PM
4344 { .name = "C9", .cp = 15, .crn = 9,
4345 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4346 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
4347};
4348
c4241c7d
PM
4349static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4350 uint64_t value)
1047b9d7 4351{
c0f4af17 4352 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
4353}
4354
4355static const ARMCPRegInfo xscale_cp_reginfo[] = {
4356 { .name = "XSCALE_CPAR",
4357 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4358 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4359 .writefn = xscale_cpar_write, },
2771db27
PM
4360 { .name = "XSCALE_AUXCR",
4361 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4362 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4363 .resetvalue = 0, },
9b37a28c
FR
4364 /*
4365 * XScale specific cache-lockdown: since we have no cache we NOP these
3b771579
PM
4366 * and hope the guest does not really rely on cache behaviour.
4367 */
4368 { .name = "XSCALE_LOCK_ICACHE_LINE",
4369 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4370 .access = PL1_W, .type = ARM_CP_NOP },
4371 { .name = "XSCALE_UNLOCK_ICACHE",
4372 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4373 .access = PL1_W, .type = ARM_CP_NOP },
4374 { .name = "XSCALE_DCACHE_LOCK",
4375 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4376 .access = PL1_RW, .type = ARM_CP_NOP },
4377 { .name = "XSCALE_UNLOCK_DCACHE",
4378 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4379 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
4380};
4381
4382static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
9b37a28c
FR
4383 /*
4384 * RAZ/WI the whole crn=15 space, when we don't have a more specific
1047b9d7
PM
4385 * implementation of this implementation-defined space.
4386 * Ideally this should eventually disappear in favour of actually
4387 * implementing the correct behaviour for all cores.
4388 */
4389 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4390 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 4391 .access = PL1_RW,
7a0e58fa 4392 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 4393 .resetvalue = 0 },
18032bec
PM
4394};
4395
c4804214
PM
4396static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4397 /* Cache status: RAZ because we have no cache so it's always clean */
4398 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 4399 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4400 .resetvalue = 0 },
c4804214
PM
4401};
4402
4403static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
a07d9df0 4404 /* We never have a block transfer operation in progress */
c4804214 4405 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 4406 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4407 .resetvalue = 0 },
30b05bba
PM
4408 /* The cache ops themselves: these all NOP for QEMU */
4409 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
04215eb1 4410 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4411 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
04215eb1 4412 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4413 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
04215eb1 4414 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4415 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
04215eb1 4416 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4417 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
04215eb1 4418 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4419 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
04215eb1 4420 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
c4804214
PM
4421};
4422
4423static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
9b37a28c
FR
4424 /*
4425 * The cache test-and-clean instructions always return (1 << 30)
c4804214
PM
4426 * to indicate that there are no dirty cache lines.
4427 */
4428 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 4429 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4430 .resetvalue = (1 << 30) },
c4804214 4431 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 4432 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4433 .resetvalue = (1 << 30) },
c4804214
PM
4434};
4435
34f90529
PM
4436static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4437 /* Ignore ReadBuffer accesses */
4438 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4439 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 4440 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 4441 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
4442};
4443
731de9e6
EI
4444static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4445{
731de9e6 4446 unsigned int cur_el = arm_current_el(env);
731de9e6 4447
e6ef0169 4448 if (arm_is_el2_enabled(env) && cur_el == 1) {
731de9e6
EI
4449 return env->cp15.vpidr_el2;
4450 }
4451 return raw_read(env, ri);
4452}
4453
06a7e647 4454static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 4455{
2fc0cc0e 4456 ARMCPU *cpu = env_archcpu(env);
eb5e1d3c
PF
4457 uint64_t mpidr = cpu->mp_affinity;
4458
81bdde9d 4459 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 4460 mpidr |= (1U << 31);
9b37a28c
FR
4461 /*
4462 * Cores which are uniprocessor (non-coherent)
81bdde9d 4463 * but still implement the MP extensions set
a8e81b31 4464 * bit 30. (For instance, Cortex-R5).
81bdde9d 4465 */
a8e81b31
PC
4466 if (cpu->mp_is_up) {
4467 mpidr |= (1u << 30);
4468 }
81bdde9d 4469 }
c4241c7d 4470 return mpidr;
81bdde9d
PM
4471}
4472
06a7e647
EI
4473static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4474{
f0d574d6 4475 unsigned int cur_el = arm_current_el(env);
f0d574d6 4476
e6ef0169 4477 if (arm_is_el2_enabled(env) && cur_el == 1) {
f0d574d6
EI
4478 return env->cp15.vmpidr_el2;
4479 }
06a7e647
EI
4480 return mpidr_read_val(env);
4481}
4482
7ac681cf 4483static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 4484 /* NOP AMAIR0/1 */
b0fe2427
PM
4485 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4486 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
84929218 4487 .access = PL1_RW, .accessfn = access_tvm_trvm,
158c276c 4488 .fgt = FGT_AMAIR_EL1,
84929218 4489 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427 4490 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 4491 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
84929218
RH
4492 .access = PL1_RW, .accessfn = access_tvm_trvm,
4493 .type = ARM_CP_CONST, .resetvalue = 0 },
891a2fe7 4494 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
4495 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4496 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4497 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 4498 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
84929218
RH
4499 .access = PL1_RW, .accessfn = access_tvm_trvm,
4500 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4501 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4502 offsetof(CPUARMState, cp15.ttbr0_ns) },
587f8b33 4503 .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
891a2fe7 4504 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
84929218
RH
4505 .access = PL1_RW, .accessfn = access_tvm_trvm,
4506 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4507 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4508 offsetof(CPUARMState, cp15.ttbr1_ns) },
587f8b33 4509 .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
7ac681cf
PM
4510};
4511
c4241c7d 4512static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4513{
c4241c7d 4514 return vfp_get_fpcr(env);
b0d2b7d0
PM
4515}
4516
c4241c7d
PM
4517static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4518 uint64_t value)
b0d2b7d0
PM
4519{
4520 vfp_set_fpcr(env, value);
b0d2b7d0
PM
4521}
4522
c4241c7d 4523static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4524{
c4241c7d 4525 return vfp_get_fpsr(env);
b0d2b7d0
PM
4526}
4527
c4241c7d
PM
4528static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4529 uint64_t value)
b0d2b7d0
PM
4530{
4531 vfp_set_fpsr(env, value);
b0d2b7d0
PM
4532}
4533
3f208fd7
PM
4534static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4535 bool isread)
c2b820fe 4536{
aaec1432 4537 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
c2b820fe
PM
4538 return CP_ACCESS_TRAP;
4539 }
4540 return CP_ACCESS_OK;
4541}
4542
4543static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4544 uint64_t value)
4545{
4546 env->daif = value & PSTATE_DAIF;
4547}
4548
220f508f
RH
4549static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4550{
4551 return env->pstate & PSTATE_PAN;
4552}
4553
4554static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4555 uint64_t value)
4556{
4557 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4558}
4559
4560static const ARMCPRegInfo pan_reginfo = {
4561 .name = "PAN", .state = ARM_CP_STATE_AA64,
4562 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4563 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4564 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4565};
4566
9eeb7a1c
RH
4567static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4568{
4569 return env->pstate & PSTATE_UAO;
4570}
4571
4572static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4573 uint64_t value)
4574{
4575 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4576}
4577
4578static const ARMCPRegInfo uao_reginfo = {
4579 .name = "UAO", .state = ARM_CP_STATE_AA64,
4580 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4581 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4582 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4583};
4584
dc8b1853
RC
4585static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4586{
4587 return env->pstate & PSTATE_DIT;
4588}
4589
4590static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4591 uint64_t value)
4592{
4593 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4594}
4595
4596static const ARMCPRegInfo dit_reginfo = {
4597 .name = "DIT", .state = ARM_CP_STATE_AA64,
4598 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4599 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4600 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4601};
4602
f2f68a78
RC
4603static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4604{
4605 return env->pstate & PSTATE_SSBS;
4606}
4607
4608static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4609 uint64_t value)
4610{
4611 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4612}
4613
4614static const ARMCPRegInfo ssbs_reginfo = {
4615 .name = "SSBS", .state = ARM_CP_STATE_AA64,
4616 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4617 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4618 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4619};
4620
38262d8a
RH
4621static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4622 const ARMCPRegInfo *ri,
4623 bool isread)
8af35c37 4624{
38262d8a
RH
4625 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4626 switch (arm_current_el(env)) {
4627 case 0:
4628 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4629 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4630 return CP_ACCESS_TRAP;
4631 }
4632 /* fall through */
4633 case 1:
4634 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4635 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4636 return CP_ACCESS_TRAP_EL2;
4637 }
4638 break;
8af35c37
PM
4639 }
4640 return CP_ACCESS_OK;
4641}
4642
2d3ce4c6 4643static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
1bed4d2e 4644{
38262d8a 4645 /* Cache invalidate/clean to Point of Unification... */
1bed4d2e
RH
4646 switch (arm_current_el(env)) {
4647 case 0:
4648 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4649 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4650 return CP_ACCESS_TRAP;
4651 }
4652 /* fall through */
4653 case 1:
2d3ce4c6
PM
4654 /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set. */
4655 if (arm_hcr_el2_eff(env) & hcrflags) {
1bed4d2e
RH
4656 return CP_ACCESS_TRAP_EL2;
4657 }
4658 break;
4659 }
4660 return CP_ACCESS_OK;
4661}
4662
2d3ce4c6
PM
4663static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4664 bool isread)
4665{
4666 return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4667}
4668
4669static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4670 bool isread)
4671{
4672 return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4673}
4674
9b37a28c
FR
4675/*
4676 * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
dbb1fb27
AB
4677 * Page D4-1736 (DDI0487A.b)
4678 */
4679
b7e0730d
RH
4680static int vae1_tlbmask(CPUARMState *env)
4681{
e04a5752 4682 uint64_t hcr = arm_hcr_el2_eff(env);
bc944d3a 4683 uint16_t mask;
e04a5752
RDC
4684
4685 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
bc944d3a
RDC
4686 mask = ARMMMUIdxBit_E20_2 |
4687 ARMMMUIdxBit_E20_2_PAN |
4688 ARMMMUIdxBit_E20_0;
b7e0730d 4689 } else {
bc944d3a 4690 mask = ARMMMUIdxBit_E10_1 |
452ef8cb
RH
4691 ARMMMUIdxBit_E10_1_PAN |
4692 ARMMMUIdxBit_E10_0;
b7e0730d 4693 }
bc944d3a 4694 return mask;
b7e0730d
RH
4695}
4696
ceaa9746
JPB
4697static int vae2_tlbmask(CPUARMState *env)
4698{
4699 uint64_t hcr = arm_hcr_el2_eff(env);
4700 uint16_t mask;
4701
4702 if (hcr & HCR_E2H) {
4703 mask = ARMMMUIdxBit_E20_2 |
4704 ARMMMUIdxBit_E20_2_PAN |
4705 ARMMMUIdxBit_E20_0;
4706 } else {
4707 mask = ARMMMUIdxBit_E2;
4708 }
4709 return mask;
4710}
4711
ea04dce7
RH
4712/* Return 56 if TBI is enabled, 64 otherwise. */
4713static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4714 uint64_t addr)
4715{
c1547bba 4716 uint64_t tcr = regime_tcr(env, mmu_idx);
ea04dce7
RH
4717 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4718 int select = extract64(addr, 55, 1);
4719
4720 return (tbi >> select) & 1 ? 56 : 64;
4721}
4722
4723static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4724{
b6ad6062 4725 uint64_t hcr = arm_hcr_el2_eff(env);
ea04dce7
RH
4726 ARMMMUIdx mmu_idx;
4727
4728 /* Only the regime of the mmu_idx below is significant. */
b6ad6062 4729 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
ea04dce7
RH
4730 mmu_idx = ARMMMUIdx_E20_0;
4731 } else {
4732 mmu_idx = ARMMMUIdx_E10_0;
4733 }
b6ad6062 4734
ea04dce7
RH
4735 return tlbbits_for_regime(env, mmu_idx, addr);
4736}
4737
ceaa9746
JPB
4738static int vae2_tlbbits(CPUARMState *env, uint64_t addr)
4739{
4740 uint64_t hcr = arm_hcr_el2_eff(env);
4741 ARMMMUIdx mmu_idx;
4742
4743 /*
4744 * Only the regime of the mmu_idx below is significant.
4745 * Regime EL2&0 has two ranges with separate TBI configuration, while EL2
4746 * only has one.
4747 */
4748 if (hcr & HCR_E2H) {
4749 mmu_idx = ARMMMUIdx_E20_2;
4750 } else {
4751 mmu_idx = ARMMMUIdx_E2;
4752 }
4753
4754 return tlbbits_for_regime(env, mmu_idx, addr);
4755}
4756
fd3ed969
PM
4757static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4758 uint64_t value)
168aa23b 4759{
29a0af61 4760 CPUState *cs = env_cpu(env);
b7e0730d 4761 int mask = vae1_tlbmask(env);
dbb1fb27 4762
b7e0730d 4763 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
168aa23b
PM
4764}
4765
b4ab8ce9
PM
4766static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4767 uint64_t value)
4768{
29a0af61 4769 CPUState *cs = env_cpu(env);
b7e0730d 4770 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4771
4772 if (tlb_force_broadcast(env)) {
527db2be
RH
4773 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4774 } else {
4775 tlb_flush_by_mmuidx(cs, mask);
b4ab8ce9 4776 }
b4ab8ce9
PM
4777}
4778
85d0dc9f
RH
4779static int e2_tlbmask(CPUARMState *env)
4780{
d902ae75
RH
4781 return (ARMMMUIdxBit_E20_0 |
4782 ARMMMUIdxBit_E20_2 |
4783 ARMMMUIdxBit_E20_2_PAN |
4784 ARMMMUIdxBit_E2);
85d0dc9f
RH
4785}
4786
90c19cdf
RH
4787static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4788 uint64_t value)
4789{
4790 CPUState *cs = env_cpu(env);
4791 int mask = alle1_tlbmask(env);
4792
4793 tlb_flush_by_mmuidx(cs, mask);
4794}
4795
fd3ed969 4796static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
4797 uint64_t value)
4798{
85d0dc9f
RH
4799 CPUState *cs = env_cpu(env);
4800 int mask = e2_tlbmask(env);
fd3ed969 4801
85d0dc9f 4802 tlb_flush_by_mmuidx(cs, mask);
fd3ed969
PM
4803}
4804
43efaa33
PM
4805static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4806 uint64_t value)
4807{
2fc0cc0e 4808 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4809 CPUState *cs = CPU(cpu);
4810
d902ae75 4811 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
43efaa33
PM
4812}
4813
fd3ed969
PM
4814static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4815 uint64_t value)
4816{
29a0af61 4817 CPUState *cs = env_cpu(env);
90c19cdf
RH
4818 int mask = alle1_tlbmask(env);
4819
4820 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
fa439fc5
PM
4821}
4822
2bfb9d75
PM
4823static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4824 uint64_t value)
4825{
29a0af61 4826 CPUState *cs = env_cpu(env);
85d0dc9f 4827 int mask = e2_tlbmask(env);
2bfb9d75 4828
85d0dc9f 4829 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
2bfb9d75
PM
4830}
4831
43efaa33
PM
4832static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4833 uint64_t value)
4834{
29a0af61 4835 CPUState *cs = env_cpu(env);
43efaa33 4836
d902ae75 4837 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
43efaa33
PM
4838}
4839
fd3ed969
PM
4840static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4841 uint64_t value)
fa439fc5 4842{
9b37a28c
FR
4843 /*
4844 * Invalidate by VA, EL2
fd3ed969
PM
4845 * Currently handles both VAE2 and VALE2, since we don't support
4846 * flush-last-level-only.
4847 */
85d0dc9f 4848 CPUState *cs = env_cpu(env);
ceaa9746 4849 int mask = vae2_tlbmask(env);
fd3ed969 4850 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ceaa9746 4851 int bits = vae2_tlbbits(env, pageaddr);
fd3ed969 4852
ceaa9746 4853 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
fd3ed969
PM
4854}
4855
43efaa33
PM
4856static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4857 uint64_t value)
4858{
9b37a28c
FR
4859 /*
4860 * Invalidate by VA, EL3
43efaa33
PM
4861 * Currently handles both VAE3 and VALE3, since we don't support
4862 * flush-last-level-only.
4863 */
2fc0cc0e 4864 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4865 CPUState *cs = CPU(cpu);
4866 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4867
d902ae75 4868 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
43efaa33
PM
4869}
4870
fd3ed969
PM
4871static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4872 uint64_t value)
4873{
90c19cdf
RH
4874 CPUState *cs = env_cpu(env);
4875 int mask = vae1_tlbmask(env);
fa439fc5 4876 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4877 int bits = vae1_tlbbits(env, pageaddr);
fa439fc5 4878
ea04dce7 4879 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
fa439fc5
PM
4880}
4881
b4ab8ce9
PM
4882static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4883 uint64_t value)
4884{
9b37a28c
FR
4885 /*
4886 * Invalidate by VA, EL1&0 (AArch64 version).
b4ab8ce9
PM
4887 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4888 * since we don't support flush-for-specific-ASID-only or
4889 * flush-last-level-only.
4890 */
90c19cdf
RH
4891 CPUState *cs = env_cpu(env);
4892 int mask = vae1_tlbmask(env);
b4ab8ce9 4893 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4894 int bits = vae1_tlbbits(env, pageaddr);
b4ab8ce9
PM
4895
4896 if (tlb_force_broadcast(env)) {
ea04dce7 4897 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
527db2be 4898 } else {
ea04dce7 4899 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
b4ab8ce9 4900 }
b4ab8ce9
PM
4901}
4902
fd3ed969
PM
4903static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4904 uint64_t value)
fa439fc5 4905{
29a0af61 4906 CPUState *cs = env_cpu(env);
ceaa9746 4907 int mask = vae2_tlbmask(env);
fd3ed969 4908 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ceaa9746 4909 int bits = vae2_tlbbits(env, pageaddr);
fa439fc5 4910
ceaa9746 4911 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
fa439fc5
PM
4912}
4913
43efaa33
PM
4914static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4915 uint64_t value)
4916{
29a0af61 4917 CPUState *cs = env_cpu(env);
43efaa33 4918 uint64_t pageaddr = sextract64(value << 12, 0, 56);
d902ae75 4919 int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
43efaa33 4920
ea04dce7 4921 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
d902ae75 4922 ARMMMUIdxBit_E3, bits);
43efaa33
PM
4923}
4924
575a94af
RH
4925static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
4926{
4927 /*
4928 * The MSB of value is the NS field, which only applies if SEL2
4929 * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
4930 */
4931 return (value >= 0
4932 && cpu_isar_feature(aa64_sel2, env_archcpu(env))
4933 && arm_is_secure_below_el3(env)
4934 ? ARMMMUIdxBit_Stage2_S
4935 : ARMMMUIdxBit_Stage2);
4936}
4937
4938static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4939 uint64_t value)
4940{
4941 CPUState *cs = env_cpu(env);
4942 int mask = ipas2e1_tlbmask(env, value);
4943 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4944
4945 if (tlb_force_broadcast(env)) {
4946 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4947 } else {
4948 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4949 }
4950}
4951
4952static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4953 uint64_t value)
4954{
4955 CPUState *cs = env_cpu(env);
4956 int mask = ipas2e1_tlbmask(env, value);
4957 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4958
4959 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4960}
4961
84940ed8 4962#ifdef TARGET_AARCH64
ab1cdb47
RH
4963typedef struct {
4964 uint64_t base;
84940ed8 4965 uint64_t length;
ab1cdb47
RH
4966} TLBIRange;
4967
3c003f70
PM
4968static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
4969{
4970 /*
4971 * Note that the TLBI range TG field encoding differs from both
4972 * TG0 and TG1 encodings.
4973 */
4974 switch (tg) {
4975 case 1:
4976 return Gran4K;
4977 case 2:
4978 return Gran16K;
4979 case 3:
4980 return Gran64K;
4981 default:
4982 return GranInvalid;
4983 }
4984}
4985
ab1cdb47
RH
4986static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4987 uint64_t value)
4988{
4989 unsigned int page_size_granule, page_shift, num, scale, exponent;
3974ff93
RH
4990 /* Extract one bit to represent the va selector in use. */
4991 uint64_t select = sextract64(value, 36, 1);
478dccbb 4992 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true, false);
ab1cdb47 4993 TLBIRange ret = { };
3c003f70 4994 ARMGranuleSize gran;
84940ed8 4995
84940ed8 4996 page_size_granule = extract64(value, 46, 2);
3c003f70 4997 gran = tlbi_range_tg_to_gran_size(page_size_granule);
84940ed8 4998
3974ff93 4999 /* The granule encoded in value must match the granule in use. */
3c003f70 5000 if (gran != param.gran) {
3974ff93 5001 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
84940ed8 5002 page_size_granule);
ab1cdb47 5003 return ret;
84940ed8
RC
5004 }
5005
3c003f70 5006 page_shift = arm_granule_bits(gran);
ab1cdb47
RH
5007 num = extract64(value, 39, 5);
5008 scale = extract64(value, 44, 2);
84940ed8 5009 exponent = (5 * scale) + 1;
84940ed8 5010
ab1cdb47 5011 ret.length = (num + 1) << (exponent + page_shift);
84940ed8 5012
3974ff93 5013 if (param.select) {
d976de21 5014 ret.base = sextract64(value, 0, 37);
84940ed8 5015 } else {
d976de21 5016 ret.base = extract64(value, 0, 37);
84940ed8 5017 }
ef56c242
RH
5018 if (param.ds) {
5019 /*
5020 * With DS=1, BaseADDR is always shifted 16 so that it is able
5021 * to address all 52 va bits. The input address is perforce
5022 * aligned on a 64k boundary regardless of translation granule.
5023 */
5024 page_shift = 16;
5025 }
d976de21 5026 ret.base <<= page_shift;
84940ed8 5027
ab1cdb47 5028 return ret;
84940ed8
RC
5029}
5030
5031static void do_rvae_write(CPUARMState *env, uint64_t value,
5032 int idxmap, bool synced)
5033{
5034 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
ab1cdb47 5035 TLBIRange range;
84940ed8
RC
5036 int bits;
5037
ab1cdb47
RH
5038 range = tlbi_aa64_get_range(env, one_idx, value);
5039 bits = tlbbits_for_regime(env, one_idx, range.base);
84940ed8
RC
5040
5041 if (synced) {
5042 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
ab1cdb47
RH
5043 range.base,
5044 range.length,
84940ed8
RC
5045 idxmap,
5046 bits);
5047 } else {
ab1cdb47
RH
5048 tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
5049 range.length, idxmap, bits);
84940ed8
RC
5050 }
5051}
5052
5053static void tlbi_aa64_rvae1_write(CPUARMState *env,
5054 const ARMCPRegInfo *ri,
5055 uint64_t value)
5056{
5057 /*
5058 * Invalidate by VA range, EL1&0.
5059 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
5060 * since we don't support flush-for-specific-ASID-only or
5061 * flush-last-level-only.
5062 */
5063
5064 do_rvae_write(env, value, vae1_tlbmask(env),
5065 tlb_force_broadcast(env));
5066}
5067
5068static void tlbi_aa64_rvae1is_write(CPUARMState *env,
5069 const ARMCPRegInfo *ri,
5070 uint64_t value)
5071{
5072 /*
5073 * Invalidate by VA range, Inner/Outer Shareable EL1&0.
5074 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
5075 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
5076 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
5077 * shareable specific flushes.
5078 */
5079
5080 do_rvae_write(env, value, vae1_tlbmask(env), true);
5081}
5082
84940ed8
RC
5083static void tlbi_aa64_rvae2_write(CPUARMState *env,
5084 const ARMCPRegInfo *ri,
5085 uint64_t value)
5086{
5087 /*
5088 * Invalidate by VA range, EL2.
5089 * Currently handles all of RVAE2 and RVALE2,
5090 * since we don't support flush-for-specific-ASID-only or
5091 * flush-last-level-only.
5092 */
5093
5094 do_rvae_write(env, value, vae2_tlbmask(env),
5095 tlb_force_broadcast(env));
5096
5097
5098}
5099
5100static void tlbi_aa64_rvae2is_write(CPUARMState *env,
5101 const ARMCPRegInfo *ri,
5102 uint64_t value)
5103{
5104 /*
5105 * Invalidate by VA range, Inner/Outer Shareable, EL2.
5106 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
5107 * since we don't support flush-for-specific-ASID-only,
5108 * flush-last-level-only or inner/outer shareable specific flushes.
5109 */
5110
5111 do_rvae_write(env, value, vae2_tlbmask(env), true);
5112
5113}
5114
5115static void tlbi_aa64_rvae3_write(CPUARMState *env,
5116 const ARMCPRegInfo *ri,
5117 uint64_t value)
5118{
5119 /*
5120 * Invalidate by VA range, EL3.
5121 * Currently handles all of RVAE3 and RVALE3,
5122 * since we don't support flush-for-specific-ASID-only or
5123 * flush-last-level-only.
5124 */
5125
d902ae75 5126 do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
84940ed8
RC
5127}
5128
5129static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5130 const ARMCPRegInfo *ri,
5131 uint64_t value)
5132{
5133 /*
5134 * Invalidate by VA range, EL3, Inner/Outer Shareable.
5135 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5136 * since we don't support flush-for-specific-ASID-only,
5137 * flush-last-level-only or inner/outer specific flushes.
5138 */
5139
d902ae75 5140 do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
84940ed8 5141}
575a94af
RH
5142
5143static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5144 uint64_t value)
5145{
5146 do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5147 tlb_force_broadcast(env));
5148}
5149
5150static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5151 const ARMCPRegInfo *ri,
5152 uint64_t value)
5153{
5154 do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5155}
84940ed8
RC
5156#endif
5157
3f208fd7
PM
5158static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5159 bool isread)
aca3f40b 5160{
4351cb72
RH
5161 int cur_el = arm_current_el(env);
5162
5163 if (cur_el < 2) {
5164 uint64_t hcr = arm_hcr_el2_eff(env);
5165
5166 if (cur_el == 0) {
5167 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5168 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5169 return CP_ACCESS_TRAP_EL2;
5170 }
5171 } else {
5172 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5173 return CP_ACCESS_TRAP;
5174 }
5175 if (hcr & HCR_TDZ) {
5176 return CP_ACCESS_TRAP_EL2;
5177 }
5178 }
5179 } else if (hcr & HCR_TDZ) {
5180 return CP_ACCESS_TRAP_EL2;
5181 }
aca3f40b
PM
5182 }
5183 return CP_ACCESS_OK;
5184}
5185
5186static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5187{
2fc0cc0e 5188 ARMCPU *cpu = env_archcpu(env);
aca3f40b
PM
5189 int dzp_bit = 1 << 4;
5190
5191 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 5192 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
5193 dzp_bit = 0;
5194 }
5195 return cpu->dcz_blocksize | dzp_bit;
5196}
5197
3f208fd7
PM
5198static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5199 bool isread)
f502cfc2 5200{
cdcf1405 5201 if (!(env->pstate & PSTATE_SP)) {
9b37a28c
FR
5202 /*
5203 * Access to SP_EL0 is undefined if it's being used as
f502cfc2
PM
5204 * the stack pointer.
5205 */
5206 return CP_ACCESS_TRAP_UNCATEGORIZED;
5207 }
5208 return CP_ACCESS_OK;
5209}
5210
5211static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5212{
5213 return env->pstate & PSTATE_SP;
5214}
5215
5216static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5217{
5218 update_spsel(env, val);
5219}
5220
137feaa9
FA
5221static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5222 uint64_t value)
5223{
2fc0cc0e 5224 ARMCPU *cpu = env_archcpu(env);
137feaa9 5225
f00faf13
RH
5226 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5227 /* M bit is RAZ/WI for PMSA with no MPU implemented */
5228 value &= ~SCTLR_M;
5229 }
5230
5231 /* ??? Lots of these bits are not implemented. */
5232
5233 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5234 if (ri->opc1 == 6) { /* SCTLR_EL3 */
5235 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5236 } else {
5237 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5238 SCTLR_ATA0 | SCTLR_ATA);
5239 }
5240 }
5241
137feaa9 5242 if (raw_read(env, ri) == value) {
9b37a28c
FR
5243 /*
5244 * Skip the TLB flush if nothing actually changed; Linux likes
137feaa9
FA
5245 * to do a lot of pointless SCTLR writes.
5246 */
5247 return;
5248 }
5249
5250 raw_write(env, ri, value);
f00faf13 5251
137feaa9 5252 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 5253 tlb_flush(CPU(cpu));
2e5dcf36 5254
2b77ad4d 5255 if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
2e5dcf36
RH
5256 /*
5257 * Normally we would always end the TB on an SCTLR write; see the
5258 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5259 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5260 * of hflags from the translator, so do it here.
5261 */
5262 arm_rebuild_hflags(env);
5263 }
137feaa9
FA
5264}
5265
80d2b43b
PM
5266static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5267 uint64_t value)
a8d64e73 5268{
01765386
PM
5269 /*
5270 * Some MDCR_EL3 bits affect whether PMU counters are running:
5271 * if we are trying to change any of those then we must
5272 * bracket this update with PMU start/finish calls.
5273 */
5274 bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5275
5276 if (pmu_op) {
5277 pmu_op_start(env);
5278 }
80d2b43b 5279 env->cp15.mdcr_el3 = value;
01765386
PM
5280 if (pmu_op) {
5281 pmu_op_finish(env);
5282 }
5283}
5284
80d2b43b
PM
5285static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5286 uint64_t value)
5287{
5288 /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5289 mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5290}
5291
01765386
PM
5292static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5293 uint64_t value)
5294{
5295 /*
5296 * Some MDCR_EL2 bits affect whether PMU counters are running:
5297 * if we are trying to change any of those then we must
5298 * bracket this update with PMU start/finish calls.
5299 */
5300 bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5301
5302 if (pmu_op) {
5303 pmu_op_start(env);
5304 }
5305 env->cp15.mdcr_el2 = value;
5306 if (pmu_op) {
5307 pmu_op_finish(env);
5308 }
a8d64e73
PM
5309}
5310
9719f125
JH
5311#ifdef CONFIG_USER_ONLY
5312/*
5313 * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
5314 * code to get around W^X restrictions, where one region is writable and the
5315 * other is executable.
5316 *
5317 * Since the executable region is never written to we cannot detect code
5318 * changes when running in user mode, and rely on the emulated JIT telling us
5319 * that the code has changed by executing this instruction.
5320 */
5321static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
5322 uint64_t value)
5323{
5324 uint64_t icache_line_mask, start_address, end_address;
5325 const ARMCPU *cpu;
5326
5327 cpu = env_archcpu(env);
5328
5329 icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
5330 start_address = value & ~icache_line_mask;
5331 end_address = value | icache_line_mask;
5332
5333 mmap_lock();
5334
5335 tb_invalidate_phys_range(start_address, end_address);
5336
5337 mmap_unlock();
5338}
5339#endif
5340
b0d2b7d0 5341static const ARMCPRegInfo v8_cp_reginfo[] = {
9b37a28c
FR
5342 /*
5343 * Minimal set of EL0-visible registers. This will need to be expanded
b0d2b7d0
PM
5344 * significantly for system emulation of AArch64 CPUs.
5345 */
5346 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5347 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5348 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
5349 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5350 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 5351 .type = ARM_CP_NO_RAW,
c2b820fe
PM
5352 .access = PL0_RW, .accessfn = aa64_daif_access,
5353 .fieldoffset = offsetof(CPUARMState, daif),
5354 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
5355 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5356 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 5357 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 5358 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
5359 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5360 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 5361 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 5362 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
5363 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5364 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 5365 .access = PL0_R, .type = ARM_CP_NO_RAW,
b19ed03c 5366 .fgt = FGT_DCZID_EL0,
aca3f40b
PM
5367 .readfn = aa64_dczid_read },
5368 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5369 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5370 .access = PL0_W, .type = ARM_CP_DC_ZVA,
5371#ifndef CONFIG_USER_ONLY
5372 /* Avoid overhead of an access check that always passes in user-mode */
5373 .accessfn = aa64_zva_access,
dd345653 5374 .fgt = FGT_DCZVA,
aca3f40b
PM
5375#endif
5376 },
0eef9d98
PM
5377 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5378 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5379 .access = PL1_R, .type = ARM_CP_CURRENTEL },
9719f125
JH
5380 /*
5381 * Instruction cache ops. All of these except `IC IVAU` NOP because we
5382 * don't emulate caches.
5383 */
8af35c37
PM
5384 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5385 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a 5386 .access = PL1_W, .type = ARM_CP_NOP,
dd345653 5387 .fgt = FGT_ICIALLUIS,
2d3ce4c6 5388 .accessfn = access_ticab },
8af35c37
PM
5389 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5390 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a 5391 .access = PL1_W, .type = ARM_CP_NOP,
dd345653 5392 .fgt = FGT_ICIALLU,
2d3ce4c6 5393 .accessfn = access_tocu },
8af35c37
PM
5394 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5395 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
9719f125 5396 .access = PL0_W,
dd345653 5397 .fgt = FGT_ICIVAU,
9719f125
JH
5398 .accessfn = access_tocu,
5399#ifdef CONFIG_USER_ONLY
5400 .type = ARM_CP_NO_RAW,
5401 .writefn = ic_ivau_write
5402#else
5403 .type = ARM_CP_NOP
5404#endif
5405 },
5406 /* Cache ops: all NOPs since we don't emulate caches */
8af35c37
PM
5407 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5408 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e 5409 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
dd345653 5410 .fgt = FGT_DCIVAC,
1bed4d2e 5411 .type = ARM_CP_NOP },
8af35c37
PM
5412 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5413 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
dd345653 5414 .fgt = FGT_DCISW,
1803d271 5415 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
5416 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5417 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5418 .access = PL0_W, .type = ARM_CP_NOP,
950037e2 5419 .fgt = FGT_DCCVAC,
1bed4d2e 5420 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
5421 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5422 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
dd345653 5423 .fgt = FGT_DCCSW,
1803d271 5424 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
5425 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5426 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5427 .access = PL0_W, .type = ARM_CP_NOP,
dd345653 5428 .fgt = FGT_DCCVAU,
2d3ce4c6 5429 .accessfn = access_tocu },
8af35c37
PM
5430 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5431 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5432 .access = PL0_W, .type = ARM_CP_NOP,
dd345653 5433 .fgt = FGT_DCCIVAC,
1bed4d2e 5434 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
5435 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5436 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
dd345653 5437 .fgt = FGT_DCCISW,
1803d271 5438 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
168aa23b
PM
5439 /* TLBI operations */
5440 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5441 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
0f66d223 5442 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 5443 .fgt = FGT_TLBIVMALLE1IS,
fd3ed969 5444 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 5445 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5446 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
0f66d223 5447 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 5448 .fgt = FGT_TLBIVAE1IS,
fd3ed969 5449 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5450 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5451 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
0f66d223 5452 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 5453 .fgt = FGT_TLBIASIDE1IS,
fd3ed969 5454 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 5455 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5456 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
0f66d223 5457 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 5458 .fgt = FGT_TLBIVAAE1IS,
fd3ed969 5459 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5460 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5461 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
0f66d223 5462 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 5463 .fgt = FGT_TLBIVALE1IS,
fd3ed969 5464 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5465 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5466 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
0f66d223 5467 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 5468 .fgt = FGT_TLBIVAALE1IS,
fd3ed969 5469 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5470 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5471 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73 5472 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 5473 .fgt = FGT_TLBIVMALLE1,
fd3ed969 5474 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 5475 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5476 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73 5477 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 5478 .fgt = FGT_TLBIVAE1,
fd3ed969 5479 .writefn = tlbi_aa64_vae1_write },
168aa23b 5480 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5481 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73 5482 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 5483 .fgt = FGT_TLBIASIDE1,
fd3ed969 5484 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 5485 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5486 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73 5487 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 5488 .fgt = FGT_TLBIVAAE1,
fd3ed969 5489 .writefn = tlbi_aa64_vae1_write },
168aa23b 5490 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5491 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73 5492 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 5493 .fgt = FGT_TLBIVALE1,
fd3ed969 5494 .writefn = tlbi_aa64_vae1_write },
168aa23b 5495 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5496 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73 5497 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 5498 .fgt = FGT_TLBIVAALE1,
fd3ed969 5499 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
5500 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5501 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
575a94af
RH
5502 .access = PL2_W, .type = ARM_CP_NO_RAW,
5503 .writefn = tlbi_aa64_ipas2e1is_write },
cea66e91
PM
5504 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5505 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
575a94af
RH
5506 .access = PL2_W, .type = ARM_CP_NO_RAW,
5507 .writefn = tlbi_aa64_ipas2e1is_write },
83ddf975
PM
5508 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5509 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5510 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 5511 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
5512 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5513 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5514 .access = PL2_W, .type = ARM_CP_NO_RAW,
5515 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
5516 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5517 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
575a94af
RH
5518 .access = PL2_W, .type = ARM_CP_NO_RAW,
5519 .writefn = tlbi_aa64_ipas2e1_write },
cea66e91
PM
5520 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5521 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
575a94af
RH
5522 .access = PL2_W, .type = ARM_CP_NO_RAW,
5523 .writefn = tlbi_aa64_ipas2e1_write },
83ddf975
PM
5524 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5525 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5526 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 5527 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
5528 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5529 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5530 .access = PL2_W, .type = ARM_CP_NO_RAW,
5531 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
5532#ifndef CONFIG_USER_ONLY
5533 /* 64 bit address translation operations */
5534 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5535 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa 5536 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
132c98cd 5537 .fgt = FGT_ATS1E1R,
1acd00ef 5538 .accessfn = at_e012_access, .writefn = ats_write64 },
19525524
PM
5539 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5540 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa 5541 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
132c98cd 5542 .fgt = FGT_ATS1E1W,
1acd00ef 5543 .accessfn = at_e012_access, .writefn = ats_write64 },
19525524
PM
5544 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5545 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
0710b2fa 5546 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
132c98cd 5547 .fgt = FGT_ATS1E0R,
1acd00ef 5548 .accessfn = at_e012_access, .writefn = ats_write64 },
19525524
PM
5549 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5550 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
0710b2fa 5551 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
132c98cd 5552 .fgt = FGT_ATS1E0W,
1acd00ef 5553 .accessfn = at_e012_access, .writefn = ats_write64 },
2a47df95 5554 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 5555 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
0710b2fa 5556 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
1acd00ef 5557 .accessfn = at_e012_access, .writefn = ats_write64 },
2a47df95 5558 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 5559 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
0710b2fa 5560 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
1acd00ef 5561 .accessfn = at_e012_access, .writefn = ats_write64 },
2a47df95 5562 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 5563 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
0710b2fa 5564 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
1acd00ef 5565 .accessfn = at_e012_access, .writefn = ats_write64 },
2a47df95 5566 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 5567 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
0710b2fa 5568 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
1acd00ef 5569 .accessfn = at_e012_access, .writefn = ats_write64 },
2a47df95
PM
5570 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5571 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5572 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
5573 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5574 .writefn = ats_write64 },
2a47df95
PM
5575 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5576 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
5577 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5578 .writefn = ats_write64 },
c96fc9b5
EI
5579 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5580 .type = ARM_CP_ALIAS,
5581 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5582 .access = PL1_RW, .resetvalue = 0,
67dd8030 5583 .fgt = FGT_PAR_EL1,
c96fc9b5
EI
5584 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5585 .writefn = par_write },
19525524 5586#endif
995939a6 5587 /* TLB invalidate last level of translation table walk */
9449fdf6 5588 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
0f66d223 5589 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
30881b73 5590 .writefn = tlbimva_is_write },
9449fdf6 5591 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
0f66d223 5592 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
fa439fc5 5593 .writefn = tlbimvaa_is_write },
9449fdf6 5594 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73
RH
5595 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5596 .writefn = tlbimva_write },
9449fdf6 5597 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73
RH
5598 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5599 .writefn = tlbimvaa_write },
541ef8c2
SS
5600 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5601 .type = ARM_CP_NO_RAW, .access = PL2_W,
5602 .writefn = tlbimva_hyp_write },
5603 { .name = "TLBIMVALHIS",
5604 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5605 .type = ARM_CP_NO_RAW, .access = PL2_W,
5606 .writefn = tlbimva_hyp_is_write },
5607 { .name = "TLBIIPAS2",
5608 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
575a94af
RH
5609 .type = ARM_CP_NO_RAW, .access = PL2_W,
5610 .writefn = tlbiipas2_hyp_write },
541ef8c2
SS
5611 { .name = "TLBIIPAS2IS",
5612 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
575a94af
RH
5613 .type = ARM_CP_NO_RAW, .access = PL2_W,
5614 .writefn = tlbiipas2is_hyp_write },
541ef8c2
SS
5615 { .name = "TLBIIPAS2L",
5616 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
575a94af
RH
5617 .type = ARM_CP_NO_RAW, .access = PL2_W,
5618 .writefn = tlbiipas2_hyp_write },
541ef8c2
SS
5619 { .name = "TLBIIPAS2LIS",
5620 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
575a94af
RH
5621 .type = ARM_CP_NO_RAW, .access = PL2_W,
5622 .writefn = tlbiipas2is_hyp_write },
9449fdf6
PM
5623 /* 32 bit cache operations */
5624 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
2d3ce4c6 5625 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
9449fdf6
PM
5626 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5627 .type = ARM_CP_NOP, .access = PL1_W },
5628 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
2d3ce4c6 5629 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
9449fdf6 5630 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
2d3ce4c6 5631 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
9449fdf6
PM
5632 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5633 .type = ARM_CP_NOP, .access = PL1_W },
5634 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5635 .type = ARM_CP_NOP, .access = PL1_W },
5636 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e 5637 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5638 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 5639 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5640 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
1bed4d2e 5641 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5642 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 5643 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5644 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
2d3ce4c6 5645 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
9449fdf6 5646 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
1bed4d2e 5647 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5648 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 5649 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5650 /* MMU Domain access control / MPU write buffer control */
0c17d68c 5651 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
84929218 5652 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
5653 .writefn = dacr_write, .raw_writefn = raw_write,
5654 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5655 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 5656 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5657 .type = ARM_CP_ALIAS,
a0618a19 5658 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
5659 .access = PL1_RW,
5660 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 5661 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5662 .type = ARM_CP_ALIAS,
a65f1de9 5663 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5664 .access = PL1_RW,
5665 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
9b37a28c
FR
5666 /*
5667 * We rely on the access checks not allowing the guest to write to the
f502cfc2
PM
5668 * state field when SPSel indicates that it's being used as the stack
5669 * pointer.
5670 */
5671 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5672 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5673 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 5674 .type = ARM_CP_ALIAS,
f502cfc2 5675 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
5676 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5677 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
beeec926 5678 .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
884b4dee 5679 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
5680 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5681 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 5682 .type = ARM_CP_NO_RAW,
f502cfc2 5683 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
5684 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5685 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
696ba377
RH
5686 .access = PL2_RW,
5687 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
a4c88675 5688 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
6a43e0b6
PM
5689 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5690 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
696ba377 5691 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6a43e0b6
PM
5692 .writefn = dacr_write, .raw_writefn = raw_write,
5693 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5694 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5695 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
696ba377 5696 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6a43e0b6
PM
5697 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5698 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5699 .type = ARM_CP_ALIAS,
5700 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5701 .access = PL2_RW,
5702 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5703 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5704 .type = ARM_CP_ALIAS,
5705 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5706 .access = PL2_RW,
5707 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5708 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5709 .type = ARM_CP_ALIAS,
5710 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5711 .access = PL2_RW,
5712 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5713 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5714 .type = ARM_CP_ALIAS,
5715 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5716 .access = PL2_RW,
5717 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73 5718 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
80d2b43b 5719 .type = ARM_CP_IO,
a8d64e73
PM
5720 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5721 .resetvalue = 0,
80d2b43b
PM
5722 .access = PL3_RW,
5723 .writefn = mdcr_el3_write,
5724 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
7f4fbfb5 5725 { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
a8d64e73
PM
5726 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5727 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5728 .writefn = sdcr_write,
5729 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
5730};
5731
d1fb4da2 5732static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
f149e3e8 5733{
2fc0cc0e 5734 ARMCPU *cpu = env_archcpu(env);
d1fb4da2
RH
5735
5736 if (arm_feature(env, ARM_FEATURE_V8)) {
5737 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5738 } else {
5739 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5740 }
f149e3e8
EI
5741
5742 if (arm_feature(env, ARM_FEATURE_EL3)) {
5743 valid_mask &= ~HCR_HCD;
77077a83 5744 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
9b37a28c
FR
5745 /*
5746 * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
77077a83
JK
5747 * However, if we're using the SMC PSCI conduit then QEMU is
5748 * effectively acting like EL3 firmware and so the guest at
5749 * EL2 should retain the ability to prevent EL1 from being
5750 * able to make SMC calls into the ersatz firmware, so in
5751 * that case HCR.TSC should be read/write.
5752 */
f149e3e8
EI
5753 valid_mask &= ~HCR_TSC;
5754 }
d1fb4da2
RH
5755
5756 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5757 if (cpu_isar_feature(aa64_vh, cpu)) {
5758 valid_mask |= HCR_E2H;
5759 }
da3d8b13
RH
5760 if (cpu_isar_feature(aa64_ras, cpu)) {
5761 valid_mask |= HCR_TERR | HCR_TEA;
5762 }
d1fb4da2
RH
5763 if (cpu_isar_feature(aa64_lor, cpu)) {
5764 valid_mask |= HCR_TLOR;
5765 }
5766 if (cpu_isar_feature(aa64_pauth, cpu)) {
5767 valid_mask |= HCR_API | HCR_APK;
5768 }
8ddb300b
RH
5769 if (cpu_isar_feature(aa64_mte, cpu)) {
5770 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5771 }
7cb1e618
RH
5772 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5773 valid_mask |= HCR_ENSCXT;
5774 }
8c7e17ef
PM
5775 if (cpu_isar_feature(aa64_fwb, cpu)) {
5776 valid_mask |= HCR_FWB;
5777 }
aa3cc42c
RH
5778 if (cpu_isar_feature(aa64_rme, cpu)) {
5779 valid_mask |= HCR_GPF;
5780 }
ef682cdb 5781 }
f149e3e8 5782
d2fd9313
PM
5783 if (cpu_isar_feature(any_evt, cpu)) {
5784 valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5785 } else if (cpu_isar_feature(any_half_evt, cpu)) {
5786 valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5787 }
5788
f149e3e8
EI
5789 /* Clear RES0 bits. */
5790 value &= valid_mask;
5791
8ddb300b
RH
5792 /*
5793 * These bits change the MMU setup:
f149e3e8
EI
5794 * HCR_VM enables stage 2 translation
5795 * HCR_PTW forbids certain page-table setups
8ddb300b
RH
5796 * HCR_DC disables stage1 and enables stage2 translation
5797 * HCR_DCT enables tagging on (disabled) stage1 translation
8c7e17ef 5798 * HCR_FWB changes the interpretation of stage2 descriptor bits
f149e3e8 5799 */
8c7e17ef
PM
5800 if ((env->cp15.hcr_el2 ^ value) &
5801 (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
d10eb08f 5802 tlb_flush(CPU(cpu));
f149e3e8 5803 }
ce4afed8 5804 env->cp15.hcr_el2 = value;
89430fc6
PM
5805
5806 /*
5807 * Updates to VI and VF require us to update the status of
5808 * virtual interrupts, which are the logical OR of these bits
5809 * and the state of the input lines from the GIC. (This requires
5810 * that we have the iothread lock, which is done by marking the
5811 * reginfo structs as ARM_CP_IO.)
5812 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5813 * possible for it to be taken immediately, because VIRQ and
5814 * VFIQ are masked unless running at EL0 or EL1, and HCR
5815 * can only be written at EL2.
5816 */
5817 g_assert(qemu_mutex_iothread_locked());
5818 arm_cpu_update_virq(cpu);
5819 arm_cpu_update_vfiq(cpu);
3c29632f 5820 arm_cpu_update_vserr(cpu);
ce4afed8
PM
5821}
5822
d1fb4da2
RH
5823static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5824{
5825 do_hcr_write(env, value, 0);
5826}
5827
ce4afed8
PM
5828static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5829 uint64_t value)
5830{
5831 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5832 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
d1fb4da2 5833 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
ce4afed8
PM
5834}
5835
5836static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5837 uint64_t value)
5838{
5839 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5840 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
d1fb4da2 5841 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
f149e3e8
EI
5842}
5843
f7778444 5844/*
b74c0443 5845 * Return the effective value of HCR_EL2, at the given security state.
f7778444
RH
5846 * Bits that are not included here:
5847 * RW (read from SCR_EL3.RW as needed)
5848 */
2d12bb96 5849uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
f7778444
RH
5850{
5851 uint64_t ret = env->cp15.hcr_el2;
5852
2d12bb96
PM
5853 assert(space != ARMSS_Root);
5854
4477020d 5855 if (!arm_is_el2_enabled_secstate(env, space)) {
f7778444
RH
5856 /*
5857 * "This register has no effect if EL2 is not enabled in the
5858 * current Security state". This is ARMv8.4-SecEL2 speak for
5859 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5860 *
5861 * Prior to that, the language was "In an implementation that
5862 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5863 * as if this field is 0 for all purposes other than a direct
5864 * read or write access of HCR_EL2". With lots of enumeration
5865 * on a per-field basis. In current QEMU, this is condition
5866 * is arm_is_secure_below_el3.
5867 *
5868 * Since the v8.4 language applies to the entire register, and
5869 * appears to be backward compatible, use that.
5870 */
4990e1d3
RH
5871 return 0;
5872 }
5873
5874 /*
5875 * For a cpu that supports both aarch64 and aarch32, we can set bits
5876 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5877 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5878 */
5879 if (!arm_el_is_aa64(env, 2)) {
5880 uint64_t aa32_valid;
5881
5882 /*
5883 * These bits are up-to-date as of ARMv8.6.
5884 * For HCR, it's easiest to list just the 2 bits that are invalid.
5885 * For HCR2, list those that are valid.
5886 */
5887 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5888 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5889 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5890 ret &= aa32_valid;
5891 }
5892
5893 if (ret & HCR_TGE) {
5894 /* These bits are up-to-date as of ARMv8.6. */
f7778444
RH
5895 if (ret & HCR_E2H) {
5896 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5897 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5898 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4990e1d3
RH
5899 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5900 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5901 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
f7778444
RH
5902 } else {
5903 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5904 }
5905 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5906 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5907 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5908 HCR_TLOR);
5909 }
5910
5911 return ret;
5912}
5913
b74c0443
RH
5914uint64_t arm_hcr_el2_eff(CPUARMState *env)
5915{
a0262ba6
RH
5916 if (arm_feature(env, ARM_FEATURE_M)) {
5917 return 0;
5918 }
2d12bb96 5919 return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
b74c0443
RH
5920}
5921
19668718
RH
5922/*
5923 * Corresponds to ARM pseudocode function ELIsInHost().
5924 */
5925bool el_is_in_host(CPUARMState *env, int el)
5926{
5927 uint64_t mask;
5928
5929 /*
5930 * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5931 * Perform the simplest bit tests first, and validate EL2 afterward.
5932 */
5933 if (el & 1) {
5934 return false; /* EL1 or EL3 */
5935 }
5936
5937 /*
5938 * Note that hcr_write() checks isar_feature_aa64_vh(),
5939 * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5940 */
5941 mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5942 if ((env->cp15.hcr_el2 & mask) != mask) {
5943 return false;
5944 }
5945
5946 /* TGE and/or E2H set: double check those bits are currently legal. */
5947 return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5948}
5949
5814d587
RH
5950static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5951 uint64_t value)
5952{
5953 uint64_t valid_mask = 0;
5954
dbc678f9
PM
5955 /* FEAT_MOPS adds MSCEn and MCE2 */
5956 if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
5957 valid_mask |= HCRX_MSCEN | HCRX_MCE2;
5958 }
5814d587
RH
5959
5960 /* Clear RES0 bits. */
5961 env->cp15.hcrx_el2 = value & valid_mask;
5962}
5963
5964static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5965 bool isread)
5966{
5967 if (arm_current_el(env) < 3
5968 && arm_feature(env, ARM_FEATURE_EL3)
5969 && !(env->cp15.scr_el3 & SCR_HXEN)) {
5970 return CP_ACCESS_TRAP_EL3;
5971 }
5972 return CP_ACCESS_OK;
5973}
5974
5975static const ARMCPRegInfo hcrx_el2_reginfo = {
5976 .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5977 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5978 .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5979 .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5980};
5981
5982/* Return the effective value of HCRX_EL2. */
5983uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5984{
5985 /*
5986 * The bits in this register behave as 0 for all purposes other than
dbc678f9
PM
5987 * direct reads of the register if SCR_EL3.HXEn is 0.
5988 * If EL2 is not enabled in the current security state, then the
5989 * bit may behave as if 0, or as if 1, depending on the bit.
5990 * For the moment, we treat the EL2-disabled case as taking
5991 * priority over the HXEn-disabled case. This is true for the only
5992 * bit for a feature which we implement where the answer is different
5993 * for the two cases (MSCEn for FEAT_MOPS).
5994 * This may need to be revisited for future bits.
5814d587 5995 */
dbc678f9
PM
5996 if (!arm_is_el2_enabled(env)) {
5997 uint64_t hcrx = 0;
5998 if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
5999 /* MSCEn behaves as 1 if EL2 is not enabled */
6000 hcrx |= HCRX_MSCEN;
6001 }
6002 return hcrx;
6003 }
6004 if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
5814d587
RH
6005 return 0;
6006 }
6007 return env->cp15.hcrx_el2;
6008}
6009
fc1120a7
PM
6010static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
6011 uint64_t value)
6012{
6013 /*
6014 * For A-profile AArch32 EL3, if NSACR.CP10
6015 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6016 */
6017 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6018 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39
RH
6019 uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6020 value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
fc1120a7
PM
6021 }
6022 env->cp15.cptr_el[2] = value;
6023}
6024
6025static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
6026{
6027 /*
6028 * For A-profile AArch32 EL3, if NSACR.CP10
6029 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6030 */
6031 uint64_t value = env->cp15.cptr_el[2];
6032
6033 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6034 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39 6035 value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
fc1120a7
PM
6036 }
6037 return value;
6038}
6039
4771cd01 6040static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8 6041 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
89430fc6 6042 .type = ARM_CP_IO,
f149e3e8
EI
6043 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6044 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
587f8b33 6045 .writefn = hcr_write, .raw_writefn = raw_write },
ce4afed8 6046 { .name = "HCR", .state = ARM_CP_STATE_AA32,
89430fc6 6047 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
6048 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6049 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 6050 .writefn = hcr_writelow },
831a2fca
PM
6051 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
6052 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
6053 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3b685ba7 6054 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 6055 .type = ARM_CP_ALIAS,
3b685ba7
EI
6056 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
6057 .access = PL2_RW,
6058 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
68e78e33 6059 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
f2c30f42
EI
6060 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
6061 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
cba517c3 6062 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
63b60551
EI
6063 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
6064 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
cba517c3
PM
6065 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
6066 .type = ARM_CP_ALIAS,
6067 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
6068 .access = PL2_RW,
6069 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
3b685ba7 6070 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 6071 .type = ARM_CP_ALIAS,
3b685ba7 6072 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
6073 .access = PL2_RW,
6074 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d79e0c06 6075 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
6076 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
6077 .access = PL2_RW, .writefn = vbar_write,
6078 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
6079 .resetvalue = 0 },
884b4dee
GB
6080 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
6081 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 6082 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 6083 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
6084 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
6085 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
6086 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
fc1120a7
PM
6087 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
6088 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
95f949ac
EI
6089 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
6090 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
6091 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
6092 .resetvalue = 0 },
6093 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 6094 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac
EI
6095 .access = PL2_RW, .type = ARM_CP_ALIAS,
6096 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
6097 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
6098 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
6099 .access = PL2_RW, .type = ARM_CP_CONST,
6100 .resetvalue = 0 },
6101 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
55b53c71 6102 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 6103 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
6104 .access = PL2_RW, .type = ARM_CP_CONST,
6105 .resetvalue = 0 },
37cd6c24
PM
6106 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
6107 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
6108 .access = PL2_RW, .type = ARM_CP_CONST,
6109 .resetvalue = 0 },
6110 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
6111 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
6112 .access = PL2_RW, .type = ARM_CP_CONST,
6113 .resetvalue = 0 },
06ec4c8c
EI
6114 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
6115 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
d06dc933 6116 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
587f8b33 6117 .raw_writefn = raw_write,
06ec4c8c 6118 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
6119 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
6120 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 6121 .type = ARM_CP_ALIAS,
68e9c2fe 6122 .access = PL2_RW, .accessfn = access_el3_aa32ns,
afbb181c 6123 .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
68e9c2fe
EI
6124 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
6125 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 6126 .access = PL2_RW,
988cc190 6127 /* no .writefn needed as this can't cause an ASID change */
68e9c2fe 6128 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
6129 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
6130 .cp = 15, .opc1 = 6, .crm = 2,
6131 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6132 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6133 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
587f8b33 6134 .writefn = vttbr_write, .raw_writefn = raw_write },
b698e9cf
EI
6135 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
6136 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
587f8b33 6137 .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
b698e9cf 6138 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
6139 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
6140 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
6141 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
6142 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
6143 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6144 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
6145 .access = PL2_RW, .resetvalue = 0,
6146 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
6147 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
6148 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
587f8b33
EA
6149 .access = PL2_RW, .resetvalue = 0,
6150 .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
a57633c0
EI
6151 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6152 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
6153 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 6154 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
6155 { .name = "TLBIALLNSNH",
6156 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
6157 .type = ARM_CP_NO_RAW, .access = PL2_W,
6158 .writefn = tlbiall_nsnh_write },
6159 { .name = "TLBIALLNSNHIS",
6160 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
6161 .type = ARM_CP_NO_RAW, .access = PL2_W,
6162 .writefn = tlbiall_nsnh_is_write },
6163 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6164 .type = ARM_CP_NO_RAW, .access = PL2_W,
6165 .writefn = tlbiall_hyp_write },
6166 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6167 .type = ARM_CP_NO_RAW, .access = PL2_W,
6168 .writefn = tlbiall_hyp_is_write },
6169 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6170 .type = ARM_CP_NO_RAW, .access = PL2_W,
6171 .writefn = tlbimva_hyp_write },
6172 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6173 .type = ARM_CP_NO_RAW, .access = PL2_W,
6174 .writefn = tlbimva_hyp_is_write },
51da9014
EI
6175 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
6176 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
696ba377 6177 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 6178 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
6179 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
6180 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
696ba377 6181 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 6182 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
6183 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
6184 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
696ba377 6185 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75
PM
6186 .writefn = tlbi_aa64_vae2_write },
6187 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
6188 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
696ba377 6189 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75 6190 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
6191 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
6192 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
696ba377 6193 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 6194 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
6195 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
6196 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
696ba377 6197 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75 6198 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 6199#ifndef CONFIG_USER_ONLY
9b37a28c
FR
6200 /*
6201 * Unlike the other EL2-related AT operations, these must
2a47df95
PM
6202 * UNDEF from EL3 if EL2 is not implemented, which is why we
6203 * define them here rather than with the rest of the AT ops.
6204 */
6205 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
6206 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6207 .access = PL2_W, .accessfn = at_s1e2_access,
696ba377
RH
6208 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6209 .writefn = ats_write64 },
2a47df95
PM
6210 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
6211 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6212 .access = PL2_W, .accessfn = at_s1e2_access,
696ba377
RH
6213 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6214 .writefn = ats_write64 },
9b37a28c
FR
6215 /*
6216 * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
14db7fe0
PM
6217 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
6218 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
6219 * to behave as if SCR.NS was 1.
6220 */
6221 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6222 .access = PL2_W,
0710b2fa 6223 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
14db7fe0
PM
6224 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6225 .access = PL2_W,
0710b2fa 6226 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
0b6440af
EI
6227 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6228 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
9b37a28c
FR
6229 /*
6230 * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
0b6440af
EI
6231 * reset values as IMPDEF. We choose to reset to 3 to comply with
6232 * both ARMv7 and ARMv8.
6233 */
f6fc36de
JPB
6234 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
6235 .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
0b6440af 6236 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
6237 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6238 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6239 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6240 .writefn = gt_cntvoff_write,
6241 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6242 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6243 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6244 .writefn = gt_cntvoff_write,
6245 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
6246 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6247 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6248 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6249 .type = ARM_CP_IO, .access = PL2_RW,
6250 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6251 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6252 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6253 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6254 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6255 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6256 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 6257 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
6258 .resetfn = gt_hyp_timer_reset,
6259 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6260 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6261 .type = ARM_CP_IO,
6262 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6263 .access = PL2_RW,
6264 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6265 .resetvalue = 0,
6266 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 6267#endif
59e05530
EI
6268 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6269 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6270 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6271 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6272 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6273 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6274 .access = PL2_RW,
6275 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
6276 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6277 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6278 .access = PL2_RW,
6279 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
6280};
6281
ce4afed8
PM
6282static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6283 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
89430fc6 6284 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
6285 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6286 .access = PL2_RW,
6287 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6288 .writefn = hcr_writehigh },
ce4afed8
PM
6289};
6290
e9152ee9
RDC
6291static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6292 bool isread)
6293{
6294 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6295 return CP_ACCESS_OK;
6296 }
6297 return CP_ACCESS_TRAP_UNCATEGORIZED;
6298}
6299
6300static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6301 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6302 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6303 .access = PL2_RW, .accessfn = sel2_access,
6304 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6305 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6306 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6307 .access = PL2_RW, .accessfn = sel2_access,
6308 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
e9152ee9
RDC
6309};
6310
2f027fc5
PM
6311static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6312 bool isread)
6313{
9b37a28c
FR
6314 /*
6315 * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
926c1b97 6316 * At Secure EL1 it traps to EL3 or EL2.
2f027fc5
PM
6317 */
6318 if (arm_current_el(env) == 3) {
6319 return CP_ACCESS_OK;
6320 }
6321 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
6322 if (env->cp15.scr_el3 & SCR_EEL2) {
6323 return CP_ACCESS_TRAP_EL2;
6324 }
2f027fc5
PM
6325 return CP_ACCESS_TRAP_EL3;
6326 }
6327 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6328 if (isread) {
6329 return CP_ACCESS_OK;
6330 }
6331 return CP_ACCESS_TRAP_UNCATEGORIZED;
6332}
6333
60fb1a87
GB
6334static const ARMCPRegInfo el3_cp_reginfo[] = {
6335 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6336 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6337 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
587f8b33 6338 .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
f80741d1 6339 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
60fb1a87 6340 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
6341 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6342 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
587f8b33 6343 .writefn = scr_write, .raw_writefn = raw_write },
60fb1a87
GB
6344 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6345 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6346 .access = PL3_RW, .resetvalue = 0,
6347 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6348 { .name = "SDER",
6349 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6350 .access = PL3_RW, .resetvalue = 0,
6351 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 6352 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
6353 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6354 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 6355 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
6356 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6357 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
f478847f 6358 .access = PL3_RW, .resetvalue = 0,
7dd8c9af 6359 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
6360 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6361 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c 6362 .access = PL3_RW,
cb4a0a34
PM
6363 /* no .writefn needed as this can't cause an ASID change */
6364 .resetvalue = 0,
11f136ee 6365 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 6366 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 6367 .type = ARM_CP_ALIAS,
81547d66
EI
6368 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6369 .access = PL3_RW,
6370 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 6371 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
6372 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6373 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
6374 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6375 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6376 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 6377 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 6378 .type = ARM_CP_ALIAS,
81547d66 6379 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
6380 .access = PL3_RW,
6381 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
6382 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6383 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6384 .access = PL3_RW, .writefn = vbar_write,
6385 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6386 .resetvalue = 0 },
c6f19164
GB
6387 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6388 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6389 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6390 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
6391 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6392 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6393 .access = PL3_RW, .resetvalue = 0,
6394 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
6395 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6396 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6397 .access = PL3_RW, .type = ARM_CP_CONST,
6398 .resetvalue = 0 },
37cd6c24
PM
6399 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6400 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6401 .access = PL3_RW, .type = ARM_CP_CONST,
6402 .resetvalue = 0 },
6403 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6404 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6405 .access = PL3_RW, .type = ARM_CP_CONST,
6406 .resetvalue = 0 },
43efaa33
PM
6407 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6408 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6409 .access = PL3_W, .type = ARM_CP_NO_RAW,
6410 .writefn = tlbi_aa64_alle3is_write },
6411 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6412 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6413 .access = PL3_W, .type = ARM_CP_NO_RAW,
6414 .writefn = tlbi_aa64_vae3is_write },
6415 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6416 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6417 .access = PL3_W, .type = ARM_CP_NO_RAW,
6418 .writefn = tlbi_aa64_vae3is_write },
6419 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6420 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6421 .access = PL3_W, .type = ARM_CP_NO_RAW,
6422 .writefn = tlbi_aa64_alle3_write },
6423 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6424 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6425 .access = PL3_W, .type = ARM_CP_NO_RAW,
6426 .writefn = tlbi_aa64_vae3_write },
6427 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6428 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6429 .access = PL3_W, .type = ARM_CP_NO_RAW,
6430 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
6431};
6432
e2cce18f
RH
6433#ifndef CONFIG_USER_ONLY
6434/* Test if system register redirection is to occur in the current state. */
6435static bool redirect_for_e2h(CPUARMState *env)
6436{
6437 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6438}
6439
6440static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6441{
6442 CPReadFn *readfn;
6443
6444 if (redirect_for_e2h(env)) {
6445 /* Switch to the saved EL2 version of the register. */
6446 ri = ri->opaque;
6447 readfn = ri->readfn;
6448 } else {
6449 readfn = ri->orig_readfn;
6450 }
6451 if (readfn == NULL) {
6452 readfn = raw_read;
6453 }
6454 return readfn(env, ri);
6455}
6456
6457static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6458 uint64_t value)
6459{
6460 CPWriteFn *writefn;
6461
6462 if (redirect_for_e2h(env)) {
6463 /* Switch to the saved EL2 version of the register. */
6464 ri = ri->opaque;
6465 writefn = ri->writefn;
6466 } else {
6467 writefn = ri->orig_writefn;
6468 }
6469 if (writefn == NULL) {
6470 writefn = raw_write;
6471 }
6472 writefn(env, ri, value);
6473}
6474
6475static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6476{
6477 struct E2HAlias {
6478 uint32_t src_key, dst_key, new_key;
6479 const char *src_name, *dst_name, *new_name;
6480 bool (*feature)(const ARMISARegisters *id);
6481 };
6482
6483#define K(op0, op1, crn, crm, op2) \
6484 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6485
6486 static const struct E2HAlias aliases[] = {
6487 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
6488 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6489 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
6490 "CPACR", "CPTR_EL2", "CPACR_EL12" },
6491 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
6492 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6493 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
6494 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6495 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
6496 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6497 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
6498 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6499 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
6500 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6501 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
6502 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6503 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
6504 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6505 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
6506 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6507 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
6508 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6509 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6510 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6511 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6512 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6513 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6514 "VBAR", "VBAR_EL2", "VBAR_EL12" },
6515 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6516 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6517 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6518 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6519
6520 /*
6521 * Note that redirection of ZCR is mentioned in the description
6522 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6523 * not in the summary table.
6524 */
6525 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
6526 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
de561988
RH
6527 { K(3, 0, 1, 2, 6), K(3, 4, 1, 2, 6), K(3, 5, 1, 2, 6),
6528 "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
e2cce18f 6529
4b779ceb
RH
6530 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
6531 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6532
7cb1e618
RH
6533 { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6534 "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6535 isar_feature_aa64_scxtnum },
6536
e2cce18f
RH
6537 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6538 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6539 };
6540#undef K
6541
6542 size_t i;
6543
6544 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6545 const struct E2HAlias *a = &aliases[i];
9da35a40 6546 ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
9da35a40 6547 bool ok;
e2cce18f
RH
6548
6549 if (a->feature && !a->feature(&cpu->isar)) {
6550 continue;
6551 }
6552
5860362d
RH
6553 src_reg = g_hash_table_lookup(cpu->cp_regs,
6554 (gpointer)(uintptr_t)a->src_key);
6555 dst_reg = g_hash_table_lookup(cpu->cp_regs,
6556 (gpointer)(uintptr_t)a->dst_key);
e2cce18f
RH
6557 g_assert(src_reg != NULL);
6558 g_assert(dst_reg != NULL);
6559
6560 /* Cross-compare names to detect typos in the keys. */
6561 g_assert(strcmp(src_reg->name, a->src_name) == 0);
6562 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6563
6564 /* None of the core system registers use opaque; we will. */
6565 g_assert(src_reg->opaque == NULL);
6566
6567 /* Create alias before redirection so we dup the right data. */
9da35a40 6568 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
9da35a40
RH
6569
6570 new_reg->name = a->new_name;
6571 new_reg->type |= ARM_CP_ALIAS;
6572 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
6573 new_reg->access &= PL2_RW | PL3_RW;
6574
5860362d
RH
6575 ok = g_hash_table_insert(cpu->cp_regs,
6576 (gpointer)(uintptr_t)a->new_key, new_reg);
9da35a40 6577 g_assert(ok);
e2cce18f
RH
6578
6579 src_reg->opaque = dst_reg;
6580 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6581 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6582 if (!src_reg->raw_readfn) {
6583 src_reg->raw_readfn = raw_read;
6584 }
6585 if (!src_reg->raw_writefn) {
6586 src_reg->raw_writefn = raw_write;
6587 }
6588 src_reg->readfn = el2_e2h_read;
6589 src_reg->writefn = el2_e2h_write;
6590 }
6591}
6592#endif
6593
3f208fd7
PM
6594static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6595 bool isread)
7da845b0 6596{
97475a89
RH
6597 int cur_el = arm_current_el(env);
6598
6599 if (cur_el < 2) {
6600 uint64_t hcr = arm_hcr_el2_eff(env);
6601
6602 if (cur_el == 0) {
6603 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6604 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6605 return CP_ACCESS_TRAP_EL2;
6606 }
6607 } else {
6608 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6609 return CP_ACCESS_TRAP;
6610 }
6611 if (hcr & HCR_TID2) {
6612 return CP_ACCESS_TRAP_EL2;
6613 }
6614 }
6615 } else if (hcr & HCR_TID2) {
6616 return CP_ACCESS_TRAP_EL2;
6617 }
7da845b0 6618 }
630fcd4d
MZ
6619
6620 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6621 return CP_ACCESS_TRAP_EL2;
6622 }
6623
7da845b0
PM
6624 return CP_ACCESS_OK;
6625}
6626
58e93b48
RH
6627/*
6628 * Check for traps to RAS registers, which are controlled
6629 * by HCR_EL2.TERR and SCR_EL3.TERR.
6630 */
6631static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6632 bool isread)
6633{
6634 int el = arm_current_el(env);
6635
6636 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6637 return CP_ACCESS_TRAP_EL2;
6638 }
6639 if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6640 return CP_ACCESS_TRAP_EL3;
6641 }
6642 return CP_ACCESS_OK;
6643}
6644
6645static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6646{
6647 int el = arm_current_el(env);
6648
6649 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6650 return env->cp15.vdisr_el2;
6651 }
6652 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6653 return 0; /* RAZ/WI */
6654 }
6655 return env->cp15.disr_el1;
6656}
6657
6658static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6659{
6660 int el = arm_current_el(env);
6661
6662 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6663 env->cp15.vdisr_el2 = val;
6664 return;
6665 }
6666 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6667 return; /* RAZ/WI */
6668 }
6669 env->cp15.disr_el1 = val;
6670}
6671
6672/*
6673 * Minimal RAS implementation with no Error Records.
6674 * Which means that all of the Error Record registers:
6675 * ERXADDR_EL1
6676 * ERXCTLR_EL1
6677 * ERXFR_EL1
6678 * ERXMISC0_EL1
6679 * ERXMISC1_EL1
6680 * ERXMISC2_EL1
6681 * ERXMISC3_EL1
6682 * ERXPFGCDN_EL1 (RASv1p1)
6683 * ERXPFGCTL_EL1 (RASv1p1)
6684 * ERXPFGF_EL1 (RASv1p1)
6685 * ERXSTATUS_EL1
6686 * and
6687 * ERRSELR_EL1
6688 * may generate UNDEFINED, which is the effect we get by not
6689 * listing them at all.
bd8db7d9
PM
6690 *
6691 * These registers have fine-grained trap bits, but UNDEF-to-EL1
6692 * is higher priority than FGT-to-EL2 so we do not need to list them
6693 * in order to check for an FGT.
58e93b48
RH
6694 */
6695static const ARMCPRegInfo minimal_ras_reginfo[] = {
6696 { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6697 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6698 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6699 .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6700 { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6701 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6702 .access = PL1_R, .accessfn = access_terr,
bd8db7d9 6703 .fgt = FGT_ERRIDR_EL1,
58e93b48
RH
6704 .type = ARM_CP_CONST, .resetvalue = 0 },
6705 { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6706 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6707 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6708 { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6709 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6710 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6711};
6712
397d922c
RH
6713/*
6714 * Return the exception level to which exceptions should be taken
6715 * via SVEAccessTrap. This excludes the check for whether the exception
6716 * should be routed through AArch64.AdvSIMDFPAccessTrap. That can easily
6717 * be found by testing 0 < fp_exception_el < sve_exception_el.
6718 *
6719 * C.f. the ARM pseudocode function CheckSVEEnabled. Note that the
6720 * pseudocode does *not* separate out the FP trap checks, but has them
6721 * all in one function.
5be5e8ed 6722 */
ced31551 6723int sve_exception_el(CPUARMState *env, int el)
5be5e8ed
RH
6724{
6725#ifndef CONFIG_USER_ONLY
aa4451b6 6726 if (el <= 1 && !el_is_in_host(env, el)) {
fab8ad39 6727 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
7701cee5
RH
6728 case 1:
6729 if (el != 0) {
6730 break;
6731 }
6732 /* fall through */
6733 case 0:
6734 case 2:
61a8c23a 6735 return 1;
5be5e8ed 6736 }
5be5e8ed
RH
6737 }
6738
7d38cb92
RH
6739 if (el <= 2 && arm_is_el2_enabled(env)) {
6740 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6741 if (env->cp15.hcr_el2 & HCR_E2H) {
fab8ad39 6742 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
d5a6fa2d 6743 case 1:
7d38cb92 6744 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
d5a6fa2d
RH
6745 break;
6746 }
6747 /* fall through */
6748 case 0:
6749 case 2:
6750 return 2;
6751 }
7d38cb92 6752 } else {
fab8ad39 6753 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
d5a6fa2d
RH
6754 return 2;
6755 }
60eed086 6756 }
5be5e8ed
RH
6757 }
6758
60eed086
RH
6759 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6760 if (arm_feature(env, ARM_FEATURE_EL3)
fab8ad39 6761 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
5be5e8ed
RH
6762 return 3;
6763 }
6764#endif
6765 return 0;
6766}
6767
6b2ca83e
RH
6768/*
6769 * Return the exception level to which exceptions should be taken for SME.
6770 * C.f. the ARM pseudocode function CheckSMEAccess.
6771 */
6772int sme_exception_el(CPUARMState *env, int el)
6773{
6774#ifndef CONFIG_USER_ONLY
6775 if (el <= 1 && !el_is_in_host(env, el)) {
6776 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6777 case 1:
6778 if (el != 0) {
6779 break;
6780 }
6781 /* fall through */
6782 case 0:
6783 case 2:
6784 return 1;
6785 }
6786 }
6787
6788 if (el <= 2 && arm_is_el2_enabled(env)) {
6789 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6790 if (env->cp15.hcr_el2 & HCR_E2H) {
6791 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6792 case 1:
6793 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6794 break;
6795 }
6796 /* fall through */
6797 case 0:
6798 case 2:
6799 return 2;
6800 }
6801 } else {
6802 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6803 return 2;
6804 }
6805 }
6806 }
6807
6808 /* CPTR_EL3. Since ESM is negative we must check for EL3. */
6809 if (arm_feature(env, ARM_FEATURE_EL3)
6810 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6811 return 3;
6812 }
6813#endif
6814 return 0;
6815}
6816
0ab5953b
RH
6817/*
6818 * Given that SVE is enabled, return the vector length for EL.
6819 */
6ca54aa9 6820uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
0ab5953b 6821{
2fc0cc0e 6822 ARMCPU *cpu = env_archcpu(env);
6ca54aa9
RH
6823 uint64_t *cr = env->vfp.zcr_el;
6824 uint32_t map = cpu->sve_vq.map;
6825 uint32_t len = ARM_MAX_VQ - 1;
6826
6827 if (sm) {
6828 cr = env->vfp.smcr_el;
6829 map = cpu->sme_vq.map;
6830 }
0ab5953b 6831
c6225beb 6832 if (el <= 1 && !el_is_in_host(env, el)) {
6ca54aa9 6833 len = MIN(len, 0xf & (uint32_t)cr[1]);
0ab5953b 6834 }
6a02a732 6835 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6ca54aa9 6836 len = MIN(len, 0xf & (uint32_t)cr[2]);
0ab5953b 6837 }
6a02a732 6838 if (arm_feature(env, ARM_FEATURE_EL3)) {
6ca54aa9
RH
6839 len = MIN(len, 0xf & (uint32_t)cr[3]);
6840 }
6841
6842 map &= MAKE_64BIT_MASK(0, len + 1);
6843 if (map != 0) {
6844 return 31 - clz32(map);
0ab5953b 6845 }
0df9142d 6846
6ca54aa9
RH
6847 /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6848 assert(sm);
6849 return ctz32(cpu->sme_vq.map);
6850}
6851
6852uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6853{
6854 return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
0ab5953b
RH
6855}
6856
5be5e8ed
RH
6857static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6858 uint64_t value)
6859{
0ab5953b 6860 int cur_el = arm_current_el(env);
5ef3cc56 6861 int old_len = sve_vqm1_for_el(env, cur_el);
0ab5953b
RH
6862 int new_len;
6863
5be5e8ed 6864 /* Bits other than [3:0] are RAZ/WI. */
7b351d98 6865 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5be5e8ed 6866 raw_write(env, ri, value & 0xf);
0ab5953b
RH
6867
6868 /*
6869 * Because we arrived here, we know both FP and SVE are enabled;
6870 * otherwise we would have trapped access to the ZCR_ELn register.
6871 */
5ef3cc56 6872 new_len = sve_vqm1_for_el(env, cur_el);
0ab5953b
RH
6873 if (new_len < old_len) {
6874 aarch64_sve_narrow_vq(env, new_len + 1);
6875 }
5be5e8ed
RH
6876}
6877
60360d82
RH
6878static const ARMCPRegInfo zcr_reginfo[] = {
6879 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6880 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6881 .access = PL1_RW, .type = ARM_CP_SVE,
6882 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6883 .writefn = zcr_write, .raw_writefn = raw_write },
6884 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6885 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6886 .access = PL2_RW, .type = ARM_CP_SVE,
6887 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6888 .writefn = zcr_write, .raw_writefn = raw_write },
6889 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6890 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6891 .access = PL3_RW, .type = ARM_CP_SVE,
6892 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6893 .writefn = zcr_write, .raw_writefn = raw_write },
5be5e8ed
RH
6894};
6895
9e5ec745
RH
6896#ifdef TARGET_AARCH64
6897static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6898 bool isread)
6899{
6900 int el = arm_current_el(env);
6901
6902 if (el == 0) {
6903 uint64_t sctlr = arm_sctlr(env, el);
6904 if (!(sctlr & SCTLR_EnTP2)) {
6905 return CP_ACCESS_TRAP;
6906 }
6907 }
6908 /* TODO: FEAT_FGT */
6909 if (el < 3
6910 && arm_feature(env, ARM_FEATURE_EL3)
6911 && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6912 return CP_ACCESS_TRAP_EL3;
6913 }
6914 return CP_ACCESS_OK;
6915}
6916
d5b1223a
RH
6917static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6918 bool isread)
6919{
6920 /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6921 if (arm_current_el(env) < 3
6922 && arm_feature(env, ARM_FEATURE_EL3)
6923 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6924 return CP_ACCESS_TRAP_EL3;
6925 }
6926 return CP_ACCESS_OK;
6927}
6928
7f2a01e7
RH
6929/* ResetSVEState */
6930static void arm_reset_sve_state(CPUARMState *env)
6931{
6932 memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
6933 /* Recall that FFR is stored as pregs[16]. */
6934 memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
6935 vfp_set_fpcr(env, 0x0800009f);
6936}
6937
2a8af382
RH
6938void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
6939{
6940 uint64_t change = (env->svcr ^ new) & mask;
6941
f4318557
RH
6942 if (change == 0) {
6943 return;
6944 }
2a8af382 6945 env->svcr ^= change;
7f2a01e7
RH
6946
6947 if (change & R_SVCR_SM_MASK) {
6948 arm_reset_sve_state(env);
6949 }
fccb4918
RH
6950
6951 /*
6952 * ResetSMEState.
6953 *
6954 * SetPSTATE_ZA zeros on enable and disable. We can zero this only
6955 * on enable: while disabled, the storage is inaccessible and the
6956 * value does not matter. We're not saving the storage in vmstate
6957 * when disabled either.
6958 */
6959 if (change & new & R_SVCR_ZA_MASK) {
6960 memset(env->zarray, 0, sizeof(env->zarray));
6961 }
f4318557 6962
2b77ad4d
FR
6963 if (tcg_enabled()) {
6964 arm_rebuild_hflags(env);
6965 }
2a8af382
RH
6966}
6967
c37e6ac9
RH
6968static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6969 uint64_t value)
6970{
2a8af382 6971 aarch64_set_svcr(env, value, -1);
c37e6ac9
RH
6972}
6973
de561988
RH
6974static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6975 uint64_t value)
6976{
6977 int cur_el = arm_current_el(env);
6978 int old_len = sve_vqm1_for_el(env, cur_el);
6979 int new_len;
6980
6981 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6982 value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6983 raw_write(env, ri, value);
6984
6985 /*
6986 * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6987 * when SVL is widened (old values kept, or zeros). Choose to keep the
6988 * current values for simplicity. But for QEMU internals, we must still
6989 * apply the narrower SVL to the Zregs and Pregs -- see the comment
6990 * above aarch64_sve_narrow_vq.
6991 */
6992 new_len = sve_vqm1_for_el(env, cur_el);
6993 if (new_len < old_len) {
6994 aarch64_sve_narrow_vq(env, new_len + 1);
6995 }
6996}
6997
9e5ec745
RH
6998static const ARMCPRegInfo sme_reginfo[] = {
6999 { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
7000 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
7001 .access = PL0_RW, .accessfn = access_tpidr2,
bd8db7d9 7002 .fgt = FGT_NTPIDR2_EL0,
9e5ec745 7003 .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
c37e6ac9
RH
7004 { .name = "SVCR", .state = ARM_CP_STATE_AA64,
7005 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
7006 .access = PL0_RW, .type = ARM_CP_SME,
7007 .fieldoffset = offsetof(CPUARMState, svcr),
7008 .writefn = svcr_write, .raw_writefn = raw_write },
de561988
RH
7009 { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
7010 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
7011 .access = PL1_RW, .type = ARM_CP_SME,
7012 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
7013 .writefn = smcr_write, .raw_writefn = raw_write },
7014 { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
7015 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
7016 .access = PL2_RW, .type = ARM_CP_SME,
7017 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
7018 .writefn = smcr_write, .raw_writefn = raw_write },
7019 { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
7020 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
7021 .access = PL3_RW, .type = ARM_CP_SME,
7022 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
7023 .writefn = smcr_write, .raw_writefn = raw_write },
d5b1223a
RH
7024 { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
7025 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
7026 .access = PL1_R, .accessfn = access_aa64_tid1,
7027 /*
7028 * IMPLEMENTOR = 0 (software)
7029 * REVISION = 0 (implementation defined)
7030 * SMPS = 0 (no streaming execution priority in QEMU)
7031 * AFFINITY = 0 (streaming sve mode not shared with other PEs)
7032 */
7033 .type = ARM_CP_CONST, .resetvalue = 0, },
7034 /*
7035 * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
7036 */
7037 { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
7038 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
7039 .access = PL1_RW, .accessfn = access_esm,
bd8db7d9 7040 .fgt = FGT_NSMPRI_EL1,
d5b1223a
RH
7041 .type = ARM_CP_CONST, .resetvalue = 0 },
7042 { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
7043 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
7044 .access = PL2_RW, .accessfn = access_esm,
7045 .type = ARM_CP_CONST, .resetvalue = 0 },
9e5ec745 7046};
ef1febe7
RH
7047
7048static void tlbi_aa64_paall_write(CPUARMState *env, const ARMCPRegInfo *ri,
7049 uint64_t value)
7050{
7051 CPUState *cs = env_cpu(env);
7052
7053 tlb_flush(cs);
7054}
7055
7056static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7057 uint64_t value)
7058{
7059 /* L0GPTSZ is RO; other bits not mentioned are RES0. */
7060 uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
7061 R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
7062 R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
7063
7064 env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
7065}
7066
7067static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
7068{
7069 env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
7070 env_archcpu(env)->reset_l0gptsz);
7071}
7072
7073static void tlbi_aa64_paallos_write(CPUARMState *env, const ARMCPRegInfo *ri,
7074 uint64_t value)
7075{
7076 CPUState *cs = env_cpu(env);
7077
7078 tlb_flush_all_cpus_synced(cs);
7079}
7080
7081static const ARMCPRegInfo rme_reginfo[] = {
7082 { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
7083 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
7084 .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
7085 .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
7086 { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
7087 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
7088 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
7089 { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
7090 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
7091 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
7092 { .name = "TLBI_PAALL", .state = ARM_CP_STATE_AA64,
7093 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 4,
7094 .access = PL3_W, .type = ARM_CP_NO_RAW,
7095 .writefn = tlbi_aa64_paall_write },
7096 { .name = "TLBI_PAALLOS", .state = ARM_CP_STATE_AA64,
7097 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 4,
7098 .access = PL3_W, .type = ARM_CP_NO_RAW,
7099 .writefn = tlbi_aa64_paallos_write },
7100 /*
7101 * QEMU does not have a way to invalidate by physical address, thus
7102 * invalidating a range of physical addresses is accomplished by
673d8215 7103 * flushing all tlb entries in the outer shareable domain,
ef1febe7
RH
7104 * just like PAALLOS.
7105 */
7106 { .name = "TLBI_RPALOS", .state = ARM_CP_STATE_AA64,
7107 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 7,
7108 .access = PL3_W, .type = ARM_CP_NO_RAW,
7109 .writefn = tlbi_aa64_paallos_write },
7110 { .name = "TLBI_RPAOS", .state = ARM_CP_STATE_AA64,
7111 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 3,
7112 .access = PL3_W, .type = ARM_CP_NO_RAW,
7113 .writefn = tlbi_aa64_paallos_write },
7114 { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
7115 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
7116 .access = PL3_W, .type = ARM_CP_NOP },
7117};
7118
7119static const ARMCPRegInfo rme_mte_reginfo[] = {
7120 { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
7121 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
7122 .access = PL3_W, .type = ARM_CP_NOP },
7123};
9e5ec745
RH
7124#endif /* TARGET_AARCH64 */
7125
24183fb6
PM
7126static void define_pmu_regs(ARMCPU *cpu)
7127{
7128 /*
7129 * v7 performance monitor control register: same implementor
7130 * field as main ID register, and we implement four counters in
7131 * addition to the cycle count register.
7132 */
24526bb9 7133 unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
24183fb6
PM
7134 ARMCPRegInfo pmcr = {
7135 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
7136 .access = PL0_RW,
dc780233 7137 .fgt = FGT_PMCR_EL0,
24183fb6
PM
7138 .type = ARM_CP_IO | ARM_CP_ALIAS,
7139 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
7140 .accessfn = pmreg_access, .writefn = pmcr_write,
7141 .raw_writefn = raw_write,
7142 };
7143 ARMCPRegInfo pmcr64 = {
7144 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
7145 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
7146 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 7147 .fgt = FGT_PMCR_EL0,
24183fb6
PM
7148 .type = ARM_CP_IO,
7149 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
24526bb9 7150 .resetvalue = cpu->isar.reset_pmcr_el0,
24183fb6
PM
7151 .writefn = pmcr_write, .raw_writefn = raw_write,
7152 };
24526bb9 7153
24183fb6
PM
7154 define_one_arm_cp_reg(cpu, &pmcr);
7155 define_one_arm_cp_reg(cpu, &pmcr64);
7156 for (i = 0; i < pmcrn; i++) {
7157 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
7158 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
7159 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
7160 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
7161 ARMCPRegInfo pmev_regs[] = {
7162 { .name = pmevcntr_name, .cp = 15, .crn = 14,
7163 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7164 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
dc780233 7165 .fgt = FGT_PMEVCNTRN_EL0,
24183fb6 7166 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
99a50d1a 7167 .accessfn = pmreg_access_xevcntr },
24183fb6
PM
7168 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
7169 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
99a50d1a 7170 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
24183fb6 7171 .type = ARM_CP_IO,
dc780233 7172 .fgt = FGT_PMEVCNTRN_EL0,
24183fb6
PM
7173 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7174 .raw_readfn = pmevcntr_rawread,
7175 .raw_writefn = pmevcntr_rawwrite },
7176 { .name = pmevtyper_name, .cp = 15, .crn = 14,
7177 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7178 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
dc780233 7179 .fgt = FGT_PMEVTYPERN_EL0,
24183fb6
PM
7180 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7181 .accessfn = pmreg_access },
7182 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
7183 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
7184 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
dc780233 7185 .fgt = FGT_PMEVTYPERN_EL0,
24183fb6
PM
7186 .type = ARM_CP_IO,
7187 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7188 .raw_writefn = pmevtyper_rawwrite },
24183fb6
PM
7189 };
7190 define_arm_cp_regs(cpu, pmev_regs);
7191 g_free(pmevcntr_name);
7192 g_free(pmevcntr_el0_name);
7193 g_free(pmevtyper_name);
7194 g_free(pmevtyper_el0_name);
7195 }
a793bcd0 7196 if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
24183fb6
PM
7197 ARMCPRegInfo v81_pmu_regs[] = {
7198 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
7199 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
7200 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 7201 .fgt = FGT_PMCEIDN_EL0,
24183fb6
PM
7202 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
7203 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
7204 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
7205 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 7206 .fgt = FGT_PMCEIDN_EL0,
24183fb6 7207 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
24183fb6
PM
7208 };
7209 define_arm_cp_regs(cpu, v81_pmu_regs);
7210 }
a793bcd0 7211 if (cpu_isar_feature(any_pmuv3p4, cpu)) {
15dd1ebd
PM
7212 static const ARMCPRegInfo v84_pmmir = {
7213 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
7214 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
7215 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 7216 .fgt = FGT_PMMIR_EL1,
15dd1ebd
PM
7217 .resetvalue = 0
7218 };
7219 define_one_arm_cp_reg(cpu, &v84_pmmir);
7220 }
24183fb6
PM
7221}
7222
0f150c84 7223#ifndef CONFIG_USER_ONLY
9b37a28c
FR
7224/*
7225 * We don't know until after realize whether there's a GICv3
96a8b92e
PM
7226 * attached, and that is what registers the gicv3 sysregs.
7227 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
7228 * at runtime.
7229 */
7230static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
7231{
2fc0cc0e 7232 ARMCPU *cpu = env_archcpu(env);
8a130a7b 7233 uint64_t pfr1 = cpu->isar.id_pfr1;
96a8b92e
PM
7234
7235 if (env->gicv3state) {
7236 pfr1 |= 1 << 28;
7237 }
7238 return pfr1;
7239}
7240
7241static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
7242{
2fc0cc0e 7243 ARMCPU *cpu = env_archcpu(env);
47576b94 7244 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
96a8b92e
PM
7245
7246 if (env->gicv3state) {
7247 pfr0 |= 1 << 24;
7248 }
7249 return pfr0;
7250}
976b99b6 7251#endif
96a8b92e 7252
9b37a28c
FR
7253/*
7254 * Shared logic between LORID and the rest of the LOR* registers.
9bd268ba 7255 * Secure state exclusion has already been dealt with.
2d7137c1 7256 */
9bd268ba
RDC
7257static CPAccessResult access_lor_ns(CPUARMState *env,
7258 const ARMCPRegInfo *ri, bool isread)
2d7137c1
RH
7259{
7260 int el = arm_current_el(env);
7261
7262 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
7263 return CP_ACCESS_TRAP_EL2;
7264 }
7265 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
7266 return CP_ACCESS_TRAP_EL3;
7267 }
7268 return CP_ACCESS_OK;
7269}
7270
2d7137c1
RH
7271static CPAccessResult access_lor_other(CPUARMState *env,
7272 const ARMCPRegInfo *ri, bool isread)
7273{
7274 if (arm_is_secure_below_el3(env)) {
7275 /* Access denied in secure mode. */
7276 return CP_ACCESS_TRAP;
7277 }
9bd268ba 7278 return access_lor_ns(env, ri, isread);
2d7137c1
RH
7279}
7280
d8564ee4
RH
7281/*
7282 * A trivial implementation of ARMv8.1-LOR leaves all of these
7283 * registers fixed at 0, which indicates that there are zero
7284 * supported Limited Ordering regions.
7285 */
7286static const ARMCPRegInfo lor_reginfo[] = {
7287 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
7288 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
7289 .access = PL1_RW, .accessfn = access_lor_other,
b19ed03c 7290 .fgt = FGT_LORSA_EL1,
d8564ee4
RH
7291 .type = ARM_CP_CONST, .resetvalue = 0 },
7292 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7293 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7294 .access = PL1_RW, .accessfn = access_lor_other,
b19ed03c 7295 .fgt = FGT_LOREA_EL1,
d8564ee4
RH
7296 .type = ARM_CP_CONST, .resetvalue = 0 },
7297 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7298 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7299 .access = PL1_RW, .accessfn = access_lor_other,
b19ed03c 7300 .fgt = FGT_LORN_EL1,
d8564ee4
RH
7301 .type = ARM_CP_CONST, .resetvalue = 0 },
7302 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7303 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7304 .access = PL1_RW, .accessfn = access_lor_other,
b19ed03c 7305 .fgt = FGT_LORC_EL1,
d8564ee4
RH
7306 .type = ARM_CP_CONST, .resetvalue = 0 },
7307 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7308 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
9bd268ba 7309 .access = PL1_R, .accessfn = access_lor_ns,
b19ed03c 7310 .fgt = FGT_LORID_EL1,
d8564ee4 7311 .type = ARM_CP_CONST, .resetvalue = 0 },
d8564ee4
RH
7312};
7313
967aa94f
RH
7314#ifdef TARGET_AARCH64
7315static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7316 bool isread)
7317{
7318 int el = arm_current_el(env);
7319
7320 if (el < 2 &&
07b034ea 7321 arm_is_el2_enabled(env) &&
967aa94f
RH
7322 !(arm_hcr_el2_eff(env) & HCR_APK)) {
7323 return CP_ACCESS_TRAP_EL2;
7324 }
7325 if (el < 3 &&
7326 arm_feature(env, ARM_FEATURE_EL3) &&
7327 !(env->cp15.scr_el3 & SCR_APK)) {
7328 return CP_ACCESS_TRAP_EL3;
7329 }
7330 return CP_ACCESS_OK;
7331}
7332
7333static const ARMCPRegInfo pauth_reginfo[] = {
7334 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7335 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7336 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7337 .fgt = FGT_APDAKEY,
108b3ba8 7338 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
967aa94f
RH
7339 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7340 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7341 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7342 .fgt = FGT_APDAKEY,
108b3ba8 7343 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
967aa94f
RH
7344 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7345 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7346 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7347 .fgt = FGT_APDBKEY,
108b3ba8 7348 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
967aa94f
RH
7349 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7350 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7351 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7352 .fgt = FGT_APDBKEY,
108b3ba8 7353 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
967aa94f
RH
7354 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7355 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7356 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7357 .fgt = FGT_APGAKEY,
108b3ba8 7358 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
967aa94f
RH
7359 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7360 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7361 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7362 .fgt = FGT_APGAKEY,
108b3ba8 7363 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
967aa94f
RH
7364 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7365 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7366 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7367 .fgt = FGT_APIAKEY,
108b3ba8 7368 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
967aa94f
RH
7369 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7370 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7371 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7372 .fgt = FGT_APIAKEY,
108b3ba8 7373 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
967aa94f
RH
7374 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7375 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7376 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7377 .fgt = FGT_APIBKEY,
108b3ba8 7378 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
967aa94f
RH
7379 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7380 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7381 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7382 .fgt = FGT_APIBKEY,
108b3ba8 7383 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
967aa94f 7384};
de390645 7385
84940ed8
RC
7386static const ARMCPRegInfo tlbirange_reginfo[] = {
7387 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7388 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
0f66d223 7389 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 7390 .fgt = FGT_TLBIRVAE1IS,
84940ed8
RC
7391 .writefn = tlbi_aa64_rvae1is_write },
7392 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7393 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
0f66d223 7394 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 7395 .fgt = FGT_TLBIRVAAE1IS,
84940ed8
RC
7396 .writefn = tlbi_aa64_rvae1is_write },
7397 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7398 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
0f66d223 7399 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 7400 .fgt = FGT_TLBIRVALE1IS,
84940ed8
RC
7401 .writefn = tlbi_aa64_rvae1is_write },
7402 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7403 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
0f66d223 7404 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 7405 .fgt = FGT_TLBIRVAALE1IS,
84940ed8
RC
7406 .writefn = tlbi_aa64_rvae1is_write },
7407 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7408 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
fe3ca86c 7409 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
bf2f0625 7410 .fgt = FGT_TLBIRVAE1OS,
84940ed8
RC
7411 .writefn = tlbi_aa64_rvae1is_write },
7412 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7413 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
fe3ca86c 7414 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
bf2f0625 7415 .fgt = FGT_TLBIRVAAE1OS,
84940ed8
RC
7416 .writefn = tlbi_aa64_rvae1is_write },
7417 { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7418 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
fe3ca86c 7419 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
bf2f0625 7420 .fgt = FGT_TLBIRVALE1OS,
84940ed8
RC
7421 .writefn = tlbi_aa64_rvae1is_write },
7422 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7423 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
fe3ca86c 7424 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
bf2f0625 7425 .fgt = FGT_TLBIRVAALE1OS,
84940ed8
RC
7426 .writefn = tlbi_aa64_rvae1is_write },
7427 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7428 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
4870f38b 7429 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 7430 .fgt = FGT_TLBIRVAE1,
84940ed8
RC
7431 .writefn = tlbi_aa64_rvae1_write },
7432 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7433 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
4870f38b 7434 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 7435 .fgt = FGT_TLBIRVAAE1,
84940ed8
RC
7436 .writefn = tlbi_aa64_rvae1_write },
7437 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7438 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
4870f38b 7439 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 7440 .fgt = FGT_TLBIRVALE1,
84940ed8
RC
7441 .writefn = tlbi_aa64_rvae1_write },
7442 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7443 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
4870f38b 7444 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 7445 .fgt = FGT_TLBIRVAALE1,
84940ed8
RC
7446 .writefn = tlbi_aa64_rvae1_write },
7447 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7448 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
575a94af
RH
7449 .access = PL2_W, .type = ARM_CP_NO_RAW,
7450 .writefn = tlbi_aa64_ripas2e1is_write },
84940ed8
RC
7451 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7452 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
575a94af
RH
7453 .access = PL2_W, .type = ARM_CP_NO_RAW,
7454 .writefn = tlbi_aa64_ripas2e1is_write },
84940ed8
RC
7455 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7456 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
696ba377 7457 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7458 .writefn = tlbi_aa64_rvae2is_write },
7459 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7460 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
696ba377 7461 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7462 .writefn = tlbi_aa64_rvae2is_write },
7463 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7464 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
575a94af
RH
7465 .access = PL2_W, .type = ARM_CP_NO_RAW,
7466 .writefn = tlbi_aa64_ripas2e1_write },
7467 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
84940ed8 7468 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
575a94af
RH
7469 .access = PL2_W, .type = ARM_CP_NO_RAW,
7470 .writefn = tlbi_aa64_ripas2e1_write },
84940ed8
RC
7471 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7472 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
696ba377 7473 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7474 .writefn = tlbi_aa64_rvae2is_write },
7475 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7476 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
696ba377 7477 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7478 .writefn = tlbi_aa64_rvae2is_write },
7479 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7480 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
696ba377 7481 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7482 .writefn = tlbi_aa64_rvae2_write },
7483 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7484 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
696ba377 7485 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7486 .writefn = tlbi_aa64_rvae2_write },
7487 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7488 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7489 .access = PL3_W, .type = ARM_CP_NO_RAW,
7490 .writefn = tlbi_aa64_rvae3is_write },
7491 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7492 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7493 .access = PL3_W, .type = ARM_CP_NO_RAW,
7494 .writefn = tlbi_aa64_rvae3is_write },
7495 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7496 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7497 .access = PL3_W, .type = ARM_CP_NO_RAW,
7498 .writefn = tlbi_aa64_rvae3is_write },
7499 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7500 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7501 .access = PL3_W, .type = ARM_CP_NO_RAW,
7502 .writefn = tlbi_aa64_rvae3is_write },
7503 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7504 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7505 .access = PL3_W, .type = ARM_CP_NO_RAW,
7506 .writefn = tlbi_aa64_rvae3_write },
7507 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7508 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7509 .access = PL3_W, .type = ARM_CP_NO_RAW,
7510 .writefn = tlbi_aa64_rvae3_write },
84940ed8
RC
7511};
7512
7113d618
RC
7513static const ARMCPRegInfo tlbios_reginfo[] = {
7514 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7515 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
fe3ca86c 7516 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
bf2f0625 7517 .fgt = FGT_TLBIVMALLE1OS,
7113d618 7518 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
7519 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7520 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
bf2f0625 7521 .fgt = FGT_TLBIVAE1OS,
fe3ca86c 7522 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
b7469ef9 7523 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
7524 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7525 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
fe3ca86c 7526 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
bf2f0625 7527 .fgt = FGT_TLBIASIDE1OS,
7113d618 7528 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
7529 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7530 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
fe3ca86c 7531 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
bf2f0625 7532 .fgt = FGT_TLBIVAAE1OS,
b7469ef9
IH
7533 .writefn = tlbi_aa64_vae1is_write },
7534 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7535 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
fe3ca86c 7536 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
bf2f0625 7537 .fgt = FGT_TLBIVALE1OS,
b7469ef9
IH
7538 .writefn = tlbi_aa64_vae1is_write },
7539 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7540 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
fe3ca86c 7541 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
bf2f0625 7542 .fgt = FGT_TLBIVAALE1OS,
b7469ef9 7543 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
7544 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7545 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
696ba377 7546 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7113d618 7547 .writefn = tlbi_aa64_alle2is_write },
b7469ef9
IH
7548 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7549 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
696ba377 7550 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
b7469ef9 7551 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
7552 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7553 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7554 .access = PL2_W, .type = ARM_CP_NO_RAW,
7555 .writefn = tlbi_aa64_alle1is_write },
b7469ef9
IH
7556 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7557 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
696ba377 7558 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
b7469ef9 7559 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
7560 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7561 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7562 .access = PL2_W, .type = ARM_CP_NO_RAW,
7563 .writefn = tlbi_aa64_alle1is_write },
7564 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7565 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7566 .access = PL2_W, .type = ARM_CP_NOP },
7567 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7568 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7569 .access = PL2_W, .type = ARM_CP_NOP },
7570 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7571 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7572 .access = PL2_W, .type = ARM_CP_NOP },
7573 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7574 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7575 .access = PL2_W, .type = ARM_CP_NOP },
7576 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7577 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7578 .access = PL3_W, .type = ARM_CP_NO_RAW,
7579 .writefn = tlbi_aa64_alle3is_write },
b7469ef9
IH
7580 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7581 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7582 .access = PL3_W, .type = ARM_CP_NO_RAW,
7583 .writefn = tlbi_aa64_vae3is_write },
7584 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7585 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7586 .access = PL3_W, .type = ARM_CP_NO_RAW,
7587 .writefn = tlbi_aa64_vae3is_write },
7113d618
RC
7588};
7589
de390645
RH
7590static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7591{
7592 Error *err = NULL;
7593 uint64_t ret;
7594
7595 /* Success sets NZCV = 0000. */
7596 env->NF = env->CF = env->VF = 0, env->ZF = 1;
7597
7598 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7599 /*
7600 * ??? Failed, for unknown reasons in the crypto subsystem.
7601 * The best we can do is log the reason and return the
7602 * timed-out indication to the guest. There is no reason
7603 * we know to expect this failure to be transitory, so the
7604 * guest may well hang retrying the operation.
7605 */
7606 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7607 ri->name, error_get_pretty(err));
7608 error_free(err);
7609
7610 env->ZF = 0; /* NZCF = 0100 */
7611 return 0;
7612 }
7613 return ret;
7614}
7615
7616/* We do not support re-seeding, so the two registers operate the same. */
7617static const ARMCPRegInfo rndr_reginfo[] = {
7618 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7619 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7620 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7621 .access = PL0_R, .readfn = rndr_readfn },
7622 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7623 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7624 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7625 .access = PL0_R, .readfn = rndr_readfn },
de390645 7626};
0d57b499 7627
0d57b499
BM
7628static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7629 uint64_t value)
7630{
7631 ARMCPU *cpu = env_archcpu(env);
7632 /* CTR_EL0 System register -> DminLine, bits [19:16] */
7633 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7634 uint64_t vaddr_in = (uint64_t) value;
7635 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7636 void *haddr;
7637 int mem_idx = cpu_mmu_index(env, false);
7638
7639 /* This won't be crossing page boundaries */
7640 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7641 if (haddr) {
cd4a47f9 7642#ifndef CONFIG_USER_ONLY
0d57b499
BM
7643
7644 ram_addr_t offset;
7645 MemoryRegion *mr;
7646
7647 /* RCU lock is already being held */
7648 mr = memory_region_from_host(haddr, &offset);
7649
7650 if (mr) {
4dfe59d1 7651 memory_region_writeback(mr, offset, dline_size);
0d57b499 7652 }
cd4a47f9 7653#endif /*CONFIG_USER_ONLY*/
0d57b499
BM
7654 }
7655}
7656
7657static const ARMCPRegInfo dcpop_reg[] = {
7658 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7659 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7660 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
dd345653 7661 .fgt = FGT_DCCVAP,
1bed4d2e 7662 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
7663};
7664
7665static const ARMCPRegInfo dcpodp_reg[] = {
7666 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7667 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7668 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
dd345653 7669 .fgt = FGT_DCCVADP,
1bed4d2e 7670 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499 7671};
0d57b499 7672
4b779ceb
RH
7673static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7674 bool isread)
7675{
7676 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7677 return CP_ACCESS_TRAP_EL2;
7678 }
7679
7680 return CP_ACCESS_OK;
7681}
7682
7683static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7684 bool isread)
7685{
7686 int el = arm_current_el(env);
7687
0da067f2 7688 if (el < 2 && arm_is_el2_enabled(env)) {
4301acd7
RH
7689 uint64_t hcr = arm_hcr_el2_eff(env);
7690 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7691 return CP_ACCESS_TRAP_EL2;
7692 }
4b779ceb
RH
7693 }
7694 if (el < 3 &&
7695 arm_feature(env, ARM_FEATURE_EL3) &&
7696 !(env->cp15.scr_el3 & SCR_ATA)) {
7697 return CP_ACCESS_TRAP_EL3;
7698 }
7699 return CP_ACCESS_OK;
7700}
7701
7702static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7703{
7704 return env->pstate & PSTATE_TCO;
7705}
7706
7707static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7708{
7709 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7710}
7711
7712static const ARMCPRegInfo mte_reginfo[] = {
7713 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7714 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7715 .access = PL1_RW, .accessfn = access_mte,
7716 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7717 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7718 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7719 .access = PL1_RW, .accessfn = access_mte,
7720 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7721 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7722 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7723 .access = PL2_RW, .accessfn = access_mte,
7724 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7725 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7726 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7727 .access = PL3_RW,
7728 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7729 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7730 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7731 .access = PL1_RW, .accessfn = access_mte,
7732 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7733 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7734 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7735 .access = PL1_RW, .accessfn = access_mte,
7736 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
4b779ceb
RH
7737 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7738 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7739 .type = ARM_CP_NO_RAW,
7740 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
5463df16
RH
7741 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7742 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7743 .type = ARM_CP_NOP, .access = PL1_W,
dd345653 7744 .fgt = FGT_DCIVAC,
5463df16
RH
7745 .accessfn = aa64_cacheop_poc_access },
7746 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7747 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
dd345653 7748 .fgt = FGT_DCISW,
5463df16
RH
7749 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7750 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7751 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7752 .type = ARM_CP_NOP, .access = PL1_W,
dd345653 7753 .fgt = FGT_DCIVAC,
5463df16
RH
7754 .accessfn = aa64_cacheop_poc_access },
7755 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7756 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
dd345653 7757 .fgt = FGT_DCISW,
5463df16
RH
7758 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7759 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7760 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
dd345653 7761 .fgt = FGT_DCCSW,
5463df16
RH
7762 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7763 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7764 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
dd345653 7765 .fgt = FGT_DCCSW,
5463df16
RH
7766 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7767 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7768 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
dd345653 7769 .fgt = FGT_DCCISW,
5463df16
RH
7770 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7771 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7772 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
dd345653 7773 .fgt = FGT_DCCISW,
5463df16 7774 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
4b779ceb
RH
7775};
7776
7777static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7778 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7779 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7780 .type = ARM_CP_CONST, .access = PL0_RW, },
4b779ceb 7781};
5463df16
RH
7782
7783static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7784 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7785 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7786 .type = ARM_CP_NOP, .access = PL0_W,
950037e2 7787 .fgt = FGT_DCCVAC,
5463df16
RH
7788 .accessfn = aa64_cacheop_poc_access },
7789 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7790 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7791 .type = ARM_CP_NOP, .access = PL0_W,
950037e2 7792 .fgt = FGT_DCCVAC,
5463df16
RH
7793 .accessfn = aa64_cacheop_poc_access },
7794 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7795 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7796 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7797 .fgt = FGT_DCCVAP,
5463df16
RH
7798 .accessfn = aa64_cacheop_poc_access },
7799 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7800 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7801 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7802 .fgt = FGT_DCCVAP,
5463df16
RH
7803 .accessfn = aa64_cacheop_poc_access },
7804 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7805 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7806 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7807 .fgt = FGT_DCCVADP,
5463df16
RH
7808 .accessfn = aa64_cacheop_poc_access },
7809 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7810 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7811 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7812 .fgt = FGT_DCCVADP,
5463df16
RH
7813 .accessfn = aa64_cacheop_poc_access },
7814 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7815 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7816 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7817 .fgt = FGT_DCCIVAC,
5463df16
RH
7818 .accessfn = aa64_cacheop_poc_access },
7819 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7820 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7821 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7822 .fgt = FGT_DCCIVAC,
5463df16 7823 .accessfn = aa64_cacheop_poc_access },
eb821168
RH
7824 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7825 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7826 .access = PL0_W, .type = ARM_CP_DC_GVA,
7827#ifndef CONFIG_USER_ONLY
7828 /* Avoid overhead of an access check that always passes in user-mode */
7829 .accessfn = aa64_zva_access,
dd345653 7830 .fgt = FGT_DCZVA,
eb821168
RH
7831#endif
7832 },
7833 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7834 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7835 .access = PL0_W, .type = ARM_CP_DC_GZVA,
7836#ifndef CONFIG_USER_ONLY
7837 /* Avoid overhead of an access check that always passes in user-mode */
7838 .accessfn = aa64_zva_access,
dd345653 7839 .fgt = FGT_DCZVA,
eb821168
RH
7840#endif
7841 },
5463df16
RH
7842};
7843
7cb1e618
RH
7844static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7845 bool isread)
7846{
7847 uint64_t hcr = arm_hcr_el2_eff(env);
7848 int el = arm_current_el(env);
7849
7850 if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7851 if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7852 if (hcr & HCR_TGE) {
7853 return CP_ACCESS_TRAP_EL2;
7854 }
7855 return CP_ACCESS_TRAP;
7856 }
7857 } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7858 return CP_ACCESS_TRAP_EL2;
7859 }
7860 if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7861 return CP_ACCESS_TRAP_EL2;
7862 }
7863 if (el < 3
7864 && arm_feature(env, ARM_FEATURE_EL3)
7865 && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7866 return CP_ACCESS_TRAP_EL3;
7867 }
7868 return CP_ACCESS_OK;
7869}
7870
7871static const ARMCPRegInfo scxtnum_reginfo[] = {
7872 { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7873 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7874 .access = PL0_RW, .accessfn = access_scxtnum,
67dd8030 7875 .fgt = FGT_SCXTNUM_EL0,
7cb1e618
RH
7876 .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7877 { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7878 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7879 .access = PL1_RW, .accessfn = access_scxtnum,
67dd8030 7880 .fgt = FGT_SCXTNUM_EL1,
7cb1e618
RH
7881 .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7882 { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7883 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7884 .access = PL2_RW, .accessfn = access_scxtnum,
7885 .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7886 { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7887 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7888 .access = PL3_RW,
7889 .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7890};
15126d9c
PM
7891
7892static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
7893 bool isread)
7894{
7895 if (arm_current_el(env) == 2 &&
7896 arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
7897 return CP_ACCESS_TRAP_EL3;
7898 }
7899 return CP_ACCESS_OK;
7900}
7901
7902static const ARMCPRegInfo fgt_reginfo[] = {
7903 { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7904 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
7905 .access = PL2_RW, .accessfn = access_fgt,
7906 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
7907 { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7908 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
7909 .access = PL2_RW, .accessfn = access_fgt,
7910 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
7911 { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7912 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
7913 .access = PL2_RW, .accessfn = access_fgt,
7914 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
7915 { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7916 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
7917 .access = PL2_RW, .accessfn = access_fgt,
7918 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
7919 { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
7920 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
7921 .access = PL2_RW, .accessfn = access_fgt,
7922 .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
7923};
7cb1e618 7924#endif /* TARGET_AARCH64 */
967aa94f 7925
cb570bd3
RH
7926static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7927 bool isread)
7928{
7929 int el = arm_current_el(env);
7930
7931 if (el == 0) {
7932 uint64_t sctlr = arm_sctlr(env, el);
7933 if (!(sctlr & SCTLR_EnRCTX)) {
7934 return CP_ACCESS_TRAP;
7935 }
7936 } else if (el == 1) {
7937 uint64_t hcr = arm_hcr_el2_eff(env);
7938 if (hcr & HCR_NV) {
7939 return CP_ACCESS_TRAP_EL2;
7940 }
7941 }
7942 return CP_ACCESS_OK;
7943}
7944
7945static const ARMCPRegInfo predinv_reginfo[] = {
7946 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7947 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
950037e2 7948 .fgt = FGT_CFPRCTX,
cb570bd3
RH
7949 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7950 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7951 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
950037e2 7952 .fgt = FGT_DVPRCTX,
cb570bd3
RH
7953 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7954 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7955 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
950037e2 7956 .fgt = FGT_CPPRCTX,
cb570bd3
RH
7957 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7958 /*
7959 * Note the AArch32 opcodes have a different OPC1.
7960 */
7961 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7962 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
950037e2 7963 .fgt = FGT_CFPRCTX,
cb570bd3
RH
7964 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7965 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7966 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
950037e2 7967 .fgt = FGT_DVPRCTX,
cb570bd3
RH
7968 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7969 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7970 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
950037e2 7971 .fgt = FGT_CPPRCTX,
cb570bd3 7972 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
cb570bd3
RH
7973};
7974
957e6155
PM
7975static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7976{
7977 /* Read the high 32 bits of the current CCSIDR */
7978 return extract64(ccsidr_read(env, ri), 32, 32);
7979}
7980
7981static const ARMCPRegInfo ccsidr2_reginfo[] = {
7982 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7983 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7984 .access = PL1_R,
e2ce5fcd 7985 .accessfn = access_tid4,
957e6155 7986 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
957e6155
PM
7987};
7988
6a4ef4e5
MZ
7989static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7990 bool isread)
7991{
7992 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7993 return CP_ACCESS_TRAP_EL2;
7994 }
7995
7996 return CP_ACCESS_OK;
7997}
7998
7999static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8000 bool isread)
8001{
8002 if (arm_feature(env, ARM_FEATURE_V8)) {
8003 return access_aa64_tid3(env, ri, isread);
8004 }
8005
8006 return CP_ACCESS_OK;
8007}
8008
f96f3d5f
MZ
8009static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
8010 bool isread)
8011{
8012 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
8013 return CP_ACCESS_TRAP_EL2;
8014 }
8015
8016 return CP_ACCESS_OK;
8017}
8018
8e228c9e
PM
8019static CPAccessResult access_joscr_jmcr(CPUARMState *env,
8020 const ARMCPRegInfo *ri, bool isread)
8021{
8022 /*
8023 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
8024 * in v7A, not in v8A.
8025 */
8026 if (!arm_feature(env, ARM_FEATURE_V8) &&
8027 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
8028 (env->cp15.hstr_el2 & HSTR_TJDBX)) {
8029 return CP_ACCESS_TRAP_EL2;
8030 }
8031 return CP_ACCESS_OK;
8032}
8033
f96f3d5f
MZ
8034static const ARMCPRegInfo jazelle_regs[] = {
8035 { .name = "JIDR",
8036 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
8037 .access = PL1_R, .accessfn = access_jazelle,
8038 .type = ARM_CP_CONST, .resetvalue = 0 },
8039 { .name = "JOSCR",
8040 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 8041 .accessfn = access_joscr_jmcr,
f96f3d5f
MZ
8042 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8043 { .name = "JMCR",
8044 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 8045 .accessfn = access_joscr_jmcr,
f96f3d5f 8046 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
f96f3d5f
MZ
8047};
8048
52d18727
RH
8049static const ARMCPRegInfo contextidr_el2 = {
8050 .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
8051 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
8052 .access = PL2_RW,
8053 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
8054};
8055
e2a1a461 8056static const ARMCPRegInfo vhe_reginfo[] = {
ed30da8e
RH
8057 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
8058 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
8059 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
587f8b33 8060 .raw_writefn = raw_write,
ed30da8e 8061 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8c94b071
RH
8062#ifndef CONFIG_USER_ONLY
8063 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
8064 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
8065 .fieldoffset =
8066 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
8067 .type = ARM_CP_IO, .access = PL2_RW,
8068 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
8069 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
8070 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
8071 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
8072 .resetfn = gt_hv_timer_reset,
8073 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
8074 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
8075 .type = ARM_CP_IO,
8076 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
8077 .access = PL2_RW,
8078 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
8079 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
bb5972e4
RH
8080 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
8081 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
8082 .type = ARM_CP_IO | ARM_CP_ALIAS,
8083 .access = PL2_RW, .accessfn = e2h_access,
8084 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
8085 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
8086 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
8087 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
8088 .type = ARM_CP_IO | ARM_CP_ALIAS,
8089 .access = PL2_RW, .accessfn = e2h_access,
8090 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
8091 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
8092 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8093 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
8094 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8095 .access = PL2_RW, .accessfn = e2h_access,
8096 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
8097 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8098 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
8099 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8100 .access = PL2_RW, .accessfn = e2h_access,
8101 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
8102 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8103 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
8104 .type = ARM_CP_IO | ARM_CP_ALIAS,
8105 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
8106 .access = PL2_RW, .accessfn = e2h_access,
8107 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
8108 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8109 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
8110 .type = ARM_CP_IO | ARM_CP_ALIAS,
8111 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
8112 .access = PL2_RW, .accessfn = e2h_access,
8113 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8c94b071 8114#endif
e2a1a461
RH
8115};
8116
04b07d29
RH
8117#ifndef CONFIG_USER_ONLY
8118static const ARMCPRegInfo ats1e1_reginfo[] = {
3999d2d2 8119 { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
04b07d29
RH
8120 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8121 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
132c98cd 8122 .fgt = FGT_ATS1E1RP,
1acd00ef 8123 .accessfn = at_e012_access, .writefn = ats_write64 },
3999d2d2 8124 { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
04b07d29
RH
8125 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8126 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
132c98cd 8127 .fgt = FGT_ATS1E1WP,
1acd00ef 8128 .accessfn = at_e012_access, .writefn = ats_write64 },
04b07d29
RH
8129};
8130
8131static const ARMCPRegInfo ats1cp_reginfo[] = {
8132 { .name = "ATS1CPRP",
8133 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8134 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8135 .writefn = ats_write },
8136 { .name = "ATS1CPWP",
8137 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8138 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8139 .writefn = ats_write },
04b07d29
RH
8140};
8141#endif
8142
f6287c24
PM
8143/*
8144 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
8145 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
8146 * is non-zero, which is never for ARMv7, optionally in ARMv8
8147 * and mandatorily for ARMv8.2 and up.
8148 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
8149 * implementation is RAZ/WI we can ignore this detail, as we
8150 * do for ACTLR.
8151 */
8152static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
8153 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
8154 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
99602377
RH
8155 .access = PL1_RW, .accessfn = access_tacr,
8156 .type = ARM_CP_CONST, .resetvalue = 0 },
f6287c24
PM
8157 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
8158 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
8159 .access = PL2_RW, .type = ARM_CP_CONST,
8160 .resetvalue = 0 },
f6287c24
PM
8161};
8162
2ceb98c0
PM
8163void register_cp_regs_for_features(ARMCPU *cpu)
8164{
8165 /* Register all the coprocessor registers based on feature bits */
8166 CPUARMState *env = &cpu->env;
8167 if (arm_feature(env, ARM_FEATURE_M)) {
8168 /* M profile has no coprocessor registers */
8169 return;
8170 }
8171
e9aa6c21 8172 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6 8173 if (!arm_feature(env, ARM_FEATURE_V8)) {
9b37a28c
FR
8174 /*
8175 * Must go early as it is full of wildcards that may be
9449fdf6
PM
8176 * overridden by later definitions.
8177 */
8178 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
8179 }
8180
7d57f408 8181 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
8182 /* The ID registers all have impdef reset values */
8183 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
8184 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
8185 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
8186 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8187 .accessfn = access_aa32_tid3,
8a130a7b 8188 .resetvalue = cpu->isar.id_pfr0 },
9b37a28c
FR
8189 /*
8190 * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
96a8b92e
PM
8191 * the value of the GIC field until after we define these regs.
8192 */
0ff644a7
PM
8193 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
8194 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e 8195 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 8196 .accessfn = access_aa32_tid3,
0f150c84
PMD
8197#ifdef CONFIG_USER_ONLY
8198 .type = ARM_CP_CONST,
8199 .resetvalue = cpu->isar.id_pfr1,
8200#else
8201 .type = ARM_CP_NO_RAW,
8202 .accessfn = access_aa32_tid3,
96a8b92e 8203 .readfn = id_pfr1_read,
0f150c84
PMD
8204 .writefn = arm_cp_write_ignore
8205#endif
8206 },
0ff644a7
PM
8207 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
8208 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
8209 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8210 .accessfn = access_aa32_tid3,
a6179538 8211 .resetvalue = cpu->isar.id_dfr0 },
0ff644a7
PM
8212 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
8213 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
8214 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8215 .accessfn = access_aa32_tid3,
8515a092 8216 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
8217 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
8218 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
8219 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8220 .accessfn = access_aa32_tid3,
10054016 8221 .resetvalue = cpu->isar.id_mmfr0 },
0ff644a7
PM
8222 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
8223 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
8224 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8225 .accessfn = access_aa32_tid3,
10054016 8226 .resetvalue = cpu->isar.id_mmfr1 },
0ff644a7
PM
8227 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
8228 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
8229 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8230 .accessfn = access_aa32_tid3,
10054016 8231 .resetvalue = cpu->isar.id_mmfr2 },
0ff644a7
PM
8232 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
8233 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
8234 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8235 .accessfn = access_aa32_tid3,
10054016 8236 .resetvalue = cpu->isar.id_mmfr3 },
0ff644a7
PM
8237 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
8238 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
8239 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8240 .accessfn = access_aa32_tid3,
47576b94 8241 .resetvalue = cpu->isar.id_isar0 },
0ff644a7
PM
8242 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
8243 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
8244 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8245 .accessfn = access_aa32_tid3,
47576b94 8246 .resetvalue = cpu->isar.id_isar1 },
0ff644a7
PM
8247 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
8248 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
8249 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8250 .accessfn = access_aa32_tid3,
47576b94 8251 .resetvalue = cpu->isar.id_isar2 },
0ff644a7
PM
8252 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
8253 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
8254 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8255 .accessfn = access_aa32_tid3,
47576b94 8256 .resetvalue = cpu->isar.id_isar3 },
0ff644a7
PM
8257 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
8258 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
8259 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8260 .accessfn = access_aa32_tid3,
47576b94 8261 .resetvalue = cpu->isar.id_isar4 },
0ff644a7
PM
8262 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
8263 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
8264 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8265 .accessfn = access_aa32_tid3,
47576b94 8266 .resetvalue = cpu->isar.id_isar5 },
e20d84c1
PM
8267 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
8268 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
8269 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8270 .accessfn = access_aa32_tid3,
10054016 8271 .resetvalue = cpu->isar.id_mmfr4 },
802abf40 8272 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8273 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
8274 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8275 .accessfn = access_aa32_tid3,
47576b94 8276 .resetvalue = cpu->isar.id_isar6 },
8515a092
PM
8277 };
8278 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
8279 define_arm_cp_regs(cpu, v6_cp_reginfo);
8280 } else {
8281 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
8282 }
4d31c596
PM
8283 if (arm_feature(env, ARM_FEATURE_V6K)) {
8284 define_arm_cp_regs(cpu, v6k_cp_reginfo);
8285 }
5e5cf9e3 8286 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 8287 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
8288 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
8289 }
327dd510
AL
8290 if (arm_feature(env, ARM_FEATURE_V7VE)) {
8291 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
8292 }
e9aa6c21 8293 if (arm_feature(env, ARM_FEATURE_V7)) {
776d4e5c 8294 ARMCPRegInfo clidr = {
7da845b0
PM
8295 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
8296 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
630fcd4d 8297 .access = PL1_R, .type = ARM_CP_CONST,
e2ce5fcd 8298 .accessfn = access_tid4,
158c276c 8299 .fgt = FGT_CLIDR_EL1,
630fcd4d 8300 .resetvalue = cpu->clidr
776d4e5c 8301 };
776d4e5c 8302 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 8303 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 8304 define_debug_regs(cpu);
24183fb6 8305 define_pmu_regs(cpu);
7d57f408
PM
8306 } else {
8307 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 8308 }
b0d2b7d0 8309 if (arm_feature(env, ARM_FEATURE_V8)) {
dde4d028
PM
8310 /*
8311 * v8 ID registers, which all have impdef reset values.
e20d84c1
PM
8312 * Note that within the ID register ranges the unused slots
8313 * must all RAZ, not UNDEF; future architecture versions may
8314 * define new registers here.
dde4d028
PM
8315 * ID registers which are AArch64 views of the AArch32 ID registers
8316 * which already existed in v6 and v7 are handled elsewhere,
8317 * in v6_idregs[].
e20d84c1 8318 */
dde4d028 8319 int i;
e60cef86 8320 ARMCPRegInfo v8_idregs[] = {
976b99b6
AB
8321 /*
8322 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
8323 * emulation because we don't know the right value for the
8324 * GIC field until after we define these regs.
96a8b92e 8325 */
e60cef86
PM
8326 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
8327 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
976b99b6
AB
8328 .access = PL1_R,
8329#ifdef CONFIG_USER_ONLY
8330 .type = ARM_CP_CONST,
8331 .resetvalue = cpu->isar.id_aa64pfr0
8332#else
8333 .type = ARM_CP_NO_RAW,
6a4ef4e5 8334 .accessfn = access_aa64_tid3,
96a8b92e 8335 .readfn = id_aa64pfr0_read,
976b99b6
AB
8336 .writefn = arm_cp_write_ignore
8337#endif
8338 },
e60cef86
PM
8339 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
8340 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
8341 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8342 .accessfn = access_aa64_tid3,
47576b94 8343 .resetvalue = cpu->isar.id_aa64pfr1},
e20d84c1
PM
8344 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8345 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
8346 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8347 .accessfn = access_aa64_tid3,
e20d84c1
PM
8348 .resetvalue = 0 },
8349 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8350 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
8351 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8352 .accessfn = access_aa64_tid3,
e20d84c1 8353 .resetvalue = 0 },
9516d772 8354 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
8355 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
8356 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8357 .accessfn = access_aa64_tid3,
2dc10fa2 8358 .resetvalue = cpu->isar.id_aa64zfr0 },
414c54d5 8359 { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
8360 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
8361 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8362 .accessfn = access_aa64_tid3,
414c54d5 8363 .resetvalue = cpu->isar.id_aa64smfr0 },
e20d84c1
PM
8364 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8365 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
8366 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8367 .accessfn = access_aa64_tid3,
e20d84c1
PM
8368 .resetvalue = 0 },
8369 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8370 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
8371 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8372 .accessfn = access_aa64_tid3,
e20d84c1 8373 .resetvalue = 0 },
e60cef86
PM
8374 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
8375 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
8376 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8377 .accessfn = access_aa64_tid3,
2a609df8 8378 .resetvalue = cpu->isar.id_aa64dfr0 },
e60cef86
PM
8379 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
8380 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
8381 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8382 .accessfn = access_aa64_tid3,
2a609df8 8383 .resetvalue = cpu->isar.id_aa64dfr1 },
e20d84c1
PM
8384 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8385 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
8386 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8387 .accessfn = access_aa64_tid3,
e20d84c1
PM
8388 .resetvalue = 0 },
8389 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8390 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
8391 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8392 .accessfn = access_aa64_tid3,
e20d84c1 8393 .resetvalue = 0 },
e60cef86
PM
8394 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
8395 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
8396 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8397 .accessfn = access_aa64_tid3,
e60cef86
PM
8398 .resetvalue = cpu->id_aa64afr0 },
8399 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
8400 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
8401 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8402 .accessfn = access_aa64_tid3,
e60cef86 8403 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
8404 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8405 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
8406 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8407 .accessfn = access_aa64_tid3,
e20d84c1
PM
8408 .resetvalue = 0 },
8409 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8410 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8411 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8412 .accessfn = access_aa64_tid3,
e20d84c1 8413 .resetvalue = 0 },
e60cef86
PM
8414 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8415 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8416 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8417 .accessfn = access_aa64_tid3,
47576b94 8418 .resetvalue = cpu->isar.id_aa64isar0 },
e60cef86
PM
8419 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8420 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8421 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8422 .accessfn = access_aa64_tid3,
47576b94 8423 .resetvalue = cpu->isar.id_aa64isar1 },
a969fe97 8424 { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
8425 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8426 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8427 .accessfn = access_aa64_tid3,
a969fe97 8428 .resetvalue = cpu->isar.id_aa64isar2 },
e20d84c1
PM
8429 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8430 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8431 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8432 .accessfn = access_aa64_tid3,
e20d84c1
PM
8433 .resetvalue = 0 },
8434 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8435 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8436 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8437 .accessfn = access_aa64_tid3,
e20d84c1
PM
8438 .resetvalue = 0 },
8439 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8440 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8441 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8442 .accessfn = access_aa64_tid3,
e20d84c1
PM
8443 .resetvalue = 0 },
8444 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8445 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8446 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8447 .accessfn = access_aa64_tid3,
e20d84c1
PM
8448 .resetvalue = 0 },
8449 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8450 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8451 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8452 .accessfn = access_aa64_tid3,
e20d84c1 8453 .resetvalue = 0 },
e60cef86
PM
8454 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8455 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8456 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8457 .accessfn = access_aa64_tid3,
3dc91ddb 8458 .resetvalue = cpu->isar.id_aa64mmfr0 },
e60cef86
PM
8459 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8460 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8461 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8462 .accessfn = access_aa64_tid3,
3dc91ddb 8463 .resetvalue = cpu->isar.id_aa64mmfr1 },
64761e10 8464 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
8465 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8466 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8467 .accessfn = access_aa64_tid3,
64761e10 8468 .resetvalue = cpu->isar.id_aa64mmfr2 },
e20d84c1
PM
8469 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8470 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8471 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8472 .accessfn = access_aa64_tid3,
e20d84c1
PM
8473 .resetvalue = 0 },
8474 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8475 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8476 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8477 .accessfn = access_aa64_tid3,
e20d84c1
PM
8478 .resetvalue = 0 },
8479 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8480 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8481 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8482 .accessfn = access_aa64_tid3,
e20d84c1
PM
8483 .resetvalue = 0 },
8484 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8485 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8486 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8487 .accessfn = access_aa64_tid3,
e20d84c1
PM
8488 .resetvalue = 0 },
8489 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8490 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8491 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8492 .accessfn = access_aa64_tid3,
e20d84c1 8493 .resetvalue = 0 },
a50c0f51
PM
8494 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8495 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8496 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8497 .accessfn = access_aa64_tid3,
47576b94 8498 .resetvalue = cpu->isar.mvfr0 },
a50c0f51
PM
8499 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8500 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8501 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8502 .accessfn = access_aa64_tid3,
47576b94 8503 .resetvalue = cpu->isar.mvfr1 },
a50c0f51
PM
8504 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8505 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8506 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8507 .accessfn = access_aa64_tid3,
47576b94 8508 .resetvalue = cpu->isar.mvfr2 },
dde4d028
PM
8509 /*
8510 * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8511 * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8512 * as RAZ, since it is in the "reserved for future ID
8513 * registers, RAZ" part of the AArch32 encoding space.
8514 */
8515 { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8516 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8517 .access = PL1_R, .type = ARM_CP_CONST,
8518 .accessfn = access_aa64_tid3,
8519 .resetvalue = 0 },
8520 { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8521 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8522 .access = PL1_R, .type = ARM_CP_CONST,
8523 .accessfn = access_aa64_tid3,
8524 .resetvalue = 0 },
8525 { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8526 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8527 .access = PL1_R, .type = ARM_CP_CONST,
8528 .accessfn = access_aa64_tid3,
8529 .resetvalue = 0 },
8530 /*
8531 * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8532 * they're also RAZ for AArch64, and in v8 are gradually
8533 * being filled with AArch64-view-of-AArch32-ID-register
8534 * for new ID registers.
8535 */
8536 { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8537 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8538 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8539 .accessfn = access_aa64_tid3,
e20d84c1 8540 .resetvalue = 0 },
1d51bc96 8541 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8542 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8543 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8544 .accessfn = access_aa64_tid3,
1d51bc96 8545 .resetvalue = cpu->isar.id_pfr2 },
d22c5649 8546 { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8547 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8548 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8549 .accessfn = access_aa64_tid3,
d22c5649 8550 .resetvalue = cpu->isar.id_dfr1 },
32957aad 8551 { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8552 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8553 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8554 .accessfn = access_aa64_tid3,
32957aad 8555 .resetvalue = cpu->isar.id_mmfr5 },
dde4d028 8556 { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8557 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8558 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8559 .accessfn = access_aa64_tid3,
e20d84c1 8560 .resetvalue = 0 },
4054bfa9
AF
8561 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8562 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8563 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 8564 .fgt = FGT_PMCEIDN_EL0,
cad86737 8565 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
4054bfa9
AF
8566 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8567 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8568 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 8569 .fgt = FGT_PMCEIDN_EL0,
4054bfa9
AF
8570 .resetvalue = cpu->pmceid0 },
8571 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8572 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8573 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 8574 .fgt = FGT_PMCEIDN_EL0,
cad86737 8575 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
4054bfa9
AF
8576 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8577 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8578 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 8579 .fgt = FGT_PMCEIDN_EL0,
4054bfa9 8580 .resetvalue = cpu->pmceid1 },
e60cef86 8581 };
6c5c0fec 8582#ifdef CONFIG_USER_ONLY
10b0220e 8583 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
6c5c0fec 8584 { .name = "ID_AA64PFR0_EL1",
bc6bd20e
ZS
8585 .exported_bits = R_ID_AA64PFR0_FP_MASK |
8586 R_ID_AA64PFR0_ADVSIMD_MASK |
8587 R_ID_AA64PFR0_SVE_MASK |
8588 R_ID_AA64PFR0_DIT_MASK,
8589 .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8590 (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
6c5c0fec 8591 { .name = "ID_AA64PFR1_EL1",
bc6bd20e
ZS
8592 .exported_bits = R_ID_AA64PFR1_BT_MASK |
8593 R_ID_AA64PFR1_SSBS_MASK |
8594 R_ID_AA64PFR1_MTE_MASK |
8595 R_ID_AA64PFR1_SME_MASK },
d040242e 8596 { .name = "ID_AA64PFR*_EL1_RESERVED",
bc6bd20e
ZS
8597 .is_glob = true },
8598 { .name = "ID_AA64ZFR0_EL1",
8599 .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8600 R_ID_AA64ZFR0_AES_MASK |
8601 R_ID_AA64ZFR0_BITPERM_MASK |
8602 R_ID_AA64ZFR0_BFLOAT16_MASK |
8603 R_ID_AA64ZFR0_SHA3_MASK |
8604 R_ID_AA64ZFR0_SM4_MASK |
8605 R_ID_AA64ZFR0_I8MM_MASK |
8606 R_ID_AA64ZFR0_F32MM_MASK |
8607 R_ID_AA64ZFR0_F64MM_MASK },
8608 { .name = "ID_AA64SMFR0_EL1",
8609 .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
5f7b71fb 8610 R_ID_AA64SMFR0_BI32I32_MASK |
bc6bd20e
ZS
8611 R_ID_AA64SMFR0_B16F32_MASK |
8612 R_ID_AA64SMFR0_F16F32_MASK |
8613 R_ID_AA64SMFR0_I8I32_MASK |
5f7b71fb
PM
8614 R_ID_AA64SMFR0_F16F16_MASK |
8615 R_ID_AA64SMFR0_B16B16_MASK |
8616 R_ID_AA64SMFR0_I16I32_MASK |
bc6bd20e
ZS
8617 R_ID_AA64SMFR0_F64F64_MASK |
8618 R_ID_AA64SMFR0_I16I64_MASK |
5f7b71fb 8619 R_ID_AA64SMFR0_SMEVER_MASK |
bc6bd20e 8620 R_ID_AA64SMFR0_FA64_MASK },
6c5c0fec 8621 { .name = "ID_AA64MMFR0_EL1",
bc6bd20e
ZS
8622 .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8623 .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8624 (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8625 { .name = "ID_AA64MMFR1_EL1",
8626 .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8627 { .name = "ID_AA64MMFR2_EL1",
8628 .exported_bits = R_ID_AA64MMFR2_AT_MASK },
d040242e 8629 { .name = "ID_AA64MMFR*_EL1_RESERVED",
bc6bd20e 8630 .is_glob = true },
6c5c0fec 8631 { .name = "ID_AA64DFR0_EL1",
bc6bd20e
ZS
8632 .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8633 { .name = "ID_AA64DFR1_EL1" },
d040242e 8634 { .name = "ID_AA64DFR*_EL1_RESERVED",
bc6bd20e 8635 .is_glob = true },
d040242e 8636 { .name = "ID_AA64AFR*",
bc6bd20e 8637 .is_glob = true },
6c5c0fec 8638 { .name = "ID_AA64ISAR0_EL1",
bc6bd20e
ZS
8639 .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8640 R_ID_AA64ISAR0_SHA1_MASK |
8641 R_ID_AA64ISAR0_SHA2_MASK |
8642 R_ID_AA64ISAR0_CRC32_MASK |
8643 R_ID_AA64ISAR0_ATOMIC_MASK |
8644 R_ID_AA64ISAR0_RDM_MASK |
8645 R_ID_AA64ISAR0_SHA3_MASK |
8646 R_ID_AA64ISAR0_SM3_MASK |
8647 R_ID_AA64ISAR0_SM4_MASK |
8648 R_ID_AA64ISAR0_DP_MASK |
8649 R_ID_AA64ISAR0_FHM_MASK |
8650 R_ID_AA64ISAR0_TS_MASK |
8651 R_ID_AA64ISAR0_RNDR_MASK },
6c5c0fec 8652 { .name = "ID_AA64ISAR1_EL1",
bc6bd20e
ZS
8653 .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8654 R_ID_AA64ISAR1_APA_MASK |
8655 R_ID_AA64ISAR1_API_MASK |
8656 R_ID_AA64ISAR1_JSCVT_MASK |
8657 R_ID_AA64ISAR1_FCMA_MASK |
8658 R_ID_AA64ISAR1_LRCPC_MASK |
8659 R_ID_AA64ISAR1_GPA_MASK |
8660 R_ID_AA64ISAR1_GPI_MASK |
8661 R_ID_AA64ISAR1_FRINTTS_MASK |
8662 R_ID_AA64ISAR1_SB_MASK |
8663 R_ID_AA64ISAR1_BF16_MASK |
8664 R_ID_AA64ISAR1_DGH_MASK |
8665 R_ID_AA64ISAR1_I8MM_MASK },
8666 { .name = "ID_AA64ISAR2_EL1",
8667 .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8668 R_ID_AA64ISAR2_RPRES_MASK |
8669 R_ID_AA64ISAR2_GPA3_MASK |
5f7b71fb
PM
8670 R_ID_AA64ISAR2_APA3_MASK |
8671 R_ID_AA64ISAR2_MOPS_MASK |
8672 R_ID_AA64ISAR2_BC_MASK |
8673 R_ID_AA64ISAR2_RPRFM_MASK |
8674 R_ID_AA64ISAR2_CSSC_MASK },
d040242e 8675 { .name = "ID_AA64ISAR*_EL1_RESERVED",
bc6bd20e 8676 .is_glob = true },
6c5c0fec
AB
8677 };
8678 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8679#endif
97198a7d
RH
8680 /*
8681 * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
8682 * TODO: For RMR, a write with bit 1 set should do something with
8683 * cpu_reset(). In the meantime, "the bit is strictly a request",
8684 * so we are in spec just ignoring writes.
8685 */
be8e8128
GB
8686 if (!arm_feature(env, ARM_FEATURE_EL3) &&
8687 !arm_feature(env, ARM_FEATURE_EL2)) {
97198a7d
RH
8688 ARMCPRegInfo el1_reset_regs[] = {
8689 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
8690 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8691 .access = PL1_R,
8692 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8693 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
8694 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8695 .access = PL1_RW, .type = ARM_CP_CONST,
8696 .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
be8e8128 8697 };
97198a7d 8698 define_arm_cp_regs(cpu, el1_reset_regs);
be8e8128 8699 }
e60cef86 8700 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0 8701 define_arm_cp_regs(cpu, v8_cp_reginfo);
dde4d028
PM
8702
8703 for (i = 4; i < 16; i++) {
8704 /*
8705 * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8706 * For pre-v8 cores there are RAZ patterns for these in
8707 * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8708 * v8 extends the "must RAZ" part of the ID register space
8709 * to also cover c0, 0, c{8-15}, {0-7}.
8710 * These are STATE_AA32 because in the AArch64 sysreg space
8711 * c4-c7 is where the AArch64 ID registers live (and we've
8712 * already defined those in v8_idregs[]), and c8-c15 are not
8713 * "must RAZ" for AArch64.
8714 */
8715 g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8716 ARMCPRegInfo v8_aa32_raz_idregs = {
8717 .name = name,
8718 .state = ARM_CP_STATE_AA32,
8719 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8720 .access = PL1_R, .type = ARM_CP_CONST,
8721 .accessfn = access_aa64_tid3,
8722 .resetvalue = 0 };
8723 define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8724 }
b0d2b7d0 8725 }
99a90811
RH
8726
8727 /*
8728 * Register the base EL2 cpregs.
8729 * Pre v8, these registers are implemented only as part of the
8730 * Virtualization Extensions (EL2 present). Beginning with v8,
8731 * if EL2 is missing but EL3 is enabled, mostly these become
8732 * RES0 from EL3, with some specific exceptions.
8733 */
8734 if (arm_feature(env, ARM_FEATURE_EL2)
8735 || (arm_feature(env, ARM_FEATURE_EL3)
8736 && arm_feature(env, ARM_FEATURE_V8))) {
f0d574d6 8737 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
8738 ARMCPRegInfo vpidr_regs[] = {
8739 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8740 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8741 .access = PL2_RW, .accessfn = access_el3_aa32ns,
696ba377
RH
8742 .resetvalue = cpu->midr,
8743 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
36476562 8744 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
8745 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8746 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8747 .access = PL2_RW, .resetvalue = cpu->midr,
696ba377 8748 .type = ARM_CP_EL3_NO_EL2_C_NZ,
731de9e6 8749 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
8750 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8751 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8752 .access = PL2_RW, .accessfn = access_el3_aa32ns,
696ba377
RH
8753 .resetvalue = vmpidr_def,
8754 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
36476562 8755 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
8756 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8757 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
696ba377
RH
8758 .access = PL2_RW, .resetvalue = vmpidr_def,
8759 .type = ARM_CP_EL3_NO_EL2_C_NZ,
f0d574d6 8760 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6 8761 };
24526bb9
PM
8762 /*
8763 * The only field of MDCR_EL2 that has a defined architectural reset
8764 * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8765 */
8766 ARMCPRegInfo mdcr_el2 = {
7f4fbfb5 8767 .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
24526bb9 8768 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
01765386 8769 .writefn = mdcr_el2_write,
24526bb9
PM
8770 .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8771 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8772 };
8773 define_one_arm_cp_reg(cpu, &mdcr_el2);
731de9e6 8774 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 8775 define_arm_cp_regs(cpu, el2_cp_reginfo);
ce4afed8
PM
8776 if (arm_feature(env, ARM_FEATURE_V8)) {
8777 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8778 }
e9152ee9
RDC
8779 if (cpu_isar_feature(aa64_sel2, cpu)) {
8780 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8781 }
97198a7d
RH
8782 /*
8783 * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
8784 * See commentary near RMR_EL1.
8785 */
be8e8128 8786 if (!arm_feature(env, ARM_FEATURE_EL3)) {
97198a7d
RH
8787 static const ARMCPRegInfo el2_reset_regs[] = {
8788 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8789 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8790 .access = PL2_R,
8791 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8792 { .name = "RVBAR", .type = ARM_CP_ALIAS,
8793 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8794 .access = PL2_R,
8795 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8796 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
8797 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
8798 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
be8e8128 8799 };
97198a7d 8800 define_arm_cp_regs(cpu, el2_reset_regs);
be8e8128 8801 }
3b685ba7 8802 }
99a90811
RH
8803
8804 /* Register the base EL3 cpregs. */
81547d66 8805 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 8806 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
8807 ARMCPRegInfo el3_regs[] = {
8808 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8809 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7 8810 .access = PL3_R,
97198a7d
RH
8811 .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
8812 { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
8813 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
8814 .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8815 { .name = "RMR", .state = ARM_CP_STATE_AA32,
8816 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8817 .access = PL3_RW, .type = ARM_CP_CONST,
8818 .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
e24fdd23
PM
8819 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8820 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8821 .access = PL3_RW,
8822 .raw_writefn = raw_write, .writefn = sctlr_write,
8823 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8824 .resetvalue = cpu->reset_sctlr },
be8e8128 8825 };
e24fdd23
PM
8826
8827 define_arm_cp_regs(cpu, el3_regs);
81547d66 8828 }
9b37a28c
FR
8829 /*
8830 * The behaviour of NSACR is sufficiently various that we don't
2f027fc5
PM
8831 * try to describe it in a single reginfo:
8832 * if EL3 is 64 bit, then trap to EL3 from S EL1,
8833 * reads as constant 0xc00 from NS EL1 and NS EL2
8834 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8835 * if v7 without EL3, register doesn't exist
8836 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8837 */
8838 if (arm_feature(env, ARM_FEATURE_EL3)) {
8839 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
10b0220e 8840 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8841 .name = "NSACR", .type = ARM_CP_CONST,
8842 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8843 .access = PL1_RW, .accessfn = nsacr_access,
8844 .resetvalue = 0xc00
8845 };
8846 define_one_arm_cp_reg(cpu, &nsacr);
8847 } else {
10b0220e 8848 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8849 .name = "NSACR",
8850 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8851 .access = PL3_RW | PL1_R,
8852 .resetvalue = 0,
8853 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8854 };
8855 define_one_arm_cp_reg(cpu, &nsacr);
8856 }
8857 } else {
8858 if (arm_feature(env, ARM_FEATURE_V8)) {
10b0220e 8859 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8860 .name = "NSACR", .type = ARM_CP_CONST,
8861 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8862 .access = PL1_R,
8863 .resetvalue = 0xc00
8864 };
8865 define_one_arm_cp_reg(cpu, &nsacr);
8866 }
8867 }
8868
452a0955 8869 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
8870 if (arm_feature(env, ARM_FEATURE_V6)) {
8871 /* PMSAv6 not implemented */
8872 assert(arm_feature(env, ARM_FEATURE_V7));
8873 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8874 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8875 } else {
8876 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8877 }
18032bec 8878 } else {
8e5d75c9 8879 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec 8880 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4036b7d1
PM
8881 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
8882 if (cpu_isar_feature(aa32_hpd, cpu)) {
ab638a32
RH
8883 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8884 }
18032bec 8885 }
c326b979
PM
8886 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8887 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8888 }
6cc7a3ae
PM
8889 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8890 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8891 }
4a501606 8892 if (arm_feature(env, ARM_FEATURE_VAPA)) {
8ce4d441
AB
8893 ARMCPRegInfo vapa_cp_reginfo[] = {
8894 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
8895 .access = PL1_RW, .resetvalue = 0,
8896 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
8897 offsetoflow32(CPUARMState, cp15.par_ns) },
8898 .writefn = par_write},
8899#ifndef CONFIG_USER_ONLY
8900 /* This underdecoding is safe because the reginfo is NO_RAW. */
8901 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
8902 .access = PL1_W, .accessfn = ats_access,
8903 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
8904#endif
8905 };
8906
8907 /*
8908 * When LPAE exists this 32-bit PAR register is an alias of the
8909 * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
8910 */
8911 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8912 vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
8913 }
4a501606
PM
8914 define_arm_cp_regs(cpu, vapa_cp_reginfo);
8915 }
c4804214
PM
8916 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8917 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8918 }
8919 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8920 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8921 }
8922 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8923 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8924 }
18032bec
PM
8925 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8926 define_arm_cp_regs(cpu, omap_cp_reginfo);
8927 }
34f90529
PM
8928 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8929 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8930 }
1047b9d7
PM
8931 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8932 define_arm_cp_regs(cpu, xscale_cp_reginfo);
8933 }
8934 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8935 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8936 }
7ac681cf
PM
8937 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8938 define_arm_cp_regs(cpu, lpae_cp_reginfo);
8939 }
873b73c0 8940 if (cpu_isar_feature(aa32_jazelle, cpu)) {
f96f3d5f
MZ
8941 define_arm_cp_regs(cpu, jazelle_regs);
8942 }
9b37a28c
FR
8943 /*
8944 * Slightly awkwardly, the OMAP and StrongARM cores need all of
7884849c
PM
8945 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8946 * be read-only (ie write causes UNDEF exception).
8947 */
8948 {
00a29f3d 8949 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
9b37a28c
FR
8950 /*
8951 * Pre-v8 MIDR space.
00a29f3d 8952 * Note that the MIDR isn't a simple constant register because
7884849c
PM
8953 * of the TI925 behaviour where writes to another register can
8954 * cause the MIDR value to change.
97ce8d61
PC
8955 *
8956 * Unimplemented registers in the c15 0 0 0 space default to
8957 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8958 * and friends override accordingly.
7884849c
PM
8959 */
8960 { .name = "MIDR",
97ce8d61 8961 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 8962 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 8963 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 8964 .readfn = midr_read,
97ce8d61
PC
8965 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8966 .type = ARM_CP_OVERRIDE },
7884849c
PM
8967 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8968 { .name = "DUMMY",
8969 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8970 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8971 { .name = "DUMMY",
8972 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8973 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8974 { .name = "DUMMY",
8975 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8976 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8977 { .name = "DUMMY",
8978 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8979 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8980 { .name = "DUMMY",
8981 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8982 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7884849c 8983 };
00a29f3d 8984 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
8985 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8986 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6 8987 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
67dd8030 8988 .fgt = FGT_MIDR_EL1,
731de9e6
EI
8989 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8990 .readfn = midr_read },
c7f786ab 8991 /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
ac00c79f
SF
8992 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8993 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8994 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
8995 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8996 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
93fbc983
MZ
8997 .access = PL1_R,
8998 .accessfn = access_aa64_tid1,
67dd8030 8999 .fgt = FGT_REVIDR_EL1,
93fbc983 9000 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d 9001 };
c7f786ab 9002 ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
acd8e83a 9003 .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
c7f786ab
TR
9004 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9005 .access = PL1_R, .resetvalue = cpu->midr
9006 };
00a29f3d
PM
9007 ARMCPRegInfo id_cp_reginfo[] = {
9008 /* These are common to v8 and pre-v8 */
9009 { .name = "CTR",
9010 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
630fcd4d
MZ
9011 .access = PL1_R, .accessfn = ctr_el0_access,
9012 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
00a29f3d
PM
9013 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
9014 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
9015 .access = PL0_R, .accessfn = ctr_el0_access,
b19ed03c 9016 .fgt = FGT_CTR_EL0,
00a29f3d
PM
9017 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9018 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
9019 { .name = "TCMTR",
9020 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
93fbc983
MZ
9021 .access = PL1_R,
9022 .accessfn = access_aa32_tid1,
9023 .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d 9024 };
8085ce63
PC
9025 /* TLBTR is specific to VMSA */
9026 ARMCPRegInfo id_tlbtr_reginfo = {
9027 .name = "TLBTR",
9028 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
93fbc983
MZ
9029 .access = PL1_R,
9030 .accessfn = access_aa32_tid1,
9031 .type = ARM_CP_CONST, .resetvalue = 0,
8085ce63 9032 };
3281af81
PC
9033 /* MPUIR is specific to PMSA V6+ */
9034 ARMCPRegInfo id_mpuir_reginfo = {
9035 .name = "MPUIR",
9036 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9037 .access = PL1_R, .type = ARM_CP_CONST,
9038 .resetvalue = cpu->pmsav7_dregion << 8
9039 };
761c4642
TR
9040 /* HMPUIR is specific to PMSA V8 */
9041 ARMCPRegInfo id_hmpuir_reginfo = {
9042 .name = "HMPUIR",
9043 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
9044 .access = PL2_R, .type = ARM_CP_CONST,
9045 .resetvalue = cpu->pmsav8r_hdregion
9046 };
10b0220e 9047 static const ARMCPRegInfo crn0_wi_reginfo = {
7884849c
PM
9048 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
9049 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
9050 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
9051 };
6c5c0fec 9052#ifdef CONFIG_USER_ONLY
10b0220e 9053 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
6c5c0fec 9054 { .name = "MIDR_EL1",
bc6bd20e
ZS
9055 .exported_bits = R_MIDR_EL1_REVISION_MASK |
9056 R_MIDR_EL1_PARTNUM_MASK |
9057 R_MIDR_EL1_ARCHITECTURE_MASK |
9058 R_MIDR_EL1_VARIANT_MASK |
9059 R_MIDR_EL1_IMPLEMENTER_MASK },
9060 { .name = "REVIDR_EL1" },
6c5c0fec
AB
9061 };
9062 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
9063#endif
7884849c
PM
9064 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
9065 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5809ac57 9066 size_t i;
9b37a28c
FR
9067 /*
9068 * Register the blanket "writes ignored" value first to cover the
a703eda1
PC
9069 * whole space. Then update the specific ID registers to allow write
9070 * access, so that they ignore writes rather than causing them to
9071 * UNDEF.
7884849c
PM
9072 */
9073 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
5809ac57
RH
9074 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
9075 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
00a29f3d 9076 }
5809ac57
RH
9077 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
9078 id_cp_reginfo[i].access = PL1_RW;
7884849c 9079 }
10006112 9080 id_mpuir_reginfo.access = PL1_RW;
3281af81 9081 id_tlbtr_reginfo.access = PL1_RW;
7884849c 9082 }
00a29f3d
PM
9083 if (arm_feature(env, ARM_FEATURE_V8)) {
9084 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
c7f786ab
TR
9085 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9086 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
9087 }
00a29f3d
PM
9088 } else {
9089 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
9090 }
a703eda1 9091 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 9092 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 9093 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
761c4642
TR
9094 } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
9095 arm_feature(env, ARM_FEATURE_V8)) {
9096 uint32_t i = 0;
9097 char *tmp_string;
9098
9099 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9100 define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
9101 define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
9102
9103 /* Register alias is only valid for first 32 indexes */
9104 for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
9105 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9106 uint8_t opc1 = extract32(i, 4, 1);
9107 uint8_t opc2 = extract32(i, 0, 1) << 2;
9108
9109 tmp_string = g_strdup_printf("PRBAR%u", i);
9110 ARMCPRegInfo tmp_prbarn_reginfo = {
9111 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9112 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9113 .access = PL1_RW, .resetvalue = 0,
9114 .accessfn = access_tvm_trvm,
9115 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9116 };
9117 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
9118 g_free(tmp_string);
9119
9120 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9121 tmp_string = g_strdup_printf("PRLAR%u", i);
9122 ARMCPRegInfo tmp_prlarn_reginfo = {
9123 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9124 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9125 .access = PL1_RW, .resetvalue = 0,
9126 .accessfn = access_tvm_trvm,
9127 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9128 };
9129 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
9130 g_free(tmp_string);
9131 }
9132
9133 /* Register alias is only valid for first 32 indexes */
9134 for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
9135 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9136 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
9137 uint8_t opc2 = extract32(i, 0, 1) << 2;
9138
9139 tmp_string = g_strdup_printf("HPRBAR%u", i);
9140 ARMCPRegInfo tmp_hprbarn_reginfo = {
9141 .name = tmp_string,
9142 .type = ARM_CP_NO_RAW,
9143 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9144 .access = PL2_RW, .resetvalue = 0,
9145 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9146 };
9147 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
9148 g_free(tmp_string);
9149
9150 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9151 tmp_string = g_strdup_printf("HPRLAR%u", i);
9152 ARMCPRegInfo tmp_hprlarn_reginfo = {
9153 .name = tmp_string,
9154 .type = ARM_CP_NO_RAW,
9155 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9156 .access = PL2_RW, .resetvalue = 0,
9157 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9158 };
9159 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
9160 g_free(tmp_string);
9161 }
3281af81
PC
9162 } else if (arm_feature(env, ARM_FEATURE_V7)) {
9163 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 9164 }
7884849c
PM
9165 }
9166
97ce8d61 9167 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
52264166
AB
9168 ARMCPRegInfo mpidr_cp_reginfo[] = {
9169 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
9170 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
67dd8030 9171 .fgt = FGT_MPIDR_EL1,
52264166 9172 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
52264166
AB
9173 };
9174#ifdef CONFIG_USER_ONLY
10b0220e 9175 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
52264166
AB
9176 { .name = "MPIDR_EL1",
9177 .fixed_bits = 0x0000000080000000 },
52264166
AB
9178 };
9179 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
9180#endif
97ce8d61
PC
9181 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
9182 }
9183
2771db27 9184 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
9185 ARMCPRegInfo auxcr_reginfo[] = {
9186 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
9187 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
99602377
RH
9188 .access = PL1_RW, .accessfn = access_tacr,
9189 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
834a6c69
PM
9190 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
9191 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
9192 .access = PL2_RW, .type = ARM_CP_CONST,
9193 .resetvalue = 0 },
9194 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
9195 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
9196 .access = PL3_RW, .type = ARM_CP_CONST,
9197 .resetvalue = 0 },
2771db27 9198 };
834a6c69 9199 define_arm_cp_regs(cpu, auxcr_reginfo);
f6287c24
PM
9200 if (cpu_isar_feature(aa32_ac2, cpu)) {
9201 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
0e0456ab 9202 }
2771db27
PM
9203 }
9204
d8ba780b 9205 if (arm_feature(env, ARM_FEATURE_CBAR)) {
d56974af
LM
9206 /*
9207 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
9208 * There are two flavours:
9209 * (1) older 32-bit only cores have a simple 32-bit CBAR
9210 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
9211 * 32-bit register visible to AArch32 at a different encoding
9212 * to the "flavour 1" register and with the bits rearranged to
9213 * be able to squash a 64-bit address into the 32-bit view.
9214 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
9215 * in future if we support AArch32-only configs of some of the
9216 * AArch64 cores we might need to add a specific feature flag
9217 * to indicate cores with "flavour 2" CBAR.
9218 */
f318cec6
PM
9219 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
9220 /* 32 bit view is [31:18] 0...0 [43:32]. */
9221 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
9222 | extract64(cpu->reset_cbar, 32, 12);
9223 ARMCPRegInfo cbar_reginfo[] = {
9224 { .name = "CBAR",
9225 .type = ARM_CP_CONST,
d56974af
LM
9226 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
9227 .access = PL1_R, .resetvalue = cbar32 },
f318cec6
PM
9228 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
9229 .type = ARM_CP_CONST,
9230 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
d56974af 9231 .access = PL1_R, .resetvalue = cpu->reset_cbar },
f318cec6
PM
9232 };
9233 /* We don't implement a r/w 64 bit CBAR currently */
9234 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
9235 define_arm_cp_regs(cpu, cbar_reginfo);
9236 } else {
9237 ARMCPRegInfo cbar = {
9238 .name = "CBAR",
9239 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
04215eb1 9240 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
f318cec6
PM
9241 .fieldoffset = offsetof(CPUARMState,
9242 cp15.c15_config_base_address)
9243 };
9244 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
9245 cbar.access = PL1_R;
9246 cbar.fieldoffset = 0;
9247 cbar.type = ARM_CP_CONST;
9248 }
9249 define_one_arm_cp_reg(cpu, &cbar);
9250 }
d8ba780b
PC
9251 }
9252
91db4642 9253 if (arm_feature(env, ARM_FEATURE_VBAR)) {
10b0220e 9254 static const ARMCPRegInfo vbar_cp_reginfo[] = {
91db4642
CLG
9255 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
9256 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
9257 .access = PL1_RW, .writefn = vbar_write,
bd8db7d9 9258 .fgt = FGT_VBAR_EL1,
91db4642
CLG
9259 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
9260 offsetof(CPUARMState, cp15.vbar_ns) },
9261 .resetvalue = 0 },
91db4642
CLG
9262 };
9263 define_arm_cp_regs(cpu, vbar_cp_reginfo);
9264 }
9265
2771db27
PM
9266 /* Generic registers whose values depend on the implementation */
9267 {
9268 ARMCPRegInfo sctlr = {
5ebafdf3 9269 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9 9270 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
84929218 9271 .access = PL1_RW, .accessfn = access_tvm_trvm,
67dd8030 9272 .fgt = FGT_SCTLR_EL1,
137feaa9
FA
9273 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
9274 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
9275 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
9276 .raw_writefn = raw_write,
2771db27
PM
9277 };
9278 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9b37a28c
FR
9279 /*
9280 * Normally we would always end the TB on an SCTLR write, but Linux
2771db27
PM
9281 * arch/arm/mach-pxa/sleep.S expects two instructions following
9282 * an MMU enable to execute from cache. Imitate this behaviour.
9283 */
9284 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
9285 }
9286 define_one_arm_cp_reg(cpu, &sctlr);
761c4642
TR
9287
9288 if (arm_feature(env, ARM_FEATURE_PMSA) &&
9289 arm_feature(env, ARM_FEATURE_V8)) {
9290 ARMCPRegInfo vsctlr = {
9291 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
9292 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
9293 .access = PL2_RW, .resetvalue = 0x0,
9294 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
9295 };
9296 define_one_arm_cp_reg(cpu, &vsctlr);
9297 }
2771db27 9298 }
5be5e8ed 9299
2d7137c1 9300 if (cpu_isar_feature(aa64_lor, cpu)) {
2d7137c1
RH
9301 define_arm_cp_regs(cpu, lor_reginfo);
9302 }
220f508f
RH
9303 if (cpu_isar_feature(aa64_pan, cpu)) {
9304 define_one_arm_cp_reg(cpu, &pan_reginfo);
9305 }
04b07d29
RH
9306#ifndef CONFIG_USER_ONLY
9307 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
9308 define_arm_cp_regs(cpu, ats1e1_reginfo);
9309 }
9310 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
9311 define_arm_cp_regs(cpu, ats1cp_reginfo);
9312 }
9313#endif
9eeb7a1c
RH
9314 if (cpu_isar_feature(aa64_uao, cpu)) {
9315 define_one_arm_cp_reg(cpu, &uao_reginfo);
9316 }
2d7137c1 9317
dc8b1853
RC
9318 if (cpu_isar_feature(aa64_dit, cpu)) {
9319 define_one_arm_cp_reg(cpu, &dit_reginfo);
9320 }
f2f68a78
RC
9321 if (cpu_isar_feature(aa64_ssbs, cpu)) {
9322 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
9323 }
58e93b48
RH
9324 if (cpu_isar_feature(any_ras, cpu)) {
9325 define_arm_cp_regs(cpu, minimal_ras_reginfo);
9326 }
dc8b1853 9327
52d18727
RH
9328 if (cpu_isar_feature(aa64_vh, cpu) ||
9329 cpu_isar_feature(aa64_debugv8p2, cpu)) {
9330 define_one_arm_cp_reg(cpu, &contextidr_el2);
9331 }
e2a1a461
RH
9332 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9333 define_arm_cp_regs(cpu, vhe_reginfo);
9334 }
9335
cd208a1c 9336 if (cpu_isar_feature(aa64_sve, cpu)) {
60360d82 9337 define_arm_cp_regs(cpu, zcr_reginfo);
5be5e8ed 9338 }
967aa94f 9339
5814d587
RH
9340 if (cpu_isar_feature(aa64_hcx, cpu)) {
9341 define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
9342 }
9343
967aa94f 9344#ifdef TARGET_AARCH64
9e5ec745
RH
9345 if (cpu_isar_feature(aa64_sme, cpu)) {
9346 define_arm_cp_regs(cpu, sme_reginfo);
9347 }
967aa94f
RH
9348 if (cpu_isar_feature(aa64_pauth, cpu)) {
9349 define_arm_cp_regs(cpu, pauth_reginfo);
9350 }
de390645
RH
9351 if (cpu_isar_feature(aa64_rndr, cpu)) {
9352 define_arm_cp_regs(cpu, rndr_reginfo);
9353 }
84940ed8
RC
9354 if (cpu_isar_feature(aa64_tlbirange, cpu)) {
9355 define_arm_cp_regs(cpu, tlbirange_reginfo);
9356 }
7113d618
RC
9357 if (cpu_isar_feature(aa64_tlbios, cpu)) {
9358 define_arm_cp_regs(cpu, tlbios_reginfo);
9359 }
0d57b499
BM
9360 /* Data Cache clean instructions up to PoP */
9361 if (cpu_isar_feature(aa64_dcpop, cpu)) {
9362 define_one_arm_cp_reg(cpu, dcpop_reg);
9363
9364 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
9365 define_one_arm_cp_reg(cpu, dcpodp_reg);
9366 }
9367 }
4b779ceb
RH
9368
9369 /*
9370 * If full MTE is enabled, add all of the system registers.
9371 * If only "instructions available at EL0" are enabled,
9372 * then define only a RAZ/WI version of PSTATE.TCO.
9373 */
9374 if (cpu_isar_feature(aa64_mte, cpu)) {
851ec6eb
RH
9375 ARMCPRegInfo gmid_reginfo = {
9376 .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
9377 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
9378 .access = PL1_R, .accessfn = access_aa64_tid5,
9379 .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
9380 };
9381 define_one_arm_cp_reg(cpu, &gmid_reginfo);
4b779ceb 9382 define_arm_cp_regs(cpu, mte_reginfo);
5463df16 9383 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb
RH
9384 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
9385 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
5463df16 9386 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb 9387 }
7cb1e618
RH
9388
9389 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
9390 define_arm_cp_regs(cpu, scxtnum_reginfo);
9391 }
15126d9c
PM
9392
9393 if (cpu_isar_feature(aa64_fgt, cpu)) {
9394 define_arm_cp_regs(cpu, fgt_reginfo);
9395 }
ef1febe7
RH
9396
9397 if (cpu_isar_feature(aa64_rme, cpu)) {
9398 define_arm_cp_regs(cpu, rme_reginfo);
9399 if (cpu_isar_feature(aa64_mte, cpu)) {
9400 define_arm_cp_regs(cpu, rme_mte_reginfo);
9401 }
9402 }
967aa94f 9403#endif
cb570bd3 9404
22e57073 9405 if (cpu_isar_feature(any_predinv, cpu)) {
cb570bd3
RH
9406 define_arm_cp_regs(cpu, predinv_reginfo);
9407 }
e2cce18f 9408
957e6155
PM
9409 if (cpu_isar_feature(any_ccidx, cpu)) {
9410 define_arm_cp_regs(cpu, ccsidr2_reginfo);
9411 }
9412
e2cce18f
RH
9413#ifndef CONFIG_USER_ONLY
9414 /*
9415 * Register redirections and aliases must be done last,
9416 * after the registers from the other extensions have been defined.
9417 */
9418 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9419 define_arm_vh_e2h_redirects_aliases(cpu);
9420 }
9421#endif
2ceb98c0
PM
9422}
9423
777dc784
PM
9424/* Sort alphabetically by type name, except for "any". */
9425static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 9426{
777dc784
PM
9427 ObjectClass *class_a = (ObjectClass *)a;
9428 ObjectClass *class_b = (ObjectClass *)b;
9429 const char *name_a, *name_b;
5adb4839 9430
777dc784
PM
9431 name_a = object_class_get_name(class_a);
9432 name_b = object_class_get_name(class_b);
51492fd1 9433 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 9434 return 1;
51492fd1 9435 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
9436 return -1;
9437 } else {
9438 return strcmp(name_a, name_b);
5adb4839
PB
9439 }
9440}
9441
777dc784 9442static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 9443{
777dc784 9444 ObjectClass *oc = data;
977c33ba 9445 CPUClass *cc = CPU_CLASS(oc);
51492fd1
AF
9446 const char *typename;
9447 char *name;
3371d272 9448
51492fd1
AF
9449 typename = object_class_get_name(oc);
9450 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
977c33ba
DB
9451 if (cc->deprecation_note) {
9452 qemu_printf(" %s (deprecated)\n", name);
9453 } else {
9454 qemu_printf(" %s\n", name);
9455 }
51492fd1 9456 g_free(name);
777dc784
PM
9457}
9458
0442428a 9459void arm_cpu_list(void)
777dc784 9460{
777dc784
PM
9461 GSList *list;
9462
9463 list = object_class_get_list(TYPE_ARM_CPU, false);
9464 list = g_slist_sort(list, arm_cpu_list_compare);
0442428a
MA
9465 qemu_printf("Available CPUs:\n");
9466 g_slist_foreach(list, arm_cpu_list_entry, NULL);
777dc784 9467 g_slist_free(list);
40f137e1
PB
9468}
9469
1859f8c3
RH
9470/*
9471 * Private utility function for define_one_arm_cp_reg_with_opaque():
9472 * add a single reginfo struct to the hash table.
9473 */
6e6efd61 9474static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
cbe64585
RH
9475 void *opaque, CPState state,
9476 CPSecureState secstate,
9c513e78
AB
9477 int crm, int opc1, int opc2,
9478 const char *name)
6e6efd61 9479{
696ba377 9480 CPUARMState *env = &cpu->env;
5860362d 9481 uint32_t key;
c27f5d3a 9482 ARMCPRegInfo *r2;
4c8c4541
RH
9483 bool is64 = r->type & ARM_CP_64BIT;
9484 bool ns = secstate & ARM_CP_SECSTATE_NS;
cac65299 9485 int cp = r->cp;
c27f5d3a 9486 size_t name_len;
696ba377 9487 bool make_const;
c27f5d3a 9488
cac65299
RH
9489 switch (state) {
9490 case ARM_CP_STATE_AA32:
9491 /* We assume it is a cp15 register if the .cp field is left unset. */
9492 if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9493 cp = 15;
9494 }
9495 key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9496 break;
9497 case ARM_CP_STATE_AA64:
9498 /*
9499 * To allow abbreviation of ARMCPRegInfo definitions, we treat
9500 * cp == 0 as equivalent to the value for "standard guest-visible
9501 * sysreg". STATE_BOTH definitions are also always "standard sysreg"
9502 * in their AArch64 view (the .cp value may be non-zero for the
9503 * benefit of the AArch32 view).
9504 */
9505 if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9506 cp = CP_REG_ARM64_SYSREG_CP;
9507 }
9508 key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9509 break;
9510 default:
9511 g_assert_not_reached();
9512 }
9513
dc44545b
RH
9514 /* Overriding of an existing definition must be explicitly requested. */
9515 if (!(r->type & ARM_CP_OVERRIDE)) {
9516 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9517 if (oldreg) {
9518 assert(oldreg->type & ARM_CP_OVERRIDE);
9519 }
9520 }
9521
696ba377
RH
9522 /*
9523 * Eliminate registers that are not present because the EL is missing.
9524 * Doing this here makes it easier to put all registers for a given
9525 * feature into the same ARMCPRegInfo array and define them all at once.
9526 */
9527 make_const = false;
9528 if (arm_feature(env, ARM_FEATURE_EL3)) {
9529 /*
9530 * An EL2 register without EL2 but with EL3 is (usually) RES0.
9531 * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9532 */
9533 int min_el = ctz32(r->access) / 2;
9534 if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9535 if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9536 return;
9537 }
9538 make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9539 }
9540 } else {
9541 CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9542 ? PL2_RW : PL1_RW);
9543 if ((r->access & max_el) == 0) {
9544 return;
9545 }
9546 }
9547
c27f5d3a
RH
9548 /* Combine cpreg and name into one allocation. */
9549 name_len = strlen(name) + 1;
9550 r2 = g_malloc(sizeof(*r2) + name_len);
9551 *r2 = *r;
9552 r2->name = memcpy(r2 + 1, name, name_len);
3f3c82a5 9553
cc946d96
RH
9554 /*
9555 * Update fields to match the instantiation, overwiting wildcards
9556 * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
3f3c82a5 9557 */
cc946d96
RH
9558 r2->cp = cp;
9559 r2->crm = crm;
9560 r2->opc1 = opc1;
9561 r2->opc2 = opc2;
9562 r2->state = state;
3f3c82a5 9563 r2->secure = secstate;
cc946d96
RH
9564 if (opaque) {
9565 r2->opaque = opaque;
9566 }
3f3c82a5 9567
696ba377
RH
9568 if (make_const) {
9569 /* This should not have been a very special register to begin. */
9570 int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9571 assert(old_special == 0 || old_special == ARM_CP_NOP);
1859f8c3 9572 /*
696ba377
RH
9573 * Set the special function to CONST, retaining the other flags.
9574 * This is important for e.g. ARM_CP_SVE so that we still
9575 * take the SVE trap if CPTR_EL3.EZ == 0.
f5a0a5a5 9576 */
696ba377
RH
9577 r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9578 /*
9579 * Usually, these registers become RES0, but there are a few
9580 * special cases like VPIDR_EL2 which have a constant non-zero
9581 * value with writes ignored.
9582 */
9583 if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9584 r2->resetvalue = 0;
9585 }
9586 /*
9587 * ARM_CP_CONST has precedence, so removing the callbacks and
9588 * offsets are not strictly necessary, but it is potentially
9589 * less confusing to debug later.
9590 */
9591 r2->readfn = NULL;
9592 r2->writefn = NULL;
9593 r2->raw_readfn = NULL;
9594 r2->raw_writefn = NULL;
9595 r2->resetfn = NULL;
9596 r2->fieldoffset = 0;
9597 r2->bank_fieldoffsets[0] = 0;
9598 r2->bank_fieldoffsets[1] = 0;
9599 } else {
9600 bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
3f3c82a5 9601
10748a96 9602 if (isbanked) {
1859f8c3 9603 /*
696ba377
RH
9604 * Register is banked (using both entries in array).
9605 * Overwriting fieldoffset as the array is only used to define
9606 * banked registers but later only fieldoffset is used.
3f3c82a5 9607 */
696ba377
RH
9608 r2->fieldoffset = r->bank_fieldoffsets[ns];
9609 }
9610 if (state == ARM_CP_STATE_AA32) {
9611 if (isbanked) {
9612 /*
9613 * If the register is banked then we don't need to migrate or
9614 * reset the 32-bit instance in certain cases:
9615 *
9616 * 1) If the register has both 32-bit and 64-bit instances
9617 * then we can count on the 64-bit instance taking care
9618 * of the non-secure bank.
9619 * 2) If ARMv8 is enabled then we can count on a 64-bit
9620 * version taking care of the secure bank. This requires
9621 * that separate 32 and 64-bit definitions are provided.
9622 */
9623 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9624 (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9625 r2->type |= ARM_CP_ALIAS;
9626 }
9627 } else if ((secstate != r->secure) && !ns) {
9628 /*
9629 * The register is not banked so we only want to allow
9630 * migration of the non-secure instance.
9631 */
7a0e58fa 9632 r2->type |= ARM_CP_ALIAS;
3f3c82a5 9633 }
3f3c82a5 9634
696ba377
RH
9635 if (HOST_BIG_ENDIAN &&
9636 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9637 r2->fieldoffset += sizeof(uint32_t);
9638 }
3f3c82a5 9639 }
f5a0a5a5 9640 }
cc946d96 9641
1859f8c3
RH
9642 /*
9643 * By convention, for wildcarded registers only the first
6e6efd61 9644 * entry is used for migration; the others are marked as
7a0e58fa 9645 * ALIAS so we don't try to transfer the register
6e6efd61 9646 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 9647 * never migratable and not even raw-accessible.
6e6efd61 9648 */
696ba377 9649 if (r2->type & ARM_CP_SPECIAL_MASK) {
7a0e58fa
PM
9650 r2->type |= ARM_CP_NO_RAW;
9651 }
9652 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
9653 ((r->opc1 == CP_ANY) && opc1 != 0) ||
9654 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 9655 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
9656 }
9657
1859f8c3
RH
9658 /*
9659 * Check that raw accesses are either forbidden or handled. Note that
375421cc
PM
9660 * we can't assert this earlier because the setup of fieldoffset for
9661 * banked registers has to be done first.
9662 */
9663 if (!(r2->type & ARM_CP_NO_RAW)) {
9664 assert(!raw_accessors_invalid(r2));
9665 }
9666
5860362d 9667 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
6e6efd61
PM
9668}
9669
9670
4b6a83fb
PM
9671void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9672 const ARMCPRegInfo *r, void *opaque)
9673{
9b37a28c
FR
9674 /*
9675 * Define implementations of coprocessor registers.
4b6a83fb
PM
9676 * We store these in a hashtable because typically
9677 * there are less than 150 registers in a space which
9678 * is 16*16*16*8*8 = 262144 in size.
9679 * Wildcarding is supported for the crm, opc1 and opc2 fields.
9680 * If a register is defined twice then the second definition is
9681 * used, so this can be used to define some generic registers and
9682 * then override them with implementation specific variations.
9683 * At least one of the original and the second definition should
9684 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9685 * against accidental use.
f5a0a5a5
PM
9686 *
9687 * The state field defines whether the register is to be
9688 * visible in the AArch32 or AArch64 execution state. If the
9689 * state is set to ARM_CP_STATE_BOTH then we synthesise a
9690 * reginfo structure for the AArch32 view, which sees the lower
9691 * 32 bits of the 64 bit register.
9692 *
9693 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9694 * be wildcarded. AArch64 registers are always considered to be 64
9695 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9696 * the register, if any.
4b6a83fb 9697 */
d95101d6 9698 int crm, opc1, opc2;
4b6a83fb
PM
9699 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9700 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9701 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9702 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9703 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9704 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
d95101d6
RH
9705 CPState state;
9706
4b6a83fb
PM
9707 /* 64 bit registers have only CRm and Opc1 fields */
9708 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
9709 /* op0 only exists in the AArch64 encodings */
9710 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9711 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9712 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
cd8be50e
PM
9713 /*
9714 * This API is only for Arm's system coprocessors (14 and 15) or
9715 * (M-profile or v7A-and-earlier only) for implementation defined
9716 * coprocessors in the range 0..7. Our decode assumes this, since
9717 * 8..13 can be used for other insns including VFP and Neon. See
9718 * valid_cp() in translate.c. Assert here that we haven't tried
9719 * to use an invalid coprocessor number.
9720 */
9721 switch (r->state) {
9722 case ARM_CP_STATE_BOTH:
9723 /* 0 has a special meaning, but otherwise the same rules as AA32. */
9724 if (r->cp == 0) {
9725 break;
9726 }
9727 /* fall through */
9728 case ARM_CP_STATE_AA32:
9729 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9730 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9731 assert(r->cp >= 14 && r->cp <= 15);
9732 } else {
9733 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9734 }
9735 break;
9736 case ARM_CP_STATE_AA64:
9737 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9738 break;
9739 default:
9740 g_assert_not_reached();
9741 }
9b37a28c
FR
9742 /*
9743 * The AArch64 pseudocode CheckSystemAccess() specifies that op1
f5a0a5a5
PM
9744 * encodes a minimum access level for the register. We roll this
9745 * runtime check into our general permission check code, so check
9746 * here that the reginfo's specified permissions are strict enough
9747 * to encompass the generic architectural permission check.
9748 */
9749 if (r->state != ARM_CP_STATE_AA32) {
39107337 9750 CPAccessRights mask;
f5a0a5a5 9751 switch (r->opc1) {
b5bd7440
AB
9752 case 0:
9753 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9754 mask = PL0U_R | PL1_RW;
9755 break;
9756 case 1: case 2:
f5a0a5a5
PM
9757 /* min_EL EL1 */
9758 mask = PL1_RW;
9759 break;
9760 case 3:
9761 /* min_EL EL0 */
9762 mask = PL0_RW;
9763 break;
9764 case 4:
b4ecf60f 9765 case 5:
f5a0a5a5
PM
9766 /* min_EL EL2 */
9767 mask = PL2_RW;
9768 break;
f5a0a5a5
PM
9769 case 6:
9770 /* min_EL EL3 */
9771 mask = PL3_RW;
9772 break;
9773 case 7:
9774 /* min_EL EL1, secure mode only (we don't check the latter) */
9775 mask = PL1_RW;
9776 break;
9777 default:
9778 /* broken reginfo with out-of-range opc1 */
d385a605 9779 g_assert_not_reached();
f5a0a5a5
PM
9780 }
9781 /* assert our permissions are not too lax (stricter is fine) */
9782 assert((r->access & ~mask) == 0);
9783 }
9784
9b37a28c
FR
9785 /*
9786 * Check that the register definition has enough info to handle
4b6a83fb
PM
9787 * reads and writes if they are permitted.
9788 */
87c3f0f2 9789 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
4b6a83fb 9790 if (r->access & PL3_R) {
3f3c82a5
FA
9791 assert((r->fieldoffset ||
9792 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9793 r->readfn);
4b6a83fb
PM
9794 }
9795 if (r->access & PL3_W) {
3f3c82a5
FA
9796 assert((r->fieldoffset ||
9797 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9798 r->writefn);
4b6a83fb
PM
9799 }
9800 }
5809ac57 9801
4b6a83fb
PM
9802 for (crm = crmmin; crm <= crmmax; crm++) {
9803 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9804 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
9805 for (state = ARM_CP_STATE_AA32;
9806 state <= ARM_CP_STATE_AA64; state++) {
9807 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9808 continue;
9809 }
3f3c82a5 9810 if (state == ARM_CP_STATE_AA32) {
9b37a28c
FR
9811 /*
9812 * Under AArch32 CP registers can be common
3f3c82a5
FA
9813 * (same for secure and non-secure world) or banked.
9814 */
9c513e78
AB
9815 char *name;
9816
3f3c82a5
FA
9817 switch (r->secure) {
9818 case ARM_CP_SECSTATE_S:
9819 case ARM_CP_SECSTATE_NS:
9820 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
9821 r->secure, crm, opc1, opc2,
9822 r->name);
3f3c82a5 9823 break;
cbe64585 9824 case ARM_CP_SECSTATE_BOTH:
9c513e78 9825 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
9826 add_cpreg_to_hashtable(cpu, r, opaque, state,
9827 ARM_CP_SECSTATE_S,
9c513e78
AB
9828 crm, opc1, opc2, name);
9829 g_free(name);
3f3c82a5
FA
9830 add_cpreg_to_hashtable(cpu, r, opaque, state,
9831 ARM_CP_SECSTATE_NS,
9c513e78 9832 crm, opc1, opc2, r->name);
3f3c82a5 9833 break;
cbe64585
RH
9834 default:
9835 g_assert_not_reached();
3f3c82a5
FA
9836 }
9837 } else {
9b37a28c
FR
9838 /*
9839 * AArch64 registers get mapped to non-secure instance
9840 * of AArch32
9841 */
3f3c82a5
FA
9842 add_cpreg_to_hashtable(cpu, r, opaque, state,
9843 ARM_CP_SECSTATE_NS,
9c513e78 9844 crm, opc1, opc2, r->name);
3f3c82a5 9845 }
f5a0a5a5 9846 }
4b6a83fb
PM
9847 }
9848 }
9849 }
9850}
9851
5809ac57
RH
9852/* Define a whole list of registers */
9853void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9854 void *opaque, size_t len)
4b6a83fb 9855{
5809ac57
RH
9856 size_t i;
9857 for (i = 0; i < len; ++i) {
9858 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
4b6a83fb
PM
9859 }
9860}
9861
6c5c0fec
AB
9862/*
9863 * Modify ARMCPRegInfo for access from userspace.
9864 *
9865 * This is a data driven modification directed by
9866 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9867 * user-space cannot alter any values and dynamic values pertaining to
9868 * execution state are hidden from user space view anyway.
9869 */
5809ac57
RH
9870void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9871 const ARMCPRegUserSpaceInfo *mods,
9872 size_t mods_len)
6c5c0fec 9873{
5809ac57
RH
9874 for (size_t mi = 0; mi < mods_len; ++mi) {
9875 const ARMCPRegUserSpaceInfo *m = mods + mi;
d040242e 9876 GPatternSpec *pat = NULL;
5809ac57 9877
d040242e
AB
9878 if (m->is_glob) {
9879 pat = g_pattern_spec_new(m->name);
9880 }
5809ac57
RH
9881 for (size_t ri = 0; ri < regs_len; ++ri) {
9882 ARMCPRegInfo *r = regs + ri;
9883
d040242e
AB
9884 if (pat && g_pattern_match_string(pat, r->name)) {
9885 r->type = ARM_CP_CONST;
9886 r->access = PL0U_R;
9887 r->resetvalue = 0;
9888 /* continue */
9889 } else if (strcmp(r->name, m->name) == 0) {
6c5c0fec
AB
9890 r->type = ARM_CP_CONST;
9891 r->access = PL0U_R;
9892 r->resetvalue &= m->exported_bits;
9893 r->resetvalue |= m->fixed_bits;
9894 break;
9895 }
9896 }
d040242e
AB
9897 if (pat) {
9898 g_pattern_spec_free(pat);
9899 }
6c5c0fec
AB
9900 }
9901}
9902
60322b39 9903const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 9904{
5860362d 9905 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
4b6a83fb
PM
9906}
9907
c4241c7d
PM
9908void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9909 uint64_t value)
4b6a83fb
PM
9910{
9911 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
9912}
9913
c4241c7d 9914uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
9915{
9916 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
9917 return 0;
9918}
9919
f5a0a5a5
PM
9920void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9921{
9922 /* Helper coprocessor reset function for do-nothing-on-reset registers */
9923}
9924
af393ffc 9925static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b 9926{
9b37a28c
FR
9927 /*
9928 * Return true if it is not valid for us to switch to
37064a8b
PM
9929 * this CPU mode (ie all the UNPREDICTABLE cases in
9930 * the ARM ARM CPSRWriteByInstr pseudocode).
9931 */
af393ffc
PM
9932
9933 /* Changes to or from Hyp via MSR and CPS are illegal. */
9934 if (write_type == CPSRWriteByInstr &&
9935 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9936 mode == ARM_CPU_MODE_HYP)) {
9937 return 1;
9938 }
9939
37064a8b
PM
9940 switch (mode) {
9941 case ARM_CPU_MODE_USR:
10eacda7 9942 return 0;
37064a8b
PM
9943 case ARM_CPU_MODE_SYS:
9944 case ARM_CPU_MODE_SVC:
9945 case ARM_CPU_MODE_ABT:
9946 case ARM_CPU_MODE_UND:
9947 case ARM_CPU_MODE_IRQ:
9948 case ARM_CPU_MODE_FIQ:
9b37a28c
FR
9949 /*
9950 * Note that we don't implement the IMPDEF NSACR.RFR which in v7
52ff951b
PM
9951 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9952 */
9b37a28c
FR
9953 /*
9954 * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
10eacda7
PM
9955 * and CPS are treated as illegal mode changes.
9956 */
9957 if (write_type == CPSRWriteByInstr &&
10eacda7 9958 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7c208e0f 9959 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10eacda7
PM
9960 return 1;
9961 }
37064a8b 9962 return 0;
e6c8fc07 9963 case ARM_CPU_MODE_HYP:
e6ef0169 9964 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
027fc527 9965 case ARM_CPU_MODE_MON:
58ae2d1f 9966 return arm_current_el(env) < 3;
37064a8b
PM
9967 default:
9968 return 1;
9969 }
9970}
9971
2f4a40e5
AZ
9972uint32_t cpsr_read(CPUARMState *env)
9973{
9974 int ZF;
6fbe23d5
PB
9975 ZF = (env->ZF == 0);
9976 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
9977 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9978 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9979 | ((env->condexec_bits & 0xfc) << 8)
af519934 9980 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
9981}
9982
50866ba5
PM
9983void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9984 CPSRWriteType write_type)
2f4a40e5 9985{
6e8801f9 9986 uint32_t changed_daif;
e784807c
PM
9987 bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9988 (mask & (CPSR_M | CPSR_E | CPSR_IL));
6e8801f9 9989
2f4a40e5 9990 if (mask & CPSR_NZCV) {
6fbe23d5
PB
9991 env->ZF = (~val) & CPSR_Z;
9992 env->NF = val;
2f4a40e5
AZ
9993 env->CF = (val >> 29) & 1;
9994 env->VF = (val << 3) & 0x80000000;
9995 }
f927dbda 9996 if (mask & CPSR_Q) {
2f4a40e5 9997 env->QF = ((val & CPSR_Q) != 0);
f927dbda
FR
9998 }
9999 if (mask & CPSR_T) {
2f4a40e5 10000 env->thumb = ((val & CPSR_T) != 0);
f927dbda 10001 }
2f4a40e5
AZ
10002 if (mask & CPSR_IT_0_1) {
10003 env->condexec_bits &= ~3;
10004 env->condexec_bits |= (val >> 25) & 3;
10005 }
10006 if (mask & CPSR_IT_2_7) {
10007 env->condexec_bits &= 3;
10008 env->condexec_bits |= (val >> 8) & 0xfc;
10009 }
10010 if (mask & CPSR_GE) {
10011 env->GE = (val >> 16) & 0xf;
10012 }
10013
9b37a28c
FR
10014 /*
10015 * In a V7 implementation that includes the security extensions but does
6e8801f9
FA
10016 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
10017 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
10018 * bits respectively.
10019 *
10020 * In a V8 implementation, it is permitted for privileged software to
10021 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
10022 */
f8c88bbc 10023 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
10024 arm_feature(env, ARM_FEATURE_EL3) &&
10025 !arm_feature(env, ARM_FEATURE_EL2) &&
10026 !arm_is_secure(env)) {
10027
10028 changed_daif = (env->daif ^ val) & mask;
10029
10030 if (changed_daif & CPSR_A) {
9b37a28c
FR
10031 /*
10032 * Check to see if we are allowed to change the masking of async
6e8801f9
FA
10033 * abort exceptions from a non-secure state.
10034 */
10035 if (!(env->cp15.scr_el3 & SCR_AW)) {
10036 qemu_log_mask(LOG_GUEST_ERROR,
10037 "Ignoring attempt to switch CPSR_A flag from "
10038 "non-secure world with SCR.AW bit clear\n");
10039 mask &= ~CPSR_A;
10040 }
10041 }
10042
10043 if (changed_daif & CPSR_F) {
9b37a28c
FR
10044 /*
10045 * Check to see if we are allowed to change the masking of FIQ
6e8801f9
FA
10046 * exceptions from a non-secure state.
10047 */
10048 if (!(env->cp15.scr_el3 & SCR_FW)) {
10049 qemu_log_mask(LOG_GUEST_ERROR,
10050 "Ignoring attempt to switch CPSR_F flag from "
10051 "non-secure world with SCR.FW bit clear\n");
10052 mask &= ~CPSR_F;
10053 }
10054
9b37a28c
FR
10055 /*
10056 * Check whether non-maskable FIQ (NMFI) support is enabled.
6e8801f9
FA
10057 * If this bit is set software is not allowed to mask
10058 * FIQs, but is allowed to set CPSR_F to 0.
10059 */
10060 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
10061 (val & CPSR_F)) {
10062 qemu_log_mask(LOG_GUEST_ERROR,
10063 "Ignoring attempt to enable CPSR_F flag "
10064 "(non-maskable FIQ [NMFI] support enabled)\n");
10065 mask &= ~CPSR_F;
10066 }
10067 }
10068 }
10069
4cc35614
PM
10070 env->daif &= ~(CPSR_AIF & mask);
10071 env->daif |= val & CPSR_AIF & mask;
10072
f8c88bbc
PM
10073 if (write_type != CPSRWriteRaw &&
10074 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9 10075 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9b37a28c
FR
10076 /*
10077 * Note that we can only get here in USR mode if this is a
8c4f0eb9
PM
10078 * gdb stub write; for this case we follow the architectural
10079 * behaviour for guest writes in USR mode of ignoring an attempt
10080 * to switch mode. (Those are caught by translate.c for writes
10081 * triggered by guest instructions.)
10082 */
10083 mask &= ~CPSR_M;
10084 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9b37a28c
FR
10085 /*
10086 * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
81907a58
PM
10087 * v7, and has defined behaviour in v8:
10088 * + leave CPSR.M untouched
10089 * + allow changes to the other CPSR fields
10090 * + set PSTATE.IL
10091 * For user changes via the GDB stub, we don't set PSTATE.IL,
10092 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
10093 */
10094 mask &= ~CPSR_M;
81907a58
PM
10095 if (write_type != CPSRWriteByGDBStub &&
10096 arm_feature(env, ARM_FEATURE_V8)) {
10097 mask |= CPSR_IL;
10098 val |= CPSR_IL;
10099 }
81e37284
PM
10100 qemu_log_mask(LOG_GUEST_ERROR,
10101 "Illegal AArch32 mode switch attempt from %s to %s\n",
10102 aarch32_mode_name(env->uncached_cpsr),
10103 aarch32_mode_name(val));
37064a8b 10104 } else {
81e37284
PM
10105 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
10106 write_type == CPSRWriteExceptionReturn ?
10107 "Exception return from AArch32" :
10108 "AArch32 mode switch from",
10109 aarch32_mode_name(env->uncached_cpsr),
10110 aarch32_mode_name(val), env->regs[15]);
37064a8b
PM
10111 switch_mode(env, val & CPSR_M);
10112 }
2f4a40e5
AZ
10113 }
10114 mask &= ~CACHED_CPSR_BITS;
10115 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
2b77ad4d 10116 if (tcg_enabled() && rebuild_hflags) {
e784807c
PM
10117 arm_rebuild_hflags(env);
10118 }
2f4a40e5
AZ
10119}
10120
b26eefb6
PB
10121/* Sign/zero extend */
10122uint32_t HELPER(sxtb16)(uint32_t x)
10123{
10124 uint32_t res;
10125 res = (uint16_t)(int8_t)x;
10126 res |= (uint32_t)(int8_t)(x >> 16) << 16;
10127 return res;
10128}
10129
e5346292
PM
10130static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
10131{
10132 /*
10133 * Take a division-by-zero exception if necessary; otherwise return
10134 * to get the usual non-trapping division behaviour (result of 0)
10135 */
10136 if (arm_feature(env, ARM_FEATURE_M)
10137 && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
10138 raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
10139 }
10140}
10141
b26eefb6
PB
10142uint32_t HELPER(uxtb16)(uint32_t x)
10143{
10144 uint32_t res;
10145 res = (uint16_t)(uint8_t)x;
10146 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
10147 return res;
10148}
10149
e5346292 10150int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
3670669c 10151{
fc7a5038 10152 if (den == 0) {
e5346292 10153 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
10154 return 0;
10155 }
10156 if (num == INT_MIN && den == -1) {
10157 return INT_MIN;
10158 }
3670669c
PB
10159 return num / den;
10160}
10161
e5346292 10162uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
3670669c 10163{
fc7a5038 10164 if (den == 0) {
e5346292 10165 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
10166 return 0;
10167 }
3670669c
PB
10168 return num / den;
10169}
10170
10171uint32_t HELPER(rbit)(uint32_t x)
10172{
42fedbca 10173 return revbit32(x);
3670669c
PB
10174}
10175
c47eaf9f 10176#ifdef CONFIG_USER_ONLY
b5ff1b31 10177
affdb64d 10178static void switch_mode(CPUARMState *env, int mode)
b5ff1b31 10179{
2fc0cc0e 10180 ARMCPU *cpu = env_archcpu(env);
a47dddd7
AF
10181
10182 if (mode != ARM_CPU_MODE_USR) {
10183 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
10184 }
b5ff1b31
FB
10185}
10186
012a906b
GB
10187uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10188 uint32_t cur_el, bool secure)
9e729b57
EI
10189{
10190 return 1;
10191}
10192
ce02049d
GB
10193void aarch64_sync_64_to_32(CPUARMState *env)
10194{
10195 g_assert_not_reached();
10196}
10197
b5ff1b31
FB
10198#else
10199
affdb64d 10200static void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
10201{
10202 int old_mode;
10203 int i;
10204
10205 old_mode = env->uncached_cpsr & CPSR_M;
f927dbda 10206 if (mode == old_mode) {
b5ff1b31 10207 return;
f927dbda 10208 }
b5ff1b31
FB
10209
10210 if (old_mode == ARM_CPU_MODE_FIQ) {
04215eb1
FR
10211 memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
10212 memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31 10213 } else if (mode == ARM_CPU_MODE_FIQ) {
04215eb1
FR
10214 memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
10215 memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
10216 }
10217
f5206413 10218 i = bank_number(old_mode);
b5ff1b31 10219 env->banked_r13[i] = env->regs[13];
b5ff1b31
FB
10220 env->banked_spsr[i] = env->spsr;
10221
f5206413 10222 i = bank_number(mode);
b5ff1b31 10223 env->regs[13] = env->banked_r13[i];
b5ff1b31 10224 env->spsr = env->banked_spsr[i];
593cfa2b
PM
10225
10226 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
10227 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
b5ff1b31
FB
10228}
10229
9b37a28c
FR
10230/*
10231 * Physical Interrupt Target EL Lookup Table
0eeb17d6
GB
10232 *
10233 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
10234 *
10235 * The below multi-dimensional table is used for looking up the target
10236 * exception level given numerous condition criteria. Specifically, the
10237 * target EL is based on SCR and HCR routing controls as well as the
10238 * currently executing EL and secure state.
10239 *
10240 * Dimensions:
10241 * target_el_table[2][2][2][2][2][4]
10242 * | | | | | +--- Current EL
10243 * | | | | +------ Non-secure(0)/Secure(1)
10244 * | | | +--------- HCR mask override
10245 * | | +------------ SCR exec state control
10246 * | +--------------- SCR mask override
10247 * +------------------ 32-bit(0)/64-bit(1) EL3
10248 *
10249 * The table values are as such:
10250 * 0-3 = EL0-EL3
10251 * -1 = Cannot occur
10252 *
10253 * The ARM ARM target EL table includes entries indicating that an "exception
10254 * is not taken". The two cases where this is applicable are:
10255 * 1) An exception is taken from EL3 but the SCR does not have the exception
10256 * routed to EL3.
10257 * 2) An exception is taken from EL2 but the HCR does not have the exception
10258 * routed to EL2.
10259 * In these two cases, the below table contain a target of EL1. This value is
10260 * returned as it is expected that the consumer of the table data will check
10261 * for "target EL >= current EL" to ensure the exception is not taken.
10262 *
10263 * SCR HCR
10264 * 64 EA AMO From
10265 * BIT IRQ IMO Non-secure Secure
10266 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
10267 */
82c39f6a 10268static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
10269 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
10270 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
10271 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
10272 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
10273 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
10274 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
10275 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
10276 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
10277 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6c85f906
RDC
10278 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
10279 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
10280 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
0eeb17d6
GB
10281 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
10282 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6c85f906
RDC
10283 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
10284 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
0eeb17d6
GB
10285};
10286
10287/*
10288 * Determine the target EL for physical exceptions
10289 */
012a906b
GB
10290uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10291 uint32_t cur_el, bool secure)
0eeb17d6 10292{
b77af26e 10293 CPUARMState *env = cpu_env(cs);
f7778444
RH
10294 bool rw;
10295 bool scr;
10296 bool hcr;
0eeb17d6 10297 int target_el;
2cde031f 10298 /* Is the highest EL AArch64? */
f7778444
RH
10299 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
10300 uint64_t hcr_el2;
2cde031f
SS
10301
10302 if (arm_feature(env, ARM_FEATURE_EL3)) {
10303 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
10304 } else {
9b37a28c
FR
10305 /*
10306 * Either EL2 is the highest EL (and so the EL2 register width
2cde031f
SS
10307 * is given by is64); or there is no EL2 or EL3, in which case
10308 * the value of 'rw' does not affect the table lookup anyway.
10309 */
10310 rw = is64;
10311 }
0eeb17d6 10312
f7778444 10313 hcr_el2 = arm_hcr_el2_eff(env);
0eeb17d6
GB
10314 switch (excp_idx) {
10315 case EXCP_IRQ:
10316 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
f7778444 10317 hcr = hcr_el2 & HCR_IMO;
0eeb17d6
GB
10318 break;
10319 case EXCP_FIQ:
10320 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
f7778444 10321 hcr = hcr_el2 & HCR_FMO;
0eeb17d6
GB
10322 break;
10323 default:
10324 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
f7778444 10325 hcr = hcr_el2 & HCR_AMO;
0eeb17d6
GB
10326 break;
10327 };
10328
d1b31428
RH
10329 /*
10330 * For these purposes, TGE and AMO/IMO/FMO both force the
10331 * interrupt to EL2. Fold TGE into the bit extracted above.
10332 */
10333 hcr |= (hcr_el2 & HCR_TGE) != 0;
10334
0eeb17d6
GB
10335 /* Perform a table-lookup for the target EL given the current state */
10336 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
10337
10338 assert(target_el > 0);
10339
10340 return target_el;
10341}
10342
fc6177af 10343void arm_log_exception(CPUState *cs)
b59f479b 10344{
fc6177af
PM
10345 int idx = cs->exception_index;
10346
b59f479b
PMD
10347 if (qemu_loglevel_mask(CPU_LOG_INT)) {
10348 const char *exc = NULL;
10349 static const char * const excnames[] = {
10350 [EXCP_UDEF] = "Undefined Instruction",
10351 [EXCP_SWI] = "SVC",
10352 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
10353 [EXCP_DATA_ABORT] = "Data Abort",
10354 [EXCP_IRQ] = "IRQ",
10355 [EXCP_FIQ] = "FIQ",
10356 [EXCP_BKPT] = "Breakpoint",
10357 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
10358 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
10359 [EXCP_HVC] = "Hypervisor Call",
10360 [EXCP_HYP_TRAP] = "Hypervisor Trap",
10361 [EXCP_SMC] = "Secure Monitor Call",
10362 [EXCP_VIRQ] = "Virtual IRQ",
10363 [EXCP_VFIQ] = "Virtual FIQ",
10364 [EXCP_SEMIHOST] = "Semihosting call",
10365 [EXCP_NOCP] = "v7M NOCP UsageFault",
10366 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
10367 [EXCP_STKOF] = "v8M STKOF UsageFault",
10368 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
10369 [EXCP_LSERR] = "v8M LSERR UsageFault",
10370 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
e5346292 10371 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
3c29632f 10372 [EXCP_VSERR] = "Virtual SERR",
11b76fda 10373 [EXCP_GPC] = "Granule Protection Check",
b59f479b
PMD
10374 };
10375
10376 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
10377 exc = excnames[idx];
10378 }
10379 if (!exc) {
10380 exc = "unknown";
10381 }
fc6177af
PM
10382 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
10383 idx, exc, cs->cpu_index);
b59f479b
PMD
10384 }
10385}
10386
a356dacf 10387/*
7aab5a8c
PMD
10388 * Function used to synchronize QEMU's AArch64 register set with AArch32
10389 * register set. This is necessary when switching between AArch32 and AArch64
10390 * execution state.
a356dacf 10391 */
7aab5a8c 10392void aarch64_sync_32_to_64(CPUARMState *env)
9ee6e8bb 10393{
7aab5a8c
PMD
10394 int i;
10395 uint32_t mode = env->uncached_cpsr & CPSR_M;
10396
10397 /* We can blanket copy R[0:7] to X[0:7] */
10398 for (i = 0; i < 8; i++) {
10399 env->xregs[i] = env->regs[i];
fd592d89 10400 }
70d74660 10401
9a223097 10402 /*
7aab5a8c
PMD
10403 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
10404 * Otherwise, they come from the banked user regs.
fd592d89 10405 */
7aab5a8c
PMD
10406 if (mode == ARM_CPU_MODE_FIQ) {
10407 for (i = 8; i < 13; i++) {
10408 env->xregs[i] = env->usr_regs[i - 8];
10409 }
10410 } else {
10411 for (i = 8; i < 13; i++) {
10412 env->xregs[i] = env->regs[i];
10413 }
fd592d89 10414 }
9ee6e8bb 10415
7aab5a8c
PMD
10416 /*
10417 * Registers x13-x23 are the various mode SP and FP registers. Registers
10418 * r13 and r14 are only copied if we are in that mode, otherwise we copy
10419 * from the mode banked register.
10420 */
10421 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10422 env->xregs[13] = env->regs[13];
10423 env->xregs[14] = env->regs[14];
10424 } else {
10425 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
10426 /* HYP is an exception in that it is copied from r14 */
10427 if (mode == ARM_CPU_MODE_HYP) {
10428 env->xregs[14] = env->regs[14];
95695eff 10429 } else {
7aab5a8c 10430 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
95695eff 10431 }
95695eff
PM
10432 }
10433
7aab5a8c
PMD
10434 if (mode == ARM_CPU_MODE_HYP) {
10435 env->xregs[15] = env->regs[13];
10436 } else {
10437 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
95695eff
PM
10438 }
10439
7aab5a8c
PMD
10440 if (mode == ARM_CPU_MODE_IRQ) {
10441 env->xregs[16] = env->regs[14];
10442 env->xregs[17] = env->regs[13];
10443 } else {
10444 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
10445 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
10446 }
95695eff 10447
7aab5a8c
PMD
10448 if (mode == ARM_CPU_MODE_SVC) {
10449 env->xregs[18] = env->regs[14];
10450 env->xregs[19] = env->regs[13];
10451 } else {
10452 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
10453 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
10454 }
95695eff 10455
7aab5a8c
PMD
10456 if (mode == ARM_CPU_MODE_ABT) {
10457 env->xregs[20] = env->regs[14];
10458 env->xregs[21] = env->regs[13];
10459 } else {
10460 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10461 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10462 }
e33cf0f8 10463
7aab5a8c
PMD
10464 if (mode == ARM_CPU_MODE_UND) {
10465 env->xregs[22] = env->regs[14];
10466 env->xregs[23] = env->regs[13];
10467 } else {
10468 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10469 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
e33cf0f8
PM
10470 }
10471
10472 /*
7aab5a8c
PMD
10473 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
10474 * mode, then we can copy from r8-r14. Otherwise, we copy from the
10475 * FIQ bank for r8-r14.
e33cf0f8 10476 */
7aab5a8c
PMD
10477 if (mode == ARM_CPU_MODE_FIQ) {
10478 for (i = 24; i < 31; i++) {
10479 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
10480 }
10481 } else {
10482 for (i = 24; i < 29; i++) {
10483 env->xregs[i] = env->fiq_regs[i - 24];
e33cf0f8 10484 }
7aab5a8c
PMD
10485 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10486 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
e33cf0f8 10487 }
7aab5a8c
PMD
10488
10489 env->pc = env->regs[15];
e33cf0f8
PM
10490}
10491
9a223097 10492/*
7aab5a8c
PMD
10493 * Function used to synchronize QEMU's AArch32 register set with AArch64
10494 * register set. This is necessary when switching between AArch32 and AArch64
10495 * execution state.
de2db7ec 10496 */
7aab5a8c 10497void aarch64_sync_64_to_32(CPUARMState *env)
9ee6e8bb 10498{
7aab5a8c
PMD
10499 int i;
10500 uint32_t mode = env->uncached_cpsr & CPSR_M;
abc24d86 10501
7aab5a8c
PMD
10502 /* We can blanket copy X[0:7] to R[0:7] */
10503 for (i = 0; i < 8; i++) {
10504 env->regs[i] = env->xregs[i];
de2db7ec 10505 }
3f0cddee 10506
9a223097 10507 /*
7aab5a8c
PMD
10508 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10509 * Otherwise, we copy x8-x12 into the banked user regs.
de2db7ec 10510 */
7aab5a8c
PMD
10511 if (mode == ARM_CPU_MODE_FIQ) {
10512 for (i = 8; i < 13; i++) {
10513 env->usr_regs[i - 8] = env->xregs[i];
10514 }
10515 } else {
10516 for (i = 8; i < 13; i++) {
10517 env->regs[i] = env->xregs[i];
10518 }
fb602cb7
PM
10519 }
10520
9a223097 10521 /*
7aab5a8c
PMD
10522 * Registers r13 & r14 depend on the current mode.
10523 * If we are in a given mode, we copy the corresponding x registers to r13
10524 * and r14. Otherwise, we copy the x register to the banked r13 and r14
10525 * for the mode.
fb602cb7 10526 */
7aab5a8c
PMD
10527 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10528 env->regs[13] = env->xregs[13];
10529 env->regs[14] = env->xregs[14];
fb602cb7 10530 } else {
7aab5a8c 10531 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
fb602cb7 10532
7aab5a8c
PMD
10533 /*
10534 * HYP is an exception in that it does not have its own banked r14 but
10535 * shares the USR r14
10536 */
10537 if (mode == ARM_CPU_MODE_HYP) {
10538 env->regs[14] = env->xregs[14];
10539 } else {
10540 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10541 }
10542 }
fb602cb7 10543
7aab5a8c
PMD
10544 if (mode == ARM_CPU_MODE_HYP) {
10545 env->regs[13] = env->xregs[15];
fb602cb7 10546 } else {
7aab5a8c 10547 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
fb602cb7 10548 }
d02a8698 10549
7aab5a8c
PMD
10550 if (mode == ARM_CPU_MODE_IRQ) {
10551 env->regs[14] = env->xregs[16];
10552 env->regs[13] = env->xregs[17];
d02a8698 10553 } else {
7aab5a8c
PMD
10554 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10555 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
d02a8698
PM
10556 }
10557
7aab5a8c
PMD
10558 if (mode == ARM_CPU_MODE_SVC) {
10559 env->regs[14] = env->xregs[18];
10560 env->regs[13] = env->xregs[19];
10561 } else {
10562 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10563 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
fb602cb7
PM
10564 }
10565
7aab5a8c
PMD
10566 if (mode == ARM_CPU_MODE_ABT) {
10567 env->regs[14] = env->xregs[20];
10568 env->regs[13] = env->xregs[21];
10569 } else {
10570 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10571 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
10572 }
10573
10574 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
10575 env->regs[14] = env->xregs[22];
10576 env->regs[13] = env->xregs[23];
ce02049d 10577 } else {
593cfa2b 10578 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
3a9148d0 10579 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
10580 }
10581
9b37a28c
FR
10582 /*
10583 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
ce02049d
GB
10584 * mode, then we can copy to r8-r14. Otherwise, we copy to the
10585 * FIQ bank for r8-r14.
10586 */
10587 if (mode == ARM_CPU_MODE_FIQ) {
10588 for (i = 24; i < 31; i++) {
10589 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
10590 }
10591 } else {
10592 for (i = 24; i < 29; i++) {
10593 env->fiq_regs[i - 24] = env->xregs[i];
10594 }
10595 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
593cfa2b 10596 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
ce02049d
GB
10597 }
10598
10599 env->regs[15] = env->pc;
10600}
10601
dea8378b
PM
10602static void take_aarch32_exception(CPUARMState *env, int new_mode,
10603 uint32_t mask, uint32_t offset,
10604 uint32_t newpc)
10605{
4a2696c0
RH
10606 int new_el;
10607
dea8378b
PM
10608 /* Change the CPU state so as to actually take the exception. */
10609 switch_mode(env, new_mode);
4a2696c0 10610
dea8378b
PM
10611 /*
10612 * For exceptions taken to AArch32 we must clear the SS bit in both
10613 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10614 */
f944a854 10615 env->pstate &= ~PSTATE_SS;
dea8378b
PM
10616 env->spsr = cpsr_read(env);
10617 /* Clear IT bits. */
10618 env->condexec_bits = 0;
10619 /* Switch to the new mode, and to the correct instruction set. */
10620 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
88828bf1
CD
10621
10622 /* This must be after mode switching. */
10623 new_el = arm_current_el(env);
10624
dea8378b
PM
10625 /* Set new mode endianness */
10626 env->uncached_cpsr &= ~CPSR_E;
4a2696c0 10627 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
dea8378b
PM
10628 env->uncached_cpsr |= CPSR_E;
10629 }
829f9fd3
PM
10630 /* J and IL must always be cleared for exception entry */
10631 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
dea8378b
PM
10632 env->daif |= mask;
10633
f2f68a78
RC
10634 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10635 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10636 env->uncached_cpsr |= CPSR_SSBS;
10637 } else {
10638 env->uncached_cpsr &= ~CPSR_SSBS;
10639 }
10640 }
10641
dea8378b
PM
10642 if (new_mode == ARM_CPU_MODE_HYP) {
10643 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10644 env->elr_el[2] = env->regs[15];
10645 } else {
4a2696c0 10646 /* CPSR.PAN is normally preserved preserved unless... */
f8af1143 10647 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
4a2696c0
RH
10648 switch (new_el) {
10649 case 3:
10650 if (!arm_is_secure_below_el3(env)) {
10651 /* ... the target is EL3, from non-secure state. */
10652 env->uncached_cpsr &= ~CPSR_PAN;
10653 break;
10654 }
10655 /* ... the target is EL3, from secure state ... */
10656 /* fall through */
10657 case 1:
10658 /* ... the target is EL1 and SCTLR.SPAN is 0. */
10659 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10660 env->uncached_cpsr |= CPSR_PAN;
10661 }
10662 break;
10663 }
10664 }
dea8378b
PM
10665 /*
10666 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10667 * and we should just guard the thumb mode on V4
10668 */
10669 if (arm_feature(env, ARM_FEATURE_V4T)) {
10670 env->thumb =
10671 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10672 }
10673 env->regs[14] = env->regs[15] + offset;
10674 }
10675 env->regs[15] = newpc;
2b77ad4d
FR
10676
10677 if (tcg_enabled()) {
10678 arm_rebuild_hflags(env);
10679 }
dea8378b
PM
10680}
10681
b9bc21ff
PM
10682static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10683{
10684 /*
10685 * Handle exception entry to Hyp mode; this is sufficiently
10686 * different to entry to other AArch32 modes that we handle it
10687 * separately here.
10688 *
10689 * The vector table entry used is always the 0x14 Hyp mode entry point,
2c023d36 10690 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
b9bc21ff
PM
10691 * The offset applied to the preferred return address is always zero
10692 * (see DDI0487C.a section G1.12.3).
10693 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10694 */
10695 uint32_t addr, mask;
10696 ARMCPU *cpu = ARM_CPU(cs);
10697 CPUARMState *env = &cpu->env;
10698
10699 switch (cs->exception_index) {
10700 case EXCP_UDEF:
10701 addr = 0x04;
10702 break;
10703 case EXCP_SWI:
2c023d36 10704 addr = 0x08;
b9bc21ff
PM
10705 break;
10706 case EXCP_BKPT:
10707 /* Fall through to prefetch abort. */
10708 case EXCP_PREFETCH_ABORT:
10709 env->cp15.ifar_s = env->exception.vaddress;
10710 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10711 (uint32_t)env->exception.vaddress);
10712 addr = 0x0c;
10713 break;
10714 case EXCP_DATA_ABORT:
10715 env->cp15.dfar_s = env->exception.vaddress;
10716 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10717 (uint32_t)env->exception.vaddress);
10718 addr = 0x10;
10719 break;
10720 case EXCP_IRQ:
10721 addr = 0x18;
10722 break;
10723 case EXCP_FIQ:
10724 addr = 0x1c;
10725 break;
10726 case EXCP_HVC:
10727 addr = 0x08;
10728 break;
10729 case EXCP_HYP_TRAP:
10730 addr = 0x14;
9bbb4ef9 10731 break;
b9bc21ff
PM
10732 default:
10733 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10734 }
10735
10736 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
2ed08180
PM
10737 if (!arm_feature(env, ARM_FEATURE_V8)) {
10738 /*
10739 * QEMU syndrome values are v8-style. v7 has the IL bit
10740 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10741 * If this is a v7 CPU, squash the IL bit in those cases.
10742 */
10743 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10744 (cs->exception_index == EXCP_DATA_ABORT &&
10745 !(env->exception.syndrome & ARM_EL_ISV)) ||
10746 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10747 env->exception.syndrome &= ~ARM_EL_IL;
10748 }
10749 }
b9bc21ff
PM
10750 env->cp15.esr_el[2] = env->exception.syndrome;
10751 }
10752
10753 if (arm_current_el(env) != 2 && addr < 0x14) {
10754 addr = 0x14;
10755 }
10756
10757 mask = 0;
10758 if (!(env->cp15.scr_el3 & SCR_EA)) {
10759 mask |= CPSR_A;
10760 }
10761 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10762 mask |= CPSR_I;
10763 }
10764 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10765 mask |= CPSR_F;
10766 }
10767
10768 addr += env->cp15.hvbar;
10769
10770 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10771}
10772
966f758c 10773static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 10774{
97a8ea5a
AF
10775 ARMCPU *cpu = ARM_CPU(cs);
10776 CPUARMState *env = &cpu->env;
b5ff1b31
FB
10777 uint32_t addr;
10778 uint32_t mask;
10779 int new_mode;
10780 uint32_t offset;
16a906fd 10781 uint32_t moe;
b5ff1b31 10782
16a906fd 10783 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
64b91e3f 10784 switch (syn_get_ec(env->exception.syndrome)) {
16a906fd
PM
10785 case EC_BREAKPOINT:
10786 case EC_BREAKPOINT_SAME_EL:
10787 moe = 1;
10788 break;
10789 case EC_WATCHPOINT:
10790 case EC_WATCHPOINT_SAME_EL:
10791 moe = 10;
10792 break;
10793 case EC_AA32_BKPT:
10794 moe = 3;
10795 break;
10796 case EC_VECTORCATCH:
10797 moe = 5;
10798 break;
10799 default:
10800 moe = 0;
10801 break;
10802 }
10803
10804 if (moe) {
10805 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10806 }
10807
b9bc21ff
PM
10808 if (env->exception.target_el == 2) {
10809 arm_cpu_do_interrupt_aarch32_hyp(cs);
10810 return;
10811 }
10812
27103424 10813 switch (cs->exception_index) {
b5ff1b31
FB
10814 case EXCP_UDEF:
10815 new_mode = ARM_CPU_MODE_UND;
10816 addr = 0x04;
10817 mask = CPSR_I;
f927dbda 10818 if (env->thumb) {
b5ff1b31 10819 offset = 2;
f927dbda 10820 } else {
b5ff1b31 10821 offset = 4;
f927dbda 10822 }
b5ff1b31
FB
10823 break;
10824 case EXCP_SWI:
10825 new_mode = ARM_CPU_MODE_SVC;
10826 addr = 0x08;
10827 mask = CPSR_I;
601d70b9 10828 /* The PC already points to the next instruction. */
b5ff1b31
FB
10829 offset = 0;
10830 break;
06c949e6 10831 case EXCP_BKPT:
9ee6e8bb
PB
10832 /* Fall through to prefetch abort. */
10833 case EXCP_PREFETCH_ABORT:
88ca1c2d 10834 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 10835 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 10836 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 10837 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
10838 new_mode = ARM_CPU_MODE_ABT;
10839 addr = 0x0c;
10840 mask = CPSR_A | CPSR_I;
10841 offset = 4;
10842 break;
10843 case EXCP_DATA_ABORT:
4a7e2d73 10844 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 10845 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 10846 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 10847 env->exception.fsr,
6cd8a264 10848 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
10849 new_mode = ARM_CPU_MODE_ABT;
10850 addr = 0x10;
10851 mask = CPSR_A | CPSR_I;
10852 offset = 8;
10853 break;
10854 case EXCP_IRQ:
10855 new_mode = ARM_CPU_MODE_IRQ;
10856 addr = 0x18;
10857 /* Disable IRQ and imprecise data aborts. */
10858 mask = CPSR_A | CPSR_I;
10859 offset = 4;
de38d23b
FA
10860 if (env->cp15.scr_el3 & SCR_IRQ) {
10861 /* IRQ routed to monitor mode */
10862 new_mode = ARM_CPU_MODE_MON;
10863 mask |= CPSR_F;
10864 }
b5ff1b31
FB
10865 break;
10866 case EXCP_FIQ:
10867 new_mode = ARM_CPU_MODE_FIQ;
10868 addr = 0x1c;
10869 /* Disable FIQ, IRQ and imprecise data aborts. */
10870 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
10871 if (env->cp15.scr_el3 & SCR_FIQ) {
10872 /* FIQ routed to monitor mode */
10873 new_mode = ARM_CPU_MODE_MON;
10874 }
b5ff1b31
FB
10875 offset = 4;
10876 break;
87a4b270
PM
10877 case EXCP_VIRQ:
10878 new_mode = ARM_CPU_MODE_IRQ;
10879 addr = 0x18;
10880 /* Disable IRQ and imprecise data aborts. */
10881 mask = CPSR_A | CPSR_I;
10882 offset = 4;
10883 break;
10884 case EXCP_VFIQ:
10885 new_mode = ARM_CPU_MODE_FIQ;
10886 addr = 0x1c;
10887 /* Disable FIQ, IRQ and imprecise data aborts. */
10888 mask = CPSR_A | CPSR_I | CPSR_F;
10889 offset = 4;
10890 break;
3c29632f
RH
10891 case EXCP_VSERR:
10892 {
10893 /*
10894 * Note that this is reported as a data abort, but the DFAR
10895 * has an UNKNOWN value. Construct the SError syndrome from
10896 * AET and ExT fields.
10897 */
10898 ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10899
10900 if (extended_addresses_enabled(env)) {
10901 env->exception.fsr = arm_fi_to_lfsc(&fi);
10902 } else {
10903 env->exception.fsr = arm_fi_to_sfsc(&fi);
10904 }
10905 env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10906 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10907 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10908 env->exception.fsr);
10909
10910 new_mode = ARM_CPU_MODE_ABT;
10911 addr = 0x10;
10912 mask = CPSR_A | CPSR_I;
10913 offset = 8;
10914 }
10915 break;
dbe9d163
FA
10916 case EXCP_SMC:
10917 new_mode = ARM_CPU_MODE_MON;
10918 addr = 0x08;
10919 mask = CPSR_A | CPSR_I | CPSR_F;
10920 offset = 0;
10921 break;
b5ff1b31 10922 default:
a47dddd7 10923 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
10924 return; /* Never happens. Keep compiler happy. */
10925 }
e89e51a1
FA
10926
10927 if (new_mode == ARM_CPU_MODE_MON) {
10928 addr += env->cp15.mvbar;
137feaa9 10929 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 10930 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 10931 addr += 0xffff0000;
8641136c 10932 } else {
9b37a28c
FR
10933 /*
10934 * ARM v7 architectures provide a vector base address register to remap
8641136c 10935 * the interrupt vector table.
e89e51a1 10936 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
10937 * Note: only bits 31:5 are valid.
10938 */
fb6c91ba 10939 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 10940 }
dbe9d163
FA
10941
10942 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10943 env->cp15.scr_el3 &= ~SCR_NS;
10944 }
10945
dea8378b 10946 take_aarch32_exception(env, new_mode, mask, offset, addr);
b5ff1b31
FB
10947}
10948
a65dabf7
PM
10949static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10950{
10951 /*
10952 * Return the register number of the AArch64 view of the AArch32
10953 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10954 * be that of the AArch32 mode the exception came from.
10955 */
10956 int mode = env->uncached_cpsr & CPSR_M;
10957
10958 switch (aarch32_reg) {
10959 case 0 ... 7:
10960 return aarch32_reg;
10961 case 8 ... 12:
10962 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10963 case 13:
10964 switch (mode) {
10965 case ARM_CPU_MODE_USR:
10966 case ARM_CPU_MODE_SYS:
10967 return 13;
10968 case ARM_CPU_MODE_HYP:
10969 return 15;
10970 case ARM_CPU_MODE_IRQ:
10971 return 17;
10972 case ARM_CPU_MODE_SVC:
10973 return 19;
10974 case ARM_CPU_MODE_ABT:
10975 return 21;
10976 case ARM_CPU_MODE_UND:
10977 return 23;
10978 case ARM_CPU_MODE_FIQ:
10979 return 29;
10980 default:
10981 g_assert_not_reached();
10982 }
10983 case 14:
10984 switch (mode) {
10985 case ARM_CPU_MODE_USR:
10986 case ARM_CPU_MODE_SYS:
10987 case ARM_CPU_MODE_HYP:
10988 return 14;
10989 case ARM_CPU_MODE_IRQ:
10990 return 16;
10991 case ARM_CPU_MODE_SVC:
10992 return 18;
10993 case ARM_CPU_MODE_ABT:
10994 return 20;
10995 case ARM_CPU_MODE_UND:
10996 return 22;
10997 case ARM_CPU_MODE_FIQ:
10998 return 30;
10999 default:
11000 g_assert_not_reached();
11001 }
11002 case 15:
11003 return 31;
11004 default:
11005 g_assert_not_reached();
11006 }
11007}
11008
f944a854
RC
11009static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
11010{
11011 uint32_t ret = cpsr_read(env);
11012
11013 /* Move DIT to the correct location for SPSR_ELx */
11014 if (ret & CPSR_DIT) {
11015 ret &= ~CPSR_DIT;
11016 ret |= PSTATE_DIT;
11017 }
11018 /* Merge PSTATE.SS into SPSR_ELx */
11019 ret |= env->pstate & PSTATE_SS;
11020
11021 return ret;
11022}
11023
7ac61020
PM
11024static bool syndrome_is_sync_extabt(uint32_t syndrome)
11025{
11026 /* Return true if this syndrome value is a synchronous external abort */
11027 switch (syn_get_ec(syndrome)) {
11028 case EC_INSNABORT:
11029 case EC_INSNABORT_SAME_EL:
11030 case EC_DATAABORT:
11031 case EC_DATAABORT_SAME_EL:
11032 /* Look at fault status code for all the synchronous ext abort cases */
11033 switch (syndrome & 0x3f) {
11034 case 0x10:
11035 case 0x13:
11036 case 0x14:
11037 case 0x15:
11038 case 0x16:
11039 case 0x17:
11040 return true;
11041 default:
11042 return false;
11043 }
11044 default:
11045 return false;
11046 }
11047}
11048
966f758c
PM
11049/* Handle exception entry to a target EL which is using AArch64 */
11050static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
11051{
11052 ARMCPU *cpu = ARM_CPU(cs);
11053 CPUARMState *env = &cpu->env;
11054 unsigned int new_el = env->exception.target_el;
11055 target_ulong addr = env->cp15.vbar_el[new_el];
11056 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
4a2696c0 11057 unsigned int old_mode;
0ab5953b 11058 unsigned int cur_el = arm_current_el(env);
a65dabf7 11059 int rt;
0ab5953b 11060
d55b2a2a
CF
11061 if (tcg_enabled()) {
11062 /*
11063 * Note that new_el can never be 0. If cur_el is 0, then
11064 * el0_a64 is is_a64(), else el0_a64 is ignored.
11065 */
11066 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
11067 }
f3a9b694 11068
0ab5953b 11069 if (cur_el < new_el) {
9b37a28c
FR
11070 /*
11071 * Entry vector offset depends on whether the implemented EL
3d6f7617
PM
11072 * immediately lower than the target level is using AArch32 or AArch64
11073 */
11074 bool is_aa64;
cb092fbb 11075 uint64_t hcr;
3d6f7617
PM
11076
11077 switch (new_el) {
11078 case 3:
11079 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
11080 break;
11081 case 2:
cb092fbb
RH
11082 hcr = arm_hcr_el2_eff(env);
11083 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11084 is_aa64 = (hcr & HCR_RW) != 0;
11085 break;
11086 }
11087 /* fall through */
3d6f7617
PM
11088 case 1:
11089 is_aa64 = is_a64(env);
11090 break;
11091 default:
11092 g_assert_not_reached();
11093 }
11094
11095 if (is_aa64) {
f3a9b694
PM
11096 addr += 0x400;
11097 } else {
11098 addr += 0x600;
11099 }
11100 } else if (pstate_read(env) & PSTATE_SP) {
11101 addr += 0x200;
11102 }
11103
f3a9b694 11104 switch (cs->exception_index) {
11b76fda
RH
11105 case EXCP_GPC:
11106 qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
11107 env->cp15.mfar_el3);
11108 /* fall through */
f3a9b694
PM
11109 case EXCP_PREFETCH_ABORT:
11110 case EXCP_DATA_ABORT:
7ac61020
PM
11111 /*
11112 * FEAT_DoubleFault allows synchronous external aborts taken to EL3
11113 * to be taken to the SError vector entrypoint.
11114 */
11115 if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
11116 syndrome_is_sync_extabt(env->exception.syndrome)) {
11117 addr += 0x180;
11118 }
f3a9b694
PM
11119 env->cp15.far_el[new_el] = env->exception.vaddress;
11120 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
11121 env->cp15.far_el[new_el]);
11122 /* fall through */
11123 case EXCP_BKPT:
11124 case EXCP_UDEF:
11125 case EXCP_SWI:
11126 case EXCP_HVC:
11127 case EXCP_HYP_TRAP:
11128 case EXCP_SMC:
a65dabf7
PM
11129 switch (syn_get_ec(env->exception.syndrome)) {
11130 case EC_ADVSIMDFPACCESSTRAP:
4be42f40
PM
11131 /*
11132 * QEMU internal FP/SIMD syndromes from AArch32 include the
11133 * TA and coproc fields which are only exposed if the exception
11134 * is taken to AArch32 Hyp mode. Mask them out to get a valid
11135 * AArch64 format syndrome.
11136 */
11137 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
a65dabf7
PM
11138 break;
11139 case EC_CP14RTTRAP:
11140 case EC_CP15RTTRAP:
11141 case EC_CP14DTTRAP:
11142 /*
11143 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
11144 * the raw register field from the insn; when taking this to
11145 * AArch64 we must convert it to the AArch64 view of the register
11146 * number. Notice that we read a 4-bit AArch32 register number and
11147 * write back a 5-bit AArch64 one.
11148 */
11149 rt = extract32(env->exception.syndrome, 5, 4);
11150 rt = aarch64_regnum(env, rt);
11151 env->exception.syndrome = deposit32(env->exception.syndrome,
11152 5, 5, rt);
11153 break;
11154 case EC_CP15RRTTRAP:
11155 case EC_CP14RRTTRAP:
11156 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
11157 rt = extract32(env->exception.syndrome, 5, 4);
11158 rt = aarch64_regnum(env, rt);
11159 env->exception.syndrome = deposit32(env->exception.syndrome,
11160 5, 5, rt);
11161 rt = extract32(env->exception.syndrome, 10, 4);
11162 rt = aarch64_regnum(env, rt);
11163 env->exception.syndrome = deposit32(env->exception.syndrome,
11164 10, 5, rt);
11165 break;
4be42f40 11166 }
f3a9b694
PM
11167 env->cp15.esr_el[new_el] = env->exception.syndrome;
11168 break;
11169 case EXCP_IRQ:
11170 case EXCP_VIRQ:
11171 addr += 0x80;
11172 break;
11173 case EXCP_FIQ:
11174 case EXCP_VFIQ:
11175 addr += 0x100;
11176 break;
3c29632f
RH
11177 case EXCP_VSERR:
11178 addr += 0x180;
11179 /* Construct the SError syndrome from IDS and ISS fields. */
11180 env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
11181 env->cp15.esr_el[new_el] = env->exception.syndrome;
11182 break;
f3a9b694
PM
11183 default:
11184 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11185 }
11186
11187 if (is_a64(env)) {
4a2696c0 11188 old_mode = pstate_read(env);
f3a9b694
PM
11189 aarch64_save_sp(env, arm_current_el(env));
11190 env->elr_el[new_el] = env->pc;
11191 } else {
f944a854 11192 old_mode = cpsr_read_for_spsr_elx(env);
f3a9b694
PM
11193 env->elr_el[new_el] = env->regs[15];
11194
11195 aarch64_sync_32_to_64(env);
11196
11197 env->condexec_bits = 0;
11198 }
4a2696c0
RH
11199 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
11200
f3a9b694
PM
11201 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
11202 env->elr_el[new_el]);
11203
4a2696c0
RH
11204 if (cpu_isar_feature(aa64_pan, cpu)) {
11205 /* The value of PSTATE.PAN is normally preserved, except when ... */
11206 new_mode |= old_mode & PSTATE_PAN;
11207 switch (new_el) {
11208 case 2:
11209 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
11210 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
11211 != (HCR_E2H | HCR_TGE)) {
11212 break;
11213 }
11214 /* fall through */
11215 case 1:
11216 /* ... the target is EL1 ... */
11217 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
11218 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
11219 new_mode |= PSTATE_PAN;
11220 }
11221 break;
11222 }
11223 }
34669338
RH
11224 if (cpu_isar_feature(aa64_mte, cpu)) {
11225 new_mode |= PSTATE_TCO;
11226 }
4a2696c0 11227
f2f68a78
RC
11228 if (cpu_isar_feature(aa64_ssbs, cpu)) {
11229 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
11230 new_mode |= PSTATE_SSBS;
11231 } else {
11232 new_mode &= ~PSTATE_SSBS;
11233 }
11234 }
11235
f3a9b694 11236 pstate_write(env, PSTATE_DAIF | new_mode);
53221552 11237 env->aarch64 = true;
f3a9b694 11238 aarch64_restore_sp(env, new_el);
2b77ad4d
FR
11239
11240 if (tcg_enabled()) {
11241 helper_rebuild_hflags_a64(env, new_el);
11242 }
f3a9b694
PM
11243
11244 env->pc = addr;
11245
11246 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
11247 new_el, env->pc, pstate_read(env));
966f758c
PM
11248}
11249
ed6e6ba9
AB
11250/*
11251 * Do semihosting call and set the appropriate return value. All the
11252 * permission and validity checks have been done at translate time.
11253 *
11254 * We only see semihosting exceptions in TCG only as they are not
11255 * trapped to the hypervisor in KVM.
11256 */
91f78c58 11257#ifdef CONFIG_TCG
a06e3a68 11258static void tcg_handle_semihosting(CPUState *cs)
ed6e6ba9 11259{
904c04de
PM
11260 ARMCPU *cpu = ARM_CPU(cs);
11261 CPUARMState *env = &cpu->env;
11262
11263 if (is_a64(env)) {
ed6e6ba9
AB
11264 qemu_log_mask(CPU_LOG_INT,
11265 "...handling as semihosting call 0x%" PRIx64 "\n",
11266 env->xregs[0]);
ed3a06b1 11267 do_common_semihosting(cs);
4ff5ef9e 11268 env->pc += 4;
904c04de 11269 } else {
904c04de
PM
11270 qemu_log_mask(CPU_LOG_INT,
11271 "...handling as semihosting call 0x%x\n",
11272 env->regs[0]);
ed3a06b1 11273 do_common_semihosting(cs);
4ff5ef9e 11274 env->regs[15] += env->thumb ? 2 : 4;
904c04de
PM
11275 }
11276}
ed6e6ba9 11277#endif
904c04de 11278
9b37a28c
FR
11279/*
11280 * Handle a CPU exception for A and R profile CPUs.
966f758c
PM
11281 * Do any appropriate logging, handle PSCI calls, and then hand off
11282 * to the AArch64-entry or AArch32-entry function depending on the
11283 * target exception level's register width.
853bfef4
CF
11284 *
11285 * Note: this is used for both TCG (as the do_interrupt tcg op),
11286 * and KVM to re-inject guest debug exceptions, and to
11287 * inject a Synchronous-External-Abort.
966f758c
PM
11288 */
11289void arm_cpu_do_interrupt(CPUState *cs)
11290{
11291 ARMCPU *cpu = ARM_CPU(cs);
11292 CPUARMState *env = &cpu->env;
11293 unsigned int new_el = env->exception.target_el;
11294
531c60a9 11295 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c 11296
fc6177af 11297 arm_log_exception(cs);
966f758c
PM
11298 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
11299 new_el);
11300 if (qemu_loglevel_mask(CPU_LOG_INT)
11301 && !excp_is_internal(cs->exception_index)) {
6568da45 11302 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
64b91e3f 11303 syn_get_ec(env->exception.syndrome),
966f758c
PM
11304 env->exception.syndrome);
11305 }
11306
0c1aaa66 11307 if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
966f758c
PM
11308 arm_handle_psci_call(cpu);
11309 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
11310 return;
11311 }
11312
ed6e6ba9
AB
11313 /*
11314 * Semihosting semantics depend on the register width of the code
11315 * that caused the exception, not the target exception level, so
11316 * must be handled here.
966f758c 11317 */
ed6e6ba9
AB
11318#ifdef CONFIG_TCG
11319 if (cs->exception_index == EXCP_SEMIHOST) {
a06e3a68 11320 tcg_handle_semihosting(cs);
904c04de
PM
11321 return;
11322 }
ed6e6ba9 11323#endif
904c04de 11324
9b37a28c
FR
11325 /*
11326 * Hooks may change global state so BQL should be held, also the
b5c53d1b
AL
11327 * BQL needs to be held for any modification of
11328 * cs->interrupt_request.
11329 */
11330 g_assert(qemu_mutex_iothread_locked());
11331
11332 arm_call_pre_el_change_hook(cpu);
11333
904c04de
PM
11334 assert(!excp_is_internal(cs->exception_index));
11335 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
11336 arm_cpu_do_interrupt_aarch64(cs);
11337 } else {
11338 arm_cpu_do_interrupt_aarch32(cs);
11339 }
f3a9b694 11340
bd7d00fc
PM
11341 arm_call_el_change_hook(cpu);
11342
f3a9b694
PM
11343 if (!kvm_enabled()) {
11344 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
11345 }
11346}
c47eaf9f 11347#endif /* !CONFIG_USER_ONLY */
0480f69a 11348
aaec1432
RH
11349uint64_t arm_sctlr(CPUARMState *env, int el)
11350{
11351 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
11352 if (el == 0) {
11353 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
d902ae75 11354 el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
aaec1432
RH
11355 }
11356 return env->cp15.sctlr_el[el];
11357}
c47eaf9f 11358
8ae08860 11359int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
b830a5ee
RH
11360{
11361 if (regime_has_2_ranges(mmu_idx)) {
11362 return extract64(tcr, 37, 2);
edc05dd4 11363 } else if (regime_is_stage2(mmu_idx)) {
b830a5ee
RH
11364 return 0; /* VTCR_EL2 */
11365 } else {
3e270f67
RH
11366 /* Replicate the single TBI bit so we always have 2 bits. */
11367 return extract32(tcr, 20, 1) * 3;
b830a5ee
RH
11368 }
11369}
11370
8ae08860 11371int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
b830a5ee
RH
11372{
11373 if (regime_has_2_ranges(mmu_idx)) {
11374 return extract64(tcr, 51, 2);
edc05dd4 11375 } else if (regime_is_stage2(mmu_idx)) {
b830a5ee
RH
11376 return 0; /* VTCR_EL2 */
11377 } else {
3e270f67
RH
11378 /* Replicate the single TBID bit so we always have 2 bits. */
11379 return extract32(tcr, 29, 1) * 3;
b830a5ee
RH
11380 }
11381}
11382
671efad1 11383int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
81ae05fa
RH
11384{
11385 if (regime_has_2_ranges(mmu_idx)) {
11386 return extract64(tcr, 57, 2);
11387 } else {
11388 /* Replicate the single TCMA bit so we always have 2 bits. */
11389 return extract32(tcr, 30, 1) * 3;
11390 }
11391}
11392
104f703d
PM
11393static ARMGranuleSize tg0_to_gran_size(int tg)
11394{
11395 switch (tg) {
11396 case 0:
11397 return Gran4K;
11398 case 1:
11399 return Gran64K;
11400 case 2:
11401 return Gran16K;
11402 default:
11403 return GranInvalid;
11404 }
11405}
11406
11407static ARMGranuleSize tg1_to_gran_size(int tg)
11408{
11409 switch (tg) {
11410 case 1:
11411 return Gran16K;
11412 case 2:
11413 return Gran4K;
11414 case 3:
11415 return Gran64K;
11416 default:
11417 return GranInvalid;
11418 }
11419}
11420
11421static inline bool have4k(ARMCPU *cpu, bool stage2)
11422{
11423 return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
11424 : cpu_isar_feature(aa64_tgran4, cpu);
11425}
11426
11427static inline bool have16k(ARMCPU *cpu, bool stage2)
11428{
11429 return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
11430 : cpu_isar_feature(aa64_tgran16, cpu);
11431}
11432
11433static inline bool have64k(ARMCPU *cpu, bool stage2)
11434{
11435 return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
11436 : cpu_isar_feature(aa64_tgran64, cpu);
11437}
11438
11439static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
11440 bool stage2)
11441{
11442 switch (gran) {
11443 case Gran4K:
11444 if (have4k(cpu, stage2)) {
11445 return gran;
11446 }
11447 break;
11448 case Gran16K:
11449 if (have16k(cpu, stage2)) {
11450 return gran;
11451 }
11452 break;
11453 case Gran64K:
11454 if (have64k(cpu, stage2)) {
11455 return gran;
11456 }
11457 break;
11458 case GranInvalid:
11459 break;
11460 }
11461 /*
11462 * If the guest selects a granule size that isn't implemented,
11463 * the architecture requires that we behave as if it selected one
11464 * that is (with an IMPDEF choice of which one to pick). We choose
11465 * to implement the smallest supported granule size.
11466 */
11467 if (have4k(cpu, stage2)) {
11468 return Gran4K;
11469 }
11470 if (have16k(cpu, stage2)) {
11471 return Gran16K;
11472 }
11473 assert(have64k(cpu, stage2));
11474 return Gran64K;
11475}
11476
b830a5ee 11477ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
478dccbb
PM
11478 ARMMMUIdx mmu_idx, bool data,
11479 bool el1_is_aa32)
ba97be9f 11480{
c1547bba 11481 uint64_t tcr = regime_tcr(env, mmu_idx);
89739227 11482 bool epd, hpd, tsz_oob, ds, ha, hd;
ef56c242 11483 int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
104f703d 11484 ARMGranuleSize gran;
ef56c242 11485 ARMCPU *cpu = env_archcpu(env);
edc05dd4 11486 bool stage2 = regime_is_stage2(mmu_idx);
ba97be9f 11487
339370b9 11488 if (!regime_has_2_ranges(mmu_idx)) {
71d18164 11489 select = 0;
ba97be9f 11490 tsz = extract32(tcr, 0, 6);
104f703d
PM
11491 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11492 if (stage2) {
ba97be9f 11493 /* VTCR_EL2 */
b830a5ee 11494 hpd = false;
ba97be9f 11495 } else {
ba97be9f
RH
11496 hpd = extract32(tcr, 24, 1);
11497 }
11498 epd = false;
ef56c242 11499 sh = extract32(tcr, 12, 2);
f4ecc015 11500 ps = extract32(tcr, 16, 3);
89739227
RH
11501 ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11502 hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
ef56c242 11503 ds = extract64(tcr, 32, 1);
ba97be9f 11504 } else {
e4c93e44
PM
11505 bool e0pd;
11506
71d18164
RH
11507 /*
11508 * Bit 55 is always between the two regions, and is canonical for
11509 * determining if address tagging is enabled.
11510 */
11511 select = extract64(va, 55, 1);
11512 if (!select) {
11513 tsz = extract32(tcr, 0, 6);
104f703d 11514 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
71d18164 11515 epd = extract32(tcr, 7, 1);
ef56c242 11516 sh = extract32(tcr, 12, 2);
71d18164 11517 hpd = extract64(tcr, 41, 1);
e4c93e44 11518 e0pd = extract64(tcr, 55, 1);
71d18164 11519 } else {
71d18164 11520 tsz = extract32(tcr, 16, 6);
104f703d 11521 gran = tg1_to_gran_size(extract32(tcr, 30, 2));
71d18164 11522 epd = extract32(tcr, 23, 1);
ef56c242 11523 sh = extract32(tcr, 28, 2);
71d18164 11524 hpd = extract64(tcr, 42, 1);
e4c93e44 11525 e0pd = extract64(tcr, 56, 1);
71d18164 11526 }
f4ecc015 11527 ps = extract64(tcr, 32, 3);
89739227
RH
11528 ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11529 hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
ef56c242 11530 ds = extract64(tcr, 59, 1);
e4c93e44
PM
11531
11532 if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11533 regime_is_user(env, mmu_idx)) {
11534 epd = true;
11535 }
ba97be9f 11536 }
c36c65ea 11537
104f703d 11538 gran = sanitize_gran_size(cpu, gran, stage2);
104f703d 11539
ef56c242 11540 if (cpu_isar_feature(aa64_st, cpu)) {
3c003f70 11541 max_tsz = 48 - (gran == Gran64K);
c36c65ea
RDC
11542 } else {
11543 max_tsz = 39;
11544 }
0af312b6 11545
ef56c242
RH
11546 /*
11547 * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11548 * adjust the effective value of DS, as documented.
11549 */
0af312b6 11550 min_tsz = 16;
3c003f70 11551 if (gran == Gran64K) {
ef56c242
RH
11552 if (cpu_isar_feature(aa64_lva, cpu)) {
11553 min_tsz = 12;
11554 }
11555 ds = false;
11556 } else if (ds) {
edc05dd4 11557 if (regime_is_stage2(mmu_idx)) {
3c003f70 11558 if (gran == Gran16K) {
ef56c242
RH
11559 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11560 } else {
11561 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11562 }
edc05dd4 11563 } else {
3c003f70 11564 if (gran == Gran16K) {
ef56c242
RH
11565 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11566 } else {
11567 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11568 }
ef56c242
RH
11569 }
11570 if (ds) {
0af312b6
RH
11571 min_tsz = 12;
11572 }
11573 }
c36c65ea 11574
478dccbb
PM
11575 if (stage2 && el1_is_aa32) {
11576 /*
11577 * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
11578 * are loosened: a configured IPA of 40 bits is permitted even if
11579 * the implemented PA is less than that (and so a 40 bit IPA would
11580 * fault for an AArch64 EL1). See R_DTLMN.
11581 */
11582 min_tsz = MIN(min_tsz, 24);
11583 }
11584
ebf93ce7
RH
11585 if (tsz > max_tsz) {
11586 tsz = max_tsz;
11587 tsz_oob = true;
11588 } else if (tsz < min_tsz) {
11589 tsz = min_tsz;
11590 tsz_oob = true;
11591 } else {
11592 tsz_oob = false;
11593 }
ba97be9f 11594
b830a5ee
RH
11595 /* Present TBI as a composite with TBID. */
11596 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11597 if (!data) {
11598 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11599 }
11600 tbi = (tbi >> select) & 1;
11601
ba97be9f
RH
11602 return (ARMVAParameters) {
11603 .tsz = tsz,
f4ecc015 11604 .ps = ps,
ef56c242 11605 .sh = sh,
ba97be9f
RH
11606 .select = select,
11607 .tbi = tbi,
11608 .epd = epd,
11609 .hpd = hpd,
ebf93ce7 11610 .tsz_oob = tsz_oob,
ef56c242 11611 .ds = ds,
89739227
RH
11612 .ha = ha,
11613 .hd = ha && hd,
3c003f70 11614 .gran = gran,
ba97be9f
RH
11615 };
11616}
11617
9b37a28c
FR
11618/*
11619 * Note that signed overflow is undefined in C. The following routines are
11620 * careful to use unsigned types where modulo arithmetic is required.
11621 * Failure to do so _will_ break on newer gcc.
11622 */
6ddbc6e4
PB
11623
11624/* Signed saturating arithmetic. */
11625
1654b2d6 11626/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
11627static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11628{
11629 uint16_t res;
11630
11631 res = a + b;
11632 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
f927dbda 11633 if (a & 0x8000) {
6ddbc6e4 11634 res = 0x8000;
f927dbda 11635 } else {
6ddbc6e4 11636 res = 0x7fff;
f927dbda 11637 }
6ddbc6e4
PB
11638 }
11639 return res;
11640}
11641
1654b2d6 11642/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
11643static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11644{
11645 uint8_t res;
11646
11647 res = a + b;
11648 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
f927dbda 11649 if (a & 0x80) {
6ddbc6e4 11650 res = 0x80;
f927dbda 11651 } else {
6ddbc6e4 11652 res = 0x7f;
f927dbda 11653 }
6ddbc6e4
PB
11654 }
11655 return res;
11656}
11657
1654b2d6 11658/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
11659static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11660{
11661 uint16_t res;
11662
11663 res = a - b;
11664 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
f927dbda 11665 if (a & 0x8000) {
6ddbc6e4 11666 res = 0x8000;
f927dbda 11667 } else {
6ddbc6e4 11668 res = 0x7fff;
f927dbda 11669 }
6ddbc6e4
PB
11670 }
11671 return res;
11672}
11673
1654b2d6 11674/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
11675static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11676{
11677 uint8_t res;
11678
11679 res = a - b;
11680 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
f927dbda 11681 if (a & 0x80) {
6ddbc6e4 11682 res = 0x80;
f927dbda 11683 } else {
6ddbc6e4 11684 res = 0x7f;
f927dbda 11685 }
6ddbc6e4
PB
11686 }
11687 return res;
11688}
11689
11690#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11691#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11692#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
11693#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
11694#define PFX q
11695
11696#include "op_addsub.h"
11697
11698/* Unsigned saturating arithmetic. */
460a09c1 11699static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
11700{
11701 uint16_t res;
11702 res = a + b;
f927dbda 11703 if (res < a) {
6ddbc6e4 11704 res = 0xffff;
f927dbda 11705 }
6ddbc6e4
PB
11706 return res;
11707}
11708
460a09c1 11709static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 11710{
f927dbda 11711 if (a > b) {
6ddbc6e4 11712 return a - b;
f927dbda 11713 } else {
6ddbc6e4 11714 return 0;
f927dbda 11715 }
6ddbc6e4
PB
11716}
11717
11718static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11719{
11720 uint8_t res;
11721 res = a + b;
f927dbda 11722 if (res < a) {
6ddbc6e4 11723 res = 0xff;
f927dbda 11724 }
6ddbc6e4
PB
11725 return res;
11726}
11727
11728static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11729{
f927dbda 11730 if (a > b) {
6ddbc6e4 11731 return a - b;
f927dbda 11732 } else {
6ddbc6e4 11733 return 0;
f927dbda 11734 }
6ddbc6e4
PB
11735}
11736
11737#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11738#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11739#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
11740#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
11741#define PFX uq
11742
11743#include "op_addsub.h"
11744
11745/* Signed modulo arithmetic. */
11746#define SARITH16(a, b, n, op) do { \
11747 int32_t sum; \
db6e2e65 11748 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
11749 RESULT(sum, n, 16); \
11750 if (sum >= 0) \
11751 ge |= 3 << (n * 2); \
04215eb1 11752 } while (0)
6ddbc6e4
PB
11753
11754#define SARITH8(a, b, n, op) do { \
11755 int32_t sum; \
db6e2e65 11756 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
11757 RESULT(sum, n, 8); \
11758 if (sum >= 0) \
11759 ge |= 1 << n; \
04215eb1 11760 } while (0)
6ddbc6e4
PB
11761
11762
11763#define ADD16(a, b, n) SARITH16(a, b, n, +)
11764#define SUB16(a, b, n) SARITH16(a, b, n, -)
11765#define ADD8(a, b, n) SARITH8(a, b, n, +)
11766#define SUB8(a, b, n) SARITH8(a, b, n, -)
11767#define PFX s
11768#define ARITH_GE
11769
11770#include "op_addsub.h"
11771
11772/* Unsigned modulo arithmetic. */
11773#define ADD16(a, b, n) do { \
11774 uint32_t sum; \
11775 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11776 RESULT(sum, n, 16); \
a87aa10b 11777 if ((sum >> 16) == 1) \
6ddbc6e4 11778 ge |= 3 << (n * 2); \
04215eb1 11779 } while (0)
6ddbc6e4
PB
11780
11781#define ADD8(a, b, n) do { \
11782 uint32_t sum; \
11783 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11784 RESULT(sum, n, 8); \
a87aa10b
AZ
11785 if ((sum >> 8) == 1) \
11786 ge |= 1 << n; \
04215eb1 11787 } while (0)
6ddbc6e4
PB
11788
11789#define SUB16(a, b, n) do { \
11790 uint32_t sum; \
11791 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11792 RESULT(sum, n, 16); \
11793 if ((sum >> 16) == 0) \
11794 ge |= 3 << (n * 2); \
04215eb1 11795 } while (0)
6ddbc6e4
PB
11796
11797#define SUB8(a, b, n) do { \
11798 uint32_t sum; \
11799 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11800 RESULT(sum, n, 8); \
11801 if ((sum >> 8) == 0) \
a87aa10b 11802 ge |= 1 << n; \
04215eb1 11803 } while (0)
6ddbc6e4
PB
11804
11805#define PFX u
11806#define ARITH_GE
11807
11808#include "op_addsub.h"
11809
11810/* Halved signed arithmetic. */
11811#define ADD16(a, b, n) \
11812 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11813#define SUB16(a, b, n) \
11814 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11815#define ADD8(a, b, n) \
11816 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11817#define SUB8(a, b, n) \
11818 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11819#define PFX sh
11820
11821#include "op_addsub.h"
11822
11823/* Halved unsigned arithmetic. */
11824#define ADD16(a, b, n) \
11825 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11826#define SUB16(a, b, n) \
11827 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11828#define ADD8(a, b, n) \
11829 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11830#define SUB8(a, b, n) \
11831 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11832#define PFX uh
11833
11834#include "op_addsub.h"
11835
11836static inline uint8_t do_usad(uint8_t a, uint8_t b)
11837{
f927dbda 11838 if (a > b) {
6ddbc6e4 11839 return a - b;
f927dbda 11840 } else {
6ddbc6e4 11841 return b - a;
f927dbda 11842 }
6ddbc6e4
PB
11843}
11844
11845/* Unsigned sum of absolute byte differences. */
11846uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11847{
11848 uint32_t sum;
11849 sum = do_usad(a, b);
11850 sum += do_usad(a >> 8, b >> 8);
bdc3b6f5 11851 sum += do_usad(a >> 16, b >> 16);
6ddbc6e4
PB
11852 sum += do_usad(a >> 24, b >> 24);
11853 return sum;
11854}
11855
11856/* For ARMv6 SEL instruction. */
11857uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11858{
11859 uint32_t mask;
11860
11861 mask = 0;
f927dbda 11862 if (flags & 1) {
6ddbc6e4 11863 mask |= 0xff;
f927dbda
FR
11864 }
11865 if (flags & 2) {
6ddbc6e4 11866 mask |= 0xff00;
f927dbda
FR
11867 }
11868 if (flags & 4) {
6ddbc6e4 11869 mask |= 0xff0000;
f927dbda
FR
11870 }
11871 if (flags & 8) {
6ddbc6e4 11872 mask |= 0xff000000;
f927dbda 11873 }
6ddbc6e4
PB
11874 return (a & mask) | (b & ~mask);
11875}
11876
9b37a28c
FR
11877/*
11878 * CRC helpers.
aa633469
PM
11879 * The upper bytes of val (above the number specified by 'bytes') must have
11880 * been zeroed out by the caller.
11881 */
eb0ecd5a
WN
11882uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11883{
11884 uint8_t buf[4];
11885
aa633469 11886 stl_le_p(buf, val);
eb0ecd5a
WN
11887
11888 /* zlib crc32 converts the accumulator and output to one's complement. */
11889 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11890}
11891
11892uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11893{
11894 uint8_t buf[4];
11895
aa633469 11896 stl_le_p(buf, val);
eb0ecd5a
WN
11897
11898 /* Linux crc32c converts the output to one's complement. */
11899 return crc32c(acc, buf, bytes) ^ 0xffffffff;
11900}
a9e01311 11901
9b37a28c
FR
11902/*
11903 * Return the exception level to which FP-disabled exceptions should
a9e01311
RH
11904 * be taken, or 0 if FP is enabled.
11905 */
ced31551 11906int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 11907{
55faa212 11908#ifndef CONFIG_USER_ONLY
d5a6fa2d
RH
11909 uint64_t hcr_el2;
11910
9b37a28c
FR
11911 /*
11912 * CPACR and the CPTR registers don't exist before v6, so FP is
a9e01311
RH
11913 * always accessible
11914 */
11915 if (!arm_feature(env, ARM_FEATURE_V6)) {
11916 return 0;
11917 }
11918
d87513c0
PM
11919 if (arm_feature(env, ARM_FEATURE_M)) {
11920 /* CPACR can cause a NOCP UsageFault taken to current security state */
11921 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11922 return 1;
11923 }
11924
11925 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11926 if (!extract32(env->v7m.nsacr, 10, 1)) {
11927 /* FP insns cause a NOCP UsageFault taken to Secure */
11928 return 3;
11929 }
11930 }
11931
11932 return 0;
11933 }
11934
d5a6fa2d
RH
11935 hcr_el2 = arm_hcr_el2_eff(env);
11936
9b37a28c
FR
11937 /*
11938 * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
a9e01311
RH
11939 * 0, 2 : trap EL0 and EL1/PL1 accesses
11940 * 1 : trap only EL0 accesses
11941 * 3 : trap no accesses
c2ddb7cf 11942 * This register is ignored if E2H+TGE are both set.
a9e01311 11943 */
d5a6fa2d 11944 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
fab8ad39 11945 int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
c2ddb7cf
RH
11946
11947 switch (fpen) {
02e1de14
RH
11948 case 1:
11949 if (cur_el != 0) {
11950 break;
11951 }
11952 /* fall through */
c2ddb7cf
RH
11953 case 0:
11954 case 2:
02e1de14
RH
11955 /* Trap from Secure PL0 or PL1 to Secure PL1. */
11956 if (!arm_el_is_aa64(env, 3)
11957 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
a9e01311
RH
11958 return 3;
11959 }
02e1de14 11960 if (cur_el <= 1) {
c2ddb7cf
RH
11961 return 1;
11962 }
11963 break;
a9e01311 11964 }
a9e01311
RH
11965 }
11966
fc1120a7
PM
11967 /*
11968 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11969 * to control non-secure access to the FPU. It doesn't have any
11970 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11971 */
11972 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11973 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11974 if (!extract32(env->cp15.nsacr, 10, 1)) {
11975 /* FP insns act as UNDEF */
11976 return cur_el == 2 ? 2 : 1;
11977 }
11978 }
11979
d5a6fa2d
RH
11980 /*
11981 * CPTR_EL2 is present in v7VE or v8, and changes format
11982 * with HCR_EL2.E2H (regardless of TGE).
a9e01311 11983 */
d5a6fa2d
RH
11984 if (cur_el <= 2) {
11985 if (hcr_el2 & HCR_E2H) {
fab8ad39 11986 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
d5a6fa2d
RH
11987 case 1:
11988 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11989 break;
11990 }
11991 /* fall through */
11992 case 0:
11993 case 2:
11994 return 2;
11995 }
11996 } else if (arm_is_el2_enabled(env)) {
fab8ad39 11997 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
d5a6fa2d
RH
11998 return 2;
11999 }
12000 }
a9e01311
RH
12001 }
12002
12003 /* CPTR_EL3 : present in v8 */
fab8ad39 12004 if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
a9e01311
RH
12005 /* Trap all FP ops to EL3 */
12006 return 3;
12007 }
55faa212 12008#endif
a9e01311
RH
12009 return 0;
12010}
12011
b9f6033c
RH
12012/* Return the exception level we're running at if this is our mmu_idx */
12013int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12014{
12015 if (mmu_idx & ARM_MMU_IDX_M) {
12016 return mmu_idx & ARM_MMU_IDX_M_PRIV;
12017 }
12018
12019 switch (mmu_idx) {
12020 case ARMMMUIdx_E10_0:
12021 case ARMMMUIdx_E20_0:
b9f6033c
RH
12022 return 0;
12023 case ARMMMUIdx_E10_1:
452ef8cb 12024 case ARMMMUIdx_E10_1_PAN:
b9f6033c
RH
12025 return 1;
12026 case ARMMMUIdx_E2:
12027 case ARMMMUIdx_E20_2:
452ef8cb 12028 case ARMMMUIdx_E20_2_PAN:
b9f6033c 12029 return 2;
d902ae75 12030 case ARMMMUIdx_E3:
b9f6033c
RH
12031 return 3;
12032 default:
12033 g_assert_not_reached();
12034 }
12035}
12036
7aab5a8c 12037#ifndef CONFIG_TCG
65e4655c
RH
12038ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12039{
7aab5a8c 12040 g_assert_not_reached();
65e4655c 12041}
7aab5a8c 12042#endif
65e4655c 12043
6f2d9d74
TK
12044static bool arm_pan_enabled(CPUARMState *env)
12045{
12046 if (is_a64(env)) {
12047 return env->pstate & PSTATE_PAN;
12048 } else {
12049 return env->uncached_cpsr & CPSR_PAN;
12050 }
12051}
12052
164690b2 12053ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 12054{
b6ad6062
RDC
12055 ARMMMUIdx idx;
12056 uint64_t hcr;
12057
65e4655c 12058 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 12059 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
12060 }
12061
6003d980 12062 /* See ARM pseudo-function ELIsInHost. */
b9f6033c
RH
12063 switch (el) {
12064 case 0:
b6ad6062
RDC
12065 hcr = arm_hcr_el2_eff(env);
12066 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
12067 idx = ARMMMUIdx_E20_0;
12068 } else {
12069 idx = ARMMMUIdx_E10_0;
6003d980 12070 }
b6ad6062 12071 break;
b9f6033c 12072 case 1:
6f2d9d74 12073 if (arm_pan_enabled(env)) {
b6ad6062
RDC
12074 idx = ARMMMUIdx_E10_1_PAN;
12075 } else {
12076 idx = ARMMMUIdx_E10_1;
66412260 12077 }
b6ad6062 12078 break;
b9f6033c 12079 case 2:
6003d980 12080 /* Note that TGE does not apply at EL2. */
b6ad6062 12081 if (arm_hcr_el2_eff(env) & HCR_E2H) {
6f2d9d74 12082 if (arm_pan_enabled(env)) {
b6ad6062
RDC
12083 idx = ARMMMUIdx_E20_2_PAN;
12084 } else {
12085 idx = ARMMMUIdx_E20_2;
66412260 12086 }
b6ad6062
RDC
12087 } else {
12088 idx = ARMMMUIdx_E2;
6003d980 12089 }
b6ad6062 12090 break;
b9f6033c 12091 case 3:
d902ae75 12092 return ARMMMUIdx_E3;
b9f6033c
RH
12093 default:
12094 g_assert_not_reached();
65e4655c 12095 }
b6ad6062 12096
b6ad6062 12097 return idx;
50494a27
RH
12098}
12099
164690b2
RH
12100ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12101{
12102 return arm_mmu_idx_el(env, arm_current_el(env));
12103}
12104
26702213
PM
12105static bool mve_no_pred(CPUARMState *env)
12106{
12107 /*
12108 * Return true if there is definitely no predication of MVE
12109 * instructions by VPR or LTPSIZE. (Returning false even if there
12110 * isn't any predication is OK; generated code will just be
12111 * a little worse.)
12112 * If the CPU does not implement MVE then this TB flag is always 0.
12113 *
12114 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
12115 * logic in gen_update_fp_context() needs to be updated to match.
12116 *
12117 * We do not include the effect of the ECI bits here -- they are
12118 * tracked in other TB flags. This simplifies the logic for
12119 * "when did we emit code that changes the MVE_NO_PRED TB flag
12120 * and thus need to end the TB?".
12121 */
12122 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
12123 return false;
12124 }
12125 if (env->v7m.vpr) {
12126 return false;
12127 }
12128 if (env->v7m.ltpsize < 4) {
12129 return false;
12130 }
12131 return true;
12132}
12133
bb5de525
AJ
12134void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
12135 uint64_t *cs_base, uint32_t *pflags)
d4d7503a 12136{
3902bfc6 12137 CPUARMTBFlags flags;
d4d7503a 12138
0ee8b24a 12139 assert_hflags_rebuild_correctly(env);
3902bfc6 12140 flags = env->hflags;
3d74e2e9 12141
a729a46b 12142 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
d4d7503a 12143 *pc = env->pc;
d4d7503a 12144 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
a729a46b 12145 DP_TBFLAG_A64(flags, BTYPE, env->btype);
08f1434a 12146 }
a9e01311
RH
12147 } else {
12148 *pc = env->regs[15];
6e33ced5
RH
12149
12150 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
12151 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12152 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12153 != env->v7m.secure) {
a729a46b 12154 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
9550d1bd
RH
12155 }
12156
12157 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12158 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12159 (env->v7m.secure &&
12160 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12161 /*
12162 * ASPEN is set, but FPCA/SFPA indicate that there is no
12163 * active FP context; we must create a new FP context before
12164 * executing any FP insn.
12165 */
a729a46b 12166 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
9550d1bd
RH
12167 }
12168
12169 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12170 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
a729a46b 12171 DP_TBFLAG_M32(flags, LSPACT, 1);
9550d1bd 12172 }
26702213
PM
12173
12174 if (mve_no_pred(env)) {
12175 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12176 }
6e33ced5 12177 } else {
bbad7c62
RH
12178 /*
12179 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12180 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12181 */
12182 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
a729a46b 12183 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
bbad7c62 12184 } else {
a729a46b
RH
12185 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12186 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
bbad7c62 12187 }
0a54d68e 12188 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
a729a46b 12189 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 12190 }
6e33ced5
RH
12191 }
12192
a729a46b
RH
12193 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12194 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
d4d7503a 12195 }
a9e01311 12196
60e12c37
RH
12197 /*
12198 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
12199 * states defined in the ARM ARM for software singlestep:
12200 * SS_ACTIVE PSTATE.SS State
12201 * 0 x Inactive (the TB flag for SS is always 0)
12202 * 1 0 Active-pending
12203 * 1 1 Active-not-pending
ae6eb1e9 12204 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
a9e01311 12205 */
a729a46b
RH
12206 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12207 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
a9e01311 12208 }
a9e01311 12209
3902bfc6 12210 *pflags = flags.flags;
a378206a 12211 *cs_base = flags.flags2;
a9e01311 12212}
0ab5953b
RH
12213
12214#ifdef TARGET_AARCH64
12215/*
12216 * The manual says that when SVE is enabled and VQ is widened the
12217 * implementation is allowed to zero the previously inaccessible
12218 * portion of the registers. The corollary to that is that when
12219 * SVE is enabled and VQ is narrowed we are also allowed to zero
12220 * the now inaccessible portion of the registers.
12221 *
12222 * The intent of this is that no predicate bit beyond VQ is ever set.
12223 * Which means that some operations on predicate registers themselves
12224 * may operate on full uint64_t or even unrolled across the maximum
12225 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
12226 * may well be cheaper than conditionals to restrict the operation
12227 * to the relevant portion of a uint16_t[16].
12228 */
12229void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12230{
12231 int i, j;
12232 uint64_t pmask;
12233
12234 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 12235 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
12236
12237 /* Zap the high bits of the zregs. */
12238 for (i = 0; i < 32; i++) {
12239 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12240 }
12241
12242 /* Zap the high bits of the pregs and ffr. */
12243 pmask = 0;
12244 if (vq & 3) {
12245 pmask = ~(-1ULL << (16 * (vq & 3)));
12246 }
12247 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12248 for (i = 0; i < 17; ++i) {
12249 env->vfp.pregs[i].p[j] &= pmask;
12250 }
12251 pmask = 0;
12252 }
12253}
12254
6a775fd6
RH
12255static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
12256{
12257 int exc_el;
12258
12259 if (sm) {
12260 exc_el = sme_exception_el(env, el);
12261 } else {
12262 exc_el = sve_exception_el(env, el);
12263 }
12264 if (exc_el) {
12265 return 0; /* disabled */
12266 }
12267 return sve_vqm1_for_el_sm(env, el, sm);
12268}
12269
0ab5953b
RH
12270/*
12271 * Notice a change in SVE vector size when changing EL.
12272 */
9a05f7b6
RH
12273void aarch64_sve_change_el(CPUARMState *env, int old_el,
12274 int new_el, bool el0_a64)
0ab5953b 12275{
2fc0cc0e 12276 ARMCPU *cpu = env_archcpu(env);
0ab5953b 12277 int old_len, new_len;
6a775fd6 12278 bool old_a64, new_a64, sm;
0ab5953b
RH
12279
12280 /* Nothing to do if no SVE. */
cd208a1c 12281 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
12282 return;
12283 }
12284
12285 /* Nothing to do if FP is disabled in either EL. */
12286 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12287 return;
12288 }
12289
04fbce76
RH
12290 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12291 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12292
12293 /*
12294 * Both AArch64.TakeException and AArch64.ExceptionReturn
12295 * invoke ResetSVEState when taking an exception from, or
12296 * returning to, AArch32 state when PSTATE.SM is enabled.
12297 */
6a775fd6
RH
12298 sm = FIELD_EX64(env->svcr, SVCR, SM);
12299 if (old_a64 != new_a64 && sm) {
04fbce76
RH
12300 arm_reset_sve_state(env);
12301 return;
12302 }
12303
0ab5953b
RH
12304 /*
12305 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12306 * at ELx, or not available because the EL is in AArch32 state, then
12307 * for all purposes other than a direct read, the ZCR_ELx.LEN field
12308 * has an effective value of 0".
12309 *
12310 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12311 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12312 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
12313 * we already have the correct register contents when encountering the
12314 * vq0->vq0 transition between EL0->EL1.
12315 */
6a775fd6
RH
12316 old_len = new_len = 0;
12317 if (old_a64) {
12318 old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12319 }
12320 if (new_a64) {
12321 new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12322 }
0ab5953b
RH
12323
12324 /* When changing vector length, clear inaccessible state. */
12325 if (new_len < old_len) {
12326 aarch64_sve_narrow_vq(env, new_len + 1);
12327 }
12328}
12329#endif
5d28ac0c
RH
12330
12331#ifndef CONFIG_USER_ONLY
12332ARMSecuritySpace arm_security_space(CPUARMState *env)
12333{
12334 if (arm_feature(env, ARM_FEATURE_M)) {
12335 return arm_secure_to_space(env->v7m.secure);
12336 }
12337
12338 /*
12339 * If EL3 is not supported then the secure state is implementation
12340 * defined, in which case QEMU defaults to non-secure.
12341 */
12342 if (!arm_feature(env, ARM_FEATURE_EL3)) {
12343 return ARMSS_NonSecure;
12344 }
12345
12346 /* Check for AArch64 EL3 or AArch32 Mon. */
12347 if (is_a64(env)) {
12348 if (extract32(env->pstate, 2, 2) == 3) {
12349 if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
12350 return ARMSS_Root;
12351 } else {
12352 return ARMSS_Secure;
12353 }
12354 }
12355 } else {
12356 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
12357 return ARMSS_Secure;
12358 }
12359 }
12360
12361 return arm_security_space_below_el3(env);
12362}
12363
12364ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
12365{
12366 assert(!arm_feature(env, ARM_FEATURE_M));
12367
12368 /*
12369 * If EL3 is not supported then the secure state is implementation
12370 * defined, in which case QEMU defaults to non-secure.
12371 */
12372 if (!arm_feature(env, ARM_FEATURE_EL3)) {
12373 return ARMSS_NonSecure;
12374 }
12375
12376 /*
12377 * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
12378 * Ignoring NSE when !NS retains consistency without having to
12379 * modify other predicates.
12380 */
12381 if (!(env->cp15.scr_el3 & SCR_NS)) {
12382 return ARMSS_Secure;
12383 } else if (env->cp15.scr_el3 & SCR_NSE) {
12384 return ARMSS_Realm;
12385 } else {
12386 return ARMSS_NonSecure;
12387 }
12388}
12389#endif /* !CONFIG_USER_ONLY */