]> git.proxmox.com Git - mirror_qemu.git/blob - target/arm/helper.c
target/arm: Handle HCR_EL2 accesses for FEAT_NV2 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
34 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
35
36 static void switch_mode(CPUARMState *env, int mode);
37
38 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
39 {
40 assert(ri->fieldoffset);
41 if (cpreg_field_is_64bit(ri)) {
42 return CPREG_FIELD64(env, ri);
43 } else {
44 return CPREG_FIELD32(env, ri);
45 }
46 }
47
48 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
49 {
50 assert(ri->fieldoffset);
51 if (cpreg_field_is_64bit(ri)) {
52 CPREG_FIELD64(env, ri) = value;
53 } else {
54 CPREG_FIELD32(env, ri) = value;
55 }
56 }
57
58 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
59 {
60 return (char *)env + ri->fieldoffset;
61 }
62
63 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
64 {
65 /* Raw read of a coprocessor register (as needed for migration, etc). */
66 if (ri->type & ARM_CP_CONST) {
67 return ri->resetvalue;
68 } else if (ri->raw_readfn) {
69 return ri->raw_readfn(env, ri);
70 } else if (ri->readfn) {
71 return ri->readfn(env, ri);
72 } else {
73 return raw_read(env, ri);
74 }
75 }
76
77 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
78 uint64_t v)
79 {
80 /*
81 * Raw write of a coprocessor register (as needed for migration, etc).
82 * Note that constant registers are treated as write-ignored; the
83 * caller should check for success by whether a readback gives the
84 * value written.
85 */
86 if (ri->type & ARM_CP_CONST) {
87 return;
88 } else if (ri->raw_writefn) {
89 ri->raw_writefn(env, ri, v);
90 } else if (ri->writefn) {
91 ri->writefn(env, ri, v);
92 } else {
93 raw_write(env, ri, v);
94 }
95 }
96
97 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
98 {
99 /*
100 * Return true if the regdef would cause an assertion if you called
101 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
102 * program bug for it not to have the NO_RAW flag).
103 * NB that returning false here doesn't necessarily mean that calling
104 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
105 * read/write access functions which are safe for raw use" from "has
106 * read/write access functions which have side effects but has forgotten
107 * to provide raw access functions".
108 * The tests here line up with the conditions in read/write_raw_cp_reg()
109 * and assertions in raw_read()/raw_write().
110 */
111 if ((ri->type & ARM_CP_CONST) ||
112 ri->fieldoffset ||
113 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
114 return false;
115 }
116 return true;
117 }
118
119 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
120 {
121 /* Write the coprocessor state from cpu->env to the (index,value) list. */
122 int i;
123 bool ok = true;
124
125 for (i = 0; i < cpu->cpreg_array_len; i++) {
126 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
127 const ARMCPRegInfo *ri;
128 uint64_t newval;
129
130 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
131 if (!ri) {
132 ok = false;
133 continue;
134 }
135 if (ri->type & ARM_CP_NO_RAW) {
136 continue;
137 }
138
139 newval = read_raw_cp_reg(&cpu->env, ri);
140 if (kvm_sync) {
141 /*
142 * Only sync if the previous list->cpustate sync succeeded.
143 * Rather than tracking the success/failure state for every
144 * item in the list, we just recheck "does the raw write we must
145 * have made in write_list_to_cpustate() read back OK" here.
146 */
147 uint64_t oldval = cpu->cpreg_values[i];
148
149 if (oldval == newval) {
150 continue;
151 }
152
153 write_raw_cp_reg(&cpu->env, ri, oldval);
154 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
155 continue;
156 }
157
158 write_raw_cp_reg(&cpu->env, ri, newval);
159 }
160 cpu->cpreg_values[i] = newval;
161 }
162 return ok;
163 }
164
165 bool write_list_to_cpustate(ARMCPU *cpu)
166 {
167 int i;
168 bool ok = true;
169
170 for (i = 0; i < cpu->cpreg_array_len; i++) {
171 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
172 uint64_t v = cpu->cpreg_values[i];
173 const ARMCPRegInfo *ri;
174
175 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
176 if (!ri) {
177 ok = false;
178 continue;
179 }
180 if (ri->type & ARM_CP_NO_RAW) {
181 continue;
182 }
183 /*
184 * Write value and confirm it reads back as written
185 * (to catch read-only registers and partially read-only
186 * registers where the incoming migration value doesn't match)
187 */
188 write_raw_cp_reg(&cpu->env, ri, v);
189 if (read_raw_cp_reg(&cpu->env, ri) != v) {
190 ok = false;
191 }
192 }
193 return ok;
194 }
195
196 static void add_cpreg_to_list(gpointer key, gpointer opaque)
197 {
198 ARMCPU *cpu = opaque;
199 uint32_t regidx = (uintptr_t)key;
200 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
201
202 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
203 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
204 /* The value array need not be initialized at this point */
205 cpu->cpreg_array_len++;
206 }
207 }
208
209 static void count_cpreg(gpointer key, gpointer opaque)
210 {
211 ARMCPU *cpu = opaque;
212 const ARMCPRegInfo *ri;
213
214 ri = g_hash_table_lookup(cpu->cp_regs, key);
215
216 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
217 cpu->cpreg_array_len++;
218 }
219 }
220
221 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
222 {
223 uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
224 uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
225
226 if (aidx > bidx) {
227 return 1;
228 }
229 if (aidx < bidx) {
230 return -1;
231 }
232 return 0;
233 }
234
235 void init_cpreg_list(ARMCPU *cpu)
236 {
237 /*
238 * Initialise the cpreg_tuples[] array based on the cp_regs hash.
239 * Note that we require cpreg_tuples[] to be sorted by key ID.
240 */
241 GList *keys;
242 int arraylen;
243
244 keys = g_hash_table_get_keys(cpu->cp_regs);
245 keys = g_list_sort(keys, cpreg_key_compare);
246
247 cpu->cpreg_array_len = 0;
248
249 g_list_foreach(keys, count_cpreg, cpu);
250
251 arraylen = cpu->cpreg_array_len;
252 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
253 cpu->cpreg_values = g_new(uint64_t, arraylen);
254 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
255 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
256 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
257 cpu->cpreg_array_len = 0;
258
259 g_list_foreach(keys, add_cpreg_to_list, cpu);
260
261 assert(cpu->cpreg_array_len == arraylen);
262
263 g_list_free(keys);
264 }
265
266 static bool arm_pan_enabled(CPUARMState *env)
267 {
268 if (is_a64(env)) {
269 if ((arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1)) == (HCR_NV | HCR_NV1)) {
270 return false;
271 }
272 return env->pstate & PSTATE_PAN;
273 } else {
274 return env->uncached_cpsr & CPSR_PAN;
275 }
276 }
277
278 /*
279 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
280 */
281 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
282 const ARMCPRegInfo *ri,
283 bool isread)
284 {
285 if (!is_a64(env) && arm_current_el(env) == 3 &&
286 arm_is_secure_below_el3(env)) {
287 return CP_ACCESS_TRAP_UNCATEGORIZED;
288 }
289 return CP_ACCESS_OK;
290 }
291
292 /*
293 * Some secure-only AArch32 registers trap to EL3 if used from
294 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
295 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
296 * We assume that the .access field is set to PL1_RW.
297 */
298 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
299 const ARMCPRegInfo *ri,
300 bool isread)
301 {
302 if (arm_current_el(env) == 3) {
303 return CP_ACCESS_OK;
304 }
305 if (arm_is_secure_below_el3(env)) {
306 if (env->cp15.scr_el3 & SCR_EEL2) {
307 return CP_ACCESS_TRAP_EL2;
308 }
309 return CP_ACCESS_TRAP_EL3;
310 }
311 /* This will be EL1 NS and EL2 NS, which just UNDEF */
312 return CP_ACCESS_TRAP_UNCATEGORIZED;
313 }
314
315 /*
316 * Check for traps to performance monitor registers, which are controlled
317 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
318 */
319 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
320 bool isread)
321 {
322 int el = arm_current_el(env);
323 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
324
325 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
326 return CP_ACCESS_TRAP_EL2;
327 }
328 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
329 return CP_ACCESS_TRAP_EL3;
330 }
331 return CP_ACCESS_OK;
332 }
333
334 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
335 CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
336 bool isread)
337 {
338 if (arm_current_el(env) == 1) {
339 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
340 if (arm_hcr_el2_eff(env) & trap) {
341 return CP_ACCESS_TRAP_EL2;
342 }
343 }
344 return CP_ACCESS_OK;
345 }
346
347 /* Check for traps from EL1 due to HCR_EL2.TSW. */
348 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
349 bool isread)
350 {
351 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
352 return CP_ACCESS_TRAP_EL2;
353 }
354 return CP_ACCESS_OK;
355 }
356
357 /* Check for traps from EL1 due to HCR_EL2.TACR. */
358 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
359 bool isread)
360 {
361 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
362 return CP_ACCESS_TRAP_EL2;
363 }
364 return CP_ACCESS_OK;
365 }
366
367 /* Check for traps from EL1 due to HCR_EL2.TTLB. */
368 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
369 bool isread)
370 {
371 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
372 return CP_ACCESS_TRAP_EL2;
373 }
374 return CP_ACCESS_OK;
375 }
376
377 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBIS. */
378 static CPAccessResult access_ttlbis(CPUARMState *env, const ARMCPRegInfo *ri,
379 bool isread)
380 {
381 if (arm_current_el(env) == 1 &&
382 (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBIS))) {
383 return CP_ACCESS_TRAP_EL2;
384 }
385 return CP_ACCESS_OK;
386 }
387
388 #ifdef TARGET_AARCH64
389 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBOS. */
390 static CPAccessResult access_ttlbos(CPUARMState *env, const ARMCPRegInfo *ri,
391 bool isread)
392 {
393 if (arm_current_el(env) == 1 &&
394 (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBOS))) {
395 return CP_ACCESS_TRAP_EL2;
396 }
397 return CP_ACCESS_OK;
398 }
399 #endif
400
401 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
402 {
403 ARMCPU *cpu = env_archcpu(env);
404
405 raw_write(env, ri, value);
406 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
407 }
408
409 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
410 {
411 ARMCPU *cpu = env_archcpu(env);
412
413 if (raw_read(env, ri) != value) {
414 /*
415 * Unlike real hardware the qemu TLB uses virtual addresses,
416 * not modified virtual addresses, so this causes a TLB flush.
417 */
418 tlb_flush(CPU(cpu));
419 raw_write(env, ri, value);
420 }
421 }
422
423 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
424 uint64_t value)
425 {
426 ARMCPU *cpu = env_archcpu(env);
427
428 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
429 && !extended_addresses_enabled(env)) {
430 /*
431 * For VMSA (when not using the LPAE long descriptor page table
432 * format) this register includes the ASID, so do a TLB flush.
433 * For PMSA it is purely a process ID and no action is needed.
434 */
435 tlb_flush(CPU(cpu));
436 }
437 raw_write(env, ri, value);
438 }
439
440 static int alle1_tlbmask(CPUARMState *env)
441 {
442 /*
443 * Note that the 'ALL' scope must invalidate both stage 1 and
444 * stage 2 translations, whereas most other scopes only invalidate
445 * stage 1 translations.
446 */
447 return (ARMMMUIdxBit_E10_1 |
448 ARMMMUIdxBit_E10_1_PAN |
449 ARMMMUIdxBit_E10_0 |
450 ARMMMUIdxBit_Stage2 |
451 ARMMMUIdxBit_Stage2_S);
452 }
453
454
455 /* IS variants of TLB operations must affect all cores */
456 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
457 uint64_t value)
458 {
459 CPUState *cs = env_cpu(env);
460
461 tlb_flush_all_cpus_synced(cs);
462 }
463
464 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
465 uint64_t value)
466 {
467 CPUState *cs = env_cpu(env);
468
469 tlb_flush_all_cpus_synced(cs);
470 }
471
472 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
473 uint64_t value)
474 {
475 CPUState *cs = env_cpu(env);
476
477 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
478 }
479
480 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
481 uint64_t value)
482 {
483 CPUState *cs = env_cpu(env);
484
485 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
486 }
487
488 /*
489 * Non-IS variants of TLB operations are upgraded to
490 * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
491 * force broadcast of these operations.
492 */
493 static bool tlb_force_broadcast(CPUARMState *env)
494 {
495 return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
496 }
497
498 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
499 uint64_t value)
500 {
501 /* Invalidate all (TLBIALL) */
502 CPUState *cs = env_cpu(env);
503
504 if (tlb_force_broadcast(env)) {
505 tlb_flush_all_cpus_synced(cs);
506 } else {
507 tlb_flush(cs);
508 }
509 }
510
511 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
512 uint64_t value)
513 {
514 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
515 CPUState *cs = env_cpu(env);
516
517 value &= TARGET_PAGE_MASK;
518 if (tlb_force_broadcast(env)) {
519 tlb_flush_page_all_cpus_synced(cs, value);
520 } else {
521 tlb_flush_page(cs, value);
522 }
523 }
524
525 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
526 uint64_t value)
527 {
528 /* Invalidate by ASID (TLBIASID) */
529 CPUState *cs = env_cpu(env);
530
531 if (tlb_force_broadcast(env)) {
532 tlb_flush_all_cpus_synced(cs);
533 } else {
534 tlb_flush(cs);
535 }
536 }
537
538 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
539 uint64_t value)
540 {
541 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
542 CPUState *cs = env_cpu(env);
543
544 value &= TARGET_PAGE_MASK;
545 if (tlb_force_broadcast(env)) {
546 tlb_flush_page_all_cpus_synced(cs, value);
547 } else {
548 tlb_flush_page(cs, value);
549 }
550 }
551
552 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
553 uint64_t value)
554 {
555 CPUState *cs = env_cpu(env);
556
557 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
558 }
559
560 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
561 uint64_t value)
562 {
563 CPUState *cs = env_cpu(env);
564
565 tlb_flush_by_mmuidx_all_cpus_synced(cs, alle1_tlbmask(env));
566 }
567
568
569 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
570 uint64_t value)
571 {
572 CPUState *cs = env_cpu(env);
573
574 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
575 }
576
577 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
578 uint64_t value)
579 {
580 CPUState *cs = env_cpu(env);
581
582 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
583 }
584
585 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
586 uint64_t value)
587 {
588 CPUState *cs = env_cpu(env);
589 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
590
591 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
592 }
593
594 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
595 uint64_t value)
596 {
597 CPUState *cs = env_cpu(env);
598 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
599
600 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
601 ARMMMUIdxBit_E2);
602 }
603
604 static void tlbiipas2_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
605 uint64_t value)
606 {
607 CPUState *cs = env_cpu(env);
608 uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
609
610 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
611 }
612
613 static void tlbiipas2is_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
614 uint64_t value)
615 {
616 CPUState *cs = env_cpu(env);
617 uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
618
619 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, ARMMMUIdxBit_Stage2);
620 }
621
622 static const ARMCPRegInfo cp_reginfo[] = {
623 /*
624 * Define the secure and non-secure FCSE identifier CP registers
625 * separately because there is no secure bank in V8 (no _EL3). This allows
626 * the secure register to be properly reset and migrated. There is also no
627 * v8 EL1 version of the register so the non-secure instance stands alone.
628 */
629 { .name = "FCSEIDR",
630 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
631 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
632 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
633 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
634 { .name = "FCSEIDR_S",
635 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
636 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
637 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
638 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
639 /*
640 * Define the secure and non-secure context identifier CP registers
641 * separately because there is no secure bank in V8 (no _EL3). This allows
642 * the secure register to be properly reset and migrated. In the
643 * non-secure case, the 32-bit register will have reset and migration
644 * disabled during registration as it is handled by the 64-bit instance.
645 */
646 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
647 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
648 .access = PL1_RW, .accessfn = access_tvm_trvm,
649 .fgt = FGT_CONTEXTIDR_EL1,
650 .secure = ARM_CP_SECSTATE_NS,
651 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
652 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
653 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
654 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
655 .access = PL1_RW, .accessfn = access_tvm_trvm,
656 .secure = ARM_CP_SECSTATE_S,
657 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
658 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
659 };
660
661 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
662 /*
663 * NB: Some of these registers exist in v8 but with more precise
664 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
665 */
666 /* MMU Domain access control / MPU write buffer control */
667 { .name = "DACR",
668 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
669 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
670 .writefn = dacr_write, .raw_writefn = raw_write,
671 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
672 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
673 /*
674 * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
675 * For v6 and v5, these mappings are overly broad.
676 */
677 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
678 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
679 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
680 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
681 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
682 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
683 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
684 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
685 /* Cache maintenance ops; some of this space may be overridden later. */
686 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
687 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
688 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
689 };
690
691 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
692 /*
693 * Not all pre-v6 cores implemented this WFI, so this is slightly
694 * over-broad.
695 */
696 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
697 .access = PL1_W, .type = ARM_CP_WFI },
698 };
699
700 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
701 /*
702 * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
703 * is UNPREDICTABLE; we choose to NOP as most implementations do).
704 */
705 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
706 .access = PL1_W, .type = ARM_CP_WFI },
707 /*
708 * L1 cache lockdown. Not architectural in v6 and earlier but in practice
709 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
710 * OMAPCP will override this space.
711 */
712 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
713 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
714 .resetvalue = 0 },
715 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
716 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
717 .resetvalue = 0 },
718 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
719 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
720 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
721 .resetvalue = 0 },
722 /*
723 * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
724 * implementing it as RAZ means the "debug architecture version" bits
725 * will read as a reserved value, which should cause Linux to not try
726 * to use the debug hardware.
727 */
728 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
729 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
730 /*
731 * MMU TLB control. Note that the wildcarding means we cover not just
732 * the unified TLB ops but also the dside/iside/inner-shareable variants.
733 */
734 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
735 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
736 .type = ARM_CP_NO_RAW },
737 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
738 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
739 .type = ARM_CP_NO_RAW },
740 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
741 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
742 .type = ARM_CP_NO_RAW },
743 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
744 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
745 .type = ARM_CP_NO_RAW },
746 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
747 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
748 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
749 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
750 };
751
752 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
753 uint64_t value)
754 {
755 uint32_t mask = 0;
756
757 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
758 if (!arm_feature(env, ARM_FEATURE_V8)) {
759 /*
760 * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
761 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
762 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
763 */
764 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
765 /* VFP coprocessor: cp10 & cp11 [23:20] */
766 mask |= R_CPACR_ASEDIS_MASK |
767 R_CPACR_D32DIS_MASK |
768 R_CPACR_CP11_MASK |
769 R_CPACR_CP10_MASK;
770
771 if (!arm_feature(env, ARM_FEATURE_NEON)) {
772 /* ASEDIS [31] bit is RAO/WI */
773 value |= R_CPACR_ASEDIS_MASK;
774 }
775
776 /*
777 * VFPv3 and upwards with NEON implement 32 double precision
778 * registers (D0-D31).
779 */
780 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
781 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
782 value |= R_CPACR_D32DIS_MASK;
783 }
784 }
785 value &= mask;
786 }
787
788 /*
789 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
790 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
791 */
792 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
793 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
794 mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
795 value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
796 }
797
798 env->cp15.cpacr_el1 = value;
799 }
800
801 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
802 {
803 /*
804 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
805 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
806 */
807 uint64_t value = env->cp15.cpacr_el1;
808
809 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
810 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
811 value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
812 }
813 return value;
814 }
815
816
817 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
818 {
819 /*
820 * Call cpacr_write() so that we reset with the correct RAO bits set
821 * for our CPU features.
822 */
823 cpacr_write(env, ri, 0);
824 }
825
826 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
827 bool isread)
828 {
829 if (arm_feature(env, ARM_FEATURE_V8)) {
830 /* Check if CPACR accesses are to be trapped to EL2 */
831 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
832 FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
833 return CP_ACCESS_TRAP_EL2;
834 /* Check if CPACR accesses are to be trapped to EL3 */
835 } else if (arm_current_el(env) < 3 &&
836 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
837 return CP_ACCESS_TRAP_EL3;
838 }
839 }
840
841 return CP_ACCESS_OK;
842 }
843
844 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
845 bool isread)
846 {
847 /* Check if CPTR accesses are set to trap to EL3 */
848 if (arm_current_el(env) == 2 &&
849 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
850 return CP_ACCESS_TRAP_EL3;
851 }
852
853 return CP_ACCESS_OK;
854 }
855
856 static const ARMCPRegInfo v6_cp_reginfo[] = {
857 /* prefetch by MVA in v6, NOP in v7 */
858 { .name = "MVA_prefetch",
859 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
860 .access = PL1_W, .type = ARM_CP_NOP },
861 /*
862 * We need to break the TB after ISB to execute self-modifying code
863 * correctly and also to take any pending interrupts immediately.
864 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
865 */
866 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
867 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
868 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
869 .access = PL0_W, .type = ARM_CP_NOP },
870 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
871 .access = PL0_W, .type = ARM_CP_NOP },
872 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
873 .access = PL1_RW, .accessfn = access_tvm_trvm,
874 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
875 offsetof(CPUARMState, cp15.ifar_ns) },
876 .resetvalue = 0, },
877 /*
878 * Watchpoint Fault Address Register : should actually only be present
879 * for 1136, 1176, 11MPCore.
880 */
881 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
882 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
883 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
884 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
885 .fgt = FGT_CPACR_EL1,
886 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
887 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
888 };
889
890 typedef struct pm_event {
891 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
892 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
893 bool (*supported)(CPUARMState *);
894 /*
895 * Retrieve the current count of the underlying event. The programmed
896 * counters hold a difference from the return value from this function
897 */
898 uint64_t (*get_count)(CPUARMState *);
899 /*
900 * Return how many nanoseconds it will take (at a minimum) for count events
901 * to occur. A negative value indicates the counter will never overflow, or
902 * that the counter has otherwise arranged for the overflow bit to be set
903 * and the PMU interrupt to be raised on overflow.
904 */
905 int64_t (*ns_per_count)(uint64_t);
906 } pm_event;
907
908 static bool event_always_supported(CPUARMState *env)
909 {
910 return true;
911 }
912
913 static uint64_t swinc_get_count(CPUARMState *env)
914 {
915 /*
916 * SW_INCR events are written directly to the pmevcntr's by writes to
917 * PMSWINC, so there is no underlying count maintained by the PMU itself
918 */
919 return 0;
920 }
921
922 static int64_t swinc_ns_per(uint64_t ignored)
923 {
924 return -1;
925 }
926
927 /*
928 * Return the underlying cycle count for the PMU cycle counters. If we're in
929 * usermode, simply return 0.
930 */
931 static uint64_t cycles_get_count(CPUARMState *env)
932 {
933 #ifndef CONFIG_USER_ONLY
934 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
935 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
936 #else
937 return cpu_get_host_ticks();
938 #endif
939 }
940
941 #ifndef CONFIG_USER_ONLY
942 static int64_t cycles_ns_per(uint64_t cycles)
943 {
944 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
945 }
946
947 static bool instructions_supported(CPUARMState *env)
948 {
949 return icount_enabled() == 1; /* Precise instruction counting */
950 }
951
952 static uint64_t instructions_get_count(CPUARMState *env)
953 {
954 return (uint64_t)icount_get_raw();
955 }
956
957 static int64_t instructions_ns_per(uint64_t icount)
958 {
959 return icount_to_ns((int64_t)icount);
960 }
961 #endif
962
963 static bool pmuv3p1_events_supported(CPUARMState *env)
964 {
965 /* For events which are supported in any v8.1 PMU */
966 return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
967 }
968
969 static bool pmuv3p4_events_supported(CPUARMState *env)
970 {
971 /* For events which are supported in any v8.1 PMU */
972 return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
973 }
974
975 static uint64_t zero_event_get_count(CPUARMState *env)
976 {
977 /* For events which on QEMU never fire, so their count is always zero */
978 return 0;
979 }
980
981 static int64_t zero_event_ns_per(uint64_t cycles)
982 {
983 /* An event which never fires can never overflow */
984 return -1;
985 }
986
987 static const pm_event pm_events[] = {
988 { .number = 0x000, /* SW_INCR */
989 .supported = event_always_supported,
990 .get_count = swinc_get_count,
991 .ns_per_count = swinc_ns_per,
992 },
993 #ifndef CONFIG_USER_ONLY
994 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
995 .supported = instructions_supported,
996 .get_count = instructions_get_count,
997 .ns_per_count = instructions_ns_per,
998 },
999 { .number = 0x011, /* CPU_CYCLES, Cycle */
1000 .supported = event_always_supported,
1001 .get_count = cycles_get_count,
1002 .ns_per_count = cycles_ns_per,
1003 },
1004 #endif
1005 { .number = 0x023, /* STALL_FRONTEND */
1006 .supported = pmuv3p1_events_supported,
1007 .get_count = zero_event_get_count,
1008 .ns_per_count = zero_event_ns_per,
1009 },
1010 { .number = 0x024, /* STALL_BACKEND */
1011 .supported = pmuv3p1_events_supported,
1012 .get_count = zero_event_get_count,
1013 .ns_per_count = zero_event_ns_per,
1014 },
1015 { .number = 0x03c, /* STALL */
1016 .supported = pmuv3p4_events_supported,
1017 .get_count = zero_event_get_count,
1018 .ns_per_count = zero_event_ns_per,
1019 },
1020 };
1021
1022 /*
1023 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1024 * events (i.e. the statistical profiling extension), this implementation
1025 * should first be updated to something sparse instead of the current
1026 * supported_event_map[] array.
1027 */
1028 #define MAX_EVENT_ID 0x3c
1029 #define UNSUPPORTED_EVENT UINT16_MAX
1030 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1031
1032 /*
1033 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1034 * of ARM event numbers to indices in our pm_events array.
1035 *
1036 * Note: Events in the 0x40XX range are not currently supported.
1037 */
1038 void pmu_init(ARMCPU *cpu)
1039 {
1040 unsigned int i;
1041
1042 /*
1043 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1044 * events to them
1045 */
1046 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1047 supported_event_map[i] = UNSUPPORTED_EVENT;
1048 }
1049 cpu->pmceid0 = 0;
1050 cpu->pmceid1 = 0;
1051
1052 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1053 const pm_event *cnt = &pm_events[i];
1054 assert(cnt->number <= MAX_EVENT_ID);
1055 /* We do not currently support events in the 0x40xx range */
1056 assert(cnt->number <= 0x3f);
1057
1058 if (cnt->supported(&cpu->env)) {
1059 supported_event_map[cnt->number] = i;
1060 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
1061 if (cnt->number & 0x20) {
1062 cpu->pmceid1 |= event_mask;
1063 } else {
1064 cpu->pmceid0 |= event_mask;
1065 }
1066 }
1067 }
1068 }
1069
1070 /*
1071 * Check at runtime whether a PMU event is supported for the current machine
1072 */
1073 static bool event_supported(uint16_t number)
1074 {
1075 if (number > MAX_EVENT_ID) {
1076 return false;
1077 }
1078 return supported_event_map[number] != UNSUPPORTED_EVENT;
1079 }
1080
1081 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1082 bool isread)
1083 {
1084 /*
1085 * Performance monitor registers user accessibility is controlled
1086 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1087 * trapping to EL2 or EL3 for other accesses.
1088 */
1089 int el = arm_current_el(env);
1090 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1091
1092 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1093 return CP_ACCESS_TRAP;
1094 }
1095 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1096 return CP_ACCESS_TRAP_EL2;
1097 }
1098 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1099 return CP_ACCESS_TRAP_EL3;
1100 }
1101
1102 return CP_ACCESS_OK;
1103 }
1104
1105 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1106 const ARMCPRegInfo *ri,
1107 bool isread)
1108 {
1109 /* ER: event counter read trap control */
1110 if (arm_feature(env, ARM_FEATURE_V8)
1111 && arm_current_el(env) == 0
1112 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1113 && isread) {
1114 return CP_ACCESS_OK;
1115 }
1116
1117 return pmreg_access(env, ri, isread);
1118 }
1119
1120 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1121 const ARMCPRegInfo *ri,
1122 bool isread)
1123 {
1124 /* SW: software increment write trap control */
1125 if (arm_feature(env, ARM_FEATURE_V8)
1126 && arm_current_el(env) == 0
1127 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1128 && !isread) {
1129 return CP_ACCESS_OK;
1130 }
1131
1132 return pmreg_access(env, ri, isread);
1133 }
1134
1135 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1136 const ARMCPRegInfo *ri,
1137 bool isread)
1138 {
1139 /* ER: event counter read trap control */
1140 if (arm_feature(env, ARM_FEATURE_V8)
1141 && arm_current_el(env) == 0
1142 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1143 return CP_ACCESS_OK;
1144 }
1145
1146 return pmreg_access(env, ri, isread);
1147 }
1148
1149 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1150 const ARMCPRegInfo *ri,
1151 bool isread)
1152 {
1153 /* CR: cycle counter read trap control */
1154 if (arm_feature(env, ARM_FEATURE_V8)
1155 && arm_current_el(env) == 0
1156 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1157 && isread) {
1158 return CP_ACCESS_OK;
1159 }
1160
1161 return pmreg_access(env, ri, isread);
1162 }
1163
1164 /*
1165 * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1166 * We use these to decide whether we need to wrap a write to MDCR_EL2
1167 * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1168 */
1169 #define MDCR_EL2_PMU_ENABLE_BITS \
1170 (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
1171 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
1172
1173 /*
1174 * Returns true if the counter (pass 31 for PMCCNTR) should count events using
1175 * the current EL, security state, and register configuration.
1176 */
1177 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1178 {
1179 uint64_t filter;
1180 bool e, p, u, nsk, nsu, nsh, m;
1181 bool enabled, prohibited = false, filtered;
1182 bool secure = arm_is_secure(env);
1183 int el = arm_current_el(env);
1184 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1185 uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
1186
1187 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1188 return false;
1189 }
1190
1191 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1192 (counter < hpmn || counter == 31)) {
1193 e = env->cp15.c9_pmcr & PMCRE;
1194 } else {
1195 e = mdcr_el2 & MDCR_HPME;
1196 }
1197 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1198
1199 /* Is event counting prohibited? */
1200 if (el == 2 && (counter < hpmn || counter == 31)) {
1201 prohibited = mdcr_el2 & MDCR_HPMD;
1202 }
1203 if (secure) {
1204 prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1205 }
1206
1207 if (counter == 31) {
1208 /*
1209 * The cycle counter defaults to running. PMCR.DP says "disable
1210 * the cycle counter when event counting is prohibited".
1211 * Some MDCR bits disable the cycle counter specifically.
1212 */
1213 prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1214 if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1215 if (secure) {
1216 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1217 }
1218 if (el == 2) {
1219 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1220 }
1221 }
1222 }
1223
1224 if (counter == 31) {
1225 filter = env->cp15.pmccfiltr_el0;
1226 } else {
1227 filter = env->cp15.c14_pmevtyper[counter];
1228 }
1229
1230 p = filter & PMXEVTYPER_P;
1231 u = filter & PMXEVTYPER_U;
1232 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1233 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1234 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1235 m = arm_el_is_aa64(env, 1) &&
1236 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1237
1238 if (el == 0) {
1239 filtered = secure ? u : u != nsu;
1240 } else if (el == 1) {
1241 filtered = secure ? p : p != nsk;
1242 } else if (el == 2) {
1243 filtered = !nsh;
1244 } else { /* EL3 */
1245 filtered = m != p;
1246 }
1247
1248 if (counter != 31) {
1249 /*
1250 * If not checking PMCCNTR, ensure the counter is setup to an event we
1251 * support
1252 */
1253 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1254 if (!event_supported(event)) {
1255 return false;
1256 }
1257 }
1258
1259 return enabled && !prohibited && !filtered;
1260 }
1261
1262 static void pmu_update_irq(CPUARMState *env)
1263 {
1264 ARMCPU *cpu = env_archcpu(env);
1265 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1266 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1267 }
1268
1269 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1270 {
1271 /*
1272 * Return true if the clock divider is enabled and the cycle counter
1273 * is supposed to tick only once every 64 clock cycles. This is
1274 * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1275 * (64-bit) cycle counter PMCR.D has no effect.
1276 */
1277 return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1278 }
1279
1280 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1281 {
1282 /* Return true if the specified event counter is configured to be 64 bit */
1283
1284 /* This isn't intended to be used with the cycle counter */
1285 assert(counter < 31);
1286
1287 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1288 return false;
1289 }
1290
1291 if (arm_feature(env, ARM_FEATURE_EL2)) {
1292 /*
1293 * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1294 * current security state, so we don't use arm_mdcr_el2_eff() here.
1295 */
1296 bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1297 int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1298
1299 if (counter >= hpmn) {
1300 return hlp;
1301 }
1302 }
1303 return env->cp15.c9_pmcr & PMCRLP;
1304 }
1305
1306 /*
1307 * Ensure c15_ccnt is the guest-visible count so that operations such as
1308 * enabling/disabling the counter or filtering, modifying the count itself,
1309 * etc. can be done logically. This is essentially a no-op if the counter is
1310 * not enabled at the time of the call.
1311 */
1312 static void pmccntr_op_start(CPUARMState *env)
1313 {
1314 uint64_t cycles = cycles_get_count(env);
1315
1316 if (pmu_counter_enabled(env, 31)) {
1317 uint64_t eff_cycles = cycles;
1318 if (pmccntr_clockdiv_enabled(env)) {
1319 eff_cycles /= 64;
1320 }
1321
1322 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1323
1324 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1325 1ull << 63 : 1ull << 31;
1326 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1327 env->cp15.c9_pmovsr |= (1ULL << 31);
1328 pmu_update_irq(env);
1329 }
1330
1331 env->cp15.c15_ccnt = new_pmccntr;
1332 }
1333 env->cp15.c15_ccnt_delta = cycles;
1334 }
1335
1336 /*
1337 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1338 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1339 * pmccntr_op_start.
1340 */
1341 static void pmccntr_op_finish(CPUARMState *env)
1342 {
1343 if (pmu_counter_enabled(env, 31)) {
1344 #ifndef CONFIG_USER_ONLY
1345 /* Calculate when the counter will next overflow */
1346 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1347 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1348 remaining_cycles = (uint32_t)remaining_cycles;
1349 }
1350 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1351
1352 if (overflow_in > 0) {
1353 int64_t overflow_at;
1354
1355 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1356 overflow_in, &overflow_at)) {
1357 ARMCPU *cpu = env_archcpu(env);
1358 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1359 }
1360 }
1361 #endif
1362
1363 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1364 if (pmccntr_clockdiv_enabled(env)) {
1365 prev_cycles /= 64;
1366 }
1367 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1368 }
1369 }
1370
1371 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1372 {
1373
1374 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1375 uint64_t count = 0;
1376 if (event_supported(event)) {
1377 uint16_t event_idx = supported_event_map[event];
1378 count = pm_events[event_idx].get_count(env);
1379 }
1380
1381 if (pmu_counter_enabled(env, counter)) {
1382 uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1383 uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1384 1ULL << 63 : 1ULL << 31;
1385
1386 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1387 env->cp15.c9_pmovsr |= (1 << counter);
1388 pmu_update_irq(env);
1389 }
1390 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1391 }
1392 env->cp15.c14_pmevcntr_delta[counter] = count;
1393 }
1394
1395 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1396 {
1397 if (pmu_counter_enabled(env, counter)) {
1398 #ifndef CONFIG_USER_ONLY
1399 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1400 uint16_t event_idx = supported_event_map[event];
1401 uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1402 int64_t overflow_in;
1403
1404 if (!pmevcntr_is_64_bit(env, counter)) {
1405 delta = (uint32_t)delta;
1406 }
1407 overflow_in = pm_events[event_idx].ns_per_count(delta);
1408
1409 if (overflow_in > 0) {
1410 int64_t overflow_at;
1411
1412 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1413 overflow_in, &overflow_at)) {
1414 ARMCPU *cpu = env_archcpu(env);
1415 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1416 }
1417 }
1418 #endif
1419
1420 env->cp15.c14_pmevcntr_delta[counter] -=
1421 env->cp15.c14_pmevcntr[counter];
1422 }
1423 }
1424
1425 void pmu_op_start(CPUARMState *env)
1426 {
1427 unsigned int i;
1428 pmccntr_op_start(env);
1429 for (i = 0; i < pmu_num_counters(env); i++) {
1430 pmevcntr_op_start(env, i);
1431 }
1432 }
1433
1434 void pmu_op_finish(CPUARMState *env)
1435 {
1436 unsigned int i;
1437 pmccntr_op_finish(env);
1438 for (i = 0; i < pmu_num_counters(env); i++) {
1439 pmevcntr_op_finish(env, i);
1440 }
1441 }
1442
1443 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1444 {
1445 pmu_op_start(&cpu->env);
1446 }
1447
1448 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1449 {
1450 pmu_op_finish(&cpu->env);
1451 }
1452
1453 void arm_pmu_timer_cb(void *opaque)
1454 {
1455 ARMCPU *cpu = opaque;
1456
1457 /*
1458 * Update all the counter values based on the current underlying counts,
1459 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1460 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1461 * counter may expire.
1462 */
1463 pmu_op_start(&cpu->env);
1464 pmu_op_finish(&cpu->env);
1465 }
1466
1467 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1468 uint64_t value)
1469 {
1470 pmu_op_start(env);
1471
1472 if (value & PMCRC) {
1473 /* The counter has been reset */
1474 env->cp15.c15_ccnt = 0;
1475 }
1476
1477 if (value & PMCRP) {
1478 unsigned int i;
1479 for (i = 0; i < pmu_num_counters(env); i++) {
1480 env->cp15.c14_pmevcntr[i] = 0;
1481 }
1482 }
1483
1484 env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1485 env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1486
1487 pmu_op_finish(env);
1488 }
1489
1490 static uint64_t pmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1491 {
1492 uint64_t pmcr = env->cp15.c9_pmcr;
1493
1494 /*
1495 * If EL2 is implemented and enabled for the current security state, reads
1496 * of PMCR.N from EL1 or EL0 return the value of MDCR_EL2.HPMN or HDCR.HPMN.
1497 */
1498 if (arm_current_el(env) <= 1 && arm_is_el2_enabled(env)) {
1499 pmcr &= ~PMCRN_MASK;
1500 pmcr |= (env->cp15.mdcr_el2 & MDCR_HPMN) << PMCRN_SHIFT;
1501 }
1502
1503 return pmcr;
1504 }
1505
1506 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1507 uint64_t value)
1508 {
1509 unsigned int i;
1510 uint64_t overflow_mask, new_pmswinc;
1511
1512 for (i = 0; i < pmu_num_counters(env); i++) {
1513 /* Increment a counter's count iff: */
1514 if ((value & (1 << i)) && /* counter's bit is set */
1515 /* counter is enabled and not filtered */
1516 pmu_counter_enabled(env, i) &&
1517 /* counter is SW_INCR */
1518 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1519 pmevcntr_op_start(env, i);
1520
1521 /*
1522 * Detect if this write causes an overflow since we can't predict
1523 * PMSWINC overflows like we can for other events
1524 */
1525 new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1526
1527 overflow_mask = pmevcntr_is_64_bit(env, i) ?
1528 1ULL << 63 : 1ULL << 31;
1529
1530 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1531 env->cp15.c9_pmovsr |= (1 << i);
1532 pmu_update_irq(env);
1533 }
1534
1535 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1536
1537 pmevcntr_op_finish(env, i);
1538 }
1539 }
1540 }
1541
1542 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1543 {
1544 uint64_t ret;
1545 pmccntr_op_start(env);
1546 ret = env->cp15.c15_ccnt;
1547 pmccntr_op_finish(env);
1548 return ret;
1549 }
1550
1551 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1552 uint64_t value)
1553 {
1554 /*
1555 * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1556 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1557 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1558 * accessed.
1559 */
1560 env->cp15.c9_pmselr = value & 0x1f;
1561 }
1562
1563 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1564 uint64_t value)
1565 {
1566 pmccntr_op_start(env);
1567 env->cp15.c15_ccnt = value;
1568 pmccntr_op_finish(env);
1569 }
1570
1571 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1572 uint64_t value)
1573 {
1574 uint64_t cur_val = pmccntr_read(env, NULL);
1575
1576 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1577 }
1578
1579 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1580 uint64_t value)
1581 {
1582 pmccntr_op_start(env);
1583 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1584 pmccntr_op_finish(env);
1585 }
1586
1587 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1588 uint64_t value)
1589 {
1590 pmccntr_op_start(env);
1591 /* M is not accessible from AArch32 */
1592 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1593 (value & PMCCFILTR);
1594 pmccntr_op_finish(env);
1595 }
1596
1597 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1598 {
1599 /* M is not visible in AArch32 */
1600 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1601 }
1602
1603 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1604 uint64_t value)
1605 {
1606 pmu_op_start(env);
1607 value &= pmu_counter_mask(env);
1608 env->cp15.c9_pmcnten |= value;
1609 pmu_op_finish(env);
1610 }
1611
1612 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1613 uint64_t value)
1614 {
1615 pmu_op_start(env);
1616 value &= pmu_counter_mask(env);
1617 env->cp15.c9_pmcnten &= ~value;
1618 pmu_op_finish(env);
1619 }
1620
1621 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1622 uint64_t value)
1623 {
1624 value &= pmu_counter_mask(env);
1625 env->cp15.c9_pmovsr &= ~value;
1626 pmu_update_irq(env);
1627 }
1628
1629 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1630 uint64_t value)
1631 {
1632 value &= pmu_counter_mask(env);
1633 env->cp15.c9_pmovsr |= value;
1634 pmu_update_irq(env);
1635 }
1636
1637 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1638 uint64_t value, const uint8_t counter)
1639 {
1640 if (counter == 31) {
1641 pmccfiltr_write(env, ri, value);
1642 } else if (counter < pmu_num_counters(env)) {
1643 pmevcntr_op_start(env, counter);
1644
1645 /*
1646 * If this counter's event type is changing, store the current
1647 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1648 * pmevcntr_op_finish has the correct baseline when it converts back to
1649 * a delta.
1650 */
1651 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1652 PMXEVTYPER_EVTCOUNT;
1653 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1654 if (old_event != new_event) {
1655 uint64_t count = 0;
1656 if (event_supported(new_event)) {
1657 uint16_t event_idx = supported_event_map[new_event];
1658 count = pm_events[event_idx].get_count(env);
1659 }
1660 env->cp15.c14_pmevcntr_delta[counter] = count;
1661 }
1662
1663 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1664 pmevcntr_op_finish(env, counter);
1665 }
1666 /*
1667 * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1668 * PMSELR value is equal to or greater than the number of implemented
1669 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1670 */
1671 }
1672
1673 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1674 const uint8_t counter)
1675 {
1676 if (counter == 31) {
1677 return env->cp15.pmccfiltr_el0;
1678 } else if (counter < pmu_num_counters(env)) {
1679 return env->cp15.c14_pmevtyper[counter];
1680 } else {
1681 /*
1682 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1683 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1684 */
1685 return 0;
1686 }
1687 }
1688
1689 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1690 uint64_t value)
1691 {
1692 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1693 pmevtyper_write(env, ri, value, counter);
1694 }
1695
1696 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1697 uint64_t value)
1698 {
1699 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1700 env->cp15.c14_pmevtyper[counter] = value;
1701
1702 /*
1703 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1704 * pmu_op_finish calls when loading saved state for a migration. Because
1705 * we're potentially updating the type of event here, the value written to
1706 * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a
1707 * different counter type. Therefore, we need to set this value to the
1708 * current count for the counter type we're writing so that pmu_op_finish
1709 * has the correct count for its calculation.
1710 */
1711 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1712 if (event_supported(event)) {
1713 uint16_t event_idx = supported_event_map[event];
1714 env->cp15.c14_pmevcntr_delta[counter] =
1715 pm_events[event_idx].get_count(env);
1716 }
1717 }
1718
1719 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1720 {
1721 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1722 return pmevtyper_read(env, ri, counter);
1723 }
1724
1725 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1726 uint64_t value)
1727 {
1728 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1729 }
1730
1731 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1732 {
1733 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1734 }
1735
1736 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1737 uint64_t value, uint8_t counter)
1738 {
1739 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1740 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1741 value &= MAKE_64BIT_MASK(0, 32);
1742 }
1743 if (counter < pmu_num_counters(env)) {
1744 pmevcntr_op_start(env, counter);
1745 env->cp15.c14_pmevcntr[counter] = value;
1746 pmevcntr_op_finish(env, counter);
1747 }
1748 /*
1749 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1750 * are CONSTRAINED UNPREDICTABLE.
1751 */
1752 }
1753
1754 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1755 uint8_t counter)
1756 {
1757 if (counter < pmu_num_counters(env)) {
1758 uint64_t ret;
1759 pmevcntr_op_start(env, counter);
1760 ret = env->cp15.c14_pmevcntr[counter];
1761 pmevcntr_op_finish(env, counter);
1762 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1763 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1764 ret &= MAKE_64BIT_MASK(0, 32);
1765 }
1766 return ret;
1767 } else {
1768 /*
1769 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1770 * are CONSTRAINED UNPREDICTABLE.
1771 */
1772 return 0;
1773 }
1774 }
1775
1776 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1777 uint64_t value)
1778 {
1779 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1780 pmevcntr_write(env, ri, value, counter);
1781 }
1782
1783 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1784 {
1785 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1786 return pmevcntr_read(env, ri, counter);
1787 }
1788
1789 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1790 uint64_t value)
1791 {
1792 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1793 assert(counter < pmu_num_counters(env));
1794 env->cp15.c14_pmevcntr[counter] = value;
1795 pmevcntr_write(env, ri, value, counter);
1796 }
1797
1798 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1799 {
1800 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1801 assert(counter < pmu_num_counters(env));
1802 return env->cp15.c14_pmevcntr[counter];
1803 }
1804
1805 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1806 uint64_t value)
1807 {
1808 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1809 }
1810
1811 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1812 {
1813 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1814 }
1815
1816 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1817 uint64_t value)
1818 {
1819 if (arm_feature(env, ARM_FEATURE_V8)) {
1820 env->cp15.c9_pmuserenr = value & 0xf;
1821 } else {
1822 env->cp15.c9_pmuserenr = value & 1;
1823 }
1824 }
1825
1826 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1827 uint64_t value)
1828 {
1829 /* We have no event counters so only the C bit can be changed */
1830 value &= pmu_counter_mask(env);
1831 env->cp15.c9_pminten |= value;
1832 pmu_update_irq(env);
1833 }
1834
1835 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1836 uint64_t value)
1837 {
1838 value &= pmu_counter_mask(env);
1839 env->cp15.c9_pminten &= ~value;
1840 pmu_update_irq(env);
1841 }
1842
1843 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1844 uint64_t value)
1845 {
1846 /*
1847 * Note that even though the AArch64 view of this register has bits
1848 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1849 * architectural requirements for bits which are RES0 only in some
1850 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1851 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1852 */
1853 raw_write(env, ri, value & ~0x1FULL);
1854 }
1855
1856 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1857 {
1858 /* Begin with base v8.0 state. */
1859 uint64_t valid_mask = 0x3fff;
1860 ARMCPU *cpu = env_archcpu(env);
1861 uint64_t changed;
1862
1863 /*
1864 * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1865 * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1866 * Instead, choose the format based on the mode of EL3.
1867 */
1868 if (arm_el_is_aa64(env, 3)) {
1869 value |= SCR_FW | SCR_AW; /* RES1 */
1870 valid_mask &= ~SCR_NET; /* RES0 */
1871
1872 if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1873 !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1874 value |= SCR_RW; /* RAO/WI */
1875 }
1876 if (cpu_isar_feature(aa64_ras, cpu)) {
1877 valid_mask |= SCR_TERR;
1878 }
1879 if (cpu_isar_feature(aa64_lor, cpu)) {
1880 valid_mask |= SCR_TLOR;
1881 }
1882 if (cpu_isar_feature(aa64_pauth, cpu)) {
1883 valid_mask |= SCR_API | SCR_APK;
1884 }
1885 if (cpu_isar_feature(aa64_sel2, cpu)) {
1886 valid_mask |= SCR_EEL2;
1887 } else if (cpu_isar_feature(aa64_rme, cpu)) {
1888 /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */
1889 value |= SCR_NS;
1890 }
1891 if (cpu_isar_feature(aa64_mte, cpu)) {
1892 valid_mask |= SCR_ATA;
1893 }
1894 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1895 valid_mask |= SCR_ENSCXT;
1896 }
1897 if (cpu_isar_feature(aa64_doublefault, cpu)) {
1898 valid_mask |= SCR_EASE | SCR_NMEA;
1899 }
1900 if (cpu_isar_feature(aa64_sme, cpu)) {
1901 valid_mask |= SCR_ENTP2;
1902 }
1903 if (cpu_isar_feature(aa64_hcx, cpu)) {
1904 valid_mask |= SCR_HXEN;
1905 }
1906 if (cpu_isar_feature(aa64_fgt, cpu)) {
1907 valid_mask |= SCR_FGTEN;
1908 }
1909 if (cpu_isar_feature(aa64_rme, cpu)) {
1910 valid_mask |= SCR_NSE | SCR_GPF;
1911 }
1912 } else {
1913 valid_mask &= ~(SCR_RW | SCR_ST);
1914 if (cpu_isar_feature(aa32_ras, cpu)) {
1915 valid_mask |= SCR_TERR;
1916 }
1917 }
1918
1919 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1920 valid_mask &= ~SCR_HCE;
1921
1922 /*
1923 * On ARMv7, SMD (or SCD as it is called in v7) is only
1924 * supported if EL2 exists. The bit is UNK/SBZP when
1925 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1926 * when EL2 is unavailable.
1927 * On ARMv8, this bit is always available.
1928 */
1929 if (arm_feature(env, ARM_FEATURE_V7) &&
1930 !arm_feature(env, ARM_FEATURE_V8)) {
1931 valid_mask &= ~SCR_SMD;
1932 }
1933 }
1934
1935 /* Clear all-context RES0 bits. */
1936 value &= valid_mask;
1937 changed = env->cp15.scr_el3 ^ value;
1938 env->cp15.scr_el3 = value;
1939
1940 /*
1941 * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
1942 * we must invalidate all TLBs below EL3.
1943 */
1944 if (changed & (SCR_NS | SCR_NSE)) {
1945 tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1946 ARMMMUIdxBit_E20_0 |
1947 ARMMMUIdxBit_E10_1 |
1948 ARMMMUIdxBit_E20_2 |
1949 ARMMMUIdxBit_E10_1_PAN |
1950 ARMMMUIdxBit_E20_2_PAN |
1951 ARMMMUIdxBit_E2));
1952 }
1953 }
1954
1955 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1956 {
1957 /*
1958 * scr_write will set the RES1 bits on an AArch64-only CPU.
1959 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1960 */
1961 scr_write(env, ri, 0);
1962 }
1963
1964 static CPAccessResult access_tid4(CPUARMState *env,
1965 const ARMCPRegInfo *ri,
1966 bool isread)
1967 {
1968 if (arm_current_el(env) == 1 &&
1969 (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1970 return CP_ACCESS_TRAP_EL2;
1971 }
1972
1973 return CP_ACCESS_OK;
1974 }
1975
1976 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1977 {
1978 ARMCPU *cpu = env_archcpu(env);
1979
1980 /*
1981 * Acquire the CSSELR index from the bank corresponding to the CCSIDR
1982 * bank
1983 */
1984 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1985 ri->secure & ARM_CP_SECSTATE_S);
1986
1987 return cpu->ccsidr[index];
1988 }
1989
1990 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1991 uint64_t value)
1992 {
1993 raw_write(env, ri, value & 0xf);
1994 }
1995
1996 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1997 {
1998 CPUState *cs = env_cpu(env);
1999 bool el1 = arm_current_el(env) == 1;
2000 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
2001 uint64_t ret = 0;
2002
2003 if (hcr_el2 & HCR_IMO) {
2004 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
2005 ret |= CPSR_I;
2006 }
2007 } else {
2008 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
2009 ret |= CPSR_I;
2010 }
2011 }
2012
2013 if (hcr_el2 & HCR_FMO) {
2014 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
2015 ret |= CPSR_F;
2016 }
2017 } else {
2018 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
2019 ret |= CPSR_F;
2020 }
2021 }
2022
2023 if (hcr_el2 & HCR_AMO) {
2024 if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
2025 ret |= CPSR_A;
2026 }
2027 }
2028
2029 return ret;
2030 }
2031
2032 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2033 bool isread)
2034 {
2035 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2036 return CP_ACCESS_TRAP_EL2;
2037 }
2038
2039 return CP_ACCESS_OK;
2040 }
2041
2042 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2043 bool isread)
2044 {
2045 if (arm_feature(env, ARM_FEATURE_V8)) {
2046 return access_aa64_tid1(env, ri, isread);
2047 }
2048
2049 return CP_ACCESS_OK;
2050 }
2051
2052 static const ARMCPRegInfo v7_cp_reginfo[] = {
2053 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2054 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2055 .access = PL1_W, .type = ARM_CP_NOP },
2056 /*
2057 * Performance monitors are implementation defined in v7,
2058 * but with an ARM recommended set of registers, which we
2059 * follow.
2060 *
2061 * Performance registers fall into three categories:
2062 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2063 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2064 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2065 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2066 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2067 */
2068 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
2069 .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
2070 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2071 .writefn = pmcntenset_write,
2072 .accessfn = pmreg_access,
2073 .fgt = FGT_PMCNTEN,
2074 .raw_writefn = raw_write },
2075 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
2076 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2077 .access = PL0_RW, .accessfn = pmreg_access,
2078 .fgt = FGT_PMCNTEN,
2079 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2080 .writefn = pmcntenset_write, .raw_writefn = raw_write },
2081 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
2082 .access = PL0_RW,
2083 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2084 .accessfn = pmreg_access,
2085 .fgt = FGT_PMCNTEN,
2086 .writefn = pmcntenclr_write,
2087 .type = ARM_CP_ALIAS | ARM_CP_IO },
2088 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2089 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2090 .access = PL0_RW, .accessfn = pmreg_access,
2091 .fgt = FGT_PMCNTEN,
2092 .type = ARM_CP_ALIAS | ARM_CP_IO,
2093 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2094 .writefn = pmcntenclr_write },
2095 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
2096 .access = PL0_RW, .type = ARM_CP_IO,
2097 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2098 .accessfn = pmreg_access,
2099 .fgt = FGT_PMOVS,
2100 .writefn = pmovsr_write,
2101 .raw_writefn = raw_write },
2102 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2103 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2104 .access = PL0_RW, .accessfn = pmreg_access,
2105 .fgt = FGT_PMOVS,
2106 .type = ARM_CP_ALIAS | ARM_CP_IO,
2107 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2108 .writefn = pmovsr_write,
2109 .raw_writefn = raw_write },
2110 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2111 .access = PL0_W, .accessfn = pmreg_access_swinc,
2112 .fgt = FGT_PMSWINC_EL0,
2113 .type = ARM_CP_NO_RAW | ARM_CP_IO,
2114 .writefn = pmswinc_write },
2115 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2116 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2117 .access = PL0_W, .accessfn = pmreg_access_swinc,
2118 .fgt = FGT_PMSWINC_EL0,
2119 .type = ARM_CP_NO_RAW | ARM_CP_IO,
2120 .writefn = pmswinc_write },
2121 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2122 .access = PL0_RW, .type = ARM_CP_ALIAS,
2123 .fgt = FGT_PMSELR_EL0,
2124 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2125 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2126 .raw_writefn = raw_write},
2127 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2128 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2129 .access = PL0_RW, .accessfn = pmreg_access_selr,
2130 .fgt = FGT_PMSELR_EL0,
2131 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2132 .writefn = pmselr_write, .raw_writefn = raw_write, },
2133 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2134 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2135 .fgt = FGT_PMCCNTR_EL0,
2136 .readfn = pmccntr_read, .writefn = pmccntr_write32,
2137 .accessfn = pmreg_access_ccntr },
2138 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2139 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2140 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2141 .fgt = FGT_PMCCNTR_EL0,
2142 .type = ARM_CP_IO,
2143 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2144 .readfn = pmccntr_read, .writefn = pmccntr_write,
2145 .raw_readfn = raw_read, .raw_writefn = raw_write, },
2146 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2147 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2148 .access = PL0_RW, .accessfn = pmreg_access,
2149 .fgt = FGT_PMCCFILTR_EL0,
2150 .type = ARM_CP_ALIAS | ARM_CP_IO,
2151 .resetvalue = 0, },
2152 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2153 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2154 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2155 .access = PL0_RW, .accessfn = pmreg_access,
2156 .fgt = FGT_PMCCFILTR_EL0,
2157 .type = ARM_CP_IO,
2158 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2159 .resetvalue = 0, },
2160 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2161 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2162 .accessfn = pmreg_access,
2163 .fgt = FGT_PMEVTYPERN_EL0,
2164 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2165 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2166 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2167 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2168 .accessfn = pmreg_access,
2169 .fgt = FGT_PMEVTYPERN_EL0,
2170 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2171 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2172 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2173 .accessfn = pmreg_access_xevcntr,
2174 .fgt = FGT_PMEVCNTRN_EL0,
2175 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2176 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2177 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2178 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2179 .accessfn = pmreg_access_xevcntr,
2180 .fgt = FGT_PMEVCNTRN_EL0,
2181 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2182 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2183 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2184 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2185 .resetvalue = 0,
2186 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2187 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2188 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2189 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2190 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2191 .resetvalue = 0,
2192 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2193 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2194 .access = PL1_RW, .accessfn = access_tpm,
2195 .fgt = FGT_PMINTEN,
2196 .type = ARM_CP_ALIAS | ARM_CP_IO,
2197 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2198 .resetvalue = 0,
2199 .writefn = pmintenset_write, .raw_writefn = raw_write },
2200 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2201 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2202 .access = PL1_RW, .accessfn = access_tpm,
2203 .fgt = FGT_PMINTEN,
2204 .type = ARM_CP_IO,
2205 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2206 .writefn = pmintenset_write, .raw_writefn = raw_write,
2207 .resetvalue = 0x0 },
2208 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2209 .access = PL1_RW, .accessfn = access_tpm,
2210 .fgt = FGT_PMINTEN,
2211 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2212 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2213 .writefn = pmintenclr_write, },
2214 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2215 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2216 .access = PL1_RW, .accessfn = access_tpm,
2217 .fgt = FGT_PMINTEN,
2218 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2219 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2220 .writefn = pmintenclr_write },
2221 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2222 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2223 .access = PL1_R,
2224 .accessfn = access_tid4,
2225 .fgt = FGT_CCSIDR_EL1,
2226 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2227 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2228 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2229 .access = PL1_RW,
2230 .accessfn = access_tid4,
2231 .fgt = FGT_CSSELR_EL1,
2232 .writefn = csselr_write, .resetvalue = 0,
2233 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2234 offsetof(CPUARMState, cp15.csselr_ns) } },
2235 /*
2236 * Auxiliary ID register: this actually has an IMPDEF value but for now
2237 * just RAZ for all cores:
2238 */
2239 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2240 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2241 .access = PL1_R, .type = ARM_CP_CONST,
2242 .accessfn = access_aa64_tid1,
2243 .fgt = FGT_AIDR_EL1,
2244 .resetvalue = 0 },
2245 /*
2246 * Auxiliary fault status registers: these also are IMPDEF, and we
2247 * choose to RAZ/WI for all cores.
2248 */
2249 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2250 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2251 .access = PL1_RW, .accessfn = access_tvm_trvm,
2252 .fgt = FGT_AFSR0_EL1,
2253 .type = ARM_CP_CONST, .resetvalue = 0 },
2254 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2255 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2256 .access = PL1_RW, .accessfn = access_tvm_trvm,
2257 .fgt = FGT_AFSR1_EL1,
2258 .type = ARM_CP_CONST, .resetvalue = 0 },
2259 /*
2260 * MAIR can just read-as-written because we don't implement caches
2261 * and so don't need to care about memory attributes.
2262 */
2263 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2264 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2265 .access = PL1_RW, .accessfn = access_tvm_trvm,
2266 .fgt = FGT_MAIR_EL1,
2267 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2268 .resetvalue = 0 },
2269 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2270 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2271 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2272 .resetvalue = 0 },
2273 /*
2274 * For non-long-descriptor page tables these are PRRR and NMRR;
2275 * regardless they still act as reads-as-written for QEMU.
2276 */
2277 /*
2278 * MAIR0/1 are defined separately from their 64-bit counterpart which
2279 * allows them to assign the correct fieldoffset based on the endianness
2280 * handled in the field definitions.
2281 */
2282 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2283 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2284 .access = PL1_RW, .accessfn = access_tvm_trvm,
2285 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2286 offsetof(CPUARMState, cp15.mair0_ns) },
2287 .resetfn = arm_cp_reset_ignore },
2288 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2289 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2290 .access = PL1_RW, .accessfn = access_tvm_trvm,
2291 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2292 offsetof(CPUARMState, cp15.mair1_ns) },
2293 .resetfn = arm_cp_reset_ignore },
2294 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2295 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2296 .fgt = FGT_ISR_EL1,
2297 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2298 /* 32 bit ITLB invalidates */
2299 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2300 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2301 .writefn = tlbiall_write },
2302 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2303 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2304 .writefn = tlbimva_write },
2305 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2306 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2307 .writefn = tlbiasid_write },
2308 /* 32 bit DTLB invalidates */
2309 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2310 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2311 .writefn = tlbiall_write },
2312 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2313 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2314 .writefn = tlbimva_write },
2315 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2316 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2317 .writefn = tlbiasid_write },
2318 /* 32 bit TLB invalidates */
2319 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2320 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2321 .writefn = tlbiall_write },
2322 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2323 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2324 .writefn = tlbimva_write },
2325 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2326 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2327 .writefn = tlbiasid_write },
2328 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2329 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2330 .writefn = tlbimvaa_write },
2331 };
2332
2333 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2334 /* 32 bit TLB invalidates, Inner Shareable */
2335 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2336 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2337 .writefn = tlbiall_is_write },
2338 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2339 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2340 .writefn = tlbimva_is_write },
2341 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2342 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2343 .writefn = tlbiasid_is_write },
2344 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2345 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2346 .writefn = tlbimvaa_is_write },
2347 };
2348
2349 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2350 /* PMOVSSET is not implemented in v7 before v7ve */
2351 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2352 .access = PL0_RW, .accessfn = pmreg_access,
2353 .fgt = FGT_PMOVS,
2354 .type = ARM_CP_ALIAS | ARM_CP_IO,
2355 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2356 .writefn = pmovsset_write,
2357 .raw_writefn = raw_write },
2358 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2359 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2360 .access = PL0_RW, .accessfn = pmreg_access,
2361 .fgt = FGT_PMOVS,
2362 .type = ARM_CP_ALIAS | ARM_CP_IO,
2363 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2364 .writefn = pmovsset_write,
2365 .raw_writefn = raw_write },
2366 };
2367
2368 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2369 uint64_t value)
2370 {
2371 value &= 1;
2372 env->teecr = value;
2373 }
2374
2375 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2376 bool isread)
2377 {
2378 /*
2379 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2380 * at all, so we don't need to check whether we're v8A.
2381 */
2382 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2383 (env->cp15.hstr_el2 & HSTR_TTEE)) {
2384 return CP_ACCESS_TRAP_EL2;
2385 }
2386 return CP_ACCESS_OK;
2387 }
2388
2389 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2390 bool isread)
2391 {
2392 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2393 return CP_ACCESS_TRAP;
2394 }
2395 return teecr_access(env, ri, isread);
2396 }
2397
2398 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2399 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2400 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2401 .resetvalue = 0,
2402 .writefn = teecr_write, .accessfn = teecr_access },
2403 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2404 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2405 .accessfn = teehbr_access, .resetvalue = 0 },
2406 };
2407
2408 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2409 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2410 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2411 .access = PL0_RW,
2412 .fgt = FGT_TPIDR_EL0,
2413 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2414 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2415 .access = PL0_RW,
2416 .fgt = FGT_TPIDR_EL0,
2417 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2418 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2419 .resetfn = arm_cp_reset_ignore },
2420 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2421 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2422 .access = PL0_R | PL1_W,
2423 .fgt = FGT_TPIDRRO_EL0,
2424 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2425 .resetvalue = 0},
2426 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2427 .access = PL0_R | PL1_W,
2428 .fgt = FGT_TPIDRRO_EL0,
2429 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2430 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2431 .resetfn = arm_cp_reset_ignore },
2432 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2433 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2434 .access = PL1_RW,
2435 .fgt = FGT_TPIDR_EL1,
2436 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2437 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2438 .access = PL1_RW,
2439 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2440 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2441 .resetvalue = 0 },
2442 };
2443
2444 #ifndef CONFIG_USER_ONLY
2445
2446 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2447 bool isread)
2448 {
2449 /*
2450 * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2451 * Writable only at the highest implemented exception level.
2452 */
2453 int el = arm_current_el(env);
2454 uint64_t hcr;
2455 uint32_t cntkctl;
2456
2457 switch (el) {
2458 case 0:
2459 hcr = arm_hcr_el2_eff(env);
2460 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2461 cntkctl = env->cp15.cnthctl_el2;
2462 } else {
2463 cntkctl = env->cp15.c14_cntkctl;
2464 }
2465 if (!extract32(cntkctl, 0, 2)) {
2466 return CP_ACCESS_TRAP;
2467 }
2468 break;
2469 case 1:
2470 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2471 arm_is_secure_below_el3(env)) {
2472 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2473 return CP_ACCESS_TRAP_UNCATEGORIZED;
2474 }
2475 break;
2476 case 2:
2477 case 3:
2478 break;
2479 }
2480
2481 if (!isread && el < arm_highest_el(env)) {
2482 return CP_ACCESS_TRAP_UNCATEGORIZED;
2483 }
2484
2485 return CP_ACCESS_OK;
2486 }
2487
2488 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2489 bool isread)
2490 {
2491 unsigned int cur_el = arm_current_el(env);
2492 bool has_el2 = arm_is_el2_enabled(env);
2493 uint64_t hcr = arm_hcr_el2_eff(env);
2494
2495 switch (cur_el) {
2496 case 0:
2497 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2498 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2499 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2500 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2501 }
2502
2503 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2504 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2505 return CP_ACCESS_TRAP;
2506 }
2507 /* fall through */
2508 case 1:
2509 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2510 if (has_el2 && timeridx == GTIMER_PHYS &&
2511 (hcr & HCR_E2H
2512 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2513 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2514 return CP_ACCESS_TRAP_EL2;
2515 }
2516 break;
2517 }
2518 return CP_ACCESS_OK;
2519 }
2520
2521 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2522 bool isread)
2523 {
2524 unsigned int cur_el = arm_current_el(env);
2525 bool has_el2 = arm_is_el2_enabled(env);
2526 uint64_t hcr = arm_hcr_el2_eff(env);
2527
2528 switch (cur_el) {
2529 case 0:
2530 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2531 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2532 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2533 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2534 }
2535
2536 /*
2537 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2538 * EL0 if EL0[PV]TEN is zero.
2539 */
2540 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2541 return CP_ACCESS_TRAP;
2542 }
2543 /* fall through */
2544
2545 case 1:
2546 if (has_el2 && timeridx == GTIMER_PHYS) {
2547 if (hcr & HCR_E2H) {
2548 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2549 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2550 return CP_ACCESS_TRAP_EL2;
2551 }
2552 } else {
2553 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2554 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2555 return CP_ACCESS_TRAP_EL2;
2556 }
2557 }
2558 }
2559 break;
2560 }
2561 return CP_ACCESS_OK;
2562 }
2563
2564 static CPAccessResult gt_pct_access(CPUARMState *env,
2565 const ARMCPRegInfo *ri,
2566 bool isread)
2567 {
2568 return gt_counter_access(env, GTIMER_PHYS, isread);
2569 }
2570
2571 static CPAccessResult gt_vct_access(CPUARMState *env,
2572 const ARMCPRegInfo *ri,
2573 bool isread)
2574 {
2575 return gt_counter_access(env, GTIMER_VIRT, isread);
2576 }
2577
2578 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2579 bool isread)
2580 {
2581 return gt_timer_access(env, GTIMER_PHYS, isread);
2582 }
2583
2584 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2585 bool isread)
2586 {
2587 return gt_timer_access(env, GTIMER_VIRT, isread);
2588 }
2589
2590 static CPAccessResult gt_stimer_access(CPUARMState *env,
2591 const ARMCPRegInfo *ri,
2592 bool isread)
2593 {
2594 /*
2595 * The AArch64 register view of the secure physical timer is
2596 * always accessible from EL3, and configurably accessible from
2597 * Secure EL1.
2598 */
2599 switch (arm_current_el(env)) {
2600 case 1:
2601 if (!arm_is_secure(env)) {
2602 return CP_ACCESS_TRAP;
2603 }
2604 if (!(env->cp15.scr_el3 & SCR_ST)) {
2605 return CP_ACCESS_TRAP_EL3;
2606 }
2607 return CP_ACCESS_OK;
2608 case 0:
2609 case 2:
2610 return CP_ACCESS_TRAP;
2611 case 3:
2612 return CP_ACCESS_OK;
2613 default:
2614 g_assert_not_reached();
2615 }
2616 }
2617
2618 static uint64_t gt_get_countervalue(CPUARMState *env)
2619 {
2620 ARMCPU *cpu = env_archcpu(env);
2621
2622 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2623 }
2624
2625 static void gt_update_irq(ARMCPU *cpu, int timeridx)
2626 {
2627 CPUARMState *env = &cpu->env;
2628 uint64_t cnthctl = env->cp15.cnthctl_el2;
2629 ARMSecuritySpace ss = arm_security_space(env);
2630 /* ISTATUS && !IMASK */
2631 int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2632
2633 /*
2634 * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2635 * It is RES0 in Secure and NonSecure state.
2636 */
2637 if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2638 ((timeridx == GTIMER_VIRT && (cnthctl & CNTHCTL_CNTVMASK)) ||
2639 (timeridx == GTIMER_PHYS && (cnthctl & CNTHCTL_CNTPMASK)))) {
2640 irqstate = 0;
2641 }
2642
2643 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2644 trace_arm_gt_update_irq(timeridx, irqstate);
2645 }
2646
2647 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2648 {
2649 /*
2650 * Changing security state between Root and Secure/NonSecure, which may
2651 * happen when switching EL, can change the effective value of CNTHCTL_EL2
2652 * mask bits. Update the IRQ state accordingly.
2653 */
2654 gt_update_irq(cpu, GTIMER_VIRT);
2655 gt_update_irq(cpu, GTIMER_PHYS);
2656 }
2657
2658 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2659 {
2660 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2661
2662 if (gt->ctl & 1) {
2663 /*
2664 * Timer enabled: calculate and set current ISTATUS, irq, and
2665 * reset timer to when ISTATUS next has to change
2666 */
2667 uint64_t offset = timeridx == GTIMER_VIRT ?
2668 cpu->env.cp15.cntvoff_el2 : 0;
2669 uint64_t count = gt_get_countervalue(&cpu->env);
2670 /* Note that this must be unsigned 64 bit arithmetic: */
2671 int istatus = count - offset >= gt->cval;
2672 uint64_t nexttick;
2673
2674 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2675
2676 if (istatus) {
2677 /*
2678 * Next transition is when (count - offset) rolls back over to 0.
2679 * If offset > count then this is when count == offset;
2680 * if offset <= count then this is when count == offset + 2^64
2681 * For the latter case we set nexttick to an "as far in future
2682 * as possible" value and let the code below handle it.
2683 */
2684 if (offset > count) {
2685 nexttick = offset;
2686 } else {
2687 nexttick = UINT64_MAX;
2688 }
2689 } else {
2690 /*
2691 * Next transition is when (count - offset) == cval, i.e.
2692 * when count == (cval + offset).
2693 * If that would overflow, then again we set up the next interrupt
2694 * for "as far in the future as possible" for the code below.
2695 */
2696 if (uadd64_overflow(gt->cval, offset, &nexttick)) {
2697 nexttick = UINT64_MAX;
2698 }
2699 }
2700 /*
2701 * Note that the desired next expiry time might be beyond the
2702 * signed-64-bit range of a QEMUTimer -- in this case we just
2703 * set the timer for as far in the future as possible. When the
2704 * timer expires we will reset the timer for any remaining period.
2705 */
2706 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2707 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2708 } else {
2709 timer_mod(cpu->gt_timer[timeridx], nexttick);
2710 }
2711 trace_arm_gt_recalc(timeridx, nexttick);
2712 } else {
2713 /* Timer disabled: ISTATUS and timer output always clear */
2714 gt->ctl &= ~4;
2715 timer_del(cpu->gt_timer[timeridx]);
2716 trace_arm_gt_recalc_disabled(timeridx);
2717 }
2718 gt_update_irq(cpu, timeridx);
2719 }
2720
2721 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2722 int timeridx)
2723 {
2724 ARMCPU *cpu = env_archcpu(env);
2725
2726 timer_del(cpu->gt_timer[timeridx]);
2727 }
2728
2729 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2730 {
2731 return gt_get_countervalue(env);
2732 }
2733
2734 static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2735 {
2736 uint64_t hcr;
2737
2738 switch (arm_current_el(env)) {
2739 case 2:
2740 hcr = arm_hcr_el2_eff(env);
2741 if (hcr & HCR_E2H) {
2742 return 0;
2743 }
2744 break;
2745 case 0:
2746 hcr = arm_hcr_el2_eff(env);
2747 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2748 return 0;
2749 }
2750 break;
2751 }
2752
2753 return env->cp15.cntvoff_el2;
2754 }
2755
2756 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2757 {
2758 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2759 }
2760
2761 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2762 int timeridx,
2763 uint64_t value)
2764 {
2765 trace_arm_gt_cval_write(timeridx, value);
2766 env->cp15.c14_timer[timeridx].cval = value;
2767 gt_recalc_timer(env_archcpu(env), timeridx);
2768 }
2769
2770 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2771 int timeridx)
2772 {
2773 uint64_t offset = 0;
2774
2775 switch (timeridx) {
2776 case GTIMER_VIRT:
2777 case GTIMER_HYPVIRT:
2778 offset = gt_virt_cnt_offset(env);
2779 break;
2780 }
2781
2782 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2783 (gt_get_countervalue(env) - offset));
2784 }
2785
2786 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2787 int timeridx,
2788 uint64_t value)
2789 {
2790 uint64_t offset = 0;
2791
2792 switch (timeridx) {
2793 case GTIMER_VIRT:
2794 case GTIMER_HYPVIRT:
2795 offset = gt_virt_cnt_offset(env);
2796 break;
2797 }
2798
2799 trace_arm_gt_tval_write(timeridx, value);
2800 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2801 sextract64(value, 0, 32);
2802 gt_recalc_timer(env_archcpu(env), timeridx);
2803 }
2804
2805 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2806 int timeridx,
2807 uint64_t value)
2808 {
2809 ARMCPU *cpu = env_archcpu(env);
2810 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2811
2812 trace_arm_gt_ctl_write(timeridx, value);
2813 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2814 if ((oldval ^ value) & 1) {
2815 /* Enable toggled */
2816 gt_recalc_timer(cpu, timeridx);
2817 } else if ((oldval ^ value) & 2) {
2818 /*
2819 * IMASK toggled: don't need to recalculate,
2820 * just set the interrupt line based on ISTATUS
2821 */
2822 trace_arm_gt_imask_toggle(timeridx);
2823 gt_update_irq(cpu, timeridx);
2824 }
2825 }
2826
2827 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2828 {
2829 gt_timer_reset(env, ri, GTIMER_PHYS);
2830 }
2831
2832 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2833 uint64_t value)
2834 {
2835 gt_cval_write(env, ri, GTIMER_PHYS, value);
2836 }
2837
2838 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2839 {
2840 return gt_tval_read(env, ri, GTIMER_PHYS);
2841 }
2842
2843 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2844 uint64_t value)
2845 {
2846 gt_tval_write(env, ri, GTIMER_PHYS, value);
2847 }
2848
2849 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2850 uint64_t value)
2851 {
2852 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2853 }
2854
2855 static int gt_phys_redir_timeridx(CPUARMState *env)
2856 {
2857 switch (arm_mmu_idx(env)) {
2858 case ARMMMUIdx_E20_0:
2859 case ARMMMUIdx_E20_2:
2860 case ARMMMUIdx_E20_2_PAN:
2861 return GTIMER_HYP;
2862 default:
2863 return GTIMER_PHYS;
2864 }
2865 }
2866
2867 static int gt_virt_redir_timeridx(CPUARMState *env)
2868 {
2869 switch (arm_mmu_idx(env)) {
2870 case ARMMMUIdx_E20_0:
2871 case ARMMMUIdx_E20_2:
2872 case ARMMMUIdx_E20_2_PAN:
2873 return GTIMER_HYPVIRT;
2874 default:
2875 return GTIMER_VIRT;
2876 }
2877 }
2878
2879 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2880 const ARMCPRegInfo *ri)
2881 {
2882 int timeridx = gt_phys_redir_timeridx(env);
2883 return env->cp15.c14_timer[timeridx].cval;
2884 }
2885
2886 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2887 uint64_t value)
2888 {
2889 int timeridx = gt_phys_redir_timeridx(env);
2890 gt_cval_write(env, ri, timeridx, value);
2891 }
2892
2893 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2894 const ARMCPRegInfo *ri)
2895 {
2896 int timeridx = gt_phys_redir_timeridx(env);
2897 return gt_tval_read(env, ri, timeridx);
2898 }
2899
2900 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2901 uint64_t value)
2902 {
2903 int timeridx = gt_phys_redir_timeridx(env);
2904 gt_tval_write(env, ri, timeridx, value);
2905 }
2906
2907 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2908 const ARMCPRegInfo *ri)
2909 {
2910 int timeridx = gt_phys_redir_timeridx(env);
2911 return env->cp15.c14_timer[timeridx].ctl;
2912 }
2913
2914 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2915 uint64_t value)
2916 {
2917 int timeridx = gt_phys_redir_timeridx(env);
2918 gt_ctl_write(env, ri, timeridx, value);
2919 }
2920
2921 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2922 {
2923 gt_timer_reset(env, ri, GTIMER_VIRT);
2924 }
2925
2926 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2927 uint64_t value)
2928 {
2929 gt_cval_write(env, ri, GTIMER_VIRT, value);
2930 }
2931
2932 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2933 {
2934 return gt_tval_read(env, ri, GTIMER_VIRT);
2935 }
2936
2937 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2938 uint64_t value)
2939 {
2940 gt_tval_write(env, ri, GTIMER_VIRT, value);
2941 }
2942
2943 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2944 uint64_t value)
2945 {
2946 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2947 }
2948
2949 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2950 uint64_t value)
2951 {
2952 ARMCPU *cpu = env_archcpu(env);
2953 uint32_t oldval = env->cp15.cnthctl_el2;
2954
2955 raw_write(env, ri, value);
2956
2957 if ((oldval ^ value) & CNTHCTL_CNTVMASK) {
2958 gt_update_irq(cpu, GTIMER_VIRT);
2959 } else if ((oldval ^ value) & CNTHCTL_CNTPMASK) {
2960 gt_update_irq(cpu, GTIMER_PHYS);
2961 }
2962 }
2963
2964 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2965 uint64_t value)
2966 {
2967 ARMCPU *cpu = env_archcpu(env);
2968
2969 trace_arm_gt_cntvoff_write(value);
2970 raw_write(env, ri, value);
2971 gt_recalc_timer(cpu, GTIMER_VIRT);
2972 }
2973
2974 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2975 const ARMCPRegInfo *ri)
2976 {
2977 int timeridx = gt_virt_redir_timeridx(env);
2978 return env->cp15.c14_timer[timeridx].cval;
2979 }
2980
2981 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2982 uint64_t value)
2983 {
2984 int timeridx = gt_virt_redir_timeridx(env);
2985 gt_cval_write(env, ri, timeridx, value);
2986 }
2987
2988 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2989 const ARMCPRegInfo *ri)
2990 {
2991 int timeridx = gt_virt_redir_timeridx(env);
2992 return gt_tval_read(env, ri, timeridx);
2993 }
2994
2995 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2996 uint64_t value)
2997 {
2998 int timeridx = gt_virt_redir_timeridx(env);
2999 gt_tval_write(env, ri, timeridx, value);
3000 }
3001
3002 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
3003 const ARMCPRegInfo *ri)
3004 {
3005 int timeridx = gt_virt_redir_timeridx(env);
3006 return env->cp15.c14_timer[timeridx].ctl;
3007 }
3008
3009 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3010 uint64_t value)
3011 {
3012 int timeridx = gt_virt_redir_timeridx(env);
3013 gt_ctl_write(env, ri, timeridx, value);
3014 }
3015
3016 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3017 {
3018 gt_timer_reset(env, ri, GTIMER_HYP);
3019 }
3020
3021 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3022 uint64_t value)
3023 {
3024 gt_cval_write(env, ri, GTIMER_HYP, value);
3025 }
3026
3027 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3028 {
3029 return gt_tval_read(env, ri, GTIMER_HYP);
3030 }
3031
3032 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3033 uint64_t value)
3034 {
3035 gt_tval_write(env, ri, GTIMER_HYP, value);
3036 }
3037
3038 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3039 uint64_t value)
3040 {
3041 gt_ctl_write(env, ri, GTIMER_HYP, value);
3042 }
3043
3044 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3045 {
3046 gt_timer_reset(env, ri, GTIMER_SEC);
3047 }
3048
3049 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3050 uint64_t value)
3051 {
3052 gt_cval_write(env, ri, GTIMER_SEC, value);
3053 }
3054
3055 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3056 {
3057 return gt_tval_read(env, ri, GTIMER_SEC);
3058 }
3059
3060 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3061 uint64_t value)
3062 {
3063 gt_tval_write(env, ri, GTIMER_SEC, value);
3064 }
3065
3066 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3067 uint64_t value)
3068 {
3069 gt_ctl_write(env, ri, GTIMER_SEC, value);
3070 }
3071
3072 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3073 {
3074 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3075 }
3076
3077 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3078 uint64_t value)
3079 {
3080 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3081 }
3082
3083 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3084 {
3085 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3086 }
3087
3088 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3089 uint64_t value)
3090 {
3091 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3092 }
3093
3094 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3095 uint64_t value)
3096 {
3097 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3098 }
3099
3100 void arm_gt_ptimer_cb(void *opaque)
3101 {
3102 ARMCPU *cpu = opaque;
3103
3104 gt_recalc_timer(cpu, GTIMER_PHYS);
3105 }
3106
3107 void arm_gt_vtimer_cb(void *opaque)
3108 {
3109 ARMCPU *cpu = opaque;
3110
3111 gt_recalc_timer(cpu, GTIMER_VIRT);
3112 }
3113
3114 void arm_gt_htimer_cb(void *opaque)
3115 {
3116 ARMCPU *cpu = opaque;
3117
3118 gt_recalc_timer(cpu, GTIMER_HYP);
3119 }
3120
3121 void arm_gt_stimer_cb(void *opaque)
3122 {
3123 ARMCPU *cpu = opaque;
3124
3125 gt_recalc_timer(cpu, GTIMER_SEC);
3126 }
3127
3128 void arm_gt_hvtimer_cb(void *opaque)
3129 {
3130 ARMCPU *cpu = opaque;
3131
3132 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3133 }
3134
3135 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3136 {
3137 ARMCPU *cpu = env_archcpu(env);
3138
3139 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3140 }
3141
3142 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3143 /*
3144 * Note that CNTFRQ is purely reads-as-written for the benefit
3145 * of software; writing it doesn't actually change the timer frequency.
3146 * Our reset value matches the fixed frequency we implement the timer at.
3147 */
3148 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3149 .type = ARM_CP_ALIAS,
3150 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3151 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3152 },
3153 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3154 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3155 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3156 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3157 .resetfn = arm_gt_cntfrq_reset,
3158 },
3159 /* overall control: mostly access permissions */
3160 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3161 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3162 .access = PL1_RW,
3163 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3164 .resetvalue = 0,
3165 },
3166 /* per-timer control */
3167 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3168 .secure = ARM_CP_SECSTATE_NS,
3169 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3170 .accessfn = gt_ptimer_access,
3171 .fieldoffset = offsetoflow32(CPUARMState,
3172 cp15.c14_timer[GTIMER_PHYS].ctl),
3173 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3174 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3175 },
3176 { .name = "CNTP_CTL_S",
3177 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3178 .secure = ARM_CP_SECSTATE_S,
3179 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3180 .accessfn = gt_ptimer_access,
3181 .fieldoffset = offsetoflow32(CPUARMState,
3182 cp15.c14_timer[GTIMER_SEC].ctl),
3183 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3184 },
3185 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3186 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3187 .type = ARM_CP_IO, .access = PL0_RW,
3188 .accessfn = gt_ptimer_access,
3189 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3190 .resetvalue = 0,
3191 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3192 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3193 },
3194 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3195 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3196 .accessfn = gt_vtimer_access,
3197 .fieldoffset = offsetoflow32(CPUARMState,
3198 cp15.c14_timer[GTIMER_VIRT].ctl),
3199 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3200 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3201 },
3202 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3203 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3204 .type = ARM_CP_IO, .access = PL0_RW,
3205 .accessfn = gt_vtimer_access,
3206 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3207 .resetvalue = 0,
3208 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3209 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3210 },
3211 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3212 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3213 .secure = ARM_CP_SECSTATE_NS,
3214 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3215 .accessfn = gt_ptimer_access,
3216 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3217 },
3218 { .name = "CNTP_TVAL_S",
3219 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3220 .secure = ARM_CP_SECSTATE_S,
3221 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3222 .accessfn = gt_ptimer_access,
3223 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3224 },
3225 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3226 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3227 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3228 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3229 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3230 },
3231 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3232 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3233 .accessfn = gt_vtimer_access,
3234 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3235 },
3236 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3237 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3238 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3239 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3240 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3241 },
3242 /* The counter itself */
3243 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3244 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3245 .accessfn = gt_pct_access,
3246 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3247 },
3248 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3249 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3250 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3251 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3252 },
3253 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3254 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3255 .accessfn = gt_vct_access,
3256 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3257 },
3258 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3259 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3260 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3261 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3262 },
3263 /* Comparison value, indicating when the timer goes off */
3264 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3265 .secure = ARM_CP_SECSTATE_NS,
3266 .access = PL0_RW,
3267 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3268 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3269 .accessfn = gt_ptimer_access,
3270 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3271 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3272 },
3273 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3274 .secure = ARM_CP_SECSTATE_S,
3275 .access = PL0_RW,
3276 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3277 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3278 .accessfn = gt_ptimer_access,
3279 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3280 },
3281 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3282 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3283 .access = PL0_RW,
3284 .type = ARM_CP_IO,
3285 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3286 .resetvalue = 0, .accessfn = gt_ptimer_access,
3287 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3288 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3289 },
3290 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3291 .access = PL0_RW,
3292 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3293 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3294 .accessfn = gt_vtimer_access,
3295 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3296 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3297 },
3298 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3299 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3300 .access = PL0_RW,
3301 .type = ARM_CP_IO,
3302 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3303 .resetvalue = 0, .accessfn = gt_vtimer_access,
3304 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3305 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3306 },
3307 /*
3308 * Secure timer -- this is actually restricted to only EL3
3309 * and configurably Secure-EL1 via the accessfn.
3310 */
3311 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3312 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3313 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3314 .accessfn = gt_stimer_access,
3315 .readfn = gt_sec_tval_read,
3316 .writefn = gt_sec_tval_write,
3317 .resetfn = gt_sec_timer_reset,
3318 },
3319 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3320 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3321 .type = ARM_CP_IO, .access = PL1_RW,
3322 .accessfn = gt_stimer_access,
3323 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3324 .resetvalue = 0,
3325 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3326 },
3327 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3328 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3329 .type = ARM_CP_IO, .access = PL1_RW,
3330 .accessfn = gt_stimer_access,
3331 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3332 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3333 },
3334 };
3335
3336 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3337 bool isread)
3338 {
3339 if (arm_current_el(env) == 1) {
3340 /* This must be a FEAT_NV access */
3341 /* TODO: FEAT_ECV will need to check CNTHCTL_EL2 here */
3342 return CP_ACCESS_OK;
3343 }
3344 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3345 return CP_ACCESS_TRAP;
3346 }
3347 return CP_ACCESS_OK;
3348 }
3349
3350 #else
3351
3352 /*
3353 * In user-mode most of the generic timer registers are inaccessible
3354 * however modern kernels (4.12+) allow access to cntvct_el0
3355 */
3356
3357 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3358 {
3359 ARMCPU *cpu = env_archcpu(env);
3360
3361 /*
3362 * Currently we have no support for QEMUTimer in linux-user so we
3363 * can't call gt_get_countervalue(env), instead we directly
3364 * call the lower level functions.
3365 */
3366 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3367 }
3368
3369 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3370 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3371 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3372 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3373 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3374 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3375 },
3376 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3377 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3378 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3379 .readfn = gt_virt_cnt_read,
3380 },
3381 };
3382
3383 #endif
3384
3385 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3386 {
3387 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3388 raw_write(env, ri, value);
3389 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3390 raw_write(env, ri, value & 0xfffff6ff);
3391 } else {
3392 raw_write(env, ri, value & 0xfffff1ff);
3393 }
3394 }
3395
3396 #ifndef CONFIG_USER_ONLY
3397 /* get_phys_addr() isn't present for user-mode-only targets */
3398
3399 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3400 bool isread)
3401 {
3402 if (ri->opc2 & 4) {
3403 /*
3404 * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3405 * Secure EL1 (which can only happen if EL3 is AArch64).
3406 * They are simply UNDEF if executed from NS EL1.
3407 * They function normally from EL2 or EL3.
3408 */
3409 if (arm_current_el(env) == 1) {
3410 if (arm_is_secure_below_el3(env)) {
3411 if (env->cp15.scr_el3 & SCR_EEL2) {
3412 return CP_ACCESS_TRAP_EL2;
3413 }
3414 return CP_ACCESS_TRAP_EL3;
3415 }
3416 return CP_ACCESS_TRAP_UNCATEGORIZED;
3417 }
3418 }
3419 return CP_ACCESS_OK;
3420 }
3421
3422 #ifdef CONFIG_TCG
3423 static int par_el1_shareability(GetPhysAddrResult *res)
3424 {
3425 /*
3426 * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3427 * memory -- see pseudocode PAREncodeShareability().
3428 */
3429 if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3430 res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3431 return 2;
3432 }
3433 return res->cacheattrs.shareability;
3434 }
3435
3436 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3437 MMUAccessType access_type, ARMMMUIdx mmu_idx,
3438 ARMSecuritySpace ss)
3439 {
3440 bool ret;
3441 uint64_t par64;
3442 bool format64 = false;
3443 ARMMMUFaultInfo fi = {};
3444 GetPhysAddrResult res = {};
3445
3446 /*
3447 * I_MXTJT: Granule protection checks are not performed on the final address
3448 * of a successful translation.
3449 */
3450 ret = get_phys_addr_with_space_nogpc(env, value, access_type, mmu_idx, ss,
3451 &res, &fi);
3452
3453 /*
3454 * ATS operations only do S1 or S1+S2 translations, so we never
3455 * have to deal with the ARMCacheAttrs format for S2 only.
3456 */
3457 assert(!res.cacheattrs.is_s2_format);
3458
3459 if (ret) {
3460 /*
3461 * Some kinds of translation fault must cause exceptions rather
3462 * than being reported in the PAR.
3463 */
3464 int current_el = arm_current_el(env);
3465 int target_el;
3466 uint32_t syn, fsr, fsc;
3467 bool take_exc = false;
3468
3469 if (fi.s1ptw && current_el == 1
3470 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3471 /*
3472 * Synchronous stage 2 fault on an access made as part of the
3473 * translation table walk for AT S1E0* or AT S1E1* insn
3474 * executed from NS EL1. If this is a synchronous external abort
3475 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3476 * to EL3. Otherwise the fault is taken as an exception to EL2,
3477 * and HPFAR_EL2 holds the faulting IPA.
3478 */
3479 if (fi.type == ARMFault_SyncExternalOnWalk &&
3480 (env->cp15.scr_el3 & SCR_EA)) {
3481 target_el = 3;
3482 } else {
3483 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3484 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3485 env->cp15.hpfar_el2 |= HPFAR_NS;
3486 }
3487 target_el = 2;
3488 }
3489 take_exc = true;
3490 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3491 /*
3492 * Synchronous external aborts during a translation table walk
3493 * are taken as Data Abort exceptions.
3494 */
3495 if (fi.stage2) {
3496 if (current_el == 3) {
3497 target_el = 3;
3498 } else {
3499 target_el = 2;
3500 }
3501 } else {
3502 target_el = exception_target_el(env);
3503 }
3504 take_exc = true;
3505 }
3506
3507 if (take_exc) {
3508 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3509 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3510 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3511 fsr = arm_fi_to_lfsc(&fi);
3512 fsc = extract32(fsr, 0, 6);
3513 } else {
3514 fsr = arm_fi_to_sfsc(&fi);
3515 fsc = 0x3f;
3516 }
3517 /*
3518 * Report exception with ESR indicating a fault due to a
3519 * translation table walk for a cache maintenance instruction.
3520 */
3521 syn = syn_data_abort_no_iss(current_el == target_el, 0,
3522 fi.ea, 1, fi.s1ptw, 1, fsc);
3523 env->exception.vaddress = value;
3524 env->exception.fsr = fsr;
3525 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3526 }
3527 }
3528
3529 if (is_a64(env)) {
3530 format64 = true;
3531 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3532 /*
3533 * ATS1Cxx:
3534 * * TTBCR.EAE determines whether the result is returned using the
3535 * 32-bit or the 64-bit PAR format
3536 * * Instructions executed in Hyp mode always use the 64bit format
3537 *
3538 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3539 * * The Non-secure TTBCR.EAE bit is set to 1
3540 * * The implementation includes EL2, and the value of HCR.VM is 1
3541 *
3542 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3543 *
3544 * ATS1Hx always uses the 64bit format.
3545 */
3546 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3547
3548 if (arm_feature(env, ARM_FEATURE_EL2)) {
3549 if (mmu_idx == ARMMMUIdx_E10_0 ||
3550 mmu_idx == ARMMMUIdx_E10_1 ||
3551 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3552 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3553 } else {
3554 format64 |= arm_current_el(env) == 2;
3555 }
3556 }
3557 }
3558
3559 if (format64) {
3560 /* Create a 64-bit PAR */
3561 par64 = (1 << 11); /* LPAE bit always set */
3562 if (!ret) {
3563 par64 |= res.f.phys_addr & ~0xfffULL;
3564 if (!res.f.attrs.secure) {
3565 par64 |= (1 << 9); /* NS */
3566 }
3567 par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3568 par64 |= par_el1_shareability(&res) << 7; /* SH */
3569 } else {
3570 uint32_t fsr = arm_fi_to_lfsc(&fi);
3571
3572 par64 |= 1; /* F */
3573 par64 |= (fsr & 0x3f) << 1; /* FS */
3574 if (fi.stage2) {
3575 par64 |= (1 << 9); /* S */
3576 }
3577 if (fi.s1ptw) {
3578 par64 |= (1 << 8); /* PTW */
3579 }
3580 }
3581 } else {
3582 /*
3583 * fsr is a DFSR/IFSR value for the short descriptor
3584 * translation table format (with WnR always clear).
3585 * Convert it to a 32-bit PAR.
3586 */
3587 if (!ret) {
3588 /* We do not set any attribute bits in the PAR */
3589 if (res.f.lg_page_size == 24
3590 && arm_feature(env, ARM_FEATURE_V7)) {
3591 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3592 } else {
3593 par64 = res.f.phys_addr & 0xfffff000;
3594 }
3595 if (!res.f.attrs.secure) {
3596 par64 |= (1 << 9); /* NS */
3597 }
3598 } else {
3599 uint32_t fsr = arm_fi_to_sfsc(&fi);
3600
3601 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3602 ((fsr & 0xf) << 1) | 1;
3603 }
3604 }
3605 return par64;
3606 }
3607 #endif /* CONFIG_TCG */
3608
3609 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3610 {
3611 #ifdef CONFIG_TCG
3612 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3613 uint64_t par64;
3614 ARMMMUIdx mmu_idx;
3615 int el = arm_current_el(env);
3616 ARMSecuritySpace ss = arm_security_space(env);
3617
3618 switch (ri->opc2 & 6) {
3619 case 0:
3620 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3621 switch (el) {
3622 case 3:
3623 mmu_idx = ARMMMUIdx_E3;
3624 break;
3625 case 2:
3626 g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */
3627 /* fall through */
3628 case 1:
3629 if (ri->crm == 9 && arm_pan_enabled(env)) {
3630 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3631 } else {
3632 mmu_idx = ARMMMUIdx_Stage1_E1;
3633 }
3634 break;
3635 default:
3636 g_assert_not_reached();
3637 }
3638 break;
3639 case 2:
3640 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3641 switch (el) {
3642 case 3:
3643 mmu_idx = ARMMMUIdx_E10_0;
3644 break;
3645 case 2:
3646 g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */
3647 mmu_idx = ARMMMUIdx_Stage1_E0;
3648 break;
3649 case 1:
3650 mmu_idx = ARMMMUIdx_Stage1_E0;
3651 break;
3652 default:
3653 g_assert_not_reached();
3654 }
3655 break;
3656 case 4:
3657 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3658 mmu_idx = ARMMMUIdx_E10_1;
3659 ss = ARMSS_NonSecure;
3660 break;
3661 case 6:
3662 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3663 mmu_idx = ARMMMUIdx_E10_0;
3664 ss = ARMSS_NonSecure;
3665 break;
3666 default:
3667 g_assert_not_reached();
3668 }
3669
3670 par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
3671
3672 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3673 #else
3674 /* Handled by hardware accelerator. */
3675 g_assert_not_reached();
3676 #endif /* CONFIG_TCG */
3677 }
3678
3679 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3680 uint64_t value)
3681 {
3682 #ifdef CONFIG_TCG
3683 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3684 uint64_t par64;
3685
3686 /* There is no SecureEL2 for AArch32. */
3687 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
3688 ARMSS_NonSecure);
3689
3690 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3691 #else
3692 /* Handled by hardware accelerator. */
3693 g_assert_not_reached();
3694 #endif /* CONFIG_TCG */
3695 }
3696
3697 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
3698 bool isread)
3699 {
3700 /*
3701 * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
3702 * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
3703 * only happen when executing at EL3 because that combination also causes an
3704 * illegal exception return. We don't need to check FEAT_RME either, because
3705 * scr_write() ensures that the NSE bit is not set otherwise.
3706 */
3707 if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
3708 return CP_ACCESS_TRAP;
3709 }
3710 return CP_ACCESS_OK;
3711 }
3712
3713 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3714 bool isread)
3715 {
3716 if (arm_current_el(env) == 3 &&
3717 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3718 return CP_ACCESS_TRAP;
3719 }
3720 return at_e012_access(env, ri, isread);
3721 }
3722
3723 static CPAccessResult at_s1e01_access(CPUARMState *env, const ARMCPRegInfo *ri,
3724 bool isread)
3725 {
3726 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_AT)) {
3727 return CP_ACCESS_TRAP_EL2;
3728 }
3729 return at_e012_access(env, ri, isread);
3730 }
3731
3732 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3733 uint64_t value)
3734 {
3735 #ifdef CONFIG_TCG
3736 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3737 ARMMMUIdx mmu_idx;
3738 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3739 bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3740
3741 switch (ri->opc2 & 6) {
3742 case 0:
3743 switch (ri->opc1) {
3744 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3745 if (ri->crm == 9 && arm_pan_enabled(env)) {
3746 mmu_idx = regime_e20 ?
3747 ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3748 } else {
3749 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3750 }
3751 break;
3752 case 4: /* AT S1E2R, AT S1E2W */
3753 mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3754 break;
3755 case 6: /* AT S1E3R, AT S1E3W */
3756 mmu_idx = ARMMMUIdx_E3;
3757 break;
3758 default:
3759 g_assert_not_reached();
3760 }
3761 break;
3762 case 2: /* AT S1E0R, AT S1E0W */
3763 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3764 break;
3765 case 4: /* AT S12E1R, AT S12E1W */
3766 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3767 break;
3768 case 6: /* AT S12E0R, AT S12E0W */
3769 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3770 break;
3771 default:
3772 g_assert_not_reached();
3773 }
3774
3775 env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3776 mmu_idx, arm_security_space(env));
3777 #else
3778 /* Handled by hardware accelerator. */
3779 g_assert_not_reached();
3780 #endif /* CONFIG_TCG */
3781 }
3782 #endif
3783
3784 /* Return basic MPU access permission bits. */
3785 static uint32_t simple_mpu_ap_bits(uint32_t val)
3786 {
3787 uint32_t ret;
3788 uint32_t mask;
3789 int i;
3790 ret = 0;
3791 mask = 3;
3792 for (i = 0; i < 16; i += 2) {
3793 ret |= (val >> i) & mask;
3794 mask <<= 2;
3795 }
3796 return ret;
3797 }
3798
3799 /* Pad basic MPU access permission bits to extended format. */
3800 static uint32_t extended_mpu_ap_bits(uint32_t val)
3801 {
3802 uint32_t ret;
3803 uint32_t mask;
3804 int i;
3805 ret = 0;
3806 mask = 3;
3807 for (i = 0; i < 16; i += 2) {
3808 ret |= (val & mask) << i;
3809 mask <<= 2;
3810 }
3811 return ret;
3812 }
3813
3814 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3815 uint64_t value)
3816 {
3817 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3818 }
3819
3820 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3821 {
3822 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3823 }
3824
3825 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3826 uint64_t value)
3827 {
3828 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3829 }
3830
3831 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3832 {
3833 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3834 }
3835
3836 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3837 {
3838 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3839
3840 if (!u32p) {
3841 return 0;
3842 }
3843
3844 u32p += env->pmsav7.rnr[M_REG_NS];
3845 return *u32p;
3846 }
3847
3848 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3849 uint64_t value)
3850 {
3851 ARMCPU *cpu = env_archcpu(env);
3852 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3853
3854 if (!u32p) {
3855 return;
3856 }
3857
3858 u32p += env->pmsav7.rnr[M_REG_NS];
3859 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3860 *u32p = value;
3861 }
3862
3863 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3864 uint64_t value)
3865 {
3866 ARMCPU *cpu = env_archcpu(env);
3867 uint32_t nrgs = cpu->pmsav7_dregion;
3868
3869 if (value >= nrgs) {
3870 qemu_log_mask(LOG_GUEST_ERROR,
3871 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3872 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3873 return;
3874 }
3875
3876 raw_write(env, ri, value);
3877 }
3878
3879 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3880 uint64_t value)
3881 {
3882 ARMCPU *cpu = env_archcpu(env);
3883
3884 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3885 env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3886 }
3887
3888 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3889 {
3890 return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3891 }
3892
3893 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3894 uint64_t value)
3895 {
3896 ARMCPU *cpu = env_archcpu(env);
3897
3898 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3899 env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3900 }
3901
3902 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3903 {
3904 return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3905 }
3906
3907 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3908 uint64_t value)
3909 {
3910 ARMCPU *cpu = env_archcpu(env);
3911
3912 /*
3913 * Ignore writes that would select not implemented region.
3914 * This is architecturally UNPREDICTABLE.
3915 */
3916 if (value >= cpu->pmsav7_dregion) {
3917 return;
3918 }
3919
3920 env->pmsav7.rnr[M_REG_NS] = value;
3921 }
3922
3923 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3924 uint64_t value)
3925 {
3926 ARMCPU *cpu = env_archcpu(env);
3927
3928 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3929 env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3930 }
3931
3932 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3933 {
3934 return env->pmsav8.hprbar[env->pmsav8.hprselr];
3935 }
3936
3937 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3938 uint64_t value)
3939 {
3940 ARMCPU *cpu = env_archcpu(env);
3941
3942 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3943 env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
3944 }
3945
3946 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3947 {
3948 return env->pmsav8.hprlar[env->pmsav8.hprselr];
3949 }
3950
3951 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3952 uint64_t value)
3953 {
3954 uint32_t n;
3955 uint32_t bit;
3956 ARMCPU *cpu = env_archcpu(env);
3957
3958 /* Ignore writes to unimplemented regions */
3959 int rmax = MIN(cpu->pmsav8r_hdregion, 32);
3960 value &= MAKE_64BIT_MASK(0, rmax);
3961
3962 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3963
3964 /* Register alias is only valid for first 32 indexes */
3965 for (n = 0; n < rmax; ++n) {
3966 bit = extract32(value, n, 1);
3967 env->pmsav8.hprlar[n] = deposit32(
3968 env->pmsav8.hprlar[n], 0, 1, bit);
3969 }
3970 }
3971
3972 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3973 {
3974 uint32_t n;
3975 uint32_t result = 0x0;
3976 ARMCPU *cpu = env_archcpu(env);
3977
3978 /* Register alias is only valid for first 32 indexes */
3979 for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
3980 if (env->pmsav8.hprlar[n] & 0x1) {
3981 result |= (0x1 << n);
3982 }
3983 }
3984 return result;
3985 }
3986
3987 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3988 uint64_t value)
3989 {
3990 ARMCPU *cpu = env_archcpu(env);
3991
3992 /*
3993 * Ignore writes that would select not implemented region.
3994 * This is architecturally UNPREDICTABLE.
3995 */
3996 if (value >= cpu->pmsav8r_hdregion) {
3997 return;
3998 }
3999
4000 env->pmsav8.hprselr = value;
4001 }
4002
4003 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
4004 uint64_t value)
4005 {
4006 ARMCPU *cpu = env_archcpu(env);
4007 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4008 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4009
4010 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4011
4012 if (ri->opc1 & 4) {
4013 if (index >= cpu->pmsav8r_hdregion) {
4014 return;
4015 }
4016 if (ri->opc2 & 0x1) {
4017 env->pmsav8.hprlar[index] = value;
4018 } else {
4019 env->pmsav8.hprbar[index] = value;
4020 }
4021 } else {
4022 if (index >= cpu->pmsav7_dregion) {
4023 return;
4024 }
4025 if (ri->opc2 & 0x1) {
4026 env->pmsav8.rlar[M_REG_NS][index] = value;
4027 } else {
4028 env->pmsav8.rbar[M_REG_NS][index] = value;
4029 }
4030 }
4031 }
4032
4033 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
4034 {
4035 ARMCPU *cpu = env_archcpu(env);
4036 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4037 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4038
4039 if (ri->opc1 & 4) {
4040 if (index >= cpu->pmsav8r_hdregion) {
4041 return 0x0;
4042 }
4043 if (ri->opc2 & 0x1) {
4044 return env->pmsav8.hprlar[index];
4045 } else {
4046 return env->pmsav8.hprbar[index];
4047 }
4048 } else {
4049 if (index >= cpu->pmsav7_dregion) {
4050 return 0x0;
4051 }
4052 if (ri->opc2 & 0x1) {
4053 return env->pmsav8.rlar[M_REG_NS][index];
4054 } else {
4055 return env->pmsav8.rbar[M_REG_NS][index];
4056 }
4057 }
4058 }
4059
4060 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
4061 { .name = "PRBAR",
4062 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
4063 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4064 .accessfn = access_tvm_trvm,
4065 .readfn = prbar_read, .writefn = prbar_write },
4066 { .name = "PRLAR",
4067 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
4068 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4069 .accessfn = access_tvm_trvm,
4070 .readfn = prlar_read, .writefn = prlar_write },
4071 { .name = "PRSELR", .resetvalue = 0,
4072 .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
4073 .access = PL1_RW, .accessfn = access_tvm_trvm,
4074 .writefn = prselr_write,
4075 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
4076 { .name = "HPRBAR", .resetvalue = 0,
4077 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
4078 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4079 .readfn = hprbar_read, .writefn = hprbar_write },
4080 { .name = "HPRLAR",
4081 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
4082 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4083 .readfn = hprlar_read, .writefn = hprlar_write },
4084 { .name = "HPRSELR", .resetvalue = 0,
4085 .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
4086 .access = PL2_RW,
4087 .writefn = hprselr_write,
4088 .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
4089 { .name = "HPRENR",
4090 .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
4091 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4092 .readfn = hprenr_read, .writefn = hprenr_write },
4093 };
4094
4095 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
4096 /*
4097 * Reset for all these registers is handled in arm_cpu_reset(),
4098 * because the PMSAv7 is also used by M-profile CPUs, which do
4099 * not register cpregs but still need the state to be reset.
4100 */
4101 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4102 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4103 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
4104 .readfn = pmsav7_read, .writefn = pmsav7_write,
4105 .resetfn = arm_cp_reset_ignore },
4106 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4107 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4108 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
4109 .readfn = pmsav7_read, .writefn = pmsav7_write,
4110 .resetfn = arm_cp_reset_ignore },
4111 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4112 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4113 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
4114 .readfn = pmsav7_read, .writefn = pmsav7_write,
4115 .resetfn = arm_cp_reset_ignore },
4116 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4117 .access = PL1_RW,
4118 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
4119 .writefn = pmsav7_rgnr_write,
4120 .resetfn = arm_cp_reset_ignore },
4121 };
4122
4123 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4124 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4125 .access = PL1_RW, .type = ARM_CP_ALIAS,
4126 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4127 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4128 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4129 .access = PL1_RW, .type = ARM_CP_ALIAS,
4130 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4131 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4132 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4133 .access = PL1_RW,
4134 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4135 .resetvalue = 0, },
4136 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4137 .access = PL1_RW,
4138 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4139 .resetvalue = 0, },
4140 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4141 .access = PL1_RW,
4142 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4143 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4144 .access = PL1_RW,
4145 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
4146 /* Protection region base and size registers */
4147 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4148 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4149 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4150 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4151 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4152 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4153 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4154 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4155 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4156 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4157 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4158 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4159 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4160 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4161 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4162 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4163 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4164 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4165 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4166 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4167 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4168 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4169 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4170 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4171 };
4172
4173 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4174 uint64_t value)
4175 {
4176 ARMCPU *cpu = env_archcpu(env);
4177
4178 if (!arm_feature(env, ARM_FEATURE_V8)) {
4179 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4180 /*
4181 * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4182 * using Long-descriptor translation table format
4183 */
4184 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4185 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4186 /*
4187 * In an implementation that includes the Security Extensions
4188 * TTBCR has additional fields PD0 [4] and PD1 [5] for
4189 * Short-descriptor translation table format.
4190 */
4191 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4192 } else {
4193 value &= TTBCR_N;
4194 }
4195 }
4196
4197 if (arm_feature(env, ARM_FEATURE_LPAE)) {
4198 /*
4199 * With LPAE the TTBCR could result in a change of ASID
4200 * via the TTBCR.A1 bit, so do a TLB flush.
4201 */
4202 tlb_flush(CPU(cpu));
4203 }
4204 raw_write(env, ri, value);
4205 }
4206
4207 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4208 uint64_t value)
4209 {
4210 ARMCPU *cpu = env_archcpu(env);
4211
4212 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4213 tlb_flush(CPU(cpu));
4214 raw_write(env, ri, value);
4215 }
4216
4217 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4218 uint64_t value)
4219 {
4220 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
4221 if (cpreg_field_is_64bit(ri) &&
4222 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4223 ARMCPU *cpu = env_archcpu(env);
4224 tlb_flush(CPU(cpu));
4225 }
4226 raw_write(env, ri, value);
4227 }
4228
4229 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4230 uint64_t value)
4231 {
4232 /*
4233 * If we are running with E2&0 regime, then an ASID is active.
4234 * Flush if that might be changing. Note we're not checking
4235 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4236 * holds the active ASID, only checking the field that might.
4237 */
4238 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4239 (arm_hcr_el2_eff(env) & HCR_E2H)) {
4240 uint16_t mask = ARMMMUIdxBit_E20_2 |
4241 ARMMMUIdxBit_E20_2_PAN |
4242 ARMMMUIdxBit_E20_0;
4243 tlb_flush_by_mmuidx(env_cpu(env), mask);
4244 }
4245 raw_write(env, ri, value);
4246 }
4247
4248 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4249 uint64_t value)
4250 {
4251 ARMCPU *cpu = env_archcpu(env);
4252 CPUState *cs = CPU(cpu);
4253
4254 /*
4255 * A change in VMID to the stage2 page table (Stage2) invalidates
4256 * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4257 */
4258 if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4259 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4260 }
4261 raw_write(env, ri, value);
4262 }
4263
4264 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4265 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4266 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4267 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4268 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4269 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4270 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4271 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4272 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4273 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4274 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4275 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4276 offsetof(CPUARMState, cp15.dfar_ns) } },
4277 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4278 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4279 .access = PL1_RW, .accessfn = access_tvm_trvm,
4280 .fgt = FGT_FAR_EL1,
4281 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4282 .resetvalue = 0, },
4283 };
4284
4285 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4286 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4287 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4288 .access = PL1_RW, .accessfn = access_tvm_trvm,
4289 .fgt = FGT_ESR_EL1,
4290 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4291 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4292 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4293 .access = PL1_RW, .accessfn = access_tvm_trvm,
4294 .fgt = FGT_TTBR0_EL1,
4295 .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4296 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4297 offsetof(CPUARMState, cp15.ttbr0_ns) } },
4298 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4299 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4300 .access = PL1_RW, .accessfn = access_tvm_trvm,
4301 .fgt = FGT_TTBR1_EL1,
4302 .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4303 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4304 offsetof(CPUARMState, cp15.ttbr1_ns) } },
4305 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4306 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4307 .access = PL1_RW, .accessfn = access_tvm_trvm,
4308 .fgt = FGT_TCR_EL1,
4309 .writefn = vmsa_tcr_el12_write,
4310 .raw_writefn = raw_write,
4311 .resetvalue = 0,
4312 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4313 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4314 .access = PL1_RW, .accessfn = access_tvm_trvm,
4315 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4316 .raw_writefn = raw_write,
4317 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4318 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4319 };
4320
4321 /*
4322 * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4323 * qemu tlbs nor adjusting cached masks.
4324 */
4325 static const ARMCPRegInfo ttbcr2_reginfo = {
4326 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4327 .access = PL1_RW, .accessfn = access_tvm_trvm,
4328 .type = ARM_CP_ALIAS,
4329 .bank_fieldoffsets = {
4330 offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4331 offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4332 },
4333 };
4334
4335 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4336 uint64_t value)
4337 {
4338 env->cp15.c15_ticonfig = value & 0xe7;
4339 /* The OS_TYPE bit in this register changes the reported CPUID! */
4340 env->cp15.c0_cpuid = (value & (1 << 5)) ?
4341 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4342 }
4343
4344 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4345 uint64_t value)
4346 {
4347 env->cp15.c15_threadid = value & 0xffff;
4348 }
4349
4350 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4351 uint64_t value)
4352 {
4353 /* Wait-for-interrupt (deprecated) */
4354 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4355 }
4356
4357 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4358 uint64_t value)
4359 {
4360 /*
4361 * On OMAP there are registers indicating the max/min index of dcache lines
4362 * containing a dirty line; cache flush operations have to reset these.
4363 */
4364 env->cp15.c15_i_max = 0x000;
4365 env->cp15.c15_i_min = 0xff0;
4366 }
4367
4368 static const ARMCPRegInfo omap_cp_reginfo[] = {
4369 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4370 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4371 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4372 .resetvalue = 0, },
4373 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4374 .access = PL1_RW, .type = ARM_CP_NOP },
4375 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4376 .access = PL1_RW,
4377 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4378 .writefn = omap_ticonfig_write },
4379 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4380 .access = PL1_RW,
4381 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4382 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4383 .access = PL1_RW, .resetvalue = 0xff0,
4384 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4385 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4386 .access = PL1_RW,
4387 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4388 .writefn = omap_threadid_write },
4389 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4390 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4391 .type = ARM_CP_NO_RAW,
4392 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4393 /*
4394 * TODO: Peripheral port remap register:
4395 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4396 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4397 * when MMU is off.
4398 */
4399 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4400 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4401 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4402 .writefn = omap_cachemaint_write },
4403 { .name = "C9", .cp = 15, .crn = 9,
4404 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4405 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4406 };
4407
4408 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4409 uint64_t value)
4410 {
4411 env->cp15.c15_cpar = value & 0x3fff;
4412 }
4413
4414 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4415 { .name = "XSCALE_CPAR",
4416 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4417 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4418 .writefn = xscale_cpar_write, },
4419 { .name = "XSCALE_AUXCR",
4420 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4421 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4422 .resetvalue = 0, },
4423 /*
4424 * XScale specific cache-lockdown: since we have no cache we NOP these
4425 * and hope the guest does not really rely on cache behaviour.
4426 */
4427 { .name = "XSCALE_LOCK_ICACHE_LINE",
4428 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4429 .access = PL1_W, .type = ARM_CP_NOP },
4430 { .name = "XSCALE_UNLOCK_ICACHE",
4431 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4432 .access = PL1_W, .type = ARM_CP_NOP },
4433 { .name = "XSCALE_DCACHE_LOCK",
4434 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4435 .access = PL1_RW, .type = ARM_CP_NOP },
4436 { .name = "XSCALE_UNLOCK_DCACHE",
4437 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4438 .access = PL1_W, .type = ARM_CP_NOP },
4439 };
4440
4441 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4442 /*
4443 * RAZ/WI the whole crn=15 space, when we don't have a more specific
4444 * implementation of this implementation-defined space.
4445 * Ideally this should eventually disappear in favour of actually
4446 * implementing the correct behaviour for all cores.
4447 */
4448 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4449 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4450 .access = PL1_RW,
4451 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4452 .resetvalue = 0 },
4453 };
4454
4455 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4456 /* Cache status: RAZ because we have no cache so it's always clean */
4457 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4458 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4459 .resetvalue = 0 },
4460 };
4461
4462 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4463 /* We never have a block transfer operation in progress */
4464 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4465 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4466 .resetvalue = 0 },
4467 /* The cache ops themselves: these all NOP for QEMU */
4468 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4469 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4470 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4471 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4472 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4473 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4474 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4475 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4476 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4477 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4478 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4479 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4480 };
4481
4482 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4483 /*
4484 * The cache test-and-clean instructions always return (1 << 30)
4485 * to indicate that there are no dirty cache lines.
4486 */
4487 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4488 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4489 .resetvalue = (1 << 30) },
4490 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4491 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4492 .resetvalue = (1 << 30) },
4493 };
4494
4495 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4496 /* Ignore ReadBuffer accesses */
4497 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4498 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4499 .access = PL1_RW, .resetvalue = 0,
4500 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4501 };
4502
4503 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4504 {
4505 unsigned int cur_el = arm_current_el(env);
4506
4507 if (arm_is_el2_enabled(env) && cur_el == 1) {
4508 return env->cp15.vpidr_el2;
4509 }
4510 return raw_read(env, ri);
4511 }
4512
4513 static uint64_t mpidr_read_val(CPUARMState *env)
4514 {
4515 ARMCPU *cpu = env_archcpu(env);
4516 uint64_t mpidr = cpu->mp_affinity;
4517
4518 if (arm_feature(env, ARM_FEATURE_V7MP)) {
4519 mpidr |= (1U << 31);
4520 /*
4521 * Cores which are uniprocessor (non-coherent)
4522 * but still implement the MP extensions set
4523 * bit 30. (For instance, Cortex-R5).
4524 */
4525 if (cpu->mp_is_up) {
4526 mpidr |= (1u << 30);
4527 }
4528 }
4529 return mpidr;
4530 }
4531
4532 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4533 {
4534 unsigned int cur_el = arm_current_el(env);
4535
4536 if (arm_is_el2_enabled(env) && cur_el == 1) {
4537 return env->cp15.vmpidr_el2;
4538 }
4539 return mpidr_read_val(env);
4540 }
4541
4542 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4543 /* NOP AMAIR0/1 */
4544 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4545 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4546 .access = PL1_RW, .accessfn = access_tvm_trvm,
4547 .fgt = FGT_AMAIR_EL1,
4548 .type = ARM_CP_CONST, .resetvalue = 0 },
4549 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4550 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4551 .access = PL1_RW, .accessfn = access_tvm_trvm,
4552 .type = ARM_CP_CONST, .resetvalue = 0 },
4553 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4554 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4555 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4556 offsetof(CPUARMState, cp15.par_ns)} },
4557 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4558 .access = PL1_RW, .accessfn = access_tvm_trvm,
4559 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4560 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4561 offsetof(CPUARMState, cp15.ttbr0_ns) },
4562 .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4563 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4564 .access = PL1_RW, .accessfn = access_tvm_trvm,
4565 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4566 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4567 offsetof(CPUARMState, cp15.ttbr1_ns) },
4568 .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4569 };
4570
4571 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4572 {
4573 return vfp_get_fpcr(env);
4574 }
4575
4576 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4577 uint64_t value)
4578 {
4579 vfp_set_fpcr(env, value);
4580 }
4581
4582 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4583 {
4584 return vfp_get_fpsr(env);
4585 }
4586
4587 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4588 uint64_t value)
4589 {
4590 vfp_set_fpsr(env, value);
4591 }
4592
4593 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4594 bool isread)
4595 {
4596 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4597 return CP_ACCESS_TRAP;
4598 }
4599 return CP_ACCESS_OK;
4600 }
4601
4602 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4603 uint64_t value)
4604 {
4605 env->daif = value & PSTATE_DAIF;
4606 }
4607
4608 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4609 {
4610 return env->pstate & PSTATE_PAN;
4611 }
4612
4613 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4614 uint64_t value)
4615 {
4616 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4617 }
4618
4619 static const ARMCPRegInfo pan_reginfo = {
4620 .name = "PAN", .state = ARM_CP_STATE_AA64,
4621 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4622 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4623 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4624 };
4625
4626 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4627 {
4628 return env->pstate & PSTATE_UAO;
4629 }
4630
4631 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4632 uint64_t value)
4633 {
4634 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4635 }
4636
4637 static const ARMCPRegInfo uao_reginfo = {
4638 .name = "UAO", .state = ARM_CP_STATE_AA64,
4639 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4640 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4641 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4642 };
4643
4644 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4645 {
4646 return env->pstate & PSTATE_DIT;
4647 }
4648
4649 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4650 uint64_t value)
4651 {
4652 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4653 }
4654
4655 static const ARMCPRegInfo dit_reginfo = {
4656 .name = "DIT", .state = ARM_CP_STATE_AA64,
4657 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4658 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4659 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4660 };
4661
4662 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4663 {
4664 return env->pstate & PSTATE_SSBS;
4665 }
4666
4667 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4668 uint64_t value)
4669 {
4670 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4671 }
4672
4673 static const ARMCPRegInfo ssbs_reginfo = {
4674 .name = "SSBS", .state = ARM_CP_STATE_AA64,
4675 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4676 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4677 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4678 };
4679
4680 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4681 const ARMCPRegInfo *ri,
4682 bool isread)
4683 {
4684 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4685 switch (arm_current_el(env)) {
4686 case 0:
4687 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4688 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4689 return CP_ACCESS_TRAP;
4690 }
4691 /* fall through */
4692 case 1:
4693 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4694 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4695 return CP_ACCESS_TRAP_EL2;
4696 }
4697 break;
4698 }
4699 return CP_ACCESS_OK;
4700 }
4701
4702 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4703 {
4704 /* Cache invalidate/clean to Point of Unification... */
4705 switch (arm_current_el(env)) {
4706 case 0:
4707 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4708 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4709 return CP_ACCESS_TRAP;
4710 }
4711 /* fall through */
4712 case 1:
4713 /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set. */
4714 if (arm_hcr_el2_eff(env) & hcrflags) {
4715 return CP_ACCESS_TRAP_EL2;
4716 }
4717 break;
4718 }
4719 return CP_ACCESS_OK;
4720 }
4721
4722 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4723 bool isread)
4724 {
4725 return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4726 }
4727
4728 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4729 bool isread)
4730 {
4731 return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4732 }
4733
4734 /*
4735 * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4736 * Page D4-1736 (DDI0487A.b)
4737 */
4738
4739 static int vae1_tlbmask(CPUARMState *env)
4740 {
4741 uint64_t hcr = arm_hcr_el2_eff(env);
4742 uint16_t mask;
4743
4744 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4745 mask = ARMMMUIdxBit_E20_2 |
4746 ARMMMUIdxBit_E20_2_PAN |
4747 ARMMMUIdxBit_E20_0;
4748 } else {
4749 mask = ARMMMUIdxBit_E10_1 |
4750 ARMMMUIdxBit_E10_1_PAN |
4751 ARMMMUIdxBit_E10_0;
4752 }
4753 return mask;
4754 }
4755
4756 static int vae2_tlbmask(CPUARMState *env)
4757 {
4758 uint64_t hcr = arm_hcr_el2_eff(env);
4759 uint16_t mask;
4760
4761 if (hcr & HCR_E2H) {
4762 mask = ARMMMUIdxBit_E20_2 |
4763 ARMMMUIdxBit_E20_2_PAN |
4764 ARMMMUIdxBit_E20_0;
4765 } else {
4766 mask = ARMMMUIdxBit_E2;
4767 }
4768 return mask;
4769 }
4770
4771 /* Return 56 if TBI is enabled, 64 otherwise. */
4772 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4773 uint64_t addr)
4774 {
4775 uint64_t tcr = regime_tcr(env, mmu_idx);
4776 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4777 int select = extract64(addr, 55, 1);
4778
4779 return (tbi >> select) & 1 ? 56 : 64;
4780 }
4781
4782 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4783 {
4784 uint64_t hcr = arm_hcr_el2_eff(env);
4785 ARMMMUIdx mmu_idx;
4786
4787 /* Only the regime of the mmu_idx below is significant. */
4788 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4789 mmu_idx = ARMMMUIdx_E20_0;
4790 } else {
4791 mmu_idx = ARMMMUIdx_E10_0;
4792 }
4793
4794 return tlbbits_for_regime(env, mmu_idx, addr);
4795 }
4796
4797 static int vae2_tlbbits(CPUARMState *env, uint64_t addr)
4798 {
4799 uint64_t hcr = arm_hcr_el2_eff(env);
4800 ARMMMUIdx mmu_idx;
4801
4802 /*
4803 * Only the regime of the mmu_idx below is significant.
4804 * Regime EL2&0 has two ranges with separate TBI configuration, while EL2
4805 * only has one.
4806 */
4807 if (hcr & HCR_E2H) {
4808 mmu_idx = ARMMMUIdx_E20_2;
4809 } else {
4810 mmu_idx = ARMMMUIdx_E2;
4811 }
4812
4813 return tlbbits_for_regime(env, mmu_idx, addr);
4814 }
4815
4816 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4817 uint64_t value)
4818 {
4819 CPUState *cs = env_cpu(env);
4820 int mask = vae1_tlbmask(env);
4821
4822 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4823 }
4824
4825 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4826 uint64_t value)
4827 {
4828 CPUState *cs = env_cpu(env);
4829 int mask = vae1_tlbmask(env);
4830
4831 if (tlb_force_broadcast(env)) {
4832 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4833 } else {
4834 tlb_flush_by_mmuidx(cs, mask);
4835 }
4836 }
4837
4838 static int e2_tlbmask(CPUARMState *env)
4839 {
4840 return (ARMMMUIdxBit_E20_0 |
4841 ARMMMUIdxBit_E20_2 |
4842 ARMMMUIdxBit_E20_2_PAN |
4843 ARMMMUIdxBit_E2);
4844 }
4845
4846 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4847 uint64_t value)
4848 {
4849 CPUState *cs = env_cpu(env);
4850 int mask = alle1_tlbmask(env);
4851
4852 tlb_flush_by_mmuidx(cs, mask);
4853 }
4854
4855 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4856 uint64_t value)
4857 {
4858 CPUState *cs = env_cpu(env);
4859 int mask = e2_tlbmask(env);
4860
4861 tlb_flush_by_mmuidx(cs, mask);
4862 }
4863
4864 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4865 uint64_t value)
4866 {
4867 ARMCPU *cpu = env_archcpu(env);
4868 CPUState *cs = CPU(cpu);
4869
4870 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
4871 }
4872
4873 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4874 uint64_t value)
4875 {
4876 CPUState *cs = env_cpu(env);
4877 int mask = alle1_tlbmask(env);
4878
4879 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4880 }
4881
4882 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4883 uint64_t value)
4884 {
4885 CPUState *cs = env_cpu(env);
4886 int mask = e2_tlbmask(env);
4887
4888 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4889 }
4890
4891 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4892 uint64_t value)
4893 {
4894 CPUState *cs = env_cpu(env);
4895
4896 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
4897 }
4898
4899 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4900 uint64_t value)
4901 {
4902 /*
4903 * Invalidate by VA, EL2
4904 * Currently handles both VAE2 and VALE2, since we don't support
4905 * flush-last-level-only.
4906 */
4907 CPUState *cs = env_cpu(env);
4908 int mask = vae2_tlbmask(env);
4909 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4910 int bits = vae2_tlbbits(env, pageaddr);
4911
4912 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4913 }
4914
4915 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4916 uint64_t value)
4917 {
4918 /*
4919 * Invalidate by VA, EL3
4920 * Currently handles both VAE3 and VALE3, since we don't support
4921 * flush-last-level-only.
4922 */
4923 ARMCPU *cpu = env_archcpu(env);
4924 CPUState *cs = CPU(cpu);
4925 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4926
4927 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
4928 }
4929
4930 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4931 uint64_t value)
4932 {
4933 CPUState *cs = env_cpu(env);
4934 int mask = vae1_tlbmask(env);
4935 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4936 int bits = vae1_tlbbits(env, pageaddr);
4937
4938 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4939 }
4940
4941 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4942 uint64_t value)
4943 {
4944 /*
4945 * Invalidate by VA, EL1&0 (AArch64 version).
4946 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4947 * since we don't support flush-for-specific-ASID-only or
4948 * flush-last-level-only.
4949 */
4950 CPUState *cs = env_cpu(env);
4951 int mask = vae1_tlbmask(env);
4952 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4953 int bits = vae1_tlbbits(env, pageaddr);
4954
4955 if (tlb_force_broadcast(env)) {
4956 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4957 } else {
4958 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
4959 }
4960 }
4961
4962 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4963 uint64_t value)
4964 {
4965 CPUState *cs = env_cpu(env);
4966 int mask = vae2_tlbmask(env);
4967 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4968 int bits = vae2_tlbbits(env, pageaddr);
4969
4970 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
4971 }
4972
4973 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4974 uint64_t value)
4975 {
4976 CPUState *cs = env_cpu(env);
4977 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4978 int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
4979
4980 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4981 ARMMMUIdxBit_E3, bits);
4982 }
4983
4984 static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
4985 {
4986 /*
4987 * The MSB of value is the NS field, which only applies if SEL2
4988 * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
4989 */
4990 return (value >= 0
4991 && cpu_isar_feature(aa64_sel2, env_archcpu(env))
4992 && arm_is_secure_below_el3(env)
4993 ? ARMMMUIdxBit_Stage2_S
4994 : ARMMMUIdxBit_Stage2);
4995 }
4996
4997 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4998 uint64_t value)
4999 {
5000 CPUState *cs = env_cpu(env);
5001 int mask = ipas2e1_tlbmask(env, value);
5002 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5003
5004 if (tlb_force_broadcast(env)) {
5005 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
5006 } else {
5007 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
5008 }
5009 }
5010
5011 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5012 uint64_t value)
5013 {
5014 CPUState *cs = env_cpu(env);
5015 int mask = ipas2e1_tlbmask(env, value);
5016 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5017
5018 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
5019 }
5020
5021 #ifdef TARGET_AARCH64
5022 typedef struct {
5023 uint64_t base;
5024 uint64_t length;
5025 } TLBIRange;
5026
5027 static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
5028 {
5029 /*
5030 * Note that the TLBI range TG field encoding differs from both
5031 * TG0 and TG1 encodings.
5032 */
5033 switch (tg) {
5034 case 1:
5035 return Gran4K;
5036 case 2:
5037 return Gran16K;
5038 case 3:
5039 return Gran64K;
5040 default:
5041 return GranInvalid;
5042 }
5043 }
5044
5045 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
5046 uint64_t value)
5047 {
5048 unsigned int page_size_granule, page_shift, num, scale, exponent;
5049 /* Extract one bit to represent the va selector in use. */
5050 uint64_t select = sextract64(value, 36, 1);
5051 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true, false);
5052 TLBIRange ret = { };
5053 ARMGranuleSize gran;
5054
5055 page_size_granule = extract64(value, 46, 2);
5056 gran = tlbi_range_tg_to_gran_size(page_size_granule);
5057
5058 /* The granule encoded in value must match the granule in use. */
5059 if (gran != param.gran) {
5060 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
5061 page_size_granule);
5062 return ret;
5063 }
5064
5065 page_shift = arm_granule_bits(gran);
5066 num = extract64(value, 39, 5);
5067 scale = extract64(value, 44, 2);
5068 exponent = (5 * scale) + 1;
5069
5070 ret.length = (num + 1) << (exponent + page_shift);
5071
5072 if (param.select) {
5073 ret.base = sextract64(value, 0, 37);
5074 } else {
5075 ret.base = extract64(value, 0, 37);
5076 }
5077 if (param.ds) {
5078 /*
5079 * With DS=1, BaseADDR is always shifted 16 so that it is able
5080 * to address all 52 va bits. The input address is perforce
5081 * aligned on a 64k boundary regardless of translation granule.
5082 */
5083 page_shift = 16;
5084 }
5085 ret.base <<= page_shift;
5086
5087 return ret;
5088 }
5089
5090 static void do_rvae_write(CPUARMState *env, uint64_t value,
5091 int idxmap, bool synced)
5092 {
5093 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
5094 TLBIRange range;
5095 int bits;
5096
5097 range = tlbi_aa64_get_range(env, one_idx, value);
5098 bits = tlbbits_for_regime(env, one_idx, range.base);
5099
5100 if (synced) {
5101 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
5102 range.base,
5103 range.length,
5104 idxmap,
5105 bits);
5106 } else {
5107 tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
5108 range.length, idxmap, bits);
5109 }
5110 }
5111
5112 static void tlbi_aa64_rvae1_write(CPUARMState *env,
5113 const ARMCPRegInfo *ri,
5114 uint64_t value)
5115 {
5116 /*
5117 * Invalidate by VA range, EL1&0.
5118 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
5119 * since we don't support flush-for-specific-ASID-only or
5120 * flush-last-level-only.
5121 */
5122
5123 do_rvae_write(env, value, vae1_tlbmask(env),
5124 tlb_force_broadcast(env));
5125 }
5126
5127 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
5128 const ARMCPRegInfo *ri,
5129 uint64_t value)
5130 {
5131 /*
5132 * Invalidate by VA range, Inner/Outer Shareable EL1&0.
5133 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
5134 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
5135 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
5136 * shareable specific flushes.
5137 */
5138
5139 do_rvae_write(env, value, vae1_tlbmask(env), true);
5140 }
5141
5142 static void tlbi_aa64_rvae2_write(CPUARMState *env,
5143 const ARMCPRegInfo *ri,
5144 uint64_t value)
5145 {
5146 /*
5147 * Invalidate by VA range, EL2.
5148 * Currently handles all of RVAE2 and RVALE2,
5149 * since we don't support flush-for-specific-ASID-only or
5150 * flush-last-level-only.
5151 */
5152
5153 do_rvae_write(env, value, vae2_tlbmask(env),
5154 tlb_force_broadcast(env));
5155
5156
5157 }
5158
5159 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
5160 const ARMCPRegInfo *ri,
5161 uint64_t value)
5162 {
5163 /*
5164 * Invalidate by VA range, Inner/Outer Shareable, EL2.
5165 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
5166 * since we don't support flush-for-specific-ASID-only,
5167 * flush-last-level-only or inner/outer shareable specific flushes.
5168 */
5169
5170 do_rvae_write(env, value, vae2_tlbmask(env), true);
5171
5172 }
5173
5174 static void tlbi_aa64_rvae3_write(CPUARMState *env,
5175 const ARMCPRegInfo *ri,
5176 uint64_t value)
5177 {
5178 /*
5179 * Invalidate by VA range, EL3.
5180 * Currently handles all of RVAE3 and RVALE3,
5181 * since we don't support flush-for-specific-ASID-only or
5182 * flush-last-level-only.
5183 */
5184
5185 do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
5186 }
5187
5188 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5189 const ARMCPRegInfo *ri,
5190 uint64_t value)
5191 {
5192 /*
5193 * Invalidate by VA range, EL3, Inner/Outer Shareable.
5194 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5195 * since we don't support flush-for-specific-ASID-only,
5196 * flush-last-level-only or inner/outer specific flushes.
5197 */
5198
5199 do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
5200 }
5201
5202 static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5203 uint64_t value)
5204 {
5205 do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5206 tlb_force_broadcast(env));
5207 }
5208
5209 static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5210 const ARMCPRegInfo *ri,
5211 uint64_t value)
5212 {
5213 do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5214 }
5215 #endif
5216
5217 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5218 bool isread)
5219 {
5220 int cur_el = arm_current_el(env);
5221
5222 if (cur_el < 2) {
5223 uint64_t hcr = arm_hcr_el2_eff(env);
5224
5225 if (cur_el == 0) {
5226 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5227 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5228 return CP_ACCESS_TRAP_EL2;
5229 }
5230 } else {
5231 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5232 return CP_ACCESS_TRAP;
5233 }
5234 if (hcr & HCR_TDZ) {
5235 return CP_ACCESS_TRAP_EL2;
5236 }
5237 }
5238 } else if (hcr & HCR_TDZ) {
5239 return CP_ACCESS_TRAP_EL2;
5240 }
5241 }
5242 return CP_ACCESS_OK;
5243 }
5244
5245 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5246 {
5247 ARMCPU *cpu = env_archcpu(env);
5248 int dzp_bit = 1 << 4;
5249
5250 /* DZP indicates whether DC ZVA access is allowed */
5251 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
5252 dzp_bit = 0;
5253 }
5254 return cpu->dcz_blocksize | dzp_bit;
5255 }
5256
5257 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5258 bool isread)
5259 {
5260 if (!(env->pstate & PSTATE_SP)) {
5261 /*
5262 * Access to SP_EL0 is undefined if it's being used as
5263 * the stack pointer.
5264 */
5265 return CP_ACCESS_TRAP_UNCATEGORIZED;
5266 }
5267 return CP_ACCESS_OK;
5268 }
5269
5270 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5271 {
5272 return env->pstate & PSTATE_SP;
5273 }
5274
5275 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5276 {
5277 update_spsel(env, val);
5278 }
5279
5280 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5281 uint64_t value)
5282 {
5283 ARMCPU *cpu = env_archcpu(env);
5284
5285 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5286 /* M bit is RAZ/WI for PMSA with no MPU implemented */
5287 value &= ~SCTLR_M;
5288 }
5289
5290 /* ??? Lots of these bits are not implemented. */
5291
5292 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5293 if (ri->opc1 == 6) { /* SCTLR_EL3 */
5294 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5295 } else {
5296 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5297 SCTLR_ATA0 | SCTLR_ATA);
5298 }
5299 }
5300
5301 if (raw_read(env, ri) == value) {
5302 /*
5303 * Skip the TLB flush if nothing actually changed; Linux likes
5304 * to do a lot of pointless SCTLR writes.
5305 */
5306 return;
5307 }
5308
5309 raw_write(env, ri, value);
5310
5311 /* This may enable/disable the MMU, so do a TLB flush. */
5312 tlb_flush(CPU(cpu));
5313
5314 if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
5315 /*
5316 * Normally we would always end the TB on an SCTLR write; see the
5317 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5318 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5319 * of hflags from the translator, so do it here.
5320 */
5321 arm_rebuild_hflags(env);
5322 }
5323 }
5324
5325 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5326 uint64_t value)
5327 {
5328 /*
5329 * Some MDCR_EL3 bits affect whether PMU counters are running:
5330 * if we are trying to change any of those then we must
5331 * bracket this update with PMU start/finish calls.
5332 */
5333 bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5334
5335 if (pmu_op) {
5336 pmu_op_start(env);
5337 }
5338 env->cp15.mdcr_el3 = value;
5339 if (pmu_op) {
5340 pmu_op_finish(env);
5341 }
5342 }
5343
5344 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5345 uint64_t value)
5346 {
5347 /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5348 mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5349 }
5350
5351 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5352 uint64_t value)
5353 {
5354 /*
5355 * Some MDCR_EL2 bits affect whether PMU counters are running:
5356 * if we are trying to change any of those then we must
5357 * bracket this update with PMU start/finish calls.
5358 */
5359 bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5360
5361 if (pmu_op) {
5362 pmu_op_start(env);
5363 }
5364 env->cp15.mdcr_el2 = value;
5365 if (pmu_op) {
5366 pmu_op_finish(env);
5367 }
5368 }
5369
5370 static CPAccessResult access_nv1(CPUARMState *env, const ARMCPRegInfo *ri,
5371 bool isread)
5372 {
5373 if (arm_current_el(env) == 1) {
5374 uint64_t hcr_nv = arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1 | HCR_NV2);
5375
5376 if (hcr_nv == (HCR_NV | HCR_NV1)) {
5377 return CP_ACCESS_TRAP_EL2;
5378 }
5379 }
5380 return CP_ACCESS_OK;
5381 }
5382
5383 #ifdef CONFIG_USER_ONLY
5384 /*
5385 * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
5386 * code to get around W^X restrictions, where one region is writable and the
5387 * other is executable.
5388 *
5389 * Since the executable region is never written to we cannot detect code
5390 * changes when running in user mode, and rely on the emulated JIT telling us
5391 * that the code has changed by executing this instruction.
5392 */
5393 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
5394 uint64_t value)
5395 {
5396 uint64_t icache_line_mask, start_address, end_address;
5397 const ARMCPU *cpu;
5398
5399 cpu = env_archcpu(env);
5400
5401 icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
5402 start_address = value & ~icache_line_mask;
5403 end_address = value | icache_line_mask;
5404
5405 mmap_lock();
5406
5407 tb_invalidate_phys_range(start_address, end_address);
5408
5409 mmap_unlock();
5410 }
5411 #endif
5412
5413 static const ARMCPRegInfo v8_cp_reginfo[] = {
5414 /*
5415 * Minimal set of EL0-visible registers. This will need to be expanded
5416 * significantly for system emulation of AArch64 CPUs.
5417 */
5418 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5419 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5420 .access = PL0_RW, .type = ARM_CP_NZCV },
5421 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5422 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5423 .type = ARM_CP_NO_RAW,
5424 .access = PL0_RW, .accessfn = aa64_daif_access,
5425 .fieldoffset = offsetof(CPUARMState, daif),
5426 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5427 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5428 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5429 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5430 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5431 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5432 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5433 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5434 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5435 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5436 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5437 .access = PL0_R, .type = ARM_CP_NO_RAW,
5438 .fgt = FGT_DCZID_EL0,
5439 .readfn = aa64_dczid_read },
5440 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5441 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5442 .access = PL0_W, .type = ARM_CP_DC_ZVA,
5443 #ifndef CONFIG_USER_ONLY
5444 /* Avoid overhead of an access check that always passes in user-mode */
5445 .accessfn = aa64_zva_access,
5446 .fgt = FGT_DCZVA,
5447 #endif
5448 },
5449 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5450 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5451 .access = PL1_R, .type = ARM_CP_CURRENTEL },
5452 /*
5453 * Instruction cache ops. All of these except `IC IVAU` NOP because we
5454 * don't emulate caches.
5455 */
5456 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5457 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5458 .access = PL1_W, .type = ARM_CP_NOP,
5459 .fgt = FGT_ICIALLUIS,
5460 .accessfn = access_ticab },
5461 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5462 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5463 .access = PL1_W, .type = ARM_CP_NOP,
5464 .fgt = FGT_ICIALLU,
5465 .accessfn = access_tocu },
5466 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5467 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5468 .access = PL0_W,
5469 .fgt = FGT_ICIVAU,
5470 .accessfn = access_tocu,
5471 #ifdef CONFIG_USER_ONLY
5472 .type = ARM_CP_NO_RAW,
5473 .writefn = ic_ivau_write
5474 #else
5475 .type = ARM_CP_NOP
5476 #endif
5477 },
5478 /* Cache ops: all NOPs since we don't emulate caches */
5479 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5480 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5481 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5482 .fgt = FGT_DCIVAC,
5483 .type = ARM_CP_NOP },
5484 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5485 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5486 .fgt = FGT_DCISW,
5487 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5488 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5489 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5490 .access = PL0_W, .type = ARM_CP_NOP,
5491 .fgt = FGT_DCCVAC,
5492 .accessfn = aa64_cacheop_poc_access },
5493 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5494 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5495 .fgt = FGT_DCCSW,
5496 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5497 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5498 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5499 .access = PL0_W, .type = ARM_CP_NOP,
5500 .fgt = FGT_DCCVAU,
5501 .accessfn = access_tocu },
5502 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5503 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5504 .access = PL0_W, .type = ARM_CP_NOP,
5505 .fgt = FGT_DCCIVAC,
5506 .accessfn = aa64_cacheop_poc_access },
5507 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5508 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5509 .fgt = FGT_DCCISW,
5510 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5511 /* TLBI operations */
5512 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
5513 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
5514 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5515 .fgt = FGT_TLBIVMALLE1IS,
5516 .writefn = tlbi_aa64_vmalle1is_write },
5517 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
5518 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
5519 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5520 .fgt = FGT_TLBIVAE1IS,
5521 .writefn = tlbi_aa64_vae1is_write },
5522 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
5523 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
5524 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5525 .fgt = FGT_TLBIASIDE1IS,
5526 .writefn = tlbi_aa64_vmalle1is_write },
5527 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
5528 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
5529 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5530 .fgt = FGT_TLBIVAAE1IS,
5531 .writefn = tlbi_aa64_vae1is_write },
5532 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
5533 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5534 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5535 .fgt = FGT_TLBIVALE1IS,
5536 .writefn = tlbi_aa64_vae1is_write },
5537 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
5538 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5539 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5540 .fgt = FGT_TLBIVAALE1IS,
5541 .writefn = tlbi_aa64_vae1is_write },
5542 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
5543 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
5544 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5545 .fgt = FGT_TLBIVMALLE1,
5546 .writefn = tlbi_aa64_vmalle1_write },
5547 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
5548 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
5549 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5550 .fgt = FGT_TLBIVAE1,
5551 .writefn = tlbi_aa64_vae1_write },
5552 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
5553 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
5554 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5555 .fgt = FGT_TLBIASIDE1,
5556 .writefn = tlbi_aa64_vmalle1_write },
5557 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
5558 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
5559 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5560 .fgt = FGT_TLBIVAAE1,
5561 .writefn = tlbi_aa64_vae1_write },
5562 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
5563 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5564 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5565 .fgt = FGT_TLBIVALE1,
5566 .writefn = tlbi_aa64_vae1_write },
5567 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
5568 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5569 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5570 .fgt = FGT_TLBIVAALE1,
5571 .writefn = tlbi_aa64_vae1_write },
5572 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5573 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5574 .access = PL2_W, .type = ARM_CP_NO_RAW,
5575 .writefn = tlbi_aa64_ipas2e1is_write },
5576 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5577 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5578 .access = PL2_W, .type = ARM_CP_NO_RAW,
5579 .writefn = tlbi_aa64_ipas2e1is_write },
5580 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5581 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5582 .access = PL2_W, .type = ARM_CP_NO_RAW,
5583 .writefn = tlbi_aa64_alle1is_write },
5584 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5585 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5586 .access = PL2_W, .type = ARM_CP_NO_RAW,
5587 .writefn = tlbi_aa64_alle1is_write },
5588 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5589 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5590 .access = PL2_W, .type = ARM_CP_NO_RAW,
5591 .writefn = tlbi_aa64_ipas2e1_write },
5592 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5593 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5594 .access = PL2_W, .type = ARM_CP_NO_RAW,
5595 .writefn = tlbi_aa64_ipas2e1_write },
5596 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5597 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5598 .access = PL2_W, .type = ARM_CP_NO_RAW,
5599 .writefn = tlbi_aa64_alle1_write },
5600 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5601 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5602 .access = PL2_W, .type = ARM_CP_NO_RAW,
5603 .writefn = tlbi_aa64_alle1is_write },
5604 #ifndef CONFIG_USER_ONLY
5605 /* 64 bit address translation operations */
5606 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5607 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5608 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5609 .fgt = FGT_ATS1E1R,
5610 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5611 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5612 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5613 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5614 .fgt = FGT_ATS1E1W,
5615 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5616 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5617 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5618 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5619 .fgt = FGT_ATS1E0R,
5620 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5621 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5622 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5623 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5624 .fgt = FGT_ATS1E0W,
5625 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5626 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5627 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5628 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5629 .accessfn = at_e012_access, .writefn = ats_write64 },
5630 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5631 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5632 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5633 .accessfn = at_e012_access, .writefn = ats_write64 },
5634 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5635 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5636 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5637 .accessfn = at_e012_access, .writefn = ats_write64 },
5638 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5639 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5640 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5641 .accessfn = at_e012_access, .writefn = ats_write64 },
5642 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5643 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5644 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5645 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5646 .writefn = ats_write64 },
5647 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5648 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5649 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5650 .writefn = ats_write64 },
5651 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5652 .type = ARM_CP_ALIAS,
5653 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5654 .access = PL1_RW, .resetvalue = 0,
5655 .fgt = FGT_PAR_EL1,
5656 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5657 .writefn = par_write },
5658 #endif
5659 /* TLB invalidate last level of translation table walk */
5660 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5661 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5662 .writefn = tlbimva_is_write },
5663 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5664 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5665 .writefn = tlbimvaa_is_write },
5666 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5667 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5668 .writefn = tlbimva_write },
5669 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5670 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5671 .writefn = tlbimvaa_write },
5672 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5673 .type = ARM_CP_NO_RAW, .access = PL2_W,
5674 .writefn = tlbimva_hyp_write },
5675 { .name = "TLBIMVALHIS",
5676 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5677 .type = ARM_CP_NO_RAW, .access = PL2_W,
5678 .writefn = tlbimva_hyp_is_write },
5679 { .name = "TLBIIPAS2",
5680 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5681 .type = ARM_CP_NO_RAW, .access = PL2_W,
5682 .writefn = tlbiipas2_hyp_write },
5683 { .name = "TLBIIPAS2IS",
5684 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5685 .type = ARM_CP_NO_RAW, .access = PL2_W,
5686 .writefn = tlbiipas2is_hyp_write },
5687 { .name = "TLBIIPAS2L",
5688 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5689 .type = ARM_CP_NO_RAW, .access = PL2_W,
5690 .writefn = tlbiipas2_hyp_write },
5691 { .name = "TLBIIPAS2LIS",
5692 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5693 .type = ARM_CP_NO_RAW, .access = PL2_W,
5694 .writefn = tlbiipas2is_hyp_write },
5695 /* 32 bit cache operations */
5696 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5697 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5698 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5699 .type = ARM_CP_NOP, .access = PL1_W },
5700 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5701 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5702 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5703 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5704 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5705 .type = ARM_CP_NOP, .access = PL1_W },
5706 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5707 .type = ARM_CP_NOP, .access = PL1_W },
5708 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5709 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5710 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5711 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5712 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5713 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5714 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5715 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5716 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5717 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5718 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5719 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5720 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5721 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5722 /* MMU Domain access control / MPU write buffer control */
5723 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5724 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5725 .writefn = dacr_write, .raw_writefn = raw_write,
5726 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5727 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5728 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5729 .type = ARM_CP_ALIAS,
5730 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5731 .access = PL1_RW, .accessfn = access_nv1,
5732 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5733 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5734 .type = ARM_CP_ALIAS,
5735 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5736 .access = PL1_RW, .accessfn = access_nv1,
5737 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5738 /*
5739 * We rely on the access checks not allowing the guest to write to the
5740 * state field when SPSel indicates that it's being used as the stack
5741 * pointer.
5742 */
5743 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5744 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5745 .access = PL1_RW, .accessfn = sp_el0_access,
5746 .type = ARM_CP_ALIAS,
5747 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5748 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5749 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5750 .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5751 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5752 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5753 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5754 .type = ARM_CP_NO_RAW,
5755 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5756 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5757 .type = ARM_CP_ALIAS,
5758 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5759 .access = PL2_RW,
5760 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5761 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5762 .type = ARM_CP_ALIAS,
5763 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5764 .access = PL2_RW,
5765 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5766 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5767 .type = ARM_CP_ALIAS,
5768 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5769 .access = PL2_RW,
5770 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5771 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5772 .type = ARM_CP_ALIAS,
5773 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5774 .access = PL2_RW,
5775 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5776 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5777 .type = ARM_CP_IO,
5778 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5779 .resetvalue = 0,
5780 .access = PL3_RW,
5781 .writefn = mdcr_el3_write,
5782 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5783 { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5784 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5785 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5786 .writefn = sdcr_write,
5787 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5788 };
5789
5790 /* These are present only when EL1 supports AArch32 */
5791 static const ARMCPRegInfo v8_aa32_el1_reginfo[] = {
5792 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5793 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5794 .access = PL2_RW,
5795 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5796 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5797 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5798 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5799 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5800 .writefn = dacr_write, .raw_writefn = raw_write,
5801 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5802 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5803 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5804 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5805 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5806 };
5807
5808 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5809 {
5810 ARMCPU *cpu = env_archcpu(env);
5811
5812 if (arm_feature(env, ARM_FEATURE_V8)) {
5813 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5814 } else {
5815 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5816 }
5817
5818 if (arm_feature(env, ARM_FEATURE_EL3)) {
5819 valid_mask &= ~HCR_HCD;
5820 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5821 /*
5822 * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5823 * However, if we're using the SMC PSCI conduit then QEMU is
5824 * effectively acting like EL3 firmware and so the guest at
5825 * EL2 should retain the ability to prevent EL1 from being
5826 * able to make SMC calls into the ersatz firmware, so in
5827 * that case HCR.TSC should be read/write.
5828 */
5829 valid_mask &= ~HCR_TSC;
5830 }
5831
5832 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5833 if (cpu_isar_feature(aa64_vh, cpu)) {
5834 valid_mask |= HCR_E2H;
5835 }
5836 if (cpu_isar_feature(aa64_ras, cpu)) {
5837 valid_mask |= HCR_TERR | HCR_TEA;
5838 }
5839 if (cpu_isar_feature(aa64_lor, cpu)) {
5840 valid_mask |= HCR_TLOR;
5841 }
5842 if (cpu_isar_feature(aa64_pauth, cpu)) {
5843 valid_mask |= HCR_API | HCR_APK;
5844 }
5845 if (cpu_isar_feature(aa64_mte, cpu)) {
5846 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5847 }
5848 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5849 valid_mask |= HCR_ENSCXT;
5850 }
5851 if (cpu_isar_feature(aa64_fwb, cpu)) {
5852 valid_mask |= HCR_FWB;
5853 }
5854 if (cpu_isar_feature(aa64_rme, cpu)) {
5855 valid_mask |= HCR_GPF;
5856 }
5857 if (cpu_isar_feature(aa64_nv, cpu)) {
5858 valid_mask |= HCR_NV | HCR_NV1 | HCR_AT;
5859 }
5860 if (cpu_isar_feature(aa64_nv2, cpu)) {
5861 valid_mask |= HCR_NV2;
5862 }
5863 }
5864
5865 if (cpu_isar_feature(any_evt, cpu)) {
5866 valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5867 } else if (cpu_isar_feature(any_half_evt, cpu)) {
5868 valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5869 }
5870
5871 /* Clear RES0 bits. */
5872 value &= valid_mask;
5873
5874 /*
5875 * These bits change the MMU setup:
5876 * HCR_VM enables stage 2 translation
5877 * HCR_PTW forbids certain page-table setups
5878 * HCR_DC disables stage1 and enables stage2 translation
5879 * HCR_DCT enables tagging on (disabled) stage1 translation
5880 * HCR_FWB changes the interpretation of stage2 descriptor bits
5881 * HCR_NV and HCR_NV1 affect interpretation of descriptor bits
5882 */
5883 if ((env->cp15.hcr_el2 ^ value) &
5884 (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) {
5885 tlb_flush(CPU(cpu));
5886 }
5887 env->cp15.hcr_el2 = value;
5888
5889 /*
5890 * Updates to VI and VF require us to update the status of
5891 * virtual interrupts, which are the logical OR of these bits
5892 * and the state of the input lines from the GIC. (This requires
5893 * that we have the BQL, which is done by marking the
5894 * reginfo structs as ARM_CP_IO.)
5895 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5896 * possible for it to be taken immediately, because VIRQ and
5897 * VFIQ are masked unless running at EL0 or EL1, and HCR
5898 * can only be written at EL2.
5899 */
5900 g_assert(bql_locked());
5901 arm_cpu_update_virq(cpu);
5902 arm_cpu_update_vfiq(cpu);
5903 arm_cpu_update_vserr(cpu);
5904 }
5905
5906 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5907 {
5908 do_hcr_write(env, value, 0);
5909 }
5910
5911 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5912 uint64_t value)
5913 {
5914 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5915 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5916 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5917 }
5918
5919 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5920 uint64_t value)
5921 {
5922 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5923 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5924 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5925 }
5926
5927 /*
5928 * Return the effective value of HCR_EL2, at the given security state.
5929 * Bits that are not included here:
5930 * RW (read from SCR_EL3.RW as needed)
5931 */
5932 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
5933 {
5934 uint64_t ret = env->cp15.hcr_el2;
5935
5936 assert(space != ARMSS_Root);
5937
5938 if (!arm_is_el2_enabled_secstate(env, space)) {
5939 /*
5940 * "This register has no effect if EL2 is not enabled in the
5941 * current Security state". This is ARMv8.4-SecEL2 speak for
5942 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5943 *
5944 * Prior to that, the language was "In an implementation that
5945 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5946 * as if this field is 0 for all purposes other than a direct
5947 * read or write access of HCR_EL2". With lots of enumeration
5948 * on a per-field basis. In current QEMU, this is condition
5949 * is arm_is_secure_below_el3.
5950 *
5951 * Since the v8.4 language applies to the entire register, and
5952 * appears to be backward compatible, use that.
5953 */
5954 return 0;
5955 }
5956
5957 /*
5958 * For a cpu that supports both aarch64 and aarch32, we can set bits
5959 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5960 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5961 */
5962 if (!arm_el_is_aa64(env, 2)) {
5963 uint64_t aa32_valid;
5964
5965 /*
5966 * These bits are up-to-date as of ARMv8.6.
5967 * For HCR, it's easiest to list just the 2 bits that are invalid.
5968 * For HCR2, list those that are valid.
5969 */
5970 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5971 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5972 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5973 ret &= aa32_valid;
5974 }
5975
5976 if (ret & HCR_TGE) {
5977 /* These bits are up-to-date as of ARMv8.6. */
5978 if (ret & HCR_E2H) {
5979 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5980 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5981 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5982 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5983 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5984 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5985 } else {
5986 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5987 }
5988 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5989 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5990 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5991 HCR_TLOR);
5992 }
5993
5994 return ret;
5995 }
5996
5997 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5998 {
5999 if (arm_feature(env, ARM_FEATURE_M)) {
6000 return 0;
6001 }
6002 return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
6003 }
6004
6005 /*
6006 * Corresponds to ARM pseudocode function ELIsInHost().
6007 */
6008 bool el_is_in_host(CPUARMState *env, int el)
6009 {
6010 uint64_t mask;
6011
6012 /*
6013 * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
6014 * Perform the simplest bit tests first, and validate EL2 afterward.
6015 */
6016 if (el & 1) {
6017 return false; /* EL1 or EL3 */
6018 }
6019
6020 /*
6021 * Note that hcr_write() checks isar_feature_aa64_vh(),
6022 * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
6023 */
6024 mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
6025 if ((env->cp15.hcr_el2 & mask) != mask) {
6026 return false;
6027 }
6028
6029 /* TGE and/or E2H set: double check those bits are currently legal. */
6030 return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
6031 }
6032
6033 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
6034 uint64_t value)
6035 {
6036 uint64_t valid_mask = 0;
6037
6038 /* FEAT_MOPS adds MSCEn and MCE2 */
6039 if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
6040 valid_mask |= HCRX_MSCEN | HCRX_MCE2;
6041 }
6042
6043 /* Clear RES0 bits. */
6044 env->cp15.hcrx_el2 = value & valid_mask;
6045 }
6046
6047 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
6048 bool isread)
6049 {
6050 if (arm_current_el(env) == 2
6051 && arm_feature(env, ARM_FEATURE_EL3)
6052 && !(env->cp15.scr_el3 & SCR_HXEN)) {
6053 return CP_ACCESS_TRAP_EL3;
6054 }
6055 return CP_ACCESS_OK;
6056 }
6057
6058 static const ARMCPRegInfo hcrx_el2_reginfo = {
6059 .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
6060 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
6061 .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
6062 .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
6063 };
6064
6065 /* Return the effective value of HCRX_EL2. */
6066 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
6067 {
6068 /*
6069 * The bits in this register behave as 0 for all purposes other than
6070 * direct reads of the register if SCR_EL3.HXEn is 0.
6071 * If EL2 is not enabled in the current security state, then the
6072 * bit may behave as if 0, or as if 1, depending on the bit.
6073 * For the moment, we treat the EL2-disabled case as taking
6074 * priority over the HXEn-disabled case. This is true for the only
6075 * bit for a feature which we implement where the answer is different
6076 * for the two cases (MSCEn for FEAT_MOPS).
6077 * This may need to be revisited for future bits.
6078 */
6079 if (!arm_is_el2_enabled(env)) {
6080 uint64_t hcrx = 0;
6081 if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
6082 /* MSCEn behaves as 1 if EL2 is not enabled */
6083 hcrx |= HCRX_MSCEN;
6084 }
6085 return hcrx;
6086 }
6087 if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
6088 return 0;
6089 }
6090 return env->cp15.hcrx_el2;
6091 }
6092
6093 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
6094 uint64_t value)
6095 {
6096 /*
6097 * For A-profile AArch32 EL3, if NSACR.CP10
6098 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6099 */
6100 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6101 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6102 uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6103 value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
6104 }
6105 env->cp15.cptr_el[2] = value;
6106 }
6107
6108 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
6109 {
6110 /*
6111 * For A-profile AArch32 EL3, if NSACR.CP10
6112 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6113 */
6114 uint64_t value = env->cp15.cptr_el[2];
6115
6116 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6117 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6118 value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6119 }
6120 return value;
6121 }
6122
6123 static const ARMCPRegInfo el2_cp_reginfo[] = {
6124 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
6125 .type = ARM_CP_IO,
6126 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6127 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6128 .writefn = hcr_write, .raw_writefn = raw_write },
6129 { .name = "HCR", .state = ARM_CP_STATE_AA32,
6130 .type = ARM_CP_ALIAS | ARM_CP_IO,
6131 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6132 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6133 .writefn = hcr_writelow },
6134 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
6135 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
6136 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6137 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
6138 .type = ARM_CP_ALIAS,
6139 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
6140 .access = PL2_RW,
6141 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
6142 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
6143 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
6144 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
6145 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
6146 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
6147 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
6148 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
6149 .type = ARM_CP_ALIAS,
6150 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
6151 .access = PL2_RW,
6152 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
6153 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
6154 .type = ARM_CP_ALIAS,
6155 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
6156 .access = PL2_RW,
6157 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
6158 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
6159 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
6160 .access = PL2_RW, .writefn = vbar_write,
6161 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
6162 .resetvalue = 0 },
6163 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
6164 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
6165 .access = PL3_RW, .type = ARM_CP_ALIAS,
6166 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
6167 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
6168 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
6169 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
6170 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
6171 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
6172 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
6173 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
6174 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
6175 .resetvalue = 0 },
6176 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
6177 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
6178 .access = PL2_RW, .type = ARM_CP_ALIAS,
6179 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
6180 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
6181 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
6182 .access = PL2_RW, .type = ARM_CP_CONST,
6183 .resetvalue = 0 },
6184 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
6185 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
6186 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
6187 .access = PL2_RW, .type = ARM_CP_CONST,
6188 .resetvalue = 0 },
6189 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
6190 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
6191 .access = PL2_RW, .type = ARM_CP_CONST,
6192 .resetvalue = 0 },
6193 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
6194 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
6195 .access = PL2_RW, .type = ARM_CP_CONST,
6196 .resetvalue = 0 },
6197 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
6198 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6199 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
6200 .raw_writefn = raw_write,
6201 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
6202 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
6203 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6204 .type = ARM_CP_ALIAS,
6205 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6206 .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
6207 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
6208 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6209 .access = PL2_RW,
6210 /* no .writefn needed as this can't cause an ASID change */
6211 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
6212 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
6213 .cp = 15, .opc1 = 6, .crm = 2,
6214 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6215 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6216 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
6217 .writefn = vttbr_write, .raw_writefn = raw_write },
6218 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
6219 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
6220 .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
6221 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
6222 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
6223 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
6224 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
6225 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
6226 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6227 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
6228 .access = PL2_RW, .resetvalue = 0,
6229 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
6230 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
6231 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
6232 .access = PL2_RW, .resetvalue = 0,
6233 .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
6234 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6235 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
6236 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6237 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6238 { .name = "TLBIALLNSNH",
6239 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
6240 .type = ARM_CP_NO_RAW, .access = PL2_W,
6241 .writefn = tlbiall_nsnh_write },
6242 { .name = "TLBIALLNSNHIS",
6243 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
6244 .type = ARM_CP_NO_RAW, .access = PL2_W,
6245 .writefn = tlbiall_nsnh_is_write },
6246 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6247 .type = ARM_CP_NO_RAW, .access = PL2_W,
6248 .writefn = tlbiall_hyp_write },
6249 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6250 .type = ARM_CP_NO_RAW, .access = PL2_W,
6251 .writefn = tlbiall_hyp_is_write },
6252 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6253 .type = ARM_CP_NO_RAW, .access = PL2_W,
6254 .writefn = tlbimva_hyp_write },
6255 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6256 .type = ARM_CP_NO_RAW, .access = PL2_W,
6257 .writefn = tlbimva_hyp_is_write },
6258 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
6259 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6260 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6261 .writefn = tlbi_aa64_alle2_write },
6262 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
6263 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6264 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6265 .writefn = tlbi_aa64_vae2_write },
6266 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
6267 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
6268 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6269 .writefn = tlbi_aa64_vae2_write },
6270 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
6271 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6272 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6273 .writefn = tlbi_aa64_alle2is_write },
6274 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
6275 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6276 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6277 .writefn = tlbi_aa64_vae2is_write },
6278 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
6279 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
6280 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6281 .writefn = tlbi_aa64_vae2is_write },
6282 #ifndef CONFIG_USER_ONLY
6283 /*
6284 * Unlike the other EL2-related AT operations, these must
6285 * UNDEF from EL3 if EL2 is not implemented, which is why we
6286 * define them here rather than with the rest of the AT ops.
6287 */
6288 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
6289 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6290 .access = PL2_W, .accessfn = at_s1e2_access,
6291 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6292 .writefn = ats_write64 },
6293 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
6294 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6295 .access = PL2_W, .accessfn = at_s1e2_access,
6296 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6297 .writefn = ats_write64 },
6298 /*
6299 * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
6300 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
6301 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
6302 * to behave as if SCR.NS was 1.
6303 */
6304 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6305 .access = PL2_W,
6306 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6307 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6308 .access = PL2_W,
6309 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6310 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6311 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
6312 /*
6313 * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
6314 * reset values as IMPDEF. We choose to reset to 3 to comply with
6315 * both ARMv7 and ARMv8.
6316 */
6317 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
6318 .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
6319 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
6320 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6321 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6322 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6323 .writefn = gt_cntvoff_write,
6324 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6325 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6326 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6327 .writefn = gt_cntvoff_write,
6328 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6329 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6330 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6331 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6332 .type = ARM_CP_IO, .access = PL2_RW,
6333 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6334 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6335 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6336 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6337 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6338 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6339 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
6340 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6341 .resetfn = gt_hyp_timer_reset,
6342 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6343 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6344 .type = ARM_CP_IO,
6345 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6346 .access = PL2_RW,
6347 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6348 .resetvalue = 0,
6349 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
6350 #endif
6351 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6352 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6353 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6354 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6355 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6356 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6357 .access = PL2_RW,
6358 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6359 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6360 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6361 .access = PL2_RW,
6362 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
6363 };
6364
6365 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6366 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
6367 .type = ARM_CP_ALIAS | ARM_CP_IO,
6368 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6369 .access = PL2_RW,
6370 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6371 .writefn = hcr_writehigh },
6372 };
6373
6374 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6375 bool isread)
6376 {
6377 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6378 return CP_ACCESS_OK;
6379 }
6380 return CP_ACCESS_TRAP_UNCATEGORIZED;
6381 }
6382
6383 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6384 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6385 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6386 .access = PL2_RW, .accessfn = sel2_access,
6387 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6388 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6389 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6390 .access = PL2_RW, .accessfn = sel2_access,
6391 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
6392 };
6393
6394 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6395 bool isread)
6396 {
6397 /*
6398 * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
6399 * At Secure EL1 it traps to EL3 or EL2.
6400 */
6401 if (arm_current_el(env) == 3) {
6402 return CP_ACCESS_OK;
6403 }
6404 if (arm_is_secure_below_el3(env)) {
6405 if (env->cp15.scr_el3 & SCR_EEL2) {
6406 return CP_ACCESS_TRAP_EL2;
6407 }
6408 return CP_ACCESS_TRAP_EL3;
6409 }
6410 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6411 if (isread) {
6412 return CP_ACCESS_OK;
6413 }
6414 return CP_ACCESS_TRAP_UNCATEGORIZED;
6415 }
6416
6417 static const ARMCPRegInfo el3_cp_reginfo[] = {
6418 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6419 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6420 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
6421 .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
6422 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
6423 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
6424 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6425 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
6426 .writefn = scr_write, .raw_writefn = raw_write },
6427 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6428 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6429 .access = PL3_RW, .resetvalue = 0,
6430 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6431 { .name = "SDER",
6432 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6433 .access = PL3_RW, .resetvalue = 0,
6434 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
6435 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
6436 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6437 .writefn = vbar_write, .resetvalue = 0,
6438 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
6439 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6440 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
6441 .access = PL3_RW, .resetvalue = 0,
6442 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
6443 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6444 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6445 .access = PL3_RW,
6446 /* no .writefn needed as this can't cause an ASID change */
6447 .resetvalue = 0,
6448 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
6449 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
6450 .type = ARM_CP_ALIAS,
6451 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6452 .access = PL3_RW,
6453 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
6454 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
6455 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6456 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
6457 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6458 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6459 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
6460 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
6461 .type = ARM_CP_ALIAS,
6462 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
6463 .access = PL3_RW,
6464 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
6465 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6466 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6467 .access = PL3_RW, .writefn = vbar_write,
6468 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6469 .resetvalue = 0 },
6470 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6471 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6472 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6473 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
6474 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6475 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6476 .access = PL3_RW, .resetvalue = 0,
6477 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
6478 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6479 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6480 .access = PL3_RW, .type = ARM_CP_CONST,
6481 .resetvalue = 0 },
6482 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6483 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6484 .access = PL3_RW, .type = ARM_CP_CONST,
6485 .resetvalue = 0 },
6486 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6487 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6488 .access = PL3_RW, .type = ARM_CP_CONST,
6489 .resetvalue = 0 },
6490 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6491 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6492 .access = PL3_W, .type = ARM_CP_NO_RAW,
6493 .writefn = tlbi_aa64_alle3is_write },
6494 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6495 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6496 .access = PL3_W, .type = ARM_CP_NO_RAW,
6497 .writefn = tlbi_aa64_vae3is_write },
6498 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6499 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6500 .access = PL3_W, .type = ARM_CP_NO_RAW,
6501 .writefn = tlbi_aa64_vae3is_write },
6502 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6503 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6504 .access = PL3_W, .type = ARM_CP_NO_RAW,
6505 .writefn = tlbi_aa64_alle3_write },
6506 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6507 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6508 .access = PL3_W, .type = ARM_CP_NO_RAW,
6509 .writefn = tlbi_aa64_vae3_write },
6510 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6511 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6512 .access = PL3_W, .type = ARM_CP_NO_RAW,
6513 .writefn = tlbi_aa64_vae3_write },
6514 };
6515
6516 #ifndef CONFIG_USER_ONLY
6517 /* Test if system register redirection is to occur in the current state. */
6518 static bool redirect_for_e2h(CPUARMState *env)
6519 {
6520 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6521 }
6522
6523 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6524 {
6525 CPReadFn *readfn;
6526
6527 if (redirect_for_e2h(env)) {
6528 /* Switch to the saved EL2 version of the register. */
6529 ri = ri->opaque;
6530 readfn = ri->readfn;
6531 } else {
6532 readfn = ri->orig_readfn;
6533 }
6534 if (readfn == NULL) {
6535 readfn = raw_read;
6536 }
6537 return readfn(env, ri);
6538 }
6539
6540 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6541 uint64_t value)
6542 {
6543 CPWriteFn *writefn;
6544
6545 if (redirect_for_e2h(env)) {
6546 /* Switch to the saved EL2 version of the register. */
6547 ri = ri->opaque;
6548 writefn = ri->writefn;
6549 } else {
6550 writefn = ri->orig_writefn;
6551 }
6552 if (writefn == NULL) {
6553 writefn = raw_write;
6554 }
6555 writefn(env, ri, value);
6556 }
6557
6558 static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri)
6559 {
6560 /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6561 return ri->orig_readfn(env, ri->opaque);
6562 }
6563
6564 static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri,
6565 uint64_t value)
6566 {
6567 /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6568 return ri->orig_writefn(env, ri->opaque, value);
6569 }
6570
6571 static CPAccessResult el2_e2h_e12_access(CPUARMState *env,
6572 const ARMCPRegInfo *ri,
6573 bool isread)
6574 {
6575 if (arm_current_el(env) == 1) {
6576 /*
6577 * This must be a FEAT_NV access (will either trap or redirect
6578 * to memory). None of the registers with _EL12 aliases want to
6579 * apply their trap controls for this kind of access, so don't
6580 * call the orig_accessfn or do the "UNDEF when E2H is 0" check.
6581 */
6582 return CP_ACCESS_OK;
6583 }
6584 /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */
6585 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6586 return CP_ACCESS_TRAP_UNCATEGORIZED;
6587 }
6588 if (ri->orig_accessfn) {
6589 return ri->orig_accessfn(env, ri->opaque, isread);
6590 }
6591 return CP_ACCESS_OK;
6592 }
6593
6594 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6595 {
6596 struct E2HAlias {
6597 uint32_t src_key, dst_key, new_key;
6598 const char *src_name, *dst_name, *new_name;
6599 bool (*feature)(const ARMISARegisters *id);
6600 };
6601
6602 #define K(op0, op1, crn, crm, op2) \
6603 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6604
6605 static const struct E2HAlias aliases[] = {
6606 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
6607 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6608 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
6609 "CPACR", "CPTR_EL2", "CPACR_EL12" },
6610 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
6611 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6612 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
6613 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6614 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
6615 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6616 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
6617 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6618 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
6619 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6620 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
6621 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6622 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
6623 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6624 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
6625 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6626 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
6627 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6628 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6629 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6630 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6631 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6632 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6633 "VBAR", "VBAR_EL2", "VBAR_EL12" },
6634 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6635 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6636 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6637 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6638
6639 /*
6640 * Note that redirection of ZCR is mentioned in the description
6641 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6642 * not in the summary table.
6643 */
6644 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
6645 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6646 { K(3, 0, 1, 2, 6), K(3, 4, 1, 2, 6), K(3, 5, 1, 2, 6),
6647 "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6648
6649 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
6650 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6651
6652 { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6653 "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6654 isar_feature_aa64_scxtnum },
6655
6656 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6657 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6658 };
6659 #undef K
6660
6661 size_t i;
6662
6663 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6664 const struct E2HAlias *a = &aliases[i];
6665 ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6666 bool ok;
6667
6668 if (a->feature && !a->feature(&cpu->isar)) {
6669 continue;
6670 }
6671
6672 src_reg = g_hash_table_lookup(cpu->cp_regs,
6673 (gpointer)(uintptr_t)a->src_key);
6674 dst_reg = g_hash_table_lookup(cpu->cp_regs,
6675 (gpointer)(uintptr_t)a->dst_key);
6676 g_assert(src_reg != NULL);
6677 g_assert(dst_reg != NULL);
6678
6679 /* Cross-compare names to detect typos in the keys. */
6680 g_assert(strcmp(src_reg->name, a->src_name) == 0);
6681 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6682
6683 /* None of the core system registers use opaque; we will. */
6684 g_assert(src_reg->opaque == NULL);
6685
6686 /* Create alias before redirection so we dup the right data. */
6687 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6688
6689 new_reg->name = a->new_name;
6690 new_reg->type |= ARM_CP_ALIAS;
6691 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
6692 new_reg->access &= PL2_RW | PL3_RW;
6693 /* The new_reg op fields are as per new_key, not the target reg */
6694 new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK)
6695 >> CP_REG_ARM64_SYSREG_CRN_SHIFT;
6696 new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK)
6697 >> CP_REG_ARM64_SYSREG_CRM_SHIFT;
6698 new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK)
6699 >> CP_REG_ARM64_SYSREG_OP0_SHIFT;
6700 new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK)
6701 >> CP_REG_ARM64_SYSREG_OP1_SHIFT;
6702 new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK)
6703 >> CP_REG_ARM64_SYSREG_OP2_SHIFT;
6704 new_reg->opaque = src_reg;
6705 new_reg->orig_readfn = src_reg->readfn ?: raw_read;
6706 new_reg->orig_writefn = src_reg->writefn ?: raw_write;
6707 new_reg->orig_accessfn = src_reg->accessfn;
6708 if (!new_reg->raw_readfn) {
6709 new_reg->raw_readfn = raw_read;
6710 }
6711 if (!new_reg->raw_writefn) {
6712 new_reg->raw_writefn = raw_write;
6713 }
6714 new_reg->readfn = el2_e2h_e12_read;
6715 new_reg->writefn = el2_e2h_e12_write;
6716 new_reg->accessfn = el2_e2h_e12_access;
6717
6718 ok = g_hash_table_insert(cpu->cp_regs,
6719 (gpointer)(uintptr_t)a->new_key, new_reg);
6720 g_assert(ok);
6721
6722 src_reg->opaque = dst_reg;
6723 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6724 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6725 if (!src_reg->raw_readfn) {
6726 src_reg->raw_readfn = raw_read;
6727 }
6728 if (!src_reg->raw_writefn) {
6729 src_reg->raw_writefn = raw_write;
6730 }
6731 src_reg->readfn = el2_e2h_read;
6732 src_reg->writefn = el2_e2h_write;
6733 }
6734 }
6735 #endif
6736
6737 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6738 bool isread)
6739 {
6740 int cur_el = arm_current_el(env);
6741
6742 if (cur_el < 2) {
6743 uint64_t hcr = arm_hcr_el2_eff(env);
6744
6745 if (cur_el == 0) {
6746 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6747 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6748 return CP_ACCESS_TRAP_EL2;
6749 }
6750 } else {
6751 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6752 return CP_ACCESS_TRAP;
6753 }
6754 if (hcr & HCR_TID2) {
6755 return CP_ACCESS_TRAP_EL2;
6756 }
6757 }
6758 } else if (hcr & HCR_TID2) {
6759 return CP_ACCESS_TRAP_EL2;
6760 }
6761 }
6762
6763 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6764 return CP_ACCESS_TRAP_EL2;
6765 }
6766
6767 return CP_ACCESS_OK;
6768 }
6769
6770 /*
6771 * Check for traps to RAS registers, which are controlled
6772 * by HCR_EL2.TERR and SCR_EL3.TERR.
6773 */
6774 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6775 bool isread)
6776 {
6777 int el = arm_current_el(env);
6778
6779 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6780 return CP_ACCESS_TRAP_EL2;
6781 }
6782 if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6783 return CP_ACCESS_TRAP_EL3;
6784 }
6785 return CP_ACCESS_OK;
6786 }
6787
6788 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6789 {
6790 int el = arm_current_el(env);
6791
6792 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6793 return env->cp15.vdisr_el2;
6794 }
6795 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6796 return 0; /* RAZ/WI */
6797 }
6798 return env->cp15.disr_el1;
6799 }
6800
6801 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6802 {
6803 int el = arm_current_el(env);
6804
6805 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6806 env->cp15.vdisr_el2 = val;
6807 return;
6808 }
6809 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6810 return; /* RAZ/WI */
6811 }
6812 env->cp15.disr_el1 = val;
6813 }
6814
6815 /*
6816 * Minimal RAS implementation with no Error Records.
6817 * Which means that all of the Error Record registers:
6818 * ERXADDR_EL1
6819 * ERXCTLR_EL1
6820 * ERXFR_EL1
6821 * ERXMISC0_EL1
6822 * ERXMISC1_EL1
6823 * ERXMISC2_EL1
6824 * ERXMISC3_EL1
6825 * ERXPFGCDN_EL1 (RASv1p1)
6826 * ERXPFGCTL_EL1 (RASv1p1)
6827 * ERXPFGF_EL1 (RASv1p1)
6828 * ERXSTATUS_EL1
6829 * and
6830 * ERRSELR_EL1
6831 * may generate UNDEFINED, which is the effect we get by not
6832 * listing them at all.
6833 *
6834 * These registers have fine-grained trap bits, but UNDEF-to-EL1
6835 * is higher priority than FGT-to-EL2 so we do not need to list them
6836 * in order to check for an FGT.
6837 */
6838 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6839 { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6840 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6841 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6842 .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6843 { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6844 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6845 .access = PL1_R, .accessfn = access_terr,
6846 .fgt = FGT_ERRIDR_EL1,
6847 .type = ARM_CP_CONST, .resetvalue = 0 },
6848 { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6849 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6850 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6851 { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6852 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6853 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6854 };
6855
6856 /*
6857 * Return the exception level to which exceptions should be taken
6858 * via SVEAccessTrap. This excludes the check for whether the exception
6859 * should be routed through AArch64.AdvSIMDFPAccessTrap. That can easily
6860 * be found by testing 0 < fp_exception_el < sve_exception_el.
6861 *
6862 * C.f. the ARM pseudocode function CheckSVEEnabled. Note that the
6863 * pseudocode does *not* separate out the FP trap checks, but has them
6864 * all in one function.
6865 */
6866 int sve_exception_el(CPUARMState *env, int el)
6867 {
6868 #ifndef CONFIG_USER_ONLY
6869 if (el <= 1 && !el_is_in_host(env, el)) {
6870 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6871 case 1:
6872 if (el != 0) {
6873 break;
6874 }
6875 /* fall through */
6876 case 0:
6877 case 2:
6878 return 1;
6879 }
6880 }
6881
6882 if (el <= 2 && arm_is_el2_enabled(env)) {
6883 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6884 if (env->cp15.hcr_el2 & HCR_E2H) {
6885 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6886 case 1:
6887 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6888 break;
6889 }
6890 /* fall through */
6891 case 0:
6892 case 2:
6893 return 2;
6894 }
6895 } else {
6896 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6897 return 2;
6898 }
6899 }
6900 }
6901
6902 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6903 if (arm_feature(env, ARM_FEATURE_EL3)
6904 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6905 return 3;
6906 }
6907 #endif
6908 return 0;
6909 }
6910
6911 /*
6912 * Return the exception level to which exceptions should be taken for SME.
6913 * C.f. the ARM pseudocode function CheckSMEAccess.
6914 */
6915 int sme_exception_el(CPUARMState *env, int el)
6916 {
6917 #ifndef CONFIG_USER_ONLY
6918 if (el <= 1 && !el_is_in_host(env, el)) {
6919 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6920 case 1:
6921 if (el != 0) {
6922 break;
6923 }
6924 /* fall through */
6925 case 0:
6926 case 2:
6927 return 1;
6928 }
6929 }
6930
6931 if (el <= 2 && arm_is_el2_enabled(env)) {
6932 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6933 if (env->cp15.hcr_el2 & HCR_E2H) {
6934 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6935 case 1:
6936 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6937 break;
6938 }
6939 /* fall through */
6940 case 0:
6941 case 2:
6942 return 2;
6943 }
6944 } else {
6945 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6946 return 2;
6947 }
6948 }
6949 }
6950
6951 /* CPTR_EL3. Since ESM is negative we must check for EL3. */
6952 if (arm_feature(env, ARM_FEATURE_EL3)
6953 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6954 return 3;
6955 }
6956 #endif
6957 return 0;
6958 }
6959
6960 /*
6961 * Given that SVE is enabled, return the vector length for EL.
6962 */
6963 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6964 {
6965 ARMCPU *cpu = env_archcpu(env);
6966 uint64_t *cr = env->vfp.zcr_el;
6967 uint32_t map = cpu->sve_vq.map;
6968 uint32_t len = ARM_MAX_VQ - 1;
6969
6970 if (sm) {
6971 cr = env->vfp.smcr_el;
6972 map = cpu->sme_vq.map;
6973 }
6974
6975 if (el <= 1 && !el_is_in_host(env, el)) {
6976 len = MIN(len, 0xf & (uint32_t)cr[1]);
6977 }
6978 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6979 len = MIN(len, 0xf & (uint32_t)cr[2]);
6980 }
6981 if (arm_feature(env, ARM_FEATURE_EL3)) {
6982 len = MIN(len, 0xf & (uint32_t)cr[3]);
6983 }
6984
6985 map &= MAKE_64BIT_MASK(0, len + 1);
6986 if (map != 0) {
6987 return 31 - clz32(map);
6988 }
6989
6990 /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6991 assert(sm);
6992 return ctz32(cpu->sme_vq.map);
6993 }
6994
6995 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6996 {
6997 return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6998 }
6999
7000 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7001 uint64_t value)
7002 {
7003 int cur_el = arm_current_el(env);
7004 int old_len = sve_vqm1_for_el(env, cur_el);
7005 int new_len;
7006
7007 /* Bits other than [3:0] are RAZ/WI. */
7008 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
7009 raw_write(env, ri, value & 0xf);
7010
7011 /*
7012 * Because we arrived here, we know both FP and SVE are enabled;
7013 * otherwise we would have trapped access to the ZCR_ELn register.
7014 */
7015 new_len = sve_vqm1_for_el(env, cur_el);
7016 if (new_len < old_len) {
7017 aarch64_sve_narrow_vq(env, new_len + 1);
7018 }
7019 }
7020
7021 static const ARMCPRegInfo zcr_reginfo[] = {
7022 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
7023 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
7024 .access = PL1_RW, .type = ARM_CP_SVE,
7025 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
7026 .writefn = zcr_write, .raw_writefn = raw_write },
7027 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
7028 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
7029 .access = PL2_RW, .type = ARM_CP_SVE,
7030 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
7031 .writefn = zcr_write, .raw_writefn = raw_write },
7032 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
7033 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
7034 .access = PL3_RW, .type = ARM_CP_SVE,
7035 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
7036 .writefn = zcr_write, .raw_writefn = raw_write },
7037 };
7038
7039 #ifdef TARGET_AARCH64
7040 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
7041 bool isread)
7042 {
7043 int el = arm_current_el(env);
7044
7045 if (el == 0) {
7046 uint64_t sctlr = arm_sctlr(env, el);
7047 if (!(sctlr & SCTLR_EnTP2)) {
7048 return CP_ACCESS_TRAP;
7049 }
7050 }
7051 /* TODO: FEAT_FGT */
7052 if (el < 3
7053 && arm_feature(env, ARM_FEATURE_EL3)
7054 && !(env->cp15.scr_el3 & SCR_ENTP2)) {
7055 return CP_ACCESS_TRAP_EL3;
7056 }
7057 return CP_ACCESS_OK;
7058 }
7059
7060 static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri,
7061 bool isread)
7062 {
7063 /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */
7064 if (arm_current_el(env) == 2
7065 && arm_feature(env, ARM_FEATURE_EL3)
7066 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7067 return CP_ACCESS_TRAP_EL3;
7068 }
7069 return CP_ACCESS_OK;
7070 }
7071
7072 static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri,
7073 bool isread)
7074 {
7075 if (arm_current_el(env) < 3
7076 && arm_feature(env, ARM_FEATURE_EL3)
7077 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7078 return CP_ACCESS_TRAP_EL3;
7079 }
7080 return CP_ACCESS_OK;
7081 }
7082
7083 /* ResetSVEState */
7084 static void arm_reset_sve_state(CPUARMState *env)
7085 {
7086 memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
7087 /* Recall that FFR is stored as pregs[16]. */
7088 memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
7089 vfp_set_fpcr(env, 0x0800009f);
7090 }
7091
7092 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
7093 {
7094 uint64_t change = (env->svcr ^ new) & mask;
7095
7096 if (change == 0) {
7097 return;
7098 }
7099 env->svcr ^= change;
7100
7101 if (change & R_SVCR_SM_MASK) {
7102 arm_reset_sve_state(env);
7103 }
7104
7105 /*
7106 * ResetSMEState.
7107 *
7108 * SetPSTATE_ZA zeros on enable and disable. We can zero this only
7109 * on enable: while disabled, the storage is inaccessible and the
7110 * value does not matter. We're not saving the storage in vmstate
7111 * when disabled either.
7112 */
7113 if (change & new & R_SVCR_ZA_MASK) {
7114 memset(env->zarray, 0, sizeof(env->zarray));
7115 }
7116
7117 if (tcg_enabled()) {
7118 arm_rebuild_hflags(env);
7119 }
7120 }
7121
7122 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7123 uint64_t value)
7124 {
7125 aarch64_set_svcr(env, value, -1);
7126 }
7127
7128 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7129 uint64_t value)
7130 {
7131 int cur_el = arm_current_el(env);
7132 int old_len = sve_vqm1_for_el(env, cur_el);
7133 int new_len;
7134
7135 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
7136 value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
7137 raw_write(env, ri, value);
7138
7139 /*
7140 * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
7141 * when SVL is widened (old values kept, or zeros). Choose to keep the
7142 * current values for simplicity. But for QEMU internals, we must still
7143 * apply the narrower SVL to the Zregs and Pregs -- see the comment
7144 * above aarch64_sve_narrow_vq.
7145 */
7146 new_len = sve_vqm1_for_el(env, cur_el);
7147 if (new_len < old_len) {
7148 aarch64_sve_narrow_vq(env, new_len + 1);
7149 }
7150 }
7151
7152 static const ARMCPRegInfo sme_reginfo[] = {
7153 { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
7154 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
7155 .access = PL0_RW, .accessfn = access_tpidr2,
7156 .fgt = FGT_NTPIDR2_EL0,
7157 .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
7158 { .name = "SVCR", .state = ARM_CP_STATE_AA64,
7159 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
7160 .access = PL0_RW, .type = ARM_CP_SME,
7161 .fieldoffset = offsetof(CPUARMState, svcr),
7162 .writefn = svcr_write, .raw_writefn = raw_write },
7163 { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
7164 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
7165 .access = PL1_RW, .type = ARM_CP_SME,
7166 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
7167 .writefn = smcr_write, .raw_writefn = raw_write },
7168 { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
7169 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
7170 .access = PL2_RW, .type = ARM_CP_SME,
7171 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
7172 .writefn = smcr_write, .raw_writefn = raw_write },
7173 { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
7174 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
7175 .access = PL3_RW, .type = ARM_CP_SME,
7176 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
7177 .writefn = smcr_write, .raw_writefn = raw_write },
7178 { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
7179 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
7180 .access = PL1_R, .accessfn = access_aa64_tid1,
7181 /*
7182 * IMPLEMENTOR = 0 (software)
7183 * REVISION = 0 (implementation defined)
7184 * SMPS = 0 (no streaming execution priority in QEMU)
7185 * AFFINITY = 0 (streaming sve mode not shared with other PEs)
7186 */
7187 .type = ARM_CP_CONST, .resetvalue = 0, },
7188 /*
7189 * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
7190 */
7191 { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
7192 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
7193 .access = PL1_RW, .accessfn = access_smpri,
7194 .fgt = FGT_NSMPRI_EL1,
7195 .type = ARM_CP_CONST, .resetvalue = 0 },
7196 { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
7197 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
7198 .access = PL2_RW, .accessfn = access_smprimap,
7199 .type = ARM_CP_CONST, .resetvalue = 0 },
7200 };
7201
7202 static void tlbi_aa64_paall_write(CPUARMState *env, const ARMCPRegInfo *ri,
7203 uint64_t value)
7204 {
7205 CPUState *cs = env_cpu(env);
7206
7207 tlb_flush(cs);
7208 }
7209
7210 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7211 uint64_t value)
7212 {
7213 /* L0GPTSZ is RO; other bits not mentioned are RES0. */
7214 uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
7215 R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
7216 R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
7217
7218 env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
7219 }
7220
7221 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
7222 {
7223 env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
7224 env_archcpu(env)->reset_l0gptsz);
7225 }
7226
7227 static void tlbi_aa64_paallos_write(CPUARMState *env, const ARMCPRegInfo *ri,
7228 uint64_t value)
7229 {
7230 CPUState *cs = env_cpu(env);
7231
7232 tlb_flush_all_cpus_synced(cs);
7233 }
7234
7235 static const ARMCPRegInfo rme_reginfo[] = {
7236 { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
7237 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
7238 .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
7239 .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
7240 { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
7241 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
7242 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
7243 { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
7244 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
7245 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
7246 { .name = "TLBI_PAALL", .state = ARM_CP_STATE_AA64,
7247 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 4,
7248 .access = PL3_W, .type = ARM_CP_NO_RAW,
7249 .writefn = tlbi_aa64_paall_write },
7250 { .name = "TLBI_PAALLOS", .state = ARM_CP_STATE_AA64,
7251 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 4,
7252 .access = PL3_W, .type = ARM_CP_NO_RAW,
7253 .writefn = tlbi_aa64_paallos_write },
7254 /*
7255 * QEMU does not have a way to invalidate by physical address, thus
7256 * invalidating a range of physical addresses is accomplished by
7257 * flushing all tlb entries in the outer shareable domain,
7258 * just like PAALLOS.
7259 */
7260 { .name = "TLBI_RPALOS", .state = ARM_CP_STATE_AA64,
7261 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 7,
7262 .access = PL3_W, .type = ARM_CP_NO_RAW,
7263 .writefn = tlbi_aa64_paallos_write },
7264 { .name = "TLBI_RPAOS", .state = ARM_CP_STATE_AA64,
7265 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 3,
7266 .access = PL3_W, .type = ARM_CP_NO_RAW,
7267 .writefn = tlbi_aa64_paallos_write },
7268 { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
7269 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
7270 .access = PL3_W, .type = ARM_CP_NOP },
7271 };
7272
7273 static const ARMCPRegInfo rme_mte_reginfo[] = {
7274 { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
7275 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
7276 .access = PL3_W, .type = ARM_CP_NOP },
7277 };
7278 #endif /* TARGET_AARCH64 */
7279
7280 static void define_pmu_regs(ARMCPU *cpu)
7281 {
7282 /*
7283 * v7 performance monitor control register: same implementor
7284 * field as main ID register, and we implement four counters in
7285 * addition to the cycle count register.
7286 */
7287 unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
7288 ARMCPRegInfo pmcr = {
7289 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
7290 .access = PL0_RW,
7291 .fgt = FGT_PMCR_EL0,
7292 .type = ARM_CP_IO | ARM_CP_ALIAS,
7293 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
7294 .accessfn = pmreg_access,
7295 .readfn = pmcr_read, .raw_readfn = raw_read,
7296 .writefn = pmcr_write, .raw_writefn = raw_write,
7297 };
7298 ARMCPRegInfo pmcr64 = {
7299 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
7300 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
7301 .access = PL0_RW, .accessfn = pmreg_access,
7302 .fgt = FGT_PMCR_EL0,
7303 .type = ARM_CP_IO,
7304 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
7305 .resetvalue = cpu->isar.reset_pmcr_el0,
7306 .readfn = pmcr_read, .raw_readfn = raw_read,
7307 .writefn = pmcr_write, .raw_writefn = raw_write,
7308 };
7309
7310 define_one_arm_cp_reg(cpu, &pmcr);
7311 define_one_arm_cp_reg(cpu, &pmcr64);
7312 for (i = 0; i < pmcrn; i++) {
7313 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
7314 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
7315 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
7316 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
7317 ARMCPRegInfo pmev_regs[] = {
7318 { .name = pmevcntr_name, .cp = 15, .crn = 14,
7319 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7320 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7321 .fgt = FGT_PMEVCNTRN_EL0,
7322 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7323 .accessfn = pmreg_access_xevcntr },
7324 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
7325 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
7326 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
7327 .type = ARM_CP_IO,
7328 .fgt = FGT_PMEVCNTRN_EL0,
7329 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7330 .raw_readfn = pmevcntr_rawread,
7331 .raw_writefn = pmevcntr_rawwrite },
7332 { .name = pmevtyper_name, .cp = 15, .crn = 14,
7333 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7334 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7335 .fgt = FGT_PMEVTYPERN_EL0,
7336 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7337 .accessfn = pmreg_access },
7338 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
7339 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
7340 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
7341 .fgt = FGT_PMEVTYPERN_EL0,
7342 .type = ARM_CP_IO,
7343 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7344 .raw_writefn = pmevtyper_rawwrite },
7345 };
7346 define_arm_cp_regs(cpu, pmev_regs);
7347 g_free(pmevcntr_name);
7348 g_free(pmevcntr_el0_name);
7349 g_free(pmevtyper_name);
7350 g_free(pmevtyper_el0_name);
7351 }
7352 if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
7353 ARMCPRegInfo v81_pmu_regs[] = {
7354 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
7355 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
7356 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7357 .fgt = FGT_PMCEIDN_EL0,
7358 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
7359 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
7360 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
7361 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7362 .fgt = FGT_PMCEIDN_EL0,
7363 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
7364 };
7365 define_arm_cp_regs(cpu, v81_pmu_regs);
7366 }
7367 if (cpu_isar_feature(any_pmuv3p4, cpu)) {
7368 static const ARMCPRegInfo v84_pmmir = {
7369 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
7370 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
7371 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7372 .fgt = FGT_PMMIR_EL1,
7373 .resetvalue = 0
7374 };
7375 define_one_arm_cp_reg(cpu, &v84_pmmir);
7376 }
7377 }
7378
7379 #ifndef CONFIG_USER_ONLY
7380 /*
7381 * We don't know until after realize whether there's a GICv3
7382 * attached, and that is what registers the gicv3 sysregs.
7383 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
7384 * at runtime.
7385 */
7386 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
7387 {
7388 ARMCPU *cpu = env_archcpu(env);
7389 uint64_t pfr1 = cpu->isar.id_pfr1;
7390
7391 if (env->gicv3state) {
7392 pfr1 |= 1 << 28;
7393 }
7394 return pfr1;
7395 }
7396
7397 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
7398 {
7399 ARMCPU *cpu = env_archcpu(env);
7400 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
7401
7402 if (env->gicv3state) {
7403 pfr0 |= 1 << 24;
7404 }
7405 return pfr0;
7406 }
7407 #endif
7408
7409 /*
7410 * Shared logic between LORID and the rest of the LOR* registers.
7411 * Secure state exclusion has already been dealt with.
7412 */
7413 static CPAccessResult access_lor_ns(CPUARMState *env,
7414 const ARMCPRegInfo *ri, bool isread)
7415 {
7416 int el = arm_current_el(env);
7417
7418 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
7419 return CP_ACCESS_TRAP_EL2;
7420 }
7421 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
7422 return CP_ACCESS_TRAP_EL3;
7423 }
7424 return CP_ACCESS_OK;
7425 }
7426
7427 static CPAccessResult access_lor_other(CPUARMState *env,
7428 const ARMCPRegInfo *ri, bool isread)
7429 {
7430 if (arm_is_secure_below_el3(env)) {
7431 /* Access denied in secure mode. */
7432 return CP_ACCESS_TRAP;
7433 }
7434 return access_lor_ns(env, ri, isread);
7435 }
7436
7437 /*
7438 * A trivial implementation of ARMv8.1-LOR leaves all of these
7439 * registers fixed at 0, which indicates that there are zero
7440 * supported Limited Ordering regions.
7441 */
7442 static const ARMCPRegInfo lor_reginfo[] = {
7443 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
7444 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
7445 .access = PL1_RW, .accessfn = access_lor_other,
7446 .fgt = FGT_LORSA_EL1,
7447 .type = ARM_CP_CONST, .resetvalue = 0 },
7448 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7449 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7450 .access = PL1_RW, .accessfn = access_lor_other,
7451 .fgt = FGT_LOREA_EL1,
7452 .type = ARM_CP_CONST, .resetvalue = 0 },
7453 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7454 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7455 .access = PL1_RW, .accessfn = access_lor_other,
7456 .fgt = FGT_LORN_EL1,
7457 .type = ARM_CP_CONST, .resetvalue = 0 },
7458 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7459 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7460 .access = PL1_RW, .accessfn = access_lor_other,
7461 .fgt = FGT_LORC_EL1,
7462 .type = ARM_CP_CONST, .resetvalue = 0 },
7463 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7464 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7465 .access = PL1_R, .accessfn = access_lor_ns,
7466 .fgt = FGT_LORID_EL1,
7467 .type = ARM_CP_CONST, .resetvalue = 0 },
7468 };
7469
7470 #ifdef TARGET_AARCH64
7471 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7472 bool isread)
7473 {
7474 int el = arm_current_el(env);
7475
7476 if (el < 2 &&
7477 arm_is_el2_enabled(env) &&
7478 !(arm_hcr_el2_eff(env) & HCR_APK)) {
7479 return CP_ACCESS_TRAP_EL2;
7480 }
7481 if (el < 3 &&
7482 arm_feature(env, ARM_FEATURE_EL3) &&
7483 !(env->cp15.scr_el3 & SCR_APK)) {
7484 return CP_ACCESS_TRAP_EL3;
7485 }
7486 return CP_ACCESS_OK;
7487 }
7488
7489 static const ARMCPRegInfo pauth_reginfo[] = {
7490 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7491 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7492 .access = PL1_RW, .accessfn = access_pauth,
7493 .fgt = FGT_APDAKEY,
7494 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7495 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7496 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7497 .access = PL1_RW, .accessfn = access_pauth,
7498 .fgt = FGT_APDAKEY,
7499 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7500 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7501 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7502 .access = PL1_RW, .accessfn = access_pauth,
7503 .fgt = FGT_APDBKEY,
7504 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7505 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7506 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7507 .access = PL1_RW, .accessfn = access_pauth,
7508 .fgt = FGT_APDBKEY,
7509 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7510 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7511 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7512 .access = PL1_RW, .accessfn = access_pauth,
7513 .fgt = FGT_APGAKEY,
7514 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7515 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7516 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7517 .access = PL1_RW, .accessfn = access_pauth,
7518 .fgt = FGT_APGAKEY,
7519 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7520 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7521 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7522 .access = PL1_RW, .accessfn = access_pauth,
7523 .fgt = FGT_APIAKEY,
7524 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7525 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7526 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7527 .access = PL1_RW, .accessfn = access_pauth,
7528 .fgt = FGT_APIAKEY,
7529 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7530 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7531 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7532 .access = PL1_RW, .accessfn = access_pauth,
7533 .fgt = FGT_APIBKEY,
7534 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7535 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7536 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7537 .access = PL1_RW, .accessfn = access_pauth,
7538 .fgt = FGT_APIBKEY,
7539 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7540 };
7541
7542 static const ARMCPRegInfo tlbirange_reginfo[] = {
7543 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7544 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
7545 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7546 .fgt = FGT_TLBIRVAE1IS,
7547 .writefn = tlbi_aa64_rvae1is_write },
7548 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7549 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
7550 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7551 .fgt = FGT_TLBIRVAAE1IS,
7552 .writefn = tlbi_aa64_rvae1is_write },
7553 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7554 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
7555 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7556 .fgt = FGT_TLBIRVALE1IS,
7557 .writefn = tlbi_aa64_rvae1is_write },
7558 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7559 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
7560 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7561 .fgt = FGT_TLBIRVAALE1IS,
7562 .writefn = tlbi_aa64_rvae1is_write },
7563 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7564 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7565 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7566 .fgt = FGT_TLBIRVAE1OS,
7567 .writefn = tlbi_aa64_rvae1is_write },
7568 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7569 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
7570 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7571 .fgt = FGT_TLBIRVAAE1OS,
7572 .writefn = tlbi_aa64_rvae1is_write },
7573 { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7574 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
7575 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7576 .fgt = FGT_TLBIRVALE1OS,
7577 .writefn = tlbi_aa64_rvae1is_write },
7578 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7579 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
7580 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7581 .fgt = FGT_TLBIRVAALE1OS,
7582 .writefn = tlbi_aa64_rvae1is_write },
7583 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7584 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7585 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7586 .fgt = FGT_TLBIRVAE1,
7587 .writefn = tlbi_aa64_rvae1_write },
7588 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7589 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
7590 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7591 .fgt = FGT_TLBIRVAAE1,
7592 .writefn = tlbi_aa64_rvae1_write },
7593 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7594 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
7595 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7596 .fgt = FGT_TLBIRVALE1,
7597 .writefn = tlbi_aa64_rvae1_write },
7598 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7599 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
7600 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7601 .fgt = FGT_TLBIRVAALE1,
7602 .writefn = tlbi_aa64_rvae1_write },
7603 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7604 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
7605 .access = PL2_W, .type = ARM_CP_NO_RAW,
7606 .writefn = tlbi_aa64_ripas2e1is_write },
7607 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7608 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
7609 .access = PL2_W, .type = ARM_CP_NO_RAW,
7610 .writefn = tlbi_aa64_ripas2e1is_write },
7611 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7612 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
7613 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7614 .writefn = tlbi_aa64_rvae2is_write },
7615 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7616 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
7617 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7618 .writefn = tlbi_aa64_rvae2is_write },
7619 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7620 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
7621 .access = PL2_W, .type = ARM_CP_NO_RAW,
7622 .writefn = tlbi_aa64_ripas2e1_write },
7623 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
7624 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
7625 .access = PL2_W, .type = ARM_CP_NO_RAW,
7626 .writefn = tlbi_aa64_ripas2e1_write },
7627 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7628 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
7629 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7630 .writefn = tlbi_aa64_rvae2is_write },
7631 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7632 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
7633 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7634 .writefn = tlbi_aa64_rvae2is_write },
7635 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7636 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
7637 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7638 .writefn = tlbi_aa64_rvae2_write },
7639 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7640 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
7641 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7642 .writefn = tlbi_aa64_rvae2_write },
7643 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7644 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7645 .access = PL3_W, .type = ARM_CP_NO_RAW,
7646 .writefn = tlbi_aa64_rvae3is_write },
7647 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7648 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7649 .access = PL3_W, .type = ARM_CP_NO_RAW,
7650 .writefn = tlbi_aa64_rvae3is_write },
7651 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7652 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7653 .access = PL3_W, .type = ARM_CP_NO_RAW,
7654 .writefn = tlbi_aa64_rvae3is_write },
7655 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7656 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7657 .access = PL3_W, .type = ARM_CP_NO_RAW,
7658 .writefn = tlbi_aa64_rvae3is_write },
7659 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7660 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7661 .access = PL3_W, .type = ARM_CP_NO_RAW,
7662 .writefn = tlbi_aa64_rvae3_write },
7663 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7664 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7665 .access = PL3_W, .type = ARM_CP_NO_RAW,
7666 .writefn = tlbi_aa64_rvae3_write },
7667 };
7668
7669 static const ARMCPRegInfo tlbios_reginfo[] = {
7670 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7671 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
7672 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7673 .fgt = FGT_TLBIVMALLE1OS,
7674 .writefn = tlbi_aa64_vmalle1is_write },
7675 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7676 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
7677 .fgt = FGT_TLBIVAE1OS,
7678 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7679 .writefn = tlbi_aa64_vae1is_write },
7680 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7681 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
7682 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7683 .fgt = FGT_TLBIASIDE1OS,
7684 .writefn = tlbi_aa64_vmalle1is_write },
7685 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7686 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
7687 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7688 .fgt = FGT_TLBIVAAE1OS,
7689 .writefn = tlbi_aa64_vae1is_write },
7690 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7691 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
7692 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7693 .fgt = FGT_TLBIVALE1OS,
7694 .writefn = tlbi_aa64_vae1is_write },
7695 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7696 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
7697 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7698 .fgt = FGT_TLBIVAALE1OS,
7699 .writefn = tlbi_aa64_vae1is_write },
7700 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7701 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
7702 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7703 .writefn = tlbi_aa64_alle2is_write },
7704 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7705 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
7706 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7707 .writefn = tlbi_aa64_vae2is_write },
7708 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7709 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7710 .access = PL2_W, .type = ARM_CP_NO_RAW,
7711 .writefn = tlbi_aa64_alle1is_write },
7712 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7713 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
7714 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7715 .writefn = tlbi_aa64_vae2is_write },
7716 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7717 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7718 .access = PL2_W, .type = ARM_CP_NO_RAW,
7719 .writefn = tlbi_aa64_alle1is_write },
7720 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7721 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7722 .access = PL2_W, .type = ARM_CP_NOP },
7723 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7724 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7725 .access = PL2_W, .type = ARM_CP_NOP },
7726 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7727 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7728 .access = PL2_W, .type = ARM_CP_NOP },
7729 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7730 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7731 .access = PL2_W, .type = ARM_CP_NOP },
7732 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7733 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7734 .access = PL3_W, .type = ARM_CP_NO_RAW,
7735 .writefn = tlbi_aa64_alle3is_write },
7736 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7737 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7738 .access = PL3_W, .type = ARM_CP_NO_RAW,
7739 .writefn = tlbi_aa64_vae3is_write },
7740 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7741 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7742 .access = PL3_W, .type = ARM_CP_NO_RAW,
7743 .writefn = tlbi_aa64_vae3is_write },
7744 };
7745
7746 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7747 {
7748 Error *err = NULL;
7749 uint64_t ret;
7750
7751 /* Success sets NZCV = 0000. */
7752 env->NF = env->CF = env->VF = 0, env->ZF = 1;
7753
7754 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7755 /*
7756 * ??? Failed, for unknown reasons in the crypto subsystem.
7757 * The best we can do is log the reason and return the
7758 * timed-out indication to the guest. There is no reason
7759 * we know to expect this failure to be transitory, so the
7760 * guest may well hang retrying the operation.
7761 */
7762 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7763 ri->name, error_get_pretty(err));
7764 error_free(err);
7765
7766 env->ZF = 0; /* NZCF = 0100 */
7767 return 0;
7768 }
7769 return ret;
7770 }
7771
7772 /* We do not support re-seeding, so the two registers operate the same. */
7773 static const ARMCPRegInfo rndr_reginfo[] = {
7774 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7775 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7776 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7777 .access = PL0_R, .readfn = rndr_readfn },
7778 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7779 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7780 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7781 .access = PL0_R, .readfn = rndr_readfn },
7782 };
7783
7784 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7785 uint64_t value)
7786 {
7787 #ifdef CONFIG_TCG
7788 ARMCPU *cpu = env_archcpu(env);
7789 /* CTR_EL0 System register -> DminLine, bits [19:16] */
7790 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7791 uint64_t vaddr_in = (uint64_t) value;
7792 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7793 void *haddr;
7794 int mem_idx = cpu_mmu_index(env, false);
7795
7796 /* This won't be crossing page boundaries */
7797 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7798 if (haddr) {
7799 #ifndef CONFIG_USER_ONLY
7800
7801 ram_addr_t offset;
7802 MemoryRegion *mr;
7803
7804 /* RCU lock is already being held */
7805 mr = memory_region_from_host(haddr, &offset);
7806
7807 if (mr) {
7808 memory_region_writeback(mr, offset, dline_size);
7809 }
7810 #endif /*CONFIG_USER_ONLY*/
7811 }
7812 #else
7813 /* Handled by hardware accelerator. */
7814 g_assert_not_reached();
7815 #endif /* CONFIG_TCG */
7816 }
7817
7818 static const ARMCPRegInfo dcpop_reg[] = {
7819 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7820 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7821 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7822 .fgt = FGT_DCCVAP,
7823 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7824 };
7825
7826 static const ARMCPRegInfo dcpodp_reg[] = {
7827 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7828 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7829 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7830 .fgt = FGT_DCCVADP,
7831 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7832 };
7833
7834 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7835 bool isread)
7836 {
7837 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7838 return CP_ACCESS_TRAP_EL2;
7839 }
7840
7841 return CP_ACCESS_OK;
7842 }
7843
7844 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7845 bool isread)
7846 {
7847 int el = arm_current_el(env);
7848 if (el < 2 && arm_is_el2_enabled(env)) {
7849 uint64_t hcr = arm_hcr_el2_eff(env);
7850 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7851 return CP_ACCESS_TRAP_EL2;
7852 }
7853 }
7854 if (el < 3 &&
7855 arm_feature(env, ARM_FEATURE_EL3) &&
7856 !(env->cp15.scr_el3 & SCR_ATA)) {
7857 return CP_ACCESS_TRAP_EL3;
7858 }
7859 return CP_ACCESS_OK;
7860 }
7861
7862 static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri,
7863 bool isread)
7864 {
7865 CPAccessResult nv1 = access_nv1(env, ri, isread);
7866
7867 if (nv1 != CP_ACCESS_OK) {
7868 return nv1;
7869 }
7870 return access_mte(env, ri, isread);
7871 }
7872
7873 static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri,
7874 bool isread)
7875 {
7876 /*
7877 * TFSR_EL2: similar to generic access_mte(), but we need to
7878 * account for FEAT_NV. At EL1 this must be a FEAT_NV access;
7879 * we will trap to EL2 and the HCR/SCR traps do not apply.
7880 */
7881 int el = arm_current_el(env);
7882
7883 if (el == 1) {
7884 return CP_ACCESS_OK;
7885 }
7886 if (el < 2 && arm_is_el2_enabled(env)) {
7887 uint64_t hcr = arm_hcr_el2_eff(env);
7888 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7889 return CP_ACCESS_TRAP_EL2;
7890 }
7891 }
7892 if (el < 3 &&
7893 arm_feature(env, ARM_FEATURE_EL3) &&
7894 !(env->cp15.scr_el3 & SCR_ATA)) {
7895 return CP_ACCESS_TRAP_EL3;
7896 }
7897 return CP_ACCESS_OK;
7898 }
7899
7900 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7901 {
7902 return env->pstate & PSTATE_TCO;
7903 }
7904
7905 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7906 {
7907 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7908 }
7909
7910 static const ARMCPRegInfo mte_reginfo[] = {
7911 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7912 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7913 .access = PL1_RW, .accessfn = access_mte,
7914 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7915 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7916 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7917 .access = PL1_RW, .accessfn = access_tfsr_el1,
7918 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7919 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7920 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7921 .access = PL2_RW, .accessfn = access_tfsr_el2,
7922 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7923 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7924 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7925 .access = PL3_RW,
7926 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7927 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7928 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7929 .access = PL1_RW, .accessfn = access_mte,
7930 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7931 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7932 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7933 .access = PL1_RW, .accessfn = access_mte,
7934 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7935 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7936 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7937 .type = ARM_CP_NO_RAW,
7938 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7939 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7940 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7941 .type = ARM_CP_NOP, .access = PL1_W,
7942 .fgt = FGT_DCIVAC,
7943 .accessfn = aa64_cacheop_poc_access },
7944 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7945 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7946 .fgt = FGT_DCISW,
7947 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7948 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7949 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7950 .type = ARM_CP_NOP, .access = PL1_W,
7951 .fgt = FGT_DCIVAC,
7952 .accessfn = aa64_cacheop_poc_access },
7953 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7954 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7955 .fgt = FGT_DCISW,
7956 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7957 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7958 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7959 .fgt = FGT_DCCSW,
7960 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7961 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7962 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7963 .fgt = FGT_DCCSW,
7964 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7965 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7966 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7967 .fgt = FGT_DCCISW,
7968 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7969 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7970 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7971 .fgt = FGT_DCCISW,
7972 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7973 };
7974
7975 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7976 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7977 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7978 .type = ARM_CP_CONST, .access = PL0_RW, },
7979 };
7980
7981 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7982 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7983 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7984 .type = ARM_CP_NOP, .access = PL0_W,
7985 .fgt = FGT_DCCVAC,
7986 .accessfn = aa64_cacheop_poc_access },
7987 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7988 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7989 .type = ARM_CP_NOP, .access = PL0_W,
7990 .fgt = FGT_DCCVAC,
7991 .accessfn = aa64_cacheop_poc_access },
7992 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7993 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7994 .type = ARM_CP_NOP, .access = PL0_W,
7995 .fgt = FGT_DCCVAP,
7996 .accessfn = aa64_cacheop_poc_access },
7997 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7998 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7999 .type = ARM_CP_NOP, .access = PL0_W,
8000 .fgt = FGT_DCCVAP,
8001 .accessfn = aa64_cacheop_poc_access },
8002 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
8003 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
8004 .type = ARM_CP_NOP, .access = PL0_W,
8005 .fgt = FGT_DCCVADP,
8006 .accessfn = aa64_cacheop_poc_access },
8007 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
8008 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
8009 .type = ARM_CP_NOP, .access = PL0_W,
8010 .fgt = FGT_DCCVADP,
8011 .accessfn = aa64_cacheop_poc_access },
8012 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
8013 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
8014 .type = ARM_CP_NOP, .access = PL0_W,
8015 .fgt = FGT_DCCIVAC,
8016 .accessfn = aa64_cacheop_poc_access },
8017 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
8018 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
8019 .type = ARM_CP_NOP, .access = PL0_W,
8020 .fgt = FGT_DCCIVAC,
8021 .accessfn = aa64_cacheop_poc_access },
8022 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
8023 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
8024 .access = PL0_W, .type = ARM_CP_DC_GVA,
8025 #ifndef CONFIG_USER_ONLY
8026 /* Avoid overhead of an access check that always passes in user-mode */
8027 .accessfn = aa64_zva_access,
8028 .fgt = FGT_DCZVA,
8029 #endif
8030 },
8031 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
8032 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
8033 .access = PL0_W, .type = ARM_CP_DC_GZVA,
8034 #ifndef CONFIG_USER_ONLY
8035 /* Avoid overhead of an access check that always passes in user-mode */
8036 .accessfn = aa64_zva_access,
8037 .fgt = FGT_DCZVA,
8038 #endif
8039 },
8040 };
8041
8042 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
8043 bool isread)
8044 {
8045 uint64_t hcr = arm_hcr_el2_eff(env);
8046 int el = arm_current_el(env);
8047
8048 if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
8049 if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
8050 if (hcr & HCR_TGE) {
8051 return CP_ACCESS_TRAP_EL2;
8052 }
8053 return CP_ACCESS_TRAP;
8054 }
8055 } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
8056 return CP_ACCESS_TRAP_EL2;
8057 }
8058 if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
8059 return CP_ACCESS_TRAP_EL2;
8060 }
8061 if (el < 3
8062 && arm_feature(env, ARM_FEATURE_EL3)
8063 && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
8064 return CP_ACCESS_TRAP_EL3;
8065 }
8066 return CP_ACCESS_OK;
8067 }
8068
8069 static CPAccessResult access_scxtnum_el1(CPUARMState *env,
8070 const ARMCPRegInfo *ri,
8071 bool isread)
8072 {
8073 CPAccessResult nv1 = access_nv1(env, ri, isread);
8074
8075 if (nv1 != CP_ACCESS_OK) {
8076 return nv1;
8077 }
8078 return access_scxtnum(env, ri, isread);
8079 }
8080
8081 static const ARMCPRegInfo scxtnum_reginfo[] = {
8082 { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
8083 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
8084 .access = PL0_RW, .accessfn = access_scxtnum,
8085 .fgt = FGT_SCXTNUM_EL0,
8086 .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
8087 { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
8088 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
8089 .access = PL1_RW, .accessfn = access_scxtnum_el1,
8090 .fgt = FGT_SCXTNUM_EL1,
8091 .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
8092 { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
8093 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
8094 .access = PL2_RW, .accessfn = access_scxtnum,
8095 .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
8096 { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
8097 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
8098 .access = PL3_RW,
8099 .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
8100 };
8101
8102 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
8103 bool isread)
8104 {
8105 if (arm_current_el(env) == 2 &&
8106 arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
8107 return CP_ACCESS_TRAP_EL3;
8108 }
8109 return CP_ACCESS_OK;
8110 }
8111
8112 static const ARMCPRegInfo fgt_reginfo[] = {
8113 { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
8114 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
8115 .access = PL2_RW, .accessfn = access_fgt,
8116 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
8117 { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
8118 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
8119 .access = PL2_RW, .accessfn = access_fgt,
8120 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
8121 { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
8122 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
8123 .access = PL2_RW, .accessfn = access_fgt,
8124 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
8125 { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
8126 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
8127 .access = PL2_RW, .accessfn = access_fgt,
8128 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
8129 { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
8130 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
8131 .access = PL2_RW, .accessfn = access_fgt,
8132 .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
8133 };
8134 #endif /* TARGET_AARCH64 */
8135
8136 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
8137 bool isread)
8138 {
8139 int el = arm_current_el(env);
8140
8141 if (el == 0) {
8142 uint64_t sctlr = arm_sctlr(env, el);
8143 if (!(sctlr & SCTLR_EnRCTX)) {
8144 return CP_ACCESS_TRAP;
8145 }
8146 } else if (el == 1) {
8147 uint64_t hcr = arm_hcr_el2_eff(env);
8148 if (hcr & HCR_NV) {
8149 return CP_ACCESS_TRAP_EL2;
8150 }
8151 }
8152 return CP_ACCESS_OK;
8153 }
8154
8155 static const ARMCPRegInfo predinv_reginfo[] = {
8156 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
8157 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
8158 .fgt = FGT_CFPRCTX,
8159 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8160 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
8161 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
8162 .fgt = FGT_DVPRCTX,
8163 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8164 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
8165 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
8166 .fgt = FGT_CPPRCTX,
8167 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8168 /*
8169 * Note the AArch32 opcodes have a different OPC1.
8170 */
8171 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
8172 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
8173 .fgt = FGT_CFPRCTX,
8174 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8175 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
8176 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
8177 .fgt = FGT_DVPRCTX,
8178 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8179 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
8180 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
8181 .fgt = FGT_CPPRCTX,
8182 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8183 };
8184
8185 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
8186 {
8187 /* Read the high 32 bits of the current CCSIDR */
8188 return extract64(ccsidr_read(env, ri), 32, 32);
8189 }
8190
8191 static const ARMCPRegInfo ccsidr2_reginfo[] = {
8192 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
8193 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
8194 .access = PL1_R,
8195 .accessfn = access_tid4,
8196 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
8197 };
8198
8199 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8200 bool isread)
8201 {
8202 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
8203 return CP_ACCESS_TRAP_EL2;
8204 }
8205
8206 return CP_ACCESS_OK;
8207 }
8208
8209 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8210 bool isread)
8211 {
8212 if (arm_feature(env, ARM_FEATURE_V8)) {
8213 return access_aa64_tid3(env, ri, isread);
8214 }
8215
8216 return CP_ACCESS_OK;
8217 }
8218
8219 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
8220 bool isread)
8221 {
8222 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
8223 return CP_ACCESS_TRAP_EL2;
8224 }
8225
8226 return CP_ACCESS_OK;
8227 }
8228
8229 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
8230 const ARMCPRegInfo *ri, bool isread)
8231 {
8232 /*
8233 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
8234 * in v7A, not in v8A.
8235 */
8236 if (!arm_feature(env, ARM_FEATURE_V8) &&
8237 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
8238 (env->cp15.hstr_el2 & HSTR_TJDBX)) {
8239 return CP_ACCESS_TRAP_EL2;
8240 }
8241 return CP_ACCESS_OK;
8242 }
8243
8244 static const ARMCPRegInfo jazelle_regs[] = {
8245 { .name = "JIDR",
8246 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
8247 .access = PL1_R, .accessfn = access_jazelle,
8248 .type = ARM_CP_CONST, .resetvalue = 0 },
8249 { .name = "JOSCR",
8250 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8251 .accessfn = access_joscr_jmcr,
8252 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8253 { .name = "JMCR",
8254 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8255 .accessfn = access_joscr_jmcr,
8256 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8257 };
8258
8259 static const ARMCPRegInfo contextidr_el2 = {
8260 .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
8261 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
8262 .access = PL2_RW,
8263 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
8264 };
8265
8266 static const ARMCPRegInfo vhe_reginfo[] = {
8267 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
8268 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
8269 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
8270 .raw_writefn = raw_write,
8271 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8272 #ifndef CONFIG_USER_ONLY
8273 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
8274 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
8275 .fieldoffset =
8276 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
8277 .type = ARM_CP_IO, .access = PL2_RW,
8278 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
8279 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
8280 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
8281 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
8282 .resetfn = gt_hv_timer_reset,
8283 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
8284 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
8285 .type = ARM_CP_IO,
8286 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
8287 .access = PL2_RW,
8288 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
8289 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
8290 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
8291 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
8292 .type = ARM_CP_IO | ARM_CP_ALIAS,
8293 .access = PL2_RW, .accessfn = e2h_access,
8294 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
8295 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
8296 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
8297 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
8298 .type = ARM_CP_IO | ARM_CP_ALIAS,
8299 .access = PL2_RW, .accessfn = e2h_access,
8300 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
8301 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
8302 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8303 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
8304 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8305 .access = PL2_RW, .accessfn = e2h_access,
8306 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
8307 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8308 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
8309 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8310 .access = PL2_RW, .accessfn = e2h_access,
8311 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
8312 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8313 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
8314 .type = ARM_CP_IO | ARM_CP_ALIAS,
8315 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
8316 .access = PL2_RW, .accessfn = e2h_access,
8317 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
8318 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8319 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
8320 .type = ARM_CP_IO | ARM_CP_ALIAS,
8321 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
8322 .access = PL2_RW, .accessfn = e2h_access,
8323 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8324 #endif
8325 };
8326
8327 #ifndef CONFIG_USER_ONLY
8328 static const ARMCPRegInfo ats1e1_reginfo[] = {
8329 { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
8330 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8331 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8332 .fgt = FGT_ATS1E1RP,
8333 .accessfn = at_s1e01_access, .writefn = ats_write64 },
8334 { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
8335 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8336 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8337 .fgt = FGT_ATS1E1WP,
8338 .accessfn = at_s1e01_access, .writefn = ats_write64 },
8339 };
8340
8341 static const ARMCPRegInfo ats1cp_reginfo[] = {
8342 { .name = "ATS1CPRP",
8343 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8344 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8345 .writefn = ats_write },
8346 { .name = "ATS1CPWP",
8347 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8348 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8349 .writefn = ats_write },
8350 };
8351 #endif
8352
8353 /*
8354 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
8355 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
8356 * is non-zero, which is never for ARMv7, optionally in ARMv8
8357 * and mandatorily for ARMv8.2 and up.
8358 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
8359 * implementation is RAZ/WI we can ignore this detail, as we
8360 * do for ACTLR.
8361 */
8362 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
8363 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
8364 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
8365 .access = PL1_RW, .accessfn = access_tacr,
8366 .type = ARM_CP_CONST, .resetvalue = 0 },
8367 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
8368 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
8369 .access = PL2_RW, .type = ARM_CP_CONST,
8370 .resetvalue = 0 },
8371 };
8372
8373 void register_cp_regs_for_features(ARMCPU *cpu)
8374 {
8375 /* Register all the coprocessor registers based on feature bits */
8376 CPUARMState *env = &cpu->env;
8377 if (arm_feature(env, ARM_FEATURE_M)) {
8378 /* M profile has no coprocessor registers */
8379 return;
8380 }
8381
8382 define_arm_cp_regs(cpu, cp_reginfo);
8383 if (!arm_feature(env, ARM_FEATURE_V8)) {
8384 /*
8385 * Must go early as it is full of wildcards that may be
8386 * overridden by later definitions.
8387 */
8388 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
8389 }
8390
8391 if (arm_feature(env, ARM_FEATURE_V6)) {
8392 /* The ID registers all have impdef reset values */
8393 ARMCPRegInfo v6_idregs[] = {
8394 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
8395 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
8396 .access = PL1_R, .type = ARM_CP_CONST,
8397 .accessfn = access_aa32_tid3,
8398 .resetvalue = cpu->isar.id_pfr0 },
8399 /*
8400 * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
8401 * the value of the GIC field until after we define these regs.
8402 */
8403 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
8404 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
8405 .access = PL1_R, .type = ARM_CP_NO_RAW,
8406 .accessfn = access_aa32_tid3,
8407 #ifdef CONFIG_USER_ONLY
8408 .type = ARM_CP_CONST,
8409 .resetvalue = cpu->isar.id_pfr1,
8410 #else
8411 .type = ARM_CP_NO_RAW,
8412 .accessfn = access_aa32_tid3,
8413 .readfn = id_pfr1_read,
8414 .writefn = arm_cp_write_ignore
8415 #endif
8416 },
8417 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
8418 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
8419 .access = PL1_R, .type = ARM_CP_CONST,
8420 .accessfn = access_aa32_tid3,
8421 .resetvalue = cpu->isar.id_dfr0 },
8422 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
8423 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
8424 .access = PL1_R, .type = ARM_CP_CONST,
8425 .accessfn = access_aa32_tid3,
8426 .resetvalue = cpu->id_afr0 },
8427 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
8428 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
8429 .access = PL1_R, .type = ARM_CP_CONST,
8430 .accessfn = access_aa32_tid3,
8431 .resetvalue = cpu->isar.id_mmfr0 },
8432 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
8433 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
8434 .access = PL1_R, .type = ARM_CP_CONST,
8435 .accessfn = access_aa32_tid3,
8436 .resetvalue = cpu->isar.id_mmfr1 },
8437 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
8438 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
8439 .access = PL1_R, .type = ARM_CP_CONST,
8440 .accessfn = access_aa32_tid3,
8441 .resetvalue = cpu->isar.id_mmfr2 },
8442 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
8443 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
8444 .access = PL1_R, .type = ARM_CP_CONST,
8445 .accessfn = access_aa32_tid3,
8446 .resetvalue = cpu->isar.id_mmfr3 },
8447 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
8448 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
8449 .access = PL1_R, .type = ARM_CP_CONST,
8450 .accessfn = access_aa32_tid3,
8451 .resetvalue = cpu->isar.id_isar0 },
8452 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
8453 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
8454 .access = PL1_R, .type = ARM_CP_CONST,
8455 .accessfn = access_aa32_tid3,
8456 .resetvalue = cpu->isar.id_isar1 },
8457 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
8458 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
8459 .access = PL1_R, .type = ARM_CP_CONST,
8460 .accessfn = access_aa32_tid3,
8461 .resetvalue = cpu->isar.id_isar2 },
8462 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
8463 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
8464 .access = PL1_R, .type = ARM_CP_CONST,
8465 .accessfn = access_aa32_tid3,
8466 .resetvalue = cpu->isar.id_isar3 },
8467 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
8468 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
8469 .access = PL1_R, .type = ARM_CP_CONST,
8470 .accessfn = access_aa32_tid3,
8471 .resetvalue = cpu->isar.id_isar4 },
8472 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
8473 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
8474 .access = PL1_R, .type = ARM_CP_CONST,
8475 .accessfn = access_aa32_tid3,
8476 .resetvalue = cpu->isar.id_isar5 },
8477 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
8478 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
8479 .access = PL1_R, .type = ARM_CP_CONST,
8480 .accessfn = access_aa32_tid3,
8481 .resetvalue = cpu->isar.id_mmfr4 },
8482 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
8483 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
8484 .access = PL1_R, .type = ARM_CP_CONST,
8485 .accessfn = access_aa32_tid3,
8486 .resetvalue = cpu->isar.id_isar6 },
8487 };
8488 define_arm_cp_regs(cpu, v6_idregs);
8489 define_arm_cp_regs(cpu, v6_cp_reginfo);
8490 } else {
8491 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
8492 }
8493 if (arm_feature(env, ARM_FEATURE_V6K)) {
8494 define_arm_cp_regs(cpu, v6k_cp_reginfo);
8495 }
8496 if (arm_feature(env, ARM_FEATURE_V7MP) &&
8497 !arm_feature(env, ARM_FEATURE_PMSA)) {
8498 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
8499 }
8500 if (arm_feature(env, ARM_FEATURE_V7VE)) {
8501 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
8502 }
8503 if (arm_feature(env, ARM_FEATURE_V7)) {
8504 ARMCPRegInfo clidr = {
8505 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
8506 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
8507 .access = PL1_R, .type = ARM_CP_CONST,
8508 .accessfn = access_tid4,
8509 .fgt = FGT_CLIDR_EL1,
8510 .resetvalue = cpu->clidr
8511 };
8512 define_one_arm_cp_reg(cpu, &clidr);
8513 define_arm_cp_regs(cpu, v7_cp_reginfo);
8514 define_debug_regs(cpu);
8515 define_pmu_regs(cpu);
8516 } else {
8517 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
8518 }
8519 if (arm_feature(env, ARM_FEATURE_V8)) {
8520 /*
8521 * v8 ID registers, which all have impdef reset values.
8522 * Note that within the ID register ranges the unused slots
8523 * must all RAZ, not UNDEF; future architecture versions may
8524 * define new registers here.
8525 * ID registers which are AArch64 views of the AArch32 ID registers
8526 * which already existed in v6 and v7 are handled elsewhere,
8527 * in v6_idregs[].
8528 */
8529 int i;
8530 ARMCPRegInfo v8_idregs[] = {
8531 /*
8532 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
8533 * emulation because we don't know the right value for the
8534 * GIC field until after we define these regs.
8535 */
8536 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
8537 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
8538 .access = PL1_R,
8539 #ifdef CONFIG_USER_ONLY
8540 .type = ARM_CP_CONST,
8541 .resetvalue = cpu->isar.id_aa64pfr0
8542 #else
8543 .type = ARM_CP_NO_RAW,
8544 .accessfn = access_aa64_tid3,
8545 .readfn = id_aa64pfr0_read,
8546 .writefn = arm_cp_write_ignore
8547 #endif
8548 },
8549 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
8550 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
8551 .access = PL1_R, .type = ARM_CP_CONST,
8552 .accessfn = access_aa64_tid3,
8553 .resetvalue = cpu->isar.id_aa64pfr1},
8554 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8555 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
8556 .access = PL1_R, .type = ARM_CP_CONST,
8557 .accessfn = access_aa64_tid3,
8558 .resetvalue = 0 },
8559 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8560 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
8561 .access = PL1_R, .type = ARM_CP_CONST,
8562 .accessfn = access_aa64_tid3,
8563 .resetvalue = 0 },
8564 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
8565 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
8566 .access = PL1_R, .type = ARM_CP_CONST,
8567 .accessfn = access_aa64_tid3,
8568 .resetvalue = cpu->isar.id_aa64zfr0 },
8569 { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
8570 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
8571 .access = PL1_R, .type = ARM_CP_CONST,
8572 .accessfn = access_aa64_tid3,
8573 .resetvalue = cpu->isar.id_aa64smfr0 },
8574 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8575 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
8576 .access = PL1_R, .type = ARM_CP_CONST,
8577 .accessfn = access_aa64_tid3,
8578 .resetvalue = 0 },
8579 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8580 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
8581 .access = PL1_R, .type = ARM_CP_CONST,
8582 .accessfn = access_aa64_tid3,
8583 .resetvalue = 0 },
8584 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
8585 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
8586 .access = PL1_R, .type = ARM_CP_CONST,
8587 .accessfn = access_aa64_tid3,
8588 .resetvalue = cpu->isar.id_aa64dfr0 },
8589 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
8590 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
8591 .access = PL1_R, .type = ARM_CP_CONST,
8592 .accessfn = access_aa64_tid3,
8593 .resetvalue = cpu->isar.id_aa64dfr1 },
8594 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8595 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
8596 .access = PL1_R, .type = ARM_CP_CONST,
8597 .accessfn = access_aa64_tid3,
8598 .resetvalue = 0 },
8599 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8600 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
8601 .access = PL1_R, .type = ARM_CP_CONST,
8602 .accessfn = access_aa64_tid3,
8603 .resetvalue = 0 },
8604 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
8605 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
8606 .access = PL1_R, .type = ARM_CP_CONST,
8607 .accessfn = access_aa64_tid3,
8608 .resetvalue = cpu->id_aa64afr0 },
8609 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
8610 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
8611 .access = PL1_R, .type = ARM_CP_CONST,
8612 .accessfn = access_aa64_tid3,
8613 .resetvalue = cpu->id_aa64afr1 },
8614 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8615 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
8616 .access = PL1_R, .type = ARM_CP_CONST,
8617 .accessfn = access_aa64_tid3,
8618 .resetvalue = 0 },
8619 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8620 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8621 .access = PL1_R, .type = ARM_CP_CONST,
8622 .accessfn = access_aa64_tid3,
8623 .resetvalue = 0 },
8624 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8625 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8626 .access = PL1_R, .type = ARM_CP_CONST,
8627 .accessfn = access_aa64_tid3,
8628 .resetvalue = cpu->isar.id_aa64isar0 },
8629 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8630 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8631 .access = PL1_R, .type = ARM_CP_CONST,
8632 .accessfn = access_aa64_tid3,
8633 .resetvalue = cpu->isar.id_aa64isar1 },
8634 { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
8635 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8636 .access = PL1_R, .type = ARM_CP_CONST,
8637 .accessfn = access_aa64_tid3,
8638 .resetvalue = cpu->isar.id_aa64isar2 },
8639 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8640 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8641 .access = PL1_R, .type = ARM_CP_CONST,
8642 .accessfn = access_aa64_tid3,
8643 .resetvalue = 0 },
8644 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8645 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8646 .access = PL1_R, .type = ARM_CP_CONST,
8647 .accessfn = access_aa64_tid3,
8648 .resetvalue = 0 },
8649 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8650 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8651 .access = PL1_R, .type = ARM_CP_CONST,
8652 .accessfn = access_aa64_tid3,
8653 .resetvalue = 0 },
8654 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8655 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8656 .access = PL1_R, .type = ARM_CP_CONST,
8657 .accessfn = access_aa64_tid3,
8658 .resetvalue = 0 },
8659 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8660 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8661 .access = PL1_R, .type = ARM_CP_CONST,
8662 .accessfn = access_aa64_tid3,
8663 .resetvalue = 0 },
8664 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8665 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8666 .access = PL1_R, .type = ARM_CP_CONST,
8667 .accessfn = access_aa64_tid3,
8668 .resetvalue = cpu->isar.id_aa64mmfr0 },
8669 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8670 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8671 .access = PL1_R, .type = ARM_CP_CONST,
8672 .accessfn = access_aa64_tid3,
8673 .resetvalue = cpu->isar.id_aa64mmfr1 },
8674 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
8675 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8676 .access = PL1_R, .type = ARM_CP_CONST,
8677 .accessfn = access_aa64_tid3,
8678 .resetvalue = cpu->isar.id_aa64mmfr2 },
8679 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8680 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8681 .access = PL1_R, .type = ARM_CP_CONST,
8682 .accessfn = access_aa64_tid3,
8683 .resetvalue = 0 },
8684 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8685 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8686 .access = PL1_R, .type = ARM_CP_CONST,
8687 .accessfn = access_aa64_tid3,
8688 .resetvalue = 0 },
8689 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8690 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8691 .access = PL1_R, .type = ARM_CP_CONST,
8692 .accessfn = access_aa64_tid3,
8693 .resetvalue = 0 },
8694 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8695 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8696 .access = PL1_R, .type = ARM_CP_CONST,
8697 .accessfn = access_aa64_tid3,
8698 .resetvalue = 0 },
8699 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8700 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8701 .access = PL1_R, .type = ARM_CP_CONST,
8702 .accessfn = access_aa64_tid3,
8703 .resetvalue = 0 },
8704 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8705 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8706 .access = PL1_R, .type = ARM_CP_CONST,
8707 .accessfn = access_aa64_tid3,
8708 .resetvalue = cpu->isar.mvfr0 },
8709 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8710 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8711 .access = PL1_R, .type = ARM_CP_CONST,
8712 .accessfn = access_aa64_tid3,
8713 .resetvalue = cpu->isar.mvfr1 },
8714 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8715 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8716 .access = PL1_R, .type = ARM_CP_CONST,
8717 .accessfn = access_aa64_tid3,
8718 .resetvalue = cpu->isar.mvfr2 },
8719 /*
8720 * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8721 * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8722 * as RAZ, since it is in the "reserved for future ID
8723 * registers, RAZ" part of the AArch32 encoding space.
8724 */
8725 { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8726 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8727 .access = PL1_R, .type = ARM_CP_CONST,
8728 .accessfn = access_aa64_tid3,
8729 .resetvalue = 0 },
8730 { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8731 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8732 .access = PL1_R, .type = ARM_CP_CONST,
8733 .accessfn = access_aa64_tid3,
8734 .resetvalue = 0 },
8735 { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8736 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8737 .access = PL1_R, .type = ARM_CP_CONST,
8738 .accessfn = access_aa64_tid3,
8739 .resetvalue = 0 },
8740 /*
8741 * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8742 * they're also RAZ for AArch64, and in v8 are gradually
8743 * being filled with AArch64-view-of-AArch32-ID-register
8744 * for new ID registers.
8745 */
8746 { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
8747 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8748 .access = PL1_R, .type = ARM_CP_CONST,
8749 .accessfn = access_aa64_tid3,
8750 .resetvalue = 0 },
8751 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
8752 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8753 .access = PL1_R, .type = ARM_CP_CONST,
8754 .accessfn = access_aa64_tid3,
8755 .resetvalue = cpu->isar.id_pfr2 },
8756 { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
8757 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8758 .access = PL1_R, .type = ARM_CP_CONST,
8759 .accessfn = access_aa64_tid3,
8760 .resetvalue = cpu->isar.id_dfr1 },
8761 { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
8762 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8763 .access = PL1_R, .type = ARM_CP_CONST,
8764 .accessfn = access_aa64_tid3,
8765 .resetvalue = cpu->isar.id_mmfr5 },
8766 { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
8767 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8768 .access = PL1_R, .type = ARM_CP_CONST,
8769 .accessfn = access_aa64_tid3,
8770 .resetvalue = 0 },
8771 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8772 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8773 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8774 .fgt = FGT_PMCEIDN_EL0,
8775 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
8776 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8777 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8778 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8779 .fgt = FGT_PMCEIDN_EL0,
8780 .resetvalue = cpu->pmceid0 },
8781 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8782 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8783 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8784 .fgt = FGT_PMCEIDN_EL0,
8785 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
8786 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8787 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8788 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8789 .fgt = FGT_PMCEIDN_EL0,
8790 .resetvalue = cpu->pmceid1 },
8791 };
8792 #ifdef CONFIG_USER_ONLY
8793 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
8794 { .name = "ID_AA64PFR0_EL1",
8795 .exported_bits = R_ID_AA64PFR0_FP_MASK |
8796 R_ID_AA64PFR0_ADVSIMD_MASK |
8797 R_ID_AA64PFR0_SVE_MASK |
8798 R_ID_AA64PFR0_DIT_MASK,
8799 .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8800 (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
8801 { .name = "ID_AA64PFR1_EL1",
8802 .exported_bits = R_ID_AA64PFR1_BT_MASK |
8803 R_ID_AA64PFR1_SSBS_MASK |
8804 R_ID_AA64PFR1_MTE_MASK |
8805 R_ID_AA64PFR1_SME_MASK },
8806 { .name = "ID_AA64PFR*_EL1_RESERVED",
8807 .is_glob = true },
8808 { .name = "ID_AA64ZFR0_EL1",
8809 .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8810 R_ID_AA64ZFR0_AES_MASK |
8811 R_ID_AA64ZFR0_BITPERM_MASK |
8812 R_ID_AA64ZFR0_BFLOAT16_MASK |
8813 R_ID_AA64ZFR0_SHA3_MASK |
8814 R_ID_AA64ZFR0_SM4_MASK |
8815 R_ID_AA64ZFR0_I8MM_MASK |
8816 R_ID_AA64ZFR0_F32MM_MASK |
8817 R_ID_AA64ZFR0_F64MM_MASK },
8818 { .name = "ID_AA64SMFR0_EL1",
8819 .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8820 R_ID_AA64SMFR0_BI32I32_MASK |
8821 R_ID_AA64SMFR0_B16F32_MASK |
8822 R_ID_AA64SMFR0_F16F32_MASK |
8823 R_ID_AA64SMFR0_I8I32_MASK |
8824 R_ID_AA64SMFR0_F16F16_MASK |
8825 R_ID_AA64SMFR0_B16B16_MASK |
8826 R_ID_AA64SMFR0_I16I32_MASK |
8827 R_ID_AA64SMFR0_F64F64_MASK |
8828 R_ID_AA64SMFR0_I16I64_MASK |
8829 R_ID_AA64SMFR0_SMEVER_MASK |
8830 R_ID_AA64SMFR0_FA64_MASK },
8831 { .name = "ID_AA64MMFR0_EL1",
8832 .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8833 .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8834 (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8835 { .name = "ID_AA64MMFR1_EL1",
8836 .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8837 { .name = "ID_AA64MMFR2_EL1",
8838 .exported_bits = R_ID_AA64MMFR2_AT_MASK },
8839 { .name = "ID_AA64MMFR*_EL1_RESERVED",
8840 .is_glob = true },
8841 { .name = "ID_AA64DFR0_EL1",
8842 .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8843 { .name = "ID_AA64DFR1_EL1" },
8844 { .name = "ID_AA64DFR*_EL1_RESERVED",
8845 .is_glob = true },
8846 { .name = "ID_AA64AFR*",
8847 .is_glob = true },
8848 { .name = "ID_AA64ISAR0_EL1",
8849 .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8850 R_ID_AA64ISAR0_SHA1_MASK |
8851 R_ID_AA64ISAR0_SHA2_MASK |
8852 R_ID_AA64ISAR0_CRC32_MASK |
8853 R_ID_AA64ISAR0_ATOMIC_MASK |
8854 R_ID_AA64ISAR0_RDM_MASK |
8855 R_ID_AA64ISAR0_SHA3_MASK |
8856 R_ID_AA64ISAR0_SM3_MASK |
8857 R_ID_AA64ISAR0_SM4_MASK |
8858 R_ID_AA64ISAR0_DP_MASK |
8859 R_ID_AA64ISAR0_FHM_MASK |
8860 R_ID_AA64ISAR0_TS_MASK |
8861 R_ID_AA64ISAR0_RNDR_MASK },
8862 { .name = "ID_AA64ISAR1_EL1",
8863 .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8864 R_ID_AA64ISAR1_APA_MASK |
8865 R_ID_AA64ISAR1_API_MASK |
8866 R_ID_AA64ISAR1_JSCVT_MASK |
8867 R_ID_AA64ISAR1_FCMA_MASK |
8868 R_ID_AA64ISAR1_LRCPC_MASK |
8869 R_ID_AA64ISAR1_GPA_MASK |
8870 R_ID_AA64ISAR1_GPI_MASK |
8871 R_ID_AA64ISAR1_FRINTTS_MASK |
8872 R_ID_AA64ISAR1_SB_MASK |
8873 R_ID_AA64ISAR1_BF16_MASK |
8874 R_ID_AA64ISAR1_DGH_MASK |
8875 R_ID_AA64ISAR1_I8MM_MASK },
8876 { .name = "ID_AA64ISAR2_EL1",
8877 .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8878 R_ID_AA64ISAR2_RPRES_MASK |
8879 R_ID_AA64ISAR2_GPA3_MASK |
8880 R_ID_AA64ISAR2_APA3_MASK |
8881 R_ID_AA64ISAR2_MOPS_MASK |
8882 R_ID_AA64ISAR2_BC_MASK |
8883 R_ID_AA64ISAR2_RPRFM_MASK |
8884 R_ID_AA64ISAR2_CSSC_MASK },
8885 { .name = "ID_AA64ISAR*_EL1_RESERVED",
8886 .is_glob = true },
8887 };
8888 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8889 #endif
8890 /*
8891 * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
8892 * TODO: For RMR, a write with bit 1 set should do something with
8893 * cpu_reset(). In the meantime, "the bit is strictly a request",
8894 * so we are in spec just ignoring writes.
8895 */
8896 if (!arm_feature(env, ARM_FEATURE_EL3) &&
8897 !arm_feature(env, ARM_FEATURE_EL2)) {
8898 ARMCPRegInfo el1_reset_regs[] = {
8899 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
8900 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8901 .access = PL1_R,
8902 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8903 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
8904 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8905 .access = PL1_RW, .type = ARM_CP_CONST,
8906 .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
8907 };
8908 define_arm_cp_regs(cpu, el1_reset_regs);
8909 }
8910 define_arm_cp_regs(cpu, v8_idregs);
8911 define_arm_cp_regs(cpu, v8_cp_reginfo);
8912 if (cpu_isar_feature(aa64_aa32_el1, cpu)) {
8913 define_arm_cp_regs(cpu, v8_aa32_el1_reginfo);
8914 }
8915
8916 for (i = 4; i < 16; i++) {
8917 /*
8918 * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8919 * For pre-v8 cores there are RAZ patterns for these in
8920 * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8921 * v8 extends the "must RAZ" part of the ID register space
8922 * to also cover c0, 0, c{8-15}, {0-7}.
8923 * These are STATE_AA32 because in the AArch64 sysreg space
8924 * c4-c7 is where the AArch64 ID registers live (and we've
8925 * already defined those in v8_idregs[]), and c8-c15 are not
8926 * "must RAZ" for AArch64.
8927 */
8928 g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8929 ARMCPRegInfo v8_aa32_raz_idregs = {
8930 .name = name,
8931 .state = ARM_CP_STATE_AA32,
8932 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8933 .access = PL1_R, .type = ARM_CP_CONST,
8934 .accessfn = access_aa64_tid3,
8935 .resetvalue = 0 };
8936 define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8937 }
8938 }
8939
8940 /*
8941 * Register the base EL2 cpregs.
8942 * Pre v8, these registers are implemented only as part of the
8943 * Virtualization Extensions (EL2 present). Beginning with v8,
8944 * if EL2 is missing but EL3 is enabled, mostly these become
8945 * RES0 from EL3, with some specific exceptions.
8946 */
8947 if (arm_feature(env, ARM_FEATURE_EL2)
8948 || (arm_feature(env, ARM_FEATURE_EL3)
8949 && arm_feature(env, ARM_FEATURE_V8))) {
8950 uint64_t vmpidr_def = mpidr_read_val(env);
8951 ARMCPRegInfo vpidr_regs[] = {
8952 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8953 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8954 .access = PL2_RW, .accessfn = access_el3_aa32ns,
8955 .resetvalue = cpu->midr,
8956 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8957 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
8958 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8959 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8960 .access = PL2_RW, .resetvalue = cpu->midr,
8961 .type = ARM_CP_EL3_NO_EL2_C_NZ,
8962 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8963 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8964 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8965 .access = PL2_RW, .accessfn = access_el3_aa32ns,
8966 .resetvalue = vmpidr_def,
8967 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8968 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
8969 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8970 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8971 .access = PL2_RW, .resetvalue = vmpidr_def,
8972 .type = ARM_CP_EL3_NO_EL2_C_NZ,
8973 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
8974 };
8975 /*
8976 * The only field of MDCR_EL2 that has a defined architectural reset
8977 * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8978 */
8979 ARMCPRegInfo mdcr_el2 = {
8980 .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
8981 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
8982 .writefn = mdcr_el2_write,
8983 .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8984 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8985 };
8986 define_one_arm_cp_reg(cpu, &mdcr_el2);
8987 define_arm_cp_regs(cpu, vpidr_regs);
8988 define_arm_cp_regs(cpu, el2_cp_reginfo);
8989 if (arm_feature(env, ARM_FEATURE_V8)) {
8990 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8991 }
8992 if (cpu_isar_feature(aa64_sel2, cpu)) {
8993 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8994 }
8995 /*
8996 * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
8997 * See commentary near RMR_EL1.
8998 */
8999 if (!arm_feature(env, ARM_FEATURE_EL3)) {
9000 static const ARMCPRegInfo el2_reset_regs[] = {
9001 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
9002 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
9003 .access = PL2_R,
9004 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9005 { .name = "RVBAR", .type = ARM_CP_ALIAS,
9006 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
9007 .access = PL2_R,
9008 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9009 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
9010 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
9011 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
9012 };
9013 define_arm_cp_regs(cpu, el2_reset_regs);
9014 }
9015 }
9016
9017 /* Register the base EL3 cpregs. */
9018 if (arm_feature(env, ARM_FEATURE_EL3)) {
9019 define_arm_cp_regs(cpu, el3_cp_reginfo);
9020 ARMCPRegInfo el3_regs[] = {
9021 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
9022 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
9023 .access = PL3_R,
9024 .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
9025 { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
9026 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
9027 .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
9028 { .name = "RMR", .state = ARM_CP_STATE_AA32,
9029 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
9030 .access = PL3_RW, .type = ARM_CP_CONST,
9031 .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
9032 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
9033 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
9034 .access = PL3_RW,
9035 .raw_writefn = raw_write, .writefn = sctlr_write,
9036 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
9037 .resetvalue = cpu->reset_sctlr },
9038 };
9039
9040 define_arm_cp_regs(cpu, el3_regs);
9041 }
9042 /*
9043 * The behaviour of NSACR is sufficiently various that we don't
9044 * try to describe it in a single reginfo:
9045 * if EL3 is 64 bit, then trap to EL3 from S EL1,
9046 * reads as constant 0xc00 from NS EL1 and NS EL2
9047 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
9048 * if v7 without EL3, register doesn't exist
9049 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
9050 */
9051 if (arm_feature(env, ARM_FEATURE_EL3)) {
9052 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
9053 static const ARMCPRegInfo nsacr = {
9054 .name = "NSACR", .type = ARM_CP_CONST,
9055 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9056 .access = PL1_RW, .accessfn = nsacr_access,
9057 .resetvalue = 0xc00
9058 };
9059 define_one_arm_cp_reg(cpu, &nsacr);
9060 } else {
9061 static const ARMCPRegInfo nsacr = {
9062 .name = "NSACR",
9063 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9064 .access = PL3_RW | PL1_R,
9065 .resetvalue = 0,
9066 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
9067 };
9068 define_one_arm_cp_reg(cpu, &nsacr);
9069 }
9070 } else {
9071 if (arm_feature(env, ARM_FEATURE_V8)) {
9072 static const ARMCPRegInfo nsacr = {
9073 .name = "NSACR", .type = ARM_CP_CONST,
9074 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9075 .access = PL1_R,
9076 .resetvalue = 0xc00
9077 };
9078 define_one_arm_cp_reg(cpu, &nsacr);
9079 }
9080 }
9081
9082 if (arm_feature(env, ARM_FEATURE_PMSA)) {
9083 if (arm_feature(env, ARM_FEATURE_V6)) {
9084 /* PMSAv6 not implemented */
9085 assert(arm_feature(env, ARM_FEATURE_V7));
9086 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
9087 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
9088 } else {
9089 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
9090 }
9091 } else {
9092 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
9093 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
9094 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
9095 if (cpu_isar_feature(aa32_hpd, cpu)) {
9096 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
9097 }
9098 }
9099 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
9100 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
9101 }
9102 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
9103 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
9104 }
9105 if (arm_feature(env, ARM_FEATURE_VAPA)) {
9106 ARMCPRegInfo vapa_cp_reginfo[] = {
9107 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
9108 .access = PL1_RW, .resetvalue = 0,
9109 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
9110 offsetoflow32(CPUARMState, cp15.par_ns) },
9111 .writefn = par_write},
9112 #ifndef CONFIG_USER_ONLY
9113 /* This underdecoding is safe because the reginfo is NO_RAW. */
9114 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
9115 .access = PL1_W, .accessfn = ats_access,
9116 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
9117 #endif
9118 };
9119
9120 /*
9121 * When LPAE exists this 32-bit PAR register is an alias of the
9122 * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
9123 */
9124 if (arm_feature(env, ARM_FEATURE_LPAE)) {
9125 vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
9126 }
9127 define_arm_cp_regs(cpu, vapa_cp_reginfo);
9128 }
9129 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
9130 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
9131 }
9132 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
9133 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
9134 }
9135 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
9136 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
9137 }
9138 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
9139 define_arm_cp_regs(cpu, omap_cp_reginfo);
9140 }
9141 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
9142 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
9143 }
9144 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9145 define_arm_cp_regs(cpu, xscale_cp_reginfo);
9146 }
9147 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
9148 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
9149 }
9150 if (arm_feature(env, ARM_FEATURE_LPAE)) {
9151 define_arm_cp_regs(cpu, lpae_cp_reginfo);
9152 }
9153 if (cpu_isar_feature(aa32_jazelle, cpu)) {
9154 define_arm_cp_regs(cpu, jazelle_regs);
9155 }
9156 /*
9157 * Slightly awkwardly, the OMAP and StrongARM cores need all of
9158 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
9159 * be read-only (ie write causes UNDEF exception).
9160 */
9161 {
9162 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
9163 /*
9164 * Pre-v8 MIDR space.
9165 * Note that the MIDR isn't a simple constant register because
9166 * of the TI925 behaviour where writes to another register can
9167 * cause the MIDR value to change.
9168 *
9169 * Unimplemented registers in the c15 0 0 0 space default to
9170 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
9171 * and friends override accordingly.
9172 */
9173 { .name = "MIDR",
9174 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
9175 .access = PL1_R, .resetvalue = cpu->midr,
9176 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
9177 .readfn = midr_read,
9178 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9179 .type = ARM_CP_OVERRIDE },
9180 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
9181 { .name = "DUMMY",
9182 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
9183 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9184 { .name = "DUMMY",
9185 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
9186 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9187 { .name = "DUMMY",
9188 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
9189 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9190 { .name = "DUMMY",
9191 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
9192 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9193 { .name = "DUMMY",
9194 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
9195 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9196 };
9197 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
9198 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
9199 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
9200 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
9201 .fgt = FGT_MIDR_EL1,
9202 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9203 .readfn = midr_read },
9204 /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
9205 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
9206 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
9207 .access = PL1_R, .resetvalue = cpu->midr },
9208 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
9209 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
9210 .access = PL1_R,
9211 .accessfn = access_aa64_tid1,
9212 .fgt = FGT_REVIDR_EL1,
9213 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
9214 };
9215 ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
9216 .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
9217 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9218 .access = PL1_R, .resetvalue = cpu->midr
9219 };
9220 ARMCPRegInfo id_cp_reginfo[] = {
9221 /* These are common to v8 and pre-v8 */
9222 { .name = "CTR",
9223 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
9224 .access = PL1_R, .accessfn = ctr_el0_access,
9225 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9226 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
9227 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
9228 .access = PL0_R, .accessfn = ctr_el0_access,
9229 .fgt = FGT_CTR_EL0,
9230 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9231 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
9232 { .name = "TCMTR",
9233 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
9234 .access = PL1_R,
9235 .accessfn = access_aa32_tid1,
9236 .type = ARM_CP_CONST, .resetvalue = 0 },
9237 };
9238 /* TLBTR is specific to VMSA */
9239 ARMCPRegInfo id_tlbtr_reginfo = {
9240 .name = "TLBTR",
9241 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
9242 .access = PL1_R,
9243 .accessfn = access_aa32_tid1,
9244 .type = ARM_CP_CONST, .resetvalue = 0,
9245 };
9246 /* MPUIR is specific to PMSA V6+ */
9247 ARMCPRegInfo id_mpuir_reginfo = {
9248 .name = "MPUIR",
9249 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9250 .access = PL1_R, .type = ARM_CP_CONST,
9251 .resetvalue = cpu->pmsav7_dregion << 8
9252 };
9253 /* HMPUIR is specific to PMSA V8 */
9254 ARMCPRegInfo id_hmpuir_reginfo = {
9255 .name = "HMPUIR",
9256 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
9257 .access = PL2_R, .type = ARM_CP_CONST,
9258 .resetvalue = cpu->pmsav8r_hdregion
9259 };
9260 static const ARMCPRegInfo crn0_wi_reginfo = {
9261 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
9262 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
9263 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
9264 };
9265 #ifdef CONFIG_USER_ONLY
9266 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
9267 { .name = "MIDR_EL1",
9268 .exported_bits = R_MIDR_EL1_REVISION_MASK |
9269 R_MIDR_EL1_PARTNUM_MASK |
9270 R_MIDR_EL1_ARCHITECTURE_MASK |
9271 R_MIDR_EL1_VARIANT_MASK |
9272 R_MIDR_EL1_IMPLEMENTER_MASK },
9273 { .name = "REVIDR_EL1" },
9274 };
9275 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
9276 #endif
9277 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
9278 arm_feature(env, ARM_FEATURE_STRONGARM)) {
9279 size_t i;
9280 /*
9281 * Register the blanket "writes ignored" value first to cover the
9282 * whole space. Then update the specific ID registers to allow write
9283 * access, so that they ignore writes rather than causing them to
9284 * UNDEF.
9285 */
9286 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
9287 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
9288 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
9289 }
9290 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
9291 id_cp_reginfo[i].access = PL1_RW;
9292 }
9293 id_mpuir_reginfo.access = PL1_RW;
9294 id_tlbtr_reginfo.access = PL1_RW;
9295 }
9296 if (arm_feature(env, ARM_FEATURE_V8)) {
9297 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
9298 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9299 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
9300 }
9301 } else {
9302 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
9303 }
9304 define_arm_cp_regs(cpu, id_cp_reginfo);
9305 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9306 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
9307 } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
9308 arm_feature(env, ARM_FEATURE_V8)) {
9309 uint32_t i = 0;
9310 char *tmp_string;
9311
9312 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9313 define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
9314 define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
9315
9316 /* Register alias is only valid for first 32 indexes */
9317 for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
9318 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9319 uint8_t opc1 = extract32(i, 4, 1);
9320 uint8_t opc2 = extract32(i, 0, 1) << 2;
9321
9322 tmp_string = g_strdup_printf("PRBAR%u", i);
9323 ARMCPRegInfo tmp_prbarn_reginfo = {
9324 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9325 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9326 .access = PL1_RW, .resetvalue = 0,
9327 .accessfn = access_tvm_trvm,
9328 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9329 };
9330 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
9331 g_free(tmp_string);
9332
9333 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9334 tmp_string = g_strdup_printf("PRLAR%u", i);
9335 ARMCPRegInfo tmp_prlarn_reginfo = {
9336 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9337 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9338 .access = PL1_RW, .resetvalue = 0,
9339 .accessfn = access_tvm_trvm,
9340 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9341 };
9342 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
9343 g_free(tmp_string);
9344 }
9345
9346 /* Register alias is only valid for first 32 indexes */
9347 for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
9348 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9349 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
9350 uint8_t opc2 = extract32(i, 0, 1) << 2;
9351
9352 tmp_string = g_strdup_printf("HPRBAR%u", i);
9353 ARMCPRegInfo tmp_hprbarn_reginfo = {
9354 .name = tmp_string,
9355 .type = ARM_CP_NO_RAW,
9356 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9357 .access = PL2_RW, .resetvalue = 0,
9358 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9359 };
9360 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
9361 g_free(tmp_string);
9362
9363 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9364 tmp_string = g_strdup_printf("HPRLAR%u", i);
9365 ARMCPRegInfo tmp_hprlarn_reginfo = {
9366 .name = tmp_string,
9367 .type = ARM_CP_NO_RAW,
9368 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9369 .access = PL2_RW, .resetvalue = 0,
9370 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9371 };
9372 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
9373 g_free(tmp_string);
9374 }
9375 } else if (arm_feature(env, ARM_FEATURE_V7)) {
9376 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9377 }
9378 }
9379
9380 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
9381 ARMCPRegInfo mpidr_cp_reginfo[] = {
9382 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
9383 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
9384 .fgt = FGT_MPIDR_EL1,
9385 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
9386 };
9387 #ifdef CONFIG_USER_ONLY
9388 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
9389 { .name = "MPIDR_EL1",
9390 .fixed_bits = 0x0000000080000000 },
9391 };
9392 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
9393 #endif
9394 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
9395 }
9396
9397 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
9398 ARMCPRegInfo auxcr_reginfo[] = {
9399 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
9400 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
9401 .access = PL1_RW, .accessfn = access_tacr,
9402 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
9403 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
9404 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
9405 .access = PL2_RW, .type = ARM_CP_CONST,
9406 .resetvalue = 0 },
9407 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
9408 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
9409 .access = PL3_RW, .type = ARM_CP_CONST,
9410 .resetvalue = 0 },
9411 };
9412 define_arm_cp_regs(cpu, auxcr_reginfo);
9413 if (cpu_isar_feature(aa32_ac2, cpu)) {
9414 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
9415 }
9416 }
9417
9418 if (arm_feature(env, ARM_FEATURE_CBAR)) {
9419 /*
9420 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
9421 * There are two flavours:
9422 * (1) older 32-bit only cores have a simple 32-bit CBAR
9423 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
9424 * 32-bit register visible to AArch32 at a different encoding
9425 * to the "flavour 1" register and with the bits rearranged to
9426 * be able to squash a 64-bit address into the 32-bit view.
9427 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
9428 * in future if we support AArch32-only configs of some of the
9429 * AArch64 cores we might need to add a specific feature flag
9430 * to indicate cores with "flavour 2" CBAR.
9431 */
9432 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
9433 /* 32 bit view is [31:18] 0...0 [43:32]. */
9434 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
9435 | extract64(cpu->reset_cbar, 32, 12);
9436 ARMCPRegInfo cbar_reginfo[] = {
9437 { .name = "CBAR",
9438 .type = ARM_CP_CONST,
9439 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
9440 .access = PL1_R, .resetvalue = cbar32 },
9441 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
9442 .type = ARM_CP_CONST,
9443 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
9444 .access = PL1_R, .resetvalue = cpu->reset_cbar },
9445 };
9446 /* We don't implement a r/w 64 bit CBAR currently */
9447 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
9448 define_arm_cp_regs(cpu, cbar_reginfo);
9449 } else {
9450 ARMCPRegInfo cbar = {
9451 .name = "CBAR",
9452 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
9453 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
9454 .fieldoffset = offsetof(CPUARMState,
9455 cp15.c15_config_base_address)
9456 };
9457 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
9458 cbar.access = PL1_R;
9459 cbar.fieldoffset = 0;
9460 cbar.type = ARM_CP_CONST;
9461 }
9462 define_one_arm_cp_reg(cpu, &cbar);
9463 }
9464 }
9465
9466 if (arm_feature(env, ARM_FEATURE_VBAR)) {
9467 static const ARMCPRegInfo vbar_cp_reginfo[] = {
9468 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
9469 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
9470 .access = PL1_RW, .writefn = vbar_write,
9471 .accessfn = access_nv1,
9472 .fgt = FGT_VBAR_EL1,
9473 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
9474 offsetof(CPUARMState, cp15.vbar_ns) },
9475 .resetvalue = 0 },
9476 };
9477 define_arm_cp_regs(cpu, vbar_cp_reginfo);
9478 }
9479
9480 /* Generic registers whose values depend on the implementation */
9481 {
9482 ARMCPRegInfo sctlr = {
9483 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
9484 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
9485 .access = PL1_RW, .accessfn = access_tvm_trvm,
9486 .fgt = FGT_SCTLR_EL1,
9487 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
9488 offsetof(CPUARMState, cp15.sctlr_ns) },
9489 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
9490 .raw_writefn = raw_write,
9491 };
9492 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9493 /*
9494 * Normally we would always end the TB on an SCTLR write, but Linux
9495 * arch/arm/mach-pxa/sleep.S expects two instructions following
9496 * an MMU enable to execute from cache. Imitate this behaviour.
9497 */
9498 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
9499 }
9500 define_one_arm_cp_reg(cpu, &sctlr);
9501
9502 if (arm_feature(env, ARM_FEATURE_PMSA) &&
9503 arm_feature(env, ARM_FEATURE_V8)) {
9504 ARMCPRegInfo vsctlr = {
9505 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
9506 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
9507 .access = PL2_RW, .resetvalue = 0x0,
9508 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
9509 };
9510 define_one_arm_cp_reg(cpu, &vsctlr);
9511 }
9512 }
9513
9514 if (cpu_isar_feature(aa64_lor, cpu)) {
9515 define_arm_cp_regs(cpu, lor_reginfo);
9516 }
9517 if (cpu_isar_feature(aa64_pan, cpu)) {
9518 define_one_arm_cp_reg(cpu, &pan_reginfo);
9519 }
9520 #ifndef CONFIG_USER_ONLY
9521 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
9522 define_arm_cp_regs(cpu, ats1e1_reginfo);
9523 }
9524 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
9525 define_arm_cp_regs(cpu, ats1cp_reginfo);
9526 }
9527 #endif
9528 if (cpu_isar_feature(aa64_uao, cpu)) {
9529 define_one_arm_cp_reg(cpu, &uao_reginfo);
9530 }
9531
9532 if (cpu_isar_feature(aa64_dit, cpu)) {
9533 define_one_arm_cp_reg(cpu, &dit_reginfo);
9534 }
9535 if (cpu_isar_feature(aa64_ssbs, cpu)) {
9536 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
9537 }
9538 if (cpu_isar_feature(any_ras, cpu)) {
9539 define_arm_cp_regs(cpu, minimal_ras_reginfo);
9540 }
9541
9542 if (cpu_isar_feature(aa64_vh, cpu) ||
9543 cpu_isar_feature(aa64_debugv8p2, cpu)) {
9544 define_one_arm_cp_reg(cpu, &contextidr_el2);
9545 }
9546 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9547 define_arm_cp_regs(cpu, vhe_reginfo);
9548 }
9549
9550 if (cpu_isar_feature(aa64_sve, cpu)) {
9551 define_arm_cp_regs(cpu, zcr_reginfo);
9552 }
9553
9554 if (cpu_isar_feature(aa64_hcx, cpu)) {
9555 define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
9556 }
9557
9558 #ifdef TARGET_AARCH64
9559 if (cpu_isar_feature(aa64_sme, cpu)) {
9560 define_arm_cp_regs(cpu, sme_reginfo);
9561 }
9562 if (cpu_isar_feature(aa64_pauth, cpu)) {
9563 define_arm_cp_regs(cpu, pauth_reginfo);
9564 }
9565 if (cpu_isar_feature(aa64_rndr, cpu)) {
9566 define_arm_cp_regs(cpu, rndr_reginfo);
9567 }
9568 if (cpu_isar_feature(aa64_tlbirange, cpu)) {
9569 define_arm_cp_regs(cpu, tlbirange_reginfo);
9570 }
9571 if (cpu_isar_feature(aa64_tlbios, cpu)) {
9572 define_arm_cp_regs(cpu, tlbios_reginfo);
9573 }
9574 /* Data Cache clean instructions up to PoP */
9575 if (cpu_isar_feature(aa64_dcpop, cpu)) {
9576 define_one_arm_cp_reg(cpu, dcpop_reg);
9577
9578 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
9579 define_one_arm_cp_reg(cpu, dcpodp_reg);
9580 }
9581 }
9582
9583 /*
9584 * If full MTE is enabled, add all of the system registers.
9585 * If only "instructions available at EL0" are enabled,
9586 * then define only a RAZ/WI version of PSTATE.TCO.
9587 */
9588 if (cpu_isar_feature(aa64_mte, cpu)) {
9589 ARMCPRegInfo gmid_reginfo = {
9590 .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
9591 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
9592 .access = PL1_R, .accessfn = access_aa64_tid5,
9593 .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
9594 };
9595 define_one_arm_cp_reg(cpu, &gmid_reginfo);
9596 define_arm_cp_regs(cpu, mte_reginfo);
9597 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9598 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
9599 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
9600 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9601 }
9602
9603 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
9604 define_arm_cp_regs(cpu, scxtnum_reginfo);
9605 }
9606
9607 if (cpu_isar_feature(aa64_fgt, cpu)) {
9608 define_arm_cp_regs(cpu, fgt_reginfo);
9609 }
9610
9611 if (cpu_isar_feature(aa64_rme, cpu)) {
9612 define_arm_cp_regs(cpu, rme_reginfo);
9613 if (cpu_isar_feature(aa64_mte, cpu)) {
9614 define_arm_cp_regs(cpu, rme_mte_reginfo);
9615 }
9616 }
9617 #endif
9618
9619 if (cpu_isar_feature(any_predinv, cpu)) {
9620 define_arm_cp_regs(cpu, predinv_reginfo);
9621 }
9622
9623 if (cpu_isar_feature(any_ccidx, cpu)) {
9624 define_arm_cp_regs(cpu, ccsidr2_reginfo);
9625 }
9626
9627 #ifndef CONFIG_USER_ONLY
9628 /*
9629 * Register redirections and aliases must be done last,
9630 * after the registers from the other extensions have been defined.
9631 */
9632 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9633 define_arm_vh_e2h_redirects_aliases(cpu);
9634 }
9635 #endif
9636 }
9637
9638 /*
9639 * Private utility function for define_one_arm_cp_reg_with_opaque():
9640 * add a single reginfo struct to the hash table.
9641 */
9642 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
9643 void *opaque, CPState state,
9644 CPSecureState secstate,
9645 int crm, int opc1, int opc2,
9646 const char *name)
9647 {
9648 CPUARMState *env = &cpu->env;
9649 uint32_t key;
9650 ARMCPRegInfo *r2;
9651 bool is64 = r->type & ARM_CP_64BIT;
9652 bool ns = secstate & ARM_CP_SECSTATE_NS;
9653 int cp = r->cp;
9654 size_t name_len;
9655 bool make_const;
9656
9657 switch (state) {
9658 case ARM_CP_STATE_AA32:
9659 /* We assume it is a cp15 register if the .cp field is left unset. */
9660 if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9661 cp = 15;
9662 }
9663 key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9664 break;
9665 case ARM_CP_STATE_AA64:
9666 /*
9667 * To allow abbreviation of ARMCPRegInfo definitions, we treat
9668 * cp == 0 as equivalent to the value for "standard guest-visible
9669 * sysreg". STATE_BOTH definitions are also always "standard sysreg"
9670 * in their AArch64 view (the .cp value may be non-zero for the
9671 * benefit of the AArch32 view).
9672 */
9673 if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9674 cp = CP_REG_ARM64_SYSREG_CP;
9675 }
9676 key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9677 break;
9678 default:
9679 g_assert_not_reached();
9680 }
9681
9682 /* Overriding of an existing definition must be explicitly requested. */
9683 if (!(r->type & ARM_CP_OVERRIDE)) {
9684 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9685 if (oldreg) {
9686 assert(oldreg->type & ARM_CP_OVERRIDE);
9687 }
9688 }
9689
9690 /*
9691 * Eliminate registers that are not present because the EL is missing.
9692 * Doing this here makes it easier to put all registers for a given
9693 * feature into the same ARMCPRegInfo array and define them all at once.
9694 */
9695 make_const = false;
9696 if (arm_feature(env, ARM_FEATURE_EL3)) {
9697 /*
9698 * An EL2 register without EL2 but with EL3 is (usually) RES0.
9699 * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9700 */
9701 int min_el = ctz32(r->access) / 2;
9702 if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9703 if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9704 return;
9705 }
9706 make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9707 }
9708 } else {
9709 CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9710 ? PL2_RW : PL1_RW);
9711 if ((r->access & max_el) == 0) {
9712 return;
9713 }
9714 }
9715
9716 /* Combine cpreg and name into one allocation. */
9717 name_len = strlen(name) + 1;
9718 r2 = g_malloc(sizeof(*r2) + name_len);
9719 *r2 = *r;
9720 r2->name = memcpy(r2 + 1, name, name_len);
9721
9722 /*
9723 * Update fields to match the instantiation, overwiting wildcards
9724 * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
9725 */
9726 r2->cp = cp;
9727 r2->crm = crm;
9728 r2->opc1 = opc1;
9729 r2->opc2 = opc2;
9730 r2->state = state;
9731 r2->secure = secstate;
9732 if (opaque) {
9733 r2->opaque = opaque;
9734 }
9735
9736 if (make_const) {
9737 /* This should not have been a very special register to begin. */
9738 int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9739 assert(old_special == 0 || old_special == ARM_CP_NOP);
9740 /*
9741 * Set the special function to CONST, retaining the other flags.
9742 * This is important for e.g. ARM_CP_SVE so that we still
9743 * take the SVE trap if CPTR_EL3.EZ == 0.
9744 */
9745 r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9746 /*
9747 * Usually, these registers become RES0, but there are a few
9748 * special cases like VPIDR_EL2 which have a constant non-zero
9749 * value with writes ignored.
9750 */
9751 if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9752 r2->resetvalue = 0;
9753 }
9754 /*
9755 * ARM_CP_CONST has precedence, so removing the callbacks and
9756 * offsets are not strictly necessary, but it is potentially
9757 * less confusing to debug later.
9758 */
9759 r2->readfn = NULL;
9760 r2->writefn = NULL;
9761 r2->raw_readfn = NULL;
9762 r2->raw_writefn = NULL;
9763 r2->resetfn = NULL;
9764 r2->fieldoffset = 0;
9765 r2->bank_fieldoffsets[0] = 0;
9766 r2->bank_fieldoffsets[1] = 0;
9767 } else {
9768 bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
9769
9770 if (isbanked) {
9771 /*
9772 * Register is banked (using both entries in array).
9773 * Overwriting fieldoffset as the array is only used to define
9774 * banked registers but later only fieldoffset is used.
9775 */
9776 r2->fieldoffset = r->bank_fieldoffsets[ns];
9777 }
9778 if (state == ARM_CP_STATE_AA32) {
9779 if (isbanked) {
9780 /*
9781 * If the register is banked then we don't need to migrate or
9782 * reset the 32-bit instance in certain cases:
9783 *
9784 * 1) If the register has both 32-bit and 64-bit instances
9785 * then we can count on the 64-bit instance taking care
9786 * of the non-secure bank.
9787 * 2) If ARMv8 is enabled then we can count on a 64-bit
9788 * version taking care of the secure bank. This requires
9789 * that separate 32 and 64-bit definitions are provided.
9790 */
9791 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9792 (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9793 r2->type |= ARM_CP_ALIAS;
9794 }
9795 } else if ((secstate != r->secure) && !ns) {
9796 /*
9797 * The register is not banked so we only want to allow
9798 * migration of the non-secure instance.
9799 */
9800 r2->type |= ARM_CP_ALIAS;
9801 }
9802
9803 if (HOST_BIG_ENDIAN &&
9804 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9805 r2->fieldoffset += sizeof(uint32_t);
9806 }
9807 }
9808 }
9809
9810 /*
9811 * By convention, for wildcarded registers only the first
9812 * entry is used for migration; the others are marked as
9813 * ALIAS so we don't try to transfer the register
9814 * multiple times. Special registers (ie NOP/WFI) are
9815 * never migratable and not even raw-accessible.
9816 */
9817 if (r2->type & ARM_CP_SPECIAL_MASK) {
9818 r2->type |= ARM_CP_NO_RAW;
9819 }
9820 if (((r->crm == CP_ANY) && crm != 0) ||
9821 ((r->opc1 == CP_ANY) && opc1 != 0) ||
9822 ((r->opc2 == CP_ANY) && opc2 != 0)) {
9823 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
9824 }
9825
9826 /*
9827 * Check that raw accesses are either forbidden or handled. Note that
9828 * we can't assert this earlier because the setup of fieldoffset for
9829 * banked registers has to be done first.
9830 */
9831 if (!(r2->type & ARM_CP_NO_RAW)) {
9832 assert(!raw_accessors_invalid(r2));
9833 }
9834
9835 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
9836 }
9837
9838
9839 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9840 const ARMCPRegInfo *r, void *opaque)
9841 {
9842 /*
9843 * Define implementations of coprocessor registers.
9844 * We store these in a hashtable because typically
9845 * there are less than 150 registers in a space which
9846 * is 16*16*16*8*8 = 262144 in size.
9847 * Wildcarding is supported for the crm, opc1 and opc2 fields.
9848 * If a register is defined twice then the second definition is
9849 * used, so this can be used to define some generic registers and
9850 * then override them with implementation specific variations.
9851 * At least one of the original and the second definition should
9852 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9853 * against accidental use.
9854 *
9855 * The state field defines whether the register is to be
9856 * visible in the AArch32 or AArch64 execution state. If the
9857 * state is set to ARM_CP_STATE_BOTH then we synthesise a
9858 * reginfo structure for the AArch32 view, which sees the lower
9859 * 32 bits of the 64 bit register.
9860 *
9861 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9862 * be wildcarded. AArch64 registers are always considered to be 64
9863 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9864 * the register, if any.
9865 */
9866 int crm, opc1, opc2;
9867 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9868 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9869 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9870 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9871 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9872 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
9873 CPState state;
9874
9875 /* 64 bit registers have only CRm and Opc1 fields */
9876 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
9877 /* op0 only exists in the AArch64 encodings */
9878 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9879 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9880 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
9881 /*
9882 * This API is only for Arm's system coprocessors (14 and 15) or
9883 * (M-profile or v7A-and-earlier only) for implementation defined
9884 * coprocessors in the range 0..7. Our decode assumes this, since
9885 * 8..13 can be used for other insns including VFP and Neon. See
9886 * valid_cp() in translate.c. Assert here that we haven't tried
9887 * to use an invalid coprocessor number.
9888 */
9889 switch (r->state) {
9890 case ARM_CP_STATE_BOTH:
9891 /* 0 has a special meaning, but otherwise the same rules as AA32. */
9892 if (r->cp == 0) {
9893 break;
9894 }
9895 /* fall through */
9896 case ARM_CP_STATE_AA32:
9897 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9898 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9899 assert(r->cp >= 14 && r->cp <= 15);
9900 } else {
9901 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9902 }
9903 break;
9904 case ARM_CP_STATE_AA64:
9905 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9906 break;
9907 default:
9908 g_assert_not_reached();
9909 }
9910 /*
9911 * The AArch64 pseudocode CheckSystemAccess() specifies that op1
9912 * encodes a minimum access level for the register. We roll this
9913 * runtime check into our general permission check code, so check
9914 * here that the reginfo's specified permissions are strict enough
9915 * to encompass the generic architectural permission check.
9916 */
9917 if (r->state != ARM_CP_STATE_AA32) {
9918 CPAccessRights mask;
9919 switch (r->opc1) {
9920 case 0:
9921 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9922 mask = PL0U_R | PL1_RW;
9923 break;
9924 case 1: case 2:
9925 /* min_EL EL1 */
9926 mask = PL1_RW;
9927 break;
9928 case 3:
9929 /* min_EL EL0 */
9930 mask = PL0_RW;
9931 break;
9932 case 4:
9933 case 5:
9934 /* min_EL EL2 */
9935 mask = PL2_RW;
9936 break;
9937 case 6:
9938 /* min_EL EL3 */
9939 mask = PL3_RW;
9940 break;
9941 case 7:
9942 /* min_EL EL1, secure mode only (we don't check the latter) */
9943 mask = PL1_RW;
9944 break;
9945 default:
9946 /* broken reginfo with out-of-range opc1 */
9947 g_assert_not_reached();
9948 }
9949 /* assert our permissions are not too lax (stricter is fine) */
9950 assert((r->access & ~mask) == 0);
9951 }
9952
9953 /*
9954 * Check that the register definition has enough info to handle
9955 * reads and writes if they are permitted.
9956 */
9957 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
9958 if (r->access & PL3_R) {
9959 assert((r->fieldoffset ||
9960 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9961 r->readfn);
9962 }
9963 if (r->access & PL3_W) {
9964 assert((r->fieldoffset ||
9965 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9966 r->writefn);
9967 }
9968 }
9969
9970 for (crm = crmmin; crm <= crmmax; crm++) {
9971 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9972 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
9973 for (state = ARM_CP_STATE_AA32;
9974 state <= ARM_CP_STATE_AA64; state++) {
9975 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9976 continue;
9977 }
9978 if (state == ARM_CP_STATE_AA32) {
9979 /*
9980 * Under AArch32 CP registers can be common
9981 * (same for secure and non-secure world) or banked.
9982 */
9983 char *name;
9984
9985 switch (r->secure) {
9986 case ARM_CP_SECSTATE_S:
9987 case ARM_CP_SECSTATE_NS:
9988 add_cpreg_to_hashtable(cpu, r, opaque, state,
9989 r->secure, crm, opc1, opc2,
9990 r->name);
9991 break;
9992 case ARM_CP_SECSTATE_BOTH:
9993 name = g_strdup_printf("%s_S", r->name);
9994 add_cpreg_to_hashtable(cpu, r, opaque, state,
9995 ARM_CP_SECSTATE_S,
9996 crm, opc1, opc2, name);
9997 g_free(name);
9998 add_cpreg_to_hashtable(cpu, r, opaque, state,
9999 ARM_CP_SECSTATE_NS,
10000 crm, opc1, opc2, r->name);
10001 break;
10002 default:
10003 g_assert_not_reached();
10004 }
10005 } else {
10006 /*
10007 * AArch64 registers get mapped to non-secure instance
10008 * of AArch32
10009 */
10010 add_cpreg_to_hashtable(cpu, r, opaque, state,
10011 ARM_CP_SECSTATE_NS,
10012 crm, opc1, opc2, r->name);
10013 }
10014 }
10015 }
10016 }
10017 }
10018 }
10019
10020 /* Define a whole list of registers */
10021 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
10022 void *opaque, size_t len)
10023 {
10024 size_t i;
10025 for (i = 0; i < len; ++i) {
10026 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
10027 }
10028 }
10029
10030 /*
10031 * Modify ARMCPRegInfo for access from userspace.
10032 *
10033 * This is a data driven modification directed by
10034 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
10035 * user-space cannot alter any values and dynamic values pertaining to
10036 * execution state are hidden from user space view anyway.
10037 */
10038 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
10039 const ARMCPRegUserSpaceInfo *mods,
10040 size_t mods_len)
10041 {
10042 for (size_t mi = 0; mi < mods_len; ++mi) {
10043 const ARMCPRegUserSpaceInfo *m = mods + mi;
10044 GPatternSpec *pat = NULL;
10045
10046 if (m->is_glob) {
10047 pat = g_pattern_spec_new(m->name);
10048 }
10049 for (size_t ri = 0; ri < regs_len; ++ri) {
10050 ARMCPRegInfo *r = regs + ri;
10051
10052 if (pat && g_pattern_match_string(pat, r->name)) {
10053 r->type = ARM_CP_CONST;
10054 r->access = PL0U_R;
10055 r->resetvalue = 0;
10056 /* continue */
10057 } else if (strcmp(r->name, m->name) == 0) {
10058 r->type = ARM_CP_CONST;
10059 r->access = PL0U_R;
10060 r->resetvalue &= m->exported_bits;
10061 r->resetvalue |= m->fixed_bits;
10062 break;
10063 }
10064 }
10065 if (pat) {
10066 g_pattern_spec_free(pat);
10067 }
10068 }
10069 }
10070
10071 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
10072 {
10073 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
10074 }
10075
10076 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
10077 uint64_t value)
10078 {
10079 /* Helper coprocessor write function for write-ignore registers */
10080 }
10081
10082 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
10083 {
10084 /* Helper coprocessor write function for read-as-zero registers */
10085 return 0;
10086 }
10087
10088 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
10089 {
10090 /* Helper coprocessor reset function for do-nothing-on-reset registers */
10091 }
10092
10093 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
10094 {
10095 /*
10096 * Return true if it is not valid for us to switch to
10097 * this CPU mode (ie all the UNPREDICTABLE cases in
10098 * the ARM ARM CPSRWriteByInstr pseudocode).
10099 */
10100
10101 /* Changes to or from Hyp via MSR and CPS are illegal. */
10102 if (write_type == CPSRWriteByInstr &&
10103 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
10104 mode == ARM_CPU_MODE_HYP)) {
10105 return 1;
10106 }
10107
10108 switch (mode) {
10109 case ARM_CPU_MODE_USR:
10110 return 0;
10111 case ARM_CPU_MODE_SYS:
10112 case ARM_CPU_MODE_SVC:
10113 case ARM_CPU_MODE_ABT:
10114 case ARM_CPU_MODE_UND:
10115 case ARM_CPU_MODE_IRQ:
10116 case ARM_CPU_MODE_FIQ:
10117 /*
10118 * Note that we don't implement the IMPDEF NSACR.RFR which in v7
10119 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
10120 */
10121 /*
10122 * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
10123 * and CPS are treated as illegal mode changes.
10124 */
10125 if (write_type == CPSRWriteByInstr &&
10126 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
10127 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10128 return 1;
10129 }
10130 return 0;
10131 case ARM_CPU_MODE_HYP:
10132 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
10133 case ARM_CPU_MODE_MON:
10134 return arm_current_el(env) < 3;
10135 default:
10136 return 1;
10137 }
10138 }
10139
10140 uint32_t cpsr_read(CPUARMState *env)
10141 {
10142 int ZF;
10143 ZF = (env->ZF == 0);
10144 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
10145 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
10146 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
10147 | ((env->condexec_bits & 0xfc) << 8)
10148 | (env->GE << 16) | (env->daif & CPSR_AIF);
10149 }
10150
10151 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
10152 CPSRWriteType write_type)
10153 {
10154 uint32_t changed_daif;
10155 bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
10156 (mask & (CPSR_M | CPSR_E | CPSR_IL));
10157
10158 if (mask & CPSR_NZCV) {
10159 env->ZF = (~val) & CPSR_Z;
10160 env->NF = val;
10161 env->CF = (val >> 29) & 1;
10162 env->VF = (val << 3) & 0x80000000;
10163 }
10164 if (mask & CPSR_Q) {
10165 env->QF = ((val & CPSR_Q) != 0);
10166 }
10167 if (mask & CPSR_T) {
10168 env->thumb = ((val & CPSR_T) != 0);
10169 }
10170 if (mask & CPSR_IT_0_1) {
10171 env->condexec_bits &= ~3;
10172 env->condexec_bits |= (val >> 25) & 3;
10173 }
10174 if (mask & CPSR_IT_2_7) {
10175 env->condexec_bits &= 3;
10176 env->condexec_bits |= (val >> 8) & 0xfc;
10177 }
10178 if (mask & CPSR_GE) {
10179 env->GE = (val >> 16) & 0xf;
10180 }
10181
10182 /*
10183 * In a V7 implementation that includes the security extensions but does
10184 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
10185 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
10186 * bits respectively.
10187 *
10188 * In a V8 implementation, it is permitted for privileged software to
10189 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
10190 */
10191 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
10192 arm_feature(env, ARM_FEATURE_EL3) &&
10193 !arm_feature(env, ARM_FEATURE_EL2) &&
10194 !arm_is_secure(env)) {
10195
10196 changed_daif = (env->daif ^ val) & mask;
10197
10198 if (changed_daif & CPSR_A) {
10199 /*
10200 * Check to see if we are allowed to change the masking of async
10201 * abort exceptions from a non-secure state.
10202 */
10203 if (!(env->cp15.scr_el3 & SCR_AW)) {
10204 qemu_log_mask(LOG_GUEST_ERROR,
10205 "Ignoring attempt to switch CPSR_A flag from "
10206 "non-secure world with SCR.AW bit clear\n");
10207 mask &= ~CPSR_A;
10208 }
10209 }
10210
10211 if (changed_daif & CPSR_F) {
10212 /*
10213 * Check to see if we are allowed to change the masking of FIQ
10214 * exceptions from a non-secure state.
10215 */
10216 if (!(env->cp15.scr_el3 & SCR_FW)) {
10217 qemu_log_mask(LOG_GUEST_ERROR,
10218 "Ignoring attempt to switch CPSR_F flag from "
10219 "non-secure world with SCR.FW bit clear\n");
10220 mask &= ~CPSR_F;
10221 }
10222
10223 /*
10224 * Check whether non-maskable FIQ (NMFI) support is enabled.
10225 * If this bit is set software is not allowed to mask
10226 * FIQs, but is allowed to set CPSR_F to 0.
10227 */
10228 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
10229 (val & CPSR_F)) {
10230 qemu_log_mask(LOG_GUEST_ERROR,
10231 "Ignoring attempt to enable CPSR_F flag "
10232 "(non-maskable FIQ [NMFI] support enabled)\n");
10233 mask &= ~CPSR_F;
10234 }
10235 }
10236 }
10237
10238 env->daif &= ~(CPSR_AIF & mask);
10239 env->daif |= val & CPSR_AIF & mask;
10240
10241 if (write_type != CPSRWriteRaw &&
10242 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
10243 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
10244 /*
10245 * Note that we can only get here in USR mode if this is a
10246 * gdb stub write; for this case we follow the architectural
10247 * behaviour for guest writes in USR mode of ignoring an attempt
10248 * to switch mode. (Those are caught by translate.c for writes
10249 * triggered by guest instructions.)
10250 */
10251 mask &= ~CPSR_M;
10252 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
10253 /*
10254 * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
10255 * v7, and has defined behaviour in v8:
10256 * + leave CPSR.M untouched
10257 * + allow changes to the other CPSR fields
10258 * + set PSTATE.IL
10259 * For user changes via the GDB stub, we don't set PSTATE.IL,
10260 * as this would be unnecessarily harsh for a user error.
10261 */
10262 mask &= ~CPSR_M;
10263 if (write_type != CPSRWriteByGDBStub &&
10264 arm_feature(env, ARM_FEATURE_V8)) {
10265 mask |= CPSR_IL;
10266 val |= CPSR_IL;
10267 }
10268 qemu_log_mask(LOG_GUEST_ERROR,
10269 "Illegal AArch32 mode switch attempt from %s to %s\n",
10270 aarch32_mode_name(env->uncached_cpsr),
10271 aarch32_mode_name(val));
10272 } else {
10273 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
10274 write_type == CPSRWriteExceptionReturn ?
10275 "Exception return from AArch32" :
10276 "AArch32 mode switch from",
10277 aarch32_mode_name(env->uncached_cpsr),
10278 aarch32_mode_name(val), env->regs[15]);
10279 switch_mode(env, val & CPSR_M);
10280 }
10281 }
10282 mask &= ~CACHED_CPSR_BITS;
10283 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
10284 if (tcg_enabled() && rebuild_hflags) {
10285 arm_rebuild_hflags(env);
10286 }
10287 }
10288
10289 #ifdef CONFIG_USER_ONLY
10290
10291 static void switch_mode(CPUARMState *env, int mode)
10292 {
10293 ARMCPU *cpu = env_archcpu(env);
10294
10295 if (mode != ARM_CPU_MODE_USR) {
10296 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
10297 }
10298 }
10299
10300 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10301 uint32_t cur_el, bool secure)
10302 {
10303 return 1;
10304 }
10305
10306 void aarch64_sync_64_to_32(CPUARMState *env)
10307 {
10308 g_assert_not_reached();
10309 }
10310
10311 #else
10312
10313 static void switch_mode(CPUARMState *env, int mode)
10314 {
10315 int old_mode;
10316 int i;
10317
10318 old_mode = env->uncached_cpsr & CPSR_M;
10319 if (mode == old_mode) {
10320 return;
10321 }
10322
10323 if (old_mode == ARM_CPU_MODE_FIQ) {
10324 memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
10325 memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
10326 } else if (mode == ARM_CPU_MODE_FIQ) {
10327 memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
10328 memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
10329 }
10330
10331 i = bank_number(old_mode);
10332 env->banked_r13[i] = env->regs[13];
10333 env->banked_spsr[i] = env->spsr;
10334
10335 i = bank_number(mode);
10336 env->regs[13] = env->banked_r13[i];
10337 env->spsr = env->banked_spsr[i];
10338
10339 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
10340 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
10341 }
10342
10343 /*
10344 * Physical Interrupt Target EL Lookup Table
10345 *
10346 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
10347 *
10348 * The below multi-dimensional table is used for looking up the target
10349 * exception level given numerous condition criteria. Specifically, the
10350 * target EL is based on SCR and HCR routing controls as well as the
10351 * currently executing EL and secure state.
10352 *
10353 * Dimensions:
10354 * target_el_table[2][2][2][2][2][4]
10355 * | | | | | +--- Current EL
10356 * | | | | +------ Non-secure(0)/Secure(1)
10357 * | | | +--------- HCR mask override
10358 * | | +------------ SCR exec state control
10359 * | +--------------- SCR mask override
10360 * +------------------ 32-bit(0)/64-bit(1) EL3
10361 *
10362 * The table values are as such:
10363 * 0-3 = EL0-EL3
10364 * -1 = Cannot occur
10365 *
10366 * The ARM ARM target EL table includes entries indicating that an "exception
10367 * is not taken". The two cases where this is applicable are:
10368 * 1) An exception is taken from EL3 but the SCR does not have the exception
10369 * routed to EL3.
10370 * 2) An exception is taken from EL2 but the HCR does not have the exception
10371 * routed to EL2.
10372 * In these two cases, the below table contain a target of EL1. This value is
10373 * returned as it is expected that the consumer of the table data will check
10374 * for "target EL >= current EL" to ensure the exception is not taken.
10375 *
10376 * SCR HCR
10377 * 64 EA AMO From
10378 * BIT IRQ IMO Non-secure Secure
10379 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
10380 */
10381 static const int8_t target_el_table[2][2][2][2][2][4] = {
10382 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
10383 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
10384 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
10385 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
10386 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
10387 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
10388 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
10389 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
10390 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
10391 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
10392 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
10393 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
10394 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
10395 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
10396 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
10397 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
10398 };
10399
10400 /*
10401 * Determine the target EL for physical exceptions
10402 */
10403 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10404 uint32_t cur_el, bool secure)
10405 {
10406 CPUARMState *env = cpu_env(cs);
10407 bool rw;
10408 bool scr;
10409 bool hcr;
10410 int target_el;
10411 /* Is the highest EL AArch64? */
10412 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
10413 uint64_t hcr_el2;
10414
10415 if (arm_feature(env, ARM_FEATURE_EL3)) {
10416 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
10417 } else {
10418 /*
10419 * Either EL2 is the highest EL (and so the EL2 register width
10420 * is given by is64); or there is no EL2 or EL3, in which case
10421 * the value of 'rw' does not affect the table lookup anyway.
10422 */
10423 rw = is64;
10424 }
10425
10426 hcr_el2 = arm_hcr_el2_eff(env);
10427 switch (excp_idx) {
10428 case EXCP_IRQ:
10429 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
10430 hcr = hcr_el2 & HCR_IMO;
10431 break;
10432 case EXCP_FIQ:
10433 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
10434 hcr = hcr_el2 & HCR_FMO;
10435 break;
10436 default:
10437 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
10438 hcr = hcr_el2 & HCR_AMO;
10439 break;
10440 };
10441
10442 /*
10443 * For these purposes, TGE and AMO/IMO/FMO both force the
10444 * interrupt to EL2. Fold TGE into the bit extracted above.
10445 */
10446 hcr |= (hcr_el2 & HCR_TGE) != 0;
10447
10448 /* Perform a table-lookup for the target EL given the current state */
10449 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
10450
10451 assert(target_el > 0);
10452
10453 return target_el;
10454 }
10455
10456 void arm_log_exception(CPUState *cs)
10457 {
10458 int idx = cs->exception_index;
10459
10460 if (qemu_loglevel_mask(CPU_LOG_INT)) {
10461 const char *exc = NULL;
10462 static const char * const excnames[] = {
10463 [EXCP_UDEF] = "Undefined Instruction",
10464 [EXCP_SWI] = "SVC",
10465 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
10466 [EXCP_DATA_ABORT] = "Data Abort",
10467 [EXCP_IRQ] = "IRQ",
10468 [EXCP_FIQ] = "FIQ",
10469 [EXCP_BKPT] = "Breakpoint",
10470 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
10471 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
10472 [EXCP_HVC] = "Hypervisor Call",
10473 [EXCP_HYP_TRAP] = "Hypervisor Trap",
10474 [EXCP_SMC] = "Secure Monitor Call",
10475 [EXCP_VIRQ] = "Virtual IRQ",
10476 [EXCP_VFIQ] = "Virtual FIQ",
10477 [EXCP_SEMIHOST] = "Semihosting call",
10478 [EXCP_NOCP] = "v7M NOCP UsageFault",
10479 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
10480 [EXCP_STKOF] = "v8M STKOF UsageFault",
10481 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
10482 [EXCP_LSERR] = "v8M LSERR UsageFault",
10483 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
10484 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
10485 [EXCP_VSERR] = "Virtual SERR",
10486 [EXCP_GPC] = "Granule Protection Check",
10487 };
10488
10489 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
10490 exc = excnames[idx];
10491 }
10492 if (!exc) {
10493 exc = "unknown";
10494 }
10495 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
10496 idx, exc, cs->cpu_index);
10497 }
10498 }
10499
10500 /*
10501 * Function used to synchronize QEMU's AArch64 register set with AArch32
10502 * register set. This is necessary when switching between AArch32 and AArch64
10503 * execution state.
10504 */
10505 void aarch64_sync_32_to_64(CPUARMState *env)
10506 {
10507 int i;
10508 uint32_t mode = env->uncached_cpsr & CPSR_M;
10509
10510 /* We can blanket copy R[0:7] to X[0:7] */
10511 for (i = 0; i < 8; i++) {
10512 env->xregs[i] = env->regs[i];
10513 }
10514
10515 /*
10516 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
10517 * Otherwise, they come from the banked user regs.
10518 */
10519 if (mode == ARM_CPU_MODE_FIQ) {
10520 for (i = 8; i < 13; i++) {
10521 env->xregs[i] = env->usr_regs[i - 8];
10522 }
10523 } else {
10524 for (i = 8; i < 13; i++) {
10525 env->xregs[i] = env->regs[i];
10526 }
10527 }
10528
10529 /*
10530 * Registers x13-x23 are the various mode SP and FP registers. Registers
10531 * r13 and r14 are only copied if we are in that mode, otherwise we copy
10532 * from the mode banked register.
10533 */
10534 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10535 env->xregs[13] = env->regs[13];
10536 env->xregs[14] = env->regs[14];
10537 } else {
10538 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
10539 /* HYP is an exception in that it is copied from r14 */
10540 if (mode == ARM_CPU_MODE_HYP) {
10541 env->xregs[14] = env->regs[14];
10542 } else {
10543 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
10544 }
10545 }
10546
10547 if (mode == ARM_CPU_MODE_HYP) {
10548 env->xregs[15] = env->regs[13];
10549 } else {
10550 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
10551 }
10552
10553 if (mode == ARM_CPU_MODE_IRQ) {
10554 env->xregs[16] = env->regs[14];
10555 env->xregs[17] = env->regs[13];
10556 } else {
10557 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
10558 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
10559 }
10560
10561 if (mode == ARM_CPU_MODE_SVC) {
10562 env->xregs[18] = env->regs[14];
10563 env->xregs[19] = env->regs[13];
10564 } else {
10565 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
10566 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
10567 }
10568
10569 if (mode == ARM_CPU_MODE_ABT) {
10570 env->xregs[20] = env->regs[14];
10571 env->xregs[21] = env->regs[13];
10572 } else {
10573 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10574 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10575 }
10576
10577 if (mode == ARM_CPU_MODE_UND) {
10578 env->xregs[22] = env->regs[14];
10579 env->xregs[23] = env->regs[13];
10580 } else {
10581 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10582 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
10583 }
10584
10585 /*
10586 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
10587 * mode, then we can copy from r8-r14. Otherwise, we copy from the
10588 * FIQ bank for r8-r14.
10589 */
10590 if (mode == ARM_CPU_MODE_FIQ) {
10591 for (i = 24; i < 31; i++) {
10592 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
10593 }
10594 } else {
10595 for (i = 24; i < 29; i++) {
10596 env->xregs[i] = env->fiq_regs[i - 24];
10597 }
10598 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10599 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
10600 }
10601
10602 env->pc = env->regs[15];
10603 }
10604
10605 /*
10606 * Function used to synchronize QEMU's AArch32 register set with AArch64
10607 * register set. This is necessary when switching between AArch32 and AArch64
10608 * execution state.
10609 */
10610 void aarch64_sync_64_to_32(CPUARMState *env)
10611 {
10612 int i;
10613 uint32_t mode = env->uncached_cpsr & CPSR_M;
10614
10615 /* We can blanket copy X[0:7] to R[0:7] */
10616 for (i = 0; i < 8; i++) {
10617 env->regs[i] = env->xregs[i];
10618 }
10619
10620 /*
10621 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10622 * Otherwise, we copy x8-x12 into the banked user regs.
10623 */
10624 if (mode == ARM_CPU_MODE_FIQ) {
10625 for (i = 8; i < 13; i++) {
10626 env->usr_regs[i - 8] = env->xregs[i];
10627 }
10628 } else {
10629 for (i = 8; i < 13; i++) {
10630 env->regs[i] = env->xregs[i];
10631 }
10632 }
10633
10634 /*
10635 * Registers r13 & r14 depend on the current mode.
10636 * If we are in a given mode, we copy the corresponding x registers to r13
10637 * and r14. Otherwise, we copy the x register to the banked r13 and r14
10638 * for the mode.
10639 */
10640 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10641 env->regs[13] = env->xregs[13];
10642 env->regs[14] = env->xregs[14];
10643 } else {
10644 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
10645
10646 /*
10647 * HYP is an exception in that it does not have its own banked r14 but
10648 * shares the USR r14
10649 */
10650 if (mode == ARM_CPU_MODE_HYP) {
10651 env->regs[14] = env->xregs[14];
10652 } else {
10653 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10654 }
10655 }
10656
10657 if (mode == ARM_CPU_MODE_HYP) {
10658 env->regs[13] = env->xregs[15];
10659 } else {
10660 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
10661 }
10662
10663 if (mode == ARM_CPU_MODE_IRQ) {
10664 env->regs[14] = env->xregs[16];
10665 env->regs[13] = env->xregs[17];
10666 } else {
10667 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10668 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
10669 }
10670
10671 if (mode == ARM_CPU_MODE_SVC) {
10672 env->regs[14] = env->xregs[18];
10673 env->regs[13] = env->xregs[19];
10674 } else {
10675 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10676 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
10677 }
10678
10679 if (mode == ARM_CPU_MODE_ABT) {
10680 env->regs[14] = env->xregs[20];
10681 env->regs[13] = env->xregs[21];
10682 } else {
10683 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10684 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
10685 }
10686
10687 if (mode == ARM_CPU_MODE_UND) {
10688 env->regs[14] = env->xregs[22];
10689 env->regs[13] = env->xregs[23];
10690 } else {
10691 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
10692 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
10693 }
10694
10695 /*
10696 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
10697 * mode, then we can copy to r8-r14. Otherwise, we copy to the
10698 * FIQ bank for r8-r14.
10699 */
10700 if (mode == ARM_CPU_MODE_FIQ) {
10701 for (i = 24; i < 31; i++) {
10702 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
10703 }
10704 } else {
10705 for (i = 24; i < 29; i++) {
10706 env->fiq_regs[i - 24] = env->xregs[i];
10707 }
10708 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
10709 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
10710 }
10711
10712 env->regs[15] = env->pc;
10713 }
10714
10715 static void take_aarch32_exception(CPUARMState *env, int new_mode,
10716 uint32_t mask, uint32_t offset,
10717 uint32_t newpc)
10718 {
10719 int new_el;
10720
10721 /* Change the CPU state so as to actually take the exception. */
10722 switch_mode(env, new_mode);
10723
10724 /*
10725 * For exceptions taken to AArch32 we must clear the SS bit in both
10726 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10727 */
10728 env->pstate &= ~PSTATE_SS;
10729 env->spsr = cpsr_read(env);
10730 /* Clear IT bits. */
10731 env->condexec_bits = 0;
10732 /* Switch to the new mode, and to the correct instruction set. */
10733 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
10734
10735 /* This must be after mode switching. */
10736 new_el = arm_current_el(env);
10737
10738 /* Set new mode endianness */
10739 env->uncached_cpsr &= ~CPSR_E;
10740 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
10741 env->uncached_cpsr |= CPSR_E;
10742 }
10743 /* J and IL must always be cleared for exception entry */
10744 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
10745 env->daif |= mask;
10746
10747 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10748 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10749 env->uncached_cpsr |= CPSR_SSBS;
10750 } else {
10751 env->uncached_cpsr &= ~CPSR_SSBS;
10752 }
10753 }
10754
10755 if (new_mode == ARM_CPU_MODE_HYP) {
10756 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10757 env->elr_el[2] = env->regs[15];
10758 } else {
10759 /* CPSR.PAN is normally preserved preserved unless... */
10760 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
10761 switch (new_el) {
10762 case 3:
10763 if (!arm_is_secure_below_el3(env)) {
10764 /* ... the target is EL3, from non-secure state. */
10765 env->uncached_cpsr &= ~CPSR_PAN;
10766 break;
10767 }
10768 /* ... the target is EL3, from secure state ... */
10769 /* fall through */
10770 case 1:
10771 /* ... the target is EL1 and SCTLR.SPAN is 0. */
10772 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10773 env->uncached_cpsr |= CPSR_PAN;
10774 }
10775 break;
10776 }
10777 }
10778 /*
10779 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10780 * and we should just guard the thumb mode on V4
10781 */
10782 if (arm_feature(env, ARM_FEATURE_V4T)) {
10783 env->thumb =
10784 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10785 }
10786 env->regs[14] = env->regs[15] + offset;
10787 }
10788 env->regs[15] = newpc;
10789
10790 if (tcg_enabled()) {
10791 arm_rebuild_hflags(env);
10792 }
10793 }
10794
10795 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10796 {
10797 /*
10798 * Handle exception entry to Hyp mode; this is sufficiently
10799 * different to entry to other AArch32 modes that we handle it
10800 * separately here.
10801 *
10802 * The vector table entry used is always the 0x14 Hyp mode entry point,
10803 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
10804 * The offset applied to the preferred return address is always zero
10805 * (see DDI0487C.a section G1.12.3).
10806 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10807 */
10808 uint32_t addr, mask;
10809 ARMCPU *cpu = ARM_CPU(cs);
10810 CPUARMState *env = &cpu->env;
10811
10812 switch (cs->exception_index) {
10813 case EXCP_UDEF:
10814 addr = 0x04;
10815 break;
10816 case EXCP_SWI:
10817 addr = 0x08;
10818 break;
10819 case EXCP_BKPT:
10820 /* Fall through to prefetch abort. */
10821 case EXCP_PREFETCH_ABORT:
10822 env->cp15.ifar_s = env->exception.vaddress;
10823 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10824 (uint32_t)env->exception.vaddress);
10825 addr = 0x0c;
10826 break;
10827 case EXCP_DATA_ABORT:
10828 env->cp15.dfar_s = env->exception.vaddress;
10829 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10830 (uint32_t)env->exception.vaddress);
10831 addr = 0x10;
10832 break;
10833 case EXCP_IRQ:
10834 addr = 0x18;
10835 break;
10836 case EXCP_FIQ:
10837 addr = 0x1c;
10838 break;
10839 case EXCP_HVC:
10840 addr = 0x08;
10841 break;
10842 case EXCP_HYP_TRAP:
10843 addr = 0x14;
10844 break;
10845 default:
10846 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10847 }
10848
10849 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
10850 if (!arm_feature(env, ARM_FEATURE_V8)) {
10851 /*
10852 * QEMU syndrome values are v8-style. v7 has the IL bit
10853 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10854 * If this is a v7 CPU, squash the IL bit in those cases.
10855 */
10856 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10857 (cs->exception_index == EXCP_DATA_ABORT &&
10858 !(env->exception.syndrome & ARM_EL_ISV)) ||
10859 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10860 env->exception.syndrome &= ~ARM_EL_IL;
10861 }
10862 }
10863 env->cp15.esr_el[2] = env->exception.syndrome;
10864 }
10865
10866 if (arm_current_el(env) != 2 && addr < 0x14) {
10867 addr = 0x14;
10868 }
10869
10870 mask = 0;
10871 if (!(env->cp15.scr_el3 & SCR_EA)) {
10872 mask |= CPSR_A;
10873 }
10874 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10875 mask |= CPSR_I;
10876 }
10877 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10878 mask |= CPSR_F;
10879 }
10880
10881 addr += env->cp15.hvbar;
10882
10883 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10884 }
10885
10886 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
10887 {
10888 ARMCPU *cpu = ARM_CPU(cs);
10889 CPUARMState *env = &cpu->env;
10890 uint32_t addr;
10891 uint32_t mask;
10892 int new_mode;
10893 uint32_t offset;
10894 uint32_t moe;
10895
10896 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
10897 switch (syn_get_ec(env->exception.syndrome)) {
10898 case EC_BREAKPOINT:
10899 case EC_BREAKPOINT_SAME_EL:
10900 moe = 1;
10901 break;
10902 case EC_WATCHPOINT:
10903 case EC_WATCHPOINT_SAME_EL:
10904 moe = 10;
10905 break;
10906 case EC_AA32_BKPT:
10907 moe = 3;
10908 break;
10909 case EC_VECTORCATCH:
10910 moe = 5;
10911 break;
10912 default:
10913 moe = 0;
10914 break;
10915 }
10916
10917 if (moe) {
10918 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10919 }
10920
10921 if (env->exception.target_el == 2) {
10922 arm_cpu_do_interrupt_aarch32_hyp(cs);
10923 return;
10924 }
10925
10926 switch (cs->exception_index) {
10927 case EXCP_UDEF:
10928 new_mode = ARM_CPU_MODE_UND;
10929 addr = 0x04;
10930 mask = CPSR_I;
10931 if (env->thumb) {
10932 offset = 2;
10933 } else {
10934 offset = 4;
10935 }
10936 break;
10937 case EXCP_SWI:
10938 new_mode = ARM_CPU_MODE_SVC;
10939 addr = 0x08;
10940 mask = CPSR_I;
10941 /* The PC already points to the next instruction. */
10942 offset = 0;
10943 break;
10944 case EXCP_BKPT:
10945 /* Fall through to prefetch abort. */
10946 case EXCP_PREFETCH_ABORT:
10947 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
10948 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
10949 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
10950 env->exception.fsr, (uint32_t)env->exception.vaddress);
10951 new_mode = ARM_CPU_MODE_ABT;
10952 addr = 0x0c;
10953 mask = CPSR_A | CPSR_I;
10954 offset = 4;
10955 break;
10956 case EXCP_DATA_ABORT:
10957 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10958 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
10959 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
10960 env->exception.fsr,
10961 (uint32_t)env->exception.vaddress);
10962 new_mode = ARM_CPU_MODE_ABT;
10963 addr = 0x10;
10964 mask = CPSR_A | CPSR_I;
10965 offset = 8;
10966 break;
10967 case EXCP_IRQ:
10968 new_mode = ARM_CPU_MODE_IRQ;
10969 addr = 0x18;
10970 /* Disable IRQ and imprecise data aborts. */
10971 mask = CPSR_A | CPSR_I;
10972 offset = 4;
10973 if (env->cp15.scr_el3 & SCR_IRQ) {
10974 /* IRQ routed to monitor mode */
10975 new_mode = ARM_CPU_MODE_MON;
10976 mask |= CPSR_F;
10977 }
10978 break;
10979 case EXCP_FIQ:
10980 new_mode = ARM_CPU_MODE_FIQ;
10981 addr = 0x1c;
10982 /* Disable FIQ, IRQ and imprecise data aborts. */
10983 mask = CPSR_A | CPSR_I | CPSR_F;
10984 if (env->cp15.scr_el3 & SCR_FIQ) {
10985 /* FIQ routed to monitor mode */
10986 new_mode = ARM_CPU_MODE_MON;
10987 }
10988 offset = 4;
10989 break;
10990 case EXCP_VIRQ:
10991 new_mode = ARM_CPU_MODE_IRQ;
10992 addr = 0x18;
10993 /* Disable IRQ and imprecise data aborts. */
10994 mask = CPSR_A | CPSR_I;
10995 offset = 4;
10996 break;
10997 case EXCP_VFIQ:
10998 new_mode = ARM_CPU_MODE_FIQ;
10999 addr = 0x1c;
11000 /* Disable FIQ, IRQ and imprecise data aborts. */
11001 mask = CPSR_A | CPSR_I | CPSR_F;
11002 offset = 4;
11003 break;
11004 case EXCP_VSERR:
11005 {
11006 /*
11007 * Note that this is reported as a data abort, but the DFAR
11008 * has an UNKNOWN value. Construct the SError syndrome from
11009 * AET and ExT fields.
11010 */
11011 ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
11012
11013 if (extended_addresses_enabled(env)) {
11014 env->exception.fsr = arm_fi_to_lfsc(&fi);
11015 } else {
11016 env->exception.fsr = arm_fi_to_sfsc(&fi);
11017 }
11018 env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
11019 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
11020 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
11021 env->exception.fsr);
11022
11023 new_mode = ARM_CPU_MODE_ABT;
11024 addr = 0x10;
11025 mask = CPSR_A | CPSR_I;
11026 offset = 8;
11027 }
11028 break;
11029 case EXCP_SMC:
11030 new_mode = ARM_CPU_MODE_MON;
11031 addr = 0x08;
11032 mask = CPSR_A | CPSR_I | CPSR_F;
11033 offset = 0;
11034 break;
11035 default:
11036 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11037 return; /* Never happens. Keep compiler happy. */
11038 }
11039
11040 if (new_mode == ARM_CPU_MODE_MON) {
11041 addr += env->cp15.mvbar;
11042 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
11043 /* High vectors. When enabled, base address cannot be remapped. */
11044 addr += 0xffff0000;
11045 } else {
11046 /*
11047 * ARM v7 architectures provide a vector base address register to remap
11048 * the interrupt vector table.
11049 * This register is only followed in non-monitor mode, and is banked.
11050 * Note: only bits 31:5 are valid.
11051 */
11052 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
11053 }
11054
11055 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
11056 env->cp15.scr_el3 &= ~SCR_NS;
11057 }
11058
11059 take_aarch32_exception(env, new_mode, mask, offset, addr);
11060 }
11061
11062 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
11063 {
11064 /*
11065 * Return the register number of the AArch64 view of the AArch32
11066 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
11067 * be that of the AArch32 mode the exception came from.
11068 */
11069 int mode = env->uncached_cpsr & CPSR_M;
11070
11071 switch (aarch32_reg) {
11072 case 0 ... 7:
11073 return aarch32_reg;
11074 case 8 ... 12:
11075 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
11076 case 13:
11077 switch (mode) {
11078 case ARM_CPU_MODE_USR:
11079 case ARM_CPU_MODE_SYS:
11080 return 13;
11081 case ARM_CPU_MODE_HYP:
11082 return 15;
11083 case ARM_CPU_MODE_IRQ:
11084 return 17;
11085 case ARM_CPU_MODE_SVC:
11086 return 19;
11087 case ARM_CPU_MODE_ABT:
11088 return 21;
11089 case ARM_CPU_MODE_UND:
11090 return 23;
11091 case ARM_CPU_MODE_FIQ:
11092 return 29;
11093 default:
11094 g_assert_not_reached();
11095 }
11096 case 14:
11097 switch (mode) {
11098 case ARM_CPU_MODE_USR:
11099 case ARM_CPU_MODE_SYS:
11100 case ARM_CPU_MODE_HYP:
11101 return 14;
11102 case ARM_CPU_MODE_IRQ:
11103 return 16;
11104 case ARM_CPU_MODE_SVC:
11105 return 18;
11106 case ARM_CPU_MODE_ABT:
11107 return 20;
11108 case ARM_CPU_MODE_UND:
11109 return 22;
11110 case ARM_CPU_MODE_FIQ:
11111 return 30;
11112 default:
11113 g_assert_not_reached();
11114 }
11115 case 15:
11116 return 31;
11117 default:
11118 g_assert_not_reached();
11119 }
11120 }
11121
11122 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
11123 {
11124 uint32_t ret = cpsr_read(env);
11125
11126 /* Move DIT to the correct location for SPSR_ELx */
11127 if (ret & CPSR_DIT) {
11128 ret &= ~CPSR_DIT;
11129 ret |= PSTATE_DIT;
11130 }
11131 /* Merge PSTATE.SS into SPSR_ELx */
11132 ret |= env->pstate & PSTATE_SS;
11133
11134 return ret;
11135 }
11136
11137 static bool syndrome_is_sync_extabt(uint32_t syndrome)
11138 {
11139 /* Return true if this syndrome value is a synchronous external abort */
11140 switch (syn_get_ec(syndrome)) {
11141 case EC_INSNABORT:
11142 case EC_INSNABORT_SAME_EL:
11143 case EC_DATAABORT:
11144 case EC_DATAABORT_SAME_EL:
11145 /* Look at fault status code for all the synchronous ext abort cases */
11146 switch (syndrome & 0x3f) {
11147 case 0x10:
11148 case 0x13:
11149 case 0x14:
11150 case 0x15:
11151 case 0x16:
11152 case 0x17:
11153 return true;
11154 default:
11155 return false;
11156 }
11157 default:
11158 return false;
11159 }
11160 }
11161
11162 /* Handle exception entry to a target EL which is using AArch64 */
11163 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
11164 {
11165 ARMCPU *cpu = ARM_CPU(cs);
11166 CPUARMState *env = &cpu->env;
11167 unsigned int new_el = env->exception.target_el;
11168 target_ulong addr = env->cp15.vbar_el[new_el];
11169 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
11170 unsigned int old_mode;
11171 unsigned int cur_el = arm_current_el(env);
11172 int rt;
11173
11174 if (tcg_enabled()) {
11175 /*
11176 * Note that new_el can never be 0. If cur_el is 0, then
11177 * el0_a64 is is_a64(), else el0_a64 is ignored.
11178 */
11179 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
11180 }
11181
11182 if (cur_el < new_el) {
11183 /*
11184 * Entry vector offset depends on whether the implemented EL
11185 * immediately lower than the target level is using AArch32 or AArch64
11186 */
11187 bool is_aa64;
11188 uint64_t hcr;
11189
11190 switch (new_el) {
11191 case 3:
11192 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
11193 break;
11194 case 2:
11195 hcr = arm_hcr_el2_eff(env);
11196 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11197 is_aa64 = (hcr & HCR_RW) != 0;
11198 break;
11199 }
11200 /* fall through */
11201 case 1:
11202 is_aa64 = is_a64(env);
11203 break;
11204 default:
11205 g_assert_not_reached();
11206 }
11207
11208 if (is_aa64) {
11209 addr += 0x400;
11210 } else {
11211 addr += 0x600;
11212 }
11213 } else if (pstate_read(env) & PSTATE_SP) {
11214 addr += 0x200;
11215 }
11216
11217 switch (cs->exception_index) {
11218 case EXCP_GPC:
11219 qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
11220 env->cp15.mfar_el3);
11221 /* fall through */
11222 case EXCP_PREFETCH_ABORT:
11223 case EXCP_DATA_ABORT:
11224 /*
11225 * FEAT_DoubleFault allows synchronous external aborts taken to EL3
11226 * to be taken to the SError vector entrypoint.
11227 */
11228 if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
11229 syndrome_is_sync_extabt(env->exception.syndrome)) {
11230 addr += 0x180;
11231 }
11232 env->cp15.far_el[new_el] = env->exception.vaddress;
11233 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
11234 env->cp15.far_el[new_el]);
11235 /* fall through */
11236 case EXCP_BKPT:
11237 case EXCP_UDEF:
11238 case EXCP_SWI:
11239 case EXCP_HVC:
11240 case EXCP_HYP_TRAP:
11241 case EXCP_SMC:
11242 switch (syn_get_ec(env->exception.syndrome)) {
11243 case EC_ADVSIMDFPACCESSTRAP:
11244 /*
11245 * QEMU internal FP/SIMD syndromes from AArch32 include the
11246 * TA and coproc fields which are only exposed if the exception
11247 * is taken to AArch32 Hyp mode. Mask them out to get a valid
11248 * AArch64 format syndrome.
11249 */
11250 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
11251 break;
11252 case EC_CP14RTTRAP:
11253 case EC_CP15RTTRAP:
11254 case EC_CP14DTTRAP:
11255 /*
11256 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
11257 * the raw register field from the insn; when taking this to
11258 * AArch64 we must convert it to the AArch64 view of the register
11259 * number. Notice that we read a 4-bit AArch32 register number and
11260 * write back a 5-bit AArch64 one.
11261 */
11262 rt = extract32(env->exception.syndrome, 5, 4);
11263 rt = aarch64_regnum(env, rt);
11264 env->exception.syndrome = deposit32(env->exception.syndrome,
11265 5, 5, rt);
11266 break;
11267 case EC_CP15RRTTRAP:
11268 case EC_CP14RRTTRAP:
11269 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
11270 rt = extract32(env->exception.syndrome, 5, 4);
11271 rt = aarch64_regnum(env, rt);
11272 env->exception.syndrome = deposit32(env->exception.syndrome,
11273 5, 5, rt);
11274 rt = extract32(env->exception.syndrome, 10, 4);
11275 rt = aarch64_regnum(env, rt);
11276 env->exception.syndrome = deposit32(env->exception.syndrome,
11277 10, 5, rt);
11278 break;
11279 }
11280 env->cp15.esr_el[new_el] = env->exception.syndrome;
11281 break;
11282 case EXCP_IRQ:
11283 case EXCP_VIRQ:
11284 addr += 0x80;
11285 break;
11286 case EXCP_FIQ:
11287 case EXCP_VFIQ:
11288 addr += 0x100;
11289 break;
11290 case EXCP_VSERR:
11291 addr += 0x180;
11292 /* Construct the SError syndrome from IDS and ISS fields. */
11293 env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
11294 env->cp15.esr_el[new_el] = env->exception.syndrome;
11295 break;
11296 default:
11297 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11298 }
11299
11300 if (is_a64(env)) {
11301 old_mode = pstate_read(env);
11302 aarch64_save_sp(env, arm_current_el(env));
11303 env->elr_el[new_el] = env->pc;
11304
11305 if (cur_el == 1 && new_el == 1 &&
11306 ((arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1)) == HCR_NV)) {
11307 /* I_ZJRNN: report EL2 in the SPSR by setting M[3:2] to 0b10 */
11308 old_mode = deposit32(old_mode, 2, 2, 2);
11309 }
11310 } else {
11311 old_mode = cpsr_read_for_spsr_elx(env);
11312 env->elr_el[new_el] = env->regs[15];
11313
11314 aarch64_sync_32_to_64(env);
11315
11316 env->condexec_bits = 0;
11317 }
11318 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
11319
11320 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
11321 env->elr_el[new_el]);
11322
11323 if (cpu_isar_feature(aa64_pan, cpu)) {
11324 /* The value of PSTATE.PAN is normally preserved, except when ... */
11325 new_mode |= old_mode & PSTATE_PAN;
11326 switch (new_el) {
11327 case 2:
11328 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
11329 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
11330 != (HCR_E2H | HCR_TGE)) {
11331 break;
11332 }
11333 /* fall through */
11334 case 1:
11335 /* ... the target is EL1 ... */
11336 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
11337 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
11338 new_mode |= PSTATE_PAN;
11339 }
11340 break;
11341 }
11342 }
11343 if (cpu_isar_feature(aa64_mte, cpu)) {
11344 new_mode |= PSTATE_TCO;
11345 }
11346
11347 if (cpu_isar_feature(aa64_ssbs, cpu)) {
11348 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
11349 new_mode |= PSTATE_SSBS;
11350 } else {
11351 new_mode &= ~PSTATE_SSBS;
11352 }
11353 }
11354
11355 pstate_write(env, PSTATE_DAIF | new_mode);
11356 env->aarch64 = true;
11357 aarch64_restore_sp(env, new_el);
11358
11359 if (tcg_enabled()) {
11360 helper_rebuild_hflags_a64(env, new_el);
11361 }
11362
11363 env->pc = addr;
11364
11365 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
11366 new_el, env->pc, pstate_read(env));
11367 }
11368
11369 /*
11370 * Do semihosting call and set the appropriate return value. All the
11371 * permission and validity checks have been done at translate time.
11372 *
11373 * We only see semihosting exceptions in TCG only as they are not
11374 * trapped to the hypervisor in KVM.
11375 */
11376 #ifdef CONFIG_TCG
11377 static void tcg_handle_semihosting(CPUState *cs)
11378 {
11379 ARMCPU *cpu = ARM_CPU(cs);
11380 CPUARMState *env = &cpu->env;
11381
11382 if (is_a64(env)) {
11383 qemu_log_mask(CPU_LOG_INT,
11384 "...handling as semihosting call 0x%" PRIx64 "\n",
11385 env->xregs[0]);
11386 do_common_semihosting(cs);
11387 env->pc += 4;
11388 } else {
11389 qemu_log_mask(CPU_LOG_INT,
11390 "...handling as semihosting call 0x%x\n",
11391 env->regs[0]);
11392 do_common_semihosting(cs);
11393 env->regs[15] += env->thumb ? 2 : 4;
11394 }
11395 }
11396 #endif
11397
11398 /*
11399 * Handle a CPU exception for A and R profile CPUs.
11400 * Do any appropriate logging, handle PSCI calls, and then hand off
11401 * to the AArch64-entry or AArch32-entry function depending on the
11402 * target exception level's register width.
11403 *
11404 * Note: this is used for both TCG (as the do_interrupt tcg op),
11405 * and KVM to re-inject guest debug exceptions, and to
11406 * inject a Synchronous-External-Abort.
11407 */
11408 void arm_cpu_do_interrupt(CPUState *cs)
11409 {
11410 ARMCPU *cpu = ARM_CPU(cs);
11411 CPUARMState *env = &cpu->env;
11412 unsigned int new_el = env->exception.target_el;
11413
11414 assert(!arm_feature(env, ARM_FEATURE_M));
11415
11416 arm_log_exception(cs);
11417 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
11418 new_el);
11419 if (qemu_loglevel_mask(CPU_LOG_INT)
11420 && !excp_is_internal(cs->exception_index)) {
11421 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
11422 syn_get_ec(env->exception.syndrome),
11423 env->exception.syndrome);
11424 }
11425
11426 if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
11427 arm_handle_psci_call(cpu);
11428 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
11429 return;
11430 }
11431
11432 /*
11433 * Semihosting semantics depend on the register width of the code
11434 * that caused the exception, not the target exception level, so
11435 * must be handled here.
11436 */
11437 #ifdef CONFIG_TCG
11438 if (cs->exception_index == EXCP_SEMIHOST) {
11439 tcg_handle_semihosting(cs);
11440 return;
11441 }
11442 #endif
11443
11444 /*
11445 * Hooks may change global state so BQL should be held, also the
11446 * BQL needs to be held for any modification of
11447 * cs->interrupt_request.
11448 */
11449 g_assert(bql_locked());
11450
11451 arm_call_pre_el_change_hook(cpu);
11452
11453 assert(!excp_is_internal(cs->exception_index));
11454 if (arm_el_is_aa64(env, new_el)) {
11455 arm_cpu_do_interrupt_aarch64(cs);
11456 } else {
11457 arm_cpu_do_interrupt_aarch32(cs);
11458 }
11459
11460 arm_call_el_change_hook(cpu);
11461
11462 if (!kvm_enabled()) {
11463 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
11464 }
11465 }
11466 #endif /* !CONFIG_USER_ONLY */
11467
11468 uint64_t arm_sctlr(CPUARMState *env, int el)
11469 {
11470 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
11471 if (el == 0) {
11472 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
11473 el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
11474 }
11475 return env->cp15.sctlr_el[el];
11476 }
11477
11478 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
11479 {
11480 if (regime_has_2_ranges(mmu_idx)) {
11481 return extract64(tcr, 37, 2);
11482 } else if (regime_is_stage2(mmu_idx)) {
11483 return 0; /* VTCR_EL2 */
11484 } else {
11485 /* Replicate the single TBI bit so we always have 2 bits. */
11486 return extract32(tcr, 20, 1) * 3;
11487 }
11488 }
11489
11490 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
11491 {
11492 if (regime_has_2_ranges(mmu_idx)) {
11493 return extract64(tcr, 51, 2);
11494 } else if (regime_is_stage2(mmu_idx)) {
11495 return 0; /* VTCR_EL2 */
11496 } else {
11497 /* Replicate the single TBID bit so we always have 2 bits. */
11498 return extract32(tcr, 29, 1) * 3;
11499 }
11500 }
11501
11502 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
11503 {
11504 if (regime_has_2_ranges(mmu_idx)) {
11505 return extract64(tcr, 57, 2);
11506 } else {
11507 /* Replicate the single TCMA bit so we always have 2 bits. */
11508 return extract32(tcr, 30, 1) * 3;
11509 }
11510 }
11511
11512 static ARMGranuleSize tg0_to_gran_size(int tg)
11513 {
11514 switch (tg) {
11515 case 0:
11516 return Gran4K;
11517 case 1:
11518 return Gran64K;
11519 case 2:
11520 return Gran16K;
11521 default:
11522 return GranInvalid;
11523 }
11524 }
11525
11526 static ARMGranuleSize tg1_to_gran_size(int tg)
11527 {
11528 switch (tg) {
11529 case 1:
11530 return Gran16K;
11531 case 2:
11532 return Gran4K;
11533 case 3:
11534 return Gran64K;
11535 default:
11536 return GranInvalid;
11537 }
11538 }
11539
11540 static inline bool have4k(ARMCPU *cpu, bool stage2)
11541 {
11542 return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
11543 : cpu_isar_feature(aa64_tgran4, cpu);
11544 }
11545
11546 static inline bool have16k(ARMCPU *cpu, bool stage2)
11547 {
11548 return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
11549 : cpu_isar_feature(aa64_tgran16, cpu);
11550 }
11551
11552 static inline bool have64k(ARMCPU *cpu, bool stage2)
11553 {
11554 return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
11555 : cpu_isar_feature(aa64_tgran64, cpu);
11556 }
11557
11558 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
11559 bool stage2)
11560 {
11561 switch (gran) {
11562 case Gran4K:
11563 if (have4k(cpu, stage2)) {
11564 return gran;
11565 }
11566 break;
11567 case Gran16K:
11568 if (have16k(cpu, stage2)) {
11569 return gran;
11570 }
11571 break;
11572 case Gran64K:
11573 if (have64k(cpu, stage2)) {
11574 return gran;
11575 }
11576 break;
11577 case GranInvalid:
11578 break;
11579 }
11580 /*
11581 * If the guest selects a granule size that isn't implemented,
11582 * the architecture requires that we behave as if it selected one
11583 * that is (with an IMPDEF choice of which one to pick). We choose
11584 * to implement the smallest supported granule size.
11585 */
11586 if (have4k(cpu, stage2)) {
11587 return Gran4K;
11588 }
11589 if (have16k(cpu, stage2)) {
11590 return Gran16K;
11591 }
11592 assert(have64k(cpu, stage2));
11593 return Gran64K;
11594 }
11595
11596 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11597 ARMMMUIdx mmu_idx, bool data,
11598 bool el1_is_aa32)
11599 {
11600 uint64_t tcr = regime_tcr(env, mmu_idx);
11601 bool epd, hpd, tsz_oob, ds, ha, hd;
11602 int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11603 ARMGranuleSize gran;
11604 ARMCPU *cpu = env_archcpu(env);
11605 bool stage2 = regime_is_stage2(mmu_idx);
11606
11607 if (!regime_has_2_ranges(mmu_idx)) {
11608 select = 0;
11609 tsz = extract32(tcr, 0, 6);
11610 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11611 if (stage2) {
11612 /* VTCR_EL2 */
11613 hpd = false;
11614 } else {
11615 hpd = extract32(tcr, 24, 1);
11616 }
11617 epd = false;
11618 sh = extract32(tcr, 12, 2);
11619 ps = extract32(tcr, 16, 3);
11620 ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11621 hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11622 ds = extract64(tcr, 32, 1);
11623 } else {
11624 bool e0pd;
11625
11626 /*
11627 * Bit 55 is always between the two regions, and is canonical for
11628 * determining if address tagging is enabled.
11629 */
11630 select = extract64(va, 55, 1);
11631 if (!select) {
11632 tsz = extract32(tcr, 0, 6);
11633 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11634 epd = extract32(tcr, 7, 1);
11635 sh = extract32(tcr, 12, 2);
11636 hpd = extract64(tcr, 41, 1);
11637 e0pd = extract64(tcr, 55, 1);
11638 } else {
11639 tsz = extract32(tcr, 16, 6);
11640 gran = tg1_to_gran_size(extract32(tcr, 30, 2));
11641 epd = extract32(tcr, 23, 1);
11642 sh = extract32(tcr, 28, 2);
11643 hpd = extract64(tcr, 42, 1);
11644 e0pd = extract64(tcr, 56, 1);
11645 }
11646 ps = extract64(tcr, 32, 3);
11647 ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11648 hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11649 ds = extract64(tcr, 59, 1);
11650
11651 if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11652 regime_is_user(env, mmu_idx)) {
11653 epd = true;
11654 }
11655 }
11656
11657 gran = sanitize_gran_size(cpu, gran, stage2);
11658
11659 if (cpu_isar_feature(aa64_st, cpu)) {
11660 max_tsz = 48 - (gran == Gran64K);
11661 } else {
11662 max_tsz = 39;
11663 }
11664
11665 /*
11666 * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11667 * adjust the effective value of DS, as documented.
11668 */
11669 min_tsz = 16;
11670 if (gran == Gran64K) {
11671 if (cpu_isar_feature(aa64_lva, cpu)) {
11672 min_tsz = 12;
11673 }
11674 ds = false;
11675 } else if (ds) {
11676 if (regime_is_stage2(mmu_idx)) {
11677 if (gran == Gran16K) {
11678 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11679 } else {
11680 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11681 }
11682 } else {
11683 if (gran == Gran16K) {
11684 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11685 } else {
11686 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11687 }
11688 }
11689 if (ds) {
11690 min_tsz = 12;
11691 }
11692 }
11693
11694 if (stage2 && el1_is_aa32) {
11695 /*
11696 * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
11697 * are loosened: a configured IPA of 40 bits is permitted even if
11698 * the implemented PA is less than that (and so a 40 bit IPA would
11699 * fault for an AArch64 EL1). See R_DTLMN.
11700 */
11701 min_tsz = MIN(min_tsz, 24);
11702 }
11703
11704 if (tsz > max_tsz) {
11705 tsz = max_tsz;
11706 tsz_oob = true;
11707 } else if (tsz < min_tsz) {
11708 tsz = min_tsz;
11709 tsz_oob = true;
11710 } else {
11711 tsz_oob = false;
11712 }
11713
11714 /* Present TBI as a composite with TBID. */
11715 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11716 if (!data) {
11717 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11718 }
11719 tbi = (tbi >> select) & 1;
11720
11721 return (ARMVAParameters) {
11722 .tsz = tsz,
11723 .ps = ps,
11724 .sh = sh,
11725 .select = select,
11726 .tbi = tbi,
11727 .epd = epd,
11728 .hpd = hpd,
11729 .tsz_oob = tsz_oob,
11730 .ds = ds,
11731 .ha = ha,
11732 .hd = ha && hd,
11733 .gran = gran,
11734 };
11735 }
11736
11737 /*
11738 * Note that signed overflow is undefined in C. The following routines are
11739 * careful to use unsigned types where modulo arithmetic is required.
11740 * Failure to do so _will_ break on newer gcc.
11741 */
11742
11743 /* Signed saturating arithmetic. */
11744
11745 /* Perform 16-bit signed saturating addition. */
11746 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11747 {
11748 uint16_t res;
11749
11750 res = a + b;
11751 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
11752 if (a & 0x8000) {
11753 res = 0x8000;
11754 } else {
11755 res = 0x7fff;
11756 }
11757 }
11758 return res;
11759 }
11760
11761 /* Perform 8-bit signed saturating addition. */
11762 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11763 {
11764 uint8_t res;
11765
11766 res = a + b;
11767 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
11768 if (a & 0x80) {
11769 res = 0x80;
11770 } else {
11771 res = 0x7f;
11772 }
11773 }
11774 return res;
11775 }
11776
11777 /* Perform 16-bit signed saturating subtraction. */
11778 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11779 {
11780 uint16_t res;
11781
11782 res = a - b;
11783 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
11784 if (a & 0x8000) {
11785 res = 0x8000;
11786 } else {
11787 res = 0x7fff;
11788 }
11789 }
11790 return res;
11791 }
11792
11793 /* Perform 8-bit signed saturating subtraction. */
11794 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11795 {
11796 uint8_t res;
11797
11798 res = a - b;
11799 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
11800 if (a & 0x80) {
11801 res = 0x80;
11802 } else {
11803 res = 0x7f;
11804 }
11805 }
11806 return res;
11807 }
11808
11809 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11810 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11811 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
11812 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
11813 #define PFX q
11814
11815 #include "op_addsub.h"
11816
11817 /* Unsigned saturating arithmetic. */
11818 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
11819 {
11820 uint16_t res;
11821 res = a + b;
11822 if (res < a) {
11823 res = 0xffff;
11824 }
11825 return res;
11826 }
11827
11828 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
11829 {
11830 if (a > b) {
11831 return a - b;
11832 } else {
11833 return 0;
11834 }
11835 }
11836
11837 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11838 {
11839 uint8_t res;
11840 res = a + b;
11841 if (res < a) {
11842 res = 0xff;
11843 }
11844 return res;
11845 }
11846
11847 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11848 {
11849 if (a > b) {
11850 return a - b;
11851 } else {
11852 return 0;
11853 }
11854 }
11855
11856 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11857 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11858 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
11859 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
11860 #define PFX uq
11861
11862 #include "op_addsub.h"
11863
11864 /* Signed modulo arithmetic. */
11865 #define SARITH16(a, b, n, op) do { \
11866 int32_t sum; \
11867 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
11868 RESULT(sum, n, 16); \
11869 if (sum >= 0) \
11870 ge |= 3 << (n * 2); \
11871 } while (0)
11872
11873 #define SARITH8(a, b, n, op) do { \
11874 int32_t sum; \
11875 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
11876 RESULT(sum, n, 8); \
11877 if (sum >= 0) \
11878 ge |= 1 << n; \
11879 } while (0)
11880
11881
11882 #define ADD16(a, b, n) SARITH16(a, b, n, +)
11883 #define SUB16(a, b, n) SARITH16(a, b, n, -)
11884 #define ADD8(a, b, n) SARITH8(a, b, n, +)
11885 #define SUB8(a, b, n) SARITH8(a, b, n, -)
11886 #define PFX s
11887 #define ARITH_GE
11888
11889 #include "op_addsub.h"
11890
11891 /* Unsigned modulo arithmetic. */
11892 #define ADD16(a, b, n) do { \
11893 uint32_t sum; \
11894 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11895 RESULT(sum, n, 16); \
11896 if ((sum >> 16) == 1) \
11897 ge |= 3 << (n * 2); \
11898 } while (0)
11899
11900 #define ADD8(a, b, n) do { \
11901 uint32_t sum; \
11902 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11903 RESULT(sum, n, 8); \
11904 if ((sum >> 8) == 1) \
11905 ge |= 1 << n; \
11906 } while (0)
11907
11908 #define SUB16(a, b, n) do { \
11909 uint32_t sum; \
11910 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11911 RESULT(sum, n, 16); \
11912 if ((sum >> 16) == 0) \
11913 ge |= 3 << (n * 2); \
11914 } while (0)
11915
11916 #define SUB8(a, b, n) do { \
11917 uint32_t sum; \
11918 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11919 RESULT(sum, n, 8); \
11920 if ((sum >> 8) == 0) \
11921 ge |= 1 << n; \
11922 } while (0)
11923
11924 #define PFX u
11925 #define ARITH_GE
11926
11927 #include "op_addsub.h"
11928
11929 /* Halved signed arithmetic. */
11930 #define ADD16(a, b, n) \
11931 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11932 #define SUB16(a, b, n) \
11933 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11934 #define ADD8(a, b, n) \
11935 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11936 #define SUB8(a, b, n) \
11937 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11938 #define PFX sh
11939
11940 #include "op_addsub.h"
11941
11942 /* Halved unsigned arithmetic. */
11943 #define ADD16(a, b, n) \
11944 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11945 #define SUB16(a, b, n) \
11946 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11947 #define ADD8(a, b, n) \
11948 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11949 #define SUB8(a, b, n) \
11950 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11951 #define PFX uh
11952
11953 #include "op_addsub.h"
11954
11955 static inline uint8_t do_usad(uint8_t a, uint8_t b)
11956 {
11957 if (a > b) {
11958 return a - b;
11959 } else {
11960 return b - a;
11961 }
11962 }
11963
11964 /* Unsigned sum of absolute byte differences. */
11965 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11966 {
11967 uint32_t sum;
11968 sum = do_usad(a, b);
11969 sum += do_usad(a >> 8, b >> 8);
11970 sum += do_usad(a >> 16, b >> 16);
11971 sum += do_usad(a >> 24, b >> 24);
11972 return sum;
11973 }
11974
11975 /* For ARMv6 SEL instruction. */
11976 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11977 {
11978 uint32_t mask;
11979
11980 mask = 0;
11981 if (flags & 1) {
11982 mask |= 0xff;
11983 }
11984 if (flags & 2) {
11985 mask |= 0xff00;
11986 }
11987 if (flags & 4) {
11988 mask |= 0xff0000;
11989 }
11990 if (flags & 8) {
11991 mask |= 0xff000000;
11992 }
11993 return (a & mask) | (b & ~mask);
11994 }
11995
11996 /*
11997 * CRC helpers.
11998 * The upper bytes of val (above the number specified by 'bytes') must have
11999 * been zeroed out by the caller.
12000 */
12001 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12002 {
12003 uint8_t buf[4];
12004
12005 stl_le_p(buf, val);
12006
12007 /* zlib crc32 converts the accumulator and output to one's complement. */
12008 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12009 }
12010
12011 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12012 {
12013 uint8_t buf[4];
12014
12015 stl_le_p(buf, val);
12016
12017 /* Linux crc32c converts the output to one's complement. */
12018 return crc32c(acc, buf, bytes) ^ 0xffffffff;
12019 }
12020
12021 /*
12022 * Return the exception level to which FP-disabled exceptions should
12023 * be taken, or 0 if FP is enabled.
12024 */
12025 int fp_exception_el(CPUARMState *env, int cur_el)
12026 {
12027 #ifndef CONFIG_USER_ONLY
12028 uint64_t hcr_el2;
12029
12030 /*
12031 * CPACR and the CPTR registers don't exist before v6, so FP is
12032 * always accessible
12033 */
12034 if (!arm_feature(env, ARM_FEATURE_V6)) {
12035 return 0;
12036 }
12037
12038 if (arm_feature(env, ARM_FEATURE_M)) {
12039 /* CPACR can cause a NOCP UsageFault taken to current security state */
12040 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
12041 return 1;
12042 }
12043
12044 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
12045 if (!extract32(env->v7m.nsacr, 10, 1)) {
12046 /* FP insns cause a NOCP UsageFault taken to Secure */
12047 return 3;
12048 }
12049 }
12050
12051 return 0;
12052 }
12053
12054 hcr_el2 = arm_hcr_el2_eff(env);
12055
12056 /*
12057 * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12058 * 0, 2 : trap EL0 and EL1/PL1 accesses
12059 * 1 : trap only EL0 accesses
12060 * 3 : trap no accesses
12061 * This register is ignored if E2H+TGE are both set.
12062 */
12063 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12064 int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
12065
12066 switch (fpen) {
12067 case 1:
12068 if (cur_el != 0) {
12069 break;
12070 }
12071 /* fall through */
12072 case 0:
12073 case 2:
12074 /* Trap from Secure PL0 or PL1 to Secure PL1. */
12075 if (!arm_el_is_aa64(env, 3)
12076 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
12077 return 3;
12078 }
12079 if (cur_el <= 1) {
12080 return 1;
12081 }
12082 break;
12083 }
12084 }
12085
12086 /*
12087 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
12088 * to control non-secure access to the FPU. It doesn't have any
12089 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
12090 */
12091 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
12092 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
12093 if (!extract32(env->cp15.nsacr, 10, 1)) {
12094 /* FP insns act as UNDEF */
12095 return cur_el == 2 ? 2 : 1;
12096 }
12097 }
12098
12099 /*
12100 * CPTR_EL2 is present in v7VE or v8, and changes format
12101 * with HCR_EL2.E2H (regardless of TGE).
12102 */
12103 if (cur_el <= 2) {
12104 if (hcr_el2 & HCR_E2H) {
12105 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
12106 case 1:
12107 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
12108 break;
12109 }
12110 /* fall through */
12111 case 0:
12112 case 2:
12113 return 2;
12114 }
12115 } else if (arm_is_el2_enabled(env)) {
12116 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
12117 return 2;
12118 }
12119 }
12120 }
12121
12122 /* CPTR_EL3 : present in v8 */
12123 if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
12124 /* Trap all FP ops to EL3 */
12125 return 3;
12126 }
12127 #endif
12128 return 0;
12129 }
12130
12131 /* Return the exception level we're running at if this is our mmu_idx */
12132 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12133 {
12134 if (mmu_idx & ARM_MMU_IDX_M) {
12135 return mmu_idx & ARM_MMU_IDX_M_PRIV;
12136 }
12137
12138 switch (mmu_idx) {
12139 case ARMMMUIdx_E10_0:
12140 case ARMMMUIdx_E20_0:
12141 return 0;
12142 case ARMMMUIdx_E10_1:
12143 case ARMMMUIdx_E10_1_PAN:
12144 return 1;
12145 case ARMMMUIdx_E2:
12146 case ARMMMUIdx_E20_2:
12147 case ARMMMUIdx_E20_2_PAN:
12148 return 2;
12149 case ARMMMUIdx_E3:
12150 return 3;
12151 default:
12152 g_assert_not_reached();
12153 }
12154 }
12155
12156 #ifndef CONFIG_TCG
12157 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12158 {
12159 g_assert_not_reached();
12160 }
12161 #endif
12162
12163 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
12164 {
12165 ARMMMUIdx idx;
12166 uint64_t hcr;
12167
12168 if (arm_feature(env, ARM_FEATURE_M)) {
12169 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
12170 }
12171
12172 /* See ARM pseudo-function ELIsInHost. */
12173 switch (el) {
12174 case 0:
12175 hcr = arm_hcr_el2_eff(env);
12176 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
12177 idx = ARMMMUIdx_E20_0;
12178 } else {
12179 idx = ARMMMUIdx_E10_0;
12180 }
12181 break;
12182 case 1:
12183 if (arm_pan_enabled(env)) {
12184 idx = ARMMMUIdx_E10_1_PAN;
12185 } else {
12186 idx = ARMMMUIdx_E10_1;
12187 }
12188 break;
12189 case 2:
12190 /* Note that TGE does not apply at EL2. */
12191 if (arm_hcr_el2_eff(env) & HCR_E2H) {
12192 if (arm_pan_enabled(env)) {
12193 idx = ARMMMUIdx_E20_2_PAN;
12194 } else {
12195 idx = ARMMMUIdx_E20_2;
12196 }
12197 } else {
12198 idx = ARMMMUIdx_E2;
12199 }
12200 break;
12201 case 3:
12202 return ARMMMUIdx_E3;
12203 default:
12204 g_assert_not_reached();
12205 }
12206
12207 return idx;
12208 }
12209
12210 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12211 {
12212 return arm_mmu_idx_el(env, arm_current_el(env));
12213 }
12214
12215 static bool mve_no_pred(CPUARMState *env)
12216 {
12217 /*
12218 * Return true if there is definitely no predication of MVE
12219 * instructions by VPR or LTPSIZE. (Returning false even if there
12220 * isn't any predication is OK; generated code will just be
12221 * a little worse.)
12222 * If the CPU does not implement MVE then this TB flag is always 0.
12223 *
12224 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
12225 * logic in gen_update_fp_context() needs to be updated to match.
12226 *
12227 * We do not include the effect of the ECI bits here -- they are
12228 * tracked in other TB flags. This simplifies the logic for
12229 * "when did we emit code that changes the MVE_NO_PRED TB flag
12230 * and thus need to end the TB?".
12231 */
12232 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
12233 return false;
12234 }
12235 if (env->v7m.vpr) {
12236 return false;
12237 }
12238 if (env->v7m.ltpsize < 4) {
12239 return false;
12240 }
12241 return true;
12242 }
12243
12244 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
12245 uint64_t *cs_base, uint32_t *pflags)
12246 {
12247 CPUARMTBFlags flags;
12248
12249 assert_hflags_rebuild_correctly(env);
12250 flags = env->hflags;
12251
12252 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
12253 *pc = env->pc;
12254 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12255 DP_TBFLAG_A64(flags, BTYPE, env->btype);
12256 }
12257 } else {
12258 *pc = env->regs[15];
12259
12260 if (arm_feature(env, ARM_FEATURE_M)) {
12261 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12262 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12263 != env->v7m.secure) {
12264 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
12265 }
12266
12267 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12268 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12269 (env->v7m.secure &&
12270 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12271 /*
12272 * ASPEN is set, but FPCA/SFPA indicate that there is no
12273 * active FP context; we must create a new FP context before
12274 * executing any FP insn.
12275 */
12276 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
12277 }
12278
12279 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12280 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
12281 DP_TBFLAG_M32(flags, LSPACT, 1);
12282 }
12283
12284 if (mve_no_pred(env)) {
12285 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12286 }
12287 } else {
12288 /*
12289 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12290 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12291 */
12292 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12293 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
12294 } else {
12295 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12296 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
12297 }
12298 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12299 DP_TBFLAG_A32(flags, VFPEN, 1);
12300 }
12301 }
12302
12303 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12304 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
12305 }
12306
12307 /*
12308 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12309 * states defined in the ARM ARM for software singlestep:
12310 * SS_ACTIVE PSTATE.SS State
12311 * 0 x Inactive (the TB flag for SS is always 0)
12312 * 1 0 Active-pending
12313 * 1 1 Active-not-pending
12314 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
12315 */
12316 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12317 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
12318 }
12319
12320 *pflags = flags.flags;
12321 *cs_base = flags.flags2;
12322 }
12323
12324 #ifdef TARGET_AARCH64
12325 /*
12326 * The manual says that when SVE is enabled and VQ is widened the
12327 * implementation is allowed to zero the previously inaccessible
12328 * portion of the registers. The corollary to that is that when
12329 * SVE is enabled and VQ is narrowed we are also allowed to zero
12330 * the now inaccessible portion of the registers.
12331 *
12332 * The intent of this is that no predicate bit beyond VQ is ever set.
12333 * Which means that some operations on predicate registers themselves
12334 * may operate on full uint64_t or even unrolled across the maximum
12335 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
12336 * may well be cheaper than conditionals to restrict the operation
12337 * to the relevant portion of a uint16_t[16].
12338 */
12339 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12340 {
12341 int i, j;
12342 uint64_t pmask;
12343
12344 assert(vq >= 1 && vq <= ARM_MAX_VQ);
12345 assert(vq <= env_archcpu(env)->sve_max_vq);
12346
12347 /* Zap the high bits of the zregs. */
12348 for (i = 0; i < 32; i++) {
12349 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12350 }
12351
12352 /* Zap the high bits of the pregs and ffr. */
12353 pmask = 0;
12354 if (vq & 3) {
12355 pmask = ~(-1ULL << (16 * (vq & 3)));
12356 }
12357 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12358 for (i = 0; i < 17; ++i) {
12359 env->vfp.pregs[i].p[j] &= pmask;
12360 }
12361 pmask = 0;
12362 }
12363 }
12364
12365 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
12366 {
12367 int exc_el;
12368
12369 if (sm) {
12370 exc_el = sme_exception_el(env, el);
12371 } else {
12372 exc_el = sve_exception_el(env, el);
12373 }
12374 if (exc_el) {
12375 return 0; /* disabled */
12376 }
12377 return sve_vqm1_for_el_sm(env, el, sm);
12378 }
12379
12380 /*
12381 * Notice a change in SVE vector size when changing EL.
12382 */
12383 void aarch64_sve_change_el(CPUARMState *env, int old_el,
12384 int new_el, bool el0_a64)
12385 {
12386 ARMCPU *cpu = env_archcpu(env);
12387 int old_len, new_len;
12388 bool old_a64, new_a64, sm;
12389
12390 /* Nothing to do if no SVE. */
12391 if (!cpu_isar_feature(aa64_sve, cpu)) {
12392 return;
12393 }
12394
12395 /* Nothing to do if FP is disabled in either EL. */
12396 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12397 return;
12398 }
12399
12400 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12401 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12402
12403 /*
12404 * Both AArch64.TakeException and AArch64.ExceptionReturn
12405 * invoke ResetSVEState when taking an exception from, or
12406 * returning to, AArch32 state when PSTATE.SM is enabled.
12407 */
12408 sm = FIELD_EX64(env->svcr, SVCR, SM);
12409 if (old_a64 != new_a64 && sm) {
12410 arm_reset_sve_state(env);
12411 return;
12412 }
12413
12414 /*
12415 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12416 * at ELx, or not available because the EL is in AArch32 state, then
12417 * for all purposes other than a direct read, the ZCR_ELx.LEN field
12418 * has an effective value of 0".
12419 *
12420 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12421 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12422 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
12423 * we already have the correct register contents when encountering the
12424 * vq0->vq0 transition between EL0->EL1.
12425 */
12426 old_len = new_len = 0;
12427 if (old_a64) {
12428 old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12429 }
12430 if (new_a64) {
12431 new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12432 }
12433
12434 /* When changing vector length, clear inaccessible state. */
12435 if (new_len < old_len) {
12436 aarch64_sve_narrow_vq(env, new_len + 1);
12437 }
12438 }
12439 #endif
12440
12441 #ifndef CONFIG_USER_ONLY
12442 ARMSecuritySpace arm_security_space(CPUARMState *env)
12443 {
12444 if (arm_feature(env, ARM_FEATURE_M)) {
12445 return arm_secure_to_space(env->v7m.secure);
12446 }
12447
12448 /*
12449 * If EL3 is not supported then the secure state is implementation
12450 * defined, in which case QEMU defaults to non-secure.
12451 */
12452 if (!arm_feature(env, ARM_FEATURE_EL3)) {
12453 return ARMSS_NonSecure;
12454 }
12455
12456 /* Check for AArch64 EL3 or AArch32 Mon. */
12457 if (is_a64(env)) {
12458 if (extract32(env->pstate, 2, 2) == 3) {
12459 if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
12460 return ARMSS_Root;
12461 } else {
12462 return ARMSS_Secure;
12463 }
12464 }
12465 } else {
12466 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
12467 return ARMSS_Secure;
12468 }
12469 }
12470
12471 return arm_security_space_below_el3(env);
12472 }
12473
12474 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
12475 {
12476 assert(!arm_feature(env, ARM_FEATURE_M));
12477
12478 /*
12479 * If EL3 is not supported then the secure state is implementation
12480 * defined, in which case QEMU defaults to non-secure.
12481 */
12482 if (!arm_feature(env, ARM_FEATURE_EL3)) {
12483 return ARMSS_NonSecure;
12484 }
12485
12486 /*
12487 * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
12488 * Ignoring NSE when !NS retains consistency without having to
12489 * modify other predicates.
12490 */
12491 if (!(env->cp15.scr_el3 & SCR_NS)) {
12492 return ARMSS_Secure;
12493 } else if (env->cp15.scr_el3 & SCR_NSE) {
12494 return ARMSS_Realm;
12495 } else {
12496 return ARMSS_NonSecure;
12497 }
12498 }
12499 #endif /* !CONFIG_USER_ONLY */