]> git.proxmox.com Git - mirror_qemu.git/blob - target/arm/helper.c
target/arm: Implement new FEAT_ECV trap bits
[mirror_qemu.git] / target / arm / helper.c
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 */
8
9 #include "qemu/osdep.h"
10 #include "qemu/log.h"
11 #include "trace.h"
12 #include "cpu.h"
13 #include "internals.h"
14 #include "cpu-features.h"
15 #include "exec/helper-proto.h"
16 #include "qemu/main-loop.h"
17 #include "qemu/timer.h"
18 #include "qemu/bitops.h"
19 #include "qemu/crc32c.h"
20 #include "qemu/qemu-print.h"
21 #include "exec/exec-all.h"
22 #include <zlib.h> /* For crc32 */
23 #include "hw/irq.h"
24 #include "sysemu/cpu-timers.h"
25 #include "sysemu/kvm.h"
26 #include "sysemu/tcg.h"
27 #include "qapi/error.h"
28 #include "qemu/guest-random.h"
29 #ifdef CONFIG_TCG
30 #include "semihosting/common-semi.h"
31 #endif
32 #include "cpregs.h"
33 #include "target/arm/gtimer.h"
34
35 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
36
37 static void switch_mode(CPUARMState *env, int mode);
38
39 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
40 {
41 assert(ri->fieldoffset);
42 if (cpreg_field_is_64bit(ri)) {
43 return CPREG_FIELD64(env, ri);
44 } else {
45 return CPREG_FIELD32(env, ri);
46 }
47 }
48
49 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
50 {
51 assert(ri->fieldoffset);
52 if (cpreg_field_is_64bit(ri)) {
53 CPREG_FIELD64(env, ri) = value;
54 } else {
55 CPREG_FIELD32(env, ri) = value;
56 }
57 }
58
59 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
60 {
61 return (char *)env + ri->fieldoffset;
62 }
63
64 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
65 {
66 /* Raw read of a coprocessor register (as needed for migration, etc). */
67 if (ri->type & ARM_CP_CONST) {
68 return ri->resetvalue;
69 } else if (ri->raw_readfn) {
70 return ri->raw_readfn(env, ri);
71 } else if (ri->readfn) {
72 return ri->readfn(env, ri);
73 } else {
74 return raw_read(env, ri);
75 }
76 }
77
78 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
79 uint64_t v)
80 {
81 /*
82 * Raw write of a coprocessor register (as needed for migration, etc).
83 * Note that constant registers are treated as write-ignored; the
84 * caller should check for success by whether a readback gives the
85 * value written.
86 */
87 if (ri->type & ARM_CP_CONST) {
88 return;
89 } else if (ri->raw_writefn) {
90 ri->raw_writefn(env, ri, v);
91 } else if (ri->writefn) {
92 ri->writefn(env, ri, v);
93 } else {
94 raw_write(env, ri, v);
95 }
96 }
97
98 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
99 {
100 /*
101 * Return true if the regdef would cause an assertion if you called
102 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
103 * program bug for it not to have the NO_RAW flag).
104 * NB that returning false here doesn't necessarily mean that calling
105 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
106 * read/write access functions which are safe for raw use" from "has
107 * read/write access functions which have side effects but has forgotten
108 * to provide raw access functions".
109 * The tests here line up with the conditions in read/write_raw_cp_reg()
110 * and assertions in raw_read()/raw_write().
111 */
112 if ((ri->type & ARM_CP_CONST) ||
113 ri->fieldoffset ||
114 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
115 return false;
116 }
117 return true;
118 }
119
120 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
121 {
122 /* Write the coprocessor state from cpu->env to the (index,value) list. */
123 int i;
124 bool ok = true;
125
126 for (i = 0; i < cpu->cpreg_array_len; i++) {
127 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
128 const ARMCPRegInfo *ri;
129 uint64_t newval;
130
131 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
132 if (!ri) {
133 ok = false;
134 continue;
135 }
136 if (ri->type & ARM_CP_NO_RAW) {
137 continue;
138 }
139
140 newval = read_raw_cp_reg(&cpu->env, ri);
141 if (kvm_sync) {
142 /*
143 * Only sync if the previous list->cpustate sync succeeded.
144 * Rather than tracking the success/failure state for every
145 * item in the list, we just recheck "does the raw write we must
146 * have made in write_list_to_cpustate() read back OK" here.
147 */
148 uint64_t oldval = cpu->cpreg_values[i];
149
150 if (oldval == newval) {
151 continue;
152 }
153
154 write_raw_cp_reg(&cpu->env, ri, oldval);
155 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
156 continue;
157 }
158
159 write_raw_cp_reg(&cpu->env, ri, newval);
160 }
161 cpu->cpreg_values[i] = newval;
162 }
163 return ok;
164 }
165
166 bool write_list_to_cpustate(ARMCPU *cpu)
167 {
168 int i;
169 bool ok = true;
170
171 for (i = 0; i < cpu->cpreg_array_len; i++) {
172 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
173 uint64_t v = cpu->cpreg_values[i];
174 const ARMCPRegInfo *ri;
175
176 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
177 if (!ri) {
178 ok = false;
179 continue;
180 }
181 if (ri->type & ARM_CP_NO_RAW) {
182 continue;
183 }
184 /*
185 * Write value and confirm it reads back as written
186 * (to catch read-only registers and partially read-only
187 * registers where the incoming migration value doesn't match)
188 */
189 write_raw_cp_reg(&cpu->env, ri, v);
190 if (read_raw_cp_reg(&cpu->env, ri) != v) {
191 ok = false;
192 }
193 }
194 return ok;
195 }
196
197 static void add_cpreg_to_list(gpointer key, gpointer opaque)
198 {
199 ARMCPU *cpu = opaque;
200 uint32_t regidx = (uintptr_t)key;
201 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
202
203 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
204 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
205 /* The value array need not be initialized at this point */
206 cpu->cpreg_array_len++;
207 }
208 }
209
210 static void count_cpreg(gpointer key, gpointer opaque)
211 {
212 ARMCPU *cpu = opaque;
213 const ARMCPRegInfo *ri;
214
215 ri = g_hash_table_lookup(cpu->cp_regs, key);
216
217 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
218 cpu->cpreg_array_len++;
219 }
220 }
221
222 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
223 {
224 uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
225 uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
226
227 if (aidx > bidx) {
228 return 1;
229 }
230 if (aidx < bidx) {
231 return -1;
232 }
233 return 0;
234 }
235
236 void init_cpreg_list(ARMCPU *cpu)
237 {
238 /*
239 * Initialise the cpreg_tuples[] array based on the cp_regs hash.
240 * Note that we require cpreg_tuples[] to be sorted by key ID.
241 */
242 GList *keys;
243 int arraylen;
244
245 keys = g_hash_table_get_keys(cpu->cp_regs);
246 keys = g_list_sort(keys, cpreg_key_compare);
247
248 cpu->cpreg_array_len = 0;
249
250 g_list_foreach(keys, count_cpreg, cpu);
251
252 arraylen = cpu->cpreg_array_len;
253 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
254 cpu->cpreg_values = g_new(uint64_t, arraylen);
255 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
256 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
257 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
258 cpu->cpreg_array_len = 0;
259
260 g_list_foreach(keys, add_cpreg_to_list, cpu);
261
262 assert(cpu->cpreg_array_len == arraylen);
263
264 g_list_free(keys);
265 }
266
267 static bool arm_pan_enabled(CPUARMState *env)
268 {
269 if (is_a64(env)) {
270 if ((arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1)) == (HCR_NV | HCR_NV1)) {
271 return false;
272 }
273 return env->pstate & PSTATE_PAN;
274 } else {
275 return env->uncached_cpsr & CPSR_PAN;
276 }
277 }
278
279 /*
280 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
281 */
282 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
283 const ARMCPRegInfo *ri,
284 bool isread)
285 {
286 if (!is_a64(env) && arm_current_el(env) == 3 &&
287 arm_is_secure_below_el3(env)) {
288 return CP_ACCESS_TRAP_UNCATEGORIZED;
289 }
290 return CP_ACCESS_OK;
291 }
292
293 /*
294 * Some secure-only AArch32 registers trap to EL3 if used from
295 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
296 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
297 * We assume that the .access field is set to PL1_RW.
298 */
299 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
300 const ARMCPRegInfo *ri,
301 bool isread)
302 {
303 if (arm_current_el(env) == 3) {
304 return CP_ACCESS_OK;
305 }
306 if (arm_is_secure_below_el3(env)) {
307 if (env->cp15.scr_el3 & SCR_EEL2) {
308 return CP_ACCESS_TRAP_EL2;
309 }
310 return CP_ACCESS_TRAP_EL3;
311 }
312 /* This will be EL1 NS and EL2 NS, which just UNDEF */
313 return CP_ACCESS_TRAP_UNCATEGORIZED;
314 }
315
316 /*
317 * Check for traps to performance monitor registers, which are controlled
318 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
319 */
320 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
321 bool isread)
322 {
323 int el = arm_current_el(env);
324 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
325
326 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
327 return CP_ACCESS_TRAP_EL2;
328 }
329 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
330 return CP_ACCESS_TRAP_EL3;
331 }
332 return CP_ACCESS_OK;
333 }
334
335 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
336 CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
337 bool isread)
338 {
339 if (arm_current_el(env) == 1) {
340 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
341 if (arm_hcr_el2_eff(env) & trap) {
342 return CP_ACCESS_TRAP_EL2;
343 }
344 }
345 return CP_ACCESS_OK;
346 }
347
348 /* Check for traps from EL1 due to HCR_EL2.TSW. */
349 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
350 bool isread)
351 {
352 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
353 return CP_ACCESS_TRAP_EL2;
354 }
355 return CP_ACCESS_OK;
356 }
357
358 /* Check for traps from EL1 due to HCR_EL2.TACR. */
359 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
360 bool isread)
361 {
362 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
363 return CP_ACCESS_TRAP_EL2;
364 }
365 return CP_ACCESS_OK;
366 }
367
368 /* Check for traps from EL1 due to HCR_EL2.TTLB. */
369 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
370 bool isread)
371 {
372 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
373 return CP_ACCESS_TRAP_EL2;
374 }
375 return CP_ACCESS_OK;
376 }
377
378 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBIS. */
379 static CPAccessResult access_ttlbis(CPUARMState *env, const ARMCPRegInfo *ri,
380 bool isread)
381 {
382 if (arm_current_el(env) == 1 &&
383 (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBIS))) {
384 return CP_ACCESS_TRAP_EL2;
385 }
386 return CP_ACCESS_OK;
387 }
388
389 #ifdef TARGET_AARCH64
390 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBOS. */
391 static CPAccessResult access_ttlbos(CPUARMState *env, const ARMCPRegInfo *ri,
392 bool isread)
393 {
394 if (arm_current_el(env) == 1 &&
395 (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBOS))) {
396 return CP_ACCESS_TRAP_EL2;
397 }
398 return CP_ACCESS_OK;
399 }
400 #endif
401
402 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
403 {
404 ARMCPU *cpu = env_archcpu(env);
405
406 raw_write(env, ri, value);
407 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
408 }
409
410 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
411 {
412 ARMCPU *cpu = env_archcpu(env);
413
414 if (raw_read(env, ri) != value) {
415 /*
416 * Unlike real hardware the qemu TLB uses virtual addresses,
417 * not modified virtual addresses, so this causes a TLB flush.
418 */
419 tlb_flush(CPU(cpu));
420 raw_write(env, ri, value);
421 }
422 }
423
424 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
425 uint64_t value)
426 {
427 ARMCPU *cpu = env_archcpu(env);
428
429 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
430 && !extended_addresses_enabled(env)) {
431 /*
432 * For VMSA (when not using the LPAE long descriptor page table
433 * format) this register includes the ASID, so do a TLB flush.
434 * For PMSA it is purely a process ID and no action is needed.
435 */
436 tlb_flush(CPU(cpu));
437 }
438 raw_write(env, ri, value);
439 }
440
441 static int alle1_tlbmask(CPUARMState *env)
442 {
443 /*
444 * Note that the 'ALL' scope must invalidate both stage 1 and
445 * stage 2 translations, whereas most other scopes only invalidate
446 * stage 1 translations.
447 */
448 return (ARMMMUIdxBit_E10_1 |
449 ARMMMUIdxBit_E10_1_PAN |
450 ARMMMUIdxBit_E10_0 |
451 ARMMMUIdxBit_Stage2 |
452 ARMMMUIdxBit_Stage2_S);
453 }
454
455
456 /* IS variants of TLB operations must affect all cores */
457 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
458 uint64_t value)
459 {
460 CPUState *cs = env_cpu(env);
461
462 tlb_flush_all_cpus_synced(cs);
463 }
464
465 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
466 uint64_t value)
467 {
468 CPUState *cs = env_cpu(env);
469
470 tlb_flush_all_cpus_synced(cs);
471 }
472
473 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
474 uint64_t value)
475 {
476 CPUState *cs = env_cpu(env);
477
478 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
479 }
480
481 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
482 uint64_t value)
483 {
484 CPUState *cs = env_cpu(env);
485
486 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
487 }
488
489 /*
490 * Non-IS variants of TLB operations are upgraded to
491 * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
492 * force broadcast of these operations.
493 */
494 static bool tlb_force_broadcast(CPUARMState *env)
495 {
496 return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
497 }
498
499 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
500 uint64_t value)
501 {
502 /* Invalidate all (TLBIALL) */
503 CPUState *cs = env_cpu(env);
504
505 if (tlb_force_broadcast(env)) {
506 tlb_flush_all_cpus_synced(cs);
507 } else {
508 tlb_flush(cs);
509 }
510 }
511
512 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
513 uint64_t value)
514 {
515 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
516 CPUState *cs = env_cpu(env);
517
518 value &= TARGET_PAGE_MASK;
519 if (tlb_force_broadcast(env)) {
520 tlb_flush_page_all_cpus_synced(cs, value);
521 } else {
522 tlb_flush_page(cs, value);
523 }
524 }
525
526 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
527 uint64_t value)
528 {
529 /* Invalidate by ASID (TLBIASID) */
530 CPUState *cs = env_cpu(env);
531
532 if (tlb_force_broadcast(env)) {
533 tlb_flush_all_cpus_synced(cs);
534 } else {
535 tlb_flush(cs);
536 }
537 }
538
539 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
540 uint64_t value)
541 {
542 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
543 CPUState *cs = env_cpu(env);
544
545 value &= TARGET_PAGE_MASK;
546 if (tlb_force_broadcast(env)) {
547 tlb_flush_page_all_cpus_synced(cs, value);
548 } else {
549 tlb_flush_page(cs, value);
550 }
551 }
552
553 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
554 uint64_t value)
555 {
556 CPUState *cs = env_cpu(env);
557
558 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
559 }
560
561 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
562 uint64_t value)
563 {
564 CPUState *cs = env_cpu(env);
565
566 tlb_flush_by_mmuidx_all_cpus_synced(cs, alle1_tlbmask(env));
567 }
568
569
570 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
571 uint64_t value)
572 {
573 CPUState *cs = env_cpu(env);
574
575 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
576 }
577
578 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
579 uint64_t value)
580 {
581 CPUState *cs = env_cpu(env);
582
583 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
584 }
585
586 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
587 uint64_t value)
588 {
589 CPUState *cs = env_cpu(env);
590 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
591
592 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
593 }
594
595 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
596 uint64_t value)
597 {
598 CPUState *cs = env_cpu(env);
599 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
600
601 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
602 ARMMMUIdxBit_E2);
603 }
604
605 static void tlbiipas2_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
606 uint64_t value)
607 {
608 CPUState *cs = env_cpu(env);
609 uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
610
611 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
612 }
613
614 static void tlbiipas2is_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
615 uint64_t value)
616 {
617 CPUState *cs = env_cpu(env);
618 uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
619
620 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, ARMMMUIdxBit_Stage2);
621 }
622
623 static const ARMCPRegInfo cp_reginfo[] = {
624 /*
625 * Define the secure and non-secure FCSE identifier CP registers
626 * separately because there is no secure bank in V8 (no _EL3). This allows
627 * the secure register to be properly reset and migrated. There is also no
628 * v8 EL1 version of the register so the non-secure instance stands alone.
629 */
630 { .name = "FCSEIDR",
631 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
632 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
633 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
634 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
635 { .name = "FCSEIDR_S",
636 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
637 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
638 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
639 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
640 /*
641 * Define the secure and non-secure context identifier CP registers
642 * separately because there is no secure bank in V8 (no _EL3). This allows
643 * the secure register to be properly reset and migrated. In the
644 * non-secure case, the 32-bit register will have reset and migration
645 * disabled during registration as it is handled by the 64-bit instance.
646 */
647 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
648 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
649 .access = PL1_RW, .accessfn = access_tvm_trvm,
650 .fgt = FGT_CONTEXTIDR_EL1,
651 .nv2_redirect_offset = 0x108 | NV2_REDIR_NV1,
652 .secure = ARM_CP_SECSTATE_NS,
653 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
654 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
655 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
656 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
657 .access = PL1_RW, .accessfn = access_tvm_trvm,
658 .secure = ARM_CP_SECSTATE_S,
659 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
660 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
661 };
662
663 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
664 /*
665 * NB: Some of these registers exist in v8 but with more precise
666 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
667 */
668 /* MMU Domain access control / MPU write buffer control */
669 { .name = "DACR",
670 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
671 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
672 .writefn = dacr_write, .raw_writefn = raw_write,
673 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
674 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
675 /*
676 * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
677 * For v6 and v5, these mappings are overly broad.
678 */
679 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
680 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
681 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
682 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
683 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
684 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
685 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
686 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
687 /* Cache maintenance ops; some of this space may be overridden later. */
688 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
689 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
690 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
691 };
692
693 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
694 /*
695 * Not all pre-v6 cores implemented this WFI, so this is slightly
696 * over-broad.
697 */
698 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
699 .access = PL1_W, .type = ARM_CP_WFI },
700 };
701
702 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
703 /*
704 * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
705 * is UNPREDICTABLE; we choose to NOP as most implementations do).
706 */
707 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
708 .access = PL1_W, .type = ARM_CP_WFI },
709 /*
710 * L1 cache lockdown. Not architectural in v6 and earlier but in practice
711 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
712 * OMAPCP will override this space.
713 */
714 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
715 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
716 .resetvalue = 0 },
717 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
718 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
719 .resetvalue = 0 },
720 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
721 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
722 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
723 .resetvalue = 0 },
724 /*
725 * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
726 * implementing it as RAZ means the "debug architecture version" bits
727 * will read as a reserved value, which should cause Linux to not try
728 * to use the debug hardware.
729 */
730 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
731 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
732 /*
733 * MMU TLB control. Note that the wildcarding means we cover not just
734 * the unified TLB ops but also the dside/iside/inner-shareable variants.
735 */
736 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
737 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
738 .type = ARM_CP_NO_RAW },
739 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
740 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
741 .type = ARM_CP_NO_RAW },
742 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
743 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
744 .type = ARM_CP_NO_RAW },
745 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
746 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
747 .type = ARM_CP_NO_RAW },
748 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
749 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
750 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
751 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
752 };
753
754 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
755 uint64_t value)
756 {
757 uint32_t mask = 0;
758
759 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
760 if (!arm_feature(env, ARM_FEATURE_V8)) {
761 /*
762 * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
763 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
764 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
765 */
766 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
767 /* VFP coprocessor: cp10 & cp11 [23:20] */
768 mask |= R_CPACR_ASEDIS_MASK |
769 R_CPACR_D32DIS_MASK |
770 R_CPACR_CP11_MASK |
771 R_CPACR_CP10_MASK;
772
773 if (!arm_feature(env, ARM_FEATURE_NEON)) {
774 /* ASEDIS [31] bit is RAO/WI */
775 value |= R_CPACR_ASEDIS_MASK;
776 }
777
778 /*
779 * VFPv3 and upwards with NEON implement 32 double precision
780 * registers (D0-D31).
781 */
782 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
783 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
784 value |= R_CPACR_D32DIS_MASK;
785 }
786 }
787 value &= mask;
788 }
789
790 /*
791 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
792 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
793 */
794 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
795 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
796 mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
797 value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
798 }
799
800 env->cp15.cpacr_el1 = value;
801 }
802
803 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
804 {
805 /*
806 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
807 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
808 */
809 uint64_t value = env->cp15.cpacr_el1;
810
811 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
812 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
813 value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
814 }
815 return value;
816 }
817
818
819 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
820 {
821 /*
822 * Call cpacr_write() so that we reset with the correct RAO bits set
823 * for our CPU features.
824 */
825 cpacr_write(env, ri, 0);
826 }
827
828 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
829 bool isread)
830 {
831 if (arm_feature(env, ARM_FEATURE_V8)) {
832 /* Check if CPACR accesses are to be trapped to EL2 */
833 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
834 FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
835 return CP_ACCESS_TRAP_EL2;
836 /* Check if CPACR accesses are to be trapped to EL3 */
837 } else if (arm_current_el(env) < 3 &&
838 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
839 return CP_ACCESS_TRAP_EL3;
840 }
841 }
842
843 return CP_ACCESS_OK;
844 }
845
846 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
847 bool isread)
848 {
849 /* Check if CPTR accesses are set to trap to EL3 */
850 if (arm_current_el(env) == 2 &&
851 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
852 return CP_ACCESS_TRAP_EL3;
853 }
854
855 return CP_ACCESS_OK;
856 }
857
858 static const ARMCPRegInfo v6_cp_reginfo[] = {
859 /* prefetch by MVA in v6, NOP in v7 */
860 { .name = "MVA_prefetch",
861 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
862 .access = PL1_W, .type = ARM_CP_NOP },
863 /*
864 * We need to break the TB after ISB to execute self-modifying code
865 * correctly and also to take any pending interrupts immediately.
866 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
867 */
868 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
869 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
870 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
871 .access = PL0_W, .type = ARM_CP_NOP },
872 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
873 .access = PL0_W, .type = ARM_CP_NOP },
874 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
875 .access = PL1_RW, .accessfn = access_tvm_trvm,
876 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
877 offsetof(CPUARMState, cp15.ifar_ns) },
878 .resetvalue = 0, },
879 /*
880 * Watchpoint Fault Address Register : should actually only be present
881 * for 1136, 1176, 11MPCore.
882 */
883 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
884 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
885 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
886 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
887 .fgt = FGT_CPACR_EL1,
888 .nv2_redirect_offset = 0x100 | NV2_REDIR_NV1,
889 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
890 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
891 };
892
893 typedef struct pm_event {
894 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
895 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
896 bool (*supported)(CPUARMState *);
897 /*
898 * Retrieve the current count of the underlying event. The programmed
899 * counters hold a difference from the return value from this function
900 */
901 uint64_t (*get_count)(CPUARMState *);
902 /*
903 * Return how many nanoseconds it will take (at a minimum) for count events
904 * to occur. A negative value indicates the counter will never overflow, or
905 * that the counter has otherwise arranged for the overflow bit to be set
906 * and the PMU interrupt to be raised on overflow.
907 */
908 int64_t (*ns_per_count)(uint64_t);
909 } pm_event;
910
911 static bool event_always_supported(CPUARMState *env)
912 {
913 return true;
914 }
915
916 static uint64_t swinc_get_count(CPUARMState *env)
917 {
918 /*
919 * SW_INCR events are written directly to the pmevcntr's by writes to
920 * PMSWINC, so there is no underlying count maintained by the PMU itself
921 */
922 return 0;
923 }
924
925 static int64_t swinc_ns_per(uint64_t ignored)
926 {
927 return -1;
928 }
929
930 /*
931 * Return the underlying cycle count for the PMU cycle counters. If we're in
932 * usermode, simply return 0.
933 */
934 static uint64_t cycles_get_count(CPUARMState *env)
935 {
936 #ifndef CONFIG_USER_ONLY
937 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
938 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
939 #else
940 return cpu_get_host_ticks();
941 #endif
942 }
943
944 #ifndef CONFIG_USER_ONLY
945 static int64_t cycles_ns_per(uint64_t cycles)
946 {
947 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
948 }
949
950 static bool instructions_supported(CPUARMState *env)
951 {
952 /* Precise instruction counting */
953 return icount_enabled() == ICOUNT_PRECISE;
954 }
955
956 static uint64_t instructions_get_count(CPUARMState *env)
957 {
958 assert(icount_enabled() == ICOUNT_PRECISE);
959 return (uint64_t)icount_get_raw();
960 }
961
962 static int64_t instructions_ns_per(uint64_t icount)
963 {
964 assert(icount_enabled() == ICOUNT_PRECISE);
965 return icount_to_ns((int64_t)icount);
966 }
967 #endif
968
969 static bool pmuv3p1_events_supported(CPUARMState *env)
970 {
971 /* For events which are supported in any v8.1 PMU */
972 return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
973 }
974
975 static bool pmuv3p4_events_supported(CPUARMState *env)
976 {
977 /* For events which are supported in any v8.1 PMU */
978 return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
979 }
980
981 static uint64_t zero_event_get_count(CPUARMState *env)
982 {
983 /* For events which on QEMU never fire, so their count is always zero */
984 return 0;
985 }
986
987 static int64_t zero_event_ns_per(uint64_t cycles)
988 {
989 /* An event which never fires can never overflow */
990 return -1;
991 }
992
993 static const pm_event pm_events[] = {
994 { .number = 0x000, /* SW_INCR */
995 .supported = event_always_supported,
996 .get_count = swinc_get_count,
997 .ns_per_count = swinc_ns_per,
998 },
999 #ifndef CONFIG_USER_ONLY
1000 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
1001 .supported = instructions_supported,
1002 .get_count = instructions_get_count,
1003 .ns_per_count = instructions_ns_per,
1004 },
1005 { .number = 0x011, /* CPU_CYCLES, Cycle */
1006 .supported = event_always_supported,
1007 .get_count = cycles_get_count,
1008 .ns_per_count = cycles_ns_per,
1009 },
1010 #endif
1011 { .number = 0x023, /* STALL_FRONTEND */
1012 .supported = pmuv3p1_events_supported,
1013 .get_count = zero_event_get_count,
1014 .ns_per_count = zero_event_ns_per,
1015 },
1016 { .number = 0x024, /* STALL_BACKEND */
1017 .supported = pmuv3p1_events_supported,
1018 .get_count = zero_event_get_count,
1019 .ns_per_count = zero_event_ns_per,
1020 },
1021 { .number = 0x03c, /* STALL */
1022 .supported = pmuv3p4_events_supported,
1023 .get_count = zero_event_get_count,
1024 .ns_per_count = zero_event_ns_per,
1025 },
1026 };
1027
1028 /*
1029 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1030 * events (i.e. the statistical profiling extension), this implementation
1031 * should first be updated to something sparse instead of the current
1032 * supported_event_map[] array.
1033 */
1034 #define MAX_EVENT_ID 0x3c
1035 #define UNSUPPORTED_EVENT UINT16_MAX
1036 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1037
1038 /*
1039 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1040 * of ARM event numbers to indices in our pm_events array.
1041 *
1042 * Note: Events in the 0x40XX range are not currently supported.
1043 */
1044 void pmu_init(ARMCPU *cpu)
1045 {
1046 unsigned int i;
1047
1048 /*
1049 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1050 * events to them
1051 */
1052 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1053 supported_event_map[i] = UNSUPPORTED_EVENT;
1054 }
1055 cpu->pmceid0 = 0;
1056 cpu->pmceid1 = 0;
1057
1058 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1059 const pm_event *cnt = &pm_events[i];
1060 assert(cnt->number <= MAX_EVENT_ID);
1061 /* We do not currently support events in the 0x40xx range */
1062 assert(cnt->number <= 0x3f);
1063
1064 if (cnt->supported(&cpu->env)) {
1065 supported_event_map[cnt->number] = i;
1066 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
1067 if (cnt->number & 0x20) {
1068 cpu->pmceid1 |= event_mask;
1069 } else {
1070 cpu->pmceid0 |= event_mask;
1071 }
1072 }
1073 }
1074 }
1075
1076 /*
1077 * Check at runtime whether a PMU event is supported for the current machine
1078 */
1079 static bool event_supported(uint16_t number)
1080 {
1081 if (number > MAX_EVENT_ID) {
1082 return false;
1083 }
1084 return supported_event_map[number] != UNSUPPORTED_EVENT;
1085 }
1086
1087 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1088 bool isread)
1089 {
1090 /*
1091 * Performance monitor registers user accessibility is controlled
1092 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1093 * trapping to EL2 or EL3 for other accesses.
1094 */
1095 int el = arm_current_el(env);
1096 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1097
1098 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1099 return CP_ACCESS_TRAP;
1100 }
1101 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1102 return CP_ACCESS_TRAP_EL2;
1103 }
1104 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1105 return CP_ACCESS_TRAP_EL3;
1106 }
1107
1108 return CP_ACCESS_OK;
1109 }
1110
1111 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1112 const ARMCPRegInfo *ri,
1113 bool isread)
1114 {
1115 /* ER: event counter read trap control */
1116 if (arm_feature(env, ARM_FEATURE_V8)
1117 && arm_current_el(env) == 0
1118 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1119 && isread) {
1120 return CP_ACCESS_OK;
1121 }
1122
1123 return pmreg_access(env, ri, isread);
1124 }
1125
1126 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1127 const ARMCPRegInfo *ri,
1128 bool isread)
1129 {
1130 /* SW: software increment write trap control */
1131 if (arm_feature(env, ARM_FEATURE_V8)
1132 && arm_current_el(env) == 0
1133 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1134 && !isread) {
1135 return CP_ACCESS_OK;
1136 }
1137
1138 return pmreg_access(env, ri, isread);
1139 }
1140
1141 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1142 const ARMCPRegInfo *ri,
1143 bool isread)
1144 {
1145 /* ER: event counter read trap control */
1146 if (arm_feature(env, ARM_FEATURE_V8)
1147 && arm_current_el(env) == 0
1148 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1149 return CP_ACCESS_OK;
1150 }
1151
1152 return pmreg_access(env, ri, isread);
1153 }
1154
1155 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1156 const ARMCPRegInfo *ri,
1157 bool isread)
1158 {
1159 /* CR: cycle counter read trap control */
1160 if (arm_feature(env, ARM_FEATURE_V8)
1161 && arm_current_el(env) == 0
1162 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1163 && isread) {
1164 return CP_ACCESS_OK;
1165 }
1166
1167 return pmreg_access(env, ri, isread);
1168 }
1169
1170 /*
1171 * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1172 * We use these to decide whether we need to wrap a write to MDCR_EL2
1173 * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1174 */
1175 #define MDCR_EL2_PMU_ENABLE_BITS \
1176 (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
1177 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
1178
1179 /*
1180 * Returns true if the counter (pass 31 for PMCCNTR) should count events using
1181 * the current EL, security state, and register configuration.
1182 */
1183 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1184 {
1185 uint64_t filter;
1186 bool e, p, u, nsk, nsu, nsh, m;
1187 bool enabled, prohibited = false, filtered;
1188 bool secure = arm_is_secure(env);
1189 int el = arm_current_el(env);
1190 uint64_t mdcr_el2;
1191 uint8_t hpmn;
1192
1193 /*
1194 * We might be called for M-profile cores where MDCR_EL2 doesn't
1195 * exist and arm_mdcr_el2_eff() will assert, so this early-exit check
1196 * must be before we read that value.
1197 */
1198 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1199 return false;
1200 }
1201
1202 mdcr_el2 = arm_mdcr_el2_eff(env);
1203 hpmn = mdcr_el2 & MDCR_HPMN;
1204
1205 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1206 (counter < hpmn || counter == 31)) {
1207 e = env->cp15.c9_pmcr & PMCRE;
1208 } else {
1209 e = mdcr_el2 & MDCR_HPME;
1210 }
1211 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1212
1213 /* Is event counting prohibited? */
1214 if (el == 2 && (counter < hpmn || counter == 31)) {
1215 prohibited = mdcr_el2 & MDCR_HPMD;
1216 }
1217 if (secure) {
1218 prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1219 }
1220
1221 if (counter == 31) {
1222 /*
1223 * The cycle counter defaults to running. PMCR.DP says "disable
1224 * the cycle counter when event counting is prohibited".
1225 * Some MDCR bits disable the cycle counter specifically.
1226 */
1227 prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1228 if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1229 if (secure) {
1230 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1231 }
1232 if (el == 2) {
1233 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1234 }
1235 }
1236 }
1237
1238 if (counter == 31) {
1239 filter = env->cp15.pmccfiltr_el0;
1240 } else {
1241 filter = env->cp15.c14_pmevtyper[counter];
1242 }
1243
1244 p = filter & PMXEVTYPER_P;
1245 u = filter & PMXEVTYPER_U;
1246 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1247 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1248 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1249 m = arm_el_is_aa64(env, 1) &&
1250 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1251
1252 if (el == 0) {
1253 filtered = secure ? u : u != nsu;
1254 } else if (el == 1) {
1255 filtered = secure ? p : p != nsk;
1256 } else if (el == 2) {
1257 filtered = !nsh;
1258 } else { /* EL3 */
1259 filtered = m != p;
1260 }
1261
1262 if (counter != 31) {
1263 /*
1264 * If not checking PMCCNTR, ensure the counter is setup to an event we
1265 * support
1266 */
1267 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1268 if (!event_supported(event)) {
1269 return false;
1270 }
1271 }
1272
1273 return enabled && !prohibited && !filtered;
1274 }
1275
1276 static void pmu_update_irq(CPUARMState *env)
1277 {
1278 ARMCPU *cpu = env_archcpu(env);
1279 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1280 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1281 }
1282
1283 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1284 {
1285 /*
1286 * Return true if the clock divider is enabled and the cycle counter
1287 * is supposed to tick only once every 64 clock cycles. This is
1288 * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1289 * (64-bit) cycle counter PMCR.D has no effect.
1290 */
1291 return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1292 }
1293
1294 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1295 {
1296 /* Return true if the specified event counter is configured to be 64 bit */
1297
1298 /* This isn't intended to be used with the cycle counter */
1299 assert(counter < 31);
1300
1301 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1302 return false;
1303 }
1304
1305 if (arm_feature(env, ARM_FEATURE_EL2)) {
1306 /*
1307 * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1308 * current security state, so we don't use arm_mdcr_el2_eff() here.
1309 */
1310 bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1311 int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1312
1313 if (counter >= hpmn) {
1314 return hlp;
1315 }
1316 }
1317 return env->cp15.c9_pmcr & PMCRLP;
1318 }
1319
1320 /*
1321 * Ensure c15_ccnt is the guest-visible count so that operations such as
1322 * enabling/disabling the counter or filtering, modifying the count itself,
1323 * etc. can be done logically. This is essentially a no-op if the counter is
1324 * not enabled at the time of the call.
1325 */
1326 static void pmccntr_op_start(CPUARMState *env)
1327 {
1328 uint64_t cycles = cycles_get_count(env);
1329
1330 if (pmu_counter_enabled(env, 31)) {
1331 uint64_t eff_cycles = cycles;
1332 if (pmccntr_clockdiv_enabled(env)) {
1333 eff_cycles /= 64;
1334 }
1335
1336 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1337
1338 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1339 1ull << 63 : 1ull << 31;
1340 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1341 env->cp15.c9_pmovsr |= (1ULL << 31);
1342 pmu_update_irq(env);
1343 }
1344
1345 env->cp15.c15_ccnt = new_pmccntr;
1346 }
1347 env->cp15.c15_ccnt_delta = cycles;
1348 }
1349
1350 /*
1351 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1352 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1353 * pmccntr_op_start.
1354 */
1355 static void pmccntr_op_finish(CPUARMState *env)
1356 {
1357 if (pmu_counter_enabled(env, 31)) {
1358 #ifndef CONFIG_USER_ONLY
1359 /* Calculate when the counter will next overflow */
1360 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1361 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1362 remaining_cycles = (uint32_t)remaining_cycles;
1363 }
1364 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1365
1366 if (overflow_in > 0) {
1367 int64_t overflow_at;
1368
1369 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1370 overflow_in, &overflow_at)) {
1371 ARMCPU *cpu = env_archcpu(env);
1372 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1373 }
1374 }
1375 #endif
1376
1377 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1378 if (pmccntr_clockdiv_enabled(env)) {
1379 prev_cycles /= 64;
1380 }
1381 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1382 }
1383 }
1384
1385 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1386 {
1387
1388 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1389 uint64_t count = 0;
1390 if (event_supported(event)) {
1391 uint16_t event_idx = supported_event_map[event];
1392 count = pm_events[event_idx].get_count(env);
1393 }
1394
1395 if (pmu_counter_enabled(env, counter)) {
1396 uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1397 uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1398 1ULL << 63 : 1ULL << 31;
1399
1400 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1401 env->cp15.c9_pmovsr |= (1 << counter);
1402 pmu_update_irq(env);
1403 }
1404 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1405 }
1406 env->cp15.c14_pmevcntr_delta[counter] = count;
1407 }
1408
1409 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1410 {
1411 if (pmu_counter_enabled(env, counter)) {
1412 #ifndef CONFIG_USER_ONLY
1413 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1414 uint16_t event_idx = supported_event_map[event];
1415 uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1416 int64_t overflow_in;
1417
1418 if (!pmevcntr_is_64_bit(env, counter)) {
1419 delta = (uint32_t)delta;
1420 }
1421 overflow_in = pm_events[event_idx].ns_per_count(delta);
1422
1423 if (overflow_in > 0) {
1424 int64_t overflow_at;
1425
1426 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1427 overflow_in, &overflow_at)) {
1428 ARMCPU *cpu = env_archcpu(env);
1429 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1430 }
1431 }
1432 #endif
1433
1434 env->cp15.c14_pmevcntr_delta[counter] -=
1435 env->cp15.c14_pmevcntr[counter];
1436 }
1437 }
1438
1439 void pmu_op_start(CPUARMState *env)
1440 {
1441 unsigned int i;
1442 pmccntr_op_start(env);
1443 for (i = 0; i < pmu_num_counters(env); i++) {
1444 pmevcntr_op_start(env, i);
1445 }
1446 }
1447
1448 void pmu_op_finish(CPUARMState *env)
1449 {
1450 unsigned int i;
1451 pmccntr_op_finish(env);
1452 for (i = 0; i < pmu_num_counters(env); i++) {
1453 pmevcntr_op_finish(env, i);
1454 }
1455 }
1456
1457 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1458 {
1459 pmu_op_start(&cpu->env);
1460 }
1461
1462 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1463 {
1464 pmu_op_finish(&cpu->env);
1465 }
1466
1467 void arm_pmu_timer_cb(void *opaque)
1468 {
1469 ARMCPU *cpu = opaque;
1470
1471 /*
1472 * Update all the counter values based on the current underlying counts,
1473 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1474 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1475 * counter may expire.
1476 */
1477 pmu_op_start(&cpu->env);
1478 pmu_op_finish(&cpu->env);
1479 }
1480
1481 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1482 uint64_t value)
1483 {
1484 pmu_op_start(env);
1485
1486 if (value & PMCRC) {
1487 /* The counter has been reset */
1488 env->cp15.c15_ccnt = 0;
1489 }
1490
1491 if (value & PMCRP) {
1492 unsigned int i;
1493 for (i = 0; i < pmu_num_counters(env); i++) {
1494 env->cp15.c14_pmevcntr[i] = 0;
1495 }
1496 }
1497
1498 env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1499 env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1500
1501 pmu_op_finish(env);
1502 }
1503
1504 static uint64_t pmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1505 {
1506 uint64_t pmcr = env->cp15.c9_pmcr;
1507
1508 /*
1509 * If EL2 is implemented and enabled for the current security state, reads
1510 * of PMCR.N from EL1 or EL0 return the value of MDCR_EL2.HPMN or HDCR.HPMN.
1511 */
1512 if (arm_current_el(env) <= 1 && arm_is_el2_enabled(env)) {
1513 pmcr &= ~PMCRN_MASK;
1514 pmcr |= (env->cp15.mdcr_el2 & MDCR_HPMN) << PMCRN_SHIFT;
1515 }
1516
1517 return pmcr;
1518 }
1519
1520 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1521 uint64_t value)
1522 {
1523 unsigned int i;
1524 uint64_t overflow_mask, new_pmswinc;
1525
1526 for (i = 0; i < pmu_num_counters(env); i++) {
1527 /* Increment a counter's count iff: */
1528 if ((value & (1 << i)) && /* counter's bit is set */
1529 /* counter is enabled and not filtered */
1530 pmu_counter_enabled(env, i) &&
1531 /* counter is SW_INCR */
1532 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1533 pmevcntr_op_start(env, i);
1534
1535 /*
1536 * Detect if this write causes an overflow since we can't predict
1537 * PMSWINC overflows like we can for other events
1538 */
1539 new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1540
1541 overflow_mask = pmevcntr_is_64_bit(env, i) ?
1542 1ULL << 63 : 1ULL << 31;
1543
1544 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1545 env->cp15.c9_pmovsr |= (1 << i);
1546 pmu_update_irq(env);
1547 }
1548
1549 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1550
1551 pmevcntr_op_finish(env, i);
1552 }
1553 }
1554 }
1555
1556 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1557 {
1558 uint64_t ret;
1559 pmccntr_op_start(env);
1560 ret = env->cp15.c15_ccnt;
1561 pmccntr_op_finish(env);
1562 return ret;
1563 }
1564
1565 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1566 uint64_t value)
1567 {
1568 /*
1569 * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1570 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1571 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1572 * accessed.
1573 */
1574 env->cp15.c9_pmselr = value & 0x1f;
1575 }
1576
1577 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1578 uint64_t value)
1579 {
1580 pmccntr_op_start(env);
1581 env->cp15.c15_ccnt = value;
1582 pmccntr_op_finish(env);
1583 }
1584
1585 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1586 uint64_t value)
1587 {
1588 uint64_t cur_val = pmccntr_read(env, NULL);
1589
1590 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1591 }
1592
1593 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1594 uint64_t value)
1595 {
1596 pmccntr_op_start(env);
1597 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1598 pmccntr_op_finish(env);
1599 }
1600
1601 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1602 uint64_t value)
1603 {
1604 pmccntr_op_start(env);
1605 /* M is not accessible from AArch32 */
1606 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1607 (value & PMCCFILTR);
1608 pmccntr_op_finish(env);
1609 }
1610
1611 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1612 {
1613 /* M is not visible in AArch32 */
1614 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1615 }
1616
1617 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1618 uint64_t value)
1619 {
1620 pmu_op_start(env);
1621 value &= pmu_counter_mask(env);
1622 env->cp15.c9_pmcnten |= value;
1623 pmu_op_finish(env);
1624 }
1625
1626 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1627 uint64_t value)
1628 {
1629 pmu_op_start(env);
1630 value &= pmu_counter_mask(env);
1631 env->cp15.c9_pmcnten &= ~value;
1632 pmu_op_finish(env);
1633 }
1634
1635 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1636 uint64_t value)
1637 {
1638 value &= pmu_counter_mask(env);
1639 env->cp15.c9_pmovsr &= ~value;
1640 pmu_update_irq(env);
1641 }
1642
1643 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1644 uint64_t value)
1645 {
1646 value &= pmu_counter_mask(env);
1647 env->cp15.c9_pmovsr |= value;
1648 pmu_update_irq(env);
1649 }
1650
1651 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1652 uint64_t value, const uint8_t counter)
1653 {
1654 if (counter == 31) {
1655 pmccfiltr_write(env, ri, value);
1656 } else if (counter < pmu_num_counters(env)) {
1657 pmevcntr_op_start(env, counter);
1658
1659 /*
1660 * If this counter's event type is changing, store the current
1661 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1662 * pmevcntr_op_finish has the correct baseline when it converts back to
1663 * a delta.
1664 */
1665 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1666 PMXEVTYPER_EVTCOUNT;
1667 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1668 if (old_event != new_event) {
1669 uint64_t count = 0;
1670 if (event_supported(new_event)) {
1671 uint16_t event_idx = supported_event_map[new_event];
1672 count = pm_events[event_idx].get_count(env);
1673 }
1674 env->cp15.c14_pmevcntr_delta[counter] = count;
1675 }
1676
1677 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1678 pmevcntr_op_finish(env, counter);
1679 }
1680 /*
1681 * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1682 * PMSELR value is equal to or greater than the number of implemented
1683 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1684 */
1685 }
1686
1687 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1688 const uint8_t counter)
1689 {
1690 if (counter == 31) {
1691 return env->cp15.pmccfiltr_el0;
1692 } else if (counter < pmu_num_counters(env)) {
1693 return env->cp15.c14_pmevtyper[counter];
1694 } else {
1695 /*
1696 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1697 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1698 */
1699 return 0;
1700 }
1701 }
1702
1703 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1704 uint64_t value)
1705 {
1706 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1707 pmevtyper_write(env, ri, value, counter);
1708 }
1709
1710 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1711 uint64_t value)
1712 {
1713 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1714 env->cp15.c14_pmevtyper[counter] = value;
1715
1716 /*
1717 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1718 * pmu_op_finish calls when loading saved state for a migration. Because
1719 * we're potentially updating the type of event here, the value written to
1720 * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a
1721 * different counter type. Therefore, we need to set this value to the
1722 * current count for the counter type we're writing so that pmu_op_finish
1723 * has the correct count for its calculation.
1724 */
1725 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1726 if (event_supported(event)) {
1727 uint16_t event_idx = supported_event_map[event];
1728 env->cp15.c14_pmevcntr_delta[counter] =
1729 pm_events[event_idx].get_count(env);
1730 }
1731 }
1732
1733 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1734 {
1735 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1736 return pmevtyper_read(env, ri, counter);
1737 }
1738
1739 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1740 uint64_t value)
1741 {
1742 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1743 }
1744
1745 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1746 {
1747 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1748 }
1749
1750 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1751 uint64_t value, uint8_t counter)
1752 {
1753 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1754 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1755 value &= MAKE_64BIT_MASK(0, 32);
1756 }
1757 if (counter < pmu_num_counters(env)) {
1758 pmevcntr_op_start(env, counter);
1759 env->cp15.c14_pmevcntr[counter] = value;
1760 pmevcntr_op_finish(env, counter);
1761 }
1762 /*
1763 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1764 * are CONSTRAINED UNPREDICTABLE.
1765 */
1766 }
1767
1768 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1769 uint8_t counter)
1770 {
1771 if (counter < pmu_num_counters(env)) {
1772 uint64_t ret;
1773 pmevcntr_op_start(env, counter);
1774 ret = env->cp15.c14_pmevcntr[counter];
1775 pmevcntr_op_finish(env, counter);
1776 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1777 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1778 ret &= MAKE_64BIT_MASK(0, 32);
1779 }
1780 return ret;
1781 } else {
1782 /*
1783 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1784 * are CONSTRAINED UNPREDICTABLE.
1785 */
1786 return 0;
1787 }
1788 }
1789
1790 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1791 uint64_t value)
1792 {
1793 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1794 pmevcntr_write(env, ri, value, counter);
1795 }
1796
1797 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1798 {
1799 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1800 return pmevcntr_read(env, ri, counter);
1801 }
1802
1803 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1804 uint64_t value)
1805 {
1806 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1807 assert(counter < pmu_num_counters(env));
1808 env->cp15.c14_pmevcntr[counter] = value;
1809 pmevcntr_write(env, ri, value, counter);
1810 }
1811
1812 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1813 {
1814 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1815 assert(counter < pmu_num_counters(env));
1816 return env->cp15.c14_pmevcntr[counter];
1817 }
1818
1819 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1820 uint64_t value)
1821 {
1822 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1823 }
1824
1825 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1826 {
1827 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1828 }
1829
1830 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1831 uint64_t value)
1832 {
1833 if (arm_feature(env, ARM_FEATURE_V8)) {
1834 env->cp15.c9_pmuserenr = value & 0xf;
1835 } else {
1836 env->cp15.c9_pmuserenr = value & 1;
1837 }
1838 }
1839
1840 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1841 uint64_t value)
1842 {
1843 /* We have no event counters so only the C bit can be changed */
1844 value &= pmu_counter_mask(env);
1845 env->cp15.c9_pminten |= value;
1846 pmu_update_irq(env);
1847 }
1848
1849 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1850 uint64_t value)
1851 {
1852 value &= pmu_counter_mask(env);
1853 env->cp15.c9_pminten &= ~value;
1854 pmu_update_irq(env);
1855 }
1856
1857 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1858 uint64_t value)
1859 {
1860 /*
1861 * Note that even though the AArch64 view of this register has bits
1862 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1863 * architectural requirements for bits which are RES0 only in some
1864 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1865 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1866 */
1867 raw_write(env, ri, value & ~0x1FULL);
1868 }
1869
1870 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1871 {
1872 /* Begin with base v8.0 state. */
1873 uint64_t valid_mask = 0x3fff;
1874 ARMCPU *cpu = env_archcpu(env);
1875 uint64_t changed;
1876
1877 /*
1878 * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1879 * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1880 * Instead, choose the format based on the mode of EL3.
1881 */
1882 if (arm_el_is_aa64(env, 3)) {
1883 value |= SCR_FW | SCR_AW; /* RES1 */
1884 valid_mask &= ~SCR_NET; /* RES0 */
1885
1886 if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1887 !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1888 value |= SCR_RW; /* RAO/WI */
1889 }
1890 if (cpu_isar_feature(aa64_ras, cpu)) {
1891 valid_mask |= SCR_TERR;
1892 }
1893 if (cpu_isar_feature(aa64_lor, cpu)) {
1894 valid_mask |= SCR_TLOR;
1895 }
1896 if (cpu_isar_feature(aa64_pauth, cpu)) {
1897 valid_mask |= SCR_API | SCR_APK;
1898 }
1899 if (cpu_isar_feature(aa64_sel2, cpu)) {
1900 valid_mask |= SCR_EEL2;
1901 } else if (cpu_isar_feature(aa64_rme, cpu)) {
1902 /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */
1903 value |= SCR_NS;
1904 }
1905 if (cpu_isar_feature(aa64_mte, cpu)) {
1906 valid_mask |= SCR_ATA;
1907 }
1908 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1909 valid_mask |= SCR_ENSCXT;
1910 }
1911 if (cpu_isar_feature(aa64_doublefault, cpu)) {
1912 valid_mask |= SCR_EASE | SCR_NMEA;
1913 }
1914 if (cpu_isar_feature(aa64_sme, cpu)) {
1915 valid_mask |= SCR_ENTP2;
1916 }
1917 if (cpu_isar_feature(aa64_hcx, cpu)) {
1918 valid_mask |= SCR_HXEN;
1919 }
1920 if (cpu_isar_feature(aa64_fgt, cpu)) {
1921 valid_mask |= SCR_FGTEN;
1922 }
1923 if (cpu_isar_feature(aa64_rme, cpu)) {
1924 valid_mask |= SCR_NSE | SCR_GPF;
1925 }
1926 } else {
1927 valid_mask &= ~(SCR_RW | SCR_ST);
1928 if (cpu_isar_feature(aa32_ras, cpu)) {
1929 valid_mask |= SCR_TERR;
1930 }
1931 }
1932
1933 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1934 valid_mask &= ~SCR_HCE;
1935
1936 /*
1937 * On ARMv7, SMD (or SCD as it is called in v7) is only
1938 * supported if EL2 exists. The bit is UNK/SBZP when
1939 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1940 * when EL2 is unavailable.
1941 * On ARMv8, this bit is always available.
1942 */
1943 if (arm_feature(env, ARM_FEATURE_V7) &&
1944 !arm_feature(env, ARM_FEATURE_V8)) {
1945 valid_mask &= ~SCR_SMD;
1946 }
1947 }
1948
1949 /* Clear all-context RES0 bits. */
1950 value &= valid_mask;
1951 changed = env->cp15.scr_el3 ^ value;
1952 env->cp15.scr_el3 = value;
1953
1954 /*
1955 * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
1956 * we must invalidate all TLBs below EL3.
1957 */
1958 if (changed & (SCR_NS | SCR_NSE)) {
1959 tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1960 ARMMMUIdxBit_E20_0 |
1961 ARMMMUIdxBit_E10_1 |
1962 ARMMMUIdxBit_E20_2 |
1963 ARMMMUIdxBit_E10_1_PAN |
1964 ARMMMUIdxBit_E20_2_PAN |
1965 ARMMMUIdxBit_E2));
1966 }
1967 }
1968
1969 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1970 {
1971 /*
1972 * scr_write will set the RES1 bits on an AArch64-only CPU.
1973 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1974 */
1975 scr_write(env, ri, 0);
1976 }
1977
1978 static CPAccessResult access_tid4(CPUARMState *env,
1979 const ARMCPRegInfo *ri,
1980 bool isread)
1981 {
1982 if (arm_current_el(env) == 1 &&
1983 (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1984 return CP_ACCESS_TRAP_EL2;
1985 }
1986
1987 return CP_ACCESS_OK;
1988 }
1989
1990 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1991 {
1992 ARMCPU *cpu = env_archcpu(env);
1993
1994 /*
1995 * Acquire the CSSELR index from the bank corresponding to the CCSIDR
1996 * bank
1997 */
1998 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1999 ri->secure & ARM_CP_SECSTATE_S);
2000
2001 return cpu->ccsidr[index];
2002 }
2003
2004 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2005 uint64_t value)
2006 {
2007 raw_write(env, ri, value & 0xf);
2008 }
2009
2010 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2011 {
2012 CPUState *cs = env_cpu(env);
2013 bool el1 = arm_current_el(env) == 1;
2014 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
2015 uint64_t ret = 0;
2016
2017 if (hcr_el2 & HCR_IMO) {
2018 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
2019 ret |= CPSR_I;
2020 }
2021 } else {
2022 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
2023 ret |= CPSR_I;
2024 }
2025 }
2026
2027 if (hcr_el2 & HCR_FMO) {
2028 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
2029 ret |= CPSR_F;
2030 }
2031 } else {
2032 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
2033 ret |= CPSR_F;
2034 }
2035 }
2036
2037 if (hcr_el2 & HCR_AMO) {
2038 if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
2039 ret |= CPSR_A;
2040 }
2041 }
2042
2043 return ret;
2044 }
2045
2046 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2047 bool isread)
2048 {
2049 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2050 return CP_ACCESS_TRAP_EL2;
2051 }
2052
2053 return CP_ACCESS_OK;
2054 }
2055
2056 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2057 bool isread)
2058 {
2059 if (arm_feature(env, ARM_FEATURE_V8)) {
2060 return access_aa64_tid1(env, ri, isread);
2061 }
2062
2063 return CP_ACCESS_OK;
2064 }
2065
2066 static const ARMCPRegInfo v7_cp_reginfo[] = {
2067 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2068 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2069 .access = PL1_W, .type = ARM_CP_NOP },
2070 /*
2071 * Performance monitors are implementation defined in v7,
2072 * but with an ARM recommended set of registers, which we
2073 * follow.
2074 *
2075 * Performance registers fall into three categories:
2076 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2077 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2078 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2079 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2080 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2081 */
2082 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
2083 .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
2084 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2085 .writefn = pmcntenset_write,
2086 .accessfn = pmreg_access,
2087 .fgt = FGT_PMCNTEN,
2088 .raw_writefn = raw_write },
2089 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
2090 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2091 .access = PL0_RW, .accessfn = pmreg_access,
2092 .fgt = FGT_PMCNTEN,
2093 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2094 .writefn = pmcntenset_write, .raw_writefn = raw_write },
2095 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
2096 .access = PL0_RW,
2097 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2098 .accessfn = pmreg_access,
2099 .fgt = FGT_PMCNTEN,
2100 .writefn = pmcntenclr_write,
2101 .type = ARM_CP_ALIAS | ARM_CP_IO },
2102 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2103 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2104 .access = PL0_RW, .accessfn = pmreg_access,
2105 .fgt = FGT_PMCNTEN,
2106 .type = ARM_CP_ALIAS | ARM_CP_IO,
2107 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2108 .writefn = pmcntenclr_write },
2109 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
2110 .access = PL0_RW, .type = ARM_CP_IO,
2111 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2112 .accessfn = pmreg_access,
2113 .fgt = FGT_PMOVS,
2114 .writefn = pmovsr_write,
2115 .raw_writefn = raw_write },
2116 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2117 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2118 .access = PL0_RW, .accessfn = pmreg_access,
2119 .fgt = FGT_PMOVS,
2120 .type = ARM_CP_ALIAS | ARM_CP_IO,
2121 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2122 .writefn = pmovsr_write,
2123 .raw_writefn = raw_write },
2124 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2125 .access = PL0_W, .accessfn = pmreg_access_swinc,
2126 .fgt = FGT_PMSWINC_EL0,
2127 .type = ARM_CP_NO_RAW | ARM_CP_IO,
2128 .writefn = pmswinc_write },
2129 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2130 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2131 .access = PL0_W, .accessfn = pmreg_access_swinc,
2132 .fgt = FGT_PMSWINC_EL0,
2133 .type = ARM_CP_NO_RAW | ARM_CP_IO,
2134 .writefn = pmswinc_write },
2135 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2136 .access = PL0_RW, .type = ARM_CP_ALIAS,
2137 .fgt = FGT_PMSELR_EL0,
2138 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2139 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2140 .raw_writefn = raw_write},
2141 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2142 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2143 .access = PL0_RW, .accessfn = pmreg_access_selr,
2144 .fgt = FGT_PMSELR_EL0,
2145 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2146 .writefn = pmselr_write, .raw_writefn = raw_write, },
2147 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2148 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2149 .fgt = FGT_PMCCNTR_EL0,
2150 .readfn = pmccntr_read, .writefn = pmccntr_write32,
2151 .accessfn = pmreg_access_ccntr },
2152 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2153 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2154 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2155 .fgt = FGT_PMCCNTR_EL0,
2156 .type = ARM_CP_IO,
2157 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2158 .readfn = pmccntr_read, .writefn = pmccntr_write,
2159 .raw_readfn = raw_read, .raw_writefn = raw_write, },
2160 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2161 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2162 .access = PL0_RW, .accessfn = pmreg_access,
2163 .fgt = FGT_PMCCFILTR_EL0,
2164 .type = ARM_CP_ALIAS | ARM_CP_IO,
2165 .resetvalue = 0, },
2166 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2167 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2168 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2169 .access = PL0_RW, .accessfn = pmreg_access,
2170 .fgt = FGT_PMCCFILTR_EL0,
2171 .type = ARM_CP_IO,
2172 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2173 .resetvalue = 0, },
2174 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2175 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2176 .accessfn = pmreg_access,
2177 .fgt = FGT_PMEVTYPERN_EL0,
2178 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2179 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2180 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2181 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2182 .accessfn = pmreg_access,
2183 .fgt = FGT_PMEVTYPERN_EL0,
2184 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2185 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2186 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2187 .accessfn = pmreg_access_xevcntr,
2188 .fgt = FGT_PMEVCNTRN_EL0,
2189 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2190 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2191 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2192 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2193 .accessfn = pmreg_access_xevcntr,
2194 .fgt = FGT_PMEVCNTRN_EL0,
2195 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2196 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2197 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2198 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2199 .resetvalue = 0,
2200 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2201 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2202 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2203 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2204 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2205 .resetvalue = 0,
2206 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2207 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2208 .access = PL1_RW, .accessfn = access_tpm,
2209 .fgt = FGT_PMINTEN,
2210 .type = ARM_CP_ALIAS | ARM_CP_IO,
2211 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2212 .resetvalue = 0,
2213 .writefn = pmintenset_write, .raw_writefn = raw_write },
2214 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2215 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2216 .access = PL1_RW, .accessfn = access_tpm,
2217 .fgt = FGT_PMINTEN,
2218 .type = ARM_CP_IO,
2219 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2220 .writefn = pmintenset_write, .raw_writefn = raw_write,
2221 .resetvalue = 0x0 },
2222 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2223 .access = PL1_RW, .accessfn = access_tpm,
2224 .fgt = FGT_PMINTEN,
2225 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2226 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2227 .writefn = pmintenclr_write, },
2228 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2229 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2230 .access = PL1_RW, .accessfn = access_tpm,
2231 .fgt = FGT_PMINTEN,
2232 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2233 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2234 .writefn = pmintenclr_write },
2235 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2236 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2237 .access = PL1_R,
2238 .accessfn = access_tid4,
2239 .fgt = FGT_CCSIDR_EL1,
2240 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2241 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2242 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2243 .access = PL1_RW,
2244 .accessfn = access_tid4,
2245 .fgt = FGT_CSSELR_EL1,
2246 .writefn = csselr_write, .resetvalue = 0,
2247 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2248 offsetof(CPUARMState, cp15.csselr_ns) } },
2249 /*
2250 * Auxiliary ID register: this actually has an IMPDEF value but for now
2251 * just RAZ for all cores:
2252 */
2253 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2254 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2255 .access = PL1_R, .type = ARM_CP_CONST,
2256 .accessfn = access_aa64_tid1,
2257 .fgt = FGT_AIDR_EL1,
2258 .resetvalue = 0 },
2259 /*
2260 * Auxiliary fault status registers: these also are IMPDEF, and we
2261 * choose to RAZ/WI for all cores.
2262 */
2263 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2264 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2265 .access = PL1_RW, .accessfn = access_tvm_trvm,
2266 .fgt = FGT_AFSR0_EL1,
2267 .nv2_redirect_offset = 0x128 | NV2_REDIR_NV1,
2268 .type = ARM_CP_CONST, .resetvalue = 0 },
2269 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2270 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2271 .access = PL1_RW, .accessfn = access_tvm_trvm,
2272 .fgt = FGT_AFSR1_EL1,
2273 .nv2_redirect_offset = 0x130 | NV2_REDIR_NV1,
2274 .type = ARM_CP_CONST, .resetvalue = 0 },
2275 /*
2276 * MAIR can just read-as-written because we don't implement caches
2277 * and so don't need to care about memory attributes.
2278 */
2279 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2280 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2281 .access = PL1_RW, .accessfn = access_tvm_trvm,
2282 .fgt = FGT_MAIR_EL1,
2283 .nv2_redirect_offset = 0x140 | NV2_REDIR_NV1,
2284 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2285 .resetvalue = 0 },
2286 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2287 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2288 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2289 .resetvalue = 0 },
2290 /*
2291 * For non-long-descriptor page tables these are PRRR and NMRR;
2292 * regardless they still act as reads-as-written for QEMU.
2293 */
2294 /*
2295 * MAIR0/1 are defined separately from their 64-bit counterpart which
2296 * allows them to assign the correct fieldoffset based on the endianness
2297 * handled in the field definitions.
2298 */
2299 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2300 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2301 .access = PL1_RW, .accessfn = access_tvm_trvm,
2302 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2303 offsetof(CPUARMState, cp15.mair0_ns) },
2304 .resetfn = arm_cp_reset_ignore },
2305 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2306 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2307 .access = PL1_RW, .accessfn = access_tvm_trvm,
2308 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2309 offsetof(CPUARMState, cp15.mair1_ns) },
2310 .resetfn = arm_cp_reset_ignore },
2311 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2312 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2313 .fgt = FGT_ISR_EL1,
2314 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2315 /* 32 bit ITLB invalidates */
2316 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2317 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2318 .writefn = tlbiall_write },
2319 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2320 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2321 .writefn = tlbimva_write },
2322 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2323 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2324 .writefn = tlbiasid_write },
2325 /* 32 bit DTLB invalidates */
2326 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2327 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2328 .writefn = tlbiall_write },
2329 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2330 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2331 .writefn = tlbimva_write },
2332 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2333 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2334 .writefn = tlbiasid_write },
2335 /* 32 bit TLB invalidates */
2336 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2337 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2338 .writefn = tlbiall_write },
2339 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2340 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2341 .writefn = tlbimva_write },
2342 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2343 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2344 .writefn = tlbiasid_write },
2345 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2346 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2347 .writefn = tlbimvaa_write },
2348 };
2349
2350 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2351 /* 32 bit TLB invalidates, Inner Shareable */
2352 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2353 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2354 .writefn = tlbiall_is_write },
2355 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2356 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2357 .writefn = tlbimva_is_write },
2358 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2359 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2360 .writefn = tlbiasid_is_write },
2361 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2362 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2363 .writefn = tlbimvaa_is_write },
2364 };
2365
2366 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2367 /* PMOVSSET is not implemented in v7 before v7ve */
2368 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2369 .access = PL0_RW, .accessfn = pmreg_access,
2370 .fgt = FGT_PMOVS,
2371 .type = ARM_CP_ALIAS | ARM_CP_IO,
2372 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2373 .writefn = pmovsset_write,
2374 .raw_writefn = raw_write },
2375 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2376 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2377 .access = PL0_RW, .accessfn = pmreg_access,
2378 .fgt = FGT_PMOVS,
2379 .type = ARM_CP_ALIAS | ARM_CP_IO,
2380 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2381 .writefn = pmovsset_write,
2382 .raw_writefn = raw_write },
2383 };
2384
2385 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2386 uint64_t value)
2387 {
2388 value &= 1;
2389 env->teecr = value;
2390 }
2391
2392 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2393 bool isread)
2394 {
2395 /*
2396 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2397 * at all, so we don't need to check whether we're v8A.
2398 */
2399 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2400 (env->cp15.hstr_el2 & HSTR_TTEE)) {
2401 return CP_ACCESS_TRAP_EL2;
2402 }
2403 return CP_ACCESS_OK;
2404 }
2405
2406 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2407 bool isread)
2408 {
2409 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2410 return CP_ACCESS_TRAP;
2411 }
2412 return teecr_access(env, ri, isread);
2413 }
2414
2415 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2416 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2417 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2418 .resetvalue = 0,
2419 .writefn = teecr_write, .accessfn = teecr_access },
2420 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2421 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2422 .accessfn = teehbr_access, .resetvalue = 0 },
2423 };
2424
2425 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2426 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2427 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2428 .access = PL0_RW,
2429 .fgt = FGT_TPIDR_EL0,
2430 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2431 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2432 .access = PL0_RW,
2433 .fgt = FGT_TPIDR_EL0,
2434 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2435 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2436 .resetfn = arm_cp_reset_ignore },
2437 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2438 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2439 .access = PL0_R | PL1_W,
2440 .fgt = FGT_TPIDRRO_EL0,
2441 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2442 .resetvalue = 0},
2443 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2444 .access = PL0_R | PL1_W,
2445 .fgt = FGT_TPIDRRO_EL0,
2446 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2447 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2448 .resetfn = arm_cp_reset_ignore },
2449 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2450 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2451 .access = PL1_RW,
2452 .fgt = FGT_TPIDR_EL1,
2453 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2454 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2455 .access = PL1_RW,
2456 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2457 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2458 .resetvalue = 0 },
2459 };
2460
2461 #ifndef CONFIG_USER_ONLY
2462
2463 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2464 bool isread)
2465 {
2466 /*
2467 * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2468 * Writable only at the highest implemented exception level.
2469 */
2470 int el = arm_current_el(env);
2471 uint64_t hcr;
2472 uint32_t cntkctl;
2473
2474 switch (el) {
2475 case 0:
2476 hcr = arm_hcr_el2_eff(env);
2477 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2478 cntkctl = env->cp15.cnthctl_el2;
2479 } else {
2480 cntkctl = env->cp15.c14_cntkctl;
2481 }
2482 if (!extract32(cntkctl, 0, 2)) {
2483 return CP_ACCESS_TRAP;
2484 }
2485 break;
2486 case 1:
2487 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2488 arm_is_secure_below_el3(env)) {
2489 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2490 return CP_ACCESS_TRAP_UNCATEGORIZED;
2491 }
2492 break;
2493 case 2:
2494 case 3:
2495 break;
2496 }
2497
2498 if (!isread && el < arm_highest_el(env)) {
2499 return CP_ACCESS_TRAP_UNCATEGORIZED;
2500 }
2501
2502 return CP_ACCESS_OK;
2503 }
2504
2505 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2506 bool isread)
2507 {
2508 unsigned int cur_el = arm_current_el(env);
2509 bool has_el2 = arm_is_el2_enabled(env);
2510 uint64_t hcr = arm_hcr_el2_eff(env);
2511
2512 switch (cur_el) {
2513 case 0:
2514 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2515 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2516 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2517 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2518 }
2519
2520 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2521 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2522 return CP_ACCESS_TRAP;
2523 }
2524 /* fall through */
2525 case 1:
2526 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2527 if (has_el2 && timeridx == GTIMER_PHYS &&
2528 (hcr & HCR_E2H
2529 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2530 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2531 return CP_ACCESS_TRAP_EL2;
2532 }
2533 if (has_el2 && timeridx == GTIMER_VIRT) {
2534 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVCT)) {
2535 return CP_ACCESS_TRAP_EL2;
2536 }
2537 }
2538 break;
2539 }
2540 return CP_ACCESS_OK;
2541 }
2542
2543 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2544 bool isread)
2545 {
2546 unsigned int cur_el = arm_current_el(env);
2547 bool has_el2 = arm_is_el2_enabled(env);
2548 uint64_t hcr = arm_hcr_el2_eff(env);
2549
2550 switch (cur_el) {
2551 case 0:
2552 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2553 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2554 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2555 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2556 }
2557
2558 /*
2559 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2560 * EL0 if EL0[PV]TEN is zero.
2561 */
2562 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2563 return CP_ACCESS_TRAP;
2564 }
2565 /* fall through */
2566
2567 case 1:
2568 if (has_el2 && timeridx == GTIMER_PHYS) {
2569 if (hcr & HCR_E2H) {
2570 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2571 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2572 return CP_ACCESS_TRAP_EL2;
2573 }
2574 } else {
2575 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2576 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2577 return CP_ACCESS_TRAP_EL2;
2578 }
2579 }
2580 }
2581 if (has_el2 && timeridx == GTIMER_VIRT) {
2582 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVT)) {
2583 return CP_ACCESS_TRAP_EL2;
2584 }
2585 }
2586 break;
2587 }
2588 return CP_ACCESS_OK;
2589 }
2590
2591 static CPAccessResult gt_pct_access(CPUARMState *env,
2592 const ARMCPRegInfo *ri,
2593 bool isread)
2594 {
2595 return gt_counter_access(env, GTIMER_PHYS, isread);
2596 }
2597
2598 static CPAccessResult gt_vct_access(CPUARMState *env,
2599 const ARMCPRegInfo *ri,
2600 bool isread)
2601 {
2602 return gt_counter_access(env, GTIMER_VIRT, isread);
2603 }
2604
2605 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2606 bool isread)
2607 {
2608 return gt_timer_access(env, GTIMER_PHYS, isread);
2609 }
2610
2611 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2612 bool isread)
2613 {
2614 return gt_timer_access(env, GTIMER_VIRT, isread);
2615 }
2616
2617 static CPAccessResult gt_stimer_access(CPUARMState *env,
2618 const ARMCPRegInfo *ri,
2619 bool isread)
2620 {
2621 /*
2622 * The AArch64 register view of the secure physical timer is
2623 * always accessible from EL3, and configurably accessible from
2624 * Secure EL1.
2625 */
2626 switch (arm_current_el(env)) {
2627 case 1:
2628 if (!arm_is_secure(env)) {
2629 return CP_ACCESS_TRAP;
2630 }
2631 if (!(env->cp15.scr_el3 & SCR_ST)) {
2632 return CP_ACCESS_TRAP_EL3;
2633 }
2634 return CP_ACCESS_OK;
2635 case 0:
2636 case 2:
2637 return CP_ACCESS_TRAP;
2638 case 3:
2639 return CP_ACCESS_OK;
2640 default:
2641 g_assert_not_reached();
2642 }
2643 }
2644
2645 static uint64_t gt_get_countervalue(CPUARMState *env)
2646 {
2647 ARMCPU *cpu = env_archcpu(env);
2648
2649 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2650 }
2651
2652 static void gt_update_irq(ARMCPU *cpu, int timeridx)
2653 {
2654 CPUARMState *env = &cpu->env;
2655 uint64_t cnthctl = env->cp15.cnthctl_el2;
2656 ARMSecuritySpace ss = arm_security_space(env);
2657 /* ISTATUS && !IMASK */
2658 int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2659
2660 /*
2661 * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2662 * It is RES0 in Secure and NonSecure state.
2663 */
2664 if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2665 ((timeridx == GTIMER_VIRT && (cnthctl & R_CNTHCTL_CNTVMASK_MASK)) ||
2666 (timeridx == GTIMER_PHYS && (cnthctl & R_CNTHCTL_CNTPMASK_MASK)))) {
2667 irqstate = 0;
2668 }
2669
2670 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2671 trace_arm_gt_update_irq(timeridx, irqstate);
2672 }
2673
2674 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2675 {
2676 /*
2677 * Changing security state between Root and Secure/NonSecure, which may
2678 * happen when switching EL, can change the effective value of CNTHCTL_EL2
2679 * mask bits. Update the IRQ state accordingly.
2680 */
2681 gt_update_irq(cpu, GTIMER_VIRT);
2682 gt_update_irq(cpu, GTIMER_PHYS);
2683 }
2684
2685 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2686 {
2687 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2688
2689 if (gt->ctl & 1) {
2690 /*
2691 * Timer enabled: calculate and set current ISTATUS, irq, and
2692 * reset timer to when ISTATUS next has to change
2693 */
2694 uint64_t offset = timeridx == GTIMER_VIRT ?
2695 cpu->env.cp15.cntvoff_el2 : 0;
2696 uint64_t count = gt_get_countervalue(&cpu->env);
2697 /* Note that this must be unsigned 64 bit arithmetic: */
2698 int istatus = count - offset >= gt->cval;
2699 uint64_t nexttick;
2700
2701 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2702
2703 if (istatus) {
2704 /*
2705 * Next transition is when (count - offset) rolls back over to 0.
2706 * If offset > count then this is when count == offset;
2707 * if offset <= count then this is when count == offset + 2^64
2708 * For the latter case we set nexttick to an "as far in future
2709 * as possible" value and let the code below handle it.
2710 */
2711 if (offset > count) {
2712 nexttick = offset;
2713 } else {
2714 nexttick = UINT64_MAX;
2715 }
2716 } else {
2717 /*
2718 * Next transition is when (count - offset) == cval, i.e.
2719 * when count == (cval + offset).
2720 * If that would overflow, then again we set up the next interrupt
2721 * for "as far in the future as possible" for the code below.
2722 */
2723 if (uadd64_overflow(gt->cval, offset, &nexttick)) {
2724 nexttick = UINT64_MAX;
2725 }
2726 }
2727 /*
2728 * Note that the desired next expiry time might be beyond the
2729 * signed-64-bit range of a QEMUTimer -- in this case we just
2730 * set the timer for as far in the future as possible. When the
2731 * timer expires we will reset the timer for any remaining period.
2732 */
2733 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2734 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2735 } else {
2736 timer_mod(cpu->gt_timer[timeridx], nexttick);
2737 }
2738 trace_arm_gt_recalc(timeridx, nexttick);
2739 } else {
2740 /* Timer disabled: ISTATUS and timer output always clear */
2741 gt->ctl &= ~4;
2742 timer_del(cpu->gt_timer[timeridx]);
2743 trace_arm_gt_recalc_disabled(timeridx);
2744 }
2745 gt_update_irq(cpu, timeridx);
2746 }
2747
2748 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2749 int timeridx)
2750 {
2751 ARMCPU *cpu = env_archcpu(env);
2752
2753 timer_del(cpu->gt_timer[timeridx]);
2754 }
2755
2756 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2757 {
2758 return gt_get_countervalue(env);
2759 }
2760
2761 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2762 {
2763 uint64_t hcr;
2764
2765 switch (arm_current_el(env)) {
2766 case 2:
2767 hcr = arm_hcr_el2_eff(env);
2768 if (hcr & HCR_E2H) {
2769 return 0;
2770 }
2771 break;
2772 case 0:
2773 hcr = arm_hcr_el2_eff(env);
2774 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2775 return 0;
2776 }
2777 break;
2778 }
2779
2780 return env->cp15.cntvoff_el2;
2781 }
2782
2783 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2784 {
2785 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2786 }
2787
2788 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2789 int timeridx,
2790 uint64_t value)
2791 {
2792 trace_arm_gt_cval_write(timeridx, value);
2793 env->cp15.c14_timer[timeridx].cval = value;
2794 gt_recalc_timer(env_archcpu(env), timeridx);
2795 }
2796
2797 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2798 int timeridx)
2799 {
2800 uint64_t offset = 0;
2801
2802 switch (timeridx) {
2803 case GTIMER_VIRT:
2804 case GTIMER_HYPVIRT:
2805 offset = gt_virt_cnt_offset(env);
2806 break;
2807 }
2808
2809 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2810 (gt_get_countervalue(env) - offset));
2811 }
2812
2813 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2814 int timeridx,
2815 uint64_t value)
2816 {
2817 uint64_t offset = 0;
2818
2819 switch (timeridx) {
2820 case GTIMER_VIRT:
2821 case GTIMER_HYPVIRT:
2822 offset = gt_virt_cnt_offset(env);
2823 break;
2824 }
2825
2826 trace_arm_gt_tval_write(timeridx, value);
2827 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2828 sextract64(value, 0, 32);
2829 gt_recalc_timer(env_archcpu(env), timeridx);
2830 }
2831
2832 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2833 int timeridx,
2834 uint64_t value)
2835 {
2836 ARMCPU *cpu = env_archcpu(env);
2837 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2838
2839 trace_arm_gt_ctl_write(timeridx, value);
2840 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2841 if ((oldval ^ value) & 1) {
2842 /* Enable toggled */
2843 gt_recalc_timer(cpu, timeridx);
2844 } else if ((oldval ^ value) & 2) {
2845 /*
2846 * IMASK toggled: don't need to recalculate,
2847 * just set the interrupt line based on ISTATUS
2848 */
2849 trace_arm_gt_imask_toggle(timeridx);
2850 gt_update_irq(cpu, timeridx);
2851 }
2852 }
2853
2854 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2855 {
2856 gt_timer_reset(env, ri, GTIMER_PHYS);
2857 }
2858
2859 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2860 uint64_t value)
2861 {
2862 gt_cval_write(env, ri, GTIMER_PHYS, value);
2863 }
2864
2865 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2866 {
2867 return gt_tval_read(env, ri, GTIMER_PHYS);
2868 }
2869
2870 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2871 uint64_t value)
2872 {
2873 gt_tval_write(env, ri, GTIMER_PHYS, value);
2874 }
2875
2876 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2877 uint64_t value)
2878 {
2879 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2880 }
2881
2882 static int gt_phys_redir_timeridx(CPUARMState *env)
2883 {
2884 switch (arm_mmu_idx(env)) {
2885 case ARMMMUIdx_E20_0:
2886 case ARMMMUIdx_E20_2:
2887 case ARMMMUIdx_E20_2_PAN:
2888 return GTIMER_HYP;
2889 default:
2890 return GTIMER_PHYS;
2891 }
2892 }
2893
2894 static int gt_virt_redir_timeridx(CPUARMState *env)
2895 {
2896 switch (arm_mmu_idx(env)) {
2897 case ARMMMUIdx_E20_0:
2898 case ARMMMUIdx_E20_2:
2899 case ARMMMUIdx_E20_2_PAN:
2900 return GTIMER_HYPVIRT;
2901 default:
2902 return GTIMER_VIRT;
2903 }
2904 }
2905
2906 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2907 const ARMCPRegInfo *ri)
2908 {
2909 int timeridx = gt_phys_redir_timeridx(env);
2910 return env->cp15.c14_timer[timeridx].cval;
2911 }
2912
2913 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2914 uint64_t value)
2915 {
2916 int timeridx = gt_phys_redir_timeridx(env);
2917 gt_cval_write(env, ri, timeridx, value);
2918 }
2919
2920 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2921 const ARMCPRegInfo *ri)
2922 {
2923 int timeridx = gt_phys_redir_timeridx(env);
2924 return gt_tval_read(env, ri, timeridx);
2925 }
2926
2927 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2928 uint64_t value)
2929 {
2930 int timeridx = gt_phys_redir_timeridx(env);
2931 gt_tval_write(env, ri, timeridx, value);
2932 }
2933
2934 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2935 const ARMCPRegInfo *ri)
2936 {
2937 int timeridx = gt_phys_redir_timeridx(env);
2938 return env->cp15.c14_timer[timeridx].ctl;
2939 }
2940
2941 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2942 uint64_t value)
2943 {
2944 int timeridx = gt_phys_redir_timeridx(env);
2945 gt_ctl_write(env, ri, timeridx, value);
2946 }
2947
2948 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2949 {
2950 gt_timer_reset(env, ri, GTIMER_VIRT);
2951 }
2952
2953 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2954 uint64_t value)
2955 {
2956 gt_cval_write(env, ri, GTIMER_VIRT, value);
2957 }
2958
2959 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2960 {
2961 return gt_tval_read(env, ri, GTIMER_VIRT);
2962 }
2963
2964 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2965 uint64_t value)
2966 {
2967 gt_tval_write(env, ri, GTIMER_VIRT, value);
2968 }
2969
2970 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2971 uint64_t value)
2972 {
2973 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2974 }
2975
2976 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2977 uint64_t value)
2978 {
2979 ARMCPU *cpu = env_archcpu(env);
2980 uint32_t oldval = env->cp15.cnthctl_el2;
2981 uint32_t valid_mask =
2982 R_CNTHCTL_EL0PCTEN_E2H1_MASK |
2983 R_CNTHCTL_EL0VCTEN_E2H1_MASK |
2984 R_CNTHCTL_EVNTEN_MASK |
2985 R_CNTHCTL_EVNTDIR_MASK |
2986 R_CNTHCTL_EVNTI_MASK |
2987 R_CNTHCTL_EL0VTEN_MASK |
2988 R_CNTHCTL_EL0PTEN_MASK |
2989 R_CNTHCTL_EL1PCTEN_E2H1_MASK |
2990 R_CNTHCTL_EL1PTEN_MASK;
2991
2992 if (cpu_isar_feature(aa64_rme, cpu)) {
2993 valid_mask |= R_CNTHCTL_CNTVMASK_MASK | R_CNTHCTL_CNTPMASK_MASK;
2994 }
2995 if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
2996 valid_mask |=
2997 R_CNTHCTL_EL1TVT_MASK |
2998 R_CNTHCTL_EL1TVCT_MASK |
2999 R_CNTHCTL_EL1NVPCT_MASK |
3000 R_CNTHCTL_EL1NVVCT_MASK |
3001 R_CNTHCTL_EVNTIS_MASK;
3002 }
3003
3004 /* Clear RES0 bits */
3005 value &= valid_mask;
3006
3007 raw_write(env, ri, value);
3008
3009 if ((oldval ^ value) & R_CNTHCTL_CNTVMASK_MASK) {
3010 gt_update_irq(cpu, GTIMER_VIRT);
3011 } else if ((oldval ^ value) & R_CNTHCTL_CNTPMASK_MASK) {
3012 gt_update_irq(cpu, GTIMER_PHYS);
3013 }
3014 }
3015
3016 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
3017 uint64_t value)
3018 {
3019 ARMCPU *cpu = env_archcpu(env);
3020
3021 trace_arm_gt_cntvoff_write(value);
3022 raw_write(env, ri, value);
3023 gt_recalc_timer(cpu, GTIMER_VIRT);
3024 }
3025
3026 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
3027 const ARMCPRegInfo *ri)
3028 {
3029 int timeridx = gt_virt_redir_timeridx(env);
3030 return env->cp15.c14_timer[timeridx].cval;
3031 }
3032
3033 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3034 uint64_t value)
3035 {
3036 int timeridx = gt_virt_redir_timeridx(env);
3037 gt_cval_write(env, ri, timeridx, value);
3038 }
3039
3040 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
3041 const ARMCPRegInfo *ri)
3042 {
3043 int timeridx = gt_virt_redir_timeridx(env);
3044 return gt_tval_read(env, ri, timeridx);
3045 }
3046
3047 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3048 uint64_t value)
3049 {
3050 int timeridx = gt_virt_redir_timeridx(env);
3051 gt_tval_write(env, ri, timeridx, value);
3052 }
3053
3054 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
3055 const ARMCPRegInfo *ri)
3056 {
3057 int timeridx = gt_virt_redir_timeridx(env);
3058 return env->cp15.c14_timer[timeridx].ctl;
3059 }
3060
3061 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3062 uint64_t value)
3063 {
3064 int timeridx = gt_virt_redir_timeridx(env);
3065 gt_ctl_write(env, ri, timeridx, value);
3066 }
3067
3068 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3069 {
3070 gt_timer_reset(env, ri, GTIMER_HYP);
3071 }
3072
3073 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3074 uint64_t value)
3075 {
3076 gt_cval_write(env, ri, GTIMER_HYP, value);
3077 }
3078
3079 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3080 {
3081 return gt_tval_read(env, ri, GTIMER_HYP);
3082 }
3083
3084 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3085 uint64_t value)
3086 {
3087 gt_tval_write(env, ri, GTIMER_HYP, value);
3088 }
3089
3090 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3091 uint64_t value)
3092 {
3093 gt_ctl_write(env, ri, GTIMER_HYP, value);
3094 }
3095
3096 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3097 {
3098 gt_timer_reset(env, ri, GTIMER_SEC);
3099 }
3100
3101 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3102 uint64_t value)
3103 {
3104 gt_cval_write(env, ri, GTIMER_SEC, value);
3105 }
3106
3107 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3108 {
3109 return gt_tval_read(env, ri, GTIMER_SEC);
3110 }
3111
3112 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3113 uint64_t value)
3114 {
3115 gt_tval_write(env, ri, GTIMER_SEC, value);
3116 }
3117
3118 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3119 uint64_t value)
3120 {
3121 gt_ctl_write(env, ri, GTIMER_SEC, value);
3122 }
3123
3124 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3125 {
3126 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3127 }
3128
3129 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3130 uint64_t value)
3131 {
3132 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3133 }
3134
3135 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3136 {
3137 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3138 }
3139
3140 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3141 uint64_t value)
3142 {
3143 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3144 }
3145
3146 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3147 uint64_t value)
3148 {
3149 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3150 }
3151
3152 void arm_gt_ptimer_cb(void *opaque)
3153 {
3154 ARMCPU *cpu = opaque;
3155
3156 gt_recalc_timer(cpu, GTIMER_PHYS);
3157 }
3158
3159 void arm_gt_vtimer_cb(void *opaque)
3160 {
3161 ARMCPU *cpu = opaque;
3162
3163 gt_recalc_timer(cpu, GTIMER_VIRT);
3164 }
3165
3166 void arm_gt_htimer_cb(void *opaque)
3167 {
3168 ARMCPU *cpu = opaque;
3169
3170 gt_recalc_timer(cpu, GTIMER_HYP);
3171 }
3172
3173 void arm_gt_stimer_cb(void *opaque)
3174 {
3175 ARMCPU *cpu = opaque;
3176
3177 gt_recalc_timer(cpu, GTIMER_SEC);
3178 }
3179
3180 void arm_gt_hvtimer_cb(void *opaque)
3181 {
3182 ARMCPU *cpu = opaque;
3183
3184 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3185 }
3186
3187 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3188 {
3189 ARMCPU *cpu = env_archcpu(env);
3190
3191 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3192 }
3193
3194 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3195 /*
3196 * Note that CNTFRQ is purely reads-as-written for the benefit
3197 * of software; writing it doesn't actually change the timer frequency.
3198 * Our reset value matches the fixed frequency we implement the timer at.
3199 */
3200 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3201 .type = ARM_CP_ALIAS,
3202 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3203 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3204 },
3205 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3206 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3207 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3208 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3209 .resetfn = arm_gt_cntfrq_reset,
3210 },
3211 /* overall control: mostly access permissions */
3212 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3213 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3214 .access = PL1_RW,
3215 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3216 .resetvalue = 0,
3217 },
3218 /* per-timer control */
3219 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3220 .secure = ARM_CP_SECSTATE_NS,
3221 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3222 .accessfn = gt_ptimer_access,
3223 .fieldoffset = offsetoflow32(CPUARMState,
3224 cp15.c14_timer[GTIMER_PHYS].ctl),
3225 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3226 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3227 },
3228 { .name = "CNTP_CTL_S",
3229 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3230 .secure = ARM_CP_SECSTATE_S,
3231 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3232 .accessfn = gt_ptimer_access,
3233 .fieldoffset = offsetoflow32(CPUARMState,
3234 cp15.c14_timer[GTIMER_SEC].ctl),
3235 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3236 },
3237 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3238 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3239 .type = ARM_CP_IO, .access = PL0_RW,
3240 .accessfn = gt_ptimer_access,
3241 .nv2_redirect_offset = 0x180 | NV2_REDIR_NV1,
3242 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3243 .resetvalue = 0,
3244 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3245 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3246 },
3247 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3248 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3249 .accessfn = gt_vtimer_access,
3250 .fieldoffset = offsetoflow32(CPUARMState,
3251 cp15.c14_timer[GTIMER_VIRT].ctl),
3252 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3253 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3254 },
3255 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3256 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3257 .type = ARM_CP_IO, .access = PL0_RW,
3258 .accessfn = gt_vtimer_access,
3259 .nv2_redirect_offset = 0x170 | NV2_REDIR_NV1,
3260 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3261 .resetvalue = 0,
3262 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3263 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3264 },
3265 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3266 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3267 .secure = ARM_CP_SECSTATE_NS,
3268 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3269 .accessfn = gt_ptimer_access,
3270 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3271 },
3272 { .name = "CNTP_TVAL_S",
3273 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3274 .secure = ARM_CP_SECSTATE_S,
3275 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3276 .accessfn = gt_ptimer_access,
3277 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3278 },
3279 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3280 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3281 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3282 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3283 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3284 },
3285 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3286 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3287 .accessfn = gt_vtimer_access,
3288 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3289 },
3290 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3291 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3292 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3293 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3294 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3295 },
3296 /* The counter itself */
3297 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3298 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3299 .accessfn = gt_pct_access,
3300 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3301 },
3302 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3303 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3304 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3305 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3306 },
3307 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3308 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3309 .accessfn = gt_vct_access,
3310 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3311 },
3312 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3313 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3314 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3315 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3316 },
3317 /* Comparison value, indicating when the timer goes off */
3318 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3319 .secure = ARM_CP_SECSTATE_NS,
3320 .access = PL0_RW,
3321 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3322 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3323 .accessfn = gt_ptimer_access,
3324 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3325 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3326 },
3327 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3328 .secure = ARM_CP_SECSTATE_S,
3329 .access = PL0_RW,
3330 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3331 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3332 .accessfn = gt_ptimer_access,
3333 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3334 },
3335 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3336 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3337 .access = PL0_RW,
3338 .type = ARM_CP_IO,
3339 .nv2_redirect_offset = 0x178 | NV2_REDIR_NV1,
3340 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3341 .resetvalue = 0, .accessfn = gt_ptimer_access,
3342 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3343 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3344 },
3345 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3346 .access = PL0_RW,
3347 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3348 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3349 .accessfn = gt_vtimer_access,
3350 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3351 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3352 },
3353 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3354 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3355 .access = PL0_RW,
3356 .type = ARM_CP_IO,
3357 .nv2_redirect_offset = 0x168 | NV2_REDIR_NV1,
3358 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3359 .resetvalue = 0, .accessfn = gt_vtimer_access,
3360 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3361 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3362 },
3363 /*
3364 * Secure timer -- this is actually restricted to only EL3
3365 * and configurably Secure-EL1 via the accessfn.
3366 */
3367 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3368 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3369 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3370 .accessfn = gt_stimer_access,
3371 .readfn = gt_sec_tval_read,
3372 .writefn = gt_sec_tval_write,
3373 .resetfn = gt_sec_timer_reset,
3374 },
3375 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3376 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3377 .type = ARM_CP_IO, .access = PL1_RW,
3378 .accessfn = gt_stimer_access,
3379 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3380 .resetvalue = 0,
3381 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3382 },
3383 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3384 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3385 .type = ARM_CP_IO, .access = PL1_RW,
3386 .accessfn = gt_stimer_access,
3387 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3388 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3389 },
3390 };
3391
3392 #else
3393
3394 /*
3395 * In user-mode most of the generic timer registers are inaccessible
3396 * however modern kernels (4.12+) allow access to cntvct_el0
3397 */
3398
3399 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3400 {
3401 ARMCPU *cpu = env_archcpu(env);
3402
3403 /*
3404 * Currently we have no support for QEMUTimer in linux-user so we
3405 * can't call gt_get_countervalue(env), instead we directly
3406 * call the lower level functions.
3407 */
3408 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3409 }
3410
3411 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3412 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3413 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3414 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3415 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3416 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3417 },
3418 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3419 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3420 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3421 .readfn = gt_virt_cnt_read,
3422 },
3423 };
3424
3425 #endif
3426
3427 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3428 {
3429 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3430 raw_write(env, ri, value);
3431 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3432 raw_write(env, ri, value & 0xfffff6ff);
3433 } else {
3434 raw_write(env, ri, value & 0xfffff1ff);
3435 }
3436 }
3437
3438 #ifndef CONFIG_USER_ONLY
3439 /* get_phys_addr() isn't present for user-mode-only targets */
3440
3441 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3442 bool isread)
3443 {
3444 if (ri->opc2 & 4) {
3445 /*
3446 * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3447 * Secure EL1 (which can only happen if EL3 is AArch64).
3448 * They are simply UNDEF if executed from NS EL1.
3449 * They function normally from EL2 or EL3.
3450 */
3451 if (arm_current_el(env) == 1) {
3452 if (arm_is_secure_below_el3(env)) {
3453 if (env->cp15.scr_el3 & SCR_EEL2) {
3454 return CP_ACCESS_TRAP_EL2;
3455 }
3456 return CP_ACCESS_TRAP_EL3;
3457 }
3458 return CP_ACCESS_TRAP_UNCATEGORIZED;
3459 }
3460 }
3461 return CP_ACCESS_OK;
3462 }
3463
3464 #ifdef CONFIG_TCG
3465 static int par_el1_shareability(GetPhysAddrResult *res)
3466 {
3467 /*
3468 * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3469 * memory -- see pseudocode PAREncodeShareability().
3470 */
3471 if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3472 res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3473 return 2;
3474 }
3475 return res->cacheattrs.shareability;
3476 }
3477
3478 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3479 MMUAccessType access_type, ARMMMUIdx mmu_idx,
3480 ARMSecuritySpace ss)
3481 {
3482 bool ret;
3483 uint64_t par64;
3484 bool format64 = false;
3485 ARMMMUFaultInfo fi = {};
3486 GetPhysAddrResult res = {};
3487
3488 /*
3489 * I_MXTJT: Granule protection checks are not performed on the final address
3490 * of a successful translation.
3491 */
3492 ret = get_phys_addr_with_space_nogpc(env, value, access_type, mmu_idx, ss,
3493 &res, &fi);
3494
3495 /*
3496 * ATS operations only do S1 or S1+S2 translations, so we never
3497 * have to deal with the ARMCacheAttrs format for S2 only.
3498 */
3499 assert(!res.cacheattrs.is_s2_format);
3500
3501 if (ret) {
3502 /*
3503 * Some kinds of translation fault must cause exceptions rather
3504 * than being reported in the PAR.
3505 */
3506 int current_el = arm_current_el(env);
3507 int target_el;
3508 uint32_t syn, fsr, fsc;
3509 bool take_exc = false;
3510
3511 if (fi.s1ptw && current_el == 1
3512 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3513 /*
3514 * Synchronous stage 2 fault on an access made as part of the
3515 * translation table walk for AT S1E0* or AT S1E1* insn
3516 * executed from NS EL1. If this is a synchronous external abort
3517 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3518 * to EL3. Otherwise the fault is taken as an exception to EL2,
3519 * and HPFAR_EL2 holds the faulting IPA.
3520 */
3521 if (fi.type == ARMFault_SyncExternalOnWalk &&
3522 (env->cp15.scr_el3 & SCR_EA)) {
3523 target_el = 3;
3524 } else {
3525 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3526 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3527 env->cp15.hpfar_el2 |= HPFAR_NS;
3528 }
3529 target_el = 2;
3530 }
3531 take_exc = true;
3532 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3533 /*
3534 * Synchronous external aborts during a translation table walk
3535 * are taken as Data Abort exceptions.
3536 */
3537 if (fi.stage2) {
3538 if (current_el == 3) {
3539 target_el = 3;
3540 } else {
3541 target_el = 2;
3542 }
3543 } else {
3544 target_el = exception_target_el(env);
3545 }
3546 take_exc = true;
3547 }
3548
3549 if (take_exc) {
3550 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3551 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3552 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3553 fsr = arm_fi_to_lfsc(&fi);
3554 fsc = extract32(fsr, 0, 6);
3555 } else {
3556 fsr = arm_fi_to_sfsc(&fi);
3557 fsc = 0x3f;
3558 }
3559 /*
3560 * Report exception with ESR indicating a fault due to a
3561 * translation table walk for a cache maintenance instruction.
3562 */
3563 syn = syn_data_abort_no_iss(current_el == target_el, 0,
3564 fi.ea, 1, fi.s1ptw, 1, fsc);
3565 env->exception.vaddress = value;
3566 env->exception.fsr = fsr;
3567 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3568 }
3569 }
3570
3571 if (is_a64(env)) {
3572 format64 = true;
3573 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3574 /*
3575 * ATS1Cxx:
3576 * * TTBCR.EAE determines whether the result is returned using the
3577 * 32-bit or the 64-bit PAR format
3578 * * Instructions executed in Hyp mode always use the 64bit format
3579 *
3580 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3581 * * The Non-secure TTBCR.EAE bit is set to 1
3582 * * The implementation includes EL2, and the value of HCR.VM is 1
3583 *
3584 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3585 *
3586 * ATS1Hx always uses the 64bit format.
3587 */
3588 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3589
3590 if (arm_feature(env, ARM_FEATURE_EL2)) {
3591 if (mmu_idx == ARMMMUIdx_E10_0 ||
3592 mmu_idx == ARMMMUIdx_E10_1 ||
3593 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3594 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3595 } else {
3596 format64 |= arm_current_el(env) == 2;
3597 }
3598 }
3599 }
3600
3601 if (format64) {
3602 /* Create a 64-bit PAR */
3603 par64 = (1 << 11); /* LPAE bit always set */
3604 if (!ret) {
3605 par64 |= res.f.phys_addr & ~0xfffULL;
3606 if (!res.f.attrs.secure) {
3607 par64 |= (1 << 9); /* NS */
3608 }
3609 par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3610 par64 |= par_el1_shareability(&res) << 7; /* SH */
3611 } else {
3612 uint32_t fsr = arm_fi_to_lfsc(&fi);
3613
3614 par64 |= 1; /* F */
3615 par64 |= (fsr & 0x3f) << 1; /* FS */
3616 if (fi.stage2) {
3617 par64 |= (1 << 9); /* S */
3618 }
3619 if (fi.s1ptw) {
3620 par64 |= (1 << 8); /* PTW */
3621 }
3622 }
3623 } else {
3624 /*
3625 * fsr is a DFSR/IFSR value for the short descriptor
3626 * translation table format (with WnR always clear).
3627 * Convert it to a 32-bit PAR.
3628 */
3629 if (!ret) {
3630 /* We do not set any attribute bits in the PAR */
3631 if (res.f.lg_page_size == 24
3632 && arm_feature(env, ARM_FEATURE_V7)) {
3633 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3634 } else {
3635 par64 = res.f.phys_addr & 0xfffff000;
3636 }
3637 if (!res.f.attrs.secure) {
3638 par64 |= (1 << 9); /* NS */
3639 }
3640 } else {
3641 uint32_t fsr = arm_fi_to_sfsc(&fi);
3642
3643 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3644 ((fsr & 0xf) << 1) | 1;
3645 }
3646 }
3647 return par64;
3648 }
3649 #endif /* CONFIG_TCG */
3650
3651 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3652 {
3653 #ifdef CONFIG_TCG
3654 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3655 uint64_t par64;
3656 ARMMMUIdx mmu_idx;
3657 int el = arm_current_el(env);
3658 ARMSecuritySpace ss = arm_security_space(env);
3659
3660 switch (ri->opc2 & 6) {
3661 case 0:
3662 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3663 switch (el) {
3664 case 3:
3665 mmu_idx = ARMMMUIdx_E3;
3666 break;
3667 case 2:
3668 g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */
3669 /* fall through */
3670 case 1:
3671 if (ri->crm == 9 && arm_pan_enabled(env)) {
3672 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3673 } else {
3674 mmu_idx = ARMMMUIdx_Stage1_E1;
3675 }
3676 break;
3677 default:
3678 g_assert_not_reached();
3679 }
3680 break;
3681 case 2:
3682 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3683 switch (el) {
3684 case 3:
3685 mmu_idx = ARMMMUIdx_E10_0;
3686 break;
3687 case 2:
3688 g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */
3689 mmu_idx = ARMMMUIdx_Stage1_E0;
3690 break;
3691 case 1:
3692 mmu_idx = ARMMMUIdx_Stage1_E0;
3693 break;
3694 default:
3695 g_assert_not_reached();
3696 }
3697 break;
3698 case 4:
3699 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3700 mmu_idx = ARMMMUIdx_E10_1;
3701 ss = ARMSS_NonSecure;
3702 break;
3703 case 6:
3704 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3705 mmu_idx = ARMMMUIdx_E10_0;
3706 ss = ARMSS_NonSecure;
3707 break;
3708 default:
3709 g_assert_not_reached();
3710 }
3711
3712 par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
3713
3714 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3715 #else
3716 /* Handled by hardware accelerator. */
3717 g_assert_not_reached();
3718 #endif /* CONFIG_TCG */
3719 }
3720
3721 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3722 uint64_t value)
3723 {
3724 #ifdef CONFIG_TCG
3725 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3726 uint64_t par64;
3727
3728 /* There is no SecureEL2 for AArch32. */
3729 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
3730 ARMSS_NonSecure);
3731
3732 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3733 #else
3734 /* Handled by hardware accelerator. */
3735 g_assert_not_reached();
3736 #endif /* CONFIG_TCG */
3737 }
3738
3739 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
3740 bool isread)
3741 {
3742 /*
3743 * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
3744 * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
3745 * only happen when executing at EL3 because that combination also causes an
3746 * illegal exception return. We don't need to check FEAT_RME either, because
3747 * scr_write() ensures that the NSE bit is not set otherwise.
3748 */
3749 if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
3750 return CP_ACCESS_TRAP;
3751 }
3752 return CP_ACCESS_OK;
3753 }
3754
3755 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3756 bool isread)
3757 {
3758 if (arm_current_el(env) == 3 &&
3759 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3760 return CP_ACCESS_TRAP;
3761 }
3762 return at_e012_access(env, ri, isread);
3763 }
3764
3765 static CPAccessResult at_s1e01_access(CPUARMState *env, const ARMCPRegInfo *ri,
3766 bool isread)
3767 {
3768 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_AT)) {
3769 return CP_ACCESS_TRAP_EL2;
3770 }
3771 return at_e012_access(env, ri, isread);
3772 }
3773
3774 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3775 uint64_t value)
3776 {
3777 #ifdef CONFIG_TCG
3778 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3779 ARMMMUIdx mmu_idx;
3780 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3781 bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3782
3783 switch (ri->opc2 & 6) {
3784 case 0:
3785 switch (ri->opc1) {
3786 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3787 if (ri->crm == 9 && arm_pan_enabled(env)) {
3788 mmu_idx = regime_e20 ?
3789 ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3790 } else {
3791 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3792 }
3793 break;
3794 case 4: /* AT S1E2R, AT S1E2W */
3795 mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3796 break;
3797 case 6: /* AT S1E3R, AT S1E3W */
3798 mmu_idx = ARMMMUIdx_E3;
3799 break;
3800 default:
3801 g_assert_not_reached();
3802 }
3803 break;
3804 case 2: /* AT S1E0R, AT S1E0W */
3805 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3806 break;
3807 case 4: /* AT S12E1R, AT S12E1W */
3808 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3809 break;
3810 case 6: /* AT S12E0R, AT S12E0W */
3811 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3812 break;
3813 default:
3814 g_assert_not_reached();
3815 }
3816
3817 env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3818 mmu_idx, arm_security_space(env));
3819 #else
3820 /* Handled by hardware accelerator. */
3821 g_assert_not_reached();
3822 #endif /* CONFIG_TCG */
3823 }
3824 #endif
3825
3826 /* Return basic MPU access permission bits. */
3827 static uint32_t simple_mpu_ap_bits(uint32_t val)
3828 {
3829 uint32_t ret;
3830 uint32_t mask;
3831 int i;
3832 ret = 0;
3833 mask = 3;
3834 for (i = 0; i < 16; i += 2) {
3835 ret |= (val >> i) & mask;
3836 mask <<= 2;
3837 }
3838 return ret;
3839 }
3840
3841 /* Pad basic MPU access permission bits to extended format. */
3842 static uint32_t extended_mpu_ap_bits(uint32_t val)
3843 {
3844 uint32_t ret;
3845 uint32_t mask;
3846 int i;
3847 ret = 0;
3848 mask = 3;
3849 for (i = 0; i < 16; i += 2) {
3850 ret |= (val & mask) << i;
3851 mask <<= 2;
3852 }
3853 return ret;
3854 }
3855
3856 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3857 uint64_t value)
3858 {
3859 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3860 }
3861
3862 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3863 {
3864 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3865 }
3866
3867 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3868 uint64_t value)
3869 {
3870 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3871 }
3872
3873 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3874 {
3875 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3876 }
3877
3878 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3879 {
3880 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3881
3882 if (!u32p) {
3883 return 0;
3884 }
3885
3886 u32p += env->pmsav7.rnr[M_REG_NS];
3887 return *u32p;
3888 }
3889
3890 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3891 uint64_t value)
3892 {
3893 ARMCPU *cpu = env_archcpu(env);
3894 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3895
3896 if (!u32p) {
3897 return;
3898 }
3899
3900 u32p += env->pmsav7.rnr[M_REG_NS];
3901 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3902 *u32p = value;
3903 }
3904
3905 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3906 uint64_t value)
3907 {
3908 ARMCPU *cpu = env_archcpu(env);
3909 uint32_t nrgs = cpu->pmsav7_dregion;
3910
3911 if (value >= nrgs) {
3912 qemu_log_mask(LOG_GUEST_ERROR,
3913 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3914 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3915 return;
3916 }
3917
3918 raw_write(env, ri, value);
3919 }
3920
3921 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3922 uint64_t value)
3923 {
3924 ARMCPU *cpu = env_archcpu(env);
3925
3926 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3927 env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3928 }
3929
3930 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3931 {
3932 return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3933 }
3934
3935 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3936 uint64_t value)
3937 {
3938 ARMCPU *cpu = env_archcpu(env);
3939
3940 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3941 env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3942 }
3943
3944 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3945 {
3946 return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3947 }
3948
3949 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3950 uint64_t value)
3951 {
3952 ARMCPU *cpu = env_archcpu(env);
3953
3954 /*
3955 * Ignore writes that would select not implemented region.
3956 * This is architecturally UNPREDICTABLE.
3957 */
3958 if (value >= cpu->pmsav7_dregion) {
3959 return;
3960 }
3961
3962 env->pmsav7.rnr[M_REG_NS] = value;
3963 }
3964
3965 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3966 uint64_t value)
3967 {
3968 ARMCPU *cpu = env_archcpu(env);
3969
3970 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3971 env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3972 }
3973
3974 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3975 {
3976 return env->pmsav8.hprbar[env->pmsav8.hprselr];
3977 }
3978
3979 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3980 uint64_t value)
3981 {
3982 ARMCPU *cpu = env_archcpu(env);
3983
3984 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3985 env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
3986 }
3987
3988 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3989 {
3990 return env->pmsav8.hprlar[env->pmsav8.hprselr];
3991 }
3992
3993 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3994 uint64_t value)
3995 {
3996 uint32_t n;
3997 uint32_t bit;
3998 ARMCPU *cpu = env_archcpu(env);
3999
4000 /* Ignore writes to unimplemented regions */
4001 int rmax = MIN(cpu->pmsav8r_hdregion, 32);
4002 value &= MAKE_64BIT_MASK(0, rmax);
4003
4004 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4005
4006 /* Register alias is only valid for first 32 indexes */
4007 for (n = 0; n < rmax; ++n) {
4008 bit = extract32(value, n, 1);
4009 env->pmsav8.hprlar[n] = deposit32(
4010 env->pmsav8.hprlar[n], 0, 1, bit);
4011 }
4012 }
4013
4014 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4015 {
4016 uint32_t n;
4017 uint32_t result = 0x0;
4018 ARMCPU *cpu = env_archcpu(env);
4019
4020 /* Register alias is only valid for first 32 indexes */
4021 for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
4022 if (env->pmsav8.hprlar[n] & 0x1) {
4023 result |= (0x1 << n);
4024 }
4025 }
4026 return result;
4027 }
4028
4029 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4030 uint64_t value)
4031 {
4032 ARMCPU *cpu = env_archcpu(env);
4033
4034 /*
4035 * Ignore writes that would select not implemented region.
4036 * This is architecturally UNPREDICTABLE.
4037 */
4038 if (value >= cpu->pmsav8r_hdregion) {
4039 return;
4040 }
4041
4042 env->pmsav8.hprselr = value;
4043 }
4044
4045 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
4046 uint64_t value)
4047 {
4048 ARMCPU *cpu = env_archcpu(env);
4049 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4050 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4051
4052 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4053
4054 if (ri->opc1 & 4) {
4055 if (index >= cpu->pmsav8r_hdregion) {
4056 return;
4057 }
4058 if (ri->opc2 & 0x1) {
4059 env->pmsav8.hprlar[index] = value;
4060 } else {
4061 env->pmsav8.hprbar[index] = value;
4062 }
4063 } else {
4064 if (index >= cpu->pmsav7_dregion) {
4065 return;
4066 }
4067 if (ri->opc2 & 0x1) {
4068 env->pmsav8.rlar[M_REG_NS][index] = value;
4069 } else {
4070 env->pmsav8.rbar[M_REG_NS][index] = value;
4071 }
4072 }
4073 }
4074
4075 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
4076 {
4077 ARMCPU *cpu = env_archcpu(env);
4078 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4079 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4080
4081 if (ri->opc1 & 4) {
4082 if (index >= cpu->pmsav8r_hdregion) {
4083 return 0x0;
4084 }
4085 if (ri->opc2 & 0x1) {
4086 return env->pmsav8.hprlar[index];
4087 } else {
4088 return env->pmsav8.hprbar[index];
4089 }
4090 } else {
4091 if (index >= cpu->pmsav7_dregion) {
4092 return 0x0;
4093 }
4094 if (ri->opc2 & 0x1) {
4095 return env->pmsav8.rlar[M_REG_NS][index];
4096 } else {
4097 return env->pmsav8.rbar[M_REG_NS][index];
4098 }
4099 }
4100 }
4101
4102 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
4103 { .name = "PRBAR",
4104 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
4105 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4106 .accessfn = access_tvm_trvm,
4107 .readfn = prbar_read, .writefn = prbar_write },
4108 { .name = "PRLAR",
4109 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
4110 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4111 .accessfn = access_tvm_trvm,
4112 .readfn = prlar_read, .writefn = prlar_write },
4113 { .name = "PRSELR", .resetvalue = 0,
4114 .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
4115 .access = PL1_RW, .accessfn = access_tvm_trvm,
4116 .writefn = prselr_write,
4117 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
4118 { .name = "HPRBAR", .resetvalue = 0,
4119 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
4120 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4121 .readfn = hprbar_read, .writefn = hprbar_write },
4122 { .name = "HPRLAR",
4123 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
4124 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4125 .readfn = hprlar_read, .writefn = hprlar_write },
4126 { .name = "HPRSELR", .resetvalue = 0,
4127 .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
4128 .access = PL2_RW,
4129 .writefn = hprselr_write,
4130 .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
4131 { .name = "HPRENR",
4132 .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
4133 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4134 .readfn = hprenr_read, .writefn = hprenr_write },
4135 };
4136
4137 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
4138 /*
4139 * Reset for all these registers is handled in arm_cpu_reset(),
4140 * because the PMSAv7 is also used by M-profile CPUs, which do
4141 * not register cpregs but still need the state to be reset.
4142 */
4143 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4144 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4145 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
4146 .readfn = pmsav7_read, .writefn = pmsav7_write,
4147 .resetfn = arm_cp_reset_ignore },
4148 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4149 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4150 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
4151 .readfn = pmsav7_read, .writefn = pmsav7_write,
4152 .resetfn = arm_cp_reset_ignore },
4153 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4154 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4155 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
4156 .readfn = pmsav7_read, .writefn = pmsav7_write,
4157 .resetfn = arm_cp_reset_ignore },
4158 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4159 .access = PL1_RW,
4160 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
4161 .writefn = pmsav7_rgnr_write,
4162 .resetfn = arm_cp_reset_ignore },
4163 };
4164
4165 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4166 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4167 .access = PL1_RW, .type = ARM_CP_ALIAS,
4168 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4169 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4170 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4171 .access = PL1_RW, .type = ARM_CP_ALIAS,
4172 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4173 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4174 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4175 .access = PL1_RW,
4176 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4177 .resetvalue = 0, },
4178 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4179 .access = PL1_RW,
4180 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4181 .resetvalue = 0, },
4182 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4183 .access = PL1_RW,
4184 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4185 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4186 .access = PL1_RW,
4187 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
4188 /* Protection region base and size registers */
4189 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4190 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4191 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4192 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4193 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4194 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4195 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4196 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4197 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4198 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4199 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4200 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4201 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4202 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4203 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4204 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4205 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4206 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4207 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4208 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4209 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4210 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4211 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4212 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4213 };
4214
4215 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4216 uint64_t value)
4217 {
4218 ARMCPU *cpu = env_archcpu(env);
4219
4220 if (!arm_feature(env, ARM_FEATURE_V8)) {
4221 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4222 /*
4223 * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4224 * using Long-descriptor translation table format
4225 */
4226 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4227 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4228 /*
4229 * In an implementation that includes the Security Extensions
4230 * TTBCR has additional fields PD0 [4] and PD1 [5] for
4231 * Short-descriptor translation table format.
4232 */
4233 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4234 } else {
4235 value &= TTBCR_N;
4236 }
4237 }
4238
4239 if (arm_feature(env, ARM_FEATURE_LPAE)) {
4240 /*
4241 * With LPAE the TTBCR could result in a change of ASID
4242 * via the TTBCR.A1 bit, so do a TLB flush.
4243 */
4244 tlb_flush(CPU(cpu));
4245 }
4246 raw_write(env, ri, value);
4247 }
4248
4249 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4250 uint64_t value)
4251 {
4252 ARMCPU *cpu = env_archcpu(env);
4253
4254 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4255 tlb_flush(CPU(cpu));
4256 raw_write(env, ri, value);
4257 }
4258
4259 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4260 uint64_t value)
4261 {
4262 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
4263 if (cpreg_field_is_64bit(ri) &&
4264 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4265 ARMCPU *cpu = env_archcpu(env);
4266 tlb_flush(CPU(cpu));
4267 }
4268 raw_write(env, ri, value);
4269 }
4270
4271 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4272 uint64_t value)
4273 {
4274 /*
4275 * If we are running with E2&0 regime, then an ASID is active.
4276 * Flush if that might be changing. Note we're not checking
4277 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4278 * holds the active ASID, only checking the field that might.
4279 */
4280 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4281 (arm_hcr_el2_eff(env) & HCR_E2H)) {
4282 uint16_t mask = ARMMMUIdxBit_E20_2 |
4283 ARMMMUIdxBit_E20_2_PAN |
4284 ARMMMUIdxBit_E20_0;
4285 tlb_flush_by_mmuidx(env_cpu(env), mask);
4286 }
4287 raw_write(env, ri, value);
4288 }
4289
4290 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4291 uint64_t value)
4292 {
4293 ARMCPU *cpu = env_archcpu(env);
4294 CPUState *cs = CPU(cpu);
4295
4296 /*
4297 * A change in VMID to the stage2 page table (Stage2) invalidates
4298 * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4299 */
4300 if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4301 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4302 }
4303 raw_write(env, ri, value);
4304 }
4305
4306 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4307 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4308 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4309 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4310 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4311 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4312 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4313 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4314 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4315 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4316 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4317 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4318 offsetof(CPUARMState, cp15.dfar_ns) } },
4319 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4320 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4321 .access = PL1_RW, .accessfn = access_tvm_trvm,
4322 .fgt = FGT_FAR_EL1,
4323 .nv2_redirect_offset = 0x220 | NV2_REDIR_NV1,
4324 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4325 .resetvalue = 0, },
4326 };
4327
4328 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4329 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4330 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4331 .access = PL1_RW, .accessfn = access_tvm_trvm,
4332 .fgt = FGT_ESR_EL1,
4333 .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1,
4334 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4335 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4336 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4337 .access = PL1_RW, .accessfn = access_tvm_trvm,
4338 .fgt = FGT_TTBR0_EL1,
4339 .nv2_redirect_offset = 0x200 | NV2_REDIR_NV1,
4340 .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4341 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4342 offsetof(CPUARMState, cp15.ttbr0_ns) } },
4343 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4344 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4345 .access = PL1_RW, .accessfn = access_tvm_trvm,
4346 .fgt = FGT_TTBR1_EL1,
4347 .nv2_redirect_offset = 0x210 | NV2_REDIR_NV1,
4348 .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4349 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4350 offsetof(CPUARMState, cp15.ttbr1_ns) } },
4351 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4352 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4353 .access = PL1_RW, .accessfn = access_tvm_trvm,
4354 .fgt = FGT_TCR_EL1,
4355 .nv2_redirect_offset = 0x120 | NV2_REDIR_NV1,
4356 .writefn = vmsa_tcr_el12_write,
4357 .raw_writefn = raw_write,
4358 .resetvalue = 0,
4359 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4360 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4361 .access = PL1_RW, .accessfn = access_tvm_trvm,
4362 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4363 .raw_writefn = raw_write,
4364 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4365 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4366 };
4367
4368 /*
4369 * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4370 * qemu tlbs nor adjusting cached masks.
4371 */
4372 static const ARMCPRegInfo ttbcr2_reginfo = {
4373 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4374 .access = PL1_RW, .accessfn = access_tvm_trvm,
4375 .type = ARM_CP_ALIAS,
4376 .bank_fieldoffsets = {
4377 offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4378 offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4379 },
4380 };
4381
4382 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4383 uint64_t value)
4384 {
4385 env->cp15.c15_ticonfig = value & 0xe7;
4386 /* The OS_TYPE bit in this register changes the reported CPUID! */
4387 env->cp15.c0_cpuid = (value & (1 << 5)) ?
4388 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4389 }
4390
4391 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4392 uint64_t value)
4393 {
4394 env->cp15.c15_threadid = value & 0xffff;
4395 }
4396
4397 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4398 uint64_t value)
4399 {
4400 /* Wait-for-interrupt (deprecated) */
4401 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4402 }
4403
4404 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4405 uint64_t value)
4406 {
4407 /*
4408 * On OMAP there are registers indicating the max/min index of dcache lines
4409 * containing a dirty line; cache flush operations have to reset these.
4410 */
4411 env->cp15.c15_i_max = 0x000;
4412 env->cp15.c15_i_min = 0xff0;
4413 }
4414
4415 static const ARMCPRegInfo omap_cp_reginfo[] = {
4416 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4417 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4418 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4419 .resetvalue = 0, },
4420 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4421 .access = PL1_RW, .type = ARM_CP_NOP },
4422 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4423 .access = PL1_RW,
4424 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4425 .writefn = omap_ticonfig_write },
4426 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4427 .access = PL1_RW,
4428 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4429 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4430 .access = PL1_RW, .resetvalue = 0xff0,
4431 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4432 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4433 .access = PL1_RW,
4434 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4435 .writefn = omap_threadid_write },
4436 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4437 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4438 .type = ARM_CP_NO_RAW,
4439 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4440 /*
4441 * TODO: Peripheral port remap register:
4442 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4443 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4444 * when MMU is off.
4445 */
4446 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4447 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4448 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4449 .writefn = omap_cachemaint_write },
4450 { .name = "C9", .cp = 15, .crn = 9,
4451 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4452 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4453 };
4454
4455 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4456 uint64_t value)
4457 {
4458 env->cp15.c15_cpar = value & 0x3fff;
4459 }
4460
4461 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4462 { .name = "XSCALE_CPAR",
4463 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4464 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4465 .writefn = xscale_cpar_write, },
4466 { .name = "XSCALE_AUXCR",
4467 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4468 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4469 .resetvalue = 0, },
4470 /*
4471 * XScale specific cache-lockdown: since we have no cache we NOP these
4472 * and hope the guest does not really rely on cache behaviour.
4473 */
4474 { .name = "XSCALE_LOCK_ICACHE_LINE",
4475 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4476 .access = PL1_W, .type = ARM_CP_NOP },
4477 { .name = "XSCALE_UNLOCK_ICACHE",
4478 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4479 .access = PL1_W, .type = ARM_CP_NOP },
4480 { .name = "XSCALE_DCACHE_LOCK",
4481 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4482 .access = PL1_RW, .type = ARM_CP_NOP },
4483 { .name = "XSCALE_UNLOCK_DCACHE",
4484 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4485 .access = PL1_W, .type = ARM_CP_NOP },
4486 };
4487
4488 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4489 /*
4490 * RAZ/WI the whole crn=15 space, when we don't have a more specific
4491 * implementation of this implementation-defined space.
4492 * Ideally this should eventually disappear in favour of actually
4493 * implementing the correct behaviour for all cores.
4494 */
4495 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4496 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4497 .access = PL1_RW,
4498 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4499 .resetvalue = 0 },
4500 };
4501
4502 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4503 /* Cache status: RAZ because we have no cache so it's always clean */
4504 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4505 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4506 .resetvalue = 0 },
4507 };
4508
4509 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4510 /* We never have a block transfer operation in progress */
4511 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4512 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4513 .resetvalue = 0 },
4514 /* The cache ops themselves: these all NOP for QEMU */
4515 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4516 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4517 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4518 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4519 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4520 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4521 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4522 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4523 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4524 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4525 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4526 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4527 };
4528
4529 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4530 /*
4531 * The cache test-and-clean instructions always return (1 << 30)
4532 * to indicate that there are no dirty cache lines.
4533 */
4534 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4535 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4536 .resetvalue = (1 << 30) },
4537 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4538 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4539 .resetvalue = (1 << 30) },
4540 };
4541
4542 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4543 /* Ignore ReadBuffer accesses */
4544 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4545 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4546 .access = PL1_RW, .resetvalue = 0,
4547 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4548 };
4549
4550 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4551 {
4552 unsigned int cur_el = arm_current_el(env);
4553
4554 if (arm_is_el2_enabled(env) && cur_el == 1) {
4555 return env->cp15.vpidr_el2;
4556 }
4557 return raw_read(env, ri);
4558 }
4559
4560 static uint64_t mpidr_read_val(CPUARMState *env)
4561 {
4562 ARMCPU *cpu = env_archcpu(env);
4563 uint64_t mpidr = cpu->mp_affinity;
4564
4565 if (arm_feature(env, ARM_FEATURE_V7MP)) {
4566 mpidr |= (1U << 31);
4567 /*
4568 * Cores which are uniprocessor (non-coherent)
4569 * but still implement the MP extensions set
4570 * bit 30. (For instance, Cortex-R5).
4571 */
4572 if (cpu->mp_is_up) {
4573 mpidr |= (1u << 30);
4574 }
4575 }
4576 return mpidr;
4577 }
4578
4579 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4580 {
4581 unsigned int cur_el = arm_current_el(env);
4582
4583 if (arm_is_el2_enabled(env) && cur_el == 1) {
4584 return env->cp15.vmpidr_el2;
4585 }
4586 return mpidr_read_val(env);
4587 }
4588
4589 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4590 /* NOP AMAIR0/1 */
4591 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4592 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4593 .access = PL1_RW, .accessfn = access_tvm_trvm,
4594 .fgt = FGT_AMAIR_EL1,
4595 .nv2_redirect_offset = 0x148 | NV2_REDIR_NV1,
4596 .type = ARM_CP_CONST, .resetvalue = 0 },
4597 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4598 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4599 .access = PL1_RW, .accessfn = access_tvm_trvm,
4600 .type = ARM_CP_CONST, .resetvalue = 0 },
4601 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4602 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4603 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4604 offsetof(CPUARMState, cp15.par_ns)} },
4605 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4606 .access = PL1_RW, .accessfn = access_tvm_trvm,
4607 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4608 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4609 offsetof(CPUARMState, cp15.ttbr0_ns) },
4610 .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4611 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4612 .access = PL1_RW, .accessfn = access_tvm_trvm,
4613 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4614 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4615 offsetof(CPUARMState, cp15.ttbr1_ns) },
4616 .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4617 };
4618
4619 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4620 {
4621 return vfp_get_fpcr(env);
4622 }
4623
4624 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4625 uint64_t value)
4626 {
4627 vfp_set_fpcr(env, value);
4628 }
4629
4630 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4631 {
4632 return vfp_get_fpsr(env);
4633 }
4634
4635 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4636 uint64_t value)
4637 {
4638 vfp_set_fpsr(env, value);
4639 }
4640
4641 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4642 bool isread)
4643 {
4644 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4645 return CP_ACCESS_TRAP;
4646 }
4647 return CP_ACCESS_OK;
4648 }
4649
4650 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4651 uint64_t value)
4652 {
4653 env->daif = value & PSTATE_DAIF;
4654 }
4655
4656 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4657 {
4658 return env->pstate & PSTATE_PAN;
4659 }
4660
4661 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4662 uint64_t value)
4663 {
4664 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4665 }
4666
4667 static const ARMCPRegInfo pan_reginfo = {
4668 .name = "PAN", .state = ARM_CP_STATE_AA64,
4669 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4670 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4671 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4672 };
4673
4674 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4675 {
4676 return env->pstate & PSTATE_UAO;
4677 }
4678
4679 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4680 uint64_t value)
4681 {
4682 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4683 }
4684
4685 static const ARMCPRegInfo uao_reginfo = {
4686 .name = "UAO", .state = ARM_CP_STATE_AA64,
4687 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4688 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4689 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4690 };
4691
4692 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4693 {
4694 return env->pstate & PSTATE_DIT;
4695 }
4696
4697 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4698 uint64_t value)
4699 {
4700 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4701 }
4702
4703 static const ARMCPRegInfo dit_reginfo = {
4704 .name = "DIT", .state = ARM_CP_STATE_AA64,
4705 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4706 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4707 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4708 };
4709
4710 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4711 {
4712 return env->pstate & PSTATE_SSBS;
4713 }
4714
4715 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4716 uint64_t value)
4717 {
4718 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4719 }
4720
4721 static const ARMCPRegInfo ssbs_reginfo = {
4722 .name = "SSBS", .state = ARM_CP_STATE_AA64,
4723 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4724 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4725 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4726 };
4727
4728 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4729 const ARMCPRegInfo *ri,
4730 bool isread)
4731 {
4732 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4733 switch (arm_current_el(env)) {
4734 case 0:
4735 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4736 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4737 return CP_ACCESS_TRAP;
4738 }
4739 /* fall through */
4740 case 1:
4741 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4742 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4743 return CP_ACCESS_TRAP_EL2;
4744 }
4745 break;
4746 }
4747 return CP_ACCESS_OK;
4748 }
4749
4750 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4751 {
4752 /* Cache invalidate/clean to Point of Unification... */
4753 switch (arm_current_el(env)) {
4754 case 0:
4755 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4756 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4757 return CP_ACCESS_TRAP;
4758 }
4759 /* fall through */
4760 case 1:
4761 /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set. */
4762 if (arm_hcr_el2_eff(env) & hcrflags) {
4763 return CP_ACCESS_TRAP_EL2;
4764 }
4765 break;
4766 }
4767 return CP_ACCESS_OK;
4768 }
4769
4770 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4771 bool isread)
4772 {
4773 return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4774 }
4775
4776 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4777 bool isread)
4778 {
4779 return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4780 }
4781
4782 /*
4783 * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4784 * Page D4-1736 (DDI0487A.b)
4785 */
4786
4787 static int vae1_tlbmask(CPUARMState *env)
4788 {
4789 uint64_t hcr = arm_hcr_el2_eff(env);
4790 uint16_t mask;
4791
4792 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4793 mask = ARMMMUIdxBit_E20_2 |
4794 ARMMMUIdxBit_E20_2_PAN |
4795 ARMMMUIdxBit_E20_0;
4796 } else {
4797 mask = ARMMMUIdxBit_E10_1 |
4798 ARMMMUIdxBit_E10_1_PAN |
4799 ARMMMUIdxBit_E10_0;
4800 }
4801 return mask;
4802 }
4803
4804 static int vae2_tlbmask(CPUARMState *env)
4805 {
4806 uint64_t hcr = arm_hcr_el2_eff(env);
4807 uint16_t mask;
4808
4809 if (hcr & HCR_E2H) {
4810 mask = ARMMMUIdxBit_E20_2 |
4811 ARMMMUIdxBit_E20_2_PAN |
4812 ARMMMUIdxBit_E20_0;
4813 } else {
4814 mask = ARMMMUIdxBit_E2;
4815 }
4816 return mask;
4817 }
4818
4819 /* Return 56 if TBI is enabled, 64 otherwise. */
4820 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4821 uint64_t addr)
4822 {
4823 uint64_t tcr = regime_tcr(env, mmu_idx);
4824 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4825 int select = extract64(addr, 55, 1);
4826
4827 return (tbi >> select) & 1 ? 56 : 64;
4828 }
4829
4830 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4831 {
4832 uint64_t hcr = arm_hcr_el2_eff(env);
4833 ARMMMUIdx mmu_idx;
4834
4835 /* Only the regime of the mmu_idx below is significant. */
4836 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4837 mmu_idx = ARMMMUIdx_E20_0;
4838 } else {
4839 mmu_idx = ARMMMUIdx_E10_0;
4840 }
4841
4842 return tlbbits_for_regime(env, mmu_idx, addr);
4843 }
4844
4845 static int vae2_tlbbits(CPUARMState *env, uint64_t addr)
4846 {
4847 uint64_t hcr = arm_hcr_el2_eff(env);
4848 ARMMMUIdx mmu_idx;
4849
4850 /*
4851 * Only the regime of the mmu_idx below is significant.
4852 * Regime EL2&0 has two ranges with separate TBI configuration, while EL2
4853 * only has one.
4854 */
4855 if (hcr & HCR_E2H) {
4856 mmu_idx = ARMMMUIdx_E20_2;
4857 } else {
4858 mmu_idx = ARMMMUIdx_E2;
4859 }
4860
4861 return tlbbits_for_regime(env, mmu_idx, addr);
4862 }
4863
4864 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4865 uint64_t value)
4866 {
4867 CPUState *cs = env_cpu(env);
4868 int mask = vae1_tlbmask(env);
4869
4870 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4871 }
4872
4873 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4874 uint64_t value)
4875 {
4876 CPUState *cs = env_cpu(env);
4877 int mask = vae1_tlbmask(env);
4878
4879 if (tlb_force_broadcast(env)) {
4880 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4881 } else {
4882 tlb_flush_by_mmuidx(cs, mask);
4883 }
4884 }
4885
4886 static int e2_tlbmask(CPUARMState *env)
4887 {
4888 return (ARMMMUIdxBit_E20_0 |
4889 ARMMMUIdxBit_E20_2 |
4890 ARMMMUIdxBit_E20_2_PAN |
4891 ARMMMUIdxBit_E2);
4892 }
4893
4894 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4895 uint64_t value)
4896 {
4897 CPUState *cs = env_cpu(env);
4898 int mask = alle1_tlbmask(env);
4899
4900 tlb_flush_by_mmuidx(cs, mask);
4901 }
4902
4903 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4904 uint64_t value)
4905 {
4906 CPUState *cs = env_cpu(env);
4907 int mask = e2_tlbmask(env);
4908
4909 tlb_flush_by_mmuidx(cs, mask);
4910 }
4911
4912 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4913 uint64_t value)
4914 {
4915 ARMCPU *cpu = env_archcpu(env);
4916 CPUState *cs = CPU(cpu);
4917
4918 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
4919 }
4920
4921 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4922 uint64_t value)
4923 {
4924 CPUState *cs = env_cpu(env);
4925 int mask = alle1_tlbmask(env);
4926
4927 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4928 }
4929
4930 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4931 uint64_t value)
4932 {
4933 CPUState *cs = env_cpu(env);
4934 int mask = e2_tlbmask(env);
4935
4936 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4937 }
4938
4939 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4940 uint64_t value)
4941 {
4942 CPUState *cs = env_cpu(env);
4943
4944 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
4945 }
4946
4947 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4948 uint64_t value)
4949 {
4950 /*
4951 * Invalidate by VA, EL2
4952 * Currently handles both VAE2 and VALE2, since we don't support
4953 * flush-last-level-only.
4954 */
4955 CPUState *cs = env_cpu(env);
4956 int mask = vae2_tlbmask(env);
4957 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4958 int bits = vae2_tlbbits(env, pageaddr);
4959
4960 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4961 }
4962
4963 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4964 uint64_t value)
4965 {
4966 /*
4967 * Invalidate by VA, EL3
4968 * Currently handles both VAE3 and VALE3, since we don't support
4969 * flush-last-level-only.
4970 */
4971 ARMCPU *cpu = env_archcpu(env);
4972 CPUState *cs = CPU(cpu);
4973 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4974
4975 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
4976 }
4977
4978 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4979 uint64_t value)
4980 {
4981 CPUState *cs = env_cpu(env);
4982 int mask = vae1_tlbmask(env);
4983 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4984 int bits = vae1_tlbbits(env, pageaddr);
4985
4986 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4987 }
4988
4989 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4990 uint64_t value)
4991 {
4992 /*
4993 * Invalidate by VA, EL1&0 (AArch64 version).
4994 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4995 * since we don't support flush-for-specific-ASID-only or
4996 * flush-last-level-only.
4997 */
4998 CPUState *cs = env_cpu(env);
4999 int mask = vae1_tlbmask(env);
5000 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5001 int bits = vae1_tlbbits(env, pageaddr);
5002
5003 if (tlb_force_broadcast(env)) {
5004 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
5005 } else {
5006 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
5007 }
5008 }
5009
5010 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5011 uint64_t value)
5012 {
5013 CPUState *cs = env_cpu(env);
5014 int mask = vae2_tlbmask(env);
5015 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5016 int bits = vae2_tlbbits(env, pageaddr);
5017
5018 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
5019 }
5020
5021 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5022 uint64_t value)
5023 {
5024 CPUState *cs = env_cpu(env);
5025 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5026 int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
5027
5028 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
5029 ARMMMUIdxBit_E3, bits);
5030 }
5031
5032 static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
5033 {
5034 /*
5035 * The MSB of value is the NS field, which only applies if SEL2
5036 * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
5037 */
5038 return (value >= 0
5039 && cpu_isar_feature(aa64_sel2, env_archcpu(env))
5040 && arm_is_secure_below_el3(env)
5041 ? ARMMMUIdxBit_Stage2_S
5042 : ARMMMUIdxBit_Stage2);
5043 }
5044
5045 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5046 uint64_t value)
5047 {
5048 CPUState *cs = env_cpu(env);
5049 int mask = ipas2e1_tlbmask(env, value);
5050 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5051
5052 if (tlb_force_broadcast(env)) {
5053 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
5054 } else {
5055 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
5056 }
5057 }
5058
5059 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5060 uint64_t value)
5061 {
5062 CPUState *cs = env_cpu(env);
5063 int mask = ipas2e1_tlbmask(env, value);
5064 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5065
5066 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
5067 }
5068
5069 #ifdef TARGET_AARCH64
5070 typedef struct {
5071 uint64_t base;
5072 uint64_t length;
5073 } TLBIRange;
5074
5075 static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
5076 {
5077 /*
5078 * Note that the TLBI range TG field encoding differs from both
5079 * TG0 and TG1 encodings.
5080 */
5081 switch (tg) {
5082 case 1:
5083 return Gran4K;
5084 case 2:
5085 return Gran16K;
5086 case 3:
5087 return Gran64K;
5088 default:
5089 return GranInvalid;
5090 }
5091 }
5092
5093 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
5094 uint64_t value)
5095 {
5096 unsigned int page_size_granule, page_shift, num, scale, exponent;
5097 /* Extract one bit to represent the va selector in use. */
5098 uint64_t select = sextract64(value, 36, 1);
5099 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true, false);
5100 TLBIRange ret = { };
5101 ARMGranuleSize gran;
5102
5103 page_size_granule = extract64(value, 46, 2);
5104 gran = tlbi_range_tg_to_gran_size(page_size_granule);
5105
5106 /* The granule encoded in value must match the granule in use. */
5107 if (gran != param.gran) {
5108 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
5109 page_size_granule);
5110 return ret;
5111 }
5112
5113 page_shift = arm_granule_bits(gran);
5114 num = extract64(value, 39, 5);
5115 scale = extract64(value, 44, 2);
5116 exponent = (5 * scale) + 1;
5117
5118 ret.length = (num + 1) << (exponent + page_shift);
5119
5120 if (param.select) {
5121 ret.base = sextract64(value, 0, 37);
5122 } else {
5123 ret.base = extract64(value, 0, 37);
5124 }
5125 if (param.ds) {
5126 /*
5127 * With DS=1, BaseADDR is always shifted 16 so that it is able
5128 * to address all 52 va bits. The input address is perforce
5129 * aligned on a 64k boundary regardless of translation granule.
5130 */
5131 page_shift = 16;
5132 }
5133 ret.base <<= page_shift;
5134
5135 return ret;
5136 }
5137
5138 static void do_rvae_write(CPUARMState *env, uint64_t value,
5139 int idxmap, bool synced)
5140 {
5141 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
5142 TLBIRange range;
5143 int bits;
5144
5145 range = tlbi_aa64_get_range(env, one_idx, value);
5146 bits = tlbbits_for_regime(env, one_idx, range.base);
5147
5148 if (synced) {
5149 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
5150 range.base,
5151 range.length,
5152 idxmap,
5153 bits);
5154 } else {
5155 tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
5156 range.length, idxmap, bits);
5157 }
5158 }
5159
5160 static void tlbi_aa64_rvae1_write(CPUARMState *env,
5161 const ARMCPRegInfo *ri,
5162 uint64_t value)
5163 {
5164 /*
5165 * Invalidate by VA range, EL1&0.
5166 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
5167 * since we don't support flush-for-specific-ASID-only or
5168 * flush-last-level-only.
5169 */
5170
5171 do_rvae_write(env, value, vae1_tlbmask(env),
5172 tlb_force_broadcast(env));
5173 }
5174
5175 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
5176 const ARMCPRegInfo *ri,
5177 uint64_t value)
5178 {
5179 /*
5180 * Invalidate by VA range, Inner/Outer Shareable EL1&0.
5181 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
5182 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
5183 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
5184 * shareable specific flushes.
5185 */
5186
5187 do_rvae_write(env, value, vae1_tlbmask(env), true);
5188 }
5189
5190 static void tlbi_aa64_rvae2_write(CPUARMState *env,
5191 const ARMCPRegInfo *ri,
5192 uint64_t value)
5193 {
5194 /*
5195 * Invalidate by VA range, EL2.
5196 * Currently handles all of RVAE2 and RVALE2,
5197 * since we don't support flush-for-specific-ASID-only or
5198 * flush-last-level-only.
5199 */
5200
5201 do_rvae_write(env, value, vae2_tlbmask(env),
5202 tlb_force_broadcast(env));
5203
5204
5205 }
5206
5207 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
5208 const ARMCPRegInfo *ri,
5209 uint64_t value)
5210 {
5211 /*
5212 * Invalidate by VA range, Inner/Outer Shareable, EL2.
5213 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
5214 * since we don't support flush-for-specific-ASID-only,
5215 * flush-last-level-only or inner/outer shareable specific flushes.
5216 */
5217
5218 do_rvae_write(env, value, vae2_tlbmask(env), true);
5219
5220 }
5221
5222 static void tlbi_aa64_rvae3_write(CPUARMState *env,
5223 const ARMCPRegInfo *ri,
5224 uint64_t value)
5225 {
5226 /*
5227 * Invalidate by VA range, EL3.
5228 * Currently handles all of RVAE3 and RVALE3,
5229 * since we don't support flush-for-specific-ASID-only or
5230 * flush-last-level-only.
5231 */
5232
5233 do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
5234 }
5235
5236 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5237 const ARMCPRegInfo *ri,
5238 uint64_t value)
5239 {
5240 /*
5241 * Invalidate by VA range, EL3, Inner/Outer Shareable.
5242 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5243 * since we don't support flush-for-specific-ASID-only,
5244 * flush-last-level-only or inner/outer specific flushes.
5245 */
5246
5247 do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
5248 }
5249
5250 static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5251 uint64_t value)
5252 {
5253 do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5254 tlb_force_broadcast(env));
5255 }
5256
5257 static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5258 const ARMCPRegInfo *ri,
5259 uint64_t value)
5260 {
5261 do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5262 }
5263 #endif
5264
5265 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5266 bool isread)
5267 {
5268 int cur_el = arm_current_el(env);
5269
5270 if (cur_el < 2) {
5271 uint64_t hcr = arm_hcr_el2_eff(env);
5272
5273 if (cur_el == 0) {
5274 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5275 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5276 return CP_ACCESS_TRAP_EL2;
5277 }
5278 } else {
5279 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5280 return CP_ACCESS_TRAP;
5281 }
5282 if (hcr & HCR_TDZ) {
5283 return CP_ACCESS_TRAP_EL2;
5284 }
5285 }
5286 } else if (hcr & HCR_TDZ) {
5287 return CP_ACCESS_TRAP_EL2;
5288 }
5289 }
5290 return CP_ACCESS_OK;
5291 }
5292
5293 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5294 {
5295 ARMCPU *cpu = env_archcpu(env);
5296 int dzp_bit = 1 << 4;
5297
5298 /* DZP indicates whether DC ZVA access is allowed */
5299 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
5300 dzp_bit = 0;
5301 }
5302 return cpu->dcz_blocksize | dzp_bit;
5303 }
5304
5305 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5306 bool isread)
5307 {
5308 if (!(env->pstate & PSTATE_SP)) {
5309 /*
5310 * Access to SP_EL0 is undefined if it's being used as
5311 * the stack pointer.
5312 */
5313 return CP_ACCESS_TRAP_UNCATEGORIZED;
5314 }
5315 return CP_ACCESS_OK;
5316 }
5317
5318 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5319 {
5320 return env->pstate & PSTATE_SP;
5321 }
5322
5323 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5324 {
5325 update_spsel(env, val);
5326 }
5327
5328 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5329 uint64_t value)
5330 {
5331 ARMCPU *cpu = env_archcpu(env);
5332
5333 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5334 /* M bit is RAZ/WI for PMSA with no MPU implemented */
5335 value &= ~SCTLR_M;
5336 }
5337
5338 /* ??? Lots of these bits are not implemented. */
5339
5340 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5341 if (ri->opc1 == 6) { /* SCTLR_EL3 */
5342 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5343 } else {
5344 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5345 SCTLR_ATA0 | SCTLR_ATA);
5346 }
5347 }
5348
5349 if (raw_read(env, ri) == value) {
5350 /*
5351 * Skip the TLB flush if nothing actually changed; Linux likes
5352 * to do a lot of pointless SCTLR writes.
5353 */
5354 return;
5355 }
5356
5357 raw_write(env, ri, value);
5358
5359 /* This may enable/disable the MMU, so do a TLB flush. */
5360 tlb_flush(CPU(cpu));
5361
5362 if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
5363 /*
5364 * Normally we would always end the TB on an SCTLR write; see the
5365 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5366 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5367 * of hflags from the translator, so do it here.
5368 */
5369 arm_rebuild_hflags(env);
5370 }
5371 }
5372
5373 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5374 uint64_t value)
5375 {
5376 /*
5377 * Some MDCR_EL3 bits affect whether PMU counters are running:
5378 * if we are trying to change any of those then we must
5379 * bracket this update with PMU start/finish calls.
5380 */
5381 bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5382
5383 if (pmu_op) {
5384 pmu_op_start(env);
5385 }
5386 env->cp15.mdcr_el3 = value;
5387 if (pmu_op) {
5388 pmu_op_finish(env);
5389 }
5390 }
5391
5392 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5393 uint64_t value)
5394 {
5395 /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5396 mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5397 }
5398
5399 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5400 uint64_t value)
5401 {
5402 /*
5403 * Some MDCR_EL2 bits affect whether PMU counters are running:
5404 * if we are trying to change any of those then we must
5405 * bracket this update with PMU start/finish calls.
5406 */
5407 bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5408
5409 if (pmu_op) {
5410 pmu_op_start(env);
5411 }
5412 env->cp15.mdcr_el2 = value;
5413 if (pmu_op) {
5414 pmu_op_finish(env);
5415 }
5416 }
5417
5418 static CPAccessResult access_nv1(CPUARMState *env, const ARMCPRegInfo *ri,
5419 bool isread)
5420 {
5421 if (arm_current_el(env) == 1) {
5422 uint64_t hcr_nv = arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1 | HCR_NV2);
5423
5424 if (hcr_nv == (HCR_NV | HCR_NV1)) {
5425 return CP_ACCESS_TRAP_EL2;
5426 }
5427 }
5428 return CP_ACCESS_OK;
5429 }
5430
5431 #ifdef CONFIG_USER_ONLY
5432 /*
5433 * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
5434 * code to get around W^X restrictions, where one region is writable and the
5435 * other is executable.
5436 *
5437 * Since the executable region is never written to we cannot detect code
5438 * changes when running in user mode, and rely on the emulated JIT telling us
5439 * that the code has changed by executing this instruction.
5440 */
5441 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
5442 uint64_t value)
5443 {
5444 uint64_t icache_line_mask, start_address, end_address;
5445 const ARMCPU *cpu;
5446
5447 cpu = env_archcpu(env);
5448
5449 icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
5450 start_address = value & ~icache_line_mask;
5451 end_address = value | icache_line_mask;
5452
5453 mmap_lock();
5454
5455 tb_invalidate_phys_range(start_address, end_address);
5456
5457 mmap_unlock();
5458 }
5459 #endif
5460
5461 static const ARMCPRegInfo v8_cp_reginfo[] = {
5462 /*
5463 * Minimal set of EL0-visible registers. This will need to be expanded
5464 * significantly for system emulation of AArch64 CPUs.
5465 */
5466 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5467 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5468 .access = PL0_RW, .type = ARM_CP_NZCV },
5469 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5470 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5471 .type = ARM_CP_NO_RAW,
5472 .access = PL0_RW, .accessfn = aa64_daif_access,
5473 .fieldoffset = offsetof(CPUARMState, daif),
5474 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5475 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5476 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5477 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5478 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5479 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5480 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5481 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5482 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5483 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5484 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5485 .access = PL0_R, .type = ARM_CP_NO_RAW,
5486 .fgt = FGT_DCZID_EL0,
5487 .readfn = aa64_dczid_read },
5488 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5489 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5490 .access = PL0_W, .type = ARM_CP_DC_ZVA,
5491 #ifndef CONFIG_USER_ONLY
5492 /* Avoid overhead of an access check that always passes in user-mode */
5493 .accessfn = aa64_zva_access,
5494 .fgt = FGT_DCZVA,
5495 #endif
5496 },
5497 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5498 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5499 .access = PL1_R, .type = ARM_CP_CURRENTEL },
5500 /*
5501 * Instruction cache ops. All of these except `IC IVAU` NOP because we
5502 * don't emulate caches.
5503 */
5504 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5505 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5506 .access = PL1_W, .type = ARM_CP_NOP,
5507 .fgt = FGT_ICIALLUIS,
5508 .accessfn = access_ticab },
5509 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5510 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5511 .access = PL1_W, .type = ARM_CP_NOP,
5512 .fgt = FGT_ICIALLU,
5513 .accessfn = access_tocu },
5514 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5515 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5516 .access = PL0_W,
5517 .fgt = FGT_ICIVAU,
5518 .accessfn = access_tocu,
5519 #ifdef CONFIG_USER_ONLY
5520 .type = ARM_CP_NO_RAW,
5521 .writefn = ic_ivau_write
5522 #else
5523 .type = ARM_CP_NOP
5524 #endif
5525 },
5526 /* Cache ops: all NOPs since we don't emulate caches */
5527 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5528 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5529 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5530 .fgt = FGT_DCIVAC,
5531 .type = ARM_CP_NOP },
5532 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5533 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5534 .fgt = FGT_DCISW,
5535 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5536 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5537 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5538 .access = PL0_W, .type = ARM_CP_NOP,
5539 .fgt = FGT_DCCVAC,
5540 .accessfn = aa64_cacheop_poc_access },
5541 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5542 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5543 .fgt = FGT_DCCSW,
5544 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5545 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5546 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5547 .access = PL0_W, .type = ARM_CP_NOP,
5548 .fgt = FGT_DCCVAU,
5549 .accessfn = access_tocu },
5550 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5551 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5552 .access = PL0_W, .type = ARM_CP_NOP,
5553 .fgt = FGT_DCCIVAC,
5554 .accessfn = aa64_cacheop_poc_access },
5555 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5556 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5557 .fgt = FGT_DCCISW,
5558 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5559 /* TLBI operations */
5560 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
5561 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
5562 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5563 .fgt = FGT_TLBIVMALLE1IS,
5564 .writefn = tlbi_aa64_vmalle1is_write },
5565 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
5566 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
5567 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5568 .fgt = FGT_TLBIVAE1IS,
5569 .writefn = tlbi_aa64_vae1is_write },
5570 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
5571 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
5572 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5573 .fgt = FGT_TLBIASIDE1IS,
5574 .writefn = tlbi_aa64_vmalle1is_write },
5575 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
5576 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
5577 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5578 .fgt = FGT_TLBIVAAE1IS,
5579 .writefn = tlbi_aa64_vae1is_write },
5580 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
5581 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5582 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5583 .fgt = FGT_TLBIVALE1IS,
5584 .writefn = tlbi_aa64_vae1is_write },
5585 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
5586 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5587 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5588 .fgt = FGT_TLBIVAALE1IS,
5589 .writefn = tlbi_aa64_vae1is_write },
5590 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
5591 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
5592 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5593 .fgt = FGT_TLBIVMALLE1,
5594 .writefn = tlbi_aa64_vmalle1_write },
5595 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
5596 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
5597 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5598 .fgt = FGT_TLBIVAE1,
5599 .writefn = tlbi_aa64_vae1_write },
5600 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
5601 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
5602 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5603 .fgt = FGT_TLBIASIDE1,
5604 .writefn = tlbi_aa64_vmalle1_write },
5605 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
5606 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
5607 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5608 .fgt = FGT_TLBIVAAE1,
5609 .writefn = tlbi_aa64_vae1_write },
5610 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
5611 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5612 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5613 .fgt = FGT_TLBIVALE1,
5614 .writefn = tlbi_aa64_vae1_write },
5615 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
5616 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5617 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5618 .fgt = FGT_TLBIVAALE1,
5619 .writefn = tlbi_aa64_vae1_write },
5620 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5621 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5622 .access = PL2_W, .type = ARM_CP_NO_RAW,
5623 .writefn = tlbi_aa64_ipas2e1is_write },
5624 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5625 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5626 .access = PL2_W, .type = ARM_CP_NO_RAW,
5627 .writefn = tlbi_aa64_ipas2e1is_write },
5628 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5629 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5630 .access = PL2_W, .type = ARM_CP_NO_RAW,
5631 .writefn = tlbi_aa64_alle1is_write },
5632 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5633 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5634 .access = PL2_W, .type = ARM_CP_NO_RAW,
5635 .writefn = tlbi_aa64_alle1is_write },
5636 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5637 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5638 .access = PL2_W, .type = ARM_CP_NO_RAW,
5639 .writefn = tlbi_aa64_ipas2e1_write },
5640 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5641 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5642 .access = PL2_W, .type = ARM_CP_NO_RAW,
5643 .writefn = tlbi_aa64_ipas2e1_write },
5644 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5645 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5646 .access = PL2_W, .type = ARM_CP_NO_RAW,
5647 .writefn = tlbi_aa64_alle1_write },
5648 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5649 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5650 .access = PL2_W, .type = ARM_CP_NO_RAW,
5651 .writefn = tlbi_aa64_alle1is_write },
5652 #ifndef CONFIG_USER_ONLY
5653 /* 64 bit address translation operations */
5654 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5655 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5656 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5657 .fgt = FGT_ATS1E1R,
5658 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5659 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5660 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5661 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5662 .fgt = FGT_ATS1E1W,
5663 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5664 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5665 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5666 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5667 .fgt = FGT_ATS1E0R,
5668 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5669 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5670 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5671 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5672 .fgt = FGT_ATS1E0W,
5673 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5674 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5675 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5676 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5677 .accessfn = at_e012_access, .writefn = ats_write64 },
5678 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5679 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5680 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5681 .accessfn = at_e012_access, .writefn = ats_write64 },
5682 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5683 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5684 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5685 .accessfn = at_e012_access, .writefn = ats_write64 },
5686 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5687 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5688 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5689 .accessfn = at_e012_access, .writefn = ats_write64 },
5690 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5691 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5692 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5693 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5694 .writefn = ats_write64 },
5695 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5696 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5697 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5698 .writefn = ats_write64 },
5699 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5700 .type = ARM_CP_ALIAS,
5701 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5702 .access = PL1_RW, .resetvalue = 0,
5703 .fgt = FGT_PAR_EL1,
5704 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5705 .writefn = par_write },
5706 #endif
5707 /* TLB invalidate last level of translation table walk */
5708 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5709 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5710 .writefn = tlbimva_is_write },
5711 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5712 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5713 .writefn = tlbimvaa_is_write },
5714 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5715 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5716 .writefn = tlbimva_write },
5717 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5718 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5719 .writefn = tlbimvaa_write },
5720 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5721 .type = ARM_CP_NO_RAW, .access = PL2_W,
5722 .writefn = tlbimva_hyp_write },
5723 { .name = "TLBIMVALHIS",
5724 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5725 .type = ARM_CP_NO_RAW, .access = PL2_W,
5726 .writefn = tlbimva_hyp_is_write },
5727 { .name = "TLBIIPAS2",
5728 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5729 .type = ARM_CP_NO_RAW, .access = PL2_W,
5730 .writefn = tlbiipas2_hyp_write },
5731 { .name = "TLBIIPAS2IS",
5732 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5733 .type = ARM_CP_NO_RAW, .access = PL2_W,
5734 .writefn = tlbiipas2is_hyp_write },
5735 { .name = "TLBIIPAS2L",
5736 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5737 .type = ARM_CP_NO_RAW, .access = PL2_W,
5738 .writefn = tlbiipas2_hyp_write },
5739 { .name = "TLBIIPAS2LIS",
5740 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5741 .type = ARM_CP_NO_RAW, .access = PL2_W,
5742 .writefn = tlbiipas2is_hyp_write },
5743 /* 32 bit cache operations */
5744 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5745 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5746 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5747 .type = ARM_CP_NOP, .access = PL1_W },
5748 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5749 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5750 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5751 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5752 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5753 .type = ARM_CP_NOP, .access = PL1_W },
5754 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5755 .type = ARM_CP_NOP, .access = PL1_W },
5756 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5757 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5758 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5759 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5760 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5761 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5762 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5763 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5764 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5765 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5766 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5767 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5768 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5769 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5770 /* MMU Domain access control / MPU write buffer control */
5771 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5772 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5773 .writefn = dacr_write, .raw_writefn = raw_write,
5774 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5775 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5776 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5777 .type = ARM_CP_ALIAS,
5778 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5779 .access = PL1_RW, .accessfn = access_nv1,
5780 .nv2_redirect_offset = 0x230 | NV2_REDIR_NV1,
5781 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5782 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5783 .type = ARM_CP_ALIAS,
5784 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5785 .access = PL1_RW, .accessfn = access_nv1,
5786 .nv2_redirect_offset = 0x160 | NV2_REDIR_NV1,
5787 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5788 /*
5789 * We rely on the access checks not allowing the guest to write to the
5790 * state field when SPSel indicates that it's being used as the stack
5791 * pointer.
5792 */
5793 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5794 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5795 .access = PL1_RW, .accessfn = sp_el0_access,
5796 .type = ARM_CP_ALIAS,
5797 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5798 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5799 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5800 .nv2_redirect_offset = 0x240,
5801 .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5802 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5803 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5804 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5805 .type = ARM_CP_NO_RAW,
5806 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5807 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5808 .type = ARM_CP_ALIAS,
5809 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5810 .access = PL2_RW,
5811 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5812 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5813 .type = ARM_CP_ALIAS,
5814 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5815 .access = PL2_RW,
5816 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5817 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5818 .type = ARM_CP_ALIAS,
5819 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5820 .access = PL2_RW,
5821 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5822 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5823 .type = ARM_CP_ALIAS,
5824 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5825 .access = PL2_RW,
5826 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5827 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5828 .type = ARM_CP_IO,
5829 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5830 .resetvalue = 0,
5831 .access = PL3_RW,
5832 .writefn = mdcr_el3_write,
5833 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5834 { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5835 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5836 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5837 .writefn = sdcr_write,
5838 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5839 };
5840
5841 /* These are present only when EL1 supports AArch32 */
5842 static const ARMCPRegInfo v8_aa32_el1_reginfo[] = {
5843 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5844 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5845 .access = PL2_RW,
5846 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5847 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5848 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5849 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5850 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5851 .writefn = dacr_write, .raw_writefn = raw_write,
5852 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5853 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5854 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5855 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5856 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5857 };
5858
5859 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5860 {
5861 ARMCPU *cpu = env_archcpu(env);
5862
5863 if (arm_feature(env, ARM_FEATURE_V8)) {
5864 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5865 } else {
5866 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5867 }
5868
5869 if (arm_feature(env, ARM_FEATURE_EL3)) {
5870 valid_mask &= ~HCR_HCD;
5871 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5872 /*
5873 * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5874 * However, if we're using the SMC PSCI conduit then QEMU is
5875 * effectively acting like EL3 firmware and so the guest at
5876 * EL2 should retain the ability to prevent EL1 from being
5877 * able to make SMC calls into the ersatz firmware, so in
5878 * that case HCR.TSC should be read/write.
5879 */
5880 valid_mask &= ~HCR_TSC;
5881 }
5882
5883 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5884 if (cpu_isar_feature(aa64_vh, cpu)) {
5885 valid_mask |= HCR_E2H;
5886 }
5887 if (cpu_isar_feature(aa64_ras, cpu)) {
5888 valid_mask |= HCR_TERR | HCR_TEA;
5889 }
5890 if (cpu_isar_feature(aa64_lor, cpu)) {
5891 valid_mask |= HCR_TLOR;
5892 }
5893 if (cpu_isar_feature(aa64_pauth, cpu)) {
5894 valid_mask |= HCR_API | HCR_APK;
5895 }
5896 if (cpu_isar_feature(aa64_mte, cpu)) {
5897 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5898 }
5899 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5900 valid_mask |= HCR_ENSCXT;
5901 }
5902 if (cpu_isar_feature(aa64_fwb, cpu)) {
5903 valid_mask |= HCR_FWB;
5904 }
5905 if (cpu_isar_feature(aa64_rme, cpu)) {
5906 valid_mask |= HCR_GPF;
5907 }
5908 if (cpu_isar_feature(aa64_nv, cpu)) {
5909 valid_mask |= HCR_NV | HCR_NV1 | HCR_AT;
5910 }
5911 if (cpu_isar_feature(aa64_nv2, cpu)) {
5912 valid_mask |= HCR_NV2;
5913 }
5914 }
5915
5916 if (cpu_isar_feature(any_evt, cpu)) {
5917 valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5918 } else if (cpu_isar_feature(any_half_evt, cpu)) {
5919 valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5920 }
5921
5922 /* Clear RES0 bits. */
5923 value &= valid_mask;
5924
5925 /*
5926 * These bits change the MMU setup:
5927 * HCR_VM enables stage 2 translation
5928 * HCR_PTW forbids certain page-table setups
5929 * HCR_DC disables stage1 and enables stage2 translation
5930 * HCR_DCT enables tagging on (disabled) stage1 translation
5931 * HCR_FWB changes the interpretation of stage2 descriptor bits
5932 * HCR_NV and HCR_NV1 affect interpretation of descriptor bits
5933 */
5934 if ((env->cp15.hcr_el2 ^ value) &
5935 (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) {
5936 tlb_flush(CPU(cpu));
5937 }
5938 env->cp15.hcr_el2 = value;
5939
5940 /*
5941 * Updates to VI and VF require us to update the status of
5942 * virtual interrupts, which are the logical OR of these bits
5943 * and the state of the input lines from the GIC. (This requires
5944 * that we have the BQL, which is done by marking the
5945 * reginfo structs as ARM_CP_IO.)
5946 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5947 * possible for it to be taken immediately, because VIRQ and
5948 * VFIQ are masked unless running at EL0 or EL1, and HCR
5949 * can only be written at EL2.
5950 */
5951 g_assert(bql_locked());
5952 arm_cpu_update_virq(cpu);
5953 arm_cpu_update_vfiq(cpu);
5954 arm_cpu_update_vserr(cpu);
5955 }
5956
5957 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5958 {
5959 do_hcr_write(env, value, 0);
5960 }
5961
5962 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5963 uint64_t value)
5964 {
5965 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5966 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5967 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5968 }
5969
5970 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5971 uint64_t value)
5972 {
5973 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5974 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5975 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5976 }
5977
5978 /*
5979 * Return the effective value of HCR_EL2, at the given security state.
5980 * Bits that are not included here:
5981 * RW (read from SCR_EL3.RW as needed)
5982 */
5983 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
5984 {
5985 uint64_t ret = env->cp15.hcr_el2;
5986
5987 assert(space != ARMSS_Root);
5988
5989 if (!arm_is_el2_enabled_secstate(env, space)) {
5990 /*
5991 * "This register has no effect if EL2 is not enabled in the
5992 * current Security state". This is ARMv8.4-SecEL2 speak for
5993 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5994 *
5995 * Prior to that, the language was "In an implementation that
5996 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5997 * as if this field is 0 for all purposes other than a direct
5998 * read or write access of HCR_EL2". With lots of enumeration
5999 * on a per-field basis. In current QEMU, this is condition
6000 * is arm_is_secure_below_el3.
6001 *
6002 * Since the v8.4 language applies to the entire register, and
6003 * appears to be backward compatible, use that.
6004 */
6005 return 0;
6006 }
6007
6008 /*
6009 * For a cpu that supports both aarch64 and aarch32, we can set bits
6010 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
6011 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
6012 */
6013 if (!arm_el_is_aa64(env, 2)) {
6014 uint64_t aa32_valid;
6015
6016 /*
6017 * These bits are up-to-date as of ARMv8.6.
6018 * For HCR, it's easiest to list just the 2 bits that are invalid.
6019 * For HCR2, list those that are valid.
6020 */
6021 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
6022 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
6023 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
6024 ret &= aa32_valid;
6025 }
6026
6027 if (ret & HCR_TGE) {
6028 /* These bits are up-to-date as of ARMv8.6. */
6029 if (ret & HCR_E2H) {
6030 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
6031 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
6032 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
6033 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
6034 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
6035 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
6036 } else {
6037 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
6038 }
6039 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
6040 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
6041 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
6042 HCR_TLOR);
6043 }
6044
6045 return ret;
6046 }
6047
6048 uint64_t arm_hcr_el2_eff(CPUARMState *env)
6049 {
6050 if (arm_feature(env, ARM_FEATURE_M)) {
6051 return 0;
6052 }
6053 return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
6054 }
6055
6056 /*
6057 * Corresponds to ARM pseudocode function ELIsInHost().
6058 */
6059 bool el_is_in_host(CPUARMState *env, int el)
6060 {
6061 uint64_t mask;
6062
6063 /*
6064 * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
6065 * Perform the simplest bit tests first, and validate EL2 afterward.
6066 */
6067 if (el & 1) {
6068 return false; /* EL1 or EL3 */
6069 }
6070
6071 /*
6072 * Note that hcr_write() checks isar_feature_aa64_vh(),
6073 * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
6074 */
6075 mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
6076 if ((env->cp15.hcr_el2 & mask) != mask) {
6077 return false;
6078 }
6079
6080 /* TGE and/or E2H set: double check those bits are currently legal. */
6081 return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
6082 }
6083
6084 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
6085 uint64_t value)
6086 {
6087 uint64_t valid_mask = 0;
6088
6089 /* FEAT_MOPS adds MSCEn and MCE2 */
6090 if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
6091 valid_mask |= HCRX_MSCEN | HCRX_MCE2;
6092 }
6093
6094 /* Clear RES0 bits. */
6095 env->cp15.hcrx_el2 = value & valid_mask;
6096 }
6097
6098 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
6099 bool isread)
6100 {
6101 if (arm_current_el(env) == 2
6102 && arm_feature(env, ARM_FEATURE_EL3)
6103 && !(env->cp15.scr_el3 & SCR_HXEN)) {
6104 return CP_ACCESS_TRAP_EL3;
6105 }
6106 return CP_ACCESS_OK;
6107 }
6108
6109 static const ARMCPRegInfo hcrx_el2_reginfo = {
6110 .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
6111 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
6112 .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
6113 .nv2_redirect_offset = 0xa0,
6114 .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
6115 };
6116
6117 /* Return the effective value of HCRX_EL2. */
6118 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
6119 {
6120 /*
6121 * The bits in this register behave as 0 for all purposes other than
6122 * direct reads of the register if SCR_EL3.HXEn is 0.
6123 * If EL2 is not enabled in the current security state, then the
6124 * bit may behave as if 0, or as if 1, depending on the bit.
6125 * For the moment, we treat the EL2-disabled case as taking
6126 * priority over the HXEn-disabled case. This is true for the only
6127 * bit for a feature which we implement where the answer is different
6128 * for the two cases (MSCEn for FEAT_MOPS).
6129 * This may need to be revisited for future bits.
6130 */
6131 if (!arm_is_el2_enabled(env)) {
6132 uint64_t hcrx = 0;
6133 if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
6134 /* MSCEn behaves as 1 if EL2 is not enabled */
6135 hcrx |= HCRX_MSCEN;
6136 }
6137 return hcrx;
6138 }
6139 if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
6140 return 0;
6141 }
6142 return env->cp15.hcrx_el2;
6143 }
6144
6145 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
6146 uint64_t value)
6147 {
6148 /*
6149 * For A-profile AArch32 EL3, if NSACR.CP10
6150 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6151 */
6152 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6153 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6154 uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6155 value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
6156 }
6157 env->cp15.cptr_el[2] = value;
6158 }
6159
6160 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
6161 {
6162 /*
6163 * For A-profile AArch32 EL3, if NSACR.CP10
6164 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6165 */
6166 uint64_t value = env->cp15.cptr_el[2];
6167
6168 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6169 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6170 value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6171 }
6172 return value;
6173 }
6174
6175 static const ARMCPRegInfo el2_cp_reginfo[] = {
6176 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
6177 .type = ARM_CP_IO,
6178 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6179 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6180 .nv2_redirect_offset = 0x78,
6181 .writefn = hcr_write, .raw_writefn = raw_write },
6182 { .name = "HCR", .state = ARM_CP_STATE_AA32,
6183 .type = ARM_CP_ALIAS | ARM_CP_IO,
6184 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6185 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6186 .writefn = hcr_writelow },
6187 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
6188 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
6189 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6190 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
6191 .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
6192 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
6193 .access = PL2_RW,
6194 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
6195 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
6196 .type = ARM_CP_NV2_REDIRECT,
6197 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
6198 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
6199 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
6200 .type = ARM_CP_NV2_REDIRECT,
6201 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
6202 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
6203 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
6204 .type = ARM_CP_ALIAS,
6205 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
6206 .access = PL2_RW,
6207 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
6208 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
6209 .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
6210 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
6211 .access = PL2_RW,
6212 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
6213 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
6214 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
6215 .access = PL2_RW, .writefn = vbar_write,
6216 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
6217 .resetvalue = 0 },
6218 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
6219 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
6220 .access = PL3_RW, .type = ARM_CP_ALIAS,
6221 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
6222 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
6223 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
6224 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
6225 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
6226 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
6227 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
6228 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
6229 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
6230 .resetvalue = 0 },
6231 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
6232 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
6233 .access = PL2_RW, .type = ARM_CP_ALIAS,
6234 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
6235 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
6236 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
6237 .access = PL2_RW, .type = ARM_CP_CONST,
6238 .resetvalue = 0 },
6239 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
6240 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
6241 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
6242 .access = PL2_RW, .type = ARM_CP_CONST,
6243 .resetvalue = 0 },
6244 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
6245 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
6246 .access = PL2_RW, .type = ARM_CP_CONST,
6247 .resetvalue = 0 },
6248 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
6249 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
6250 .access = PL2_RW, .type = ARM_CP_CONST,
6251 .resetvalue = 0 },
6252 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
6253 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6254 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
6255 .raw_writefn = raw_write,
6256 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
6257 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
6258 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6259 .type = ARM_CP_ALIAS,
6260 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6261 .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
6262 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
6263 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6264 .access = PL2_RW,
6265 .nv2_redirect_offset = 0x40,
6266 /* no .writefn needed as this can't cause an ASID change */
6267 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
6268 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
6269 .cp = 15, .opc1 = 6, .crm = 2,
6270 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6271 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6272 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
6273 .writefn = vttbr_write, .raw_writefn = raw_write },
6274 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
6275 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
6276 .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
6277 .nv2_redirect_offset = 0x20,
6278 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
6279 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
6280 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
6281 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
6282 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
6283 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6284 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
6285 .access = PL2_RW, .resetvalue = 0,
6286 .nv2_redirect_offset = 0x90,
6287 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
6288 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
6289 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
6290 .access = PL2_RW, .resetvalue = 0,
6291 .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
6292 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6293 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
6294 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6295 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6296 { .name = "TLBIALLNSNH",
6297 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
6298 .type = ARM_CP_NO_RAW, .access = PL2_W,
6299 .writefn = tlbiall_nsnh_write },
6300 { .name = "TLBIALLNSNHIS",
6301 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
6302 .type = ARM_CP_NO_RAW, .access = PL2_W,
6303 .writefn = tlbiall_nsnh_is_write },
6304 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6305 .type = ARM_CP_NO_RAW, .access = PL2_W,
6306 .writefn = tlbiall_hyp_write },
6307 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6308 .type = ARM_CP_NO_RAW, .access = PL2_W,
6309 .writefn = tlbiall_hyp_is_write },
6310 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6311 .type = ARM_CP_NO_RAW, .access = PL2_W,
6312 .writefn = tlbimva_hyp_write },
6313 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6314 .type = ARM_CP_NO_RAW, .access = PL2_W,
6315 .writefn = tlbimva_hyp_is_write },
6316 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
6317 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6318 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6319 .writefn = tlbi_aa64_alle2_write },
6320 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
6321 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6322 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6323 .writefn = tlbi_aa64_vae2_write },
6324 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
6325 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
6326 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6327 .writefn = tlbi_aa64_vae2_write },
6328 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
6329 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6330 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6331 .writefn = tlbi_aa64_alle2is_write },
6332 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
6333 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6334 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6335 .writefn = tlbi_aa64_vae2is_write },
6336 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
6337 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
6338 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6339 .writefn = tlbi_aa64_vae2is_write },
6340 #ifndef CONFIG_USER_ONLY
6341 /*
6342 * Unlike the other EL2-related AT operations, these must
6343 * UNDEF from EL3 if EL2 is not implemented, which is why we
6344 * define them here rather than with the rest of the AT ops.
6345 */
6346 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
6347 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6348 .access = PL2_W, .accessfn = at_s1e2_access,
6349 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6350 .writefn = ats_write64 },
6351 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
6352 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6353 .access = PL2_W, .accessfn = at_s1e2_access,
6354 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6355 .writefn = ats_write64 },
6356 /*
6357 * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
6358 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
6359 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
6360 * to behave as if SCR.NS was 1.
6361 */
6362 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6363 .access = PL2_W,
6364 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6365 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6366 .access = PL2_W,
6367 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6368 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6369 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
6370 /*
6371 * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
6372 * reset values as IMPDEF. We choose to reset to 3 to comply with
6373 * both ARMv7 and ARMv8.
6374 */
6375 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
6376 .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
6377 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
6378 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6379 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6380 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6381 .writefn = gt_cntvoff_write,
6382 .nv2_redirect_offset = 0x60,
6383 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6384 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6385 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6386 .writefn = gt_cntvoff_write,
6387 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6388 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6389 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6390 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6391 .type = ARM_CP_IO, .access = PL2_RW,
6392 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6393 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6394 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6395 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6396 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6397 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6398 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
6399 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6400 .resetfn = gt_hyp_timer_reset,
6401 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6402 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6403 .type = ARM_CP_IO,
6404 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6405 .access = PL2_RW,
6406 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6407 .resetvalue = 0,
6408 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
6409 #endif
6410 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6411 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6412 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6413 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6414 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6415 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6416 .access = PL2_RW,
6417 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6418 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6419 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6420 .access = PL2_RW,
6421 .nv2_redirect_offset = 0x80,
6422 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
6423 };
6424
6425 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6426 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
6427 .type = ARM_CP_ALIAS | ARM_CP_IO,
6428 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6429 .access = PL2_RW,
6430 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6431 .writefn = hcr_writehigh },
6432 };
6433
6434 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6435 bool isread)
6436 {
6437 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6438 return CP_ACCESS_OK;
6439 }
6440 return CP_ACCESS_TRAP_UNCATEGORIZED;
6441 }
6442
6443 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6444 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6445 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6446 .access = PL2_RW, .accessfn = sel2_access,
6447 .nv2_redirect_offset = 0x30,
6448 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6449 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6450 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6451 .access = PL2_RW, .accessfn = sel2_access,
6452 .nv2_redirect_offset = 0x48,
6453 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
6454 };
6455
6456 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6457 bool isread)
6458 {
6459 /*
6460 * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
6461 * At Secure EL1 it traps to EL3 or EL2.
6462 */
6463 if (arm_current_el(env) == 3) {
6464 return CP_ACCESS_OK;
6465 }
6466 if (arm_is_secure_below_el3(env)) {
6467 if (env->cp15.scr_el3 & SCR_EEL2) {
6468 return CP_ACCESS_TRAP_EL2;
6469 }
6470 return CP_ACCESS_TRAP_EL3;
6471 }
6472 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6473 if (isread) {
6474 return CP_ACCESS_OK;
6475 }
6476 return CP_ACCESS_TRAP_UNCATEGORIZED;
6477 }
6478
6479 static const ARMCPRegInfo el3_cp_reginfo[] = {
6480 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6481 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6482 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
6483 .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
6484 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
6485 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
6486 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6487 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
6488 .writefn = scr_write, .raw_writefn = raw_write },
6489 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6490 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6491 .access = PL3_RW, .resetvalue = 0,
6492 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6493 { .name = "SDER",
6494 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6495 .access = PL3_RW, .resetvalue = 0,
6496 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
6497 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
6498 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6499 .writefn = vbar_write, .resetvalue = 0,
6500 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
6501 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6502 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
6503 .access = PL3_RW, .resetvalue = 0,
6504 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
6505 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6506 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6507 .access = PL3_RW,
6508 /* no .writefn needed as this can't cause an ASID change */
6509 .resetvalue = 0,
6510 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
6511 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
6512 .type = ARM_CP_ALIAS,
6513 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6514 .access = PL3_RW,
6515 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
6516 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
6517 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6518 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
6519 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6520 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6521 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
6522 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
6523 .type = ARM_CP_ALIAS,
6524 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
6525 .access = PL3_RW,
6526 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
6527 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6528 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6529 .access = PL3_RW, .writefn = vbar_write,
6530 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6531 .resetvalue = 0 },
6532 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6533 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6534 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6535 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
6536 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6537 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6538 .access = PL3_RW, .resetvalue = 0,
6539 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
6540 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6541 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6542 .access = PL3_RW, .type = ARM_CP_CONST,
6543 .resetvalue = 0 },
6544 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6545 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6546 .access = PL3_RW, .type = ARM_CP_CONST,
6547 .resetvalue = 0 },
6548 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6549 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6550 .access = PL3_RW, .type = ARM_CP_CONST,
6551 .resetvalue = 0 },
6552 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6553 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6554 .access = PL3_W, .type = ARM_CP_NO_RAW,
6555 .writefn = tlbi_aa64_alle3is_write },
6556 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6557 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6558 .access = PL3_W, .type = ARM_CP_NO_RAW,
6559 .writefn = tlbi_aa64_vae3is_write },
6560 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6561 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6562 .access = PL3_W, .type = ARM_CP_NO_RAW,
6563 .writefn = tlbi_aa64_vae3is_write },
6564 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6565 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6566 .access = PL3_W, .type = ARM_CP_NO_RAW,
6567 .writefn = tlbi_aa64_alle3_write },
6568 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6569 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6570 .access = PL3_W, .type = ARM_CP_NO_RAW,
6571 .writefn = tlbi_aa64_vae3_write },
6572 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6573 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6574 .access = PL3_W, .type = ARM_CP_NO_RAW,
6575 .writefn = tlbi_aa64_vae3_write },
6576 };
6577
6578 #ifndef CONFIG_USER_ONLY
6579
6580 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
6581 bool isread)
6582 {
6583 if (arm_current_el(env) == 1) {
6584 /* This must be a FEAT_NV access */
6585 return CP_ACCESS_OK;
6586 }
6587 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6588 return CP_ACCESS_TRAP_UNCATEGORIZED;
6589 }
6590 return CP_ACCESS_OK;
6591 }
6592
6593 static CPAccessResult access_el1nvpct(CPUARMState *env, const ARMCPRegInfo *ri,
6594 bool isread)
6595 {
6596 if (arm_current_el(env) == 1) {
6597 /* This must be a FEAT_NV access with NVx == 101 */
6598 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVPCT)) {
6599 return CP_ACCESS_TRAP_EL2;
6600 }
6601 }
6602 return e2h_access(env, ri, isread);
6603 }
6604
6605 static CPAccessResult access_el1nvvct(CPUARMState *env, const ARMCPRegInfo *ri,
6606 bool isread)
6607 {
6608 if (arm_current_el(env) == 1) {
6609 /* This must be a FEAT_NV access with NVx == 101 */
6610 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVVCT)) {
6611 return CP_ACCESS_TRAP_EL2;
6612 }
6613 }
6614 return e2h_access(env, ri, isread);
6615 }
6616
6617 /* Test if system register redirection is to occur in the current state. */
6618 static bool redirect_for_e2h(CPUARMState *env)
6619 {
6620 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6621 }
6622
6623 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6624 {
6625 CPReadFn *readfn;
6626
6627 if (redirect_for_e2h(env)) {
6628 /* Switch to the saved EL2 version of the register. */
6629 ri = ri->opaque;
6630 readfn = ri->readfn;
6631 } else {
6632 readfn = ri->orig_readfn;
6633 }
6634 if (readfn == NULL) {
6635 readfn = raw_read;
6636 }
6637 return readfn(env, ri);
6638 }
6639
6640 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6641 uint64_t value)
6642 {
6643 CPWriteFn *writefn;
6644
6645 if (redirect_for_e2h(env)) {
6646 /* Switch to the saved EL2 version of the register. */
6647 ri = ri->opaque;
6648 writefn = ri->writefn;
6649 } else {
6650 writefn = ri->orig_writefn;
6651 }
6652 if (writefn == NULL) {
6653 writefn = raw_write;
6654 }
6655 writefn(env, ri, value);
6656 }
6657
6658 static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri)
6659 {
6660 /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6661 return ri->orig_readfn(env, ri->opaque);
6662 }
6663
6664 static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri,
6665 uint64_t value)
6666 {
6667 /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6668 return ri->orig_writefn(env, ri->opaque, value);
6669 }
6670
6671 static CPAccessResult el2_e2h_e12_access(CPUARMState *env,
6672 const ARMCPRegInfo *ri,
6673 bool isread)
6674 {
6675 if (arm_current_el(env) == 1) {
6676 /*
6677 * This must be a FEAT_NV access (will either trap or redirect
6678 * to memory). None of the registers with _EL12 aliases want to
6679 * apply their trap controls for this kind of access, so don't
6680 * call the orig_accessfn or do the "UNDEF when E2H is 0" check.
6681 */
6682 return CP_ACCESS_OK;
6683 }
6684 /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */
6685 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6686 return CP_ACCESS_TRAP_UNCATEGORIZED;
6687 }
6688 if (ri->orig_accessfn) {
6689 return ri->orig_accessfn(env, ri->opaque, isread);
6690 }
6691 return CP_ACCESS_OK;
6692 }
6693
6694 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6695 {
6696 struct E2HAlias {
6697 uint32_t src_key, dst_key, new_key;
6698 const char *src_name, *dst_name, *new_name;
6699 bool (*feature)(const ARMISARegisters *id);
6700 };
6701
6702 #define K(op0, op1, crn, crm, op2) \
6703 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6704
6705 static const struct E2HAlias aliases[] = {
6706 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
6707 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6708 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
6709 "CPACR", "CPTR_EL2", "CPACR_EL12" },
6710 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
6711 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6712 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
6713 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6714 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
6715 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6716 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
6717 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6718 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
6719 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6720 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
6721 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6722 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
6723 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6724 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
6725 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6726 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
6727 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6728 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6729 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6730 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6731 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6732 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6733 "VBAR", "VBAR_EL2", "VBAR_EL12" },
6734 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6735 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6736 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6737 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6738
6739 /*
6740 * Note that redirection of ZCR is mentioned in the description
6741 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6742 * not in the summary table.
6743 */
6744 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
6745 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6746 { K(3, 0, 1, 2, 6), K(3, 4, 1, 2, 6), K(3, 5, 1, 2, 6),
6747 "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6748
6749 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
6750 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6751
6752 { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6753 "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6754 isar_feature_aa64_scxtnum },
6755
6756 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6757 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6758 };
6759 #undef K
6760
6761 size_t i;
6762
6763 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6764 const struct E2HAlias *a = &aliases[i];
6765 ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6766 bool ok;
6767
6768 if (a->feature && !a->feature(&cpu->isar)) {
6769 continue;
6770 }
6771
6772 src_reg = g_hash_table_lookup(cpu->cp_regs,
6773 (gpointer)(uintptr_t)a->src_key);
6774 dst_reg = g_hash_table_lookup(cpu->cp_regs,
6775 (gpointer)(uintptr_t)a->dst_key);
6776 g_assert(src_reg != NULL);
6777 g_assert(dst_reg != NULL);
6778
6779 /* Cross-compare names to detect typos in the keys. */
6780 g_assert(strcmp(src_reg->name, a->src_name) == 0);
6781 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6782
6783 /* None of the core system registers use opaque; we will. */
6784 g_assert(src_reg->opaque == NULL);
6785
6786 /* Create alias before redirection so we dup the right data. */
6787 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6788
6789 new_reg->name = a->new_name;
6790 new_reg->type |= ARM_CP_ALIAS;
6791 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
6792 new_reg->access &= PL2_RW | PL3_RW;
6793 /* The new_reg op fields are as per new_key, not the target reg */
6794 new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK)
6795 >> CP_REG_ARM64_SYSREG_CRN_SHIFT;
6796 new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK)
6797 >> CP_REG_ARM64_SYSREG_CRM_SHIFT;
6798 new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK)
6799 >> CP_REG_ARM64_SYSREG_OP0_SHIFT;
6800 new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK)
6801 >> CP_REG_ARM64_SYSREG_OP1_SHIFT;
6802 new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK)
6803 >> CP_REG_ARM64_SYSREG_OP2_SHIFT;
6804 new_reg->opaque = src_reg;
6805 new_reg->orig_readfn = src_reg->readfn ?: raw_read;
6806 new_reg->orig_writefn = src_reg->writefn ?: raw_write;
6807 new_reg->orig_accessfn = src_reg->accessfn;
6808 if (!new_reg->raw_readfn) {
6809 new_reg->raw_readfn = raw_read;
6810 }
6811 if (!new_reg->raw_writefn) {
6812 new_reg->raw_writefn = raw_write;
6813 }
6814 new_reg->readfn = el2_e2h_e12_read;
6815 new_reg->writefn = el2_e2h_e12_write;
6816 new_reg->accessfn = el2_e2h_e12_access;
6817
6818 /*
6819 * If the _EL1 register is redirected to memory by FEAT_NV2,
6820 * then it shares the offset with the _EL12 register,
6821 * and which one is redirected depends on HCR_EL2.NV1.
6822 */
6823 if (new_reg->nv2_redirect_offset) {
6824 assert(new_reg->nv2_redirect_offset & NV2_REDIR_NV1);
6825 new_reg->nv2_redirect_offset &= ~NV2_REDIR_NV1;
6826 new_reg->nv2_redirect_offset |= NV2_REDIR_NO_NV1;
6827 }
6828
6829 ok = g_hash_table_insert(cpu->cp_regs,
6830 (gpointer)(uintptr_t)a->new_key, new_reg);
6831 g_assert(ok);
6832
6833 src_reg->opaque = dst_reg;
6834 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6835 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6836 if (!src_reg->raw_readfn) {
6837 src_reg->raw_readfn = raw_read;
6838 }
6839 if (!src_reg->raw_writefn) {
6840 src_reg->raw_writefn = raw_write;
6841 }
6842 src_reg->readfn = el2_e2h_read;
6843 src_reg->writefn = el2_e2h_write;
6844 }
6845 }
6846 #endif
6847
6848 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6849 bool isread)
6850 {
6851 int cur_el = arm_current_el(env);
6852
6853 if (cur_el < 2) {
6854 uint64_t hcr = arm_hcr_el2_eff(env);
6855
6856 if (cur_el == 0) {
6857 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6858 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6859 return CP_ACCESS_TRAP_EL2;
6860 }
6861 } else {
6862 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6863 return CP_ACCESS_TRAP;
6864 }
6865 if (hcr & HCR_TID2) {
6866 return CP_ACCESS_TRAP_EL2;
6867 }
6868 }
6869 } else if (hcr & HCR_TID2) {
6870 return CP_ACCESS_TRAP_EL2;
6871 }
6872 }
6873
6874 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6875 return CP_ACCESS_TRAP_EL2;
6876 }
6877
6878 return CP_ACCESS_OK;
6879 }
6880
6881 /*
6882 * Check for traps to RAS registers, which are controlled
6883 * by HCR_EL2.TERR and SCR_EL3.TERR.
6884 */
6885 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6886 bool isread)
6887 {
6888 int el = arm_current_el(env);
6889
6890 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6891 return CP_ACCESS_TRAP_EL2;
6892 }
6893 if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6894 return CP_ACCESS_TRAP_EL3;
6895 }
6896 return CP_ACCESS_OK;
6897 }
6898
6899 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6900 {
6901 int el = arm_current_el(env);
6902
6903 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6904 return env->cp15.vdisr_el2;
6905 }
6906 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6907 return 0; /* RAZ/WI */
6908 }
6909 return env->cp15.disr_el1;
6910 }
6911
6912 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6913 {
6914 int el = arm_current_el(env);
6915
6916 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6917 env->cp15.vdisr_el2 = val;
6918 return;
6919 }
6920 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6921 return; /* RAZ/WI */
6922 }
6923 env->cp15.disr_el1 = val;
6924 }
6925
6926 /*
6927 * Minimal RAS implementation with no Error Records.
6928 * Which means that all of the Error Record registers:
6929 * ERXADDR_EL1
6930 * ERXCTLR_EL1
6931 * ERXFR_EL1
6932 * ERXMISC0_EL1
6933 * ERXMISC1_EL1
6934 * ERXMISC2_EL1
6935 * ERXMISC3_EL1
6936 * ERXPFGCDN_EL1 (RASv1p1)
6937 * ERXPFGCTL_EL1 (RASv1p1)
6938 * ERXPFGF_EL1 (RASv1p1)
6939 * ERXSTATUS_EL1
6940 * and
6941 * ERRSELR_EL1
6942 * may generate UNDEFINED, which is the effect we get by not
6943 * listing them at all.
6944 *
6945 * These registers have fine-grained trap bits, but UNDEF-to-EL1
6946 * is higher priority than FGT-to-EL2 so we do not need to list them
6947 * in order to check for an FGT.
6948 */
6949 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6950 { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6951 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6952 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6953 .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6954 { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6955 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6956 .access = PL1_R, .accessfn = access_terr,
6957 .fgt = FGT_ERRIDR_EL1,
6958 .type = ARM_CP_CONST, .resetvalue = 0 },
6959 { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6960 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6961 .nv2_redirect_offset = 0x500,
6962 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6963 { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6964 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6965 .nv2_redirect_offset = 0x508,
6966 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6967 };
6968
6969 /*
6970 * Return the exception level to which exceptions should be taken
6971 * via SVEAccessTrap. This excludes the check for whether the exception
6972 * should be routed through AArch64.AdvSIMDFPAccessTrap. That can easily
6973 * be found by testing 0 < fp_exception_el < sve_exception_el.
6974 *
6975 * C.f. the ARM pseudocode function CheckSVEEnabled. Note that the
6976 * pseudocode does *not* separate out the FP trap checks, but has them
6977 * all in one function.
6978 */
6979 int sve_exception_el(CPUARMState *env, int el)
6980 {
6981 #ifndef CONFIG_USER_ONLY
6982 if (el <= 1 && !el_is_in_host(env, el)) {
6983 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6984 case 1:
6985 if (el != 0) {
6986 break;
6987 }
6988 /* fall through */
6989 case 0:
6990 case 2:
6991 return 1;
6992 }
6993 }
6994
6995 if (el <= 2 && arm_is_el2_enabled(env)) {
6996 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6997 if (env->cp15.hcr_el2 & HCR_E2H) {
6998 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6999 case 1:
7000 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
7001 break;
7002 }
7003 /* fall through */
7004 case 0:
7005 case 2:
7006 return 2;
7007 }
7008 } else {
7009 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
7010 return 2;
7011 }
7012 }
7013 }
7014
7015 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
7016 if (arm_feature(env, ARM_FEATURE_EL3)
7017 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
7018 return 3;
7019 }
7020 #endif
7021 return 0;
7022 }
7023
7024 /*
7025 * Return the exception level to which exceptions should be taken for SME.
7026 * C.f. the ARM pseudocode function CheckSMEAccess.
7027 */
7028 int sme_exception_el(CPUARMState *env, int el)
7029 {
7030 #ifndef CONFIG_USER_ONLY
7031 if (el <= 1 && !el_is_in_host(env, el)) {
7032 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
7033 case 1:
7034 if (el != 0) {
7035 break;
7036 }
7037 /* fall through */
7038 case 0:
7039 case 2:
7040 return 1;
7041 }
7042 }
7043
7044 if (el <= 2 && arm_is_el2_enabled(env)) {
7045 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
7046 if (env->cp15.hcr_el2 & HCR_E2H) {
7047 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
7048 case 1:
7049 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
7050 break;
7051 }
7052 /* fall through */
7053 case 0:
7054 case 2:
7055 return 2;
7056 }
7057 } else {
7058 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
7059 return 2;
7060 }
7061 }
7062 }
7063
7064 /* CPTR_EL3. Since ESM is negative we must check for EL3. */
7065 if (arm_feature(env, ARM_FEATURE_EL3)
7066 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7067 return 3;
7068 }
7069 #endif
7070 return 0;
7071 }
7072
7073 /*
7074 * Given that SVE is enabled, return the vector length for EL.
7075 */
7076 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
7077 {
7078 ARMCPU *cpu = env_archcpu(env);
7079 uint64_t *cr = env->vfp.zcr_el;
7080 uint32_t map = cpu->sve_vq.map;
7081 uint32_t len = ARM_MAX_VQ - 1;
7082
7083 if (sm) {
7084 cr = env->vfp.smcr_el;
7085 map = cpu->sme_vq.map;
7086 }
7087
7088 if (el <= 1 && !el_is_in_host(env, el)) {
7089 len = MIN(len, 0xf & (uint32_t)cr[1]);
7090 }
7091 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
7092 len = MIN(len, 0xf & (uint32_t)cr[2]);
7093 }
7094 if (arm_feature(env, ARM_FEATURE_EL3)) {
7095 len = MIN(len, 0xf & (uint32_t)cr[3]);
7096 }
7097
7098 map &= MAKE_64BIT_MASK(0, len + 1);
7099 if (map != 0) {
7100 return 31 - clz32(map);
7101 }
7102
7103 /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
7104 assert(sm);
7105 return ctz32(cpu->sme_vq.map);
7106 }
7107
7108 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
7109 {
7110 return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
7111 }
7112
7113 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7114 uint64_t value)
7115 {
7116 int cur_el = arm_current_el(env);
7117 int old_len = sve_vqm1_for_el(env, cur_el);
7118 int new_len;
7119
7120 /* Bits other than [3:0] are RAZ/WI. */
7121 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
7122 raw_write(env, ri, value & 0xf);
7123
7124 /*
7125 * Because we arrived here, we know both FP and SVE are enabled;
7126 * otherwise we would have trapped access to the ZCR_ELn register.
7127 */
7128 new_len = sve_vqm1_for_el(env, cur_el);
7129 if (new_len < old_len) {
7130 aarch64_sve_narrow_vq(env, new_len + 1);
7131 }
7132 }
7133
7134 static const ARMCPRegInfo zcr_reginfo[] = {
7135 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
7136 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
7137 .nv2_redirect_offset = 0x1e0 | NV2_REDIR_NV1,
7138 .access = PL1_RW, .type = ARM_CP_SVE,
7139 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
7140 .writefn = zcr_write, .raw_writefn = raw_write },
7141 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
7142 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
7143 .access = PL2_RW, .type = ARM_CP_SVE,
7144 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
7145 .writefn = zcr_write, .raw_writefn = raw_write },
7146 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
7147 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
7148 .access = PL3_RW, .type = ARM_CP_SVE,
7149 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
7150 .writefn = zcr_write, .raw_writefn = raw_write },
7151 };
7152
7153 #ifdef TARGET_AARCH64
7154 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
7155 bool isread)
7156 {
7157 int el = arm_current_el(env);
7158
7159 if (el == 0) {
7160 uint64_t sctlr = arm_sctlr(env, el);
7161 if (!(sctlr & SCTLR_EnTP2)) {
7162 return CP_ACCESS_TRAP;
7163 }
7164 }
7165 /* TODO: FEAT_FGT */
7166 if (el < 3
7167 && arm_feature(env, ARM_FEATURE_EL3)
7168 && !(env->cp15.scr_el3 & SCR_ENTP2)) {
7169 return CP_ACCESS_TRAP_EL3;
7170 }
7171 return CP_ACCESS_OK;
7172 }
7173
7174 static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri,
7175 bool isread)
7176 {
7177 /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */
7178 if (arm_current_el(env) == 2
7179 && arm_feature(env, ARM_FEATURE_EL3)
7180 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7181 return CP_ACCESS_TRAP_EL3;
7182 }
7183 return CP_ACCESS_OK;
7184 }
7185
7186 static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri,
7187 bool isread)
7188 {
7189 if (arm_current_el(env) < 3
7190 && arm_feature(env, ARM_FEATURE_EL3)
7191 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7192 return CP_ACCESS_TRAP_EL3;
7193 }
7194 return CP_ACCESS_OK;
7195 }
7196
7197 /* ResetSVEState */
7198 static void arm_reset_sve_state(CPUARMState *env)
7199 {
7200 memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
7201 /* Recall that FFR is stored as pregs[16]. */
7202 memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
7203 vfp_set_fpcr(env, 0x0800009f);
7204 }
7205
7206 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
7207 {
7208 uint64_t change = (env->svcr ^ new) & mask;
7209
7210 if (change == 0) {
7211 return;
7212 }
7213 env->svcr ^= change;
7214
7215 if (change & R_SVCR_SM_MASK) {
7216 arm_reset_sve_state(env);
7217 }
7218
7219 /*
7220 * ResetSMEState.
7221 *
7222 * SetPSTATE_ZA zeros on enable and disable. We can zero this only
7223 * on enable: while disabled, the storage is inaccessible and the
7224 * value does not matter. We're not saving the storage in vmstate
7225 * when disabled either.
7226 */
7227 if (change & new & R_SVCR_ZA_MASK) {
7228 memset(env->zarray, 0, sizeof(env->zarray));
7229 }
7230
7231 if (tcg_enabled()) {
7232 arm_rebuild_hflags(env);
7233 }
7234 }
7235
7236 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7237 uint64_t value)
7238 {
7239 aarch64_set_svcr(env, value, -1);
7240 }
7241
7242 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7243 uint64_t value)
7244 {
7245 int cur_el = arm_current_el(env);
7246 int old_len = sve_vqm1_for_el(env, cur_el);
7247 int new_len;
7248
7249 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
7250 value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
7251 raw_write(env, ri, value);
7252
7253 /*
7254 * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
7255 * when SVL is widened (old values kept, or zeros). Choose to keep the
7256 * current values for simplicity. But for QEMU internals, we must still
7257 * apply the narrower SVL to the Zregs and Pregs -- see the comment
7258 * above aarch64_sve_narrow_vq.
7259 */
7260 new_len = sve_vqm1_for_el(env, cur_el);
7261 if (new_len < old_len) {
7262 aarch64_sve_narrow_vq(env, new_len + 1);
7263 }
7264 }
7265
7266 static const ARMCPRegInfo sme_reginfo[] = {
7267 { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
7268 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
7269 .access = PL0_RW, .accessfn = access_tpidr2,
7270 .fgt = FGT_NTPIDR2_EL0,
7271 .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
7272 { .name = "SVCR", .state = ARM_CP_STATE_AA64,
7273 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
7274 .access = PL0_RW, .type = ARM_CP_SME,
7275 .fieldoffset = offsetof(CPUARMState, svcr),
7276 .writefn = svcr_write, .raw_writefn = raw_write },
7277 { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
7278 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
7279 .nv2_redirect_offset = 0x1f0 | NV2_REDIR_NV1,
7280 .access = PL1_RW, .type = ARM_CP_SME,
7281 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
7282 .writefn = smcr_write, .raw_writefn = raw_write },
7283 { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
7284 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
7285 .access = PL2_RW, .type = ARM_CP_SME,
7286 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
7287 .writefn = smcr_write, .raw_writefn = raw_write },
7288 { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
7289 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
7290 .access = PL3_RW, .type = ARM_CP_SME,
7291 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
7292 .writefn = smcr_write, .raw_writefn = raw_write },
7293 { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
7294 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
7295 .access = PL1_R, .accessfn = access_aa64_tid1,
7296 /*
7297 * IMPLEMENTOR = 0 (software)
7298 * REVISION = 0 (implementation defined)
7299 * SMPS = 0 (no streaming execution priority in QEMU)
7300 * AFFINITY = 0 (streaming sve mode not shared with other PEs)
7301 */
7302 .type = ARM_CP_CONST, .resetvalue = 0, },
7303 /*
7304 * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
7305 */
7306 { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
7307 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
7308 .access = PL1_RW, .accessfn = access_smpri,
7309 .fgt = FGT_NSMPRI_EL1,
7310 .type = ARM_CP_CONST, .resetvalue = 0 },
7311 { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
7312 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
7313 .nv2_redirect_offset = 0x1f8,
7314 .access = PL2_RW, .accessfn = access_smprimap,
7315 .type = ARM_CP_CONST, .resetvalue = 0 },
7316 };
7317
7318 static void tlbi_aa64_paall_write(CPUARMState *env, const ARMCPRegInfo *ri,
7319 uint64_t value)
7320 {
7321 CPUState *cs = env_cpu(env);
7322
7323 tlb_flush(cs);
7324 }
7325
7326 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7327 uint64_t value)
7328 {
7329 /* L0GPTSZ is RO; other bits not mentioned are RES0. */
7330 uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
7331 R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
7332 R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
7333
7334 env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
7335 }
7336
7337 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
7338 {
7339 env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
7340 env_archcpu(env)->reset_l0gptsz);
7341 }
7342
7343 static void tlbi_aa64_paallos_write(CPUARMState *env, const ARMCPRegInfo *ri,
7344 uint64_t value)
7345 {
7346 CPUState *cs = env_cpu(env);
7347
7348 tlb_flush_all_cpus_synced(cs);
7349 }
7350
7351 static const ARMCPRegInfo rme_reginfo[] = {
7352 { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
7353 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
7354 .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
7355 .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
7356 { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
7357 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
7358 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
7359 { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
7360 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
7361 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
7362 { .name = "TLBI_PAALL", .state = ARM_CP_STATE_AA64,
7363 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 4,
7364 .access = PL3_W, .type = ARM_CP_NO_RAW,
7365 .writefn = tlbi_aa64_paall_write },
7366 { .name = "TLBI_PAALLOS", .state = ARM_CP_STATE_AA64,
7367 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 4,
7368 .access = PL3_W, .type = ARM_CP_NO_RAW,
7369 .writefn = tlbi_aa64_paallos_write },
7370 /*
7371 * QEMU does not have a way to invalidate by physical address, thus
7372 * invalidating a range of physical addresses is accomplished by
7373 * flushing all tlb entries in the outer shareable domain,
7374 * just like PAALLOS.
7375 */
7376 { .name = "TLBI_RPALOS", .state = ARM_CP_STATE_AA64,
7377 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 7,
7378 .access = PL3_W, .type = ARM_CP_NO_RAW,
7379 .writefn = tlbi_aa64_paallos_write },
7380 { .name = "TLBI_RPAOS", .state = ARM_CP_STATE_AA64,
7381 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 3,
7382 .access = PL3_W, .type = ARM_CP_NO_RAW,
7383 .writefn = tlbi_aa64_paallos_write },
7384 { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
7385 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
7386 .access = PL3_W, .type = ARM_CP_NOP },
7387 };
7388
7389 static const ARMCPRegInfo rme_mte_reginfo[] = {
7390 { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
7391 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
7392 .access = PL3_W, .type = ARM_CP_NOP },
7393 };
7394 #endif /* TARGET_AARCH64 */
7395
7396 static void define_pmu_regs(ARMCPU *cpu)
7397 {
7398 /*
7399 * v7 performance monitor control register: same implementor
7400 * field as main ID register, and we implement four counters in
7401 * addition to the cycle count register.
7402 */
7403 unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
7404 ARMCPRegInfo pmcr = {
7405 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
7406 .access = PL0_RW,
7407 .fgt = FGT_PMCR_EL0,
7408 .type = ARM_CP_IO | ARM_CP_ALIAS,
7409 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
7410 .accessfn = pmreg_access,
7411 .readfn = pmcr_read, .raw_readfn = raw_read,
7412 .writefn = pmcr_write, .raw_writefn = raw_write,
7413 };
7414 ARMCPRegInfo pmcr64 = {
7415 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
7416 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
7417 .access = PL0_RW, .accessfn = pmreg_access,
7418 .fgt = FGT_PMCR_EL0,
7419 .type = ARM_CP_IO,
7420 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
7421 .resetvalue = cpu->isar.reset_pmcr_el0,
7422 .readfn = pmcr_read, .raw_readfn = raw_read,
7423 .writefn = pmcr_write, .raw_writefn = raw_write,
7424 };
7425
7426 define_one_arm_cp_reg(cpu, &pmcr);
7427 define_one_arm_cp_reg(cpu, &pmcr64);
7428 for (i = 0; i < pmcrn; i++) {
7429 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
7430 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
7431 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
7432 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
7433 ARMCPRegInfo pmev_regs[] = {
7434 { .name = pmevcntr_name, .cp = 15, .crn = 14,
7435 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7436 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7437 .fgt = FGT_PMEVCNTRN_EL0,
7438 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7439 .accessfn = pmreg_access_xevcntr },
7440 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
7441 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
7442 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
7443 .type = ARM_CP_IO,
7444 .fgt = FGT_PMEVCNTRN_EL0,
7445 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7446 .raw_readfn = pmevcntr_rawread,
7447 .raw_writefn = pmevcntr_rawwrite },
7448 { .name = pmevtyper_name, .cp = 15, .crn = 14,
7449 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7450 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7451 .fgt = FGT_PMEVTYPERN_EL0,
7452 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7453 .accessfn = pmreg_access },
7454 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
7455 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
7456 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
7457 .fgt = FGT_PMEVTYPERN_EL0,
7458 .type = ARM_CP_IO,
7459 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7460 .raw_writefn = pmevtyper_rawwrite },
7461 };
7462 define_arm_cp_regs(cpu, pmev_regs);
7463 g_free(pmevcntr_name);
7464 g_free(pmevcntr_el0_name);
7465 g_free(pmevtyper_name);
7466 g_free(pmevtyper_el0_name);
7467 }
7468 if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
7469 ARMCPRegInfo v81_pmu_regs[] = {
7470 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
7471 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
7472 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7473 .fgt = FGT_PMCEIDN_EL0,
7474 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
7475 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
7476 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
7477 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7478 .fgt = FGT_PMCEIDN_EL0,
7479 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
7480 };
7481 define_arm_cp_regs(cpu, v81_pmu_regs);
7482 }
7483 if (cpu_isar_feature(any_pmuv3p4, cpu)) {
7484 static const ARMCPRegInfo v84_pmmir = {
7485 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
7486 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
7487 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7488 .fgt = FGT_PMMIR_EL1,
7489 .resetvalue = 0
7490 };
7491 define_one_arm_cp_reg(cpu, &v84_pmmir);
7492 }
7493 }
7494
7495 #ifndef CONFIG_USER_ONLY
7496 /*
7497 * We don't know until after realize whether there's a GICv3
7498 * attached, and that is what registers the gicv3 sysregs.
7499 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
7500 * at runtime.
7501 */
7502 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
7503 {
7504 ARMCPU *cpu = env_archcpu(env);
7505 uint64_t pfr1 = cpu->isar.id_pfr1;
7506
7507 if (env->gicv3state) {
7508 pfr1 |= 1 << 28;
7509 }
7510 return pfr1;
7511 }
7512
7513 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
7514 {
7515 ARMCPU *cpu = env_archcpu(env);
7516 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
7517
7518 if (env->gicv3state) {
7519 pfr0 |= 1 << 24;
7520 }
7521 return pfr0;
7522 }
7523 #endif
7524
7525 /*
7526 * Shared logic between LORID and the rest of the LOR* registers.
7527 * Secure state exclusion has already been dealt with.
7528 */
7529 static CPAccessResult access_lor_ns(CPUARMState *env,
7530 const ARMCPRegInfo *ri, bool isread)
7531 {
7532 int el = arm_current_el(env);
7533
7534 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
7535 return CP_ACCESS_TRAP_EL2;
7536 }
7537 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
7538 return CP_ACCESS_TRAP_EL3;
7539 }
7540 return CP_ACCESS_OK;
7541 }
7542
7543 static CPAccessResult access_lor_other(CPUARMState *env,
7544 const ARMCPRegInfo *ri, bool isread)
7545 {
7546 if (arm_is_secure_below_el3(env)) {
7547 /* Access denied in secure mode. */
7548 return CP_ACCESS_TRAP;
7549 }
7550 return access_lor_ns(env, ri, isread);
7551 }
7552
7553 /*
7554 * A trivial implementation of ARMv8.1-LOR leaves all of these
7555 * registers fixed at 0, which indicates that there are zero
7556 * supported Limited Ordering regions.
7557 */
7558 static const ARMCPRegInfo lor_reginfo[] = {
7559 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
7560 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
7561 .access = PL1_RW, .accessfn = access_lor_other,
7562 .fgt = FGT_LORSA_EL1,
7563 .type = ARM_CP_CONST, .resetvalue = 0 },
7564 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7565 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7566 .access = PL1_RW, .accessfn = access_lor_other,
7567 .fgt = FGT_LOREA_EL1,
7568 .type = ARM_CP_CONST, .resetvalue = 0 },
7569 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7570 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7571 .access = PL1_RW, .accessfn = access_lor_other,
7572 .fgt = FGT_LORN_EL1,
7573 .type = ARM_CP_CONST, .resetvalue = 0 },
7574 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7575 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7576 .access = PL1_RW, .accessfn = access_lor_other,
7577 .fgt = FGT_LORC_EL1,
7578 .type = ARM_CP_CONST, .resetvalue = 0 },
7579 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7580 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7581 .access = PL1_R, .accessfn = access_lor_ns,
7582 .fgt = FGT_LORID_EL1,
7583 .type = ARM_CP_CONST, .resetvalue = 0 },
7584 };
7585
7586 #ifdef TARGET_AARCH64
7587 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7588 bool isread)
7589 {
7590 int el = arm_current_el(env);
7591
7592 if (el < 2 &&
7593 arm_is_el2_enabled(env) &&
7594 !(arm_hcr_el2_eff(env) & HCR_APK)) {
7595 return CP_ACCESS_TRAP_EL2;
7596 }
7597 if (el < 3 &&
7598 arm_feature(env, ARM_FEATURE_EL3) &&
7599 !(env->cp15.scr_el3 & SCR_APK)) {
7600 return CP_ACCESS_TRAP_EL3;
7601 }
7602 return CP_ACCESS_OK;
7603 }
7604
7605 static const ARMCPRegInfo pauth_reginfo[] = {
7606 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7607 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7608 .access = PL1_RW, .accessfn = access_pauth,
7609 .fgt = FGT_APDAKEY,
7610 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7611 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7612 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7613 .access = PL1_RW, .accessfn = access_pauth,
7614 .fgt = FGT_APDAKEY,
7615 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7616 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7617 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7618 .access = PL1_RW, .accessfn = access_pauth,
7619 .fgt = FGT_APDBKEY,
7620 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7621 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7622 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7623 .access = PL1_RW, .accessfn = access_pauth,
7624 .fgt = FGT_APDBKEY,
7625 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7626 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7627 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7628 .access = PL1_RW, .accessfn = access_pauth,
7629 .fgt = FGT_APGAKEY,
7630 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7631 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7632 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7633 .access = PL1_RW, .accessfn = access_pauth,
7634 .fgt = FGT_APGAKEY,
7635 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7636 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7637 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7638 .access = PL1_RW, .accessfn = access_pauth,
7639 .fgt = FGT_APIAKEY,
7640 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7641 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7642 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7643 .access = PL1_RW, .accessfn = access_pauth,
7644 .fgt = FGT_APIAKEY,
7645 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7646 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7647 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7648 .access = PL1_RW, .accessfn = access_pauth,
7649 .fgt = FGT_APIBKEY,
7650 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7651 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7652 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7653 .access = PL1_RW, .accessfn = access_pauth,
7654 .fgt = FGT_APIBKEY,
7655 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7656 };
7657
7658 static const ARMCPRegInfo tlbirange_reginfo[] = {
7659 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7660 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
7661 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7662 .fgt = FGT_TLBIRVAE1IS,
7663 .writefn = tlbi_aa64_rvae1is_write },
7664 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7665 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
7666 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7667 .fgt = FGT_TLBIRVAAE1IS,
7668 .writefn = tlbi_aa64_rvae1is_write },
7669 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7670 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
7671 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7672 .fgt = FGT_TLBIRVALE1IS,
7673 .writefn = tlbi_aa64_rvae1is_write },
7674 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7675 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
7676 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7677 .fgt = FGT_TLBIRVAALE1IS,
7678 .writefn = tlbi_aa64_rvae1is_write },
7679 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7680 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7681 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7682 .fgt = FGT_TLBIRVAE1OS,
7683 .writefn = tlbi_aa64_rvae1is_write },
7684 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7685 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
7686 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7687 .fgt = FGT_TLBIRVAAE1OS,
7688 .writefn = tlbi_aa64_rvae1is_write },
7689 { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7690 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
7691 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7692 .fgt = FGT_TLBIRVALE1OS,
7693 .writefn = tlbi_aa64_rvae1is_write },
7694 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7695 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
7696 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7697 .fgt = FGT_TLBIRVAALE1OS,
7698 .writefn = tlbi_aa64_rvae1is_write },
7699 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7700 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7701 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7702 .fgt = FGT_TLBIRVAE1,
7703 .writefn = tlbi_aa64_rvae1_write },
7704 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7705 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
7706 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7707 .fgt = FGT_TLBIRVAAE1,
7708 .writefn = tlbi_aa64_rvae1_write },
7709 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7710 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
7711 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7712 .fgt = FGT_TLBIRVALE1,
7713 .writefn = tlbi_aa64_rvae1_write },
7714 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7715 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
7716 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7717 .fgt = FGT_TLBIRVAALE1,
7718 .writefn = tlbi_aa64_rvae1_write },
7719 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7720 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
7721 .access = PL2_W, .type = ARM_CP_NO_RAW,
7722 .writefn = tlbi_aa64_ripas2e1is_write },
7723 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7724 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
7725 .access = PL2_W, .type = ARM_CP_NO_RAW,
7726 .writefn = tlbi_aa64_ripas2e1is_write },
7727 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7728 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
7729 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7730 .writefn = tlbi_aa64_rvae2is_write },
7731 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7732 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
7733 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7734 .writefn = tlbi_aa64_rvae2is_write },
7735 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7736 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
7737 .access = PL2_W, .type = ARM_CP_NO_RAW,
7738 .writefn = tlbi_aa64_ripas2e1_write },
7739 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
7740 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
7741 .access = PL2_W, .type = ARM_CP_NO_RAW,
7742 .writefn = tlbi_aa64_ripas2e1_write },
7743 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7744 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
7745 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7746 .writefn = tlbi_aa64_rvae2is_write },
7747 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7748 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
7749 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7750 .writefn = tlbi_aa64_rvae2is_write },
7751 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7752 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
7753 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7754 .writefn = tlbi_aa64_rvae2_write },
7755 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7756 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
7757 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7758 .writefn = tlbi_aa64_rvae2_write },
7759 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7760 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7761 .access = PL3_W, .type = ARM_CP_NO_RAW,
7762 .writefn = tlbi_aa64_rvae3is_write },
7763 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7764 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7765 .access = PL3_W, .type = ARM_CP_NO_RAW,
7766 .writefn = tlbi_aa64_rvae3is_write },
7767 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7768 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7769 .access = PL3_W, .type = ARM_CP_NO_RAW,
7770 .writefn = tlbi_aa64_rvae3is_write },
7771 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7772 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7773 .access = PL3_W, .type = ARM_CP_NO_RAW,
7774 .writefn = tlbi_aa64_rvae3is_write },
7775 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7776 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7777 .access = PL3_W, .type = ARM_CP_NO_RAW,
7778 .writefn = tlbi_aa64_rvae3_write },
7779 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7780 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7781 .access = PL3_W, .type = ARM_CP_NO_RAW,
7782 .writefn = tlbi_aa64_rvae3_write },
7783 };
7784
7785 static const ARMCPRegInfo tlbios_reginfo[] = {
7786 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7787 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
7788 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7789 .fgt = FGT_TLBIVMALLE1OS,
7790 .writefn = tlbi_aa64_vmalle1is_write },
7791 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7792 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
7793 .fgt = FGT_TLBIVAE1OS,
7794 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7795 .writefn = tlbi_aa64_vae1is_write },
7796 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7797 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
7798 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7799 .fgt = FGT_TLBIASIDE1OS,
7800 .writefn = tlbi_aa64_vmalle1is_write },
7801 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7802 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
7803 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7804 .fgt = FGT_TLBIVAAE1OS,
7805 .writefn = tlbi_aa64_vae1is_write },
7806 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7807 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
7808 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7809 .fgt = FGT_TLBIVALE1OS,
7810 .writefn = tlbi_aa64_vae1is_write },
7811 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7812 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
7813 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7814 .fgt = FGT_TLBIVAALE1OS,
7815 .writefn = tlbi_aa64_vae1is_write },
7816 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7817 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
7818 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7819 .writefn = tlbi_aa64_alle2is_write },
7820 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7821 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
7822 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7823 .writefn = tlbi_aa64_vae2is_write },
7824 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7825 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7826 .access = PL2_W, .type = ARM_CP_NO_RAW,
7827 .writefn = tlbi_aa64_alle1is_write },
7828 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7829 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
7830 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7831 .writefn = tlbi_aa64_vae2is_write },
7832 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7833 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7834 .access = PL2_W, .type = ARM_CP_NO_RAW,
7835 .writefn = tlbi_aa64_alle1is_write },
7836 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7837 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7838 .access = PL2_W, .type = ARM_CP_NOP },
7839 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7840 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7841 .access = PL2_W, .type = ARM_CP_NOP },
7842 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7843 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7844 .access = PL2_W, .type = ARM_CP_NOP },
7845 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7846 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7847 .access = PL2_W, .type = ARM_CP_NOP },
7848 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7849 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7850 .access = PL3_W, .type = ARM_CP_NO_RAW,
7851 .writefn = tlbi_aa64_alle3is_write },
7852 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7853 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7854 .access = PL3_W, .type = ARM_CP_NO_RAW,
7855 .writefn = tlbi_aa64_vae3is_write },
7856 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7857 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7858 .access = PL3_W, .type = ARM_CP_NO_RAW,
7859 .writefn = tlbi_aa64_vae3is_write },
7860 };
7861
7862 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7863 {
7864 Error *err = NULL;
7865 uint64_t ret;
7866
7867 /* Success sets NZCV = 0000. */
7868 env->NF = env->CF = env->VF = 0, env->ZF = 1;
7869
7870 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7871 /*
7872 * ??? Failed, for unknown reasons in the crypto subsystem.
7873 * The best we can do is log the reason and return the
7874 * timed-out indication to the guest. There is no reason
7875 * we know to expect this failure to be transitory, so the
7876 * guest may well hang retrying the operation.
7877 */
7878 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7879 ri->name, error_get_pretty(err));
7880 error_free(err);
7881
7882 env->ZF = 0; /* NZCF = 0100 */
7883 return 0;
7884 }
7885 return ret;
7886 }
7887
7888 /* We do not support re-seeding, so the two registers operate the same. */
7889 static const ARMCPRegInfo rndr_reginfo[] = {
7890 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7891 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7892 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7893 .access = PL0_R, .readfn = rndr_readfn },
7894 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7895 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7896 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7897 .access = PL0_R, .readfn = rndr_readfn },
7898 };
7899
7900 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7901 uint64_t value)
7902 {
7903 #ifdef CONFIG_TCG
7904 ARMCPU *cpu = env_archcpu(env);
7905 /* CTR_EL0 System register -> DminLine, bits [19:16] */
7906 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7907 uint64_t vaddr_in = (uint64_t) value;
7908 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7909 void *haddr;
7910 int mem_idx = arm_env_mmu_index(env);
7911
7912 /* This won't be crossing page boundaries */
7913 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7914 if (haddr) {
7915 #ifndef CONFIG_USER_ONLY
7916
7917 ram_addr_t offset;
7918 MemoryRegion *mr;
7919
7920 /* RCU lock is already being held */
7921 mr = memory_region_from_host(haddr, &offset);
7922
7923 if (mr) {
7924 memory_region_writeback(mr, offset, dline_size);
7925 }
7926 #endif /*CONFIG_USER_ONLY*/
7927 }
7928 #else
7929 /* Handled by hardware accelerator. */
7930 g_assert_not_reached();
7931 #endif /* CONFIG_TCG */
7932 }
7933
7934 static const ARMCPRegInfo dcpop_reg[] = {
7935 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7936 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7937 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7938 .fgt = FGT_DCCVAP,
7939 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7940 };
7941
7942 static const ARMCPRegInfo dcpodp_reg[] = {
7943 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7944 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7945 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7946 .fgt = FGT_DCCVADP,
7947 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7948 };
7949
7950 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7951 bool isread)
7952 {
7953 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7954 return CP_ACCESS_TRAP_EL2;
7955 }
7956
7957 return CP_ACCESS_OK;
7958 }
7959
7960 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7961 bool isread)
7962 {
7963 int el = arm_current_el(env);
7964 if (el < 2 && arm_is_el2_enabled(env)) {
7965 uint64_t hcr = arm_hcr_el2_eff(env);
7966 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7967 return CP_ACCESS_TRAP_EL2;
7968 }
7969 }
7970 if (el < 3 &&
7971 arm_feature(env, ARM_FEATURE_EL3) &&
7972 !(env->cp15.scr_el3 & SCR_ATA)) {
7973 return CP_ACCESS_TRAP_EL3;
7974 }
7975 return CP_ACCESS_OK;
7976 }
7977
7978 static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri,
7979 bool isread)
7980 {
7981 CPAccessResult nv1 = access_nv1(env, ri, isread);
7982
7983 if (nv1 != CP_ACCESS_OK) {
7984 return nv1;
7985 }
7986 return access_mte(env, ri, isread);
7987 }
7988
7989 static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri,
7990 bool isread)
7991 {
7992 /*
7993 * TFSR_EL2: similar to generic access_mte(), but we need to
7994 * account for FEAT_NV. At EL1 this must be a FEAT_NV access;
7995 * if NV2 is enabled then we will redirect this to TFSR_EL1
7996 * after doing the HCR and SCR ATA traps; otherwise this will
7997 * be a trap to EL2 and the HCR/SCR traps do not apply.
7998 */
7999 int el = arm_current_el(env);
8000
8001 if (el == 1 && (arm_hcr_el2_eff(env) & HCR_NV2)) {
8002 return CP_ACCESS_OK;
8003 }
8004 if (el < 2 && arm_is_el2_enabled(env)) {
8005 uint64_t hcr = arm_hcr_el2_eff(env);
8006 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
8007 return CP_ACCESS_TRAP_EL2;
8008 }
8009 }
8010 if (el < 3 &&
8011 arm_feature(env, ARM_FEATURE_EL3) &&
8012 !(env->cp15.scr_el3 & SCR_ATA)) {
8013 return CP_ACCESS_TRAP_EL3;
8014 }
8015 return CP_ACCESS_OK;
8016 }
8017
8018 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
8019 {
8020 return env->pstate & PSTATE_TCO;
8021 }
8022
8023 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
8024 {
8025 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
8026 }
8027
8028 static const ARMCPRegInfo mte_reginfo[] = {
8029 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
8030 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
8031 .access = PL1_RW, .accessfn = access_mte,
8032 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
8033 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
8034 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
8035 .access = PL1_RW, .accessfn = access_tfsr_el1,
8036 .nv2_redirect_offset = 0x190 | NV2_REDIR_NV1,
8037 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
8038 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
8039 .type = ARM_CP_NV2_REDIRECT,
8040 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
8041 .access = PL2_RW, .accessfn = access_tfsr_el2,
8042 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
8043 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
8044 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
8045 .access = PL3_RW,
8046 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
8047 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
8048 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
8049 .access = PL1_RW, .accessfn = access_mte,
8050 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
8051 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
8052 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
8053 .access = PL1_RW, .accessfn = access_mte,
8054 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
8055 { .name = "TCO", .state = ARM_CP_STATE_AA64,
8056 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
8057 .type = ARM_CP_NO_RAW,
8058 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
8059 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
8060 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
8061 .type = ARM_CP_NOP, .access = PL1_W,
8062 .fgt = FGT_DCIVAC,
8063 .accessfn = aa64_cacheop_poc_access },
8064 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
8065 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
8066 .fgt = FGT_DCISW,
8067 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8068 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
8069 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
8070 .type = ARM_CP_NOP, .access = PL1_W,
8071 .fgt = FGT_DCIVAC,
8072 .accessfn = aa64_cacheop_poc_access },
8073 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
8074 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
8075 .fgt = FGT_DCISW,
8076 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8077 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
8078 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
8079 .fgt = FGT_DCCSW,
8080 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8081 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
8082 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
8083 .fgt = FGT_DCCSW,
8084 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8085 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
8086 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
8087 .fgt = FGT_DCCISW,
8088 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8089 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
8090 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
8091 .fgt = FGT_DCCISW,
8092 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8093 };
8094
8095 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
8096 { .name = "TCO", .state = ARM_CP_STATE_AA64,
8097 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
8098 .type = ARM_CP_CONST, .access = PL0_RW, },
8099 };
8100
8101 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
8102 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
8103 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
8104 .type = ARM_CP_NOP, .access = PL0_W,
8105 .fgt = FGT_DCCVAC,
8106 .accessfn = aa64_cacheop_poc_access },
8107 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
8108 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
8109 .type = ARM_CP_NOP, .access = PL0_W,
8110 .fgt = FGT_DCCVAC,
8111 .accessfn = aa64_cacheop_poc_access },
8112 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
8113 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
8114 .type = ARM_CP_NOP, .access = PL0_W,
8115 .fgt = FGT_DCCVAP,
8116 .accessfn = aa64_cacheop_poc_access },
8117 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
8118 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
8119 .type = ARM_CP_NOP, .access = PL0_W,
8120 .fgt = FGT_DCCVAP,
8121 .accessfn = aa64_cacheop_poc_access },
8122 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
8123 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
8124 .type = ARM_CP_NOP, .access = PL0_W,
8125 .fgt = FGT_DCCVADP,
8126 .accessfn = aa64_cacheop_poc_access },
8127 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
8128 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
8129 .type = ARM_CP_NOP, .access = PL0_W,
8130 .fgt = FGT_DCCVADP,
8131 .accessfn = aa64_cacheop_poc_access },
8132 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
8133 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
8134 .type = ARM_CP_NOP, .access = PL0_W,
8135 .fgt = FGT_DCCIVAC,
8136 .accessfn = aa64_cacheop_poc_access },
8137 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
8138 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
8139 .type = ARM_CP_NOP, .access = PL0_W,
8140 .fgt = FGT_DCCIVAC,
8141 .accessfn = aa64_cacheop_poc_access },
8142 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
8143 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
8144 .access = PL0_W, .type = ARM_CP_DC_GVA,
8145 #ifndef CONFIG_USER_ONLY
8146 /* Avoid overhead of an access check that always passes in user-mode */
8147 .accessfn = aa64_zva_access,
8148 .fgt = FGT_DCZVA,
8149 #endif
8150 },
8151 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
8152 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
8153 .access = PL0_W, .type = ARM_CP_DC_GZVA,
8154 #ifndef CONFIG_USER_ONLY
8155 /* Avoid overhead of an access check that always passes in user-mode */
8156 .accessfn = aa64_zva_access,
8157 .fgt = FGT_DCZVA,
8158 #endif
8159 },
8160 };
8161
8162 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
8163 bool isread)
8164 {
8165 uint64_t hcr = arm_hcr_el2_eff(env);
8166 int el = arm_current_el(env);
8167
8168 if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
8169 if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
8170 if (hcr & HCR_TGE) {
8171 return CP_ACCESS_TRAP_EL2;
8172 }
8173 return CP_ACCESS_TRAP;
8174 }
8175 } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
8176 return CP_ACCESS_TRAP_EL2;
8177 }
8178 if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
8179 return CP_ACCESS_TRAP_EL2;
8180 }
8181 if (el < 3
8182 && arm_feature(env, ARM_FEATURE_EL3)
8183 && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
8184 return CP_ACCESS_TRAP_EL3;
8185 }
8186 return CP_ACCESS_OK;
8187 }
8188
8189 static CPAccessResult access_scxtnum_el1(CPUARMState *env,
8190 const ARMCPRegInfo *ri,
8191 bool isread)
8192 {
8193 CPAccessResult nv1 = access_nv1(env, ri, isread);
8194
8195 if (nv1 != CP_ACCESS_OK) {
8196 return nv1;
8197 }
8198 return access_scxtnum(env, ri, isread);
8199 }
8200
8201 static const ARMCPRegInfo scxtnum_reginfo[] = {
8202 { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
8203 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
8204 .access = PL0_RW, .accessfn = access_scxtnum,
8205 .fgt = FGT_SCXTNUM_EL0,
8206 .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
8207 { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
8208 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
8209 .access = PL1_RW, .accessfn = access_scxtnum_el1,
8210 .fgt = FGT_SCXTNUM_EL1,
8211 .nv2_redirect_offset = 0x188 | NV2_REDIR_NV1,
8212 .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
8213 { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
8214 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
8215 .access = PL2_RW, .accessfn = access_scxtnum,
8216 .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
8217 { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
8218 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
8219 .access = PL3_RW,
8220 .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
8221 };
8222
8223 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
8224 bool isread)
8225 {
8226 if (arm_current_el(env) == 2 &&
8227 arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
8228 return CP_ACCESS_TRAP_EL3;
8229 }
8230 return CP_ACCESS_OK;
8231 }
8232
8233 static const ARMCPRegInfo fgt_reginfo[] = {
8234 { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
8235 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
8236 .nv2_redirect_offset = 0x1b8,
8237 .access = PL2_RW, .accessfn = access_fgt,
8238 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
8239 { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
8240 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
8241 .nv2_redirect_offset = 0x1c0,
8242 .access = PL2_RW, .accessfn = access_fgt,
8243 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
8244 { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
8245 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
8246 .nv2_redirect_offset = 0x1d0,
8247 .access = PL2_RW, .accessfn = access_fgt,
8248 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
8249 { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
8250 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
8251 .nv2_redirect_offset = 0x1d8,
8252 .access = PL2_RW, .accessfn = access_fgt,
8253 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
8254 { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
8255 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
8256 .nv2_redirect_offset = 0x1c8,
8257 .access = PL2_RW, .accessfn = access_fgt,
8258 .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
8259 };
8260
8261 static void vncr_write(CPUARMState *env, const ARMCPRegInfo *ri,
8262 uint64_t value)
8263 {
8264 /*
8265 * Clear the RES0 bottom 12 bits; this means at runtime we can guarantee
8266 * that VNCR_EL2 + offset is 64-bit aligned. We don't need to do anything
8267 * about the RESS bits at the top -- we choose the "generate an EL2
8268 * translation abort on use" CONSTRAINED UNPREDICTABLE option (i.e. let
8269 * the ptw.c code detect the resulting invalid address).
8270 */
8271 env->cp15.vncr_el2 = value & ~0xfffULL;
8272 }
8273
8274 static const ARMCPRegInfo nv2_reginfo[] = {
8275 { .name = "VNCR_EL2", .state = ARM_CP_STATE_AA64,
8276 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 2, .opc2 = 0,
8277 .access = PL2_RW,
8278 .writefn = vncr_write,
8279 .nv2_redirect_offset = 0xb0,
8280 .fieldoffset = offsetof(CPUARMState, cp15.vncr_el2) },
8281 };
8282
8283 #endif /* TARGET_AARCH64 */
8284
8285 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
8286 bool isread)
8287 {
8288 int el = arm_current_el(env);
8289
8290 if (el == 0) {
8291 uint64_t sctlr = arm_sctlr(env, el);
8292 if (!(sctlr & SCTLR_EnRCTX)) {
8293 return CP_ACCESS_TRAP;
8294 }
8295 } else if (el == 1) {
8296 uint64_t hcr = arm_hcr_el2_eff(env);
8297 if (hcr & HCR_NV) {
8298 return CP_ACCESS_TRAP_EL2;
8299 }
8300 }
8301 return CP_ACCESS_OK;
8302 }
8303
8304 static const ARMCPRegInfo predinv_reginfo[] = {
8305 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
8306 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
8307 .fgt = FGT_CFPRCTX,
8308 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8309 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
8310 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
8311 .fgt = FGT_DVPRCTX,
8312 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8313 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
8314 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
8315 .fgt = FGT_CPPRCTX,
8316 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8317 /*
8318 * Note the AArch32 opcodes have a different OPC1.
8319 */
8320 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
8321 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
8322 .fgt = FGT_CFPRCTX,
8323 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8324 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
8325 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
8326 .fgt = FGT_DVPRCTX,
8327 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8328 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
8329 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
8330 .fgt = FGT_CPPRCTX,
8331 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8332 };
8333
8334 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
8335 {
8336 /* Read the high 32 bits of the current CCSIDR */
8337 return extract64(ccsidr_read(env, ri), 32, 32);
8338 }
8339
8340 static const ARMCPRegInfo ccsidr2_reginfo[] = {
8341 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
8342 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
8343 .access = PL1_R,
8344 .accessfn = access_tid4,
8345 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
8346 };
8347
8348 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8349 bool isread)
8350 {
8351 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
8352 return CP_ACCESS_TRAP_EL2;
8353 }
8354
8355 return CP_ACCESS_OK;
8356 }
8357
8358 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8359 bool isread)
8360 {
8361 if (arm_feature(env, ARM_FEATURE_V8)) {
8362 return access_aa64_tid3(env, ri, isread);
8363 }
8364
8365 return CP_ACCESS_OK;
8366 }
8367
8368 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
8369 bool isread)
8370 {
8371 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
8372 return CP_ACCESS_TRAP_EL2;
8373 }
8374
8375 return CP_ACCESS_OK;
8376 }
8377
8378 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
8379 const ARMCPRegInfo *ri, bool isread)
8380 {
8381 /*
8382 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
8383 * in v7A, not in v8A.
8384 */
8385 if (!arm_feature(env, ARM_FEATURE_V8) &&
8386 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
8387 (env->cp15.hstr_el2 & HSTR_TJDBX)) {
8388 return CP_ACCESS_TRAP_EL2;
8389 }
8390 return CP_ACCESS_OK;
8391 }
8392
8393 static const ARMCPRegInfo jazelle_regs[] = {
8394 { .name = "JIDR",
8395 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
8396 .access = PL1_R, .accessfn = access_jazelle,
8397 .type = ARM_CP_CONST, .resetvalue = 0 },
8398 { .name = "JOSCR",
8399 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8400 .accessfn = access_joscr_jmcr,
8401 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8402 { .name = "JMCR",
8403 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8404 .accessfn = access_joscr_jmcr,
8405 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8406 };
8407
8408 static const ARMCPRegInfo contextidr_el2 = {
8409 .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
8410 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
8411 .access = PL2_RW,
8412 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
8413 };
8414
8415 static const ARMCPRegInfo vhe_reginfo[] = {
8416 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
8417 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
8418 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
8419 .raw_writefn = raw_write,
8420 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8421 #ifndef CONFIG_USER_ONLY
8422 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
8423 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
8424 .fieldoffset =
8425 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
8426 .type = ARM_CP_IO, .access = PL2_RW,
8427 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
8428 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
8429 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
8430 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
8431 .resetfn = gt_hv_timer_reset,
8432 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
8433 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
8434 .type = ARM_CP_IO,
8435 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
8436 .access = PL2_RW,
8437 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
8438 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
8439 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
8440 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
8441 .type = ARM_CP_IO | ARM_CP_ALIAS,
8442 .access = PL2_RW, .accessfn = access_el1nvpct,
8443 .nv2_redirect_offset = 0x180 | NV2_REDIR_NO_NV1,
8444 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
8445 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
8446 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
8447 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
8448 .type = ARM_CP_IO | ARM_CP_ALIAS,
8449 .access = PL2_RW, .accessfn = access_el1nvvct,
8450 .nv2_redirect_offset = 0x170 | NV2_REDIR_NO_NV1,
8451 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
8452 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
8453 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8454 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
8455 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8456 .access = PL2_RW, .accessfn = e2h_access,
8457 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
8458 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8459 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
8460 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8461 .access = PL2_RW, .accessfn = e2h_access,
8462 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
8463 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8464 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
8465 .type = ARM_CP_IO | ARM_CP_ALIAS,
8466 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
8467 .nv2_redirect_offset = 0x178 | NV2_REDIR_NO_NV1,
8468 .access = PL2_RW, .accessfn = access_el1nvpct,
8469 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
8470 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8471 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
8472 .type = ARM_CP_IO | ARM_CP_ALIAS,
8473 .nv2_redirect_offset = 0x168 | NV2_REDIR_NO_NV1,
8474 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
8475 .access = PL2_RW, .accessfn = access_el1nvvct,
8476 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8477 #endif
8478 };
8479
8480 #ifndef CONFIG_USER_ONLY
8481 static const ARMCPRegInfo ats1e1_reginfo[] = {
8482 { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
8483 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8484 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8485 .fgt = FGT_ATS1E1RP,
8486 .accessfn = at_s1e01_access, .writefn = ats_write64 },
8487 { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
8488 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8489 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8490 .fgt = FGT_ATS1E1WP,
8491 .accessfn = at_s1e01_access, .writefn = ats_write64 },
8492 };
8493
8494 static const ARMCPRegInfo ats1cp_reginfo[] = {
8495 { .name = "ATS1CPRP",
8496 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8497 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8498 .writefn = ats_write },
8499 { .name = "ATS1CPWP",
8500 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8501 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8502 .writefn = ats_write },
8503 };
8504 #endif
8505
8506 /*
8507 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
8508 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
8509 * is non-zero, which is never for ARMv7, optionally in ARMv8
8510 * and mandatorily for ARMv8.2 and up.
8511 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
8512 * implementation is RAZ/WI we can ignore this detail, as we
8513 * do for ACTLR.
8514 */
8515 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
8516 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
8517 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
8518 .access = PL1_RW, .accessfn = access_tacr,
8519 .type = ARM_CP_CONST, .resetvalue = 0 },
8520 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
8521 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
8522 .access = PL2_RW, .type = ARM_CP_CONST,
8523 .resetvalue = 0 },
8524 };
8525
8526 void register_cp_regs_for_features(ARMCPU *cpu)
8527 {
8528 /* Register all the coprocessor registers based on feature bits */
8529 CPUARMState *env = &cpu->env;
8530 if (arm_feature(env, ARM_FEATURE_M)) {
8531 /* M profile has no coprocessor registers */
8532 return;
8533 }
8534
8535 define_arm_cp_regs(cpu, cp_reginfo);
8536 if (!arm_feature(env, ARM_FEATURE_V8)) {
8537 /*
8538 * Must go early as it is full of wildcards that may be
8539 * overridden by later definitions.
8540 */
8541 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
8542 }
8543
8544 if (arm_feature(env, ARM_FEATURE_V6)) {
8545 /* The ID registers all have impdef reset values */
8546 ARMCPRegInfo v6_idregs[] = {
8547 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
8548 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
8549 .access = PL1_R, .type = ARM_CP_CONST,
8550 .accessfn = access_aa32_tid3,
8551 .resetvalue = cpu->isar.id_pfr0 },
8552 /*
8553 * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
8554 * the value of the GIC field until after we define these regs.
8555 */
8556 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
8557 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
8558 .access = PL1_R, .type = ARM_CP_NO_RAW,
8559 .accessfn = access_aa32_tid3,
8560 #ifdef CONFIG_USER_ONLY
8561 .type = ARM_CP_CONST,
8562 .resetvalue = cpu->isar.id_pfr1,
8563 #else
8564 .type = ARM_CP_NO_RAW,
8565 .accessfn = access_aa32_tid3,
8566 .readfn = id_pfr1_read,
8567 .writefn = arm_cp_write_ignore
8568 #endif
8569 },
8570 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
8571 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
8572 .access = PL1_R, .type = ARM_CP_CONST,
8573 .accessfn = access_aa32_tid3,
8574 .resetvalue = cpu->isar.id_dfr0 },
8575 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
8576 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
8577 .access = PL1_R, .type = ARM_CP_CONST,
8578 .accessfn = access_aa32_tid3,
8579 .resetvalue = cpu->id_afr0 },
8580 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
8581 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
8582 .access = PL1_R, .type = ARM_CP_CONST,
8583 .accessfn = access_aa32_tid3,
8584 .resetvalue = cpu->isar.id_mmfr0 },
8585 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
8586 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
8587 .access = PL1_R, .type = ARM_CP_CONST,
8588 .accessfn = access_aa32_tid3,
8589 .resetvalue = cpu->isar.id_mmfr1 },
8590 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
8591 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
8592 .access = PL1_R, .type = ARM_CP_CONST,
8593 .accessfn = access_aa32_tid3,
8594 .resetvalue = cpu->isar.id_mmfr2 },
8595 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
8596 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
8597 .access = PL1_R, .type = ARM_CP_CONST,
8598 .accessfn = access_aa32_tid3,
8599 .resetvalue = cpu->isar.id_mmfr3 },
8600 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
8601 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
8602 .access = PL1_R, .type = ARM_CP_CONST,
8603 .accessfn = access_aa32_tid3,
8604 .resetvalue = cpu->isar.id_isar0 },
8605 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
8606 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
8607 .access = PL1_R, .type = ARM_CP_CONST,
8608 .accessfn = access_aa32_tid3,
8609 .resetvalue = cpu->isar.id_isar1 },
8610 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
8611 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
8612 .access = PL1_R, .type = ARM_CP_CONST,
8613 .accessfn = access_aa32_tid3,
8614 .resetvalue = cpu->isar.id_isar2 },
8615 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
8616 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
8617 .access = PL1_R, .type = ARM_CP_CONST,
8618 .accessfn = access_aa32_tid3,
8619 .resetvalue = cpu->isar.id_isar3 },
8620 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
8621 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
8622 .access = PL1_R, .type = ARM_CP_CONST,
8623 .accessfn = access_aa32_tid3,
8624 .resetvalue = cpu->isar.id_isar4 },
8625 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
8626 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
8627 .access = PL1_R, .type = ARM_CP_CONST,
8628 .accessfn = access_aa32_tid3,
8629 .resetvalue = cpu->isar.id_isar5 },
8630 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
8631 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
8632 .access = PL1_R, .type = ARM_CP_CONST,
8633 .accessfn = access_aa32_tid3,
8634 .resetvalue = cpu->isar.id_mmfr4 },
8635 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
8636 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
8637 .access = PL1_R, .type = ARM_CP_CONST,
8638 .accessfn = access_aa32_tid3,
8639 .resetvalue = cpu->isar.id_isar6 },
8640 };
8641 define_arm_cp_regs(cpu, v6_idregs);
8642 define_arm_cp_regs(cpu, v6_cp_reginfo);
8643 } else {
8644 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
8645 }
8646 if (arm_feature(env, ARM_FEATURE_V6K)) {
8647 define_arm_cp_regs(cpu, v6k_cp_reginfo);
8648 }
8649 if (arm_feature(env, ARM_FEATURE_V7MP) &&
8650 !arm_feature(env, ARM_FEATURE_PMSA)) {
8651 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
8652 }
8653 if (arm_feature(env, ARM_FEATURE_V7VE)) {
8654 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
8655 }
8656 if (arm_feature(env, ARM_FEATURE_V7)) {
8657 ARMCPRegInfo clidr = {
8658 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
8659 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
8660 .access = PL1_R, .type = ARM_CP_CONST,
8661 .accessfn = access_tid4,
8662 .fgt = FGT_CLIDR_EL1,
8663 .resetvalue = cpu->clidr
8664 };
8665 define_one_arm_cp_reg(cpu, &clidr);
8666 define_arm_cp_regs(cpu, v7_cp_reginfo);
8667 define_debug_regs(cpu);
8668 define_pmu_regs(cpu);
8669 } else {
8670 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
8671 }
8672 if (arm_feature(env, ARM_FEATURE_V8)) {
8673 /*
8674 * v8 ID registers, which all have impdef reset values.
8675 * Note that within the ID register ranges the unused slots
8676 * must all RAZ, not UNDEF; future architecture versions may
8677 * define new registers here.
8678 * ID registers which are AArch64 views of the AArch32 ID registers
8679 * which already existed in v6 and v7 are handled elsewhere,
8680 * in v6_idregs[].
8681 */
8682 int i;
8683 ARMCPRegInfo v8_idregs[] = {
8684 /*
8685 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
8686 * emulation because we don't know the right value for the
8687 * GIC field until after we define these regs.
8688 */
8689 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
8690 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
8691 .access = PL1_R,
8692 #ifdef CONFIG_USER_ONLY
8693 .type = ARM_CP_CONST,
8694 .resetvalue = cpu->isar.id_aa64pfr0
8695 #else
8696 .type = ARM_CP_NO_RAW,
8697 .accessfn = access_aa64_tid3,
8698 .readfn = id_aa64pfr0_read,
8699 .writefn = arm_cp_write_ignore
8700 #endif
8701 },
8702 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
8703 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
8704 .access = PL1_R, .type = ARM_CP_CONST,
8705 .accessfn = access_aa64_tid3,
8706 .resetvalue = cpu->isar.id_aa64pfr1},
8707 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8708 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
8709 .access = PL1_R, .type = ARM_CP_CONST,
8710 .accessfn = access_aa64_tid3,
8711 .resetvalue = 0 },
8712 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8713 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
8714 .access = PL1_R, .type = ARM_CP_CONST,
8715 .accessfn = access_aa64_tid3,
8716 .resetvalue = 0 },
8717 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
8718 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
8719 .access = PL1_R, .type = ARM_CP_CONST,
8720 .accessfn = access_aa64_tid3,
8721 .resetvalue = cpu->isar.id_aa64zfr0 },
8722 { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
8723 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
8724 .access = PL1_R, .type = ARM_CP_CONST,
8725 .accessfn = access_aa64_tid3,
8726 .resetvalue = cpu->isar.id_aa64smfr0 },
8727 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8728 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
8729 .access = PL1_R, .type = ARM_CP_CONST,
8730 .accessfn = access_aa64_tid3,
8731 .resetvalue = 0 },
8732 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8733 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
8734 .access = PL1_R, .type = ARM_CP_CONST,
8735 .accessfn = access_aa64_tid3,
8736 .resetvalue = 0 },
8737 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
8738 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
8739 .access = PL1_R, .type = ARM_CP_CONST,
8740 .accessfn = access_aa64_tid3,
8741 .resetvalue = cpu->isar.id_aa64dfr0 },
8742 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
8743 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
8744 .access = PL1_R, .type = ARM_CP_CONST,
8745 .accessfn = access_aa64_tid3,
8746 .resetvalue = cpu->isar.id_aa64dfr1 },
8747 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8748 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
8749 .access = PL1_R, .type = ARM_CP_CONST,
8750 .accessfn = access_aa64_tid3,
8751 .resetvalue = 0 },
8752 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8753 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
8754 .access = PL1_R, .type = ARM_CP_CONST,
8755 .accessfn = access_aa64_tid3,
8756 .resetvalue = 0 },
8757 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
8758 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
8759 .access = PL1_R, .type = ARM_CP_CONST,
8760 .accessfn = access_aa64_tid3,
8761 .resetvalue = cpu->id_aa64afr0 },
8762 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
8763 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
8764 .access = PL1_R, .type = ARM_CP_CONST,
8765 .accessfn = access_aa64_tid3,
8766 .resetvalue = cpu->id_aa64afr1 },
8767 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8768 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
8769 .access = PL1_R, .type = ARM_CP_CONST,
8770 .accessfn = access_aa64_tid3,
8771 .resetvalue = 0 },
8772 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8773 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8774 .access = PL1_R, .type = ARM_CP_CONST,
8775 .accessfn = access_aa64_tid3,
8776 .resetvalue = 0 },
8777 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8778 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8779 .access = PL1_R, .type = ARM_CP_CONST,
8780 .accessfn = access_aa64_tid3,
8781 .resetvalue = cpu->isar.id_aa64isar0 },
8782 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8783 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8784 .access = PL1_R, .type = ARM_CP_CONST,
8785 .accessfn = access_aa64_tid3,
8786 .resetvalue = cpu->isar.id_aa64isar1 },
8787 { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
8788 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8789 .access = PL1_R, .type = ARM_CP_CONST,
8790 .accessfn = access_aa64_tid3,
8791 .resetvalue = cpu->isar.id_aa64isar2 },
8792 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8793 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8794 .access = PL1_R, .type = ARM_CP_CONST,
8795 .accessfn = access_aa64_tid3,
8796 .resetvalue = 0 },
8797 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8798 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8799 .access = PL1_R, .type = ARM_CP_CONST,
8800 .accessfn = access_aa64_tid3,
8801 .resetvalue = 0 },
8802 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8803 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8804 .access = PL1_R, .type = ARM_CP_CONST,
8805 .accessfn = access_aa64_tid3,
8806 .resetvalue = 0 },
8807 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8808 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8809 .access = PL1_R, .type = ARM_CP_CONST,
8810 .accessfn = access_aa64_tid3,
8811 .resetvalue = 0 },
8812 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8813 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8814 .access = PL1_R, .type = ARM_CP_CONST,
8815 .accessfn = access_aa64_tid3,
8816 .resetvalue = 0 },
8817 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8818 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8819 .access = PL1_R, .type = ARM_CP_CONST,
8820 .accessfn = access_aa64_tid3,
8821 .resetvalue = cpu->isar.id_aa64mmfr0 },
8822 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8823 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8824 .access = PL1_R, .type = ARM_CP_CONST,
8825 .accessfn = access_aa64_tid3,
8826 .resetvalue = cpu->isar.id_aa64mmfr1 },
8827 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
8828 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8829 .access = PL1_R, .type = ARM_CP_CONST,
8830 .accessfn = access_aa64_tid3,
8831 .resetvalue = cpu->isar.id_aa64mmfr2 },
8832 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8833 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8834 .access = PL1_R, .type = ARM_CP_CONST,
8835 .accessfn = access_aa64_tid3,
8836 .resetvalue = 0 },
8837 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8838 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8839 .access = PL1_R, .type = ARM_CP_CONST,
8840 .accessfn = access_aa64_tid3,
8841 .resetvalue = 0 },
8842 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8843 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8844 .access = PL1_R, .type = ARM_CP_CONST,
8845 .accessfn = access_aa64_tid3,
8846 .resetvalue = 0 },
8847 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8848 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8849 .access = PL1_R, .type = ARM_CP_CONST,
8850 .accessfn = access_aa64_tid3,
8851 .resetvalue = 0 },
8852 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8853 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8854 .access = PL1_R, .type = ARM_CP_CONST,
8855 .accessfn = access_aa64_tid3,
8856 .resetvalue = 0 },
8857 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8858 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8859 .access = PL1_R, .type = ARM_CP_CONST,
8860 .accessfn = access_aa64_tid3,
8861 .resetvalue = cpu->isar.mvfr0 },
8862 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8863 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8864 .access = PL1_R, .type = ARM_CP_CONST,
8865 .accessfn = access_aa64_tid3,
8866 .resetvalue = cpu->isar.mvfr1 },
8867 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8868 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8869 .access = PL1_R, .type = ARM_CP_CONST,
8870 .accessfn = access_aa64_tid3,
8871 .resetvalue = cpu->isar.mvfr2 },
8872 /*
8873 * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8874 * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8875 * as RAZ, since it is in the "reserved for future ID
8876 * registers, RAZ" part of the AArch32 encoding space.
8877 */
8878 { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8879 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8880 .access = PL1_R, .type = ARM_CP_CONST,
8881 .accessfn = access_aa64_tid3,
8882 .resetvalue = 0 },
8883 { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8884 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8885 .access = PL1_R, .type = ARM_CP_CONST,
8886 .accessfn = access_aa64_tid3,
8887 .resetvalue = 0 },
8888 { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8889 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8890 .access = PL1_R, .type = ARM_CP_CONST,
8891 .accessfn = access_aa64_tid3,
8892 .resetvalue = 0 },
8893 /*
8894 * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8895 * they're also RAZ for AArch64, and in v8 are gradually
8896 * being filled with AArch64-view-of-AArch32-ID-register
8897 * for new ID registers.
8898 */
8899 { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
8900 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8901 .access = PL1_R, .type = ARM_CP_CONST,
8902 .accessfn = access_aa64_tid3,
8903 .resetvalue = 0 },
8904 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
8905 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8906 .access = PL1_R, .type = ARM_CP_CONST,
8907 .accessfn = access_aa64_tid3,
8908 .resetvalue = cpu->isar.id_pfr2 },
8909 { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
8910 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8911 .access = PL1_R, .type = ARM_CP_CONST,
8912 .accessfn = access_aa64_tid3,
8913 .resetvalue = cpu->isar.id_dfr1 },
8914 { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
8915 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8916 .access = PL1_R, .type = ARM_CP_CONST,
8917 .accessfn = access_aa64_tid3,
8918 .resetvalue = cpu->isar.id_mmfr5 },
8919 { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
8920 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8921 .access = PL1_R, .type = ARM_CP_CONST,
8922 .accessfn = access_aa64_tid3,
8923 .resetvalue = 0 },
8924 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8925 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8926 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8927 .fgt = FGT_PMCEIDN_EL0,
8928 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
8929 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8930 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8931 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8932 .fgt = FGT_PMCEIDN_EL0,
8933 .resetvalue = cpu->pmceid0 },
8934 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8935 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8936 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8937 .fgt = FGT_PMCEIDN_EL0,
8938 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
8939 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8940 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8941 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8942 .fgt = FGT_PMCEIDN_EL0,
8943 .resetvalue = cpu->pmceid1 },
8944 };
8945 #ifdef CONFIG_USER_ONLY
8946 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
8947 { .name = "ID_AA64PFR0_EL1",
8948 .exported_bits = R_ID_AA64PFR0_FP_MASK |
8949 R_ID_AA64PFR0_ADVSIMD_MASK |
8950 R_ID_AA64PFR0_SVE_MASK |
8951 R_ID_AA64PFR0_DIT_MASK,
8952 .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8953 (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
8954 { .name = "ID_AA64PFR1_EL1",
8955 .exported_bits = R_ID_AA64PFR1_BT_MASK |
8956 R_ID_AA64PFR1_SSBS_MASK |
8957 R_ID_AA64PFR1_MTE_MASK |
8958 R_ID_AA64PFR1_SME_MASK },
8959 { .name = "ID_AA64PFR*_EL1_RESERVED",
8960 .is_glob = true },
8961 { .name = "ID_AA64ZFR0_EL1",
8962 .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8963 R_ID_AA64ZFR0_AES_MASK |
8964 R_ID_AA64ZFR0_BITPERM_MASK |
8965 R_ID_AA64ZFR0_BFLOAT16_MASK |
8966 R_ID_AA64ZFR0_B16B16_MASK |
8967 R_ID_AA64ZFR0_SHA3_MASK |
8968 R_ID_AA64ZFR0_SM4_MASK |
8969 R_ID_AA64ZFR0_I8MM_MASK |
8970 R_ID_AA64ZFR0_F32MM_MASK |
8971 R_ID_AA64ZFR0_F64MM_MASK },
8972 { .name = "ID_AA64SMFR0_EL1",
8973 .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8974 R_ID_AA64SMFR0_BI32I32_MASK |
8975 R_ID_AA64SMFR0_B16F32_MASK |
8976 R_ID_AA64SMFR0_F16F32_MASK |
8977 R_ID_AA64SMFR0_I8I32_MASK |
8978 R_ID_AA64SMFR0_F16F16_MASK |
8979 R_ID_AA64SMFR0_B16B16_MASK |
8980 R_ID_AA64SMFR0_I16I32_MASK |
8981 R_ID_AA64SMFR0_F64F64_MASK |
8982 R_ID_AA64SMFR0_I16I64_MASK |
8983 R_ID_AA64SMFR0_SMEVER_MASK |
8984 R_ID_AA64SMFR0_FA64_MASK },
8985 { .name = "ID_AA64MMFR0_EL1",
8986 .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8987 .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8988 (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8989 { .name = "ID_AA64MMFR1_EL1",
8990 .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8991 { .name = "ID_AA64MMFR2_EL1",
8992 .exported_bits = R_ID_AA64MMFR2_AT_MASK },
8993 { .name = "ID_AA64MMFR*_EL1_RESERVED",
8994 .is_glob = true },
8995 { .name = "ID_AA64DFR0_EL1",
8996 .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8997 { .name = "ID_AA64DFR1_EL1" },
8998 { .name = "ID_AA64DFR*_EL1_RESERVED",
8999 .is_glob = true },
9000 { .name = "ID_AA64AFR*",
9001 .is_glob = true },
9002 { .name = "ID_AA64ISAR0_EL1",
9003 .exported_bits = R_ID_AA64ISAR0_AES_MASK |
9004 R_ID_AA64ISAR0_SHA1_MASK |
9005 R_ID_AA64ISAR0_SHA2_MASK |
9006 R_ID_AA64ISAR0_CRC32_MASK |
9007 R_ID_AA64ISAR0_ATOMIC_MASK |
9008 R_ID_AA64ISAR0_RDM_MASK |
9009 R_ID_AA64ISAR0_SHA3_MASK |
9010 R_ID_AA64ISAR0_SM3_MASK |
9011 R_ID_AA64ISAR0_SM4_MASK |
9012 R_ID_AA64ISAR0_DP_MASK |
9013 R_ID_AA64ISAR0_FHM_MASK |
9014 R_ID_AA64ISAR0_TS_MASK |
9015 R_ID_AA64ISAR0_RNDR_MASK },
9016 { .name = "ID_AA64ISAR1_EL1",
9017 .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
9018 R_ID_AA64ISAR1_APA_MASK |
9019 R_ID_AA64ISAR1_API_MASK |
9020 R_ID_AA64ISAR1_JSCVT_MASK |
9021 R_ID_AA64ISAR1_FCMA_MASK |
9022 R_ID_AA64ISAR1_LRCPC_MASK |
9023 R_ID_AA64ISAR1_GPA_MASK |
9024 R_ID_AA64ISAR1_GPI_MASK |
9025 R_ID_AA64ISAR1_FRINTTS_MASK |
9026 R_ID_AA64ISAR1_SB_MASK |
9027 R_ID_AA64ISAR1_BF16_MASK |
9028 R_ID_AA64ISAR1_DGH_MASK |
9029 R_ID_AA64ISAR1_I8MM_MASK },
9030 { .name = "ID_AA64ISAR2_EL1",
9031 .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
9032 R_ID_AA64ISAR2_RPRES_MASK |
9033 R_ID_AA64ISAR2_GPA3_MASK |
9034 R_ID_AA64ISAR2_APA3_MASK |
9035 R_ID_AA64ISAR2_MOPS_MASK |
9036 R_ID_AA64ISAR2_BC_MASK |
9037 R_ID_AA64ISAR2_RPRFM_MASK |
9038 R_ID_AA64ISAR2_CSSC_MASK },
9039 { .name = "ID_AA64ISAR*_EL1_RESERVED",
9040 .is_glob = true },
9041 };
9042 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
9043 #endif
9044 /*
9045 * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
9046 * TODO: For RMR, a write with bit 1 set should do something with
9047 * cpu_reset(). In the meantime, "the bit is strictly a request",
9048 * so we are in spec just ignoring writes.
9049 */
9050 if (!arm_feature(env, ARM_FEATURE_EL3) &&
9051 !arm_feature(env, ARM_FEATURE_EL2)) {
9052 ARMCPRegInfo el1_reset_regs[] = {
9053 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
9054 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
9055 .access = PL1_R,
9056 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9057 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
9058 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
9059 .access = PL1_RW, .type = ARM_CP_CONST,
9060 .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
9061 };
9062 define_arm_cp_regs(cpu, el1_reset_regs);
9063 }
9064 define_arm_cp_regs(cpu, v8_idregs);
9065 define_arm_cp_regs(cpu, v8_cp_reginfo);
9066 if (cpu_isar_feature(aa64_aa32_el1, cpu)) {
9067 define_arm_cp_regs(cpu, v8_aa32_el1_reginfo);
9068 }
9069
9070 for (i = 4; i < 16; i++) {
9071 /*
9072 * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
9073 * For pre-v8 cores there are RAZ patterns for these in
9074 * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
9075 * v8 extends the "must RAZ" part of the ID register space
9076 * to also cover c0, 0, c{8-15}, {0-7}.
9077 * These are STATE_AA32 because in the AArch64 sysreg space
9078 * c4-c7 is where the AArch64 ID registers live (and we've
9079 * already defined those in v8_idregs[]), and c8-c15 are not
9080 * "must RAZ" for AArch64.
9081 */
9082 g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
9083 ARMCPRegInfo v8_aa32_raz_idregs = {
9084 .name = name,
9085 .state = ARM_CP_STATE_AA32,
9086 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
9087 .access = PL1_R, .type = ARM_CP_CONST,
9088 .accessfn = access_aa64_tid3,
9089 .resetvalue = 0 };
9090 define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
9091 }
9092 }
9093
9094 /*
9095 * Register the base EL2 cpregs.
9096 * Pre v8, these registers are implemented only as part of the
9097 * Virtualization Extensions (EL2 present). Beginning with v8,
9098 * if EL2 is missing but EL3 is enabled, mostly these become
9099 * RES0 from EL3, with some specific exceptions.
9100 */
9101 if (arm_feature(env, ARM_FEATURE_EL2)
9102 || (arm_feature(env, ARM_FEATURE_EL3)
9103 && arm_feature(env, ARM_FEATURE_V8))) {
9104 uint64_t vmpidr_def = mpidr_read_val(env);
9105 ARMCPRegInfo vpidr_regs[] = {
9106 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
9107 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
9108 .access = PL2_RW, .accessfn = access_el3_aa32ns,
9109 .resetvalue = cpu->midr,
9110 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
9111 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
9112 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
9113 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
9114 .access = PL2_RW, .resetvalue = cpu->midr,
9115 .type = ARM_CP_EL3_NO_EL2_C_NZ,
9116 .nv2_redirect_offset = 0x88,
9117 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
9118 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
9119 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
9120 .access = PL2_RW, .accessfn = access_el3_aa32ns,
9121 .resetvalue = vmpidr_def,
9122 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
9123 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
9124 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
9125 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
9126 .access = PL2_RW, .resetvalue = vmpidr_def,
9127 .type = ARM_CP_EL3_NO_EL2_C_NZ,
9128 .nv2_redirect_offset = 0x50,
9129 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
9130 };
9131 /*
9132 * The only field of MDCR_EL2 that has a defined architectural reset
9133 * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
9134 */
9135 ARMCPRegInfo mdcr_el2 = {
9136 .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
9137 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
9138 .writefn = mdcr_el2_write,
9139 .access = PL2_RW, .resetvalue = pmu_num_counters(env),
9140 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
9141 };
9142 define_one_arm_cp_reg(cpu, &mdcr_el2);
9143 define_arm_cp_regs(cpu, vpidr_regs);
9144 define_arm_cp_regs(cpu, el2_cp_reginfo);
9145 if (arm_feature(env, ARM_FEATURE_V8)) {
9146 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
9147 }
9148 if (cpu_isar_feature(aa64_sel2, cpu)) {
9149 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
9150 }
9151 /*
9152 * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
9153 * See commentary near RMR_EL1.
9154 */
9155 if (!arm_feature(env, ARM_FEATURE_EL3)) {
9156 static const ARMCPRegInfo el2_reset_regs[] = {
9157 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
9158 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
9159 .access = PL2_R,
9160 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9161 { .name = "RVBAR", .type = ARM_CP_ALIAS,
9162 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
9163 .access = PL2_R,
9164 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9165 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
9166 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
9167 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
9168 };
9169 define_arm_cp_regs(cpu, el2_reset_regs);
9170 }
9171 }
9172
9173 /* Register the base EL3 cpregs. */
9174 if (arm_feature(env, ARM_FEATURE_EL3)) {
9175 define_arm_cp_regs(cpu, el3_cp_reginfo);
9176 ARMCPRegInfo el3_regs[] = {
9177 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
9178 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
9179 .access = PL3_R,
9180 .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
9181 { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
9182 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
9183 .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
9184 { .name = "RMR", .state = ARM_CP_STATE_AA32,
9185 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
9186 .access = PL3_RW, .type = ARM_CP_CONST,
9187 .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
9188 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
9189 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
9190 .access = PL3_RW,
9191 .raw_writefn = raw_write, .writefn = sctlr_write,
9192 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
9193 .resetvalue = cpu->reset_sctlr },
9194 };
9195
9196 define_arm_cp_regs(cpu, el3_regs);
9197 }
9198 /*
9199 * The behaviour of NSACR is sufficiently various that we don't
9200 * try to describe it in a single reginfo:
9201 * if EL3 is 64 bit, then trap to EL3 from S EL1,
9202 * reads as constant 0xc00 from NS EL1 and NS EL2
9203 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
9204 * if v7 without EL3, register doesn't exist
9205 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
9206 */
9207 if (arm_feature(env, ARM_FEATURE_EL3)) {
9208 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
9209 static const ARMCPRegInfo nsacr = {
9210 .name = "NSACR", .type = ARM_CP_CONST,
9211 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9212 .access = PL1_RW, .accessfn = nsacr_access,
9213 .resetvalue = 0xc00
9214 };
9215 define_one_arm_cp_reg(cpu, &nsacr);
9216 } else {
9217 static const ARMCPRegInfo nsacr = {
9218 .name = "NSACR",
9219 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9220 .access = PL3_RW | PL1_R,
9221 .resetvalue = 0,
9222 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
9223 };
9224 define_one_arm_cp_reg(cpu, &nsacr);
9225 }
9226 } else {
9227 if (arm_feature(env, ARM_FEATURE_V8)) {
9228 static const ARMCPRegInfo nsacr = {
9229 .name = "NSACR", .type = ARM_CP_CONST,
9230 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9231 .access = PL1_R,
9232 .resetvalue = 0xc00
9233 };
9234 define_one_arm_cp_reg(cpu, &nsacr);
9235 }
9236 }
9237
9238 if (arm_feature(env, ARM_FEATURE_PMSA)) {
9239 if (arm_feature(env, ARM_FEATURE_V6)) {
9240 /* PMSAv6 not implemented */
9241 assert(arm_feature(env, ARM_FEATURE_V7));
9242 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
9243 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
9244 } else {
9245 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
9246 }
9247 } else {
9248 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
9249 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
9250 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
9251 if (cpu_isar_feature(aa32_hpd, cpu)) {
9252 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
9253 }
9254 }
9255 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
9256 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
9257 }
9258 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
9259 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
9260 }
9261 if (arm_feature(env, ARM_FEATURE_VAPA)) {
9262 ARMCPRegInfo vapa_cp_reginfo[] = {
9263 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
9264 .access = PL1_RW, .resetvalue = 0,
9265 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
9266 offsetoflow32(CPUARMState, cp15.par_ns) },
9267 .writefn = par_write},
9268 #ifndef CONFIG_USER_ONLY
9269 /* This underdecoding is safe because the reginfo is NO_RAW. */
9270 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
9271 .access = PL1_W, .accessfn = ats_access,
9272 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
9273 #endif
9274 };
9275
9276 /*
9277 * When LPAE exists this 32-bit PAR register is an alias of the
9278 * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
9279 */
9280 if (arm_feature(env, ARM_FEATURE_LPAE)) {
9281 vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
9282 }
9283 define_arm_cp_regs(cpu, vapa_cp_reginfo);
9284 }
9285 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
9286 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
9287 }
9288 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
9289 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
9290 }
9291 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
9292 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
9293 }
9294 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
9295 define_arm_cp_regs(cpu, omap_cp_reginfo);
9296 }
9297 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
9298 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
9299 }
9300 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9301 define_arm_cp_regs(cpu, xscale_cp_reginfo);
9302 }
9303 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
9304 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
9305 }
9306 if (arm_feature(env, ARM_FEATURE_LPAE)) {
9307 define_arm_cp_regs(cpu, lpae_cp_reginfo);
9308 }
9309 if (cpu_isar_feature(aa32_jazelle, cpu)) {
9310 define_arm_cp_regs(cpu, jazelle_regs);
9311 }
9312 /*
9313 * Slightly awkwardly, the OMAP and StrongARM cores need all of
9314 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
9315 * be read-only (ie write causes UNDEF exception).
9316 */
9317 {
9318 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
9319 /*
9320 * Pre-v8 MIDR space.
9321 * Note that the MIDR isn't a simple constant register because
9322 * of the TI925 behaviour where writes to another register can
9323 * cause the MIDR value to change.
9324 *
9325 * Unimplemented registers in the c15 0 0 0 space default to
9326 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
9327 * and friends override accordingly.
9328 */
9329 { .name = "MIDR",
9330 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
9331 .access = PL1_R, .resetvalue = cpu->midr,
9332 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
9333 .readfn = midr_read,
9334 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9335 .type = ARM_CP_OVERRIDE },
9336 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
9337 { .name = "DUMMY",
9338 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
9339 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9340 { .name = "DUMMY",
9341 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
9342 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9343 { .name = "DUMMY",
9344 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
9345 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9346 { .name = "DUMMY",
9347 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
9348 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9349 { .name = "DUMMY",
9350 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
9351 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9352 };
9353 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
9354 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
9355 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
9356 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
9357 .fgt = FGT_MIDR_EL1,
9358 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9359 .readfn = midr_read },
9360 /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
9361 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
9362 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
9363 .access = PL1_R, .resetvalue = cpu->midr },
9364 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
9365 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
9366 .access = PL1_R,
9367 .accessfn = access_aa64_tid1,
9368 .fgt = FGT_REVIDR_EL1,
9369 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
9370 };
9371 ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
9372 .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
9373 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9374 .access = PL1_R, .resetvalue = cpu->midr
9375 };
9376 ARMCPRegInfo id_cp_reginfo[] = {
9377 /* These are common to v8 and pre-v8 */
9378 { .name = "CTR",
9379 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
9380 .access = PL1_R, .accessfn = ctr_el0_access,
9381 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9382 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
9383 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
9384 .access = PL0_R, .accessfn = ctr_el0_access,
9385 .fgt = FGT_CTR_EL0,
9386 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9387 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
9388 { .name = "TCMTR",
9389 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
9390 .access = PL1_R,
9391 .accessfn = access_aa32_tid1,
9392 .type = ARM_CP_CONST, .resetvalue = 0 },
9393 };
9394 /* TLBTR is specific to VMSA */
9395 ARMCPRegInfo id_tlbtr_reginfo = {
9396 .name = "TLBTR",
9397 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
9398 .access = PL1_R,
9399 .accessfn = access_aa32_tid1,
9400 .type = ARM_CP_CONST, .resetvalue = 0,
9401 };
9402 /* MPUIR is specific to PMSA V6+ */
9403 ARMCPRegInfo id_mpuir_reginfo = {
9404 .name = "MPUIR",
9405 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9406 .access = PL1_R, .type = ARM_CP_CONST,
9407 .resetvalue = cpu->pmsav7_dregion << 8
9408 };
9409 /* HMPUIR is specific to PMSA V8 */
9410 ARMCPRegInfo id_hmpuir_reginfo = {
9411 .name = "HMPUIR",
9412 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
9413 .access = PL2_R, .type = ARM_CP_CONST,
9414 .resetvalue = cpu->pmsav8r_hdregion
9415 };
9416 static const ARMCPRegInfo crn0_wi_reginfo = {
9417 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
9418 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
9419 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
9420 };
9421 #ifdef CONFIG_USER_ONLY
9422 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
9423 { .name = "MIDR_EL1",
9424 .exported_bits = R_MIDR_EL1_REVISION_MASK |
9425 R_MIDR_EL1_PARTNUM_MASK |
9426 R_MIDR_EL1_ARCHITECTURE_MASK |
9427 R_MIDR_EL1_VARIANT_MASK |
9428 R_MIDR_EL1_IMPLEMENTER_MASK },
9429 { .name = "REVIDR_EL1" },
9430 };
9431 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
9432 #endif
9433 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
9434 arm_feature(env, ARM_FEATURE_STRONGARM)) {
9435 size_t i;
9436 /*
9437 * Register the blanket "writes ignored" value first to cover the
9438 * whole space. Then update the specific ID registers to allow write
9439 * access, so that they ignore writes rather than causing them to
9440 * UNDEF.
9441 */
9442 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
9443 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
9444 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
9445 }
9446 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
9447 id_cp_reginfo[i].access = PL1_RW;
9448 }
9449 id_mpuir_reginfo.access = PL1_RW;
9450 id_tlbtr_reginfo.access = PL1_RW;
9451 }
9452 if (arm_feature(env, ARM_FEATURE_V8)) {
9453 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
9454 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9455 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
9456 }
9457 } else {
9458 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
9459 }
9460 define_arm_cp_regs(cpu, id_cp_reginfo);
9461 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9462 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
9463 } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
9464 arm_feature(env, ARM_FEATURE_V8)) {
9465 uint32_t i = 0;
9466 char *tmp_string;
9467
9468 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9469 define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
9470 define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
9471
9472 /* Register alias is only valid for first 32 indexes */
9473 for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
9474 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9475 uint8_t opc1 = extract32(i, 4, 1);
9476 uint8_t opc2 = extract32(i, 0, 1) << 2;
9477
9478 tmp_string = g_strdup_printf("PRBAR%u", i);
9479 ARMCPRegInfo tmp_prbarn_reginfo = {
9480 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9481 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9482 .access = PL1_RW, .resetvalue = 0,
9483 .accessfn = access_tvm_trvm,
9484 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9485 };
9486 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
9487 g_free(tmp_string);
9488
9489 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9490 tmp_string = g_strdup_printf("PRLAR%u", i);
9491 ARMCPRegInfo tmp_prlarn_reginfo = {
9492 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9493 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9494 .access = PL1_RW, .resetvalue = 0,
9495 .accessfn = access_tvm_trvm,
9496 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9497 };
9498 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
9499 g_free(tmp_string);
9500 }
9501
9502 /* Register alias is only valid for first 32 indexes */
9503 for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
9504 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9505 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
9506 uint8_t opc2 = extract32(i, 0, 1) << 2;
9507
9508 tmp_string = g_strdup_printf("HPRBAR%u", i);
9509 ARMCPRegInfo tmp_hprbarn_reginfo = {
9510 .name = tmp_string,
9511 .type = ARM_CP_NO_RAW,
9512 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9513 .access = PL2_RW, .resetvalue = 0,
9514 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9515 };
9516 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
9517 g_free(tmp_string);
9518
9519 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9520 tmp_string = g_strdup_printf("HPRLAR%u", i);
9521 ARMCPRegInfo tmp_hprlarn_reginfo = {
9522 .name = tmp_string,
9523 .type = ARM_CP_NO_RAW,
9524 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9525 .access = PL2_RW, .resetvalue = 0,
9526 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9527 };
9528 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
9529 g_free(tmp_string);
9530 }
9531 } else if (arm_feature(env, ARM_FEATURE_V7)) {
9532 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9533 }
9534 }
9535
9536 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
9537 ARMCPRegInfo mpidr_cp_reginfo[] = {
9538 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
9539 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
9540 .fgt = FGT_MPIDR_EL1,
9541 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
9542 };
9543 #ifdef CONFIG_USER_ONLY
9544 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
9545 { .name = "MPIDR_EL1",
9546 .fixed_bits = 0x0000000080000000 },
9547 };
9548 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
9549 #endif
9550 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
9551 }
9552
9553 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
9554 ARMCPRegInfo auxcr_reginfo[] = {
9555 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
9556 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
9557 .access = PL1_RW, .accessfn = access_tacr,
9558 .nv2_redirect_offset = 0x118,
9559 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
9560 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
9561 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
9562 .access = PL2_RW, .type = ARM_CP_CONST,
9563 .resetvalue = 0 },
9564 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
9565 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
9566 .access = PL3_RW, .type = ARM_CP_CONST,
9567 .resetvalue = 0 },
9568 };
9569 define_arm_cp_regs(cpu, auxcr_reginfo);
9570 if (cpu_isar_feature(aa32_ac2, cpu)) {
9571 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
9572 }
9573 }
9574
9575 if (arm_feature(env, ARM_FEATURE_CBAR)) {
9576 /*
9577 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
9578 * There are two flavours:
9579 * (1) older 32-bit only cores have a simple 32-bit CBAR
9580 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
9581 * 32-bit register visible to AArch32 at a different encoding
9582 * to the "flavour 1" register and with the bits rearranged to
9583 * be able to squash a 64-bit address into the 32-bit view.
9584 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
9585 * in future if we support AArch32-only configs of some of the
9586 * AArch64 cores we might need to add a specific feature flag
9587 * to indicate cores with "flavour 2" CBAR.
9588 */
9589 if (arm_feature(env, ARM_FEATURE_V8)) {
9590 /* 32 bit view is [31:18] 0...0 [43:32]. */
9591 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
9592 | extract64(cpu->reset_cbar, 32, 12);
9593 ARMCPRegInfo cbar_reginfo[] = {
9594 { .name = "CBAR",
9595 .type = ARM_CP_CONST,
9596 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
9597 .access = PL1_R, .resetvalue = cbar32 },
9598 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
9599 .type = ARM_CP_CONST,
9600 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
9601 .access = PL1_R, .resetvalue = cpu->reset_cbar },
9602 };
9603 /* We don't implement a r/w 64 bit CBAR currently */
9604 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
9605 define_arm_cp_regs(cpu, cbar_reginfo);
9606 } else {
9607 ARMCPRegInfo cbar = {
9608 .name = "CBAR",
9609 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
9610 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
9611 .fieldoffset = offsetof(CPUARMState,
9612 cp15.c15_config_base_address)
9613 };
9614 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
9615 cbar.access = PL1_R;
9616 cbar.fieldoffset = 0;
9617 cbar.type = ARM_CP_CONST;
9618 }
9619 define_one_arm_cp_reg(cpu, &cbar);
9620 }
9621 }
9622
9623 if (arm_feature(env, ARM_FEATURE_VBAR)) {
9624 static const ARMCPRegInfo vbar_cp_reginfo[] = {
9625 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
9626 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
9627 .access = PL1_RW, .writefn = vbar_write,
9628 .accessfn = access_nv1,
9629 .fgt = FGT_VBAR_EL1,
9630 .nv2_redirect_offset = 0x250 | NV2_REDIR_NV1,
9631 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
9632 offsetof(CPUARMState, cp15.vbar_ns) },
9633 .resetvalue = 0 },
9634 };
9635 define_arm_cp_regs(cpu, vbar_cp_reginfo);
9636 }
9637
9638 /* Generic registers whose values depend on the implementation */
9639 {
9640 ARMCPRegInfo sctlr = {
9641 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
9642 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
9643 .access = PL1_RW, .accessfn = access_tvm_trvm,
9644 .fgt = FGT_SCTLR_EL1,
9645 .nv2_redirect_offset = 0x110 | NV2_REDIR_NV1,
9646 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
9647 offsetof(CPUARMState, cp15.sctlr_ns) },
9648 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
9649 .raw_writefn = raw_write,
9650 };
9651 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9652 /*
9653 * Normally we would always end the TB on an SCTLR write, but Linux
9654 * arch/arm/mach-pxa/sleep.S expects two instructions following
9655 * an MMU enable to execute from cache. Imitate this behaviour.
9656 */
9657 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
9658 }
9659 define_one_arm_cp_reg(cpu, &sctlr);
9660
9661 if (arm_feature(env, ARM_FEATURE_PMSA) &&
9662 arm_feature(env, ARM_FEATURE_V8)) {
9663 ARMCPRegInfo vsctlr = {
9664 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
9665 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
9666 .access = PL2_RW, .resetvalue = 0x0,
9667 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
9668 };
9669 define_one_arm_cp_reg(cpu, &vsctlr);
9670 }
9671 }
9672
9673 if (cpu_isar_feature(aa64_lor, cpu)) {
9674 define_arm_cp_regs(cpu, lor_reginfo);
9675 }
9676 if (cpu_isar_feature(aa64_pan, cpu)) {
9677 define_one_arm_cp_reg(cpu, &pan_reginfo);
9678 }
9679 #ifndef CONFIG_USER_ONLY
9680 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
9681 define_arm_cp_regs(cpu, ats1e1_reginfo);
9682 }
9683 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
9684 define_arm_cp_regs(cpu, ats1cp_reginfo);
9685 }
9686 #endif
9687 if (cpu_isar_feature(aa64_uao, cpu)) {
9688 define_one_arm_cp_reg(cpu, &uao_reginfo);
9689 }
9690
9691 if (cpu_isar_feature(aa64_dit, cpu)) {
9692 define_one_arm_cp_reg(cpu, &dit_reginfo);
9693 }
9694 if (cpu_isar_feature(aa64_ssbs, cpu)) {
9695 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
9696 }
9697 if (cpu_isar_feature(any_ras, cpu)) {
9698 define_arm_cp_regs(cpu, minimal_ras_reginfo);
9699 }
9700
9701 if (cpu_isar_feature(aa64_vh, cpu) ||
9702 cpu_isar_feature(aa64_debugv8p2, cpu)) {
9703 define_one_arm_cp_reg(cpu, &contextidr_el2);
9704 }
9705 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9706 define_arm_cp_regs(cpu, vhe_reginfo);
9707 }
9708
9709 if (cpu_isar_feature(aa64_sve, cpu)) {
9710 define_arm_cp_regs(cpu, zcr_reginfo);
9711 }
9712
9713 if (cpu_isar_feature(aa64_hcx, cpu)) {
9714 define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
9715 }
9716
9717 #ifdef TARGET_AARCH64
9718 if (cpu_isar_feature(aa64_sme, cpu)) {
9719 define_arm_cp_regs(cpu, sme_reginfo);
9720 }
9721 if (cpu_isar_feature(aa64_pauth, cpu)) {
9722 define_arm_cp_regs(cpu, pauth_reginfo);
9723 }
9724 if (cpu_isar_feature(aa64_rndr, cpu)) {
9725 define_arm_cp_regs(cpu, rndr_reginfo);
9726 }
9727 if (cpu_isar_feature(aa64_tlbirange, cpu)) {
9728 define_arm_cp_regs(cpu, tlbirange_reginfo);
9729 }
9730 if (cpu_isar_feature(aa64_tlbios, cpu)) {
9731 define_arm_cp_regs(cpu, tlbios_reginfo);
9732 }
9733 /* Data Cache clean instructions up to PoP */
9734 if (cpu_isar_feature(aa64_dcpop, cpu)) {
9735 define_one_arm_cp_reg(cpu, dcpop_reg);
9736
9737 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
9738 define_one_arm_cp_reg(cpu, dcpodp_reg);
9739 }
9740 }
9741
9742 /*
9743 * If full MTE is enabled, add all of the system registers.
9744 * If only "instructions available at EL0" are enabled,
9745 * then define only a RAZ/WI version of PSTATE.TCO.
9746 */
9747 if (cpu_isar_feature(aa64_mte, cpu)) {
9748 ARMCPRegInfo gmid_reginfo = {
9749 .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
9750 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
9751 .access = PL1_R, .accessfn = access_aa64_tid5,
9752 .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
9753 };
9754 define_one_arm_cp_reg(cpu, &gmid_reginfo);
9755 define_arm_cp_regs(cpu, mte_reginfo);
9756 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9757 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
9758 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
9759 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9760 }
9761
9762 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
9763 define_arm_cp_regs(cpu, scxtnum_reginfo);
9764 }
9765
9766 if (cpu_isar_feature(aa64_fgt, cpu)) {
9767 define_arm_cp_regs(cpu, fgt_reginfo);
9768 }
9769
9770 if (cpu_isar_feature(aa64_rme, cpu)) {
9771 define_arm_cp_regs(cpu, rme_reginfo);
9772 if (cpu_isar_feature(aa64_mte, cpu)) {
9773 define_arm_cp_regs(cpu, rme_mte_reginfo);
9774 }
9775 }
9776
9777 if (cpu_isar_feature(aa64_nv2, cpu)) {
9778 define_arm_cp_regs(cpu, nv2_reginfo);
9779 }
9780 #endif
9781
9782 if (cpu_isar_feature(any_predinv, cpu)) {
9783 define_arm_cp_regs(cpu, predinv_reginfo);
9784 }
9785
9786 if (cpu_isar_feature(any_ccidx, cpu)) {
9787 define_arm_cp_regs(cpu, ccsidr2_reginfo);
9788 }
9789
9790 #ifndef CONFIG_USER_ONLY
9791 /*
9792 * Register redirections and aliases must be done last,
9793 * after the registers from the other extensions have been defined.
9794 */
9795 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9796 define_arm_vh_e2h_redirects_aliases(cpu);
9797 }
9798 #endif
9799 }
9800
9801 /*
9802 * Private utility function for define_one_arm_cp_reg_with_opaque():
9803 * add a single reginfo struct to the hash table.
9804 */
9805 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
9806 void *opaque, CPState state,
9807 CPSecureState secstate,
9808 int crm, int opc1, int opc2,
9809 const char *name)
9810 {
9811 CPUARMState *env = &cpu->env;
9812 uint32_t key;
9813 ARMCPRegInfo *r2;
9814 bool is64 = r->type & ARM_CP_64BIT;
9815 bool ns = secstate & ARM_CP_SECSTATE_NS;
9816 int cp = r->cp;
9817 size_t name_len;
9818 bool make_const;
9819
9820 switch (state) {
9821 case ARM_CP_STATE_AA32:
9822 /* We assume it is a cp15 register if the .cp field is left unset. */
9823 if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9824 cp = 15;
9825 }
9826 key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9827 break;
9828 case ARM_CP_STATE_AA64:
9829 /*
9830 * To allow abbreviation of ARMCPRegInfo definitions, we treat
9831 * cp == 0 as equivalent to the value for "standard guest-visible
9832 * sysreg". STATE_BOTH definitions are also always "standard sysreg"
9833 * in their AArch64 view (the .cp value may be non-zero for the
9834 * benefit of the AArch32 view).
9835 */
9836 if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9837 cp = CP_REG_ARM64_SYSREG_CP;
9838 }
9839 key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9840 break;
9841 default:
9842 g_assert_not_reached();
9843 }
9844
9845 /* Overriding of an existing definition must be explicitly requested. */
9846 if (!(r->type & ARM_CP_OVERRIDE)) {
9847 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9848 if (oldreg) {
9849 assert(oldreg->type & ARM_CP_OVERRIDE);
9850 }
9851 }
9852
9853 /*
9854 * Eliminate registers that are not present because the EL is missing.
9855 * Doing this here makes it easier to put all registers for a given
9856 * feature into the same ARMCPRegInfo array and define them all at once.
9857 */
9858 make_const = false;
9859 if (arm_feature(env, ARM_FEATURE_EL3)) {
9860 /*
9861 * An EL2 register without EL2 but with EL3 is (usually) RES0.
9862 * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9863 */
9864 int min_el = ctz32(r->access) / 2;
9865 if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9866 if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9867 return;
9868 }
9869 make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9870 }
9871 } else {
9872 CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9873 ? PL2_RW : PL1_RW);
9874 if ((r->access & max_el) == 0) {
9875 return;
9876 }
9877 }
9878
9879 /* Combine cpreg and name into one allocation. */
9880 name_len = strlen(name) + 1;
9881 r2 = g_malloc(sizeof(*r2) + name_len);
9882 *r2 = *r;
9883 r2->name = memcpy(r2 + 1, name, name_len);
9884
9885 /*
9886 * Update fields to match the instantiation, overwiting wildcards
9887 * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
9888 */
9889 r2->cp = cp;
9890 r2->crm = crm;
9891 r2->opc1 = opc1;
9892 r2->opc2 = opc2;
9893 r2->state = state;
9894 r2->secure = secstate;
9895 if (opaque) {
9896 r2->opaque = opaque;
9897 }
9898
9899 if (make_const) {
9900 /* This should not have been a very special register to begin. */
9901 int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9902 assert(old_special == 0 || old_special == ARM_CP_NOP);
9903 /*
9904 * Set the special function to CONST, retaining the other flags.
9905 * This is important for e.g. ARM_CP_SVE so that we still
9906 * take the SVE trap if CPTR_EL3.EZ == 0.
9907 */
9908 r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9909 /*
9910 * Usually, these registers become RES0, but there are a few
9911 * special cases like VPIDR_EL2 which have a constant non-zero
9912 * value with writes ignored.
9913 */
9914 if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9915 r2->resetvalue = 0;
9916 }
9917 /*
9918 * ARM_CP_CONST has precedence, so removing the callbacks and
9919 * offsets are not strictly necessary, but it is potentially
9920 * less confusing to debug later.
9921 */
9922 r2->readfn = NULL;
9923 r2->writefn = NULL;
9924 r2->raw_readfn = NULL;
9925 r2->raw_writefn = NULL;
9926 r2->resetfn = NULL;
9927 r2->fieldoffset = 0;
9928 r2->bank_fieldoffsets[0] = 0;
9929 r2->bank_fieldoffsets[1] = 0;
9930 } else {
9931 bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
9932
9933 if (isbanked) {
9934 /*
9935 * Register is banked (using both entries in array).
9936 * Overwriting fieldoffset as the array is only used to define
9937 * banked registers but later only fieldoffset is used.
9938 */
9939 r2->fieldoffset = r->bank_fieldoffsets[ns];
9940 }
9941 if (state == ARM_CP_STATE_AA32) {
9942 if (isbanked) {
9943 /*
9944 * If the register is banked then we don't need to migrate or
9945 * reset the 32-bit instance in certain cases:
9946 *
9947 * 1) If the register has both 32-bit and 64-bit instances
9948 * then we can count on the 64-bit instance taking care
9949 * of the non-secure bank.
9950 * 2) If ARMv8 is enabled then we can count on a 64-bit
9951 * version taking care of the secure bank. This requires
9952 * that separate 32 and 64-bit definitions are provided.
9953 */
9954 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9955 (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9956 r2->type |= ARM_CP_ALIAS;
9957 }
9958 } else if ((secstate != r->secure) && !ns) {
9959 /*
9960 * The register is not banked so we only want to allow
9961 * migration of the non-secure instance.
9962 */
9963 r2->type |= ARM_CP_ALIAS;
9964 }
9965
9966 if (HOST_BIG_ENDIAN &&
9967 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9968 r2->fieldoffset += sizeof(uint32_t);
9969 }
9970 }
9971 }
9972
9973 /*
9974 * By convention, for wildcarded registers only the first
9975 * entry is used for migration; the others are marked as
9976 * ALIAS so we don't try to transfer the register
9977 * multiple times. Special registers (ie NOP/WFI) are
9978 * never migratable and not even raw-accessible.
9979 */
9980 if (r2->type & ARM_CP_SPECIAL_MASK) {
9981 r2->type |= ARM_CP_NO_RAW;
9982 }
9983 if (((r->crm == CP_ANY) && crm != 0) ||
9984 ((r->opc1 == CP_ANY) && opc1 != 0) ||
9985 ((r->opc2 == CP_ANY) && opc2 != 0)) {
9986 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
9987 }
9988
9989 /*
9990 * Check that raw accesses are either forbidden or handled. Note that
9991 * we can't assert this earlier because the setup of fieldoffset for
9992 * banked registers has to be done first.
9993 */
9994 if (!(r2->type & ARM_CP_NO_RAW)) {
9995 assert(!raw_accessors_invalid(r2));
9996 }
9997
9998 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
9999 }
10000
10001
10002 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
10003 const ARMCPRegInfo *r, void *opaque)
10004 {
10005 /*
10006 * Define implementations of coprocessor registers.
10007 * We store these in a hashtable because typically
10008 * there are less than 150 registers in a space which
10009 * is 16*16*16*8*8 = 262144 in size.
10010 * Wildcarding is supported for the crm, opc1 and opc2 fields.
10011 * If a register is defined twice then the second definition is
10012 * used, so this can be used to define some generic registers and
10013 * then override them with implementation specific variations.
10014 * At least one of the original and the second definition should
10015 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
10016 * against accidental use.
10017 *
10018 * The state field defines whether the register is to be
10019 * visible in the AArch32 or AArch64 execution state. If the
10020 * state is set to ARM_CP_STATE_BOTH then we synthesise a
10021 * reginfo structure for the AArch32 view, which sees the lower
10022 * 32 bits of the 64 bit register.
10023 *
10024 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
10025 * be wildcarded. AArch64 registers are always considered to be 64
10026 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
10027 * the register, if any.
10028 */
10029 int crm, opc1, opc2;
10030 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
10031 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
10032 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
10033 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
10034 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
10035 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
10036 CPState state;
10037
10038 /* 64 bit registers have only CRm and Opc1 fields */
10039 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
10040 /* op0 only exists in the AArch64 encodings */
10041 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
10042 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
10043 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
10044 /*
10045 * This API is only for Arm's system coprocessors (14 and 15) or
10046 * (M-profile or v7A-and-earlier only) for implementation defined
10047 * coprocessors in the range 0..7. Our decode assumes this, since
10048 * 8..13 can be used for other insns including VFP and Neon. See
10049 * valid_cp() in translate.c. Assert here that we haven't tried
10050 * to use an invalid coprocessor number.
10051 */
10052 switch (r->state) {
10053 case ARM_CP_STATE_BOTH:
10054 /* 0 has a special meaning, but otherwise the same rules as AA32. */
10055 if (r->cp == 0) {
10056 break;
10057 }
10058 /* fall through */
10059 case ARM_CP_STATE_AA32:
10060 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
10061 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
10062 assert(r->cp >= 14 && r->cp <= 15);
10063 } else {
10064 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
10065 }
10066 break;
10067 case ARM_CP_STATE_AA64:
10068 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
10069 break;
10070 default:
10071 g_assert_not_reached();
10072 }
10073 /*
10074 * The AArch64 pseudocode CheckSystemAccess() specifies that op1
10075 * encodes a minimum access level for the register. We roll this
10076 * runtime check into our general permission check code, so check
10077 * here that the reginfo's specified permissions are strict enough
10078 * to encompass the generic architectural permission check.
10079 */
10080 if (r->state != ARM_CP_STATE_AA32) {
10081 CPAccessRights mask;
10082 switch (r->opc1) {
10083 case 0:
10084 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
10085 mask = PL0U_R | PL1_RW;
10086 break;
10087 case 1: case 2:
10088 /* min_EL EL1 */
10089 mask = PL1_RW;
10090 break;
10091 case 3:
10092 /* min_EL EL0 */
10093 mask = PL0_RW;
10094 break;
10095 case 4:
10096 case 5:
10097 /* min_EL EL2 */
10098 mask = PL2_RW;
10099 break;
10100 case 6:
10101 /* min_EL EL3 */
10102 mask = PL3_RW;
10103 break;
10104 case 7:
10105 /* min_EL EL1, secure mode only (we don't check the latter) */
10106 mask = PL1_RW;
10107 break;
10108 default:
10109 /* broken reginfo with out-of-range opc1 */
10110 g_assert_not_reached();
10111 }
10112 /* assert our permissions are not too lax (stricter is fine) */
10113 assert((r->access & ~mask) == 0);
10114 }
10115
10116 /*
10117 * Check that the register definition has enough info to handle
10118 * reads and writes if they are permitted.
10119 */
10120 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
10121 if (r->access & PL3_R) {
10122 assert((r->fieldoffset ||
10123 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
10124 r->readfn);
10125 }
10126 if (r->access & PL3_W) {
10127 assert((r->fieldoffset ||
10128 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
10129 r->writefn);
10130 }
10131 }
10132
10133 for (crm = crmmin; crm <= crmmax; crm++) {
10134 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
10135 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
10136 for (state = ARM_CP_STATE_AA32;
10137 state <= ARM_CP_STATE_AA64; state++) {
10138 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
10139 continue;
10140 }
10141 if (state == ARM_CP_STATE_AA32) {
10142 /*
10143 * Under AArch32 CP registers can be common
10144 * (same for secure and non-secure world) or banked.
10145 */
10146 char *name;
10147
10148 switch (r->secure) {
10149 case ARM_CP_SECSTATE_S:
10150 case ARM_CP_SECSTATE_NS:
10151 add_cpreg_to_hashtable(cpu, r, opaque, state,
10152 r->secure, crm, opc1, opc2,
10153 r->name);
10154 break;
10155 case ARM_CP_SECSTATE_BOTH:
10156 name = g_strdup_printf("%s_S", r->name);
10157 add_cpreg_to_hashtable(cpu, r, opaque, state,
10158 ARM_CP_SECSTATE_S,
10159 crm, opc1, opc2, name);
10160 g_free(name);
10161 add_cpreg_to_hashtable(cpu, r, opaque, state,
10162 ARM_CP_SECSTATE_NS,
10163 crm, opc1, opc2, r->name);
10164 break;
10165 default:
10166 g_assert_not_reached();
10167 }
10168 } else {
10169 /*
10170 * AArch64 registers get mapped to non-secure instance
10171 * of AArch32
10172 */
10173 add_cpreg_to_hashtable(cpu, r, opaque, state,
10174 ARM_CP_SECSTATE_NS,
10175 crm, opc1, opc2, r->name);
10176 }
10177 }
10178 }
10179 }
10180 }
10181 }
10182
10183 /* Define a whole list of registers */
10184 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
10185 void *opaque, size_t len)
10186 {
10187 size_t i;
10188 for (i = 0; i < len; ++i) {
10189 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
10190 }
10191 }
10192
10193 /*
10194 * Modify ARMCPRegInfo for access from userspace.
10195 *
10196 * This is a data driven modification directed by
10197 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
10198 * user-space cannot alter any values and dynamic values pertaining to
10199 * execution state are hidden from user space view anyway.
10200 */
10201 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
10202 const ARMCPRegUserSpaceInfo *mods,
10203 size_t mods_len)
10204 {
10205 for (size_t mi = 0; mi < mods_len; ++mi) {
10206 const ARMCPRegUserSpaceInfo *m = mods + mi;
10207 GPatternSpec *pat = NULL;
10208
10209 if (m->is_glob) {
10210 pat = g_pattern_spec_new(m->name);
10211 }
10212 for (size_t ri = 0; ri < regs_len; ++ri) {
10213 ARMCPRegInfo *r = regs + ri;
10214
10215 if (pat && g_pattern_match_string(pat, r->name)) {
10216 r->type = ARM_CP_CONST;
10217 r->access = PL0U_R;
10218 r->resetvalue = 0;
10219 /* continue */
10220 } else if (strcmp(r->name, m->name) == 0) {
10221 r->type = ARM_CP_CONST;
10222 r->access = PL0U_R;
10223 r->resetvalue &= m->exported_bits;
10224 r->resetvalue |= m->fixed_bits;
10225 break;
10226 }
10227 }
10228 if (pat) {
10229 g_pattern_spec_free(pat);
10230 }
10231 }
10232 }
10233
10234 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
10235 {
10236 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
10237 }
10238
10239 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
10240 uint64_t value)
10241 {
10242 /* Helper coprocessor write function for write-ignore registers */
10243 }
10244
10245 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
10246 {
10247 /* Helper coprocessor write function for read-as-zero registers */
10248 return 0;
10249 }
10250
10251 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
10252 {
10253 /* Helper coprocessor reset function for do-nothing-on-reset registers */
10254 }
10255
10256 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
10257 {
10258 /*
10259 * Return true if it is not valid for us to switch to
10260 * this CPU mode (ie all the UNPREDICTABLE cases in
10261 * the ARM ARM CPSRWriteByInstr pseudocode).
10262 */
10263
10264 /* Changes to or from Hyp via MSR and CPS are illegal. */
10265 if (write_type == CPSRWriteByInstr &&
10266 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
10267 mode == ARM_CPU_MODE_HYP)) {
10268 return 1;
10269 }
10270
10271 switch (mode) {
10272 case ARM_CPU_MODE_USR:
10273 return 0;
10274 case ARM_CPU_MODE_SYS:
10275 case ARM_CPU_MODE_SVC:
10276 case ARM_CPU_MODE_ABT:
10277 case ARM_CPU_MODE_UND:
10278 case ARM_CPU_MODE_IRQ:
10279 case ARM_CPU_MODE_FIQ:
10280 /*
10281 * Note that we don't implement the IMPDEF NSACR.RFR which in v7
10282 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
10283 */
10284 /*
10285 * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
10286 * and CPS are treated as illegal mode changes.
10287 */
10288 if (write_type == CPSRWriteByInstr &&
10289 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
10290 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10291 return 1;
10292 }
10293 return 0;
10294 case ARM_CPU_MODE_HYP:
10295 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
10296 case ARM_CPU_MODE_MON:
10297 return arm_current_el(env) < 3;
10298 default:
10299 return 1;
10300 }
10301 }
10302
10303 uint32_t cpsr_read(CPUARMState *env)
10304 {
10305 int ZF;
10306 ZF = (env->ZF == 0);
10307 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
10308 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
10309 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
10310 | ((env->condexec_bits & 0xfc) << 8)
10311 | (env->GE << 16) | (env->daif & CPSR_AIF);
10312 }
10313
10314 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
10315 CPSRWriteType write_type)
10316 {
10317 uint32_t changed_daif;
10318 bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
10319 (mask & (CPSR_M | CPSR_E | CPSR_IL));
10320
10321 if (mask & CPSR_NZCV) {
10322 env->ZF = (~val) & CPSR_Z;
10323 env->NF = val;
10324 env->CF = (val >> 29) & 1;
10325 env->VF = (val << 3) & 0x80000000;
10326 }
10327 if (mask & CPSR_Q) {
10328 env->QF = ((val & CPSR_Q) != 0);
10329 }
10330 if (mask & CPSR_T) {
10331 env->thumb = ((val & CPSR_T) != 0);
10332 }
10333 if (mask & CPSR_IT_0_1) {
10334 env->condexec_bits &= ~3;
10335 env->condexec_bits |= (val >> 25) & 3;
10336 }
10337 if (mask & CPSR_IT_2_7) {
10338 env->condexec_bits &= 3;
10339 env->condexec_bits |= (val >> 8) & 0xfc;
10340 }
10341 if (mask & CPSR_GE) {
10342 env->GE = (val >> 16) & 0xf;
10343 }
10344
10345 /*
10346 * In a V7 implementation that includes the security extensions but does
10347 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
10348 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
10349 * bits respectively.
10350 *
10351 * In a V8 implementation, it is permitted for privileged software to
10352 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
10353 */
10354 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
10355 arm_feature(env, ARM_FEATURE_EL3) &&
10356 !arm_feature(env, ARM_FEATURE_EL2) &&
10357 !arm_is_secure(env)) {
10358
10359 changed_daif = (env->daif ^ val) & mask;
10360
10361 if (changed_daif & CPSR_A) {
10362 /*
10363 * Check to see if we are allowed to change the masking of async
10364 * abort exceptions from a non-secure state.
10365 */
10366 if (!(env->cp15.scr_el3 & SCR_AW)) {
10367 qemu_log_mask(LOG_GUEST_ERROR,
10368 "Ignoring attempt to switch CPSR_A flag from "
10369 "non-secure world with SCR.AW bit clear\n");
10370 mask &= ~CPSR_A;
10371 }
10372 }
10373
10374 if (changed_daif & CPSR_F) {
10375 /*
10376 * Check to see if we are allowed to change the masking of FIQ
10377 * exceptions from a non-secure state.
10378 */
10379 if (!(env->cp15.scr_el3 & SCR_FW)) {
10380 qemu_log_mask(LOG_GUEST_ERROR,
10381 "Ignoring attempt to switch CPSR_F flag from "
10382 "non-secure world with SCR.FW bit clear\n");
10383 mask &= ~CPSR_F;
10384 }
10385
10386 /*
10387 * Check whether non-maskable FIQ (NMFI) support is enabled.
10388 * If this bit is set software is not allowed to mask
10389 * FIQs, but is allowed to set CPSR_F to 0.
10390 */
10391 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
10392 (val & CPSR_F)) {
10393 qemu_log_mask(LOG_GUEST_ERROR,
10394 "Ignoring attempt to enable CPSR_F flag "
10395 "(non-maskable FIQ [NMFI] support enabled)\n");
10396 mask &= ~CPSR_F;
10397 }
10398 }
10399 }
10400
10401 env->daif &= ~(CPSR_AIF & mask);
10402 env->daif |= val & CPSR_AIF & mask;
10403
10404 if (write_type != CPSRWriteRaw &&
10405 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
10406 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
10407 /*
10408 * Note that we can only get here in USR mode if this is a
10409 * gdb stub write; for this case we follow the architectural
10410 * behaviour for guest writes in USR mode of ignoring an attempt
10411 * to switch mode. (Those are caught by translate.c for writes
10412 * triggered by guest instructions.)
10413 */
10414 mask &= ~CPSR_M;
10415 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
10416 /*
10417 * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
10418 * v7, and has defined behaviour in v8:
10419 * + leave CPSR.M untouched
10420 * + allow changes to the other CPSR fields
10421 * + set PSTATE.IL
10422 * For user changes via the GDB stub, we don't set PSTATE.IL,
10423 * as this would be unnecessarily harsh for a user error.
10424 */
10425 mask &= ~CPSR_M;
10426 if (write_type != CPSRWriteByGDBStub &&
10427 arm_feature(env, ARM_FEATURE_V8)) {
10428 mask |= CPSR_IL;
10429 val |= CPSR_IL;
10430 }
10431 qemu_log_mask(LOG_GUEST_ERROR,
10432 "Illegal AArch32 mode switch attempt from %s to %s\n",
10433 aarch32_mode_name(env->uncached_cpsr),
10434 aarch32_mode_name(val));
10435 } else {
10436 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
10437 write_type == CPSRWriteExceptionReturn ?
10438 "Exception return from AArch32" :
10439 "AArch32 mode switch from",
10440 aarch32_mode_name(env->uncached_cpsr),
10441 aarch32_mode_name(val), env->regs[15]);
10442 switch_mode(env, val & CPSR_M);
10443 }
10444 }
10445 mask &= ~CACHED_CPSR_BITS;
10446 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
10447 if (tcg_enabled() && rebuild_hflags) {
10448 arm_rebuild_hflags(env);
10449 }
10450 }
10451
10452 #ifdef CONFIG_USER_ONLY
10453
10454 static void switch_mode(CPUARMState *env, int mode)
10455 {
10456 ARMCPU *cpu = env_archcpu(env);
10457
10458 if (mode != ARM_CPU_MODE_USR) {
10459 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
10460 }
10461 }
10462
10463 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10464 uint32_t cur_el, bool secure)
10465 {
10466 return 1;
10467 }
10468
10469 void aarch64_sync_64_to_32(CPUARMState *env)
10470 {
10471 g_assert_not_reached();
10472 }
10473
10474 #else
10475
10476 static void switch_mode(CPUARMState *env, int mode)
10477 {
10478 int old_mode;
10479 int i;
10480
10481 old_mode = env->uncached_cpsr & CPSR_M;
10482 if (mode == old_mode) {
10483 return;
10484 }
10485
10486 if (old_mode == ARM_CPU_MODE_FIQ) {
10487 memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
10488 memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
10489 } else if (mode == ARM_CPU_MODE_FIQ) {
10490 memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
10491 memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
10492 }
10493
10494 i = bank_number(old_mode);
10495 env->banked_r13[i] = env->regs[13];
10496 env->banked_spsr[i] = env->spsr;
10497
10498 i = bank_number(mode);
10499 env->regs[13] = env->banked_r13[i];
10500 env->spsr = env->banked_spsr[i];
10501
10502 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
10503 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
10504 }
10505
10506 /*
10507 * Physical Interrupt Target EL Lookup Table
10508 *
10509 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
10510 *
10511 * The below multi-dimensional table is used for looking up the target
10512 * exception level given numerous condition criteria. Specifically, the
10513 * target EL is based on SCR and HCR routing controls as well as the
10514 * currently executing EL and secure state.
10515 *
10516 * Dimensions:
10517 * target_el_table[2][2][2][2][2][4]
10518 * | | | | | +--- Current EL
10519 * | | | | +------ Non-secure(0)/Secure(1)
10520 * | | | +--------- HCR mask override
10521 * | | +------------ SCR exec state control
10522 * | +--------------- SCR mask override
10523 * +------------------ 32-bit(0)/64-bit(1) EL3
10524 *
10525 * The table values are as such:
10526 * 0-3 = EL0-EL3
10527 * -1 = Cannot occur
10528 *
10529 * The ARM ARM target EL table includes entries indicating that an "exception
10530 * is not taken". The two cases where this is applicable are:
10531 * 1) An exception is taken from EL3 but the SCR does not have the exception
10532 * routed to EL3.
10533 * 2) An exception is taken from EL2 but the HCR does not have the exception
10534 * routed to EL2.
10535 * In these two cases, the below table contain a target of EL1. This value is
10536 * returned as it is expected that the consumer of the table data will check
10537 * for "target EL >= current EL" to ensure the exception is not taken.
10538 *
10539 * SCR HCR
10540 * 64 EA AMO From
10541 * BIT IRQ IMO Non-secure Secure
10542 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
10543 */
10544 static const int8_t target_el_table[2][2][2][2][2][4] = {
10545 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
10546 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
10547 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
10548 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
10549 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
10550 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
10551 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
10552 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
10553 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
10554 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
10555 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
10556 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
10557 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
10558 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
10559 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
10560 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
10561 };
10562
10563 /*
10564 * Determine the target EL for physical exceptions
10565 */
10566 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10567 uint32_t cur_el, bool secure)
10568 {
10569 CPUARMState *env = cpu_env(cs);
10570 bool rw;
10571 bool scr;
10572 bool hcr;
10573 int target_el;
10574 /* Is the highest EL AArch64? */
10575 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
10576 uint64_t hcr_el2;
10577
10578 if (arm_feature(env, ARM_FEATURE_EL3)) {
10579 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
10580 } else {
10581 /*
10582 * Either EL2 is the highest EL (and so the EL2 register width
10583 * is given by is64); or there is no EL2 or EL3, in which case
10584 * the value of 'rw' does not affect the table lookup anyway.
10585 */
10586 rw = is64;
10587 }
10588
10589 hcr_el2 = arm_hcr_el2_eff(env);
10590 switch (excp_idx) {
10591 case EXCP_IRQ:
10592 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
10593 hcr = hcr_el2 & HCR_IMO;
10594 break;
10595 case EXCP_FIQ:
10596 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
10597 hcr = hcr_el2 & HCR_FMO;
10598 break;
10599 default:
10600 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
10601 hcr = hcr_el2 & HCR_AMO;
10602 break;
10603 };
10604
10605 /*
10606 * For these purposes, TGE and AMO/IMO/FMO both force the
10607 * interrupt to EL2. Fold TGE into the bit extracted above.
10608 */
10609 hcr |= (hcr_el2 & HCR_TGE) != 0;
10610
10611 /* Perform a table-lookup for the target EL given the current state */
10612 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
10613
10614 assert(target_el > 0);
10615
10616 return target_el;
10617 }
10618
10619 void arm_log_exception(CPUState *cs)
10620 {
10621 int idx = cs->exception_index;
10622
10623 if (qemu_loglevel_mask(CPU_LOG_INT)) {
10624 const char *exc = NULL;
10625 static const char * const excnames[] = {
10626 [EXCP_UDEF] = "Undefined Instruction",
10627 [EXCP_SWI] = "SVC",
10628 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
10629 [EXCP_DATA_ABORT] = "Data Abort",
10630 [EXCP_IRQ] = "IRQ",
10631 [EXCP_FIQ] = "FIQ",
10632 [EXCP_BKPT] = "Breakpoint",
10633 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
10634 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
10635 [EXCP_HVC] = "Hypervisor Call",
10636 [EXCP_HYP_TRAP] = "Hypervisor Trap",
10637 [EXCP_SMC] = "Secure Monitor Call",
10638 [EXCP_VIRQ] = "Virtual IRQ",
10639 [EXCP_VFIQ] = "Virtual FIQ",
10640 [EXCP_SEMIHOST] = "Semihosting call",
10641 [EXCP_NOCP] = "v7M NOCP UsageFault",
10642 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
10643 [EXCP_STKOF] = "v8M STKOF UsageFault",
10644 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
10645 [EXCP_LSERR] = "v8M LSERR UsageFault",
10646 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
10647 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
10648 [EXCP_VSERR] = "Virtual SERR",
10649 [EXCP_GPC] = "Granule Protection Check",
10650 };
10651
10652 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
10653 exc = excnames[idx];
10654 }
10655 if (!exc) {
10656 exc = "unknown";
10657 }
10658 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
10659 idx, exc, cs->cpu_index);
10660 }
10661 }
10662
10663 /*
10664 * Function used to synchronize QEMU's AArch64 register set with AArch32
10665 * register set. This is necessary when switching between AArch32 and AArch64
10666 * execution state.
10667 */
10668 void aarch64_sync_32_to_64(CPUARMState *env)
10669 {
10670 int i;
10671 uint32_t mode = env->uncached_cpsr & CPSR_M;
10672
10673 /* We can blanket copy R[0:7] to X[0:7] */
10674 for (i = 0; i < 8; i++) {
10675 env->xregs[i] = env->regs[i];
10676 }
10677
10678 /*
10679 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
10680 * Otherwise, they come from the banked user regs.
10681 */
10682 if (mode == ARM_CPU_MODE_FIQ) {
10683 for (i = 8; i < 13; i++) {
10684 env->xregs[i] = env->usr_regs[i - 8];
10685 }
10686 } else {
10687 for (i = 8; i < 13; i++) {
10688 env->xregs[i] = env->regs[i];
10689 }
10690 }
10691
10692 /*
10693 * Registers x13-x23 are the various mode SP and FP registers. Registers
10694 * r13 and r14 are only copied if we are in that mode, otherwise we copy
10695 * from the mode banked register.
10696 */
10697 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10698 env->xregs[13] = env->regs[13];
10699 env->xregs[14] = env->regs[14];
10700 } else {
10701 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
10702 /* HYP is an exception in that it is copied from r14 */
10703 if (mode == ARM_CPU_MODE_HYP) {
10704 env->xregs[14] = env->regs[14];
10705 } else {
10706 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
10707 }
10708 }
10709
10710 if (mode == ARM_CPU_MODE_HYP) {
10711 env->xregs[15] = env->regs[13];
10712 } else {
10713 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
10714 }
10715
10716 if (mode == ARM_CPU_MODE_IRQ) {
10717 env->xregs[16] = env->regs[14];
10718 env->xregs[17] = env->regs[13];
10719 } else {
10720 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
10721 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
10722 }
10723
10724 if (mode == ARM_CPU_MODE_SVC) {
10725 env->xregs[18] = env->regs[14];
10726 env->xregs[19] = env->regs[13];
10727 } else {
10728 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
10729 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
10730 }
10731
10732 if (mode == ARM_CPU_MODE_ABT) {
10733 env->xregs[20] = env->regs[14];
10734 env->xregs[21] = env->regs[13];
10735 } else {
10736 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10737 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10738 }
10739
10740 if (mode == ARM_CPU_MODE_UND) {
10741 env->xregs[22] = env->regs[14];
10742 env->xregs[23] = env->regs[13];
10743 } else {
10744 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10745 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
10746 }
10747
10748 /*
10749 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
10750 * mode, then we can copy from r8-r14. Otherwise, we copy from the
10751 * FIQ bank for r8-r14.
10752 */
10753 if (mode == ARM_CPU_MODE_FIQ) {
10754 for (i = 24; i < 31; i++) {
10755 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
10756 }
10757 } else {
10758 for (i = 24; i < 29; i++) {
10759 env->xregs[i] = env->fiq_regs[i - 24];
10760 }
10761 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10762 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
10763 }
10764
10765 env->pc = env->regs[15];
10766 }
10767
10768 /*
10769 * Function used to synchronize QEMU's AArch32 register set with AArch64
10770 * register set. This is necessary when switching between AArch32 and AArch64
10771 * execution state.
10772 */
10773 void aarch64_sync_64_to_32(CPUARMState *env)
10774 {
10775 int i;
10776 uint32_t mode = env->uncached_cpsr & CPSR_M;
10777
10778 /* We can blanket copy X[0:7] to R[0:7] */
10779 for (i = 0; i < 8; i++) {
10780 env->regs[i] = env->xregs[i];
10781 }
10782
10783 /*
10784 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10785 * Otherwise, we copy x8-x12 into the banked user regs.
10786 */
10787 if (mode == ARM_CPU_MODE_FIQ) {
10788 for (i = 8; i < 13; i++) {
10789 env->usr_regs[i - 8] = env->xregs[i];
10790 }
10791 } else {
10792 for (i = 8; i < 13; i++) {
10793 env->regs[i] = env->xregs[i];
10794 }
10795 }
10796
10797 /*
10798 * Registers r13 & r14 depend on the current mode.
10799 * If we are in a given mode, we copy the corresponding x registers to r13
10800 * and r14. Otherwise, we copy the x register to the banked r13 and r14
10801 * for the mode.
10802 */
10803 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10804 env->regs[13] = env->xregs[13];
10805 env->regs[14] = env->xregs[14];
10806 } else {
10807 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
10808
10809 /*
10810 * HYP is an exception in that it does not have its own banked r14 but
10811 * shares the USR r14
10812 */
10813 if (mode == ARM_CPU_MODE_HYP) {
10814 env->regs[14] = env->xregs[14];
10815 } else {
10816 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10817 }
10818 }
10819
10820 if (mode == ARM_CPU_MODE_HYP) {
10821 env->regs[13] = env->xregs[15];
10822 } else {
10823 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
10824 }
10825
10826 if (mode == ARM_CPU_MODE_IRQ) {
10827 env->regs[14] = env->xregs[16];
10828 env->regs[13] = env->xregs[17];
10829 } else {
10830 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10831 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
10832 }
10833
10834 if (mode == ARM_CPU_MODE_SVC) {
10835 env->regs[14] = env->xregs[18];
10836 env->regs[13] = env->xregs[19];
10837 } else {
10838 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10839 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
10840 }
10841
10842 if (mode == ARM_CPU_MODE_ABT) {
10843 env->regs[14] = env->xregs[20];
10844 env->regs[13] = env->xregs[21];
10845 } else {
10846 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10847 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
10848 }
10849
10850 if (mode == ARM_CPU_MODE_UND) {
10851 env->regs[14] = env->xregs[22];
10852 env->regs[13] = env->xregs[23];
10853 } else {
10854 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
10855 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
10856 }
10857
10858 /*
10859 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
10860 * mode, then we can copy to r8-r14. Otherwise, we copy to the
10861 * FIQ bank for r8-r14.
10862 */
10863 if (mode == ARM_CPU_MODE_FIQ) {
10864 for (i = 24; i < 31; i++) {
10865 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
10866 }
10867 } else {
10868 for (i = 24; i < 29; i++) {
10869 env->fiq_regs[i - 24] = env->xregs[i];
10870 }
10871 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
10872 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
10873 }
10874
10875 env->regs[15] = env->pc;
10876 }
10877
10878 static void take_aarch32_exception(CPUARMState *env, int new_mode,
10879 uint32_t mask, uint32_t offset,
10880 uint32_t newpc)
10881 {
10882 int new_el;
10883
10884 /* Change the CPU state so as to actually take the exception. */
10885 switch_mode(env, new_mode);
10886
10887 /*
10888 * For exceptions taken to AArch32 we must clear the SS bit in both
10889 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10890 */
10891 env->pstate &= ~PSTATE_SS;
10892 env->spsr = cpsr_read(env);
10893 /* Clear IT bits. */
10894 env->condexec_bits = 0;
10895 /* Switch to the new mode, and to the correct instruction set. */
10896 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
10897
10898 /* This must be after mode switching. */
10899 new_el = arm_current_el(env);
10900
10901 /* Set new mode endianness */
10902 env->uncached_cpsr &= ~CPSR_E;
10903 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
10904 env->uncached_cpsr |= CPSR_E;
10905 }
10906 /* J and IL must always be cleared for exception entry */
10907 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
10908 env->daif |= mask;
10909
10910 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10911 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10912 env->uncached_cpsr |= CPSR_SSBS;
10913 } else {
10914 env->uncached_cpsr &= ~CPSR_SSBS;
10915 }
10916 }
10917
10918 if (new_mode == ARM_CPU_MODE_HYP) {
10919 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10920 env->elr_el[2] = env->regs[15];
10921 } else {
10922 /* CPSR.PAN is normally preserved preserved unless... */
10923 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
10924 switch (new_el) {
10925 case 3:
10926 if (!arm_is_secure_below_el3(env)) {
10927 /* ... the target is EL3, from non-secure state. */
10928 env->uncached_cpsr &= ~CPSR_PAN;
10929 break;
10930 }
10931 /* ... the target is EL3, from secure state ... */
10932 /* fall through */
10933 case 1:
10934 /* ... the target is EL1 and SCTLR.SPAN is 0. */
10935 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10936 env->uncached_cpsr |= CPSR_PAN;
10937 }
10938 break;
10939 }
10940 }
10941 /*
10942 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10943 * and we should just guard the thumb mode on V4
10944 */
10945 if (arm_feature(env, ARM_FEATURE_V4T)) {
10946 env->thumb =
10947 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10948 }
10949 env->regs[14] = env->regs[15] + offset;
10950 }
10951 env->regs[15] = newpc;
10952
10953 if (tcg_enabled()) {
10954 arm_rebuild_hflags(env);
10955 }
10956 }
10957
10958 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10959 {
10960 /*
10961 * Handle exception entry to Hyp mode; this is sufficiently
10962 * different to entry to other AArch32 modes that we handle it
10963 * separately here.
10964 *
10965 * The vector table entry used is always the 0x14 Hyp mode entry point,
10966 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
10967 * The offset applied to the preferred return address is always zero
10968 * (see DDI0487C.a section G1.12.3).
10969 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10970 */
10971 uint32_t addr, mask;
10972 ARMCPU *cpu = ARM_CPU(cs);
10973 CPUARMState *env = &cpu->env;
10974
10975 switch (cs->exception_index) {
10976 case EXCP_UDEF:
10977 addr = 0x04;
10978 break;
10979 case EXCP_SWI:
10980 addr = 0x08;
10981 break;
10982 case EXCP_BKPT:
10983 /* Fall through to prefetch abort. */
10984 case EXCP_PREFETCH_ABORT:
10985 env->cp15.ifar_s = env->exception.vaddress;
10986 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10987 (uint32_t)env->exception.vaddress);
10988 addr = 0x0c;
10989 break;
10990 case EXCP_DATA_ABORT:
10991 env->cp15.dfar_s = env->exception.vaddress;
10992 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10993 (uint32_t)env->exception.vaddress);
10994 addr = 0x10;
10995 break;
10996 case EXCP_IRQ:
10997 addr = 0x18;
10998 break;
10999 case EXCP_FIQ:
11000 addr = 0x1c;
11001 break;
11002 case EXCP_HVC:
11003 addr = 0x08;
11004 break;
11005 case EXCP_HYP_TRAP:
11006 addr = 0x14;
11007 break;
11008 default:
11009 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11010 }
11011
11012 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
11013 if (!arm_feature(env, ARM_FEATURE_V8)) {
11014 /*
11015 * QEMU syndrome values are v8-style. v7 has the IL bit
11016 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
11017 * If this is a v7 CPU, squash the IL bit in those cases.
11018 */
11019 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
11020 (cs->exception_index == EXCP_DATA_ABORT &&
11021 !(env->exception.syndrome & ARM_EL_ISV)) ||
11022 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
11023 env->exception.syndrome &= ~ARM_EL_IL;
11024 }
11025 }
11026 env->cp15.esr_el[2] = env->exception.syndrome;
11027 }
11028
11029 if (arm_current_el(env) != 2 && addr < 0x14) {
11030 addr = 0x14;
11031 }
11032
11033 mask = 0;
11034 if (!(env->cp15.scr_el3 & SCR_EA)) {
11035 mask |= CPSR_A;
11036 }
11037 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
11038 mask |= CPSR_I;
11039 }
11040 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
11041 mask |= CPSR_F;
11042 }
11043
11044 addr += env->cp15.hvbar;
11045
11046 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
11047 }
11048
11049 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
11050 {
11051 ARMCPU *cpu = ARM_CPU(cs);
11052 CPUARMState *env = &cpu->env;
11053 uint32_t addr;
11054 uint32_t mask;
11055 int new_mode;
11056 uint32_t offset;
11057 uint32_t moe;
11058
11059 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
11060 switch (syn_get_ec(env->exception.syndrome)) {
11061 case EC_BREAKPOINT:
11062 case EC_BREAKPOINT_SAME_EL:
11063 moe = 1;
11064 break;
11065 case EC_WATCHPOINT:
11066 case EC_WATCHPOINT_SAME_EL:
11067 moe = 10;
11068 break;
11069 case EC_AA32_BKPT:
11070 moe = 3;
11071 break;
11072 case EC_VECTORCATCH:
11073 moe = 5;
11074 break;
11075 default:
11076 moe = 0;
11077 break;
11078 }
11079
11080 if (moe) {
11081 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
11082 }
11083
11084 if (env->exception.target_el == 2) {
11085 /* Debug exceptions are reported differently on AArch32 */
11086 switch (syn_get_ec(env->exception.syndrome)) {
11087 case EC_BREAKPOINT:
11088 case EC_BREAKPOINT_SAME_EL:
11089 case EC_AA32_BKPT:
11090 case EC_VECTORCATCH:
11091 env->exception.syndrome = syn_insn_abort(arm_current_el(env) == 2,
11092 0, 0, 0x22);
11093 break;
11094 case EC_WATCHPOINT:
11095 env->exception.syndrome = syn_set_ec(env->exception.syndrome,
11096 EC_DATAABORT);
11097 break;
11098 case EC_WATCHPOINT_SAME_EL:
11099 env->exception.syndrome = syn_set_ec(env->exception.syndrome,
11100 EC_DATAABORT_SAME_EL);
11101 break;
11102 }
11103 arm_cpu_do_interrupt_aarch32_hyp(cs);
11104 return;
11105 }
11106
11107 switch (cs->exception_index) {
11108 case EXCP_UDEF:
11109 new_mode = ARM_CPU_MODE_UND;
11110 addr = 0x04;
11111 mask = CPSR_I;
11112 if (env->thumb) {
11113 offset = 2;
11114 } else {
11115 offset = 4;
11116 }
11117 break;
11118 case EXCP_SWI:
11119 new_mode = ARM_CPU_MODE_SVC;
11120 addr = 0x08;
11121 mask = CPSR_I;
11122 /* The PC already points to the next instruction. */
11123 offset = 0;
11124 break;
11125 case EXCP_BKPT:
11126 /* Fall through to prefetch abort. */
11127 case EXCP_PREFETCH_ABORT:
11128 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
11129 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
11130 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
11131 env->exception.fsr, (uint32_t)env->exception.vaddress);
11132 new_mode = ARM_CPU_MODE_ABT;
11133 addr = 0x0c;
11134 mask = CPSR_A | CPSR_I;
11135 offset = 4;
11136 break;
11137 case EXCP_DATA_ABORT:
11138 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
11139 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
11140 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
11141 env->exception.fsr,
11142 (uint32_t)env->exception.vaddress);
11143 new_mode = ARM_CPU_MODE_ABT;
11144 addr = 0x10;
11145 mask = CPSR_A | CPSR_I;
11146 offset = 8;
11147 break;
11148 case EXCP_IRQ:
11149 new_mode = ARM_CPU_MODE_IRQ;
11150 addr = 0x18;
11151 /* Disable IRQ and imprecise data aborts. */
11152 mask = CPSR_A | CPSR_I;
11153 offset = 4;
11154 if (env->cp15.scr_el3 & SCR_IRQ) {
11155 /* IRQ routed to monitor mode */
11156 new_mode = ARM_CPU_MODE_MON;
11157 mask |= CPSR_F;
11158 }
11159 break;
11160 case EXCP_FIQ:
11161 new_mode = ARM_CPU_MODE_FIQ;
11162 addr = 0x1c;
11163 /* Disable FIQ, IRQ and imprecise data aborts. */
11164 mask = CPSR_A | CPSR_I | CPSR_F;
11165 if (env->cp15.scr_el3 & SCR_FIQ) {
11166 /* FIQ routed to monitor mode */
11167 new_mode = ARM_CPU_MODE_MON;
11168 }
11169 offset = 4;
11170 break;
11171 case EXCP_VIRQ:
11172 new_mode = ARM_CPU_MODE_IRQ;
11173 addr = 0x18;
11174 /* Disable IRQ and imprecise data aborts. */
11175 mask = CPSR_A | CPSR_I;
11176 offset = 4;
11177 break;
11178 case EXCP_VFIQ:
11179 new_mode = ARM_CPU_MODE_FIQ;
11180 addr = 0x1c;
11181 /* Disable FIQ, IRQ and imprecise data aborts. */
11182 mask = CPSR_A | CPSR_I | CPSR_F;
11183 offset = 4;
11184 break;
11185 case EXCP_VSERR:
11186 {
11187 /*
11188 * Note that this is reported as a data abort, but the DFAR
11189 * has an UNKNOWN value. Construct the SError syndrome from
11190 * AET and ExT fields.
11191 */
11192 ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
11193
11194 if (extended_addresses_enabled(env)) {
11195 env->exception.fsr = arm_fi_to_lfsc(&fi);
11196 } else {
11197 env->exception.fsr = arm_fi_to_sfsc(&fi);
11198 }
11199 env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
11200 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
11201 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
11202 env->exception.fsr);
11203
11204 new_mode = ARM_CPU_MODE_ABT;
11205 addr = 0x10;
11206 mask = CPSR_A | CPSR_I;
11207 offset = 8;
11208 }
11209 break;
11210 case EXCP_SMC:
11211 new_mode = ARM_CPU_MODE_MON;
11212 addr = 0x08;
11213 mask = CPSR_A | CPSR_I | CPSR_F;
11214 offset = 0;
11215 break;
11216 default:
11217 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11218 return; /* Never happens. Keep compiler happy. */
11219 }
11220
11221 if (new_mode == ARM_CPU_MODE_MON) {
11222 addr += env->cp15.mvbar;
11223 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
11224 /* High vectors. When enabled, base address cannot be remapped. */
11225 addr += 0xffff0000;
11226 } else {
11227 /*
11228 * ARM v7 architectures provide a vector base address register to remap
11229 * the interrupt vector table.
11230 * This register is only followed in non-monitor mode, and is banked.
11231 * Note: only bits 31:5 are valid.
11232 */
11233 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
11234 }
11235
11236 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
11237 env->cp15.scr_el3 &= ~SCR_NS;
11238 }
11239
11240 take_aarch32_exception(env, new_mode, mask, offset, addr);
11241 }
11242
11243 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
11244 {
11245 /*
11246 * Return the register number of the AArch64 view of the AArch32
11247 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
11248 * be that of the AArch32 mode the exception came from.
11249 */
11250 int mode = env->uncached_cpsr & CPSR_M;
11251
11252 switch (aarch32_reg) {
11253 case 0 ... 7:
11254 return aarch32_reg;
11255 case 8 ... 12:
11256 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
11257 case 13:
11258 switch (mode) {
11259 case ARM_CPU_MODE_USR:
11260 case ARM_CPU_MODE_SYS:
11261 return 13;
11262 case ARM_CPU_MODE_HYP:
11263 return 15;
11264 case ARM_CPU_MODE_IRQ:
11265 return 17;
11266 case ARM_CPU_MODE_SVC:
11267 return 19;
11268 case ARM_CPU_MODE_ABT:
11269 return 21;
11270 case ARM_CPU_MODE_UND:
11271 return 23;
11272 case ARM_CPU_MODE_FIQ:
11273 return 29;
11274 default:
11275 g_assert_not_reached();
11276 }
11277 case 14:
11278 switch (mode) {
11279 case ARM_CPU_MODE_USR:
11280 case ARM_CPU_MODE_SYS:
11281 case ARM_CPU_MODE_HYP:
11282 return 14;
11283 case ARM_CPU_MODE_IRQ:
11284 return 16;
11285 case ARM_CPU_MODE_SVC:
11286 return 18;
11287 case ARM_CPU_MODE_ABT:
11288 return 20;
11289 case ARM_CPU_MODE_UND:
11290 return 22;
11291 case ARM_CPU_MODE_FIQ:
11292 return 30;
11293 default:
11294 g_assert_not_reached();
11295 }
11296 case 15:
11297 return 31;
11298 default:
11299 g_assert_not_reached();
11300 }
11301 }
11302
11303 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
11304 {
11305 uint32_t ret = cpsr_read(env);
11306
11307 /* Move DIT to the correct location for SPSR_ELx */
11308 if (ret & CPSR_DIT) {
11309 ret &= ~CPSR_DIT;
11310 ret |= PSTATE_DIT;
11311 }
11312 /* Merge PSTATE.SS into SPSR_ELx */
11313 ret |= env->pstate & PSTATE_SS;
11314
11315 return ret;
11316 }
11317
11318 static bool syndrome_is_sync_extabt(uint32_t syndrome)
11319 {
11320 /* Return true if this syndrome value is a synchronous external abort */
11321 switch (syn_get_ec(syndrome)) {
11322 case EC_INSNABORT:
11323 case EC_INSNABORT_SAME_EL:
11324 case EC_DATAABORT:
11325 case EC_DATAABORT_SAME_EL:
11326 /* Look at fault status code for all the synchronous ext abort cases */
11327 switch (syndrome & 0x3f) {
11328 case 0x10:
11329 case 0x13:
11330 case 0x14:
11331 case 0x15:
11332 case 0x16:
11333 case 0x17:
11334 return true;
11335 default:
11336 return false;
11337 }
11338 default:
11339 return false;
11340 }
11341 }
11342
11343 /* Handle exception entry to a target EL which is using AArch64 */
11344 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
11345 {
11346 ARMCPU *cpu = ARM_CPU(cs);
11347 CPUARMState *env = &cpu->env;
11348 unsigned int new_el = env->exception.target_el;
11349 target_ulong addr = env->cp15.vbar_el[new_el];
11350 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
11351 unsigned int old_mode;
11352 unsigned int cur_el = arm_current_el(env);
11353 int rt;
11354
11355 if (tcg_enabled()) {
11356 /*
11357 * Note that new_el can never be 0. If cur_el is 0, then
11358 * el0_a64 is is_a64(), else el0_a64 is ignored.
11359 */
11360 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
11361 }
11362
11363 if (cur_el < new_el) {
11364 /*
11365 * Entry vector offset depends on whether the implemented EL
11366 * immediately lower than the target level is using AArch32 or AArch64
11367 */
11368 bool is_aa64;
11369 uint64_t hcr;
11370
11371 switch (new_el) {
11372 case 3:
11373 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
11374 break;
11375 case 2:
11376 hcr = arm_hcr_el2_eff(env);
11377 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11378 is_aa64 = (hcr & HCR_RW) != 0;
11379 break;
11380 }
11381 /* fall through */
11382 case 1:
11383 is_aa64 = is_a64(env);
11384 break;
11385 default:
11386 g_assert_not_reached();
11387 }
11388
11389 if (is_aa64) {
11390 addr += 0x400;
11391 } else {
11392 addr += 0x600;
11393 }
11394 } else if (pstate_read(env) & PSTATE_SP) {
11395 addr += 0x200;
11396 }
11397
11398 switch (cs->exception_index) {
11399 case EXCP_GPC:
11400 qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
11401 env->cp15.mfar_el3);
11402 /* fall through */
11403 case EXCP_PREFETCH_ABORT:
11404 case EXCP_DATA_ABORT:
11405 /*
11406 * FEAT_DoubleFault allows synchronous external aborts taken to EL3
11407 * to be taken to the SError vector entrypoint.
11408 */
11409 if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
11410 syndrome_is_sync_extabt(env->exception.syndrome)) {
11411 addr += 0x180;
11412 }
11413 env->cp15.far_el[new_el] = env->exception.vaddress;
11414 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
11415 env->cp15.far_el[new_el]);
11416 /* fall through */
11417 case EXCP_BKPT:
11418 case EXCP_UDEF:
11419 case EXCP_SWI:
11420 case EXCP_HVC:
11421 case EXCP_HYP_TRAP:
11422 case EXCP_SMC:
11423 switch (syn_get_ec(env->exception.syndrome)) {
11424 case EC_ADVSIMDFPACCESSTRAP:
11425 /*
11426 * QEMU internal FP/SIMD syndromes from AArch32 include the
11427 * TA and coproc fields which are only exposed if the exception
11428 * is taken to AArch32 Hyp mode. Mask them out to get a valid
11429 * AArch64 format syndrome.
11430 */
11431 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
11432 break;
11433 case EC_CP14RTTRAP:
11434 case EC_CP15RTTRAP:
11435 case EC_CP14DTTRAP:
11436 /*
11437 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
11438 * the raw register field from the insn; when taking this to
11439 * AArch64 we must convert it to the AArch64 view of the register
11440 * number. Notice that we read a 4-bit AArch32 register number and
11441 * write back a 5-bit AArch64 one.
11442 */
11443 rt = extract32(env->exception.syndrome, 5, 4);
11444 rt = aarch64_regnum(env, rt);
11445 env->exception.syndrome = deposit32(env->exception.syndrome,
11446 5, 5, rt);
11447 break;
11448 case EC_CP15RRTTRAP:
11449 case EC_CP14RRTTRAP:
11450 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
11451 rt = extract32(env->exception.syndrome, 5, 4);
11452 rt = aarch64_regnum(env, rt);
11453 env->exception.syndrome = deposit32(env->exception.syndrome,
11454 5, 5, rt);
11455 rt = extract32(env->exception.syndrome, 10, 4);
11456 rt = aarch64_regnum(env, rt);
11457 env->exception.syndrome = deposit32(env->exception.syndrome,
11458 10, 5, rt);
11459 break;
11460 }
11461 env->cp15.esr_el[new_el] = env->exception.syndrome;
11462 break;
11463 case EXCP_IRQ:
11464 case EXCP_VIRQ:
11465 addr += 0x80;
11466 break;
11467 case EXCP_FIQ:
11468 case EXCP_VFIQ:
11469 addr += 0x100;
11470 break;
11471 case EXCP_VSERR:
11472 addr += 0x180;
11473 /* Construct the SError syndrome from IDS and ISS fields. */
11474 env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
11475 env->cp15.esr_el[new_el] = env->exception.syndrome;
11476 break;
11477 default:
11478 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11479 }
11480
11481 if (is_a64(env)) {
11482 old_mode = pstate_read(env);
11483 aarch64_save_sp(env, arm_current_el(env));
11484 env->elr_el[new_el] = env->pc;
11485
11486 if (cur_el == 1 && new_el == 1) {
11487 uint64_t hcr = arm_hcr_el2_eff(env);
11488 if ((hcr & (HCR_NV | HCR_NV1 | HCR_NV2)) == HCR_NV ||
11489 (hcr & (HCR_NV | HCR_NV2)) == (HCR_NV | HCR_NV2)) {
11490 /*
11491 * FEAT_NV, FEAT_NV2 may need to report EL2 in the SPSR
11492 * by setting M[3:2] to 0b10.
11493 * If NV2 is disabled, change SPSR when NV,NV1 == 1,0 (I_ZJRNN)
11494 * If NV2 is enabled, change SPSR when NV is 1 (I_DBTLM)
11495 */
11496 old_mode = deposit32(old_mode, 2, 2, 2);
11497 }
11498 }
11499 } else {
11500 old_mode = cpsr_read_for_spsr_elx(env);
11501 env->elr_el[new_el] = env->regs[15];
11502
11503 aarch64_sync_32_to_64(env);
11504
11505 env->condexec_bits = 0;
11506 }
11507 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
11508
11509 qemu_log_mask(CPU_LOG_INT, "...with SPSR 0x%x\n", old_mode);
11510 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
11511 env->elr_el[new_el]);
11512
11513 if (cpu_isar_feature(aa64_pan, cpu)) {
11514 /* The value of PSTATE.PAN is normally preserved, except when ... */
11515 new_mode |= old_mode & PSTATE_PAN;
11516 switch (new_el) {
11517 case 2:
11518 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
11519 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
11520 != (HCR_E2H | HCR_TGE)) {
11521 break;
11522 }
11523 /* fall through */
11524 case 1:
11525 /* ... the target is EL1 ... */
11526 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
11527 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
11528 new_mode |= PSTATE_PAN;
11529 }
11530 break;
11531 }
11532 }
11533 if (cpu_isar_feature(aa64_mte, cpu)) {
11534 new_mode |= PSTATE_TCO;
11535 }
11536
11537 if (cpu_isar_feature(aa64_ssbs, cpu)) {
11538 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
11539 new_mode |= PSTATE_SSBS;
11540 } else {
11541 new_mode &= ~PSTATE_SSBS;
11542 }
11543 }
11544
11545 pstate_write(env, PSTATE_DAIF | new_mode);
11546 env->aarch64 = true;
11547 aarch64_restore_sp(env, new_el);
11548
11549 if (tcg_enabled()) {
11550 helper_rebuild_hflags_a64(env, new_el);
11551 }
11552
11553 env->pc = addr;
11554
11555 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
11556 new_el, env->pc, pstate_read(env));
11557 }
11558
11559 /*
11560 * Do semihosting call and set the appropriate return value. All the
11561 * permission and validity checks have been done at translate time.
11562 *
11563 * We only see semihosting exceptions in TCG only as they are not
11564 * trapped to the hypervisor in KVM.
11565 */
11566 #ifdef CONFIG_TCG
11567 static void tcg_handle_semihosting(CPUState *cs)
11568 {
11569 ARMCPU *cpu = ARM_CPU(cs);
11570 CPUARMState *env = &cpu->env;
11571
11572 if (is_a64(env)) {
11573 qemu_log_mask(CPU_LOG_INT,
11574 "...handling as semihosting call 0x%" PRIx64 "\n",
11575 env->xregs[0]);
11576 do_common_semihosting(cs);
11577 env->pc += 4;
11578 } else {
11579 qemu_log_mask(CPU_LOG_INT,
11580 "...handling as semihosting call 0x%x\n",
11581 env->regs[0]);
11582 do_common_semihosting(cs);
11583 env->regs[15] += env->thumb ? 2 : 4;
11584 }
11585 }
11586 #endif
11587
11588 /*
11589 * Handle a CPU exception for A and R profile CPUs.
11590 * Do any appropriate logging, handle PSCI calls, and then hand off
11591 * to the AArch64-entry or AArch32-entry function depending on the
11592 * target exception level's register width.
11593 *
11594 * Note: this is used for both TCG (as the do_interrupt tcg op),
11595 * and KVM to re-inject guest debug exceptions, and to
11596 * inject a Synchronous-External-Abort.
11597 */
11598 void arm_cpu_do_interrupt(CPUState *cs)
11599 {
11600 ARMCPU *cpu = ARM_CPU(cs);
11601 CPUARMState *env = &cpu->env;
11602 unsigned int new_el = env->exception.target_el;
11603
11604 assert(!arm_feature(env, ARM_FEATURE_M));
11605
11606 arm_log_exception(cs);
11607 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
11608 new_el);
11609 if (qemu_loglevel_mask(CPU_LOG_INT)
11610 && !excp_is_internal(cs->exception_index)) {
11611 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
11612 syn_get_ec(env->exception.syndrome),
11613 env->exception.syndrome);
11614 }
11615
11616 if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
11617 arm_handle_psci_call(cpu);
11618 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
11619 return;
11620 }
11621
11622 /*
11623 * Semihosting semantics depend on the register width of the code
11624 * that caused the exception, not the target exception level, so
11625 * must be handled here.
11626 */
11627 #ifdef CONFIG_TCG
11628 if (cs->exception_index == EXCP_SEMIHOST) {
11629 tcg_handle_semihosting(cs);
11630 return;
11631 }
11632 #endif
11633
11634 /*
11635 * Hooks may change global state so BQL should be held, also the
11636 * BQL needs to be held for any modification of
11637 * cs->interrupt_request.
11638 */
11639 g_assert(bql_locked());
11640
11641 arm_call_pre_el_change_hook(cpu);
11642
11643 assert(!excp_is_internal(cs->exception_index));
11644 if (arm_el_is_aa64(env, new_el)) {
11645 arm_cpu_do_interrupt_aarch64(cs);
11646 } else {
11647 arm_cpu_do_interrupt_aarch32(cs);
11648 }
11649
11650 arm_call_el_change_hook(cpu);
11651
11652 if (!kvm_enabled()) {
11653 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
11654 }
11655 }
11656 #endif /* !CONFIG_USER_ONLY */
11657
11658 uint64_t arm_sctlr(CPUARMState *env, int el)
11659 {
11660 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
11661 if (el == 0) {
11662 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
11663 el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
11664 }
11665 return env->cp15.sctlr_el[el];
11666 }
11667
11668 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
11669 {
11670 if (regime_has_2_ranges(mmu_idx)) {
11671 return extract64(tcr, 37, 2);
11672 } else if (regime_is_stage2(mmu_idx)) {
11673 return 0; /* VTCR_EL2 */
11674 } else {
11675 /* Replicate the single TBI bit so we always have 2 bits. */
11676 return extract32(tcr, 20, 1) * 3;
11677 }
11678 }
11679
11680 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
11681 {
11682 if (regime_has_2_ranges(mmu_idx)) {
11683 return extract64(tcr, 51, 2);
11684 } else if (regime_is_stage2(mmu_idx)) {
11685 return 0; /* VTCR_EL2 */
11686 } else {
11687 /* Replicate the single TBID bit so we always have 2 bits. */
11688 return extract32(tcr, 29, 1) * 3;
11689 }
11690 }
11691
11692 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
11693 {
11694 if (regime_has_2_ranges(mmu_idx)) {
11695 return extract64(tcr, 57, 2);
11696 } else {
11697 /* Replicate the single TCMA bit so we always have 2 bits. */
11698 return extract32(tcr, 30, 1) * 3;
11699 }
11700 }
11701
11702 static ARMGranuleSize tg0_to_gran_size(int tg)
11703 {
11704 switch (tg) {
11705 case 0:
11706 return Gran4K;
11707 case 1:
11708 return Gran64K;
11709 case 2:
11710 return Gran16K;
11711 default:
11712 return GranInvalid;
11713 }
11714 }
11715
11716 static ARMGranuleSize tg1_to_gran_size(int tg)
11717 {
11718 switch (tg) {
11719 case 1:
11720 return Gran16K;
11721 case 2:
11722 return Gran4K;
11723 case 3:
11724 return Gran64K;
11725 default:
11726 return GranInvalid;
11727 }
11728 }
11729
11730 static inline bool have4k(ARMCPU *cpu, bool stage2)
11731 {
11732 return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
11733 : cpu_isar_feature(aa64_tgran4, cpu);
11734 }
11735
11736 static inline bool have16k(ARMCPU *cpu, bool stage2)
11737 {
11738 return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
11739 : cpu_isar_feature(aa64_tgran16, cpu);
11740 }
11741
11742 static inline bool have64k(ARMCPU *cpu, bool stage2)
11743 {
11744 return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
11745 : cpu_isar_feature(aa64_tgran64, cpu);
11746 }
11747
11748 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
11749 bool stage2)
11750 {
11751 switch (gran) {
11752 case Gran4K:
11753 if (have4k(cpu, stage2)) {
11754 return gran;
11755 }
11756 break;
11757 case Gran16K:
11758 if (have16k(cpu, stage2)) {
11759 return gran;
11760 }
11761 break;
11762 case Gran64K:
11763 if (have64k(cpu, stage2)) {
11764 return gran;
11765 }
11766 break;
11767 case GranInvalid:
11768 break;
11769 }
11770 /*
11771 * If the guest selects a granule size that isn't implemented,
11772 * the architecture requires that we behave as if it selected one
11773 * that is (with an IMPDEF choice of which one to pick). We choose
11774 * to implement the smallest supported granule size.
11775 */
11776 if (have4k(cpu, stage2)) {
11777 return Gran4K;
11778 }
11779 if (have16k(cpu, stage2)) {
11780 return Gran16K;
11781 }
11782 assert(have64k(cpu, stage2));
11783 return Gran64K;
11784 }
11785
11786 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11787 ARMMMUIdx mmu_idx, bool data,
11788 bool el1_is_aa32)
11789 {
11790 uint64_t tcr = regime_tcr(env, mmu_idx);
11791 bool epd, hpd, tsz_oob, ds, ha, hd;
11792 int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11793 ARMGranuleSize gran;
11794 ARMCPU *cpu = env_archcpu(env);
11795 bool stage2 = regime_is_stage2(mmu_idx);
11796
11797 if (!regime_has_2_ranges(mmu_idx)) {
11798 select = 0;
11799 tsz = extract32(tcr, 0, 6);
11800 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11801 if (stage2) {
11802 /* VTCR_EL2 */
11803 hpd = false;
11804 } else {
11805 hpd = extract32(tcr, 24, 1);
11806 }
11807 epd = false;
11808 sh = extract32(tcr, 12, 2);
11809 ps = extract32(tcr, 16, 3);
11810 ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11811 hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11812 ds = extract64(tcr, 32, 1);
11813 } else {
11814 bool e0pd;
11815
11816 /*
11817 * Bit 55 is always between the two regions, and is canonical for
11818 * determining if address tagging is enabled.
11819 */
11820 select = extract64(va, 55, 1);
11821 if (!select) {
11822 tsz = extract32(tcr, 0, 6);
11823 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11824 epd = extract32(tcr, 7, 1);
11825 sh = extract32(tcr, 12, 2);
11826 hpd = extract64(tcr, 41, 1);
11827 e0pd = extract64(tcr, 55, 1);
11828 } else {
11829 tsz = extract32(tcr, 16, 6);
11830 gran = tg1_to_gran_size(extract32(tcr, 30, 2));
11831 epd = extract32(tcr, 23, 1);
11832 sh = extract32(tcr, 28, 2);
11833 hpd = extract64(tcr, 42, 1);
11834 e0pd = extract64(tcr, 56, 1);
11835 }
11836 ps = extract64(tcr, 32, 3);
11837 ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11838 hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11839 ds = extract64(tcr, 59, 1);
11840
11841 if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11842 regime_is_user(env, mmu_idx)) {
11843 epd = true;
11844 }
11845 }
11846
11847 gran = sanitize_gran_size(cpu, gran, stage2);
11848
11849 if (cpu_isar_feature(aa64_st, cpu)) {
11850 max_tsz = 48 - (gran == Gran64K);
11851 } else {
11852 max_tsz = 39;
11853 }
11854
11855 /*
11856 * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11857 * adjust the effective value of DS, as documented.
11858 */
11859 min_tsz = 16;
11860 if (gran == Gran64K) {
11861 if (cpu_isar_feature(aa64_lva, cpu)) {
11862 min_tsz = 12;
11863 }
11864 ds = false;
11865 } else if (ds) {
11866 if (regime_is_stage2(mmu_idx)) {
11867 if (gran == Gran16K) {
11868 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11869 } else {
11870 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11871 }
11872 } else {
11873 if (gran == Gran16K) {
11874 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11875 } else {
11876 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11877 }
11878 }
11879 if (ds) {
11880 min_tsz = 12;
11881 }
11882 }
11883
11884 if (stage2 && el1_is_aa32) {
11885 /*
11886 * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
11887 * are loosened: a configured IPA of 40 bits is permitted even if
11888 * the implemented PA is less than that (and so a 40 bit IPA would
11889 * fault for an AArch64 EL1). See R_DTLMN.
11890 */
11891 min_tsz = MIN(min_tsz, 24);
11892 }
11893
11894 if (tsz > max_tsz) {
11895 tsz = max_tsz;
11896 tsz_oob = true;
11897 } else if (tsz < min_tsz) {
11898 tsz = min_tsz;
11899 tsz_oob = true;
11900 } else {
11901 tsz_oob = false;
11902 }
11903
11904 /* Present TBI as a composite with TBID. */
11905 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11906 if (!data) {
11907 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11908 }
11909 tbi = (tbi >> select) & 1;
11910
11911 return (ARMVAParameters) {
11912 .tsz = tsz,
11913 .ps = ps,
11914 .sh = sh,
11915 .select = select,
11916 .tbi = tbi,
11917 .epd = epd,
11918 .hpd = hpd,
11919 .tsz_oob = tsz_oob,
11920 .ds = ds,
11921 .ha = ha,
11922 .hd = ha && hd,
11923 .gran = gran,
11924 };
11925 }
11926
11927 /*
11928 * Note that signed overflow is undefined in C. The following routines are
11929 * careful to use unsigned types where modulo arithmetic is required.
11930 * Failure to do so _will_ break on newer gcc.
11931 */
11932
11933 /* Signed saturating arithmetic. */
11934
11935 /* Perform 16-bit signed saturating addition. */
11936 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11937 {
11938 uint16_t res;
11939
11940 res = a + b;
11941 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
11942 if (a & 0x8000) {
11943 res = 0x8000;
11944 } else {
11945 res = 0x7fff;
11946 }
11947 }
11948 return res;
11949 }
11950
11951 /* Perform 8-bit signed saturating addition. */
11952 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11953 {
11954 uint8_t res;
11955
11956 res = a + b;
11957 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
11958 if (a & 0x80) {
11959 res = 0x80;
11960 } else {
11961 res = 0x7f;
11962 }
11963 }
11964 return res;
11965 }
11966
11967 /* Perform 16-bit signed saturating subtraction. */
11968 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11969 {
11970 uint16_t res;
11971
11972 res = a - b;
11973 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
11974 if (a & 0x8000) {
11975 res = 0x8000;
11976 } else {
11977 res = 0x7fff;
11978 }
11979 }
11980 return res;
11981 }
11982
11983 /* Perform 8-bit signed saturating subtraction. */
11984 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11985 {
11986 uint8_t res;
11987
11988 res = a - b;
11989 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
11990 if (a & 0x80) {
11991 res = 0x80;
11992 } else {
11993 res = 0x7f;
11994 }
11995 }
11996 return res;
11997 }
11998
11999 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
12000 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
12001 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
12002 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
12003 #define PFX q
12004
12005 #include "op_addsub.h"
12006
12007 /* Unsigned saturating arithmetic. */
12008 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
12009 {
12010 uint16_t res;
12011 res = a + b;
12012 if (res < a) {
12013 res = 0xffff;
12014 }
12015 return res;
12016 }
12017
12018 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
12019 {
12020 if (a > b) {
12021 return a - b;
12022 } else {
12023 return 0;
12024 }
12025 }
12026
12027 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
12028 {
12029 uint8_t res;
12030 res = a + b;
12031 if (res < a) {
12032 res = 0xff;
12033 }
12034 return res;
12035 }
12036
12037 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
12038 {
12039 if (a > b) {
12040 return a - b;
12041 } else {
12042 return 0;
12043 }
12044 }
12045
12046 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
12047 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
12048 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
12049 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
12050 #define PFX uq
12051
12052 #include "op_addsub.h"
12053
12054 /* Signed modulo arithmetic. */
12055 #define SARITH16(a, b, n, op) do { \
12056 int32_t sum; \
12057 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
12058 RESULT(sum, n, 16); \
12059 if (sum >= 0) \
12060 ge |= 3 << (n * 2); \
12061 } while (0)
12062
12063 #define SARITH8(a, b, n, op) do { \
12064 int32_t sum; \
12065 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
12066 RESULT(sum, n, 8); \
12067 if (sum >= 0) \
12068 ge |= 1 << n; \
12069 } while (0)
12070
12071
12072 #define ADD16(a, b, n) SARITH16(a, b, n, +)
12073 #define SUB16(a, b, n) SARITH16(a, b, n, -)
12074 #define ADD8(a, b, n) SARITH8(a, b, n, +)
12075 #define SUB8(a, b, n) SARITH8(a, b, n, -)
12076 #define PFX s
12077 #define ARITH_GE
12078
12079 #include "op_addsub.h"
12080
12081 /* Unsigned modulo arithmetic. */
12082 #define ADD16(a, b, n) do { \
12083 uint32_t sum; \
12084 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
12085 RESULT(sum, n, 16); \
12086 if ((sum >> 16) == 1) \
12087 ge |= 3 << (n * 2); \
12088 } while (0)
12089
12090 #define ADD8(a, b, n) do { \
12091 uint32_t sum; \
12092 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
12093 RESULT(sum, n, 8); \
12094 if ((sum >> 8) == 1) \
12095 ge |= 1 << n; \
12096 } while (0)
12097
12098 #define SUB16(a, b, n) do { \
12099 uint32_t sum; \
12100 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
12101 RESULT(sum, n, 16); \
12102 if ((sum >> 16) == 0) \
12103 ge |= 3 << (n * 2); \
12104 } while (0)
12105
12106 #define SUB8(a, b, n) do { \
12107 uint32_t sum; \
12108 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
12109 RESULT(sum, n, 8); \
12110 if ((sum >> 8) == 0) \
12111 ge |= 1 << n; \
12112 } while (0)
12113
12114 #define PFX u
12115 #define ARITH_GE
12116
12117 #include "op_addsub.h"
12118
12119 /* Halved signed arithmetic. */
12120 #define ADD16(a, b, n) \
12121 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
12122 #define SUB16(a, b, n) \
12123 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
12124 #define ADD8(a, b, n) \
12125 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
12126 #define SUB8(a, b, n) \
12127 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
12128 #define PFX sh
12129
12130 #include "op_addsub.h"
12131
12132 /* Halved unsigned arithmetic. */
12133 #define ADD16(a, b, n) \
12134 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12135 #define SUB16(a, b, n) \
12136 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12137 #define ADD8(a, b, n) \
12138 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12139 #define SUB8(a, b, n) \
12140 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12141 #define PFX uh
12142
12143 #include "op_addsub.h"
12144
12145 static inline uint8_t do_usad(uint8_t a, uint8_t b)
12146 {
12147 if (a > b) {
12148 return a - b;
12149 } else {
12150 return b - a;
12151 }
12152 }
12153
12154 /* Unsigned sum of absolute byte differences. */
12155 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
12156 {
12157 uint32_t sum;
12158 sum = do_usad(a, b);
12159 sum += do_usad(a >> 8, b >> 8);
12160 sum += do_usad(a >> 16, b >> 16);
12161 sum += do_usad(a >> 24, b >> 24);
12162 return sum;
12163 }
12164
12165 /* For ARMv6 SEL instruction. */
12166 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
12167 {
12168 uint32_t mask;
12169
12170 mask = 0;
12171 if (flags & 1) {
12172 mask |= 0xff;
12173 }
12174 if (flags & 2) {
12175 mask |= 0xff00;
12176 }
12177 if (flags & 4) {
12178 mask |= 0xff0000;
12179 }
12180 if (flags & 8) {
12181 mask |= 0xff000000;
12182 }
12183 return (a & mask) | (b & ~mask);
12184 }
12185
12186 /*
12187 * CRC helpers.
12188 * The upper bytes of val (above the number specified by 'bytes') must have
12189 * been zeroed out by the caller.
12190 */
12191 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12192 {
12193 uint8_t buf[4];
12194
12195 stl_le_p(buf, val);
12196
12197 /* zlib crc32 converts the accumulator and output to one's complement. */
12198 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12199 }
12200
12201 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12202 {
12203 uint8_t buf[4];
12204
12205 stl_le_p(buf, val);
12206
12207 /* Linux crc32c converts the output to one's complement. */
12208 return crc32c(acc, buf, bytes) ^ 0xffffffff;
12209 }
12210
12211 /*
12212 * Return the exception level to which FP-disabled exceptions should
12213 * be taken, or 0 if FP is enabled.
12214 */
12215 int fp_exception_el(CPUARMState *env, int cur_el)
12216 {
12217 #ifndef CONFIG_USER_ONLY
12218 uint64_t hcr_el2;
12219
12220 /*
12221 * CPACR and the CPTR registers don't exist before v6, so FP is
12222 * always accessible
12223 */
12224 if (!arm_feature(env, ARM_FEATURE_V6)) {
12225 return 0;
12226 }
12227
12228 if (arm_feature(env, ARM_FEATURE_M)) {
12229 /* CPACR can cause a NOCP UsageFault taken to current security state */
12230 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
12231 return 1;
12232 }
12233
12234 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
12235 if (!extract32(env->v7m.nsacr, 10, 1)) {
12236 /* FP insns cause a NOCP UsageFault taken to Secure */
12237 return 3;
12238 }
12239 }
12240
12241 return 0;
12242 }
12243
12244 hcr_el2 = arm_hcr_el2_eff(env);
12245
12246 /*
12247 * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12248 * 0, 2 : trap EL0 and EL1/PL1 accesses
12249 * 1 : trap only EL0 accesses
12250 * 3 : trap no accesses
12251 * This register is ignored if E2H+TGE are both set.
12252 */
12253 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12254 int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
12255
12256 switch (fpen) {
12257 case 1:
12258 if (cur_el != 0) {
12259 break;
12260 }
12261 /* fall through */
12262 case 0:
12263 case 2:
12264 /* Trap from Secure PL0 or PL1 to Secure PL1. */
12265 if (!arm_el_is_aa64(env, 3)
12266 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
12267 return 3;
12268 }
12269 if (cur_el <= 1) {
12270 return 1;
12271 }
12272 break;
12273 }
12274 }
12275
12276 /*
12277 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
12278 * to control non-secure access to the FPU. It doesn't have any
12279 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
12280 */
12281 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
12282 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
12283 if (!extract32(env->cp15.nsacr, 10, 1)) {
12284 /* FP insns act as UNDEF */
12285 return cur_el == 2 ? 2 : 1;
12286 }
12287 }
12288
12289 /*
12290 * CPTR_EL2 is present in v7VE or v8, and changes format
12291 * with HCR_EL2.E2H (regardless of TGE).
12292 */
12293 if (cur_el <= 2) {
12294 if (hcr_el2 & HCR_E2H) {
12295 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
12296 case 1:
12297 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
12298 break;
12299 }
12300 /* fall through */
12301 case 0:
12302 case 2:
12303 return 2;
12304 }
12305 } else if (arm_is_el2_enabled(env)) {
12306 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
12307 return 2;
12308 }
12309 }
12310 }
12311
12312 /* CPTR_EL3 : present in v8 */
12313 if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
12314 /* Trap all FP ops to EL3 */
12315 return 3;
12316 }
12317 #endif
12318 return 0;
12319 }
12320
12321 /* Return the exception level we're running at if this is our mmu_idx */
12322 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12323 {
12324 if (mmu_idx & ARM_MMU_IDX_M) {
12325 return mmu_idx & ARM_MMU_IDX_M_PRIV;
12326 }
12327
12328 switch (mmu_idx) {
12329 case ARMMMUIdx_E10_0:
12330 case ARMMMUIdx_E20_0:
12331 return 0;
12332 case ARMMMUIdx_E10_1:
12333 case ARMMMUIdx_E10_1_PAN:
12334 return 1;
12335 case ARMMMUIdx_E2:
12336 case ARMMMUIdx_E20_2:
12337 case ARMMMUIdx_E20_2_PAN:
12338 return 2;
12339 case ARMMMUIdx_E3:
12340 return 3;
12341 default:
12342 g_assert_not_reached();
12343 }
12344 }
12345
12346 #ifndef CONFIG_TCG
12347 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12348 {
12349 g_assert_not_reached();
12350 }
12351 #endif
12352
12353 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
12354 {
12355 ARMMMUIdx idx;
12356 uint64_t hcr;
12357
12358 if (arm_feature(env, ARM_FEATURE_M)) {
12359 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
12360 }
12361
12362 /* See ARM pseudo-function ELIsInHost. */
12363 switch (el) {
12364 case 0:
12365 hcr = arm_hcr_el2_eff(env);
12366 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
12367 idx = ARMMMUIdx_E20_0;
12368 } else {
12369 idx = ARMMMUIdx_E10_0;
12370 }
12371 break;
12372 case 1:
12373 if (arm_pan_enabled(env)) {
12374 idx = ARMMMUIdx_E10_1_PAN;
12375 } else {
12376 idx = ARMMMUIdx_E10_1;
12377 }
12378 break;
12379 case 2:
12380 /* Note that TGE does not apply at EL2. */
12381 if (arm_hcr_el2_eff(env) & HCR_E2H) {
12382 if (arm_pan_enabled(env)) {
12383 idx = ARMMMUIdx_E20_2_PAN;
12384 } else {
12385 idx = ARMMMUIdx_E20_2;
12386 }
12387 } else {
12388 idx = ARMMMUIdx_E2;
12389 }
12390 break;
12391 case 3:
12392 return ARMMMUIdx_E3;
12393 default:
12394 g_assert_not_reached();
12395 }
12396
12397 return idx;
12398 }
12399
12400 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12401 {
12402 return arm_mmu_idx_el(env, arm_current_el(env));
12403 }
12404
12405 static bool mve_no_pred(CPUARMState *env)
12406 {
12407 /*
12408 * Return true if there is definitely no predication of MVE
12409 * instructions by VPR or LTPSIZE. (Returning false even if there
12410 * isn't any predication is OK; generated code will just be
12411 * a little worse.)
12412 * If the CPU does not implement MVE then this TB flag is always 0.
12413 *
12414 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
12415 * logic in gen_update_fp_context() needs to be updated to match.
12416 *
12417 * We do not include the effect of the ECI bits here -- they are
12418 * tracked in other TB flags. This simplifies the logic for
12419 * "when did we emit code that changes the MVE_NO_PRED TB flag
12420 * and thus need to end the TB?".
12421 */
12422 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
12423 return false;
12424 }
12425 if (env->v7m.vpr) {
12426 return false;
12427 }
12428 if (env->v7m.ltpsize < 4) {
12429 return false;
12430 }
12431 return true;
12432 }
12433
12434 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
12435 uint64_t *cs_base, uint32_t *pflags)
12436 {
12437 CPUARMTBFlags flags;
12438
12439 assert_hflags_rebuild_correctly(env);
12440 flags = env->hflags;
12441
12442 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
12443 *pc = env->pc;
12444 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12445 DP_TBFLAG_A64(flags, BTYPE, env->btype);
12446 }
12447 } else {
12448 *pc = env->regs[15];
12449
12450 if (arm_feature(env, ARM_FEATURE_M)) {
12451 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12452 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12453 != env->v7m.secure) {
12454 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
12455 }
12456
12457 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12458 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12459 (env->v7m.secure &&
12460 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12461 /*
12462 * ASPEN is set, but FPCA/SFPA indicate that there is no
12463 * active FP context; we must create a new FP context before
12464 * executing any FP insn.
12465 */
12466 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
12467 }
12468
12469 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12470 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
12471 DP_TBFLAG_M32(flags, LSPACT, 1);
12472 }
12473
12474 if (mve_no_pred(env)) {
12475 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12476 }
12477 } else {
12478 /*
12479 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12480 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12481 */
12482 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12483 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
12484 } else {
12485 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12486 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
12487 }
12488 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12489 DP_TBFLAG_A32(flags, VFPEN, 1);
12490 }
12491 }
12492
12493 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12494 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
12495 }
12496
12497 /*
12498 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12499 * states defined in the ARM ARM for software singlestep:
12500 * SS_ACTIVE PSTATE.SS State
12501 * 0 x Inactive (the TB flag for SS is always 0)
12502 * 1 0 Active-pending
12503 * 1 1 Active-not-pending
12504 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
12505 */
12506 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12507 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
12508 }
12509
12510 *pflags = flags.flags;
12511 *cs_base = flags.flags2;
12512 }
12513
12514 #ifdef TARGET_AARCH64
12515 /*
12516 * The manual says that when SVE is enabled and VQ is widened the
12517 * implementation is allowed to zero the previously inaccessible
12518 * portion of the registers. The corollary to that is that when
12519 * SVE is enabled and VQ is narrowed we are also allowed to zero
12520 * the now inaccessible portion of the registers.
12521 *
12522 * The intent of this is that no predicate bit beyond VQ is ever set.
12523 * Which means that some operations on predicate registers themselves
12524 * may operate on full uint64_t or even unrolled across the maximum
12525 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
12526 * may well be cheaper than conditionals to restrict the operation
12527 * to the relevant portion of a uint16_t[16].
12528 */
12529 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12530 {
12531 int i, j;
12532 uint64_t pmask;
12533
12534 assert(vq >= 1 && vq <= ARM_MAX_VQ);
12535 assert(vq <= env_archcpu(env)->sve_max_vq);
12536
12537 /* Zap the high bits of the zregs. */
12538 for (i = 0; i < 32; i++) {
12539 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12540 }
12541
12542 /* Zap the high bits of the pregs and ffr. */
12543 pmask = 0;
12544 if (vq & 3) {
12545 pmask = ~(-1ULL << (16 * (vq & 3)));
12546 }
12547 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12548 for (i = 0; i < 17; ++i) {
12549 env->vfp.pregs[i].p[j] &= pmask;
12550 }
12551 pmask = 0;
12552 }
12553 }
12554
12555 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
12556 {
12557 int exc_el;
12558
12559 if (sm) {
12560 exc_el = sme_exception_el(env, el);
12561 } else {
12562 exc_el = sve_exception_el(env, el);
12563 }
12564 if (exc_el) {
12565 return 0; /* disabled */
12566 }
12567 return sve_vqm1_for_el_sm(env, el, sm);
12568 }
12569
12570 /*
12571 * Notice a change in SVE vector size when changing EL.
12572 */
12573 void aarch64_sve_change_el(CPUARMState *env, int old_el,
12574 int new_el, bool el0_a64)
12575 {
12576 ARMCPU *cpu = env_archcpu(env);
12577 int old_len, new_len;
12578 bool old_a64, new_a64, sm;
12579
12580 /* Nothing to do if no SVE. */
12581 if (!cpu_isar_feature(aa64_sve, cpu)) {
12582 return;
12583 }
12584
12585 /* Nothing to do if FP is disabled in either EL. */
12586 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12587 return;
12588 }
12589
12590 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12591 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12592
12593 /*
12594 * Both AArch64.TakeException and AArch64.ExceptionReturn
12595 * invoke ResetSVEState when taking an exception from, or
12596 * returning to, AArch32 state when PSTATE.SM is enabled.
12597 */
12598 sm = FIELD_EX64(env->svcr, SVCR, SM);
12599 if (old_a64 != new_a64 && sm) {
12600 arm_reset_sve_state(env);
12601 return;
12602 }
12603
12604 /*
12605 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12606 * at ELx, or not available because the EL is in AArch32 state, then
12607 * for all purposes other than a direct read, the ZCR_ELx.LEN field
12608 * has an effective value of 0".
12609 *
12610 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12611 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12612 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
12613 * we already have the correct register contents when encountering the
12614 * vq0->vq0 transition between EL0->EL1.
12615 */
12616 old_len = new_len = 0;
12617 if (old_a64) {
12618 old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12619 }
12620 if (new_a64) {
12621 new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12622 }
12623
12624 /* When changing vector length, clear inaccessible state. */
12625 if (new_len < old_len) {
12626 aarch64_sve_narrow_vq(env, new_len + 1);
12627 }
12628 }
12629 #endif
12630
12631 #ifndef CONFIG_USER_ONLY
12632 ARMSecuritySpace arm_security_space(CPUARMState *env)
12633 {
12634 if (arm_feature(env, ARM_FEATURE_M)) {
12635 return arm_secure_to_space(env->v7m.secure);
12636 }
12637
12638 /*
12639 * If EL3 is not supported then the secure state is implementation
12640 * defined, in which case QEMU defaults to non-secure.
12641 */
12642 if (!arm_feature(env, ARM_FEATURE_EL3)) {
12643 return ARMSS_NonSecure;
12644 }
12645
12646 /* Check for AArch64 EL3 or AArch32 Mon. */
12647 if (is_a64(env)) {
12648 if (extract32(env->pstate, 2, 2) == 3) {
12649 if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
12650 return ARMSS_Root;
12651 } else {
12652 return ARMSS_Secure;
12653 }
12654 }
12655 } else {
12656 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
12657 return ARMSS_Secure;
12658 }
12659 }
12660
12661 return arm_security_space_below_el3(env);
12662 }
12663
12664 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
12665 {
12666 assert(!arm_feature(env, ARM_FEATURE_M));
12667
12668 /*
12669 * If EL3 is not supported then the secure state is implementation
12670 * defined, in which case QEMU defaults to non-secure.
12671 */
12672 if (!arm_feature(env, ARM_FEATURE_EL3)) {
12673 return ARMSS_NonSecure;
12674 }
12675
12676 /*
12677 * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
12678 * Ignoring NSE when !NS retains consistency without having to
12679 * modify other predicates.
12680 */
12681 if (!(env->cp15.scr_el3 & SCR_NS)) {
12682 return ARMSS_Secure;
12683 } else if (env->cp15.scr_el3 & SCR_NSE) {
12684 return ARMSS_Realm;
12685 } else {
12686 return ARMSS_NonSecure;
12687 }
12688 }
12689 #endif /* !CONFIG_USER_ONLY */