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;
2656 }
2657 if (!(env->cp15.scr_el3 & SCR_ST)) {
2658 return CP_ACCESS_TRAP_EL3;
2659 }
2660 return CP_ACCESS_OK;
2661 case 0:
2662 case 2:
2663 return CP_ACCESS_TRAP;
2664 case 3:
2665 return CP_ACCESS_OK;
2666 default:
2667 g_assert_not_reached();
2668 }
2669 }
2670
gt_get_countervalue(CPUARMState * env)2671 uint64_t gt_get_countervalue(CPUARMState *env)
2672 {
2673 ARMCPU *cpu = env_archcpu(env);
2674
2675 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2676 }
2677
gt_update_irq(ARMCPU * cpu,int timeridx)2678 static void gt_update_irq(ARMCPU *cpu, int timeridx)
2679 {
2680 CPUARMState *env = &cpu->env;
2681 uint64_t cnthctl = env->cp15.cnthctl_el2;
2682 ARMSecuritySpace ss = arm_security_space(env);
2683 /* ISTATUS && !IMASK */
2684 int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2685
2686 /*
2687 * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2688 * It is RES0 in Secure and NonSecure state.
2689 */
2690 if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2691 ((timeridx == GTIMER_VIRT && (cnthctl & R_CNTHCTL_CNTVMASK_MASK)) ||
2692 (timeridx == GTIMER_PHYS && (cnthctl & R_CNTHCTL_CNTPMASK_MASK)))) {
2693 irqstate = 0;
2694 }
2695
2696 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2697 trace_arm_gt_update_irq(timeridx, irqstate);
2698 }
2699
gt_rme_post_el_change(ARMCPU * cpu,void * ignored)2700 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2701 {
2702 /*
2703 * Changing security state between Root and Secure/NonSecure, which may
2704 * happen when switching EL, can change the effective value of CNTHCTL_EL2
2705 * mask bits. Update the IRQ state accordingly.
2706 */
2707 gt_update_irq(cpu, GTIMER_VIRT);
2708 gt_update_irq(cpu, GTIMER_PHYS);
2709 }
2710
gt_phys_raw_cnt_offset(CPUARMState * env)2711 static uint64_t gt_phys_raw_cnt_offset(CPUARMState *env)
2712 {
2713 if ((env->cp15.scr_el3 & SCR_ECVEN) &&
2714 FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, ECV) &&
2715 arm_is_el2_enabled(env) &&
2716 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
2717 return env->cp15.cntpoff_el2;
2718 }
2719 return 0;
2720 }
2721
gt_phys_cnt_offset(CPUARMState * env)2722 static uint64_t gt_phys_cnt_offset(CPUARMState *env)
2723 {
2724 if (arm_current_el(env) >= 2) {
2725 return 0;
2726 }
2727 return gt_phys_raw_cnt_offset(env);
2728 }
2729
gt_recalc_timer(ARMCPU * cpu,int timeridx)2730 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2731 {
2732 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2733
2734 if (gt->ctl & 1) {
2735 /*
2736 * Timer enabled: calculate and set current ISTATUS, irq, and
2737 * reset timer to when ISTATUS next has to change
2738 */
2739 uint64_t offset = timeridx == GTIMER_VIRT ?
2740 cpu->env.cp15.cntvoff_el2 : gt_phys_raw_cnt_offset(&cpu->env);
2741 uint64_t count = gt_get_countervalue(&cpu->env);
2742 /* Note that this must be unsigned 64 bit arithmetic: */
2743 int istatus = count - offset >= gt->cval;
2744 uint64_t nexttick;
2745
2746 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2747
2748 if (istatus) {
2749 /*
2750 * Next transition is when (count - offset) rolls back over to 0.
2751 * If offset > count then this is when count == offset;
2752 * if offset <= count then this is when count == offset + 2^64
2753 * For the latter case we set nexttick to an "as far in future
2754 * as possible" value and let the code below handle it.
2755 */
2756 if (offset > count) {
2757 nexttick = offset;
2758 } else {
2759 nexttick = UINT64_MAX;
2760 }
2761 } else {
2762 /*
2763 * Next transition is when (count - offset) == cval, i.e.
2764 * when count == (cval + offset).
2765 * If that would overflow, then again we set up the next interrupt
2766 * for "as far in the future as possible" for the code below.
2767 */
2768 if (uadd64_overflow(gt->cval, offset, &nexttick)) {
2769 nexttick = UINT64_MAX;
2770 }
2771 }
2772 /*
2773 * Note that the desired next expiry time might be beyond the
2774 * signed-64-bit range of a QEMUTimer -- in this case we just
2775 * set the timer for as far in the future as possible. When the
2776 * timer expires we will reset the timer for any remaining period.
2777 */
2778 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2779 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2780 } else {
2781 timer_mod(cpu->gt_timer[timeridx], nexttick);
2782 }
2783 trace_arm_gt_recalc(timeridx, nexttick);
2784 } else {
2785 /* Timer disabled: ISTATUS and timer output always clear */
2786 gt->ctl &= ~4;
2787 timer_del(cpu->gt_timer[timeridx]);
2788 trace_arm_gt_recalc_disabled(timeridx);
2789 }
2790 gt_update_irq(cpu, timeridx);
2791 }
2792
gt_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx)2793 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2794 int timeridx)
2795 {
2796 ARMCPU *cpu = env_archcpu(env);
2797
2798 timer_del(cpu->gt_timer[timeridx]);
2799 }
2800
gt_cnt_read(CPUARMState * env,const ARMCPRegInfo * ri)2801 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2802 {
2803 return gt_get_countervalue(env) - gt_phys_cnt_offset(env);
2804 }
2805
gt_virt_cnt_offset(CPUARMState * env)2806 uint64_t gt_virt_cnt_offset(CPUARMState *env)
2807 {
2808 uint64_t hcr;
2809
2810 switch (arm_current_el(env)) {
2811 case 2:
2812 hcr = arm_hcr_el2_eff(env);
2813 if (hcr & HCR_E2H) {
2814 return 0;
2815 }
2816 break;
2817 case 0:
2818 hcr = arm_hcr_el2_eff(env);
2819 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2820 return 0;
2821 }
2822 break;
2823 }
2824
2825 return env->cp15.cntvoff_el2;
2826 }
2827
gt_virt_cnt_read(CPUARMState * env,const ARMCPRegInfo * ri)2828 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2829 {
2830 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
2831 }
2832
gt_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx,uint64_t value)2833 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2834 int timeridx,
2835 uint64_t value)
2836 {
2837 trace_arm_gt_cval_write(timeridx, value);
2838 env->cp15.c14_timer[timeridx].cval = value;
2839 gt_recalc_timer(env_archcpu(env), timeridx);
2840 }
2841
gt_tval_read(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx)2842 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2843 int timeridx)
2844 {
2845 uint64_t offset = 0;
2846
2847 switch (timeridx) {
2848 case GTIMER_VIRT:
2849 case GTIMER_HYPVIRT:
2850 offset = gt_virt_cnt_offset(env);
2851 break;
2852 case GTIMER_PHYS:
2853 offset = gt_phys_cnt_offset(env);
2854 break;
2855 }
2856
2857 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2858 (gt_get_countervalue(env) - offset));
2859 }
2860
gt_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx,uint64_t value)2861 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2862 int timeridx,
2863 uint64_t value)
2864 {
2865 uint64_t offset = 0;
2866
2867 switch (timeridx) {
2868 case GTIMER_VIRT:
2869 case GTIMER_HYPVIRT:
2870 offset = gt_virt_cnt_offset(env);
2871 break;
2872 case GTIMER_PHYS:
2873 offset = gt_phys_cnt_offset(env);
2874 break;
2875 }
2876
2877 trace_arm_gt_tval_write(timeridx, value);
2878 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2879 sextract64(value, 0, 32);
2880 gt_recalc_timer(env_archcpu(env), timeridx);
2881 }
2882
gt_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx,uint64_t value)2883 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2884 int timeridx,
2885 uint64_t value)
2886 {
2887 ARMCPU *cpu = env_archcpu(env);
2888 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2889
2890 trace_arm_gt_ctl_write(timeridx, value);
2891 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2892 if ((oldval ^ value) & 1) {
2893 /* Enable toggled */
2894 gt_recalc_timer(cpu, timeridx);
2895 } else if ((oldval ^ value) & 2) {
2896 /*
2897 * IMASK toggled: don't need to recalculate,
2898 * just set the interrupt line based on ISTATUS
2899 */
2900 trace_arm_gt_imask_toggle(timeridx);
2901 gt_update_irq(cpu, timeridx);
2902 }
2903 }
2904
gt_phys_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)2905 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2906 {
2907 gt_timer_reset(env, ri, GTIMER_PHYS);
2908 }
2909
gt_phys_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2910 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2911 uint64_t value)
2912 {
2913 gt_cval_write(env, ri, GTIMER_PHYS, value);
2914 }
2915
gt_phys_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)2916 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2917 {
2918 return gt_tval_read(env, ri, GTIMER_PHYS);
2919 }
2920
gt_phys_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2921 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2922 uint64_t value)
2923 {
2924 gt_tval_write(env, ri, GTIMER_PHYS, value);
2925 }
2926
gt_phys_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2927 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2928 uint64_t value)
2929 {
2930 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2931 }
2932
gt_phys_redir_timeridx(CPUARMState * env)2933 static int gt_phys_redir_timeridx(CPUARMState *env)
2934 {
2935 switch (arm_mmu_idx(env)) {
2936 case ARMMMUIdx_E20_0:
2937 case ARMMMUIdx_E20_2:
2938 case ARMMMUIdx_E20_2_PAN:
2939 return GTIMER_HYP;
2940 default:
2941 return GTIMER_PHYS;
2942 }
2943 }
2944
gt_virt_redir_timeridx(CPUARMState * env)2945 static int gt_virt_redir_timeridx(CPUARMState *env)
2946 {
2947 switch (arm_mmu_idx(env)) {
2948 case ARMMMUIdx_E20_0:
2949 case ARMMMUIdx_E20_2:
2950 case ARMMMUIdx_E20_2_PAN:
2951 return GTIMER_HYPVIRT;
2952 default:
2953 return GTIMER_VIRT;
2954 }
2955 }
2956
gt_phys_redir_cval_read(CPUARMState * env,const ARMCPRegInfo * ri)2957 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2958 const ARMCPRegInfo *ri)
2959 {
2960 int timeridx = gt_phys_redir_timeridx(env);
2961 return env->cp15.c14_timer[timeridx].cval;
2962 }
2963
gt_phys_redir_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2964 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2965 uint64_t value)
2966 {
2967 int timeridx = gt_phys_redir_timeridx(env);
2968 gt_cval_write(env, ri, timeridx, value);
2969 }
2970
gt_phys_redir_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)2971 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2972 const ARMCPRegInfo *ri)
2973 {
2974 int timeridx = gt_phys_redir_timeridx(env);
2975 return gt_tval_read(env, ri, timeridx);
2976 }
2977
gt_phys_redir_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2978 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2979 uint64_t value)
2980 {
2981 int timeridx = gt_phys_redir_timeridx(env);
2982 gt_tval_write(env, ri, timeridx, value);
2983 }
2984
gt_phys_redir_ctl_read(CPUARMState * env,const ARMCPRegInfo * ri)2985 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2986 const ARMCPRegInfo *ri)
2987 {
2988 int timeridx = gt_phys_redir_timeridx(env);
2989 return env->cp15.c14_timer[timeridx].ctl;
2990 }
2991
gt_phys_redir_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2992 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2993 uint64_t value)
2994 {
2995 int timeridx = gt_phys_redir_timeridx(env);
2996 gt_ctl_write(env, ri, timeridx, value);
2997 }
2998
gt_virt_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)2999 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3000 {
3001 gt_timer_reset(env, ri, GTIMER_VIRT);
3002 }
3003
gt_virt_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3004 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3005 uint64_t value)
3006 {
3007 gt_cval_write(env, ri, GTIMER_VIRT, value);
3008 }
3009
gt_virt_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3010 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3011 {
3012 return gt_tval_read(env, ri, GTIMER_VIRT);
3013 }
3014
gt_virt_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3015 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3016 uint64_t value)
3017 {
3018 gt_tval_write(env, ri, GTIMER_VIRT, value);
3019 }
3020
gt_virt_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3021 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3022 uint64_t value)
3023 {
3024 gt_ctl_write(env, ri, GTIMER_VIRT, value);
3025 }
3026
gt_cnthctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3027 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3028 uint64_t value)
3029 {
3030 ARMCPU *cpu = env_archcpu(env);
3031 uint32_t oldval = env->cp15.cnthctl_el2;
3032 uint32_t valid_mask =
3033 R_CNTHCTL_EL0PCTEN_E2H1_MASK |
3034 R_CNTHCTL_EL0VCTEN_E2H1_MASK |
3035 R_CNTHCTL_EVNTEN_MASK |
3036 R_CNTHCTL_EVNTDIR_MASK |
3037 R_CNTHCTL_EVNTI_MASK |
3038 R_CNTHCTL_EL0VTEN_MASK |
3039 R_CNTHCTL_EL0PTEN_MASK |
3040 R_CNTHCTL_EL1PCTEN_E2H1_MASK |
3041 R_CNTHCTL_EL1PTEN_MASK;
3042
3043 if (cpu_isar_feature(aa64_rme, cpu)) {
3044 valid_mask |= R_CNTHCTL_CNTVMASK_MASK | R_CNTHCTL_CNTPMASK_MASK;
3045 }
3046 if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
3047 valid_mask |=
3048 R_CNTHCTL_EL1TVT_MASK |
3049 R_CNTHCTL_EL1TVCT_MASK |
3050 R_CNTHCTL_EL1NVPCT_MASK |
3051 R_CNTHCTL_EL1NVVCT_MASK |
3052 R_CNTHCTL_EVNTIS_MASK;
3053 }
3054 if (cpu_isar_feature(aa64_ecv, cpu)) {
3055 valid_mask |= R_CNTHCTL_ECV_MASK;
3056 }
3057
3058 /* Clear RES0 bits */
3059 value &= valid_mask;
3060
3061 raw_write(env, ri, value);
3062
3063 if ((oldval ^ value) & R_CNTHCTL_CNTVMASK_MASK) {
3064 gt_update_irq(cpu, GTIMER_VIRT);
3065 } else if ((oldval ^ value) & R_CNTHCTL_CNTPMASK_MASK) {
3066 gt_update_irq(cpu, GTIMER_PHYS);
3067 }
3068 }
3069
gt_cntvoff_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3070 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
3071 uint64_t value)
3072 {
3073 ARMCPU *cpu = env_archcpu(env);
3074
3075 trace_arm_gt_cntvoff_write(value);
3076 raw_write(env, ri, value);
3077 gt_recalc_timer(cpu, GTIMER_VIRT);
3078 }
3079
gt_virt_redir_cval_read(CPUARMState * env,const ARMCPRegInfo * ri)3080 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
3081 const ARMCPRegInfo *ri)
3082 {
3083 int timeridx = gt_virt_redir_timeridx(env);
3084 return env->cp15.c14_timer[timeridx].cval;
3085 }
3086
gt_virt_redir_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3087 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3088 uint64_t value)
3089 {
3090 int timeridx = gt_virt_redir_timeridx(env);
3091 gt_cval_write(env, ri, timeridx, value);
3092 }
3093
gt_virt_redir_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3094 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
3095 const ARMCPRegInfo *ri)
3096 {
3097 int timeridx = gt_virt_redir_timeridx(env);
3098 return gt_tval_read(env, ri, timeridx);
3099 }
3100
gt_virt_redir_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3101 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3102 uint64_t value)
3103 {
3104 int timeridx = gt_virt_redir_timeridx(env);
3105 gt_tval_write(env, ri, timeridx, value);
3106 }
3107
gt_virt_redir_ctl_read(CPUARMState * env,const ARMCPRegInfo * ri)3108 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
3109 const ARMCPRegInfo *ri)
3110 {
3111 int timeridx = gt_virt_redir_timeridx(env);
3112 return env->cp15.c14_timer[timeridx].ctl;
3113 }
3114
gt_virt_redir_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3115 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3116 uint64_t value)
3117 {
3118 int timeridx = gt_virt_redir_timeridx(env);
3119 gt_ctl_write(env, ri, timeridx, value);
3120 }
3121
gt_hyp_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)3122 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3123 {
3124 gt_timer_reset(env, ri, GTIMER_HYP);
3125 }
3126
gt_hyp_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3127 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3128 uint64_t value)
3129 {
3130 gt_cval_write(env, ri, GTIMER_HYP, value);
3131 }
3132
gt_hyp_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3133 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3134 {
3135 return gt_tval_read(env, ri, GTIMER_HYP);
3136 }
3137
gt_hyp_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3138 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3139 uint64_t value)
3140 {
3141 gt_tval_write(env, ri, GTIMER_HYP, value);
3142 }
3143
gt_hyp_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3144 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3145 uint64_t value)
3146 {
3147 gt_ctl_write(env, ri, GTIMER_HYP, value);
3148 }
3149
gt_sec_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)3150 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3151 {
3152 gt_timer_reset(env, ri, GTIMER_SEC);
3153 }
3154
gt_sec_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3155 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3156 uint64_t value)
3157 {
3158 gt_cval_write(env, ri, GTIMER_SEC, value);
3159 }
3160
gt_sec_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3161 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3162 {
3163 return gt_tval_read(env, ri, GTIMER_SEC);
3164 }
3165
gt_sec_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3166 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3167 uint64_t value)
3168 {
3169 gt_tval_write(env, ri, GTIMER_SEC, value);
3170 }
3171
gt_sec_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3172 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3173 uint64_t value)
3174 {
3175 gt_ctl_write(env, ri, GTIMER_SEC, value);
3176 }
3177
gt_hv_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)3178 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3179 {
3180 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3181 }
3182
gt_hv_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3183 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3184 uint64_t value)
3185 {
3186 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3187 }
3188
gt_hv_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3189 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3190 {
3191 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3192 }
3193
gt_hv_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3194 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3195 uint64_t value)
3196 {
3197 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3198 }
3199
gt_hv_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3200 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3201 uint64_t value)
3202 {
3203 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3204 }
3205
arm_gt_ptimer_cb(void * opaque)3206 void arm_gt_ptimer_cb(void *opaque)
3207 {
3208 ARMCPU *cpu = opaque;
3209
3210 gt_recalc_timer(cpu, GTIMER_PHYS);
3211 }
3212
arm_gt_vtimer_cb(void * opaque)3213 void arm_gt_vtimer_cb(void *opaque)
3214 {
3215 ARMCPU *cpu = opaque;
3216
3217 gt_recalc_timer(cpu, GTIMER_VIRT);
3218 }
3219
arm_gt_htimer_cb(void * opaque)3220 void arm_gt_htimer_cb(void *opaque)
3221 {
3222 ARMCPU *cpu = opaque;
3223
3224 gt_recalc_timer(cpu, GTIMER_HYP);
3225 }
3226
arm_gt_stimer_cb(void * opaque)3227 void arm_gt_stimer_cb(void *opaque)
3228 {
3229 ARMCPU *cpu = opaque;
3230
3231 gt_recalc_timer(cpu, GTIMER_SEC);
3232 }
3233
arm_gt_hvtimer_cb(void * opaque)3234 void arm_gt_hvtimer_cb(void *opaque)
3235 {
3236 ARMCPU *cpu = opaque;
3237
3238 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3239 }
3240
3241 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3242 /*
3243 * Note that CNTFRQ is purely reads-as-written for the benefit
3244 * of software; writing it doesn't actually change the timer frequency.
3245 * Our reset value matches the fixed frequency we implement the timer at.
3246 */
3247 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3248 .type = ARM_CP_ALIAS,
3249 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3250 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3251 },
3252 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3253 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3254 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3255 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3256 .resetfn = arm_gt_cntfrq_reset,
3257 },
3258 /* overall control: mostly access permissions */
3259 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3260 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3261 .access = PL1_RW,
3262 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3263 .resetvalue = 0,
3264 },
3265 /* per-timer control */
3266 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3267 .secure = ARM_CP_SECSTATE_NS,
3268 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3269 .accessfn = gt_ptimer_access,
3270 .fieldoffset = offsetoflow32(CPUARMState,
3271 cp15.c14_timer[GTIMER_PHYS].ctl),
3272 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3273 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3274 },
3275 { .name = "CNTP_CTL_S",
3276 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3277 .secure = ARM_CP_SECSTATE_S,
3278 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3279 .accessfn = gt_ptimer_access,
3280 .fieldoffset = offsetoflow32(CPUARMState,
3281 cp15.c14_timer[GTIMER_SEC].ctl),
3282 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3283 },
3284 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3285 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3286 .type = ARM_CP_IO, .access = PL0_RW,
3287 .accessfn = gt_ptimer_access,
3288 .nv2_redirect_offset = 0x180 | NV2_REDIR_NV1,
3289 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3290 .resetvalue = 0,
3291 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3292 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3293 },
3294 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3295 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3296 .accessfn = gt_vtimer_access,
3297 .fieldoffset = offsetoflow32(CPUARMState,
3298 cp15.c14_timer[GTIMER_VIRT].ctl),
3299 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3300 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3301 },
3302 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3303 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3304 .type = ARM_CP_IO, .access = PL0_RW,
3305 .accessfn = gt_vtimer_access,
3306 .nv2_redirect_offset = 0x170 | NV2_REDIR_NV1,
3307 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3308 .resetvalue = 0,
3309 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3310 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3311 },
3312 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3313 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3314 .secure = ARM_CP_SECSTATE_NS,
3315 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3316 .accessfn = gt_ptimer_access,
3317 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3318 },
3319 { .name = "CNTP_TVAL_S",
3320 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3321 .secure = ARM_CP_SECSTATE_S,
3322 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3323 .accessfn = gt_ptimer_access,
3324 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3325 },
3326 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3327 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3328 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3329 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3330 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3331 },
3332 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3333 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3334 .accessfn = gt_vtimer_access,
3335 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3336 },
3337 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3338 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3339 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3340 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3341 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3342 },
3343 /* The counter itself */
3344 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3345 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3346 .accessfn = gt_pct_access,
3347 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3348 },
3349 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3350 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3351 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3352 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3353 },
3354 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3355 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3356 .accessfn = gt_vct_access,
3357 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3358 },
3359 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3360 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3361 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3362 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3363 },
3364 /* Comparison value, indicating when the timer goes off */
3365 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3366 .secure = ARM_CP_SECSTATE_NS,
3367 .access = PL0_RW,
3368 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3369 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3370 .accessfn = gt_ptimer_access,
3371 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3372 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3373 },
3374 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3375 .secure = ARM_CP_SECSTATE_S,
3376 .access = PL0_RW,
3377 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3378 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3379 .accessfn = gt_ptimer_access,
3380 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3381 },
3382 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3383 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3384 .access = PL0_RW,
3385 .type = ARM_CP_IO,
3386 .nv2_redirect_offset = 0x178 | NV2_REDIR_NV1,
3387 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3388 .resetvalue = 0, .accessfn = gt_ptimer_access,
3389 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3390 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3391 },
3392 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3393 .access = PL0_RW,
3394 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3395 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3396 .accessfn = gt_vtimer_access,
3397 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3398 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3399 },
3400 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3401 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3402 .access = PL0_RW,
3403 .type = ARM_CP_IO,
3404 .nv2_redirect_offset = 0x168 | NV2_REDIR_NV1,
3405 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3406 .resetvalue = 0, .accessfn = gt_vtimer_access,
3407 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3408 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3409 },
3410 /*
3411 * Secure timer -- this is actually restricted to only EL3
3412 * and configurably Secure-EL1 via the accessfn.
3413 */
3414 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3415 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3416 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3417 .accessfn = gt_stimer_access,
3418 .readfn = gt_sec_tval_read,
3419 .writefn = gt_sec_tval_write,
3420 .resetfn = gt_sec_timer_reset,
3421 },
3422 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3423 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3424 .type = ARM_CP_IO, .access = PL1_RW,
3425 .accessfn = gt_stimer_access,
3426 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3427 .resetvalue = 0,
3428 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3429 },
3430 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3431 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3432 .type = ARM_CP_IO, .access = PL1_RW,
3433 .accessfn = gt_stimer_access,
3434 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3435 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3436 },
3437 };
3438
3439 /*
3440 * FEAT_ECV adds extra views of CNTVCT_EL0 and CNTPCT_EL0 which
3441 * are "self-synchronizing". For QEMU all sysregs are self-synchronizing,
3442 * so our implementations here are identical to the normal registers.
3443 */
3444 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3445 { .name = "CNTVCTSS", .cp = 15, .crm = 14, .opc1 = 9,
3446 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3447 .accessfn = gt_vct_access,
3448 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3449 },
3450 { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3451 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3452 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3453 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3454 },
3455 { .name = "CNTPCTSS", .cp = 15, .crm = 14, .opc1 = 8,
3456 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3457 .accessfn = gt_pct_access,
3458 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3459 },
3460 { .name = "CNTPCTSS_EL0", .state = ARM_CP_STATE_AA64,
3461 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 5,
3462 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3463 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3464 },
3465 };
3466
gt_cntpoff_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)3467 static CPAccessResult gt_cntpoff_access(CPUARMState *env,
3468 const ARMCPRegInfo *ri,
3469 bool isread)
3470 {
3471 if (arm_current_el(env) == 2 && arm_feature(env, ARM_FEATURE_EL3) &&
3472 !(env->cp15.scr_el3 & SCR_ECVEN)) {
3473 return CP_ACCESS_TRAP_EL3;
3474 }
3475 return CP_ACCESS_OK;
3476 }
3477
gt_cntpoff_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3478 static void gt_cntpoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
3479 uint64_t value)
3480 {
3481 ARMCPU *cpu = env_archcpu(env);
3482
3483 trace_arm_gt_cntpoff_write(value);
3484 raw_write(env, ri, value);
3485 gt_recalc_timer(cpu, GTIMER_PHYS);
3486 }
3487
3488 static const ARMCPRegInfo gen_timer_cntpoff_reginfo = {
3489 .name = "CNTPOFF_EL2", .state = ARM_CP_STATE_AA64,
3490 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 6,
3491 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3492 .accessfn = gt_cntpoff_access, .writefn = gt_cntpoff_write,
3493 .nv2_redirect_offset = 0x1a8,
3494 .fieldoffset = offsetof(CPUARMState, cp15.cntpoff_el2),
3495 };
3496 #else
3497
3498 /*
3499 * In user-mode most of the generic timer registers are inaccessible
3500 * however modern kernels (4.12+) allow access to cntvct_el0
3501 */
3502
gt_virt_cnt_read(CPUARMState * env,const ARMCPRegInfo * ri)3503 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3504 {
3505 ARMCPU *cpu = env_archcpu(env);
3506
3507 /*
3508 * Currently we have no support for QEMUTimer in linux-user so we
3509 * can't call gt_get_countervalue(env), instead we directly
3510 * call the lower level functions.
3511 */
3512 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3513 }
3514
3515 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3516 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3517 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3518 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3519 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3520 .resetfn = arm_gt_cntfrq_reset,
3521 },
3522 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3523 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3524 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3525 .readfn = gt_virt_cnt_read,
3526 },
3527 };
3528
3529 /*
3530 * CNTVCTSS_EL0 has the same trap conditions as CNTVCT_EL0, so it also
3531 * is exposed to userspace by Linux.
3532 */
3533 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3534 { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3535 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3536 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3537 .readfn = gt_virt_cnt_read,
3538 },
3539 };
3540
3541 #endif
3542
par_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3543 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3544 {
3545 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3546 raw_write(env, ri, value);
3547 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3548 raw_write(env, ri, value & 0xfffff6ff);
3549 } else {
3550 raw_write(env, ri, value & 0xfffff1ff);
3551 }
3552 }
3553
3554 #ifndef CONFIG_USER_ONLY
3555 /* get_phys_addr() isn't present for user-mode-only targets */
3556
ats_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)3557 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3558 bool isread)
3559 {
3560 if (ri->opc2 & 4) {
3561 /*
3562 * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3563 * Secure EL1 (which can only happen if EL3 is AArch64).
3564 * They are simply UNDEF if executed from NS EL1.
3565 * They function normally from EL2 or EL3.
3566 */
3567 if (arm_current_el(env) == 1) {
3568 if (arm_is_secure_below_el3(env)) {
3569 if (env->cp15.scr_el3 & SCR_EEL2) {
3570 return CP_ACCESS_TRAP_EL2;
3571 }
3572 return CP_ACCESS_TRAP_EL3;
3573 }
3574 return CP_ACCESS_TRAP_UNCATEGORIZED;
3575 }
3576 }
3577 return CP_ACCESS_OK;
3578 }
3579
3580 #ifdef CONFIG_TCG
par_el1_shareability(GetPhysAddrResult * res)3581 static int par_el1_shareability(GetPhysAddrResult *res)
3582 {
3583 /*
3584 * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3585 * memory -- see pseudocode PAREncodeShareability().
3586 */
3587 if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3588 res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3589 return 2;
3590 }
3591 return res->cacheattrs.shareability;
3592 }
3593
do_ats_write(CPUARMState * env,uint64_t value,MMUAccessType access_type,ARMMMUIdx mmu_idx,ARMSecuritySpace ss)3594 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3595 MMUAccessType access_type, ARMMMUIdx mmu_idx,
3596 ARMSecuritySpace ss)
3597 {
3598 bool ret;
3599 uint64_t par64;
3600 bool format64 = false;
3601 ARMMMUFaultInfo fi = {};
3602 GetPhysAddrResult res = {};
3603
3604 /*
3605 * I_MXTJT: Granule protection checks are not performed on the final address
3606 * of a successful translation.
3607 */
3608 ret = get_phys_addr_with_space_nogpc(env, value, access_type, mmu_idx, ss,
3609 &res, &fi);
3610
3611 /*
3612 * ATS operations only do S1 or S1+S2 translations, so we never
3613 * have to deal with the ARMCacheAttrs format for S2 only.
3614 */
3615 assert(!res.cacheattrs.is_s2_format);
3616
3617 if (ret) {
3618 /*
3619 * Some kinds of translation fault must cause exceptions rather
3620 * than being reported in the PAR.
3621 */
3622 int current_el = arm_current_el(env);
3623 int target_el;
3624 uint32_t syn, fsr, fsc;
3625 bool take_exc = false;
3626
3627 if (fi.s1ptw && current_el == 1
3628 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3629 /*
3630 * Synchronous stage 2 fault on an access made as part of the
3631 * translation table walk for AT S1E0* or AT S1E1* insn
3632 * executed from NS EL1. If this is a synchronous external abort
3633 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3634 * to EL3. Otherwise the fault is taken as an exception to EL2,
3635 * and HPFAR_EL2 holds the faulting IPA.
3636 */
3637 if (fi.type == ARMFault_SyncExternalOnWalk &&
3638 (env->cp15.scr_el3 & SCR_EA)) {
3639 target_el = 3;
3640 } else {
3641 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3642 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3643 env->cp15.hpfar_el2 |= HPFAR_NS;
3644 }
3645 target_el = 2;
3646 }
3647 take_exc = true;
3648 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3649 /*
3650 * Synchronous external aborts during a translation table walk
3651 * are taken as Data Abort exceptions.
3652 */
3653 if (fi.stage2) {
3654 if (current_el == 3) {
3655 target_el = 3;
3656 } else {
3657 target_el = 2;
3658 }
3659 } else {
3660 target_el = exception_target_el(env);
3661 }
3662 take_exc = true;
3663 }
3664
3665 if (take_exc) {
3666 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3667 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3668 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3669 fsr = arm_fi_to_lfsc(&fi);
3670 fsc = extract32(fsr, 0, 6);
3671 } else {
3672 fsr = arm_fi_to_sfsc(&fi);
3673 fsc = 0x3f;
3674 }
3675 /*
3676 * Report exception with ESR indicating a fault due to a
3677 * translation table walk for a cache maintenance instruction.
3678 */
3679 syn = syn_data_abort_no_iss(current_el == target_el, 0,
3680 fi.ea, 1, fi.s1ptw, 1, fsc);
3681 env->exception.vaddress = value;
3682 env->exception.fsr = fsr;
3683 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3684 }
3685 }
3686
3687 if (is_a64(env)) {
3688 format64 = true;
3689 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3690 /*
3691 * ATS1Cxx:
3692 * * TTBCR.EAE determines whether the result is returned using the
3693 * 32-bit or the 64-bit PAR format
3694 * * Instructions executed in Hyp mode always use the 64bit format
3695 *
3696 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3697 * * The Non-secure TTBCR.EAE bit is set to 1
3698 * * The implementation includes EL2, and the value of HCR.VM is 1
3699 *
3700 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3701 *
3702 * ATS1Hx always uses the 64bit format.
3703 */
3704 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3705
3706 if (arm_feature(env, ARM_FEATURE_EL2)) {
3707 if (mmu_idx == ARMMMUIdx_E10_0 ||
3708 mmu_idx == ARMMMUIdx_E10_1 ||
3709 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3710 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3711 } else {
3712 format64 |= arm_current_el(env) == 2;
3713 }
3714 }
3715 }
3716
3717 if (format64) {
3718 /* Create a 64-bit PAR */
3719 par64 = (1 << 11); /* LPAE bit always set */
3720 if (!ret) {
3721 par64 |= res.f.phys_addr & ~0xfffULL;
3722 if (!res.f.attrs.secure) {
3723 par64 |= (1 << 9); /* NS */
3724 }
3725 par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3726 par64 |= par_el1_shareability(&res) << 7; /* SH */
3727 } else {
3728 uint32_t fsr = arm_fi_to_lfsc(&fi);
3729
3730 par64 |= 1; /* F */
3731 par64 |= (fsr & 0x3f) << 1; /* FS */
3732 if (fi.stage2) {
3733 par64 |= (1 << 9); /* S */
3734 }
3735 if (fi.s1ptw) {
3736 par64 |= (1 << 8); /* PTW */
3737 }
3738 }
3739 } else {
3740 /*
3741 * fsr is a DFSR/IFSR value for the short descriptor
3742 * translation table format (with WnR always clear).
3743 * Convert it to a 32-bit PAR.
3744 */
3745 if (!ret) {
3746 /* We do not set any attribute bits in the PAR */
3747 if (res.f.lg_page_size == 24
3748 && arm_feature(env, ARM_FEATURE_V7)) {
3749 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3750 } else {
3751 par64 = res.f.phys_addr & 0xfffff000;
3752 }
3753 if (!res.f.attrs.secure) {
3754 par64 |= (1 << 9); /* NS */
3755 }
3756 } else {
3757 uint32_t fsr = arm_fi_to_sfsc(&fi);
3758
3759 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3760 ((fsr & 0xf) << 1) | 1;
3761 }
3762 }
3763 return par64;
3764 }
3765 #endif /* CONFIG_TCG */
3766
ats_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3767 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3768 {
3769 #ifdef CONFIG_TCG
3770 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3771 uint64_t par64;
3772 ARMMMUIdx mmu_idx;
3773 int el = arm_current_el(env);
3774 ARMSecuritySpace ss = arm_security_space(env);
3775
3776 switch (ri->opc2 & 6) {
3777 case 0:
3778 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3779 switch (el) {
3780 case 3:
3781 if (ri->crm == 9 && arm_pan_enabled(env)) {
3782 mmu_idx = ARMMMUIdx_E30_3_PAN;
3783 } else {
3784 mmu_idx = ARMMMUIdx_E3;
3785 }
3786 break;
3787 case 2:
3788 g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */
3789 /* fall through */
3790 case 1:
3791 if (ri->crm == 9 && arm_pan_enabled(env)) {
3792 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3793 } else {
3794 mmu_idx = ARMMMUIdx_Stage1_E1;
3795 }
3796 break;
3797 default:
3798 g_assert_not_reached();
3799 }
3800 break;
3801 case 2:
3802 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3803 switch (el) {
3804 case 3:
3805 mmu_idx = ARMMMUIdx_E30_0;
3806 break;
3807 case 2:
3808 g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */
3809 mmu_idx = ARMMMUIdx_Stage1_E0;
3810 break;
3811 case 1:
3812 mmu_idx = ARMMMUIdx_Stage1_E0;
3813 break;
3814 default:
3815 g_assert_not_reached();
3816 }
3817 break;
3818 case 4:
3819 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3820 mmu_idx = ARMMMUIdx_E10_1;
3821 ss = ARMSS_NonSecure;
3822 break;
3823 case 6:
3824 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3825 mmu_idx = ARMMMUIdx_E10_0;
3826 ss = ARMSS_NonSecure;
3827 break;
3828 default:
3829 g_assert_not_reached();
3830 }
3831
3832 par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
3833
3834 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3835 #else
3836 /* Handled by hardware accelerator. */
3837 g_assert_not_reached();
3838 #endif /* CONFIG_TCG */
3839 }
3840
ats1h_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3841 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3842 uint64_t value)
3843 {
3844 #ifdef CONFIG_TCG
3845 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3846 uint64_t par64;
3847
3848 /* There is no SecureEL2 for AArch32. */
3849 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
3850 ARMSS_NonSecure);
3851
3852 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3853 #else
3854 /* Handled by hardware accelerator. */
3855 g_assert_not_reached();
3856 #endif /* CONFIG_TCG */
3857 }
3858
at_e012_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)3859 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
3860 bool isread)
3861 {
3862 /*
3863 * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
3864 * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
3865 * only happen when executing at EL3 because that combination also causes an
3866 * illegal exception return. We don't need to check FEAT_RME either, because
3867 * scr_write() ensures that the NSE bit is not set otherwise.
3868 */
3869 if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
3870 return CP_ACCESS_TRAP;
3871 }
3872 return CP_ACCESS_OK;
3873 }
3874
at_s1e2_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)3875 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3876 bool isread)
3877 {
3878 if (arm_current_el(env) == 3 &&
3879 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3880 return CP_ACCESS_TRAP;
3881 }
3882 return at_e012_access(env, ri, isread);
3883 }
3884
at_s1e01_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)3885 static CPAccessResult at_s1e01_access(CPUARMState *env, const ARMCPRegInfo *ri,
3886 bool isread)
3887 {
3888 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_AT)) {
3889 return CP_ACCESS_TRAP_EL2;
3890 }
3891 return at_e012_access(env, ri, isread);
3892 }
3893
ats_write64(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3894 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3895 uint64_t value)
3896 {
3897 #ifdef CONFIG_TCG
3898 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3899 ARMMMUIdx mmu_idx;
3900 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3901 bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3902 bool for_el3 = false;
3903 ARMSecuritySpace ss;
3904
3905 switch (ri->opc2 & 6) {
3906 case 0:
3907 switch (ri->opc1) {
3908 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3909 if (ri->crm == 9 && arm_pan_enabled(env)) {
3910 mmu_idx = regime_e20 ?
3911 ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3912 } else {
3913 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3914 }
3915 break;
3916 case 4: /* AT S1E2R, AT S1E2W */
3917 mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3918 break;
3919 case 6: /* AT S1E3R, AT S1E3W */
3920 mmu_idx = ARMMMUIdx_E3;
3921 for_el3 = true;
3922 break;
3923 default:
3924 g_assert_not_reached();
3925 }
3926 break;
3927 case 2: /* AT S1E0R, AT S1E0W */
3928 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3929 break;
3930 case 4: /* AT S12E1R, AT S12E1W */
3931 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3932 break;
3933 case 6: /* AT S12E0R, AT S12E0W */
3934 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3935 break;
3936 default:
3937 g_assert_not_reached();
3938 }
3939
3940 ss = for_el3 ? arm_security_space(env) : arm_security_space_below_el3(env);
3941 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx, ss);
3942 #else
3943 /* Handled by hardware accelerator. */
3944 g_assert_not_reached();
3945 #endif /* CONFIG_TCG */
3946 }
3947 #endif
3948
3949 /* Return basic MPU access permission bits. */
simple_mpu_ap_bits(uint32_t val)3950 static uint32_t simple_mpu_ap_bits(uint32_t val)
3951 {
3952 uint32_t ret;
3953 uint32_t mask;
3954 int i;
3955 ret = 0;
3956 mask = 3;
3957 for (i = 0; i < 16; i += 2) {
3958 ret |= (val >> i) & mask;
3959 mask <<= 2;
3960 }
3961 return ret;
3962 }
3963
3964 /* Pad basic MPU access permission bits to extended format. */
extended_mpu_ap_bits(uint32_t val)3965 static uint32_t extended_mpu_ap_bits(uint32_t val)
3966 {
3967 uint32_t ret;
3968 uint32_t mask;
3969 int i;
3970 ret = 0;
3971 mask = 3;
3972 for (i = 0; i < 16; i += 2) {
3973 ret |= (val & mask) << i;
3974 mask <<= 2;
3975 }
3976 return ret;
3977 }
3978
pmsav5_data_ap_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3979 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3980 uint64_t value)
3981 {
3982 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3983 }
3984
pmsav5_data_ap_read(CPUARMState * env,const ARMCPRegInfo * ri)3985 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3986 {
3987 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3988 }
3989
pmsav5_insn_ap_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3990 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3991 uint64_t value)
3992 {
3993 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3994 }
3995
pmsav5_insn_ap_read(CPUARMState * env,const ARMCPRegInfo * ri)3996 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3997 {
3998 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3999 }
4000
pmsav7_read(CPUARMState * env,const ARMCPRegInfo * ri)4001 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
4002 {
4003 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
4004
4005 if (!u32p) {
4006 return 0;
4007 }
4008
4009 u32p += env->pmsav7.rnr[M_REG_NS];
4010 return *u32p;
4011 }
4012
pmsav7_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4013 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
4014 uint64_t value)
4015 {
4016 ARMCPU *cpu = env_archcpu(env);
4017 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
4018
4019 if (!u32p) {
4020 return;
4021 }
4022
4023 u32p += env->pmsav7.rnr[M_REG_NS];
4024 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4025 *u32p = value;
4026 }
4027
pmsav7_rgnr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4028 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4029 uint64_t value)
4030 {
4031 ARMCPU *cpu = env_archcpu(env);
4032 uint32_t nrgs = cpu->pmsav7_dregion;
4033
4034 if (value >= nrgs) {
4035 qemu_log_mask(LOG_GUEST_ERROR,
4036 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
4037 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
4038 return;
4039 }
4040
4041 raw_write(env, ri, value);
4042 }
4043
prbar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4044 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4045 uint64_t value)
4046 {
4047 ARMCPU *cpu = env_archcpu(env);
4048
4049 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4050 env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
4051 }
4052
prbar_read(CPUARMState * env,const ARMCPRegInfo * ri)4053 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4054 {
4055 return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
4056 }
4057
prlar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4058 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4059 uint64_t value)
4060 {
4061 ARMCPU *cpu = env_archcpu(env);
4062
4063 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4064 env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
4065 }
4066
prlar_read(CPUARMState * env,const ARMCPRegInfo * ri)4067 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4068 {
4069 return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
4070 }
4071
prselr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4072 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4073 uint64_t value)
4074 {
4075 ARMCPU *cpu = env_archcpu(env);
4076
4077 /*
4078 * Ignore writes that would select not implemented region.
4079 * This is architecturally UNPREDICTABLE.
4080 */
4081 if (value >= cpu->pmsav7_dregion) {
4082 return;
4083 }
4084
4085 env->pmsav7.rnr[M_REG_NS] = value;
4086 }
4087
hprbar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4088 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4089 uint64_t value)
4090 {
4091 ARMCPU *cpu = env_archcpu(env);
4092
4093 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4094 env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
4095 }
4096
hprbar_read(CPUARMState * env,const ARMCPRegInfo * ri)4097 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4098 {
4099 return env->pmsav8.hprbar[env->pmsav8.hprselr];
4100 }
4101
hprlar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4102 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4103 uint64_t value)
4104 {
4105 ARMCPU *cpu = env_archcpu(env);
4106
4107 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4108 env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
4109 }
4110
hprlar_read(CPUARMState * env,const ARMCPRegInfo * ri)4111 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4112 {
4113 return env->pmsav8.hprlar[env->pmsav8.hprselr];
4114 }
4115
hprenr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4116 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4117 uint64_t value)
4118 {
4119 uint32_t n;
4120 uint32_t bit;
4121 ARMCPU *cpu = env_archcpu(env);
4122
4123 /* Ignore writes to unimplemented regions */
4124 int rmax = MIN(cpu->pmsav8r_hdregion, 32);
4125 value &= MAKE_64BIT_MASK(0, rmax);
4126
4127 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4128
4129 /* Register alias is only valid for first 32 indexes */
4130 for (n = 0; n < rmax; ++n) {
4131 bit = extract32(value, n, 1);
4132 env->pmsav8.hprlar[n] = deposit32(
4133 env->pmsav8.hprlar[n], 0, 1, bit);
4134 }
4135 }
4136
hprenr_read(CPUARMState * env,const ARMCPRegInfo * ri)4137 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4138 {
4139 uint32_t n;
4140 uint32_t result = 0x0;
4141 ARMCPU *cpu = env_archcpu(env);
4142
4143 /* Register alias is only valid for first 32 indexes */
4144 for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
4145 if (env->pmsav8.hprlar[n] & 0x1) {
4146 result |= (0x1 << n);
4147 }
4148 }
4149 return result;
4150 }
4151
hprselr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4152 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4153 uint64_t value)
4154 {
4155 ARMCPU *cpu = env_archcpu(env);
4156
4157 /*
4158 * Ignore writes that would select not implemented region.
4159 * This is architecturally UNPREDICTABLE.
4160 */
4161 if (value >= cpu->pmsav8r_hdregion) {
4162 return;
4163 }
4164
4165 env->pmsav8.hprselr = value;
4166 }
4167
pmsav8r_regn_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4168 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
4169 uint64_t value)
4170 {
4171 ARMCPU *cpu = env_archcpu(env);
4172 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4173 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4174
4175 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4176
4177 if (ri->opc1 & 4) {
4178 if (index >= cpu->pmsav8r_hdregion) {
4179 return;
4180 }
4181 if (ri->opc2 & 0x1) {
4182 env->pmsav8.hprlar[index] = value;
4183 } else {
4184 env->pmsav8.hprbar[index] = value;
4185 }
4186 } else {
4187 if (index >= cpu->pmsav7_dregion) {
4188 return;
4189 }
4190 if (ri->opc2 & 0x1) {
4191 env->pmsav8.rlar[M_REG_NS][index] = value;
4192 } else {
4193 env->pmsav8.rbar[M_REG_NS][index] = value;
4194 }
4195 }
4196 }
4197
pmsav8r_regn_read(CPUARMState * env,const ARMCPRegInfo * ri)4198 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
4199 {
4200 ARMCPU *cpu = env_archcpu(env);
4201 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4202 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4203
4204 if (ri->opc1 & 4) {
4205 if (index >= cpu->pmsav8r_hdregion) {
4206 return 0x0;
4207 }
4208 if (ri->opc2 & 0x1) {
4209 return env->pmsav8.hprlar[index];
4210 } else {
4211 return env->pmsav8.hprbar[index];
4212 }
4213 } else {
4214 if (index >= cpu->pmsav7_dregion) {
4215 return 0x0;
4216 }
4217 if (ri->opc2 & 0x1) {
4218 return env->pmsav8.rlar[M_REG_NS][index];
4219 } else {
4220 return env->pmsav8.rbar[M_REG_NS][index];
4221 }
4222 }
4223 }
4224
4225 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
4226 { .name = "PRBAR",
4227 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
4228 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4229 .accessfn = access_tvm_trvm,
4230 .readfn = prbar_read, .writefn = prbar_write },
4231 { .name = "PRLAR",
4232 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
4233 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4234 .accessfn = access_tvm_trvm,
4235 .readfn = prlar_read, .writefn = prlar_write },
4236 { .name = "PRSELR", .resetvalue = 0,
4237 .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
4238 .access = PL1_RW, .accessfn = access_tvm_trvm,
4239 .writefn = prselr_write,
4240 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
4241 { .name = "HPRBAR", .resetvalue = 0,
4242 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
4243 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4244 .readfn = hprbar_read, .writefn = hprbar_write },
4245 { .name = "HPRLAR",
4246 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
4247 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4248 .readfn = hprlar_read, .writefn = hprlar_write },
4249 { .name = "HPRSELR", .resetvalue = 0,
4250 .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
4251 .access = PL2_RW,
4252 .writefn = hprselr_write,
4253 .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
4254 { .name = "HPRENR",
4255 .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
4256 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4257 .readfn = hprenr_read, .writefn = hprenr_write },
4258 };
4259
4260 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
4261 /*
4262 * Reset for all these registers is handled in arm_cpu_reset(),
4263 * because the PMSAv7 is also used by M-profile CPUs, which do
4264 * not register cpregs but still need the state to be reset.
4265 */
4266 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4267 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4268 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
4269 .readfn = pmsav7_read, .writefn = pmsav7_write,
4270 .resetfn = arm_cp_reset_ignore },
4271 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4272 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4273 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
4274 .readfn = pmsav7_read, .writefn = pmsav7_write,
4275 .resetfn = arm_cp_reset_ignore },
4276 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4277 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4278 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
4279 .readfn = pmsav7_read, .writefn = pmsav7_write,
4280 .resetfn = arm_cp_reset_ignore },
4281 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4282 .access = PL1_RW,
4283 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
4284 .writefn = pmsav7_rgnr_write,
4285 .resetfn = arm_cp_reset_ignore },
4286 };
4287
4288 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4289 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4290 .access = PL1_RW, .type = ARM_CP_ALIAS,
4291 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4292 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4293 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4294 .access = PL1_RW, .type = ARM_CP_ALIAS,
4295 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4296 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4297 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4298 .access = PL1_RW,
4299 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4300 .resetvalue = 0, },
4301 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4302 .access = PL1_RW,
4303 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4304 .resetvalue = 0, },
4305 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4306 .access = PL1_RW,
4307 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4308 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4309 .access = PL1_RW,
4310 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
4311 /* Protection region base and size registers */
4312 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4313 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4314 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4315 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4316 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4317 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4318 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4319 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4320 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4321 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4322 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4323 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4324 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4325 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4326 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4327 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4328 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4329 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4330 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4331 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4332 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4333 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4334 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4335 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4336 };
4337
vmsa_ttbcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4338 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4339 uint64_t value)
4340 {
4341 ARMCPU *cpu = env_archcpu(env);
4342
4343 if (!arm_feature(env, ARM_FEATURE_V8)) {
4344 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4345 /*
4346 * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4347 * using Long-descriptor translation table format
4348 */
4349 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4350 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4351 /*
4352 * In an implementation that includes the Security Extensions
4353 * TTBCR has additional fields PD0 [4] and PD1 [5] for
4354 * Short-descriptor translation table format.
4355 */
4356 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4357 } else {
4358 value &= TTBCR_N;
4359 }
4360 }
4361
4362 if (arm_feature(env, ARM_FEATURE_LPAE)) {
4363 /*
4364 * With LPAE the TTBCR could result in a change of ASID
4365 * via the TTBCR.A1 bit, so do a TLB flush.
4366 */
4367 tlb_flush(CPU(cpu));
4368 }
4369 raw_write(env, ri, value);
4370 }
4371
vmsa_tcr_el12_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4372 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4373 uint64_t value)
4374 {
4375 ARMCPU *cpu = env_archcpu(env);
4376
4377 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4378 tlb_flush(CPU(cpu));
4379 raw_write(env, ri, value);
4380 }
4381
vmsa_ttbr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4382 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4383 uint64_t value)
4384 {
4385 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
4386 if (cpreg_field_is_64bit(ri) &&
4387 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4388 ARMCPU *cpu = env_archcpu(env);
4389 tlb_flush(CPU(cpu));
4390 }
4391 raw_write(env, ri, value);
4392 }
4393
vmsa_tcr_ttbr_el2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4394 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4395 uint64_t value)
4396 {
4397 /*
4398 * If we are running with E2&0 regime, then an ASID is active.
4399 * Flush if that might be changing. Note we're not checking
4400 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4401 * holds the active ASID, only checking the field that might.
4402 */
4403 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4404 (arm_hcr_el2_eff(env) & HCR_E2H)) {
4405 uint16_t mask = ARMMMUIdxBit_E20_2 |
4406 ARMMMUIdxBit_E20_2_PAN |
4407 ARMMMUIdxBit_E20_0;
4408 tlb_flush_by_mmuidx(env_cpu(env), mask);
4409 }
4410 raw_write(env, ri, value);
4411 }
4412
vttbr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4413 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4414 uint64_t value)
4415 {
4416 ARMCPU *cpu = env_archcpu(env);
4417 CPUState *cs = CPU(cpu);
4418
4419 /*
4420 * A change in VMID to the stage2 page table (Stage2) invalidates
4421 * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4422 */
4423 if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4424 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4425 }
4426 raw_write(env, ri, value);
4427 }
4428
4429 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4430 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4431 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4432 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4433 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4434 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4435 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4436 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4437 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4438 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4439 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4440 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4441 offsetof(CPUARMState, cp15.dfar_ns) } },
4442 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4443 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4444 .access = PL1_RW, .accessfn = access_tvm_trvm,
4445 .fgt = FGT_FAR_EL1,
4446 .nv2_redirect_offset = 0x220 | NV2_REDIR_NV1,
4447 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4448 .resetvalue = 0, },
4449 };
4450
4451 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4452 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4453 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4454 .access = PL1_RW, .accessfn = access_tvm_trvm,
4455 .fgt = FGT_ESR_EL1,
4456 .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1,
4457 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4458 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4459 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4460 .access = PL1_RW, .accessfn = access_tvm_trvm,
4461 .fgt = FGT_TTBR0_EL1,
4462 .nv2_redirect_offset = 0x200 | NV2_REDIR_NV1,
4463 .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4464 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4465 offsetof(CPUARMState, cp15.ttbr0_ns) } },
4466 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4467 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4468 .access = PL1_RW, .accessfn = access_tvm_trvm,
4469 .fgt = FGT_TTBR1_EL1,
4470 .nv2_redirect_offset = 0x210 | NV2_REDIR_NV1,
4471 .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4472 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4473 offsetof(CPUARMState, cp15.ttbr1_ns) } },
4474 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4475 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4476 .access = PL1_RW, .accessfn = access_tvm_trvm,
4477 .fgt = FGT_TCR_EL1,
4478 .nv2_redirect_offset = 0x120 | NV2_REDIR_NV1,
4479 .writefn = vmsa_tcr_el12_write,
4480 .raw_writefn = raw_write,
4481 .resetvalue = 0,
4482 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4483 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4484 .access = PL1_RW, .accessfn = access_tvm_trvm,
4485 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4486 .raw_writefn = raw_write,
4487 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4488 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4489 };
4490
4491 /*
4492 * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4493 * qemu tlbs nor adjusting cached masks.
4494 */
4495 static const ARMCPRegInfo ttbcr2_reginfo = {
4496 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4497 .access = PL1_RW, .accessfn = access_tvm_trvm,
4498 .type = ARM_CP_ALIAS,
4499 .bank_fieldoffsets = {
4500 offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4501 offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4502 },
4503 };
4504
omap_ticonfig_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4505 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4506 uint64_t value)
4507 {
4508 env->cp15.c15_ticonfig = value & 0xe7;
4509 /* The OS_TYPE bit in this register changes the reported CPUID! */
4510 env->cp15.c0_cpuid = (value & (1 << 5)) ?
4511 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4512 }
4513
omap_threadid_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4514 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4515 uint64_t value)
4516 {
4517 env->cp15.c15_threadid = value & 0xffff;
4518 }
4519
omap_wfi_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4520 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4521 uint64_t value)
4522 {
4523 /* Wait-for-interrupt (deprecated) */
4524 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4525 }
4526
omap_cachemaint_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4527 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4528 uint64_t value)
4529 {
4530 /*
4531 * On OMAP there are registers indicating the max/min index of dcache lines
4532 * containing a dirty line; cache flush operations have to reset these.
4533 */
4534 env->cp15.c15_i_max = 0x000;
4535 env->cp15.c15_i_min = 0xff0;
4536 }
4537
4538 static const ARMCPRegInfo omap_cp_reginfo[] = {
4539 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4540 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4541 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4542 .resetvalue = 0, },
4543 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4544 .access = PL1_RW, .type = ARM_CP_NOP },
4545 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4546 .access = PL1_RW,
4547 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4548 .writefn = omap_ticonfig_write },
4549 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4550 .access = PL1_RW,
4551 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4552 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4553 .access = PL1_RW, .resetvalue = 0xff0,
4554 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4555 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4556 .access = PL1_RW,
4557 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4558 .writefn = omap_threadid_write },
4559 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4560 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4561 .type = ARM_CP_NO_RAW,
4562 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4563 /*
4564 * TODO: Peripheral port remap register:
4565 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4566 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4567 * when MMU is off.
4568 */
4569 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4570 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4571 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4572 .writefn = omap_cachemaint_write },
4573 { .name = "C9", .cp = 15, .crn = 9,
4574 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4575 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4576 };
4577
xscale_cpar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4578 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4579 uint64_t value)
4580 {
4581 env->cp15.c15_cpar = value & 0x3fff;
4582 }
4583
4584 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4585 { .name = "XSCALE_CPAR",
4586 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4587 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4588 .writefn = xscale_cpar_write, },
4589 { .name = "XSCALE_AUXCR",
4590 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4591 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4592 .resetvalue = 0, },
4593 /*
4594 * XScale specific cache-lockdown: since we have no cache we NOP these
4595 * and hope the guest does not really rely on cache behaviour.
4596 */
4597 { .name = "XSCALE_LOCK_ICACHE_LINE",
4598 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4599 .access = PL1_W, .type = ARM_CP_NOP },
4600 { .name = "XSCALE_UNLOCK_ICACHE",
4601 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4602 .access = PL1_W, .type = ARM_CP_NOP },
4603 { .name = "XSCALE_DCACHE_LOCK",
4604 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4605 .access = PL1_RW, .type = ARM_CP_NOP },
4606 { .name = "XSCALE_UNLOCK_DCACHE",
4607 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4608 .access = PL1_W, .type = ARM_CP_NOP },
4609 };
4610
4611 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4612 /*
4613 * RAZ/WI the whole crn=15 space, when we don't have a more specific
4614 * implementation of this implementation-defined space.
4615 * Ideally this should eventually disappear in favour of actually
4616 * implementing the correct behaviour for all cores.
4617 */
4618 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4619 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4620 .access = PL1_RW,
4621 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4622 .resetvalue = 0 },
4623 };
4624
4625 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4626 /* Cache status: RAZ because we have no cache so it's always clean */
4627 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4628 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4629 .resetvalue = 0 },
4630 };
4631
4632 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4633 /* We never have a block transfer operation in progress */
4634 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4635 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4636 .resetvalue = 0 },
4637 /* The cache ops themselves: these all NOP for QEMU */
4638 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4639 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4640 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4641 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4642 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4643 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4644 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4645 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4646 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4647 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4648 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4649 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4650 };
4651
4652 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4653 /*
4654 * The cache test-and-clean instructions always return (1 << 30)
4655 * to indicate that there are no dirty cache lines.
4656 */
4657 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4658 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4659 .resetvalue = (1 << 30) },
4660 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4661 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4662 .resetvalue = (1 << 30) },
4663 };
4664
4665 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4666 /* Ignore ReadBuffer accesses */
4667 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4668 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4669 .access = PL1_RW, .resetvalue = 0,
4670 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4671 };
4672
midr_read(CPUARMState * env,const ARMCPRegInfo * ri)4673 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4674 {
4675 unsigned int cur_el = arm_current_el(env);
4676
4677 if (arm_is_el2_enabled(env) && cur_el == 1) {
4678 return env->cp15.vpidr_el2;
4679 }
4680 return raw_read(env, ri);
4681 }
4682
mpidr_read_val(CPUARMState * env)4683 static uint64_t mpidr_read_val(CPUARMState *env)
4684 {
4685 ARMCPU *cpu = env_archcpu(env);
4686 uint64_t mpidr = cpu->mp_affinity;
4687
4688 if (arm_feature(env, ARM_FEATURE_V7MP)) {
4689 mpidr |= (1U << 31);
4690 /*
4691 * Cores which are uniprocessor (non-coherent)
4692 * but still implement the MP extensions set
4693 * bit 30. (For instance, Cortex-R5).
4694 */
4695 if (cpu->mp_is_up) {
4696 mpidr |= (1u << 30);
4697 }
4698 }
4699 return mpidr;
4700 }
4701
mpidr_read(CPUARMState * env,const ARMCPRegInfo * ri)4702 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4703 {
4704 unsigned int cur_el = arm_current_el(env);
4705
4706 if (arm_is_el2_enabled(env) && cur_el == 1) {
4707 return env->cp15.vmpidr_el2;
4708 }
4709 return mpidr_read_val(env);
4710 }
4711
4712 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4713 /* NOP AMAIR0/1 */
4714 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4715 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4716 .access = PL1_RW, .accessfn = access_tvm_trvm,
4717 .fgt = FGT_AMAIR_EL1,
4718 .nv2_redirect_offset = 0x148 | NV2_REDIR_NV1,
4719 .type = ARM_CP_CONST, .resetvalue = 0 },
4720 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4721 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4722 .access = PL1_RW, .accessfn = access_tvm_trvm,
4723 .type = ARM_CP_CONST, .resetvalue = 0 },
4724 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4725 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4726 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4727 offsetof(CPUARMState, cp15.par_ns)} },
4728 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4729 .access = PL1_RW, .accessfn = access_tvm_trvm,
4730 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4731 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4732 offsetof(CPUARMState, cp15.ttbr0_ns) },
4733 .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4734 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4735 .access = PL1_RW, .accessfn = access_tvm_trvm,
4736 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4737 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4738 offsetof(CPUARMState, cp15.ttbr1_ns) },
4739 .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4740 };
4741
aa64_fpcr_read(CPUARMState * env,const ARMCPRegInfo * ri)4742 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4743 {
4744 return vfp_get_fpcr(env);
4745 }
4746
aa64_fpcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4747 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4748 uint64_t value)
4749 {
4750 vfp_set_fpcr(env, value);
4751 }
4752
aa64_fpsr_read(CPUARMState * env,const ARMCPRegInfo * ri)4753 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4754 {
4755 return vfp_get_fpsr(env);
4756 }
4757
aa64_fpsr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4758 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4759 uint64_t value)
4760 {
4761 vfp_set_fpsr(env, value);
4762 }
4763
aa64_daif_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4764 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4765 bool isread)
4766 {
4767 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4768 return CP_ACCESS_TRAP;
4769 }
4770 return CP_ACCESS_OK;
4771 }
4772
aa64_daif_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4773 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4774 uint64_t value)
4775 {
4776 env->daif = value & PSTATE_DAIF;
4777 }
4778
aa64_pan_read(CPUARMState * env,const ARMCPRegInfo * ri)4779 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4780 {
4781 return env->pstate & PSTATE_PAN;
4782 }
4783
aa64_pan_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4784 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4785 uint64_t value)
4786 {
4787 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4788 }
4789
4790 static const ARMCPRegInfo pan_reginfo = {
4791 .name = "PAN", .state = ARM_CP_STATE_AA64,
4792 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4793 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4794 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4795 };
4796
aa64_uao_read(CPUARMState * env,const ARMCPRegInfo * ri)4797 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4798 {
4799 return env->pstate & PSTATE_UAO;
4800 }
4801
aa64_uao_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4802 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4803 uint64_t value)
4804 {
4805 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4806 }
4807
4808 static const ARMCPRegInfo uao_reginfo = {
4809 .name = "UAO", .state = ARM_CP_STATE_AA64,
4810 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4811 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4812 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4813 };
4814
aa64_dit_read(CPUARMState * env,const ARMCPRegInfo * ri)4815 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4816 {
4817 return env->pstate & PSTATE_DIT;
4818 }
4819
aa64_dit_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4820 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4821 uint64_t value)
4822 {
4823 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4824 }
4825
4826 static const ARMCPRegInfo dit_reginfo = {
4827 .name = "DIT", .state = ARM_CP_STATE_AA64,
4828 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4829 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4830 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4831 };
4832
aa64_ssbs_read(CPUARMState * env,const ARMCPRegInfo * ri)4833 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4834 {
4835 return env->pstate & PSTATE_SSBS;
4836 }
4837
aa64_ssbs_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4838 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4839 uint64_t value)
4840 {
4841 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4842 }
4843
4844 static const ARMCPRegInfo ssbs_reginfo = {
4845 .name = "SSBS", .state = ARM_CP_STATE_AA64,
4846 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4847 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4848 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4849 };
4850
aa64_cacheop_poc_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4851 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4852 const ARMCPRegInfo *ri,
4853 bool isread)
4854 {
4855 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4856 switch (arm_current_el(env)) {
4857 case 0:
4858 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4859 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4860 return CP_ACCESS_TRAP;
4861 }
4862 /* fall through */
4863 case 1:
4864 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4865 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4866 return CP_ACCESS_TRAP_EL2;
4867 }
4868 break;
4869 }
4870 return CP_ACCESS_OK;
4871 }
4872
do_cacheop_pou_access(CPUARMState * env,uint64_t hcrflags)4873 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4874 {
4875 /* Cache invalidate/clean to Point of Unification... */
4876 switch (arm_current_el(env)) {
4877 case 0:
4878 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4879 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4880 return CP_ACCESS_TRAP;
4881 }
4882 /* fall through */
4883 case 1:
4884 /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set. */
4885 if (arm_hcr_el2_eff(env) & hcrflags) {
4886 return CP_ACCESS_TRAP_EL2;
4887 }
4888 break;
4889 }
4890 return CP_ACCESS_OK;
4891 }
4892
access_ticab(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4893 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4894 bool isread)
4895 {
4896 return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4897 }
4898
access_tocu(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4899 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4900 bool isread)
4901 {
4902 return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4903 }
4904
4905 /*
4906 * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
4907 * Page D4-1736 (DDI0487A.b)
4908 */
4909
vae1_tlbmask(CPUARMState * env)4910 static int vae1_tlbmask(CPUARMState *env)
4911 {
4912 uint64_t hcr = arm_hcr_el2_eff(env);
4913 uint16_t mask;
4914
4915 assert(arm_feature(env, ARM_FEATURE_AARCH64));
4916
4917 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4918 mask = ARMMMUIdxBit_E20_2 |
4919 ARMMMUIdxBit_E20_2_PAN |
4920 ARMMMUIdxBit_E20_0;
4921 } else {
4922 /* This is AArch64 only, so we don't need to touch the EL30_x TLBs */
4923 mask = ARMMMUIdxBit_E10_1 |
4924 ARMMMUIdxBit_E10_1_PAN |
4925 ARMMMUIdxBit_E10_0;
4926 }
4927 return mask;
4928 }
4929
vae2_tlbmask(CPUARMState * env)4930 static int vae2_tlbmask(CPUARMState *env)
4931 {
4932 uint64_t hcr = arm_hcr_el2_eff(env);
4933 uint16_t mask;
4934
4935 if (hcr & HCR_E2H) {
4936 mask = ARMMMUIdxBit_E20_2 |
4937 ARMMMUIdxBit_E20_2_PAN |
4938 ARMMMUIdxBit_E20_0;
4939 } else {
4940 mask = ARMMMUIdxBit_E2;
4941 }
4942 return mask;
4943 }
4944
4945 /* Return 56 if TBI is enabled, 64 otherwise. */
tlbbits_for_regime(CPUARMState * env,ARMMMUIdx mmu_idx,uint64_t addr)4946 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4947 uint64_t addr)
4948 {
4949 uint64_t tcr = regime_tcr(env, mmu_idx);
4950 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4951 int select = extract64(addr, 55, 1);
4952
4953 return (tbi >> select) & 1 ? 56 : 64;
4954 }
4955
vae1_tlbbits(CPUARMState * env,uint64_t addr)4956 static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4957 {
4958 uint64_t hcr = arm_hcr_el2_eff(env);
4959 ARMMMUIdx mmu_idx;
4960
4961 assert(arm_feature(env, ARM_FEATURE_AARCH64));
4962
4963 /* Only the regime of the mmu_idx below is significant. */
4964 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4965 mmu_idx = ARMMMUIdx_E20_0;
4966 } else {
4967 mmu_idx = ARMMMUIdx_E10_0;
4968 }
4969
4970 return tlbbits_for_regime(env, mmu_idx, addr);
4971 }
4972
vae2_tlbbits(CPUARMState * env,uint64_t addr)4973 static int vae2_tlbbits(CPUARMState *env, uint64_t addr)
4974 {
4975 uint64_t hcr = arm_hcr_el2_eff(env);
4976 ARMMMUIdx mmu_idx;
4977
4978 /*
4979 * Only the regime of the mmu_idx below is significant.
4980 * Regime EL2&0 has two ranges with separate TBI configuration, while EL2
4981 * only has one.
4982 */
4983 if (hcr & HCR_E2H) {
4984 mmu_idx = ARMMMUIdx_E20_2;
4985 } else {
4986 mmu_idx = ARMMMUIdx_E2;
4987 }
4988
4989 return tlbbits_for_regime(env, mmu_idx, addr);
4990 }
4991
tlbi_aa64_vmalle1is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4992 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4993 uint64_t value)
4994 {
4995 CPUState *cs = env_cpu(env);
4996 int mask = vae1_tlbmask(env);
4997
4998 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4999 }
5000
tlbi_aa64_vmalle1_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5001 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5002 uint64_t value)
5003 {
5004 CPUState *cs = env_cpu(env);
5005 int mask = vae1_tlbmask(env);
5006
5007 if (tlb_force_broadcast(env)) {
5008 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
5009 } else {
5010 tlb_flush_by_mmuidx(cs, mask);
5011 }
5012 }
5013
e2_tlbmask(CPUARMState * env)5014 static int e2_tlbmask(CPUARMState *env)
5015 {
5016 return (ARMMMUIdxBit_E20_0 |
5017 ARMMMUIdxBit_E20_2 |
5018 ARMMMUIdxBit_E20_2_PAN |
5019 ARMMMUIdxBit_E2);
5020 }
5021
tlbi_aa64_alle1_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5022 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5023 uint64_t value)
5024 {
5025 CPUState *cs = env_cpu(env);
5026 int mask = alle1_tlbmask(env);
5027
5028 tlb_flush_by_mmuidx(cs, mask);
5029 }
5030
tlbi_aa64_alle2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5031 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5032 uint64_t value)
5033 {
5034 CPUState *cs = env_cpu(env);
5035 int mask = e2_tlbmask(env);
5036
5037 tlb_flush_by_mmuidx(cs, mask);
5038 }
5039
tlbi_aa64_alle3_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5040 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5041 uint64_t value)
5042 {
5043 ARMCPU *cpu = env_archcpu(env);
5044 CPUState *cs = CPU(cpu);
5045
5046 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
5047 }
5048
tlbi_aa64_alle1is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5049 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5050 uint64_t value)
5051 {
5052 CPUState *cs = env_cpu(env);
5053 int mask = alle1_tlbmask(env);
5054
5055 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
5056 }
5057
tlbi_aa64_alle2is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5058 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5059 uint64_t value)
5060 {
5061 CPUState *cs = env_cpu(env);
5062 int mask = e2_tlbmask(env);
5063
5064 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
5065 }
5066
tlbi_aa64_alle3is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5067 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5068 uint64_t value)
5069 {
5070 CPUState *cs = env_cpu(env);
5071
5072 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
5073 }
5074
tlbi_aa64_vae2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5075 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5076 uint64_t value)
5077 {
5078 /*
5079 * Invalidate by VA, EL2
5080 * Currently handles both VAE2 and VALE2, since we don't support
5081 * flush-last-level-only.
5082 */
5083 CPUState *cs = env_cpu(env);
5084 int mask = vae2_tlbmask(env);
5085 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5086 int bits = vae2_tlbbits(env, pageaddr);
5087
5088 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
5089 }
5090
tlbi_aa64_vae3_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5091 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5092 uint64_t value)
5093 {
5094 /*
5095 * Invalidate by VA, EL3
5096 * Currently handles both VAE3 and VALE3, since we don't support
5097 * flush-last-level-only.
5098 */
5099 ARMCPU *cpu = env_archcpu(env);
5100 CPUState *cs = CPU(cpu);
5101 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5102
5103 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
5104 }
5105
tlbi_aa64_vae1is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5106 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5107 uint64_t value)
5108 {
5109 CPUState *cs = env_cpu(env);
5110 int mask = vae1_tlbmask(env);
5111 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5112 int bits = vae1_tlbbits(env, pageaddr);
5113
5114 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
5115 }
5116
tlbi_aa64_vae1_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5117 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5118 uint64_t value)
5119 {
5120 /*
5121 * Invalidate by VA, EL1&0 (AArch64 version).
5122 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
5123 * since we don't support flush-for-specific-ASID-only or
5124 * flush-last-level-only.
5125 */
5126 CPUState *cs = env_cpu(env);
5127 int mask = vae1_tlbmask(env);
5128 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5129 int bits = vae1_tlbbits(env, pageaddr);
5130
5131 if (tlb_force_broadcast(env)) {
5132 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
5133 } else {
5134 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
5135 }
5136 }
5137
tlbi_aa64_vae2is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5138 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5139 uint64_t value)
5140 {
5141 CPUState *cs = env_cpu(env);
5142 int mask = vae2_tlbmask(env);
5143 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5144 int bits = vae2_tlbbits(env, pageaddr);
5145
5146 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
5147 }
5148
tlbi_aa64_vae3is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5149 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5150 uint64_t value)
5151 {
5152 CPUState *cs = env_cpu(env);
5153 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5154 int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
5155
5156 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
5157 ARMMMUIdxBit_E3, bits);
5158 }
5159
ipas2e1_tlbmask(CPUARMState * env,int64_t value)5160 static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
5161 {
5162 /*
5163 * The MSB of value is the NS field, which only applies if SEL2
5164 * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
5165 */
5166 return (value >= 0
5167 && cpu_isar_feature(aa64_sel2, env_archcpu(env))
5168 && arm_is_secure_below_el3(env)
5169 ? ARMMMUIdxBit_Stage2_S
5170 : ARMMMUIdxBit_Stage2);
5171 }
5172
tlbi_aa64_ipas2e1_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5173 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5174 uint64_t value)
5175 {
5176 CPUState *cs = env_cpu(env);
5177 int mask = ipas2e1_tlbmask(env, value);
5178 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5179
5180 if (tlb_force_broadcast(env)) {
5181 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
5182 } else {
5183 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
5184 }
5185 }
5186
tlbi_aa64_ipas2e1is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5187 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
5188 uint64_t value)
5189 {
5190 CPUState *cs = env_cpu(env);
5191 int mask = ipas2e1_tlbmask(env, value);
5192 uint64_t pageaddr = sextract64(value << 12, 0, 56);
5193
5194 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
5195 }
5196
5197 #ifdef TARGET_AARCH64
5198 typedef struct {
5199 uint64_t base;
5200 uint64_t length;
5201 } TLBIRange;
5202
tlbi_range_tg_to_gran_size(int tg)5203 static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
5204 {
5205 /*
5206 * Note that the TLBI range TG field encoding differs from both
5207 * TG0 and TG1 encodings.
5208 */
5209 switch (tg) {
5210 case 1:
5211 return Gran4K;
5212 case 2:
5213 return Gran16K;
5214 case 3:
5215 return Gran64K;
5216 default:
5217 return GranInvalid;
5218 }
5219 }
5220
tlbi_aa64_get_range(CPUARMState * env,ARMMMUIdx mmuidx,uint64_t value)5221 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
5222 uint64_t value)
5223 {
5224 unsigned int page_size_granule, page_shift, num, scale, exponent;
5225 /* Extract one bit to represent the va selector in use. */
5226 uint64_t select = sextract64(value, 36, 1);
5227 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true, false);
5228 TLBIRange ret = { };
5229 ARMGranuleSize gran;
5230
5231 page_size_granule = extract64(value, 46, 2);
5232 gran = tlbi_range_tg_to_gran_size(page_size_granule);
5233
5234 /* The granule encoded in value must match the granule in use. */
5235 if (gran != param.gran) {
5236 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
5237 page_size_granule);
5238 return ret;
5239 }
5240
5241 page_shift = arm_granule_bits(gran);
5242 num = extract64(value, 39, 5);
5243 scale = extract64(value, 44, 2);
5244 exponent = (5 * scale) + 1;
5245
5246 ret.length = (num + 1) << (exponent + page_shift);
5247
5248 if (param.select) {
5249 ret.base = sextract64(value, 0, 37);
5250 } else {
5251 ret.base = extract64(value, 0, 37);
5252 }
5253 if (param.ds) {
5254 /*
5255 * With DS=1, BaseADDR is always shifted 16 so that it is able
5256 * to address all 52 va bits. The input address is perforce
5257 * aligned on a 64k boundary regardless of translation granule.
5258 */
5259 page_shift = 16;
5260 }
5261 ret.base <<= page_shift;
5262
5263 return ret;
5264 }
5265
do_rvae_write(CPUARMState * env,uint64_t value,int idxmap,bool synced)5266 static void do_rvae_write(CPUARMState *env, uint64_t value,
5267 int idxmap, bool synced)
5268 {
5269 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
5270 TLBIRange range;
5271 int bits;
5272
5273 range = tlbi_aa64_get_range(env, one_idx, value);
5274 bits = tlbbits_for_regime(env, one_idx, range.base);
5275
5276 if (synced) {
5277 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
5278 range.base,
5279 range.length,
5280 idxmap,
5281 bits);
5282 } else {
5283 tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
5284 range.length, idxmap, bits);
5285 }
5286 }
5287
tlbi_aa64_rvae1_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5288 static void tlbi_aa64_rvae1_write(CPUARMState *env,
5289 const ARMCPRegInfo *ri,
5290 uint64_t value)
5291 {
5292 /*
5293 * Invalidate by VA range, EL1&0.
5294 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
5295 * since we don't support flush-for-specific-ASID-only or
5296 * flush-last-level-only.
5297 */
5298
5299 do_rvae_write(env, value, vae1_tlbmask(env),
5300 tlb_force_broadcast(env));
5301 }
5302
tlbi_aa64_rvae1is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5303 static void tlbi_aa64_rvae1is_write(CPUARMState *env,
5304 const ARMCPRegInfo *ri,
5305 uint64_t value)
5306 {
5307 /*
5308 * Invalidate by VA range, Inner/Outer Shareable EL1&0.
5309 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
5310 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
5311 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
5312 * shareable specific flushes.
5313 */
5314
5315 do_rvae_write(env, value, vae1_tlbmask(env), true);
5316 }
5317
tlbi_aa64_rvae2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5318 static void tlbi_aa64_rvae2_write(CPUARMState *env,
5319 const ARMCPRegInfo *ri,
5320 uint64_t value)
5321 {
5322 /*
5323 * Invalidate by VA range, EL2.
5324 * Currently handles all of RVAE2 and RVALE2,
5325 * since we don't support flush-for-specific-ASID-only or
5326 * flush-last-level-only.
5327 */
5328
5329 do_rvae_write(env, value, vae2_tlbmask(env),
5330 tlb_force_broadcast(env));
5331
5332
5333 }
5334
tlbi_aa64_rvae2is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5335 static void tlbi_aa64_rvae2is_write(CPUARMState *env,
5336 const ARMCPRegInfo *ri,
5337 uint64_t value)
5338 {
5339 /*
5340 * Invalidate by VA range, Inner/Outer Shareable, EL2.
5341 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
5342 * since we don't support flush-for-specific-ASID-only,
5343 * flush-last-level-only or inner/outer shareable specific flushes.
5344 */
5345
5346 do_rvae_write(env, value, vae2_tlbmask(env), true);
5347
5348 }
5349
tlbi_aa64_rvae3_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5350 static void tlbi_aa64_rvae3_write(CPUARMState *env,
5351 const ARMCPRegInfo *ri,
5352 uint64_t value)
5353 {
5354 /*
5355 * Invalidate by VA range, EL3.
5356 * Currently handles all of RVAE3 and RVALE3,
5357 * since we don't support flush-for-specific-ASID-only or
5358 * flush-last-level-only.
5359 */
5360
5361 do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
5362 }
5363
tlbi_aa64_rvae3is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5364 static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5365 const ARMCPRegInfo *ri,
5366 uint64_t value)
5367 {
5368 /*
5369 * Invalidate by VA range, EL3, Inner/Outer Shareable.
5370 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5371 * since we don't support flush-for-specific-ASID-only,
5372 * flush-last-level-only or inner/outer specific flushes.
5373 */
5374
5375 do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
5376 }
5377
tlbi_aa64_ripas2e1_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5378 static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5379 uint64_t value)
5380 {
5381 do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5382 tlb_force_broadcast(env));
5383 }
5384
tlbi_aa64_ripas2e1is_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5385 static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5386 const ARMCPRegInfo *ri,
5387 uint64_t value)
5388 {
5389 do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5390 }
5391 #endif
5392
aa64_zva_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)5393 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5394 bool isread)
5395 {
5396 int cur_el = arm_current_el(env);
5397
5398 if (cur_el < 2) {
5399 uint64_t hcr = arm_hcr_el2_eff(env);
5400
5401 if (cur_el == 0) {
5402 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5403 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5404 return CP_ACCESS_TRAP_EL2;
5405 }
5406 } else {
5407 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5408 return CP_ACCESS_TRAP;
5409 }
5410 if (hcr & HCR_TDZ) {
5411 return CP_ACCESS_TRAP_EL2;
5412 }
5413 }
5414 } else if (hcr & HCR_TDZ) {
5415 return CP_ACCESS_TRAP_EL2;
5416 }
5417 }
5418 return CP_ACCESS_OK;
5419 }
5420
aa64_dczid_read(CPUARMState * env,const ARMCPRegInfo * ri)5421 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5422 {
5423 ARMCPU *cpu = env_archcpu(env);
5424 int dzp_bit = 1 << 4;
5425
5426 /* DZP indicates whether DC ZVA access is allowed */
5427 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
5428 dzp_bit = 0;
5429 }
5430 return cpu->dcz_blocksize | dzp_bit;
5431 }
5432
sp_el0_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)5433 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5434 bool isread)
5435 {
5436 if (!(env->pstate & PSTATE_SP)) {
5437 /*
5438 * Access to SP_EL0 is undefined if it's being used as
5439 * the stack pointer.
5440 */
5441 return CP_ACCESS_TRAP_UNCATEGORIZED;
5442 }
5443 return CP_ACCESS_OK;
5444 }
5445
spsel_read(CPUARMState * env,const ARMCPRegInfo * ri)5446 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5447 {
5448 return env->pstate & PSTATE_SP;
5449 }
5450
spsel_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t val)5451 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5452 {
5453 update_spsel(env, val);
5454 }
5455
sctlr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5456 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5457 uint64_t value)
5458 {
5459 ARMCPU *cpu = env_archcpu(env);
5460
5461 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5462 /* M bit is RAZ/WI for PMSA with no MPU implemented */
5463 value &= ~SCTLR_M;
5464 }
5465
5466 /* ??? Lots of these bits are not implemented. */
5467
5468 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5469 if (ri->opc1 == 6) { /* SCTLR_EL3 */
5470 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5471 } else {
5472 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5473 SCTLR_ATA0 | SCTLR_ATA);
5474 }
5475 }
5476
5477 if (raw_read(env, ri) == value) {
5478 /*
5479 * Skip the TLB flush if nothing actually changed; Linux likes
5480 * to do a lot of pointless SCTLR writes.
5481 */
5482 return;
5483 }
5484
5485 raw_write(env, ri, value);
5486
5487 /* This may enable/disable the MMU, so do a TLB flush. */
5488 tlb_flush(CPU(cpu));
5489
5490 if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
5491 /*
5492 * Normally we would always end the TB on an SCTLR write; see the
5493 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5494 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5495 * of hflags from the translator, so do it here.
5496 */
5497 arm_rebuild_hflags(env);
5498 }
5499 }
5500
mdcr_el3_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5501 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5502 uint64_t value)
5503 {
5504 /*
5505 * Some MDCR_EL3 bits affect whether PMU counters are running:
5506 * if we are trying to change any of those then we must
5507 * bracket this update with PMU start/finish calls.
5508 */
5509 bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5510
5511 if (pmu_op) {
5512 pmu_op_start(env);
5513 }
5514 env->cp15.mdcr_el3 = value;
5515 if (pmu_op) {
5516 pmu_op_finish(env);
5517 }
5518 }
5519
sdcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5520 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5521 uint64_t value)
5522 {
5523 /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5524 mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5525 }
5526
mdcr_el2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5527 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5528 uint64_t value)
5529 {
5530 /*
5531 * Some MDCR_EL2 bits affect whether PMU counters are running:
5532 * if we are trying to change any of those then we must
5533 * bracket this update with PMU start/finish calls.
5534 */
5535 bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5536
5537 if (pmu_op) {
5538 pmu_op_start(env);
5539 }
5540 env->cp15.mdcr_el2 = value;
5541 if (pmu_op) {
5542 pmu_op_finish(env);
5543 }
5544 }
5545
access_nv1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)5546 static CPAccessResult access_nv1(CPUARMState *env, const ARMCPRegInfo *ri,
5547 bool isread)
5548 {
5549 if (arm_current_el(env) == 1) {
5550 uint64_t hcr_nv = arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1 | HCR_NV2);
5551
5552 if (hcr_nv == (HCR_NV | HCR_NV1)) {
5553 return CP_ACCESS_TRAP_EL2;
5554 }
5555 }
5556 return CP_ACCESS_OK;
5557 }
5558
5559 #ifdef CONFIG_USER_ONLY
5560 /*
5561 * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
5562 * code to get around W^X restrictions, where one region is writable and the
5563 * other is executable.
5564 *
5565 * Since the executable region is never written to we cannot detect code
5566 * changes when running in user mode, and rely on the emulated JIT telling us
5567 * that the code has changed by executing this instruction.
5568 */
ic_ivau_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5569 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
5570 uint64_t value)
5571 {
5572 uint64_t icache_line_mask, start_address, end_address;
5573 const ARMCPU *cpu;
5574
5575 cpu = env_archcpu(env);
5576
5577 icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
5578 start_address = value & ~icache_line_mask;
5579 end_address = value | icache_line_mask;
5580
5581 mmap_lock();
5582
5583 tb_invalidate_phys_range(start_address, end_address);
5584
5585 mmap_unlock();
5586 }
5587 #endif
5588
5589 static const ARMCPRegInfo v8_cp_reginfo[] = {
5590 /*
5591 * Minimal set of EL0-visible registers. This will need to be expanded
5592 * significantly for system emulation of AArch64 CPUs.
5593 */
5594 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5595 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5596 .access = PL0_RW, .type = ARM_CP_NZCV },
5597 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5598 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5599 .type = ARM_CP_NO_RAW,
5600 .access = PL0_RW, .accessfn = aa64_daif_access,
5601 .fieldoffset = offsetof(CPUARMState, daif),
5602 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5603 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5604 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5605 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5606 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5607 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5608 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5609 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5610 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5611 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5612 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5613 .access = PL0_R, .type = ARM_CP_NO_RAW,
5614 .fgt = FGT_DCZID_EL0,
5615 .readfn = aa64_dczid_read },
5616 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5617 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5618 .access = PL0_W, .type = ARM_CP_DC_ZVA,
5619 #ifndef CONFIG_USER_ONLY
5620 /* Avoid overhead of an access check that always passes in user-mode */
5621 .accessfn = aa64_zva_access,
5622 .fgt = FGT_DCZVA,
5623 #endif
5624 },
5625 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5626 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5627 .access = PL1_R, .type = ARM_CP_CURRENTEL },
5628 /*
5629 * Instruction cache ops. All of these except `IC IVAU` NOP because we
5630 * don't emulate caches.
5631 */
5632 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5633 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5634 .access = PL1_W, .type = ARM_CP_NOP,
5635 .fgt = FGT_ICIALLUIS,
5636 .accessfn = access_ticab },
5637 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5638 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5639 .access = PL1_W, .type = ARM_CP_NOP,
5640 .fgt = FGT_ICIALLU,
5641 .accessfn = access_tocu },
5642 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5643 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5644 .access = PL0_W,
5645 .fgt = FGT_ICIVAU,
5646 .accessfn = access_tocu,
5647 #ifdef CONFIG_USER_ONLY
5648 .type = ARM_CP_NO_RAW,
5649 .writefn = ic_ivau_write
5650 #else
5651 .type = ARM_CP_NOP
5652 #endif
5653 },
5654 /* Cache ops: all NOPs since we don't emulate caches */
5655 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5656 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5657 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5658 .fgt = FGT_DCIVAC,
5659 .type = ARM_CP_NOP },
5660 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5661 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5662 .fgt = FGT_DCISW,
5663 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5664 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5665 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5666 .access = PL0_W, .type = ARM_CP_NOP,
5667 .fgt = FGT_DCCVAC,
5668 .accessfn = aa64_cacheop_poc_access },
5669 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5670 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5671 .fgt = FGT_DCCSW,
5672 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5673 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5674 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5675 .access = PL0_W, .type = ARM_CP_NOP,
5676 .fgt = FGT_DCCVAU,
5677 .accessfn = access_tocu },
5678 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5679 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5680 .access = PL0_W, .type = ARM_CP_NOP,
5681 .fgt = FGT_DCCIVAC,
5682 .accessfn = aa64_cacheop_poc_access },
5683 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5684 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5685 .fgt = FGT_DCCISW,
5686 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5687 /* TLBI operations */
5688 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
5689 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
5690 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5691 .fgt = FGT_TLBIVMALLE1IS,
5692 .writefn = tlbi_aa64_vmalle1is_write },
5693 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
5694 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
5695 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5696 .fgt = FGT_TLBIVAE1IS,
5697 .writefn = tlbi_aa64_vae1is_write },
5698 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
5699 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
5700 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5701 .fgt = FGT_TLBIASIDE1IS,
5702 .writefn = tlbi_aa64_vmalle1is_write },
5703 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
5704 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
5705 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5706 .fgt = FGT_TLBIVAAE1IS,
5707 .writefn = tlbi_aa64_vae1is_write },
5708 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
5709 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5710 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5711 .fgt = FGT_TLBIVALE1IS,
5712 .writefn = tlbi_aa64_vae1is_write },
5713 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
5714 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5715 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
5716 .fgt = FGT_TLBIVAALE1IS,
5717 .writefn = tlbi_aa64_vae1is_write },
5718 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
5719 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
5720 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5721 .fgt = FGT_TLBIVMALLE1,
5722 .writefn = tlbi_aa64_vmalle1_write },
5723 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
5724 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
5725 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5726 .fgt = FGT_TLBIVAE1,
5727 .writefn = tlbi_aa64_vae1_write },
5728 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
5729 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
5730 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5731 .fgt = FGT_TLBIASIDE1,
5732 .writefn = tlbi_aa64_vmalle1_write },
5733 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
5734 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
5735 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5736 .fgt = FGT_TLBIVAAE1,
5737 .writefn = tlbi_aa64_vae1_write },
5738 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
5739 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5740 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5741 .fgt = FGT_TLBIVALE1,
5742 .writefn = tlbi_aa64_vae1_write },
5743 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
5744 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5745 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
5746 .fgt = FGT_TLBIVAALE1,
5747 .writefn = tlbi_aa64_vae1_write },
5748 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5749 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5750 .access = PL2_W, .type = ARM_CP_NO_RAW,
5751 .writefn = tlbi_aa64_ipas2e1is_write },
5752 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5753 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5754 .access = PL2_W, .type = ARM_CP_NO_RAW,
5755 .writefn = tlbi_aa64_ipas2e1is_write },
5756 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5757 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5758 .access = PL2_W, .type = ARM_CP_NO_RAW,
5759 .writefn = tlbi_aa64_alle1is_write },
5760 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5761 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5762 .access = PL2_W, .type = ARM_CP_NO_RAW,
5763 .writefn = tlbi_aa64_alle1is_write },
5764 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5765 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5766 .access = PL2_W, .type = ARM_CP_NO_RAW,
5767 .writefn = tlbi_aa64_ipas2e1_write },
5768 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5769 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5770 .access = PL2_W, .type = ARM_CP_NO_RAW,
5771 .writefn = tlbi_aa64_ipas2e1_write },
5772 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5773 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5774 .access = PL2_W, .type = ARM_CP_NO_RAW,
5775 .writefn = tlbi_aa64_alle1_write },
5776 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5777 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5778 .access = PL2_W, .type = ARM_CP_NO_RAW,
5779 .writefn = tlbi_aa64_alle1is_write },
5780 #ifndef CONFIG_USER_ONLY
5781 /* 64 bit address translation operations */
5782 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5783 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5784 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5785 .fgt = FGT_ATS1E1R,
5786 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5787 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5788 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5789 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5790 .fgt = FGT_ATS1E1W,
5791 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5792 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5793 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5794 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5795 .fgt = FGT_ATS1E0R,
5796 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5797 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5798 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5799 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5800 .fgt = FGT_ATS1E0W,
5801 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5802 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5803 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5804 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5805 .accessfn = at_e012_access, .writefn = ats_write64 },
5806 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5807 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5808 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5809 .accessfn = at_e012_access, .writefn = ats_write64 },
5810 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5811 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5812 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5813 .accessfn = at_e012_access, .writefn = ats_write64 },
5814 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5815 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5816 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5817 .accessfn = at_e012_access, .writefn = ats_write64 },
5818 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5819 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5820 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5821 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5822 .writefn = ats_write64 },
5823 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5824 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5825 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5826 .writefn = ats_write64 },
5827 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5828 .type = ARM_CP_ALIAS,
5829 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5830 .access = PL1_RW, .resetvalue = 0,
5831 .fgt = FGT_PAR_EL1,
5832 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5833 .writefn = par_write },
5834 #endif
5835 /* TLB invalidate last level of translation table walk */
5836 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
5837 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5838 .writefn = tlbimva_is_write },
5839 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
5840 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
5841 .writefn = tlbimvaa_is_write },
5842 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
5843 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5844 .writefn = tlbimva_write },
5845 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
5846 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5847 .writefn = tlbimvaa_write },
5848 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5849 .type = ARM_CP_NO_RAW, .access = PL2_W,
5850 .writefn = tlbimva_hyp_write },
5851 { .name = "TLBIMVALHIS",
5852 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5853 .type = ARM_CP_NO_RAW, .access = PL2_W,
5854 .writefn = tlbimva_hyp_is_write },
5855 { .name = "TLBIIPAS2",
5856 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
5857 .type = ARM_CP_NO_RAW, .access = PL2_W,
5858 .writefn = tlbiipas2_hyp_write },
5859 { .name = "TLBIIPAS2IS",
5860 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
5861 .type = ARM_CP_NO_RAW, .access = PL2_W,
5862 .writefn = tlbiipas2is_hyp_write },
5863 { .name = "TLBIIPAS2L",
5864 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
5865 .type = ARM_CP_NO_RAW, .access = PL2_W,
5866 .writefn = tlbiipas2_hyp_write },
5867 { .name = "TLBIIPAS2LIS",
5868 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
5869 .type = ARM_CP_NO_RAW, .access = PL2_W,
5870 .writefn = tlbiipas2is_hyp_write },
5871 /* 32 bit cache operations */
5872 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5873 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5874 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5875 .type = ARM_CP_NOP, .access = PL1_W },
5876 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5877 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5878 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5879 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5880 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5881 .type = ARM_CP_NOP, .access = PL1_W },
5882 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5883 .type = ARM_CP_NOP, .access = PL1_W },
5884 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5885 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5886 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5887 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5888 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5889 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5890 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5891 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5892 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5893 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5894 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5895 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5896 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5897 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5898 /* MMU Domain access control / MPU write buffer control */
5899 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5900 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5901 .writefn = dacr_write, .raw_writefn = raw_write,
5902 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5903 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5904 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5905 .type = ARM_CP_ALIAS,
5906 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5907 .access = PL1_RW, .accessfn = access_nv1,
5908 .nv2_redirect_offset = 0x230 | NV2_REDIR_NV1,
5909 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5910 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5911 .type = ARM_CP_ALIAS,
5912 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5913 .access = PL1_RW, .accessfn = access_nv1,
5914 .nv2_redirect_offset = 0x160 | NV2_REDIR_NV1,
5915 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5916 /*
5917 * We rely on the access checks not allowing the guest to write to the
5918 * state field when SPSel indicates that it's being used as the stack
5919 * pointer.
5920 */
5921 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5922 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5923 .access = PL1_RW, .accessfn = sp_el0_access,
5924 .type = ARM_CP_ALIAS,
5925 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5926 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5927 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5928 .nv2_redirect_offset = 0x240,
5929 .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5930 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5931 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5932 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5933 .type = ARM_CP_NO_RAW,
5934 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5935 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5936 .type = ARM_CP_ALIAS,
5937 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5938 .access = PL2_RW,
5939 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5940 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5941 .type = ARM_CP_ALIAS,
5942 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5943 .access = PL2_RW,
5944 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5945 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5946 .type = ARM_CP_ALIAS,
5947 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5948 .access = PL2_RW,
5949 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5950 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5951 .type = ARM_CP_ALIAS,
5952 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5953 .access = PL2_RW,
5954 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5955 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5956 .type = ARM_CP_IO,
5957 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5958 .resetvalue = 0,
5959 .access = PL3_RW,
5960 .writefn = mdcr_el3_write,
5961 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5962 { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5963 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5964 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5965 .writefn = sdcr_write,
5966 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5967 };
5968
5969 /* These are present only when EL1 supports AArch32 */
5970 static const ARMCPRegInfo v8_aa32_el1_reginfo[] = {
5971 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5972 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5973 .access = PL2_RW,
5974 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5975 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5976 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5977 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5978 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5979 .writefn = dacr_write, .raw_writefn = raw_write,
5980 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5981 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5982 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5983 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5984 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5985 };
5986
do_hcr_write(CPUARMState * env,uint64_t value,uint64_t valid_mask)5987 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5988 {
5989 ARMCPU *cpu = env_archcpu(env);
5990
5991 if (arm_feature(env, ARM_FEATURE_V8)) {
5992 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5993 } else {
5994 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5995 }
5996
5997 if (arm_feature(env, ARM_FEATURE_EL3)) {
5998 valid_mask &= ~HCR_HCD;
5999 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
6000 /*
6001 * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
6002 * However, if we're using the SMC PSCI conduit then QEMU is
6003 * effectively acting like EL3 firmware and so the guest at
6004 * EL2 should retain the ability to prevent EL1 from being
6005 * able to make SMC calls into the ersatz firmware, so in
6006 * that case HCR.TSC should be read/write.
6007 */
6008 valid_mask &= ~HCR_TSC;
6009 }
6010
6011 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
6012 if (cpu_isar_feature(aa64_vh, cpu)) {
6013 valid_mask |= HCR_E2H;
6014 }
6015 if (cpu_isar_feature(aa64_ras, cpu)) {
6016 valid_mask |= HCR_TERR | HCR_TEA;
6017 }
6018 if (cpu_isar_feature(aa64_lor, cpu)) {
6019 valid_mask |= HCR_TLOR;
6020 }
6021 if (cpu_isar_feature(aa64_pauth, cpu)) {
6022 valid_mask |= HCR_API | HCR_APK;
6023 }
6024 if (cpu_isar_feature(aa64_mte, cpu)) {
6025 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
6026 }
6027 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
6028 valid_mask |= HCR_ENSCXT;
6029 }
6030 if (cpu_isar_feature(aa64_fwb, cpu)) {
6031 valid_mask |= HCR_FWB;
6032 }
6033 if (cpu_isar_feature(aa64_rme, cpu)) {
6034 valid_mask |= HCR_GPF;
6035 }
6036 if (cpu_isar_feature(aa64_nv, cpu)) {
6037 valid_mask |= HCR_NV | HCR_NV1 | HCR_AT;
6038 }
6039 if (cpu_isar_feature(aa64_nv2, cpu)) {
6040 valid_mask |= HCR_NV2;
6041 }
6042 }
6043
6044 if (cpu_isar_feature(any_evt, cpu)) {
6045 valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
6046 } else if (cpu_isar_feature(any_half_evt, cpu)) {
6047 valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
6048 }
6049
6050 /* Clear RES0 bits. */
6051 value &= valid_mask;
6052
6053 /*
6054 * These bits change the MMU setup:
6055 * HCR_VM enables stage 2 translation
6056 * HCR_PTW forbids certain page-table setups
6057 * HCR_DC disables stage1 and enables stage2 translation
6058 * HCR_DCT enables tagging on (disabled) stage1 translation
6059 * HCR_FWB changes the interpretation of stage2 descriptor bits
6060 * HCR_NV and HCR_NV1 affect interpretation of descriptor bits
6061 */
6062 if ((env->cp15.hcr_el2 ^ value) &
6063 (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) {
6064 tlb_flush(CPU(cpu));
6065 }
6066 env->cp15.hcr_el2 = value;
6067
6068 /*
6069 * Updates to VI and VF require us to update the status of
6070 * virtual interrupts, which are the logical OR of these bits
6071 * and the state of the input lines from the GIC. (This requires
6072 * that we have the BQL, which is done by marking the
6073 * reginfo structs as ARM_CP_IO.)
6074 * Note that if a write to HCR pends a VIRQ or VFIQ or VINMI or
6075 * VFNMI, it is never possible for it to be taken immediately
6076 * because VIRQ, VFIQ, VINMI and VFNMI are masked unless running
6077 * at EL0 or EL1, and HCR can only be written at EL2.
6078 */
6079 g_assert(bql_locked());
6080 arm_cpu_update_virq(cpu);
6081 arm_cpu_update_vfiq(cpu);
6082 arm_cpu_update_vserr(cpu);
6083 if (cpu_isar_feature(aa64_nmi, cpu)) {
6084 arm_cpu_update_vinmi(cpu);
6085 arm_cpu_update_vfnmi(cpu);
6086 }
6087 }
6088
hcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6089 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
6090 {
6091 do_hcr_write(env, value, 0);
6092 }
6093
hcr_writehigh(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6094 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
6095 uint64_t value)
6096 {
6097 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
6098 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
6099 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
6100 }
6101
hcr_writelow(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6102 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
6103 uint64_t value)
6104 {
6105 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
6106 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
6107 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
6108 }
6109
6110 /*
6111 * Return the effective value of HCR_EL2, at the given security state.
6112 * Bits that are not included here:
6113 * RW (read from SCR_EL3.RW as needed)
6114 */
arm_hcr_el2_eff_secstate(CPUARMState * env,ARMSecuritySpace space)6115 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
6116 {
6117 uint64_t ret = env->cp15.hcr_el2;
6118
6119 assert(space != ARMSS_Root);
6120
6121 if (!arm_is_el2_enabled_secstate(env, space)) {
6122 /*
6123 * "This register has no effect if EL2 is not enabled in the
6124 * current Security state". This is ARMv8.4-SecEL2 speak for
6125 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
6126 *
6127 * Prior to that, the language was "In an implementation that
6128 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
6129 * as if this field is 0 for all purposes other than a direct
6130 * read or write access of HCR_EL2". With lots of enumeration
6131 * on a per-field basis. In current QEMU, this is condition
6132 * is arm_is_secure_below_el3.
6133 *
6134 * Since the v8.4 language applies to the entire register, and
6135 * appears to be backward compatible, use that.
6136 */
6137 return 0;
6138 }
6139
6140 /*
6141 * For a cpu that supports both aarch64 and aarch32, we can set bits
6142 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
6143 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
6144 */
6145 if (!arm_el_is_aa64(env, 2)) {
6146 uint64_t aa32_valid;
6147
6148 /*
6149 * These bits are up-to-date as of ARMv8.6.
6150 * For HCR, it's easiest to list just the 2 bits that are invalid.
6151 * For HCR2, list those that are valid.
6152 */
6153 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
6154 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
6155 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
6156 ret &= aa32_valid;
6157 }
6158
6159 if (ret & HCR_TGE) {
6160 /* These bits are up-to-date as of ARMv8.6. */
6161 if (ret & HCR_E2H) {
6162 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
6163 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
6164 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
6165 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
6166 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
6167 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
6168 } else {
6169 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
6170 }
6171 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
6172 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
6173 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
6174 HCR_TLOR);
6175 }
6176
6177 return ret;
6178 }
6179
arm_hcr_el2_eff(CPUARMState * env)6180 uint64_t arm_hcr_el2_eff(CPUARMState *env)
6181 {
6182 if (arm_feature(env, ARM_FEATURE_M)) {
6183 return 0;
6184 }
6185 return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
6186 }
6187
6188 /*
6189 * Corresponds to ARM pseudocode function ELIsInHost().
6190 */
el_is_in_host(CPUARMState * env,int el)6191 bool el_is_in_host(CPUARMState *env, int el)
6192 {
6193 uint64_t mask;
6194
6195 /*
6196 * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
6197 * Perform the simplest bit tests first, and validate EL2 afterward.
6198 */
6199 if (el & 1) {
6200 return false; /* EL1 or EL3 */
6201 }
6202
6203 /*
6204 * Note that hcr_write() checks isar_feature_aa64_vh(),
6205 * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
6206 */
6207 mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
6208 if ((env->cp15.hcr_el2 & mask) != mask) {
6209 return false;
6210 }
6211
6212 /* TGE and/or E2H set: double check those bits are currently legal. */
6213 return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
6214 }
6215
hcrx_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6216 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
6217 uint64_t value)
6218 {
6219 ARMCPU *cpu = env_archcpu(env);
6220 uint64_t valid_mask = 0;
6221
6222 /* FEAT_MOPS adds MSCEn and MCE2 */
6223 if (cpu_isar_feature(aa64_mops, cpu)) {
6224 valid_mask |= HCRX_MSCEN | HCRX_MCE2;
6225 }
6226
6227 /* FEAT_NMI adds TALLINT, VINMI and VFNMI */
6228 if (cpu_isar_feature(aa64_nmi, cpu)) {
6229 valid_mask |= HCRX_TALLINT | HCRX_VINMI | HCRX_VFNMI;
6230 }
6231
6232 /* Clear RES0 bits. */
6233 env->cp15.hcrx_el2 = value & valid_mask;
6234
6235 /*
6236 * Updates to VINMI and VFNMI require us to update the status of
6237 * virtual NMI, which are the logical OR of these bits
6238 * and the state of the input lines from the GIC. (This requires
6239 * that we have the BQL, which is done by marking the
6240 * reginfo structs as ARM_CP_IO.)
6241 * Note that if a write to HCRX pends a VINMI or VFNMI it is never
6242 * possible for it to be taken immediately, because VINMI and
6243 * VFNMI are masked unless running at EL0 or EL1, and HCRX
6244 * can only be written at EL2.
6245 */
6246 if (cpu_isar_feature(aa64_nmi, cpu)) {
6247 g_assert(bql_locked());
6248 arm_cpu_update_vinmi(cpu);
6249 arm_cpu_update_vfnmi(cpu);
6250 }
6251 }
6252
access_hxen(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6253 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
6254 bool isread)
6255 {
6256 if (arm_current_el(env) == 2
6257 && arm_feature(env, ARM_FEATURE_EL3)
6258 && !(env->cp15.scr_el3 & SCR_HXEN)) {
6259 return CP_ACCESS_TRAP_EL3;
6260 }
6261 return CP_ACCESS_OK;
6262 }
6263
6264 static const ARMCPRegInfo hcrx_el2_reginfo = {
6265 .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
6266 .type = ARM_CP_IO,
6267 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
6268 .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
6269 .nv2_redirect_offset = 0xa0,
6270 .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
6271 };
6272
6273 /* Return the effective value of HCRX_EL2. */
arm_hcrx_el2_eff(CPUARMState * env)6274 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
6275 {
6276 /*
6277 * The bits in this register behave as 0 for all purposes other than
6278 * direct reads of the register if SCR_EL3.HXEn is 0.
6279 * If EL2 is not enabled in the current security state, then the
6280 * bit may behave as if 0, or as if 1, depending on the bit.
6281 * For the moment, we treat the EL2-disabled case as taking
6282 * priority over the HXEn-disabled case. This is true for the only
6283 * bit for a feature which we implement where the answer is different
6284 * for the two cases (MSCEn for FEAT_MOPS).
6285 * This may need to be revisited for future bits.
6286 */
6287 if (!arm_is_el2_enabled(env)) {
6288 uint64_t hcrx = 0;
6289 if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
6290 /* MSCEn behaves as 1 if EL2 is not enabled */
6291 hcrx |= HCRX_MSCEN;
6292 }
6293 return hcrx;
6294 }
6295 if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
6296 return 0;
6297 }
6298 return env->cp15.hcrx_el2;
6299 }
6300
cptr_el2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6301 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
6302 uint64_t value)
6303 {
6304 /*
6305 * For A-profile AArch32 EL3, if NSACR.CP10
6306 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6307 */
6308 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6309 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6310 uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6311 value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
6312 }
6313 env->cp15.cptr_el[2] = value;
6314 }
6315
cptr_el2_read(CPUARMState * env,const ARMCPRegInfo * ri)6316 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
6317 {
6318 /*
6319 * For A-profile AArch32 EL3, if NSACR.CP10
6320 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
6321 */
6322 uint64_t value = env->cp15.cptr_el[2];
6323
6324 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
6325 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
6326 value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
6327 }
6328 return value;
6329 }
6330
6331 static const ARMCPRegInfo el2_cp_reginfo[] = {
6332 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
6333 .type = ARM_CP_IO,
6334 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6335 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6336 .nv2_redirect_offset = 0x78,
6337 .writefn = hcr_write, .raw_writefn = raw_write },
6338 { .name = "HCR", .state = ARM_CP_STATE_AA32,
6339 .type = ARM_CP_ALIAS | ARM_CP_IO,
6340 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
6341 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
6342 .writefn = hcr_writelow },
6343 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
6344 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
6345 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
6346 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
6347 .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
6348 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
6349 .access = PL2_RW,
6350 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
6351 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
6352 .type = ARM_CP_NV2_REDIRECT,
6353 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
6354 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
6355 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
6356 .type = ARM_CP_NV2_REDIRECT,
6357 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
6358 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
6359 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
6360 .type = ARM_CP_ALIAS,
6361 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
6362 .access = PL2_RW,
6363 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
6364 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
6365 .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
6366 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
6367 .access = PL2_RW,
6368 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
6369 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
6370 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
6371 .access = PL2_RW, .writefn = vbar_write,
6372 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
6373 .resetvalue = 0 },
6374 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
6375 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
6376 .access = PL3_RW, .type = ARM_CP_ALIAS,
6377 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
6378 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
6379 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
6380 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
6381 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
6382 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
6383 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
6384 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
6385 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
6386 .resetvalue = 0 },
6387 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
6388 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
6389 .access = PL2_RW, .type = ARM_CP_ALIAS,
6390 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
6391 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
6392 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
6393 .access = PL2_RW, .type = ARM_CP_CONST,
6394 .resetvalue = 0 },
6395 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
6396 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
6397 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
6398 .access = PL2_RW, .type = ARM_CP_CONST,
6399 .resetvalue = 0 },
6400 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
6401 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
6402 .access = PL2_RW, .type = ARM_CP_CONST,
6403 .resetvalue = 0 },
6404 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
6405 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
6406 .access = PL2_RW, .type = ARM_CP_CONST,
6407 .resetvalue = 0 },
6408 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
6409 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
6410 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
6411 .raw_writefn = raw_write,
6412 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
6413 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
6414 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6415 .type = ARM_CP_ALIAS,
6416 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6417 .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
6418 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
6419 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
6420 .access = PL2_RW,
6421 .nv2_redirect_offset = 0x40,
6422 /* no .writefn needed as this can't cause an ASID change */
6423 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
6424 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
6425 .cp = 15, .opc1 = 6, .crm = 2,
6426 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6427 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6428 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
6429 .writefn = vttbr_write, .raw_writefn = raw_write },
6430 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
6431 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
6432 .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
6433 .nv2_redirect_offset = 0x20,
6434 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
6435 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
6436 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
6437 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
6438 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
6439 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6440 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
6441 .access = PL2_RW, .resetvalue = 0,
6442 .nv2_redirect_offset = 0x90,
6443 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
6444 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
6445 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
6446 .access = PL2_RW, .resetvalue = 0,
6447 .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
6448 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6449 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
6450 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
6451 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6452 { .name = "TLBIALLNSNH",
6453 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
6454 .type = ARM_CP_NO_RAW, .access = PL2_W,
6455 .writefn = tlbiall_nsnh_write },
6456 { .name = "TLBIALLNSNHIS",
6457 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
6458 .type = ARM_CP_NO_RAW, .access = PL2_W,
6459 .writefn = tlbiall_nsnh_is_write },
6460 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6461 .type = ARM_CP_NO_RAW, .access = PL2_W,
6462 .writefn = tlbiall_hyp_write },
6463 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6464 .type = ARM_CP_NO_RAW, .access = PL2_W,
6465 .writefn = tlbiall_hyp_is_write },
6466 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6467 .type = ARM_CP_NO_RAW, .access = PL2_W,
6468 .writefn = tlbimva_hyp_write },
6469 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6470 .type = ARM_CP_NO_RAW, .access = PL2_W,
6471 .writefn = tlbimva_hyp_is_write },
6472 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
6473 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6474 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6475 .writefn = tlbi_aa64_alle2_write },
6476 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
6477 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6478 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6479 .writefn = tlbi_aa64_vae2_write },
6480 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
6481 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
6482 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6483 .writefn = tlbi_aa64_vae2_write },
6484 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
6485 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6486 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6487 .writefn = tlbi_aa64_alle2is_write },
6488 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
6489 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6490 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6491 .writefn = tlbi_aa64_vae2is_write },
6492 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
6493 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
6494 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
6495 .writefn = tlbi_aa64_vae2is_write },
6496 #ifndef CONFIG_USER_ONLY
6497 /*
6498 * Unlike the other EL2-related AT operations, these must
6499 * UNDEF from EL3 if EL2 is not implemented, which is why we
6500 * define them here rather than with the rest of the AT ops.
6501 */
6502 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
6503 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6504 .access = PL2_W, .accessfn = at_s1e2_access,
6505 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6506 .writefn = ats_write64 },
6507 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
6508 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6509 .access = PL2_W, .accessfn = at_s1e2_access,
6510 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6511 .writefn = ats_write64 },
6512 /*
6513 * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
6514 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
6515 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
6516 * to behave as if SCR.NS was 1.
6517 */
6518 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6519 .access = PL2_W,
6520 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6521 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6522 .access = PL2_W,
6523 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
6524 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6525 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
6526 /*
6527 * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
6528 * reset values as IMPDEF. We choose to reset to 3 to comply with
6529 * both ARMv7 and ARMv8.
6530 */
6531 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
6532 .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
6533 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
6534 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6535 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6536 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6537 .writefn = gt_cntvoff_write,
6538 .nv2_redirect_offset = 0x60,
6539 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6540 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6541 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6542 .writefn = gt_cntvoff_write,
6543 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6544 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6545 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6546 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6547 .type = ARM_CP_IO, .access = PL2_RW,
6548 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6549 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6550 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6551 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6552 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6553 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6554 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
6555 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
6556 .resetfn = gt_hyp_timer_reset,
6557 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6558 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6559 .type = ARM_CP_IO,
6560 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6561 .access = PL2_RW,
6562 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6563 .resetvalue = 0,
6564 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
6565 #endif
6566 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6567 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6568 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6569 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6570 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6571 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6572 .access = PL2_RW,
6573 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6574 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6575 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6576 .access = PL2_RW,
6577 .nv2_redirect_offset = 0x80,
6578 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
6579 };
6580
6581 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6582 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
6583 .type = ARM_CP_ALIAS | ARM_CP_IO,
6584 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6585 .access = PL2_RW,
6586 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6587 .writefn = hcr_writehigh },
6588 };
6589
sel2_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6590 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6591 bool isread)
6592 {
6593 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6594 return CP_ACCESS_OK;
6595 }
6596 return CP_ACCESS_TRAP_UNCATEGORIZED;
6597 }
6598
6599 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6600 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6601 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6602 .access = PL2_RW, .accessfn = sel2_access,
6603 .nv2_redirect_offset = 0x30,
6604 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6605 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6606 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6607 .access = PL2_RW, .accessfn = sel2_access,
6608 .nv2_redirect_offset = 0x48,
6609 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
6610 };
6611
nsacr_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6612 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6613 bool isread)
6614 {
6615 /*
6616 * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
6617 * At Secure EL1 it traps to EL3 or EL2.
6618 */
6619 if (arm_current_el(env) == 3) {
6620 return CP_ACCESS_OK;
6621 }
6622 if (arm_is_secure_below_el3(env)) {
6623 if (env->cp15.scr_el3 & SCR_EEL2) {
6624 return CP_ACCESS_TRAP_EL2;
6625 }
6626 return CP_ACCESS_TRAP_EL3;
6627 }
6628 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6629 if (isread) {
6630 return CP_ACCESS_OK;
6631 }
6632 return CP_ACCESS_TRAP_UNCATEGORIZED;
6633 }
6634
6635 static const ARMCPRegInfo el3_cp_reginfo[] = {
6636 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6637 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6638 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
6639 .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
6640 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
6641 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
6642 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6643 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
6644 .writefn = scr_write, .raw_writefn = raw_write },
6645 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6646 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6647 .access = PL3_RW, .resetvalue = 0,
6648 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6649 { .name = "SDER",
6650 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6651 .access = PL3_RW, .resetvalue = 0,
6652 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
6653 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
6654 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6655 .writefn = vbar_write, .resetvalue = 0,
6656 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
6657 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6658 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
6659 .access = PL3_RW, .resetvalue = 0,
6660 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
6661 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6662 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6663 .access = PL3_RW,
6664 /* no .writefn needed as this can't cause an ASID change */
6665 .resetvalue = 0,
6666 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
6667 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
6668 .type = ARM_CP_ALIAS,
6669 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6670 .access = PL3_RW,
6671 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
6672 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
6673 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6674 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
6675 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6676 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6677 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
6678 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
6679 .type = ARM_CP_ALIAS,
6680 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
6681 .access = PL3_RW,
6682 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
6683 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6684 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6685 .access = PL3_RW, .writefn = vbar_write,
6686 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6687 .resetvalue = 0 },
6688 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6689 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6690 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6691 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
6692 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6693 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6694 .access = PL3_RW, .resetvalue = 0,
6695 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
6696 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6697 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6698 .access = PL3_RW, .type = ARM_CP_CONST,
6699 .resetvalue = 0 },
6700 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6701 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6702 .access = PL3_RW, .type = ARM_CP_CONST,
6703 .resetvalue = 0 },
6704 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6705 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6706 .access = PL3_RW, .type = ARM_CP_CONST,
6707 .resetvalue = 0 },
6708 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6709 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6710 .access = PL3_W, .type = ARM_CP_NO_RAW,
6711 .writefn = tlbi_aa64_alle3is_write },
6712 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6713 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6714 .access = PL3_W, .type = ARM_CP_NO_RAW,
6715 .writefn = tlbi_aa64_vae3is_write },
6716 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6717 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6718 .access = PL3_W, .type = ARM_CP_NO_RAW,
6719 .writefn = tlbi_aa64_vae3is_write },
6720 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6721 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6722 .access = PL3_W, .type = ARM_CP_NO_RAW,
6723 .writefn = tlbi_aa64_alle3_write },
6724 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6725 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6726 .access = PL3_W, .type = ARM_CP_NO_RAW,
6727 .writefn = tlbi_aa64_vae3_write },
6728 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6729 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6730 .access = PL3_W, .type = ARM_CP_NO_RAW,
6731 .writefn = tlbi_aa64_vae3_write },
6732 };
6733
6734 #ifndef CONFIG_USER_ONLY
6735
e2h_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6736 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
6737 bool isread)
6738 {
6739 if (arm_current_el(env) == 1) {
6740 /* This must be a FEAT_NV access */
6741 return CP_ACCESS_OK;
6742 }
6743 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6744 return CP_ACCESS_TRAP_UNCATEGORIZED;
6745 }
6746 return CP_ACCESS_OK;
6747 }
6748
access_el1nvpct(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6749 static CPAccessResult access_el1nvpct(CPUARMState *env, const ARMCPRegInfo *ri,
6750 bool isread)
6751 {
6752 if (arm_current_el(env) == 1) {
6753 /* This must be a FEAT_NV access with NVx == 101 */
6754 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVPCT)) {
6755 return CP_ACCESS_TRAP_EL2;
6756 }
6757 }
6758 return e2h_access(env, ri, isread);
6759 }
6760
access_el1nvvct(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6761 static CPAccessResult access_el1nvvct(CPUARMState *env, const ARMCPRegInfo *ri,
6762 bool isread)
6763 {
6764 if (arm_current_el(env) == 1) {
6765 /* This must be a FEAT_NV access with NVx == 101 */
6766 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVVCT)) {
6767 return CP_ACCESS_TRAP_EL2;
6768 }
6769 }
6770 return e2h_access(env, ri, isread);
6771 }
6772
6773 /* Test if system register redirection is to occur in the current state. */
redirect_for_e2h(CPUARMState * env)6774 static bool redirect_for_e2h(CPUARMState *env)
6775 {
6776 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6777 }
6778
el2_e2h_read(CPUARMState * env,const ARMCPRegInfo * ri)6779 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6780 {
6781 CPReadFn *readfn;
6782
6783 if (redirect_for_e2h(env)) {
6784 /* Switch to the saved EL2 version of the register. */
6785 ri = ri->opaque;
6786 readfn = ri->readfn;
6787 } else {
6788 readfn = ri->orig_readfn;
6789 }
6790 if (readfn == NULL) {
6791 readfn = raw_read;
6792 }
6793 return readfn(env, ri);
6794 }
6795
el2_e2h_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6796 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6797 uint64_t value)
6798 {
6799 CPWriteFn *writefn;
6800
6801 if (redirect_for_e2h(env)) {
6802 /* Switch to the saved EL2 version of the register. */
6803 ri = ri->opaque;
6804 writefn = ri->writefn;
6805 } else {
6806 writefn = ri->orig_writefn;
6807 }
6808 if (writefn == NULL) {
6809 writefn = raw_write;
6810 }
6811 writefn(env, ri, value);
6812 }
6813
el2_e2h_e12_read(CPUARMState * env,const ARMCPRegInfo * ri)6814 static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri)
6815 {
6816 /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6817 return ri->orig_readfn(env, ri->opaque);
6818 }
6819
el2_e2h_e12_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6820 static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri,
6821 uint64_t value)
6822 {
6823 /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6824 return ri->orig_writefn(env, ri->opaque, value);
6825 }
6826
el2_e2h_e12_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6827 static CPAccessResult el2_e2h_e12_access(CPUARMState *env,
6828 const ARMCPRegInfo *ri,
6829 bool isread)
6830 {
6831 if (arm_current_el(env) == 1) {
6832 /*
6833 * This must be a FEAT_NV access (will either trap or redirect
6834 * to memory). None of the registers with _EL12 aliases want to
6835 * apply their trap controls for this kind of access, so don't
6836 * call the orig_accessfn or do the "UNDEF when E2H is 0" check.
6837 */
6838 return CP_ACCESS_OK;
6839 }
6840 /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */
6841 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6842 return CP_ACCESS_TRAP_UNCATEGORIZED;
6843 }
6844 if (ri->orig_accessfn) {
6845 return ri->orig_accessfn(env, ri->opaque, isread);
6846 }
6847 return CP_ACCESS_OK;
6848 }
6849
define_arm_vh_e2h_redirects_aliases(ARMCPU * cpu)6850 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6851 {
6852 struct E2HAlias {
6853 uint32_t src_key, dst_key, new_key;
6854 const char *src_name, *dst_name, *new_name;
6855 bool (*feature)(const ARMISARegisters *id);
6856 };
6857
6858 #define K(op0, op1, crn, crm, op2) \
6859 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6860
6861 static const struct E2HAlias aliases[] = {
6862 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
6863 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6864 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
6865 "CPACR", "CPTR_EL2", "CPACR_EL12" },
6866 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
6867 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6868 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
6869 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6870 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
6871 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6872 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
6873 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6874 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
6875 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6876 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
6877 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6878 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
6879 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6880 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
6881 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6882 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
6883 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6884 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6885 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6886 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6887 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6888 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6889 "VBAR", "VBAR_EL2", "VBAR_EL12" },
6890 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6891 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6892 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6893 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6894
6895 /*
6896 * Note that redirection of ZCR is mentioned in the description
6897 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6898 * not in the summary table.
6899 */
6900 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
6901 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6902 { K(3, 0, 1, 2, 6), K(3, 4, 1, 2, 6), K(3, 5, 1, 2, 6),
6903 "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6904
6905 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
6906 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6907
6908 { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6909 "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6910 isar_feature_aa64_scxtnum },
6911
6912 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6913 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6914 };
6915 #undef K
6916
6917 size_t i;
6918
6919 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6920 const struct E2HAlias *a = &aliases[i];
6921 ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6922 bool ok;
6923
6924 if (a->feature && !a->feature(&cpu->isar)) {
6925 continue;
6926 }
6927
6928 src_reg = g_hash_table_lookup(cpu->cp_regs,
6929 (gpointer)(uintptr_t)a->src_key);
6930 dst_reg = g_hash_table_lookup(cpu->cp_regs,
6931 (gpointer)(uintptr_t)a->dst_key);
6932 g_assert(src_reg != NULL);
6933 g_assert(dst_reg != NULL);
6934
6935 /* Cross-compare names to detect typos in the keys. */
6936 g_assert(strcmp(src_reg->name, a->src_name) == 0);
6937 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6938
6939 /* None of the core system registers use opaque; we will. */
6940 g_assert(src_reg->opaque == NULL);
6941
6942 /* Create alias before redirection so we dup the right data. */
6943 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6944
6945 new_reg->name = a->new_name;
6946 new_reg->type |= ARM_CP_ALIAS;
6947 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
6948 new_reg->access &= PL2_RW | PL3_RW;
6949 /* The new_reg op fields are as per new_key, not the target reg */
6950 new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK)
6951 >> CP_REG_ARM64_SYSREG_CRN_SHIFT;
6952 new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK)
6953 >> CP_REG_ARM64_SYSREG_CRM_SHIFT;
6954 new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK)
6955 >> CP_REG_ARM64_SYSREG_OP0_SHIFT;
6956 new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK)
6957 >> CP_REG_ARM64_SYSREG_OP1_SHIFT;
6958 new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK)
6959 >> CP_REG_ARM64_SYSREG_OP2_SHIFT;
6960 new_reg->opaque = src_reg;
6961 new_reg->orig_readfn = src_reg->readfn ?: raw_read;
6962 new_reg->orig_writefn = src_reg->writefn ?: raw_write;
6963 new_reg->orig_accessfn = src_reg->accessfn;
6964 if (!new_reg->raw_readfn) {
6965 new_reg->raw_readfn = raw_read;
6966 }
6967 if (!new_reg->raw_writefn) {
6968 new_reg->raw_writefn = raw_write;
6969 }
6970 new_reg->readfn = el2_e2h_e12_read;
6971 new_reg->writefn = el2_e2h_e12_write;
6972 new_reg->accessfn = el2_e2h_e12_access;
6973
6974 /*
6975 * If the _EL1 register is redirected to memory by FEAT_NV2,
6976 * then it shares the offset with the _EL12 register,
6977 * and which one is redirected depends on HCR_EL2.NV1.
6978 */
6979 if (new_reg->nv2_redirect_offset) {
6980 assert(new_reg->nv2_redirect_offset & NV2_REDIR_NV1);
6981 new_reg->nv2_redirect_offset &= ~NV2_REDIR_NV1;
6982 new_reg->nv2_redirect_offset |= NV2_REDIR_NO_NV1;
6983 }
6984
6985 ok = g_hash_table_insert(cpu->cp_regs,
6986 (gpointer)(uintptr_t)a->new_key, new_reg);
6987 g_assert(ok);
6988
6989 src_reg->opaque = dst_reg;
6990 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6991 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6992 if (!src_reg->raw_readfn) {
6993 src_reg->raw_readfn = raw_read;
6994 }
6995 if (!src_reg->raw_writefn) {
6996 src_reg->raw_writefn = raw_write;
6997 }
6998 src_reg->readfn = el2_e2h_read;
6999 src_reg->writefn = el2_e2h_write;
7000 }
7001 }
7002 #endif
7003
ctr_el0_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7004 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
7005 bool isread)
7006 {
7007 int cur_el = arm_current_el(env);
7008
7009 if (cur_el < 2) {
7010 uint64_t hcr = arm_hcr_el2_eff(env);
7011
7012 if (cur_el == 0) {
7013 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
7014 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
7015 return CP_ACCESS_TRAP_EL2;
7016 }
7017 } else {
7018 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
7019 return CP_ACCESS_TRAP;
7020 }
7021 if (hcr & HCR_TID2) {
7022 return CP_ACCESS_TRAP_EL2;
7023 }
7024 }
7025 } else if (hcr & HCR_TID2) {
7026 return CP_ACCESS_TRAP_EL2;
7027 }
7028 }
7029
7030 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
7031 return CP_ACCESS_TRAP_EL2;
7032 }
7033
7034 return CP_ACCESS_OK;
7035 }
7036
7037 /*
7038 * Check for traps to RAS registers, which are controlled
7039 * by HCR_EL2.TERR and SCR_EL3.TERR.
7040 */
access_terr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7041 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
7042 bool isread)
7043 {
7044 int el = arm_current_el(env);
7045
7046 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
7047 return CP_ACCESS_TRAP_EL2;
7048 }
7049 if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
7050 return CP_ACCESS_TRAP_EL3;
7051 }
7052 return CP_ACCESS_OK;
7053 }
7054
disr_read(CPUARMState * env,const ARMCPRegInfo * ri)7055 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
7056 {
7057 int el = arm_current_el(env);
7058
7059 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
7060 return env->cp15.vdisr_el2;
7061 }
7062 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
7063 return 0; /* RAZ/WI */
7064 }
7065 return env->cp15.disr_el1;
7066 }
7067
disr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t val)7068 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7069 {
7070 int el = arm_current_el(env);
7071
7072 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
7073 env->cp15.vdisr_el2 = val;
7074 return;
7075 }
7076 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
7077 return; /* RAZ/WI */
7078 }
7079 env->cp15.disr_el1 = val;
7080 }
7081
7082 /*
7083 * Minimal RAS implementation with no Error Records.
7084 * Which means that all of the Error Record registers:
7085 * ERXADDR_EL1
7086 * ERXCTLR_EL1
7087 * ERXFR_EL1
7088 * ERXMISC0_EL1
7089 * ERXMISC1_EL1
7090 * ERXMISC2_EL1
7091 * ERXMISC3_EL1
7092 * ERXPFGCDN_EL1 (RASv1p1)
7093 * ERXPFGCTL_EL1 (RASv1p1)
7094 * ERXPFGF_EL1 (RASv1p1)
7095 * ERXSTATUS_EL1
7096 * and
7097 * ERRSELR_EL1
7098 * may generate UNDEFINED, which is the effect we get by not
7099 * listing them at all.
7100 *
7101 * These registers have fine-grained trap bits, but UNDEF-to-EL1
7102 * is higher priority than FGT-to-EL2 so we do not need to list them
7103 * in order to check for an FGT.
7104 */
7105 static const ARMCPRegInfo minimal_ras_reginfo[] = {
7106 { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
7107 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
7108 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
7109 .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
7110 { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
7111 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
7112 .access = PL1_R, .accessfn = access_terr,
7113 .fgt = FGT_ERRIDR_EL1,
7114 .type = ARM_CP_CONST, .resetvalue = 0 },
7115 { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
7116 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
7117 .nv2_redirect_offset = 0x500,
7118 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
7119 { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
7120 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
7121 .nv2_redirect_offset = 0x508,
7122 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
7123 };
7124
7125 /*
7126 * Return the exception level to which exceptions should be taken
7127 * via SVEAccessTrap. This excludes the check for whether the exception
7128 * should be routed through AArch64.AdvSIMDFPAccessTrap. That can easily
7129 * be found by testing 0 < fp_exception_el < sve_exception_el.
7130 *
7131 * C.f. the ARM pseudocode function CheckSVEEnabled. Note that the
7132 * pseudocode does *not* separate out the FP trap checks, but has them
7133 * all in one function.
7134 */
sve_exception_el(CPUARMState * env,int el)7135 int sve_exception_el(CPUARMState *env, int el)
7136 {
7137 #ifndef CONFIG_USER_ONLY
7138 if (el <= 1 && !el_is_in_host(env, el)) {
7139 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
7140 case 1:
7141 if (el != 0) {
7142 break;
7143 }
7144 /* fall through */
7145 case 0:
7146 case 2:
7147 return 1;
7148 }
7149 }
7150
7151 if (el <= 2 && arm_is_el2_enabled(env)) {
7152 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
7153 if (env->cp15.hcr_el2 & HCR_E2H) {
7154 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
7155 case 1:
7156 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
7157 break;
7158 }
7159 /* fall through */
7160 case 0:
7161 case 2:
7162 return 2;
7163 }
7164 } else {
7165 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
7166 return 2;
7167 }
7168 }
7169 }
7170
7171 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
7172 if (arm_feature(env, ARM_FEATURE_EL3)
7173 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
7174 return 3;
7175 }
7176 #endif
7177 return 0;
7178 }
7179
7180 /*
7181 * Return the exception level to which exceptions should be taken for SME.
7182 * C.f. the ARM pseudocode function CheckSMEAccess.
7183 */
sme_exception_el(CPUARMState * env,int el)7184 int sme_exception_el(CPUARMState *env, int el)
7185 {
7186 #ifndef CONFIG_USER_ONLY
7187 if (el <= 1 && !el_is_in_host(env, el)) {
7188 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
7189 case 1:
7190 if (el != 0) {
7191 break;
7192 }
7193 /* fall through */
7194 case 0:
7195 case 2:
7196 return 1;
7197 }
7198 }
7199
7200 if (el <= 2 && arm_is_el2_enabled(env)) {
7201 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
7202 if (env->cp15.hcr_el2 & HCR_E2H) {
7203 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
7204 case 1:
7205 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
7206 break;
7207 }
7208 /* fall through */
7209 case 0:
7210 case 2:
7211 return 2;
7212 }
7213 } else {
7214 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
7215 return 2;
7216 }
7217 }
7218 }
7219
7220 /* CPTR_EL3. Since ESM is negative we must check for EL3. */
7221 if (arm_feature(env, ARM_FEATURE_EL3)
7222 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7223 return 3;
7224 }
7225 #endif
7226 return 0;
7227 }
7228
7229 /*
7230 * Given that SVE is enabled, return the vector length for EL.
7231 */
sve_vqm1_for_el_sm(CPUARMState * env,int el,bool sm)7232 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
7233 {
7234 ARMCPU *cpu = env_archcpu(env);
7235 uint64_t *cr = env->vfp.zcr_el;
7236 uint32_t map = cpu->sve_vq.map;
7237 uint32_t len = ARM_MAX_VQ - 1;
7238
7239 if (sm) {
7240 cr = env->vfp.smcr_el;
7241 map = cpu->sme_vq.map;
7242 }
7243
7244 if (el <= 1 && !el_is_in_host(env, el)) {
7245 len = MIN(len, 0xf & (uint32_t)cr[1]);
7246 }
7247 if (el <= 2 && arm_is_el2_enabled(env)) {
7248 len = MIN(len, 0xf & (uint32_t)cr[2]);
7249 }
7250 if (arm_feature(env, ARM_FEATURE_EL3)) {
7251 len = MIN(len, 0xf & (uint32_t)cr[3]);
7252 }
7253
7254 map &= MAKE_64BIT_MASK(0, len + 1);
7255 if (map != 0) {
7256 return 31 - clz32(map);
7257 }
7258
7259 /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
7260 assert(sm);
7261 return ctz32(cpu->sme_vq.map);
7262 }
7263
sve_vqm1_for_el(CPUARMState * env,int el)7264 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
7265 {
7266 return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
7267 }
7268
zcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7269 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7270 uint64_t value)
7271 {
7272 int cur_el = arm_current_el(env);
7273 int old_len = sve_vqm1_for_el(env, cur_el);
7274 int new_len;
7275
7276 /* Bits other than [3:0] are RAZ/WI. */
7277 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
7278 raw_write(env, ri, value & 0xf);
7279
7280 /*
7281 * Because we arrived here, we know both FP and SVE are enabled;
7282 * otherwise we would have trapped access to the ZCR_ELn register.
7283 */
7284 new_len = sve_vqm1_for_el(env, cur_el);
7285 if (new_len < old_len) {
7286 aarch64_sve_narrow_vq(env, new_len + 1);
7287 }
7288 }
7289
7290 static const ARMCPRegInfo zcr_reginfo[] = {
7291 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
7292 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
7293 .nv2_redirect_offset = 0x1e0 | NV2_REDIR_NV1,
7294 .access = PL1_RW, .type = ARM_CP_SVE,
7295 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
7296 .writefn = zcr_write, .raw_writefn = raw_write },
7297 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
7298 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
7299 .access = PL2_RW, .type = ARM_CP_SVE,
7300 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
7301 .writefn = zcr_write, .raw_writefn = raw_write },
7302 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
7303 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
7304 .access = PL3_RW, .type = ARM_CP_SVE,
7305 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
7306 .writefn = zcr_write, .raw_writefn = raw_write },
7307 };
7308
7309 #ifdef TARGET_AARCH64
access_tpidr2(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7310 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
7311 bool isread)
7312 {
7313 int el = arm_current_el(env);
7314
7315 if (el == 0) {
7316 uint64_t sctlr = arm_sctlr(env, el);
7317 if (!(sctlr & SCTLR_EnTP2)) {
7318 return CP_ACCESS_TRAP;
7319 }
7320 }
7321 /* TODO: FEAT_FGT */
7322 if (el < 3
7323 && arm_feature(env, ARM_FEATURE_EL3)
7324 && !(env->cp15.scr_el3 & SCR_ENTP2)) {
7325 return CP_ACCESS_TRAP_EL3;
7326 }
7327 return CP_ACCESS_OK;
7328 }
7329
access_smprimap(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7330 static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri,
7331 bool isread)
7332 {
7333 /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */
7334 if (arm_current_el(env) == 2
7335 && arm_feature(env, ARM_FEATURE_EL3)
7336 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7337 return CP_ACCESS_TRAP_EL3;
7338 }
7339 return CP_ACCESS_OK;
7340 }
7341
access_smpri(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7342 static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri,
7343 bool isread)
7344 {
7345 if (arm_current_el(env) < 3
7346 && arm_feature(env, ARM_FEATURE_EL3)
7347 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
7348 return CP_ACCESS_TRAP_EL3;
7349 }
7350 return CP_ACCESS_OK;
7351 }
7352
7353 /* ResetSVEState */
arm_reset_sve_state(CPUARMState * env)7354 static void arm_reset_sve_state(CPUARMState *env)
7355 {
7356 memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
7357 /* Recall that FFR is stored as pregs[16]. */
7358 memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
7359 vfp_set_fpcr(env, 0x0800009f);
7360 }
7361
aarch64_set_svcr(CPUARMState * env,uint64_t new,uint64_t mask)7362 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
7363 {
7364 uint64_t change = (env->svcr ^ new) & mask;
7365
7366 if (change == 0) {
7367 return;
7368 }
7369 env->svcr ^= change;
7370
7371 if (change & R_SVCR_SM_MASK) {
7372 arm_reset_sve_state(env);
7373 }
7374
7375 /*
7376 * ResetSMEState.
7377 *
7378 * SetPSTATE_ZA zeros on enable and disable. We can zero this only
7379 * on enable: while disabled, the storage is inaccessible and the
7380 * value does not matter. We're not saving the storage in vmstate
7381 * when disabled either.
7382 */
7383 if (change & new & R_SVCR_ZA_MASK) {
7384 memset(env->zarray, 0, sizeof(env->zarray));
7385 }
7386
7387 if (tcg_enabled()) {
7388 arm_rebuild_hflags(env);
7389 }
7390 }
7391
svcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7392 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7393 uint64_t value)
7394 {
7395 aarch64_set_svcr(env, value, -1);
7396 }
7397
smcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7398 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7399 uint64_t value)
7400 {
7401 int cur_el = arm_current_el(env);
7402 int old_len = sve_vqm1_for_el(env, cur_el);
7403 int new_len;
7404
7405 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
7406 value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
7407 raw_write(env, ri, value);
7408
7409 /*
7410 * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
7411 * when SVL is widened (old values kept, or zeros). Choose to keep the
7412 * current values for simplicity. But for QEMU internals, we must still
7413 * apply the narrower SVL to the Zregs and Pregs -- see the comment
7414 * above aarch64_sve_narrow_vq.
7415 */
7416 new_len = sve_vqm1_for_el(env, cur_el);
7417 if (new_len < old_len) {
7418 aarch64_sve_narrow_vq(env, new_len + 1);
7419 }
7420 }
7421
7422 static const ARMCPRegInfo sme_reginfo[] = {
7423 { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
7424 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
7425 .access = PL0_RW, .accessfn = access_tpidr2,
7426 .fgt = FGT_NTPIDR2_EL0,
7427 .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
7428 { .name = "SVCR", .state = ARM_CP_STATE_AA64,
7429 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
7430 .access = PL0_RW, .type = ARM_CP_SME,
7431 .fieldoffset = offsetof(CPUARMState, svcr),
7432 .writefn = svcr_write, .raw_writefn = raw_write },
7433 { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
7434 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
7435 .nv2_redirect_offset = 0x1f0 | NV2_REDIR_NV1,
7436 .access = PL1_RW, .type = ARM_CP_SME,
7437 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
7438 .writefn = smcr_write, .raw_writefn = raw_write },
7439 { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
7440 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
7441 .access = PL2_RW, .type = ARM_CP_SME,
7442 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
7443 .writefn = smcr_write, .raw_writefn = raw_write },
7444 { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
7445 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
7446 .access = PL3_RW, .type = ARM_CP_SME,
7447 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
7448 .writefn = smcr_write, .raw_writefn = raw_write },
7449 { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
7450 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
7451 .access = PL1_R, .accessfn = access_aa64_tid1,
7452 /*
7453 * IMPLEMENTOR = 0 (software)
7454 * REVISION = 0 (implementation defined)
7455 * SMPS = 0 (no streaming execution priority in QEMU)
7456 * AFFINITY = 0 (streaming sve mode not shared with other PEs)
7457 */
7458 .type = ARM_CP_CONST, .resetvalue = 0, },
7459 /*
7460 * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
7461 */
7462 { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
7463 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
7464 .access = PL1_RW, .accessfn = access_smpri,
7465 .fgt = FGT_NSMPRI_EL1,
7466 .type = ARM_CP_CONST, .resetvalue = 0 },
7467 { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
7468 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
7469 .nv2_redirect_offset = 0x1f8,
7470 .access = PL2_RW, .accessfn = access_smprimap,
7471 .type = ARM_CP_CONST, .resetvalue = 0 },
7472 };
7473
tlbi_aa64_paall_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7474 static void tlbi_aa64_paall_write(CPUARMState *env, const ARMCPRegInfo *ri,
7475 uint64_t value)
7476 {
7477 CPUState *cs = env_cpu(env);
7478
7479 tlb_flush(cs);
7480 }
7481
gpccr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7482 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7483 uint64_t value)
7484 {
7485 /* L0GPTSZ is RO; other bits not mentioned are RES0. */
7486 uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
7487 R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
7488 R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
7489
7490 env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
7491 }
7492
gpccr_reset(CPUARMState * env,const ARMCPRegInfo * ri)7493 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
7494 {
7495 env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
7496 env_archcpu(env)->reset_l0gptsz);
7497 }
7498
tlbi_aa64_paallos_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7499 static void tlbi_aa64_paallos_write(CPUARMState *env, const ARMCPRegInfo *ri,
7500 uint64_t value)
7501 {
7502 CPUState *cs = env_cpu(env);
7503
7504 tlb_flush_all_cpus_synced(cs);
7505 }
7506
7507 static const ARMCPRegInfo rme_reginfo[] = {
7508 { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
7509 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
7510 .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
7511 .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
7512 { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
7513 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
7514 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
7515 { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
7516 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
7517 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
7518 { .name = "TLBI_PAALL", .state = ARM_CP_STATE_AA64,
7519 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 4,
7520 .access = PL3_W, .type = ARM_CP_NO_RAW,
7521 .writefn = tlbi_aa64_paall_write },
7522 { .name = "TLBI_PAALLOS", .state = ARM_CP_STATE_AA64,
7523 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 4,
7524 .access = PL3_W, .type = ARM_CP_NO_RAW,
7525 .writefn = tlbi_aa64_paallos_write },
7526 /*
7527 * QEMU does not have a way to invalidate by physical address, thus
7528 * invalidating a range of physical addresses is accomplished by
7529 * flushing all tlb entries in the outer shareable domain,
7530 * just like PAALLOS.
7531 */
7532 { .name = "TLBI_RPALOS", .state = ARM_CP_STATE_AA64,
7533 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 7,
7534 .access = PL3_W, .type = ARM_CP_NO_RAW,
7535 .writefn = tlbi_aa64_paallos_write },
7536 { .name = "TLBI_RPAOS", .state = ARM_CP_STATE_AA64,
7537 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 3,
7538 .access = PL3_W, .type = ARM_CP_NO_RAW,
7539 .writefn = tlbi_aa64_paallos_write },
7540 { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
7541 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
7542 .access = PL3_W, .type = ARM_CP_NOP },
7543 };
7544
7545 static const ARMCPRegInfo rme_mte_reginfo[] = {
7546 { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
7547 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
7548 .access = PL3_W, .type = ARM_CP_NOP },
7549 };
7550
aa64_allint_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7551 static void aa64_allint_write(CPUARMState *env, const ARMCPRegInfo *ri,
7552 uint64_t value)
7553 {
7554 env->pstate = (env->pstate & ~PSTATE_ALLINT) | (value & PSTATE_ALLINT);
7555 }
7556
aa64_allint_read(CPUARMState * env,const ARMCPRegInfo * ri)7557 static uint64_t aa64_allint_read(CPUARMState *env, const ARMCPRegInfo *ri)
7558 {
7559 return env->pstate & PSTATE_ALLINT;
7560 }
7561
aa64_allint_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7562 static CPAccessResult aa64_allint_access(CPUARMState *env,
7563 const ARMCPRegInfo *ri, bool isread)
7564 {
7565 if (!isread && arm_current_el(env) == 1 &&
7566 (arm_hcrx_el2_eff(env) & HCRX_TALLINT)) {
7567 return CP_ACCESS_TRAP_EL2;
7568 }
7569 return CP_ACCESS_OK;
7570 }
7571
7572 static const ARMCPRegInfo nmi_reginfo[] = {
7573 { .name = "ALLINT", .state = ARM_CP_STATE_AA64,
7574 .opc0 = 3, .opc1 = 0, .opc2 = 0, .crn = 4, .crm = 3,
7575 .type = ARM_CP_NO_RAW,
7576 .access = PL1_RW, .accessfn = aa64_allint_access,
7577 .fieldoffset = offsetof(CPUARMState, pstate),
7578 .writefn = aa64_allint_write, .readfn = aa64_allint_read,
7579 .resetfn = arm_cp_reset_ignore },
7580 };
7581 #endif /* TARGET_AARCH64 */
7582
define_pmu_regs(ARMCPU * cpu)7583 static void define_pmu_regs(ARMCPU *cpu)
7584 {
7585 /*
7586 * v7 performance monitor control register: same implementor
7587 * field as main ID register, and we implement four counters in
7588 * addition to the cycle count register.
7589 */
7590 unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
7591 ARMCPRegInfo pmcr = {
7592 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
7593 .access = PL0_RW,
7594 .fgt = FGT_PMCR_EL0,
7595 .type = ARM_CP_IO | ARM_CP_ALIAS,
7596 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
7597 .accessfn = pmreg_access,
7598 .readfn = pmcr_read, .raw_readfn = raw_read,
7599 .writefn = pmcr_write, .raw_writefn = raw_write,
7600 };
7601 ARMCPRegInfo pmcr64 = {
7602 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
7603 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
7604 .access = PL0_RW, .accessfn = pmreg_access,
7605 .fgt = FGT_PMCR_EL0,
7606 .type = ARM_CP_IO,
7607 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
7608 .resetvalue = cpu->isar.reset_pmcr_el0,
7609 .readfn = pmcr_read, .raw_readfn = raw_read,
7610 .writefn = pmcr_write, .raw_writefn = raw_write,
7611 };
7612
7613 define_one_arm_cp_reg(cpu, &pmcr);
7614 define_one_arm_cp_reg(cpu, &pmcr64);
7615 for (i = 0; i < pmcrn; i++) {
7616 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
7617 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
7618 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
7619 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
7620 ARMCPRegInfo pmev_regs[] = {
7621 { .name = pmevcntr_name, .cp = 15, .crn = 14,
7622 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7623 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7624 .fgt = FGT_PMEVCNTRN_EL0,
7625 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7626 .accessfn = pmreg_access_xevcntr },
7627 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
7628 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
7629 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
7630 .type = ARM_CP_IO,
7631 .fgt = FGT_PMEVCNTRN_EL0,
7632 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
7633 .raw_readfn = pmevcntr_rawread,
7634 .raw_writefn = pmevcntr_rawwrite },
7635 { .name = pmevtyper_name, .cp = 15, .crn = 14,
7636 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
7637 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
7638 .fgt = FGT_PMEVTYPERN_EL0,
7639 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7640 .accessfn = pmreg_access },
7641 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
7642 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
7643 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
7644 .fgt = FGT_PMEVTYPERN_EL0,
7645 .type = ARM_CP_IO,
7646 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
7647 .raw_writefn = pmevtyper_rawwrite },
7648 };
7649 define_arm_cp_regs(cpu, pmev_regs);
7650 g_free(pmevcntr_name);
7651 g_free(pmevcntr_el0_name);
7652 g_free(pmevtyper_name);
7653 g_free(pmevtyper_el0_name);
7654 }
7655 if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
7656 ARMCPRegInfo v81_pmu_regs[] = {
7657 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
7658 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
7659 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7660 .fgt = FGT_PMCEIDN_EL0,
7661 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
7662 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
7663 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
7664 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7665 .fgt = FGT_PMCEIDN_EL0,
7666 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
7667 };
7668 define_arm_cp_regs(cpu, v81_pmu_regs);
7669 }
7670 if (cpu_isar_feature(any_pmuv3p4, cpu)) {
7671 static const ARMCPRegInfo v84_pmmir = {
7672 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
7673 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
7674 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
7675 .fgt = FGT_PMMIR_EL1,
7676 .resetvalue = 0
7677 };
7678 define_one_arm_cp_reg(cpu, &v84_pmmir);
7679 }
7680 }
7681
7682 #ifndef CONFIG_USER_ONLY
7683 /*
7684 * We don't know until after realize whether there's a GICv3
7685 * attached, and that is what registers the gicv3 sysregs.
7686 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
7687 * at runtime.
7688 */
id_pfr1_read(CPUARMState * env,const ARMCPRegInfo * ri)7689 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
7690 {
7691 ARMCPU *cpu = env_archcpu(env);
7692 uint64_t pfr1 = cpu->isar.id_pfr1;
7693
7694 if (env->gicv3state) {
7695 pfr1 |= 1 << 28;
7696 }
7697 return pfr1;
7698 }
7699
id_aa64pfr0_read(CPUARMState * env,const ARMCPRegInfo * ri)7700 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
7701 {
7702 ARMCPU *cpu = env_archcpu(env);
7703 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
7704
7705 if (env->gicv3state) {
7706 pfr0 |= 1 << 24;
7707 }
7708 return pfr0;
7709 }
7710 #endif
7711
7712 /*
7713 * Shared logic between LORID and the rest of the LOR* registers.
7714 * Secure state exclusion has already been dealt with.
7715 */
access_lor_ns(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7716 static CPAccessResult access_lor_ns(CPUARMState *env,
7717 const ARMCPRegInfo *ri, bool isread)
7718 {
7719 int el = arm_current_el(env);
7720
7721 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
7722 return CP_ACCESS_TRAP_EL2;
7723 }
7724 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
7725 return CP_ACCESS_TRAP_EL3;
7726 }
7727 return CP_ACCESS_OK;
7728 }
7729
access_lor_other(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7730 static CPAccessResult access_lor_other(CPUARMState *env,
7731 const ARMCPRegInfo *ri, bool isread)
7732 {
7733 if (arm_is_secure_below_el3(env)) {
7734 /* Access denied in secure mode. */
7735 return CP_ACCESS_TRAP;
7736 }
7737 return access_lor_ns(env, ri, isread);
7738 }
7739
7740 /*
7741 * A trivial implementation of ARMv8.1-LOR leaves all of these
7742 * registers fixed at 0, which indicates that there are zero
7743 * supported Limited Ordering regions.
7744 */
7745 static const ARMCPRegInfo lor_reginfo[] = {
7746 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
7747 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
7748 .access = PL1_RW, .accessfn = access_lor_other,
7749 .fgt = FGT_LORSA_EL1,
7750 .type = ARM_CP_CONST, .resetvalue = 0 },
7751 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7752 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7753 .access = PL1_RW, .accessfn = access_lor_other,
7754 .fgt = FGT_LOREA_EL1,
7755 .type = ARM_CP_CONST, .resetvalue = 0 },
7756 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7757 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7758 .access = PL1_RW, .accessfn = access_lor_other,
7759 .fgt = FGT_LORN_EL1,
7760 .type = ARM_CP_CONST, .resetvalue = 0 },
7761 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7762 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7763 .access = PL1_RW, .accessfn = access_lor_other,
7764 .fgt = FGT_LORC_EL1,
7765 .type = ARM_CP_CONST, .resetvalue = 0 },
7766 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7767 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7768 .access = PL1_R, .accessfn = access_lor_ns,
7769 .fgt = FGT_LORID_EL1,
7770 .type = ARM_CP_CONST, .resetvalue = 0 },
7771 };
7772
7773 #ifdef TARGET_AARCH64
access_pauth(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7774 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7775 bool isread)
7776 {
7777 int el = arm_current_el(env);
7778
7779 if (el < 2 &&
7780 arm_is_el2_enabled(env) &&
7781 !(arm_hcr_el2_eff(env) & HCR_APK)) {
7782 return CP_ACCESS_TRAP_EL2;
7783 }
7784 if (el < 3 &&
7785 arm_feature(env, ARM_FEATURE_EL3) &&
7786 !(env->cp15.scr_el3 & SCR_APK)) {
7787 return CP_ACCESS_TRAP_EL3;
7788 }
7789 return CP_ACCESS_OK;
7790 }
7791
7792 static const ARMCPRegInfo pauth_reginfo[] = {
7793 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7794 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7795 .access = PL1_RW, .accessfn = access_pauth,
7796 .fgt = FGT_APDAKEY,
7797 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7798 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7799 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7800 .access = PL1_RW, .accessfn = access_pauth,
7801 .fgt = FGT_APDAKEY,
7802 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7803 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7804 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7805 .access = PL1_RW, .accessfn = access_pauth,
7806 .fgt = FGT_APDBKEY,
7807 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7808 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7809 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7810 .access = PL1_RW, .accessfn = access_pauth,
7811 .fgt = FGT_APDBKEY,
7812 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7813 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7814 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7815 .access = PL1_RW, .accessfn = access_pauth,
7816 .fgt = FGT_APGAKEY,
7817 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7818 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7819 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7820 .access = PL1_RW, .accessfn = access_pauth,
7821 .fgt = FGT_APGAKEY,
7822 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7823 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7824 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7825 .access = PL1_RW, .accessfn = access_pauth,
7826 .fgt = FGT_APIAKEY,
7827 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7828 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7829 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7830 .access = PL1_RW, .accessfn = access_pauth,
7831 .fgt = FGT_APIAKEY,
7832 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7833 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7834 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7835 .access = PL1_RW, .accessfn = access_pauth,
7836 .fgt = FGT_APIBKEY,
7837 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7838 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7839 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7840 .access = PL1_RW, .accessfn = access_pauth,
7841 .fgt = FGT_APIBKEY,
7842 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7843 };
7844
7845 static const ARMCPRegInfo tlbirange_reginfo[] = {
7846 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7847 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
7848 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7849 .fgt = FGT_TLBIRVAE1IS,
7850 .writefn = tlbi_aa64_rvae1is_write },
7851 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7852 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
7853 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7854 .fgt = FGT_TLBIRVAAE1IS,
7855 .writefn = tlbi_aa64_rvae1is_write },
7856 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7857 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
7858 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7859 .fgt = FGT_TLBIRVALE1IS,
7860 .writefn = tlbi_aa64_rvae1is_write },
7861 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7862 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
7863 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
7864 .fgt = FGT_TLBIRVAALE1IS,
7865 .writefn = tlbi_aa64_rvae1is_write },
7866 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7867 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
7868 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7869 .fgt = FGT_TLBIRVAE1OS,
7870 .writefn = tlbi_aa64_rvae1is_write },
7871 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7872 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
7873 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7874 .fgt = FGT_TLBIRVAAE1OS,
7875 .writefn = tlbi_aa64_rvae1is_write },
7876 { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7877 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
7878 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7879 .fgt = FGT_TLBIRVALE1OS,
7880 .writefn = tlbi_aa64_rvae1is_write },
7881 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7882 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
7883 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7884 .fgt = FGT_TLBIRVAALE1OS,
7885 .writefn = tlbi_aa64_rvae1is_write },
7886 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7887 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
7888 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7889 .fgt = FGT_TLBIRVAE1,
7890 .writefn = tlbi_aa64_rvae1_write },
7891 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7892 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
7893 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7894 .fgt = FGT_TLBIRVAAE1,
7895 .writefn = tlbi_aa64_rvae1_write },
7896 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7897 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
7898 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7899 .fgt = FGT_TLBIRVALE1,
7900 .writefn = tlbi_aa64_rvae1_write },
7901 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7902 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
7903 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
7904 .fgt = FGT_TLBIRVAALE1,
7905 .writefn = tlbi_aa64_rvae1_write },
7906 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7907 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
7908 .access = PL2_W, .type = ARM_CP_NO_RAW,
7909 .writefn = tlbi_aa64_ripas2e1is_write },
7910 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7911 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
7912 .access = PL2_W, .type = ARM_CP_NO_RAW,
7913 .writefn = tlbi_aa64_ripas2e1is_write },
7914 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7915 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
7916 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7917 .writefn = tlbi_aa64_rvae2is_write },
7918 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7919 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
7920 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7921 .writefn = tlbi_aa64_rvae2is_write },
7922 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7923 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
7924 .access = PL2_W, .type = ARM_CP_NO_RAW,
7925 .writefn = tlbi_aa64_ripas2e1_write },
7926 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
7927 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
7928 .access = PL2_W, .type = ARM_CP_NO_RAW,
7929 .writefn = tlbi_aa64_ripas2e1_write },
7930 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7931 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
7932 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7933 .writefn = tlbi_aa64_rvae2is_write },
7934 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7935 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
7936 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7937 .writefn = tlbi_aa64_rvae2is_write },
7938 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7939 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
7940 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7941 .writefn = tlbi_aa64_rvae2_write },
7942 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7943 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
7944 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7945 .writefn = tlbi_aa64_rvae2_write },
7946 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7947 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7948 .access = PL3_W, .type = ARM_CP_NO_RAW,
7949 .writefn = tlbi_aa64_rvae3is_write },
7950 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7951 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7952 .access = PL3_W, .type = ARM_CP_NO_RAW,
7953 .writefn = tlbi_aa64_rvae3is_write },
7954 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7955 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7956 .access = PL3_W, .type = ARM_CP_NO_RAW,
7957 .writefn = tlbi_aa64_rvae3is_write },
7958 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7959 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7960 .access = PL3_W, .type = ARM_CP_NO_RAW,
7961 .writefn = tlbi_aa64_rvae3is_write },
7962 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7963 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7964 .access = PL3_W, .type = ARM_CP_NO_RAW,
7965 .writefn = tlbi_aa64_rvae3_write },
7966 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7967 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7968 .access = PL3_W, .type = ARM_CP_NO_RAW,
7969 .writefn = tlbi_aa64_rvae3_write },
7970 };
7971
7972 static const ARMCPRegInfo tlbios_reginfo[] = {
7973 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7974 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
7975 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7976 .fgt = FGT_TLBIVMALLE1OS,
7977 .writefn = tlbi_aa64_vmalle1is_write },
7978 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7979 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
7980 .fgt = FGT_TLBIVAE1OS,
7981 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7982 .writefn = tlbi_aa64_vae1is_write },
7983 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7984 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
7985 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7986 .fgt = FGT_TLBIASIDE1OS,
7987 .writefn = tlbi_aa64_vmalle1is_write },
7988 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7989 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
7990 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7991 .fgt = FGT_TLBIVAAE1OS,
7992 .writefn = tlbi_aa64_vae1is_write },
7993 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7994 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
7995 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7996 .fgt = FGT_TLBIVALE1OS,
7997 .writefn = tlbi_aa64_vae1is_write },
7998 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7999 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
8000 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
8001 .fgt = FGT_TLBIVAALE1OS,
8002 .writefn = tlbi_aa64_vae1is_write },
8003 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
8004 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
8005 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
8006 .writefn = tlbi_aa64_alle2is_write },
8007 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
8008 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
8009 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
8010 .writefn = tlbi_aa64_vae2is_write },
8011 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
8012 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
8013 .access = PL2_W, .type = ARM_CP_NO_RAW,
8014 .writefn = tlbi_aa64_alle1is_write },
8015 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
8016 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
8017 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
8018 .writefn = tlbi_aa64_vae2is_write },
8019 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
8020 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
8021 .access = PL2_W, .type = ARM_CP_NO_RAW,
8022 .writefn = tlbi_aa64_alle1is_write },
8023 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
8024 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
8025 .access = PL2_W, .type = ARM_CP_NOP },
8026 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
8027 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
8028 .access = PL2_W, .type = ARM_CP_NOP },
8029 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
8030 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
8031 .access = PL2_W, .type = ARM_CP_NOP },
8032 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
8033 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
8034 .access = PL2_W, .type = ARM_CP_NOP },
8035 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
8036 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
8037 .access = PL3_W, .type = ARM_CP_NO_RAW,
8038 .writefn = tlbi_aa64_alle3is_write },
8039 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
8040 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
8041 .access = PL3_W, .type = ARM_CP_NO_RAW,
8042 .writefn = tlbi_aa64_vae3is_write },
8043 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
8044 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
8045 .access = PL3_W, .type = ARM_CP_NO_RAW,
8046 .writefn = tlbi_aa64_vae3is_write },
8047 };
8048
rndr_readfn(CPUARMState * env,const ARMCPRegInfo * ri)8049 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
8050 {
8051 Error *err = NULL;
8052 uint64_t ret;
8053
8054 /* Success sets NZCV = 0000. */
8055 env->NF = env->CF = env->VF = 0, env->ZF = 1;
8056
8057 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
8058 /*
8059 * ??? Failed, for unknown reasons in the crypto subsystem.
8060 * The best we can do is log the reason and return the
8061 * timed-out indication to the guest. There is no reason
8062 * we know to expect this failure to be transitory, so the
8063 * guest may well hang retrying the operation.
8064 */
8065 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
8066 ri->name, error_get_pretty(err));
8067 error_free(err);
8068
8069 env->ZF = 0; /* NZCF = 0100 */
8070 return 0;
8071 }
8072 return ret;
8073 }
8074
8075 /* We do not support re-seeding, so the two registers operate the same. */
8076 static const ARMCPRegInfo rndr_reginfo[] = {
8077 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
8078 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
8079 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
8080 .access = PL0_R, .readfn = rndr_readfn },
8081 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
8082 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
8083 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
8084 .access = PL0_R, .readfn = rndr_readfn },
8085 };
8086
dccvap_writefn(CPUARMState * env,const ARMCPRegInfo * opaque,uint64_t value)8087 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
8088 uint64_t value)
8089 {
8090 #ifdef CONFIG_TCG
8091 ARMCPU *cpu = env_archcpu(env);
8092 /* CTR_EL0 System register -> DminLine, bits [19:16] */
8093 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
8094 uint64_t vaddr_in = (uint64_t) value;
8095 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
8096 void *haddr;
8097 int mem_idx = arm_env_mmu_index(env);
8098
8099 /* This won't be crossing page boundaries */
8100 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
8101 if (haddr) {
8102 #ifndef CONFIG_USER_ONLY
8103
8104 ram_addr_t offset;
8105 MemoryRegion *mr;
8106
8107 /* RCU lock is already being held */
8108 mr = memory_region_from_host(haddr, &offset);
8109
8110 if (mr) {
8111 memory_region_writeback(mr, offset, dline_size);
8112 }
8113 #endif /*CONFIG_USER_ONLY*/
8114 }
8115 #else
8116 /* Handled by hardware accelerator. */
8117 g_assert_not_reached();
8118 #endif /* CONFIG_TCG */
8119 }
8120
8121 static const ARMCPRegInfo dcpop_reg[] = {
8122 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
8123 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
8124 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
8125 .fgt = FGT_DCCVAP,
8126 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
8127 };
8128
8129 static const ARMCPRegInfo dcpodp_reg[] = {
8130 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
8131 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
8132 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
8133 .fgt = FGT_DCCVADP,
8134 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
8135 };
8136
access_aa64_tid5(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8137 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
8138 bool isread)
8139 {
8140 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
8141 return CP_ACCESS_TRAP_EL2;
8142 }
8143
8144 return CP_ACCESS_OK;
8145 }
8146
access_mte(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8147 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
8148 bool isread)
8149 {
8150 int el = arm_current_el(env);
8151 if (el < 2 && arm_is_el2_enabled(env)) {
8152 uint64_t hcr = arm_hcr_el2_eff(env);
8153 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
8154 return CP_ACCESS_TRAP_EL2;
8155 }
8156 }
8157 if (el < 3 &&
8158 arm_feature(env, ARM_FEATURE_EL3) &&
8159 !(env->cp15.scr_el3 & SCR_ATA)) {
8160 return CP_ACCESS_TRAP_EL3;
8161 }
8162 return CP_ACCESS_OK;
8163 }
8164
access_tfsr_el1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8165 static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri,
8166 bool isread)
8167 {
8168 CPAccessResult nv1 = access_nv1(env, ri, isread);
8169
8170 if (nv1 != CP_ACCESS_OK) {
8171 return nv1;
8172 }
8173 return access_mte(env, ri, isread);
8174 }
8175
access_tfsr_el2(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8176 static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri,
8177 bool isread)
8178 {
8179 /*
8180 * TFSR_EL2: similar to generic access_mte(), but we need to
8181 * account for FEAT_NV. At EL1 this must be a FEAT_NV access;
8182 * if NV2 is enabled then we will redirect this to TFSR_EL1
8183 * after doing the HCR and SCR ATA traps; otherwise this will
8184 * be a trap to EL2 and the HCR/SCR traps do not apply.
8185 */
8186 int el = arm_current_el(env);
8187
8188 if (el == 1 && (arm_hcr_el2_eff(env) & HCR_NV2)) {
8189 return CP_ACCESS_OK;
8190 }
8191 if (el < 2 && arm_is_el2_enabled(env)) {
8192 uint64_t hcr = arm_hcr_el2_eff(env);
8193 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
8194 return CP_ACCESS_TRAP_EL2;
8195 }
8196 }
8197 if (el < 3 &&
8198 arm_feature(env, ARM_FEATURE_EL3) &&
8199 !(env->cp15.scr_el3 & SCR_ATA)) {
8200 return CP_ACCESS_TRAP_EL3;
8201 }
8202 return CP_ACCESS_OK;
8203 }
8204
tco_read(CPUARMState * env,const ARMCPRegInfo * ri)8205 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
8206 {
8207 return env->pstate & PSTATE_TCO;
8208 }
8209
tco_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t val)8210 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
8211 {
8212 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
8213 }
8214
8215 static const ARMCPRegInfo mte_reginfo[] = {
8216 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
8217 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
8218 .access = PL1_RW, .accessfn = access_mte,
8219 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
8220 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
8221 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
8222 .access = PL1_RW, .accessfn = access_tfsr_el1,
8223 .nv2_redirect_offset = 0x190 | NV2_REDIR_NV1,
8224 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
8225 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
8226 .type = ARM_CP_NV2_REDIRECT,
8227 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
8228 .access = PL2_RW, .accessfn = access_tfsr_el2,
8229 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
8230 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
8231 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
8232 .access = PL3_RW,
8233 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
8234 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
8235 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
8236 .access = PL1_RW, .accessfn = access_mte,
8237 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
8238 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
8239 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
8240 .access = PL1_RW, .accessfn = access_mte,
8241 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
8242 { .name = "TCO", .state = ARM_CP_STATE_AA64,
8243 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
8244 .type = ARM_CP_NO_RAW,
8245 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
8246 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
8247 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
8248 .type = ARM_CP_NOP, .access = PL1_W,
8249 .fgt = FGT_DCIVAC,
8250 .accessfn = aa64_cacheop_poc_access },
8251 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
8252 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
8253 .fgt = FGT_DCISW,
8254 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8255 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
8256 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
8257 .type = ARM_CP_NOP, .access = PL1_W,
8258 .fgt = FGT_DCIVAC,
8259 .accessfn = aa64_cacheop_poc_access },
8260 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
8261 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
8262 .fgt = FGT_DCISW,
8263 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8264 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
8265 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
8266 .fgt = FGT_DCCSW,
8267 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8268 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
8269 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
8270 .fgt = FGT_DCCSW,
8271 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8272 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
8273 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
8274 .fgt = FGT_DCCISW,
8275 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8276 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
8277 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
8278 .fgt = FGT_DCCISW,
8279 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
8280 };
8281
8282 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
8283 { .name = "TCO", .state = ARM_CP_STATE_AA64,
8284 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
8285 .type = ARM_CP_CONST, .access = PL0_RW, },
8286 };
8287
8288 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
8289 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
8290 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
8291 .type = ARM_CP_NOP, .access = PL0_W,
8292 .fgt = FGT_DCCVAC,
8293 .accessfn = aa64_cacheop_poc_access },
8294 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
8295 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
8296 .type = ARM_CP_NOP, .access = PL0_W,
8297 .fgt = FGT_DCCVAC,
8298 .accessfn = aa64_cacheop_poc_access },
8299 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
8300 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
8301 .type = ARM_CP_NOP, .access = PL0_W,
8302 .fgt = FGT_DCCVAP,
8303 .accessfn = aa64_cacheop_poc_access },
8304 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
8305 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
8306 .type = ARM_CP_NOP, .access = PL0_W,
8307 .fgt = FGT_DCCVAP,
8308 .accessfn = aa64_cacheop_poc_access },
8309 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
8310 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
8311 .type = ARM_CP_NOP, .access = PL0_W,
8312 .fgt = FGT_DCCVADP,
8313 .accessfn = aa64_cacheop_poc_access },
8314 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
8315 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
8316 .type = ARM_CP_NOP, .access = PL0_W,
8317 .fgt = FGT_DCCVADP,
8318 .accessfn = aa64_cacheop_poc_access },
8319 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
8320 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
8321 .type = ARM_CP_NOP, .access = PL0_W,
8322 .fgt = FGT_DCCIVAC,
8323 .accessfn = aa64_cacheop_poc_access },
8324 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
8325 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
8326 .type = ARM_CP_NOP, .access = PL0_W,
8327 .fgt = FGT_DCCIVAC,
8328 .accessfn = aa64_cacheop_poc_access },
8329 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
8330 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
8331 .access = PL0_W, .type = ARM_CP_DC_GVA,
8332 #ifndef CONFIG_USER_ONLY
8333 /* Avoid overhead of an access check that always passes in user-mode */
8334 .accessfn = aa64_zva_access,
8335 .fgt = FGT_DCZVA,
8336 #endif
8337 },
8338 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
8339 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
8340 .access = PL0_W, .type = ARM_CP_DC_GZVA,
8341 #ifndef CONFIG_USER_ONLY
8342 /* Avoid overhead of an access check that always passes in user-mode */
8343 .accessfn = aa64_zva_access,
8344 .fgt = FGT_DCZVA,
8345 #endif
8346 },
8347 };
8348
access_scxtnum(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8349 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
8350 bool isread)
8351 {
8352 uint64_t hcr = arm_hcr_el2_eff(env);
8353 int el = arm_current_el(env);
8354
8355 if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
8356 if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
8357 if (hcr & HCR_TGE) {
8358 return CP_ACCESS_TRAP_EL2;
8359 }
8360 return CP_ACCESS_TRAP;
8361 }
8362 } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
8363 return CP_ACCESS_TRAP_EL2;
8364 }
8365 if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
8366 return CP_ACCESS_TRAP_EL2;
8367 }
8368 if (el < 3
8369 && arm_feature(env, ARM_FEATURE_EL3)
8370 && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
8371 return CP_ACCESS_TRAP_EL3;
8372 }
8373 return CP_ACCESS_OK;
8374 }
8375
access_scxtnum_el1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8376 static CPAccessResult access_scxtnum_el1(CPUARMState *env,
8377 const ARMCPRegInfo *ri,
8378 bool isread)
8379 {
8380 CPAccessResult nv1 = access_nv1(env, ri, isread);
8381
8382 if (nv1 != CP_ACCESS_OK) {
8383 return nv1;
8384 }
8385 return access_scxtnum(env, ri, isread);
8386 }
8387
8388 static const ARMCPRegInfo scxtnum_reginfo[] = {
8389 { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
8390 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
8391 .access = PL0_RW, .accessfn = access_scxtnum,
8392 .fgt = FGT_SCXTNUM_EL0,
8393 .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
8394 { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
8395 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
8396 .access = PL1_RW, .accessfn = access_scxtnum_el1,
8397 .fgt = FGT_SCXTNUM_EL1,
8398 .nv2_redirect_offset = 0x188 | NV2_REDIR_NV1,
8399 .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
8400 { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
8401 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
8402 .access = PL2_RW, .accessfn = access_scxtnum,
8403 .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
8404 { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
8405 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
8406 .access = PL3_RW,
8407 .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
8408 };
8409
access_fgt(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8410 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
8411 bool isread)
8412 {
8413 if (arm_current_el(env) == 2 &&
8414 arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
8415 return CP_ACCESS_TRAP_EL3;
8416 }
8417 return CP_ACCESS_OK;
8418 }
8419
8420 static const ARMCPRegInfo fgt_reginfo[] = {
8421 { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
8422 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
8423 .nv2_redirect_offset = 0x1b8,
8424 .access = PL2_RW, .accessfn = access_fgt,
8425 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
8426 { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
8427 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
8428 .nv2_redirect_offset = 0x1c0,
8429 .access = PL2_RW, .accessfn = access_fgt,
8430 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
8431 { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
8432 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
8433 .nv2_redirect_offset = 0x1d0,
8434 .access = PL2_RW, .accessfn = access_fgt,
8435 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
8436 { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
8437 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
8438 .nv2_redirect_offset = 0x1d8,
8439 .access = PL2_RW, .accessfn = access_fgt,
8440 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
8441 { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
8442 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
8443 .nv2_redirect_offset = 0x1c8,
8444 .access = PL2_RW, .accessfn = access_fgt,
8445 .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
8446 };
8447
vncr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)8448 static void vncr_write(CPUARMState *env, const ARMCPRegInfo *ri,
8449 uint64_t value)
8450 {
8451 /*
8452 * Clear the RES0 bottom 12 bits; this means at runtime we can guarantee
8453 * that VNCR_EL2 + offset is 64-bit aligned. We don't need to do anything
8454 * about the RESS bits at the top -- we choose the "generate an EL2
8455 * translation abort on use" CONSTRAINED UNPREDICTABLE option (i.e. let
8456 * the ptw.c code detect the resulting invalid address).
8457 */
8458 env->cp15.vncr_el2 = value & ~0xfffULL;
8459 }
8460
8461 static const ARMCPRegInfo nv2_reginfo[] = {
8462 { .name = "VNCR_EL2", .state = ARM_CP_STATE_AA64,
8463 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 2, .opc2 = 0,
8464 .access = PL2_RW,
8465 .writefn = vncr_write,
8466 .nv2_redirect_offset = 0xb0,
8467 .fieldoffset = offsetof(CPUARMState, cp15.vncr_el2) },
8468 };
8469
8470 #endif /* TARGET_AARCH64 */
8471
access_predinv(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8472 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
8473 bool isread)
8474 {
8475 int el = arm_current_el(env);
8476
8477 if (el == 0) {
8478 uint64_t sctlr = arm_sctlr(env, el);
8479 if (!(sctlr & SCTLR_EnRCTX)) {
8480 return CP_ACCESS_TRAP;
8481 }
8482 } else if (el == 1) {
8483 uint64_t hcr = arm_hcr_el2_eff(env);
8484 if (hcr & HCR_NV) {
8485 return CP_ACCESS_TRAP_EL2;
8486 }
8487 }
8488 return CP_ACCESS_OK;
8489 }
8490
8491 static const ARMCPRegInfo predinv_reginfo[] = {
8492 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
8493 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
8494 .fgt = FGT_CFPRCTX,
8495 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8496 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
8497 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
8498 .fgt = FGT_DVPRCTX,
8499 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8500 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
8501 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
8502 .fgt = FGT_CPPRCTX,
8503 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8504 /*
8505 * Note the AArch32 opcodes have a different OPC1.
8506 */
8507 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
8508 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
8509 .fgt = FGT_CFPRCTX,
8510 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8511 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
8512 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
8513 .fgt = FGT_DVPRCTX,
8514 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8515 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
8516 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
8517 .fgt = FGT_CPPRCTX,
8518 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
8519 };
8520
ccsidr2_read(CPUARMState * env,const ARMCPRegInfo * ri)8521 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
8522 {
8523 /* Read the high 32 bits of the current CCSIDR */
8524 return extract64(ccsidr_read(env, ri), 32, 32);
8525 }
8526
8527 static const ARMCPRegInfo ccsidr2_reginfo[] = {
8528 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
8529 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
8530 .access = PL1_R,
8531 .accessfn = access_tid4,
8532 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
8533 };
8534
access_aa64_tid3(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8535 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8536 bool isread)
8537 {
8538 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
8539 return CP_ACCESS_TRAP_EL2;
8540 }
8541
8542 return CP_ACCESS_OK;
8543 }
8544
access_aa32_tid3(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8545 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
8546 bool isread)
8547 {
8548 if (arm_feature(env, ARM_FEATURE_V8)) {
8549 return access_aa64_tid3(env, ri, isread);
8550 }
8551
8552 return CP_ACCESS_OK;
8553 }
8554
access_jazelle(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8555 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
8556 bool isread)
8557 {
8558 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
8559 return CP_ACCESS_TRAP_EL2;
8560 }
8561
8562 return CP_ACCESS_OK;
8563 }
8564
access_joscr_jmcr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)8565 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
8566 const ARMCPRegInfo *ri, bool isread)
8567 {
8568 /*
8569 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
8570 * in v7A, not in v8A.
8571 */
8572 if (!arm_feature(env, ARM_FEATURE_V8) &&
8573 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
8574 (env->cp15.hstr_el2 & HSTR_TJDBX)) {
8575 return CP_ACCESS_TRAP_EL2;
8576 }
8577 return CP_ACCESS_OK;
8578 }
8579
8580 static const ARMCPRegInfo jazelle_regs[] = {
8581 { .name = "JIDR",
8582 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
8583 .access = PL1_R, .accessfn = access_jazelle,
8584 .type = ARM_CP_CONST, .resetvalue = 0 },
8585 { .name = "JOSCR",
8586 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8587 .accessfn = access_joscr_jmcr,
8588 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8589 { .name = "JMCR",
8590 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8591 .accessfn = access_joscr_jmcr,
8592 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
8593 };
8594
8595 static const ARMCPRegInfo contextidr_el2 = {
8596 .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
8597 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
8598 .access = PL2_RW,
8599 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
8600 };
8601
8602 static const ARMCPRegInfo vhe_reginfo[] = {
8603 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
8604 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
8605 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
8606 .raw_writefn = raw_write,
8607 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8608 #ifndef CONFIG_USER_ONLY
8609 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
8610 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
8611 .fieldoffset =
8612 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
8613 .type = ARM_CP_IO, .access = PL2_RW,
8614 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
8615 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
8616 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
8617 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
8618 .resetfn = gt_hv_timer_reset,
8619 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
8620 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
8621 .type = ARM_CP_IO,
8622 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
8623 .access = PL2_RW,
8624 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
8625 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
8626 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
8627 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
8628 .type = ARM_CP_IO | ARM_CP_ALIAS,
8629 .access = PL2_RW, .accessfn = access_el1nvpct,
8630 .nv2_redirect_offset = 0x180 | NV2_REDIR_NO_NV1,
8631 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
8632 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
8633 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
8634 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
8635 .type = ARM_CP_IO | ARM_CP_ALIAS,
8636 .access = PL2_RW, .accessfn = access_el1nvvct,
8637 .nv2_redirect_offset = 0x170 | NV2_REDIR_NO_NV1,
8638 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
8639 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
8640 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8641 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
8642 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8643 .access = PL2_RW, .accessfn = e2h_access,
8644 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
8645 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
8646 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
8647 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
8648 .access = PL2_RW, .accessfn = e2h_access,
8649 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
8650 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8651 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
8652 .type = ARM_CP_IO | ARM_CP_ALIAS,
8653 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
8654 .nv2_redirect_offset = 0x178 | NV2_REDIR_NO_NV1,
8655 .access = PL2_RW, .accessfn = access_el1nvpct,
8656 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
8657 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
8658 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
8659 .type = ARM_CP_IO | ARM_CP_ALIAS,
8660 .nv2_redirect_offset = 0x168 | NV2_REDIR_NO_NV1,
8661 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
8662 .access = PL2_RW, .accessfn = access_el1nvvct,
8663 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8664 #endif
8665 };
8666
8667 #ifndef CONFIG_USER_ONLY
8668 static const ARMCPRegInfo ats1e1_reginfo[] = {
8669 { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
8670 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8671 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8672 .fgt = FGT_ATS1E1RP,
8673 .accessfn = at_s1e01_access, .writefn = ats_write64 },
8674 { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
8675 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8676 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8677 .fgt = FGT_ATS1E1WP,
8678 .accessfn = at_s1e01_access, .writefn = ats_write64 },
8679 };
8680
8681 static const ARMCPRegInfo ats1cp_reginfo[] = {
8682 { .name = "ATS1CPRP",
8683 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
8684 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8685 .writefn = ats_write },
8686 { .name = "ATS1CPWP",
8687 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
8688 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
8689 .writefn = ats_write },
8690 };
8691 #endif
8692
8693 /*
8694 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
8695 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
8696 * is non-zero, which is never for ARMv7, optionally in ARMv8
8697 * and mandatorily for ARMv8.2 and up.
8698 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
8699 * implementation is RAZ/WI we can ignore this detail, as we
8700 * do for ACTLR.
8701 */
8702 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
8703 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
8704 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
8705 .access = PL1_RW, .accessfn = access_tacr,
8706 .type = ARM_CP_CONST, .resetvalue = 0 },
8707 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
8708 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
8709 .access = PL2_RW, .type = ARM_CP_CONST,
8710 .resetvalue = 0 },
8711 };
8712
register_cp_regs_for_features(ARMCPU * cpu)8713 void register_cp_regs_for_features(ARMCPU *cpu)
8714 {
8715 /* Register all the coprocessor registers based on feature bits */
8716 CPUARMState *env = &cpu->env;
8717 if (arm_feature(env, ARM_FEATURE_M)) {
8718 /* M profile has no coprocessor registers */
8719 return;
8720 }
8721
8722 define_arm_cp_regs(cpu, cp_reginfo);
8723 if (!arm_feature(env, ARM_FEATURE_V8)) {
8724 /*
8725 * Must go early as it is full of wildcards that may be
8726 * overridden by later definitions.
8727 */
8728 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
8729 }
8730
8731 if (arm_feature(env, ARM_FEATURE_V6)) {
8732 /* The ID registers all have impdef reset values */
8733 ARMCPRegInfo v6_idregs[] = {
8734 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
8735 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
8736 .access = PL1_R, .type = ARM_CP_CONST,
8737 .accessfn = access_aa32_tid3,
8738 .resetvalue = cpu->isar.id_pfr0 },
8739 /*
8740 * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
8741 * the value of the GIC field until after we define these regs.
8742 */
8743 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
8744 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
8745 .access = PL1_R, .type = ARM_CP_NO_RAW,
8746 .accessfn = access_aa32_tid3,
8747 #ifdef CONFIG_USER_ONLY
8748 .type = ARM_CP_CONST,
8749 .resetvalue = cpu->isar.id_pfr1,
8750 #else
8751 .type = ARM_CP_NO_RAW,
8752 .accessfn = access_aa32_tid3,
8753 .readfn = id_pfr1_read,
8754 .writefn = arm_cp_write_ignore
8755 #endif
8756 },
8757 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
8758 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
8759 .access = PL1_R, .type = ARM_CP_CONST,
8760 .accessfn = access_aa32_tid3,
8761 .resetvalue = cpu->isar.id_dfr0 },
8762 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
8763 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
8764 .access = PL1_R, .type = ARM_CP_CONST,
8765 .accessfn = access_aa32_tid3,
8766 .resetvalue = cpu->id_afr0 },
8767 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
8768 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
8769 .access = PL1_R, .type = ARM_CP_CONST,
8770 .accessfn = access_aa32_tid3,
8771 .resetvalue = cpu->isar.id_mmfr0 },
8772 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
8773 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
8774 .access = PL1_R, .type = ARM_CP_CONST,
8775 .accessfn = access_aa32_tid3,
8776 .resetvalue = cpu->isar.id_mmfr1 },
8777 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
8778 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
8779 .access = PL1_R, .type = ARM_CP_CONST,
8780 .accessfn = access_aa32_tid3,
8781 .resetvalue = cpu->isar.id_mmfr2 },
8782 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
8783 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
8784 .access = PL1_R, .type = ARM_CP_CONST,
8785 .accessfn = access_aa32_tid3,
8786 .resetvalue = cpu->isar.id_mmfr3 },
8787 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
8788 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
8789 .access = PL1_R, .type = ARM_CP_CONST,
8790 .accessfn = access_aa32_tid3,
8791 .resetvalue = cpu->isar.id_isar0 },
8792 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
8793 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
8794 .access = PL1_R, .type = ARM_CP_CONST,
8795 .accessfn = access_aa32_tid3,
8796 .resetvalue = cpu->isar.id_isar1 },
8797 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
8798 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
8799 .access = PL1_R, .type = ARM_CP_CONST,
8800 .accessfn = access_aa32_tid3,
8801 .resetvalue = cpu->isar.id_isar2 },
8802 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
8803 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
8804 .access = PL1_R, .type = ARM_CP_CONST,
8805 .accessfn = access_aa32_tid3,
8806 .resetvalue = cpu->isar.id_isar3 },
8807 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
8808 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
8809 .access = PL1_R, .type = ARM_CP_CONST,
8810 .accessfn = access_aa32_tid3,
8811 .resetvalue = cpu->isar.id_isar4 },
8812 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
8813 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
8814 .access = PL1_R, .type = ARM_CP_CONST,
8815 .accessfn = access_aa32_tid3,
8816 .resetvalue = cpu->isar.id_isar5 },
8817 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
8818 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
8819 .access = PL1_R, .type = ARM_CP_CONST,
8820 .accessfn = access_aa32_tid3,
8821 .resetvalue = cpu->isar.id_mmfr4 },
8822 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
8823 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
8824 .access = PL1_R, .type = ARM_CP_CONST,
8825 .accessfn = access_aa32_tid3,
8826 .resetvalue = cpu->isar.id_isar6 },
8827 };
8828 define_arm_cp_regs(cpu, v6_idregs);
8829 define_arm_cp_regs(cpu, v6_cp_reginfo);
8830 } else {
8831 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
8832 }
8833 if (arm_feature(env, ARM_FEATURE_V6K)) {
8834 define_arm_cp_regs(cpu, v6k_cp_reginfo);
8835 }
8836 if (arm_feature(env, ARM_FEATURE_V7MP) &&
8837 !arm_feature(env, ARM_FEATURE_PMSA)) {
8838 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
8839 }
8840 if (arm_feature(env, ARM_FEATURE_V7VE)) {
8841 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
8842 }
8843 if (arm_feature(env, ARM_FEATURE_V7)) {
8844 ARMCPRegInfo clidr = {
8845 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
8846 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
8847 .access = PL1_R, .type = ARM_CP_CONST,
8848 .accessfn = access_tid4,
8849 .fgt = FGT_CLIDR_EL1,
8850 .resetvalue = cpu->clidr
8851 };
8852 define_one_arm_cp_reg(cpu, &clidr);
8853 define_arm_cp_regs(cpu, v7_cp_reginfo);
8854 define_debug_regs(cpu);
8855 define_pmu_regs(cpu);
8856 } else {
8857 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
8858 }
8859 if (arm_feature(env, ARM_FEATURE_V8)) {
8860 /*
8861 * v8 ID registers, which all have impdef reset values.
8862 * Note that within the ID register ranges the unused slots
8863 * must all RAZ, not UNDEF; future architecture versions may
8864 * define new registers here.
8865 * ID registers which are AArch64 views of the AArch32 ID registers
8866 * which already existed in v6 and v7 are handled elsewhere,
8867 * in v6_idregs[].
8868 */
8869 int i;
8870 ARMCPRegInfo v8_idregs[] = {
8871 /*
8872 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
8873 * emulation because we don't know the right value for the
8874 * GIC field until after we define these regs.
8875 */
8876 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
8877 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
8878 .access = PL1_R,
8879 #ifdef CONFIG_USER_ONLY
8880 .type = ARM_CP_CONST,
8881 .resetvalue = cpu->isar.id_aa64pfr0
8882 #else
8883 .type = ARM_CP_NO_RAW,
8884 .accessfn = access_aa64_tid3,
8885 .readfn = id_aa64pfr0_read,
8886 .writefn = arm_cp_write_ignore
8887 #endif
8888 },
8889 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
8890 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
8891 .access = PL1_R, .type = ARM_CP_CONST,
8892 .accessfn = access_aa64_tid3,
8893 .resetvalue = cpu->isar.id_aa64pfr1},
8894 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8895 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
8896 .access = PL1_R, .type = ARM_CP_CONST,
8897 .accessfn = access_aa64_tid3,
8898 .resetvalue = 0 },
8899 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8900 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
8901 .access = PL1_R, .type = ARM_CP_CONST,
8902 .accessfn = access_aa64_tid3,
8903 .resetvalue = 0 },
8904 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
8905 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
8906 .access = PL1_R, .type = ARM_CP_CONST,
8907 .accessfn = access_aa64_tid3,
8908 .resetvalue = cpu->isar.id_aa64zfr0 },
8909 { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
8910 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
8911 .access = PL1_R, .type = ARM_CP_CONST,
8912 .accessfn = access_aa64_tid3,
8913 .resetvalue = cpu->isar.id_aa64smfr0 },
8914 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8915 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
8916 .access = PL1_R, .type = ARM_CP_CONST,
8917 .accessfn = access_aa64_tid3,
8918 .resetvalue = 0 },
8919 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8920 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
8921 .access = PL1_R, .type = ARM_CP_CONST,
8922 .accessfn = access_aa64_tid3,
8923 .resetvalue = 0 },
8924 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
8925 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
8926 .access = PL1_R, .type = ARM_CP_CONST,
8927 .accessfn = access_aa64_tid3,
8928 .resetvalue = cpu->isar.id_aa64dfr0 },
8929 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
8930 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
8931 .access = PL1_R, .type = ARM_CP_CONST,
8932 .accessfn = access_aa64_tid3,
8933 .resetvalue = cpu->isar.id_aa64dfr1 },
8934 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8935 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
8936 .access = PL1_R, .type = ARM_CP_CONST,
8937 .accessfn = access_aa64_tid3,
8938 .resetvalue = 0 },
8939 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8940 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
8941 .access = PL1_R, .type = ARM_CP_CONST,
8942 .accessfn = access_aa64_tid3,
8943 .resetvalue = 0 },
8944 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
8945 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
8946 .access = PL1_R, .type = ARM_CP_CONST,
8947 .accessfn = access_aa64_tid3,
8948 .resetvalue = cpu->id_aa64afr0 },
8949 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
8950 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
8951 .access = PL1_R, .type = ARM_CP_CONST,
8952 .accessfn = access_aa64_tid3,
8953 .resetvalue = cpu->id_aa64afr1 },
8954 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8955 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
8956 .access = PL1_R, .type = ARM_CP_CONST,
8957 .accessfn = access_aa64_tid3,
8958 .resetvalue = 0 },
8959 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8960 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8961 .access = PL1_R, .type = ARM_CP_CONST,
8962 .accessfn = access_aa64_tid3,
8963 .resetvalue = 0 },
8964 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8965 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8966 .access = PL1_R, .type = ARM_CP_CONST,
8967 .accessfn = access_aa64_tid3,
8968 .resetvalue = cpu->isar.id_aa64isar0 },
8969 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8970 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8971 .access = PL1_R, .type = ARM_CP_CONST,
8972 .accessfn = access_aa64_tid3,
8973 .resetvalue = cpu->isar.id_aa64isar1 },
8974 { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
8975 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8976 .access = PL1_R, .type = ARM_CP_CONST,
8977 .accessfn = access_aa64_tid3,
8978 .resetvalue = cpu->isar.id_aa64isar2 },
8979 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8980 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8981 .access = PL1_R, .type = ARM_CP_CONST,
8982 .accessfn = access_aa64_tid3,
8983 .resetvalue = 0 },
8984 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8985 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8986 .access = PL1_R, .type = ARM_CP_CONST,
8987 .accessfn = access_aa64_tid3,
8988 .resetvalue = 0 },
8989 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8990 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8991 .access = PL1_R, .type = ARM_CP_CONST,
8992 .accessfn = access_aa64_tid3,
8993 .resetvalue = 0 },
8994 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8995 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8996 .access = PL1_R, .type = ARM_CP_CONST,
8997 .accessfn = access_aa64_tid3,
8998 .resetvalue = 0 },
8999 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9000 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
9001 .access = PL1_R, .type = ARM_CP_CONST,
9002 .accessfn = access_aa64_tid3,
9003 .resetvalue = 0 },
9004 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
9005 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
9006 .access = PL1_R, .type = ARM_CP_CONST,
9007 .accessfn = access_aa64_tid3,
9008 .resetvalue = cpu->isar.id_aa64mmfr0 },
9009 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
9010 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
9011 .access = PL1_R, .type = ARM_CP_CONST,
9012 .accessfn = access_aa64_tid3,
9013 .resetvalue = cpu->isar.id_aa64mmfr1 },
9014 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
9015 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
9016 .access = PL1_R, .type = ARM_CP_CONST,
9017 .accessfn = access_aa64_tid3,
9018 .resetvalue = cpu->isar.id_aa64mmfr2 },
9019 { .name = "ID_AA64MMFR3_EL1", .state = ARM_CP_STATE_AA64,
9020 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
9021 .access = PL1_R, .type = ARM_CP_CONST,
9022 .accessfn = access_aa64_tid3,
9023 .resetvalue = cpu->isar.id_aa64mmfr3 },
9024 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9025 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
9026 .access = PL1_R, .type = ARM_CP_CONST,
9027 .accessfn = access_aa64_tid3,
9028 .resetvalue = 0 },
9029 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9030 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
9031 .access = PL1_R, .type = ARM_CP_CONST,
9032 .accessfn = access_aa64_tid3,
9033 .resetvalue = 0 },
9034 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9035 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
9036 .access = PL1_R, .type = ARM_CP_CONST,
9037 .accessfn = access_aa64_tid3,
9038 .resetvalue = 0 },
9039 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
9040 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
9041 .access = PL1_R, .type = ARM_CP_CONST,
9042 .accessfn = access_aa64_tid3,
9043 .resetvalue = 0 },
9044 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
9045 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
9046 .access = PL1_R, .type = ARM_CP_CONST,
9047 .accessfn = access_aa64_tid3,
9048 .resetvalue = cpu->isar.mvfr0 },
9049 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
9050 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
9051 .access = PL1_R, .type = ARM_CP_CONST,
9052 .accessfn = access_aa64_tid3,
9053 .resetvalue = cpu->isar.mvfr1 },
9054 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
9055 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
9056 .access = PL1_R, .type = ARM_CP_CONST,
9057 .accessfn = access_aa64_tid3,
9058 .resetvalue = cpu->isar.mvfr2 },
9059 /*
9060 * "0, c0, c3, {0,1,2}" are the encodings corresponding to
9061 * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
9062 * as RAZ, since it is in the "reserved for future ID
9063 * registers, RAZ" part of the AArch32 encoding space.
9064 */
9065 { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
9066 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
9067 .access = PL1_R, .type = ARM_CP_CONST,
9068 .accessfn = access_aa64_tid3,
9069 .resetvalue = 0 },
9070 { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
9071 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
9072 .access = PL1_R, .type = ARM_CP_CONST,
9073 .accessfn = access_aa64_tid3,
9074 .resetvalue = 0 },
9075 { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
9076 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
9077 .access = PL1_R, .type = ARM_CP_CONST,
9078 .accessfn = access_aa64_tid3,
9079 .resetvalue = 0 },
9080 /*
9081 * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
9082 * they're also RAZ for AArch64, and in v8 are gradually
9083 * being filled with AArch64-view-of-AArch32-ID-register
9084 * for new ID registers.
9085 */
9086 { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
9087 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
9088 .access = PL1_R, .type = ARM_CP_CONST,
9089 .accessfn = access_aa64_tid3,
9090 .resetvalue = 0 },
9091 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
9092 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
9093 .access = PL1_R, .type = ARM_CP_CONST,
9094 .accessfn = access_aa64_tid3,
9095 .resetvalue = cpu->isar.id_pfr2 },
9096 { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
9097 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
9098 .access = PL1_R, .type = ARM_CP_CONST,
9099 .accessfn = access_aa64_tid3,
9100 .resetvalue = cpu->isar.id_dfr1 },
9101 { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
9102 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
9103 .access = PL1_R, .type = ARM_CP_CONST,
9104 .accessfn = access_aa64_tid3,
9105 .resetvalue = cpu->isar.id_mmfr5 },
9106 { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
9107 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
9108 .access = PL1_R, .type = ARM_CP_CONST,
9109 .accessfn = access_aa64_tid3,
9110 .resetvalue = 0 },
9111 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
9112 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
9113 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
9114 .fgt = FGT_PMCEIDN_EL0,
9115 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
9116 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
9117 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
9118 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
9119 .fgt = FGT_PMCEIDN_EL0,
9120 .resetvalue = cpu->pmceid0 },
9121 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
9122 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
9123 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
9124 .fgt = FGT_PMCEIDN_EL0,
9125 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
9126 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
9127 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
9128 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
9129 .fgt = FGT_PMCEIDN_EL0,
9130 .resetvalue = cpu->pmceid1 },
9131 };
9132 #ifdef CONFIG_USER_ONLY
9133 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
9134 { .name = "ID_AA64PFR0_EL1",
9135 .exported_bits = R_ID_AA64PFR0_FP_MASK |
9136 R_ID_AA64PFR0_ADVSIMD_MASK |
9137 R_ID_AA64PFR0_SVE_MASK |
9138 R_ID_AA64PFR0_DIT_MASK,
9139 .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
9140 (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
9141 { .name = "ID_AA64PFR1_EL1",
9142 .exported_bits = R_ID_AA64PFR1_BT_MASK |
9143 R_ID_AA64PFR1_SSBS_MASK |
9144 R_ID_AA64PFR1_MTE_MASK |
9145 R_ID_AA64PFR1_SME_MASK },
9146 { .name = "ID_AA64PFR*_EL1_RESERVED",
9147 .is_glob = true },
9148 { .name = "ID_AA64ZFR0_EL1",
9149 .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
9150 R_ID_AA64ZFR0_AES_MASK |
9151 R_ID_AA64ZFR0_BITPERM_MASK |
9152 R_ID_AA64ZFR0_BFLOAT16_MASK |
9153 R_ID_AA64ZFR0_B16B16_MASK |
9154 R_ID_AA64ZFR0_SHA3_MASK |
9155 R_ID_AA64ZFR0_SM4_MASK |
9156 R_ID_AA64ZFR0_I8MM_MASK |
9157 R_ID_AA64ZFR0_F32MM_MASK |
9158 R_ID_AA64ZFR0_F64MM_MASK },
9159 { .name = "ID_AA64SMFR0_EL1",
9160 .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
9161 R_ID_AA64SMFR0_BI32I32_MASK |
9162 R_ID_AA64SMFR0_B16F32_MASK |
9163 R_ID_AA64SMFR0_F16F32_MASK |
9164 R_ID_AA64SMFR0_I8I32_MASK |
9165 R_ID_AA64SMFR0_F16F16_MASK |
9166 R_ID_AA64SMFR0_B16B16_MASK |
9167 R_ID_AA64SMFR0_I16I32_MASK |
9168 R_ID_AA64SMFR0_F64F64_MASK |
9169 R_ID_AA64SMFR0_I16I64_MASK |
9170 R_ID_AA64SMFR0_SMEVER_MASK |
9171 R_ID_AA64SMFR0_FA64_MASK },
9172 { .name = "ID_AA64MMFR0_EL1",
9173 .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
9174 .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
9175 (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
9176 { .name = "ID_AA64MMFR1_EL1",
9177 .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
9178 { .name = "ID_AA64MMFR2_EL1",
9179 .exported_bits = R_ID_AA64MMFR2_AT_MASK },
9180 { .name = "ID_AA64MMFR3_EL1",
9181 .exported_bits = 0 },
9182 { .name = "ID_AA64MMFR*_EL1_RESERVED",
9183 .is_glob = true },
9184 { .name = "ID_AA64DFR0_EL1",
9185 .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
9186 { .name = "ID_AA64DFR1_EL1" },
9187 { .name = "ID_AA64DFR*_EL1_RESERVED",
9188 .is_glob = true },
9189 { .name = "ID_AA64AFR*",
9190 .is_glob = true },
9191 { .name = "ID_AA64ISAR0_EL1",
9192 .exported_bits = R_ID_AA64ISAR0_AES_MASK |
9193 R_ID_AA64ISAR0_SHA1_MASK |
9194 R_ID_AA64ISAR0_SHA2_MASK |
9195 R_ID_AA64ISAR0_CRC32_MASK |
9196 R_ID_AA64ISAR0_ATOMIC_MASK |
9197 R_ID_AA64ISAR0_RDM_MASK |
9198 R_ID_AA64ISAR0_SHA3_MASK |
9199 R_ID_AA64ISAR0_SM3_MASK |
9200 R_ID_AA64ISAR0_SM4_MASK |
9201 R_ID_AA64ISAR0_DP_MASK |
9202 R_ID_AA64ISAR0_FHM_MASK |
9203 R_ID_AA64ISAR0_TS_MASK |
9204 R_ID_AA64ISAR0_RNDR_MASK },
9205 { .name = "ID_AA64ISAR1_EL1",
9206 .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
9207 R_ID_AA64ISAR1_APA_MASK |
9208 R_ID_AA64ISAR1_API_MASK |
9209 R_ID_AA64ISAR1_JSCVT_MASK |
9210 R_ID_AA64ISAR1_FCMA_MASK |
9211 R_ID_AA64ISAR1_LRCPC_MASK |
9212 R_ID_AA64ISAR1_GPA_MASK |
9213 R_ID_AA64ISAR1_GPI_MASK |
9214 R_ID_AA64ISAR1_FRINTTS_MASK |
9215 R_ID_AA64ISAR1_SB_MASK |
9216 R_ID_AA64ISAR1_BF16_MASK |
9217 R_ID_AA64ISAR1_DGH_MASK |
9218 R_ID_AA64ISAR1_I8MM_MASK },
9219 { .name = "ID_AA64ISAR2_EL1",
9220 .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
9221 R_ID_AA64ISAR2_RPRES_MASK |
9222 R_ID_AA64ISAR2_GPA3_MASK |
9223 R_ID_AA64ISAR2_APA3_MASK |
9224 R_ID_AA64ISAR2_MOPS_MASK |
9225 R_ID_AA64ISAR2_BC_MASK |
9226 R_ID_AA64ISAR2_RPRFM_MASK |
9227 R_ID_AA64ISAR2_CSSC_MASK },
9228 { .name = "ID_AA64ISAR*_EL1_RESERVED",
9229 .is_glob = true },
9230 };
9231 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
9232 #endif
9233 /*
9234 * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
9235 * TODO: For RMR, a write with bit 1 set should do something with
9236 * cpu_reset(). In the meantime, "the bit is strictly a request",
9237 * so we are in spec just ignoring writes.
9238 */
9239 if (!arm_feature(env, ARM_FEATURE_EL3) &&
9240 !arm_feature(env, ARM_FEATURE_EL2)) {
9241 ARMCPRegInfo el1_reset_regs[] = {
9242 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
9243 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
9244 .access = PL1_R,
9245 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9246 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
9247 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
9248 .access = PL1_RW, .type = ARM_CP_CONST,
9249 .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
9250 };
9251 define_arm_cp_regs(cpu, el1_reset_regs);
9252 }
9253 define_arm_cp_regs(cpu, v8_idregs);
9254 define_arm_cp_regs(cpu, v8_cp_reginfo);
9255 if (cpu_isar_feature(aa64_aa32_el1, cpu)) {
9256 define_arm_cp_regs(cpu, v8_aa32_el1_reginfo);
9257 }
9258
9259 for (i = 4; i < 16; i++) {
9260 /*
9261 * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
9262 * For pre-v8 cores there are RAZ patterns for these in
9263 * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
9264 * v8 extends the "must RAZ" part of the ID register space
9265 * to also cover c0, 0, c{8-15}, {0-7}.
9266 * These are STATE_AA32 because in the AArch64 sysreg space
9267 * c4-c7 is where the AArch64 ID registers live (and we've
9268 * already defined those in v8_idregs[]), and c8-c15 are not
9269 * "must RAZ" for AArch64.
9270 */
9271 g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
9272 ARMCPRegInfo v8_aa32_raz_idregs = {
9273 .name = name,
9274 .state = ARM_CP_STATE_AA32,
9275 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
9276 .access = PL1_R, .type = ARM_CP_CONST,
9277 .accessfn = access_aa64_tid3,
9278 .resetvalue = 0 };
9279 define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
9280 }
9281 }
9282
9283 /*
9284 * Register the base EL2 cpregs.
9285 * Pre v8, these registers are implemented only as part of the
9286 * Virtualization Extensions (EL2 present). Beginning with v8,
9287 * if EL2 is missing but EL3 is enabled, mostly these become
9288 * RES0 from EL3, with some specific exceptions.
9289 */
9290 if (arm_feature(env, ARM_FEATURE_EL2)
9291 || (arm_feature(env, ARM_FEATURE_EL3)
9292 && arm_feature(env, ARM_FEATURE_V8))) {
9293 uint64_t vmpidr_def = mpidr_read_val(env);
9294 ARMCPRegInfo vpidr_regs[] = {
9295 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
9296 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
9297 .access = PL2_RW, .accessfn = access_el3_aa32ns,
9298 .resetvalue = cpu->midr,
9299 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
9300 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
9301 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
9302 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
9303 .access = PL2_RW, .resetvalue = cpu->midr,
9304 .type = ARM_CP_EL3_NO_EL2_C_NZ,
9305 .nv2_redirect_offset = 0x88,
9306 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
9307 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
9308 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
9309 .access = PL2_RW, .accessfn = access_el3_aa32ns,
9310 .resetvalue = vmpidr_def,
9311 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
9312 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
9313 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
9314 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
9315 .access = PL2_RW, .resetvalue = vmpidr_def,
9316 .type = ARM_CP_EL3_NO_EL2_C_NZ,
9317 .nv2_redirect_offset = 0x50,
9318 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
9319 };
9320 /*
9321 * The only field of MDCR_EL2 that has a defined architectural reset
9322 * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
9323 */
9324 ARMCPRegInfo mdcr_el2 = {
9325 .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
9326 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
9327 .writefn = mdcr_el2_write,
9328 .access = PL2_RW, .resetvalue = pmu_num_counters(env),
9329 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
9330 };
9331 define_one_arm_cp_reg(cpu, &mdcr_el2);
9332 define_arm_cp_regs(cpu, vpidr_regs);
9333 define_arm_cp_regs(cpu, el2_cp_reginfo);
9334 if (arm_feature(env, ARM_FEATURE_V8)) {
9335 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
9336 }
9337 if (cpu_isar_feature(aa64_sel2, cpu)) {
9338 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
9339 }
9340 /*
9341 * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
9342 * See commentary near RMR_EL1.
9343 */
9344 if (!arm_feature(env, ARM_FEATURE_EL3)) {
9345 static const ARMCPRegInfo el2_reset_regs[] = {
9346 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
9347 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
9348 .access = PL2_R,
9349 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9350 { .name = "RVBAR", .type = ARM_CP_ALIAS,
9351 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
9352 .access = PL2_R,
9353 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
9354 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
9355 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
9356 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
9357 };
9358 define_arm_cp_regs(cpu, el2_reset_regs);
9359 }
9360 }
9361
9362 /* Register the base EL3 cpregs. */
9363 if (arm_feature(env, ARM_FEATURE_EL3)) {
9364 define_arm_cp_regs(cpu, el3_cp_reginfo);
9365 ARMCPRegInfo el3_regs[] = {
9366 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
9367 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
9368 .access = PL3_R,
9369 .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
9370 { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
9371 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
9372 .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
9373 { .name = "RMR", .state = ARM_CP_STATE_AA32,
9374 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
9375 .access = PL3_RW, .type = ARM_CP_CONST,
9376 .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
9377 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
9378 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
9379 .access = PL3_RW,
9380 .raw_writefn = raw_write, .writefn = sctlr_write,
9381 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
9382 .resetvalue = cpu->reset_sctlr },
9383 };
9384
9385 define_arm_cp_regs(cpu, el3_regs);
9386 }
9387 /*
9388 * The behaviour of NSACR is sufficiently various that we don't
9389 * try to describe it in a single reginfo:
9390 * if EL3 is 64 bit, then trap to EL3 from S EL1,
9391 * reads as constant 0xc00 from NS EL1 and NS EL2
9392 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
9393 * if v7 without EL3, register doesn't exist
9394 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
9395 */
9396 if (arm_feature(env, ARM_FEATURE_EL3)) {
9397 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
9398 static const ARMCPRegInfo nsacr = {
9399 .name = "NSACR", .type = ARM_CP_CONST,
9400 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9401 .access = PL1_RW, .accessfn = nsacr_access,
9402 .resetvalue = 0xc00
9403 };
9404 define_one_arm_cp_reg(cpu, &nsacr);
9405 } else {
9406 static const ARMCPRegInfo nsacr = {
9407 .name = "NSACR",
9408 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9409 .access = PL3_RW | PL1_R,
9410 .resetvalue = 0,
9411 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
9412 };
9413 define_one_arm_cp_reg(cpu, &nsacr);
9414 }
9415 } else {
9416 if (arm_feature(env, ARM_FEATURE_V8)) {
9417 static const ARMCPRegInfo nsacr = {
9418 .name = "NSACR", .type = ARM_CP_CONST,
9419 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
9420 .access = PL1_R,
9421 .resetvalue = 0xc00
9422 };
9423 define_one_arm_cp_reg(cpu, &nsacr);
9424 }
9425 }
9426
9427 if (arm_feature(env, ARM_FEATURE_PMSA)) {
9428 if (arm_feature(env, ARM_FEATURE_V6)) {
9429 /* PMSAv6 not implemented */
9430 assert(arm_feature(env, ARM_FEATURE_V7));
9431 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
9432 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
9433 } else {
9434 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
9435 }
9436 } else {
9437 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
9438 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
9439 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
9440 if (cpu_isar_feature(aa32_hpd, cpu)) {
9441 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
9442 }
9443 }
9444 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
9445 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
9446 }
9447 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
9448 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
9449 }
9450 if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
9451 define_arm_cp_regs(cpu, gen_timer_ecv_cp_reginfo);
9452 }
9453 #ifndef CONFIG_USER_ONLY
9454 if (cpu_isar_feature(aa64_ecv, cpu)) {
9455 define_one_arm_cp_reg(cpu, &gen_timer_cntpoff_reginfo);
9456 }
9457 #endif
9458 if (arm_feature(env, ARM_FEATURE_VAPA)) {
9459 ARMCPRegInfo vapa_cp_reginfo[] = {
9460 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
9461 .access = PL1_RW, .resetvalue = 0,
9462 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
9463 offsetoflow32(CPUARMState, cp15.par_ns) },
9464 .writefn = par_write},
9465 #ifndef CONFIG_USER_ONLY
9466 /* This underdecoding is safe because the reginfo is NO_RAW. */
9467 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
9468 .access = PL1_W, .accessfn = ats_access,
9469 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
9470 #endif
9471 };
9472
9473 /*
9474 * When LPAE exists this 32-bit PAR register is an alias of the
9475 * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
9476 */
9477 if (arm_feature(env, ARM_FEATURE_LPAE)) {
9478 vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
9479 }
9480 define_arm_cp_regs(cpu, vapa_cp_reginfo);
9481 }
9482 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
9483 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
9484 }
9485 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
9486 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
9487 }
9488 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
9489 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
9490 }
9491 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
9492 define_arm_cp_regs(cpu, omap_cp_reginfo);
9493 }
9494 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
9495 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
9496 }
9497 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9498 define_arm_cp_regs(cpu, xscale_cp_reginfo);
9499 }
9500 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
9501 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
9502 }
9503 if (arm_feature(env, ARM_FEATURE_LPAE)) {
9504 define_arm_cp_regs(cpu, lpae_cp_reginfo);
9505 }
9506 if (cpu_isar_feature(aa32_jazelle, cpu)) {
9507 define_arm_cp_regs(cpu, jazelle_regs);
9508 }
9509 /*
9510 * Slightly awkwardly, the OMAP and StrongARM cores need all of
9511 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
9512 * be read-only (ie write causes UNDEF exception).
9513 */
9514 {
9515 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
9516 /*
9517 * Pre-v8 MIDR space.
9518 * Note that the MIDR isn't a simple constant register because
9519 * of the TI925 behaviour where writes to another register can
9520 * cause the MIDR value to change.
9521 *
9522 * Unimplemented registers in the c15 0 0 0 space default to
9523 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
9524 * and friends override accordingly.
9525 */
9526 { .name = "MIDR",
9527 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
9528 .access = PL1_R, .resetvalue = cpu->midr,
9529 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
9530 .readfn = midr_read,
9531 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9532 .type = ARM_CP_OVERRIDE },
9533 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
9534 { .name = "DUMMY",
9535 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
9536 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9537 { .name = "DUMMY",
9538 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
9539 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9540 { .name = "DUMMY",
9541 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
9542 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9543 { .name = "DUMMY",
9544 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
9545 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9546 { .name = "DUMMY",
9547 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
9548 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9549 };
9550 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
9551 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
9552 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
9553 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
9554 .fgt = FGT_MIDR_EL1,
9555 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
9556 .readfn = midr_read },
9557 /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
9558 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
9559 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
9560 .access = PL1_R, .resetvalue = cpu->midr },
9561 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
9562 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
9563 .access = PL1_R,
9564 .accessfn = access_aa64_tid1,
9565 .fgt = FGT_REVIDR_EL1,
9566 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
9567 };
9568 ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
9569 .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
9570 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9571 .access = PL1_R, .resetvalue = cpu->midr
9572 };
9573 ARMCPRegInfo id_cp_reginfo[] = {
9574 /* These are common to v8 and pre-v8 */
9575 { .name = "CTR",
9576 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
9577 .access = PL1_R, .accessfn = ctr_el0_access,
9578 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9579 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
9580 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
9581 .access = PL0_R, .accessfn = ctr_el0_access,
9582 .fgt = FGT_CTR_EL0,
9583 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
9584 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
9585 { .name = "TCMTR",
9586 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
9587 .access = PL1_R,
9588 .accessfn = access_aa32_tid1,
9589 .type = ARM_CP_CONST, .resetvalue = 0 },
9590 };
9591 /* TLBTR is specific to VMSA */
9592 ARMCPRegInfo id_tlbtr_reginfo = {
9593 .name = "TLBTR",
9594 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
9595 .access = PL1_R,
9596 .accessfn = access_aa32_tid1,
9597 .type = ARM_CP_CONST, .resetvalue = 0,
9598 };
9599 /* MPUIR is specific to PMSA V6+ */
9600 ARMCPRegInfo id_mpuir_reginfo = {
9601 .name = "MPUIR",
9602 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
9603 .access = PL1_R, .type = ARM_CP_CONST,
9604 .resetvalue = cpu->pmsav7_dregion << 8
9605 };
9606 /* HMPUIR is specific to PMSA V8 */
9607 ARMCPRegInfo id_hmpuir_reginfo = {
9608 .name = "HMPUIR",
9609 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
9610 .access = PL2_R, .type = ARM_CP_CONST,
9611 .resetvalue = cpu->pmsav8r_hdregion
9612 };
9613 static const ARMCPRegInfo crn0_wi_reginfo = {
9614 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
9615 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
9616 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
9617 };
9618 #ifdef CONFIG_USER_ONLY
9619 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
9620 { .name = "MIDR_EL1",
9621 .exported_bits = R_MIDR_EL1_REVISION_MASK |
9622 R_MIDR_EL1_PARTNUM_MASK |
9623 R_MIDR_EL1_ARCHITECTURE_MASK |
9624 R_MIDR_EL1_VARIANT_MASK |
9625 R_MIDR_EL1_IMPLEMENTER_MASK },
9626 { .name = "REVIDR_EL1" },
9627 };
9628 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
9629 #endif
9630 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
9631 arm_feature(env, ARM_FEATURE_STRONGARM)) {
9632 size_t i;
9633 /*
9634 * Register the blanket "writes ignored" value first to cover the
9635 * whole space. Then update the specific ID registers to allow write
9636 * access, so that they ignore writes rather than causing them to
9637 * UNDEF.
9638 */
9639 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
9640 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
9641 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
9642 }
9643 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
9644 id_cp_reginfo[i].access = PL1_RW;
9645 }
9646 id_mpuir_reginfo.access = PL1_RW;
9647 id_tlbtr_reginfo.access = PL1_RW;
9648 }
9649 if (arm_feature(env, ARM_FEATURE_V8)) {
9650 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
9651 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9652 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
9653 }
9654 } else {
9655 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
9656 }
9657 define_arm_cp_regs(cpu, id_cp_reginfo);
9658 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
9659 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
9660 } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
9661 arm_feature(env, ARM_FEATURE_V8)) {
9662 uint32_t i = 0;
9663 char *tmp_string;
9664
9665 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9666 define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
9667 define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
9668
9669 /* Register alias is only valid for first 32 indexes */
9670 for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
9671 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9672 uint8_t opc1 = extract32(i, 4, 1);
9673 uint8_t opc2 = extract32(i, 0, 1) << 2;
9674
9675 tmp_string = g_strdup_printf("PRBAR%u", i);
9676 ARMCPRegInfo tmp_prbarn_reginfo = {
9677 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9678 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9679 .access = PL1_RW, .resetvalue = 0,
9680 .accessfn = access_tvm_trvm,
9681 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9682 };
9683 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
9684 g_free(tmp_string);
9685
9686 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9687 tmp_string = g_strdup_printf("PRLAR%u", i);
9688 ARMCPRegInfo tmp_prlarn_reginfo = {
9689 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
9690 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9691 .access = PL1_RW, .resetvalue = 0,
9692 .accessfn = access_tvm_trvm,
9693 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9694 };
9695 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
9696 g_free(tmp_string);
9697 }
9698
9699 /* Register alias is only valid for first 32 indexes */
9700 for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
9701 uint8_t crm = 0b1000 | extract32(i, 1, 3);
9702 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
9703 uint8_t opc2 = extract32(i, 0, 1) << 2;
9704
9705 tmp_string = g_strdup_printf("HPRBAR%u", i);
9706 ARMCPRegInfo tmp_hprbarn_reginfo = {
9707 .name = tmp_string,
9708 .type = ARM_CP_NO_RAW,
9709 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9710 .access = PL2_RW, .resetvalue = 0,
9711 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9712 };
9713 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
9714 g_free(tmp_string);
9715
9716 opc2 = extract32(i, 0, 1) << 2 | 0x1;
9717 tmp_string = g_strdup_printf("HPRLAR%u", i);
9718 ARMCPRegInfo tmp_hprlarn_reginfo = {
9719 .name = tmp_string,
9720 .type = ARM_CP_NO_RAW,
9721 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
9722 .access = PL2_RW, .resetvalue = 0,
9723 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
9724 };
9725 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
9726 g_free(tmp_string);
9727 }
9728 } else if (arm_feature(env, ARM_FEATURE_V7)) {
9729 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
9730 }
9731 }
9732
9733 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
9734 ARMCPRegInfo mpidr_cp_reginfo[] = {
9735 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
9736 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
9737 .fgt = FGT_MPIDR_EL1,
9738 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
9739 };
9740 #ifdef CONFIG_USER_ONLY
9741 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
9742 { .name = "MPIDR_EL1",
9743 .fixed_bits = 0x0000000080000000 },
9744 };
9745 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
9746 #endif
9747 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
9748 }
9749
9750 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
9751 ARMCPRegInfo auxcr_reginfo[] = {
9752 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
9753 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
9754 .access = PL1_RW, .accessfn = access_tacr,
9755 .nv2_redirect_offset = 0x118,
9756 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
9757 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
9758 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
9759 .access = PL2_RW, .type = ARM_CP_CONST,
9760 .resetvalue = 0 },
9761 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
9762 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
9763 .access = PL3_RW, .type = ARM_CP_CONST,
9764 .resetvalue = 0 },
9765 };
9766 define_arm_cp_regs(cpu, auxcr_reginfo);
9767 if (cpu_isar_feature(aa32_ac2, cpu)) {
9768 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
9769 }
9770 }
9771
9772 if (arm_feature(env, ARM_FEATURE_CBAR)) {
9773 /*
9774 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
9775 * There are two flavours:
9776 * (1) older 32-bit only cores have a simple 32-bit CBAR
9777 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
9778 * 32-bit register visible to AArch32 at a different encoding
9779 * to the "flavour 1" register and with the bits rearranged to
9780 * be able to squash a 64-bit address into the 32-bit view.
9781 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
9782 * in future if we support AArch32-only configs of some of the
9783 * AArch64 cores we might need to add a specific feature flag
9784 * to indicate cores with "flavour 2" CBAR.
9785 */
9786 if (arm_feature(env, ARM_FEATURE_V8)) {
9787 /* 32 bit view is [31:18] 0...0 [43:32]. */
9788 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
9789 | extract64(cpu->reset_cbar, 32, 12);
9790 ARMCPRegInfo cbar_reginfo[] = {
9791 { .name = "CBAR",
9792 .type = ARM_CP_CONST,
9793 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
9794 .access = PL1_R, .resetvalue = cbar32 },
9795 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
9796 .type = ARM_CP_CONST,
9797 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
9798 .access = PL1_R, .resetvalue = cpu->reset_cbar },
9799 };
9800 /* We don't implement a r/w 64 bit CBAR currently */
9801 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
9802 define_arm_cp_regs(cpu, cbar_reginfo);
9803 } else {
9804 ARMCPRegInfo cbar = {
9805 .name = "CBAR",
9806 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
9807 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
9808 .fieldoffset = offsetof(CPUARMState,
9809 cp15.c15_config_base_address)
9810 };
9811 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
9812 cbar.access = PL1_R;
9813 cbar.fieldoffset = 0;
9814 cbar.type = ARM_CP_CONST;
9815 }
9816 define_one_arm_cp_reg(cpu, &cbar);
9817 }
9818 }
9819
9820 if (arm_feature(env, ARM_FEATURE_VBAR)) {
9821 static const ARMCPRegInfo vbar_cp_reginfo[] = {
9822 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
9823 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
9824 .access = PL1_RW, .writefn = vbar_write,
9825 .accessfn = access_nv1,
9826 .fgt = FGT_VBAR_EL1,
9827 .nv2_redirect_offset = 0x250 | NV2_REDIR_NV1,
9828 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
9829 offsetof(CPUARMState, cp15.vbar_ns) },
9830 .resetvalue = 0 },
9831 };
9832 define_arm_cp_regs(cpu, vbar_cp_reginfo);
9833 }
9834
9835 /* Generic registers whose values depend on the implementation */
9836 {
9837 ARMCPRegInfo sctlr = {
9838 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
9839 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
9840 .access = PL1_RW, .accessfn = access_tvm_trvm,
9841 .fgt = FGT_SCTLR_EL1,
9842 .nv2_redirect_offset = 0x110 | NV2_REDIR_NV1,
9843 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
9844 offsetof(CPUARMState, cp15.sctlr_ns) },
9845 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
9846 .raw_writefn = raw_write,
9847 };
9848 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9849 /*
9850 * Normally we would always end the TB on an SCTLR write, but Linux
9851 * arch/arm/mach-pxa/sleep.S expects two instructions following
9852 * an MMU enable to execute from cache. Imitate this behaviour.
9853 */
9854 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
9855 }
9856 define_one_arm_cp_reg(cpu, &sctlr);
9857
9858 if (arm_feature(env, ARM_FEATURE_PMSA) &&
9859 arm_feature(env, ARM_FEATURE_V8)) {
9860 ARMCPRegInfo vsctlr = {
9861 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
9862 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
9863 .access = PL2_RW, .resetvalue = 0x0,
9864 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
9865 };
9866 define_one_arm_cp_reg(cpu, &vsctlr);
9867 }
9868 }
9869
9870 if (cpu_isar_feature(aa64_lor, cpu)) {
9871 define_arm_cp_regs(cpu, lor_reginfo);
9872 }
9873 if (cpu_isar_feature(aa64_pan, cpu)) {
9874 define_one_arm_cp_reg(cpu, &pan_reginfo);
9875 }
9876 #ifndef CONFIG_USER_ONLY
9877 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
9878 define_arm_cp_regs(cpu, ats1e1_reginfo);
9879 }
9880 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
9881 define_arm_cp_regs(cpu, ats1cp_reginfo);
9882 }
9883 #endif
9884 if (cpu_isar_feature(aa64_uao, cpu)) {
9885 define_one_arm_cp_reg(cpu, &uao_reginfo);
9886 }
9887
9888 if (cpu_isar_feature(aa64_dit, cpu)) {
9889 define_one_arm_cp_reg(cpu, &dit_reginfo);
9890 }
9891 if (cpu_isar_feature(aa64_ssbs, cpu)) {
9892 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
9893 }
9894 if (cpu_isar_feature(any_ras, cpu)) {
9895 define_arm_cp_regs(cpu, minimal_ras_reginfo);
9896 }
9897
9898 if (cpu_isar_feature(aa64_vh, cpu) ||
9899 cpu_isar_feature(aa64_debugv8p2, cpu)) {
9900 define_one_arm_cp_reg(cpu, &contextidr_el2);
9901 }
9902 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9903 define_arm_cp_regs(cpu, vhe_reginfo);
9904 }
9905
9906 if (cpu_isar_feature(aa64_sve, cpu)) {
9907 define_arm_cp_regs(cpu, zcr_reginfo);
9908 }
9909
9910 if (cpu_isar_feature(aa64_hcx, cpu)) {
9911 define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
9912 }
9913
9914 #ifdef TARGET_AARCH64
9915 if (cpu_isar_feature(aa64_sme, cpu)) {
9916 define_arm_cp_regs(cpu, sme_reginfo);
9917 }
9918 if (cpu_isar_feature(aa64_pauth, cpu)) {
9919 define_arm_cp_regs(cpu, pauth_reginfo);
9920 }
9921 if (cpu_isar_feature(aa64_rndr, cpu)) {
9922 define_arm_cp_regs(cpu, rndr_reginfo);
9923 }
9924 if (cpu_isar_feature(aa64_tlbirange, cpu)) {
9925 define_arm_cp_regs(cpu, tlbirange_reginfo);
9926 }
9927 if (cpu_isar_feature(aa64_tlbios, cpu)) {
9928 define_arm_cp_regs(cpu, tlbios_reginfo);
9929 }
9930 /* Data Cache clean instructions up to PoP */
9931 if (cpu_isar_feature(aa64_dcpop, cpu)) {
9932 define_one_arm_cp_reg(cpu, dcpop_reg);
9933
9934 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
9935 define_one_arm_cp_reg(cpu, dcpodp_reg);
9936 }
9937 }
9938
9939 /*
9940 * If full MTE is enabled, add all of the system registers.
9941 * If only "instructions available at EL0" are enabled,
9942 * then define only a RAZ/WI version of PSTATE.TCO.
9943 */
9944 if (cpu_isar_feature(aa64_mte, cpu)) {
9945 ARMCPRegInfo gmid_reginfo = {
9946 .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
9947 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
9948 .access = PL1_R, .accessfn = access_aa64_tid5,
9949 .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
9950 };
9951 define_one_arm_cp_reg(cpu, &gmid_reginfo);
9952 define_arm_cp_regs(cpu, mte_reginfo);
9953 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9954 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
9955 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
9956 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
9957 }
9958
9959 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
9960 define_arm_cp_regs(cpu, scxtnum_reginfo);
9961 }
9962
9963 if (cpu_isar_feature(aa64_fgt, cpu)) {
9964 define_arm_cp_regs(cpu, fgt_reginfo);
9965 }
9966
9967 if (cpu_isar_feature(aa64_rme, cpu)) {
9968 define_arm_cp_regs(cpu, rme_reginfo);
9969 if (cpu_isar_feature(aa64_mte, cpu)) {
9970 define_arm_cp_regs(cpu, rme_mte_reginfo);
9971 }
9972 }
9973
9974 if (cpu_isar_feature(aa64_nv2, cpu)) {
9975 define_arm_cp_regs(cpu, nv2_reginfo);
9976 }
9977
9978 if (cpu_isar_feature(aa64_nmi, cpu)) {
9979 define_arm_cp_regs(cpu, nmi_reginfo);
9980 }
9981 #endif
9982
9983 if (cpu_isar_feature(any_predinv, cpu)) {
9984 define_arm_cp_regs(cpu, predinv_reginfo);
9985 }
9986
9987 if (cpu_isar_feature(any_ccidx, cpu)) {
9988 define_arm_cp_regs(cpu, ccsidr2_reginfo);
9989 }
9990
9991 #ifndef CONFIG_USER_ONLY
9992 /*
9993 * Register redirections and aliases must be done last,
9994 * after the registers from the other extensions have been defined.
9995 */
9996 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9997 define_arm_vh_e2h_redirects_aliases(cpu);
9998 }
9999 #endif
10000 }
10001
10002 /*
10003 * Private utility function for define_one_arm_cp_reg_with_opaque():
10004 * add a single reginfo struct to the hash table.
10005 */
add_cpreg_to_hashtable(ARMCPU * cpu,const ARMCPRegInfo * r,void * opaque,CPState state,CPSecureState secstate,int crm,int opc1,int opc2,const char * name)10006 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
10007 void *opaque, CPState state,
10008 CPSecureState secstate,
10009 int crm, int opc1, int opc2,
10010 const char *name)
10011 {
10012 CPUARMState *env = &cpu->env;
10013 uint32_t key;
10014 ARMCPRegInfo *r2;
10015 bool is64 = r->type & ARM_CP_64BIT;
10016 bool ns = secstate & ARM_CP_SECSTATE_NS;
10017 int cp = r->cp;
10018 size_t name_len;
10019 bool make_const;
10020
10021 switch (state) {
10022 case ARM_CP_STATE_AA32:
10023 /* We assume it is a cp15 register if the .cp field is left unset. */
10024 if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
10025 cp = 15;
10026 }
10027 key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
10028 break;
10029 case ARM_CP_STATE_AA64:
10030 /*
10031 * To allow abbreviation of ARMCPRegInfo definitions, we treat
10032 * cp == 0 as equivalent to the value for "standard guest-visible
10033 * sysreg". STATE_BOTH definitions are also always "standard sysreg"
10034 * in their AArch64 view (the .cp value may be non-zero for the
10035 * benefit of the AArch32 view).
10036 */
10037 if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
10038 cp = CP_REG_ARM64_SYSREG_CP;
10039 }
10040 key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
10041 break;
10042 default:
10043 g_assert_not_reached();
10044 }
10045
10046 /* Overriding of an existing definition must be explicitly requested. */
10047 if (!(r->type & ARM_CP_OVERRIDE)) {
10048 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
10049 if (oldreg) {
10050 assert(oldreg->type & ARM_CP_OVERRIDE);
10051 }
10052 }
10053
10054 /*
10055 * Eliminate registers that are not present because the EL is missing.
10056 * Doing this here makes it easier to put all registers for a given
10057 * feature into the same ARMCPRegInfo array and define them all at once.
10058 */
10059 make_const = false;
10060 if (arm_feature(env, ARM_FEATURE_EL3)) {
10061 /*
10062 * An EL2 register without EL2 but with EL3 is (usually) RES0.
10063 * See rule RJFFP in section D1.1.3 of DDI0487H.a.
10064 */
10065 int min_el = ctz32(r->access) / 2;
10066 if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
10067 if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
10068 return;
10069 }
10070 make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
10071 }
10072 } else {
10073 CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
10074 ? PL2_RW : PL1_RW);
10075 if ((r->access & max_el) == 0) {
10076 return;
10077 }
10078 }
10079
10080 /* Combine cpreg and name into one allocation. */
10081 name_len = strlen(name) + 1;
10082 r2 = g_malloc(sizeof(*r2) + name_len);
10083 *r2 = *r;
10084 r2->name = memcpy(r2 + 1, name, name_len);
10085
10086 /*
10087 * Update fields to match the instantiation, overwiting wildcards
10088 * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
10089 */
10090 r2->cp = cp;
10091 r2->crm = crm;
10092 r2->opc1 = opc1;
10093 r2->opc2 = opc2;
10094 r2->state = state;
10095 r2->secure = secstate;
10096 if (opaque) {
10097 r2->opaque = opaque;
10098 }
10099
10100 if (make_const) {
10101 /* This should not have been a very special register to begin. */
10102 int old_special = r2->type & ARM_CP_SPECIAL_MASK;
10103 assert(old_special == 0 || old_special == ARM_CP_NOP);
10104 /*
10105 * Set the special function to CONST, retaining the other flags.
10106 * This is important for e.g. ARM_CP_SVE so that we still
10107 * take the SVE trap if CPTR_EL3.EZ == 0.
10108 */
10109 r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
10110 /*
10111 * Usually, these registers become RES0, but there are a few
10112 * special cases like VPIDR_EL2 which have a constant non-zero
10113 * value with writes ignored.
10114 */
10115 if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
10116 r2->resetvalue = 0;
10117 }
10118 /*
10119 * ARM_CP_CONST has precedence, so removing the callbacks and
10120 * offsets are not strictly necessary, but it is potentially
10121 * less confusing to debug later.
10122 */
10123 r2->readfn = NULL;
10124 r2->writefn = NULL;
10125 r2->raw_readfn = NULL;
10126 r2->raw_writefn = NULL;
10127 r2->resetfn = NULL;
10128 r2->fieldoffset = 0;
10129 r2->bank_fieldoffsets[0] = 0;
10130 r2->bank_fieldoffsets[1] = 0;
10131 } else {
10132 bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
10133
10134 if (isbanked) {
10135 /*
10136 * Register is banked (using both entries in array).
10137 * Overwriting fieldoffset as the array is only used to define
10138 * banked registers but later only fieldoffset is used.
10139 */
10140 r2->fieldoffset = r->bank_fieldoffsets[ns];
10141 }
10142 if (state == ARM_CP_STATE_AA32) {
10143 if (isbanked) {
10144 /*
10145 * If the register is banked then we don't need to migrate or
10146 * reset the 32-bit instance in certain cases:
10147 *
10148 * 1) If the register has both 32-bit and 64-bit instances
10149 * then we can count on the 64-bit instance taking care
10150 * of the non-secure bank.
10151 * 2) If ARMv8 is enabled then we can count on a 64-bit
10152 * version taking care of the secure bank. This requires
10153 * that separate 32 and 64-bit definitions are provided.
10154 */
10155 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
10156 (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
10157 r2->type |= ARM_CP_ALIAS;
10158 }
10159 } else if ((secstate != r->secure) && !ns) {
10160 /*
10161 * The register is not banked so we only want to allow
10162 * migration of the non-secure instance.
10163 */
10164 r2->type |= ARM_CP_ALIAS;
10165 }
10166
10167 if (HOST_BIG_ENDIAN &&
10168 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
10169 r2->fieldoffset += sizeof(uint32_t);
10170 }
10171 }
10172 }
10173
10174 /*
10175 * By convention, for wildcarded registers only the first
10176 * entry is used for migration; the others are marked as
10177 * ALIAS so we don't try to transfer the register
10178 * multiple times. Special registers (ie NOP/WFI) are
10179 * never migratable and not even raw-accessible.
10180 */
10181 if (r2->type & ARM_CP_SPECIAL_MASK) {
10182 r2->type |= ARM_CP_NO_RAW;
10183 }
10184 if (((r->crm == CP_ANY) && crm != 0) ||
10185 ((r->opc1 == CP_ANY) && opc1 != 0) ||
10186 ((r->opc2 == CP_ANY) && opc2 != 0)) {
10187 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
10188 }
10189
10190 /*
10191 * Check that raw accesses are either forbidden or handled. Note that
10192 * we can't assert this earlier because the setup of fieldoffset for
10193 * banked registers has to be done first.
10194 */
10195 if (!(r2->type & ARM_CP_NO_RAW)) {
10196 assert(!raw_accessors_invalid(r2));
10197 }
10198
10199 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
10200 }
10201
10202
define_one_arm_cp_reg_with_opaque(ARMCPU * cpu,const ARMCPRegInfo * r,void * opaque)10203 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
10204 const ARMCPRegInfo *r, void *opaque)
10205 {
10206 /*
10207 * Define implementations of coprocessor registers.
10208 * We store these in a hashtable because typically
10209 * there are less than 150 registers in a space which
10210 * is 16*16*16*8*8 = 262144 in size.
10211 * Wildcarding is supported for the crm, opc1 and opc2 fields.
10212 * If a register is defined twice then the second definition is
10213 * used, so this can be used to define some generic registers and
10214 * then override them with implementation specific variations.
10215 * At least one of the original and the second definition should
10216 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
10217 * against accidental use.
10218 *
10219 * The state field defines whether the register is to be
10220 * visible in the AArch32 or AArch64 execution state. If the
10221 * state is set to ARM_CP_STATE_BOTH then we synthesise a
10222 * reginfo structure for the AArch32 view, which sees the lower
10223 * 32 bits of the 64 bit register.
10224 *
10225 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
10226 * be wildcarded. AArch64 registers are always considered to be 64
10227 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
10228 * the register, if any.
10229 */
10230 int crm, opc1, opc2;
10231 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
10232 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
10233 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
10234 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
10235 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
10236 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
10237 CPState state;
10238
10239 /* 64 bit registers have only CRm and Opc1 fields */
10240 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
10241 /* op0 only exists in the AArch64 encodings */
10242 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
10243 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
10244 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
10245 /*
10246 * This API is only for Arm's system coprocessors (14 and 15) or
10247 * (M-profile or v7A-and-earlier only) for implementation defined
10248 * coprocessors in the range 0..7. Our decode assumes this, since
10249 * 8..13 can be used for other insns including VFP and Neon. See
10250 * valid_cp() in translate.c. Assert here that we haven't tried
10251 * to use an invalid coprocessor number.
10252 */
10253 switch (r->state) {
10254 case ARM_CP_STATE_BOTH:
10255 /* 0 has a special meaning, but otherwise the same rules as AA32. */
10256 if (r->cp == 0) {
10257 break;
10258 }
10259 /* fall through */
10260 case ARM_CP_STATE_AA32:
10261 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
10262 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
10263 assert(r->cp >= 14 && r->cp <= 15);
10264 } else {
10265 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
10266 }
10267 break;
10268 case ARM_CP_STATE_AA64:
10269 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
10270 break;
10271 default:
10272 g_assert_not_reached();
10273 }
10274 /*
10275 * The AArch64 pseudocode CheckSystemAccess() specifies that op1
10276 * encodes a minimum access level for the register. We roll this
10277 * runtime check into our general permission check code, so check
10278 * here that the reginfo's specified permissions are strict enough
10279 * to encompass the generic architectural permission check.
10280 */
10281 if (r->state != ARM_CP_STATE_AA32) {
10282 CPAccessRights mask;
10283 switch (r->opc1) {
10284 case 0:
10285 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
10286 mask = PL0U_R | PL1_RW;
10287 break;
10288 case 1: case 2:
10289 /* min_EL EL1 */
10290 mask = PL1_RW;
10291 break;
10292 case 3:
10293 /* min_EL EL0 */
10294 mask = PL0_RW;
10295 break;
10296 case 4:
10297 case 5:
10298 /* min_EL EL2 */
10299 mask = PL2_RW;
10300 break;
10301 case 6:
10302 /* min_EL EL3 */
10303 mask = PL3_RW;
10304 break;
10305 case 7:
10306 /* min_EL EL1, secure mode only (we don't check the latter) */
10307 mask = PL1_RW;
10308 break;
10309 default:
10310 /* broken reginfo with out-of-range opc1 */
10311 g_assert_not_reached();
10312 }
10313 /* assert our permissions are not too lax (stricter is fine) */
10314 assert((r->access & ~mask) == 0);
10315 }
10316
10317 /*
10318 * Check that the register definition has enough info to handle
10319 * reads and writes if they are permitted.
10320 */
10321 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
10322 if (r->access & PL3_R) {
10323 assert((r->fieldoffset ||
10324 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
10325 r->readfn);
10326 }
10327 if (r->access & PL3_W) {
10328 assert((r->fieldoffset ||
10329 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
10330 r->writefn);
10331 }
10332 }
10333
10334 for (crm = crmmin; crm <= crmmax; crm++) {
10335 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
10336 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
10337 for (state = ARM_CP_STATE_AA32;
10338 state <= ARM_CP_STATE_AA64; state++) {
10339 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
10340 continue;
10341 }
10342 if (state == ARM_CP_STATE_AA32) {
10343 /*
10344 * Under AArch32 CP registers can be common
10345 * (same for secure and non-secure world) or banked.
10346 */
10347 char *name;
10348
10349 switch (r->secure) {
10350 case ARM_CP_SECSTATE_S:
10351 case ARM_CP_SECSTATE_NS:
10352 add_cpreg_to_hashtable(cpu, r, opaque, state,
10353 r->secure, crm, opc1, opc2,
10354 r->name);
10355 break;
10356 case ARM_CP_SECSTATE_BOTH:
10357 name = g_strdup_printf("%s_S", r->name);
10358 add_cpreg_to_hashtable(cpu, r, opaque, state,
10359 ARM_CP_SECSTATE_S,
10360 crm, opc1, opc2, name);
10361 g_free(name);
10362 add_cpreg_to_hashtable(cpu, r, opaque, state,
10363 ARM_CP_SECSTATE_NS,
10364 crm, opc1, opc2, r->name);
10365 break;
10366 default:
10367 g_assert_not_reached();
10368 }
10369 } else {
10370 /*
10371 * AArch64 registers get mapped to non-secure instance
10372 * of AArch32
10373 */
10374 add_cpreg_to_hashtable(cpu, r, opaque, state,
10375 ARM_CP_SECSTATE_NS,
10376 crm, opc1, opc2, r->name);
10377 }
10378 }
10379 }
10380 }
10381 }
10382 }
10383
10384 /* Define a whole list of registers */
define_arm_cp_regs_with_opaque_len(ARMCPU * cpu,const ARMCPRegInfo * regs,void * opaque,size_t len)10385 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
10386 void *opaque, size_t len)
10387 {
10388 size_t i;
10389 for (i = 0; i < len; ++i) {
10390 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
10391 }
10392 }
10393
10394 /*
10395 * Modify ARMCPRegInfo for access from userspace.
10396 *
10397 * This is a data driven modification directed by
10398 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
10399 * user-space cannot alter any values and dynamic values pertaining to
10400 * execution state are hidden from user space view anyway.
10401 */
modify_arm_cp_regs_with_len(ARMCPRegInfo * regs,size_t regs_len,const ARMCPRegUserSpaceInfo * mods,size_t mods_len)10402 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
10403 const ARMCPRegUserSpaceInfo *mods,
10404 size_t mods_len)
10405 {
10406 for (size_t mi = 0; mi < mods_len; ++mi) {
10407 const ARMCPRegUserSpaceInfo *m = mods + mi;
10408 GPatternSpec *pat = NULL;
10409
10410 if (m->is_glob) {
10411 pat = g_pattern_spec_new(m->name);
10412 }
10413 for (size_t ri = 0; ri < regs_len; ++ri) {
10414 ARMCPRegInfo *r = regs + ri;
10415
10416 if (pat && g_pattern_match_string(pat, r->name)) {
10417 r->type = ARM_CP_CONST;
10418 r->access = PL0U_R;
10419 r->resetvalue = 0;
10420 /* continue */
10421 } else if (strcmp(r->name, m->name) == 0) {
10422 r->type = ARM_CP_CONST;
10423 r->access = PL0U_R;
10424 r->resetvalue &= m->exported_bits;
10425 r->resetvalue |= m->fixed_bits;
10426 break;
10427 }
10428 }
10429 if (pat) {
10430 g_pattern_spec_free(pat);
10431 }
10432 }
10433 }
10434
get_arm_cp_reginfo(GHashTable * cpregs,uint32_t encoded_cp)10435 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
10436 {
10437 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
10438 }
10439
arm_cp_write_ignore(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)10440 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
10441 uint64_t value)
10442 {
10443 /* Helper coprocessor write function for write-ignore registers */
10444 }
10445
arm_cp_read_zero(CPUARMState * env,const ARMCPRegInfo * ri)10446 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
10447 {
10448 /* Helper coprocessor write function for read-as-zero registers */
10449 return 0;
10450 }
10451
arm_cp_reset_ignore(CPUARMState * env,const ARMCPRegInfo * opaque)10452 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
10453 {
10454 /* Helper coprocessor reset function for do-nothing-on-reset registers */
10455 }
10456
bad_mode_switch(CPUARMState * env,int mode,CPSRWriteType write_type)10457 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
10458 {
10459 /*
10460 * Return true if it is not valid for us to switch to
10461 * this CPU mode (ie all the UNPREDICTABLE cases in
10462 * the ARM ARM CPSRWriteByInstr pseudocode).
10463 */
10464
10465 /* Changes to or from Hyp via MSR and CPS are illegal. */
10466 if (write_type == CPSRWriteByInstr &&
10467 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
10468 mode == ARM_CPU_MODE_HYP)) {
10469 return 1;
10470 }
10471
10472 switch (mode) {
10473 case ARM_CPU_MODE_USR:
10474 return 0;
10475 case ARM_CPU_MODE_SYS:
10476 case ARM_CPU_MODE_SVC:
10477 case ARM_CPU_MODE_ABT:
10478 case ARM_CPU_MODE_UND:
10479 case ARM_CPU_MODE_IRQ:
10480 case ARM_CPU_MODE_FIQ:
10481 /*
10482 * Note that we don't implement the IMPDEF NSACR.RFR which in v7
10483 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
10484 */
10485 /*
10486 * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
10487 * and CPS are treated as illegal mode changes.
10488 */
10489 if (write_type == CPSRWriteByInstr &&
10490 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
10491 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10492 return 1;
10493 }
10494 return 0;
10495 case ARM_CPU_MODE_HYP:
10496 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
10497 case ARM_CPU_MODE_MON:
10498 return arm_current_el(env) < 3;
10499 default:
10500 return 1;
10501 }
10502 }
10503
cpsr_read(CPUARMState * env)10504 uint32_t cpsr_read(CPUARMState *env)
10505 {
10506 int ZF;
10507 ZF = (env->ZF == 0);
10508 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
10509 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
10510 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
10511 | ((env->condexec_bits & 0xfc) << 8)
10512 | (env->GE << 16) | (env->daif & CPSR_AIF);
10513 }
10514
cpsr_write(CPUARMState * env,uint32_t val,uint32_t mask,CPSRWriteType write_type)10515 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
10516 CPSRWriteType write_type)
10517 {
10518 uint32_t changed_daif;
10519 bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
10520 (mask & (CPSR_M | CPSR_E | CPSR_IL));
10521
10522 if (mask & CPSR_NZCV) {
10523 env->ZF = (~val) & CPSR_Z;
10524 env->NF = val;
10525 env->CF = (val >> 29) & 1;
10526 env->VF = (val << 3) & 0x80000000;
10527 }
10528 if (mask & CPSR_Q) {
10529 env->QF = ((val & CPSR_Q) != 0);
10530 }
10531 if (mask & CPSR_T) {
10532 env->thumb = ((val & CPSR_T) != 0);
10533 }
10534 if (mask & CPSR_IT_0_1) {
10535 env->condexec_bits &= ~3;
10536 env->condexec_bits |= (val >> 25) & 3;
10537 }
10538 if (mask & CPSR_IT_2_7) {
10539 env->condexec_bits &= 3;
10540 env->condexec_bits |= (val >> 8) & 0xfc;
10541 }
10542 if (mask & CPSR_GE) {
10543 env->GE = (val >> 16) & 0xf;
10544 }
10545
10546 /*
10547 * In a V7 implementation that includes the security extensions but does
10548 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
10549 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
10550 * bits respectively.
10551 *
10552 * In a V8 implementation, it is permitted for privileged software to
10553 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
10554 */
10555 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
10556 arm_feature(env, ARM_FEATURE_EL3) &&
10557 !arm_feature(env, ARM_FEATURE_EL2) &&
10558 !arm_is_secure(env)) {
10559
10560 changed_daif = (env->daif ^ val) & mask;
10561
10562 if (changed_daif & CPSR_A) {
10563 /*
10564 * Check to see if we are allowed to change the masking of async
10565 * abort exceptions from a non-secure state.
10566 */
10567 if (!(env->cp15.scr_el3 & SCR_AW)) {
10568 qemu_log_mask(LOG_GUEST_ERROR,
10569 "Ignoring attempt to switch CPSR_A flag from "
10570 "non-secure world with SCR.AW bit clear\n");
10571 mask &= ~CPSR_A;
10572 }
10573 }
10574
10575 if (changed_daif & CPSR_F) {
10576 /*
10577 * Check to see if we are allowed to change the masking of FIQ
10578 * exceptions from a non-secure state.
10579 */
10580 if (!(env->cp15.scr_el3 & SCR_FW)) {
10581 qemu_log_mask(LOG_GUEST_ERROR,
10582 "Ignoring attempt to switch CPSR_F flag from "
10583 "non-secure world with SCR.FW bit clear\n");
10584 mask &= ~CPSR_F;
10585 }
10586
10587 /*
10588 * Check whether non-maskable FIQ (NMFI) support is enabled.
10589 * If this bit is set software is not allowed to mask
10590 * FIQs, but is allowed to set CPSR_F to 0.
10591 */
10592 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
10593 (val & CPSR_F)) {
10594 qemu_log_mask(LOG_GUEST_ERROR,
10595 "Ignoring attempt to enable CPSR_F flag "
10596 "(non-maskable FIQ [NMFI] support enabled)\n");
10597 mask &= ~CPSR_F;
10598 }
10599 }
10600 }
10601
10602 env->daif &= ~(CPSR_AIF & mask);
10603 env->daif |= val & CPSR_AIF & mask;
10604
10605 if (write_type != CPSRWriteRaw &&
10606 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
10607 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
10608 /*
10609 * Note that we can only get here in USR mode if this is a
10610 * gdb stub write; for this case we follow the architectural
10611 * behaviour for guest writes in USR mode of ignoring an attempt
10612 * to switch mode. (Those are caught by translate.c for writes
10613 * triggered by guest instructions.)
10614 */
10615 mask &= ~CPSR_M;
10616 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
10617 /*
10618 * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
10619 * v7, and has defined behaviour in v8:
10620 * + leave CPSR.M untouched
10621 * + allow changes to the other CPSR fields
10622 * + set PSTATE.IL
10623 * For user changes via the GDB stub, we don't set PSTATE.IL,
10624 * as this would be unnecessarily harsh for a user error.
10625 */
10626 mask &= ~CPSR_M;
10627 if (write_type != CPSRWriteByGDBStub &&
10628 arm_feature(env, ARM_FEATURE_V8)) {
10629 mask |= CPSR_IL;
10630 val |= CPSR_IL;
10631 }
10632 qemu_log_mask(LOG_GUEST_ERROR,
10633 "Illegal AArch32 mode switch attempt from %s to %s\n",
10634 aarch32_mode_name(env->uncached_cpsr),
10635 aarch32_mode_name(val));
10636 } else {
10637 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
10638 write_type == CPSRWriteExceptionReturn ?
10639 "Exception return from AArch32" :
10640 "AArch32 mode switch from",
10641 aarch32_mode_name(env->uncached_cpsr),
10642 aarch32_mode_name(val), env->regs[15]);
10643 switch_mode(env, val & CPSR_M);
10644 }
10645 }
10646 mask &= ~CACHED_CPSR_BITS;
10647 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
10648 if (tcg_enabled() && rebuild_hflags) {
10649 arm_rebuild_hflags(env);
10650 }
10651 }
10652
10653 #ifdef CONFIG_USER_ONLY
10654
switch_mode(CPUARMState * env,int mode)10655 static void switch_mode(CPUARMState *env, int mode)
10656 {
10657 ARMCPU *cpu = env_archcpu(env);
10658
10659 if (mode != ARM_CPU_MODE_USR) {
10660 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
10661 }
10662 }
10663
arm_phys_excp_target_el(CPUState * cs,uint32_t excp_idx,uint32_t cur_el,bool secure)10664 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10665 uint32_t cur_el, bool secure)
10666 {
10667 return 1;
10668 }
10669
aarch64_sync_64_to_32(CPUARMState * env)10670 void aarch64_sync_64_to_32(CPUARMState *env)
10671 {
10672 g_assert_not_reached();
10673 }
10674
10675 #else
10676
switch_mode(CPUARMState * env,int mode)10677 static void switch_mode(CPUARMState *env, int mode)
10678 {
10679 int old_mode;
10680 int i;
10681
10682 old_mode = env->uncached_cpsr & CPSR_M;
10683 if (mode == old_mode) {
10684 return;
10685 }
10686
10687 if (old_mode == ARM_CPU_MODE_FIQ) {
10688 memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
10689 memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
10690 } else if (mode == ARM_CPU_MODE_FIQ) {
10691 memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
10692 memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
10693 }
10694
10695 i = bank_number(old_mode);
10696 env->banked_r13[i] = env->regs[13];
10697 env->banked_spsr[i] = env->spsr;
10698
10699 i = bank_number(mode);
10700 env->regs[13] = env->banked_r13[i];
10701 env->spsr = env->banked_spsr[i];
10702
10703 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
10704 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
10705 }
10706
10707 /*
10708 * Physical Interrupt Target EL Lookup Table
10709 *
10710 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
10711 *
10712 * The below multi-dimensional table is used for looking up the target
10713 * exception level given numerous condition criteria. Specifically, the
10714 * target EL is based on SCR and HCR routing controls as well as the
10715 * currently executing EL and secure state.
10716 *
10717 * Dimensions:
10718 * target_el_table[2][2][2][2][2][4]
10719 * | | | | | +--- Current EL
10720 * | | | | +------ Non-secure(0)/Secure(1)
10721 * | | | +--------- HCR mask override
10722 * | | +------------ SCR exec state control
10723 * | +--------------- SCR mask override
10724 * +------------------ 32-bit(0)/64-bit(1) EL3
10725 *
10726 * The table values are as such:
10727 * 0-3 = EL0-EL3
10728 * -1 = Cannot occur
10729 *
10730 * The ARM ARM target EL table includes entries indicating that an "exception
10731 * is not taken". The two cases where this is applicable are:
10732 * 1) An exception is taken from EL3 but the SCR does not have the exception
10733 * routed to EL3.
10734 * 2) An exception is taken from EL2 but the HCR does not have the exception
10735 * routed to EL2.
10736 * In these two cases, the below table contain a target of EL1. This value is
10737 * returned as it is expected that the consumer of the table data will check
10738 * for "target EL >= current EL" to ensure the exception is not taken.
10739 *
10740 * SCR HCR
10741 * 64 EA AMO From
10742 * BIT IRQ IMO Non-secure Secure
10743 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
10744 */
10745 static const int8_t target_el_table[2][2][2][2][2][4] = {
10746 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
10747 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
10748 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
10749 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
10750 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
10751 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
10752 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
10753 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
10754 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
10755 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
10756 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
10757 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
10758 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
10759 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
10760 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
10761 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
10762 };
10763
10764 /*
10765 * Determine the target EL for physical exceptions
10766 */
arm_phys_excp_target_el(CPUState * cs,uint32_t excp_idx,uint32_t cur_el,bool secure)10767 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10768 uint32_t cur_el, bool secure)
10769 {
10770 CPUARMState *env = cpu_env(cs);
10771 bool rw;
10772 bool scr;
10773 bool hcr;
10774 int target_el;
10775 /* Is the highest EL AArch64? */
10776 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
10777 uint64_t hcr_el2;
10778
10779 if (arm_feature(env, ARM_FEATURE_EL3)) {
10780 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
10781 } else {
10782 /*
10783 * Either EL2 is the highest EL (and so the EL2 register width
10784 * is given by is64); or there is no EL2 or EL3, in which case
10785 * the value of 'rw' does not affect the table lookup anyway.
10786 */
10787 rw = is64;
10788 }
10789
10790 hcr_el2 = arm_hcr_el2_eff(env);
10791 switch (excp_idx) {
10792 case EXCP_IRQ:
10793 case EXCP_NMI:
10794 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
10795 hcr = hcr_el2 & HCR_IMO;
10796 break;
10797 case EXCP_FIQ:
10798 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
10799 hcr = hcr_el2 & HCR_FMO;
10800 break;
10801 default:
10802 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
10803 hcr = hcr_el2 & HCR_AMO;
10804 break;
10805 };
10806
10807 /*
10808 * For these purposes, TGE and AMO/IMO/FMO both force the
10809 * interrupt to EL2. Fold TGE into the bit extracted above.
10810 */
10811 hcr |= (hcr_el2 & HCR_TGE) != 0;
10812
10813 /* Perform a table-lookup for the target EL given the current state */
10814 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
10815
10816 assert(target_el > 0);
10817
10818 return target_el;
10819 }
10820
arm_log_exception(CPUState * cs)10821 void arm_log_exception(CPUState *cs)
10822 {
10823 int idx = cs->exception_index;
10824
10825 if (qemu_loglevel_mask(CPU_LOG_INT)) {
10826 const char *exc = NULL;
10827 static const char * const excnames[] = {
10828 [EXCP_UDEF] = "Undefined Instruction",
10829 [EXCP_SWI] = "SVC",
10830 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
10831 [EXCP_DATA_ABORT] = "Data Abort",
10832 [EXCP_IRQ] = "IRQ",
10833 [EXCP_FIQ] = "FIQ",
10834 [EXCP_BKPT] = "Breakpoint",
10835 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
10836 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
10837 [EXCP_HVC] = "Hypervisor Call",
10838 [EXCP_HYP_TRAP] = "Hypervisor Trap",
10839 [EXCP_SMC] = "Secure Monitor Call",
10840 [EXCP_VIRQ] = "Virtual IRQ",
10841 [EXCP_VFIQ] = "Virtual FIQ",
10842 [EXCP_SEMIHOST] = "Semihosting call",
10843 [EXCP_NOCP] = "v7M NOCP UsageFault",
10844 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
10845 [EXCP_STKOF] = "v8M STKOF UsageFault",
10846 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
10847 [EXCP_LSERR] = "v8M LSERR UsageFault",
10848 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
10849 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
10850 [EXCP_VSERR] = "Virtual SERR",
10851 [EXCP_GPC] = "Granule Protection Check",
10852 [EXCP_NMI] = "NMI",
10853 [EXCP_VINMI] = "Virtual IRQ NMI",
10854 [EXCP_VFNMI] = "Virtual FIQ NMI",
10855 };
10856
10857 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
10858 exc = excnames[idx];
10859 }
10860 if (!exc) {
10861 exc = "unknown";
10862 }
10863 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
10864 idx, exc, cs->cpu_index);
10865 }
10866 }
10867
10868 /*
10869 * Function used to synchronize QEMU's AArch64 register set with AArch32
10870 * register set. This is necessary when switching between AArch32 and AArch64
10871 * execution state.
10872 */
aarch64_sync_32_to_64(CPUARMState * env)10873 void aarch64_sync_32_to_64(CPUARMState *env)
10874 {
10875 int i;
10876 uint32_t mode = env->uncached_cpsr & CPSR_M;
10877
10878 /* We can blanket copy R[0:7] to X[0:7] */
10879 for (i = 0; i < 8; i++) {
10880 env->xregs[i] = env->regs[i];
10881 }
10882
10883 /*
10884 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
10885 * Otherwise, they come from the banked user regs.
10886 */
10887 if (mode == ARM_CPU_MODE_FIQ) {
10888 for (i = 8; i < 13; i++) {
10889 env->xregs[i] = env->usr_regs[i - 8];
10890 }
10891 } else {
10892 for (i = 8; i < 13; i++) {
10893 env->xregs[i] = env->regs[i];
10894 }
10895 }
10896
10897 /*
10898 * Registers x13-x23 are the various mode SP and FP registers. Registers
10899 * r13 and r14 are only copied if we are in that mode, otherwise we copy
10900 * from the mode banked register.
10901 */
10902 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10903 env->xregs[13] = env->regs[13];
10904 env->xregs[14] = env->regs[14];
10905 } else {
10906 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
10907 /* HYP is an exception in that it is copied from r14 */
10908 if (mode == ARM_CPU_MODE_HYP) {
10909 env->xregs[14] = env->regs[14];
10910 } else {
10911 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
10912 }
10913 }
10914
10915 if (mode == ARM_CPU_MODE_HYP) {
10916 env->xregs[15] = env->regs[13];
10917 } else {
10918 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
10919 }
10920
10921 if (mode == ARM_CPU_MODE_IRQ) {
10922 env->xregs[16] = env->regs[14];
10923 env->xregs[17] = env->regs[13];
10924 } else {
10925 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
10926 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
10927 }
10928
10929 if (mode == ARM_CPU_MODE_SVC) {
10930 env->xregs[18] = env->regs[14];
10931 env->xregs[19] = env->regs[13];
10932 } else {
10933 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
10934 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
10935 }
10936
10937 if (mode == ARM_CPU_MODE_ABT) {
10938 env->xregs[20] = env->regs[14];
10939 env->xregs[21] = env->regs[13];
10940 } else {
10941 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10942 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10943 }
10944
10945 if (mode == ARM_CPU_MODE_UND) {
10946 env->xregs[22] = env->regs[14];
10947 env->xregs[23] = env->regs[13];
10948 } else {
10949 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10950 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
10951 }
10952
10953 /*
10954 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
10955 * mode, then we can copy from r8-r14. Otherwise, we copy from the
10956 * FIQ bank for r8-r14.
10957 */
10958 if (mode == ARM_CPU_MODE_FIQ) {
10959 for (i = 24; i < 31; i++) {
10960 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
10961 }
10962 } else {
10963 for (i = 24; i < 29; i++) {
10964 env->xregs[i] = env->fiq_regs[i - 24];
10965 }
10966 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10967 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
10968 }
10969
10970 env->pc = env->regs[15];
10971 }
10972
10973 /*
10974 * Function used to synchronize QEMU's AArch32 register set with AArch64
10975 * register set. This is necessary when switching between AArch32 and AArch64
10976 * execution state.
10977 */
aarch64_sync_64_to_32(CPUARMState * env)10978 void aarch64_sync_64_to_32(CPUARMState *env)
10979 {
10980 int i;
10981 uint32_t mode = env->uncached_cpsr & CPSR_M;
10982
10983 /* We can blanket copy X[0:7] to R[0:7] */
10984 for (i = 0; i < 8; i++) {
10985 env->regs[i] = env->xregs[i];
10986 }
10987
10988 /*
10989 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10990 * Otherwise, we copy x8-x12 into the banked user regs.
10991 */
10992 if (mode == ARM_CPU_MODE_FIQ) {
10993 for (i = 8; i < 13; i++) {
10994 env->usr_regs[i - 8] = env->xregs[i];
10995 }
10996 } else {
10997 for (i = 8; i < 13; i++) {
10998 env->regs[i] = env->xregs[i];
10999 }
11000 }
11001
11002 /*
11003 * Registers r13 & r14 depend on the current mode.
11004 * If we are in a given mode, we copy the corresponding x registers to r13
11005 * and r14. Otherwise, we copy the x register to the banked r13 and r14
11006 * for the mode.
11007 */
11008 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
11009 env->regs[13] = env->xregs[13];
11010 env->regs[14] = env->xregs[14];
11011 } else {
11012 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
11013
11014 /*
11015 * HYP is an exception in that it does not have its own banked r14 but
11016 * shares the USR r14
11017 */
11018 if (mode == ARM_CPU_MODE_HYP) {
11019 env->regs[14] = env->xregs[14];
11020 } else {
11021 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
11022 }
11023 }
11024
11025 if (mode == ARM_CPU_MODE_HYP) {
11026 env->regs[13] = env->xregs[15];
11027 } else {
11028 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
11029 }
11030
11031 if (mode == ARM_CPU_MODE_IRQ) {
11032 env->regs[14] = env->xregs[16];
11033 env->regs[13] = env->xregs[17];
11034 } else {
11035 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
11036 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
11037 }
11038
11039 if (mode == ARM_CPU_MODE_SVC) {
11040 env->regs[14] = env->xregs[18];
11041 env->regs[13] = env->xregs[19];
11042 } else {
11043 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
11044 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
11045 }
11046
11047 if (mode == ARM_CPU_MODE_ABT) {
11048 env->regs[14] = env->xregs[20];
11049 env->regs[13] = env->xregs[21];
11050 } else {
11051 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
11052 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
11053 }
11054
11055 if (mode == ARM_CPU_MODE_UND) {
11056 env->regs[14] = env->xregs[22];
11057 env->regs[13] = env->xregs[23];
11058 } else {
11059 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
11060 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
11061 }
11062
11063 /*
11064 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
11065 * mode, then we can copy to r8-r14. Otherwise, we copy to the
11066 * FIQ bank for r8-r14.
11067 */
11068 if (mode == ARM_CPU_MODE_FIQ) {
11069 for (i = 24; i < 31; i++) {
11070 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
11071 }
11072 } else {
11073 for (i = 24; i < 29; i++) {
11074 env->fiq_regs[i - 24] = env->xregs[i];
11075 }
11076 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
11077 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
11078 }
11079
11080 env->regs[15] = env->pc;
11081 }
11082
take_aarch32_exception(CPUARMState * env,int new_mode,uint32_t mask,uint32_t offset,uint32_t newpc)11083 static void take_aarch32_exception(CPUARMState *env, int new_mode,
11084 uint32_t mask, uint32_t offset,
11085 uint32_t newpc)
11086 {
11087 int new_el;
11088
11089 /* Change the CPU state so as to actually take the exception. */
11090 switch_mode(env, new_mode);
11091
11092 /*
11093 * For exceptions taken to AArch32 we must clear the SS bit in both
11094 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
11095 */
11096 env->pstate &= ~PSTATE_SS;
11097 env->spsr = cpsr_read(env);
11098 /* Clear IT bits. */
11099 env->condexec_bits = 0;
11100 /* Switch to the new mode, and to the correct instruction set. */
11101 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
11102
11103 /* This must be after mode switching. */
11104 new_el = arm_current_el(env);
11105
11106 /* Set new mode endianness */
11107 env->uncached_cpsr &= ~CPSR_E;
11108 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
11109 env->uncached_cpsr |= CPSR_E;
11110 }
11111 /* J and IL must always be cleared for exception entry */
11112 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
11113 env->daif |= mask;
11114
11115 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
11116 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
11117 env->uncached_cpsr |= CPSR_SSBS;
11118 } else {
11119 env->uncached_cpsr &= ~CPSR_SSBS;
11120 }
11121 }
11122
11123 if (new_mode == ARM_CPU_MODE_HYP) {
11124 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
11125 env->elr_el[2] = env->regs[15];
11126 } else {
11127 /* CPSR.PAN is normally preserved preserved unless... */
11128 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
11129 switch (new_el) {
11130 case 3:
11131 if (!arm_is_secure_below_el3(env)) {
11132 /* ... the target is EL3, from non-secure state. */
11133 env->uncached_cpsr &= ~CPSR_PAN;
11134 break;
11135 }
11136 /* ... the target is EL3, from secure state ... */
11137 /* fall through */
11138 case 1:
11139 /* ... the target is EL1 and SCTLR.SPAN is 0. */
11140 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
11141 env->uncached_cpsr |= CPSR_PAN;
11142 }
11143 break;
11144 }
11145 }
11146 /*
11147 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
11148 * and we should just guard the thumb mode on V4
11149 */
11150 if (arm_feature(env, ARM_FEATURE_V4T)) {
11151 env->thumb =
11152 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
11153 }
11154 env->regs[14] = env->regs[15] + offset;
11155 }
11156 env->regs[15] = newpc;
11157
11158 if (tcg_enabled()) {
11159 arm_rebuild_hflags(env);
11160 }
11161 }
11162
arm_cpu_do_interrupt_aarch32_hyp(CPUState * cs)11163 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
11164 {
11165 /*
11166 * Handle exception entry to Hyp mode; this is sufficiently
11167 * different to entry to other AArch32 modes that we handle it
11168 * separately here.
11169 *
11170 * The vector table entry used is always the 0x14 Hyp mode entry point,
11171 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
11172 * The offset applied to the preferred return address is always zero
11173 * (see DDI0487C.a section G1.12.3).
11174 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
11175 */
11176 uint32_t addr, mask;
11177 ARMCPU *cpu = ARM_CPU(cs);
11178 CPUARMState *env = &cpu->env;
11179
11180 switch (cs->exception_index) {
11181 case EXCP_UDEF:
11182 addr = 0x04;
11183 break;
11184 case EXCP_SWI:
11185 addr = 0x08;
11186 break;
11187 case EXCP_BKPT:
11188 /* Fall through to prefetch abort. */
11189 case EXCP_PREFETCH_ABORT:
11190 env->cp15.ifar_s = env->exception.vaddress;
11191 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
11192 (uint32_t)env->exception.vaddress);
11193 addr = 0x0c;
11194 break;
11195 case EXCP_DATA_ABORT:
11196 env->cp15.dfar_s = env->exception.vaddress;
11197 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
11198 (uint32_t)env->exception.vaddress);
11199 addr = 0x10;
11200 break;
11201 case EXCP_IRQ:
11202 addr = 0x18;
11203 break;
11204 case EXCP_FIQ:
11205 addr = 0x1c;
11206 break;
11207 case EXCP_HVC:
11208 addr = 0x08;
11209 break;
11210 case EXCP_HYP_TRAP:
11211 addr = 0x14;
11212 break;
11213 default:
11214 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11215 }
11216
11217 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
11218 if (!arm_feature(env, ARM_FEATURE_V8)) {
11219 /*
11220 * QEMU syndrome values are v8-style. v7 has the IL bit
11221 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
11222 * If this is a v7 CPU, squash the IL bit in those cases.
11223 */
11224 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
11225 (cs->exception_index == EXCP_DATA_ABORT &&
11226 !(env->exception.syndrome & ARM_EL_ISV)) ||
11227 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
11228 env->exception.syndrome &= ~ARM_EL_IL;
11229 }
11230 }
11231 env->cp15.esr_el[2] = env->exception.syndrome;
11232 }
11233
11234 if (arm_current_el(env) != 2 && addr < 0x14) {
11235 addr = 0x14;
11236 }
11237
11238 mask = 0;
11239 if (!(env->cp15.scr_el3 & SCR_EA)) {
11240 mask |= CPSR_A;
11241 }
11242 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
11243 mask |= CPSR_I;
11244 }
11245 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
11246 mask |= CPSR_F;
11247 }
11248
11249 addr += env->cp15.hvbar;
11250
11251 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
11252 }
11253
arm_cpu_do_interrupt_aarch32(CPUState * cs)11254 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
11255 {
11256 ARMCPU *cpu = ARM_CPU(cs);
11257 CPUARMState *env = &cpu->env;
11258 uint32_t addr;
11259 uint32_t mask;
11260 int new_mode;
11261 uint32_t offset;
11262 uint32_t moe;
11263
11264 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
11265 switch (syn_get_ec(env->exception.syndrome)) {
11266 case EC_BREAKPOINT:
11267 case EC_BREAKPOINT_SAME_EL:
11268 moe = 1;
11269 break;
11270 case EC_WATCHPOINT:
11271 case EC_WATCHPOINT_SAME_EL:
11272 moe = 10;
11273 break;
11274 case EC_AA32_BKPT:
11275 moe = 3;
11276 break;
11277 case EC_VECTORCATCH:
11278 moe = 5;
11279 break;
11280 default:
11281 moe = 0;
11282 break;
11283 }
11284
11285 if (moe) {
11286 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
11287 }
11288
11289 if (env->exception.target_el == 2) {
11290 /* Debug exceptions are reported differently on AArch32 */
11291 switch (syn_get_ec(env->exception.syndrome)) {
11292 case EC_BREAKPOINT:
11293 case EC_BREAKPOINT_SAME_EL:
11294 case EC_AA32_BKPT:
11295 case EC_VECTORCATCH:
11296 env->exception.syndrome = syn_insn_abort(arm_current_el(env) == 2,
11297 0, 0, 0x22);
11298 break;
11299 case EC_WATCHPOINT:
11300 env->exception.syndrome = syn_set_ec(env->exception.syndrome,
11301 EC_DATAABORT);
11302 break;
11303 case EC_WATCHPOINT_SAME_EL:
11304 env->exception.syndrome = syn_set_ec(env->exception.syndrome,
11305 EC_DATAABORT_SAME_EL);
11306 break;
11307 }
11308 arm_cpu_do_interrupt_aarch32_hyp(cs);
11309 return;
11310 }
11311
11312 switch (cs->exception_index) {
11313 case EXCP_UDEF:
11314 new_mode = ARM_CPU_MODE_UND;
11315 addr = 0x04;
11316 mask = CPSR_I;
11317 if (env->thumb) {
11318 offset = 2;
11319 } else {
11320 offset = 4;
11321 }
11322 break;
11323 case EXCP_SWI:
11324 new_mode = ARM_CPU_MODE_SVC;
11325 addr = 0x08;
11326 mask = CPSR_I;
11327 /* The PC already points to the next instruction. */
11328 offset = 0;
11329 break;
11330 case EXCP_BKPT:
11331 /* Fall through to prefetch abort. */
11332 case EXCP_PREFETCH_ABORT:
11333 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
11334 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
11335 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
11336 env->exception.fsr, (uint32_t)env->exception.vaddress);
11337 new_mode = ARM_CPU_MODE_ABT;
11338 addr = 0x0c;
11339 mask = CPSR_A | CPSR_I;
11340 offset = 4;
11341 break;
11342 case EXCP_DATA_ABORT:
11343 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
11344 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
11345 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
11346 env->exception.fsr,
11347 (uint32_t)env->exception.vaddress);
11348 new_mode = ARM_CPU_MODE_ABT;
11349 addr = 0x10;
11350 mask = CPSR_A | CPSR_I;
11351 offset = 8;
11352 break;
11353 case EXCP_IRQ:
11354 new_mode = ARM_CPU_MODE_IRQ;
11355 addr = 0x18;
11356 /* Disable IRQ and imprecise data aborts. */
11357 mask = CPSR_A | CPSR_I;
11358 offset = 4;
11359 if (env->cp15.scr_el3 & SCR_IRQ) {
11360 /* IRQ routed to monitor mode */
11361 new_mode = ARM_CPU_MODE_MON;
11362 mask |= CPSR_F;
11363 }
11364 break;
11365 case EXCP_FIQ:
11366 new_mode = ARM_CPU_MODE_FIQ;
11367 addr = 0x1c;
11368 /* Disable FIQ, IRQ and imprecise data aborts. */
11369 mask = CPSR_A | CPSR_I | CPSR_F;
11370 if (env->cp15.scr_el3 & SCR_FIQ) {
11371 /* FIQ routed to monitor mode */
11372 new_mode = ARM_CPU_MODE_MON;
11373 }
11374 offset = 4;
11375 break;
11376 case EXCP_VIRQ:
11377 new_mode = ARM_CPU_MODE_IRQ;
11378 addr = 0x18;
11379 /* Disable IRQ and imprecise data aborts. */
11380 mask = CPSR_A | CPSR_I;
11381 offset = 4;
11382 break;
11383 case EXCP_VFIQ:
11384 new_mode = ARM_CPU_MODE_FIQ;
11385 addr = 0x1c;
11386 /* Disable FIQ, IRQ and imprecise data aborts. */
11387 mask = CPSR_A | CPSR_I | CPSR_F;
11388 offset = 4;
11389 break;
11390 case EXCP_VSERR:
11391 {
11392 /*
11393 * Note that this is reported as a data abort, but the DFAR
11394 * has an UNKNOWN value. Construct the SError syndrome from
11395 * AET and ExT fields.
11396 */
11397 ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
11398
11399 if (extended_addresses_enabled(env)) {
11400 env->exception.fsr = arm_fi_to_lfsc(&fi);
11401 } else {
11402 env->exception.fsr = arm_fi_to_sfsc(&fi);
11403 }
11404 env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
11405 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
11406 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
11407 env->exception.fsr);
11408
11409 new_mode = ARM_CPU_MODE_ABT;
11410 addr = 0x10;
11411 mask = CPSR_A | CPSR_I;
11412 offset = 8;
11413 }
11414 break;
11415 case EXCP_SMC:
11416 new_mode = ARM_CPU_MODE_MON;
11417 addr = 0x08;
11418 mask = CPSR_A | CPSR_I | CPSR_F;
11419 offset = 0;
11420 break;
11421 default:
11422 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11423 return; /* Never happens. Keep compiler happy. */
11424 }
11425
11426 if (new_mode == ARM_CPU_MODE_MON) {
11427 addr += env->cp15.mvbar;
11428 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
11429 /* High vectors. When enabled, base address cannot be remapped. */
11430 addr += 0xffff0000;
11431 } else {
11432 /*
11433 * ARM v7 architectures provide a vector base address register to remap
11434 * the interrupt vector table.
11435 * This register is only followed in non-monitor mode, and is banked.
11436 * Note: only bits 31:5 are valid.
11437 */
11438 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
11439 }
11440
11441 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
11442 env->cp15.scr_el3 &= ~SCR_NS;
11443 }
11444
11445 take_aarch32_exception(env, new_mode, mask, offset, addr);
11446 }
11447
aarch64_regnum(CPUARMState * env,int aarch32_reg)11448 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
11449 {
11450 /*
11451 * Return the register number of the AArch64 view of the AArch32
11452 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
11453 * be that of the AArch32 mode the exception came from.
11454 */
11455 int mode = env->uncached_cpsr & CPSR_M;
11456
11457 switch (aarch32_reg) {
11458 case 0 ... 7:
11459 return aarch32_reg;
11460 case 8 ... 12:
11461 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
11462 case 13:
11463 switch (mode) {
11464 case ARM_CPU_MODE_USR:
11465 case ARM_CPU_MODE_SYS:
11466 return 13;
11467 case ARM_CPU_MODE_HYP:
11468 return 15;
11469 case ARM_CPU_MODE_IRQ:
11470 return 17;
11471 case ARM_CPU_MODE_SVC:
11472 return 19;
11473 case ARM_CPU_MODE_ABT:
11474 return 21;
11475 case ARM_CPU_MODE_UND:
11476 return 23;
11477 case ARM_CPU_MODE_FIQ:
11478 return 29;
11479 default:
11480 g_assert_not_reached();
11481 }
11482 case 14:
11483 switch (mode) {
11484 case ARM_CPU_MODE_USR:
11485 case ARM_CPU_MODE_SYS:
11486 case ARM_CPU_MODE_HYP:
11487 return 14;
11488 case ARM_CPU_MODE_IRQ:
11489 return 16;
11490 case ARM_CPU_MODE_SVC:
11491 return 18;
11492 case ARM_CPU_MODE_ABT:
11493 return 20;
11494 case ARM_CPU_MODE_UND:
11495 return 22;
11496 case ARM_CPU_MODE_FIQ:
11497 return 30;
11498 default:
11499 g_assert_not_reached();
11500 }
11501 case 15:
11502 return 31;
11503 default:
11504 g_assert_not_reached();
11505 }
11506 }
11507
cpsr_read_for_spsr_elx(CPUARMState * env)11508 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
11509 {
11510 uint32_t ret = cpsr_read(env);
11511
11512 /* Move DIT to the correct location for SPSR_ELx */
11513 if (ret & CPSR_DIT) {
11514 ret &= ~CPSR_DIT;
11515 ret |= PSTATE_DIT;
11516 }
11517 /* Merge PSTATE.SS into SPSR_ELx */
11518 ret |= env->pstate & PSTATE_SS;
11519
11520 return ret;
11521 }
11522
syndrome_is_sync_extabt(uint32_t syndrome)11523 static bool syndrome_is_sync_extabt(uint32_t syndrome)
11524 {
11525 /* Return true if this syndrome value is a synchronous external abort */
11526 switch (syn_get_ec(syndrome)) {
11527 case EC_INSNABORT:
11528 case EC_INSNABORT_SAME_EL:
11529 case EC_DATAABORT:
11530 case EC_DATAABORT_SAME_EL:
11531 /* Look at fault status code for all the synchronous ext abort cases */
11532 switch (syndrome & 0x3f) {
11533 case 0x10:
11534 case 0x13:
11535 case 0x14:
11536 case 0x15:
11537 case 0x16:
11538 case 0x17:
11539 return true;
11540 default:
11541 return false;
11542 }
11543 default:
11544 return false;
11545 }
11546 }
11547
11548 /* Handle exception entry to a target EL which is using AArch64 */
arm_cpu_do_interrupt_aarch64(CPUState * cs)11549 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
11550 {
11551 ARMCPU *cpu = ARM_CPU(cs);
11552 CPUARMState *env = &cpu->env;
11553 unsigned int new_el = env->exception.target_el;
11554 target_ulong addr = env->cp15.vbar_el[new_el];
11555 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
11556 unsigned int old_mode;
11557 unsigned int cur_el = arm_current_el(env);
11558 int rt;
11559
11560 if (tcg_enabled()) {
11561 /*
11562 * Note that new_el can never be 0. If cur_el is 0, then
11563 * el0_a64 is is_a64(), else el0_a64 is ignored.
11564 */
11565 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
11566 }
11567
11568 if (cur_el < new_el) {
11569 /*
11570 * Entry vector offset depends on whether the implemented EL
11571 * immediately lower than the target level is using AArch32 or AArch64
11572 */
11573 bool is_aa64;
11574 uint64_t hcr;
11575
11576 switch (new_el) {
11577 case 3:
11578 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
11579 break;
11580 case 2:
11581 hcr = arm_hcr_el2_eff(env);
11582 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11583 is_aa64 = (hcr & HCR_RW) != 0;
11584 break;
11585 }
11586 /* fall through */
11587 case 1:
11588 is_aa64 = is_a64(env);
11589 break;
11590 default:
11591 g_assert_not_reached();
11592 }
11593
11594 if (is_aa64) {
11595 addr += 0x400;
11596 } else {
11597 addr += 0x600;
11598 }
11599 } else if (pstate_read(env) & PSTATE_SP) {
11600 addr += 0x200;
11601 }
11602
11603 switch (cs->exception_index) {
11604 case EXCP_GPC:
11605 qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
11606 env->cp15.mfar_el3);
11607 /* fall through */
11608 case EXCP_PREFETCH_ABORT:
11609 case EXCP_DATA_ABORT:
11610 /*
11611 * FEAT_DoubleFault allows synchronous external aborts taken to EL3
11612 * to be taken to the SError vector entrypoint.
11613 */
11614 if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
11615 syndrome_is_sync_extabt(env->exception.syndrome)) {
11616 addr += 0x180;
11617 }
11618 env->cp15.far_el[new_el] = env->exception.vaddress;
11619 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
11620 env->cp15.far_el[new_el]);
11621 /* fall through */
11622 case EXCP_BKPT:
11623 case EXCP_UDEF:
11624 case EXCP_SWI:
11625 case EXCP_HVC:
11626 case EXCP_HYP_TRAP:
11627 case EXCP_SMC:
11628 switch (syn_get_ec(env->exception.syndrome)) {
11629 case EC_ADVSIMDFPACCESSTRAP:
11630 /*
11631 * QEMU internal FP/SIMD syndromes from AArch32 include the
11632 * TA and coproc fields which are only exposed if the exception
11633 * is taken to AArch32 Hyp mode. Mask them out to get a valid
11634 * AArch64 format syndrome.
11635 */
11636 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
11637 break;
11638 case EC_CP14RTTRAP:
11639 case EC_CP15RTTRAP:
11640 case EC_CP14DTTRAP:
11641 /*
11642 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
11643 * the raw register field from the insn; when taking this to
11644 * AArch64 we must convert it to the AArch64 view of the register
11645 * number. Notice that we read a 4-bit AArch32 register number and
11646 * write back a 5-bit AArch64 one.
11647 */
11648 rt = extract32(env->exception.syndrome, 5, 4);
11649 rt = aarch64_regnum(env, rt);
11650 env->exception.syndrome = deposit32(env->exception.syndrome,
11651 5, 5, rt);
11652 break;
11653 case EC_CP15RRTTRAP:
11654 case EC_CP14RRTTRAP:
11655 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
11656 rt = extract32(env->exception.syndrome, 5, 4);
11657 rt = aarch64_regnum(env, rt);
11658 env->exception.syndrome = deposit32(env->exception.syndrome,
11659 5, 5, rt);
11660 rt = extract32(env->exception.syndrome, 10, 4);
11661 rt = aarch64_regnum(env, rt);
11662 env->exception.syndrome = deposit32(env->exception.syndrome,
11663 10, 5, rt);
11664 break;
11665 }
11666 env->cp15.esr_el[new_el] = env->exception.syndrome;
11667 break;
11668 case EXCP_IRQ:
11669 case EXCP_VIRQ:
11670 case EXCP_NMI:
11671 case EXCP_VINMI:
11672 addr += 0x80;
11673 break;
11674 case EXCP_FIQ:
11675 case EXCP_VFIQ:
11676 case EXCP_VFNMI:
11677 addr += 0x100;
11678 break;
11679 case EXCP_VSERR:
11680 addr += 0x180;
11681 /* Construct the SError syndrome from IDS and ISS fields. */
11682 env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
11683 env->cp15.esr_el[new_el] = env->exception.syndrome;
11684 break;
11685 default:
11686 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
11687 }
11688
11689 if (is_a64(env)) {
11690 old_mode = pstate_read(env);
11691 aarch64_save_sp(env, arm_current_el(env));
11692 env->elr_el[new_el] = env->pc;
11693
11694 if (cur_el == 1 && new_el == 1) {
11695 uint64_t hcr = arm_hcr_el2_eff(env);
11696 if ((hcr & (HCR_NV | HCR_NV1 | HCR_NV2)) == HCR_NV ||
11697 (hcr & (HCR_NV | HCR_NV2)) == (HCR_NV | HCR_NV2)) {
11698 /*
11699 * FEAT_NV, FEAT_NV2 may need to report EL2 in the SPSR
11700 * by setting M[3:2] to 0b10.
11701 * If NV2 is disabled, change SPSR when NV,NV1 == 1,0 (I_ZJRNN)
11702 * If NV2 is enabled, change SPSR when NV is 1 (I_DBTLM)
11703 */
11704 old_mode = deposit32(old_mode, 2, 2, 2);
11705 }
11706 }
11707 } else {
11708 old_mode = cpsr_read_for_spsr_elx(env);
11709 env->elr_el[new_el] = env->regs[15];
11710
11711 aarch64_sync_32_to_64(env);
11712
11713 env->condexec_bits = 0;
11714 }
11715 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
11716
11717 qemu_log_mask(CPU_LOG_INT, "...with SPSR 0x%x\n", old_mode);
11718 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
11719 env->elr_el[new_el]);
11720
11721 if (cpu_isar_feature(aa64_pan, cpu)) {
11722 /* The value of PSTATE.PAN is normally preserved, except when ... */
11723 new_mode |= old_mode & PSTATE_PAN;
11724 switch (new_el) {
11725 case 2:
11726 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
11727 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
11728 != (HCR_E2H | HCR_TGE)) {
11729 break;
11730 }
11731 /* fall through */
11732 case 1:
11733 /* ... the target is EL1 ... */
11734 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
11735 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
11736 new_mode |= PSTATE_PAN;
11737 }
11738 break;
11739 }
11740 }
11741 if (cpu_isar_feature(aa64_mte, cpu)) {
11742 new_mode |= PSTATE_TCO;
11743 }
11744
11745 if (cpu_isar_feature(aa64_ssbs, cpu)) {
11746 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
11747 new_mode |= PSTATE_SSBS;
11748 } else {
11749 new_mode &= ~PSTATE_SSBS;
11750 }
11751 }
11752
11753 if (cpu_isar_feature(aa64_nmi, cpu)) {
11754 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPINTMASK)) {
11755 new_mode |= PSTATE_ALLINT;
11756 } else {
11757 new_mode &= ~PSTATE_ALLINT;
11758 }
11759 }
11760
11761 pstate_write(env, PSTATE_DAIF | new_mode);
11762 env->aarch64 = true;
11763 aarch64_restore_sp(env, new_el);
11764
11765 if (tcg_enabled()) {
11766 helper_rebuild_hflags_a64(env, new_el);
11767 }
11768
11769 env->pc = addr;
11770
11771 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
11772 new_el, env->pc, pstate_read(env));
11773 }
11774
11775 /*
11776 * Do semihosting call and set the appropriate return value. All the
11777 * permission and validity checks have been done at translate time.
11778 *
11779 * We only see semihosting exceptions in TCG only as they are not
11780 * trapped to the hypervisor in KVM.
11781 */
11782 #ifdef CONFIG_TCG
tcg_handle_semihosting(CPUState * cs)11783 static void tcg_handle_semihosting(CPUState *cs)
11784 {
11785 ARMCPU *cpu = ARM_CPU(cs);
11786 CPUARMState *env = &cpu->env;
11787
11788 if (is_a64(env)) {
11789 qemu_log_mask(CPU_LOG_INT,
11790 "...handling as semihosting call 0x%" PRIx64 "\n",
11791 env->xregs[0]);
11792 do_common_semihosting(cs);
11793 env->pc += 4;
11794 } else {
11795 qemu_log_mask(CPU_LOG_INT,
11796 "...handling as semihosting call 0x%x\n",
11797 env->regs[0]);
11798 do_common_semihosting(cs);
11799 env->regs[15] += env->thumb ? 2 : 4;
11800 }
11801 }
11802 #endif
11803
11804 /*
11805 * Handle a CPU exception for A and R profile CPUs.
11806 * Do any appropriate logging, handle PSCI calls, and then hand off
11807 * to the AArch64-entry or AArch32-entry function depending on the
11808 * target exception level's register width.
11809 *
11810 * Note: this is used for both TCG (as the do_interrupt tcg op),
11811 * and KVM to re-inject guest debug exceptions, and to
11812 * inject a Synchronous-External-Abort.
11813 */
arm_cpu_do_interrupt(CPUState * cs)11814 void arm_cpu_do_interrupt(CPUState *cs)
11815 {
11816 ARMCPU *cpu = ARM_CPU(cs);
11817 CPUARMState *env = &cpu->env;
11818 unsigned int new_el = env->exception.target_el;
11819
11820 assert(!arm_feature(env, ARM_FEATURE_M));
11821
11822 arm_log_exception(cs);
11823 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
11824 new_el);
11825 if (qemu_loglevel_mask(CPU_LOG_INT)
11826 && !excp_is_internal(cs->exception_index)) {
11827 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
11828 syn_get_ec(env->exception.syndrome),
11829 env->exception.syndrome);
11830 }
11831
11832 if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
11833 arm_handle_psci_call(cpu);
11834 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
11835 return;
11836 }
11837
11838 /*
11839 * Semihosting semantics depend on the register width of the code
11840 * that caused the exception, not the target exception level, so
11841 * must be handled here.
11842 */
11843 #ifdef CONFIG_TCG
11844 if (cs->exception_index == EXCP_SEMIHOST) {
11845 tcg_handle_semihosting(cs);
11846 return;
11847 }
11848 #endif
11849
11850 /*
11851 * Hooks may change global state so BQL should be held, also the
11852 * BQL needs to be held for any modification of
11853 * cs->interrupt_request.
11854 */
11855 g_assert(bql_locked());
11856
11857 arm_call_pre_el_change_hook(cpu);
11858
11859 assert(!excp_is_internal(cs->exception_index));
11860 if (arm_el_is_aa64(env, new_el)) {
11861 arm_cpu_do_interrupt_aarch64(cs);
11862 } else {
11863 arm_cpu_do_interrupt_aarch32(cs);
11864 }
11865
11866 arm_call_el_change_hook(cpu);
11867
11868 if (!kvm_enabled()) {
11869 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
11870 }
11871 }
11872 #endif /* !CONFIG_USER_ONLY */
11873
arm_sctlr(CPUARMState * env,int el)11874 uint64_t arm_sctlr(CPUARMState *env, int el)
11875 {
11876 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0 or EL3&0 */
11877 if (el == 0) {
11878 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
11879 switch (mmu_idx) {
11880 case ARMMMUIdx_E20_0:
11881 el = 2;
11882 break;
11883 case ARMMMUIdx_E30_0:
11884 el = 3;
11885 break;
11886 default:
11887 el = 1;
11888 break;
11889 }
11890 }
11891 return env->cp15.sctlr_el[el];
11892 }
11893
aa64_va_parameter_tbi(uint64_t tcr,ARMMMUIdx mmu_idx)11894 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
11895 {
11896 if (regime_has_2_ranges(mmu_idx)) {
11897 return extract64(tcr, 37, 2);
11898 } else if (regime_is_stage2(mmu_idx)) {
11899 return 0; /* VTCR_EL2 */
11900 } else {
11901 /* Replicate the single TBI bit so we always have 2 bits. */
11902 return extract32(tcr, 20, 1) * 3;
11903 }
11904 }
11905
aa64_va_parameter_tbid(uint64_t tcr,ARMMMUIdx mmu_idx)11906 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
11907 {
11908 if (regime_has_2_ranges(mmu_idx)) {
11909 return extract64(tcr, 51, 2);
11910 } else if (regime_is_stage2(mmu_idx)) {
11911 return 0; /* VTCR_EL2 */
11912 } else {
11913 /* Replicate the single TBID bit so we always have 2 bits. */
11914 return extract32(tcr, 29, 1) * 3;
11915 }
11916 }
11917
aa64_va_parameter_tcma(uint64_t tcr,ARMMMUIdx mmu_idx)11918 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
11919 {
11920 if (regime_has_2_ranges(mmu_idx)) {
11921 return extract64(tcr, 57, 2);
11922 } else {
11923 /* Replicate the single TCMA bit so we always have 2 bits. */
11924 return extract32(tcr, 30, 1) * 3;
11925 }
11926 }
11927
tg0_to_gran_size(int tg)11928 static ARMGranuleSize tg0_to_gran_size(int tg)
11929 {
11930 switch (tg) {
11931 case 0:
11932 return Gran4K;
11933 case 1:
11934 return Gran64K;
11935 case 2:
11936 return Gran16K;
11937 default:
11938 return GranInvalid;
11939 }
11940 }
11941
tg1_to_gran_size(int tg)11942 static ARMGranuleSize tg1_to_gran_size(int tg)
11943 {
11944 switch (tg) {
11945 case 1:
11946 return Gran16K;
11947 case 2:
11948 return Gran4K;
11949 case 3:
11950 return Gran64K;
11951 default:
11952 return GranInvalid;
11953 }
11954 }
11955
have4k(ARMCPU * cpu,bool stage2)11956 static inline bool have4k(ARMCPU *cpu, bool stage2)
11957 {
11958 return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
11959 : cpu_isar_feature(aa64_tgran4, cpu);
11960 }
11961
have16k(ARMCPU * cpu,bool stage2)11962 static inline bool have16k(ARMCPU *cpu, bool stage2)
11963 {
11964 return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
11965 : cpu_isar_feature(aa64_tgran16, cpu);
11966 }
11967
have64k(ARMCPU * cpu,bool stage2)11968 static inline bool have64k(ARMCPU *cpu, bool stage2)
11969 {
11970 return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
11971 : cpu_isar_feature(aa64_tgran64, cpu);
11972 }
11973
sanitize_gran_size(ARMCPU * cpu,ARMGranuleSize gran,bool stage2)11974 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
11975 bool stage2)
11976 {
11977 switch (gran) {
11978 case Gran4K:
11979 if (have4k(cpu, stage2)) {
11980 return gran;
11981 }
11982 break;
11983 case Gran16K:
11984 if (have16k(cpu, stage2)) {
11985 return gran;
11986 }
11987 break;
11988 case Gran64K:
11989 if (have64k(cpu, stage2)) {
11990 return gran;
11991 }
11992 break;
11993 case GranInvalid:
11994 break;
11995 }
11996 /*
11997 * If the guest selects a granule size that isn't implemented,
11998 * the architecture requires that we behave as if it selected one
11999 * that is (with an IMPDEF choice of which one to pick). We choose
12000 * to implement the smallest supported granule size.
12001 */
12002 if (have4k(cpu, stage2)) {
12003 return Gran4K;
12004 }
12005 if (have16k(cpu, stage2)) {
12006 return Gran16K;
12007 }
12008 assert(have64k(cpu, stage2));
12009 return Gran64K;
12010 }
12011
aa64_va_parameters(CPUARMState * env,uint64_t va,ARMMMUIdx mmu_idx,bool data,bool el1_is_aa32)12012 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
12013 ARMMMUIdx mmu_idx, bool data,
12014 bool el1_is_aa32)
12015 {
12016 uint64_t tcr = regime_tcr(env, mmu_idx);
12017 bool epd, hpd, tsz_oob, ds, ha, hd;
12018 int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
12019 ARMGranuleSize gran;
12020 ARMCPU *cpu = env_archcpu(env);
12021 bool stage2 = regime_is_stage2(mmu_idx);
12022
12023 if (!regime_has_2_ranges(mmu_idx)) {
12024 select = 0;
12025 tsz = extract32(tcr, 0, 6);
12026 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
12027 if (stage2) {
12028 /* VTCR_EL2 */
12029 hpd = false;
12030 } else {
12031 hpd = extract32(tcr, 24, 1);
12032 }
12033 epd = false;
12034 sh = extract32(tcr, 12, 2);
12035 ps = extract32(tcr, 16, 3);
12036 ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
12037 hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
12038 ds = extract64(tcr, 32, 1);
12039 } else {
12040 bool e0pd;
12041
12042 /*
12043 * Bit 55 is always between the two regions, and is canonical for
12044 * determining if address tagging is enabled.
12045 */
12046 select = extract64(va, 55, 1);
12047 if (!select) {
12048 tsz = extract32(tcr, 0, 6);
12049 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
12050 epd = extract32(tcr, 7, 1);
12051 sh = extract32(tcr, 12, 2);
12052 hpd = extract64(tcr, 41, 1);
12053 e0pd = extract64(tcr, 55, 1);
12054 } else {
12055 tsz = extract32(tcr, 16, 6);
12056 gran = tg1_to_gran_size(extract32(tcr, 30, 2));
12057 epd = extract32(tcr, 23, 1);
12058 sh = extract32(tcr, 28, 2);
12059 hpd = extract64(tcr, 42, 1);
12060 e0pd = extract64(tcr, 56, 1);
12061 }
12062 ps = extract64(tcr, 32, 3);
12063 ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
12064 hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
12065 ds = extract64(tcr, 59, 1);
12066
12067 if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
12068 regime_is_user(env, mmu_idx)) {
12069 epd = true;
12070 }
12071 }
12072
12073 gran = sanitize_gran_size(cpu, gran, stage2);
12074
12075 if (cpu_isar_feature(aa64_st, cpu)) {
12076 max_tsz = 48 - (gran == Gran64K);
12077 } else {
12078 max_tsz = 39;
12079 }
12080
12081 /*
12082 * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
12083 * adjust the effective value of DS, as documented.
12084 */
12085 min_tsz = 16;
12086 if (gran == Gran64K) {
12087 if (cpu_isar_feature(aa64_lva, cpu)) {
12088 min_tsz = 12;
12089 }
12090 ds = false;
12091 } else if (ds) {
12092 if (regime_is_stage2(mmu_idx)) {
12093 if (gran == Gran16K) {
12094 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
12095 } else {
12096 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
12097 }
12098 } else {
12099 if (gran == Gran16K) {
12100 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
12101 } else {
12102 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
12103 }
12104 }
12105 if (ds) {
12106 min_tsz = 12;
12107 }
12108 }
12109
12110 if (stage2 && el1_is_aa32) {
12111 /*
12112 * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
12113 * are loosened: a configured IPA of 40 bits is permitted even if
12114 * the implemented PA is less than that (and so a 40 bit IPA would
12115 * fault for an AArch64 EL1). See R_DTLMN.
12116 */
12117 min_tsz = MIN(min_tsz, 24);
12118 }
12119
12120 if (tsz > max_tsz) {
12121 tsz = max_tsz;
12122 tsz_oob = true;
12123 } else if (tsz < min_tsz) {
12124 tsz = min_tsz;
12125 tsz_oob = true;
12126 } else {
12127 tsz_oob = false;
12128 }
12129
12130 /* Present TBI as a composite with TBID. */
12131 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
12132 if (!data) {
12133 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
12134 }
12135 tbi = (tbi >> select) & 1;
12136
12137 return (ARMVAParameters) {
12138 .tsz = tsz,
12139 .ps = ps,
12140 .sh = sh,
12141 .select = select,
12142 .tbi = tbi,
12143 .epd = epd,
12144 .hpd = hpd,
12145 .tsz_oob = tsz_oob,
12146 .ds = ds,
12147 .ha = ha,
12148 .hd = ha && hd,
12149 .gran = gran,
12150 };
12151 }
12152
12153 /*
12154 * Note that signed overflow is undefined in C. The following routines are
12155 * careful to use unsigned types where modulo arithmetic is required.
12156 * Failure to do so _will_ break on newer gcc.
12157 */
12158
12159 /* Signed saturating arithmetic. */
12160
12161 /* Perform 16-bit signed saturating addition. */
add16_sat(uint16_t a,uint16_t b)12162 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
12163 {
12164 uint16_t res;
12165
12166 res = a + b;
12167 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
12168 if (a & 0x8000) {
12169 res = 0x8000;
12170 } else {
12171 res = 0x7fff;
12172 }
12173 }
12174 return res;
12175 }
12176
12177 /* Perform 8-bit signed saturating addition. */
add8_sat(uint8_t a,uint8_t b)12178 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
12179 {
12180 uint8_t res;
12181
12182 res = a + b;
12183 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
12184 if (a & 0x80) {
12185 res = 0x80;
12186 } else {
12187 res = 0x7f;
12188 }
12189 }
12190 return res;
12191 }
12192
12193 /* Perform 16-bit signed saturating subtraction. */
sub16_sat(uint16_t a,uint16_t b)12194 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
12195 {
12196 uint16_t res;
12197
12198 res = a - b;
12199 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
12200 if (a & 0x8000) {
12201 res = 0x8000;
12202 } else {
12203 res = 0x7fff;
12204 }
12205 }
12206 return res;
12207 }
12208
12209 /* Perform 8-bit signed saturating subtraction. */
sub8_sat(uint8_t a,uint8_t b)12210 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
12211 {
12212 uint8_t res;
12213
12214 res = a - b;
12215 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
12216 if (a & 0x80) {
12217 res = 0x80;
12218 } else {
12219 res = 0x7f;
12220 }
12221 }
12222 return res;
12223 }
12224
12225 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
12226 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
12227 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
12228 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
12229 #define PFX q
12230
12231 #include "op_addsub.h"
12232
12233 /* Unsigned saturating arithmetic. */
add16_usat(uint16_t a,uint16_t b)12234 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
12235 {
12236 uint16_t res;
12237 res = a + b;
12238 if (res < a) {
12239 res = 0xffff;
12240 }
12241 return res;
12242 }
12243
sub16_usat(uint16_t a,uint16_t b)12244 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
12245 {
12246 if (a > b) {
12247 return a - b;
12248 } else {
12249 return 0;
12250 }
12251 }
12252
add8_usat(uint8_t a,uint8_t b)12253 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
12254 {
12255 uint8_t res;
12256 res = a + b;
12257 if (res < a) {
12258 res = 0xff;
12259 }
12260 return res;
12261 }
12262
sub8_usat(uint8_t a,uint8_t b)12263 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
12264 {
12265 if (a > b) {
12266 return a - b;
12267 } else {
12268 return 0;
12269 }
12270 }
12271
12272 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
12273 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
12274 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
12275 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
12276 #define PFX uq
12277
12278 #include "op_addsub.h"
12279
12280 /* Signed modulo arithmetic. */
12281 #define SARITH16(a, b, n, op) do { \
12282 int32_t sum; \
12283 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
12284 RESULT(sum, n, 16); \
12285 if (sum >= 0) \
12286 ge |= 3 << (n * 2); \
12287 } while (0)
12288
12289 #define SARITH8(a, b, n, op) do { \
12290 int32_t sum; \
12291 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
12292 RESULT(sum, n, 8); \
12293 if (sum >= 0) \
12294 ge |= 1 << n; \
12295 } while (0)
12296
12297
12298 #define ADD16(a, b, n) SARITH16(a, b, n, +)
12299 #define SUB16(a, b, n) SARITH16(a, b, n, -)
12300 #define ADD8(a, b, n) SARITH8(a, b, n, +)
12301 #define SUB8(a, b, n) SARITH8(a, b, n, -)
12302 #define PFX s
12303 #define ARITH_GE
12304
12305 #include "op_addsub.h"
12306
12307 /* Unsigned modulo arithmetic. */
12308 #define ADD16(a, b, n) do { \
12309 uint32_t sum; \
12310 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
12311 RESULT(sum, n, 16); \
12312 if ((sum >> 16) == 1) \
12313 ge |= 3 << (n * 2); \
12314 } while (0)
12315
12316 #define ADD8(a, b, n) do { \
12317 uint32_t sum; \
12318 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
12319 RESULT(sum, n, 8); \
12320 if ((sum >> 8) == 1) \
12321 ge |= 1 << n; \
12322 } while (0)
12323
12324 #define SUB16(a, b, n) do { \
12325 uint32_t sum; \
12326 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
12327 RESULT(sum, n, 16); \
12328 if ((sum >> 16) == 0) \
12329 ge |= 3 << (n * 2); \
12330 } while (0)
12331
12332 #define SUB8(a, b, n) do { \
12333 uint32_t sum; \
12334 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
12335 RESULT(sum, n, 8); \
12336 if ((sum >> 8) == 0) \
12337 ge |= 1 << n; \
12338 } while (0)
12339
12340 #define PFX u
12341 #define ARITH_GE
12342
12343 #include "op_addsub.h"
12344
12345 /* Halved signed arithmetic. */
12346 #define ADD16(a, b, n) \
12347 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
12348 #define SUB16(a, b, n) \
12349 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
12350 #define ADD8(a, b, n) \
12351 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
12352 #define SUB8(a, b, n) \
12353 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
12354 #define PFX sh
12355
12356 #include "op_addsub.h"
12357
12358 /* Halved unsigned arithmetic. */
12359 #define ADD16(a, b, n) \
12360 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12361 #define SUB16(a, b, n) \
12362 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
12363 #define ADD8(a, b, n) \
12364 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12365 #define SUB8(a, b, n) \
12366 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
12367 #define PFX uh
12368
12369 #include "op_addsub.h"
12370
do_usad(uint8_t a,uint8_t b)12371 static inline uint8_t do_usad(uint8_t a, uint8_t b)
12372 {
12373 if (a > b) {
12374 return a - b;
12375 } else {
12376 return b - a;
12377 }
12378 }
12379
12380 /* Unsigned sum of absolute byte differences. */
HELPER(usad8)12381 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
12382 {
12383 uint32_t sum;
12384 sum = do_usad(a, b);
12385 sum += do_usad(a >> 8, b >> 8);
12386 sum += do_usad(a >> 16, b >> 16);
12387 sum += do_usad(a >> 24, b >> 24);
12388 return sum;
12389 }
12390
12391 /* For ARMv6 SEL instruction. */
HELPER(sel_flags)12392 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
12393 {
12394 uint32_t mask;
12395
12396 mask = 0;
12397 if (flags & 1) {
12398 mask |= 0xff;
12399 }
12400 if (flags & 2) {
12401 mask |= 0xff00;
12402 }
12403 if (flags & 4) {
12404 mask |= 0xff0000;
12405 }
12406 if (flags & 8) {
12407 mask |= 0xff000000;
12408 }
12409 return (a & mask) | (b & ~mask);
12410 }
12411
12412 /*
12413 * CRC helpers.
12414 * The upper bytes of val (above the number specified by 'bytes') must have
12415 * been zeroed out by the caller.
12416 */
HELPER(crc32)12417 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
12418 {
12419 uint8_t buf[4];
12420
12421 stl_le_p(buf, val);
12422
12423 /* zlib crc32 converts the accumulator and output to one's complement. */
12424 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
12425 }
12426
HELPER(crc32c)12427 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
12428 {
12429 uint8_t buf[4];
12430
12431 stl_le_p(buf, val);
12432
12433 /* Linux crc32c converts the output to one's complement. */
12434 return crc32c(acc, buf, bytes) ^ 0xffffffff;
12435 }
12436
12437 /*
12438 * Return the exception level to which FP-disabled exceptions should
12439 * be taken, or 0 if FP is enabled.
12440 */
fp_exception_el(CPUARMState * env,int cur_el)12441 int fp_exception_el(CPUARMState *env, int cur_el)
12442 {
12443 #ifndef CONFIG_USER_ONLY
12444 uint64_t hcr_el2;
12445
12446 /*
12447 * CPACR and the CPTR registers don't exist before v6, so FP is
12448 * always accessible
12449 */
12450 if (!arm_feature(env, ARM_FEATURE_V6)) {
12451 return 0;
12452 }
12453
12454 if (arm_feature(env, ARM_FEATURE_M)) {
12455 /* CPACR can cause a NOCP UsageFault taken to current security state */
12456 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
12457 return 1;
12458 }
12459
12460 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
12461 if (!extract32(env->v7m.nsacr, 10, 1)) {
12462 /* FP insns cause a NOCP UsageFault taken to Secure */
12463 return 3;
12464 }
12465 }
12466
12467 return 0;
12468 }
12469
12470 hcr_el2 = arm_hcr_el2_eff(env);
12471
12472 /*
12473 * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
12474 * 0, 2 : trap EL0 and EL1/PL1 accesses
12475 * 1 : trap only EL0 accesses
12476 * 3 : trap no accesses
12477 * This register is ignored if E2H+TGE are both set.
12478 */
12479 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
12480 int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
12481
12482 switch (fpen) {
12483 case 1:
12484 if (cur_el != 0) {
12485 break;
12486 }
12487 /* fall through */
12488 case 0:
12489 case 2:
12490 /* Trap from Secure PL0 or PL1 to Secure PL1. */
12491 if (!arm_el_is_aa64(env, 3)
12492 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
12493 return 3;
12494 }
12495 if (cur_el <= 1) {
12496 return 1;
12497 }
12498 break;
12499 }
12500 }
12501
12502 /*
12503 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
12504 * to control non-secure access to the FPU. It doesn't have any
12505 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
12506 */
12507 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
12508 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
12509 if (!extract32(env->cp15.nsacr, 10, 1)) {
12510 /* FP insns act as UNDEF */
12511 return cur_el == 2 ? 2 : 1;
12512 }
12513 }
12514
12515 /*
12516 * CPTR_EL2 is present in v7VE or v8, and changes format
12517 * with HCR_EL2.E2H (regardless of TGE).
12518 */
12519 if (cur_el <= 2) {
12520 if (hcr_el2 & HCR_E2H) {
12521 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
12522 case 1:
12523 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
12524 break;
12525 }
12526 /* fall through */
12527 case 0:
12528 case 2:
12529 return 2;
12530 }
12531 } else if (arm_is_el2_enabled(env)) {
12532 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
12533 return 2;
12534 }
12535 }
12536 }
12537
12538 /* CPTR_EL3 : present in v8 */
12539 if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
12540 /* Trap all FP ops to EL3 */
12541 return 3;
12542 }
12543 #endif
12544 return 0;
12545 }
12546
12547 /* Return the exception level we're running at if this is our mmu_idx */
arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)12548 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
12549 {
12550 if (mmu_idx & ARM_MMU_IDX_M) {
12551 return mmu_idx & ARM_MMU_IDX_M_PRIV;
12552 }
12553
12554 switch (mmu_idx) {
12555 case ARMMMUIdx_E10_0:
12556 case ARMMMUIdx_E20_0:
12557 case ARMMMUIdx_E30_0:
12558 return 0;
12559 case ARMMMUIdx_E10_1:
12560 case ARMMMUIdx_E10_1_PAN:
12561 return 1;
12562 case ARMMMUIdx_E2:
12563 case ARMMMUIdx_E20_2:
12564 case ARMMMUIdx_E20_2_PAN:
12565 return 2;
12566 case ARMMMUIdx_E3:
12567 case ARMMMUIdx_E30_3_PAN:
12568 return 3;
12569 default:
12570 g_assert_not_reached();
12571 }
12572 }
12573
12574 #ifndef CONFIG_TCG
arm_v7m_mmu_idx_for_secstate(CPUARMState * env,bool secstate)12575 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
12576 {
12577 g_assert_not_reached();
12578 }
12579 #endif
12580
arm_mmu_idx_el(CPUARMState * env,int el)12581 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
12582 {
12583 ARMMMUIdx idx;
12584 uint64_t hcr;
12585
12586 if (arm_feature(env, ARM_FEATURE_M)) {
12587 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
12588 }
12589
12590 /* See ARM pseudo-function ELIsInHost. */
12591 switch (el) {
12592 case 0:
12593 hcr = arm_hcr_el2_eff(env);
12594 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
12595 idx = ARMMMUIdx_E20_0;
12596 } else if (arm_is_secure_below_el3(env) &&
12597 !arm_el_is_aa64(env, 3)) {
12598 idx = ARMMMUIdx_E30_0;
12599 } else {
12600 idx = ARMMMUIdx_E10_0;
12601 }
12602 break;
12603 case 1:
12604 if (arm_pan_enabled(env)) {
12605 idx = ARMMMUIdx_E10_1_PAN;
12606 } else {
12607 idx = ARMMMUIdx_E10_1;
12608 }
12609 break;
12610 case 2:
12611 /* Note that TGE does not apply at EL2. */
12612 if (arm_hcr_el2_eff(env) & HCR_E2H) {
12613 if (arm_pan_enabled(env)) {
12614 idx = ARMMMUIdx_E20_2_PAN;
12615 } else {
12616 idx = ARMMMUIdx_E20_2;
12617 }
12618 } else {
12619 idx = ARMMMUIdx_E2;
12620 }
12621 break;
12622 case 3:
12623 if (!arm_el_is_aa64(env, 3) && arm_pan_enabled(env)) {
12624 return ARMMMUIdx_E30_3_PAN;
12625 }
12626 return ARMMMUIdx_E3;
12627 default:
12628 g_assert_not_reached();
12629 }
12630
12631 return idx;
12632 }
12633
arm_mmu_idx(CPUARMState * env)12634 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
12635 {
12636 return arm_mmu_idx_el(env, arm_current_el(env));
12637 }
12638
mve_no_pred(CPUARMState * env)12639 static bool mve_no_pred(CPUARMState *env)
12640 {
12641 /*
12642 * Return true if there is definitely no predication of MVE
12643 * instructions by VPR or LTPSIZE. (Returning false even if there
12644 * isn't any predication is OK; generated code will just be
12645 * a little worse.)
12646 * If the CPU does not implement MVE then this TB flag is always 0.
12647 *
12648 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
12649 * logic in gen_update_fp_context() needs to be updated to match.
12650 *
12651 * We do not include the effect of the ECI bits here -- they are
12652 * tracked in other TB flags. This simplifies the logic for
12653 * "when did we emit code that changes the MVE_NO_PRED TB flag
12654 * and thus need to end the TB?".
12655 */
12656 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
12657 return false;
12658 }
12659 if (env->v7m.vpr) {
12660 return false;
12661 }
12662 if (env->v7m.ltpsize < 4) {
12663 return false;
12664 }
12665 return true;
12666 }
12667
cpu_get_tb_cpu_state(CPUARMState * env,vaddr * pc,uint64_t * cs_base,uint32_t * pflags)12668 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
12669 uint64_t *cs_base, uint32_t *pflags)
12670 {
12671 CPUARMTBFlags flags;
12672
12673 assert_hflags_rebuild_correctly(env);
12674 flags = env->hflags;
12675
12676 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
12677 *pc = env->pc;
12678 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
12679 DP_TBFLAG_A64(flags, BTYPE, env->btype);
12680 }
12681 } else {
12682 *pc = env->regs[15];
12683
12684 if (arm_feature(env, ARM_FEATURE_M)) {
12685 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12686 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12687 != env->v7m.secure) {
12688 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
12689 }
12690
12691 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12692 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12693 (env->v7m.secure &&
12694 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12695 /*
12696 * ASPEN is set, but FPCA/SFPA indicate that there is no
12697 * active FP context; we must create a new FP context before
12698 * executing any FP insn.
12699 */
12700 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
12701 }
12702
12703 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12704 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
12705 DP_TBFLAG_M32(flags, LSPACT, 1);
12706 }
12707
12708 if (mve_no_pred(env)) {
12709 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12710 }
12711 } else {
12712 /*
12713 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12714 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12715 */
12716 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
12717 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
12718 } else {
12719 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12720 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
12721 }
12722 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
12723 DP_TBFLAG_A32(flags, VFPEN, 1);
12724 }
12725 }
12726
12727 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12728 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
12729 }
12730
12731 /*
12732 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
12733 * states defined in the ARM ARM for software singlestep:
12734 * SS_ACTIVE PSTATE.SS State
12735 * 0 x Inactive (the TB flag for SS is always 0)
12736 * 1 0 Active-pending
12737 * 1 1 Active-not-pending
12738 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
12739 */
12740 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12741 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
12742 }
12743
12744 *pflags = flags.flags;
12745 *cs_base = flags.flags2;
12746 }
12747
12748 #ifdef TARGET_AARCH64
12749 /*
12750 * The manual says that when SVE is enabled and VQ is widened the
12751 * implementation is allowed to zero the previously inaccessible
12752 * portion of the registers. The corollary to that is that when
12753 * SVE is enabled and VQ is narrowed we are also allowed to zero
12754 * the now inaccessible portion of the registers.
12755 *
12756 * The intent of this is that no predicate bit beyond VQ is ever set.
12757 * Which means that some operations on predicate registers themselves
12758 * may operate on full uint64_t or even unrolled across the maximum
12759 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
12760 * may well be cheaper than conditionals to restrict the operation
12761 * to the relevant portion of a uint16_t[16].
12762 */
aarch64_sve_narrow_vq(CPUARMState * env,unsigned vq)12763 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12764 {
12765 int i, j;
12766 uint64_t pmask;
12767
12768 assert(vq >= 1 && vq <= ARM_MAX_VQ);
12769 assert(vq <= env_archcpu(env)->sve_max_vq);
12770
12771 /* Zap the high bits of the zregs. */
12772 for (i = 0; i < 32; i++) {
12773 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12774 }
12775
12776 /* Zap the high bits of the pregs and ffr. */
12777 pmask = 0;
12778 if (vq & 3) {
12779 pmask = ~(-1ULL << (16 * (vq & 3)));
12780 }
12781 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12782 for (i = 0; i < 17; ++i) {
12783 env->vfp.pregs[i].p[j] &= pmask;
12784 }
12785 pmask = 0;
12786 }
12787 }
12788
sve_vqm1_for_el_sm_ena(CPUARMState * env,int el,bool sm)12789 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
12790 {
12791 int exc_el;
12792
12793 if (sm) {
12794 exc_el = sme_exception_el(env, el);
12795 } else {
12796 exc_el = sve_exception_el(env, el);
12797 }
12798 if (exc_el) {
12799 return 0; /* disabled */
12800 }
12801 return sve_vqm1_for_el_sm(env, el, sm);
12802 }
12803
12804 /*
12805 * Notice a change in SVE vector size when changing EL.
12806 */
aarch64_sve_change_el(CPUARMState * env,int old_el,int new_el,bool el0_a64)12807 void aarch64_sve_change_el(CPUARMState *env, int old_el,
12808 int new_el, bool el0_a64)
12809 {
12810 ARMCPU *cpu = env_archcpu(env);
12811 int old_len, new_len;
12812 bool old_a64, new_a64, sm;
12813
12814 /* Nothing to do if no SVE. */
12815 if (!cpu_isar_feature(aa64_sve, cpu)) {
12816 return;
12817 }
12818
12819 /* Nothing to do if FP is disabled in either EL. */
12820 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12821 return;
12822 }
12823
12824 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12825 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12826
12827 /*
12828 * Both AArch64.TakeException and AArch64.ExceptionReturn
12829 * invoke ResetSVEState when taking an exception from, or
12830 * returning to, AArch32 state when PSTATE.SM is enabled.
12831 */
12832 sm = FIELD_EX64(env->svcr, SVCR, SM);
12833 if (old_a64 != new_a64 && sm) {
12834 arm_reset_sve_state(env);
12835 return;
12836 }
12837
12838 /*
12839 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12840 * at ELx, or not available because the EL is in AArch32 state, then
12841 * for all purposes other than a direct read, the ZCR_ELx.LEN field
12842 * has an effective value of 0".
12843 *
12844 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12845 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12846 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
12847 * we already have the correct register contents when encountering the
12848 * vq0->vq0 transition between EL0->EL1.
12849 */
12850 old_len = new_len = 0;
12851 if (old_a64) {
12852 old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12853 }
12854 if (new_a64) {
12855 new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12856 }
12857
12858 /* When changing vector length, clear inaccessible state. */
12859 if (new_len < old_len) {
12860 aarch64_sve_narrow_vq(env, new_len + 1);
12861 }
12862 }
12863 #endif
12864
12865 #ifndef CONFIG_USER_ONLY
arm_security_space(CPUARMState * env)12866 ARMSecuritySpace arm_security_space(CPUARMState *env)
12867 {
12868 if (arm_feature(env, ARM_FEATURE_M)) {
12869 return arm_secure_to_space(env->v7m.secure);
12870 }
12871
12872 /*
12873 * If EL3 is not supported then the secure state is implementation
12874 * defined, in which case QEMU defaults to non-secure.
12875 */
12876 if (!arm_feature(env, ARM_FEATURE_EL3)) {
12877 return ARMSS_NonSecure;
12878 }
12879
12880 /* Check for AArch64 EL3 or AArch32 Mon. */
12881 if (is_a64(env)) {
12882 if (extract32(env->pstate, 2, 2) == 3) {
12883 if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
12884 return ARMSS_Root;
12885 } else {
12886 return ARMSS_Secure;
12887 }
12888 }
12889 } else {
12890 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
12891 return ARMSS_Secure;
12892 }
12893 }
12894
12895 return arm_security_space_below_el3(env);
12896 }
12897
arm_security_space_below_el3(CPUARMState * env)12898 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
12899 {
12900 assert(!arm_feature(env, ARM_FEATURE_M));
12901
12902 /*
12903 * If EL3 is not supported then the secure state is implementation
12904 * defined, in which case QEMU defaults to non-secure.
12905 */
12906 if (!arm_feature(env, ARM_FEATURE_EL3)) {
12907 return ARMSS_NonSecure;
12908 }
12909
12910 /*
12911 * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
12912 * Ignoring NSE when !NS retains consistency without having to
12913 * modify other predicates.
12914 */
12915 if (!(env->cp15.scr_el3 & SCR_NS)) {
12916 return ARMSS_Secure;
12917 } else if (env->cp15.scr_el3 & SCR_NSE) {
12918 return ARMSS_Realm;
12919 } else {
12920 return ARMSS_NonSecure;
12921 }
12922 }
12923 #endif /* !CONFIG_USER_ONLY */
12924