1 /*
2 * ARM generic helpers.
3 *
4 * This code is licensed under the GNU GPL v2 or later.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "qemu/osdep.h"
10 #include "qemu/log.h"
11 #include "trace.h"
12 #include "cpu.h"
13 #include "internals.h"
14 #include "cpu-features.h"
15 #include "exec/helper-proto.h"
16 #include "qemu/main-loop.h"
17 #include "qemu/timer.h"
18 #include "qemu/bitops.h"
19 #include "qemu/crc32c.h"
20 #include "qemu/qemu-print.h"
21 #include "exec/exec-all.h"
22 #include <zlib.h> /* for crc32 */
23 #include "hw/irq.h"
24 #include "sysemu/cpu-timers.h"
25 #include "sysemu/kvm.h"
26 #include "sysemu/tcg.h"
27 #include "qapi/error.h"
28 #include "qemu/guest-random.h"
29 #ifdef CONFIG_TCG
30 #include "semihosting/common-semi.h"
31 #endif
32 #include "cpregs.h"
33 #include "target/arm/gtimer.h"
34
35 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
36
37 static void switch_mode(CPUARMState *env, int mode);
38
raw_read(CPUARMState * env,const ARMCPRegInfo * ri)39 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
40 {
41 assert(ri->fieldoffset);
42 if (cpreg_field_is_64bit(ri)) {
43 return CPREG_FIELD64(env, ri);
44 } else {
45 return CPREG_FIELD32(env, ri);
46 }
47 }
48
raw_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)49 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
50 {
51 assert(ri->fieldoffset);
52 if (cpreg_field_is_64bit(ri)) {
53 CPREG_FIELD64(env, ri) = value;
54 } else {
55 CPREG_FIELD32(env, ri) = value;
56 }
57 }
58
raw_ptr(CPUARMState * env,const ARMCPRegInfo * ri)59 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
60 {
61 return (char *)env + ri->fieldoffset;
62 }
63
read_raw_cp_reg(CPUARMState * env,const ARMCPRegInfo * ri)64 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
65 {
66 /* Raw read of a coprocessor register (as needed for migration, etc). */
67 if (ri->type & ARM_CP_CONST) {
68 return ri->resetvalue;
69 } else if (ri->raw_readfn) {
70 return ri->raw_readfn(env, ri);
71 } else if (ri->readfn) {
72 return ri->readfn(env, ri);
73 } else {
74 return raw_read(env, ri);
75 }
76 }
77
write_raw_cp_reg(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t v)78 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
79 uint64_t v)
80 {
81 /*
82 * Raw write of a coprocessor register (as needed for migration, etc).
83 * Note that constant registers are treated as write-ignored; the
84 * caller should check for success by whether a readback gives the
85 * value written.
86 */
87 if (ri->type & ARM_CP_CONST) {
88 return;
89 } else if (ri->raw_writefn) {
90 ri->raw_writefn(env, ri, v);
91 } else if (ri->writefn) {
92 ri->writefn(env, ri, v);
93 } else {
94 raw_write(env, ri, v);
95 }
96 }
97
raw_accessors_invalid(const ARMCPRegInfo * ri)98 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
99 {
100 /*
101 * Return true if the regdef would cause an assertion if you called
102 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
103 * program bug for it not to have the NO_RAW flag).
104 * NB that returning false here doesn't necessarily mean that calling
105 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
106 * read/write access functions which are safe for raw use" from "has
107 * read/write access functions which have side effects but has forgotten
108 * to provide raw access functions".
109 * The tests here line up with the conditions in read/write_raw_cp_reg()
110 * and assertions in raw_read()/raw_write().
111 */
112 if ((ri->type & ARM_CP_CONST) ||
113 ri->fieldoffset ||
114 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
115 return false;
116 }
117 return true;
118 }
119
write_cpustate_to_list(ARMCPU * cpu,bool kvm_sync)120 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
121 {
122 /* Write the coprocessor state from cpu->env to the (index,value) list. */
123 int i;
124 bool ok = true;
125
126 for (i = 0; i < cpu->cpreg_array_len; i++) {
127 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
128 const ARMCPRegInfo *ri;
129 uint64_t newval;
130
131 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
132 if (!ri) {
133 ok = false;
134 continue;
135 }
136 if (ri->type & ARM_CP_NO_RAW) {
137 continue;
138 }
139
140 newval = read_raw_cp_reg(&cpu->env, ri);
141 if (kvm_sync) {
142 /*
143 * Only sync if the previous list->cpustate sync succeeded.
144 * Rather than tracking the success/failure state for every
145 * item in the list, we just recheck "does the raw write we must
146 * have made in write_list_to_cpustate() read back OK" here.
147 */
148 uint64_t oldval = cpu->cpreg_values[i];
149
150 if (oldval == newval) {
151 continue;
152 }
153
154 write_raw_cp_reg(&cpu->env, ri, oldval);
155 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
156 continue;
157 }
158
159 write_raw_cp_reg(&cpu->env, ri, newval);
160 }
161 cpu->cpreg_values[i] = newval;
162 }
163 return ok;
164 }
165
write_list_to_cpustate(ARMCPU * cpu)166 bool write_list_to_cpustate(ARMCPU *cpu)
167 {
168 int i;
169 bool ok = true;
170
171 for (i = 0; i < cpu->cpreg_array_len; i++) {
172 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
173 uint64_t v = cpu->cpreg_values[i];
174 const ARMCPRegInfo *ri;
175
176 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
177 if (!ri) {
178 ok = false;
179 continue;
180 }
181 if (ri->type & ARM_CP_NO_RAW) {
182 continue;
183 }
184 /*
185 * Write value and confirm it reads back as written
186 * (to catch read-only registers and partially read-only
187 * registers where the incoming migration value doesn't match)
188 */
189 write_raw_cp_reg(&cpu->env, ri, v);
190 if (read_raw_cp_reg(&cpu->env, ri) != v) {
191 ok = false;
192 }
193 }
194 return ok;
195 }
196
add_cpreg_to_list(gpointer key,gpointer opaque)197 static void add_cpreg_to_list(gpointer key, gpointer opaque)
198 {
199 ARMCPU *cpu = opaque;
200 uint32_t regidx = (uintptr_t)key;
201 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
202
203 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
204 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
205 /* The value array need not be initialized at this point */
206 cpu->cpreg_array_len++;
207 }
208 }
209
count_cpreg(gpointer key,gpointer opaque)210 static void count_cpreg(gpointer key, gpointer opaque)
211 {
212 ARMCPU *cpu = opaque;
213 const ARMCPRegInfo *ri;
214
215 ri = g_hash_table_lookup(cpu->cp_regs, key);
216
217 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
218 cpu->cpreg_array_len++;
219 }
220 }
221
cpreg_key_compare(gconstpointer a,gconstpointer b)222 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
223 {
224 uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
225 uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
226
227 if (aidx > bidx) {
228 return 1;
229 }
230 if (aidx < bidx) {
231 return -1;
232 }
233 return 0;
234 }
235
init_cpreg_list(ARMCPU * cpu)236 void init_cpreg_list(ARMCPU *cpu)
237 {
238 /*
239 * Initialise the cpreg_tuples[] array based on the cp_regs hash.
240 * Note that we require cpreg_tuples[] to be sorted by key ID.
241 */
242 GList *keys;
243 int arraylen;
244
245 keys = g_hash_table_get_keys(cpu->cp_regs);
246 keys = g_list_sort(keys, cpreg_key_compare);
247
248 cpu->cpreg_array_len = 0;
249
250 g_list_foreach(keys, count_cpreg, cpu);
251
252 arraylen = cpu->cpreg_array_len;
253 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
254 cpu->cpreg_values = g_new(uint64_t, arraylen);
255 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
256 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
257 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
258 cpu->cpreg_array_len = 0;
259
260 g_list_foreach(keys, add_cpreg_to_list, cpu);
261
262 assert(cpu->cpreg_array_len == arraylen);
263
264 g_list_free(keys);
265 }
266
arm_pan_enabled(CPUARMState * env)267 static bool arm_pan_enabled(CPUARMState *env)
268 {
269 if (is_a64(env)) {
270 if ((arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1)) == (HCR_NV | HCR_NV1)) {
271 return false;
272 }
273 return env->pstate & PSTATE_PAN;
274 } else {
275 return env->uncached_cpsr & CPSR_PAN;
276 }
277 }
278
279 /*
280 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
281 */
access_el3_aa32ns(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)282 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
283 const ARMCPRegInfo *ri,
284 bool isread)
285 {
286 if (!is_a64(env) && arm_current_el(env) == 3 &&
287 arm_is_secure_below_el3(env)) {
288 return CP_ACCESS_TRAP_UNCATEGORIZED;
289 }
290 return CP_ACCESS_OK;
291 }
292
293 /*
294 * Some secure-only AArch32 registers trap to EL3 if used from
295 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
296 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
297 * We assume that the .access field is set to PL1_RW.
298 */
access_trap_aa32s_el1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)299 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
300 const ARMCPRegInfo *ri,
301 bool isread)
302 {
303 if (arm_current_el(env) == 3) {
304 return CP_ACCESS_OK;
305 }
306 if (arm_is_secure_below_el3(env)) {
307 if (env->cp15.scr_el3 & SCR_EEL2) {
308 return CP_ACCESS_TRAP_EL2;
309 }
310 return CP_ACCESS_TRAP_EL3;
311 }
312 /* This will be EL1 NS and EL2 NS, which just UNDEF */
313 return CP_ACCESS_TRAP_UNCATEGORIZED;
314 }
315
316 /*
317 * Check for traps to performance monitor registers, which are controlled
318 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
319 */
access_tpm(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)320 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
321 bool isread)
322 {
323 int el = arm_current_el(env);
324 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
325
326 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
327 return CP_ACCESS_TRAP_EL2;
328 }
329 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
330 return CP_ACCESS_TRAP_EL3;
331 }
332 return CP_ACCESS_OK;
333 }
334
335 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
access_tvm_trvm(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)336 CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
337 bool isread)
338 {
339 if (arm_current_el(env) == 1) {
340 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
341 if (arm_hcr_el2_eff(env) & trap) {
342 return CP_ACCESS_TRAP_EL2;
343 }
344 }
345 return CP_ACCESS_OK;
346 }
347
348 /* Check for traps from EL1 due to HCR_EL2.TSW. */
access_tsw(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)349 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
350 bool isread)
351 {
352 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
353 return CP_ACCESS_TRAP_EL2;
354 }
355 return CP_ACCESS_OK;
356 }
357
358 /* Check for traps from EL1 due to HCR_EL2.TACR. */
access_tacr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)359 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
360 bool isread)
361 {
362 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
363 return CP_ACCESS_TRAP_EL2;
364 }
365 return CP_ACCESS_OK;
366 }
367
368 /* Check for traps from EL1 due to HCR_EL2.TTLB. */
access_ttlb(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)369 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
370 bool isread)
371 {
372 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
373 return CP_ACCESS_TRAP_EL2;
374 }
375 return CP_ACCESS_OK;
376 }
377
378 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBIS. */
access_ttlbis(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)379 static CPAccessResult access_ttlbis(CPUARMState *env, const ARMCPRegInfo *ri,
380 bool isread)
381 {
382 if (arm_current_el(env) == 1 &&
383 (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBIS))) {
384 return CP_ACCESS_TRAP_EL2;
385 }
386 return CP_ACCESS_OK;
387 }
388
389 #ifdef TARGET_AARCH64
390 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBOS. */
access_ttlbos(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)391 static CPAccessResult access_ttlbos(CPUARMState *env, const ARMCPRegInfo *ri,
392 bool isread)
393 {
394 if (arm_current_el(env) == 1 &&
395 (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBOS))) {
396 return CP_ACCESS_TRAP_EL2;
397 }
398 return CP_ACCESS_OK;
399 }
400 #endif
401
dacr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)402 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
403 {
404 ARMCPU *cpu = env_archcpu(env);
405
406 raw_write(env, ri, value);
407 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
408 }
409
fcse_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)410 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
411 {
412 ARMCPU *cpu = env_archcpu(env);
413
414 if (raw_read(env, ri) != value) {
415 /*
416 * Unlike real hardware the qemu TLB uses virtual addresses,
417 * not modified virtual addresses, so this causes a TLB flush.
418 */
419 tlb_flush(CPU(cpu));
420 raw_write(env, ri, value);
421 }
422 }
423
contextidr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)424 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
425 uint64_t value)
426 {
427 ARMCPU *cpu = env_archcpu(env);
428
429 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
430 && !extended_addresses_enabled(env)) {
431 /*
432 * For VMSA (when not using the LPAE long descriptor page table
433 * format) this register includes the ASID, so do a TLB flush.
434 * For PMSA it is purely a process ID and no action is needed.
435 */
436 tlb_flush(CPU(cpu));
437 }
438 raw_write(env, ri, value);
439 }
440
alle1_tlbmask(CPUARMState * env)441 static int alle1_tlbmask(CPUARMState *env)
442 {
443 /*
444 * Note that the 'ALL' scope must invalidate both stage 1 and
445 * stage 2 translations, whereas most other scopes only invalidate
446 * stage 1 translations.
447 *
448 * For AArch32 this is only used for TLBIALLNSNH and VTTBR
449 * writes, so only needs to apply to NS PL1&0, not S PL1&0.
450 */
451 return (ARMMMUIdxBit_E10_1 |
452 ARMMMUIdxBit_E10_1_PAN |
453 ARMMMUIdxBit_E10_0 |
454 ARMMMUIdxBit_Stage2 |
455 ARMMMUIdxBit_Stage2_S);
456 }
457
458
459 /* IS variants of TLB operations must affect all cores */
tlbiall_is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)460 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
461 uint64_t value)
462 {
463 CPUState *cs = env_cpu(env);
464
465 tlb_flush_all_cpus_synced(cs);
466 }
467
tlbiasid_is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)468 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
469 uint64_t value)
470 {
471 CPUState *cs = env_cpu(env);
472
473 tlb_flush_all_cpus_synced(cs);
474 }
475
tlbimva_is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)476 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
477 uint64_t value)
478 {
479 CPUState *cs = env_cpu(env);
480
481 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
482 }
483
tlbimvaa_is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)484 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
485 uint64_t value)
486 {
487 CPUState *cs = env_cpu(env);
488
489 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
490 }
491
492 /*
493 * Non-IS variants of TLB operations are upgraded to
494 * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
495 * force broadcast of these operations.
496 */
tlb_force_broadcast(CPUARMState * env)497 static bool tlb_force_broadcast(CPUARMState *env)
498 {
499 return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
500 }
501
tlbiall_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)502 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
503 uint64_t value)
504 {
505 /* Invalidate all (TLBIALL) */
506 CPUState *cs = env_cpu(env);
507
508 if (tlb_force_broadcast(env)) {
509 tlb_flush_all_cpus_synced(cs);
510 } else {
511 tlb_flush(cs);
512 }
513 }
514
tlbimva_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)515 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
516 uint64_t value)
517 {
518 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
519 CPUState *cs = env_cpu(env);
520
521 value &= TARGET_PAGE_MASK;
522 if (tlb_force_broadcast(env)) {
523 tlb_flush_page_all_cpus_synced(cs, value);
524 } else {
525 tlb_flush_page(cs, value);
526 }
527 }
528
tlbiasid_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)529 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
530 uint64_t value)
531 {
532 /* Invalidate by ASID (TLBIASID) */
533 CPUState *cs = env_cpu(env);
534
535 if (tlb_force_broadcast(env)) {
536 tlb_flush_all_cpus_synced(cs);
537 } else {
538 tlb_flush(cs);
539 }
540 }
541
tlbimvaa_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)542 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
543 uint64_t value)
544 {
545 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
546 CPUState *cs = env_cpu(env);
547
548 value &= TARGET_PAGE_MASK;
549 if (tlb_force_broadcast(env)) {
550 tlb_flush_page_all_cpus_synced(cs, value);
551 } else {
552 tlb_flush_page(cs, value);
553 }
554 }
555
tlbiall_nsnh_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)556 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
557 uint64_t value)
558 {
559 CPUState *cs = env_cpu(env);
560
561 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
562 }
563
tlbiall_nsnh_is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)564 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
565 uint64_t value)
566 {
567 CPUState *cs = env_cpu(env);
568
569 tlb_flush_by_mmuidx_all_cpus_synced(cs, alle1_tlbmask(env));
570 }
571
572
tlbiall_hyp_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)573 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
574 uint64_t value)
575 {
576 CPUState *cs = env_cpu(env);
577
578 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
579 }
580
tlbiall_hyp_is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)581 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
582 uint64_t value)
583 {
584 CPUState *cs = env_cpu(env);
585
586 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
587 }
588
tlbimva_hyp_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)589 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
590 uint64_t value)
591 {
592 CPUState *cs = env_cpu(env);
593 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
594
595 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
596 }
597
tlbimva_hyp_is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)598 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
599 uint64_t value)
600 {
601 CPUState *cs = env_cpu(env);
602 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
603
604 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
605 ARMMMUIdxBit_E2);
606 }
607
tlbiipas2_hyp_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)608 static void tlbiipas2_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
609 uint64_t value)
610 {
611 CPUState *cs = env_cpu(env);
612 uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
613
614 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
615 }
616
tlbiipas2is_hyp_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)617 static void tlbiipas2is_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
618 uint64_t value)
619 {
620 CPUState *cs = env_cpu(env);
621 uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
622
623 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, ARMMMUIdxBit_Stage2);
624 }
625
626 static const ARMCPRegInfo cp_reginfo[] = {
627 /*
628 * Define the secure and non-secure FCSE identifier CP registers
629 * separately because there is no secure bank in V8 (no _EL3). This allows
630 * the secure register to be properly reset and migrated. There is also no
631 * v8 EL1 version of the register so the non-secure instance stands alone.
632 */
633 { .name = "FCSEIDR",
634 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
635 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
636 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
637 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
638 { .name = "FCSEIDR_S",
639 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
640 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
641 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
642 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
643 /*
644 * Define the secure and non-secure context identifier CP registers
645 * separately because there is no secure bank in V8 (no _EL3). This allows
646 * the secure register to be properly reset and migrated. In the
647 * non-secure case, the 32-bit register will have reset and migration
648 * disabled during registration as it is handled by the 64-bit instance.
649 */
650 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
651 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
652 .access = PL1_RW, .accessfn = access_tvm_trvm,
653 .fgt = FGT_CONTEXTIDR_EL1,
654 .nv2_redirect_offset = 0x108 | NV2_REDIR_NV1,
655 .secure = ARM_CP_SECSTATE_NS,
656 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
657 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
658 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
659 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
660 .access = PL1_RW, .accessfn = access_tvm_trvm,
661 .secure = ARM_CP_SECSTATE_S,
662 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
663 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
664 };
665
666 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
667 /*
668 * NB: Some of these registers exist in v8 but with more precise
669 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
670 */
671 /* MMU Domain access control / MPU write buffer control */
672 { .name = "DACR",
673 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
674 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
675 .writefn = dacr_write, .raw_writefn = raw_write,
676 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
677 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
678 /*
679 * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
680 * For v6 and v5, these mappings are overly broad.
681 */
682 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
683 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
684 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
685 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
686 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
687 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
688 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
689 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
690 /* Cache maintenance ops; some of this space may be overridden later. */
691 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
692 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
693 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
694 };
695
696 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
697 /*
698 * Not all pre-v6 cores implemented this WFI, so this is slightly
699 * over-broad.
700 */
701 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
702 .access = PL1_W, .type = ARM_CP_WFI },
703 };
704
705 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
706 /*
707 * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
708 * is UNPREDICTABLE; we choose to NOP as most implementations do).
709 */
710 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
711 .access = PL1_W, .type = ARM_CP_WFI },
712 /*
713 * L1 cache lockdown. Not architectural in v6 and earlier but in practice
714 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
715 * OMAPCP will override this space.
716 */
717 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
718 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
719 .resetvalue = 0 },
720 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
721 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
722 .resetvalue = 0 },
723 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
724 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
725 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
726 .resetvalue = 0 },
727 /*
728 * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
729 * implementing it as RAZ means the "debug architecture version" bits
730 * will read as a reserved value, which should cause Linux to not try
731 * to use the debug hardware.
732 */
733 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
734 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
735 /*
736 * MMU TLB control. Note that the wildcarding means we cover not just
737 * the unified TLB ops but also the dside/iside/inner-shareable variants.
738 */
739 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
740 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
741 .type = ARM_CP_NO_RAW },
742 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
743 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
744 .type = ARM_CP_NO_RAW },
745 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
746 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
747 .type = ARM_CP_NO_RAW },
748 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
749 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
750 .type = ARM_CP_NO_RAW },
751 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
752 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
753 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
754 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
755 };
756
cpacr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)757 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
758 uint64_t value)
759 {
760 uint32_t mask = 0;
761
762 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
763 if (!arm_feature(env, ARM_FEATURE_V8)) {
764 /*
765 * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
766 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
767 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
768 */
769 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
770 /* VFP coprocessor: cp10 & cp11 [23:20] */
771 mask |= R_CPACR_ASEDIS_MASK |
772 R_CPACR_D32DIS_MASK |
773 R_CPACR_CP11_MASK |
774 R_CPACR_CP10_MASK;
775
776 if (!arm_feature(env, ARM_FEATURE_NEON)) {
777 /* ASEDIS [31] bit is RAO/WI */
778 value |= R_CPACR_ASEDIS_MASK;
779 }
780
781 /*
782 * VFPv3 and upwards with NEON implement 32 double precision
783 * registers (D0-D31).
784 */
785 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
786 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
787 value |= R_CPACR_D32DIS_MASK;
788 }
789 }
790 value &= mask;
791 }
792
793 /*
794 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
795 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
796 */
797 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
798 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
799 mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
800 value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
801 }
802
803 env->cp15.cpacr_el1 = value;
804 }
805
cpacr_read(CPUARMState * env,const ARMCPRegInfo * ri)806 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
807 {
808 /*
809 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
810 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
811 */
812 uint64_t value = env->cp15.cpacr_el1;
813
814 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
815 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
816 value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
817 }
818 return value;
819 }
820
821
cpacr_reset(CPUARMState * env,const ARMCPRegInfo * ri)822 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
823 {
824 /*
825 * Call cpacr_write() so that we reset with the correct RAO bits set
826 * for our CPU features.
827 */
828 cpacr_write(env, ri, 0);
829 }
830
cpacr_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)831 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
832 bool isread)
833 {
834 if (arm_feature(env, ARM_FEATURE_V8)) {
835 /* Check if CPACR accesses are to be trapped to EL2 */
836 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
837 FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
838 return CP_ACCESS_TRAP_EL2;
839 /* Check if CPACR accesses are to be trapped to EL3 */
840 } else if (arm_current_el(env) < 3 &&
841 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
842 return CP_ACCESS_TRAP_EL3;
843 }
844 }
845
846 return CP_ACCESS_OK;
847 }
848
cptr_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)849 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
850 bool isread)
851 {
852 /* Check if CPTR accesses are set to trap to EL3 */
853 if (arm_current_el(env) == 2 &&
854 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
855 return CP_ACCESS_TRAP_EL3;
856 }
857
858 return CP_ACCESS_OK;
859 }
860
861 static const ARMCPRegInfo v6_cp_reginfo[] = {
862 /* prefetch by MVA in v6, NOP in v7 */
863 { .name = "MVA_prefetch",
864 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
865 .access = PL1_W, .type = ARM_CP_NOP },
866 /*
867 * We need to break the TB after ISB to execute self-modifying code
868 * correctly and also to take any pending interrupts immediately.
869 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
870 */
871 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
872 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
873 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
874 .access = PL0_W, .type = ARM_CP_NOP },
875 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
876 .access = PL0_W, .type = ARM_CP_NOP },
877 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
878 .access = PL1_RW, .accessfn = access_tvm_trvm,
879 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
880 offsetof(CPUARMState, cp15.ifar_ns) },
881 .resetvalue = 0, },
882 /*
883 * Watchpoint Fault Address Register : should actually only be present
884 * for 1136, 1176, 11MPCore.
885 */
886 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
887 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
888 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
889 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
890 .fgt = FGT_CPACR_EL1,
891 .nv2_redirect_offset = 0x100 | NV2_REDIR_NV1,
892 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
893 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
894 };
895
896 typedef struct pm_event {
897 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
898 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
899 bool (*supported)(CPUARMState *);
900 /*
901 * Retrieve the current count of the underlying event. The programmed
902 * counters hold a difference from the return value from this function
903 */
904 uint64_t (*get_count)(CPUARMState *);
905 /*
906 * Return how many nanoseconds it will take (at a minimum) for count events
907 * to occur. A negative value indicates the counter will never overflow, or
908 * that the counter has otherwise arranged for the overflow bit to be set
909 * and the PMU interrupt to be raised on overflow.
910 */
911 int64_t (*ns_per_count)(uint64_t);
912 } pm_event;
913
event_always_supported(CPUARMState * env)914 static bool event_always_supported(CPUARMState *env)
915 {
916 return true;
917 }
918
swinc_get_count(CPUARMState * env)919 static uint64_t swinc_get_count(CPUARMState *env)
920 {
921 /*
922 * SW_INCR events are written directly to the pmevcntr's by writes to
923 * PMSWINC, so there is no underlying count maintained by the PMU itself
924 */
925 return 0;
926 }
927
swinc_ns_per(uint64_t ignored)928 static int64_t swinc_ns_per(uint64_t ignored)
929 {
930 return -1;
931 }
932
933 /*
934 * Return the underlying cycle count for the PMU cycle counters. If we're in
935 * usermode, simply return 0.
936 */
cycles_get_count(CPUARMState * env)937 static uint64_t cycles_get_count(CPUARMState *env)
938 {
939 #ifndef CONFIG_USER_ONLY
940 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
941 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
942 #else
943 return cpu_get_host_ticks();
944 #endif
945 }
946
947 #ifndef CONFIG_USER_ONLY
cycles_ns_per(uint64_t cycles)948 static int64_t cycles_ns_per(uint64_t cycles)
949 {
950 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
951 }
952
instructions_supported(CPUARMState * env)953 static bool instructions_supported(CPUARMState *env)
954 {
955 /* Precise instruction counting */
956 return icount_enabled() == ICOUNT_PRECISE;
957 }
958
instructions_get_count(CPUARMState * env)959 static uint64_t instructions_get_count(CPUARMState *env)
960 {
961 assert(icount_enabled() == ICOUNT_PRECISE);
962 return (uint64_t)icount_get_raw();
963 }
964
instructions_ns_per(uint64_t icount)965 static int64_t instructions_ns_per(uint64_t icount)
966 {
967 assert(icount_enabled() == ICOUNT_PRECISE);
968 return icount_to_ns((int64_t)icount);
969 }
970 #endif
971
pmuv3p1_events_supported(CPUARMState * env)972 static bool pmuv3p1_events_supported(CPUARMState *env)
973 {
974 /* For events which are supported in any v8.1 PMU */
975 return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
976 }
977
pmuv3p4_events_supported(CPUARMState * env)978 static bool pmuv3p4_events_supported(CPUARMState *env)
979 {
980 /* For events which are supported in any v8.1 PMU */
981 return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
982 }
983
zero_event_get_count(CPUARMState * env)984 static uint64_t zero_event_get_count(CPUARMState *env)
985 {
986 /* For events which on QEMU never fire, so their count is always zero */
987 return 0;
988 }
989
zero_event_ns_per(uint64_t cycles)990 static int64_t zero_event_ns_per(uint64_t cycles)
991 {
992 /* An event which never fires can never overflow */
993 return -1;
994 }
995
996 static const pm_event pm_events[] = {
997 { .number = 0x000, /* SW_INCR */
998 .supported = event_always_supported,
999 .get_count = swinc_get_count,
1000 .ns_per_count = swinc_ns_per,
1001 },
1002 #ifndef CONFIG_USER_ONLY
1003 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
1004 .supported = instructions_supported,
1005 .get_count = instructions_get_count,
1006 .ns_per_count = instructions_ns_per,
1007 },
1008 { .number = 0x011, /* CPU_CYCLES, Cycle */
1009 .supported = event_always_supported,
1010 .get_count = cycles_get_count,
1011 .ns_per_count = cycles_ns_per,
1012 },
1013 #endif
1014 { .number = 0x023, /* STALL_FRONTEND */
1015 .supported = pmuv3p1_events_supported,
1016 .get_count = zero_event_get_count,
1017 .ns_per_count = zero_event_ns_per,
1018 },
1019 { .number = 0x024, /* STALL_BACKEND */
1020 .supported = pmuv3p1_events_supported,
1021 .get_count = zero_event_get_count,
1022 .ns_per_count = zero_event_ns_per,
1023 },
1024 { .number = 0x03c, /* STALL */
1025 .supported = pmuv3p4_events_supported,
1026 .get_count = zero_event_get_count,
1027 .ns_per_count = zero_event_ns_per,
1028 },
1029 };
1030
1031 /*
1032 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1033 * events (i.e. the statistical profiling extension), this implementation
1034 * should first be updated to something sparse instead of the current
1035 * supported_event_map[] array.
1036 */
1037 #define MAX_EVENT_ID 0x3c
1038 #define UNSUPPORTED_EVENT UINT16_MAX
1039 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1040
1041 /*
1042 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1043 * of ARM event numbers to indices in our pm_events array.
1044 *
1045 * Note: Events in the 0x40XX range are not currently supported.
1046 */
pmu_init(ARMCPU * cpu)1047 void pmu_init(ARMCPU *cpu)
1048 {
1049 unsigned int i;
1050
1051 /*
1052 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1053 * events to them
1054 */
1055 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1056 supported_event_map[i] = UNSUPPORTED_EVENT;
1057 }
1058 cpu->pmceid0 = 0;
1059 cpu->pmceid1 = 0;
1060
1061 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1062 const pm_event *cnt = &pm_events[i];
1063 assert(cnt->number <= MAX_EVENT_ID);
1064 /* We do not currently support events in the 0x40xx range */
1065 assert(cnt->number <= 0x3f);
1066
1067 if (cnt->supported(&cpu->env)) {
1068 supported_event_map[cnt->number] = i;
1069 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
1070 if (cnt->number & 0x20) {
1071 cpu->pmceid1 |= event_mask;
1072 } else {
1073 cpu->pmceid0 |= event_mask;
1074 }
1075 }
1076 }
1077 }
1078
1079 /*
1080 * Check at runtime whether a PMU event is supported for the current machine
1081 */
event_supported(uint16_t number)1082 static bool event_supported(uint16_t number)
1083 {
1084 if (number > MAX_EVENT_ID) {
1085 return false;
1086 }
1087 return supported_event_map[number] != UNSUPPORTED_EVENT;
1088 }
1089
pmreg_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)1090 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1091 bool isread)
1092 {
1093 /*
1094 * Performance monitor registers user accessibility is controlled
1095 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1096 * trapping to EL2 or EL3 for other accesses.
1097 */
1098 int el = arm_current_el(env);
1099 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1100
1101 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
1102 return CP_ACCESS_TRAP;
1103 }
1104 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1105 return CP_ACCESS_TRAP_EL2;
1106 }
1107 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1108 return CP_ACCESS_TRAP_EL3;
1109 }
1110
1111 return CP_ACCESS_OK;
1112 }
1113
pmreg_access_xevcntr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)1114 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1115 const ARMCPRegInfo *ri,
1116 bool isread)
1117 {
1118 /* ER: event counter read trap control */
1119 if (arm_feature(env, ARM_FEATURE_V8)
1120 && arm_current_el(env) == 0
1121 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1122 && isread) {
1123 return CP_ACCESS_OK;
1124 }
1125
1126 return pmreg_access(env, ri, isread);
1127 }
1128
pmreg_access_swinc(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)1129 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1130 const ARMCPRegInfo *ri,
1131 bool isread)
1132 {
1133 /* SW: software increment write trap control */
1134 if (arm_feature(env, ARM_FEATURE_V8)
1135 && arm_current_el(env) == 0
1136 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1137 && !isread) {
1138 return CP_ACCESS_OK;
1139 }
1140
1141 return pmreg_access(env, ri, isread);
1142 }
1143
pmreg_access_selr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)1144 static CPAccessResult pmreg_access_selr(CPUARMState *env,
1145 const ARMCPRegInfo *ri,
1146 bool isread)
1147 {
1148 /* ER: event counter read trap control */
1149 if (arm_feature(env, ARM_FEATURE_V8)
1150 && arm_current_el(env) == 0
1151 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1152 return CP_ACCESS_OK;
1153 }
1154
1155 return pmreg_access(env, ri, isread);
1156 }
1157
pmreg_access_ccntr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)1158 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1159 const ARMCPRegInfo *ri,
1160 bool isread)
1161 {
1162 /* CR: cycle counter read trap control */
1163 if (arm_feature(env, ARM_FEATURE_V8)
1164 && arm_current_el(env) == 0
1165 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1166 && isread) {
1167 return CP_ACCESS_OK;
1168 }
1169
1170 return pmreg_access(env, ri, isread);
1171 }
1172
1173 /*
1174 * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1175 * We use these to decide whether we need to wrap a write to MDCR_EL2
1176 * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1177 */
1178 #define MDCR_EL2_PMU_ENABLE_BITS \
1179 (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
1180 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
1181
1182 /*
1183 * Returns true if the counter (pass 31 for PMCCNTR) should count events using
1184 * the current EL, security state, and register configuration.
1185 */
pmu_counter_enabled(CPUARMState * env,uint8_t counter)1186 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
1187 {
1188 uint64_t filter;
1189 bool e, p, u, nsk, nsu, nsh, m;
1190 bool enabled, prohibited = false, filtered;
1191 bool secure = arm_is_secure(env);
1192 int el = arm_current_el(env);
1193 uint64_t mdcr_el2;
1194 uint8_t hpmn;
1195
1196 /*
1197 * We might be called for M-profile cores where MDCR_EL2 doesn't
1198 * exist and arm_mdcr_el2_eff() will assert, so this early-exit check
1199 * must be before we read that value.
1200 */
1201 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1202 return false;
1203 }
1204
1205 mdcr_el2 = arm_mdcr_el2_eff(env);
1206 hpmn = mdcr_el2 & MDCR_HPMN;
1207
1208 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1209 (counter < hpmn || counter == 31)) {
1210 e = env->cp15.c9_pmcr & PMCRE;
1211 } else {
1212 e = mdcr_el2 & MDCR_HPME;
1213 }
1214 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1215
1216 /* Is event counting prohibited? */
1217 if (el == 2 && (counter < hpmn || counter == 31)) {
1218 prohibited = mdcr_el2 & MDCR_HPMD;
1219 }
1220 if (secure) {
1221 prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1222 }
1223
1224 if (counter == 31) {
1225 /*
1226 * The cycle counter defaults to running. PMCR.DP says "disable
1227 * the cycle counter when event counting is prohibited".
1228 * Some MDCR bits disable the cycle counter specifically.
1229 */
1230 prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1231 if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1232 if (secure) {
1233 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1234 }
1235 if (el == 2) {
1236 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1237 }
1238 }
1239 }
1240
1241 if (counter == 31) {
1242 filter = env->cp15.pmccfiltr_el0;
1243 } else {
1244 filter = env->cp15.c14_pmevtyper[counter];
1245 }
1246
1247 p = filter & PMXEVTYPER_P;
1248 u = filter & PMXEVTYPER_U;
1249 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1250 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1251 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1252 m = arm_el_is_aa64(env, 1) &&
1253 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1254
1255 if (el == 0) {
1256 filtered = secure ? u : u != nsu;
1257 } else if (el == 1) {
1258 filtered = secure ? p : p != nsk;
1259 } else if (el == 2) {
1260 filtered = !nsh;
1261 } else { /* EL3 */
1262 filtered = m != p;
1263 }
1264
1265 if (counter != 31) {
1266 /*
1267 * If not checking PMCCNTR, ensure the counter is setup to an event we
1268 * support
1269 */
1270 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1271 if (!event_supported(event)) {
1272 return false;
1273 }
1274 }
1275
1276 return enabled && !prohibited && !filtered;
1277 }
1278
pmu_update_irq(CPUARMState * env)1279 static void pmu_update_irq(CPUARMState *env)
1280 {
1281 ARMCPU *cpu = env_archcpu(env);
1282 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1283 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1284 }
1285
pmccntr_clockdiv_enabled(CPUARMState * env)1286 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1287 {
1288 /*
1289 * Return true if the clock divider is enabled and the cycle counter
1290 * is supposed to tick only once every 64 clock cycles. This is
1291 * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1292 * (64-bit) cycle counter PMCR.D has no effect.
1293 */
1294 return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1295 }
1296
pmevcntr_is_64_bit(CPUARMState * env,int counter)1297 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1298 {
1299 /* Return true if the specified event counter is configured to be 64 bit */
1300
1301 /* This isn't intended to be used with the cycle counter */
1302 assert(counter < 31);
1303
1304 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1305 return false;
1306 }
1307
1308 if (arm_feature(env, ARM_FEATURE_EL2)) {
1309 /*
1310 * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1311 * current security state, so we don't use arm_mdcr_el2_eff() here.
1312 */
1313 bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1314 int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1315
1316 if (counter >= hpmn) {
1317 return hlp;
1318 }
1319 }
1320 return env->cp15.c9_pmcr & PMCRLP;
1321 }
1322
1323 /*
1324 * Ensure c15_ccnt is the guest-visible count so that operations such as
1325 * enabling/disabling the counter or filtering, modifying the count itself,
1326 * etc. can be done logically. This is essentially a no-op if the counter is
1327 * not enabled at the time of the call.
1328 */
pmccntr_op_start(CPUARMState * env)1329 static void pmccntr_op_start(CPUARMState *env)
1330 {
1331 uint64_t cycles = cycles_get_count(env);
1332
1333 if (pmu_counter_enabled(env, 31)) {
1334 uint64_t eff_cycles = cycles;
1335 if (pmccntr_clockdiv_enabled(env)) {
1336 eff_cycles /= 64;
1337 }
1338
1339 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1340
1341 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1342 1ull << 63 : 1ull << 31;
1343 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1344 env->cp15.c9_pmovsr |= (1ULL << 31);
1345 pmu_update_irq(env);
1346 }
1347
1348 env->cp15.c15_ccnt = new_pmccntr;
1349 }
1350 env->cp15.c15_ccnt_delta = cycles;
1351 }
1352
1353 /*
1354 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1355 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1356 * pmccntr_op_start.
1357 */
pmccntr_op_finish(CPUARMState * env)1358 static void pmccntr_op_finish(CPUARMState *env)
1359 {
1360 if (pmu_counter_enabled(env, 31)) {
1361 #ifndef CONFIG_USER_ONLY
1362 /* Calculate when the counter will next overflow */
1363 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1364 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1365 remaining_cycles = (uint32_t)remaining_cycles;
1366 }
1367 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1368
1369 if (overflow_in > 0) {
1370 int64_t overflow_at;
1371
1372 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1373 overflow_in, &overflow_at)) {
1374 ARMCPU *cpu = env_archcpu(env);
1375 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1376 }
1377 }
1378 #endif
1379
1380 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1381 if (pmccntr_clockdiv_enabled(env)) {
1382 prev_cycles /= 64;
1383 }
1384 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1385 }
1386 }
1387
pmevcntr_op_start(CPUARMState * env,uint8_t counter)1388 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1389 {
1390
1391 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1392 uint64_t count = 0;
1393 if (event_supported(event)) {
1394 uint16_t event_idx = supported_event_map[event];
1395 count = pm_events[event_idx].get_count(env);
1396 }
1397
1398 if (pmu_counter_enabled(env, counter)) {
1399 uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1400 uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1401 1ULL << 63 : 1ULL << 31;
1402
1403 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1404 env->cp15.c9_pmovsr |= (1 << counter);
1405 pmu_update_irq(env);
1406 }
1407 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1408 }
1409 env->cp15.c14_pmevcntr_delta[counter] = count;
1410 }
1411
pmevcntr_op_finish(CPUARMState * env,uint8_t counter)1412 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1413 {
1414 if (pmu_counter_enabled(env, counter)) {
1415 #ifndef CONFIG_USER_ONLY
1416 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1417 uint16_t event_idx = supported_event_map[event];
1418 uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1419 int64_t overflow_in;
1420
1421 if (!pmevcntr_is_64_bit(env, counter)) {
1422 delta = (uint32_t)delta;
1423 }
1424 overflow_in = pm_events[event_idx].ns_per_count(delta);
1425
1426 if (overflow_in > 0) {
1427 int64_t overflow_at;
1428
1429 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1430 overflow_in, &overflow_at)) {
1431 ARMCPU *cpu = env_archcpu(env);
1432 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1433 }
1434 }
1435 #endif
1436
1437 env->cp15.c14_pmevcntr_delta[counter] -=
1438 env->cp15.c14_pmevcntr[counter];
1439 }
1440 }
1441
pmu_op_start(CPUARMState * env)1442 void pmu_op_start(CPUARMState *env)
1443 {
1444 unsigned int i;
1445 pmccntr_op_start(env);
1446 for (i = 0; i < pmu_num_counters(env); i++) {
1447 pmevcntr_op_start(env, i);
1448 }
1449 }
1450
pmu_op_finish(CPUARMState * env)1451 void pmu_op_finish(CPUARMState *env)
1452 {
1453 unsigned int i;
1454 pmccntr_op_finish(env);
1455 for (i = 0; i < pmu_num_counters(env); i++) {
1456 pmevcntr_op_finish(env, i);
1457 }
1458 }
1459
pmu_pre_el_change(ARMCPU * cpu,void * ignored)1460 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1461 {
1462 pmu_op_start(&cpu->env);
1463 }
1464
pmu_post_el_change(ARMCPU * cpu,void * ignored)1465 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1466 {
1467 pmu_op_finish(&cpu->env);
1468 }
1469
arm_pmu_timer_cb(void * opaque)1470 void arm_pmu_timer_cb(void *opaque)
1471 {
1472 ARMCPU *cpu = opaque;
1473
1474 /*
1475 * Update all the counter values based on the current underlying counts,
1476 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1477 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1478 * counter may expire.
1479 */
1480 pmu_op_start(&cpu->env);
1481 pmu_op_finish(&cpu->env);
1482 }
1483
pmcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1484 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1485 uint64_t value)
1486 {
1487 pmu_op_start(env);
1488
1489 if (value & PMCRC) {
1490 /* The counter has been reset */
1491 env->cp15.c15_ccnt = 0;
1492 }
1493
1494 if (value & PMCRP) {
1495 unsigned int i;
1496 for (i = 0; i < pmu_num_counters(env); i++) {
1497 env->cp15.c14_pmevcntr[i] = 0;
1498 }
1499 }
1500
1501 env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1502 env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1503
1504 pmu_op_finish(env);
1505 }
1506
pmcr_read(CPUARMState * env,const ARMCPRegInfo * ri)1507 static uint64_t pmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1508 {
1509 uint64_t pmcr = env->cp15.c9_pmcr;
1510
1511 /*
1512 * If EL2 is implemented and enabled for the current security state, reads
1513 * of PMCR.N from EL1 or EL0 return the value of MDCR_EL2.HPMN or HDCR.HPMN.
1514 */
1515 if (arm_current_el(env) <= 1 && arm_is_el2_enabled(env)) {
1516 pmcr &= ~PMCRN_MASK;
1517 pmcr |= (env->cp15.mdcr_el2 & MDCR_HPMN) << PMCRN_SHIFT;
1518 }
1519
1520 return pmcr;
1521 }
1522
pmswinc_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1523 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1524 uint64_t value)
1525 {
1526 unsigned int i;
1527 uint64_t overflow_mask, new_pmswinc;
1528
1529 for (i = 0; i < pmu_num_counters(env); i++) {
1530 /* Increment a counter's count iff: */
1531 if ((value & (1 << i)) && /* counter's bit is set */
1532 /* counter is enabled and not filtered */
1533 pmu_counter_enabled(env, i) &&
1534 /* counter is SW_INCR */
1535 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1536 pmevcntr_op_start(env, i);
1537
1538 /*
1539 * Detect if this write causes an overflow since we can't predict
1540 * PMSWINC overflows like we can for other events
1541 */
1542 new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1543
1544 overflow_mask = pmevcntr_is_64_bit(env, i) ?
1545 1ULL << 63 : 1ULL << 31;
1546
1547 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1548 env->cp15.c9_pmovsr |= (1 << i);
1549 pmu_update_irq(env);
1550 }
1551
1552 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1553
1554 pmevcntr_op_finish(env, i);
1555 }
1556 }
1557 }
1558
pmccntr_read(CPUARMState * env,const ARMCPRegInfo * ri)1559 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1560 {
1561 uint64_t ret;
1562 pmccntr_op_start(env);
1563 ret = env->cp15.c15_ccnt;
1564 pmccntr_op_finish(env);
1565 return ret;
1566 }
1567
pmselr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1568 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1569 uint64_t value)
1570 {
1571 /*
1572 * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1573 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1574 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1575 * accessed.
1576 */
1577 env->cp15.c9_pmselr = value & 0x1f;
1578 }
1579
pmccntr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1580 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1581 uint64_t value)
1582 {
1583 pmccntr_op_start(env);
1584 env->cp15.c15_ccnt = value;
1585 pmccntr_op_finish(env);
1586 }
1587
pmccntr_write32(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1588 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1589 uint64_t value)
1590 {
1591 uint64_t cur_val = pmccntr_read(env, NULL);
1592
1593 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1594 }
1595
pmccfiltr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1596 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1597 uint64_t value)
1598 {
1599 pmccntr_op_start(env);
1600 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1601 pmccntr_op_finish(env);
1602 }
1603
pmccfiltr_write_a32(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1604 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1605 uint64_t value)
1606 {
1607 pmccntr_op_start(env);
1608 /* M is not accessible from AArch32 */
1609 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1610 (value & PMCCFILTR);
1611 pmccntr_op_finish(env);
1612 }
1613
pmccfiltr_read_a32(CPUARMState * env,const ARMCPRegInfo * ri)1614 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1615 {
1616 /* M is not visible in AArch32 */
1617 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1618 }
1619
pmcntenset_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1620 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1621 uint64_t value)
1622 {
1623 pmu_op_start(env);
1624 value &= pmu_counter_mask(env);
1625 env->cp15.c9_pmcnten |= value;
1626 pmu_op_finish(env);
1627 }
1628
pmcntenclr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1629 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1630 uint64_t value)
1631 {
1632 pmu_op_start(env);
1633 value &= pmu_counter_mask(env);
1634 env->cp15.c9_pmcnten &= ~value;
1635 pmu_op_finish(env);
1636 }
1637
pmovsr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1638 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1639 uint64_t value)
1640 {
1641 value &= pmu_counter_mask(env);
1642 env->cp15.c9_pmovsr &= ~value;
1643 pmu_update_irq(env);
1644 }
1645
pmovsset_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1646 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1647 uint64_t value)
1648 {
1649 value &= pmu_counter_mask(env);
1650 env->cp15.c9_pmovsr |= value;
1651 pmu_update_irq(env);
1652 }
1653
pmevtyper_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value,const uint8_t counter)1654 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1655 uint64_t value, const uint8_t counter)
1656 {
1657 if (counter == 31) {
1658 pmccfiltr_write(env, ri, value);
1659 } else if (counter < pmu_num_counters(env)) {
1660 pmevcntr_op_start(env, counter);
1661
1662 /*
1663 * If this counter's event type is changing, store the current
1664 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1665 * pmevcntr_op_finish has the correct baseline when it converts back to
1666 * a delta.
1667 */
1668 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1669 PMXEVTYPER_EVTCOUNT;
1670 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1671 if (old_event != new_event) {
1672 uint64_t count = 0;
1673 if (event_supported(new_event)) {
1674 uint16_t event_idx = supported_event_map[new_event];
1675 count = pm_events[event_idx].get_count(env);
1676 }
1677 env->cp15.c14_pmevcntr_delta[counter] = count;
1678 }
1679
1680 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1681 pmevcntr_op_finish(env, counter);
1682 }
1683 /*
1684 * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1685 * PMSELR value is equal to or greater than the number of implemented
1686 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1687 */
1688 }
1689
pmevtyper_read(CPUARMState * env,const ARMCPRegInfo * ri,const uint8_t counter)1690 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1691 const uint8_t counter)
1692 {
1693 if (counter == 31) {
1694 return env->cp15.pmccfiltr_el0;
1695 } else if (counter < pmu_num_counters(env)) {
1696 return env->cp15.c14_pmevtyper[counter];
1697 } else {
1698 /*
1699 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1700 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1701 */
1702 return 0;
1703 }
1704 }
1705
pmevtyper_writefn(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1706 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1707 uint64_t value)
1708 {
1709 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1710 pmevtyper_write(env, ri, value, counter);
1711 }
1712
pmevtyper_rawwrite(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1713 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1714 uint64_t value)
1715 {
1716 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1717 env->cp15.c14_pmevtyper[counter] = value;
1718
1719 /*
1720 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1721 * pmu_op_finish calls when loading saved state for a migration. Because
1722 * we're potentially updating the type of event here, the value written to
1723 * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a
1724 * different counter type. Therefore, we need to set this value to the
1725 * current count for the counter type we're writing so that pmu_op_finish
1726 * has the correct count for its calculation.
1727 */
1728 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1729 if (event_supported(event)) {
1730 uint16_t event_idx = supported_event_map[event];
1731 env->cp15.c14_pmevcntr_delta[counter] =
1732 pm_events[event_idx].get_count(env);
1733 }
1734 }
1735
pmevtyper_readfn(CPUARMState * env,const ARMCPRegInfo * ri)1736 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1737 {
1738 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1739 return pmevtyper_read(env, ri, counter);
1740 }
1741
pmxevtyper_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1742 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1743 uint64_t value)
1744 {
1745 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1746 }
1747
pmxevtyper_read(CPUARMState * env,const ARMCPRegInfo * ri)1748 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1749 {
1750 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1751 }
1752
pmevcntr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value,uint8_t counter)1753 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1754 uint64_t value, uint8_t counter)
1755 {
1756 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1757 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1758 value &= MAKE_64BIT_MASK(0, 32);
1759 }
1760 if (counter < pmu_num_counters(env)) {
1761 pmevcntr_op_start(env, counter);
1762 env->cp15.c14_pmevcntr[counter] = value;
1763 pmevcntr_op_finish(env, counter);
1764 }
1765 /*
1766 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1767 * are CONSTRAINED UNPREDICTABLE.
1768 */
1769 }
1770
pmevcntr_read(CPUARMState * env,const ARMCPRegInfo * ri,uint8_t counter)1771 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1772 uint8_t counter)
1773 {
1774 if (counter < pmu_num_counters(env)) {
1775 uint64_t ret;
1776 pmevcntr_op_start(env, counter);
1777 ret = env->cp15.c14_pmevcntr[counter];
1778 pmevcntr_op_finish(env, counter);
1779 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1780 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1781 ret &= MAKE_64BIT_MASK(0, 32);
1782 }
1783 return ret;
1784 } else {
1785 /*
1786 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1787 * are CONSTRAINED UNPREDICTABLE.
1788 */
1789 return 0;
1790 }
1791 }
1792
pmevcntr_writefn(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1793 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1794 uint64_t value)
1795 {
1796 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1797 pmevcntr_write(env, ri, value, counter);
1798 }
1799
pmevcntr_readfn(CPUARMState * env,const ARMCPRegInfo * ri)1800 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1801 {
1802 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1803 return pmevcntr_read(env, ri, counter);
1804 }
1805
pmevcntr_rawwrite(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1806 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1807 uint64_t value)
1808 {
1809 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1810 assert(counter < pmu_num_counters(env));
1811 env->cp15.c14_pmevcntr[counter] = value;
1812 pmevcntr_write(env, ri, value, counter);
1813 }
1814
pmevcntr_rawread(CPUARMState * env,const ARMCPRegInfo * ri)1815 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1816 {
1817 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1818 assert(counter < pmu_num_counters(env));
1819 return env->cp15.c14_pmevcntr[counter];
1820 }
1821
pmxevcntr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1822 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1823 uint64_t value)
1824 {
1825 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1826 }
1827
pmxevcntr_read(CPUARMState * env,const ARMCPRegInfo * ri)1828 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1829 {
1830 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1831 }
1832
pmuserenr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1833 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1834 uint64_t value)
1835 {
1836 if (arm_feature(env, ARM_FEATURE_V8)) {
1837 env->cp15.c9_pmuserenr = value & 0xf;
1838 } else {
1839 env->cp15.c9_pmuserenr = value & 1;
1840 }
1841 }
1842
pmintenset_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1843 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1844 uint64_t value)
1845 {
1846 /* We have no event counters so only the C bit can be changed */
1847 value &= pmu_counter_mask(env);
1848 env->cp15.c9_pminten |= value;
1849 pmu_update_irq(env);
1850 }
1851
pmintenclr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1852 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1853 uint64_t value)
1854 {
1855 value &= pmu_counter_mask(env);
1856 env->cp15.c9_pminten &= ~value;
1857 pmu_update_irq(env);
1858 }
1859
vbar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1860 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1861 uint64_t value)
1862 {
1863 /*
1864 * Note that even though the AArch64 view of this register has bits
1865 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1866 * architectural requirements for bits which are RES0 only in some
1867 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1868 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1869 */
1870 raw_write(env, ri, value & ~0x1FULL);
1871 }
1872
scr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1873 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1874 {
1875 /* Begin with base v8.0 state. */
1876 uint64_t valid_mask = 0x3fff;
1877 ARMCPU *cpu = env_archcpu(env);
1878 uint64_t changed;
1879
1880 /*
1881 * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1882 * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1883 * Instead, choose the format based on the mode of EL3.
1884 */
1885 if (arm_el_is_aa64(env, 3)) {
1886 value |= SCR_FW | SCR_AW; /* RES1 */
1887 valid_mask &= ~SCR_NET; /* RES0 */
1888
1889 if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1890 !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1891 value |= SCR_RW; /* RAO/WI */
1892 }
1893 if (cpu_isar_feature(aa64_ras, cpu)) {
1894 valid_mask |= SCR_TERR;
1895 }
1896 if (cpu_isar_feature(aa64_lor, cpu)) {
1897 valid_mask |= SCR_TLOR;
1898 }
1899 if (cpu_isar_feature(aa64_pauth, cpu)) {
1900 valid_mask |= SCR_API | SCR_APK;
1901 }
1902 if (cpu_isar_feature(aa64_sel2, cpu)) {
1903 valid_mask |= SCR_EEL2;
1904 } else if (cpu_isar_feature(aa64_rme, cpu)) {
1905 /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */
1906 value |= SCR_NS;
1907 }
1908 if (cpu_isar_feature(aa64_mte, cpu)) {
1909 valid_mask |= SCR_ATA;
1910 }
1911 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1912 valid_mask |= SCR_ENSCXT;
1913 }
1914 if (cpu_isar_feature(aa64_doublefault, cpu)) {
1915 valid_mask |= SCR_EASE | SCR_NMEA;
1916 }
1917 if (cpu_isar_feature(aa64_sme, cpu)) {
1918 valid_mask |= SCR_ENTP2;
1919 }
1920 if (cpu_isar_feature(aa64_hcx, cpu)) {
1921 valid_mask |= SCR_HXEN;
1922 }
1923 if (cpu_isar_feature(aa64_fgt, cpu)) {
1924 valid_mask |= SCR_FGTEN;
1925 }
1926 if (cpu_isar_feature(aa64_rme, cpu)) {
1927 valid_mask |= SCR_NSE | SCR_GPF;
1928 }
1929 if (cpu_isar_feature(aa64_ecv, cpu)) {
1930 valid_mask |= SCR_ECVEN;
1931 }
1932 } else {
1933 valid_mask &= ~(SCR_RW | SCR_ST);
1934 if (cpu_isar_feature(aa32_ras, cpu)) {
1935 valid_mask |= SCR_TERR;
1936 }
1937 }
1938
1939 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1940 valid_mask &= ~SCR_HCE;
1941
1942 /*
1943 * On ARMv7, SMD (or SCD as it is called in v7) is only
1944 * supported if EL2 exists. The bit is UNK/SBZP when
1945 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1946 * when EL2 is unavailable.
1947 * On ARMv8, this bit is always available.
1948 */
1949 if (arm_feature(env, ARM_FEATURE_V7) &&
1950 !arm_feature(env, ARM_FEATURE_V8)) {
1951 valid_mask &= ~SCR_SMD;
1952 }
1953 }
1954
1955 /* Clear all-context RES0 bits. */
1956 value &= valid_mask;
1957 changed = env->cp15.scr_el3 ^ value;
1958 env->cp15.scr_el3 = value;
1959
1960 /*
1961 * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
1962 * we must invalidate all TLBs below EL3.
1963 */
1964 if (changed & (SCR_NS | SCR_NSE)) {
1965 tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1966 ARMMMUIdxBit_E20_0 |
1967 ARMMMUIdxBit_E10_1 |
1968 ARMMMUIdxBit_E20_2 |
1969 ARMMMUIdxBit_E10_1_PAN |
1970 ARMMMUIdxBit_E20_2_PAN |
1971 ARMMMUIdxBit_E2));
1972 }
1973 }
1974
scr_reset(CPUARMState * env,const ARMCPRegInfo * ri)1975 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1976 {
1977 /*
1978 * scr_write will set the RES1 bits on an AArch64-only CPU.
1979 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1980 */
1981 scr_write(env, ri, 0);
1982 }
1983
access_tid4(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)1984 static CPAccessResult access_tid4(CPUARMState *env,
1985 const ARMCPRegInfo *ri,
1986 bool isread)
1987 {
1988 if (arm_current_el(env) == 1 &&
1989 (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1990 return CP_ACCESS_TRAP_EL2;
1991 }
1992
1993 return CP_ACCESS_OK;
1994 }
1995
ccsidr_read(CPUARMState * env,const ARMCPRegInfo * ri)1996 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1997 {
1998 ARMCPU *cpu = env_archcpu(env);
1999
2000 /*
2001 * Acquire the CSSELR index from the bank corresponding to the CCSIDR
2002 * bank
2003 */
2004 uint32_t index = A32_BANKED_REG_GET(env, csselr,
2005 ri->secure & ARM_CP_SECSTATE_S);
2006
2007 return cpu->ccsidr[index];
2008 }
2009
csselr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2010 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2011 uint64_t value)
2012 {
2013 raw_write(env, ri, value & 0xf);
2014 }
2015
isr_read(CPUARMState * env,const ARMCPRegInfo * ri)2016 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2017 {
2018 CPUState *cs = env_cpu(env);
2019 bool el1 = arm_current_el(env) == 1;
2020 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
2021 uint64_t ret = 0;
2022
2023 if (hcr_el2 & HCR_IMO) {
2024 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
2025 ret |= CPSR_I;
2026 }
2027 if (cs->interrupt_request & CPU_INTERRUPT_VINMI) {
2028 ret |= ISR_IS;
2029 ret |= CPSR_I;
2030 }
2031 } else {
2032 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
2033 ret |= CPSR_I;
2034 }
2035
2036 if (cs->interrupt_request & CPU_INTERRUPT_NMI) {
2037 ret |= ISR_IS;
2038 ret |= CPSR_I;
2039 }
2040 }
2041
2042 if (hcr_el2 & HCR_FMO) {
2043 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
2044 ret |= CPSR_F;
2045 }
2046 if (cs->interrupt_request & CPU_INTERRUPT_VFNMI) {
2047 ret |= ISR_FS;
2048 ret |= CPSR_F;
2049 }
2050 } else {
2051 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
2052 ret |= CPSR_F;
2053 }
2054 }
2055
2056 if (hcr_el2 & HCR_AMO) {
2057 if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
2058 ret |= CPSR_A;
2059 }
2060 }
2061
2062 return ret;
2063 }
2064
access_aa64_tid1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2065 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2066 bool isread)
2067 {
2068 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2069 return CP_ACCESS_TRAP_EL2;
2070 }
2071
2072 return CP_ACCESS_OK;
2073 }
2074
access_aa32_tid1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2075 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2076 bool isread)
2077 {
2078 if (arm_feature(env, ARM_FEATURE_V8)) {
2079 return access_aa64_tid1(env, ri, isread);
2080 }
2081
2082 return CP_ACCESS_OK;
2083 }
2084
2085 static const ARMCPRegInfo v7_cp_reginfo[] = {
2086 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2087 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2088 .access = PL1_W, .type = ARM_CP_NOP },
2089 /*
2090 * Performance monitors are implementation defined in v7,
2091 * but with an ARM recommended set of registers, which we
2092 * follow.
2093 *
2094 * Performance registers fall into three categories:
2095 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2096 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2097 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2098 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2099 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2100 */
2101 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
2102 .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
2103 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2104 .writefn = pmcntenset_write,
2105 .accessfn = pmreg_access,
2106 .fgt = FGT_PMCNTEN,
2107 .raw_writefn = raw_write },
2108 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
2109 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2110 .access = PL0_RW, .accessfn = pmreg_access,
2111 .fgt = FGT_PMCNTEN,
2112 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2113 .writefn = pmcntenset_write, .raw_writefn = raw_write },
2114 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
2115 .access = PL0_RW,
2116 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
2117 .accessfn = pmreg_access,
2118 .fgt = FGT_PMCNTEN,
2119 .writefn = pmcntenclr_write,
2120 .type = ARM_CP_ALIAS | ARM_CP_IO },
2121 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2122 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2123 .access = PL0_RW, .accessfn = pmreg_access,
2124 .fgt = FGT_PMCNTEN,
2125 .type = ARM_CP_ALIAS | ARM_CP_IO,
2126 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2127 .writefn = pmcntenclr_write },
2128 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
2129 .access = PL0_RW, .type = ARM_CP_IO,
2130 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2131 .accessfn = pmreg_access,
2132 .fgt = FGT_PMOVS,
2133 .writefn = pmovsr_write,
2134 .raw_writefn = raw_write },
2135 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2136 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2137 .access = PL0_RW, .accessfn = pmreg_access,
2138 .fgt = FGT_PMOVS,
2139 .type = ARM_CP_ALIAS | ARM_CP_IO,
2140 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2141 .writefn = pmovsr_write,
2142 .raw_writefn = raw_write },
2143 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
2144 .access = PL0_W, .accessfn = pmreg_access_swinc,
2145 .fgt = FGT_PMSWINC_EL0,
2146 .type = ARM_CP_NO_RAW | ARM_CP_IO,
2147 .writefn = pmswinc_write },
2148 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2149 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
2150 .access = PL0_W, .accessfn = pmreg_access_swinc,
2151 .fgt = FGT_PMSWINC_EL0,
2152 .type = ARM_CP_NO_RAW | ARM_CP_IO,
2153 .writefn = pmswinc_write },
2154 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2155 .access = PL0_RW, .type = ARM_CP_ALIAS,
2156 .fgt = FGT_PMSELR_EL0,
2157 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
2158 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
2159 .raw_writefn = raw_write},
2160 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2161 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
2162 .access = PL0_RW, .accessfn = pmreg_access_selr,
2163 .fgt = FGT_PMSELR_EL0,
2164 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2165 .writefn = pmselr_write, .raw_writefn = raw_write, },
2166 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
2167 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
2168 .fgt = FGT_PMCCNTR_EL0,
2169 .readfn = pmccntr_read, .writefn = pmccntr_write32,
2170 .accessfn = pmreg_access_ccntr },
2171 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2172 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
2173 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
2174 .fgt = FGT_PMCCNTR_EL0,
2175 .type = ARM_CP_IO,
2176 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2177 .readfn = pmccntr_read, .writefn = pmccntr_write,
2178 .raw_readfn = raw_read, .raw_writefn = raw_write, },
2179 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2180 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2181 .access = PL0_RW, .accessfn = pmreg_access,
2182 .fgt = FGT_PMCCFILTR_EL0,
2183 .type = ARM_CP_ALIAS | ARM_CP_IO,
2184 .resetvalue = 0, },
2185 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2186 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
2187 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
2188 .access = PL0_RW, .accessfn = pmreg_access,
2189 .fgt = FGT_PMCCFILTR_EL0,
2190 .type = ARM_CP_IO,
2191 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2192 .resetvalue = 0, },
2193 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
2194 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2195 .accessfn = pmreg_access,
2196 .fgt = FGT_PMEVTYPERN_EL0,
2197 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2198 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2199 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
2200 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2201 .accessfn = pmreg_access,
2202 .fgt = FGT_PMEVTYPERN_EL0,
2203 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2204 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
2205 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2206 .accessfn = pmreg_access_xevcntr,
2207 .fgt = FGT_PMEVCNTRN_EL0,
2208 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2209 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2210 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2211 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2212 .accessfn = pmreg_access_xevcntr,
2213 .fgt = FGT_PMEVCNTRN_EL0,
2214 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2215 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2216 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2217 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2218 .resetvalue = 0,
2219 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2220 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2221 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2222 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2223 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2224 .resetvalue = 0,
2225 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2226 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2227 .access = PL1_RW, .accessfn = access_tpm,
2228 .fgt = FGT_PMINTEN,
2229 .type = ARM_CP_ALIAS | ARM_CP_IO,
2230 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2231 .resetvalue = 0,
2232 .writefn = pmintenset_write, .raw_writefn = raw_write },
2233 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2234 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2235 .access = PL1_RW, .accessfn = access_tpm,
2236 .fgt = FGT_PMINTEN,
2237 .type = ARM_CP_IO,
2238 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2239 .writefn = pmintenset_write, .raw_writefn = raw_write,
2240 .resetvalue = 0x0 },
2241 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2242 .access = PL1_RW, .accessfn = access_tpm,
2243 .fgt = FGT_PMINTEN,
2244 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2245 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2246 .writefn = pmintenclr_write, },
2247 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2248 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2249 .access = PL1_RW, .accessfn = access_tpm,
2250 .fgt = FGT_PMINTEN,
2251 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2252 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2253 .writefn = pmintenclr_write },
2254 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2255 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2256 .access = PL1_R,
2257 .accessfn = access_tid4,
2258 .fgt = FGT_CCSIDR_EL1,
2259 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2260 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2261 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2262 .access = PL1_RW,
2263 .accessfn = access_tid4,
2264 .fgt = FGT_CSSELR_EL1,
2265 .writefn = csselr_write, .resetvalue = 0,
2266 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2267 offsetof(CPUARMState, cp15.csselr_ns) } },
2268 /*
2269 * Auxiliary ID register: this actually has an IMPDEF value but for now
2270 * just RAZ for all cores:
2271 */
2272 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2273 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2274 .access = PL1_R, .type = ARM_CP_CONST,
2275 .accessfn = access_aa64_tid1,
2276 .fgt = FGT_AIDR_EL1,
2277 .resetvalue = 0 },
2278 /*
2279 * Auxiliary fault status registers: these also are IMPDEF, and we
2280 * choose to RAZ/WI for all cores.
2281 */
2282 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2283 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2284 .access = PL1_RW, .accessfn = access_tvm_trvm,
2285 .fgt = FGT_AFSR0_EL1,
2286 .nv2_redirect_offset = 0x128 | NV2_REDIR_NV1,
2287 .type = ARM_CP_CONST, .resetvalue = 0 },
2288 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2289 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2290 .access = PL1_RW, .accessfn = access_tvm_trvm,
2291 .fgt = FGT_AFSR1_EL1,
2292 .nv2_redirect_offset = 0x130 | NV2_REDIR_NV1,
2293 .type = ARM_CP_CONST, .resetvalue = 0 },
2294 /*
2295 * MAIR can just read-as-written because we don't implement caches
2296 * and so don't need to care about memory attributes.
2297 */
2298 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2299 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2300 .access = PL1_RW, .accessfn = access_tvm_trvm,
2301 .fgt = FGT_MAIR_EL1,
2302 .nv2_redirect_offset = 0x140 | NV2_REDIR_NV1,
2303 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2304 .resetvalue = 0 },
2305 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2306 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2307 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2308 .resetvalue = 0 },
2309 /*
2310 * For non-long-descriptor page tables these are PRRR and NMRR;
2311 * regardless they still act as reads-as-written for QEMU.
2312 */
2313 /*
2314 * MAIR0/1 are defined separately from their 64-bit counterpart which
2315 * allows them to assign the correct fieldoffset based on the endianness
2316 * handled in the field definitions.
2317 */
2318 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2319 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2320 .access = PL1_RW, .accessfn = access_tvm_trvm,
2321 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2322 offsetof(CPUARMState, cp15.mair0_ns) },
2323 .resetfn = arm_cp_reset_ignore },
2324 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2325 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2326 .access = PL1_RW, .accessfn = access_tvm_trvm,
2327 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2328 offsetof(CPUARMState, cp15.mair1_ns) },
2329 .resetfn = arm_cp_reset_ignore },
2330 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2331 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2332 .fgt = FGT_ISR_EL1,
2333 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2334 /* 32 bit ITLB invalidates */
2335 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
2336 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2337 .writefn = tlbiall_write },
2338 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
2339 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2340 .writefn = tlbimva_write },
2341 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
2342 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2343 .writefn = tlbiasid_write },
2344 /* 32 bit DTLB invalidates */
2345 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
2346 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2347 .writefn = tlbiall_write },
2348 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
2349 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2350 .writefn = tlbimva_write },
2351 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
2352 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2353 .writefn = tlbiasid_write },
2354 /* 32 bit TLB invalidates */
2355 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
2356 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2357 .writefn = tlbiall_write },
2358 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
2359 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2360 .writefn = tlbimva_write },
2361 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
2362 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2363 .writefn = tlbiasid_write },
2364 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
2365 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2366 .writefn = tlbimvaa_write },
2367 };
2368
2369 static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2370 /* 32 bit TLB invalidates, Inner Shareable */
2371 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
2372 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2373 .writefn = tlbiall_is_write },
2374 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
2375 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2376 .writefn = tlbimva_is_write },
2377 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
2378 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2379 .writefn = tlbiasid_is_write },
2380 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
2381 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
2382 .writefn = tlbimvaa_is_write },
2383 };
2384
2385 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2386 /* PMOVSSET is not implemented in v7 before v7ve */
2387 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2388 .access = PL0_RW, .accessfn = pmreg_access,
2389 .fgt = FGT_PMOVS,
2390 .type = ARM_CP_ALIAS | ARM_CP_IO,
2391 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2392 .writefn = pmovsset_write,
2393 .raw_writefn = raw_write },
2394 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2395 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2396 .access = PL0_RW, .accessfn = pmreg_access,
2397 .fgt = FGT_PMOVS,
2398 .type = ARM_CP_ALIAS | ARM_CP_IO,
2399 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2400 .writefn = pmovsset_write,
2401 .raw_writefn = raw_write },
2402 };
2403
teecr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2404 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2405 uint64_t value)
2406 {
2407 value &= 1;
2408 env->teecr = value;
2409 }
2410
teecr_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2411 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2412 bool isread)
2413 {
2414 /*
2415 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2416 * at all, so we don't need to check whether we're v8A.
2417 */
2418 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2419 (env->cp15.hstr_el2 & HSTR_TTEE)) {
2420 return CP_ACCESS_TRAP_EL2;
2421 }
2422 return CP_ACCESS_OK;
2423 }
2424
teehbr_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2425 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2426 bool isread)
2427 {
2428 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2429 return CP_ACCESS_TRAP;
2430 }
2431 return teecr_access(env, ri, isread);
2432 }
2433
2434 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2435 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2436 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2437 .resetvalue = 0,
2438 .writefn = teecr_write, .accessfn = teecr_access },
2439 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2440 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2441 .accessfn = teehbr_access, .resetvalue = 0 },
2442 };
2443
2444 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2445 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2446 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2447 .access = PL0_RW,
2448 .fgt = FGT_TPIDR_EL0,
2449 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2450 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2451 .access = PL0_RW,
2452 .fgt = FGT_TPIDR_EL0,
2453 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2454 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2455 .resetfn = arm_cp_reset_ignore },
2456 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2457 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2458 .access = PL0_R | PL1_W,
2459 .fgt = FGT_TPIDRRO_EL0,
2460 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2461 .resetvalue = 0},
2462 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2463 .access = PL0_R | PL1_W,
2464 .fgt = FGT_TPIDRRO_EL0,
2465 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2466 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2467 .resetfn = arm_cp_reset_ignore },
2468 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2469 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2470 .access = PL1_RW,
2471 .fgt = FGT_TPIDR_EL1,
2472 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2473 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2474 .access = PL1_RW,
2475 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2476 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2477 .resetvalue = 0 },
2478 };
2479
arm_gt_cntfrq_reset(CPUARMState * env,const ARMCPRegInfo * opaque)2480 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2481 {
2482 ARMCPU *cpu = env_archcpu(env);
2483
2484 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2485 }
2486
2487 #ifndef CONFIG_USER_ONLY
2488
gt_cntfrq_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2489 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2490 bool isread)
2491 {
2492 /*
2493 * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2494 * Writable only at the highest implemented exception level.
2495 */
2496 int el = arm_current_el(env);
2497 uint64_t hcr;
2498 uint32_t cntkctl;
2499
2500 switch (el) {
2501 case 0:
2502 hcr = arm_hcr_el2_eff(env);
2503 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2504 cntkctl = env->cp15.cnthctl_el2;
2505 } else {
2506 cntkctl = env->cp15.c14_cntkctl;
2507 }
2508 if (!extract32(cntkctl, 0, 2)) {
2509 return CP_ACCESS_TRAP;
2510 }
2511 break;
2512 case 1:
2513 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2514 arm_is_secure_below_el3(env)) {
2515 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2516 return CP_ACCESS_TRAP_UNCATEGORIZED;
2517 }
2518 break;
2519 case 2:
2520 case 3:
2521 break;
2522 }
2523
2524 if (!isread && el < arm_highest_el(env)) {
2525 return CP_ACCESS_TRAP_UNCATEGORIZED;
2526 }
2527
2528 return CP_ACCESS_OK;
2529 }
2530
gt_counter_access(CPUARMState * env,int timeridx,bool isread)2531 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2532 bool isread)
2533 {
2534 unsigned int cur_el = arm_current_el(env);
2535 bool has_el2 = arm_is_el2_enabled(env);
2536 uint64_t hcr = arm_hcr_el2_eff(env);
2537
2538 switch (cur_el) {
2539 case 0:
2540 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2541 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2542 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2543 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2544 }
2545
2546 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2547 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2548 return CP_ACCESS_TRAP;
2549 }
2550 /* fall through */
2551 case 1:
2552 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2553 if (has_el2 && timeridx == GTIMER_PHYS &&
2554 (hcr & HCR_E2H
2555 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2556 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2557 return CP_ACCESS_TRAP_EL2;
2558 }
2559 if (has_el2 && timeridx == GTIMER_VIRT) {
2560 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVCT)) {
2561 return CP_ACCESS_TRAP_EL2;
2562 }
2563 }
2564 break;
2565 }
2566 return CP_ACCESS_OK;
2567 }
2568
gt_timer_access(CPUARMState * env,int timeridx,bool isread)2569 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2570 bool isread)
2571 {
2572 unsigned int cur_el = arm_current_el(env);
2573 bool has_el2 = arm_is_el2_enabled(env);
2574 uint64_t hcr = arm_hcr_el2_eff(env);
2575
2576 switch (cur_el) {
2577 case 0:
2578 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2579 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2580 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2581 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2582 }
2583
2584 /*
2585 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2586 * EL0 if EL0[PV]TEN is zero.
2587 */
2588 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2589 return CP_ACCESS_TRAP;
2590 }
2591 /* fall through */
2592
2593 case 1:
2594 if (has_el2 && timeridx == GTIMER_PHYS) {
2595 if (hcr & HCR_E2H) {
2596 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2597 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2598 return CP_ACCESS_TRAP_EL2;
2599 }
2600 } else {
2601 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2602 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2603 return CP_ACCESS_TRAP_EL2;
2604 }
2605 }
2606 }
2607 if (has_el2 && timeridx == GTIMER_VIRT) {
2608 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVT)) {
2609 return CP_ACCESS_TRAP_EL2;
2610 }
2611 }
2612 break;
2613 }
2614 return CP_ACCESS_OK;
2615 }
2616
gt_pct_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2617 static CPAccessResult gt_pct_access(CPUARMState *env,
2618 const ARMCPRegInfo *ri,
2619 bool isread)
2620 {
2621 return gt_counter_access(env, GTIMER_PHYS, isread);
2622 }
2623
gt_vct_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2624 static CPAccessResult gt_vct_access(CPUARMState *env,
2625 const ARMCPRegInfo *ri,
2626 bool isread)
2627 {
2628 return gt_counter_access(env, GTIMER_VIRT, isread);
2629 }
2630
gt_ptimer_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2631 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2632 bool isread)
2633 {
2634 return gt_timer_access(env, GTIMER_PHYS, isread);
2635 }
2636
gt_vtimer_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2637 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2638 bool isread)
2639 {
2640 return gt_timer_access(env, GTIMER_VIRT, isread);
2641 }
2642
gt_stimer_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2643 static CPAccessResult gt_stimer_access(CPUARMState *env,
2644 const ARMCPRegInfo *ri,
2645 bool isread)
2646 {
2647 /*
2648 * The AArch64 register view of the secure physical timer is
2649 * always accessible from EL3, and configurably accessible from
2650 * Secure EL1.
2651 */
2652 switch (arm_current_el(env)) {
2653 case 1:
2654 if (!arm_is_secure(env)) {
2655 return CP_ACCESS_TRAP_UNCATEGORIZED;
2656 }
2657 if (arm_is_el2_enabled(env)) {
2658 return CP_ACCESS_TRAP_UNCATEGORIZED;
2659 }
2660 if (!(env->cp15.scr_el3 & SCR_ST)) {
2661 return CP_ACCESS_TRAP_EL3;
2662 }
2663 return CP_ACCESS_OK;
2664 case 0:
2665 case 2:
2666 return CP_ACCESS_TRAP_UNCATEGORIZED;
2667 case 3:
2668 return CP_ACCESS_OK;
2669 default:
2670 g_assert_not_reached();
2671 }
2672 }
2673
gt_sel2timer_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2674 static CPAccessResult gt_sel2timer_access(CPUARMState *env,
2675 const ARMCPRegInfo *ri,
2676 bool isread)
2677 {
2678 /*
2679 * The AArch64 register view of the secure EL2 timers are mostly
2680 * accessible from EL3 and EL2 although can also be trapped to EL2
2681 * from EL1 depending on nested virt config.
2682 */
2683 switch (arm_current_el(env)) {
2684 case 0: /* UNDEFINED */
2685 return CP_ACCESS_TRAP_UNCATEGORIZED;
2686 case 1:
2687 if (!arm_is_secure(env)) {
2688 /* UNDEFINED */
2689 return CP_ACCESS_TRAP_UNCATEGORIZED;
2690 } else if (arm_hcr_el2_eff(env) & HCR_NV) {
2691 /* Aarch64.SystemAccessTrap(EL2, 0x18) */
2692 return CP_ACCESS_TRAP_EL2;
2693 }
2694 /* UNDEFINED */
2695 return CP_ACCESS_TRAP_UNCATEGORIZED;
2696 case 2:
2697 if (!arm_is_secure(env)) {
2698 /* UNDEFINED */
2699 return CP_ACCESS_TRAP_UNCATEGORIZED;
2700 }
2701 return CP_ACCESS_OK;
2702 case 3:
2703 if (env->cp15.scr_el3 & SCR_EEL2) {
2704 return CP_ACCESS_OK;
2705 } else {
2706 return CP_ACCESS_TRAP_UNCATEGORIZED;
2707 }
2708 default:
2709 g_assert_not_reached();
2710 }
2711 }
2712
gt_get_countervalue(CPUARMState * env)2713 uint64_t gt_get_countervalue(CPUARMState *env)
2714 {
2715 ARMCPU *cpu = env_archcpu(env);
2716
2717 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2718 }
2719
gt_update_irq(ARMCPU * cpu,int timeridx)2720 static void gt_update_irq(ARMCPU *cpu, int timeridx)
2721 {
2722 CPUARMState *env = &cpu->env;
2723 uint64_t cnthctl = env->cp15.cnthctl_el2;
2724 ARMSecuritySpace ss = arm_security_space(env);
2725 /* ISTATUS && !IMASK */
2726 int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2727
2728 /*
2729 * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2730 * It is RES0 in Secure and NonSecure state.
2731 */
2732 if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2733 ((timeridx == GTIMER_VIRT && (cnthctl & R_CNTHCTL_CNTVMASK_MASK)) ||
2734 (timeridx == GTIMER_PHYS && (cnthctl & R_CNTHCTL_CNTPMASK_MASK)))) {
2735 irqstate = 0;
2736 }
2737
2738 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2739 trace_arm_gt_update_irq(timeridx, irqstate);
2740 }
2741
gt_rme_post_el_change(ARMCPU * cpu,void * ignored)2742 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2743 {
2744 /*
2745 * Changing security state between Root and Secure/NonSecure, which may
2746 * happen when switching EL, can change the effective value of CNTHCTL_EL2
2747 * mask bits. Update the IRQ state accordingly.
2748 */
2749 gt_update_irq(cpu, GTIMER_VIRT);
2750 gt_update_irq(cpu, GTIMER_PHYS);
2751 }
2752
gt_phys_raw_cnt_offset(CPUARMState * env)2753 static uint64_t gt_phys_raw_cnt_offset(CPUARMState *env)
2754 {
2755 if ((env->cp15.scr_el3 & SCR_ECVEN) &&
2756 FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, ECV) &&
2757 arm_is_el2_enabled(env) &&
2758 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
2759 return env->cp15.cntpoff_el2;
2760 }
2761 return 0;
2762 }
2763
gt_indirect_access_timer_offset(CPUARMState * env,int timeridx)2764 static uint64_t gt_indirect_access_timer_offset(CPUARMState *env, int timeridx)
2765 {
2766 /*
2767 * Return the timer offset to use for indirect accesses to the timer.
2768 * This is the Offset value as defined in D12.2.4.1 "Operation of the
2769 * CompareValue views of the timers".
2770 *
2771 * The condition here is not always the same as the condition for
2772 * whether to apply an offset register when doing a direct read of
2773 * the counter sysreg; those conditions are described in the
2774 * access pseudocode for each counter register.
2775 */
2776 switch (timeridx) {
2777 case GTIMER_PHYS:
2778 return gt_phys_raw_cnt_offset(env);
2779 case GTIMER_VIRT:
2780 return env->cp15.cntvoff_el2;
2781 case GTIMER_HYP:
2782 case GTIMER_SEC:
2783 case GTIMER_HYPVIRT:
2784 case GTIMER_S_EL2_PHYS:
2785 case GTIMER_S_EL2_VIRT:
2786 return 0;
2787 default:
2788 g_assert_not_reached();
2789 }
2790 }
2791
gt_direct_access_timer_offset(CPUARMState * env,int timeridx)2792 uint64_t gt_direct_access_timer_offset(CPUARMState *env, int timeridx)
2793 {
2794 /*
2795 * Return the timer offset to use for direct accesses to the
2796 * counter registers CNTPCT and CNTVCT, and for direct accesses
2797 * to the CNT*_TVAL registers.
2798 *
2799 * This isn't exactly the same as the indirect-access offset,
2800 * because here we also care about what EL the register access
2801 * is being made from.
2802 *
2803 * This corresponds to the access pseudocode for the registers.
2804 */
2805 uint64_t hcr;
2806
2807 switch (timeridx) {
2808 case GTIMER_PHYS:
2809 if (arm_current_el(env) >= 2) {
2810 return 0;
2811 }
2812 return gt_phys_raw_cnt_offset(env);
2813 case GTIMER_VIRT:
2814 switch (arm_current_el(env)) {
2815 case 2:
2816 hcr = arm_hcr_el2_eff(env);
2817 if (hcr & HCR_E2H) {
2818 return 0;
2819 }
2820 break;
2821 case 0:
2822 hcr = arm_hcr_el2_eff(env);
2823 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2824 return 0;
2825 }
2826 break;
2827 }
2828 return env->cp15.cntvoff_el2;
2829 case GTIMER_HYP:
2830 case GTIMER_SEC:
2831 case GTIMER_HYPVIRT:
2832 case GTIMER_S_EL2_PHYS:
2833 case GTIMER_S_EL2_VIRT:
2834 return 0;
2835 default:
2836 g_assert_not_reached();
2837 }
2838 }
2839
gt_recalc_timer(ARMCPU * cpu,int timeridx)2840 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2841 {
2842 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2843
2844 if (gt->ctl & 1) {
2845 /*
2846 * Timer enabled: calculate and set current ISTATUS, irq, and
2847 * reset timer to when ISTATUS next has to change
2848 */
2849 uint64_t offset = gt_indirect_access_timer_offset(&cpu->env, timeridx);
2850 uint64_t count = gt_get_countervalue(&cpu->env);
2851 /* Note that this must be unsigned 64 bit arithmetic: */
2852 int istatus = count - offset >= gt->cval;
2853 uint64_t nexttick;
2854
2855 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2856
2857 if (istatus) {
2858 /*
2859 * Next transition is when (count - offset) rolls back over to 0.
2860 * If offset > count then this is when count == offset;
2861 * if offset <= count then this is when count == offset + 2^64
2862 * For the latter case we set nexttick to an "as far in future
2863 * as possible" value and let the code below handle it.
2864 */
2865 if (offset > count) {
2866 nexttick = offset;
2867 } else {
2868 nexttick = UINT64_MAX;
2869 }
2870 } else {
2871 /*
2872 * Next transition is when (count - offset) == cval, i.e.
2873 * when count == (cval + offset).
2874 * If that would overflow, then again we set up the next interrupt
2875 * for "as far in the future as possible" for the code below.
2876 */
2877 if (uadd64_overflow(gt->cval, offset, &nexttick)) {
2878 nexttick = UINT64_MAX;
2879 }
2880 }
2881 /*
2882 * Note that the desired next expiry time might be beyond the
2883 * signed-64-bit range of a QEMUTimer -- in this case we just
2884 * set the timer for as far in the future as possible. When the
2885 * timer expires we will reset the timer for any remaining period.
2886 */
2887 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2888 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2889 } else {
2890 timer_mod(cpu->gt_timer[timeridx], nexttick);
2891 }
2892 trace_arm_gt_recalc(timeridx, nexttick);
2893 } else {
2894 /* Timer disabled: ISTATUS and timer output always clear */
2895 gt->ctl &= ~4;
2896 timer_del(cpu->gt_timer[timeridx]);
2897 trace_arm_gt_recalc_disabled(timeridx);
2898 }
2899 gt_update_irq(cpu, timeridx);
2900 }
2901
gt_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx)2902 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2903 int timeridx)
2904 {
2905 ARMCPU *cpu = env_archcpu(env);
2906
2907 timer_del(cpu->gt_timer[timeridx]);
2908 }
2909
gt_cnt_read(CPUARMState * env,const ARMCPRegInfo * ri)2910 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2911 {
2912 uint64_t offset = gt_direct_access_timer_offset(env, GTIMER_PHYS);
2913 return gt_get_countervalue(env) - offset;
2914 }
2915
gt_virt_cnt_read(CPUARMState * env,const ARMCPRegInfo * ri)2916 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2917 {
2918 uint64_t offset = gt_direct_access_timer_offset(env, GTIMER_VIRT);
2919 return gt_get_countervalue(env) - offset;
2920 }
2921
gt_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx,uint64_t value)2922 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2923 int timeridx,
2924 uint64_t value)
2925 {
2926 trace_arm_gt_cval_write(timeridx, value);
2927 env->cp15.c14_timer[timeridx].cval = value;
2928 gt_recalc_timer(env_archcpu(env), timeridx);
2929 }
2930
do_tval_read(CPUARMState * env,int timeridx,uint64_t offset)2931 static uint64_t do_tval_read(CPUARMState *env, int timeridx, uint64_t offset)
2932 {
2933 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2934 (gt_get_countervalue(env) - offset));
2935 }
2936
gt_tval_read(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx)2937 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2938 int timeridx)
2939 {
2940 uint64_t offset = gt_direct_access_timer_offset(env, timeridx);
2941
2942 return do_tval_read(env, timeridx, offset);
2943 }
2944
do_tval_write(CPUARMState * env,int timeridx,uint64_t value,uint64_t offset)2945 static void do_tval_write(CPUARMState *env, int timeridx, uint64_t value,
2946 uint64_t offset)
2947 {
2948 trace_arm_gt_tval_write(timeridx, value);
2949 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2950 sextract64(value, 0, 32);
2951 gt_recalc_timer(env_archcpu(env), timeridx);
2952 }
2953
gt_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx,uint64_t value)2954 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2955 int timeridx,
2956 uint64_t value)
2957 {
2958 uint64_t offset = gt_direct_access_timer_offset(env, timeridx);
2959
2960 do_tval_write(env, timeridx, value, offset);
2961 }
2962
gt_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx,uint64_t value)2963 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2964 int timeridx,
2965 uint64_t value)
2966 {
2967 ARMCPU *cpu = env_archcpu(env);
2968 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2969
2970 trace_arm_gt_ctl_write(timeridx, value);
2971 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2972 if ((oldval ^ value) & 1) {
2973 /* Enable toggled */
2974 gt_recalc_timer(cpu, timeridx);
2975 } else if ((oldval ^ value) & 2) {
2976 /*
2977 * IMASK toggled: don't need to recalculate,
2978 * just set the interrupt line based on ISTATUS
2979 */
2980 trace_arm_gt_imask_toggle(timeridx);
2981 gt_update_irq(cpu, timeridx);
2982 }
2983 }
2984
gt_phys_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)2985 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2986 {
2987 gt_timer_reset(env, ri, GTIMER_PHYS);
2988 }
2989
gt_phys_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2990 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2991 uint64_t value)
2992 {
2993 gt_cval_write(env, ri, GTIMER_PHYS, value);
2994 }
2995
gt_phys_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)2996 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2997 {
2998 return gt_tval_read(env, ri, GTIMER_PHYS);
2999 }
3000
gt_phys_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3001 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3002 uint64_t value)
3003 {
3004 gt_tval_write(env, ri, GTIMER_PHYS, value);
3005 }
3006
gt_phys_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3007 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3008 uint64_t value)
3009 {
3010 gt_ctl_write(env, ri, GTIMER_PHYS, value);
3011 }
3012
gt_phys_redir_timeridx(CPUARMState * env)3013 static int gt_phys_redir_timeridx(CPUARMState *env)
3014 {
3015 switch (arm_mmu_idx(env)) {
3016 case ARMMMUIdx_E20_0:
3017 case ARMMMUIdx_E20_2:
3018 case ARMMMUIdx_E20_2_PAN:
3019 return GTIMER_HYP;
3020 default:
3021 return GTIMER_PHYS;
3022 }
3023 }
3024
gt_virt_redir_timeridx(CPUARMState * env)3025 static int gt_virt_redir_timeridx(CPUARMState *env)
3026 {
3027 switch (arm_mmu_idx(env)) {
3028 case ARMMMUIdx_E20_0:
3029 case ARMMMUIdx_E20_2:
3030 case ARMMMUIdx_E20_2_PAN:
3031 return GTIMER_HYPVIRT;
3032 default:
3033 return GTIMER_VIRT;
3034 }
3035 }
3036
gt_phys_redir_cval_read(CPUARMState * env,const ARMCPRegInfo * ri)3037 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
3038 const ARMCPRegInfo *ri)
3039 {
3040 int timeridx = gt_phys_redir_timeridx(env);
3041 return env->cp15.c14_timer[timeridx].cval;
3042 }
3043
gt_phys_redir_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3044 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3045 uint64_t value)
3046 {
3047 int timeridx = gt_phys_redir_timeridx(env);
3048 gt_cval_write(env, ri, timeridx, value);
3049 }
3050
gt_phys_redir_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3051 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
3052 const ARMCPRegInfo *ri)
3053 {
3054 int timeridx = gt_phys_redir_timeridx(env);
3055 return gt_tval_read(env, ri, timeridx);
3056 }
3057
gt_phys_redir_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3058 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3059 uint64_t value)
3060 {
3061 int timeridx = gt_phys_redir_timeridx(env);
3062 gt_tval_write(env, ri, timeridx, value);
3063 }
3064
gt_phys_redir_ctl_read(CPUARMState * env,const ARMCPRegInfo * ri)3065 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
3066 const ARMCPRegInfo *ri)
3067 {
3068 int timeridx = gt_phys_redir_timeridx(env);
3069 return env->cp15.c14_timer[timeridx].ctl;
3070 }
3071
gt_phys_redir_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3072 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3073 uint64_t value)
3074 {
3075 int timeridx = gt_phys_redir_timeridx(env);
3076 gt_ctl_write(env, ri, timeridx, value);
3077 }
3078
gt_virt_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)3079 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3080 {
3081 gt_timer_reset(env, ri, GTIMER_VIRT);
3082 }
3083
gt_virt_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3084 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3085 uint64_t value)
3086 {
3087 gt_cval_write(env, ri, GTIMER_VIRT, value);
3088 }
3089
gt_virt_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3090 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3091 {
3092 /*
3093 * This is CNTV_TVAL_EL02; unlike the underlying CNTV_TVAL_EL0
3094 * we always apply CNTVOFF_EL2. Special case that here rather
3095 * than going into the generic gt_tval_read() and then having
3096 * to re-detect that it's this register.
3097 * Note that the accessfn/perms mean we know we're at EL2 or EL3 here.
3098 */
3099 return do_tval_read(env, GTIMER_VIRT, env->cp15.cntvoff_el2);
3100 }
3101
gt_virt_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3102 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3103 uint64_t value)
3104 {
3105 /* Similarly for writes to CNTV_TVAL_EL02 */
3106 do_tval_write(env, GTIMER_VIRT, value, env->cp15.cntvoff_el2);
3107 }
3108
gt_virt_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3109 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3110 uint64_t value)
3111 {
3112 gt_ctl_write(env, ri, GTIMER_VIRT, value);
3113 }
3114
gt_cnthctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3115 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3116 uint64_t value)
3117 {
3118 ARMCPU *cpu = env_archcpu(env);
3119 uint32_t oldval = env->cp15.cnthctl_el2;
3120 uint32_t valid_mask =
3121 R_CNTHCTL_EL0PCTEN_E2H1_MASK |
3122 R_CNTHCTL_EL0VCTEN_E2H1_MASK |
3123 R_CNTHCTL_EVNTEN_MASK |
3124 R_CNTHCTL_EVNTDIR_MASK |
3125 R_CNTHCTL_EVNTI_MASK |
3126 R_CNTHCTL_EL0VTEN_MASK |
3127 R_CNTHCTL_EL0PTEN_MASK |
3128 R_CNTHCTL_EL1PCTEN_E2H1_MASK |
3129 R_CNTHCTL_EL1PTEN_MASK;
3130
3131 if (cpu_isar_feature(aa64_rme, cpu)) {
3132 valid_mask |= R_CNTHCTL_CNTVMASK_MASK | R_CNTHCTL_CNTPMASK_MASK;
3133 }
3134 if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
3135 valid_mask |=
3136 R_CNTHCTL_EL1TVT_MASK |
3137 R_CNTHCTL_EL1TVCT_MASK |
3138 R_CNTHCTL_EL1NVPCT_MASK |
3139 R_CNTHCTL_EL1NVVCT_MASK |
3140 R_CNTHCTL_EVNTIS_MASK;
3141 }
3142 if (cpu_isar_feature(aa64_ecv, cpu)) {
3143 valid_mask |= R_CNTHCTL_ECV_MASK;
3144 }
3145
3146 /* Clear RES0 bits */
3147 value &= valid_mask;
3148
3149 raw_write(env, ri, value);
3150
3151 if ((oldval ^ value) & R_CNTHCTL_CNTVMASK_MASK) {
3152 gt_update_irq(cpu, GTIMER_VIRT);
3153 } else if ((oldval ^ value) & R_CNTHCTL_CNTPMASK_MASK) {
3154 gt_update_irq(cpu, GTIMER_PHYS);
3155 }
3156 }
3157
gt_cntvoff_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3158 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
3159 uint64_t value)
3160 {
3161 ARMCPU *cpu = env_archcpu(env);
3162
3163 trace_arm_gt_cntvoff_write(value);
3164 raw_write(env, ri, value);
3165 gt_recalc_timer(cpu, GTIMER_VIRT);
3166 }
3167
gt_virt_redir_cval_read(CPUARMState * env,const ARMCPRegInfo * ri)3168 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
3169 const ARMCPRegInfo *ri)
3170 {
3171 int timeridx = gt_virt_redir_timeridx(env);
3172 return env->cp15.c14_timer[timeridx].cval;
3173 }
3174
gt_virt_redir_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3175 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3176 uint64_t value)
3177 {
3178 int timeridx = gt_virt_redir_timeridx(env);
3179 gt_cval_write(env, ri, timeridx, value);
3180 }
3181
gt_virt_redir_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3182 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
3183 const ARMCPRegInfo *ri)
3184 {
3185 int timeridx = gt_virt_redir_timeridx(env);
3186 return gt_tval_read(env, ri, timeridx);
3187 }
3188
gt_virt_redir_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3189 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3190 uint64_t value)
3191 {
3192 int timeridx = gt_virt_redir_timeridx(env);
3193 gt_tval_write(env, ri, timeridx, value);
3194 }
3195
gt_virt_redir_ctl_read(CPUARMState * env,const ARMCPRegInfo * ri)3196 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
3197 const ARMCPRegInfo *ri)
3198 {
3199 int timeridx = gt_virt_redir_timeridx(env);
3200 return env->cp15.c14_timer[timeridx].ctl;
3201 }
3202
gt_virt_redir_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3203 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3204 uint64_t value)
3205 {
3206 int timeridx = gt_virt_redir_timeridx(env);
3207 gt_ctl_write(env, ri, timeridx, value);
3208 }
3209
gt_hyp_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)3210 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3211 {
3212 gt_timer_reset(env, ri, GTIMER_HYP);
3213 }
3214
gt_hyp_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3215 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3216 uint64_t value)
3217 {
3218 gt_cval_write(env, ri, GTIMER_HYP, value);
3219 }
3220
gt_hyp_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3221 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3222 {
3223 return gt_tval_read(env, ri, GTIMER_HYP);
3224 }
3225
gt_hyp_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3226 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3227 uint64_t value)
3228 {
3229 gt_tval_write(env, ri, GTIMER_HYP, value);
3230 }
3231
gt_hyp_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3232 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3233 uint64_t value)
3234 {
3235 gt_ctl_write(env, ri, GTIMER_HYP, value);
3236 }
3237
gt_sec_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)3238 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3239 {
3240 gt_timer_reset(env, ri, GTIMER_SEC);
3241 }
3242
gt_sec_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3243 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3244 uint64_t value)
3245 {
3246 gt_cval_write(env, ri, GTIMER_SEC, value);
3247 }
3248
gt_sec_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3249 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3250 {
3251 return gt_tval_read(env, ri, GTIMER_SEC);
3252 }
3253
gt_sec_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3254 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3255 uint64_t value)
3256 {
3257 gt_tval_write(env, ri, GTIMER_SEC, value);
3258 }
3259
gt_sec_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3260 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3261 uint64_t value)
3262 {
3263 gt_ctl_write(env, ri, GTIMER_SEC, value);
3264 }
3265
gt_sec_pel2_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)3266 static void gt_sec_pel2_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3267 {
3268 gt_timer_reset(env, ri, GTIMER_S_EL2_PHYS);
3269 }
3270
gt_sec_pel2_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3271 static void gt_sec_pel2_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3272 uint64_t value)
3273 {
3274 gt_cval_write(env, ri, GTIMER_S_EL2_PHYS, value);
3275 }
3276
gt_sec_pel2_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3277 static uint64_t gt_sec_pel2_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3278 {
3279 return gt_tval_read(env, ri, GTIMER_S_EL2_PHYS);
3280 }
3281
gt_sec_pel2_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3282 static void gt_sec_pel2_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3283 uint64_t value)
3284 {
3285 gt_tval_write(env, ri, GTIMER_S_EL2_PHYS, value);
3286 }
3287
gt_sec_pel2_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3288 static void gt_sec_pel2_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3289 uint64_t value)
3290 {
3291 gt_ctl_write(env, ri, GTIMER_S_EL2_PHYS, value);
3292 }
3293
gt_sec_vel2_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)3294 static void gt_sec_vel2_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3295 {
3296 gt_timer_reset(env, ri, GTIMER_S_EL2_VIRT);
3297 }
3298
gt_sec_vel2_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3299 static void gt_sec_vel2_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3300 uint64_t value)
3301 {
3302 gt_cval_write(env, ri, GTIMER_S_EL2_VIRT, value);
3303 }
3304
gt_sec_vel2_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3305 static uint64_t gt_sec_vel2_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3306 {
3307 return gt_tval_read(env, ri, GTIMER_S_EL2_VIRT);
3308 }
3309
gt_sec_vel2_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3310 static void gt_sec_vel2_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3311 uint64_t value)
3312 {
3313 gt_tval_write(env, ri, GTIMER_S_EL2_VIRT, value);
3314 }
3315
gt_sec_vel2_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3316 static void gt_sec_vel2_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3317 uint64_t value)
3318 {
3319 gt_ctl_write(env, ri, GTIMER_S_EL2_VIRT, value);
3320 }
3321
gt_hv_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)3322 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3323 {
3324 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3325 }
3326
gt_hv_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3327 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3328 uint64_t value)
3329 {
3330 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3331 }
3332
gt_hv_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3333 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3334 {
3335 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3336 }
3337
gt_hv_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3338 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3339 uint64_t value)
3340 {
3341 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3342 }
3343
gt_hv_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3344 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3345 uint64_t value)
3346 {
3347 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3348 }
3349
arm_gt_ptimer_cb(void * opaque)3350 void arm_gt_ptimer_cb(void *opaque)
3351 {
3352 ARMCPU *cpu = opaque;
3353
3354 gt_recalc_timer(cpu, GTIMER_PHYS);
3355 }
3356
arm_gt_vtimer_cb(void * opaque)3357 void arm_gt_vtimer_cb(void *opaque)
3358 {
3359 ARMCPU *cpu = opaque;
3360
3361 gt_recalc_timer(cpu, GTIMER_VIRT);
3362 }
3363
arm_gt_htimer_cb(void * opaque)3364 void arm_gt_htimer_cb(void *opaque)
3365 {
3366 ARMCPU *cpu = opaque;
3367
3368 gt_recalc_timer(cpu, GTIMER_HYP);
3369 }
3370
arm_gt_stimer_cb(void * opaque)3371 void arm_gt_stimer_cb(void *opaque)
3372 {
3373 ARMCPU *cpu = opaque;
3374
3375 gt_recalc_timer(cpu, GTIMER_SEC);
3376 }
3377
arm_gt_sel2timer_cb(void * opaque)3378 void arm_gt_sel2timer_cb(void *opaque)
3379 {
3380 ARMCPU *cpu = opaque;
3381
3382 gt_recalc_timer(cpu, GTIMER_S_EL2_PHYS);
3383 }
3384
arm_gt_sel2vtimer_cb(void * opaque)3385 void arm_gt_sel2vtimer_cb(void *opaque)
3386 {
3387 ARMCPU *cpu = opaque;
3388
3389 gt_recalc_timer(cpu, GTIMER_S_EL2_VIRT);
3390 }
3391
arm_gt_hvtimer_cb(void * opaque)3392 void arm_gt_hvtimer_cb(void *opaque)
3393 {
3394 ARMCPU *cpu = opaque;
3395
3396 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3397 }
3398
3399 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3400 /*
3401 * Note that CNTFRQ is purely reads-as-written for the benefit
3402 * of software; writing it doesn't actually change the timer frequency.
3403 * Our reset value matches the fixed frequency we implement the timer at.
3404 */
3405 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3406 .type = ARM_CP_ALIAS,
3407 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3408 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3409 },
3410 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3411 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3412 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3413 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3414 .resetfn = arm_gt_cntfrq_reset,
3415 },
3416 /* overall control: mostly access permissions */
3417 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3418 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3419 .access = PL1_RW,
3420 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3421 .resetvalue = 0,
3422 },
3423 /* per-timer control */
3424 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3425 .secure = ARM_CP_SECSTATE_NS,
3426 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3427 .accessfn = gt_ptimer_access,
3428 .fieldoffset = offsetoflow32(CPUARMState,
3429 cp15.c14_timer[GTIMER_PHYS].ctl),
3430 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3431 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3432 },
3433 { .name = "CNTP_CTL_S",
3434 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3435 .secure = ARM_CP_SECSTATE_S,
3436 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3437 .accessfn = gt_ptimer_access,
3438 .fieldoffset = offsetoflow32(CPUARMState,
3439 cp15.c14_timer[GTIMER_SEC].ctl),
3440 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3441 },
3442 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3443 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3444 .type = ARM_CP_IO, .access = PL0_RW,
3445 .accessfn = gt_ptimer_access,
3446 .nv2_redirect_offset = 0x180 | NV2_REDIR_NV1,
3447 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3448 .resetvalue = 0,
3449 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3450 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3451 },
3452 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3453 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3454 .accessfn = gt_vtimer_access,
3455 .fieldoffset = offsetoflow32(CPUARMState,
3456 cp15.c14_timer[GTIMER_VIRT].ctl),
3457 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3458 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3459 },
3460 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3461 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3462 .type = ARM_CP_IO, .access = PL0_RW,
3463 .accessfn = gt_vtimer_access,
3464 .nv2_redirect_offset = 0x170 | NV2_REDIR_NV1,
3465 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3466 .resetvalue = 0,
3467 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3468 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3469 },
3470 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3471 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3472 .secure = ARM_CP_SECSTATE_NS,
3473 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3474 .accessfn = gt_ptimer_access,
3475 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3476 },
3477 { .name = "CNTP_TVAL_S",
3478 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3479 .secure = ARM_CP_SECSTATE_S,
3480 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3481 .accessfn = gt_ptimer_access,
3482 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3483 },
3484 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3485 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3486 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3487 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3488 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3489 },
3490 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3491 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3492 .accessfn = gt_vtimer_access,
3493 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3494 },
3495 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3496 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3497 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3498 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3499 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3500 },
3501 /* The counter itself */
3502 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3503 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3504 .accessfn = gt_pct_access,
3505 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3506 },
3507 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3508 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3509 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3510 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3511 },
3512 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3513 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3514 .accessfn = gt_vct_access,
3515 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3516 },
3517 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3518 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3519 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3520 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3521 },
3522 /* Comparison value, indicating when the timer goes off */
3523 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3524 .secure = ARM_CP_SECSTATE_NS,
3525 .access = PL0_RW,
3526 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3527 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3528 .accessfn = gt_ptimer_access,
3529 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3530 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3531 },
3532 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3533 .secure = ARM_CP_SECSTATE_S,
3534 .access = PL0_RW,
3535 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3536 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3537 .accessfn = gt_ptimer_access,
3538 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3539 },
3540 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3541 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3542 .access = PL0_RW,
3543 .type = ARM_CP_IO,
3544 .nv2_redirect_offset = 0x178 | NV2_REDIR_NV1,
3545 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3546 .resetvalue = 0, .accessfn = gt_ptimer_access,
3547 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3548 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3549 },
3550 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3551 .access = PL0_RW,
3552 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3553 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3554 .accessfn = gt_vtimer_access,
3555 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3556 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3557 },
3558 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3559 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3560 .access = PL0_RW,
3561 .type = ARM_CP_IO,
3562 .nv2_redirect_offset = 0x168 | NV2_REDIR_NV1,
3563 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3564 .resetvalue = 0, .accessfn = gt_vtimer_access,
3565 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3566 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3567 },
3568 /*
3569 * Secure timer -- this is actually restricted to only EL3
3570 * and configurably Secure-EL1 via the accessfn.
3571 */
3572 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3573 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3574 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3575 .accessfn = gt_stimer_access,
3576 .readfn = gt_sec_tval_read,
3577 .writefn = gt_sec_tval_write,
3578 .resetfn = gt_sec_timer_reset,
3579 },
3580 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3581 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3582 .type = ARM_CP_IO, .access = PL1_RW,
3583 .accessfn = gt_stimer_access,
3584 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3585 .resetvalue = 0,
3586 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3587 },
3588 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3589 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3590 .type = ARM_CP_IO, .access = PL1_RW,
3591 .accessfn = gt_stimer_access,
3592 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3593 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3594 },
3595 };
3596
3597 /*
3598 * FEAT_ECV adds extra views of CNTVCT_EL0 and CNTPCT_EL0 which
3599 * are "self-synchronizing". For QEMU all sysregs are self-synchronizing,
3600 * so our implementations here are identical to the normal registers.
3601 */
3602 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3603 { .name = "CNTVCTSS", .cp = 15, .crm = 14, .opc1 = 9,
3604 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3605 .accessfn = gt_vct_access,
3606 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3607 },
3608 { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3609 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3610 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3611 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3612 },
3613 { .name = "CNTPCTSS", .cp = 15, .crm = 14, .opc1 = 8,
3614 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3615 .accessfn = gt_pct_access,
3616 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3617 },
3618 { .name = "CNTPCTSS_EL0", .state = ARM_CP_STATE_AA64,
3619 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 5,
3620 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3621 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3622 },
3623 };
3624
gt_cntpoff_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)3625 static CPAccessResult gt_cntpoff_access(CPUARMState *env,
3626 const ARMCPRegInfo *ri,
3627 bool isread)
3628 {
3629 if (arm_current_el(env) == 2 && arm_feature(env, ARM_FEATURE_EL3) &&
3630 !(env->cp15.scr_el3 & SCR_ECVEN)) {
3631 return CP_ACCESS_TRAP_EL3;
3632 }
3633 return CP_ACCESS_OK;
3634 }
3635
gt_cntpoff_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3636 static void gt_cntpoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
3637 uint64_t value)
3638 {
3639 ARMCPU *cpu = env_archcpu(env);
3640
3641 trace_arm_gt_cntpoff_write(value);
3642 raw_write(env, ri, value);
3643 gt_recalc_timer(cpu, GTIMER_PHYS);
3644 }
3645
3646 static const ARMCPRegInfo gen_timer_cntpoff_reginfo = {
3647 .name = "CNTPOFF_EL2", .state = ARM_CP_STATE_AA64,
3648 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 6,
3649 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3650 .accessfn = gt_cntpoff_access, .writefn = gt_cntpoff_write,
3651 .nv2_redirect_offset = 0x1a8,
3652 .fieldoffset = offsetof(CPUARMState, cp15.cntpoff_el2),
3653 };
3654 #else
3655
3656 /*
3657 * In user-mode most of the generic timer registers are inaccessible
3658 * however modern kernels (4.12+) allow access to cntvct_el0
3659 */
3660
gt_virt_cnt_read(CPUARMState * env,const ARMCPRegInfo * ri)3661 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3662 {
3663 ARMCPU *cpu = env_archcpu(env);
3664
3665 /*
3666 * Currently we have no support for QEMUTimer in linux-user so we
3667 * can't call gt_get_countervalue(env), instead we directly
3668 * call the lower level functions.
3669 */
3670 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3671 }
3672
3673 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3674 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3675 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3676 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3677 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3678 .resetfn = arm_gt_cntfrq_reset,
3679 },
3680 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3681 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3682 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3683 .readfn = gt_virt_cnt_read,
3684 },
3685 };
3686
3687 /*
3688 * CNTVCTSS_EL0 has the same trap conditions as CNTVCT_EL0, so it also
3689 * is exposed to userspace by Linux.
3690 */
3691 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3692 { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3693 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3694 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3695 .readfn = gt_virt_cnt_read,
3696 },
3697 };
3698
3699 #endif
3700
par_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3701 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3702 {
3703 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3704 raw_write(env, ri, value);
3705 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3706 raw_write(env, ri, value & 0xfffff6ff);
3707 } else {
3708 raw_write(env, ri, value & 0xfffff1ff);
3709 }
3710 }
3711
3712 #ifndef CONFIG_USER_ONLY
3713 /* get_phys_addr() isn't present for user-mode-only targets */
3714
ats_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)3715 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3716 bool isread)
3717 {
3718 if (ri->opc2 & 4) {
3719 /*
3720 * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3721 * Secure EL1 (which can only happen if EL3 is AArch64).
3722 * They are simply UNDEF if executed from NS EL1.
3723 * They function normally from EL2 or EL3.
3724 */
3725 if (arm_current_el(env) == 1) {
3726 if (arm_is_secure_below_el3(env)) {
3727 if (env->cp15.scr_el3 & SCR_EEL2) {
3728 return CP_ACCESS_TRAP_EL2;
3729 }
3730 return CP_ACCESS_TRAP_EL3;
3731 }
3732 return CP_ACCESS_TRAP_UNCATEGORIZED;
3733 }
3734 }
3735 return CP_ACCESS_OK;
3736 }
3737
3738 #ifdef CONFIG_TCG
par_el1_shareability(GetPhysAddrResult * res)3739 static int par_el1_shareability(GetPhysAddrResult *res)
3740 {
3741 /*
3742 * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3743 * memory -- see pseudocode PAREncodeShareability().
3744 */
3745 if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3746 res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3747 return 2;
3748 }
3749 return res->cacheattrs.shareability;
3750 }
3751
do_ats_write(CPUARMState * env,uint64_t value,MMUAccessType access_type,ARMMMUIdx mmu_idx,ARMSecuritySpace ss)3752 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3753 MMUAccessType access_type, ARMMMUIdx mmu_idx,
3754 ARMSecuritySpace ss)
3755 {
3756 bool ret;
3757 uint64_t par64;
3758 bool format64 = false;
3759 ARMMMUFaultInfo fi = {};
3760 GetPhysAddrResult res = {};
3761
3762 /*
3763 * I_MXTJT: Granule protection checks are not performed on the final
3764 * address of a successful translation. This is a translation not a
3765 * memory reference, so "memop = none = 0".
3766 */
3767 ret = get_phys_addr_with_space_nogpc(env, value, access_type, 0,
3768 mmu_idx, ss, &res, &fi);
3769
3770 /*
3771 * ATS operations only do S1 or S1+S2 translations, so we never
3772 * have to deal with the ARMCacheAttrs format for S2 only.
3773 */
3774 assert(!res.cacheattrs.is_s2_format);
3775
3776 if (ret) {
3777 /*
3778 * Some kinds of translation fault must cause exceptions rather
3779 * than being reported in the PAR.
3780 */
3781 int current_el = arm_current_el(env);
3782 int target_el;
3783 uint32_t syn, fsr, fsc;
3784 bool take_exc = false;
3785
3786 if (fi.s1ptw && current_el == 1
3787 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3788 /*
3789 * Synchronous stage 2 fault on an access made as part of the
3790 * translation table walk for AT S1E0* or AT S1E1* insn
3791 * executed from NS EL1. If this is a synchronous external abort
3792 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3793 * to EL3. Otherwise the fault is taken as an exception to EL2,
3794 * and HPFAR_EL2 holds the faulting IPA.
3795 */
3796 if (fi.type == ARMFault_SyncExternalOnWalk &&
3797 (env->cp15.scr_el3 & SCR_EA)) {
3798 target_el = 3;
3799 } else {
3800 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3801 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3802 env->cp15.hpfar_el2 |= HPFAR_NS;
3803 }
3804 target_el = 2;
3805 }
3806 take_exc = true;
3807 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3808 /*
3809 * Synchronous external aborts during a translation table walk
3810 * are taken as Data Abort exceptions.
3811 */
3812 if (fi.stage2) {
3813 if (current_el == 3) {
3814 target_el = 3;
3815 } else {
3816 target_el = 2;
3817 }
3818 } else {
3819 target_el = exception_target_el(env);
3820 }
3821 take_exc = true;
3822 }
3823
3824 if (take_exc) {
3825 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3826 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3827 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3828 fsr = arm_fi_to_lfsc(&fi);
3829 fsc = extract32(fsr, 0, 6);
3830 } else {
3831 fsr = arm_fi_to_sfsc(&fi);
3832 fsc = 0x3f;
3833 }
3834 /*
3835 * Report exception with ESR indicating a fault due to a
3836 * translation table walk for a cache maintenance instruction.
3837 */
3838 syn = syn_data_abort_no_iss(current_el == target_el, 0,
3839 fi.ea, 1, fi.s1ptw, 1, fsc);
3840 env->exception.vaddress = value;
3841 env->exception.fsr = fsr;
3842 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3843 }
3844 }
3845
3846 if (is_a64(env)) {
3847 format64 = true;
3848 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3849 /*
3850 * ATS1Cxx:
3851 * * TTBCR.EAE determines whether the result is returned using the
3852 * 32-bit or the 64-bit PAR format
3853 * * Instructions executed in Hyp mode always use the 64bit format
3854 *
3855 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3856 * * The Non-secure TTBCR.EAE bit is set to 1
3857 * * The implementation includes EL2, and the value of HCR.VM is 1
3858 *
3859 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3860 *
3861 * ATS1Hx always uses the 64bit format.
3862 */
3863 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3864
3865 if (arm_feature(env, ARM_FEATURE_EL2)) {
3866 if (mmu_idx == ARMMMUIdx_E10_0 ||
3867 mmu_idx == ARMMMUIdx_E10_1 ||
3868 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3869 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3870 } else {
3871 format64 |= arm_current_el(env) == 2;
3872 }
3873 }
3874 }
3875
3876 if (format64) {
3877 /* Create a 64-bit PAR */
3878 par64 = (1 << 11); /* LPAE bit always set */
3879 if (!ret) {
3880 par64 |= res.f.phys_addr & ~0xfffULL;
3881 if (!res.f.attrs.secure) {
3882 par64 |= (1 << 9); /* NS */
3883 }
3884 par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3885 par64 |= par_el1_shareability(&res) << 7; /* SH */
3886 } else {
3887 uint32_t fsr = arm_fi_to_lfsc(&fi);
3888
3889 par64 |= 1; /* F */
3890 par64 |= (fsr & 0x3f) << 1; /* FS */
3891 if (fi.stage2) {
3892 par64 |= (1 << 9); /* S */
3893 }
3894 if (fi.s1ptw) {
3895 par64 |= (1 << 8); /* PTW */
3896 }
3897 }
3898 } else {
3899 /*
3900 * fsr is a DFSR/IFSR value for the short descriptor
3901 * translation table format (with WnR always clear).
3902 * Convert it to a 32-bit PAR.
3903 */
3904 if (!ret) {
3905 /* We do not set any attribute bits in the PAR */
3906 if (res.f.lg_page_size == 24
3907 && arm_feature(env, ARM_FEATURE_V7)) {
3908 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3909 } else {
3910 par64 = res.f.phys_addr & 0xfffff000;
3911 }
3912 if (!res.f.attrs.secure) {
3913 par64 |= (1 << 9); /* NS */
3914 }
3915 } else {
3916 uint32_t fsr = arm_fi_to_sfsc(&fi);
3917
3918 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3919 ((fsr & 0xf) << 1) | 1;
3920 }
3921 }
3922 return par64;
3923 }
3924 #endif /* CONFIG_TCG */
3925
ats_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3926 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3927 {
3928 #ifdef CONFIG_TCG
3929 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3930 uint64_t par64;
3931 ARMMMUIdx mmu_idx;
3932 int el = arm_current_el(env);
3933 ARMSecuritySpace ss = arm_security_space(env);
3934
3935 switch (ri->opc2 & 6) {
3936 case 0:
3937 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3938 switch (el) {
3939 case 3:
3940 if (ri->crm == 9 && arm_pan_enabled(env)) {
3941 mmu_idx = ARMMMUIdx_E30_3_PAN;
3942 } else {
3943 mmu_idx = ARMMMUIdx_E3;
3944 }
3945 break;
3946 case 2:
3947 g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */
3948 /* fall through */
3949 case 1:
3950 if (ri->crm == 9 && arm_pan_enabled(env)) {
3951 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3952 } else {
3953 mmu_idx = ARMMMUIdx_Stage1_E1;
3954 }
3955 break;
3956 default:
3957 g_assert_not_reached();
3958 }
3959 break;
3960 case 2:
3961 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3962 switch (el) {
3963 case 3:
3964 mmu_idx = ARMMMUIdx_E30_0;
3965 break;
3966 case 2:
3967 g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */
3968 mmu_idx = ARMMMUIdx_Stage1_E0;
3969 break;
3970 case 1:
3971 mmu_idx = ARMMMUIdx_Stage1_E0;
3972 break;
3973 default:
3974 g_assert_not_reached();
3975 }
3976 break;
3977 case 4:
3978 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3979 mmu_idx = ARMMMUIdx_E10_1;
3980 ss = ARMSS_NonSecure;
3981 break;
3982 case 6:
3983 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3984 mmu_idx = ARMMMUIdx_E10_0;
3985 ss = ARMSS_NonSecure;
3986 break;
3987 default:
3988 g_assert_not_reached();
3989 }
3990
3991 par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
3992
3993 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3994 #else
3995 /* Handled by hardware accelerator. */
3996 g_assert_not_reached();
3997 #endif /* CONFIG_TCG */
3998 }
3999
ats1h_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4000 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
4001 uint64_t value)
4002 {
4003 #ifdef CONFIG_TCG
4004 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
4005 uint64_t par64;
4006
4007 /* There is no SecureEL2 for AArch32. */
4008 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
4009 ARMSS_NonSecure);
4010
4011 A32_BANKED_CURRENT_REG_SET(env, par, par64);
4012 #else
4013 /* Handled by hardware accelerator. */
4014 g_assert_not_reached();
4015 #endif /* CONFIG_TCG */
4016 }
4017
at_e012_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4018 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
4019 bool isread)
4020 {
4021 /*
4022 * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
4023 * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
4024 * only happen when executing at EL3 because that combination also causes an
4025 * illegal exception return. We don't need to check FEAT_RME either, because
4026 * scr_write() ensures that the NSE bit is not set otherwise.
4027 */
4028 if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
4029 return CP_ACCESS_TRAP_UNCATEGORIZED;
4030 }
4031 return CP_ACCESS_OK;
4032 }
4033
at_s1e2_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4034 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
4035 bool isread)
4036 {
4037 if (arm_current_el(env) == 3 &&
4038 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
4039 return CP_ACCESS_TRAP_UNCATEGORIZED;
4040 }
4041 return at_e012_access(env, ri, isread);
4042 }
4043
at_s1e01_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4044 static CPAccessResult at_s1e01_access(CPUARMState *env, const ARMCPRegInfo *ri,
4045 bool isread)
4046 {
4047 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_AT)) {
4048 return CP_ACCESS_TRAP_EL2;
4049 }
4050 return at_e012_access(env, ri, isread);
4051 }
4052
ats_write64(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4053 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
4054 uint64_t value)
4055 {
4056 #ifdef CONFIG_TCG
4057 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
4058 ARMMMUIdx mmu_idx;
4059 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
4060 bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
4061 bool for_el3 = false;
4062 ARMSecuritySpace ss;
4063
4064 switch (ri->opc2 & 6) {
4065 case 0:
4066 switch (ri->opc1) {
4067 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
4068 if (ri->crm == 9 && arm_pan_enabled(env)) {
4069 mmu_idx = regime_e20 ?
4070 ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
4071 } else {
4072 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
4073 }
4074 break;
4075 case 4: /* AT S1E2R, AT S1E2W */
4076 mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
4077 break;
4078 case 6: /* AT S1E3R, AT S1E3W */
4079 mmu_idx = ARMMMUIdx_E3;
4080 for_el3 = true;
4081 break;
4082 default:
4083 g_assert_not_reached();
4084 }
4085 break;
4086 case 2: /* AT S1E0R, AT S1E0W */
4087 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
4088 break;
4089 case 4: /* AT S12E1R, AT S12E1W */
4090 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
4091 break;
4092 case 6: /* AT S12E0R, AT S12E0W */
4093 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
4094 break;
4095 default:
4096 g_assert_not_reached();
4097 }
4098
4099 ss = for_el3 ? arm_security_space(env) : arm_security_space_below_el3(env);
4100 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx, ss);
4101 #else
4102 /* Handled by hardware accelerator. */
4103 g_assert_not_reached();
4104 #endif /* CONFIG_TCG */
4105 }
4106 #endif
4107
4108 /* Return basic MPU access permission bits. */
simple_mpu_ap_bits(uint32_t val)4109 static uint32_t simple_mpu_ap_bits(uint32_t val)
4110 {
4111 uint32_t ret;
4112 uint32_t mask;
4113 int i;
4114 ret = 0;
4115 mask = 3;
4116 for (i = 0; i < 16; i += 2) {
4117 ret |= (val >> i) & mask;
4118 mask <<= 2;
4119 }
4120 return ret;
4121 }
4122
4123 /* Pad basic MPU access permission bits to extended format. */
extended_mpu_ap_bits(uint32_t val)4124 static uint32_t extended_mpu_ap_bits(uint32_t val)
4125 {
4126 uint32_t ret;
4127 uint32_t mask;
4128 int i;
4129 ret = 0;
4130 mask = 3;
4131 for (i = 0; i < 16; i += 2) {
4132 ret |= (val & mask) << i;
4133 mask <<= 2;
4134 }
4135 return ret;
4136 }
4137
pmsav5_data_ap_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4138 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
4139 uint64_t value)
4140 {
4141 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
4142 }
4143
pmsav5_data_ap_read(CPUARMState * env,const ARMCPRegInfo * ri)4144 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
4145 {
4146 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
4147 }
4148
pmsav5_insn_ap_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4149 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
4150 uint64_t value)
4151 {
4152 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
4153 }
4154
pmsav5_insn_ap_read(CPUARMState * env,const ARMCPRegInfo * ri)4155 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
4156 {
4157 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
4158 }
4159
pmsav7_read(CPUARMState * env,const ARMCPRegInfo * ri)4160 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
4161 {
4162 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
4163
4164 if (!u32p) {
4165 return 0;
4166 }
4167
4168 u32p += env->pmsav7.rnr[M_REG_NS];
4169 return *u32p;
4170 }
4171
pmsav7_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4172 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
4173 uint64_t value)
4174 {
4175 ARMCPU *cpu = env_archcpu(env);
4176 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
4177
4178 if (!u32p) {
4179 return;
4180 }
4181
4182 u32p += env->pmsav7.rnr[M_REG_NS];
4183 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4184 *u32p = value;
4185 }
4186
pmsav7_rgnr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4187 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4188 uint64_t value)
4189 {
4190 ARMCPU *cpu = env_archcpu(env);
4191 uint32_t nrgs = cpu->pmsav7_dregion;
4192
4193 if (value >= nrgs) {
4194 qemu_log_mask(LOG_GUEST_ERROR,
4195 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
4196 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
4197 return;
4198 }
4199
4200 raw_write(env, ri, value);
4201 }
4202
prbar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4203 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4204 uint64_t value)
4205 {
4206 ARMCPU *cpu = env_archcpu(env);
4207
4208 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4209 env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
4210 }
4211
prbar_read(CPUARMState * env,const ARMCPRegInfo * ri)4212 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4213 {
4214 return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
4215 }
4216
prlar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4217 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4218 uint64_t value)
4219 {
4220 ARMCPU *cpu = env_archcpu(env);
4221
4222 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4223 env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
4224 }
4225
prlar_read(CPUARMState * env,const ARMCPRegInfo * ri)4226 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4227 {
4228 return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
4229 }
4230
prselr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4231 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4232 uint64_t value)
4233 {
4234 ARMCPU *cpu = env_archcpu(env);
4235
4236 /*
4237 * Ignore writes that would select not implemented region.
4238 * This is architecturally UNPREDICTABLE.
4239 */
4240 if (value >= cpu->pmsav7_dregion) {
4241 return;
4242 }
4243
4244 env->pmsav7.rnr[M_REG_NS] = value;
4245 }
4246
hprbar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4247 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4248 uint64_t value)
4249 {
4250 ARMCPU *cpu = env_archcpu(env);
4251
4252 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4253 env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
4254 }
4255
hprbar_read(CPUARMState * env,const ARMCPRegInfo * ri)4256 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4257 {
4258 return env->pmsav8.hprbar[env->pmsav8.hprselr];
4259 }
4260
hprlar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4261 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4262 uint64_t value)
4263 {
4264 ARMCPU *cpu = env_archcpu(env);
4265
4266 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4267 env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
4268 }
4269
hprlar_read(CPUARMState * env,const ARMCPRegInfo * ri)4270 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4271 {
4272 return env->pmsav8.hprlar[env->pmsav8.hprselr];
4273 }
4274
hprenr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4275 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4276 uint64_t value)
4277 {
4278 uint32_t n;
4279 uint32_t bit;
4280 ARMCPU *cpu = env_archcpu(env);
4281
4282 /* Ignore writes to unimplemented regions */
4283 int rmax = MIN(cpu->pmsav8r_hdregion, 32);
4284 value &= MAKE_64BIT_MASK(0, rmax);
4285
4286 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4287
4288 /* Register alias is only valid for first 32 indexes */
4289 for (n = 0; n < rmax; ++n) {
4290 bit = extract32(value, n, 1);
4291 env->pmsav8.hprlar[n] = deposit32(
4292 env->pmsav8.hprlar[n], 0, 1, bit);
4293 }
4294 }
4295
hprenr_read(CPUARMState * env,const ARMCPRegInfo * ri)4296 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4297 {
4298 uint32_t n;
4299 uint32_t result = 0x0;
4300 ARMCPU *cpu = env_archcpu(env);
4301
4302 /* Register alias is only valid for first 32 indexes */
4303 for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
4304 if (env->pmsav8.hprlar[n] & 0x1) {
4305 result |= (0x1 << n);
4306 }
4307 }
4308 return result;
4309 }
4310
hprselr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4311 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4312 uint64_t value)
4313 {
4314 ARMCPU *cpu = env_archcpu(env);
4315
4316 /*
4317 * Ignore writes that would select not implemented region.
4318 * This is architecturally UNPREDICTABLE.
4319 */
4320 if (value >= cpu->pmsav8r_hdregion) {
4321 return;
4322 }
4323
4324 env->pmsav8.hprselr = value;
4325 }
4326
pmsav8r_regn_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4327 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
4328 uint64_t value)
4329 {
4330 ARMCPU *cpu = env_archcpu(env);
4331 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4332 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4333
4334 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4335
4336 if (ri->opc1 & 4) {
4337 if (index >= cpu->pmsav8r_hdregion) {
4338 return;
4339 }
4340 if (ri->opc2 & 0x1) {
4341 env->pmsav8.hprlar[index] = value;
4342 } else {
4343 env->pmsav8.hprbar[index] = value;
4344 }
4345 } else {
4346 if (index >= cpu->pmsav7_dregion) {
4347 return;
4348 }
4349 if (ri->opc2 & 0x1) {
4350 env->pmsav8.rlar[M_REG_NS][index] = value;
4351 } else {
4352 env->pmsav8.rbar[M_REG_NS][index] = value;
4353 }
4354 }
4355 }
4356
pmsav8r_regn_read(CPUARMState * env,const ARMCPRegInfo * ri)4357 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
4358 {
4359 ARMCPU *cpu = env_archcpu(env);
4360 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4361 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4362
4363 if (ri->opc1 & 4) {
4364 if (index >= cpu->pmsav8r_hdregion) {
4365 return 0x0;
4366 }
4367 if (ri->opc2 & 0x1) {
4368 return env->pmsav8.hprlar[index];
4369 } else {
4370 return env->pmsav8.hprbar[index];
4371 }
4372 } else {
4373 if (index >= cpu->pmsav7_dregion) {
4374 return 0x0;
4375 }
4376 if (ri->opc2 & 0x1) {
4377 return env->pmsav8.rlar[M_REG_NS][index];
4378 } else {
4379 return env->pmsav8.rbar[M_REG_NS][index];
4380 }
4381 }
4382 }
4383
4384 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
4385 { .name = "PRBAR",
4386 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
4387 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4388 .accessfn = access_tvm_trvm,
4389 .readfn = prbar_read, .writefn = prbar_write },
4390 { .name = "PRLAR",
4391 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
4392 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4393 .accessfn = access_tvm_trvm,
4394 .readfn = prlar_read, .writefn = prlar_write },
4395 { .name = "PRSELR", .resetvalue = 0,
4396 .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
4397 .access = PL1_RW, .accessfn = access_tvm_trvm,
4398 .writefn = prselr_write,
4399 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
4400 { .name = "HPRBAR", .resetvalue = 0,
4401 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
4402 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4403 .readfn = hprbar_read, .writefn = hprbar_write },
4404 { .name = "HPRLAR",
4405 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
4406 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4407 .readfn = hprlar_read, .writefn = hprlar_write },
4408 { .name = "HPRSELR", .resetvalue = 0,
4409 .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
4410 .access = PL2_RW,
4411 .writefn = hprselr_write,
4412 .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
4413 { .name = "HPRENR",
4414 .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
4415 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4416 .readfn = hprenr_read, .writefn = hprenr_write },
4417 };
4418
4419 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
4420 /*
4421 * Reset for all these registers is handled in arm_cpu_reset(),
4422 * because the PMSAv7 is also used by M-profile CPUs, which do
4423 * not register cpregs but still need the state to be reset.
4424 */
4425 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4426 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4427 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
4428 .readfn = pmsav7_read, .writefn = pmsav7_write,
4429 .resetfn = arm_cp_reset_ignore },
4430 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4431 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4432 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
4433 .readfn = pmsav7_read, .writefn = pmsav7_write,
4434 .resetfn = arm_cp_reset_ignore },
4435 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4436 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4437 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
4438 .readfn = pmsav7_read, .writefn = pmsav7_write,
4439 .resetfn = arm_cp_reset_ignore },
4440 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4441 .access = PL1_RW,
4442 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
4443 .writefn = pmsav7_rgnr_write,
4444 .resetfn = arm_cp_reset_ignore },
4445 };
4446
4447 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4448 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4449 .access = PL1_RW, .type = ARM_CP_ALIAS,
4450 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4451 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4452 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4453 .access = PL1_RW, .type = ARM_CP_ALIAS,
4454 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4455 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4456 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4457 .access = PL1_RW,
4458 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4459 .resetvalue = 0, },
4460 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4461 .access = PL1_RW,
4462 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4463 .resetvalue = 0, },
4464 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4465 .access = PL1_RW,
4466 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4467 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4468 .access = PL1_RW,
4469 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
4470 /* Protection region base and size registers */
4471 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4472 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4473 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4474 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4475 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4476 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4477 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4478 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4479 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4480 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4481 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4482 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4483 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4484 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4485 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4486 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4487 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4488 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4489 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4490 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4491 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4492 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4493 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4494 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4495 };
4496
vmsa_ttbcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4497 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4498 uint64_t value)
4499 {
4500 ARMCPU *cpu = env_archcpu(env);
4501
4502 if (!arm_feature(env, ARM_FEATURE_V8)) {
4503 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4504 /*
4505 * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4506 * using Long-descriptor translation table format
4507 */
4508 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4509 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4510 /*
4511 * In an implementation that includes the Security Extensions
4512 * TTBCR has additional fields PD0 [4] and PD1 [5] for
4513 * Short-descriptor translation table format.
4514 */
4515 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4516 } else {
4517 value &= TTBCR_N;
4518 }
4519 }
4520
4521 if (arm_feature(env, ARM_FEATURE_LPAE)) {
4522 /*
4523 * With LPAE the TTBCR could result in a change of ASID
4524 * via the TTBCR.A1 bit, so do a TLB flush.
4525 */
4526 tlb_flush(CPU(cpu));
4527 }
4528 raw_write(env, ri, value);
4529 }
4530
vmsa_tcr_el12_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4531 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4532 uint64_t value)
4533 {
4534 ARMCPU *cpu = env_archcpu(env);
4535
4536 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4537 tlb_flush(CPU(cpu));
4538 raw_write(env, ri, value);
4539 }
4540
vmsa_ttbr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4541 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4542 uint64_t value)
4543 {
4544 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
4545 if (cpreg_field_is_64bit(ri) &&
4546 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4547 ARMCPU *cpu = env_archcpu(env);
4548 tlb_flush(CPU(cpu));
4549 }
4550 raw_write(env, ri, value);
4551 }
4552
vmsa_tcr_ttbr_el2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4553 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4554 uint64_t value)
4555 {
4556 /*
4557 * If we are running with E2&0 regime, then an ASID is active.
4558 * Flush if that might be changing. Note we're not checking
4559 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4560 * holds the active ASID, only checking the field that might.
4561 */
4562 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4563 (arm_hcr_el2_eff(env) & HCR_E2H)) {
4564 uint16_t mask = ARMMMUIdxBit_E20_2 |
4565 ARMMMUIdxBit_E20_2_PAN |
4566 ARMMMUIdxBit_E20_0;
4567 tlb_flush_by_mmuidx(env_cpu(env), mask);
4568 }
4569 raw_write(env, ri, value);
4570 }
4571
vttbr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4572 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4573 uint64_t value)
4574 {
4575 ARMCPU *cpu = env_archcpu(env);
4576 CPUState *cs = CPU(cpu);
4577
4578 /*
4579 * A change in VMID to the stage2 page table (Stage2) invalidates
4580 * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4581 */
4582 if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4583 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4584 }
4585 raw_write(env, ri, value);
4586 }
4587
4588 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4589 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4590 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4591 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4592 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4593 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4594 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4595 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4596 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4597 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4598 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4599 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4600 offsetof(CPUARMState, cp15.dfar_ns) } },
4601 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4602 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4603 .access = PL1_RW, .accessfn = access_tvm_trvm,
4604 .fgt = FGT_FAR_EL1,
4605 .nv2_redirect_offset = 0x220 | NV2_REDIR_NV1,
4606 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4607 .resetvalue = 0, },
4608 };
4609
4610 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4611 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4612 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4613 .access = PL1_RW, .accessfn = access_tvm_trvm,
4614 .fgt = FGT_ESR_EL1,
4615 .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1,
4616 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4617 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4618 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4619 .access = PL1_RW, .accessfn = access_tvm_trvm,
4620 .fgt = FGT_TTBR0_EL1,
4621 .nv2_redirect_offset = 0x200 | NV2_REDIR_NV1,
4622 .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4623 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4624 offsetof(CPUARMState, cp15.ttbr0_ns) } },
4625 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4626 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4627 .access = PL1_RW, .accessfn = access_tvm_trvm,
4628 .fgt = FGT_TTBR1_EL1,
4629 .nv2_redirect_offset = 0x210 | NV2_REDIR_NV1,
4630 .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4631 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4632 offsetof(CPUARMState, cp15.ttbr1_ns) } },
4633 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4634 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4635 .access = PL1_RW, .accessfn = access_tvm_trvm,
4636 .fgt = FGT_TCR_EL1,
4637 .nv2_redirect_offset = 0x120 | NV2_REDIR_NV1,
4638 .writefn = vmsa_tcr_el12_write,
4639 .raw_writefn = raw_write,
4640 .resetvalue = 0,
4641 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4642 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4643 .access = PL1_RW, .accessfn = access_tvm_trvm,
4644 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4645 .raw_writefn = raw_write,
4646 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4647 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4648 };
4649
4650 /*
4651 * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4652 * qemu tlbs nor adjusting cached masks.
4653 */
4654 static const ARMCPRegInfo ttbcr2_reginfo = {
4655 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4656 .access = PL1_RW, .accessfn = access_tvm_trvm,
4657 .type = ARM_CP_ALIAS,
4658 .bank_fieldoffsets = {
4659 offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4660 offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4661 },
4662 };
4663
omap_ticonfig_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4664 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4665 uint64_t value)
4666 {
4667 env->cp15.c15_ticonfig = value & 0xe7;
4668 /* The OS_TYPE bit in this register changes the reported CPUID! */
4669 env->cp15.c0_cpuid = (value & (1 << 5)) ?
4670 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4671 }
4672
omap_threadid_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4673 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4674 uint64_t value)
4675 {
4676 env->cp15.c15_threadid = value & 0xffff;
4677 }
4678
omap_wfi_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4679 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4680 uint64_t value)
4681 {
4682 /* Wait-for-interrupt (deprecated) */
4683 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4684 }
4685
omap_cachemaint_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4686 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4687 uint64_t value)
4688 {
4689 /*
4690 * On OMAP there are registers indicating the max/min index of dcache lines
4691 * containing a dirty line; cache flush operations have to reset these.
4692 */
4693 env->cp15.c15_i_max = 0x000;
4694 env->cp15.c15_i_min = 0xff0;
4695 }
4696
4697 static const ARMCPRegInfo omap_cp_reginfo[] = {
4698 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4699 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4700 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4701 .resetvalue = 0, },
4702 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4703 .access = PL1_RW, .type = ARM_CP_NOP },
4704 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4705 .access = PL1_RW,
4706 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4707 .writefn = omap_ticonfig_write },
4708 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4709 .access = PL1_RW,
4710 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4711 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4712 .access = PL1_RW, .resetvalue = 0xff0,
4713 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4714 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4715 .access = PL1_RW,
4716 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4717 .writefn = omap_threadid_write },
4718 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4719 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4720 .type = ARM_CP_NO_RAW,
4721 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4722 /*
4723 * TODO: Peripheral port remap register:
4724 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4725 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4726 * when MMU is off.
4727 */
4728 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4729 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4730 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4731 .writefn = omap_cachemaint_write },
4732 { .name = "C9", .cp = 15, .crn = 9,
4733 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4734 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4735 };
4736
xscale_cpar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4737 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4738 uint64_t value)
4739 {
4740 env->cp15.c15_cpar = value & 0x3fff;
4741 }
4742
4743 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4744 { .name = "XSCALE_CPAR",
4745 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4746 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4747 .writefn = xscale_cpar_write, },
4748 { .name = "XSCALE_AUXCR",
4749 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4750 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4751 .resetvalue = 0, },
4752 /*
4753 * XScale specific cache-lockdown: since we have no cache we NOP these
4754 * and hope the guest does not really rely on cache behaviour.
4755 */
4756 { .name = "XSCALE_LOCK_ICACHE_LINE",
4757 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4758 .access = PL1_W, .type = ARM_CP_NOP },
4759 { .name = "XSCALE_UNLOCK_ICACHE",
4760 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4761 .access = PL1_W, .type = ARM_CP_NOP },
4762 { .name = "XSCALE_DCACHE_LOCK",
4763 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4764 .access = PL1_RW, .type = ARM_CP_NOP },
4765 { .name = "XSCALE_UNLOCK_DCACHE",
4766 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4767 .access = PL1_W, .type = ARM_CP_NOP },
4768 };
4769
4770 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4771 /*
4772 * RAZ/WI the whole crn=15 space, when we don't have a more specific
4773 * implementation of this implementation-defined space.
4774 * Ideally this should eventually disappear in favour of actually
4775 * implementing the correct behaviour for all cores.
4776 */
4777 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4778 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4779 .access = PL1_RW,
4780 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4781 .resetvalue = 0 },
4782 };
4783
4784 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4785 /* Cache status: RAZ because we have no cache so it's always clean */
4786 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4787 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4788 .resetvalue = 0 },
4789 };
4790
4791 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4792 /* We never have a block transfer operation in progress */
4793 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4794 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4795 .resetvalue = 0 },
4796 /* The cache ops themselves: these all NOP for QEMU */
4797 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4798 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4799 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4800 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4801 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4802 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4803 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4804 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4805 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4806 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4807 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4808 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4809 };
4810
4811 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4812 /*
4813 * The cache test-and-clean instructions always return (1 << 30)
4814 * to indicate that there are no dirty cache lines.
4815 */
4816 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4817 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4818 .resetvalue = (1 << 30) },
4819 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4820 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4821 .resetvalue = (1 << 30) },
4822 };
4823
4824 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4825 /* Ignore ReadBuffer accesses */
4826 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4827 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4828 .access = PL1_RW, .resetvalue = 0,
4829 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4830 };
4831
midr_read(CPUARMState * env,const ARMCPRegInfo * ri)4832 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4833 {
4834 unsigned int cur_el = arm_current_el(env);
4835
4836 if (arm_is_el2_enabled(env) && cur_el == 1) {
4837 return env->cp15.vpidr_el2;
4838 }
4839 return raw_read(env, ri);
4840 }
4841
mpidr_read_val(CPUARMState * env)4842 static uint64_t mpidr_read_val(CPUARMState *env)
4843 {
4844 ARMCPU *cpu = env_archcpu(env);
4845 uint64_t mpidr = cpu->mp_affinity;
4846
4847 if (arm_feature(env, ARM_FEATURE_V7MP)) {
4848 mpidr |= (1U << 31);
4849 /*
4850 * Cores which are uniprocessor (non-coherent)
4851 * but still implement the MP extensions set
4852 * bit 30. (For instance, Cortex-R5).
4853 */
4854 if (cpu->mp_is_up) {
4855 mpidr |= (1u << 30);
4856 }
4857 }
4858 return mpidr;
4859 }
4860
mpidr_read(CPUARMState * env,const ARMCPRegInfo * ri)4861 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4862 {
4863 unsigned int cur_el = arm_current_el(env);
4864
4865 if (arm_is_el2_enabled(env) && cur_el == 1) {
4866 return env->cp15.vmpidr_el2;
4867 }
4868 return mpidr_read_val(env);
4869 }
4870
4871 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4872 /* NOP AMAIR0/1 */
4873 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4874 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4875 .access = PL1_RW, .accessfn = access_tvm_trvm,
4876 .fgt = FGT_AMAIR_EL1,
4877 .nv2_redirect_offset = 0x148 | NV2_REDIR_NV1,
4878 .type = ARM_CP_CONST, .resetvalue = 0 },
4879 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4880 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4881 .access = PL1_RW, .accessfn = access_tvm_trvm,
4882 .type = ARM_CP_CONST, .resetvalue = 0 },
4883 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4884 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4885 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4886 offsetof(CPUARMState, cp15.par_ns)} },
4887 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4888 .access = PL1_RW, .accessfn = access_tvm_trvm,
4889 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4890 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4891 offsetof(CPUARMState, cp15.ttbr0_ns) },
4892 .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4893 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4894 .access = PL1_RW, .accessfn = access_tvm_trvm,
4895 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4896 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4897 offsetof(CPUARMState, cp15.ttbr1_ns) },
4898 .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4899 };
4900
aa64_fpcr_read(CPUARMState * env,const ARMCPRegInfo * ri)4901 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4902 {
4903 return vfp_get_fpcr(env);
4904 }
4905
aa64_fpcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4906 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4907 uint64_t value)
4908 {
4909 vfp_set_fpcr(env, value);
4910 }
4911
aa64_fpsr_read(CPUARMState * env,const ARMCPRegInfo * ri)4912 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4913 {
4914 return vfp_get_fpsr(env);
4915 }
4916
aa64_fpsr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4917 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4918 uint64_t value)
4919 {
4920 vfp_set_fpsr(env, value);
4921 }
4922
aa64_daif_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4923 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4924 bool isread)
4925 {
4926 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4927 return CP_ACCESS_TRAP;
4928 }
4929 return CP_ACCESS_OK;
4930 }
4931
aa64_daif_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4932 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4933 uint64_t value)
4934 {
4935 env->daif = value & PSTATE_DAIF;
4936 }
4937
aa64_pan_read(CPUARMState * env,const ARMCPRegInfo * ri)4938 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4939 {
4940 return env->pstate & PSTATE_PAN;
4941 }
4942
aa64_pan_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4943 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4944 uint64_t value)
4945 {
4946 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4947 }
4948
4949 static const ARMCPRegInfo pan_reginfo = {
4950 .name = "PAN", .state = ARM_CP_STATE_AA64,
4951 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4952 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4953 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4954 };
4955
aa64_uao_read(CPUARMState * env,const ARMCPRegInfo * ri)4956 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4957 {
4958 return env->pstate & PSTATE_UAO;
4959 }
4960
aa64_uao_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4961 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4962 uint64_t value)
4963 {
4964 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4965 }
4966
4967 static const ARMCPRegInfo uao_reginfo = {
4968 .name = "UAO", .state = ARM_CP_STATE_AA64,
4969 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4970 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4971 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4972 };
4973
aa64_dit_read(CPUARMState * env,const ARMCPRegInfo * ri)4974 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4975 {
4976 return env->pstate & PSTATE_DIT;
4977 }
4978
aa64_dit_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4979 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4980 uint64_t value)
4981 {
4982 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4983 }
4984
4985 static const ARMCPRegInfo dit_reginfo = {
4986 .name = "DIT", .state = ARM_CP_STATE_AA64,
4987 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4988 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4989 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4990 };
4991
aa64_ssbs_read(CPUARMState * env,const ARMCPRegInfo * ri)4992 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4993 {
4994 return env->pstate & PSTATE_SSBS;
4995 }
4996
aa64_ssbs_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4997 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4998 uint64_t value)
4999 {
5000 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
5001 }
5002
5003 static const ARMCPRegInfo ssbs_reginfo = {
5004 .name = "SSBS", .state = ARM_CP_STATE_AA64,
5005 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
5006 .type = ARM_CP_NO_RAW, .access = PL0_RW,
5007 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
5008 };
5009
aa64_cacheop_poc_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)5010 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
5011 const ARMCPRegInfo *ri,
5012 bool isread)
5013 {
5014 /* Cache invalidate/clean to Point of Coherency or Persistence... */
5015 switch (arm_current_el(env)) {
5016 case 0:
5017 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
5018 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
5019 return CP_ACCESS_TRAP;
5020 }
5021 /* fall through */
5022 case 1:
5023 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
5024 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
5025 return CP_ACCESS_TRAP_EL2;
5026 }
5027 break;
5028 }
5029 return CP_ACCESS_OK;
5030 }
5031
do_cacheop_pou_access(CPUARMState * env,uint64_t hcrflags)5032 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
5033 {
5034 /* Cache invalidate/clean to Point of Unification... */
5035 switch (arm_current_el(env)) {
5036 case 0:
5037 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
5038 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
5039 return CP_ACCESS_TRAP;
5040 }
5041 /* fall through */
5042 case 1:
5043 /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set. */
5044 if (arm_hcr_el2_eff(env) & hcrflags) {
5045 return CP_ACCESS_TRAP_EL2;
5046 }
5047 break;
5048 }
5049 return CP_ACCESS_OK;
5050 }
5051
access_ticab(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)5052 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
5053 bool isread)
5054 {
5055 return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
5056 }
5057
access_tocu(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)5058 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
5059 bool isread)
5060 {
5061 return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
5062 }
5063
5064 /*
5065 * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
5066 * Page D4-1736 (DDI0487A.b)
5067 */
5068
vae1_tlbmask(CPUARMState * env)5069 static int vae1_tlbmask(CPUARMState *env)
5070 {
5071 uint64_t hcr = arm_hcr_el2_eff(env);
5072 uint16_t mask;
5073
5074 assert(arm_feature(env, ARM_FEATURE_AARCH64));
5075
5076 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5077 mask = ARMMMUIdxBit_E20_2 |
5078 ARMMMUIdxBit_E20_2_PAN |
5079 ARMMMUIdxBit_E20_0;
5080 } else {
5081 /* This is AArch64 only, so we don't need to touch the EL30_x TLBs */
5082 mask = ARMMMUIdxBit_E10_1 |
5083 ARMMMUIdxBit_E10_1_PAN |
5084 ARMMMUIdxBit_E10_0;
5085 }
5086 return mask;
5087 }
5088
vae2_tlbmask(CPUARMState * env)5089 static int vae2_tlbmask(CPUARMState *env)
5090 {
5091 uint64_t hcr = arm_hcr_el2_eff(env);
5092 uint16_t mask;
5093
5094 if (hcr & HCR_E2H) {
5095 mask = ARMMMUIdxBit_E20_2 |
5096 ARMMMUIdxBit_E20_2_PAN |
5097 ARMMMUIdxBit_E20_0;
5098 } else {
5099 mask = ARMMMUIdxBit_E2;
5100 }
5101 return mask;
5102 }
5103
5104 /* Return 56 if TBI is enabled, 64 otherwise. */
tlbbits_for_regime(CPUARMState * env,ARMMMUIdx mmu_idx,uint64_t addr)5105 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
5106 uint64_t addr)
5107 {
5108 uint64_t tcr = regime_tcr(env, mmu_idx);
5109 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
5110 int select = extract64(addr, 55, 1);
5111
5112 return (tbi >> select) & 1 ? 56 : 64;
5113 }
5114
vae1_tlbbits(CPUARMState * env,uint64_t addr)5115 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
5116 {
5117 uint64_t hcr = arm_hcr_el2_eff(env);
5118 ARMMMUIdx mmu_idx;
5119
5120 assert(arm_feature(env, ARM_FEATURE_AARCH64));
5121
5122 /* Only the regime of the mmu_idx below is significant. */
5123 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5124 mmu_idx = ARMMMUIdx_E20_0;
5125 } else {
5126 mmu_idx = ARMMMUIdx_E10_0;
5127 }
5128
5129 return tlbbits_for_regime(env, mmu_idx, addr);
5130 }
5131
vae2_tlbbits(CPUARMState * env,uint64_t addr)5132 static int vae2_tlbbits(CPUARMState *env, uint64_t addr)
5133 {
5134 uint64_t hcr = arm_hcr_el2_eff(env);
5135 ARMMMUIdx mmu_idx;
5136
5137 /*
5138 * Only the regime of the mmu_idx below is significant.
5139 * Regime EL2&0 has two ranges with separate TBI configuration, while EL2
5140 * only has one.
5141 */
5142 if (hcr & HCR_E2H) {
5143 mmu_idx = ARMMMUIdx_E20_2;
5144 } else {
5145 mmu_idx = ARMMMUIdx_E2;
5146 }
5147
5148 return tlbbits_for_regime(env, mmu_idx, addr);
5149 }
5150
tlbi_aa64_vmalle1is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5151 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5152 uint64_t value)
5153 {
5154 CPUState *cs = env_cpu(env);
5155 int mask = vae1_tlbmask(env);
5156
5157 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
5158 }
5159
tlbi_aa64_vmalle1_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5160 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5161 uint64_t value)
5162 {
5163 CPUState *cs = env_cpu(env);
5164 int mask = vae1_tlbmask(env);
5165
5166 if (tlb_force_broadcast(env)) {
5167 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
5168 } else {
5169 tlb_flush_by_mmuidx(cs, mask);
5170 }
5171 }
5172
e2_tlbmask(CPUARMState * env)5173 static int e2_tlbmask(CPUARMState *env)
5174 {
5175 return (ARMMMUIdxBit_E20_0 |
5176 ARMMMUIdxBit_E20_2 |
5177 ARMMMUIdxBit_E20_2_PAN |
5178 ARMMMUIdxBit_E2);
5179 }
5180
tlbi_aa64_alle1_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5181 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5182 uint64_t value)
5183 {
5184 CPUState *cs = env_cpu(env);
5185 int mask = alle1_tlbmask(env);
5186
5187 tlb_flush_by_mmuidx(cs, mask);
5188 }
5189
tlbi_aa64_alle2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5190 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5191 uint64_t value)
5192 {
5193 CPUState *cs = env_cpu(env);
5194 int mask = e2_tlbmask(env);
5195
5196 tlb_flush_by_mmuidx(cs, mask);
5197 }
5198
tlbi_aa64_alle3_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5199 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5200 uint64_t value)
5201 {
5202 ARMCPU *cpu = env_archcpu(env);
5203 CPUState *cs = CPU(cpu);
5204
5205 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
5206 }
5207
tlbi_aa64_alle1is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5208 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5209 uint64_t value)
5210 {
5211 CPUState *cs = env_cpu(env);
5212 int mask = alle1_tlbmask(env);
5213
5214 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
5215 }
5216
tlbi_aa64_alle2is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5217 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5218 uint64_t value)
5219 {
5220 CPUState *cs = env_cpu(env);
5221 int mask = e2_tlbmask(env);
5222
5223 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
5224 }
5225
tlbi_aa64_alle3is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5226 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5227 uint64_t value)
5228 {
5229 CPUState *cs = env_cpu(env);
5230
5231 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
5232 }
5233
tlbi_aa64_vae2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5234 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5235 uint64_t value)
5236 {
5237 /*
5238 * Invalidate by VA, EL2
5239 * Currently handles both VAE2 and VALE2, since we don't support
5240 * flush-last-level-only.
5241 */
5242 CPUState *cs = env_cpu(env);
5243 int mask = vae2_tlbmask(env);
5244 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5245 int bits = vae2_tlbbits(env, pageaddr);
5246
5247 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
5248 }
5249
tlbi_aa64_vae3_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5250 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5251 uint64_t value)
5252 {
5253 /*
5254 * Invalidate by VA, EL3
5255 * Currently handles both VAE3 and VALE3, since we don't support
5256 * flush-last-level-only.
5257 */
5258 ARMCPU *cpu = env_archcpu(env);
5259 CPUState *cs = CPU(cpu);
5260 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5261
5262 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
5263 }
5264
tlbi_aa64_vae1is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5265 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5266 uint64_t value)
5267 {
5268 CPUState *cs = env_cpu(env);
5269 int mask = vae1_tlbmask(env);
5270 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5271 int bits = vae1_tlbbits(env, pageaddr);
5272
5273 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
5274 }
5275
tlbi_aa64_vae1_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5276 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5277 uint64_t value)
5278 {
5279 /*
5280 * Invalidate by VA, EL1&0 (AArch64 version).
5281 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
5282 * since we don't support flush-for-specific-ASID-only or
5283 * flush-last-level-only.
5284 */
5285 CPUState *cs = env_cpu(env);
5286 int mask = vae1_tlbmask(env);
5287 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5288 int bits = vae1_tlbbits(env, pageaddr);
5289
5290 if (tlb_force_broadcast(env)) {
5291 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
5292 } else {
5293 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
5294 }
5295 }
5296
tlbi_aa64_vae2is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5297 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5298 uint64_t value)
5299 {
5300 CPUState *cs = env_cpu(env);
5301 int mask = vae2_tlbmask(env);
5302 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5303 int bits = vae2_tlbbits(env, pageaddr);
5304
5305 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
5306 }
5307
tlbi_aa64_vae3is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5308 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5309 uint64_t value)
5310 {
5311 CPUState *cs = env_cpu(env);
5312 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5313 int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
5314
5315 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
5316 ARMMMUIdxBit_E3, bits);
5317 }
5318
ipas2e1_tlbmask(CPUARMState * env,int64_t value)5319 static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
5320 {
5321 /*
5322 * The MSB of value is the NS field, which only applies if SEL2
5323 * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
5324 */
5325 return (value >= 0
5326 && cpu_isar_feature(aa64_sel2, env_archcpu(env))
5327 && arm_is_secure_below_el3(env)
5328 ? ARMMMUIdxBit_Stage2_S
5329 : ARMMMUIdxBit_Stage2);
5330 }
5331
tlbi_aa64_ipas2e1_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5332 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5333 uint64_t value)
5334 {
5335 CPUState *cs = env_cpu(env);
5336 int mask = ipas2e1_tlbmask(env, value);
5337 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5338
5339 if (tlb_force_broadcast(env)) {
5340 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
5341 } else {
5342 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
5343 }
5344 }
5345
tlbi_aa64_ipas2e1is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5346 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5347 uint64_t value)
5348 {
5349 CPUState *cs = env_cpu(env);
5350 int mask = ipas2e1_tlbmask(env, value);
5351 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5352
5353 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
5354 }
5355
5356 #ifdef TARGET_AARCH64
5357 typedef struct {
5358 uint64_t base;
5359 uint64_t length;
5360 } TLBIRange;
5361
tlbi_range_tg_to_gran_size(int tg)5362 static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
5363 {
5364 /*
5365 * Note that the TLBI range TG field encoding differs from both
5366 * TG0 and TG1 encodings.
5367 */
5368 switch (tg) {
5369 case 1:
5370 return Gran4K;
5371 case 2:
5372 return Gran16K;
5373 case 3:
5374 return Gran64K;
5375 default:
5376 return GranInvalid;
5377 }
5378 }
5379
tlbi_aa64_get_range(CPUARMState * env,ARMMMUIdx mmuidx,uint64_t value)5380 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
5381 uint64_t value)
5382 {
5383 unsigned int page_size_granule, page_shift, num, scale, exponent;
5384 /* Extract one bit to represent the va selector in use. */
5385 uint64_t select = sextract64(value, 36, 1);
5386 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true, false);
5387 TLBIRange ret = { };
5388 ARMGranuleSize gran;
5389
5390 page_size_granule = extract64(value, 46, 2);
5391 gran = tlbi_range_tg_to_gran_size(page_size_granule);
5392
5393 /* The granule encoded in value must match the granule in use. */
5394 if (gran != param.gran) {
5395 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
5396 page_size_granule);
5397 return ret;
5398 }
5399
5400 page_shift = arm_granule_bits(gran);
5401 num = extract64(value, 39, 5);
5402 scale = extract64(value, 44, 2);
5403 exponent = (5 * scale) + 1;
5404
5405 ret.length = (num + 1) << (exponent + page_shift);
5406
5407 if (param.select) {
5408 ret.base = sextract64(value, 0, 37);
5409 } else {
5410 ret.base = extract64(value, 0, 37);
5411 }
5412 if (param.ds) {
5413 /*
5414 * With DS=1, BaseADDR is always shifted 16 so that it is able
5415 * to address all 52 va bits. The input address is perforce
5416 * aligned on a 64k boundary regardless of translation granule.
5417 */
5418 page_shift = 16;
5419 }
5420 ret.base <<= page_shift;
5421
5422 return ret;
5423 }
5424
do_rvae_write(CPUARMState * env,uint64_t value,int idxmap,bool synced)5425 static void do_rvae_write(CPUARMState *env, uint64_t value,
5426 int idxmap, bool synced)
5427 {
5428 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
5429 TLBIRange range;
5430 int bits;
5431
5432 range = tlbi_aa64_get_range(env, one_idx, value);
5433 bits = tlbbits_for_regime(env, one_idx, range.base);
5434
5435 if (synced) {
5436 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
5437 range.base,
5438 range.length,
5439 idxmap,
5440 bits);
5441 } else {
5442 tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
5443 range.length, idxmap, bits);
5444 }
5445 }
5446
tlbi_aa64_rvae1_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5447 static void tlbi_aa64_rvae1_write(CPUARMState *env,
5448 const ARMCPRegInfo *ri,
5449 uint64_t value)
5450 {
5451 /*
5452 * Invalidate by VA range, EL1&0.
5453 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
5454 * since we don't support flush-for-specific-ASID-only or
5455 * flush-last-level-only.
5456 */
5457
5458 do_rvae_write(env, value, vae1_tlbmask(env),
5459 tlb_force_broadcast(env));
5460 }
5461
tlbi_aa64_rvae1is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5462 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
5463 const ARMCPRegInfo *ri,
5464 uint64_t value)
5465 {
5466 /*
5467 * Invalidate by VA range, Inner/Outer Shareable EL1&0.
5468 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
5469 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
5470 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
5471 * shareable specific flushes.
5472 */
5473
5474 do_rvae_write(env, value, vae1_tlbmask(env), true);
5475 }
5476
tlbi_aa64_rvae2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5477 static void tlbi_aa64_rvae2_write(CPUARMState *env,
5478 const ARMCPRegInfo *ri,
5479 uint64_t value)
5480 {
5481 /*
5482 * Invalidate by VA range, EL2.
5483 * Currently handles all of RVAE2 and RVALE2,
5484 * since we don't support flush-for-specific-ASID-only or
5485 * flush-last-level-only.
5486 */
5487
5488 do_rvae_write(env, value, vae2_tlbmask(env),
5489 tlb_force_broadcast(env));
5490
5491
5492 }
5493
tlbi_aa64_rvae2is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5494 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
5495 const ARMCPRegInfo *ri,
5496 uint64_t value)
5497 {
5498 /*
5499 * Invalidate by VA range, Inner/Outer Shareable, EL2.
5500 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
5501 * since we don't support flush-for-specific-ASID-only,
5502 * flush-last-level-only or inner/outer shareable specific flushes.
5503 */
5504
5505 do_rvae_write(env, value, vae2_tlbmask(env), true);
5506
5507 }
5508
tlbi_aa64_rvae3_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5509 static void tlbi_aa64_rvae3_write(CPUARMState *env,
5510 const ARMCPRegInfo *ri,
5511 uint64_t value)
5512 {
5513 /*
5514 * Invalidate by VA range, EL3.
5515 * Currently handles all of RVAE3 and RVALE3,
5516 * since we don't support flush-for-specific-ASID-only or
5517 * flush-last-level-only.
5518 */
5519
5520 do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
5521 }
5522
tlbi_aa64_rvae3is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5523 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5524 const ARMCPRegInfo *ri,
5525 uint64_t value)
5526 {
5527 /*
5528 * Invalidate by VA range, EL3, Inner/Outer Shareable.
5529 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5530 * since we don't support flush-for-specific-ASID-only,
5531 * flush-last-level-only or inner/outer specific flushes.
5532 */
5533
5534 do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
5535 }
5536
tlbi_aa64_ripas2e1_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5537 static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5538 uint64_t value)
5539 {
5540 do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5541 tlb_force_broadcast(env));
5542 }
5543
tlbi_aa64_ripas2e1is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5544 static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5545 const ARMCPRegInfo *ri,
5546 uint64_t value)
5547 {
5548 do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5549 }
5550 #endif
5551
aa64_zva_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)5552 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5553 bool isread)
5554 {
5555 int cur_el = arm_current_el(env);
5556
5557 if (cur_el < 2) {
5558 uint64_t hcr = arm_hcr_el2_eff(env);
5559
5560 if (cur_el == 0) {
5561 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5562 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5563 return CP_ACCESS_TRAP_EL2;
5564 }
5565 } else {
5566 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5567 return CP_ACCESS_TRAP;
5568 }
5569 if (hcr & HCR_TDZ) {
5570 return CP_ACCESS_TRAP_EL2;
5571 }
5572 }
5573 } else if (hcr & HCR_TDZ) {
5574 return CP_ACCESS_TRAP_EL2;
5575 }
5576 }
5577 return CP_ACCESS_OK;
5578 }
5579
aa64_dczid_read(CPUARMState * env,const ARMCPRegInfo * ri)5580 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5581 {
5582 ARMCPU *cpu = env_archcpu(env);
5583 int dzp_bit = 1 << 4;
5584
5585 /* DZP indicates whether DC ZVA access is allowed */
5586 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
5587 dzp_bit = 0;
5588 }
5589 return cpu->dcz_blocksize | dzp_bit;
5590 }
5591
sp_el0_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)5592 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5593 bool isread)
5594 {
5595 if (!(env->pstate & PSTATE_SP)) {
5596 /*
5597 * Access to SP_EL0 is undefined if it's being used as
5598 * the stack pointer.
5599 */
5600 return CP_ACCESS_TRAP_UNCATEGORIZED;
5601 }
5602 return CP_ACCESS_OK;
5603 }
5604
spsel_read(CPUARMState * env,const ARMCPRegInfo * ri)5605 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5606 {
5607 return env->pstate & PSTATE_SP;
5608 }
5609
spsel_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t val)5610 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5611 {
5612 update_spsel(env, val);
5613 }
5614
sctlr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5615 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5616 uint64_t value)
5617 {
5618 ARMCPU *cpu = env_archcpu(env);
5619
5620 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5621 /* M bit is RAZ/WI for PMSA with no MPU implemented */
5622 value &= ~SCTLR_M;
5623 }
5624
5625 /* ??? Lots of these bits are not implemented. */
5626
5627 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5628 if (ri->opc1 == 6) { /* SCTLR_EL3 */
5629 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5630 } else {
5631 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5632 SCTLR_ATA0 | SCTLR_ATA);
5633 }
5634 }
5635
5636 if (raw_read(env, ri) == value) {
5637 /*
5638 * Skip the TLB flush if nothing actually changed; Linux likes
5639 * to do a lot of pointless SCTLR writes.
5640 */
5641 return;
5642 }
5643
5644 raw_write(env, ri, value);
5645
5646 /* This may enable/disable the MMU, so do a TLB flush. */
5647 tlb_flush(CPU(cpu));
5648
5649 if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
5650 /*
5651 * Normally we would always end the TB on an SCTLR write; see the
5652 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5653 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5654 * of hflags from the translator, so do it here.
5655 */
5656 arm_rebuild_hflags(env);
5657 }
5658 }
5659
mdcr_el3_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5660 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5661 uint64_t value)
5662 {
5663 /*
5664 * Some MDCR_EL3 bits affect whether PMU counters are running:
5665 * if we are trying to change any of those then we must
5666 * bracket this update with PMU start/finish calls.
5667 */
5668 bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5669
5670 if (pmu_op) {
5671 pmu_op_start(env);
5672 }
5673 env->cp15.mdcr_el3 = value;
5674 if (pmu_op) {
5675 pmu_op_finish(env);
5676 }
5677 }
5678
sdcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5679 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5680 uint64_t value)
5681 {
5682 /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5683 mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5684 }
5685
mdcr_el2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5686 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5687 uint64_t value)
5688 {
5689 /*
5690 * Some MDCR_EL2 bits affect whether PMU counters are running:
5691 * if we are trying to change any of those then we must
5692 * bracket this update with PMU start/finish calls.
5693 */
5694 bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5695
5696 if (pmu_op) {
5697 pmu_op_start(env);
5698 }
5699 env->cp15.mdcr_el2 = value;
5700 if (pmu_op) {
5701 pmu_op_finish(env);
5702 }
5703 }
5704
access_nv1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)5705 static CPAccessResult access_nv1(CPUARMState *env, const ARMCPRegInfo *ri,
5706 bool isread)
5707 {
5708 if (arm_current_el(env) == 1) {
5709 uint64_t hcr_nv = arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1 | HCR_NV2);
5710
5711 if (hcr_nv == (HCR_NV | HCR_NV1)) {
5712 return CP_ACCESS_TRAP_EL2;
5713 }
5714 }
5715 return CP_ACCESS_OK;
5716 }
5717
5718 #ifdef CONFIG_USER_ONLY
5719 /*
5720 * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
5721 * code to get around W^X restrictions, where one region is writable and the
5722 * other is executable.
5723 *
5724 * Since the executable region is never written to we cannot detect code
5725 * changes when running in user mode, and rely on the emulated JIT telling us
5726 * that the code has changed by executing this instruction.
5727 */
ic_ivau_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5728 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
5729 uint64_t value)
5730 {
5731 uint64_t icache_line_mask, start_address, end_address;
5732 const ARMCPU *cpu;
5733
5734 cpu = env_archcpu(env);
5735
5736 icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
5737 start_address = value & ~icache_line_mask;
5738 end_address = value | icache_line_mask;
5739
5740 mmap_lock();
5741
5742 tb_invalidate_phys_range(start_address, end_address);
5743
5744 mmap_unlock();
5745 }
5746 #endif
5747
5748 static const ARMCPRegInfo v8_cp_reginfo[] = {
5749 /*
5750 * Minimal set of EL0-visible registers. This will need to be expanded
5751 * significantly for system emulation of AArch64 CPUs.
5752 */
5753 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5754 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5755 .access = PL0_RW, .type = ARM_CP_NZCV },
5756 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5757 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5758 .type = ARM_CP_NO_RAW,
5759 .access = PL0_RW, .accessfn = aa64_daif_access,
5760 .fieldoffset = offsetof(CPUARMState, daif),
5761 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5762 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5763 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5764 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5765 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5766 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5767 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5768 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5769 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5770 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5771 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5772 .access = PL0_R, .type = ARM_CP_NO_RAW,
5773 .fgt = FGT_DCZID_EL0,
5774 .readfn = aa64_dczid_read },
5775 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5776 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5777 .access = PL0_W, .type = ARM_CP_DC_ZVA,
5778 #ifndef CONFIG_USER_ONLY
5779 /* Avoid overhead of an access check that always passes in user-mode */
5780 .accessfn = aa64_zva_access,
5781 .fgt = FGT_DCZVA,
5782 #endif
5783 },
5784 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5785 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5786 .access = PL1_R, .type = ARM_CP_CURRENTEL },
5787 /*
5788 * Instruction cache ops. All of these except `IC IVAU` NOP because we
5789 * don't emulate caches.
5790 */
5791 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5792 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5793 .access = PL1_W, .type = ARM_CP_NOP,
5794 .fgt = FGT_ICIALLUIS,
5795 .accessfn = access_ticab },
5796 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5797 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5798 .access = PL1_W, .type = ARM_CP_NOP,
5799 .fgt = FGT_ICIALLU,
5800 .accessfn = access_tocu },
5801 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5802 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5803 .access = PL0_W,
5804 .fgt = FGT_ICIVAU,
5805 .accessfn = access_tocu,
5806 #ifdef CONFIG_USER_ONLY
5807 .type = ARM_CP_NO_RAW,
5808 .writefn = ic_ivau_write
5809 #else
5810 .type = ARM_CP_NOP
5811 #endif
5812 },
5813 /* Cache ops: all NOPs since we don't emulate caches */
5814 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5815 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5816 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5817 .fgt = FGT_DCIVAC,
5818 .type = ARM_CP_NOP },
5819 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5820 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5821 .fgt = FGT_DCISW,
5822 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5823 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5824 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5825 .access = PL0_W, .type = ARM_CP_NOP,
5826 .fgt = FGT_DCCVAC,
5827 .accessfn = aa64_cacheop_poc_access },
5828 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5829 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5830 .fgt = FGT_DCCSW,
5831 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5832 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5833 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5834 .access = PL0_W, .type = ARM_CP_NOP,
5835 .fgt = FGT_DCCVAU,
5836 .accessfn = access_tocu },
5837 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5838 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5839 .access = PL0_W, .type = ARM_CP_NOP,
5840 .fgt = FGT_DCCIVAC,
5841 .accessfn = aa64_cacheop_poc_access },
5842 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5843 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5844 .fgt = FGT_DCCISW,
5845 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5846 /* TLBI operations */
5847 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
5848 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
5849 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5850 .fgt = FGT_TLBIVMALLE1IS,
5851 .writefn = tlbi_aa64_vmalle1is_write },
5852 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
5853 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
5854 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5855 .fgt = FGT_TLBIVAE1IS,
5856 .writefn = tlbi_aa64_vae1is_write },
5857 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
5858 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
5859 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5860 .fgt = FGT_TLBIASIDE1IS,
5861 .writefn = tlbi_aa64_vmalle1is_write },
5862 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
5863 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
5864 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5865 .fgt = FGT_TLBIVAAE1IS,
5866 .writefn = tlbi_aa64_vae1is_write },
5867 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
5868 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5869 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5870 .fgt = FGT_TLBIVALE1IS,
5871 .writefn = tlbi_aa64_vae1is_write },
5872 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
5873 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5874 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5875 .fgt = FGT_TLBIVAALE1IS,
5876 .writefn = tlbi_aa64_vae1is_write },
5877 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
5878 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
5879 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5880 .fgt = FGT_TLBIVMALLE1,
5881 .writefn = tlbi_aa64_vmalle1_write },
5882 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
5883 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
5884 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5885 .fgt = FGT_TLBIVAE1,
5886 .writefn = tlbi_aa64_vae1_write },
5887 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
5888 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
5889 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5890 .fgt = FGT_TLBIASIDE1,
5891 .writefn = tlbi_aa64_vmalle1_write },
5892 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
5893 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
5894 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5895 .fgt = FGT_TLBIVAAE1,
5896 .writefn = tlbi_aa64_vae1_write },
5897 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
5898 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5899 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5900 .fgt = FGT_TLBIVALE1,
5901 .writefn = tlbi_aa64_vae1_write },
5902 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
5903 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5904 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5905 .fgt = FGT_TLBIVAALE1,
5906 .writefn = tlbi_aa64_vae1_write },
5907 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5908 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5909 .access = PL2_W, .type = ARM_CP_NO_RAW,
5910 .writefn = tlbi_aa64_ipas2e1is_write },
5911 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5912 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5913 .access = PL2_W, .type = ARM_CP_NO_RAW,
5914 .writefn = tlbi_aa64_ipas2e1is_write },
5915 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5916 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5917 .access = PL2_W, .type = ARM_CP_NO_RAW,
5918 .writefn = tlbi_aa64_alle1is_write },
5919 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5920 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5921 .access = PL2_W, .type = ARM_CP_NO_RAW,
5922 .writefn = tlbi_aa64_alle1is_write },
5923 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5924 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5925 .access = PL2_W, .type = ARM_CP_NO_RAW,
5926 .writefn = tlbi_aa64_ipas2e1_write },
5927 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5928 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5929 .access = PL2_W, .type = ARM_CP_NO_RAW,
5930 .writefn = tlbi_aa64_ipas2e1_write },
5931 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5932 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5933 .access = PL2_W, .type = ARM_CP_NO_RAW,
5934 .writefn = tlbi_aa64_alle1_write },
5935 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5936 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5937 .access = PL2_W, .type = ARM_CP_NO_RAW,
5938 .writefn = tlbi_aa64_alle1is_write },
5939 #ifndef CONFIG_USER_ONLY
5940 /* 64 bit address translation operations */
5941 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5942 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5943 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5944 .fgt = FGT_ATS1E1R,
5945 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5946 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5947 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5948 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5949 .fgt = FGT_ATS1E1W,
5950 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5951 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5952 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5953 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5954 .fgt = FGT_ATS1E0R,
5955 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5956 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5957 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5958 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5959 .fgt = FGT_ATS1E0W,
5960 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5961 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5962 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5963 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5964 .accessfn = at_e012_access, .writefn = ats_write64 },
5965 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5966 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5967 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5968 .accessfn = at_e012_access, .writefn = ats_write64 },
5969 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5970 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5971 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5972 .accessfn = at_e012_access, .writefn = ats_write64 },
5973 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5974 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5975 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5976 .accessfn = at_e012_access, .writefn = ats_write64 },
5977 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5978 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5979 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5980 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5981 .writefn = ats_write64 },
5982 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5983 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5984 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5985 .writefn = ats_write64 },
5986 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5987 .type = ARM_CP_ALIAS,
5988 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5989 .access = PL1_RW, .resetvalue = 0,
5990 .fgt = FGT_PAR_EL1,
5991 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5992 .writefn = par_write },
5993 #endif
5994 /* TLB invalidate last level of translation table walk */
5995 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5996 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5997 .writefn = tlbimva_is_write },
5998 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5999 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
6000 .writefn = tlbimvaa_is_write },
6001 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
6002 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
6003 .writefn = tlbimva_write },
6004 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
6005 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
6006 .writefn = tlbimvaa_write },
6007 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
6008 .type = ARM_CP_NO_RAW, .access = PL2_W,
6009 .writefn = tlbimva_hyp_write },
6010 { .name = "TLBIMVALHIS",
6011 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
6012 .type = ARM_CP_NO_RAW, .access = PL2_W,
6013 .writefn = tlbimva_hyp_is_write },
6014 { .name = "TLBIIPAS2",
6015 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
6016 .type = ARM_CP_NO_RAW, .access = PL2_W,
6017 .writefn = tlbiipas2_hyp_write },
6018 { .name = "TLBIIPAS2IS",
6019 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
6020 .type = ARM_CP_NO_RAW, .access = PL2_W,
6021 .writefn = tlbiipas2is_hyp_write },
6022 { .name = "TLBIIPAS2L",
6023 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
6024 .type = ARM_CP_NO_RAW, .access = PL2_W,
6025 .writefn = tlbiipas2_hyp_write },
6026 { .name = "TLBIIPAS2LIS",
6027 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
6028 .type = ARM_CP_NO_RAW, .access = PL2_W,
6029 .writefn = tlbiipas2is_hyp_write },
6030 /* 32 bit cache operations */
6031 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
6032 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
6033 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
6034 .type = ARM_CP_NOP, .access = PL1_W },
6035 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
6036 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
6037 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
6038 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
6039 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
6040 .type = ARM_CP_NOP, .access = PL1_W },
6041 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
6042 .type = ARM_CP_NOP, .access = PL1_W },
6043 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
6044 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
6045 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
6046 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6047 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
6048 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
6049 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
6050 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6051 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
6052 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
6053 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
6054 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
6055 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
6056 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
6057 /* MMU Domain access control / MPU write buffer control */
6058 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
6059 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
6060 .writefn = dacr_write, .raw_writefn = raw_write,
6061 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
6062 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
6063 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
6064 .type = ARM_CP_ALIAS,
6065 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6066 .access = PL1_RW, .accessfn = access_nv1,
6067 .nv2_redirect_offset = 0x230 | NV2_REDIR_NV1,
6068 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
6069 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
6070 .type = ARM_CP_ALIAS,
6071 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
6072 .access = PL1_RW, .accessfn = access_nv1,
6073 .nv2_redirect_offset = 0x160 | NV2_REDIR_NV1,
6074 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
6075 /*
6076 * We rely on the access checks not allowing the guest to write to the
6077 * state field when SPSel indicates that it's being used as the stack
6078 * pointer.
6079 */
6080 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
6081 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
6082 .access = PL1_RW, .accessfn = sp_el0_access,
6083 .type = ARM_CP_ALIAS,
6084 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
6085 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
6086 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
6087 .nv2_redirect_offset = 0x240,
6088 .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
6089 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
6090 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
6091 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
6092 .type = ARM_CP_NO_RAW,
6093 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
6094 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
6095 .type = ARM_CP_ALIAS,
6096 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
6097 .access = PL2_RW,
6098 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
6099 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
6100 .type = ARM_CP_ALIAS,
6101 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
6102 .access = PL2_RW,
6103 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
6104 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
6105 .type = ARM_CP_ALIAS,
6106 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
6107 .access = PL2_RW,
6108 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
6109 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
6110 .type = ARM_CP_ALIAS,
6111 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
6112 .access = PL2_RW,
6113 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
6114 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
6115 .type = ARM_CP_IO,
6116 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
6117 .resetvalue = 0,
6118 .access = PL3_RW,
6119 .writefn = mdcr_el3_write,
6120 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
6121 { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
6122 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
6123 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6124 .writefn = sdcr_write,
6125 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
6126 };
6127
6128 /* These are present only when EL1 supports AArch32 */
6129 static const ARMCPRegInfo v8_aa32_el1_reginfo[] = {
6130 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
6131 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
6132 .access = PL2_RW,
6133 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
6134 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
6135 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
6136 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
6137 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6138 .writefn = dacr_write, .raw_writefn = raw_write,
6139 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
6140 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
6141 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
6142 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6143 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
6144 };
6145
do_hcr_write(CPUARMState * env,uint64_t value,uint64_t valid_mask)6146 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
6147 {
6148 ARMCPU *cpu = env_archcpu(env);
6149
6150 if (arm_feature(env, ARM_FEATURE_V8)) {
6151 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
6152 } else {
6153 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
6154 }
6155
6156 if (arm_feature(env, ARM_FEATURE_EL3)) {
6157 valid_mask &= ~HCR_HCD;
6158 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
6159 /*
6160 * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
6161 * However, if we're using the SMC PSCI conduit then QEMU is
6162 * effectively acting like EL3 firmware and so the guest at
6163 * EL2 should retain the ability to prevent EL1 from being
6164 * able to make SMC calls into the ersatz firmware, so in
6165 * that case HCR.TSC should be read/write.
6166 */
6167 valid_mask &= ~HCR_TSC;
6168 }
6169
6170 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
6171 if (cpu_isar_feature(aa64_vh, cpu)) {
6172 valid_mask |= HCR_E2H;
6173 }
6174 if (cpu_isar_feature(aa64_ras, cpu)) {
6175 valid_mask |= HCR_TERR | HCR_TEA;
6176 }
6177 if (cpu_isar_feature(aa64_lor, cpu)) {
6178 valid_mask |= HCR_TLOR;
6179 }
6180 if (cpu_isar_feature(aa64_pauth, cpu)) {
6181 valid_mask |= HCR_API | HCR_APK;
6182 }
6183 if (cpu_isar_feature(aa64_mte, cpu)) {
6184 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
6185 }
6186 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
6187 valid_mask |= HCR_ENSCXT;
6188 }
6189 if (cpu_isar_feature(aa64_fwb, cpu)) {
6190 valid_mask |= HCR_FWB;
6191 }
6192 if (cpu_isar_feature(aa64_rme, cpu)) {
6193 valid_mask |= HCR_GPF;
6194 }
6195 if (cpu_isar_feature(aa64_nv, cpu)) {
6196 valid_mask |= HCR_NV | HCR_NV1 | HCR_AT;
6197 }
6198 if (cpu_isar_feature(aa64_nv2, cpu)) {
6199 valid_mask |= HCR_NV2;
6200 }
6201 }
6202
6203 if (cpu_isar_feature(any_evt, cpu)) {
6204 valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
6205 } else if (cpu_isar_feature(any_half_evt, cpu)) {
6206 valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
6207 }
6208
6209 /* Clear RES0 bits. */
6210 value &= valid_mask;
6211
6212 /*
6213 * These bits change the MMU setup:
6214 * HCR_VM enables stage 2 translation
6215 * HCR_PTW forbids certain page-table setups
6216 * HCR_DC disables stage1 and enables stage2 translation
6217 * HCR_DCT enables tagging on (disabled) stage1 translation
6218 * HCR_FWB changes the interpretation of stage2 descriptor bits
6219 * HCR_NV and HCR_NV1 affect interpretation of descriptor bits
6220 */
6221 if ((env->cp15.hcr_el2 ^ value) &
6222 (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) {
6223 tlb_flush(CPU(cpu));
6224 }
6225 env->cp15.hcr_el2 = value;
6226
6227 /*
6228 * Updates to VI and VF require us to update the status of
6229 * virtual interrupts, which are the logical OR of these bits
6230 * and the state of the input lines from the GIC. (This requires
6231 * that we have the BQL, which is done by marking the
6232 * reginfo structs as ARM_CP_IO.)
6233 * Note that if a write to HCR pends a VIRQ or VFIQ or VINMI or
6234 * VFNMI, it is never possible for it to be taken immediately
6235 * because VIRQ, VFIQ, VINMI and VFNMI are masked unless running
6236 * at EL0 or EL1, and HCR can only be written at EL2.
6237 */
6238 g_assert(bql_locked());
6239 arm_cpu_update_virq(cpu);
6240 arm_cpu_update_vfiq(cpu);
6241 arm_cpu_update_vserr(cpu);
6242 if (cpu_isar_feature(aa64_nmi, cpu)) {
6243 arm_cpu_update_vinmi(cpu);
6244 arm_cpu_update_vfnmi(cpu);
6245 }
6246 }
6247
hcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6248 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
6249 {
6250 do_hcr_write(env, value, 0);
6251 }
6252
hcr_writehigh(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6253 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
6254 uint64_t value)
6255 {
6256 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
6257 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
6258 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
6259 }
6260
hcr_writelow(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6261 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
6262 uint64_t value)
6263 {
6264 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
6265 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
6266 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
6267 }
6268
6269 /*
6270 * Return the effective value of HCR_EL2, at the given security state.
6271 * Bits that are not included here:
6272 * RW (read from SCR_EL3.RW as needed)
6273 */
arm_hcr_el2_eff_secstate(CPUARMState * env,ARMSecuritySpace space)6274 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
6275 {
6276 uint64_t ret = env->cp15.hcr_el2;
6277
6278 assert(space != ARMSS_Root);
6279
6280 if (!arm_is_el2_enabled_secstate(env, space)) {
6281 /*
6282 * "This register has no effect if EL2 is not enabled in the
6283 * current Security state". This is ARMv8.4-SecEL2 speak for
6284 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
6285 *
6286 * Prior to that, the language was "In an implementation that
6287 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
6288 * as if this field is 0 for all purposes other than a direct
6289 * read or write access of HCR_EL2". With lots of enumeration
6290 * on a per-field basis. In current QEMU, this is condition
6291 * is arm_is_secure_below_el3.
6292 *
6293 * Since the v8.4 language applies to the entire register, and
6294 * appears to be backward compatible, use that.
6295 */
6296 return 0;
6297 }
6298
6299 /*
6300 * For a cpu that supports both aarch64 and aarch32, we can set bits
6301 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
6302 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
6303 */
6304 if (!arm_el_is_aa64(env, 2)) {
6305 uint64_t aa32_valid;
6306
6307 /*
6308 * These bits are up-to-date as of ARMv8.6.
6309 * For HCR, it's easiest to list just the 2 bits that are invalid.
6310 * For HCR2, list those that are valid.
6311 */
6312 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
6313 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
6314 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
6315 ret &= aa32_valid;
6316 }
6317
6318 if (ret & HCR_TGE) {
6319 /* These bits are up-to-date as of ARMv8.6. */
6320 if (ret & HCR_E2H) {
6321 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
6322 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
6323 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
6324 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
6325 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
6326 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
6327 } else {
6328 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
6329 }
6330 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
6331 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
6332 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
6333 HCR_TLOR);
6334 }
6335
6336 return ret;
6337 }
6338
arm_hcr_el2_eff(CPUARMState * env)6339 uint64_t arm_hcr_el2_eff(CPUARMState *env)
6340 {
6341 if (arm_feature(env, ARM_FEATURE_M)) {
6342 return 0;
6343 }
6344 return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
6345 }
6346
6347 /*
6348 * Corresponds to ARM pseudocode function ELIsInHost().
6349 */
el_is_in_host(CPUARMState * env,int el)6350 bool el_is_in_host(CPUARMState *env, int el)
6351 {
6352 uint64_t mask;
6353
6354 /*
6355 * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
6356 * Perform the simplest bit tests first, and validate EL2 afterward.
6357 */
6358 if (el & 1) {
6359 return false; /* EL1 or EL3 */
6360 }
6361
6362 /*
6363 * Note that hcr_write() checks isar_feature_aa64_vh(),
6364 * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
6365 */
6366 mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
6367 if ((env->cp15.hcr_el2 & mask) != mask) {
6368 return false;
6369 }
6370
6371 /* TGE and/or E2H set: double check those bits are currently legal. */
6372 return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
6373 }
6374
hcrx_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6375 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
6376 uint64_t value)
6377 {
6378 ARMCPU *cpu = env_archcpu(env);
6379 uint64_t valid_mask = 0;
6380
6381 /* FEAT_MOPS adds MSCEn and MCE2 */
6382 if (cpu_isar_feature(aa64_mops, cpu)) {
6383 valid_mask |= HCRX_MSCEN | HCRX_MCE2;
6384 }
6385
6386 /* FEAT_NMI adds TALLINT, VINMI and VFNMI */
6387 if (cpu_isar_feature(aa64_nmi, cpu)) {
6388 valid_mask |= HCRX_TALLINT | HCRX_VINMI | HCRX_VFNMI;
6389 }
6390 /* FEAT_CMOW adds CMOW */
6391
6392 if (cpu_isar_feature(aa64_cmow, cpu)) {
6393 valid_mask |= HCRX_CMOW;
6394 }
6395
6396 /* Clear RES0 bits. */
6397 env->cp15.hcrx_el2 = value & valid_mask;
6398
6399 /*
6400 * Updates to VINMI and VFNMI require us to update the status of
6401 * virtual NMI, which are the logical OR of these bits
6402 * and the state of the input lines from the GIC. (This requires
6403 * that we have the BQL, which is done by marking the
6404 * reginfo structs as ARM_CP_IO.)
6405 * Note that if a write to HCRX pends a VINMI or VFNMI it is never
6406 * possible for it to be taken immediately, because VINMI and
6407 * VFNMI are masked unless running at EL0 or EL1, and HCRX
6408 * can only be written at EL2.
6409 */
6410 if (cpu_isar_feature(aa64_nmi, cpu)) {
6411 g_assert(bql_locked());
6412 arm_cpu_update_vinmi(cpu);
6413 arm_cpu_update_vfnmi(cpu);
6414 }
6415 }
6416
access_hxen(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6417 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
6418 bool isread)
6419 {
6420 if (arm_current_el(env) == 2
6421 && arm_feature(env, ARM_FEATURE_EL3)
6422 && !(env->cp15.scr_el3 & SCR_HXEN)) {
6423 return CP_ACCESS_TRAP_EL3;
6424 }
6425 return CP_ACCESS_OK;
6426 }
6427
6428 static const ARMCPRegInfo hcrx_el2_reginfo = {
6429 .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
6430 .type = ARM_CP_IO,
6431 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
6432 .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
6433 .nv2_redirect_offset = 0xa0,
6434 .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
6435 };
6436
6437 /* Return the effective value of HCRX_EL2. */
arm_hcrx_el2_eff(CPUARMState * env)6438 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
6439 {
6440 /*
6441 * The bits in this register behave as 0 for all purposes other than
6442 * direct reads of the register if SCR_EL3.HXEn is 0.
6443 * If EL2 is not enabled in the current security state, then the
6444 * bit may behave as if 0, or as if 1, depending on the bit.
6445 * For the moment, we treat the EL2-disabled case as taking
6446 * priority over the HXEn-disabled case. This is true for the only
6447 * bit for a feature which we implement where the answer is different
6448 * for the two cases (MSCEn for FEAT_MOPS).
6449 * This may need to be revisited for future bits.
6450 */
6451 if (!arm_is_el2_enabled(env)) {
6452 uint64_t hcrx = 0;
6453 if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
6454 /* MSCEn behaves as 1 if EL2 is not enabled */
6455 hcrx |= HCRX_MSCEN;
6456 }
6457 return hcrx;
6458 }
6459 if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
6460 return 0;
6461 }
6462 return env->cp15.hcrx_el2;
6463 }
6464
cptr_el2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6465 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
6466 uint64_t value)
6467 {
6468 /*
6469 * For A-profile AArch32 EL3, if NSACR.CP10
6470 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6471 */
6472 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6473 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6474 uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6475 value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
6476 }
6477 env->cp15.cptr_el[2] = value;
6478 }
6479
cptr_el2_read(CPUARMState * env,const ARMCPRegInfo * ri)6480 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
6481 {
6482 /*
6483 * For A-profile AArch32 EL3, if NSACR.CP10
6484 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6485 */
6486 uint64_t value = env->cp15.cptr_el[2];
6487
6488 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6489 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6490 value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6491 }
6492 return value;
6493 }
6494
6495 static const ARMCPRegInfo el2_cp_reginfo[] = {
6496 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
6497 .type = ARM_CP_IO,
6498 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6499 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6500 .nv2_redirect_offset = 0x78,
6501 .writefn = hcr_write, .raw_writefn = raw_write },
6502 { .name = "HCR", .state = ARM_CP_STATE_AA32,
6503 .type = ARM_CP_ALIAS | ARM_CP_IO,
6504 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6505 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6506 .writefn = hcr_writelow },
6507 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
6508 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
6509 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6510 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
6511 .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
6512 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
6513 .access = PL2_RW,
6514 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
6515 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
6516 .type = ARM_CP_NV2_REDIRECT,
6517 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
6518 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
6519 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
6520 .type = ARM_CP_NV2_REDIRECT,
6521 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
6522 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
6523 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
6524 .type = ARM_CP_ALIAS,
6525 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
6526 .access = PL2_RW,
6527 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
6528 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
6529 .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
6530 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
6531 .access = PL2_RW,
6532 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
6533 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
6534 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
6535 .access = PL2_RW, .writefn = vbar_write,
6536 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
6537 .resetvalue = 0 },
6538 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
6539 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
6540 .access = PL3_RW, .type = ARM_CP_ALIAS,
6541 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
6542 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
6543 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
6544 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
6545 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
6546 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
6547 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
6548 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
6549 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
6550 .resetvalue = 0 },
6551 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
6552 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
6553 .access = PL2_RW, .type = ARM_CP_ALIAS,
6554 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
6555 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
6556 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
6557 .access = PL2_RW, .type = ARM_CP_CONST,
6558 .resetvalue = 0 },
6559 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
6560 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
6561 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
6562 .access = PL2_RW, .type = ARM_CP_CONST,
6563 .resetvalue = 0 },
6564 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
6565 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
6566 .access = PL2_RW, .type = ARM_CP_CONST,
6567 .resetvalue = 0 },
6568 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
6569 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
6570 .access = PL2_RW, .type = ARM_CP_CONST,
6571 .resetvalue = 0 },
6572 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
6573 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6574 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
6575 .raw_writefn = raw_write,
6576 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
6577 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
6578 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6579 .type = ARM_CP_ALIAS,
6580 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6581 .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
6582 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
6583 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6584 .access = PL2_RW,
6585 .nv2_redirect_offset = 0x40,
6586 /* no .writefn needed as this can't cause an ASID change */
6587 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
6588 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
6589 .cp = 15, .opc1 = 6, .crm = 2,
6590 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6591 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6592 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
6593 .writefn = vttbr_write, .raw_writefn = raw_write },
6594 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
6595 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
6596 .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
6597 .nv2_redirect_offset = 0x20,
6598 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
6599 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
6600 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
6601 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
6602 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
6603 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6604 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
6605 .access = PL2_RW, .resetvalue = 0,
6606 .nv2_redirect_offset = 0x90,
6607 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
6608 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
6609 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
6610 .access = PL2_RW, .resetvalue = 0,
6611 .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
6612 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6613 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
6614 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6615 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6616 { .name = "TLBIALLNSNH",
6617 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
6618 .type = ARM_CP_NO_RAW, .access = PL2_W,
6619 .writefn = tlbiall_nsnh_write },
6620 { .name = "TLBIALLNSNHIS",
6621 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
6622 .type = ARM_CP_NO_RAW, .access = PL2_W,
6623 .writefn = tlbiall_nsnh_is_write },
6624 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6625 .type = ARM_CP_NO_RAW, .access = PL2_W,
6626 .writefn = tlbiall_hyp_write },
6627 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6628 .type = ARM_CP_NO_RAW, .access = PL2_W,
6629 .writefn = tlbiall_hyp_is_write },
6630 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6631 .type = ARM_CP_NO_RAW, .access = PL2_W,
6632 .writefn = tlbimva_hyp_write },
6633 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6634 .type = ARM_CP_NO_RAW, .access = PL2_W,
6635 .writefn = tlbimva_hyp_is_write },
6636 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
6637 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6638 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6639 .writefn = tlbi_aa64_alle2_write },
6640 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
6641 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6642 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6643 .writefn = tlbi_aa64_vae2_write },
6644 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
6645 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
6646 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6647 .writefn = tlbi_aa64_vae2_write },
6648 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
6649 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6650 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6651 .writefn = tlbi_aa64_alle2is_write },
6652 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
6653 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6654 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6655 .writefn = tlbi_aa64_vae2is_write },
6656 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
6657 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
6658 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6659 .writefn = tlbi_aa64_vae2is_write },
6660 #ifndef CONFIG_USER_ONLY
6661 /*
6662 * Unlike the other EL2-related AT operations, these must
6663 * UNDEF from EL3 if EL2 is not implemented, which is why we
6664 * define them here rather than with the rest of the AT ops.
6665 */
6666 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
6667 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6668 .access = PL2_W, .accessfn = at_s1e2_access,
6669 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6670 .writefn = ats_write64 },
6671 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
6672 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6673 .access = PL2_W, .accessfn = at_s1e2_access,
6674 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6675 .writefn = ats_write64 },
6676 /*
6677 * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
6678 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
6679 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
6680 * to behave as if SCR.NS was 1.
6681 */
6682 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6683 .access = PL2_W,
6684 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6685 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6686 .access = PL2_W,
6687 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6688 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6689 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
6690 /*
6691 * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
6692 * reset values as IMPDEF. We choose to reset to 3 to comply with
6693 * both ARMv7 and ARMv8.
6694 */
6695 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
6696 .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
6697 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
6698 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6699 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6700 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6701 .writefn = gt_cntvoff_write,
6702 .nv2_redirect_offset = 0x60,
6703 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6704 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6705 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6706 .writefn = gt_cntvoff_write,
6707 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6708 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6709 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6710 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6711 .type = ARM_CP_IO, .access = PL2_RW,
6712 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6713 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6714 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6715 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6716 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6717 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6718 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
6719 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6720 .resetfn = gt_hyp_timer_reset,
6721 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6722 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6723 .type = ARM_CP_IO,
6724 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6725 .access = PL2_RW,
6726 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6727 .resetvalue = 0,
6728 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
6729 #endif
6730 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6731 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6732 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6733 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6734 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6735 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6736 .access = PL2_RW,
6737 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6738 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6739 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6740 .access = PL2_RW,
6741 .nv2_redirect_offset = 0x80,
6742 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
6743 };
6744
6745 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6746 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
6747 .type = ARM_CP_ALIAS | ARM_CP_IO,
6748 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6749 .access = PL2_RW,
6750 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6751 .writefn = hcr_writehigh },
6752 };
6753
sel2_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6754 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6755 bool isread)
6756 {
6757 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6758 return CP_ACCESS_OK;
6759 }
6760 return CP_ACCESS_TRAP_UNCATEGORIZED;
6761 }
6762
6763 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6764 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6765 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6766 .access = PL2_RW, .accessfn = sel2_access,
6767 .nv2_redirect_offset = 0x30,
6768 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6769 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6770 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6771 .access = PL2_RW, .accessfn = sel2_access,
6772 .nv2_redirect_offset = 0x48,
6773 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
6774 #ifndef CONFIG_USER_ONLY
6775 /* Secure EL2 Physical Timer */
6776 { .name = "CNTHPS_TVAL_EL2", .state = ARM_CP_STATE_AA64,
6777 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 0,
6778 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6779 .accessfn = gt_sel2timer_access,
6780 .readfn = gt_sec_pel2_tval_read,
6781 .writefn = gt_sec_pel2_tval_write,
6782 .resetfn = gt_sec_pel2_timer_reset,
6783 },
6784 { .name = "CNTHPS_CTL_EL2", .state = ARM_CP_STATE_AA64,
6785 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 1,
6786 .type = ARM_CP_IO, .access = PL2_RW,
6787 .accessfn = gt_sel2timer_access,
6788 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_PHYS].ctl),
6789 .resetvalue = 0,
6790 .writefn = gt_sec_pel2_ctl_write, .raw_writefn = raw_write,
6791 },
6792 { .name = "CNTHPS_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6793 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 2,
6794 .type = ARM_CP_IO, .access = PL2_RW,
6795 .accessfn = gt_sel2timer_access,
6796 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_PHYS].cval),
6797 .writefn = gt_sec_pel2_cval_write, .raw_writefn = raw_write,
6798 },
6799 /* Secure EL2 Virtual Timer */
6800 { .name = "CNTHVS_TVAL_EL2", .state = ARM_CP_STATE_AA64,
6801 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 0,
6802 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6803 .accessfn = gt_sel2timer_access,
6804 .readfn = gt_sec_vel2_tval_read,
6805 .writefn = gt_sec_vel2_tval_write,
6806 .resetfn = gt_sec_vel2_timer_reset,
6807 },
6808 { .name = "CNTHVS_CTL_EL2", .state = ARM_CP_STATE_AA64,
6809 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 1,
6810 .type = ARM_CP_IO, .access = PL2_RW,
6811 .accessfn = gt_sel2timer_access,
6812 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_VIRT].ctl),
6813 .resetvalue = 0,
6814 .writefn = gt_sec_vel2_ctl_write, .raw_writefn = raw_write,
6815 },
6816 { .name = "CNTHVS_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6817 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 2,
6818 .type = ARM_CP_IO, .access = PL2_RW,
6819 .accessfn = gt_sel2timer_access,
6820 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_VIRT].cval),
6821 .writefn = gt_sec_vel2_cval_write, .raw_writefn = raw_write,
6822 },
6823 #endif
6824 };
6825
nsacr_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6826 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6827 bool isread)
6828 {
6829 /*
6830 * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
6831 * At Secure EL1 it traps to EL3 or EL2.
6832 */
6833 if (arm_current_el(env) == 3) {
6834 return CP_ACCESS_OK;
6835 }
6836 if (arm_is_secure_below_el3(env)) {
6837 if (env->cp15.scr_el3 & SCR_EEL2) {
6838 return CP_ACCESS_TRAP_EL2;
6839 }
6840 return CP_ACCESS_TRAP_EL3;
6841 }
6842 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6843 if (isread) {
6844 return CP_ACCESS_OK;
6845 }
6846 return CP_ACCESS_TRAP_UNCATEGORIZED;
6847 }
6848
6849 static const ARMCPRegInfo el3_cp_reginfo[] = {
6850 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6851 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6852 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
6853 .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
6854 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
6855 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
6856 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6857 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
6858 .writefn = scr_write, .raw_writefn = raw_write },
6859 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6860 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6861 .access = PL3_RW, .resetvalue = 0,
6862 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6863 { .name = "SDER",
6864 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6865 .access = PL3_RW, .resetvalue = 0,
6866 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
6867 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
6868 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6869 .writefn = vbar_write, .resetvalue = 0,
6870 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
6871 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6872 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
6873 .access = PL3_RW, .resetvalue = 0,
6874 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
6875 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6876 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6877 .access = PL3_RW,
6878 /* no .writefn needed as this can't cause an ASID change */
6879 .resetvalue = 0,
6880 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
6881 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
6882 .type = ARM_CP_ALIAS,
6883 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6884 .access = PL3_RW,
6885 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
6886 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
6887 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6888 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
6889 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6890 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6891 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
6892 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
6893 .type = ARM_CP_ALIAS,
6894 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
6895 .access = PL3_RW,
6896 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
6897 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6898 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6899 .access = PL3_RW, .writefn = vbar_write,
6900 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6901 .resetvalue = 0 },
6902 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6903 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6904 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6905 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
6906 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6907 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6908 .access = PL3_RW, .resetvalue = 0,
6909 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
6910 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6911 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6912 .access = PL3_RW, .type = ARM_CP_CONST,
6913 .resetvalue = 0 },
6914 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6915 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6916 .access = PL3_RW, .type = ARM_CP_CONST,
6917 .resetvalue = 0 },
6918 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6919 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6920 .access = PL3_RW, .type = ARM_CP_CONST,
6921 .resetvalue = 0 },
6922 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6923 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6924 .access = PL3_W, .type = ARM_CP_NO_RAW,
6925 .writefn = tlbi_aa64_alle3is_write },
6926 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6927 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6928 .access = PL3_W, .type = ARM_CP_NO_RAW,
6929 .writefn = tlbi_aa64_vae3is_write },
6930 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6931 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6932 .access = PL3_W, .type = ARM_CP_NO_RAW,
6933 .writefn = tlbi_aa64_vae3is_write },
6934 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6935 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6936 .access = PL3_W, .type = ARM_CP_NO_RAW,
6937 .writefn = tlbi_aa64_alle3_write },
6938 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6939 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6940 .access = PL3_W, .type = ARM_CP_NO_RAW,
6941 .writefn = tlbi_aa64_vae3_write },
6942 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6943 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6944 .access = PL3_W, .type = ARM_CP_NO_RAW,
6945 .writefn = tlbi_aa64_vae3_write },
6946 };
6947
6948 #ifndef CONFIG_USER_ONLY
6949
e2h_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6950 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
6951 bool isread)
6952 {
6953 if (arm_current_el(env) == 1) {
6954 /* This must be a FEAT_NV access */
6955 return CP_ACCESS_OK;
6956 }
6957 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6958 return CP_ACCESS_TRAP_UNCATEGORIZED;
6959 }
6960 return CP_ACCESS_OK;
6961 }
6962
access_el1nvpct(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6963 static CPAccessResult access_el1nvpct(CPUARMState *env, const ARMCPRegInfo *ri,
6964 bool isread)
6965 {
6966 if (arm_current_el(env) == 1) {
6967 /* This must be a FEAT_NV access with NVx == 101 */
6968 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVPCT)) {
6969 return CP_ACCESS_TRAP_EL2;
6970 }
6971 }
6972 return e2h_access(env, ri, isread);
6973 }
6974
access_el1nvvct(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6975 static CPAccessResult access_el1nvvct(CPUARMState *env, const ARMCPRegInfo *ri,
6976 bool isread)
6977 {
6978 if (arm_current_el(env) == 1) {
6979 /* This must be a FEAT_NV access with NVx == 101 */
6980 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVVCT)) {
6981 return CP_ACCESS_TRAP_EL2;
6982 }
6983 }
6984 return e2h_access(env, ri, isread);
6985 }
6986
6987 /* Test if system register redirection is to occur in the current state. */
redirect_for_e2h(CPUARMState * env)6988 static bool redirect_for_e2h(CPUARMState *env)
6989 {
6990 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6991 }
6992
el2_e2h_read(CPUARMState * env,const ARMCPRegInfo * ri)6993 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6994 {
6995 CPReadFn *readfn;
6996
6997 if (redirect_for_e2h(env)) {
6998 /* Switch to the saved EL2 version of the register. */
6999 ri = ri->opaque;
7000 readfn = ri->readfn;
7001 } else {
7002 readfn = ri->orig_readfn;
7003 }
7004 if (readfn == NULL) {
7005 readfn = raw_read;
7006 }
7007 return readfn(env, ri);
7008 }
7009
el2_e2h_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7010 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
7011 uint64_t value)
7012 {
7013 CPWriteFn *writefn;
7014
7015 if (redirect_for_e2h(env)) {
7016 /* Switch to the saved EL2 version of the register. */
7017 ri = ri->opaque;
7018 writefn = ri->writefn;
7019 } else {
7020 writefn = ri->orig_writefn;
7021 }
7022 if (writefn == NULL) {
7023 writefn = raw_write;
7024 }
7025 writefn(env, ri, value);
7026 }
7027
el2_e2h_e12_read(CPUARMState * env,const ARMCPRegInfo * ri)7028 static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri)
7029 {
7030 /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
7031 return ri->orig_readfn(env, ri->opaque);
7032 }
7033
el2_e2h_e12_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7034 static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri,
7035 uint64_t value)
7036 {
7037 /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
7038 return ri->orig_writefn(env, ri->opaque, value);
7039 }
7040
el2_e2h_e12_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7041 static CPAccessResult el2_e2h_e12_access(CPUARMState *env,
7042 const ARMCPRegInfo *ri,
7043 bool isread)
7044 {
7045 if (arm_current_el(env) == 1) {
7046 /*
7047 * This must be a FEAT_NV access (will either trap or redirect
7048 * to memory). None of the registers with _EL12 aliases want to
7049 * apply their trap controls for this kind of access, so don't
7050 * call the orig_accessfn or do the "UNDEF when E2H is 0" check.
7051 */
7052 return CP_ACCESS_OK;
7053 }
7054 /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */
7055 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
7056 return CP_ACCESS_TRAP_UNCATEGORIZED;
7057 }
7058 if (ri->orig_accessfn) {
7059 return ri->orig_accessfn(env, ri->opaque, isread);
7060 }
7061 return CP_ACCESS_OK;
7062 }
7063
define_arm_vh_e2h_redirects_aliases(ARMCPU * cpu)7064 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
7065 {
7066 struct E2HAlias {
7067 uint32_t src_key, dst_key, new_key;
7068 const char *src_name, *dst_name, *new_name;
7069 bool (*feature)(const ARMISARegisters *id);
7070 };
7071
7072 #define K(op0, op1, crn, crm, op2) \
7073 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
7074
7075 static const struct E2HAlias aliases[] = {
7076 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
7077 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
7078 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
7079 "CPACR", "CPTR_EL2", "CPACR_EL12" },
7080 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
7081 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
7082 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
7083 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
7084 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
7085 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
7086 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
7087 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
7088 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
7089 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
7090 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
7091 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
7092 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
7093 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
7094 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
7095 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
7096 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
7097 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
7098 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
7099 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
7100 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
7101 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
7102 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
7103 "VBAR", "VBAR_EL2", "VBAR_EL12" },
7104 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
7105 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
7106 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
7107 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
7108
7109 /*
7110 * Note that redirection of ZCR is mentioned in the description
7111 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
7112 * not in the summary table.
7113 */
7114 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
7115 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
7116 { K(3, 0, 1, 2, 6), K(3, 4, 1, 2, 6), K(3, 5, 1, 2, 6),
7117 "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
7118
7119 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
7120 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
7121
7122 { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
7123 "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
7124 isar_feature_aa64_scxtnum },
7125
7126 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
7127 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
7128 };
7129 #undef K
7130
7131 size_t i;
7132
7133 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
7134 const struct E2HAlias *a = &aliases[i];
7135 ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
7136 bool ok;
7137
7138 if (a->feature && !a->feature(&cpu->isar)) {
7139 continue;
7140 }
7141
7142 src_reg = g_hash_table_lookup(cpu->cp_regs,
7143 (gpointer)(uintptr_t)a->src_key);
7144 dst_reg = g_hash_table_lookup(cpu->cp_regs,
7145 (gpointer)(uintptr_t)a->dst_key);
7146 g_assert(src_reg != NULL);
7147 g_assert(dst_reg != NULL);
7148
7149 /* Cross-compare names to detect typos in the keys. */
7150 g_assert(strcmp(src_reg->name, a->src_name) == 0);
7151 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
7152
7153 /* None of the core system registers use opaque; we will. */
7154 g_assert(src_reg->opaque == NULL);
7155
7156 /* Create alias before redirection so we dup the right data. */
7157 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
7158
7159 new_reg->name = a->new_name;
7160 new_reg->type |= ARM_CP_ALIAS;
7161 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
7162 new_reg->access &= PL2_RW | PL3_RW;
7163 /* The new_reg op fields are as per new_key, not the target reg */
7164 new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK)
7165 >> CP_REG_ARM64_SYSREG_CRN_SHIFT;
7166 new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK)
7167 >> CP_REG_ARM64_SYSREG_CRM_SHIFT;
7168 new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK)
7169 >> CP_REG_ARM64_SYSREG_OP0_SHIFT;
7170 new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK)
7171 >> CP_REG_ARM64_SYSREG_OP1_SHIFT;
7172 new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK)
7173 >> CP_REG_ARM64_SYSREG_OP2_SHIFT;
7174 new_reg->opaque = src_reg;
7175 new_reg->orig_readfn = src_reg->readfn ?: raw_read;
7176 new_reg->orig_writefn = src_reg->writefn ?: raw_write;
7177 new_reg->orig_accessfn = src_reg->accessfn;
7178 if (!new_reg->raw_readfn) {
7179 new_reg->raw_readfn = raw_read;
7180 }
7181 if (!new_reg->raw_writefn) {
7182 new_reg->raw_writefn = raw_write;
7183 }
7184 new_reg->readfn = el2_e2h_e12_read;
7185 new_reg->writefn = el2_e2h_e12_write;
7186 new_reg->accessfn = el2_e2h_e12_access;
7187
7188 /*
7189 * If the _EL1 register is redirected to memory by FEAT_NV2,
7190 * then it shares the offset with the _EL12 register,
7191 * and which one is redirected depends on HCR_EL2.NV1.
7192 */
7193 if (new_reg->nv2_redirect_offset) {
7194 assert(new_reg->nv2_redirect_offset & NV2_REDIR_NV1);
7195 new_reg->nv2_redirect_offset &= ~NV2_REDIR_NV1;
7196 new_reg->nv2_redirect_offset |= NV2_REDIR_NO_NV1;
7197 }
7198
7199 ok = g_hash_table_insert(cpu->cp_regs,
7200 (gpointer)(uintptr_t)a->new_key, new_reg);
7201 g_assert(ok);
7202
7203 src_reg->opaque = dst_reg;
7204 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
7205 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
7206 if (!src_reg->raw_readfn) {
7207 src_reg->raw_readfn = raw_read;
7208 }
7209 if (!src_reg->raw_writefn) {
7210 src_reg->raw_writefn = raw_write;
7211 }
7212 src_reg->readfn = el2_e2h_read;
7213 src_reg->writefn = el2_e2h_write;
7214 }
7215 }
7216 #endif
7217
ctr_el0_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7218 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
7219 bool isread)
7220 {
7221 int cur_el = arm_current_el(env);
7222
7223 if (cur_el < 2) {
7224 uint64_t hcr = arm_hcr_el2_eff(env);
7225
7226 if (cur_el == 0) {
7227 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
7228 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
7229 return CP_ACCESS_TRAP_EL2;
7230 }
7231 } else {
7232 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
7233 return CP_ACCESS_TRAP;
7234 }
7235 if (hcr & HCR_TID2) {
7236 return CP_ACCESS_TRAP_EL2;
7237 }
7238 }
7239 } else if (hcr & HCR_TID2) {
7240 return CP_ACCESS_TRAP_EL2;
7241 }
7242 }
7243
7244 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
7245 return CP_ACCESS_TRAP_EL2;
7246 }
7247
7248 return CP_ACCESS_OK;
7249 }
7250
7251 /*
7252 * Check for traps to RAS registers, which are controlled
7253 * by HCR_EL2.TERR and SCR_EL3.TERR.
7254 */
access_terr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7255 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
7256 bool isread)
7257 {
7258 int el = arm_current_el(env);
7259
7260 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
7261 return CP_ACCESS_TRAP_EL2;
7262 }
7263 if (!arm_is_el3_or_mon(env) && (env->cp15.scr_el3 & SCR_TERR)) {
7264 return CP_ACCESS_TRAP_EL3;
7265 }
7266 return CP_ACCESS_OK;
7267 }
7268
disr_read(CPUARMState * env,const ARMCPRegInfo * ri)7269 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
7270 {
7271 int el = arm_current_el(env);
7272
7273 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
7274 return env->cp15.vdisr_el2;
7275 }
7276 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
7277 return 0; /* RAZ/WI */
7278 }
7279 return env->cp15.disr_el1;
7280 }
7281
disr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t val)7282 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7283 {
7284 int el = arm_current_el(env);
7285
7286 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
7287 env->cp15.vdisr_el2 = val;
7288 return;
7289 }
7290 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
7291 return; /* RAZ/WI */
7292 }
7293 env->cp15.disr_el1 = val;
7294 }
7295
7296 /*
7297 * Minimal RAS implementation with no Error Records.
7298 * Which means that all of the Error Record registers:
7299 * ERXADDR_EL1
7300 * ERXCTLR_EL1
7301 * ERXFR_EL1
7302 * ERXMISC0_EL1
7303 * ERXMISC1_EL1
7304 * ERXMISC2_EL1
7305 * ERXMISC3_EL1
7306 * ERXPFGCDN_EL1 (RASv1p1)
7307 * ERXPFGCTL_EL1 (RASv1p1)
7308 * ERXPFGF_EL1 (RASv1p1)
7309 * ERXSTATUS_EL1
7310 * and
7311 * ERRSELR_EL1
7312 * may generate UNDEFINED, which is the effect we get by not
7313 * listing them at all.
7314 *
7315 * These registers have fine-grained trap bits, but UNDEF-to-EL1
7316 * is higher priority than FGT-to-EL2 so we do not need to list them
7317 * in order to check for an FGT.
7318 */
7319 static const ARMCPRegInfo minimal_ras_reginfo[] = {
7320 { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
7321 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
7322 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
7323 .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
7324 { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
7325 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
7326 .access = PL1_R, .accessfn = access_terr,
7327 .fgt = FGT_ERRIDR_EL1,
7328 .type = ARM_CP_CONST, .resetvalue = 0 },
7329 { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
7330 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
7331 .nv2_redirect_offset = 0x500,
7332 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
7333 { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
7334 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
7335 .nv2_redirect_offset = 0x508,
7336 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
7337 };
7338
7339 /*
7340 * Return the exception level to which exceptions should be taken
7341 * via SVEAccessTrap. This excludes the check for whether the exception
7342 * should be routed through AArch64.AdvSIMDFPAccessTrap. That can easily
7343 * be found by testing 0 < fp_exception_el < sve_exception_el.
7344 *
7345 * C.f. the ARM pseudocode function CheckSVEEnabled. Note that the
7346 * pseudocode does *not* separate out the FP trap checks, but has them
7347 * all in one function.
7348 */
sve_exception_el(CPUARMState * env,int el)7349 int sve_exception_el(CPUARMState *env, int el)
7350 {
7351 #ifndef CONFIG_USER_ONLY
7352 if (el <= 1 && !el_is_in_host(env, el)) {
7353 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
7354 case 1:
7355 if (el != 0) {
7356 break;
7357 }
7358 /* fall through */
7359 case 0:
7360 case 2:
7361 return 1;
7362 }
7363 }
7364
7365 if (el <= 2 && arm_is_el2_enabled(env)) {
7366 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
7367 if (env->cp15.hcr_el2 & HCR_E2H) {
7368 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
7369 case 1:
7370 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
7371 break;
7372 }
7373 /* fall through */
7374 case 0:
7375 case 2:
7376 return 2;
7377 }
7378 } else {
7379 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
7380 return 2;
7381 }
7382 }
7383 }
7384
7385 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
7386 if (arm_feature(env, ARM_FEATURE_EL3)
7387 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
7388 return 3;
7389 }
7390 #endif
7391 return 0;
7392 }
7393
7394 /*
7395 * Return the exception level to which exceptions should be taken for SME.
7396 * C.f. the ARM pseudocode function CheckSMEAccess.
7397 */
sme_exception_el(CPUARMState * env,int el)7398 int sme_exception_el(CPUARMState *env, int el)
7399 {
7400 #ifndef CONFIG_USER_ONLY
7401 if (el <= 1 && !el_is_in_host(env, el)) {
7402 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
7403 case 1:
7404 if (el != 0) {
7405 break;
7406 }
7407 /* fall through */
7408 case 0:
7409 case 2:
7410 return 1;
7411 }
7412 }
7413
7414 if (el <= 2 && arm_is_el2_enabled(env)) {
7415 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
7416 if (env->cp15.hcr_el2 & HCR_E2H) {
7417 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
7418 case 1:
7419 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
7420 break;
7421 }
7422 /* fall through */
7423 case 0:
7424 case 2:
7425 return 2;
7426 }
7427 } else {
7428 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
7429 return 2;
7430 }
7431 }
7432 }
7433
7434 /* CPTR_EL3. Since ESM is negative we must check for EL3. */
7435 if (arm_feature(env, ARM_FEATURE_EL3)
7436 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7437 return 3;
7438 }
7439 #endif
7440 return 0;
7441 }
7442
7443 /*
7444 * Given that SVE is enabled, return the vector length for EL.
7445 */
sve_vqm1_for_el_sm(CPUARMState * env,int el,bool sm)7446 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
7447 {
7448 ARMCPU *cpu = env_archcpu(env);
7449 uint64_t *cr = env->vfp.zcr_el;
7450 uint32_t map = cpu->sve_vq.map;
7451 uint32_t len = ARM_MAX_VQ - 1;
7452
7453 if (sm) {
7454 cr = env->vfp.smcr_el;
7455 map = cpu->sme_vq.map;
7456 }
7457
7458 if (el <= 1 && !el_is_in_host(env, el)) {
7459 len = MIN(len, 0xf & (uint32_t)cr[1]);
7460 }
7461 if (el <= 2 && arm_is_el2_enabled(env)) {
7462 len = MIN(len, 0xf & (uint32_t)cr[2]);
7463 }
7464 if (arm_feature(env, ARM_FEATURE_EL3)) {
7465 len = MIN(len, 0xf & (uint32_t)cr[3]);
7466 }
7467
7468 map &= MAKE_64BIT_MASK(0, len + 1);
7469 if (map != 0) {
7470 return 31 - clz32(map);
7471 }
7472
7473 /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
7474 assert(sm);
7475 return ctz32(cpu->sme_vq.map);
7476 }
7477
sve_vqm1_for_el(CPUARMState * env,int el)7478 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
7479 {
7480 return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
7481 }
7482
zcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7483 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7484 uint64_t value)
7485 {
7486 int cur_el = arm_current_el(env);
7487 int old_len = sve_vqm1_for_el(env, cur_el);
7488 int new_len;
7489
7490 /* Bits other than [3:0] are RAZ/WI. */
7491 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
7492 raw_write(env, ri, value & 0xf);
7493
7494 /*
7495 * Because we arrived here, we know both FP and SVE are enabled;
7496 * otherwise we would have trapped access to the ZCR_ELn register.
7497 */
7498 new_len = sve_vqm1_for_el(env, cur_el);
7499 if (new_len < old_len) {
7500 aarch64_sve_narrow_vq(env, new_len + 1);
7501 }
7502 }
7503
7504 static const ARMCPRegInfo zcr_reginfo[] = {
7505 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
7506 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
7507 .nv2_redirect_offset = 0x1e0 | NV2_REDIR_NV1,
7508 .access = PL1_RW, .type = ARM_CP_SVE,
7509 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
7510 .writefn = zcr_write, .raw_writefn = raw_write },
7511 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
7512 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
7513 .access = PL2_RW, .type = ARM_CP_SVE,
7514 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
7515 .writefn = zcr_write, .raw_writefn = raw_write },
7516 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
7517 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
7518 .access = PL3_RW, .type = ARM_CP_SVE,
7519 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
7520 .writefn = zcr_write, .raw_writefn = raw_write },
7521 };
7522
7523 #ifdef TARGET_AARCH64
access_tpidr2(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7524 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
7525 bool isread)
7526 {
7527 int el = arm_current_el(env);
7528
7529 if (el == 0) {
7530 uint64_t sctlr = arm_sctlr(env, el);
7531 if (!(sctlr & SCTLR_EnTP2)) {
7532 return CP_ACCESS_TRAP;
7533 }
7534 }
7535 /* TODO: FEAT_FGT */
7536 if (el < 3
7537 && arm_feature(env, ARM_FEATURE_EL3)
7538 && !(env->cp15.scr_el3 & SCR_ENTP2)) {
7539 return CP_ACCESS_TRAP_EL3;
7540 }
7541 return CP_ACCESS_OK;
7542 }
7543
access_smprimap(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7544 static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri,
7545 bool isread)
7546 {
7547 /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */
7548 if (arm_current_el(env) == 2
7549 && arm_feature(env, ARM_FEATURE_EL3)
7550 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7551 return CP_ACCESS_TRAP_EL3;
7552 }
7553 return CP_ACCESS_OK;
7554 }
7555
access_smpri(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7556 static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri,
7557 bool isread)
7558 {
7559 if (arm_current_el(env) < 3
7560 && arm_feature(env, ARM_FEATURE_EL3)
7561 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7562 return CP_ACCESS_TRAP_EL3;
7563 }
7564 return CP_ACCESS_OK;
7565 }
7566
7567 /* ResetSVEState */
arm_reset_sve_state(CPUARMState * env)7568 static void arm_reset_sve_state(CPUARMState *env)
7569 {
7570 memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
7571 /* Recall that FFR is stored as pregs[16]. */
7572 memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
7573 vfp_set_fpsr(env, 0x0800009f);
7574 }
7575
aarch64_set_svcr(CPUARMState * env,uint64_t new,uint64_t mask)7576 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
7577 {
7578 uint64_t change = (env->svcr ^ new) & mask;
7579
7580 if (change == 0) {
7581 return;
7582 }
7583 env->svcr ^= change;
7584
7585 if (change & R_SVCR_SM_MASK) {
7586 arm_reset_sve_state(env);
7587 }
7588
7589 /*
7590 * ResetSMEState.
7591 *
7592 * SetPSTATE_ZA zeros on enable and disable. We can zero this only
7593 * on enable: while disabled, the storage is inaccessible and the
7594 * value does not matter. We're not saving the storage in vmstate
7595 * when disabled either.
7596 */
7597 if (change & new & R_SVCR_ZA_MASK) {
7598 memset(env->zarray, 0, sizeof(env->zarray));
7599 }
7600
7601 if (tcg_enabled()) {
7602 arm_rebuild_hflags(env);
7603 }
7604 }
7605
svcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7606 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7607 uint64_t value)
7608 {
7609 aarch64_set_svcr(env, value, -1);
7610 }
7611
smcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7612 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7613 uint64_t value)
7614 {
7615 int cur_el = arm_current_el(env);
7616 int old_len = sve_vqm1_for_el(env, cur_el);
7617 int new_len;
7618
7619 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
7620 value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
7621 raw_write(env, ri, value);
7622
7623 /*
7624 * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
7625 * when SVL is widened (old values kept, or zeros). Choose to keep the
7626 * current values for simplicity. But for QEMU internals, we must still
7627 * apply the narrower SVL to the Zregs and Pregs -- see the comment
7628 * above aarch64_sve_narrow_vq.
7629 */
7630 new_len = sve_vqm1_for_el(env, cur_el);
7631 if (new_len < old_len) {
7632 aarch64_sve_narrow_vq(env, new_len + 1);
7633 }
7634 }
7635
7636 static const ARMCPRegInfo sme_reginfo[] = {
7637 { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
7638 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
7639 .access = PL0_RW, .accessfn = access_tpidr2,
7640 .fgt = FGT_NTPIDR2_EL0,
7641 .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
7642 { .name = "SVCR", .state = ARM_CP_STATE_AA64,
7643 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
7644 .access = PL0_RW, .type = ARM_CP_SME,
7645 .fieldoffset = offsetof(CPUARMState, svcr),
7646 .writefn = svcr_write, .raw_writefn = raw_write },
7647 { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
7648 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
7649 .nv2_redirect_offset = 0x1f0 | NV2_REDIR_NV1,
7650 .access = PL1_RW, .type = ARM_CP_SME,
7651 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
7652 .writefn = smcr_write, .raw_writefn = raw_write },
7653 { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
7654 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
7655 .access = PL2_RW, .type = ARM_CP_SME,
7656 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
7657 .writefn = smcr_write, .raw_writefn = raw_write },
7658 { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
7659 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
7660 .access = PL3_RW, .type = ARM_CP_SME,
7661 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
7662 .writefn = smcr_write, .raw_writefn = raw_write },
7663 { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
7664 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
7665 .access = PL1_R, .accessfn = access_aa64_tid1,
7666 /*
7667 * IMPLEMENTOR = 0 (software)
7668 * REVISION = 0 (implementation defined)
7669 * SMPS = 0 (no streaming execution priority in QEMU)
7670 * AFFINITY = 0 (streaming sve mode not shared with other PEs)
7671 */
7672 .type = ARM_CP_CONST, .resetvalue = 0, },
7673 /*
7674 * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
7675 */
7676 { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
7677 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
7678 .access = PL1_RW, .accessfn = access_smpri,
7679 .fgt = FGT_NSMPRI_EL1,
7680 .type = ARM_CP_CONST, .resetvalue = 0 },
7681 { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
7682 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
7683 .nv2_redirect_offset = 0x1f8,
7684 .access = PL2_RW, .accessfn = access_smprimap,
7685 .type = ARM_CP_CONST, .resetvalue = 0 },
7686 };
7687
tlbi_aa64_paall_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7688 static void tlbi_aa64_paall_write(CPUARMState *env, const ARMCPRegInfo *ri,
7689 uint64_t value)
7690 {
7691 CPUState *cs = env_cpu(env);
7692
7693 tlb_flush(cs);
7694 }
7695
gpccr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7696 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7697 uint64_t value)
7698 {
7699 /* L0GPTSZ is RO; other bits not mentioned are RES0. */
7700 uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
7701 R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
7702 R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
7703
7704 env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
7705 }
7706
gpccr_reset(CPUARMState * env,const ARMCPRegInfo * ri)7707 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
7708 {
7709 env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
7710 env_archcpu(env)->reset_l0gptsz);
7711 }
7712
tlbi_aa64_paallos_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7713 static void tlbi_aa64_paallos_write(CPUARMState *env, const ARMCPRegInfo *ri,
7714 uint64_t value)
7715 {
7716 CPUState *cs = env_cpu(env);
7717
7718 tlb_flush_all_cpus_synced(cs);
7719 }
7720
7721 static const ARMCPRegInfo rme_reginfo[] = {
7722 { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
7723 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
7724 .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
7725 .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
7726 { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
7727 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
7728 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
7729 { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
7730 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
7731 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
7732 { .name = "TLBI_PAALL", .state = ARM_CP_STATE_AA64,
7733 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 4,
7734 .access = PL3_W, .type = ARM_CP_NO_RAW,
7735 .writefn = tlbi_aa64_paall_write },
7736 { .name = "TLBI_PAALLOS", .state = ARM_CP_STATE_AA64,
7737 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 4,
7738 .access = PL3_W, .type = ARM_CP_NO_RAW,
7739 .writefn = tlbi_aa64_paallos_write },
7740 /*
7741 * QEMU does not have a way to invalidate by physical address, thus
7742 * invalidating a range of physical addresses is accomplished by
7743 * flushing all tlb entries in the outer shareable domain,
7744 * just like PAALLOS.
7745 */
7746 { .name = "TLBI_RPALOS", .state = ARM_CP_STATE_AA64,
7747 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 7,
7748 .access = PL3_W, .type = ARM_CP_NO_RAW,
7749 .writefn = tlbi_aa64_paallos_write },
7750 { .name = "TLBI_RPAOS", .state = ARM_CP_STATE_AA64,
7751 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 3,
7752 .access = PL3_W, .type = ARM_CP_NO_RAW,
7753 .writefn = tlbi_aa64_paallos_write },
7754 { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
7755 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
7756 .access = PL3_W, .type = ARM_CP_NOP },
7757 };
7758
7759 static const ARMCPRegInfo rme_mte_reginfo[] = {
7760 { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
7761 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
7762 .access = PL3_W, .type = ARM_CP_NOP },
7763 };
7764
aa64_allint_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7765 static void aa64_allint_write(CPUARMState *env, const ARMCPRegInfo *ri,
7766 uint64_t value)
7767 {
7768 env->pstate = (env->pstate & ~PSTATE_ALLINT) | (value & PSTATE_ALLINT);
7769 }
7770
aa64_allint_read(CPUARMState * env,const ARMCPRegInfo * ri)7771 static uint64_t aa64_allint_read(CPUARMState *env, const ARMCPRegInfo *ri)
7772 {
7773 return env->pstate & PSTATE_ALLINT;
7774 }
7775
aa64_allint_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7776 static CPAccessResult aa64_allint_access(CPUARMState *env,
7777 const ARMCPRegInfo *ri, bool isread)
7778 {
7779 if (!isread && arm_current_el(env) == 1 &&
7780 (arm_hcrx_el2_eff(env) & HCRX_TALLINT)) {
7781 return CP_ACCESS_TRAP_EL2;
7782 }
7783 return CP_ACCESS_OK;
7784 }
7785
7786 static const ARMCPRegInfo nmi_reginfo[] = {
7787 { .name = "ALLINT", .state = ARM_CP_STATE_AA64,
7788 .opc0 = 3, .opc1 = 0, .opc2 = 0, .crn = 4, .crm = 3,
7789 .type = ARM_CP_NO_RAW,
7790 .access = PL1_RW, .accessfn = aa64_allint_access,
7791 .fieldoffset = offsetof(CPUARMState, pstate),
7792 .writefn = aa64_allint_write, .readfn = aa64_allint_read,
7793 .resetfn = arm_cp_reset_ignore },
7794 };
7795 #endif /* TARGET_AARCH64 */
7796
define_pmu_regs(ARMCPU * cpu)7797 static void define_pmu_regs(ARMCPU *cpu)
7798 {
7799 /*
7800 * v7 performance monitor control register: same implementor
7801 * field as main ID register, and we implement four counters in
7802 * addition to the cycle count register.
7803 */
7804 unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
7805 ARMCPRegInfo pmcr = {
7806 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
7807 .access = PL0_RW,
7808 .fgt = FGT_PMCR_EL0,
7809 .type = ARM_CP_IO | ARM_CP_ALIAS,
7810 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
7811 .accessfn = pmreg_access,
7812 .readfn = pmcr_read, .raw_readfn = raw_read,
7813 .writefn = pmcr_write, .raw_writefn = raw_write,
7814 };
7815 ARMCPRegInfo pmcr64 = {
7816 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
7817 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
7818 .access = PL0_RW, .accessfn = pmreg_access,
7819 .fgt = FGT_PMCR_EL0,
7820 .type = ARM_CP_IO,
7821 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
7822 .resetvalue = cpu->isar.reset_pmcr_el0,
7823 .readfn = pmcr_read, .raw_readfn = raw_read,
7824 .writefn = pmcr_write, .raw_writefn = raw_write,
7825 };
7826
7827 define_one_arm_cp_reg(cpu, &pmcr);
7828 define_one_arm_cp_reg(cpu, &pmcr64);
7829 for (i = 0; i < pmcrn; i++) {
7830 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
7831 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
7832 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
7833 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
7834 ARMCPRegInfo pmev_regs[] = {
7835 { .name = pmevcntr_name, .cp = 15, .crn = 14,
7836 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7837 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7838 .fgt = FGT_PMEVCNTRN_EL0,
7839 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7840 .accessfn = pmreg_access_xevcntr },
7841 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
7842 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
7843 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
7844 .type = ARM_CP_IO,
7845 .fgt = FGT_PMEVCNTRN_EL0,
7846 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7847 .raw_readfn = pmevcntr_rawread,
7848 .raw_writefn = pmevcntr_rawwrite },
7849 { .name = pmevtyper_name, .cp = 15, .crn = 14,
7850 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7851 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7852 .fgt = FGT_PMEVTYPERN_EL0,
7853 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7854 .accessfn = pmreg_access },
7855 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
7856 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
7857 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
7858 .fgt = FGT_PMEVTYPERN_EL0,
7859 .type = ARM_CP_IO,
7860 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7861 .raw_writefn = pmevtyper_rawwrite },
7862 };
7863 define_arm_cp_regs(cpu, pmev_regs);
7864 g_free(pmevcntr_name);
7865 g_free(pmevcntr_el0_name);
7866 g_free(pmevtyper_name);
7867 g_free(pmevtyper_el0_name);
7868 }
7869 if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
7870 ARMCPRegInfo v81_pmu_regs[] = {
7871 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
7872 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
7873 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7874 .fgt = FGT_PMCEIDN_EL0,
7875 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
7876 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
7877 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
7878 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7879 .fgt = FGT_PMCEIDN_EL0,
7880 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
7881 };
7882 define_arm_cp_regs(cpu, v81_pmu_regs);
7883 }
7884 if (cpu_isar_feature(any_pmuv3p4, cpu)) {
7885 static const ARMCPRegInfo v84_pmmir = {
7886 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
7887 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
7888 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7889 .fgt = FGT_PMMIR_EL1,
7890 .resetvalue = 0
7891 };
7892 define_one_arm_cp_reg(cpu, &v84_pmmir);
7893 }
7894 }
7895
7896 #ifndef CONFIG_USER_ONLY
7897 /*
7898 * We don't know until after realize whether there's a GICv3
7899 * attached, and that is what registers the gicv3 sysregs.
7900 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
7901 * at runtime.
7902 */
id_pfr1_read(CPUARMState * env,const ARMCPRegInfo * ri)7903 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
7904 {
7905 ARMCPU *cpu = env_archcpu(env);
7906 uint64_t pfr1 = cpu->isar.id_pfr1;
7907
7908 if (env->gicv3state) {
7909 pfr1 |= 1 << 28;
7910 }
7911 return pfr1;
7912 }
7913
id_aa64pfr0_read(CPUARMState * env,const ARMCPRegInfo * ri)7914 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
7915 {
7916 ARMCPU *cpu = env_archcpu(env);
7917 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
7918
7919 if (env->gicv3state) {
7920 pfr0 |= 1 << 24;
7921 }
7922 return pfr0;
7923 }
7924 #endif
7925
7926 /*
7927 * Shared logic between LORID and the rest of the LOR* registers.
7928 * Secure state exclusion has already been dealt with.
7929 */
access_lor_ns(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7930 static CPAccessResult access_lor_ns(CPUARMState *env,
7931 const ARMCPRegInfo *ri, bool isread)
7932 {
7933 int el = arm_current_el(env);
7934
7935 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
7936 return CP_ACCESS_TRAP_EL2;
7937 }
7938 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
7939 return CP_ACCESS_TRAP_EL3;
7940 }
7941 return CP_ACCESS_OK;
7942 }
7943
access_lor_other(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7944 static CPAccessResult access_lor_other(CPUARMState *env,
7945 const ARMCPRegInfo *ri, bool isread)
7946 {
7947 if (arm_is_secure_below_el3(env)) {
7948 /* UNDEF if SCR_EL3.NS == 0 */
7949 return CP_ACCESS_TRAP_UNCATEGORIZED;
7950 }
7951 return access_lor_ns(env, ri, isread);
7952 }
7953
7954 /*
7955 * A trivial implementation of ARMv8.1-LOR leaves all of these
7956 * registers fixed at 0, which indicates that there are zero
7957 * supported Limited Ordering regions.
7958 */
7959 static const ARMCPRegInfo lor_reginfo[] = {
7960 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
7961 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
7962 .access = PL1_RW, .accessfn = access_lor_other,
7963 .fgt = FGT_LORSA_EL1,
7964 .type = ARM_CP_CONST, .resetvalue = 0 },
7965 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7966 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7967 .access = PL1_RW, .accessfn = access_lor_other,
7968 .fgt = FGT_LOREA_EL1,
7969 .type = ARM_CP_CONST, .resetvalue = 0 },
7970 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7971 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7972 .access = PL1_RW, .accessfn = access_lor_other,
7973 .fgt = FGT_LORN_EL1,
7974 .type = ARM_CP_CONST, .resetvalue = 0 },
7975 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7976 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7977 .access = PL1_RW, .accessfn = access_lor_other,
7978 .fgt = FGT_LORC_EL1,
7979 .type = ARM_CP_CONST, .resetvalue = 0 },
7980 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7981 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7982 .access = PL1_R, .accessfn = access_lor_ns,
7983 .fgt = FGT_LORID_EL1,
7984 .type = ARM_CP_CONST, .resetvalue = 0 },
7985 };
7986
7987 #ifdef TARGET_AARCH64
access_pauth(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7988 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7989 bool isread)
7990 {
7991 int el = arm_current_el(env);
7992
7993 if (el < 2 &&
7994 arm_is_el2_enabled(env) &&
7995 !(arm_hcr_el2_eff(env) & HCR_APK)) {
7996 return CP_ACCESS_TRAP_EL2;
7997 }
7998 if (el < 3 &&
7999 arm_feature(env, ARM_FEATURE_EL3) &&
8000 !(env->cp15.scr_el3 & SCR_APK)) {
8001 return CP_ACCESS_TRAP_EL3;
8002 }
8003 return CP_ACCESS_OK;
8004 }
8005
8006 static const ARMCPRegInfo pauth_reginfo[] = {
8007 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
8008 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
8009 .access = PL1_RW, .accessfn = access_pauth,
8010 .fgt = FGT_APDAKEY,
8011 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
8012 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
8013 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
8014 .access = PL1_RW, .accessfn = access_pauth,
8015 .fgt = FGT_APDAKEY,
8016 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
8017 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
8018 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
8019 .access = PL1_RW, .accessfn = access_pauth,
8020 .fgt = FGT_APDBKEY,
8021 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
8022 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
8023 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
8024 .access = PL1_RW, .accessfn = access_pauth,
8025 .fgt = FGT_APDBKEY,
8026 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
8027 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
8028 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
8029 .access = PL1_RW, .accessfn = access_pauth,
8030 .fgt = FGT_APGAKEY,
8031 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
8032 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
8033 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
8034 .access = PL1_RW, .accessfn = access_pauth,
8035 .fgt = FGT_APGAKEY,
8036 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
8037 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
8038 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
8039 .access = PL1_RW, .accessfn = access_pauth,
8040 .fgt = FGT_APIAKEY,
8041 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
8042 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
8043 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
8044 .access = PL1_RW, .accessfn = access_pauth,
8045 .fgt = FGT_APIAKEY,
8046 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
8047 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
8048 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
8049 .access = PL1_RW, .accessfn = access_pauth,
8050 .fgt = FGT_APIBKEY,
8051 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
8052 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
8053 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
8054 .access = PL1_RW, .accessfn = access_pauth,
8055 .fgt = FGT_APIBKEY,
8056 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
8057 };
8058
8059 static const ARMCPRegInfo tlbirange_reginfo[] = {
8060 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
8061 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
8062 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
8063 .fgt = FGT_TLBIRVAE1IS,
8064 .writefn = tlbi_aa64_rvae1is_write },
8065 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
8066 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
8067 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
8068 .fgt = FGT_TLBIRVAAE1IS,
8069 .writefn = tlbi_aa64_rvae1is_write },
8070 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
8071 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
8072 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
8073 .fgt = FGT_TLBIRVALE1IS,
8074 .writefn = tlbi_aa64_rvae1is_write },
8075 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
8076 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
8077 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
8078 .fgt = FGT_TLBIRVAALE1IS,
8079 .writefn = tlbi_aa64_rvae1is_write },
8080 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
8081 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
8082 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
8083 .fgt = FGT_TLBIRVAE1OS,
8084 .writefn = tlbi_aa64_rvae1is_write },
8085 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
8086 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
8087 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
8088 .fgt = FGT_TLBIRVAAE1OS,
8089 .writefn = tlbi_aa64_rvae1is_write },
8090 { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
8091 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
8092 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
8093 .fgt = FGT_TLBIRVALE1OS,
8094 .writefn = tlbi_aa64_rvae1is_write },
8095 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
8096 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
8097 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
8098 .fgt = FGT_TLBIRVAALE1OS,
8099 .writefn = tlbi_aa64_rvae1is_write },
8100 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
8101 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
8102 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
8103 .fgt = FGT_TLBIRVAE1,
8104 .writefn = tlbi_aa64_rvae1_write },
8105 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
8106 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
8107 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
8108 .fgt = FGT_TLBIRVAAE1,
8109 .writefn = tlbi_aa64_rvae1_write },
8110 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
8111 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
8112 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
8113 .fgt = FGT_TLBIRVALE1,
8114 .writefn = tlbi_aa64_rvae1_write },
8115 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
8116 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
8117 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
8118 .fgt = FGT_TLBIRVAALE1,
8119 .writefn = tlbi_aa64_rvae1_write },
8120 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
8121 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
8122 .access = PL2_W, .type = ARM_CP_NO_RAW,
8123 .writefn = tlbi_aa64_ripas2e1is_write },
8124 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
8125 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
8126 .access = PL2_W, .type = ARM_CP_NO_RAW,
8127 .writefn = tlbi_aa64_ripas2e1is_write },
8128 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
8129 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
8130 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
8131 .writefn = tlbi_aa64_rvae2is_write },
8132 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
8133 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
8134 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
8135 .writefn = tlbi_aa64_rvae2is_write },
8136 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
8137 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
8138 .access = PL2_W, .type = ARM_CP_NO_RAW,
8139 .writefn = tlbi_aa64_ripas2e1_write },
8140 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
8141 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
8142 .access = PL2_W, .type = ARM_CP_NO_RAW,
8143 .writefn = tlbi_aa64_ripas2e1_write },
8144 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
8145 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
8146 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
8147 .writefn = tlbi_aa64_rvae2is_write },
8148 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
8149 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
8150 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
8151 .writefn = tlbi_aa64_rvae2is_write },
8152 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
8153 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
8154 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
8155 .writefn = tlbi_aa64_rvae2_write },
8156 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
8157 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
8158 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
8159 .writefn = tlbi_aa64_rvae2_write },
8160 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
8161 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
8162 .access = PL3_W, .type = ARM_CP_NO_RAW,
8163 .writefn = tlbi_aa64_rvae3is_write },
8164 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
8165 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
8166 .access = PL3_W, .type = ARM_CP_NO_RAW,
8167 .writefn = tlbi_aa64_rvae3is_write },
8168 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
8169 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
8170 .access = PL3_W, .type = ARM_CP_NO_RAW,
8171 .writefn = tlbi_aa64_rvae3is_write },
8172 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
8173 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
8174 .access = PL3_W, .type = ARM_CP_NO_RAW,
8175 .writefn = tlbi_aa64_rvae3is_write },
8176 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
8177 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
8178 .access = PL3_W, .type = ARM_CP_NO_RAW,
8179 .writefn = tlbi_aa64_rvae3_write },
8180 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
8181 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
8182 .access = PL3_W, .type = ARM_CP_NO_RAW,
8183 .writefn = tlbi_aa64_rvae3_write },
8184 };
8185
8186 static const ARMCPRegInfo tlbios_reginfo[] = {
8187 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
8188 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
8189 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
8190 .fgt = FGT_TLBIVMALLE1OS,
8191 .writefn = tlbi_aa64_vmalle1is_write },
8192 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
8193 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
8194 .fgt = FGT_TLBIVAE1OS,
8195 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
8196 .writefn = tlbi_aa64_vae1is_write },
8197 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
8198 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
8199 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
8200 .fgt = FGT_TLBIASIDE1OS,
8201 .writefn = tlbi_aa64_vmalle1is_write },
8202 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
8203 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
8204 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
8205 .fgt = FGT_TLBIVAAE1OS,
8206 .writefn = tlbi_aa64_vae1is_write },
8207 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
8208 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
8209 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
8210 .fgt = FGT_TLBIVALE1OS,
8211 .writefn = tlbi_aa64_vae1is_write },
8212 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
8213 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
8214 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
8215 .fgt = FGT_TLBIVAALE1OS,
8216 .writefn = tlbi_aa64_vae1is_write },
8217 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
8218 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
8219 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
8220 .writefn = tlbi_aa64_alle2is_write },
8221 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
8222 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
8223 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
8224 .writefn = tlbi_aa64_vae2is_write },
8225 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
8226 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
8227 .access = PL2_W, .type = ARM_CP_NO_RAW,
8228 .writefn = tlbi_aa64_alle1is_write },
8229 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
8230 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
8231 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
8232 .writefn = tlbi_aa64_vae2is_write },
8233 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
8234 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
8235 .access = PL2_W, .type = ARM_CP_NO_RAW,
8236 .writefn = tlbi_aa64_alle1is_write },
8237 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
8238 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
8239 .access = PL2_W, .type = ARM_CP_NOP },
8240 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
8241 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
8242 .access = PL2_W, .type = ARM_CP_NOP },
8243 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
8244 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
8245 .access = PL2_W, .type = ARM_CP_NOP },
8246 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
8247 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
8248 .access = PL2_W, .type = ARM_CP_NOP },
8249 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
8250 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
8251 .access = PL3_W, .type = ARM_CP_NO_RAW,
8252 .writefn = tlbi_aa64_alle3is_write },
8253 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
8254 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
8255 .access = PL3_W, .type = ARM_CP_NO_RAW,
8256 .writefn = tlbi_aa64_vae3is_write },
8257 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
8258 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
8259 .access = PL3_W, .type = ARM_CP_NO_RAW,
8260 .writefn = tlbi_aa64_vae3is_write },
8261 };
8262
rndr_readfn(CPUARMState * env,const ARMCPRegInfo * ri)8263 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
8264 {
8265 Error *err = NULL;
8266 uint64_t ret;
8267
8268 /* Success sets NZCV = 0000. */
8269 env->NF = env->CF = env->VF = 0, env->ZF = 1;
8270
8271 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
8272 /*
8273 * ??? Failed, for unknown reasons in the crypto subsystem.
8274 * The best we can do is log the reason and return the
8275 * timed-out indication to the guest. There is no reason
8276 * we know to expect this failure to be transitory, so the
8277 * guest may well hang retrying the operation.
8278 */
8279 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
8280 ri->name, error_get_pretty(err));
8281 error_free(err);
8282
8283 env->ZF = 0; /* NZCF = 0100 */
8284 return 0;
8285 }
8286 return ret;
8287 }
8288
8289 /* We do not support re-seeding, so the two registers operate the same. */
8290 static const ARMCPRegInfo rndr_reginfo[] = {
8291 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
8292 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
8293 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
8294 .access = PL0_R, .readfn = rndr_readfn },
8295 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
8296 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
8297 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
8298 .access = PL0_R, .readfn = rndr_readfn },
8299 };
8300
dccvap_writefn(CPUARMState * env,const ARMCPRegInfo * opaque,uint64_t value)8301 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
8302 uint64_t value)
8303 {
8304 #ifdef CONFIG_TCG
8305 ARMCPU *cpu = env_archcpu(env);
8306 /* CTR_EL0 System register -> DminLine, bits [19:16] */
8307 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
8308 uint64_t vaddr_in = (uint64_t) value;
8309 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
8310 void *haddr;
8311 int mem_idx = arm_env_mmu_index(env);
8312
8313 /* This won't be crossing page boundaries */
8314 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
8315 if (haddr) {
8316 #ifndef CONFIG_USER_ONLY
8317
8318 ram_addr_t offset;
8319 MemoryRegion *mr;
8320
8321 /* RCU lock is already being held */
8322 mr = memory_region_from_host(haddr, &offset);
8323
8324 if (mr) {
8325 memory_region_writeback(mr, offset, dline_size);
8326 }
8327 #endif /*CONFIG_USER_ONLY*/
8328 }
8329 #else
8330 /* Handled by hardware accelerator. */
8331 g_assert_not_reached();
8332 #endif /* CONFIG_TCG */
8333 }
8334
8335 static const ARMCPRegInfo dcpop_reg[] = {
8336 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
8337 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
8338 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
8339 .fgt = FGT_DCCVAP,
8340 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
8341 };
8342
8343 static const ARMCPRegInfo dcpodp_reg[] = {
8344 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
8345 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
8346 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
8347 .fgt = FGT_DCCVADP,
8348 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
8349 };
8350
access_aa64_tid5(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8351 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
8352 bool isread)
8353 {
8354 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
8355 return CP_ACCESS_TRAP_EL2;
8356 }
8357
8358 return CP_ACCESS_OK;
8359 }
8360
access_mte(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8361 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
8362 bool isread)
8363 {
8364 int el = arm_current_el(env);
8365 if (el < 2 && arm_is_el2_enabled(env)) {
8366 uint64_t hcr = arm_hcr_el2_eff(env);
8367 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
8368 return CP_ACCESS_TRAP_EL2;
8369 }
8370 }
8371 if (el < 3 &&
8372 arm_feature(env, ARM_FEATURE_EL3) &&
8373 !(env->cp15.scr_el3 & SCR_ATA)) {
8374 return CP_ACCESS_TRAP_EL3;
8375 }
8376 return CP_ACCESS_OK;
8377 }
8378
access_tfsr_el1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8379 static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri,
8380 bool isread)
8381 {
8382 CPAccessResult nv1 = access_nv1(env, ri, isread);
8383
8384 if (nv1 != CP_ACCESS_OK) {
8385 return nv1;
8386 }
8387 return access_mte(env, ri, isread);
8388 }
8389
access_tfsr_el2(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8390 static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri,
8391 bool isread)
8392 {
8393 /*
8394 * TFSR_EL2: similar to generic access_mte(), but we need to
8395 * account for FEAT_NV. At EL1 this must be a FEAT_NV access;
8396 * if NV2 is enabled then we will redirect this to TFSR_EL1
8397 * after doing the HCR and SCR ATA traps; otherwise this will
8398 * be a trap to EL2 and the HCR/SCR traps do not apply.
8399 */
8400 int el = arm_current_el(env);
8401
8402 if (el == 1 && (arm_hcr_el2_eff(env) & HCR_NV2)) {
8403 return CP_ACCESS_OK;
8404 }
8405 if (el < 2 && arm_is_el2_enabled(env)) {
8406 uint64_t hcr = arm_hcr_el2_eff(env);
8407 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
8408 return CP_ACCESS_TRAP_EL2;
8409 }
8410 }
8411 if (el < 3 &&
8412 arm_feature(env, ARM_FEATURE_EL3) &&
8413 !(env->cp15.scr_el3 & SCR_ATA)) {
8414 return CP_ACCESS_TRAP_EL3;
8415 }
8416 return CP_ACCESS_OK;
8417 }
8418
tco_read(CPUARMState * env,const ARMCPRegInfo * ri)8419 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
8420 {
8421 return env->pstate & PSTATE_TCO;
8422 }
8423
tco_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t val)8424 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
8425 {
8426 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
8427 }
8428
8429 static const ARMCPRegInfo mte_reginfo[] = {
8430 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
8431 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
8432 .access = PL1_RW, .accessfn = access_mte,
8433 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
8434 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
8435 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
8436 .access = PL1_RW, .accessfn = access_tfsr_el1,
8437 .nv2_redirect_offset = 0x190 | NV2_REDIR_NV1,
8438 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
8439 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
8440 .type = ARM_CP_NV2_REDIRECT,
8441 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
8442 .access = PL2_RW, .accessfn = access_tfsr_el2,
8443 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
8444 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
8445 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
8446 .access = PL3_RW,
8447 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
8448 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
8449 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
8450 .access = PL1_RW, .accessfn = access_mte,
8451 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
8452 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
8453 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
8454 .access = PL1_RW, .accessfn = access_mte,
8455 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
8456 { .name = "TCO", .state = ARM_CP_STATE_AA64,
8457 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
8458 .type = ARM_CP_NO_RAW,
8459 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
8460 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
8461 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
8462 .type = ARM_CP_NOP, .access = PL1_W,
8463 .fgt = FGT_DCIVAC,
8464 .accessfn = aa64_cacheop_poc_access },
8465 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
8466 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
8467 .fgt = FGT_DCISW,
8468 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8469 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
8470 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
8471 .type = ARM_CP_NOP, .access = PL1_W,
8472 .fgt = FGT_DCIVAC,
8473 .accessfn = aa64_cacheop_poc_access },
8474 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
8475 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
8476 .fgt = FGT_DCISW,
8477 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8478 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
8479 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
8480 .fgt = FGT_DCCSW,
8481 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8482 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
8483 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
8484 .fgt = FGT_DCCSW,
8485 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8486 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
8487 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
8488 .fgt = FGT_DCCISW,
8489 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8490 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
8491 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
8492 .fgt = FGT_DCCISW,
8493 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8494 };
8495
8496 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
8497 { .name = "TCO", .state = ARM_CP_STATE_AA64,
8498 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
8499 .type = ARM_CP_CONST, .access = PL0_RW, },
8500 };
8501
8502 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
8503 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
8504 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
8505 .type = ARM_CP_NOP, .access = PL0_W,
8506 .fgt = FGT_DCCVAC,
8507 .accessfn = aa64_cacheop_poc_access },
8508 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
8509 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
8510 .type = ARM_CP_NOP, .access = PL0_W,
8511 .fgt = FGT_DCCVAC,
8512 .accessfn = aa64_cacheop_poc_access },
8513 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
8514 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
8515 .type = ARM_CP_NOP, .access = PL0_W,
8516 .fgt = FGT_DCCVAP,
8517 .accessfn = aa64_cacheop_poc_access },
8518 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
8519 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
8520 .type = ARM_CP_NOP, .access = PL0_W,
8521 .fgt = FGT_DCCVAP,
8522 .accessfn = aa64_cacheop_poc_access },
8523 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
8524 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
8525 .type = ARM_CP_NOP, .access = PL0_W,
8526 .fgt = FGT_DCCVADP,
8527 .accessfn = aa64_cacheop_poc_access },
8528 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
8529 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
8530 .type = ARM_CP_NOP, .access = PL0_W,
8531 .fgt = FGT_DCCVADP,
8532 .accessfn = aa64_cacheop_poc_access },
8533 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
8534 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
8535 .type = ARM_CP_NOP, .access = PL0_W,
8536 .fgt = FGT_DCCIVAC,
8537 .accessfn = aa64_cacheop_poc_access },
8538 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
8539 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
8540 .type = ARM_CP_NOP, .access = PL0_W,
8541 .fgt = FGT_DCCIVAC,
8542 .accessfn = aa64_cacheop_poc_access },
8543 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
8544 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
8545 .access = PL0_W, .type = ARM_CP_DC_GVA,
8546 #ifndef CONFIG_USER_ONLY
8547 /* Avoid overhead of an access check that always passes in user-mode */
8548 .accessfn = aa64_zva_access,
8549 .fgt = FGT_DCZVA,
8550 #endif
8551 },
8552 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
8553 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
8554 .access = PL0_W, .type = ARM_CP_DC_GZVA,
8555 #ifndef CONFIG_USER_ONLY
8556 /* Avoid overhead of an access check that always passes in user-mode */
8557 .accessfn = aa64_zva_access,
8558 .fgt = FGT_DCZVA,
8559 #endif
8560 },
8561 };
8562
access_scxtnum(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8563 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
8564 bool isread)
8565 {
8566 uint64_t hcr = arm_hcr_el2_eff(env);
8567 int el = arm_current_el(env);
8568
8569 if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
8570 if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
8571 if (hcr & HCR_TGE) {
8572 return CP_ACCESS_TRAP_EL2;
8573 }
8574 return CP_ACCESS_TRAP;
8575 }
8576 } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
8577 return CP_ACCESS_TRAP_EL2;
8578 }
8579 if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
8580 return CP_ACCESS_TRAP_EL2;
8581 }
8582 if (el < 3
8583 && arm_feature(env, ARM_FEATURE_EL3)
8584 && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
8585 return CP_ACCESS_TRAP_EL3;
8586 }
8587 return CP_ACCESS_OK;
8588 }
8589
access_scxtnum_el1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8590 static CPAccessResult access_scxtnum_el1(CPUARMState *env,
8591 const ARMCPRegInfo *ri,
8592 bool isread)
8593 {
8594 CPAccessResult nv1 = access_nv1(env, ri, isread);
8595
8596 if (nv1 != CP_ACCESS_OK) {
8597 return nv1;
8598 }
8599 return access_scxtnum(env, ri, isread);
8600 }
8601
8602 static const ARMCPRegInfo scxtnum_reginfo[] = {
8603 { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
8604 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
8605 .access = PL0_RW, .accessfn = access_scxtnum,
8606 .fgt = FGT_SCXTNUM_EL0,
8607 .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
8608 { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
8609 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
8610 .access = PL1_RW, .accessfn = access_scxtnum_el1,
8611 .fgt = FGT_SCXTNUM_EL1,
8612 .nv2_redirect_offset = 0x188 | NV2_REDIR_NV1,
8613 .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
8614 { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
8615 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
8616 .access = PL2_RW, .accessfn = access_scxtnum,
8617 .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
8618 { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
8619 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
8620 .access = PL3_RW,
8621 .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
8622 };
8623
access_fgt(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8624 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
8625 bool isread)
8626 {
8627 if (arm_current_el(env) == 2 &&
8628 arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
8629 return CP_ACCESS_TRAP_EL3;
8630 }
8631 return CP_ACCESS_OK;
8632 }
8633
8634 static const ARMCPRegInfo fgt_reginfo[] = {
8635 { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
8636 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
8637 .nv2_redirect_offset = 0x1b8,
8638 .access = PL2_RW, .accessfn = access_fgt,
8639 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
8640 { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
8641 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
8642 .nv2_redirect_offset = 0x1c0,
8643 .access = PL2_RW, .accessfn = access_fgt,
8644 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
8645 { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
8646 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
8647 .nv2_redirect_offset = 0x1d0,
8648 .access = PL2_RW, .accessfn = access_fgt,
8649 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
8650 { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
8651 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
8652 .nv2_redirect_offset = 0x1d8,
8653 .access = PL2_RW, .accessfn = access_fgt,
8654 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
8655 { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
8656 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
8657 .nv2_redirect_offset = 0x1c8,
8658 .access = PL2_RW, .accessfn = access_fgt,
8659 .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
8660 };
8661
vncr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)8662 static void vncr_write(CPUARMState *env, const ARMCPRegInfo *ri,
8663 uint64_t value)
8664 {
8665 /*
8666 * Clear the RES0 bottom 12 bits; this means at runtime we can guarantee
8667 * that VNCR_EL2 + offset is 64-bit aligned. We don't need to do anything
8668 * about the RESS bits at the top -- we choose the "generate an EL2
8669 * translation abort on use" CONSTRAINED UNPREDICTABLE option (i.e. let
8670 * the ptw.c code detect the resulting invalid address).
8671 */
8672 env->cp15.vncr_el2 = value & ~0xfffULL;
8673 }
8674
8675 static const ARMCPRegInfo nv2_reginfo[] = {
8676 { .name = "VNCR_EL2", .state = ARM_CP_STATE_AA64,
8677 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 2, .opc2 = 0,
8678 .access = PL2_RW,
8679 .writefn = vncr_write,
8680 .nv2_redirect_offset = 0xb0,
8681 .fieldoffset = offsetof(CPUARMState, cp15.vncr_el2) },
8682 };
8683
8684 #endif /* TARGET_AARCH64 */
8685
access_predinv(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8686 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
8687 bool isread)
8688 {
8689 int el = arm_current_el(env);
8690
8691 if (el == 0) {
8692 uint64_t sctlr = arm_sctlr(env, el);
8693 if (!(sctlr & SCTLR_EnRCTX)) {
8694 return CP_ACCESS_TRAP;
8695 }
8696 } else if (el == 1) {
8697 uint64_t hcr = arm_hcr_el2_eff(env);
8698 if (hcr & HCR_NV) {
8699 return CP_ACCESS_TRAP_EL2;
8700 }
8701 }
8702 return CP_ACCESS_OK;
8703 }
8704
8705 static const ARMCPRegInfo predinv_reginfo[] = {
8706 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
8707 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
8708 .fgt = FGT_CFPRCTX,
8709 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8710 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
8711 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
8712 .fgt = FGT_DVPRCTX,
8713 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8714 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
8715 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
8716 .fgt = FGT_CPPRCTX,
8717 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8718 /*
8719 * Note the AArch32 opcodes have a different OPC1.
8720 */
8721 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
8722 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
8723 .fgt = FGT_CFPRCTX,
8724 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8725 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
8726 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
8727 .fgt = FGT_DVPRCTX,
8728 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8729 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
8730 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
8731 .fgt = FGT_CPPRCTX,
8732 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8733 };
8734
ccsidr2_read(CPUARMState * env,const ARMCPRegInfo * ri)8735 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
8736 {
8737 /* Read the high 32 bits of the current CCSIDR */
8738 return extract64(ccsidr_read(env, ri), 32, 32);
8739 }
8740
8741 static const ARMCPRegInfo ccsidr2_reginfo[] = {
8742 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
8743 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
8744 .access = PL1_R,
8745 .accessfn = access_tid4,
8746 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
8747 };
8748
access_aa64_tid3(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8749 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8750 bool isread)
8751 {
8752 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
8753 return CP_ACCESS_TRAP_EL2;
8754 }
8755
8756 return CP_ACCESS_OK;
8757 }
8758
access_aa32_tid3(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8759 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8760 bool isread)
8761 {
8762 if (arm_feature(env, ARM_FEATURE_V8)) {
8763 return access_aa64_tid3(env, ri, isread);
8764 }
8765
8766 return CP_ACCESS_OK;
8767 }
8768
access_jazelle(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8769 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
8770 bool isread)
8771 {
8772 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
8773 return CP_ACCESS_TRAP_EL2;
8774 }
8775
8776 return CP_ACCESS_OK;
8777 }
8778
access_joscr_jmcr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8779 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
8780 const ARMCPRegInfo *ri, bool isread)
8781 {
8782 /*
8783 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
8784 * in v7A, not in v8A.
8785 */
8786 if (!arm_feature(env, ARM_FEATURE_V8) &&
8787 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
8788 (env->cp15.hstr_el2 & HSTR_TJDBX)) {
8789 return CP_ACCESS_TRAP_EL2;
8790 }
8791 return CP_ACCESS_OK;
8792 }
8793
8794 static const ARMCPRegInfo jazelle_regs[] = {
8795 { .name = "JIDR",
8796 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
8797 .access = PL1_R, .accessfn = access_jazelle,
8798 .type = ARM_CP_CONST, .resetvalue = 0 },
8799 { .name = "JOSCR",
8800 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8801 .accessfn = access_joscr_jmcr,
8802 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8803 { .name = "JMCR",
8804 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8805 .accessfn = access_joscr_jmcr,
8806 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8807 };
8808
8809 static const ARMCPRegInfo contextidr_el2 = {
8810 .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
8811 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
8812 .access = PL2_RW,
8813 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
8814 };
8815
8816 static const ARMCPRegInfo vhe_reginfo[] = {
8817 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
8818 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
8819 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
8820 .raw_writefn = raw_write,
8821 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8822 #ifndef CONFIG_USER_ONLY
8823 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
8824 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
8825 .fieldoffset =
8826 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
8827 .type = ARM_CP_IO, .access = PL2_RW,
8828 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
8829 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
8830 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
8831 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
8832 .resetfn = gt_hv_timer_reset,
8833 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
8834 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
8835 .type = ARM_CP_IO,
8836 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
8837 .access = PL2_RW,
8838 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
8839 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
8840 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
8841 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
8842 .type = ARM_CP_IO | ARM_CP_ALIAS,
8843 .access = PL2_RW, .accessfn = access_el1nvpct,
8844 .nv2_redirect_offset = 0x180 | NV2_REDIR_NO_NV1,
8845 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
8846 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
8847 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
8848 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
8849 .type = ARM_CP_IO | ARM_CP_ALIAS,
8850 .access = PL2_RW, .accessfn = access_el1nvvct,
8851 .nv2_redirect_offset = 0x170 | NV2_REDIR_NO_NV1,
8852 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
8853 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
8854 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8855 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
8856 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8857 .access = PL2_RW, .accessfn = e2h_access,
8858 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
8859 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8860 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
8861 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8862 .access = PL2_RW, .accessfn = e2h_access,
8863 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
8864 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8865 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
8866 .type = ARM_CP_IO | ARM_CP_ALIAS,
8867 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
8868 .nv2_redirect_offset = 0x178 | NV2_REDIR_NO_NV1,
8869 .access = PL2_RW, .accessfn = access_el1nvpct,
8870 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
8871 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8872 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
8873 .type = ARM_CP_IO | ARM_CP_ALIAS,
8874 .nv2_redirect_offset = 0x168 | NV2_REDIR_NO_NV1,
8875 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
8876 .access = PL2_RW, .accessfn = access_el1nvvct,
8877 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8878 #endif
8879 };
8880
8881 #ifndef CONFIG_USER_ONLY
8882 static const ARMCPRegInfo ats1e1_reginfo[] = {
8883 { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
8884 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8885 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8886 .fgt = FGT_ATS1E1RP,
8887 .accessfn = at_s1e01_access, .writefn = ats_write64 },
8888 { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
8889 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8890 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8891 .fgt = FGT_ATS1E1WP,
8892 .accessfn = at_s1e01_access, .writefn = ats_write64 },
8893 };
8894
8895 static const ARMCPRegInfo ats1cp_reginfo[] = {
8896 { .name = "ATS1CPRP",
8897 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8898 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8899 .writefn = ats_write },
8900 { .name = "ATS1CPWP",
8901 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8902 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8903 .writefn = ats_write },
8904 };
8905 #endif
8906
8907 /*
8908 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
8909 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
8910 * is non-zero, which is never for ARMv7, optionally in ARMv8
8911 * and mandatorily for ARMv8.2 and up.
8912 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
8913 * implementation is RAZ/WI we can ignore this detail, as we
8914 * do for ACTLR.
8915 */
8916 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
8917 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
8918 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
8919 .access = PL1_RW, .accessfn = access_tacr,
8920 .type = ARM_CP_CONST, .resetvalue = 0 },
8921 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
8922 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
8923 .access = PL2_RW, .type = ARM_CP_CONST,
8924 .resetvalue = 0 },
8925 };
8926
register_cp_regs_for_features(ARMCPU * cpu)8927 void register_cp_regs_for_features(ARMCPU *cpu)
8928 {
8929 /* Register all the coprocessor registers based on feature bits */
8930 CPUARMState *env = &cpu->env;
8931 if (arm_feature(env, ARM_FEATURE_M)) {
8932 /* M profile has no coprocessor registers */
8933 return;
8934 }
8935
8936 define_arm_cp_regs(cpu, cp_reginfo);
8937 if (!arm_feature(env, ARM_FEATURE_V8)) {
8938 /*
8939 * Must go early as it is full of wildcards that may be
8940 * overridden by later definitions.
8941 */
8942 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
8943 }
8944
8945 if (arm_feature(env, ARM_FEATURE_V6)) {
8946 /* The ID registers all have impdef reset values */
8947 ARMCPRegInfo v6_idregs[] = {
8948 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
8949 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
8950 .access = PL1_R, .type = ARM_CP_CONST,
8951 .accessfn = access_aa32_tid3,
8952 .resetvalue = cpu->isar.id_pfr0 },
8953 /*
8954 * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
8955 * the value of the GIC field until after we define these regs.
8956 */
8957 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
8958 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
8959 .access = PL1_R, .type = ARM_CP_NO_RAW,
8960 .accessfn = access_aa32_tid3,
8961 #ifdef CONFIG_USER_ONLY
8962 .type = ARM_CP_CONST,
8963 .resetvalue = cpu->isar.id_pfr1,
8964 #else
8965 .type = ARM_CP_NO_RAW,
8966 .accessfn = access_aa32_tid3,
8967 .readfn = id_pfr1_read,
8968 .writefn = arm_cp_write_ignore
8969 #endif
8970 },
8971 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
8972 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
8973 .access = PL1_R, .type = ARM_CP_CONST,
8974 .accessfn = access_aa32_tid3,
8975 .resetvalue = cpu->isar.id_dfr0 },
8976 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
8977 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
8978 .access = PL1_R, .type = ARM_CP_CONST,
8979 .accessfn = access_aa32_tid3,
8980 .resetvalue = cpu->id_afr0 },
8981 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
8982 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
8983 .access = PL1_R, .type = ARM_CP_CONST,
8984 .accessfn = access_aa32_tid3,
8985 .resetvalue = cpu->isar.id_mmfr0 },
8986 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
8987 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
8988 .access = PL1_R, .type = ARM_CP_CONST,
8989 .accessfn = access_aa32_tid3,
8990 .resetvalue = cpu->isar.id_mmfr1 },
8991 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
8992 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
8993 .access = PL1_R, .type = ARM_CP_CONST,
8994 .accessfn = access_aa32_tid3,
8995 .resetvalue = cpu->isar.id_mmfr2 },
8996 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
8997 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
8998 .access = PL1_R, .type = ARM_CP_CONST,
8999 .accessfn = access_aa32_tid3,
9000 .resetvalue = cpu->isar.id_mmfr3 },
9001 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
9002 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
9003 .access = PL1_R, .type = ARM_CP_CONST,
9004 .accessfn = access_aa32_tid3,
9005 .resetvalue = cpu->isar.id_isar0 },
9006 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
9007 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
9008 .access = PL1_R, .type = ARM_CP_CONST,
9009 .accessfn = access_aa32_tid3,
9010 .resetvalue = cpu->isar.id_isar1 },
9011 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
9012 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
9013 .access = PL1_R, .type = ARM_CP_CONST,
9014 .accessfn = access_aa32_tid3,
9015 .resetvalue = cpu->isar.id_isar2 },
9016 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
9017 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
9018 .access = PL1_R, .type = ARM_CP_CONST,
9019 .accessfn = access_aa32_tid3,
9020 .resetvalue = cpu->isar.id_isar3 },
9021 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
9022 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
9023 .access = PL1_R, .type = ARM_CP_CONST,
9024 .accessfn = access_aa32_tid3,
9025 .resetvalue = cpu->isar.id_isar4 },
9026 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
9027 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
9028 .access = PL1_R, .type = ARM_CP_CONST,
9029 .accessfn = access_aa32_tid3,
9030 .resetvalue = cpu->isar.id_isar5 },
9031 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
9032 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
9033 .access = PL1_R, .type = ARM_CP_CONST,
9034 .accessfn = access_aa32_tid3,
9035 .resetvalue = cpu->isar.id_mmfr4 },
9036 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
9037 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
9038 .access = PL1_R, .type = ARM_CP_CONST,
9039 .accessfn = access_aa32_tid3,
9040 .resetvalue = cpu->isar.id_isar6 },
9041 };
9042 define_arm_cp_regs(cpu, v6_idregs);
9043 define_arm_cp_regs(cpu, v6_cp_reginfo);
9044 } else {
9045 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
9046 }
9047 if (arm_feature(env, ARM_FEATURE_V6K)) {
9048 define_arm_cp_regs(cpu, v6k_cp_reginfo);
9049 }
9050 if (arm_feature(env, ARM_FEATURE_V7MP) &&
9051 !arm_feature(env, ARM_FEATURE_PMSA)) {
9052 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
9053 }
9054 if (arm_feature(env, ARM_FEATURE_V7VE)) {
9055 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
9056 }
9057 if (arm_feature(env, ARM_FEATURE_V7)) {
9058 ARMCPRegInfo clidr = {
9059 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
9060 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
9061 .access = PL1_R, .type = ARM_CP_CONST,
9062 .accessfn = access_tid4,
9063 .fgt = FGT_CLIDR_EL1,
9064 .resetvalue = cpu->clidr
9065 };
9066 define_one_arm_cp_reg(cpu, &clidr);
9067 define_arm_cp_regs(cpu, v7_cp_reginfo);
9068 define_debug_regs(cpu);
9069 define_pmu_regs(cpu);
9070 } else {
9071 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
9072 }
9073 if (arm_feature(env, ARM_FEATURE_V8)) {
9074 /*
9075 * v8 ID registers, which all have impdef reset values.
9076 * Note that within the ID register ranges the unused slots
9077 * must all RAZ, not UNDEF; future architecture versions may
9078 * define new registers here.
9079 * ID registers which are AArch64 views of the AArch32 ID registers
9080 * which already existed in v6 and v7 are handled elsewhere,
9081 * in v6_idregs[].
9082 */
9083 int i;
9084 ARMCPRegInfo v8_idregs[] = {
9085 /*
9086 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
9087 * emulation because we don't know the right value for the
9088 * GIC field until after we define these regs.
9089 */
9090 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
9091 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
9092 .access = PL1_R,
9093 #ifdef CONFIG_USER_ONLY
9094 .type = ARM_CP_CONST,
9095 .resetvalue = cpu->isar.id_aa64pfr0
9096 #else
9097 .type = ARM_CP_NO_RAW,
9098 .accessfn = access_aa64_tid3,
9099 .readfn = id_aa64pfr0_read,
9100 .writefn = arm_cp_write_ignore
9101 #endif
9102 },
9103 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
9104 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
9105 .access = PL1_R, .type = ARM_CP_CONST,
9106 .accessfn = access_aa64_tid3,
9107 .resetvalue = cpu->isar.id_aa64pfr1},
9108 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9109 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
9110 .access = PL1_R, .type = ARM_CP_CONST,
9111 .accessfn = access_aa64_tid3,
9112 .resetvalue = 0 },
9113 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9114 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
9115 .access = PL1_R, .type = ARM_CP_CONST,
9116 .accessfn = access_aa64_tid3,
9117 .resetvalue = 0 },
9118 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
9119 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
9120 .access = PL1_R, .type = ARM_CP_CONST,
9121 .accessfn = access_aa64_tid3,
9122 .resetvalue = cpu->isar.id_aa64zfr0 },
9123 { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
9124 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
9125 .access = PL1_R, .type = ARM_CP_CONST,
9126 .accessfn = access_aa64_tid3,
9127 .resetvalue = cpu->isar.id_aa64smfr0 },
9128 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9129 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
9130 .access = PL1_R, .type = ARM_CP_CONST,
9131 .accessfn = access_aa64_tid3,
9132 .resetvalue = 0 },
9133 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9134 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
9135 .access = PL1_R, .type = ARM_CP_CONST,
9136 .accessfn = access_aa64_tid3,
9137 .resetvalue = 0 },
9138 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
9139 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
9140 .access = PL1_R, .type = ARM_CP_CONST,
9141 .accessfn = access_aa64_tid3,
9142 .resetvalue = cpu->isar.id_aa64dfr0 },
9143 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
9144 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
9145 .access = PL1_R, .type = ARM_CP_CONST,
9146 .accessfn = access_aa64_tid3,
9147 .resetvalue = cpu->isar.id_aa64dfr1 },
9148 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9149 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
9150 .access = PL1_R, .type = ARM_CP_CONST,
9151 .accessfn = access_aa64_tid3,
9152 .resetvalue = 0 },
9153 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9154 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
9155 .access = PL1_R, .type = ARM_CP_CONST,
9156 .accessfn = access_aa64_tid3,
9157 .resetvalue = 0 },
9158 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
9159 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
9160 .access = PL1_R, .type = ARM_CP_CONST,
9161 .accessfn = access_aa64_tid3,
9162 .resetvalue = cpu->id_aa64afr0 },
9163 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
9164 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
9165 .access = PL1_R, .type = ARM_CP_CONST,
9166 .accessfn = access_aa64_tid3,
9167 .resetvalue = cpu->id_aa64afr1 },
9168 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9169 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
9170 .access = PL1_R, .type = ARM_CP_CONST,
9171 .accessfn = access_aa64_tid3,
9172 .resetvalue = 0 },
9173 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9174 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
9175 .access = PL1_R, .type = ARM_CP_CONST,
9176 .accessfn = access_aa64_tid3,
9177 .resetvalue = 0 },
9178 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
9179 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
9180 .access = PL1_R, .type = ARM_CP_CONST,
9181 .accessfn = access_aa64_tid3,
9182 .resetvalue = cpu->isar.id_aa64isar0 },
9183 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
9184 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
9185 .access = PL1_R, .type = ARM_CP_CONST,
9186 .accessfn = access_aa64_tid3,
9187 .resetvalue = cpu->isar.id_aa64isar1 },
9188 { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
9189 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
9190 .access = PL1_R, .type = ARM_CP_CONST,
9191 .accessfn = access_aa64_tid3,
9192 .resetvalue = cpu->isar.id_aa64isar2 },
9193 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9194 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
9195 .access = PL1_R, .type = ARM_CP_CONST,
9196 .accessfn = access_aa64_tid3,
9197 .resetvalue = 0 },
9198 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9199 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
9200 .access = PL1_R, .type = ARM_CP_CONST,
9201 .accessfn = access_aa64_tid3,
9202 .resetvalue = 0 },
9203 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9204 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
9205 .access = PL1_R, .type = ARM_CP_CONST,
9206 .accessfn = access_aa64_tid3,
9207 .resetvalue = 0 },
9208 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9209 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
9210 .access = PL1_R, .type = ARM_CP_CONST,
9211 .accessfn = access_aa64_tid3,
9212 .resetvalue = 0 },
9213 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9214 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
9215 .access = PL1_R, .type = ARM_CP_CONST,
9216 .accessfn = access_aa64_tid3,
9217 .resetvalue = 0 },
9218 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
9219 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
9220 .access = PL1_R, .type = ARM_CP_CONST,
9221 .accessfn = access_aa64_tid3,
9222 .resetvalue = cpu->isar.id_aa64mmfr0 },
9223 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
9224 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
9225 .access = PL1_R, .type = ARM_CP_CONST,
9226 .accessfn = access_aa64_tid3,
9227 .resetvalue = cpu->isar.id_aa64mmfr1 },
9228 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
9229 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
9230 .access = PL1_R, .type = ARM_CP_CONST,
9231 .accessfn = access_aa64_tid3,
9232 .resetvalue = cpu->isar.id_aa64mmfr2 },
9233 { .name = "ID_AA64MMFR3_EL1", .state = ARM_CP_STATE_AA64,
9234 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
9235 .access = PL1_R, .type = ARM_CP_CONST,
9236 .accessfn = access_aa64_tid3,
9237 .resetvalue = cpu->isar.id_aa64mmfr3 },
9238 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9239 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
9240 .access = PL1_R, .type = ARM_CP_CONST,
9241 .accessfn = access_aa64_tid3,
9242 .resetvalue = 0 },
9243 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9244 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
9245 .access = PL1_R, .type = ARM_CP_CONST,
9246 .accessfn = access_aa64_tid3,
9247 .resetvalue = 0 },
9248 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9249 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
9250 .access = PL1_R, .type = ARM_CP_CONST,
9251 .accessfn = access_aa64_tid3,
9252 .resetvalue = 0 },
9253 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9254 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
9255 .access = PL1_R, .type = ARM_CP_CONST,
9256 .accessfn = access_aa64_tid3,
9257 .resetvalue = 0 },
9258 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
9259 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
9260 .access = PL1_R, .type = ARM_CP_CONST,
9261 .accessfn = access_aa64_tid3,
9262 .resetvalue = cpu->isar.mvfr0 },
9263 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
9264 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
9265 .access = PL1_R, .type = ARM_CP_CONST,
9266 .accessfn = access_aa64_tid3,
9267 .resetvalue = cpu->isar.mvfr1 },
9268 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
9269 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
9270 .access = PL1_R, .type = ARM_CP_CONST,
9271 .accessfn = access_aa64_tid3,
9272 .resetvalue = cpu->isar.mvfr2 },
9273 /*
9274 * "0, c0, c3, {0,1,2}" are the encodings corresponding to
9275 * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
9276 * as RAZ, since it is in the "reserved for future ID
9277 * registers, RAZ" part of the AArch32 encoding space.
9278 */
9279 { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
9280 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
9281 .access = PL1_R, .type = ARM_CP_CONST,
9282 .accessfn = access_aa64_tid3,
9283 .resetvalue = 0 },
9284 { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
9285 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
9286 .access = PL1_R, .type = ARM_CP_CONST,
9287 .accessfn = access_aa64_tid3,
9288 .resetvalue = 0 },
9289 { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
9290 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
9291 .access = PL1_R, .type = ARM_CP_CONST,
9292 .accessfn = access_aa64_tid3,
9293 .resetvalue = 0 },
9294 /*
9295 * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
9296 * they're also RAZ for AArch64, and in v8 are gradually
9297 * being filled with AArch64-view-of-AArch32-ID-register
9298 * for new ID registers.
9299 */
9300 { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
9301 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
9302 .access = PL1_R, .type = ARM_CP_CONST,
9303 .accessfn = access_aa64_tid3,
9304 .resetvalue = 0 },
9305 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
9306 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
9307 .access = PL1_R, .type = ARM_CP_CONST,
9308 .accessfn = access_aa64_tid3,
9309 .resetvalue = cpu->isar.id_pfr2 },
9310 { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
9311 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
9312 .access = PL1_R, .type = ARM_CP_CONST,
9313 .accessfn = access_aa64_tid3,
9314 .resetvalue = cpu->isar.id_dfr1 },
9315 { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
9316 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
9317 .access = PL1_R, .type = ARM_CP_CONST,
9318 .accessfn = access_aa64_tid3,
9319 .resetvalue = cpu->isar.id_mmfr5 },
9320 { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
9321 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
9322 .access = PL1_R, .type = ARM_CP_CONST,
9323 .accessfn = access_aa64_tid3,
9324 .resetvalue = 0 },
9325 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
9326 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
9327 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
9328 .fgt = FGT_PMCEIDN_EL0,
9329 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
9330 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
9331 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
9332 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
9333 .fgt = FGT_PMCEIDN_EL0,
9334 .resetvalue = cpu->pmceid0 },
9335 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
9336 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
9337 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
9338 .fgt = FGT_PMCEIDN_EL0,
9339 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
9340 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
9341 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
9342 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
9343 .fgt = FGT_PMCEIDN_EL0,
9344 .resetvalue = cpu->pmceid1 },
9345 };
9346 #ifdef CONFIG_USER_ONLY
9347 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
9348 { .name = "ID_AA64PFR0_EL1",
9349 .exported_bits = R_ID_AA64PFR0_FP_MASK |
9350 R_ID_AA64PFR0_ADVSIMD_MASK |
9351 R_ID_AA64PFR0_SVE_MASK |
9352 R_ID_AA64PFR0_DIT_MASK,
9353 .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
9354 (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
9355 { .name = "ID_AA64PFR1_EL1",
9356 .exported_bits = R_ID_AA64PFR1_BT_MASK |
9357 R_ID_AA64PFR1_SSBS_MASK |
9358 R_ID_AA64PFR1_MTE_MASK |
9359 R_ID_AA64PFR1_SME_MASK },
9360 { .name = "ID_AA64PFR*_EL1_RESERVED",
9361 .is_glob = true },
9362 { .name = "ID_AA64ZFR0_EL1",
9363 .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
9364 R_ID_AA64ZFR0_AES_MASK |
9365 R_ID_AA64ZFR0_BITPERM_MASK |
9366 R_ID_AA64ZFR0_BFLOAT16_MASK |
9367 R_ID_AA64ZFR0_B16B16_MASK |
9368 R_ID_AA64ZFR0_SHA3_MASK |
9369 R_ID_AA64ZFR0_SM4_MASK |
9370 R_ID_AA64ZFR0_I8MM_MASK |
9371 R_ID_AA64ZFR0_F32MM_MASK |
9372 R_ID_AA64ZFR0_F64MM_MASK },
9373 { .name = "ID_AA64SMFR0_EL1",
9374 .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
9375 R_ID_AA64SMFR0_BI32I32_MASK |
9376 R_ID_AA64SMFR0_B16F32_MASK |
9377 R_ID_AA64SMFR0_F16F32_MASK |
9378 R_ID_AA64SMFR0_I8I32_MASK |
9379 R_ID_AA64SMFR0_F16F16_MASK |
9380 R_ID_AA64SMFR0_B16B16_MASK |
9381 R_ID_AA64SMFR0_I16I32_MASK |
9382 R_ID_AA64SMFR0_F64F64_MASK |
9383 R_ID_AA64SMFR0_I16I64_MASK |
9384 R_ID_AA64SMFR0_SMEVER_MASK |
9385 R_ID_AA64SMFR0_FA64_MASK },
9386 { .name = "ID_AA64MMFR0_EL1",
9387 .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
9388 .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
9389 (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
9390 { .name = "ID_AA64MMFR1_EL1",
9391 .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
9392 { .name = "ID_AA64MMFR2_EL1",
9393 .exported_bits = R_ID_AA64MMFR2_AT_MASK },
9394 { .name = "ID_AA64MMFR3_EL1",
9395 .exported_bits = 0 },
9396 { .name = "ID_AA64MMFR*_EL1_RESERVED",
9397 .is_glob = true },
9398 { .name = "ID_AA64DFR0_EL1",
9399 .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
9400 { .name = "ID_AA64DFR1_EL1" },
9401 { .name = "ID_AA64DFR*_EL1_RESERVED",
9402 .is_glob = true },
9403 { .name = "ID_AA64AFR*",
9404 .is_glob = true },
9405 { .name = "ID_AA64ISAR0_EL1",
9406 .exported_bits = R_ID_AA64ISAR0_AES_MASK |
9407 R_ID_AA64ISAR0_SHA1_MASK |
9408 R_ID_AA64ISAR0_SHA2_MASK |
9409 R_ID_AA64ISAR0_CRC32_MASK |
9410 R_ID_AA64ISAR0_ATOMIC_MASK |
9411 R_ID_AA64ISAR0_RDM_MASK |
9412 R_ID_AA64ISAR0_SHA3_MASK |
9413 R_ID_AA64ISAR0_SM3_MASK |
9414 R_ID_AA64ISAR0_SM4_MASK |
9415 R_ID_AA64ISAR0_DP_MASK |
9416 R_ID_AA64ISAR0_FHM_MASK |
9417 R_ID_AA64ISAR0_TS_MASK |
9418 R_ID_AA64ISAR0_RNDR_MASK },
9419 { .name = "ID_AA64ISAR1_EL1",
9420 .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
9421 R_ID_AA64ISAR1_APA_MASK |
9422 R_ID_AA64ISAR1_API_MASK |
9423 R_ID_AA64ISAR1_JSCVT_MASK |
9424 R_ID_AA64ISAR1_FCMA_MASK |
9425 R_ID_AA64ISAR1_LRCPC_MASK |
9426 R_ID_AA64ISAR1_GPA_MASK |
9427 R_ID_AA64ISAR1_GPI_MASK |
9428 R_ID_AA64ISAR1_FRINTTS_MASK |
9429 R_ID_AA64ISAR1_SB_MASK |
9430 R_ID_AA64ISAR1_BF16_MASK |
9431 R_ID_AA64ISAR1_DGH_MASK |
9432 R_ID_AA64ISAR1_I8MM_MASK },
9433 { .name = "ID_AA64ISAR2_EL1",
9434 .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
9435 R_ID_AA64ISAR2_RPRES_MASK |
9436 R_ID_AA64ISAR2_GPA3_MASK |
9437 R_ID_AA64ISAR2_APA3_MASK |
9438 R_ID_AA64ISAR2_MOPS_MASK |
9439 R_ID_AA64ISAR2_BC_MASK |
9440 R_ID_AA64ISAR2_RPRFM_MASK |
9441 R_ID_AA64ISAR2_CSSC_MASK },
9442 { .name = "ID_AA64ISAR*_EL1_RESERVED",
9443 .is_glob = true },
9444 };
9445 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
9446 #endif
9447 /*
9448 * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
9449 * TODO: For RMR, a write with bit 1 set should do something with
9450 * cpu_reset(). In the meantime, "the bit is strictly a request",
9451 * so we are in spec just ignoring writes.
9452 */
9453 if (!arm_feature(env, ARM_FEATURE_EL3) &&
9454 !arm_feature(env, ARM_FEATURE_EL2)) {
9455 ARMCPRegInfo el1_reset_regs[] = {
9456 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
9457 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
9458 .access = PL1_R,
9459 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9460 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
9461 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
9462 .access = PL1_RW, .type = ARM_CP_CONST,
9463 .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
9464 };
9465 define_arm_cp_regs(cpu, el1_reset_regs);
9466 }
9467 define_arm_cp_regs(cpu, v8_idregs);
9468 define_arm_cp_regs(cpu, v8_cp_reginfo);
9469 if (cpu_isar_feature(aa64_aa32_el1, cpu)) {
9470 define_arm_cp_regs(cpu, v8_aa32_el1_reginfo);
9471 }
9472
9473 for (i = 4; i < 16; i++) {
9474 /*
9475 * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
9476 * For pre-v8 cores there are RAZ patterns for these in
9477 * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
9478 * v8 extends the "must RAZ" part of the ID register space
9479 * to also cover c0, 0, c{8-15}, {0-7}.
9480 * These are STATE_AA32 because in the AArch64 sysreg space
9481 * c4-c7 is where the AArch64 ID registers live (and we've
9482 * already defined those in v8_idregs[]), and c8-c15 are not
9483 * "must RAZ" for AArch64.
9484 */
9485 g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
9486 ARMCPRegInfo v8_aa32_raz_idregs = {
9487 .name = name,
9488 .state = ARM_CP_STATE_AA32,
9489 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
9490 .access = PL1_R, .type = ARM_CP_CONST,
9491 .accessfn = access_aa64_tid3,
9492 .resetvalue = 0 };
9493 define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
9494 }
9495 }
9496
9497 /*
9498 * Register the base EL2 cpregs.
9499 * Pre v8, these registers are implemented only as part of the
9500 * Virtualization Extensions (EL2 present). Beginning with v8,
9501 * if EL2 is missing but EL3 is enabled, mostly these become
9502 * RES0 from EL3, with some specific exceptions.
9503 */
9504 if (arm_feature(env, ARM_FEATURE_EL2)
9505 || (arm_feature(env, ARM_FEATURE_EL3)
9506 && arm_feature(env, ARM_FEATURE_V8))) {
9507 uint64_t vmpidr_def = mpidr_read_val(env);
9508 ARMCPRegInfo vpidr_regs[] = {
9509 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
9510 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
9511 .access = PL2_RW, .accessfn = access_el3_aa32ns,
9512 .resetvalue = cpu->midr,
9513 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
9514 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
9515 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
9516 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
9517 .access = PL2_RW, .resetvalue = cpu->midr,
9518 .type = ARM_CP_EL3_NO_EL2_C_NZ,
9519 .nv2_redirect_offset = 0x88,
9520 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
9521 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
9522 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
9523 .access = PL2_RW, .accessfn = access_el3_aa32ns,
9524 .resetvalue = vmpidr_def,
9525 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
9526 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
9527 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
9528 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
9529 .access = PL2_RW, .resetvalue = vmpidr_def,
9530 .type = ARM_CP_EL3_NO_EL2_C_NZ,
9531 .nv2_redirect_offset = 0x50,
9532 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
9533 };
9534 /*
9535 * The only field of MDCR_EL2 that has a defined architectural reset
9536 * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
9537 */
9538 ARMCPRegInfo mdcr_el2 = {
9539 .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
9540 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
9541 .writefn = mdcr_el2_write,
9542 .access = PL2_RW, .resetvalue = pmu_num_counters(env),
9543 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
9544 };
9545 define_one_arm_cp_reg(cpu, &mdcr_el2);
9546 define_arm_cp_regs(cpu, vpidr_regs);
9547 define_arm_cp_regs(cpu, el2_cp_reginfo);
9548 if (arm_feature(env, ARM_FEATURE_V8)) {
9549 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
9550 }
9551 if (cpu_isar_feature(aa64_sel2, cpu)) {
9552 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
9553 }
9554 /*
9555 * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
9556 * See commentary near RMR_EL1.
9557 */
9558 if (!arm_feature(env, ARM_FEATURE_EL3)) {
9559 static const ARMCPRegInfo el2_reset_regs[] = {
9560 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
9561 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
9562 .access = PL2_R,
9563 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9564 { .name = "RVBAR", .type = ARM_CP_ALIAS,
9565 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
9566 .access = PL2_R,
9567 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9568 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
9569 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
9570 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
9571 };
9572 define_arm_cp_regs(cpu, el2_reset_regs);
9573 }
9574 }
9575
9576 /* Register the base EL3 cpregs. */
9577 if (arm_feature(env, ARM_FEATURE_EL3)) {
9578 define_arm_cp_regs(cpu, el3_cp_reginfo);
9579 ARMCPRegInfo el3_regs[] = {
9580 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
9581 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
9582 .access = PL3_R,
9583 .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
9584 { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
9585 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
9586 .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
9587 { .name = "RMR", .state = ARM_CP_STATE_AA32,
9588 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
9589 .access = PL3_RW, .type = ARM_CP_CONST,
9590 .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
9591 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
9592 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
9593 .access = PL3_RW,
9594 .raw_writefn = raw_write, .writefn = sctlr_write,
9595 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
9596 .resetvalue = cpu->reset_sctlr },
9597 };
9598
9599 define_arm_cp_regs(cpu, el3_regs);
9600 }
9601 /*
9602 * The behaviour of NSACR is sufficiently various that we don't
9603 * try to describe it in a single reginfo:
9604 * if EL3 is 64 bit, then trap to EL3 from S EL1,
9605 * reads as constant 0xc00 from NS EL1 and NS EL2
9606 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
9607 * if v7 without EL3, register doesn't exist
9608 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
9609 */
9610 if (arm_feature(env, ARM_FEATURE_EL3)) {
9611 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
9612 static const ARMCPRegInfo nsacr = {
9613 .name = "NSACR", .type = ARM_CP_CONST,
9614 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9615 .access = PL1_RW, .accessfn = nsacr_access,
9616 .resetvalue = 0xc00
9617 };
9618 define_one_arm_cp_reg(cpu, &nsacr);
9619 } else {
9620 static const ARMCPRegInfo nsacr = {
9621 .name = "NSACR",
9622 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9623 .access = PL3_RW | PL1_R,
9624 .resetvalue = 0,
9625 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
9626 };
9627 define_one_arm_cp_reg(cpu, &nsacr);
9628 }
9629 } else {
9630 if (arm_feature(env, ARM_FEATURE_V8)) {
9631 static const ARMCPRegInfo nsacr = {
9632 .name = "NSACR", .type = ARM_CP_CONST,
9633 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9634 .access = PL1_R,
9635 .resetvalue = 0xc00
9636 };
9637 define_one_arm_cp_reg(cpu, &nsacr);
9638 }
9639 }
9640
9641 if (arm_feature(env, ARM_FEATURE_PMSA)) {
9642 if (arm_feature(env, ARM_FEATURE_V6)) {
9643 /* PMSAv6 not implemented */
9644 assert(arm_feature(env, ARM_FEATURE_V7));
9645 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
9646 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
9647 } else {
9648 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
9649 }
9650 } else {
9651 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
9652 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
9653 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
9654 if (cpu_isar_feature(aa32_hpd, cpu)) {
9655 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
9656 }
9657 }
9658 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
9659 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
9660 }
9661 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
9662 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
9663 }
9664 if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
9665 define_arm_cp_regs(cpu, gen_timer_ecv_cp_reginfo);
9666 }
9667 #ifndef CONFIG_USER_ONLY
9668 if (cpu_isar_feature(aa64_ecv, cpu)) {
9669 define_one_arm_cp_reg(cpu, &gen_timer_cntpoff_reginfo);
9670 }
9671 #endif
9672 if (arm_feature(env, ARM_FEATURE_VAPA)) {
9673 ARMCPRegInfo vapa_cp_reginfo[] = {
9674 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
9675 .access = PL1_RW, .resetvalue = 0,
9676 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
9677 offsetoflow32(CPUARMState, cp15.par_ns) },
9678 .writefn = par_write},
9679 #ifndef CONFIG_USER_ONLY
9680 /* This underdecoding is safe because the reginfo is NO_RAW. */
9681 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
9682 .access = PL1_W, .accessfn = ats_access,
9683 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
9684 #endif
9685 };
9686
9687 /*
9688 * When LPAE exists this 32-bit PAR register is an alias of the
9689 * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
9690 */
9691 if (arm_feature(env, ARM_FEATURE_LPAE)) {
9692 vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
9693 }
9694 define_arm_cp_regs(cpu, vapa_cp_reginfo);
9695 }
9696 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
9697 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
9698 }
9699 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
9700 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
9701 }
9702 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
9703 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
9704 }
9705 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
9706 define_arm_cp_regs(cpu, omap_cp_reginfo);
9707 }
9708 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
9709 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
9710 }
9711 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9712 define_arm_cp_regs(cpu, xscale_cp_reginfo);
9713 }
9714 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
9715 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
9716 }
9717 if (arm_feature(env, ARM_FEATURE_LPAE)) {
9718 define_arm_cp_regs(cpu, lpae_cp_reginfo);
9719 }
9720 if (cpu_isar_feature(aa32_jazelle, cpu)) {
9721 define_arm_cp_regs(cpu, jazelle_regs);
9722 }
9723 /*
9724 * Slightly awkwardly, the OMAP and StrongARM cores need all of
9725 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
9726 * be read-only (ie write causes UNDEF exception).
9727 */
9728 {
9729 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
9730 /*
9731 * Pre-v8 MIDR space.
9732 * Note that the MIDR isn't a simple constant register because
9733 * of the TI925 behaviour where writes to another register can
9734 * cause the MIDR value to change.
9735 *
9736 * Unimplemented registers in the c15 0 0 0 space default to
9737 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
9738 * and friends override accordingly.
9739 */
9740 { .name = "MIDR",
9741 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
9742 .access = PL1_R, .resetvalue = cpu->midr,
9743 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
9744 .readfn = midr_read,
9745 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9746 .type = ARM_CP_OVERRIDE },
9747 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
9748 { .name = "DUMMY",
9749 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
9750 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9751 { .name = "DUMMY",
9752 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
9753 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9754 { .name = "DUMMY",
9755 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
9756 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9757 { .name = "DUMMY",
9758 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
9759 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9760 { .name = "DUMMY",
9761 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
9762 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9763 };
9764 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
9765 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
9766 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
9767 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
9768 .fgt = FGT_MIDR_EL1,
9769 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9770 .readfn = midr_read },
9771 /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
9772 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
9773 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
9774 .access = PL1_R, .resetvalue = cpu->midr },
9775 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
9776 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
9777 .access = PL1_R,
9778 .accessfn = access_aa64_tid1,
9779 .fgt = FGT_REVIDR_EL1,
9780 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
9781 };
9782 ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
9783 .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
9784 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9785 .access = PL1_R, .resetvalue = cpu->midr
9786 };
9787 ARMCPRegInfo id_cp_reginfo[] = {
9788 /* These are common to v8 and pre-v8 */
9789 { .name = "CTR",
9790 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
9791 .access = PL1_R, .accessfn = ctr_el0_access,
9792 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9793 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
9794 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
9795 .access = PL0_R, .accessfn = ctr_el0_access,
9796 .fgt = FGT_CTR_EL0,
9797 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9798 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
9799 { .name = "TCMTR",
9800 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
9801 .access = PL1_R,
9802 .accessfn = access_aa32_tid1,
9803 .type = ARM_CP_CONST, .resetvalue = 0 },
9804 };
9805 /* TLBTR is specific to VMSA */
9806 ARMCPRegInfo id_tlbtr_reginfo = {
9807 .name = "TLBTR",
9808 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
9809 .access = PL1_R,
9810 .accessfn = access_aa32_tid1,
9811 .type = ARM_CP_CONST, .resetvalue = 0,
9812 };
9813 /* MPUIR is specific to PMSA V6+ */
9814 ARMCPRegInfo id_mpuir_reginfo = {
9815 .name = "MPUIR",
9816 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9817 .access = PL1_R, .type = ARM_CP_CONST,
9818 .resetvalue = cpu->pmsav7_dregion << 8
9819 };
9820 /* HMPUIR is specific to PMSA V8 */
9821 ARMCPRegInfo id_hmpuir_reginfo = {
9822 .name = "HMPUIR",
9823 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
9824 .access = PL2_R, .type = ARM_CP_CONST,
9825 .resetvalue = cpu->pmsav8r_hdregion
9826 };
9827 static const ARMCPRegInfo crn0_wi_reginfo = {
9828 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
9829 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
9830 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
9831 };
9832 #ifdef CONFIG_USER_ONLY
9833 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
9834 { .name = "MIDR_EL1",
9835 .exported_bits = R_MIDR_EL1_REVISION_MASK |
9836 R_MIDR_EL1_PARTNUM_MASK |
9837 R_MIDR_EL1_ARCHITECTURE_MASK |
9838 R_MIDR_EL1_VARIANT_MASK |
9839 R_MIDR_EL1_IMPLEMENTER_MASK },
9840 { .name = "REVIDR_EL1" },
9841 };
9842 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
9843 #endif
9844 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
9845 arm_feature(env, ARM_FEATURE_STRONGARM)) {
9846 size_t i;
9847 /*
9848 * Register the blanket "writes ignored" value first to cover the
9849 * whole space. Then update the specific ID registers to allow write
9850 * access, so that they ignore writes rather than causing them to
9851 * UNDEF.
9852 */
9853 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
9854 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
9855 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
9856 }
9857 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
9858 id_cp_reginfo[i].access = PL1_RW;
9859 }
9860 id_mpuir_reginfo.access = PL1_RW;
9861 id_tlbtr_reginfo.access = PL1_RW;
9862 }
9863 if (arm_feature(env, ARM_FEATURE_V8)) {
9864 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
9865 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9866 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
9867 }
9868 } else {
9869 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
9870 }
9871 define_arm_cp_regs(cpu, id_cp_reginfo);
9872 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9873 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
9874 } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
9875 arm_feature(env, ARM_FEATURE_V8)) {
9876 uint32_t i = 0;
9877 char *tmp_string;
9878
9879 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9880 define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
9881 define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
9882
9883 /* Register alias is only valid for first 32 indexes */
9884 for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
9885 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9886 uint8_t opc1 = extract32(i, 4, 1);
9887 uint8_t opc2 = extract32(i, 0, 1) << 2;
9888
9889 tmp_string = g_strdup_printf("PRBAR%u", i);
9890 ARMCPRegInfo tmp_prbarn_reginfo = {
9891 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9892 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9893 .access = PL1_RW, .resetvalue = 0,
9894 .accessfn = access_tvm_trvm,
9895 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9896 };
9897 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
9898 g_free(tmp_string);
9899
9900 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9901 tmp_string = g_strdup_printf("PRLAR%u", i);
9902 ARMCPRegInfo tmp_prlarn_reginfo = {
9903 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9904 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9905 .access = PL1_RW, .resetvalue = 0,
9906 .accessfn = access_tvm_trvm,
9907 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9908 };
9909 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
9910 g_free(tmp_string);
9911 }
9912
9913 /* Register alias is only valid for first 32 indexes */
9914 for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
9915 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9916 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
9917 uint8_t opc2 = extract32(i, 0, 1) << 2;
9918
9919 tmp_string = g_strdup_printf("HPRBAR%u", i);
9920 ARMCPRegInfo tmp_hprbarn_reginfo = {
9921 .name = tmp_string,
9922 .type = ARM_CP_NO_RAW,
9923 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9924 .access = PL2_RW, .resetvalue = 0,
9925 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9926 };
9927 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
9928 g_free(tmp_string);
9929
9930 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9931 tmp_string = g_strdup_printf("HPRLAR%u", i);
9932 ARMCPRegInfo tmp_hprlarn_reginfo = {
9933 .name = tmp_string,
9934 .type = ARM_CP_NO_RAW,
9935 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9936 .access = PL2_RW, .resetvalue = 0,
9937 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9938 };
9939 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
9940 g_free(tmp_string);
9941 }
9942 } else if (arm_feature(env, ARM_FEATURE_V7)) {
9943 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9944 }
9945 }
9946
9947 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
9948 ARMCPRegInfo mpidr_cp_reginfo[] = {
9949 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
9950 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
9951 .fgt = FGT_MPIDR_EL1,
9952 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
9953 };
9954 #ifdef CONFIG_USER_ONLY
9955 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
9956 { .name = "MPIDR_EL1",
9957 .fixed_bits = 0x0000000080000000 },
9958 };
9959 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
9960 #endif
9961 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
9962 }
9963
9964 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
9965 ARMCPRegInfo auxcr_reginfo[] = {
9966 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
9967 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
9968 .access = PL1_RW, .accessfn = access_tacr,
9969 .nv2_redirect_offset = 0x118,
9970 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
9971 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
9972 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
9973 .access = PL2_RW, .type = ARM_CP_CONST,
9974 .resetvalue = 0 },
9975 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
9976 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
9977 .access = PL3_RW, .type = ARM_CP_CONST,
9978 .resetvalue = 0 },
9979 };
9980 define_arm_cp_regs(cpu, auxcr_reginfo);
9981 if (cpu_isar_feature(aa32_ac2, cpu)) {
9982 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
9983 }
9984 }
9985
9986 if (arm_feature(env, ARM_FEATURE_CBAR)) {
9987 /*
9988 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
9989 * There are two flavours:
9990 * (1) older 32-bit only cores have a simple 32-bit CBAR
9991 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
9992 * 32-bit register visible to AArch32 at a different encoding
9993 * to the "flavour 1" register and with the bits rearranged to
9994 * be able to squash a 64-bit address into the 32-bit view.
9995 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
9996 * in future if we support AArch32-only configs of some of the
9997 * AArch64 cores we might need to add a specific feature flag
9998 * to indicate cores with "flavour 2" CBAR.
9999 */
10000 if (arm_feature(env, ARM_FEATURE_V8)) {
10001 /* 32 bit view is [31:18] 0...0 [43:32]. */
10002 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
10003 | extract64(cpu->reset_cbar, 32, 12);
10004 ARMCPRegInfo cbar_reginfo[] = {
10005 { .name = "CBAR",
10006 .type = ARM_CP_CONST,
10007 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
10008 .access = PL1_R, .resetvalue = cbar32 },
10009 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
10010 .type = ARM_CP_CONST,
10011 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
10012 .access = PL1_R, .resetvalue = cpu->reset_cbar },
10013 };
10014 /* We don't implement a r/w 64 bit CBAR currently */
10015 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
10016 define_arm_cp_regs(cpu, cbar_reginfo);
10017 } else {
10018 ARMCPRegInfo cbar = {
10019 .name = "CBAR",
10020 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
10021 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
10022 .fieldoffset = offsetof(CPUARMState,
10023 cp15.c15_config_base_address)
10024 };
10025 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
10026 cbar.access = PL1_R;
10027 cbar.fieldoffset = 0;
10028 cbar.type = ARM_CP_CONST;
10029 }
10030 define_one_arm_cp_reg(cpu, &cbar);
10031 }
10032 }
10033
10034 if (arm_feature(env, ARM_FEATURE_VBAR)) {
10035 static const ARMCPRegInfo vbar_cp_reginfo[] = {
10036 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
10037 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
10038 .access = PL1_RW, .writefn = vbar_write,
10039 .accessfn = access_nv1,
10040 .fgt = FGT_VBAR_EL1,
10041 .nv2_redirect_offset = 0x250 | NV2_REDIR_NV1,
10042 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
10043 offsetof(CPUARMState, cp15.vbar_ns) },
10044 .resetvalue = 0 },
10045 };
10046 define_arm_cp_regs(cpu, vbar_cp_reginfo);
10047 }
10048
10049 /* Generic registers whose values depend on the implementation */
10050 {
10051 ARMCPRegInfo sctlr = {
10052 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
10053 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
10054 .access = PL1_RW, .accessfn = access_tvm_trvm,
10055 .fgt = FGT_SCTLR_EL1,
10056 .nv2_redirect_offset = 0x110 | NV2_REDIR_NV1,
10057 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
10058 offsetof(CPUARMState, cp15.sctlr_ns) },
10059 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
10060 .raw_writefn = raw_write,
10061 };
10062 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
10063 /*
10064 * Normally we would always end the TB on an SCTLR write, but Linux
10065 * arch/arm/mach-pxa/sleep.S expects two instructions following
10066 * an MMU enable to execute from cache. Imitate this behaviour.
10067 */
10068 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
10069 }
10070 define_one_arm_cp_reg(cpu, &sctlr);
10071
10072 if (arm_feature(env, ARM_FEATURE_PMSA) &&
10073 arm_feature(env, ARM_FEATURE_V8)) {
10074 ARMCPRegInfo vsctlr = {
10075 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
10076 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
10077 .access = PL2_RW, .resetvalue = 0x0,
10078 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
10079 };
10080 define_one_arm_cp_reg(cpu, &vsctlr);
10081 }
10082 }
10083
10084 if (cpu_isar_feature(aa64_lor, cpu)) {
10085 define_arm_cp_regs(cpu, lor_reginfo);
10086 }
10087 if (cpu_isar_feature(aa64_pan, cpu)) {
10088 define_one_arm_cp_reg(cpu, &pan_reginfo);
10089 }
10090 #ifndef CONFIG_USER_ONLY
10091 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
10092 define_arm_cp_regs(cpu, ats1e1_reginfo);
10093 }
10094 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
10095 define_arm_cp_regs(cpu, ats1cp_reginfo);
10096 }
10097 #endif
10098 if (cpu_isar_feature(aa64_uao, cpu)) {
10099 define_one_arm_cp_reg(cpu, &uao_reginfo);
10100 }
10101
10102 if (cpu_isar_feature(aa64_dit, cpu)) {
10103 define_one_arm_cp_reg(cpu, &dit_reginfo);
10104 }
10105 if (cpu_isar_feature(aa64_ssbs, cpu)) {
10106 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
10107 }
10108 if (cpu_isar_feature(any_ras, cpu)) {
10109 define_arm_cp_regs(cpu, minimal_ras_reginfo);
10110 }
10111
10112 if (cpu_isar_feature(aa64_vh, cpu) ||
10113 cpu_isar_feature(aa64_debugv8p2, cpu)) {
10114 define_one_arm_cp_reg(cpu, &contextidr_el2);
10115 }
10116 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
10117 define_arm_cp_regs(cpu, vhe_reginfo);
10118 }
10119
10120 if (cpu_isar_feature(aa64_sve, cpu)) {
10121 define_arm_cp_regs(cpu, zcr_reginfo);
10122 }
10123
10124 if (cpu_isar_feature(aa64_hcx, cpu)) {
10125 define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
10126 }
10127
10128 #ifdef TARGET_AARCH64
10129 if (cpu_isar_feature(aa64_sme, cpu)) {
10130 define_arm_cp_regs(cpu, sme_reginfo);
10131 }
10132 if (cpu_isar_feature(aa64_pauth, cpu)) {
10133 define_arm_cp_regs(cpu, pauth_reginfo);
10134 }
10135 if (cpu_isar_feature(aa64_rndr, cpu)) {
10136 define_arm_cp_regs(cpu, rndr_reginfo);
10137 }
10138 if (cpu_isar_feature(aa64_tlbirange, cpu)) {
10139 define_arm_cp_regs(cpu, tlbirange_reginfo);
10140 }
10141 if (cpu_isar_feature(aa64_tlbios, cpu)) {
10142 define_arm_cp_regs(cpu, tlbios_reginfo);
10143 }
10144 /* Data Cache clean instructions up to PoP */
10145 if (cpu_isar_feature(aa64_dcpop, cpu)) {
10146 define_one_arm_cp_reg(cpu, dcpop_reg);
10147
10148 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
10149 define_one_arm_cp_reg(cpu, dcpodp_reg);
10150 }
10151 }
10152
10153 /*
10154 * If full MTE is enabled, add all of the system registers.
10155 * If only "instructions available at EL0" are enabled,
10156 * then define only a RAZ/WI version of PSTATE.TCO.
10157 */
10158 if (cpu_isar_feature(aa64_mte, cpu)) {
10159 ARMCPRegInfo gmid_reginfo = {
10160 .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
10161 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
10162 .access = PL1_R, .accessfn = access_aa64_tid5,
10163 .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
10164 };
10165 define_one_arm_cp_reg(cpu, &gmid_reginfo);
10166 define_arm_cp_regs(cpu, mte_reginfo);
10167 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
10168 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
10169 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
10170 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
10171 }
10172
10173 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
10174 define_arm_cp_regs(cpu, scxtnum_reginfo);
10175 }
10176
10177 if (cpu_isar_feature(aa64_fgt, cpu)) {
10178 define_arm_cp_regs(cpu, fgt_reginfo);
10179 }
10180
10181 if (cpu_isar_feature(aa64_rme, cpu)) {
10182 define_arm_cp_regs(cpu, rme_reginfo);
10183 if (cpu_isar_feature(aa64_mte, cpu)) {
10184 define_arm_cp_regs(cpu, rme_mte_reginfo);
10185 }
10186 }
10187
10188 if (cpu_isar_feature(aa64_nv2, cpu)) {
10189 define_arm_cp_regs(cpu, nv2_reginfo);
10190 }
10191
10192 if (cpu_isar_feature(aa64_nmi, cpu)) {
10193 define_arm_cp_regs(cpu, nmi_reginfo);
10194 }
10195 #endif
10196
10197 if (cpu_isar_feature(any_predinv, cpu)) {
10198 define_arm_cp_regs(cpu, predinv_reginfo);
10199 }
10200
10201 if (cpu_isar_feature(any_ccidx, cpu)) {
10202 define_arm_cp_regs(cpu, ccsidr2_reginfo);
10203 }
10204
10205 #ifndef CONFIG_USER_ONLY
10206 /*
10207 * Register redirections and aliases must be done last,
10208 * after the registers from the other extensions have been defined.
10209 */
10210 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
10211 define_arm_vh_e2h_redirects_aliases(cpu);
10212 }
10213 #endif
10214 }
10215
10216 /*
10217 * Private utility function for define_one_arm_cp_reg_with_opaque():
10218 * add a single reginfo struct to the hash table.
10219 */
add_cpreg_to_hashtable(ARMCPU * cpu,const ARMCPRegInfo * r,void * opaque,CPState state,CPSecureState secstate,int crm,int opc1,int opc2,const char * name)10220 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
10221 void *opaque, CPState state,
10222 CPSecureState secstate,
10223 int crm, int opc1, int opc2,
10224 const char *name)
10225 {
10226 CPUARMState *env = &cpu->env;
10227 uint32_t key;
10228 ARMCPRegInfo *r2;
10229 bool is64 = r->type & ARM_CP_64BIT;
10230 bool ns = secstate & ARM_CP_SECSTATE_NS;
10231 int cp = r->cp;
10232 size_t name_len;
10233 bool make_const;
10234
10235 switch (state) {
10236 case ARM_CP_STATE_AA32:
10237 /* We assume it is a cp15 register if the .cp field is left unset. */
10238 if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
10239 cp = 15;
10240 }
10241 key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
10242 break;
10243 case ARM_CP_STATE_AA64:
10244 /*
10245 * To allow abbreviation of ARMCPRegInfo definitions, we treat
10246 * cp == 0 as equivalent to the value for "standard guest-visible
10247 * sysreg". STATE_BOTH definitions are also always "standard sysreg"
10248 * in their AArch64 view (the .cp value may be non-zero for the
10249 * benefit of the AArch32 view).
10250 */
10251 if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
10252 cp = CP_REG_ARM64_SYSREG_CP;
10253 }
10254 key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
10255 break;
10256 default:
10257 g_assert_not_reached();
10258 }
10259
10260 /* Overriding of an existing definition must be explicitly requested. */
10261 if (!(r->type & ARM_CP_OVERRIDE)) {
10262 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
10263 if (oldreg) {
10264 assert(oldreg->type & ARM_CP_OVERRIDE);
10265 }
10266 }
10267
10268 /*
10269 * Eliminate registers that are not present because the EL is missing.
10270 * Doing this here makes it easier to put all registers for a given
10271 * feature into the same ARMCPRegInfo array and define them all at once.
10272 */
10273 make_const = false;
10274 if (arm_feature(env, ARM_FEATURE_EL3)) {
10275 /*
10276 * An EL2 register without EL2 but with EL3 is (usually) RES0.
10277 * See rule RJFFP in section D1.1.3 of DDI0487H.a.
10278 */
10279 int min_el = ctz32(r->access) / 2;
10280 if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
10281 if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
10282 return;
10283 }
10284 make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
10285 }
10286 } else {
10287 CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
10288 ? PL2_RW : PL1_RW);
10289 if ((r->access & max_el) == 0) {
10290 return;
10291 }
10292 }
10293
10294 /* Combine cpreg and name into one allocation. */
10295 name_len = strlen(name) + 1;
10296 r2 = g_malloc(sizeof(*r2) + name_len);
10297 *r2 = *r;
10298 r2->name = memcpy(r2 + 1, name, name_len);
10299
10300 /*
10301 * Update fields to match the instantiation, overwiting wildcards
10302 * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
10303 */
10304 r2->cp = cp;
10305 r2->crm = crm;
10306 r2->opc1 = opc1;
10307 r2->opc2 = opc2;
10308 r2->state = state;
10309 r2->secure = secstate;
10310 if (opaque) {
10311 r2->opaque = opaque;
10312 }
10313
10314 if (make_const) {
10315 /* This should not have been a very special register to begin. */
10316 int old_special = r2->type & ARM_CP_SPECIAL_MASK;
10317 assert(old_special == 0 || old_special == ARM_CP_NOP);
10318 /*
10319 * Set the special function to CONST, retaining the other flags.
10320 * This is important for e.g. ARM_CP_SVE so that we still
10321 * take the SVE trap if CPTR_EL3.EZ == 0.
10322 */
10323 r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
10324 /*
10325 * Usually, these registers become RES0, but there are a few
10326 * special cases like VPIDR_EL2 which have a constant non-zero
10327 * value with writes ignored.
10328 */
10329 if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
10330 r2->resetvalue = 0;
10331 }
10332 /*
10333 * ARM_CP_CONST has precedence, so removing the callbacks and
10334 * offsets are not strictly necessary, but it is potentially
10335 * less confusing to debug later.
10336 */
10337 r2->readfn = NULL;
10338 r2->writefn = NULL;
10339 r2->raw_readfn = NULL;
10340 r2->raw_writefn = NULL;
10341 r2->resetfn = NULL;
10342 r2->fieldoffset = 0;
10343 r2->bank_fieldoffsets[0] = 0;
10344 r2->bank_fieldoffsets[1] = 0;
10345 } else {
10346 bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
10347
10348 if (isbanked) {
10349 /*
10350 * Register is banked (using both entries in array).
10351 * Overwriting fieldoffset as the array is only used to define
10352 * banked registers but later only fieldoffset is used.
10353 */
10354 r2->fieldoffset = r->bank_fieldoffsets[ns];
10355 }
10356 if (state == ARM_CP_STATE_AA32) {
10357 if (isbanked) {
10358 /*
10359 * If the register is banked then we don't need to migrate or
10360 * reset the 32-bit instance in certain cases:
10361 *
10362 * 1) If the register has both 32-bit and 64-bit instances
10363 * then we can count on the 64-bit instance taking care
10364 * of the non-secure bank.
10365 * 2) If ARMv8 is enabled then we can count on a 64-bit
10366 * version taking care of the secure bank. This requires
10367 * that separate 32 and 64-bit definitions are provided.
10368 */
10369 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
10370 (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
10371 r2->type |= ARM_CP_ALIAS;
10372 }
10373 } else if ((secstate != r->secure) && !ns) {
10374 /*
10375 * The register is not banked so we only want to allow
10376 * migration of the non-secure instance.
10377 */
10378 r2->type |= ARM_CP_ALIAS;
10379 }
10380
10381 if (HOST_BIG_ENDIAN &&
10382 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
10383 r2->fieldoffset += sizeof(uint32_t);
10384 }
10385 }
10386 }
10387
10388 /*
10389 * By convention, for wildcarded registers only the first
10390 * entry is used for migration; the others are marked as
10391 * ALIAS so we don't try to transfer the register
10392 * multiple times. Special registers (ie NOP/WFI) are
10393 * never migratable and not even raw-accessible.
10394 */
10395 if (r2->type & ARM_CP_SPECIAL_MASK) {
10396 r2->type |= ARM_CP_NO_RAW;
10397 }
10398 if (((r->crm == CP_ANY) && crm != 0) ||
10399 ((r->opc1 == CP_ANY) && opc1 != 0) ||
10400 ((r->opc2 == CP_ANY) && opc2 != 0)) {
10401 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
10402 }
10403
10404 /*
10405 * Check that raw accesses are either forbidden or handled. Note that
10406 * we can't assert this earlier because the setup of fieldoffset for
10407 * banked registers has to be done first.
10408 */
10409 if (!(r2->type & ARM_CP_NO_RAW)) {
10410 assert(!raw_accessors_invalid(r2));
10411 }
10412
10413 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
10414 }
10415
10416
define_one_arm_cp_reg_with_opaque(ARMCPU * cpu,const ARMCPRegInfo * r,void * opaque)10417 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
10418 const ARMCPRegInfo *r, void *opaque)
10419 {
10420 /*
10421 * Define implementations of coprocessor registers.
10422 * We store these in a hashtable because typically
10423 * there are less than 150 registers in a space which
10424 * is 16*16*16*8*8 = 262144 in size.
10425 * Wildcarding is supported for the crm, opc1 and opc2 fields.
10426 * If a register is defined twice then the second definition is
10427 * used, so this can be used to define some generic registers and
10428 * then override them with implementation specific variations.
10429 * At least one of the original and the second definition should
10430 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
10431 * against accidental use.
10432 *
10433 * The state field defines whether the register is to be
10434 * visible in the AArch32 or AArch64 execution state. If the
10435 * state is set to ARM_CP_STATE_BOTH then we synthesise a
10436 * reginfo structure for the AArch32 view, which sees the lower
10437 * 32 bits of the 64 bit register.
10438 *
10439 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
10440 * be wildcarded. AArch64 registers are always considered to be 64
10441 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
10442 * the register, if any.
10443 */
10444 int crm, opc1, opc2;
10445 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
10446 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
10447 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
10448 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
10449 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
10450 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
10451 CPState state;
10452
10453 /* 64 bit registers have only CRm and Opc1 fields */
10454 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
10455 /* op0 only exists in the AArch64 encodings */
10456 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
10457 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
10458 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
10459 /*
10460 * This API is only for Arm's system coprocessors (14 and 15) or
10461 * (M-profile or v7A-and-earlier only) for implementation defined
10462 * coprocessors in the range 0..7. Our decode assumes this, since
10463 * 8..13 can be used for other insns including VFP and Neon. See
10464 * valid_cp() in translate.c. Assert here that we haven't tried
10465 * to use an invalid coprocessor number.
10466 */
10467 switch (r->state) {
10468 case ARM_CP_STATE_BOTH:
10469 /* 0 has a special meaning, but otherwise the same rules as AA32. */
10470 if (r->cp == 0) {
10471 break;
10472 }
10473 /* fall through */
10474 case ARM_CP_STATE_AA32:
10475 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
10476 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
10477 assert(r->cp >= 14 && r->cp <= 15);
10478 } else {
10479 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
10480 }
10481 break;
10482 case ARM_CP_STATE_AA64:
10483 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
10484 break;
10485 default:
10486 g_assert_not_reached();
10487 }
10488 /*
10489 * The AArch64 pseudocode CheckSystemAccess() specifies that op1
10490 * encodes a minimum access level for the register. We roll this
10491 * runtime check into our general permission check code, so check
10492 * here that the reginfo's specified permissions are strict enough
10493 * to encompass the generic architectural permission check.
10494 */
10495 if (r->state != ARM_CP_STATE_AA32) {
10496 CPAccessRights mask;
10497 switch (r->opc1) {
10498 case 0:
10499 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
10500 mask = PL0U_R | PL1_RW;
10501 break;
10502 case 1: case 2:
10503 /* min_EL EL1 */
10504 mask = PL1_RW;
10505 break;
10506 case 3:
10507 /* min_EL EL0 */
10508 mask = PL0_RW;
10509 break;
10510 case 4:
10511 case 5:
10512 /* min_EL EL2 */
10513 mask = PL2_RW;
10514 break;
10515 case 6:
10516 /* min_EL EL3 */
10517 mask = PL3_RW;
10518 break;
10519 case 7:
10520 /* min_EL EL1, secure mode only (we don't check the latter) */
10521 mask = PL1_RW;
10522 break;
10523 default:
10524 /* broken reginfo with out-of-range opc1 */
10525 g_assert_not_reached();
10526 }
10527 /* assert our permissions are not too lax (stricter is fine) */
10528 assert((r->access & ~mask) == 0);
10529 }
10530
10531 /*
10532 * Check that the register definition has enough info to handle
10533 * reads and writes if they are permitted.
10534 */
10535 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
10536 if (r->access & PL3_R) {
10537 assert((r->fieldoffset ||
10538 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
10539 r->readfn);
10540 }
10541 if (r->access & PL3_W) {
10542 assert((r->fieldoffset ||
10543 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
10544 r->writefn);
10545 }
10546 }
10547
10548 for (crm = crmmin; crm <= crmmax; crm++) {
10549 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
10550 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
10551 for (state = ARM_CP_STATE_AA32;
10552 state <= ARM_CP_STATE_AA64; state++) {
10553 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
10554 continue;
10555 }
10556 if (state == ARM_CP_STATE_AA32) {
10557 /*
10558 * Under AArch32 CP registers can be common
10559 * (same for secure and non-secure world) or banked.
10560 */
10561 char *name;
10562
10563 switch (r->secure) {
10564 case ARM_CP_SECSTATE_S:
10565 case ARM_CP_SECSTATE_NS:
10566 add_cpreg_to_hashtable(cpu, r, opaque, state,
10567 r->secure, crm, opc1, opc2,
10568 r->name);
10569 break;
10570 case ARM_CP_SECSTATE_BOTH:
10571 name = g_strdup_printf("%s_S", r->name);
10572 add_cpreg_to_hashtable(cpu, r, opaque, state,
10573 ARM_CP_SECSTATE_S,
10574 crm, opc1, opc2, name);
10575 g_free(name);
10576 add_cpreg_to_hashtable(cpu, r, opaque, state,
10577 ARM_CP_SECSTATE_NS,
10578 crm, opc1, opc2, r->name);
10579 break;
10580 default:
10581 g_assert_not_reached();
10582 }
10583 } else {
10584 /*
10585 * AArch64 registers get mapped to non-secure instance
10586 * of AArch32
10587 */
10588 add_cpreg_to_hashtable(cpu, r, opaque, state,
10589 ARM_CP_SECSTATE_NS,
10590 crm, opc1, opc2, r->name);
10591 }
10592 }
10593 }
10594 }
10595 }
10596 }
10597
10598 /* Define a whole list of registers */
define_arm_cp_regs_with_opaque_len(ARMCPU * cpu,const ARMCPRegInfo * regs,void * opaque,size_t len)10599 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
10600 void *opaque, size_t len)
10601 {
10602 size_t i;
10603 for (i = 0; i < len; ++i) {
10604 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
10605 }
10606 }
10607
10608 /*
10609 * Modify ARMCPRegInfo for access from userspace.
10610 *
10611 * This is a data driven modification directed by
10612 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
10613 * user-space cannot alter any values and dynamic values pertaining to
10614 * execution state are hidden from user space view anyway.
10615 */
modify_arm_cp_regs_with_len(ARMCPRegInfo * regs,size_t regs_len,const ARMCPRegUserSpaceInfo * mods,size_t mods_len)10616 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
10617 const ARMCPRegUserSpaceInfo *mods,
10618 size_t mods_len)
10619 {
10620 for (size_t mi = 0; mi < mods_len; ++mi) {
10621 const ARMCPRegUserSpaceInfo *m = mods + mi;
10622 GPatternSpec *pat = NULL;
10623
10624 if (m->is_glob) {
10625 pat = g_pattern_spec_new(m->name);
10626 }
10627 for (size_t ri = 0; ri < regs_len; ++ri) {
10628 ARMCPRegInfo *r = regs + ri;
10629
10630 if (pat && g_pattern_match_string(pat, r->name)) {
10631 r->type = ARM_CP_CONST;
10632 r->access = PL0U_R;
10633 r->resetvalue = 0;
10634 /* continue */
10635 } else if (strcmp(r->name, m->name) == 0) {
10636 r->type = ARM_CP_CONST;
10637 r->access = PL0U_R;
10638 r->resetvalue &= m->exported_bits;
10639 r->resetvalue |= m->fixed_bits;
10640 break;
10641 }
10642 }
10643 if (pat) {
10644 g_pattern_spec_free(pat);
10645 }
10646 }
10647 }
10648
get_arm_cp_reginfo(GHashTable * cpregs,uint32_t encoded_cp)10649 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
10650 {
10651 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
10652 }
10653
arm_cp_write_ignore(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)10654 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
10655 uint64_t value)
10656 {
10657 /* Helper coprocessor write function for write-ignore registers */
10658 }
10659
arm_cp_read_zero(CPUARMState * env,const ARMCPRegInfo * ri)10660 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
10661 {
10662 /* Helper coprocessor write function for read-as-zero registers */
10663 return 0;
10664 }
10665
arm_cp_reset_ignore(CPUARMState * env,const ARMCPRegInfo * opaque)10666 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
10667 {
10668 /* Helper coprocessor reset function for do-nothing-on-reset registers */
10669 }
10670
bad_mode_switch(CPUARMState * env,int mode,CPSRWriteType write_type)10671 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
10672 {
10673 /*
10674 * Return true if it is not valid for us to switch to
10675 * this CPU mode (ie all the UNPREDICTABLE cases in
10676 * the ARM ARM CPSRWriteByInstr pseudocode).
10677 */
10678
10679 /* Changes to or from Hyp via MSR and CPS are illegal. */
10680 if (write_type == CPSRWriteByInstr &&
10681 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
10682 mode == ARM_CPU_MODE_HYP)) {
10683 return 1;
10684 }
10685
10686 switch (mode) {
10687 case ARM_CPU_MODE_USR:
10688 return 0;
10689 case ARM_CPU_MODE_SYS:
10690 case ARM_CPU_MODE_SVC:
10691 case ARM_CPU_MODE_ABT:
10692 case ARM_CPU_MODE_UND:
10693 case ARM_CPU_MODE_IRQ:
10694 case ARM_CPU_MODE_FIQ:
10695 /*
10696 * Note that we don't implement the IMPDEF NSACR.RFR which in v7
10697 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
10698 */
10699 /*
10700 * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
10701 * and CPS are treated as illegal mode changes.
10702 */
10703 if (write_type == CPSRWriteByInstr &&
10704 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
10705 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10706 return 1;
10707 }
10708 return 0;
10709 case ARM_CPU_MODE_HYP:
10710 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
10711 case ARM_CPU_MODE_MON:
10712 return arm_current_el(env) < 3;
10713 default:
10714 return 1;
10715 }
10716 }
10717
cpsr_read(CPUARMState * env)10718 uint32_t cpsr_read(CPUARMState *env)
10719 {
10720 int ZF;
10721 ZF = (env->ZF == 0);
10722 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
10723 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
10724 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
10725 | ((env->condexec_bits & 0xfc) << 8)
10726 | (env->GE << 16) | (env->daif & CPSR_AIF);
10727 }
10728
cpsr_write(CPUARMState * env,uint32_t val,uint32_t mask,CPSRWriteType write_type)10729 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
10730 CPSRWriteType write_type)
10731 {
10732 uint32_t changed_daif;
10733 bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
10734 (mask & (CPSR_M | CPSR_E | CPSR_IL));
10735
10736 if (mask & CPSR_NZCV) {
10737 env->ZF = (~val) & CPSR_Z;
10738 env->NF = val;
10739 env->CF = (val >> 29) & 1;
10740 env->VF = (val << 3) & 0x80000000;
10741 }
10742 if (mask & CPSR_Q) {
10743 env->QF = ((val & CPSR_Q) != 0);
10744 }
10745 if (mask & CPSR_T) {
10746 env->thumb = ((val & CPSR_T) != 0);
10747 }
10748 if (mask & CPSR_IT_0_1) {
10749 env->condexec_bits &= ~3;
10750 env->condexec_bits |= (val >> 25) & 3;
10751 }
10752 if (mask & CPSR_IT_2_7) {
10753 env->condexec_bits &= 3;
10754 env->condexec_bits |= (val >> 8) & 0xfc;
10755 }
10756 if (mask & CPSR_GE) {
10757 env->GE = (val >> 16) & 0xf;
10758 }
10759
10760 /*
10761 * In a V7 implementation that includes the security extensions but does
10762 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
10763 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
10764 * bits respectively.
10765 *
10766 * In a V8 implementation, it is permitted for privileged software to
10767 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
10768 */
10769 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
10770 arm_feature(env, ARM_FEATURE_EL3) &&
10771 !arm_feature(env, ARM_FEATURE_EL2) &&
10772 !arm_is_secure(env)) {
10773
10774 changed_daif = (env->daif ^ val) & mask;
10775
10776 if (changed_daif & CPSR_A) {
10777 /*
10778 * Check to see if we are allowed to change the masking of async
10779 * abort exceptions from a non-secure state.
10780 */
10781 if (!(env->cp15.scr_el3 & SCR_AW)) {
10782 qemu_log_mask(LOG_GUEST_ERROR,
10783 "Ignoring attempt to switch CPSR_A flag from "
10784 "non-secure world with SCR.AW bit clear\n");
10785 mask &= ~CPSR_A;
10786 }
10787 }
10788
10789 if (changed_daif & CPSR_F) {
10790 /*
10791 * Check to see if we are allowed to change the masking of FIQ
10792 * exceptions from a non-secure state.
10793 */
10794 if (!(env->cp15.scr_el3 & SCR_FW)) {
10795 qemu_log_mask(LOG_GUEST_ERROR,
10796 "Ignoring attempt to switch CPSR_F flag from "
10797 "non-secure world with SCR.FW bit clear\n");
10798 mask &= ~CPSR_F;
10799 }
10800
10801 /*
10802 * Check whether non-maskable FIQ (NMFI) support is enabled.
10803 * If this bit is set software is not allowed to mask
10804 * FIQs, but is allowed to set CPSR_F to 0.
10805 */
10806 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
10807 (val & CPSR_F)) {
10808 qemu_log_mask(LOG_GUEST_ERROR,
10809 "Ignoring attempt to enable CPSR_F flag "
10810 "(non-maskable FIQ [NMFI] support enabled)\n");
10811 mask &= ~CPSR_F;
10812 }
10813 }
10814 }
10815
10816 env->daif &= ~(CPSR_AIF & mask);
10817 env->daif |= val & CPSR_AIF & mask;
10818
10819 if (write_type != CPSRWriteRaw &&
10820 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
10821 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
10822 /*
10823 * Note that we can only get here in USR mode if this is a
10824 * gdb stub write; for this case we follow the architectural
10825 * behaviour for guest writes in USR mode of ignoring an attempt
10826 * to switch mode. (Those are caught by translate.c for writes
10827 * triggered by guest instructions.)
10828 */
10829 mask &= ~CPSR_M;
10830 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
10831 /*
10832 * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
10833 * v7, and has defined behaviour in v8:
10834 * + leave CPSR.M untouched
10835 * + allow changes to the other CPSR fields
10836 * + set PSTATE.IL
10837 * For user changes via the GDB stub, we don't set PSTATE.IL,
10838 * as this would be unnecessarily harsh for a user error.
10839 */
10840 mask &= ~CPSR_M;
10841 if (write_type != CPSRWriteByGDBStub &&
10842 arm_feature(env, ARM_FEATURE_V8)) {
10843 mask |= CPSR_IL;
10844 val |= CPSR_IL;
10845 }
10846 qemu_log_mask(LOG_GUEST_ERROR,
10847 "Illegal AArch32 mode switch attempt from %s to %s\n",
10848 aarch32_mode_name(env->uncached_cpsr),
10849 aarch32_mode_name(val));
10850 } else {
10851 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
10852 write_type == CPSRWriteExceptionReturn ?
10853 "Exception return from AArch32" :
10854 "AArch32 mode switch from",
10855 aarch32_mode_name(env->uncached_cpsr),
10856 aarch32_mode_name(val), env->regs[15]);
10857 switch_mode(env, val & CPSR_M);
10858 }
10859 }
10860 mask &= ~CACHED_CPSR_BITS;
10861 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
10862 if (tcg_enabled() && rebuild_hflags) {
10863 arm_rebuild_hflags(env);
10864 }
10865 }
10866
10867 #ifdef CONFIG_USER_ONLY
10868
switch_mode(CPUARMState * env,int mode)10869 static void switch_mode(CPUARMState *env, int mode)
10870 {
10871 ARMCPU *cpu = env_archcpu(env);
10872
10873 if (mode != ARM_CPU_MODE_USR) {
10874 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
10875 }
10876 }
10877
arm_phys_excp_target_el(CPUState * cs,uint32_t excp_idx,uint32_t cur_el,bool secure)10878 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10879 uint32_t cur_el, bool secure)
10880 {
10881 return 1;
10882 }
10883
aarch64_sync_64_to_32(CPUARMState * env)10884 void aarch64_sync_64_to_32(CPUARMState *env)
10885 {
10886 g_assert_not_reached();
10887 }
10888
10889 #else
10890
switch_mode(CPUARMState * env,int mode)10891 static void switch_mode(CPUARMState *env, int mode)
10892 {
10893 int old_mode;
10894 int i;
10895
10896 old_mode = env->uncached_cpsr & CPSR_M;
10897 if (mode == old_mode) {
10898 return;
10899 }
10900
10901 if (old_mode == ARM_CPU_MODE_FIQ) {
10902 memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
10903 memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
10904 } else if (mode == ARM_CPU_MODE_FIQ) {
10905 memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
10906 memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
10907 }
10908
10909 i = bank_number(old_mode);
10910 env->banked_r13[i] = env->regs[13];
10911 env->banked_spsr[i] = env->spsr;
10912
10913 i = bank_number(mode);
10914 env->regs[13] = env->banked_r13[i];
10915 env->spsr = env->banked_spsr[i];
10916
10917 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
10918 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
10919 }
10920
10921 /*
10922 * Physical Interrupt Target EL Lookup Table
10923 *
10924 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
10925 *
10926 * The below multi-dimensional table is used for looking up the target
10927 * exception level given numerous condition criteria. Specifically, the
10928 * target EL is based on SCR and HCR routing controls as well as the
10929 * currently executing EL and secure state.
10930 *
10931 * Dimensions:
10932 * target_el_table[2][2][2][2][2][4]
10933 * | | | | | +--- Current EL
10934 * | | | | +------ Non-secure(0)/Secure(1)
10935 * | | | +--------- HCR mask override
10936 * | | +------------ SCR exec state control
10937 * | +--------------- SCR mask override
10938 * +------------------ 32-bit(0)/64-bit(1) EL3
10939 *
10940 * The table values are as such:
10941 * 0-3 = EL0-EL3
10942 * -1 = Cannot occur
10943 *
10944 * The ARM ARM target EL table includes entries indicating that an "exception
10945 * is not taken". The two cases where this is applicable are:
10946 * 1) An exception is taken from EL3 but the SCR does not have the exception
10947 * routed to EL3.
10948 * 2) An exception is taken from EL2 but the HCR does not have the exception
10949 * routed to EL2.
10950 * In these two cases, the below table contain a target of EL1. This value is
10951 * returned as it is expected that the consumer of the table data will check
10952 * for "target EL >= current EL" to ensure the exception is not taken.
10953 *
10954 * SCR HCR
10955 * 64 EA AMO From
10956 * BIT IRQ IMO Non-secure Secure
10957 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
10958 */
10959 static const int8_t target_el_table[2][2][2][2][2][4] = {
10960 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
10961 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
10962 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
10963 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
10964 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
10965 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
10966 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
10967 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
10968 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
10969 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
10970 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
10971 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
10972 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
10973 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
10974 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
10975 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
10976 };
10977
10978 /*
10979 * Determine the target EL for physical exceptions
10980 */
arm_phys_excp_target_el(CPUState * cs,uint32_t excp_idx,uint32_t cur_el,bool secure)10981 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10982 uint32_t cur_el, bool secure)
10983 {
10984 CPUARMState *env = cpu_env(cs);
10985 bool rw;
10986 bool scr;
10987 bool hcr;
10988 int target_el;
10989 /* Is the highest EL AArch64? */
10990 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
10991 uint64_t hcr_el2;
10992
10993 if (arm_feature(env, ARM_FEATURE_EL3)) {
10994 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
10995 } else {
10996 /*
10997 * Either EL2 is the highest EL (and so the EL2 register width
10998 * is given by is64); or there is no EL2 or EL3, in which case
10999 * the value of 'rw' does not affect the table lookup anyway.
11000 */
11001 rw = is64;
11002 }
11003
11004 hcr_el2 = arm_hcr_el2_eff(env);
11005 switch (excp_idx) {
11006 case EXCP_IRQ:
11007 case EXCP_NMI:
11008 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
11009 hcr = hcr_el2 & HCR_IMO;
11010 break;
11011 case EXCP_FIQ:
11012 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
11013 hcr = hcr_el2 & HCR_FMO;
11014 break;
11015 default:
11016 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
11017 hcr = hcr_el2 & HCR_AMO;
11018 break;
11019 };
11020
11021 /*
11022 * For these purposes, TGE and AMO/IMO/FMO both force the
11023 * interrupt to EL2. Fold TGE into the bit extracted above.
11024 */
11025 hcr |= (hcr_el2 & HCR_TGE) != 0;
11026
11027 /* Perform a table-lookup for the target EL given the current state */
11028 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
11029
11030 assert(target_el > 0);
11031
11032 return target_el;
11033 }
11034
arm_log_exception(CPUState * cs)11035 void arm_log_exception(CPUState *cs)
11036 {
11037 int idx = cs->exception_index;
11038
11039 if (qemu_loglevel_mask(CPU_LOG_INT)) {
11040 const char *exc = NULL;
11041 static const char * const excnames[] = {
11042 [EXCP_UDEF] = "Undefined Instruction",
11043 [EXCP_SWI] = "SVC",
11044 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
11045 [EXCP_DATA_ABORT] = "Data Abort",
11046 [EXCP_IRQ] = "IRQ",
11047 [EXCP_FIQ] = "FIQ",
11048 [EXCP_BKPT] = "Breakpoint",
11049 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
11050 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
11051 [EXCP_HVC] = "Hypervisor Call",
11052 [EXCP_HYP_TRAP] = "Hypervisor Trap",
11053 [EXCP_SMC] = "Secure Monitor Call",
11054 [EXCP_VIRQ] = "Virtual IRQ",
11055 [EXCP_VFIQ] = "Virtual FIQ",
11056 [EXCP_SEMIHOST] = "Semihosting call",
11057 [EXCP_NOCP] = "v7M NOCP UsageFault",
11058 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
11059 [EXCP_STKOF] = "v8M STKOF UsageFault",
11060 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
11061 [EXCP_LSERR] = "v8M LSERR UsageFault",
11062 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
11063 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
11064 [EXCP_VSERR] = "Virtual SERR",
11065 [EXCP_GPC] = "Granule Protection Check",
11066 [EXCP_NMI] = "NMI",
11067 [EXCP_VINMI] = "Virtual IRQ NMI",
11068 [EXCP_VFNMI] = "Virtual FIQ NMI",
11069 [EXCP_MON_TRAP] = "Monitor Trap",
11070 };
11071
11072 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
11073 exc = excnames[idx];
11074 }
11075 if (!exc) {
11076 exc = "unknown";
11077 }
11078 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
11079 idx, exc, cs->cpu_index);
11080 }
11081 }
11082
11083 /*
11084 * Function used to synchronize QEMU's AArch64 register set with AArch32
11085 * register set. This is necessary when switching between AArch32 and AArch64
11086 * execution state.
11087 */
aarch64_sync_32_to_64(CPUARMState * env)11088 void aarch64_sync_32_to_64(CPUARMState *env)
11089 {
11090 int i;
11091 uint32_t mode = env->uncached_cpsr & CPSR_M;
11092
11093 /* We can blanket copy R[0:7] to X[0:7] */
11094 for (i = 0; i < 8; i++) {
11095 env->xregs[i] = env->regs[i];
11096 }
11097
11098 /*
11099 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
11100 * Otherwise, they come from the banked user regs.
11101 */
11102 if (mode == ARM_CPU_MODE_FIQ) {
11103 for (i = 8; i < 13; i++) {
11104 env->xregs[i] = env->usr_regs[i - 8];
11105 }
11106 } else {
11107 for (i = 8; i < 13; i++) {
11108 env->xregs[i] = env->regs[i];
11109 }
11110 }
11111
11112 /*
11113 * Registers x13-x23 are the various mode SP and FP registers. Registers
11114 * r13 and r14 are only copied if we are in that mode, otherwise we copy
11115 * from the mode banked register.
11116 */
11117 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
11118 env->xregs[13] = env->regs[13];
11119 env->xregs[14] = env->regs[14];
11120 } else {
11121 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
11122 /* HYP is an exception in that it is copied from r14 */
11123 if (mode == ARM_CPU_MODE_HYP) {
11124 env->xregs[14] = env->regs[14];
11125 } else {
11126 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
11127 }
11128 }
11129
11130 if (mode == ARM_CPU_MODE_HYP) {
11131 env->xregs[15] = env->regs[13];
11132 } else {
11133 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
11134 }
11135
11136 if (mode == ARM_CPU_MODE_IRQ) {
11137 env->xregs[16] = env->regs[14];
11138 env->xregs[17] = env->regs[13];
11139 } else {
11140 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
11141 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
11142 }
11143
11144 if (mode == ARM_CPU_MODE_SVC) {
11145 env->xregs[18] = env->regs[14];
11146 env->xregs[19] = env->regs[13];
11147 } else {
11148 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
11149 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
11150 }
11151
11152 if (mode == ARM_CPU_MODE_ABT) {
11153 env->xregs[20] = env->regs[14];
11154 env->xregs[21] = env->regs[13];
11155 } else {
11156 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
11157 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
11158 }
11159
11160 if (mode == ARM_CPU_MODE_UND) {
11161 env->xregs[22] = env->regs[14];
11162 env->xregs[23] = env->regs[13];
11163 } else {
11164 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
11165 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
11166 }
11167
11168 /*
11169 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
11170 * mode, then we can copy from r8-r14. Otherwise, we copy from the
11171 * FIQ bank for r8-r14.
11172 */
11173 if (mode == ARM_CPU_MODE_FIQ) {
11174 for (i = 24; i < 31; i++) {
11175 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
11176 }
11177 } else {
11178 for (i = 24; i < 29; i++) {
11179 env->xregs[i] = env->fiq_regs[i - 24];
11180 }
11181 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
11182 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
11183 }
11184
11185 env->pc = env->regs[15];
11186 }
11187
11188 /*
11189 * Function used to synchronize QEMU's AArch32 register set with AArch64
11190 * register set. This is necessary when switching between AArch32 and AArch64
11191 * execution state.
11192 */
aarch64_sync_64_to_32(CPUARMState * env)11193 void aarch64_sync_64_to_32(CPUARMState *env)
11194 {
11195 int i;
11196 uint32_t mode = env->uncached_cpsr & CPSR_M;
11197
11198 /* We can blanket copy X[0:7] to R[0:7] */
11199 for (i = 0; i < 8; i++) {
11200 env->regs[i] = env->xregs[i];
11201 }
11202
11203 /*
11204 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
11205 * Otherwise, we copy x8-x12 into the banked user regs.
11206 */
11207 if (mode == ARM_CPU_MODE_FIQ) {
11208 for (i = 8; i < 13; i++) {
11209 env->usr_regs[i - 8] = env->xregs[i];
11210 }
11211 } else {
11212 for (i = 8; i < 13; i++) {
11213 env->regs[i] = env->xregs[i];
11214 }
11215 }
11216
11217 /*
11218 * Registers r13 & r14 depend on the current mode.
11219 * If we are in a given mode, we copy the corresponding x registers to r13
11220 * and r14. Otherwise, we copy the x register to the banked r13 and r14
11221 * for the mode.
11222 */
11223 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
11224 env->regs[13] = env->xregs[13];
11225 env->regs[14] = env->xregs[14];
11226 } else {
11227 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
11228
11229 /*
11230 * HYP is an exception in that it does not have its own banked r14 but
11231 * shares the USR r14
11232 */
11233 if (mode == ARM_CPU_MODE_HYP) {
11234 env->regs[14] = env->xregs[14];
11235 } else {
11236 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
11237 }
11238 }
11239
11240 if (mode == ARM_CPU_MODE_HYP) {
11241 env->regs[13] = env->xregs[15];
11242 } else {
11243 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
11244 }
11245
11246 if (mode == ARM_CPU_MODE_IRQ) {
11247 env->regs[14] = env->xregs[16];
11248 env->regs[13] = env->xregs[17];
11249 } else {
11250 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
11251 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
11252 }
11253
11254 if (mode == ARM_CPU_MODE_SVC) {
11255 env->regs[14] = env->xregs[18];
11256 env->regs[13] = env->xregs[19];
11257 } else {
11258 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
11259 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
11260 }
11261
11262 if (mode == ARM_CPU_MODE_ABT) {
11263 env->regs[14] = env->xregs[20];
11264 env->regs[13] = env->xregs[21];
11265 } else {
11266 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
11267 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
11268 }
11269
11270 if (mode == ARM_CPU_MODE_UND) {
11271 env->regs[14] = env->xregs[22];
11272 env->regs[13] = env->xregs[23];
11273 } else {
11274 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
11275 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
11276 }
11277
11278 /*
11279 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
11280 * mode, then we can copy to r8-r14. Otherwise, we copy to the
11281 * FIQ bank for r8-r14.
11282 */
11283 if (mode == ARM_CPU_MODE_FIQ) {
11284 for (i = 24; i < 31; i++) {
11285 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
11286 }
11287 } else {
11288 for (i = 24; i < 29; i++) {
11289 env->fiq_regs[i - 24] = env->xregs[i];
11290 }
11291 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
11292 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
11293 }
11294
11295 env->regs[15] = env->pc;
11296 }
11297
take_aarch32_exception(CPUARMState * env,int new_mode,uint32_t mask,uint32_t offset,uint32_t newpc)11298 static void take_aarch32_exception(CPUARMState *env, int new_mode,
11299 uint32_t mask, uint32_t offset,
11300 uint32_t newpc)
11301 {
11302 int new_el;
11303
11304 /* Change the CPU state so as to actually take the exception. */
11305 switch_mode(env, new_mode);
11306
11307 /*
11308 * For exceptions taken to AArch32 we must clear the SS bit in both
11309 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
11310 */
11311 env->pstate &= ~PSTATE_SS;
11312 env->spsr = cpsr_read(env);
11313 /* Clear IT bits. */
11314 env->condexec_bits = 0;
11315 /* Switch to the new mode, and to the correct instruction set. */
11316 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
11317
11318 /* This must be after mode switching. */
11319 new_el = arm_current_el(env);
11320
11321 /* Set new mode endianness */
11322 env->uncached_cpsr &= ~CPSR_E;
11323 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
11324 env->uncached_cpsr |= CPSR_E;
11325 }
11326 /* J and IL must always be cleared for exception entry */
11327 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
11328 env->daif |= mask;
11329
11330 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
11331 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
11332 env->uncached_cpsr |= CPSR_SSBS;
11333 } else {
11334 env->uncached_cpsr &= ~CPSR_SSBS;
11335 }
11336 }
11337
11338 if (new_mode == ARM_CPU_MODE_HYP) {
11339 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
11340 env->elr_el[2] = env->regs[15];
11341 } else {
11342 /* CPSR.PAN is normally preserved preserved unless... */
11343 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
11344 switch (new_el) {
11345 case 3:
11346 if (!arm_is_secure_below_el3(env)) {
11347 /* ... the target is EL3, from non-secure state. */
11348 env->uncached_cpsr &= ~CPSR_PAN;
11349 break;
11350 }
11351 /* ... the target is EL3, from secure state ... */
11352 /* fall through */
11353 case 1:
11354 /* ... the target is EL1 and SCTLR.SPAN is 0. */
11355 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
11356 env->uncached_cpsr |= CPSR_PAN;
11357 }
11358 break;
11359 }
11360 }
11361 /*
11362 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
11363 * and we should just guard the thumb mode on V4
11364 */
11365 if (arm_feature(env, ARM_FEATURE_V4T)) {
11366 env->thumb =
11367 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
11368 }
11369 env->regs[14] = env->regs[15] + offset;
11370 }
11371 env->regs[15] = newpc;
11372
11373 if (tcg_enabled()) {
11374 arm_rebuild_hflags(env);
11375 }
11376 }
11377
arm_cpu_do_interrupt_aarch32_hyp(CPUState * cs)11378 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
11379 {
11380 /*
11381 * Handle exception entry to Hyp mode; this is sufficiently
11382 * different to entry to other AArch32 modes that we handle it
11383 * separately here.
11384 *
11385 * The vector table entry used is always the 0x14 Hyp mode entry point,
11386 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
11387 * The offset applied to the preferred return address is always zero
11388 * (see DDI0487C.a section G1.12.3).
11389 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
11390 */
11391 uint32_t addr, mask;
11392 ARMCPU *cpu = ARM_CPU(cs);
11393 CPUARMState *env = &cpu->env;
11394
11395 switch (cs->exception_index) {
11396 case EXCP_UDEF:
11397 addr = 0x04;
11398 break;
11399 case EXCP_SWI:
11400 addr = 0x08;
11401 break;
11402 case EXCP_BKPT:
11403 /* Fall through to prefetch abort. */
11404 case EXCP_PREFETCH_ABORT:
11405 env->cp15.ifar_s = env->exception.vaddress;
11406 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
11407 (uint32_t)env->exception.vaddress);
11408 addr = 0x0c;
11409 break;
11410 case EXCP_DATA_ABORT:
11411 env->cp15.dfar_s = env->exception.vaddress;
11412 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
11413 (uint32_t)env->exception.vaddress);
11414 addr = 0x10;
11415 break;
11416 case EXCP_IRQ:
11417 addr = 0x18;
11418 break;
11419 case EXCP_FIQ:
11420 addr = 0x1c;
11421 break;
11422 case EXCP_HVC:
11423 addr = 0x08;
11424 break;
11425 case EXCP_HYP_TRAP:
11426 addr = 0x14;
11427 break;
11428 default:
11429 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11430 }
11431
11432 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
11433 if (!arm_feature(env, ARM_FEATURE_V8)) {
11434 /*
11435 * QEMU syndrome values are v8-style. v7 has the IL bit
11436 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
11437 * If this is a v7 CPU, squash the IL bit in those cases.
11438 */
11439 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
11440 (cs->exception_index == EXCP_DATA_ABORT &&
11441 !(env->exception.syndrome & ARM_EL_ISV)) ||
11442 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
11443 env->exception.syndrome &= ~ARM_EL_IL;
11444 }
11445 }
11446 env->cp15.esr_el[2] = env->exception.syndrome;
11447 }
11448
11449 if (arm_current_el(env) != 2 && addr < 0x14) {
11450 addr = 0x14;
11451 }
11452
11453 mask = 0;
11454 if (!(env->cp15.scr_el3 & SCR_EA)) {
11455 mask |= CPSR_A;
11456 }
11457 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
11458 mask |= CPSR_I;
11459 }
11460 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
11461 mask |= CPSR_F;
11462 }
11463
11464 addr += env->cp15.hvbar;
11465
11466 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
11467 }
11468
arm_cpu_do_interrupt_aarch32(CPUState * cs)11469 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
11470 {
11471 ARMCPU *cpu = ARM_CPU(cs);
11472 CPUARMState *env = &cpu->env;
11473 uint32_t addr;
11474 uint32_t mask;
11475 int new_mode;
11476 uint32_t offset;
11477 uint32_t moe;
11478
11479 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
11480 switch (syn_get_ec(env->exception.syndrome)) {
11481 case EC_BREAKPOINT:
11482 case EC_BREAKPOINT_SAME_EL:
11483 moe = 1;
11484 break;
11485 case EC_WATCHPOINT:
11486 case EC_WATCHPOINT_SAME_EL:
11487 moe = 10;
11488 break;
11489 case EC_AA32_BKPT:
11490 moe = 3;
11491 break;
11492 case EC_VECTORCATCH:
11493 moe = 5;
11494 break;
11495 default:
11496 moe = 0;
11497 break;
11498 }
11499
11500 if (moe) {
11501 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
11502 }
11503
11504 if (env->exception.target_el == 2) {
11505 /* Debug exceptions are reported differently on AArch32 */
11506 switch (syn_get_ec(env->exception.syndrome)) {
11507 case EC_BREAKPOINT:
11508 case EC_BREAKPOINT_SAME_EL:
11509 case EC_AA32_BKPT:
11510 case EC_VECTORCATCH:
11511 env->exception.syndrome = syn_insn_abort(arm_current_el(env) == 2,
11512 0, 0, 0x22);
11513 break;
11514 case EC_WATCHPOINT:
11515 env->exception.syndrome = syn_set_ec(env->exception.syndrome,
11516 EC_DATAABORT);
11517 break;
11518 case EC_WATCHPOINT_SAME_EL:
11519 env->exception.syndrome = syn_set_ec(env->exception.syndrome,
11520 EC_DATAABORT_SAME_EL);
11521 break;
11522 }
11523 arm_cpu_do_interrupt_aarch32_hyp(cs);
11524 return;
11525 }
11526
11527 switch (cs->exception_index) {
11528 case EXCP_UDEF:
11529 new_mode = ARM_CPU_MODE_UND;
11530 addr = 0x04;
11531 mask = CPSR_I;
11532 if (env->thumb) {
11533 offset = 2;
11534 } else {
11535 offset = 4;
11536 }
11537 break;
11538 case EXCP_SWI:
11539 new_mode = ARM_CPU_MODE_SVC;
11540 addr = 0x08;
11541 mask = CPSR_I;
11542 /* The PC already points to the next instruction. */
11543 offset = 0;
11544 break;
11545 case EXCP_BKPT:
11546 /* Fall through to prefetch abort. */
11547 case EXCP_PREFETCH_ABORT:
11548 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
11549 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
11550 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
11551 env->exception.fsr, (uint32_t)env->exception.vaddress);
11552 new_mode = ARM_CPU_MODE_ABT;
11553 addr = 0x0c;
11554 mask = CPSR_A | CPSR_I;
11555 offset = 4;
11556 break;
11557 case EXCP_DATA_ABORT:
11558 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
11559 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
11560 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
11561 env->exception.fsr,
11562 (uint32_t)env->exception.vaddress);
11563 new_mode = ARM_CPU_MODE_ABT;
11564 addr = 0x10;
11565 mask = CPSR_A | CPSR_I;
11566 offset = 8;
11567 break;
11568 case EXCP_IRQ:
11569 new_mode = ARM_CPU_MODE_IRQ;
11570 addr = 0x18;
11571 /* Disable IRQ and imprecise data aborts. */
11572 mask = CPSR_A | CPSR_I;
11573 offset = 4;
11574 if (env->cp15.scr_el3 & SCR_IRQ) {
11575 /* IRQ routed to monitor mode */
11576 new_mode = ARM_CPU_MODE_MON;
11577 mask |= CPSR_F;
11578 }
11579 break;
11580 case EXCP_FIQ:
11581 new_mode = ARM_CPU_MODE_FIQ;
11582 addr = 0x1c;
11583 /* Disable FIQ, IRQ and imprecise data aborts. */
11584 mask = CPSR_A | CPSR_I | CPSR_F;
11585 if (env->cp15.scr_el3 & SCR_FIQ) {
11586 /* FIQ routed to monitor mode */
11587 new_mode = ARM_CPU_MODE_MON;
11588 }
11589 offset = 4;
11590 break;
11591 case EXCP_VIRQ:
11592 new_mode = ARM_CPU_MODE_IRQ;
11593 addr = 0x18;
11594 /* Disable IRQ and imprecise data aborts. */
11595 mask = CPSR_A | CPSR_I;
11596 offset = 4;
11597 break;
11598 case EXCP_VFIQ:
11599 new_mode = ARM_CPU_MODE_FIQ;
11600 addr = 0x1c;
11601 /* Disable FIQ, IRQ and imprecise data aborts. */
11602 mask = CPSR_A | CPSR_I | CPSR_F;
11603 offset = 4;
11604 break;
11605 case EXCP_VSERR:
11606 {
11607 /*
11608 * Note that this is reported as a data abort, but the DFAR
11609 * has an UNKNOWN value. Construct the SError syndrome from
11610 * AET and ExT fields.
11611 */
11612 ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
11613
11614 if (extended_addresses_enabled(env)) {
11615 env->exception.fsr = arm_fi_to_lfsc(&fi);
11616 } else {
11617 env->exception.fsr = arm_fi_to_sfsc(&fi);
11618 }
11619 env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
11620 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
11621 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
11622 env->exception.fsr);
11623
11624 new_mode = ARM_CPU_MODE_ABT;
11625 addr = 0x10;
11626 mask = CPSR_A | CPSR_I;
11627 offset = 8;
11628 }
11629 break;
11630 case EXCP_SMC:
11631 new_mode = ARM_CPU_MODE_MON;
11632 addr = 0x08;
11633 mask = CPSR_A | CPSR_I | CPSR_F;
11634 offset = 0;
11635 break;
11636 case EXCP_MON_TRAP:
11637 new_mode = ARM_CPU_MODE_MON;
11638 addr = 0x04;
11639 mask = CPSR_A | CPSR_I | CPSR_F;
11640 if (env->thumb) {
11641 offset = 2;
11642 } else {
11643 offset = 4;
11644 }
11645 break;
11646 default:
11647 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11648 return; /* Never happens. Keep compiler happy. */
11649 }
11650
11651 if (new_mode == ARM_CPU_MODE_MON) {
11652 addr += env->cp15.mvbar;
11653 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
11654 /* High vectors. When enabled, base address cannot be remapped. */
11655 addr += 0xffff0000;
11656 } else {
11657 /*
11658 * ARM v7 architectures provide a vector base address register to remap
11659 * the interrupt vector table.
11660 * This register is only followed in non-monitor mode, and is banked.
11661 * Note: only bits 31:5 are valid.
11662 */
11663 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
11664 }
11665
11666 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
11667 env->cp15.scr_el3 &= ~SCR_NS;
11668 }
11669
11670 take_aarch32_exception(env, new_mode, mask, offset, addr);
11671 }
11672
aarch64_regnum(CPUARMState * env,int aarch32_reg)11673 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
11674 {
11675 /*
11676 * Return the register number of the AArch64 view of the AArch32
11677 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
11678 * be that of the AArch32 mode the exception came from.
11679 */
11680 int mode = env->uncached_cpsr & CPSR_M;
11681
11682 switch (aarch32_reg) {
11683 case 0 ... 7:
11684 return aarch32_reg;
11685 case 8 ... 12:
11686 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
11687 case 13:
11688 switch (mode) {
11689 case ARM_CPU_MODE_USR:
11690 case ARM_CPU_MODE_SYS:
11691 return 13;
11692 case ARM_CPU_MODE_HYP:
11693 return 15;
11694 case ARM_CPU_MODE_IRQ:
11695 return 17;
11696 case ARM_CPU_MODE_SVC:
11697 return 19;
11698 case ARM_CPU_MODE_ABT:
11699 return 21;
11700 case ARM_CPU_MODE_UND:
11701 return 23;
11702 case ARM_CPU_MODE_FIQ:
11703 return 29;
11704 default:
11705 g_assert_not_reached();
11706 }
11707 case 14:
11708 switch (mode) {
11709 case ARM_CPU_MODE_USR:
11710 case ARM_CPU_MODE_SYS:
11711 case ARM_CPU_MODE_HYP:
11712 return 14;
11713 case ARM_CPU_MODE_IRQ:
11714 return 16;
11715 case ARM_CPU_MODE_SVC:
11716 return 18;
11717 case ARM_CPU_MODE_ABT:
11718 return 20;
11719 case ARM_CPU_MODE_UND:
11720 return 22;
11721 case ARM_CPU_MODE_FIQ:
11722 return 30;
11723 default:
11724 g_assert_not_reached();
11725 }
11726 case 15:
11727 return 31;
11728 default:
11729 g_assert_not_reached();
11730 }
11731 }
11732
cpsr_read_for_spsr_elx(CPUARMState * env)11733 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
11734 {
11735 uint32_t ret = cpsr_read(env);
11736
11737 /* Move DIT to the correct location for SPSR_ELx */
11738 if (ret & CPSR_DIT) {
11739 ret &= ~CPSR_DIT;
11740 ret |= PSTATE_DIT;
11741 }
11742 /* Merge PSTATE.SS into SPSR_ELx */
11743 ret |= env->pstate & PSTATE_SS;
11744
11745 return ret;
11746 }
11747
syndrome_is_sync_extabt(uint32_t syndrome)11748 static bool syndrome_is_sync_extabt(uint32_t syndrome)
11749 {
11750 /* Return true if this syndrome value is a synchronous external abort */
11751 switch (syn_get_ec(syndrome)) {
11752 case EC_INSNABORT:
11753 case EC_INSNABORT_SAME_EL:
11754 case EC_DATAABORT:
11755 case EC_DATAABORT_SAME_EL:
11756 /* Look at fault status code for all the synchronous ext abort cases */
11757 switch (syndrome & 0x3f) {
11758 case 0x10:
11759 case 0x13:
11760 case 0x14:
11761 case 0x15:
11762 case 0x16:
11763 case 0x17:
11764 return true;
11765 default:
11766 return false;
11767 }
11768 default:
11769 return false;
11770 }
11771 }
11772
11773 /* Handle exception entry to a target EL which is using AArch64 */
arm_cpu_do_interrupt_aarch64(CPUState * cs)11774 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
11775 {
11776 ARMCPU *cpu = ARM_CPU(cs);
11777 CPUARMState *env = &cpu->env;
11778 unsigned int new_el = env->exception.target_el;
11779 target_ulong addr = env->cp15.vbar_el[new_el];
11780 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
11781 unsigned int old_mode;
11782 unsigned int cur_el = arm_current_el(env);
11783 int rt;
11784
11785 if (tcg_enabled()) {
11786 /*
11787 * Note that new_el can never be 0. If cur_el is 0, then
11788 * el0_a64 is is_a64(), else el0_a64 is ignored.
11789 */
11790 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
11791 }
11792
11793 if (cur_el < new_el) {
11794 /*
11795 * Entry vector offset depends on whether the implemented EL
11796 * immediately lower than the target level is using AArch32 or AArch64
11797 */
11798 bool is_aa64;
11799 uint64_t hcr;
11800
11801 switch (new_el) {
11802 case 3:
11803 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
11804 break;
11805 case 2:
11806 hcr = arm_hcr_el2_eff(env);
11807 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11808 is_aa64 = (hcr & HCR_RW) != 0;
11809 break;
11810 }
11811 /* fall through */
11812 case 1:
11813 is_aa64 = is_a64(env);
11814 break;
11815 default:
11816 g_assert_not_reached();
11817 }
11818
11819 if (is_aa64) {
11820 addr += 0x400;
11821 } else {
11822 addr += 0x600;
11823 }
11824 } else if (pstate_read(env) & PSTATE_SP) {
11825 addr += 0x200;
11826 }
11827
11828 switch (cs->exception_index) {
11829 case EXCP_GPC:
11830 qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
11831 env->cp15.mfar_el3);
11832 /* fall through */
11833 case EXCP_PREFETCH_ABORT:
11834 case EXCP_DATA_ABORT:
11835 /*
11836 * FEAT_DoubleFault allows synchronous external aborts taken to EL3
11837 * to be taken to the SError vector entrypoint.
11838 */
11839 if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
11840 syndrome_is_sync_extabt(env->exception.syndrome)) {
11841 addr += 0x180;
11842 }
11843 env->cp15.far_el[new_el] = env->exception.vaddress;
11844 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
11845 env->cp15.far_el[new_el]);
11846 /* fall through */
11847 case EXCP_BKPT:
11848 case EXCP_UDEF:
11849 case EXCP_SWI:
11850 case EXCP_HVC:
11851 case EXCP_HYP_TRAP:
11852 case EXCP_SMC:
11853 switch (syn_get_ec(env->exception.syndrome)) {
11854 case EC_ADVSIMDFPACCESSTRAP:
11855 /*
11856 * QEMU internal FP/SIMD syndromes from AArch32 include the
11857 * TA and coproc fields which are only exposed if the exception
11858 * is taken to AArch32 Hyp mode. Mask them out to get a valid
11859 * AArch64 format syndrome.
11860 */
11861 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
11862 break;
11863 case EC_CP14RTTRAP:
11864 case EC_CP15RTTRAP:
11865 case EC_CP14DTTRAP:
11866 /*
11867 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
11868 * the raw register field from the insn; when taking this to
11869 * AArch64 we must convert it to the AArch64 view of the register
11870 * number. Notice that we read a 4-bit AArch32 register number and
11871 * write back a 5-bit AArch64 one.
11872 */
11873 rt = extract32(env->exception.syndrome, 5, 4);
11874 rt = aarch64_regnum(env, rt);
11875 env->exception.syndrome = deposit32(env->exception.syndrome,
11876 5, 5, rt);
11877 break;
11878 case EC_CP15RRTTRAP:
11879 case EC_CP14RRTTRAP:
11880 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
11881 rt = extract32(env->exception.syndrome, 5, 4);
11882 rt = aarch64_regnum(env, rt);
11883 env->exception.syndrome = deposit32(env->exception.syndrome,
11884 5, 5, rt);
11885 rt = extract32(env->exception.syndrome, 10, 4);
11886 rt = aarch64_regnum(env, rt);
11887 env->exception.syndrome = deposit32(env->exception.syndrome,
11888 10, 5, rt);
11889 break;
11890 }
11891 env->cp15.esr_el[new_el] = env->exception.syndrome;
11892 break;
11893 case EXCP_IRQ:
11894 case EXCP_VIRQ:
11895 case EXCP_NMI:
11896 case EXCP_VINMI:
11897 addr += 0x80;
11898 break;
11899 case EXCP_FIQ:
11900 case EXCP_VFIQ:
11901 case EXCP_VFNMI:
11902 addr += 0x100;
11903 break;
11904 case EXCP_VSERR:
11905 addr += 0x180;
11906 /* Construct the SError syndrome from IDS and ISS fields. */
11907 env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
11908 env->cp15.esr_el[new_el] = env->exception.syndrome;
11909 break;
11910 default:
11911 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11912 }
11913
11914 if (is_a64(env)) {
11915 old_mode = pstate_read(env);
11916 aarch64_save_sp(env, arm_current_el(env));
11917 env->elr_el[new_el] = env->pc;
11918
11919 if (cur_el == 1 && new_el == 1) {
11920 uint64_t hcr = arm_hcr_el2_eff(env);
11921 if ((hcr & (HCR_NV | HCR_NV1 | HCR_NV2)) == HCR_NV ||
11922 (hcr & (HCR_NV | HCR_NV2)) == (HCR_NV | HCR_NV2)) {
11923 /*
11924 * FEAT_NV, FEAT_NV2 may need to report EL2 in the SPSR
11925 * by setting M[3:2] to 0b10.
11926 * If NV2 is disabled, change SPSR when NV,NV1 == 1,0 (I_ZJRNN)
11927 * If NV2 is enabled, change SPSR when NV is 1 (I_DBTLM)
11928 */
11929 old_mode = deposit32(old_mode, 2, 2, 2);
11930 }
11931 }
11932 } else {
11933 old_mode = cpsr_read_for_spsr_elx(env);
11934 env->elr_el[new_el] = env->regs[15];
11935
11936 aarch64_sync_32_to_64(env);
11937
11938 env->condexec_bits = 0;
11939 }
11940 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
11941
11942 qemu_log_mask(CPU_LOG_INT, "...with SPSR 0x%x\n", old_mode);
11943 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
11944 env->elr_el[new_el]);
11945
11946 if (cpu_isar_feature(aa64_pan, cpu)) {
11947 /* The value of PSTATE.PAN is normally preserved, except when ... */
11948 new_mode |= old_mode & PSTATE_PAN;
11949 switch (new_el) {
11950 case 2:
11951 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
11952 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
11953 != (HCR_E2H | HCR_TGE)) {
11954 break;
11955 }
11956 /* fall through */
11957 case 1:
11958 /* ... the target is EL1 ... */
11959 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
11960 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
11961 new_mode |= PSTATE_PAN;
11962 }
11963 break;
11964 }
11965 }
11966 if (cpu_isar_feature(aa64_mte, cpu)) {
11967 new_mode |= PSTATE_TCO;
11968 }
11969
11970 if (cpu_isar_feature(aa64_ssbs, cpu)) {
11971 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
11972 new_mode |= PSTATE_SSBS;
11973 } else {
11974 new_mode &= ~PSTATE_SSBS;
11975 }
11976 }
11977
11978 if (cpu_isar_feature(aa64_nmi, cpu)) {
11979 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPINTMASK)) {
11980 new_mode |= PSTATE_ALLINT;
11981 } else {
11982 new_mode &= ~PSTATE_ALLINT;
11983 }
11984 }
11985
11986 pstate_write(env, PSTATE_DAIF | new_mode);
11987 env->aarch64 = true;
11988 aarch64_restore_sp(env, new_el);
11989
11990 if (tcg_enabled()) {
11991 helper_rebuild_hflags_a64(env, new_el);
11992 }
11993
11994 env->pc = addr;
11995
11996 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
11997 new_el, env->pc, pstate_read(env));
11998 }
11999
12000 /*
12001 * Do semihosting call and set the appropriate return value. All the
12002 * permission and validity checks have been done at translate time.
12003 *
12004 * We only see semihosting exceptions in TCG only as they are not
12005 * trapped to the hypervisor in KVM.
12006 */
12007 #ifdef CONFIG_TCG
tcg_handle_semihosting(CPUState * cs)12008 static void tcg_handle_semihosting(CPUState *cs)
12009 {
12010 ARMCPU *cpu = ARM_CPU(cs);
12011 CPUARMState *env = &cpu->env;
12012
12013 if (is_a64(env)) {
12014 qemu_log_mask(CPU_LOG_INT,
12015 "...handling as semihosting call 0x%" PRIx64 "\n",
12016 env->xregs[0]);
12017 do_common_semihosting(cs);
12018 env->pc += 4;
12019 } else {
12020 qemu_log_mask(CPU_LOG_INT,
12021 "...handling as semihosting call 0x%x\n",
12022 env->regs[0]);
12023 do_common_semihosting(cs);
12024 env->regs[15] += env->thumb ? 2 : 4;
12025 }
12026 }
12027 #endif
12028
12029 /*
12030 * Handle a CPU exception for A and R profile CPUs.
12031 * Do any appropriate logging, handle PSCI calls, and then hand off
12032 * to the AArch64-entry or AArch32-entry function depending on the
12033 * target exception level's register width.
12034 *
12035 * Note: this is used for both TCG (as the do_interrupt tcg op),
12036 * and KVM to re-inject guest debug exceptions, and to
12037 * inject a Synchronous-External-Abort.
12038 */
arm_cpu_do_interrupt(CPUState * cs)12039 void arm_cpu_do_interrupt(CPUState *cs)
12040 {
12041 ARMCPU *cpu = ARM_CPU(cs);
12042 CPUARMState *env = &cpu->env;
12043 unsigned int new_el = env->exception.target_el;
12044
12045 assert(!arm_feature(env, ARM_FEATURE_M));
12046
12047 arm_log_exception(cs);
12048 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
12049 new_el);
12050 if (qemu_loglevel_mask(CPU_LOG_INT)
12051 && !excp_is_internal(cs->exception_index)) {
12052 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
12053 syn_get_ec(env->exception.syndrome),
12054 env->exception.syndrome);
12055 }
12056
12057 if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
12058 arm_handle_psci_call(cpu);
12059 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
12060 return;
12061 }
12062
12063 /*
12064 * Semihosting semantics depend on the register width of the code
12065 * that caused the exception, not the target exception level, so
12066 * must be handled here.
12067 */
12068 #ifdef CONFIG_TCG
12069 if (cs->exception_index == EXCP_SEMIHOST) {
12070 tcg_handle_semihosting(cs);
12071 return;
12072 }
12073 #endif
12074
12075 /*
12076 * Hooks may change global state so BQL should be held, also the
12077 * BQL needs to be held for any modification of
12078 * cs->interrupt_request.
12079 */
12080 g_assert(bql_locked());
12081
12082 arm_call_pre_el_change_hook(cpu);
12083
12084 assert(!excp_is_internal(cs->exception_index));
12085 if (arm_el_is_aa64(env, new_el)) {
12086 arm_cpu_do_interrupt_aarch64(cs);
12087 } else {
12088 arm_cpu_do_interrupt_aarch32(cs);
12089 }
12090
12091 arm_call_el_change_hook(cpu);
12092
12093 if (!kvm_enabled()) {
12094 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
12095 }
12096 }
12097 #endif /* !CONFIG_USER_ONLY */
12098
arm_sctlr(CPUARMState * env,int el)12099 uint64_t arm_sctlr(CPUARMState *env, int el)
12100 {
12101 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0 or EL3&0 */
12102 if (el == 0) {
12103 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
12104 switch (mmu_idx) {
12105 case ARMMMUIdx_E20_0:
12106 el = 2;
12107 break;
12108 case ARMMMUIdx_E30_0:
12109 el = 3;
12110 break;
12111 default:
12112 el = 1;
12113 break;
12114 }
12115 }
12116 return env->cp15.sctlr_el[el];
12117 }
12118
aa64_va_parameter_tbi(uint64_t tcr,ARMMMUIdx mmu_idx)12119 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
12120 {
12121 if (regime_has_2_ranges(mmu_idx)) {
12122 return extract64(tcr, 37, 2);
12123 } else if (regime_is_stage2(mmu_idx)) {
12124 return 0; /* VTCR_EL2 */
12125 } else {
12126 /* Replicate the single TBI bit so we always have 2 bits. */
12127 return extract32(tcr, 20, 1) * 3;
12128 }
12129 }
12130
aa64_va_parameter_tbid(uint64_t tcr,ARMMMUIdx mmu_idx)12131 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
12132 {
12133 if (regime_has_2_ranges(mmu_idx)) {
12134 return extract64(tcr, 51, 2);
12135 } else if (regime_is_stage2(mmu_idx)) {
12136 return 0; /* VTCR_EL2 */
12137 } else {
12138 /* Replicate the single TBID bit so we always have 2 bits. */
12139 return extract32(tcr, 29, 1) * 3;
12140 }
12141 }
12142
aa64_va_parameter_tcma(uint64_t tcr,ARMMMUIdx mmu_idx)12143 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
12144 {
12145 if (regime_has_2_ranges(mmu_idx)) {
12146 return extract64(tcr, 57, 2);
12147 } else {
12148 /* Replicate the single TCMA bit so we always have 2 bits. */
12149 return extract32(tcr, 30, 1) * 3;
12150 }
12151 }
12152
tg0_to_gran_size(int tg)12153 static ARMGranuleSize tg0_to_gran_size(int tg)
12154 {
12155 switch (tg) {
12156 case 0:
12157 return Gran4K;
12158 case 1:
12159 return Gran64K;
12160 case 2:
12161 return Gran16K;
12162 default:
12163 return GranInvalid;
12164 }
12165 }
12166
tg1_to_gran_size(int tg)12167 static ARMGranuleSize tg1_to_gran_size(int tg)
12168 {
12169 switch (tg) {
12170 case 1:
12171 return Gran16K;
12172 case 2:
12173 return Gran4K;
12174 case 3:
12175 return Gran64K;
12176 default:
12177 return GranInvalid;
12178 }
12179 }
12180
have4k(ARMCPU * cpu,bool stage2)12181 static inline bool have4k(ARMCPU *cpu, bool stage2)
12182 {
12183 return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
12184 : cpu_isar_feature(aa64_tgran4, cpu);
12185 }
12186
have16k(ARMCPU * cpu,bool stage2)12187 static inline bool have16k(ARMCPU *cpu, bool stage2)
12188 {
12189 return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
12190 : cpu_isar_feature(aa64_tgran16, cpu);
12191 }
12192
have64k(ARMCPU * cpu,bool stage2)12193 static inline bool have64k(ARMCPU *cpu, bool stage2)
12194 {
12195 return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
12196 : cpu_isar_feature(aa64_tgran64, cpu);
12197 }
12198
sanitize_gran_size(ARMCPU * cpu,ARMGranuleSize gran,bool stage2)12199 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
12200 bool stage2)
12201 {
12202 switch (gran) {
12203 case Gran4K:
12204 if (have4k(cpu, stage2)) {
12205 return gran;
12206 }
12207 break;
12208 case Gran16K:
12209 if (have16k(cpu, stage2)) {
12210 return gran;
12211 }
12212 break;
12213 case Gran64K:
12214 if (have64k(cpu, stage2)) {
12215 return gran;
12216 }
12217 break;
12218 case GranInvalid:
12219 break;
12220 }
12221 /*
12222 * If the guest selects a granule size that isn't implemented,
12223 * the architecture requires that we behave as if it selected one
12224 * that is (with an IMPDEF choice of which one to pick). We choose
12225 * to implement the smallest supported granule size.
12226 */
12227 if (have4k(cpu, stage2)) {
12228 return Gran4K;
12229 }
12230 if (have16k(cpu, stage2)) {
12231 return Gran16K;
12232 }
12233 assert(have64k(cpu, stage2));
12234 return Gran64K;
12235 }
12236
aa64_va_parameters(CPUARMState * env,uint64_t va,ARMMMUIdx mmu_idx,bool data,bool el1_is_aa32)12237 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
12238 ARMMMUIdx mmu_idx, bool data,
12239 bool el1_is_aa32)
12240 {
12241 uint64_t tcr = regime_tcr(env, mmu_idx);
12242 bool epd, hpd, tsz_oob, ds, ha, hd;
12243 int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
12244 ARMGranuleSize gran;
12245 ARMCPU *cpu = env_archcpu(env);
12246 bool stage2 = regime_is_stage2(mmu_idx);
12247
12248 if (!regime_has_2_ranges(mmu_idx)) {
12249 select = 0;
12250 tsz = extract32(tcr, 0, 6);
12251 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
12252 if (stage2) {
12253 /* VTCR_EL2 */
12254 hpd = false;
12255 } else {
12256 hpd = extract32(tcr, 24, 1);
12257 }
12258 epd = false;
12259 sh = extract32(tcr, 12, 2);
12260 ps = extract32(tcr, 16, 3);
12261 ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
12262 hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
12263 ds = extract64(tcr, 32, 1);
12264 } else {
12265 bool e0pd;
12266
12267 /*
12268 * Bit 55 is always between the two regions, and is canonical for
12269 * determining if address tagging is enabled.
12270 */
12271 select = extract64(va, 55, 1);
12272 if (!select) {
12273 tsz = extract32(tcr, 0, 6);
12274 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
12275 epd = extract32(tcr, 7, 1);
12276 sh = extract32(tcr, 12, 2);
12277 hpd = extract64(tcr, 41, 1);
12278 e0pd = extract64(tcr, 55, 1);
12279 } else {
12280 tsz = extract32(tcr, 16, 6);
12281 gran = tg1_to_gran_size(extract32(tcr, 30, 2));
12282 epd = extract32(tcr, 23, 1);
12283 sh = extract32(tcr, 28, 2);
12284 hpd = extract64(tcr, 42, 1);
12285 e0pd = extract64(tcr, 56, 1);
12286 }
12287 ps = extract64(tcr, 32, 3);
12288 ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
12289 hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
12290 ds = extract64(tcr, 59, 1);
12291
12292 if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
12293 regime_is_user(env, mmu_idx)) {
12294 epd = true;
12295 }
12296 }
12297
12298 gran = sanitize_gran_size(cpu, gran, stage2);
12299
12300 if (cpu_isar_feature(aa64_st, cpu)) {
12301 max_tsz = 48 - (gran == Gran64K);
12302 } else {
12303 max_tsz = 39;
12304 }
12305
12306 /*
12307 * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
12308 * adjust the effective value of DS, as documented.
12309 */
12310 min_tsz = 16;
12311 if (gran == Gran64K) {
12312 if (cpu_isar_feature(aa64_lva, cpu)) {
12313 min_tsz = 12;
12314 }
12315 ds = false;
12316 } else if (ds) {
12317 if (regime_is_stage2(mmu_idx)) {
12318 if (gran == Gran16K) {
12319 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
12320 } else {
12321 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
12322 }
12323 } else {
12324 if (gran == Gran16K) {
12325 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
12326 } else {
12327 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
12328 }
12329 }
12330 if (ds) {
12331 min_tsz = 12;
12332 }
12333 }
12334
12335 if (stage2 && el1_is_aa32) {
12336 /*
12337 * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
12338 * are loosened: a configured IPA of 40 bits is permitted even if
12339 * the implemented PA is less than that (and so a 40 bit IPA would
12340 * fault for an AArch64 EL1). See R_DTLMN.
12341 */
12342 min_tsz = MIN(min_tsz, 24);
12343 }
12344
12345 if (tsz > max_tsz) {
12346 tsz = max_tsz;
12347 tsz_oob = true;
12348 } else if (tsz < min_tsz) {
12349 tsz = min_tsz;
12350 tsz_oob = true;
12351 } else {
12352 tsz_oob = false;
12353 }
12354
12355 /* Present TBI as a composite with TBID. */
12356 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
12357 if (!data) {
12358 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
12359 }
12360 tbi = (tbi >> select) & 1;
12361
12362 return (ARMVAParameters) {
12363 .tsz = tsz,
12364 .ps = ps,
12365 .sh = sh,
12366 .select = select,
12367 .tbi = tbi,
12368 .epd = epd,
12369 .hpd = hpd,
12370 .tsz_oob = tsz_oob,
12371 .ds = ds,
12372 .ha = ha,
12373 .hd = ha && hd,
12374 .gran = gran,
12375 };
12376 }
12377
12378 /*
12379 * Note that signed overflow is undefined in C. The following routines are
12380 * careful to use unsigned types where modulo arithmetic is required.
12381 * Failure to do so _will_ break on newer gcc.
12382 */
12383
12384 /* Signed saturating arithmetic. */
12385
12386 /* Perform 16-bit signed saturating addition. */
add16_sat(uint16_t a,uint16_t b)12387 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
12388 {
12389 uint16_t res;
12390
12391 res = a + b;
12392 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
12393 if (a & 0x8000) {
12394 res = 0x8000;
12395 } else {
12396 res = 0x7fff;
12397 }
12398 }
12399 return res;
12400 }
12401
12402 /* Perform 8-bit signed saturating addition. */
add8_sat(uint8_t a,uint8_t b)12403 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
12404 {
12405 uint8_t res;
12406
12407 res = a + b;
12408 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
12409 if (a & 0x80) {
12410 res = 0x80;
12411 } else {
12412 res = 0x7f;
12413 }
12414 }
12415 return res;
12416 }
12417
12418 /* Perform 16-bit signed saturating subtraction. */
sub16_sat(uint16_t a,uint16_t b)12419 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
12420 {
12421 uint16_t res;
12422
12423 res = a - b;
12424 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
12425 if (a & 0x8000) {
12426 res = 0x8000;
12427 } else {
12428 res = 0x7fff;
12429 }
12430 }
12431 return res;
12432 }
12433
12434 /* Perform 8-bit signed saturating subtraction. */
sub8_sat(uint8_t a,uint8_t b)12435 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
12436 {
12437 uint8_t res;
12438
12439 res = a - b;
12440 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
12441 if (a & 0x80) {
12442 res = 0x80;
12443 } else {
12444 res = 0x7f;
12445 }
12446 }
12447 return res;
12448 }
12449
12450 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
12451 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
12452 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
12453 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
12454 #define PFX q
12455
12456 #include "op_addsub.h"
12457
12458 /* Unsigned saturating arithmetic. */
add16_usat(uint16_t a,uint16_t b)12459 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
12460 {
12461 uint16_t res;
12462 res = a + b;
12463 if (res < a) {
12464 res = 0xffff;
12465 }
12466 return res;
12467 }
12468
sub16_usat(uint16_t a,uint16_t b)12469 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
12470 {
12471 if (a > b) {
12472 return a - b;
12473 } else {
12474 return 0;
12475 }
12476 }
12477
add8_usat(uint8_t a,uint8_t b)12478 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
12479 {
12480 uint8_t res;
12481 res = a + b;
12482 if (res < a) {
12483 res = 0xff;
12484 }
12485 return res;
12486 }
12487
sub8_usat(uint8_t a,uint8_t b)12488 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
12489 {
12490 if (a > b) {
12491 return a - b;
12492 } else {
12493 return 0;
12494 }
12495 }
12496
12497 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
12498 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
12499 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
12500 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
12501 #define PFX uq
12502
12503 #include "op_addsub.h"
12504
12505 /* Signed modulo arithmetic. */
12506 #define SARITH16(a, b, n, op) do { \
12507 int32_t sum; \
12508 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
12509 RESULT(sum, n, 16); \
12510 if (sum >= 0) \
12511 ge |= 3 << (n * 2); \
12512 } while (0)
12513
12514 #define SARITH8(a, b, n, op) do { \
12515 int32_t sum; \
12516 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
12517 RESULT(sum, n, 8); \
12518 if (sum >= 0) \
12519 ge |= 1 << n; \
12520 } while (0)
12521
12522
12523 #define ADD16(a, b, n) SARITH16(a, b, n, +)
12524 #define SUB16(a, b, n) SARITH16(a, b, n, -)
12525 #define ADD8(a, b, n) SARITH8(a, b, n, +)
12526 #define SUB8(a, b, n) SARITH8(a, b, n, -)
12527 #define PFX s
12528 #define ARITH_GE
12529
12530 #include "op_addsub.h"
12531
12532 /* Unsigned modulo arithmetic. */
12533 #define ADD16(a, b, n) do { \
12534 uint32_t sum; \
12535 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
12536 RESULT(sum, n, 16); \
12537 if ((sum >> 16) == 1) \
12538 ge |= 3 << (n * 2); \
12539 } while (0)
12540
12541 #define ADD8(a, b, n) do { \
12542 uint32_t sum; \
12543 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
12544 RESULT(sum, n, 8); \
12545 if ((sum >> 8) == 1) \
12546 ge |= 1 << n; \
12547 } while (0)
12548
12549 #define SUB16(a, b, n) do { \
12550 uint32_t sum; \
12551 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
12552 RESULT(sum, n, 16); \
12553 if ((sum >> 16) == 0) \
12554 ge |= 3 << (n * 2); \
12555 } while (0)
12556
12557 #define SUB8(a, b, n) do { \
12558 uint32_t sum; \
12559 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
12560 RESULT(sum, n, 8); \
12561 if ((sum >> 8) == 0) \
12562 ge |= 1 << n; \
12563 } while (0)
12564
12565 #define PFX u
12566 #define ARITH_GE
12567
12568 #include "op_addsub.h"
12569
12570 /* Halved signed arithmetic. */
12571 #define ADD16(a, b, n) \
12572 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
12573 #define SUB16(a, b, n) \
12574 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
12575 #define ADD8(a, b, n) \
12576 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
12577 #define SUB8(a, b, n) \
12578 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
12579 #define PFX sh
12580
12581 #include "op_addsub.h"
12582
12583 /* Halved unsigned arithmetic. */
12584 #define ADD16(a, b, n) \
12585 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12586 #define SUB16(a, b, n) \
12587 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12588 #define ADD8(a, b, n) \
12589 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12590 #define SUB8(a, b, n) \
12591 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12592 #define PFX uh
12593
12594 #include "op_addsub.h"
12595
do_usad(uint8_t a,uint8_t b)12596 static inline uint8_t do_usad(uint8_t a, uint8_t b)
12597 {
12598 if (a > b) {
12599 return a - b;
12600 } else {
12601 return b - a;
12602 }
12603 }
12604
12605 /* Unsigned sum of absolute byte differences. */
HELPER(usad8)12606 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
12607 {
12608 uint32_t sum;
12609 sum = do_usad(a, b);
12610 sum += do_usad(a >> 8, b >> 8);
12611 sum += do_usad(a >> 16, b >> 16);
12612 sum += do_usad(a >> 24, b >> 24);
12613 return sum;
12614 }
12615
12616 /* For ARMv6 SEL instruction. */
HELPER(sel_flags)12617 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
12618 {
12619 uint32_t mask;
12620
12621 mask = 0;
12622 if (flags & 1) {
12623 mask |= 0xff;
12624 }
12625 if (flags & 2) {
12626 mask |= 0xff00;
12627 }
12628 if (flags & 4) {
12629 mask |= 0xff0000;
12630 }
12631 if (flags & 8) {
12632 mask |= 0xff000000;
12633 }
12634 return (a & mask) | (b & ~mask);
12635 }
12636
12637 /*
12638 * CRC helpers.
12639 * The upper bytes of val (above the number specified by 'bytes') must have
12640 * been zeroed out by the caller.
12641 */
HELPER(crc32)12642 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12643 {
12644 uint8_t buf[4];
12645
12646 stl_le_p(buf, val);
12647
12648 /* zlib crc32 converts the accumulator and output to one's complement. */
12649 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12650 }
12651
HELPER(crc32c)12652 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12653 {
12654 uint8_t buf[4];
12655
12656 stl_le_p(buf, val);
12657
12658 /* Linux crc32c converts the output to one's complement. */
12659 return crc32c(acc, buf, bytes) ^ 0xffffffff;
12660 }
12661
12662 /*
12663 * Return the exception level to which FP-disabled exceptions should
12664 * be taken, or 0 if FP is enabled.
12665 */
fp_exception_el(CPUARMState * env,int cur_el)12666 int fp_exception_el(CPUARMState *env, int cur_el)
12667 {
12668 #ifndef CONFIG_USER_ONLY
12669 uint64_t hcr_el2;
12670
12671 /*
12672 * CPACR and the CPTR registers don't exist before v6, so FP is
12673 * always accessible
12674 */
12675 if (!arm_feature(env, ARM_FEATURE_V6)) {
12676 return 0;
12677 }
12678
12679 if (arm_feature(env, ARM_FEATURE_M)) {
12680 /* CPACR can cause a NOCP UsageFault taken to current security state */
12681 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
12682 return 1;
12683 }
12684
12685 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
12686 if (!extract32(env->v7m.nsacr, 10, 1)) {
12687 /* FP insns cause a NOCP UsageFault taken to Secure */
12688 return 3;
12689 }
12690 }
12691
12692 return 0;
12693 }
12694
12695 hcr_el2 = arm_hcr_el2_eff(env);
12696
12697 /*
12698 * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12699 * 0, 2 : trap EL0 and EL1/PL1 accesses
12700 * 1 : trap only EL0 accesses
12701 * 3 : trap no accesses
12702 * This register is ignored if E2H+TGE are both set.
12703 */
12704 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12705 int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
12706
12707 switch (fpen) {
12708 case 1:
12709 if (cur_el != 0) {
12710 break;
12711 }
12712 /* fall through */
12713 case 0:
12714 case 2:
12715 /* Trap from Secure PL0 or PL1 to Secure PL1. */
12716 if (!arm_el_is_aa64(env, 3)
12717 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
12718 return 3;
12719 }
12720 if (cur_el <= 1) {
12721 return 1;
12722 }
12723 break;
12724 }
12725 }
12726
12727 /*
12728 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
12729 * to control non-secure access to the FPU. It doesn't have any
12730 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
12731 */
12732 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
12733 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
12734 if (!extract32(env->cp15.nsacr, 10, 1)) {
12735 /* FP insns act as UNDEF */
12736 return cur_el == 2 ? 2 : 1;
12737 }
12738 }
12739
12740 /*
12741 * CPTR_EL2 is present in v7VE or v8, and changes format
12742 * with HCR_EL2.E2H (regardless of TGE).
12743 */
12744 if (cur_el <= 2) {
12745 if (hcr_el2 & HCR_E2H) {
12746 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
12747 case 1:
12748 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
12749 break;
12750 }
12751 /* fall through */
12752 case 0:
12753 case 2:
12754 return 2;
12755 }
12756 } else if (arm_is_el2_enabled(env)) {
12757 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
12758 return 2;
12759 }
12760 }
12761 }
12762
12763 /* CPTR_EL3 : present in v8 */
12764 if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
12765 /* Trap all FP ops to EL3 */
12766 return 3;
12767 }
12768 #endif
12769 return 0;
12770 }
12771
12772 /* Return the exception level we're running at if this is our mmu_idx */
arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)12773 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12774 {
12775 if (mmu_idx & ARM_MMU_IDX_M) {
12776 return mmu_idx & ARM_MMU_IDX_M_PRIV;
12777 }
12778
12779 switch (mmu_idx) {
12780 case ARMMMUIdx_E10_0:
12781 case ARMMMUIdx_E20_0:
12782 case ARMMMUIdx_E30_0:
12783 return 0;
12784 case ARMMMUIdx_E10_1:
12785 case ARMMMUIdx_E10_1_PAN:
12786 return 1;
12787 case ARMMMUIdx_E2:
12788 case ARMMMUIdx_E20_2:
12789 case ARMMMUIdx_E20_2_PAN:
12790 return 2;
12791 case ARMMMUIdx_E3:
12792 case ARMMMUIdx_E30_3_PAN:
12793 return 3;
12794 default:
12795 g_assert_not_reached();
12796 }
12797 }
12798
12799 #ifndef CONFIG_TCG
arm_v7m_mmu_idx_for_secstate(CPUARMState * env,bool secstate)12800 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12801 {
12802 g_assert_not_reached();
12803 }
12804 #endif
12805
arm_mmu_idx_el(CPUARMState * env,int el)12806 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
12807 {
12808 ARMMMUIdx idx;
12809 uint64_t hcr;
12810
12811 if (arm_feature(env, ARM_FEATURE_M)) {
12812 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
12813 }
12814
12815 /* See ARM pseudo-function ELIsInHost. */
12816 switch (el) {
12817 case 0:
12818 hcr = arm_hcr_el2_eff(env);
12819 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
12820 idx = ARMMMUIdx_E20_0;
12821 } else if (arm_is_secure_below_el3(env) &&
12822 !arm_el_is_aa64(env, 3)) {
12823 idx = ARMMMUIdx_E30_0;
12824 } else {
12825 idx = ARMMMUIdx_E10_0;
12826 }
12827 break;
12828 case 1:
12829 if (arm_pan_enabled(env)) {
12830 idx = ARMMMUIdx_E10_1_PAN;
12831 } else {
12832 idx = ARMMMUIdx_E10_1;
12833 }
12834 break;
12835 case 2:
12836 /* Note that TGE does not apply at EL2. */
12837 if (arm_hcr_el2_eff(env) & HCR_E2H) {
12838 if (arm_pan_enabled(env)) {
12839 idx = ARMMMUIdx_E20_2_PAN;
12840 } else {
12841 idx = ARMMMUIdx_E20_2;
12842 }
12843 } else {
12844 idx = ARMMMUIdx_E2;
12845 }
12846 break;
12847 case 3:
12848 if (!arm_el_is_aa64(env, 3) && arm_pan_enabled(env)) {
12849 return ARMMMUIdx_E30_3_PAN;
12850 }
12851 return ARMMMUIdx_E3;
12852 default:
12853 g_assert_not_reached();
12854 }
12855
12856 return idx;
12857 }
12858
arm_mmu_idx(CPUARMState * env)12859 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12860 {
12861 return arm_mmu_idx_el(env, arm_current_el(env));
12862 }
12863
mve_no_pred(CPUARMState * env)12864 static bool mve_no_pred(CPUARMState *env)
12865 {
12866 /*
12867 * Return true if there is definitely no predication of MVE
12868 * instructions by VPR or LTPSIZE. (Returning false even if there
12869 * isn't any predication is OK; generated code will just be
12870 * a little worse.)
12871 * If the CPU does not implement MVE then this TB flag is always 0.
12872 *
12873 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
12874 * logic in gen_update_fp_context() needs to be updated to match.
12875 *
12876 * We do not include the effect of the ECI bits here -- they are
12877 * tracked in other TB flags. This simplifies the logic for
12878 * "when did we emit code that changes the MVE_NO_PRED TB flag
12879 * and thus need to end the TB?".
12880 */
12881 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
12882 return false;
12883 }
12884 if (env->v7m.vpr) {
12885 return false;
12886 }
12887 if (env->v7m.ltpsize < 4) {
12888 return false;
12889 }
12890 return true;
12891 }
12892
cpu_get_tb_cpu_state(CPUARMState * env,vaddr * pc,uint64_t * cs_base,uint32_t * pflags)12893 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
12894 uint64_t *cs_base, uint32_t *pflags)
12895 {
12896 CPUARMTBFlags flags;
12897
12898 assert_hflags_rebuild_correctly(env);
12899 flags = env->hflags;
12900
12901 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
12902 *pc = env->pc;
12903 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12904 DP_TBFLAG_A64(flags, BTYPE, env->btype);
12905 }
12906 } else {
12907 *pc = env->regs[15];
12908
12909 if (arm_feature(env, ARM_FEATURE_M)) {
12910 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12911 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12912 != env->v7m.secure) {
12913 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
12914 }
12915
12916 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12917 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12918 (env->v7m.secure &&
12919 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12920 /*
12921 * ASPEN is set, but FPCA/SFPA indicate that there is no
12922 * active FP context; we must create a new FP context before
12923 * executing any FP insn.
12924 */
12925 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
12926 }
12927
12928 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12929 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
12930 DP_TBFLAG_M32(flags, LSPACT, 1);
12931 }
12932
12933 if (mve_no_pred(env)) {
12934 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12935 }
12936 } else {
12937 /*
12938 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12939 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12940 */
12941 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12942 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
12943 } else {
12944 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12945 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
12946 }
12947 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12948 DP_TBFLAG_A32(flags, VFPEN, 1);
12949 }
12950 }
12951
12952 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12953 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
12954 }
12955
12956 /*
12957 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12958 * states defined in the ARM ARM for software singlestep:
12959 * SS_ACTIVE PSTATE.SS State
12960 * 0 x Inactive (the TB flag for SS is always 0)
12961 * 1 0 Active-pending
12962 * 1 1 Active-not-pending
12963 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
12964 */
12965 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12966 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
12967 }
12968
12969 *pflags = flags.flags;
12970 *cs_base = flags.flags2;
12971 }
12972
12973 #ifdef TARGET_AARCH64
12974 /*
12975 * The manual says that when SVE is enabled and VQ is widened the
12976 * implementation is allowed to zero the previously inaccessible
12977 * portion of the registers. The corollary to that is that when
12978 * SVE is enabled and VQ is narrowed we are also allowed to zero
12979 * the now inaccessible portion of the registers.
12980 *
12981 * The intent of this is that no predicate bit beyond VQ is ever set.
12982 * Which means that some operations on predicate registers themselves
12983 * may operate on full uint64_t or even unrolled across the maximum
12984 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
12985 * may well be cheaper than conditionals to restrict the operation
12986 * to the relevant portion of a uint16_t[16].
12987 */
aarch64_sve_narrow_vq(CPUARMState * env,unsigned vq)12988 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12989 {
12990 int i, j;
12991 uint64_t pmask;
12992
12993 assert(vq >= 1 && vq <= ARM_MAX_VQ);
12994 assert(vq <= env_archcpu(env)->sve_max_vq);
12995
12996 /* Zap the high bits of the zregs. */
12997 for (i = 0; i < 32; i++) {
12998 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12999 }
13000
13001 /* Zap the high bits of the pregs and ffr. */
13002 pmask = 0;
13003 if (vq & 3) {
13004 pmask = ~(-1ULL << (16 * (vq & 3)));
13005 }
13006 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
13007 for (i = 0; i < 17; ++i) {
13008 env->vfp.pregs[i].p[j] &= pmask;
13009 }
13010 pmask = 0;
13011 }
13012 }
13013
sve_vqm1_for_el_sm_ena(CPUARMState * env,int el,bool sm)13014 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
13015 {
13016 int exc_el;
13017
13018 if (sm) {
13019 exc_el = sme_exception_el(env, el);
13020 } else {
13021 exc_el = sve_exception_el(env, el);
13022 }
13023 if (exc_el) {
13024 return 0; /* disabled */
13025 }
13026 return sve_vqm1_for_el_sm(env, el, sm);
13027 }
13028
13029 /*
13030 * Notice a change in SVE vector size when changing EL.
13031 */
aarch64_sve_change_el(CPUARMState * env,int old_el,int new_el,bool el0_a64)13032 void aarch64_sve_change_el(CPUARMState *env, int old_el,
13033 int new_el, bool el0_a64)
13034 {
13035 ARMCPU *cpu = env_archcpu(env);
13036 int old_len, new_len;
13037 bool old_a64, new_a64, sm;
13038
13039 /* Nothing to do if no SVE. */
13040 if (!cpu_isar_feature(aa64_sve, cpu)) {
13041 return;
13042 }
13043
13044 /* Nothing to do if FP is disabled in either EL. */
13045 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
13046 return;
13047 }
13048
13049 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
13050 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
13051
13052 /*
13053 * Both AArch64.TakeException and AArch64.ExceptionReturn
13054 * invoke ResetSVEState when taking an exception from, or
13055 * returning to, AArch32 state when PSTATE.SM is enabled.
13056 */
13057 sm = FIELD_EX64(env->svcr, SVCR, SM);
13058 if (old_a64 != new_a64 && sm) {
13059 arm_reset_sve_state(env);
13060 return;
13061 }
13062
13063 /*
13064 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
13065 * at ELx, or not available because the EL is in AArch32 state, then
13066 * for all purposes other than a direct read, the ZCR_ELx.LEN field
13067 * has an effective value of 0".
13068 *
13069 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
13070 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
13071 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
13072 * we already have the correct register contents when encountering the
13073 * vq0->vq0 transition between EL0->EL1.
13074 */
13075 old_len = new_len = 0;
13076 if (old_a64) {
13077 old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
13078 }
13079 if (new_a64) {
13080 new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
13081 }
13082
13083 /* When changing vector length, clear inaccessible state. */
13084 if (new_len < old_len) {
13085 aarch64_sve_narrow_vq(env, new_len + 1);
13086 }
13087 }
13088 #endif
13089
13090 #ifndef CONFIG_USER_ONLY
arm_security_space(CPUARMState * env)13091 ARMSecuritySpace arm_security_space(CPUARMState *env)
13092 {
13093 if (arm_feature(env, ARM_FEATURE_M)) {
13094 return arm_secure_to_space(env->v7m.secure);
13095 }
13096
13097 /*
13098 * If EL3 is not supported then the secure state is implementation
13099 * defined, in which case QEMU defaults to non-secure.
13100 */
13101 if (!arm_feature(env, ARM_FEATURE_EL3)) {
13102 return ARMSS_NonSecure;
13103 }
13104
13105 /* Check for AArch64 EL3 or AArch32 Mon. */
13106 if (is_a64(env)) {
13107 if (extract32(env->pstate, 2, 2) == 3) {
13108 if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
13109 return ARMSS_Root;
13110 } else {
13111 return ARMSS_Secure;
13112 }
13113 }
13114 } else {
13115 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
13116 return ARMSS_Secure;
13117 }
13118 }
13119
13120 return arm_security_space_below_el3(env);
13121 }
13122
arm_security_space_below_el3(CPUARMState * env)13123 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
13124 {
13125 assert(!arm_feature(env, ARM_FEATURE_M));
13126
13127 /*
13128 * If EL3 is not supported then the secure state is implementation
13129 * defined, in which case QEMU defaults to non-secure.
13130 */
13131 if (!arm_feature(env, ARM_FEATURE_EL3)) {
13132 return ARMSS_NonSecure;
13133 }
13134
13135 /*
13136 * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
13137 * Ignoring NSE when !NS retains consistency without having to
13138 * modify other predicates.
13139 */
13140 if (!(env->cp15.scr_el3 & SCR_NS)) {
13141 return ARMSS_Secure;
13142 } else if (env->cp15.scr_el3 & SCR_NSE) {
13143 return ARMSS_Realm;
13144 } else {
13145 return ARMSS_NonSecure;
13146 }
13147 }
13148 #endif /* !CONFIG_USER_ONLY */
13149