xref: /qemu/target/arm/helper.c (revision 5cb8b0988bdf1e1b22f66925604fe9a44a568993)
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/page-protection.h"
16 #include "exec/mmap-lock.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/timer.h"
19 #include "qemu/bitops.h"
20 #include "qemu/qemu-print.h"
21 #include "exec/cputlb.h"
22 #include "exec/translation-block.h"
23 #include "hw/irq.h"
24 #include "system/cpu-timers.h"
25 #include "exec/icount.h"
26 #include "system/kvm.h"
27 #include "system/tcg.h"
28 #include "qapi/error.h"
29 #include "qemu/guest-random.h"
30 #ifdef CONFIG_TCG
31 #include "accel/tcg/probe.h"
32 #include "accel/tcg/getpc.h"
33 #include "semihosting/common-semi.h"
34 #endif
35 #include "cpregs.h"
36 #include "target/arm/gtimer.h"
37 
38 #define HELPER_H "tcg/helper.h"
39 #include "exec/helper-proto.h.inc"
40 
41 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
42 
43 static void switch_mode(CPUARMState *env, int mode);
44 
raw_read(CPUARMState * env,const ARMCPRegInfo * ri)45 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
46 {
47     assert(ri->fieldoffset);
48     if (cpreg_field_is_64bit(ri)) {
49         return CPREG_FIELD64(env, ri);
50     } else {
51         return CPREG_FIELD32(env, ri);
52     }
53 }
54 
raw_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)55 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
56 {
57     assert(ri->fieldoffset);
58     if (cpreg_field_is_64bit(ri)) {
59         CPREG_FIELD64(env, ri) = value;
60     } else {
61         CPREG_FIELD32(env, ri) = value;
62     }
63 }
64 
raw_ptr(CPUARMState * env,const ARMCPRegInfo * ri)65 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
66 {
67     return (char *)env + ri->fieldoffset;
68 }
69 
read_raw_cp_reg(CPUARMState * env,const ARMCPRegInfo * ri)70 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
71 {
72     /* Raw read of a coprocessor register (as needed for migration, etc). */
73     if (ri->type & ARM_CP_CONST) {
74         return ri->resetvalue;
75     } else if (ri->raw_readfn) {
76         return ri->raw_readfn(env, ri);
77     } else if (ri->readfn) {
78         return ri->readfn(env, ri);
79     } else {
80         return raw_read(env, ri);
81     }
82 }
83 
write_raw_cp_reg(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t v)84 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
85                              uint64_t v)
86 {
87     /*
88      * Raw write of a coprocessor register (as needed for migration, etc).
89      * Note that constant registers are treated as write-ignored; the
90      * caller should check for success by whether a readback gives the
91      * value written.
92      */
93     if (ri->type & ARM_CP_CONST) {
94         return;
95     } else if (ri->raw_writefn) {
96         ri->raw_writefn(env, ri, v);
97     } else if (ri->writefn) {
98         ri->writefn(env, ri, v);
99     } else {
100         raw_write(env, ri, v);
101     }
102 }
103 
raw_accessors_invalid(const ARMCPRegInfo * ri)104 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
105 {
106    /*
107     * Return true if the regdef would cause an assertion if you called
108     * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
109     * program bug for it not to have the NO_RAW flag).
110     * NB that returning false here doesn't necessarily mean that calling
111     * read/write_raw_cp_reg() is safe, because we can't distinguish "has
112     * read/write access functions which are safe for raw use" from "has
113     * read/write access functions which have side effects but has forgotten
114     * to provide raw access functions".
115     * The tests here line up with the conditions in read/write_raw_cp_reg()
116     * and assertions in raw_read()/raw_write().
117     */
118     if ((ri->type & ARM_CP_CONST) ||
119         ri->fieldoffset ||
120         ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
121         return false;
122     }
123     return true;
124 }
125 
write_cpustate_to_list(ARMCPU * cpu,bool kvm_sync)126 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
127 {
128     /* Write the coprocessor state from cpu->env to the (index,value) list. */
129     int i;
130     bool ok = true;
131 
132     for (i = 0; i < cpu->cpreg_array_len; i++) {
133         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
134         const ARMCPRegInfo *ri;
135         uint64_t newval;
136 
137         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
138         if (!ri) {
139             ok = false;
140             continue;
141         }
142         if (ri->type & ARM_CP_NO_RAW) {
143             continue;
144         }
145 
146         newval = read_raw_cp_reg(&cpu->env, ri);
147         if (kvm_sync) {
148             /*
149              * Only sync if the previous list->cpustate sync succeeded.
150              * Rather than tracking the success/failure state for every
151              * item in the list, we just recheck "does the raw write we must
152              * have made in write_list_to_cpustate() read back OK" here.
153              */
154             uint64_t oldval = cpu->cpreg_values[i];
155 
156             if (oldval == newval) {
157                 continue;
158             }
159 
160             write_raw_cp_reg(&cpu->env, ri, oldval);
161             if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
162                 continue;
163             }
164 
165             write_raw_cp_reg(&cpu->env, ri, newval);
166         }
167         cpu->cpreg_values[i] = newval;
168     }
169     return ok;
170 }
171 
write_list_to_cpustate(ARMCPU * cpu)172 bool write_list_to_cpustate(ARMCPU *cpu)
173 {
174     int i;
175     bool ok = true;
176 
177     for (i = 0; i < cpu->cpreg_array_len; i++) {
178         uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
179         uint64_t v = cpu->cpreg_values[i];
180         const ARMCPRegInfo *ri;
181 
182         ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
183         if (!ri) {
184             ok = false;
185             continue;
186         }
187         if (ri->type & ARM_CP_NO_RAW) {
188             continue;
189         }
190         /*
191          * Write value and confirm it reads back as written
192          * (to catch read-only registers and partially read-only
193          * registers where the incoming migration value doesn't match)
194          */
195         write_raw_cp_reg(&cpu->env, ri, v);
196         if (read_raw_cp_reg(&cpu->env, ri) != v) {
197             ok = false;
198         }
199     }
200     return ok;
201 }
202 
add_cpreg_to_list(gpointer key,gpointer opaque)203 static void add_cpreg_to_list(gpointer key, gpointer opaque)
204 {
205     ARMCPU *cpu = opaque;
206     uint32_t regidx = (uintptr_t)key;
207     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
208 
209     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
210         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
211         /* The value array need not be initialized at this point */
212         cpu->cpreg_array_len++;
213     }
214 }
215 
count_cpreg(gpointer key,gpointer opaque)216 static void count_cpreg(gpointer key, gpointer opaque)
217 {
218     ARMCPU *cpu = opaque;
219     const ARMCPRegInfo *ri;
220 
221     ri = g_hash_table_lookup(cpu->cp_regs, key);
222 
223     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
224         cpu->cpreg_array_len++;
225     }
226 }
227 
cpreg_key_compare(gconstpointer a,gconstpointer b,gpointer d)228 static gint cpreg_key_compare(gconstpointer a, gconstpointer b, gpointer d)
229 {
230     uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
231     uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
232 
233     if (aidx > bidx) {
234         return 1;
235     }
236     if (aidx < bidx) {
237         return -1;
238     }
239     return 0;
240 }
241 
init_cpreg_list(ARMCPU * cpu)242 void init_cpreg_list(ARMCPU *cpu)
243 {
244     /*
245      * Initialise the cpreg_tuples[] array based on the cp_regs hash.
246      * Note that we require cpreg_tuples[] to be sorted by key ID.
247      */
248     GList *keys;
249     int arraylen;
250 
251     keys = g_hash_table_get_keys(cpu->cp_regs);
252     keys = g_list_sort_with_data(keys, cpreg_key_compare, NULL);
253 
254     cpu->cpreg_array_len = 0;
255 
256     g_list_foreach(keys, count_cpreg, cpu);
257 
258     arraylen = cpu->cpreg_array_len;
259     cpu->cpreg_indexes = g_new(uint64_t, arraylen);
260     cpu->cpreg_values = g_new(uint64_t, arraylen);
261     cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
262     cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
263     cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
264     cpu->cpreg_array_len = 0;
265 
266     g_list_foreach(keys, add_cpreg_to_list, cpu);
267 
268     assert(cpu->cpreg_array_len == arraylen);
269 
270     g_list_free(keys);
271 }
272 
arm_pan_enabled(CPUARMState * env)273 static bool arm_pan_enabled(CPUARMState *env)
274 {
275     if (is_a64(env)) {
276         if ((arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1)) == (HCR_NV | HCR_NV1)) {
277             return false;
278         }
279         return env->pstate & PSTATE_PAN;
280     } else {
281         return env->uncached_cpsr & CPSR_PAN;
282     }
283 }
284 
285 /*
286  * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
287  */
access_el3_aa32ns(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)288 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
289                                         const ARMCPRegInfo *ri,
290                                         bool isread)
291 {
292     if (!is_a64(env) && arm_current_el(env) == 3 &&
293         arm_is_secure_below_el3(env)) {
294         return CP_ACCESS_UNDEFINED;
295     }
296     return CP_ACCESS_OK;
297 }
298 
299 /*
300  * Some secure-only AArch32 registers trap to EL3 if used from
301  * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
302  * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
303  * We assume that the .access field is set to PL1_RW.
304  */
access_trap_aa32s_el1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)305 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
306                                             const ARMCPRegInfo *ri,
307                                             bool isread)
308 {
309     if (arm_current_el(env) == 3) {
310         return CP_ACCESS_OK;
311     }
312     if (arm_is_secure_below_el3(env)) {
313         if (env->cp15.scr_el3 & SCR_EEL2) {
314             return CP_ACCESS_TRAP_EL2;
315         }
316         return CP_ACCESS_TRAP_EL3;
317     }
318     /* This will be EL1 NS and EL2 NS, which just UNDEF */
319     return CP_ACCESS_UNDEFINED;
320 }
321 
322 /*
323  * Check for traps to performance monitor registers, which are controlled
324  * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
325  */
access_tpm(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)326 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
327                                  bool isread)
328 {
329     int el = arm_current_el(env);
330     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
331 
332     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
333         return CP_ACCESS_TRAP_EL2;
334     }
335     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
336         return CP_ACCESS_TRAP_EL3;
337     }
338     return CP_ACCESS_OK;
339 }
340 
341 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM.  */
access_tvm_trvm(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)342 CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
343                                bool isread)
344 {
345     if (arm_current_el(env) == 1) {
346         uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
347         if (arm_hcr_el2_eff(env) & trap) {
348             return CP_ACCESS_TRAP_EL2;
349         }
350     }
351     return CP_ACCESS_OK;
352 }
353 
354 /* Check for traps from EL1 due to HCR_EL2.TSW.  */
access_tsw(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)355 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
356                                  bool isread)
357 {
358     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
359         return CP_ACCESS_TRAP_EL2;
360     }
361     return CP_ACCESS_OK;
362 }
363 
364 /* Check for traps from EL1 due to HCR_EL2.TACR.  */
access_tacr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)365 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
366                                   bool isread)
367 {
368     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
369         return CP_ACCESS_TRAP_EL2;
370     }
371     return CP_ACCESS_OK;
372 }
373 
dacr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)374 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
375 {
376     ARMCPU *cpu = env_archcpu(env);
377 
378     raw_write(env, ri, value);
379     tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
380 }
381 
fcse_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)382 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
383 {
384     ARMCPU *cpu = env_archcpu(env);
385 
386     if (raw_read(env, ri) != value) {
387         /*
388          * Unlike real hardware the qemu TLB uses virtual addresses,
389          * not modified virtual addresses, so this causes a TLB flush.
390          */
391         tlb_flush(CPU(cpu));
392         raw_write(env, ri, value);
393     }
394 }
395 
contextidr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)396 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
397                              uint64_t value)
398 {
399     ARMCPU *cpu = env_archcpu(env);
400 
401     if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
402         && !extended_addresses_enabled(env)) {
403         /*
404          * For VMSA (when not using the LPAE long descriptor page table
405          * format) this register includes the ASID, so do a TLB flush.
406          * For PMSA it is purely a process ID and no action is needed.
407          */
408         tlb_flush(CPU(cpu));
409     }
410     raw_write(env, ri, value);
411 }
412 
alle1_tlbmask(CPUARMState * env)413 int alle1_tlbmask(CPUARMState *env)
414 {
415     /*
416      * Note that the 'ALL' scope must invalidate both stage 1 and
417      * stage 2 translations, whereas most other scopes only invalidate
418      * stage 1 translations.
419      *
420      * For AArch32 this is only used for TLBIALLNSNH and VTTBR
421      * writes, so only needs to apply to NS PL1&0, not S PL1&0.
422      */
423     return (ARMMMUIdxBit_E10_1 |
424             ARMMMUIdxBit_E10_1_PAN |
425             ARMMMUIdxBit_E10_0 |
426             ARMMMUIdxBit_Stage2 |
427             ARMMMUIdxBit_Stage2_S);
428 }
429 
430 static const ARMCPRegInfo cp_reginfo[] = {
431     /*
432      * Define the secure and non-secure FCSE identifier CP registers
433      * separately because there is no secure bank in V8 (no _EL3).  This allows
434      * the secure register to be properly reset and migrated. There is also no
435      * v8 EL1 version of the register so the non-secure instance stands alone.
436      */
437     { .name = "FCSEIDR",
438       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
439       .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
440       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
441       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
442     { .name = "FCSEIDR_S",
443       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
444       .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
445       .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
446       .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
447     /*
448      * Define the secure and non-secure context identifier CP registers
449      * separately because there is no secure bank in V8 (no _EL3).  This allows
450      * the secure register to be properly reset and migrated.  In the
451      * non-secure case, the 32-bit register will have reset and migration
452      * disabled during registration as it is handled by the 64-bit instance.
453      */
454     { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
455       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
456       .access = PL1_RW, .accessfn = access_tvm_trvm,
457       .fgt = FGT_CONTEXTIDR_EL1,
458       .nv2_redirect_offset = 0x108 | NV2_REDIR_NV1,
459       .secure = ARM_CP_SECSTATE_NS,
460       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
461       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
462     { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
463       .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
464       .access = PL1_RW, .accessfn = access_tvm_trvm,
465       .secure = ARM_CP_SECSTATE_S,
466       .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
467       .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
468 };
469 
470 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
471     /*
472      * NB: Some of these registers exist in v8 but with more precise
473      * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
474      */
475     /* MMU Domain access control / MPU write buffer control */
476     { .name = "DACR",
477       .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
478       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
479       .writefn = dacr_write, .raw_writefn = raw_write,
480       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
481                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
482     /*
483      * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
484      * For v6 and v5, these mappings are overly broad.
485      */
486     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
487       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
488     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
489       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
490     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
491       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
492     { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
493       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
494     /* Cache maintenance ops; some of this space may be overridden later. */
495     { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
496       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
497       .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
498 };
499 
500 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
501     /*
502      * Not all pre-v6 cores implemented this WFI, so this is slightly
503      * over-broad.
504      */
505     { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
506       .access = PL1_W, .type = ARM_CP_WFI },
507 };
508 
509 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
510     /*
511      * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
512      * is UNPREDICTABLE; we choose to NOP as most implementations do).
513      */
514     { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
515       .access = PL1_W, .type = ARM_CP_WFI },
516     /*
517      * L1 cache lockdown. Not architectural in v6 and earlier but in practice
518      * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
519      * OMAPCP will override this space.
520      */
521     { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
522       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
523       .resetvalue = 0 },
524     { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
525       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
526       .resetvalue = 0 },
527     /* v6 doesn't have the cache ID registers but Linux reads them anyway */
528     { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
529       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
530       .resetvalue = 0 },
531     /*
532      * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
533      * implementing it as RAZ means the "debug architecture version" bits
534      * will read as a reserved value, which should cause Linux to not try
535      * to use the debug hardware.
536      */
537     { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
538       .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
539     { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
540       .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
541     { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
542       .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
543 };
544 
cpacr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)545 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
546                         uint64_t value)
547 {
548     uint32_t mask = 0;
549 
550     /* In ARMv8 most bits of CPACR_EL1 are RES0. */
551     if (!arm_feature(env, ARM_FEATURE_V8)) {
552         /*
553          * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
554          * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
555          * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
556          */
557         if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
558             /* VFP coprocessor: cp10 & cp11 [23:20] */
559             mask |= R_CPACR_ASEDIS_MASK |
560                     R_CPACR_D32DIS_MASK |
561                     R_CPACR_CP11_MASK |
562                     R_CPACR_CP10_MASK;
563 
564             if (!arm_feature(env, ARM_FEATURE_NEON)) {
565                 /* ASEDIS [31] bit is RAO/WI */
566                 value |= R_CPACR_ASEDIS_MASK;
567             }
568 
569             /*
570              * VFPv3 and upwards with NEON implement 32 double precision
571              * registers (D0-D31).
572              */
573             if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
574                 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
575                 value |= R_CPACR_D32DIS_MASK;
576             }
577         }
578         value &= mask;
579     }
580 
581     /*
582      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
583      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
584      */
585     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
586         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
587         mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
588         value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
589     }
590 
591     env->cp15.cpacr_el1 = value;
592 }
593 
cpacr_read(CPUARMState * env,const ARMCPRegInfo * ri)594 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
595 {
596     /*
597      * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
598      * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
599      */
600     uint64_t value = env->cp15.cpacr_el1;
601 
602     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
603         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
604         value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
605     }
606     return value;
607 }
608 
609 
cpacr_reset(CPUARMState * env,const ARMCPRegInfo * ri)610 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
611 {
612     /*
613      * Call cpacr_write() so that we reset with the correct RAO bits set
614      * for our CPU features.
615      */
616     cpacr_write(env, ri, 0);
617 }
618 
cpacr_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)619 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
620                                    bool isread)
621 {
622     if (arm_feature(env, ARM_FEATURE_V8)) {
623         /* Check if CPACR accesses are to be trapped to EL2 */
624         if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
625             FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
626             return CP_ACCESS_TRAP_EL2;
627         /* Check if CPACR accesses are to be trapped to EL3 */
628         } else if (arm_current_el(env) < 3 &&
629                    FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
630             return CP_ACCESS_TRAP_EL3;
631         }
632     }
633 
634     return CP_ACCESS_OK;
635 }
636 
cptr_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)637 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
638                                   bool isread)
639 {
640     /* Check if CPTR accesses are set to trap to EL3 */
641     if (arm_current_el(env) == 2 &&
642         FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
643         return CP_ACCESS_TRAP_EL3;
644     }
645 
646     return CP_ACCESS_OK;
647 }
648 
649 static const ARMCPRegInfo v6_cp_reginfo[] = {
650     /* prefetch by MVA in v6, NOP in v7 */
651     { .name = "MVA_prefetch",
652       .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
653       .access = PL1_W, .type = ARM_CP_NOP },
654     /*
655      * We need to break the TB after ISB to execute self-modifying code
656      * correctly and also to take any pending interrupts immediately.
657      * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
658      */
659     { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
660       .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
661     { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
662       .access = PL0_W, .type = ARM_CP_NOP },
663     { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
664       .access = PL0_W, .type = ARM_CP_NOP },
665     { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
666       .access = PL1_RW, .accessfn = access_tvm_trvm,
667       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
668                              offsetof(CPUARMState, cp15.ifar_ns) },
669       .resetvalue = 0, },
670     /*
671      * Watchpoint Fault Address Register : should actually only be present
672      * for 1136, 1176, 11MPCore.
673      */
674     { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
675       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
676     { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
677       .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
678       .fgt = FGT_CPACR_EL1,
679       .nv2_redirect_offset = 0x100 | NV2_REDIR_NV1,
680       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
681       .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
682 };
683 
684 typedef struct pm_event {
685     uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
686     /* If the event is supported on this CPU (used to generate PMCEID[01]) */
687     bool (*supported)(CPUARMState *);
688     /*
689      * Retrieve the current count of the underlying event. The programmed
690      * counters hold a difference from the return value from this function
691      */
692     uint64_t (*get_count)(CPUARMState *);
693     /*
694      * Return how many nanoseconds it will take (at a minimum) for count events
695      * to occur. A negative value indicates the counter will never overflow, or
696      * that the counter has otherwise arranged for the overflow bit to be set
697      * and the PMU interrupt to be raised on overflow.
698      */
699     int64_t (*ns_per_count)(uint64_t);
700 } pm_event;
701 
event_always_supported(CPUARMState * env)702 static bool event_always_supported(CPUARMState *env)
703 {
704     return true;
705 }
706 
swinc_get_count(CPUARMState * env)707 static uint64_t swinc_get_count(CPUARMState *env)
708 {
709     /*
710      * SW_INCR events are written directly to the pmevcntr's by writes to
711      * PMSWINC, so there is no underlying count maintained by the PMU itself
712      */
713     return 0;
714 }
715 
swinc_ns_per(uint64_t ignored)716 static int64_t swinc_ns_per(uint64_t ignored)
717 {
718     return -1;
719 }
720 
721 /*
722  * Return the underlying cycle count for the PMU cycle counters. If we're in
723  * usermode, simply return 0.
724  */
cycles_get_count(CPUARMState * env)725 static uint64_t cycles_get_count(CPUARMState *env)
726 {
727 #ifndef CONFIG_USER_ONLY
728     return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
729                    ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
730 #else
731     return cpu_get_host_ticks();
732 #endif
733 }
734 
735 #ifndef CONFIG_USER_ONLY
cycles_ns_per(uint64_t cycles)736 static int64_t cycles_ns_per(uint64_t cycles)
737 {
738     return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
739 }
740 
instructions_supported(CPUARMState * env)741 static bool instructions_supported(CPUARMState *env)
742 {
743     /* Precise instruction counting */
744     return icount_enabled() == ICOUNT_PRECISE;
745 }
746 
instructions_get_count(CPUARMState * env)747 static uint64_t instructions_get_count(CPUARMState *env)
748 {
749     assert(icount_enabled() == ICOUNT_PRECISE);
750     return (uint64_t)icount_get_raw();
751 }
752 
instructions_ns_per(uint64_t icount)753 static int64_t instructions_ns_per(uint64_t icount)
754 {
755     assert(icount_enabled() == ICOUNT_PRECISE);
756     return icount_to_ns((int64_t)icount);
757 }
758 #endif
759 
pmuv3p1_events_supported(CPUARMState * env)760 static bool pmuv3p1_events_supported(CPUARMState *env)
761 {
762     /* For events which are supported in any v8.1 PMU */
763     return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
764 }
765 
pmuv3p4_events_supported(CPUARMState * env)766 static bool pmuv3p4_events_supported(CPUARMState *env)
767 {
768     /* For events which are supported in any v8.1 PMU */
769     return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
770 }
771 
zero_event_get_count(CPUARMState * env)772 static uint64_t zero_event_get_count(CPUARMState *env)
773 {
774     /* For events which on QEMU never fire, so their count is always zero */
775     return 0;
776 }
777 
zero_event_ns_per(uint64_t cycles)778 static int64_t zero_event_ns_per(uint64_t cycles)
779 {
780     /* An event which never fires can never overflow */
781     return -1;
782 }
783 
784 static const pm_event pm_events[] = {
785     { .number = 0x000, /* SW_INCR */
786       .supported = event_always_supported,
787       .get_count = swinc_get_count,
788       .ns_per_count = swinc_ns_per,
789     },
790 #ifndef CONFIG_USER_ONLY
791     { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
792       .supported = instructions_supported,
793       .get_count = instructions_get_count,
794       .ns_per_count = instructions_ns_per,
795     },
796     { .number = 0x011, /* CPU_CYCLES, Cycle */
797       .supported = event_always_supported,
798       .get_count = cycles_get_count,
799       .ns_per_count = cycles_ns_per,
800     },
801 #endif
802     { .number = 0x023, /* STALL_FRONTEND */
803       .supported = pmuv3p1_events_supported,
804       .get_count = zero_event_get_count,
805       .ns_per_count = zero_event_ns_per,
806     },
807     { .number = 0x024, /* STALL_BACKEND */
808       .supported = pmuv3p1_events_supported,
809       .get_count = zero_event_get_count,
810       .ns_per_count = zero_event_ns_per,
811     },
812     { .number = 0x03c, /* STALL */
813       .supported = pmuv3p4_events_supported,
814       .get_count = zero_event_get_count,
815       .ns_per_count = zero_event_ns_per,
816     },
817 };
818 
819 /*
820  * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
821  * events (i.e. the statistical profiling extension), this implementation
822  * should first be updated to something sparse instead of the current
823  * supported_event_map[] array.
824  */
825 #define MAX_EVENT_ID 0x3c
826 #define UNSUPPORTED_EVENT UINT16_MAX
827 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
828 
829 /*
830  * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
831  * of ARM event numbers to indices in our pm_events array.
832  *
833  * Note: Events in the 0x40XX range are not currently supported.
834  */
pmu_init(ARMCPU * cpu)835 void pmu_init(ARMCPU *cpu)
836 {
837     unsigned int i;
838 
839     /*
840      * Empty supported_event_map and cpu->pmceid[01] before adding supported
841      * events to them
842      */
843     for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
844         supported_event_map[i] = UNSUPPORTED_EVENT;
845     }
846     cpu->pmceid0 = 0;
847     cpu->pmceid1 = 0;
848 
849     for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
850         const pm_event *cnt = &pm_events[i];
851         assert(cnt->number <= MAX_EVENT_ID);
852         /* We do not currently support events in the 0x40xx range */
853         assert(cnt->number <= 0x3f);
854 
855         if (cnt->supported(&cpu->env)) {
856             supported_event_map[cnt->number] = i;
857             uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
858             if (cnt->number & 0x20) {
859                 cpu->pmceid1 |= event_mask;
860             } else {
861                 cpu->pmceid0 |= event_mask;
862             }
863         }
864     }
865 }
866 
867 /*
868  * Check at runtime whether a PMU event is supported for the current machine
869  */
event_supported(uint16_t number)870 static bool event_supported(uint16_t number)
871 {
872     if (number > MAX_EVENT_ID) {
873         return false;
874     }
875     return supported_event_map[number] != UNSUPPORTED_EVENT;
876 }
877 
pmreg_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)878 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
879                                    bool isread)
880 {
881     /*
882      * Performance monitor registers user accessibility is controlled
883      * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
884      * trapping to EL2 or EL3 for other accesses.
885      */
886     int el = arm_current_el(env);
887     uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
888 
889     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
890         return CP_ACCESS_TRAP_EL1;
891     }
892     if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
893         return CP_ACCESS_TRAP_EL2;
894     }
895     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
896         return CP_ACCESS_TRAP_EL3;
897     }
898 
899     return CP_ACCESS_OK;
900 }
901 
pmreg_access_xevcntr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)902 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
903                                            const ARMCPRegInfo *ri,
904                                            bool isread)
905 {
906     /* ER: event counter read trap control */
907     if (arm_feature(env, ARM_FEATURE_V8)
908         && arm_current_el(env) == 0
909         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
910         && isread) {
911         return CP_ACCESS_OK;
912     }
913 
914     return pmreg_access(env, ri, isread);
915 }
916 
pmreg_access_swinc(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)917 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
918                                          const ARMCPRegInfo *ri,
919                                          bool isread)
920 {
921     /* SW: software increment write trap control */
922     if (arm_feature(env, ARM_FEATURE_V8)
923         && arm_current_el(env) == 0
924         && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
925         && !isread) {
926         return CP_ACCESS_OK;
927     }
928 
929     return pmreg_access(env, ri, isread);
930 }
931 
pmreg_access_selr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)932 static CPAccessResult pmreg_access_selr(CPUARMState *env,
933                                         const ARMCPRegInfo *ri,
934                                         bool isread)
935 {
936     /* ER: event counter read trap control */
937     if (arm_feature(env, ARM_FEATURE_V8)
938         && arm_current_el(env) == 0
939         && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
940         return CP_ACCESS_OK;
941     }
942 
943     return pmreg_access(env, ri, isread);
944 }
945 
pmreg_access_ccntr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)946 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
947                                          const ARMCPRegInfo *ri,
948                                          bool isread)
949 {
950     /* CR: cycle counter read trap control */
951     if (arm_feature(env, ARM_FEATURE_V8)
952         && arm_current_el(env) == 0
953         && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
954         && isread) {
955         return CP_ACCESS_OK;
956     }
957 
958     return pmreg_access(env, ri, isread);
959 }
960 
961 /*
962  * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
963  * We use these to decide whether we need to wrap a write to MDCR_EL2
964  * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
965  */
966 #define MDCR_EL2_PMU_ENABLE_BITS \
967     (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
968 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
969 
970 /*
971  * Returns true if the counter (pass 31 for PMCCNTR) should count events using
972  * the current EL, security state, and register configuration.
973  */
pmu_counter_enabled(CPUARMState * env,uint8_t counter)974 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
975 {
976     uint64_t filter;
977     bool e, p, u, nsk, nsu, nsh, m;
978     bool enabled, prohibited = false, filtered;
979     bool secure = arm_is_secure(env);
980     int el = arm_current_el(env);
981     uint64_t mdcr_el2;
982     uint8_t hpmn;
983 
984     /*
985      * We might be called for M-profile cores where MDCR_EL2 doesn't
986      * exist and arm_mdcr_el2_eff() will assert, so this early-exit check
987      * must be before we read that value.
988      */
989     if (!arm_feature(env, ARM_FEATURE_PMU)) {
990         return false;
991     }
992 
993     mdcr_el2 = arm_mdcr_el2_eff(env);
994     hpmn = mdcr_el2 & MDCR_HPMN;
995 
996     if (!arm_feature(env, ARM_FEATURE_EL2) ||
997             (counter < hpmn || counter == 31)) {
998         e = env->cp15.c9_pmcr & PMCRE;
999     } else {
1000         e = mdcr_el2 & MDCR_HPME;
1001     }
1002     enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
1003 
1004     /* Is event counting prohibited? */
1005     if (el == 2 && (counter < hpmn || counter == 31)) {
1006         prohibited = mdcr_el2 & MDCR_HPMD;
1007     }
1008     if (secure) {
1009         prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1010     }
1011 
1012     if (counter == 31) {
1013         /*
1014          * The cycle counter defaults to running. PMCR.DP says "disable
1015          * the cycle counter when event counting is prohibited".
1016          * Some MDCR bits disable the cycle counter specifically.
1017          */
1018         prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1019         if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1020             if (secure) {
1021                 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1022             }
1023             if (el == 2) {
1024                 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1025             }
1026         }
1027     }
1028 
1029     if (counter == 31) {
1030         filter = env->cp15.pmccfiltr_el0;
1031     } else {
1032         filter = env->cp15.c14_pmevtyper[counter];
1033     }
1034 
1035     p   = filter & PMXEVTYPER_P;
1036     u   = filter & PMXEVTYPER_U;
1037     nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1038     nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1039     nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1040     m   = arm_el_is_aa64(env, 1) &&
1041               arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1042 
1043     if (el == 0) {
1044         filtered = secure ? u : u != nsu;
1045     } else if (el == 1) {
1046         filtered = secure ? p : p != nsk;
1047     } else if (el == 2) {
1048         filtered = !nsh;
1049     } else { /* EL3 */
1050         filtered = m != p;
1051     }
1052 
1053     if (counter != 31) {
1054         /*
1055          * If not checking PMCCNTR, ensure the counter is setup to an event we
1056          * support
1057          */
1058         uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1059         if (!event_supported(event)) {
1060             return false;
1061         }
1062     }
1063 
1064     return enabled && !prohibited && !filtered;
1065 }
1066 
pmu_update_irq(CPUARMState * env)1067 static void pmu_update_irq(CPUARMState *env)
1068 {
1069     ARMCPU *cpu = env_archcpu(env);
1070     qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1071             (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1072 }
1073 
pmccntr_clockdiv_enabled(CPUARMState * env)1074 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1075 {
1076     /*
1077      * Return true if the clock divider is enabled and the cycle counter
1078      * is supposed to tick only once every 64 clock cycles. This is
1079      * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1080      * (64-bit) cycle counter PMCR.D has no effect.
1081      */
1082     return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1083 }
1084 
pmevcntr_is_64_bit(CPUARMState * env,int counter)1085 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1086 {
1087     /* Return true if the specified event counter is configured to be 64 bit */
1088 
1089     /* This isn't intended to be used with the cycle counter */
1090     assert(counter < 31);
1091 
1092     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1093         return false;
1094     }
1095 
1096     if (arm_feature(env, ARM_FEATURE_EL2)) {
1097         /*
1098          * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1099          * current security state, so we don't use arm_mdcr_el2_eff() here.
1100          */
1101         bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1102         int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1103 
1104         if (counter >= hpmn) {
1105             return hlp;
1106         }
1107     }
1108     return env->cp15.c9_pmcr & PMCRLP;
1109 }
1110 
1111 /*
1112  * Ensure c15_ccnt is the guest-visible count so that operations such as
1113  * enabling/disabling the counter or filtering, modifying the count itself,
1114  * etc. can be done logically. This is essentially a no-op if the counter is
1115  * not enabled at the time of the call.
1116  */
pmccntr_op_start(CPUARMState * env)1117 static void pmccntr_op_start(CPUARMState *env)
1118 {
1119     uint64_t cycles = cycles_get_count(env);
1120 
1121     if (pmu_counter_enabled(env, 31)) {
1122         uint64_t eff_cycles = cycles;
1123         if (pmccntr_clockdiv_enabled(env)) {
1124             eff_cycles /= 64;
1125         }
1126 
1127         uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1128 
1129         uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1130                                  1ull << 63 : 1ull << 31;
1131         if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1132             env->cp15.c9_pmovsr |= (1ULL << 31);
1133             pmu_update_irq(env);
1134         }
1135 
1136         env->cp15.c15_ccnt = new_pmccntr;
1137     }
1138     env->cp15.c15_ccnt_delta = cycles;
1139 }
1140 
1141 /*
1142  * If PMCCNTR is enabled, recalculate the delta between the clock and the
1143  * guest-visible count. A call to pmccntr_op_finish should follow every call to
1144  * pmccntr_op_start.
1145  */
pmccntr_op_finish(CPUARMState * env)1146 static void pmccntr_op_finish(CPUARMState *env)
1147 {
1148     if (pmu_counter_enabled(env, 31)) {
1149 #ifndef CONFIG_USER_ONLY
1150         /* Calculate when the counter will next overflow */
1151         uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1152         if (!(env->cp15.c9_pmcr & PMCRLC)) {
1153             remaining_cycles = (uint32_t)remaining_cycles;
1154         }
1155         int64_t overflow_in = cycles_ns_per(remaining_cycles);
1156 
1157         if (overflow_in > 0) {
1158             int64_t overflow_at;
1159 
1160             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1161                                  overflow_in, &overflow_at)) {
1162                 ARMCPU *cpu = env_archcpu(env);
1163                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1164             }
1165         }
1166 #endif
1167 
1168         uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1169         if (pmccntr_clockdiv_enabled(env)) {
1170             prev_cycles /= 64;
1171         }
1172         env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1173     }
1174 }
1175 
pmevcntr_op_start(CPUARMState * env,uint8_t counter)1176 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1177 {
1178 
1179     uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1180     uint64_t count = 0;
1181     if (event_supported(event)) {
1182         uint16_t event_idx = supported_event_map[event];
1183         count = pm_events[event_idx].get_count(env);
1184     }
1185 
1186     if (pmu_counter_enabled(env, counter)) {
1187         uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1188         uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1189             1ULL << 63 : 1ULL << 31;
1190 
1191         if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1192             env->cp15.c9_pmovsr |= (1 << counter);
1193             pmu_update_irq(env);
1194         }
1195         env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1196     }
1197     env->cp15.c14_pmevcntr_delta[counter] = count;
1198 }
1199 
pmevcntr_op_finish(CPUARMState * env,uint8_t counter)1200 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1201 {
1202     if (pmu_counter_enabled(env, counter)) {
1203 #ifndef CONFIG_USER_ONLY
1204         uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1205         uint16_t event_idx = supported_event_map[event];
1206         uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1207         int64_t overflow_in;
1208 
1209         if (!pmevcntr_is_64_bit(env, counter)) {
1210             delta = (uint32_t)delta;
1211         }
1212         overflow_in = pm_events[event_idx].ns_per_count(delta);
1213 
1214         if (overflow_in > 0) {
1215             int64_t overflow_at;
1216 
1217             if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1218                                  overflow_in, &overflow_at)) {
1219                 ARMCPU *cpu = env_archcpu(env);
1220                 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1221             }
1222         }
1223 #endif
1224 
1225         env->cp15.c14_pmevcntr_delta[counter] -=
1226             env->cp15.c14_pmevcntr[counter];
1227     }
1228 }
1229 
pmu_op_start(CPUARMState * env)1230 void pmu_op_start(CPUARMState *env)
1231 {
1232     unsigned int i;
1233     pmccntr_op_start(env);
1234     for (i = 0; i < pmu_num_counters(env); i++) {
1235         pmevcntr_op_start(env, i);
1236     }
1237 }
1238 
pmu_op_finish(CPUARMState * env)1239 void pmu_op_finish(CPUARMState *env)
1240 {
1241     unsigned int i;
1242     pmccntr_op_finish(env);
1243     for (i = 0; i < pmu_num_counters(env); i++) {
1244         pmevcntr_op_finish(env, i);
1245     }
1246 }
1247 
pmu_pre_el_change(ARMCPU * cpu,void * ignored)1248 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1249 {
1250     pmu_op_start(&cpu->env);
1251 }
1252 
pmu_post_el_change(ARMCPU * cpu,void * ignored)1253 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1254 {
1255     pmu_op_finish(&cpu->env);
1256 }
1257 
arm_pmu_timer_cb(void * opaque)1258 void arm_pmu_timer_cb(void *opaque)
1259 {
1260     ARMCPU *cpu = opaque;
1261 
1262     /*
1263      * Update all the counter values based on the current underlying counts,
1264      * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1265      * has the effect of setting the cpu->pmu_timer to the next earliest time a
1266      * counter may expire.
1267      */
1268     pmu_op_start(&cpu->env);
1269     pmu_op_finish(&cpu->env);
1270 }
1271 
pmcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1272 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1273                        uint64_t value)
1274 {
1275     pmu_op_start(env);
1276 
1277     if (value & PMCRC) {
1278         /* The counter has been reset */
1279         env->cp15.c15_ccnt = 0;
1280     }
1281 
1282     if (value & PMCRP) {
1283         unsigned int i;
1284         for (i = 0; i < pmu_num_counters(env); i++) {
1285             env->cp15.c14_pmevcntr[i] = 0;
1286         }
1287     }
1288 
1289     env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1290     env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1291 
1292     pmu_op_finish(env);
1293 }
1294 
pmcr_read(CPUARMState * env,const ARMCPRegInfo * ri)1295 static uint64_t pmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1296 {
1297     uint64_t pmcr = env->cp15.c9_pmcr;
1298 
1299     /*
1300      * If EL2 is implemented and enabled for the current security state, reads
1301      * of PMCR.N from EL1 or EL0 return the value of MDCR_EL2.HPMN or HDCR.HPMN.
1302      */
1303     if (arm_current_el(env) <= 1 && arm_is_el2_enabled(env)) {
1304         pmcr &= ~PMCRN_MASK;
1305         pmcr |= (env->cp15.mdcr_el2 & MDCR_HPMN) << PMCRN_SHIFT;
1306     }
1307 
1308     return pmcr;
1309 }
1310 
pmswinc_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1311 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1312                           uint64_t value)
1313 {
1314     unsigned int i;
1315     uint64_t overflow_mask, new_pmswinc;
1316 
1317     for (i = 0; i < pmu_num_counters(env); i++) {
1318         /* Increment a counter's count iff: */
1319         if ((value & (1 << i)) && /* counter's bit is set */
1320                 /* counter is enabled and not filtered */
1321                 pmu_counter_enabled(env, i) &&
1322                 /* counter is SW_INCR */
1323                 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1324             pmevcntr_op_start(env, i);
1325 
1326             /*
1327              * Detect if this write causes an overflow since we can't predict
1328              * PMSWINC overflows like we can for other events
1329              */
1330             new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1331 
1332             overflow_mask = pmevcntr_is_64_bit(env, i) ?
1333                 1ULL << 63 : 1ULL << 31;
1334 
1335             if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1336                 env->cp15.c9_pmovsr |= (1 << i);
1337                 pmu_update_irq(env);
1338             }
1339 
1340             env->cp15.c14_pmevcntr[i] = new_pmswinc;
1341 
1342             pmevcntr_op_finish(env, i);
1343         }
1344     }
1345 }
1346 
pmccntr_read(CPUARMState * env,const ARMCPRegInfo * ri)1347 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1348 {
1349     uint64_t ret;
1350     pmccntr_op_start(env);
1351     ret = env->cp15.c15_ccnt;
1352     pmccntr_op_finish(env);
1353     return ret;
1354 }
1355 
pmselr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1356 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1357                          uint64_t value)
1358 {
1359     /*
1360      * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1361      * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1362      * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1363      * accessed.
1364      */
1365     env->cp15.c9_pmselr = value & 0x1f;
1366 }
1367 
pmccntr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1368 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1369                         uint64_t value)
1370 {
1371     pmccntr_op_start(env);
1372     env->cp15.c15_ccnt = value;
1373     pmccntr_op_finish(env);
1374 }
1375 
pmccntr_write32(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1376 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1377                             uint64_t value)
1378 {
1379     uint64_t cur_val = pmccntr_read(env, NULL);
1380 
1381     pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1382 }
1383 
pmccfiltr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1384 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1385                             uint64_t value)
1386 {
1387     pmccntr_op_start(env);
1388     env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1389     pmccntr_op_finish(env);
1390 }
1391 
pmccfiltr_write_a32(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1392 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1393                             uint64_t value)
1394 {
1395     pmccntr_op_start(env);
1396     /* M is not accessible from AArch32 */
1397     env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1398         (value & PMCCFILTR);
1399     pmccntr_op_finish(env);
1400 }
1401 
pmccfiltr_read_a32(CPUARMState * env,const ARMCPRegInfo * ri)1402 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1403 {
1404     /* M is not visible in AArch32 */
1405     return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1406 }
1407 
pmcntenset_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1408 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1409                             uint64_t value)
1410 {
1411     pmu_op_start(env);
1412     value &= pmu_counter_mask(env);
1413     env->cp15.c9_pmcnten |= value;
1414     pmu_op_finish(env);
1415 }
1416 
pmcntenclr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1417 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1418                              uint64_t value)
1419 {
1420     pmu_op_start(env);
1421     value &= pmu_counter_mask(env);
1422     env->cp15.c9_pmcnten &= ~value;
1423     pmu_op_finish(env);
1424 }
1425 
pmovsr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1426 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1427                          uint64_t value)
1428 {
1429     value &= pmu_counter_mask(env);
1430     env->cp15.c9_pmovsr &= ~value;
1431     pmu_update_irq(env);
1432 }
1433 
pmovsset_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1434 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1435                          uint64_t value)
1436 {
1437     value &= pmu_counter_mask(env);
1438     env->cp15.c9_pmovsr |= value;
1439     pmu_update_irq(env);
1440 }
1441 
pmevtyper_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value,const uint8_t counter)1442 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1443                              uint64_t value, const uint8_t counter)
1444 {
1445     if (counter == 31) {
1446         pmccfiltr_write(env, ri, value);
1447     } else if (counter < pmu_num_counters(env)) {
1448         pmevcntr_op_start(env, counter);
1449 
1450         /*
1451          * If this counter's event type is changing, store the current
1452          * underlying count for the new type in c14_pmevcntr_delta[counter] so
1453          * pmevcntr_op_finish has the correct baseline when it converts back to
1454          * a delta.
1455          */
1456         uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1457             PMXEVTYPER_EVTCOUNT;
1458         uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1459         if (old_event != new_event) {
1460             uint64_t count = 0;
1461             if (event_supported(new_event)) {
1462                 uint16_t event_idx = supported_event_map[new_event];
1463                 count = pm_events[event_idx].get_count(env);
1464             }
1465             env->cp15.c14_pmevcntr_delta[counter] = count;
1466         }
1467 
1468         env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1469         pmevcntr_op_finish(env, counter);
1470     }
1471     /*
1472      * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1473      * PMSELR value is equal to or greater than the number of implemented
1474      * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1475      */
1476 }
1477 
pmevtyper_read(CPUARMState * env,const ARMCPRegInfo * ri,const uint8_t counter)1478 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1479                                const uint8_t counter)
1480 {
1481     if (counter == 31) {
1482         return env->cp15.pmccfiltr_el0;
1483     } else if (counter < pmu_num_counters(env)) {
1484         return env->cp15.c14_pmevtyper[counter];
1485     } else {
1486       /*
1487        * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1488        * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1489        */
1490         return 0;
1491     }
1492 }
1493 
pmevtyper_writefn(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1494 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1495                               uint64_t value)
1496 {
1497     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1498     pmevtyper_write(env, ri, value, counter);
1499 }
1500 
pmevtyper_rawwrite(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1501 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1502                                uint64_t value)
1503 {
1504     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1505     env->cp15.c14_pmevtyper[counter] = value;
1506 
1507     /*
1508      * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1509      * pmu_op_finish calls when loading saved state for a migration. Because
1510      * we're potentially updating the type of event here, the value written to
1511      * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a
1512      * different counter type. Therefore, we need to set this value to the
1513      * current count for the counter type we're writing so that pmu_op_finish
1514      * has the correct count for its calculation.
1515      */
1516     uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1517     if (event_supported(event)) {
1518         uint16_t event_idx = supported_event_map[event];
1519         env->cp15.c14_pmevcntr_delta[counter] =
1520             pm_events[event_idx].get_count(env);
1521     }
1522 }
1523 
pmevtyper_readfn(CPUARMState * env,const ARMCPRegInfo * ri)1524 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1525 {
1526     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1527     return pmevtyper_read(env, ri, counter);
1528 }
1529 
pmxevtyper_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1530 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1531                              uint64_t value)
1532 {
1533     pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1534 }
1535 
pmxevtyper_read(CPUARMState * env,const ARMCPRegInfo * ri)1536 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1537 {
1538     return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1539 }
1540 
pmevcntr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value,uint8_t counter)1541 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1542                              uint64_t value, uint8_t counter)
1543 {
1544     if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1545         /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1546         value &= MAKE_64BIT_MASK(0, 32);
1547     }
1548     if (counter < pmu_num_counters(env)) {
1549         pmevcntr_op_start(env, counter);
1550         env->cp15.c14_pmevcntr[counter] = value;
1551         pmevcntr_op_finish(env, counter);
1552     }
1553     /*
1554      * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1555      * are CONSTRAINED UNPREDICTABLE.
1556      */
1557 }
1558 
pmevcntr_read(CPUARMState * env,const ARMCPRegInfo * ri,uint8_t counter)1559 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1560                               uint8_t counter)
1561 {
1562     if (counter < pmu_num_counters(env)) {
1563         uint64_t ret;
1564         pmevcntr_op_start(env, counter);
1565         ret = env->cp15.c14_pmevcntr[counter];
1566         pmevcntr_op_finish(env, counter);
1567         if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1568             /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1569             ret &= MAKE_64BIT_MASK(0, 32);
1570         }
1571         return ret;
1572     } else {
1573       /*
1574        * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1575        * are CONSTRAINED UNPREDICTABLE.
1576        */
1577         return 0;
1578     }
1579 }
1580 
pmevcntr_writefn(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1581 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1582                              uint64_t value)
1583 {
1584     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1585     pmevcntr_write(env, ri, value, counter);
1586 }
1587 
pmevcntr_readfn(CPUARMState * env,const ARMCPRegInfo * ri)1588 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1589 {
1590     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1591     return pmevcntr_read(env, ri, counter);
1592 }
1593 
pmevcntr_rawwrite(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1594 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1595                              uint64_t value)
1596 {
1597     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1598     assert(counter < pmu_num_counters(env));
1599     env->cp15.c14_pmevcntr[counter] = value;
1600     pmevcntr_write(env, ri, value, counter);
1601 }
1602 
pmevcntr_rawread(CPUARMState * env,const ARMCPRegInfo * ri)1603 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1604 {
1605     uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1606     assert(counter < pmu_num_counters(env));
1607     return env->cp15.c14_pmevcntr[counter];
1608 }
1609 
pmxevcntr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1610 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1611                              uint64_t value)
1612 {
1613     pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1614 }
1615 
pmxevcntr_read(CPUARMState * env,const ARMCPRegInfo * ri)1616 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1617 {
1618     return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1619 }
1620 
pmuserenr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1621 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1622                             uint64_t value)
1623 {
1624     if (arm_feature(env, ARM_FEATURE_V8)) {
1625         env->cp15.c9_pmuserenr = value & 0xf;
1626     } else {
1627         env->cp15.c9_pmuserenr = value & 1;
1628     }
1629 }
1630 
pmintenset_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1631 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1632                              uint64_t value)
1633 {
1634     /* We have no event counters so only the C bit can be changed */
1635     value &= pmu_counter_mask(env);
1636     env->cp15.c9_pminten |= value;
1637     pmu_update_irq(env);
1638 }
1639 
pmintenclr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1640 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1641                              uint64_t value)
1642 {
1643     value &= pmu_counter_mask(env);
1644     env->cp15.c9_pminten &= ~value;
1645     pmu_update_irq(env);
1646 }
1647 
vbar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1648 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1649                        uint64_t value)
1650 {
1651     /*
1652      * Note that even though the AArch64 view of this register has bits
1653      * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1654      * architectural requirements for bits which are RES0 only in some
1655      * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1656      * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1657      */
1658     raw_write(env, ri, value & ~0x1FULL);
1659 }
1660 
scr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1661 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1662 {
1663     /* Begin with base v8.0 state.  */
1664     uint64_t valid_mask = 0x3fff;
1665     ARMCPU *cpu = env_archcpu(env);
1666     uint64_t changed;
1667 
1668     /*
1669      * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1670      * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1671      * Instead, choose the format based on the mode of EL3.
1672      */
1673     if (arm_el_is_aa64(env, 3)) {
1674         value |= SCR_FW | SCR_AW;      /* RES1 */
1675         valid_mask &= ~SCR_NET;        /* RES0 */
1676 
1677         if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1678             !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1679             value |= SCR_RW;           /* RAO/WI */
1680         }
1681         if (cpu_isar_feature(aa64_ras, cpu)) {
1682             valid_mask |= SCR_TERR;
1683         }
1684         if (cpu_isar_feature(aa64_lor, cpu)) {
1685             valid_mask |= SCR_TLOR;
1686         }
1687         if (cpu_isar_feature(aa64_pauth, cpu)) {
1688             valid_mask |= SCR_API | SCR_APK;
1689         }
1690         if (cpu_isar_feature(aa64_sel2, cpu)) {
1691             valid_mask |= SCR_EEL2;
1692         } else if (cpu_isar_feature(aa64_rme, cpu)) {
1693             /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */
1694             value |= SCR_NS;
1695         }
1696         if (cpu_isar_feature(aa64_mte, cpu)) {
1697             valid_mask |= SCR_ATA;
1698         }
1699         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1700             valid_mask |= SCR_ENSCXT;
1701         }
1702         if (cpu_isar_feature(aa64_doublefault, cpu)) {
1703             valid_mask |= SCR_EASE | SCR_NMEA;
1704         }
1705         if (cpu_isar_feature(aa64_sme, cpu)) {
1706             valid_mask |= SCR_ENTP2;
1707         }
1708         if (cpu_isar_feature(aa64_hcx, cpu)) {
1709             valid_mask |= SCR_HXEN;
1710         }
1711         if (cpu_isar_feature(aa64_fgt, cpu)) {
1712             valid_mask |= SCR_FGTEN;
1713         }
1714         if (cpu_isar_feature(aa64_rme, cpu)) {
1715             valid_mask |= SCR_NSE | SCR_GPF;
1716         }
1717         if (cpu_isar_feature(aa64_ecv, cpu)) {
1718             valid_mask |= SCR_ECVEN;
1719         }
1720     } else {
1721         valid_mask &= ~(SCR_RW | SCR_ST);
1722         if (cpu_isar_feature(aa32_ras, cpu)) {
1723             valid_mask |= SCR_TERR;
1724         }
1725     }
1726 
1727     if (!arm_feature(env, ARM_FEATURE_EL2)) {
1728         valid_mask &= ~SCR_HCE;
1729 
1730         /*
1731          * On ARMv7, SMD (or SCD as it is called in v7) is only
1732          * supported if EL2 exists. The bit is UNK/SBZP when
1733          * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1734          * when EL2 is unavailable.
1735          * On ARMv8, this bit is always available.
1736          */
1737         if (arm_feature(env, ARM_FEATURE_V7) &&
1738             !arm_feature(env, ARM_FEATURE_V8)) {
1739             valid_mask &= ~SCR_SMD;
1740         }
1741     }
1742 
1743     /* Clear all-context RES0 bits.  */
1744     value &= valid_mask;
1745     changed = env->cp15.scr_el3 ^ value;
1746     env->cp15.scr_el3 = value;
1747 
1748     /*
1749      * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
1750      * we must invalidate all TLBs below EL3.
1751      */
1752     if (changed & (SCR_NS | SCR_NSE)) {
1753         tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1754                                            ARMMMUIdxBit_E20_0 |
1755                                            ARMMMUIdxBit_E10_1 |
1756                                            ARMMMUIdxBit_E20_2 |
1757                                            ARMMMUIdxBit_E10_1_PAN |
1758                                            ARMMMUIdxBit_E20_2_PAN |
1759                                            ARMMMUIdxBit_E2));
1760     }
1761 }
1762 
scr_reset(CPUARMState * env,const ARMCPRegInfo * ri)1763 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1764 {
1765     /*
1766      * scr_write will set the RES1 bits on an AArch64-only CPU.
1767      * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1768      */
1769     scr_write(env, ri, 0);
1770 }
1771 
access_tid4(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)1772 static CPAccessResult access_tid4(CPUARMState *env,
1773                                   const ARMCPRegInfo *ri,
1774                                   bool isread)
1775 {
1776     if (arm_current_el(env) == 1 &&
1777         (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1778         return CP_ACCESS_TRAP_EL2;
1779     }
1780 
1781     return CP_ACCESS_OK;
1782 }
1783 
ccsidr_read(CPUARMState * env,const ARMCPRegInfo * ri)1784 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1785 {
1786     ARMCPU *cpu = env_archcpu(env);
1787 
1788     /*
1789      * Acquire the CSSELR index from the bank corresponding to the CCSIDR
1790      * bank
1791      */
1792     uint32_t index = A32_BANKED_REG_GET(env, csselr,
1793                                         ri->secure & ARM_CP_SECSTATE_S);
1794 
1795     return cpu->ccsidr[index];
1796 }
1797 
csselr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1798 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1799                          uint64_t value)
1800 {
1801     raw_write(env, ri, value & 0xf);
1802 }
1803 
isr_read(CPUARMState * env,const ARMCPRegInfo * ri)1804 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1805 {
1806     CPUState *cs = env_cpu(env);
1807     bool el1 = arm_current_el(env) == 1;
1808     uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1809     uint64_t ret = 0;
1810 
1811     if (hcr_el2 & HCR_IMO) {
1812         if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1813             ret |= CPSR_I;
1814         }
1815         if (cs->interrupt_request & CPU_INTERRUPT_VINMI) {
1816             ret |= ISR_IS;
1817             ret |= CPSR_I;
1818         }
1819     } else {
1820         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1821             ret |= CPSR_I;
1822         }
1823 
1824         if (cs->interrupt_request & CPU_INTERRUPT_NMI) {
1825             ret |= ISR_IS;
1826             ret |= CPSR_I;
1827         }
1828     }
1829 
1830     if (hcr_el2 & HCR_FMO) {
1831         if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1832             ret |= CPSR_F;
1833         }
1834         if (cs->interrupt_request & CPU_INTERRUPT_VFNMI) {
1835             ret |= ISR_FS;
1836             ret |= CPSR_F;
1837         }
1838     } else {
1839         if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1840             ret |= CPSR_F;
1841         }
1842     }
1843 
1844     if (hcr_el2 & HCR_AMO) {
1845         if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1846             ret |= CPSR_A;
1847         }
1848     }
1849 
1850     return ret;
1851 }
1852 
access_aa64_tid1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)1853 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1854                                        bool isread)
1855 {
1856     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1857         return CP_ACCESS_TRAP_EL2;
1858     }
1859 
1860     return CP_ACCESS_OK;
1861 }
1862 
access_aa32_tid1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)1863 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1864                                        bool isread)
1865 {
1866     if (arm_feature(env, ARM_FEATURE_V8)) {
1867         return access_aa64_tid1(env, ri, isread);
1868     }
1869 
1870     return CP_ACCESS_OK;
1871 }
1872 
1873 static const ARMCPRegInfo v7_cp_reginfo[] = {
1874     /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1875     { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1876       .access = PL1_W, .type = ARM_CP_NOP },
1877     /*
1878      * Performance monitors are implementation defined in v7,
1879      * but with an ARM recommended set of registers, which we
1880      * follow.
1881      *
1882      * Performance registers fall into three categories:
1883      *  (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1884      *  (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1885      *  (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1886      * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1887      * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1888      */
1889     { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1890       .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
1891       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1892       .writefn = pmcntenset_write,
1893       .accessfn = pmreg_access,
1894       .fgt = FGT_PMCNTEN,
1895       .raw_writefn = raw_write },
1896     { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
1897       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1898       .access = PL0_RW, .accessfn = pmreg_access,
1899       .fgt = FGT_PMCNTEN,
1900       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1901       .writefn = pmcntenset_write, .raw_writefn = raw_write },
1902     { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1903       .access = PL0_RW,
1904       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1905       .accessfn = pmreg_access,
1906       .fgt = FGT_PMCNTEN,
1907       .writefn = pmcntenclr_write,
1908       .type = ARM_CP_ALIAS | ARM_CP_IO },
1909     { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1910       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1911       .access = PL0_RW, .accessfn = pmreg_access,
1912       .fgt = FGT_PMCNTEN,
1913       .type = ARM_CP_ALIAS | ARM_CP_IO,
1914       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1915       .writefn = pmcntenclr_write },
1916     { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1917       .access = PL0_RW, .type = ARM_CP_IO,
1918       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
1919       .accessfn = pmreg_access,
1920       .fgt = FGT_PMOVS,
1921       .writefn = pmovsr_write,
1922       .raw_writefn = raw_write },
1923     { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1924       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1925       .access = PL0_RW, .accessfn = pmreg_access,
1926       .fgt = FGT_PMOVS,
1927       .type = ARM_CP_ALIAS | ARM_CP_IO,
1928       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1929       .writefn = pmovsr_write,
1930       .raw_writefn = raw_write },
1931     { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1932       .access = PL0_W, .accessfn = pmreg_access_swinc,
1933       .fgt = FGT_PMSWINC_EL0,
1934       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1935       .writefn = pmswinc_write },
1936     { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1937       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
1938       .access = PL0_W, .accessfn = pmreg_access_swinc,
1939       .fgt = FGT_PMSWINC_EL0,
1940       .type = ARM_CP_NO_RAW | ARM_CP_IO,
1941       .writefn = pmswinc_write },
1942     { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1943       .access = PL0_RW, .type = ARM_CP_ALIAS,
1944       .fgt = FGT_PMSELR_EL0,
1945       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1946       .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1947       .raw_writefn = raw_write},
1948     { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1949       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1950       .access = PL0_RW, .accessfn = pmreg_access_selr,
1951       .fgt = FGT_PMSELR_EL0,
1952       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1953       .writefn = pmselr_write, .raw_writefn = raw_write, },
1954     { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1955       .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
1956       .fgt = FGT_PMCCNTR_EL0,
1957       .readfn = pmccntr_read, .writefn = pmccntr_write32,
1958       .accessfn = pmreg_access_ccntr },
1959     { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1960       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1961       .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1962       .fgt = FGT_PMCCNTR_EL0,
1963       .type = ARM_CP_IO,
1964       .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1965       .readfn = pmccntr_read, .writefn = pmccntr_write,
1966       .raw_readfn = raw_read, .raw_writefn = raw_write, },
1967     { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1968       .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
1969       .access = PL0_RW, .accessfn = pmreg_access,
1970       .fgt = FGT_PMCCFILTR_EL0,
1971       .type = ARM_CP_ALIAS | ARM_CP_IO,
1972       .resetvalue = 0, },
1973     { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1974       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
1975       .writefn = pmccfiltr_write, .raw_writefn = raw_write,
1976       .access = PL0_RW, .accessfn = pmreg_access,
1977       .fgt = FGT_PMCCFILTR_EL0,
1978       .type = ARM_CP_IO,
1979       .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1980       .resetvalue = 0, },
1981     { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1982       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1983       .accessfn = pmreg_access,
1984       .fgt = FGT_PMEVTYPERN_EL0,
1985       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1986     { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1987       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1988       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1989       .accessfn = pmreg_access,
1990       .fgt = FGT_PMEVTYPERN_EL0,
1991       .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1992     { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
1993       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1994       .accessfn = pmreg_access_xevcntr,
1995       .fgt = FGT_PMEVCNTRN_EL0,
1996       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
1997     { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
1998       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
1999       .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2000       .accessfn = pmreg_access_xevcntr,
2001       .fgt = FGT_PMEVCNTRN_EL0,
2002       .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2003     { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
2004       .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2005       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2006       .resetvalue = 0,
2007       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2008     { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2009       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2010       .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2011       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2012       .resetvalue = 0,
2013       .writefn = pmuserenr_write, .raw_writefn = raw_write },
2014     { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2015       .access = PL1_RW, .accessfn = access_tpm,
2016       .fgt = FGT_PMINTEN,
2017       .type = ARM_CP_ALIAS | ARM_CP_IO,
2018       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2019       .resetvalue = 0,
2020       .writefn = pmintenset_write, .raw_writefn = raw_write },
2021     { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2022       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2023       .access = PL1_RW, .accessfn = access_tpm,
2024       .fgt = FGT_PMINTEN,
2025       .type = ARM_CP_IO,
2026       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2027       .writefn = pmintenset_write, .raw_writefn = raw_write,
2028       .resetvalue = 0x0 },
2029     { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2030       .access = PL1_RW, .accessfn = access_tpm,
2031       .fgt = FGT_PMINTEN,
2032       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2033       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2034       .writefn = pmintenclr_write, },
2035     { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2036       .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2037       .access = PL1_RW, .accessfn = access_tpm,
2038       .fgt = FGT_PMINTEN,
2039       .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2040       .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2041       .writefn = pmintenclr_write },
2042     { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2043       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2044       .access = PL1_R,
2045       .accessfn = access_tid4,
2046       .fgt = FGT_CCSIDR_EL1,
2047       .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2048     { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2049       .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2050       .access = PL1_RW,
2051       .accessfn = access_tid4,
2052       .fgt = FGT_CSSELR_EL1,
2053       .writefn = csselr_write, .resetvalue = 0,
2054       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2055                              offsetof(CPUARMState, cp15.csselr_ns) } },
2056     /*
2057      * Auxiliary ID register: this actually has an IMPDEF value but for now
2058      * just RAZ for all cores:
2059      */
2060     { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2061       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2062       .access = PL1_R, .type = ARM_CP_CONST,
2063       .accessfn = access_aa64_tid1,
2064       .fgt = FGT_AIDR_EL1,
2065       .resetvalue = 0 },
2066     /*
2067      * Auxiliary fault status registers: these also are IMPDEF, and we
2068      * choose to RAZ/WI for all cores.
2069      */
2070     { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2071       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2072       .access = PL1_RW, .accessfn = access_tvm_trvm,
2073       .fgt = FGT_AFSR0_EL1,
2074       .nv2_redirect_offset = 0x128 | NV2_REDIR_NV1,
2075       .type = ARM_CP_CONST, .resetvalue = 0 },
2076     { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2077       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2078       .access = PL1_RW, .accessfn = access_tvm_trvm,
2079       .fgt = FGT_AFSR1_EL1,
2080       .nv2_redirect_offset = 0x130 | NV2_REDIR_NV1,
2081       .type = ARM_CP_CONST, .resetvalue = 0 },
2082     /*
2083      * MAIR can just read-as-written because we don't implement caches
2084      * and so don't need to care about memory attributes.
2085      */
2086     { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2087       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2088       .access = PL1_RW, .accessfn = access_tvm_trvm,
2089       .fgt = FGT_MAIR_EL1,
2090       .nv2_redirect_offset = 0x140 | NV2_REDIR_NV1,
2091       .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2092       .resetvalue = 0 },
2093     { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2094       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2095       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2096       .resetvalue = 0 },
2097     /*
2098      * For non-long-descriptor page tables these are PRRR and NMRR;
2099      * regardless they still act as reads-as-written for QEMU.
2100      */
2101      /*
2102       * MAIR0/1 are defined separately from their 64-bit counterpart which
2103       * allows them to assign the correct fieldoffset based on the endianness
2104       * handled in the field definitions.
2105       */
2106     { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2107       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2108       .access = PL1_RW, .accessfn = access_tvm_trvm,
2109       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2110                              offsetof(CPUARMState, cp15.mair0_ns) },
2111       .resetfn = arm_cp_reset_ignore },
2112     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2113       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2114       .access = PL1_RW, .accessfn = access_tvm_trvm,
2115       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2116                              offsetof(CPUARMState, cp15.mair1_ns) },
2117       .resetfn = arm_cp_reset_ignore },
2118     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2119       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2120       .fgt = FGT_ISR_EL1,
2121       .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2122 };
2123 
2124 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2125     /* PMOVSSET is not implemented in v7 before v7ve */
2126     { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2127       .access = PL0_RW, .accessfn = pmreg_access,
2128       .fgt = FGT_PMOVS,
2129       .type = ARM_CP_ALIAS | ARM_CP_IO,
2130       .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2131       .writefn = pmovsset_write,
2132       .raw_writefn = raw_write },
2133     { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2134       .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2135       .access = PL0_RW, .accessfn = pmreg_access,
2136       .fgt = FGT_PMOVS,
2137       .type = ARM_CP_ALIAS | ARM_CP_IO,
2138       .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2139       .writefn = pmovsset_write,
2140       .raw_writefn = raw_write },
2141 };
2142 
teecr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2143 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2144                         uint64_t value)
2145 {
2146     value &= 1;
2147     env->teecr = value;
2148 }
2149 
teecr_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2150 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2151                                    bool isread)
2152 {
2153     /*
2154      * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2155      * at all, so we don't need to check whether we're v8A.
2156      */
2157     if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2158         (env->cp15.hstr_el2 & HSTR_TTEE)) {
2159         return CP_ACCESS_TRAP_EL2;
2160     }
2161     return CP_ACCESS_OK;
2162 }
2163 
teehbr_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2164 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2165                                     bool isread)
2166 {
2167     if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2168         return CP_ACCESS_TRAP_EL1;
2169     }
2170     return teecr_access(env, ri, isread);
2171 }
2172 
2173 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2174     { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2175       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2176       .resetvalue = 0,
2177       .writefn = teecr_write, .accessfn = teecr_access },
2178     { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2179       .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2180       .accessfn = teehbr_access, .resetvalue = 0 },
2181 };
2182 
2183 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2184     { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2185       .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2186       .access = PL0_RW,
2187       .fgt = FGT_TPIDR_EL0,
2188       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2189     { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2190       .access = PL0_RW,
2191       .fgt = FGT_TPIDR_EL0,
2192       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2193                              offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2194       .resetfn = arm_cp_reset_ignore },
2195     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2196       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2197       .access = PL0_R | PL1_W,
2198       .fgt = FGT_TPIDRRO_EL0,
2199       .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2200       .resetvalue = 0},
2201     { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2202       .access = PL0_R | PL1_W,
2203       .fgt = FGT_TPIDRRO_EL0,
2204       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2205                              offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2206       .resetfn = arm_cp_reset_ignore },
2207     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2208       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2209       .access = PL1_RW,
2210       .fgt = FGT_TPIDR_EL1,
2211       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2212     { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2213       .access = PL1_RW,
2214       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2215                              offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2216       .resetvalue = 0 },
2217 };
2218 
arm_gt_cntfrq_reset(CPUARMState * env,const ARMCPRegInfo * opaque)2219 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2220 {
2221     ARMCPU *cpu = env_archcpu(env);
2222 
2223     cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2224 }
2225 
2226 #ifndef CONFIG_USER_ONLY
2227 
gt_cntfrq_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2228 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2229                                        bool isread)
2230 {
2231     /*
2232      * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2233      * Writable only at the highest implemented exception level.
2234      */
2235     int el = arm_current_el(env);
2236     uint64_t hcr;
2237     uint32_t cntkctl;
2238 
2239     switch (el) {
2240     case 0:
2241         hcr = arm_hcr_el2_eff(env);
2242         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2243             cntkctl = env->cp15.cnthctl_el2;
2244         } else {
2245             cntkctl = env->cp15.c14_cntkctl;
2246         }
2247         if (!extract32(cntkctl, 0, 2)) {
2248             return CP_ACCESS_TRAP_EL1;
2249         }
2250         break;
2251     case 1:
2252         if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2253             arm_is_secure_below_el3(env)) {
2254             /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2255             return CP_ACCESS_UNDEFINED;
2256         }
2257         break;
2258     case 2:
2259     case 3:
2260         break;
2261     }
2262 
2263     if (!isread && el < arm_highest_el(env)) {
2264         return CP_ACCESS_UNDEFINED;
2265     }
2266 
2267     return CP_ACCESS_OK;
2268 }
2269 
gt_counter_access(CPUARMState * env,int timeridx,bool isread)2270 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2271                                         bool isread)
2272 {
2273     unsigned int cur_el = arm_current_el(env);
2274     bool has_el2 = arm_is_el2_enabled(env);
2275     uint64_t hcr = arm_hcr_el2_eff(env);
2276 
2277     switch (cur_el) {
2278     case 0:
2279         /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2280         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2281             return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2282                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2283         }
2284 
2285         /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2286         if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2287             return CP_ACCESS_TRAP_EL1;
2288         }
2289         /* fall through */
2290     case 1:
2291         /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2292         if (has_el2 && timeridx == GTIMER_PHYS &&
2293             (hcr & HCR_E2H
2294              ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2295              : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2296             return CP_ACCESS_TRAP_EL2;
2297         }
2298         if (has_el2 && timeridx == GTIMER_VIRT) {
2299             if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVCT)) {
2300                 return CP_ACCESS_TRAP_EL2;
2301             }
2302         }
2303         break;
2304     }
2305     return CP_ACCESS_OK;
2306 }
2307 
gt_timer_access(CPUARMState * env,int timeridx,bool isread)2308 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2309                                       bool isread)
2310 {
2311     unsigned int cur_el = arm_current_el(env);
2312     bool has_el2 = arm_is_el2_enabled(env);
2313     uint64_t hcr = arm_hcr_el2_eff(env);
2314 
2315     switch (cur_el) {
2316     case 0:
2317         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2318             /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2319             return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2320                     ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2321         }
2322 
2323         /*
2324          * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2325          * EL0 if EL0[PV]TEN is zero.
2326          */
2327         if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2328             return CP_ACCESS_TRAP_EL1;
2329         }
2330         /* fall through */
2331 
2332     case 1:
2333         if (has_el2 && timeridx == GTIMER_PHYS) {
2334             if (hcr & HCR_E2H) {
2335                 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2336                 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2337                     return CP_ACCESS_TRAP_EL2;
2338                 }
2339             } else {
2340                 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2341                 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2342                     return CP_ACCESS_TRAP_EL2;
2343                 }
2344             }
2345         }
2346         if (has_el2 && timeridx == GTIMER_VIRT) {
2347             if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVT)) {
2348                 return CP_ACCESS_TRAP_EL2;
2349             }
2350         }
2351         break;
2352     }
2353     return CP_ACCESS_OK;
2354 }
2355 
gt_pct_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2356 static CPAccessResult gt_pct_access(CPUARMState *env,
2357                                     const ARMCPRegInfo *ri,
2358                                     bool isread)
2359 {
2360     return gt_counter_access(env, GTIMER_PHYS, isread);
2361 }
2362 
gt_vct_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2363 static CPAccessResult gt_vct_access(CPUARMState *env,
2364                                     const ARMCPRegInfo *ri,
2365                                     bool isread)
2366 {
2367     return gt_counter_access(env, GTIMER_VIRT, isread);
2368 }
2369 
gt_ptimer_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2370 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2371                                        bool isread)
2372 {
2373     return gt_timer_access(env, GTIMER_PHYS, isread);
2374 }
2375 
gt_vtimer_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2376 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2377                                        bool isread)
2378 {
2379     return gt_timer_access(env, GTIMER_VIRT, isread);
2380 }
2381 
gt_stimer_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2382 static CPAccessResult gt_stimer_access(CPUARMState *env,
2383                                        const ARMCPRegInfo *ri,
2384                                        bool isread)
2385 {
2386     /*
2387      * The AArch64 register view of the secure physical timer is
2388      * always accessible from EL3, and configurably accessible from
2389      * Secure EL1.
2390      */
2391     switch (arm_current_el(env)) {
2392     case 1:
2393         if (!arm_is_secure(env)) {
2394             return CP_ACCESS_UNDEFINED;
2395         }
2396         if (arm_is_el2_enabled(env)) {
2397             return CP_ACCESS_UNDEFINED;
2398         }
2399         if (!(env->cp15.scr_el3 & SCR_ST)) {
2400             return CP_ACCESS_TRAP_EL3;
2401         }
2402         return CP_ACCESS_OK;
2403     case 0:
2404     case 2:
2405         return CP_ACCESS_UNDEFINED;
2406     case 3:
2407         return CP_ACCESS_OK;
2408     default:
2409         g_assert_not_reached();
2410     }
2411 }
2412 
gt_sel2timer_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2413 static CPAccessResult gt_sel2timer_access(CPUARMState *env,
2414                                           const ARMCPRegInfo *ri,
2415                                           bool isread)
2416 {
2417     /*
2418      * The AArch64 register view of the secure EL2 timers are mostly
2419      * accessible from EL3 and EL2 although can also be trapped to EL2
2420      * from EL1 depending on nested virt config.
2421      */
2422     switch (arm_current_el(env)) {
2423     case 0: /* UNDEFINED */
2424         return CP_ACCESS_UNDEFINED;
2425     case 1:
2426         if (!arm_is_secure(env)) {
2427             /* UNDEFINED */
2428             return CP_ACCESS_UNDEFINED;
2429         } else if (arm_hcr_el2_eff(env) & HCR_NV) {
2430             /* Aarch64.SystemAccessTrap(EL2, 0x18) */
2431             return CP_ACCESS_TRAP_EL2;
2432         }
2433         /* UNDEFINED */
2434         return CP_ACCESS_UNDEFINED;
2435     case 2:
2436         if (!arm_is_secure(env)) {
2437             /* UNDEFINED */
2438             return CP_ACCESS_UNDEFINED;
2439         }
2440         return CP_ACCESS_OK;
2441     case 3:
2442         if (env->cp15.scr_el3 & SCR_EEL2) {
2443             return CP_ACCESS_OK;
2444         } else {
2445             return CP_ACCESS_UNDEFINED;
2446         }
2447     default:
2448         g_assert_not_reached();
2449     }
2450 }
2451 
gt_get_countervalue(CPUARMState * env)2452 uint64_t gt_get_countervalue(CPUARMState *env)
2453 {
2454     ARMCPU *cpu = env_archcpu(env);
2455 
2456     return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2457 }
2458 
gt_update_irq(ARMCPU * cpu,int timeridx)2459 static void gt_update_irq(ARMCPU *cpu, int timeridx)
2460 {
2461     CPUARMState *env = &cpu->env;
2462     uint64_t cnthctl = env->cp15.cnthctl_el2;
2463     ARMSecuritySpace ss = arm_security_space(env);
2464     /* ISTATUS && !IMASK */
2465     int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2466 
2467     /*
2468      * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2469      * It is RES0 in Secure and NonSecure state.
2470      */
2471     if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2472         ((timeridx == GTIMER_VIRT && (cnthctl & R_CNTHCTL_CNTVMASK_MASK)) ||
2473          (timeridx == GTIMER_PHYS && (cnthctl & R_CNTHCTL_CNTPMASK_MASK)))) {
2474         irqstate = 0;
2475     }
2476 
2477     qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2478     trace_arm_gt_update_irq(timeridx, irqstate);
2479 }
2480 
gt_rme_post_el_change(ARMCPU * cpu,void * ignored)2481 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2482 {
2483     /*
2484      * Changing security state between Root and Secure/NonSecure, which may
2485      * happen when switching EL, can change the effective value of CNTHCTL_EL2
2486      * mask bits. Update the IRQ state accordingly.
2487      */
2488     gt_update_irq(cpu, GTIMER_VIRT);
2489     gt_update_irq(cpu, GTIMER_PHYS);
2490 }
2491 
gt_phys_raw_cnt_offset(CPUARMState * env)2492 static uint64_t gt_phys_raw_cnt_offset(CPUARMState *env)
2493 {
2494     if ((env->cp15.scr_el3 & SCR_ECVEN) &&
2495         FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, ECV) &&
2496         arm_is_el2_enabled(env) &&
2497         (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
2498         return env->cp15.cntpoff_el2;
2499     }
2500     return 0;
2501 }
2502 
gt_indirect_access_timer_offset(CPUARMState * env,int timeridx)2503 static uint64_t gt_indirect_access_timer_offset(CPUARMState *env, int timeridx)
2504 {
2505     /*
2506      * Return the timer offset to use for indirect accesses to the timer.
2507      * This is the Offset value as defined in D12.2.4.1 "Operation of the
2508      * CompareValue views of the timers".
2509      *
2510      * The condition here is not always the same as the condition for
2511      * whether to apply an offset register when doing a direct read of
2512      * the counter sysreg; those conditions are described in the
2513      * access pseudocode for each counter register.
2514      */
2515     switch (timeridx) {
2516     case GTIMER_PHYS:
2517         return gt_phys_raw_cnt_offset(env);
2518     case GTIMER_VIRT:
2519         return env->cp15.cntvoff_el2;
2520     case GTIMER_HYP:
2521     case GTIMER_SEC:
2522     case GTIMER_HYPVIRT:
2523     case GTIMER_S_EL2_PHYS:
2524     case GTIMER_S_EL2_VIRT:
2525         return 0;
2526     default:
2527         g_assert_not_reached();
2528     }
2529 }
2530 
gt_direct_access_timer_offset(CPUARMState * env,int timeridx)2531 uint64_t gt_direct_access_timer_offset(CPUARMState *env, int timeridx)
2532 {
2533     /*
2534      * Return the timer offset to use for direct accesses to the
2535      * counter registers CNTPCT and CNTVCT, and for direct accesses
2536      * to the CNT*_TVAL registers.
2537      *
2538      * This isn't exactly the same as the indirect-access offset,
2539      * because here we also care about what EL the register access
2540      * is being made from.
2541      *
2542      * This corresponds to the access pseudocode for the registers.
2543      */
2544     uint64_t hcr;
2545 
2546     switch (timeridx) {
2547     case GTIMER_PHYS:
2548         if (arm_current_el(env) >= 2) {
2549             return 0;
2550         }
2551         return gt_phys_raw_cnt_offset(env);
2552     case GTIMER_VIRT:
2553         switch (arm_current_el(env)) {
2554         case 2:
2555             hcr = arm_hcr_el2_eff(env);
2556             if (hcr & HCR_E2H) {
2557                 return 0;
2558             }
2559             break;
2560         case 0:
2561             hcr = arm_hcr_el2_eff(env);
2562             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2563                 return 0;
2564             }
2565             break;
2566         }
2567         return env->cp15.cntvoff_el2;
2568     case GTIMER_HYP:
2569     case GTIMER_SEC:
2570     case GTIMER_HYPVIRT:
2571     case GTIMER_S_EL2_PHYS:
2572     case GTIMER_S_EL2_VIRT:
2573         return 0;
2574     default:
2575         g_assert_not_reached();
2576     }
2577 }
2578 
gt_recalc_timer(ARMCPU * cpu,int timeridx)2579 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2580 {
2581     ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2582 
2583     if (gt->ctl & 1) {
2584         /*
2585          * Timer enabled: calculate and set current ISTATUS, irq, and
2586          * reset timer to when ISTATUS next has to change
2587          */
2588         uint64_t offset = gt_indirect_access_timer_offset(&cpu->env, timeridx);
2589         uint64_t count = gt_get_countervalue(&cpu->env);
2590         /* Note that this must be unsigned 64 bit arithmetic: */
2591         int istatus = count - offset >= gt->cval;
2592         uint64_t nexttick;
2593 
2594         gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2595 
2596         if (istatus) {
2597             /*
2598              * Next transition is when (count - offset) rolls back over to 0.
2599              * If offset > count then this is when count == offset;
2600              * if offset <= count then this is when count == offset + 2^64
2601              * For the latter case we set nexttick to an "as far in future
2602              * as possible" value and let the code below handle it.
2603              */
2604             if (offset > count) {
2605                 nexttick = offset;
2606             } else {
2607                 nexttick = UINT64_MAX;
2608             }
2609         } else {
2610             /*
2611              * Next transition is when (count - offset) == cval, i.e.
2612              * when count == (cval + offset).
2613              * If that would overflow, then again we set up the next interrupt
2614              * for "as far in the future as possible" for the code below.
2615              */
2616             if (uadd64_overflow(gt->cval, offset, &nexttick)) {
2617                 nexttick = UINT64_MAX;
2618             }
2619         }
2620         /*
2621          * Note that the desired next expiry time might be beyond the
2622          * signed-64-bit range of a QEMUTimer -- in this case we just
2623          * set the timer for as far in the future as possible. When the
2624          * timer expires we will reset the timer for any remaining period.
2625          */
2626         if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2627             timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2628         } else {
2629             timer_mod(cpu->gt_timer[timeridx], nexttick);
2630         }
2631         trace_arm_gt_recalc(timeridx, nexttick);
2632     } else {
2633         /* Timer disabled: ISTATUS and timer output always clear */
2634         gt->ctl &= ~4;
2635         timer_del(cpu->gt_timer[timeridx]);
2636         trace_arm_gt_recalc_disabled(timeridx);
2637     }
2638     gt_update_irq(cpu, timeridx);
2639 }
2640 
gt_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx)2641 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2642                            int timeridx)
2643 {
2644     ARMCPU *cpu = env_archcpu(env);
2645 
2646     timer_del(cpu->gt_timer[timeridx]);
2647 }
2648 
gt_cnt_read(CPUARMState * env,const ARMCPRegInfo * ri)2649 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2650 {
2651     uint64_t offset = gt_direct_access_timer_offset(env, GTIMER_PHYS);
2652     return gt_get_countervalue(env) - offset;
2653 }
2654 
gt_virt_cnt_read(CPUARMState * env,const ARMCPRegInfo * ri)2655 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2656 {
2657     uint64_t offset = gt_direct_access_timer_offset(env, GTIMER_VIRT);
2658     return gt_get_countervalue(env) - offset;
2659 }
2660 
gt_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx,uint64_t value)2661 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2662                           int timeridx,
2663                           uint64_t value)
2664 {
2665     trace_arm_gt_cval_write(timeridx, value);
2666     env->cp15.c14_timer[timeridx].cval = value;
2667     gt_recalc_timer(env_archcpu(env), timeridx);
2668 }
2669 
do_tval_read(CPUARMState * env,int timeridx,uint64_t offset)2670 static uint64_t do_tval_read(CPUARMState *env, int timeridx, uint64_t offset)
2671 {
2672     return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2673                       (gt_get_countervalue(env) - offset));
2674 }
2675 
gt_tval_read(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx)2676 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2677                              int timeridx)
2678 {
2679     uint64_t offset = gt_direct_access_timer_offset(env, timeridx);
2680 
2681     return do_tval_read(env, timeridx, offset);
2682 }
2683 
do_tval_write(CPUARMState * env,int timeridx,uint64_t value,uint64_t offset)2684 static void do_tval_write(CPUARMState *env, int timeridx, uint64_t value,
2685                           uint64_t offset)
2686 {
2687     trace_arm_gt_tval_write(timeridx, value);
2688     env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2689                                          sextract64(value, 0, 32);
2690     gt_recalc_timer(env_archcpu(env), timeridx);
2691 }
2692 
gt_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx,uint64_t value)2693 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2694                           int timeridx,
2695                           uint64_t value)
2696 {
2697     uint64_t offset = gt_direct_access_timer_offset(env, timeridx);
2698 
2699     do_tval_write(env, timeridx, value, offset);
2700 }
2701 
gt_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx,uint64_t value)2702 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2703                          int timeridx,
2704                          uint64_t value)
2705 {
2706     ARMCPU *cpu = env_archcpu(env);
2707     uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2708 
2709     trace_arm_gt_ctl_write(timeridx, value);
2710     env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2711     if ((oldval ^ value) & 1) {
2712         /* Enable toggled */
2713         gt_recalc_timer(cpu, timeridx);
2714     } else if ((oldval ^ value) & 2) {
2715         /*
2716          * IMASK toggled: don't need to recalculate,
2717          * just set the interrupt line based on ISTATUS
2718          */
2719         trace_arm_gt_imask_toggle(timeridx);
2720         gt_update_irq(cpu, timeridx);
2721     }
2722 }
2723 
gt_phys_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)2724 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2725 {
2726     gt_timer_reset(env, ri, GTIMER_PHYS);
2727 }
2728 
gt_phys_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2729 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2730                                uint64_t value)
2731 {
2732     gt_cval_write(env, ri, GTIMER_PHYS, value);
2733 }
2734 
gt_phys_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)2735 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2736 {
2737     return gt_tval_read(env, ri, GTIMER_PHYS);
2738 }
2739 
gt_phys_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2740 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2741                                uint64_t value)
2742 {
2743     gt_tval_write(env, ri, GTIMER_PHYS, value);
2744 }
2745 
gt_phys_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2746 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2747                               uint64_t value)
2748 {
2749     gt_ctl_write(env, ri, GTIMER_PHYS, value);
2750 }
2751 
gt_phys_redir_timeridx(CPUARMState * env)2752 static int gt_phys_redir_timeridx(CPUARMState *env)
2753 {
2754     switch (arm_mmu_idx(env)) {
2755     case ARMMMUIdx_E20_0:
2756     case ARMMMUIdx_E20_2:
2757     case ARMMMUIdx_E20_2_PAN:
2758         return GTIMER_HYP;
2759     default:
2760         return GTIMER_PHYS;
2761     }
2762 }
2763 
gt_virt_redir_timeridx(CPUARMState * env)2764 static int gt_virt_redir_timeridx(CPUARMState *env)
2765 {
2766     switch (arm_mmu_idx(env)) {
2767     case ARMMMUIdx_E20_0:
2768     case ARMMMUIdx_E20_2:
2769     case ARMMMUIdx_E20_2_PAN:
2770         return GTIMER_HYPVIRT;
2771     default:
2772         return GTIMER_VIRT;
2773     }
2774 }
2775 
gt_phys_redir_cval_read(CPUARMState * env,const ARMCPRegInfo * ri)2776 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2777                                         const ARMCPRegInfo *ri)
2778 {
2779     int timeridx = gt_phys_redir_timeridx(env);
2780     return env->cp15.c14_timer[timeridx].cval;
2781 }
2782 
gt_phys_redir_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2783 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2784                                      uint64_t value)
2785 {
2786     int timeridx = gt_phys_redir_timeridx(env);
2787     gt_cval_write(env, ri, timeridx, value);
2788 }
2789 
gt_phys_redir_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)2790 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2791                                         const ARMCPRegInfo *ri)
2792 {
2793     int timeridx = gt_phys_redir_timeridx(env);
2794     return gt_tval_read(env, ri, timeridx);
2795 }
2796 
gt_phys_redir_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2797 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2798                                      uint64_t value)
2799 {
2800     int timeridx = gt_phys_redir_timeridx(env);
2801     gt_tval_write(env, ri, timeridx, value);
2802 }
2803 
gt_phys_redir_ctl_read(CPUARMState * env,const ARMCPRegInfo * ri)2804 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2805                                        const ARMCPRegInfo *ri)
2806 {
2807     int timeridx = gt_phys_redir_timeridx(env);
2808     return env->cp15.c14_timer[timeridx].ctl;
2809 }
2810 
gt_phys_redir_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2811 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2812                                     uint64_t value)
2813 {
2814     int timeridx = gt_phys_redir_timeridx(env);
2815     gt_ctl_write(env, ri, timeridx, value);
2816 }
2817 
gt_virt_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)2818 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2819 {
2820     gt_timer_reset(env, ri, GTIMER_VIRT);
2821 }
2822 
gt_virt_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2823 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2824                                uint64_t value)
2825 {
2826     gt_cval_write(env, ri, GTIMER_VIRT, value);
2827 }
2828 
gt_virt_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)2829 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2830 {
2831     /*
2832      * This is CNTV_TVAL_EL02; unlike the underlying CNTV_TVAL_EL0
2833      * we always apply CNTVOFF_EL2. Special case that here rather
2834      * than going into the generic gt_tval_read() and then having
2835      * to re-detect that it's this register.
2836      * Note that the accessfn/perms mean we know we're at EL2 or EL3 here.
2837      */
2838     return do_tval_read(env, GTIMER_VIRT, env->cp15.cntvoff_el2);
2839 }
2840 
gt_virt_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2841 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2842                                uint64_t value)
2843 {
2844     /* Similarly for writes to CNTV_TVAL_EL02 */
2845     do_tval_write(env, GTIMER_VIRT, value, env->cp15.cntvoff_el2);
2846 }
2847 
gt_virt_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2848 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2849                               uint64_t value)
2850 {
2851     gt_ctl_write(env, ri, GTIMER_VIRT, value);
2852 }
2853 
gt_cnthctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2854 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2855                              uint64_t value)
2856 {
2857     ARMCPU *cpu = env_archcpu(env);
2858     uint32_t oldval = env->cp15.cnthctl_el2;
2859     uint32_t valid_mask =
2860         R_CNTHCTL_EL0PCTEN_E2H1_MASK |
2861         R_CNTHCTL_EL0VCTEN_E2H1_MASK |
2862         R_CNTHCTL_EVNTEN_MASK |
2863         R_CNTHCTL_EVNTDIR_MASK |
2864         R_CNTHCTL_EVNTI_MASK |
2865         R_CNTHCTL_EL0VTEN_MASK |
2866         R_CNTHCTL_EL0PTEN_MASK |
2867         R_CNTHCTL_EL1PCTEN_E2H1_MASK |
2868         R_CNTHCTL_EL1PTEN_MASK;
2869 
2870     if (cpu_isar_feature(aa64_rme, cpu)) {
2871         valid_mask |= R_CNTHCTL_CNTVMASK_MASK | R_CNTHCTL_CNTPMASK_MASK;
2872     }
2873     if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
2874         valid_mask |=
2875             R_CNTHCTL_EL1TVT_MASK |
2876             R_CNTHCTL_EL1TVCT_MASK |
2877             R_CNTHCTL_EL1NVPCT_MASK |
2878             R_CNTHCTL_EL1NVVCT_MASK |
2879             R_CNTHCTL_EVNTIS_MASK;
2880     }
2881     if (cpu_isar_feature(aa64_ecv, cpu)) {
2882         valid_mask |= R_CNTHCTL_ECV_MASK;
2883     }
2884 
2885     /* Clear RES0 bits */
2886     value &= valid_mask;
2887 
2888     raw_write(env, ri, value);
2889 
2890     if ((oldval ^ value) & R_CNTHCTL_CNTVMASK_MASK) {
2891         gt_update_irq(cpu, GTIMER_VIRT);
2892     } else if ((oldval ^ value) & R_CNTHCTL_CNTPMASK_MASK) {
2893         gt_update_irq(cpu, GTIMER_PHYS);
2894     }
2895 }
2896 
gt_cntvoff_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2897 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2898                               uint64_t value)
2899 {
2900     ARMCPU *cpu = env_archcpu(env);
2901 
2902     trace_arm_gt_cntvoff_write(value);
2903     raw_write(env, ri, value);
2904     gt_recalc_timer(cpu, GTIMER_VIRT);
2905 }
2906 
gt_virt_redir_cval_read(CPUARMState * env,const ARMCPRegInfo * ri)2907 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2908                                         const ARMCPRegInfo *ri)
2909 {
2910     int timeridx = gt_virt_redir_timeridx(env);
2911     return env->cp15.c14_timer[timeridx].cval;
2912 }
2913 
gt_virt_redir_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2914 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2915                                      uint64_t value)
2916 {
2917     int timeridx = gt_virt_redir_timeridx(env);
2918     gt_cval_write(env, ri, timeridx, value);
2919 }
2920 
gt_virt_redir_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)2921 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2922                                         const ARMCPRegInfo *ri)
2923 {
2924     int timeridx = gt_virt_redir_timeridx(env);
2925     return gt_tval_read(env, ri, timeridx);
2926 }
2927 
gt_virt_redir_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2928 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2929                                      uint64_t value)
2930 {
2931     int timeridx = gt_virt_redir_timeridx(env);
2932     gt_tval_write(env, ri, timeridx, value);
2933 }
2934 
gt_virt_redir_ctl_read(CPUARMState * env,const ARMCPRegInfo * ri)2935 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2936                                        const ARMCPRegInfo *ri)
2937 {
2938     int timeridx = gt_virt_redir_timeridx(env);
2939     return env->cp15.c14_timer[timeridx].ctl;
2940 }
2941 
gt_virt_redir_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2942 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2943                                     uint64_t value)
2944 {
2945     int timeridx = gt_virt_redir_timeridx(env);
2946     gt_ctl_write(env, ri, timeridx, value);
2947 }
2948 
gt_hyp_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)2949 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2950 {
2951     gt_timer_reset(env, ri, GTIMER_HYP);
2952 }
2953 
gt_hyp_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2954 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2955                               uint64_t value)
2956 {
2957     gt_cval_write(env, ri, GTIMER_HYP, value);
2958 }
2959 
gt_hyp_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)2960 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2961 {
2962     return gt_tval_read(env, ri, GTIMER_HYP);
2963 }
2964 
gt_hyp_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2965 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2966                               uint64_t value)
2967 {
2968     gt_tval_write(env, ri, GTIMER_HYP, value);
2969 }
2970 
gt_hyp_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2971 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2972                               uint64_t value)
2973 {
2974     gt_ctl_write(env, ri, GTIMER_HYP, value);
2975 }
2976 
gt_sec_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)2977 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2978 {
2979     gt_timer_reset(env, ri, GTIMER_SEC);
2980 }
2981 
gt_sec_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2982 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2983                               uint64_t value)
2984 {
2985     gt_cval_write(env, ri, GTIMER_SEC, value);
2986 }
2987 
gt_sec_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)2988 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2989 {
2990     return gt_tval_read(env, ri, GTIMER_SEC);
2991 }
2992 
gt_sec_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2993 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2994                               uint64_t value)
2995 {
2996     gt_tval_write(env, ri, GTIMER_SEC, value);
2997 }
2998 
gt_sec_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2999 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3000                               uint64_t value)
3001 {
3002     gt_ctl_write(env, ri, GTIMER_SEC, value);
3003 }
3004 
gt_sec_pel2_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)3005 static void gt_sec_pel2_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3006 {
3007     gt_timer_reset(env, ri, GTIMER_S_EL2_PHYS);
3008 }
3009 
gt_sec_pel2_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3010 static void gt_sec_pel2_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3011                                    uint64_t value)
3012 {
3013     gt_cval_write(env, ri, GTIMER_S_EL2_PHYS, value);
3014 }
3015 
gt_sec_pel2_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3016 static uint64_t gt_sec_pel2_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3017 {
3018     return gt_tval_read(env, ri, GTIMER_S_EL2_PHYS);
3019 }
3020 
gt_sec_pel2_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3021 static void gt_sec_pel2_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3022                               uint64_t value)
3023 {
3024     gt_tval_write(env, ri, GTIMER_S_EL2_PHYS, value);
3025 }
3026 
gt_sec_pel2_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3027 static void gt_sec_pel2_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3028                               uint64_t value)
3029 {
3030     gt_ctl_write(env, ri, GTIMER_S_EL2_PHYS, value);
3031 }
3032 
gt_sec_vel2_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)3033 static void gt_sec_vel2_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3034 {
3035     gt_timer_reset(env, ri, GTIMER_S_EL2_VIRT);
3036 }
3037 
gt_sec_vel2_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3038 static void gt_sec_vel2_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3039                               uint64_t value)
3040 {
3041     gt_cval_write(env, ri, GTIMER_S_EL2_VIRT, value);
3042 }
3043 
gt_sec_vel2_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3044 static uint64_t gt_sec_vel2_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3045 {
3046     return gt_tval_read(env, ri, GTIMER_S_EL2_VIRT);
3047 }
3048 
gt_sec_vel2_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3049 static void gt_sec_vel2_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3050                                    uint64_t value)
3051 {
3052     gt_tval_write(env, ri, GTIMER_S_EL2_VIRT, value);
3053 }
3054 
gt_sec_vel2_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3055 static void gt_sec_vel2_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3056                               uint64_t value)
3057 {
3058     gt_ctl_write(env, ri, GTIMER_S_EL2_VIRT, value);
3059 }
3060 
gt_hv_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)3061 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3062 {
3063     gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3064 }
3065 
gt_hv_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3066 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3067                              uint64_t value)
3068 {
3069     gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3070 }
3071 
gt_hv_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3072 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3073 {
3074     return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3075 }
3076 
gt_hv_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3077 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3078                              uint64_t value)
3079 {
3080     gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3081 }
3082 
gt_hv_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3083 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3084                             uint64_t value)
3085 {
3086     gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3087 }
3088 
arm_gt_ptimer_cb(void * opaque)3089 void arm_gt_ptimer_cb(void *opaque)
3090 {
3091     ARMCPU *cpu = opaque;
3092 
3093     gt_recalc_timer(cpu, GTIMER_PHYS);
3094 }
3095 
arm_gt_vtimer_cb(void * opaque)3096 void arm_gt_vtimer_cb(void *opaque)
3097 {
3098     ARMCPU *cpu = opaque;
3099 
3100     gt_recalc_timer(cpu, GTIMER_VIRT);
3101 }
3102 
arm_gt_htimer_cb(void * opaque)3103 void arm_gt_htimer_cb(void *opaque)
3104 {
3105     ARMCPU *cpu = opaque;
3106 
3107     gt_recalc_timer(cpu, GTIMER_HYP);
3108 }
3109 
arm_gt_stimer_cb(void * opaque)3110 void arm_gt_stimer_cb(void *opaque)
3111 {
3112     ARMCPU *cpu = opaque;
3113 
3114     gt_recalc_timer(cpu, GTIMER_SEC);
3115 }
3116 
arm_gt_sel2timer_cb(void * opaque)3117 void arm_gt_sel2timer_cb(void *opaque)
3118 {
3119     ARMCPU *cpu = opaque;
3120 
3121     gt_recalc_timer(cpu, GTIMER_S_EL2_PHYS);
3122 }
3123 
arm_gt_sel2vtimer_cb(void * opaque)3124 void arm_gt_sel2vtimer_cb(void *opaque)
3125 {
3126     ARMCPU *cpu = opaque;
3127 
3128     gt_recalc_timer(cpu, GTIMER_S_EL2_VIRT);
3129 }
3130 
arm_gt_hvtimer_cb(void * opaque)3131 void arm_gt_hvtimer_cb(void *opaque)
3132 {
3133     ARMCPU *cpu = opaque;
3134 
3135     gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3136 }
3137 
3138 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3139     /*
3140      * Note that CNTFRQ is purely reads-as-written for the benefit
3141      * of software; writing it doesn't actually change the timer frequency.
3142      * Our reset value matches the fixed frequency we implement the timer at.
3143      */
3144     { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3145       .type = ARM_CP_ALIAS,
3146       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3147       .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3148     },
3149     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3150       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3151       .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3152       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3153       .resetfn = arm_gt_cntfrq_reset,
3154     },
3155     /* overall control: mostly access permissions */
3156     { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3157       .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3158       .access = PL1_RW,
3159       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3160       .resetvalue = 0,
3161     },
3162     /* per-timer control */
3163     { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3164       .secure = ARM_CP_SECSTATE_NS,
3165       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3166       .accessfn = gt_ptimer_access,
3167       .fieldoffset = offsetoflow32(CPUARMState,
3168                                    cp15.c14_timer[GTIMER_PHYS].ctl),
3169       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3170       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3171     },
3172     { .name = "CNTP_CTL_S",
3173       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3174       .secure = ARM_CP_SECSTATE_S,
3175       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3176       .accessfn = gt_ptimer_access,
3177       .fieldoffset = offsetoflow32(CPUARMState,
3178                                    cp15.c14_timer[GTIMER_SEC].ctl),
3179       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3180     },
3181     { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3182       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3183       .type = ARM_CP_IO, .access = PL0_RW,
3184       .accessfn = gt_ptimer_access,
3185       .nv2_redirect_offset = 0x180 | NV2_REDIR_NV1,
3186       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3187       .resetvalue = 0,
3188       .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3189       .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3190     },
3191     { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3192       .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3193       .accessfn = gt_vtimer_access,
3194       .fieldoffset = offsetoflow32(CPUARMState,
3195                                    cp15.c14_timer[GTIMER_VIRT].ctl),
3196       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3197       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3198     },
3199     { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3200       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3201       .type = ARM_CP_IO, .access = PL0_RW,
3202       .accessfn = gt_vtimer_access,
3203       .nv2_redirect_offset = 0x170 | NV2_REDIR_NV1,
3204       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3205       .resetvalue = 0,
3206       .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3207       .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3208     },
3209     /* TimerValue views: a 32 bit downcounting view of the underlying state */
3210     { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3211       .secure = ARM_CP_SECSTATE_NS,
3212       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3213       .accessfn = gt_ptimer_access,
3214       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3215     },
3216     { .name = "CNTP_TVAL_S",
3217       .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3218       .secure = ARM_CP_SECSTATE_S,
3219       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3220       .accessfn = gt_ptimer_access,
3221       .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3222     },
3223     { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3224       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3225       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3226       .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3227       .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3228     },
3229     { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3230       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3231       .accessfn = gt_vtimer_access,
3232       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3233     },
3234     { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3235       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3236       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3237       .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3238       .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3239     },
3240     /* The counter itself */
3241     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3242       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3243       .accessfn = gt_pct_access,
3244       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3245     },
3246     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3247       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3248       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3249       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3250     },
3251     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3252       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3253       .accessfn = gt_vct_access,
3254       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3255     },
3256     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3257       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3258       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3259       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3260     },
3261     /* Comparison value, indicating when the timer goes off */
3262     { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3263       .secure = ARM_CP_SECSTATE_NS,
3264       .access = PL0_RW,
3265       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3266       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3267       .accessfn = gt_ptimer_access,
3268       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3269       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3270     },
3271     { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3272       .secure = ARM_CP_SECSTATE_S,
3273       .access = PL0_RW,
3274       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3275       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3276       .accessfn = gt_ptimer_access,
3277       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3278     },
3279     { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3280       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3281       .access = PL0_RW,
3282       .type = ARM_CP_IO,
3283       .nv2_redirect_offset = 0x178 | NV2_REDIR_NV1,
3284       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3285       .resetvalue = 0, .accessfn = gt_ptimer_access,
3286       .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3287       .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3288     },
3289     { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3290       .access = PL0_RW,
3291       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3292       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3293       .accessfn = gt_vtimer_access,
3294       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3295       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3296     },
3297     { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3298       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3299       .access = PL0_RW,
3300       .type = ARM_CP_IO,
3301       .nv2_redirect_offset = 0x168 | NV2_REDIR_NV1,
3302       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3303       .resetvalue = 0, .accessfn = gt_vtimer_access,
3304       .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3305       .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3306     },
3307     /*
3308      * Secure timer -- this is actually restricted to only EL3
3309      * and configurably Secure-EL1 via the accessfn.
3310      */
3311     { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3312       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3313       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3314       .accessfn = gt_stimer_access,
3315       .readfn = gt_sec_tval_read,
3316       .writefn = gt_sec_tval_write,
3317       .resetfn = gt_sec_timer_reset,
3318     },
3319     { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3320       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3321       .type = ARM_CP_IO, .access = PL1_RW,
3322       .accessfn = gt_stimer_access,
3323       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3324       .resetvalue = 0,
3325       .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3326     },
3327     { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3328       .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3329       .type = ARM_CP_IO, .access = PL1_RW,
3330       .accessfn = gt_stimer_access,
3331       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3332       .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3333     },
3334 };
3335 
3336 /*
3337  * FEAT_ECV adds extra views of CNTVCT_EL0 and CNTPCT_EL0 which
3338  * are "self-synchronizing". For QEMU all sysregs are self-synchronizing,
3339  * so our implementations here are identical to the normal registers.
3340  */
3341 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3342     { .name = "CNTVCTSS", .cp = 15, .crm = 14, .opc1 = 9,
3343       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3344       .accessfn = gt_vct_access,
3345       .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3346     },
3347     { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3348       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3349       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3350       .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3351     },
3352     { .name = "CNTPCTSS", .cp = 15, .crm = 14, .opc1 = 8,
3353       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3354       .accessfn = gt_pct_access,
3355       .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3356     },
3357     { .name = "CNTPCTSS_EL0", .state = ARM_CP_STATE_AA64,
3358       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 5,
3359       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3360       .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3361     },
3362 };
3363 
gt_cntpoff_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)3364 static CPAccessResult gt_cntpoff_access(CPUARMState *env,
3365                                         const ARMCPRegInfo *ri,
3366                                         bool isread)
3367 {
3368     if (arm_current_el(env) == 2 && arm_feature(env, ARM_FEATURE_EL3) &&
3369         !(env->cp15.scr_el3 & SCR_ECVEN)) {
3370         return CP_ACCESS_TRAP_EL3;
3371     }
3372     return CP_ACCESS_OK;
3373 }
3374 
gt_cntpoff_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3375 static void gt_cntpoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
3376                               uint64_t value)
3377 {
3378     ARMCPU *cpu = env_archcpu(env);
3379 
3380     trace_arm_gt_cntpoff_write(value);
3381     raw_write(env, ri, value);
3382     gt_recalc_timer(cpu, GTIMER_PHYS);
3383 }
3384 
3385 static const ARMCPRegInfo gen_timer_cntpoff_reginfo = {
3386     .name = "CNTPOFF_EL2", .state = ARM_CP_STATE_AA64,
3387     .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 6,
3388     .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3389     .accessfn = gt_cntpoff_access, .writefn = gt_cntpoff_write,
3390     .nv2_redirect_offset = 0x1a8,
3391     .fieldoffset = offsetof(CPUARMState, cp15.cntpoff_el2),
3392 };
3393 #else
3394 
3395 /*
3396  * In user-mode most of the generic timer registers are inaccessible
3397  * however modern kernels (4.12+) allow access to cntvct_el0
3398  */
3399 
gt_virt_cnt_read(CPUARMState * env,const ARMCPRegInfo * ri)3400 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3401 {
3402     ARMCPU *cpu = env_archcpu(env);
3403 
3404     /*
3405      * Currently we have no support for QEMUTimer in linux-user so we
3406      * can't call gt_get_countervalue(env), instead we directly
3407      * call the lower level functions.
3408      */
3409     return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3410 }
3411 
3412 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3413     { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3414       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3415       .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3416       .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3417       .resetfn = arm_gt_cntfrq_reset,
3418     },
3419     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3420       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3421       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3422       .readfn = gt_virt_cnt_read,
3423     },
3424 };
3425 
3426 /*
3427  * CNTVCTSS_EL0 has the same trap conditions as CNTVCT_EL0, so it also
3428  * is exposed to userspace by Linux.
3429  */
3430 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3431     { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3432       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3433       .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3434       .readfn = gt_virt_cnt_read,
3435     },
3436 };
3437 
3438 #endif
3439 
par_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3440 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3441 {
3442     if (arm_feature(env, ARM_FEATURE_LPAE)) {
3443         raw_write(env, ri, value);
3444     } else if (arm_feature(env, ARM_FEATURE_V7)) {
3445         raw_write(env, ri, value & 0xfffff6ff);
3446     } else {
3447         raw_write(env, ri, value & 0xfffff1ff);
3448     }
3449 }
3450 
3451 #ifndef CONFIG_USER_ONLY
3452 /* get_phys_addr() isn't present for user-mode-only targets */
3453 
ats_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)3454 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3455                                  bool isread)
3456 {
3457     if (ri->opc2 & 4) {
3458         /*
3459          * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3460          * Secure EL1 (which can only happen if EL3 is AArch64).
3461          * They are simply UNDEF if executed from NS EL1.
3462          * They function normally from EL2 or EL3.
3463          */
3464         if (arm_current_el(env) == 1) {
3465             if (arm_is_secure_below_el3(env)) {
3466                 if (env->cp15.scr_el3 & SCR_EEL2) {
3467                     return CP_ACCESS_TRAP_EL2;
3468                 }
3469                 return CP_ACCESS_TRAP_EL3;
3470             }
3471             return CP_ACCESS_UNDEFINED;
3472         }
3473     }
3474     return CP_ACCESS_OK;
3475 }
3476 
3477 #ifdef CONFIG_TCG
par_el1_shareability(GetPhysAddrResult * res)3478 static int par_el1_shareability(GetPhysAddrResult *res)
3479 {
3480     /*
3481      * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3482      * memory -- see pseudocode PAREncodeShareability().
3483      */
3484     if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3485         res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3486         return 2;
3487     }
3488     return res->cacheattrs.shareability;
3489 }
3490 
do_ats_write(CPUARMState * env,uint64_t value,MMUAccessType access_type,ARMMMUIdx mmu_idx,ARMSecuritySpace ss)3491 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3492                              MMUAccessType access_type, ARMMMUIdx mmu_idx,
3493                              ARMSecuritySpace ss)
3494 {
3495     bool ret;
3496     uint64_t par64;
3497     bool format64 = false;
3498     ARMMMUFaultInfo fi = {};
3499     GetPhysAddrResult res = {};
3500 
3501     /*
3502      * I_MXTJT: Granule protection checks are not performed on the final
3503      * address of a successful translation.  This is a translation not a
3504      * memory reference, so "memop = none = 0".
3505      */
3506     ret = get_phys_addr_with_space_nogpc(env, value, access_type, 0,
3507                                          mmu_idx, ss, &res, &fi);
3508 
3509     /*
3510      * ATS operations only do S1 or S1+S2 translations, so we never
3511      * have to deal with the ARMCacheAttrs format for S2 only.
3512      */
3513     assert(!res.cacheattrs.is_s2_format);
3514 
3515     if (ret) {
3516         /*
3517          * Some kinds of translation fault must cause exceptions rather
3518          * than being reported in the PAR.
3519          */
3520         int current_el = arm_current_el(env);
3521         int target_el;
3522         uint32_t syn, fsr, fsc;
3523         bool take_exc = false;
3524 
3525         if (fi.s1ptw && current_el == 1
3526             && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3527             /*
3528              * Synchronous stage 2 fault on an access made as part of the
3529              * translation table walk for AT S1E0* or AT S1E1* insn
3530              * executed from NS EL1. If this is a synchronous external abort
3531              * and SCR_EL3.EA == 1, then we take a synchronous external abort
3532              * to EL3. Otherwise the fault is taken as an exception to EL2,
3533              * and HPFAR_EL2 holds the faulting IPA.
3534              */
3535             if (fi.type == ARMFault_SyncExternalOnWalk &&
3536                 (env->cp15.scr_el3 & SCR_EA)) {
3537                 target_el = 3;
3538             } else {
3539                 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3540                 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3541                     env->cp15.hpfar_el2 |= HPFAR_NS;
3542                 }
3543                 target_el = 2;
3544             }
3545             take_exc = true;
3546         } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3547             /*
3548              * Synchronous external aborts during a translation table walk
3549              * are taken as Data Abort exceptions.
3550              */
3551             if (fi.stage2) {
3552                 if (current_el == 3) {
3553                     target_el = 3;
3554                 } else {
3555                     target_el = 2;
3556                 }
3557             } else {
3558                 target_el = exception_target_el(env);
3559             }
3560             take_exc = true;
3561         }
3562 
3563         if (take_exc) {
3564             /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3565             if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3566                 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3567                 fsr = arm_fi_to_lfsc(&fi);
3568                 fsc = extract32(fsr, 0, 6);
3569             } else {
3570                 fsr = arm_fi_to_sfsc(&fi);
3571                 fsc = 0x3f;
3572             }
3573             /*
3574              * Report exception with ESR indicating a fault due to a
3575              * translation table walk for a cache maintenance instruction.
3576              */
3577             syn = syn_data_abort_no_iss(current_el == target_el, 0,
3578                                         fi.ea, 1, fi.s1ptw, 1, fsc);
3579             env->exception.vaddress = value;
3580             env->exception.fsr = fsr;
3581             raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3582         }
3583     }
3584 
3585     if (is_a64(env)) {
3586         format64 = true;
3587     } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3588         /*
3589          * ATS1Cxx:
3590          * * TTBCR.EAE determines whether the result is returned using the
3591          *   32-bit or the 64-bit PAR format
3592          * * Instructions executed in Hyp mode always use the 64bit format
3593          *
3594          * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3595          * * The Non-secure TTBCR.EAE bit is set to 1
3596          * * The implementation includes EL2, and the value of HCR.VM is 1
3597          *
3598          * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3599          *
3600          * ATS1Hx always uses the 64bit format.
3601          */
3602         format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3603 
3604         if (arm_feature(env, ARM_FEATURE_EL2)) {
3605             if (mmu_idx == ARMMMUIdx_E10_0 ||
3606                 mmu_idx == ARMMMUIdx_E10_1 ||
3607                 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3608                 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3609             } else {
3610                 format64 |= arm_current_el(env) == 2;
3611             }
3612         }
3613     }
3614 
3615     if (format64) {
3616         /* Create a 64-bit PAR */
3617         par64 = (1 << 11); /* LPAE bit always set */
3618         if (!ret) {
3619             par64 |= res.f.phys_addr & ~0xfffULL;
3620             if (!res.f.attrs.secure) {
3621                 par64 |= (1 << 9); /* NS */
3622             }
3623             par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3624             par64 |= par_el1_shareability(&res) << 7; /* SH */
3625         } else {
3626             uint32_t fsr = arm_fi_to_lfsc(&fi);
3627 
3628             par64 |= 1; /* F */
3629             par64 |= (fsr & 0x3f) << 1; /* FS */
3630             if (fi.stage2) {
3631                 par64 |= (1 << 9); /* S */
3632             }
3633             if (fi.s1ptw) {
3634                 par64 |= (1 << 8); /* PTW */
3635             }
3636         }
3637     } else {
3638         /*
3639          * fsr is a DFSR/IFSR value for the short descriptor
3640          * translation table format (with WnR always clear).
3641          * Convert it to a 32-bit PAR.
3642          */
3643         if (!ret) {
3644             /* We do not set any attribute bits in the PAR */
3645             if (res.f.lg_page_size == 24
3646                 && arm_feature(env, ARM_FEATURE_V7)) {
3647                 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3648             } else {
3649                 par64 = res.f.phys_addr & 0xfffff000;
3650             }
3651             if (!res.f.attrs.secure) {
3652                 par64 |= (1 << 9); /* NS */
3653             }
3654         } else {
3655             uint32_t fsr = arm_fi_to_sfsc(&fi);
3656 
3657             par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3658                     ((fsr & 0xf) << 1) | 1;
3659         }
3660     }
3661     return par64;
3662 }
3663 #endif /* CONFIG_TCG */
3664 
ats_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3665 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3666 {
3667 #ifdef CONFIG_TCG
3668     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3669     uint64_t par64;
3670     ARMMMUIdx mmu_idx;
3671     int el = arm_current_el(env);
3672     ARMSecuritySpace ss = arm_security_space(env);
3673 
3674     switch (ri->opc2 & 6) {
3675     case 0:
3676         /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3677         switch (el) {
3678         case 3:
3679             if (ri->crm == 9 && arm_pan_enabled(env)) {
3680                 mmu_idx = ARMMMUIdx_E30_3_PAN;
3681             } else {
3682                 mmu_idx = ARMMMUIdx_E3;
3683             }
3684             break;
3685         case 2:
3686             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3687             /* fall through */
3688         case 1:
3689             if (ri->crm == 9 && arm_pan_enabled(env)) {
3690                 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3691             } else {
3692                 mmu_idx = ARMMMUIdx_Stage1_E1;
3693             }
3694             break;
3695         default:
3696             g_assert_not_reached();
3697         }
3698         break;
3699     case 2:
3700         /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3701         switch (el) {
3702         case 3:
3703             mmu_idx = ARMMMUIdx_E30_0;
3704             break;
3705         case 2:
3706             g_assert(ss != ARMSS_Secure);  /* ARMv8.4-SecEL2 is 64-bit only */
3707             mmu_idx = ARMMMUIdx_Stage1_E0;
3708             break;
3709         case 1:
3710             mmu_idx = ARMMMUIdx_Stage1_E0;
3711             break;
3712         default:
3713             g_assert_not_reached();
3714         }
3715         break;
3716     case 4:
3717         /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3718         mmu_idx = ARMMMUIdx_E10_1;
3719         ss = ARMSS_NonSecure;
3720         break;
3721     case 6:
3722         /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3723         mmu_idx = ARMMMUIdx_E10_0;
3724         ss = ARMSS_NonSecure;
3725         break;
3726     default:
3727         g_assert_not_reached();
3728     }
3729 
3730     par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
3731 
3732     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3733 #else
3734     /* Handled by hardware accelerator. */
3735     g_assert_not_reached();
3736 #endif /* CONFIG_TCG */
3737 }
3738 
ats1h_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3739 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3740                         uint64_t value)
3741 {
3742 #ifdef CONFIG_TCG
3743     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3744     uint64_t par64;
3745 
3746     /* There is no SecureEL2 for AArch32. */
3747     par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
3748                          ARMSS_NonSecure);
3749 
3750     A32_BANKED_CURRENT_REG_SET(env, par, par64);
3751 #else
3752     /* Handled by hardware accelerator. */
3753     g_assert_not_reached();
3754 #endif /* CONFIG_TCG */
3755 }
3756 
at_e012_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)3757 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
3758                                      bool isread)
3759 {
3760     /*
3761      * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
3762      * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
3763      * only happen when executing at EL3 because that combination also causes an
3764      * illegal exception return. We don't need to check FEAT_RME either, because
3765      * scr_write() ensures that the NSE bit is not set otherwise.
3766      */
3767     if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
3768         return CP_ACCESS_UNDEFINED;
3769     }
3770     return CP_ACCESS_OK;
3771 }
3772 
at_s1e2_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)3773 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3774                                      bool isread)
3775 {
3776     if (arm_current_el(env) == 3 &&
3777         !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3778         return CP_ACCESS_UNDEFINED;
3779     }
3780     return at_e012_access(env, ri, isread);
3781 }
3782 
at_s1e01_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)3783 static CPAccessResult at_s1e01_access(CPUARMState *env, const ARMCPRegInfo *ri,
3784                                       bool isread)
3785 {
3786     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_AT)) {
3787         return CP_ACCESS_TRAP_EL2;
3788     }
3789     return at_e012_access(env, ri, isread);
3790 }
3791 
ats_write64(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3792 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3793                         uint64_t value)
3794 {
3795 #ifdef CONFIG_TCG
3796     MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3797     ARMMMUIdx mmu_idx;
3798     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3799     bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3800     bool for_el3 = false;
3801     ARMSecuritySpace ss;
3802 
3803     switch (ri->opc2 & 6) {
3804     case 0:
3805         switch (ri->opc1) {
3806         case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3807             if (ri->crm == 9 && arm_pan_enabled(env)) {
3808                 mmu_idx = regime_e20 ?
3809                           ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3810             } else {
3811                 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3812             }
3813             break;
3814         case 4: /* AT S1E2R, AT S1E2W */
3815             mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3816             break;
3817         case 6: /* AT S1E3R, AT S1E3W */
3818             mmu_idx = ARMMMUIdx_E3;
3819             for_el3 = true;
3820             break;
3821         default:
3822             g_assert_not_reached();
3823         }
3824         break;
3825     case 2: /* AT S1E0R, AT S1E0W */
3826         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3827         break;
3828     case 4: /* AT S12E1R, AT S12E1W */
3829         mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3830         break;
3831     case 6: /* AT S12E0R, AT S12E0W */
3832         mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3833         break;
3834     default:
3835         g_assert_not_reached();
3836     }
3837 
3838     ss = for_el3 ? arm_security_space(env) : arm_security_space_below_el3(env);
3839     env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx, ss);
3840 #else
3841     /* Handled by hardware accelerator. */
3842     g_assert_not_reached();
3843 #endif /* CONFIG_TCG */
3844 }
3845 #endif
3846 
3847 /* Return basic MPU access permission bits.  */
simple_mpu_ap_bits(uint32_t val)3848 static uint32_t simple_mpu_ap_bits(uint32_t val)
3849 {
3850     uint32_t ret;
3851     uint32_t mask;
3852     int i;
3853     ret = 0;
3854     mask = 3;
3855     for (i = 0; i < 16; i += 2) {
3856         ret |= (val >> i) & mask;
3857         mask <<= 2;
3858     }
3859     return ret;
3860 }
3861 
3862 /* Pad basic MPU access permission bits to extended format.  */
extended_mpu_ap_bits(uint32_t val)3863 static uint32_t extended_mpu_ap_bits(uint32_t val)
3864 {
3865     uint32_t ret;
3866     uint32_t mask;
3867     int i;
3868     ret = 0;
3869     mask = 3;
3870     for (i = 0; i < 16; i += 2) {
3871         ret |= (val & mask) << i;
3872         mask <<= 2;
3873     }
3874     return ret;
3875 }
3876 
pmsav5_data_ap_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3877 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3878                                  uint64_t value)
3879 {
3880     env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3881 }
3882 
pmsav5_data_ap_read(CPUARMState * env,const ARMCPRegInfo * ri)3883 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3884 {
3885     return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3886 }
3887 
pmsav5_insn_ap_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3888 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3889                                  uint64_t value)
3890 {
3891     env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3892 }
3893 
pmsav5_insn_ap_read(CPUARMState * env,const ARMCPRegInfo * ri)3894 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3895 {
3896     return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3897 }
3898 
pmsav7_read(CPUARMState * env,const ARMCPRegInfo * ri)3899 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3900 {
3901     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3902 
3903     if (!u32p) {
3904         return 0;
3905     }
3906 
3907     u32p += env->pmsav7.rnr[M_REG_NS];
3908     return *u32p;
3909 }
3910 
pmsav7_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3911 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3912                          uint64_t value)
3913 {
3914     ARMCPU *cpu = env_archcpu(env);
3915     uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3916 
3917     if (!u32p) {
3918         return;
3919     }
3920 
3921     u32p += env->pmsav7.rnr[M_REG_NS];
3922     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3923     *u32p = value;
3924 }
3925 
pmsav7_rgnr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3926 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3927                               uint64_t value)
3928 {
3929     ARMCPU *cpu = env_archcpu(env);
3930     uint32_t nrgs = cpu->pmsav7_dregion;
3931 
3932     if (value >= nrgs) {
3933         qemu_log_mask(LOG_GUEST_ERROR,
3934                       "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3935                       " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3936         return;
3937     }
3938 
3939     raw_write(env, ri, value);
3940 }
3941 
prbar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3942 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3943                           uint64_t value)
3944 {
3945     ARMCPU *cpu = env_archcpu(env);
3946 
3947     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3948     env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3949 }
3950 
prbar_read(CPUARMState * env,const ARMCPRegInfo * ri)3951 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3952 {
3953     return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3954 }
3955 
prlar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3956 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3957                           uint64_t value)
3958 {
3959     ARMCPU *cpu = env_archcpu(env);
3960 
3961     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3962     env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3963 }
3964 
prlar_read(CPUARMState * env,const ARMCPRegInfo * ri)3965 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3966 {
3967     return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3968 }
3969 
prselr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3970 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3971                            uint64_t value)
3972 {
3973     ARMCPU *cpu = env_archcpu(env);
3974 
3975     /*
3976      * Ignore writes that would select not implemented region.
3977      * This is architecturally UNPREDICTABLE.
3978      */
3979     if (value >= cpu->pmsav7_dregion) {
3980         return;
3981     }
3982 
3983     env->pmsav7.rnr[M_REG_NS] = value;
3984 }
3985 
hprbar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3986 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3987                           uint64_t value)
3988 {
3989     ARMCPU *cpu = env_archcpu(env);
3990 
3991     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3992     env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3993 }
3994 
hprbar_read(CPUARMState * env,const ARMCPRegInfo * ri)3995 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3996 {
3997     return env->pmsav8.hprbar[env->pmsav8.hprselr];
3998 }
3999 
hprlar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4000 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4001                           uint64_t value)
4002 {
4003     ARMCPU *cpu = env_archcpu(env);
4004 
4005     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4006     env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
4007 }
4008 
hprlar_read(CPUARMState * env,const ARMCPRegInfo * ri)4009 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4010 {
4011     return env->pmsav8.hprlar[env->pmsav8.hprselr];
4012 }
4013 
hprenr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4014 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4015                           uint64_t value)
4016 {
4017     uint32_t n;
4018     uint32_t bit;
4019     ARMCPU *cpu = env_archcpu(env);
4020 
4021     /* Ignore writes to unimplemented regions */
4022     int rmax = MIN(cpu->pmsav8r_hdregion, 32);
4023     value &= MAKE_64BIT_MASK(0, rmax);
4024 
4025     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4026 
4027     /* Register alias is only valid for first 32 indexes */
4028     for (n = 0; n < rmax; ++n) {
4029         bit = extract32(value, n, 1);
4030         env->pmsav8.hprlar[n] = deposit32(
4031                     env->pmsav8.hprlar[n], 0, 1, bit);
4032     }
4033 }
4034 
hprenr_read(CPUARMState * env,const ARMCPRegInfo * ri)4035 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4036 {
4037     uint32_t n;
4038     uint32_t result = 0x0;
4039     ARMCPU *cpu = env_archcpu(env);
4040 
4041     /* Register alias is only valid for first 32 indexes */
4042     for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
4043         if (env->pmsav8.hprlar[n] & 0x1) {
4044             result |= (0x1 << n);
4045         }
4046     }
4047     return result;
4048 }
4049 
hprselr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4050 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4051                            uint64_t value)
4052 {
4053     ARMCPU *cpu = env_archcpu(env);
4054 
4055     /*
4056      * Ignore writes that would select not implemented region.
4057      * This is architecturally UNPREDICTABLE.
4058      */
4059     if (value >= cpu->pmsav8r_hdregion) {
4060         return;
4061     }
4062 
4063     env->pmsav8.hprselr = value;
4064 }
4065 
pmsav8r_regn_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4066 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
4067                           uint64_t value)
4068 {
4069     ARMCPU *cpu = env_archcpu(env);
4070     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4071                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4072 
4073     tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4074 
4075     if (ri->opc1 & 4) {
4076         if (index >= cpu->pmsav8r_hdregion) {
4077             return;
4078         }
4079         if (ri->opc2 & 0x1) {
4080             env->pmsav8.hprlar[index] = value;
4081         } else {
4082             env->pmsav8.hprbar[index] = value;
4083         }
4084     } else {
4085         if (index >= cpu->pmsav7_dregion) {
4086             return;
4087         }
4088         if (ri->opc2 & 0x1) {
4089             env->pmsav8.rlar[M_REG_NS][index] = value;
4090         } else {
4091             env->pmsav8.rbar[M_REG_NS][index] = value;
4092         }
4093     }
4094 }
4095 
pmsav8r_regn_read(CPUARMState * env,const ARMCPRegInfo * ri)4096 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
4097 {
4098     ARMCPU *cpu = env_archcpu(env);
4099     uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4100                     (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4101 
4102     if (ri->opc1 & 4) {
4103         if (index >= cpu->pmsav8r_hdregion) {
4104             return 0x0;
4105         }
4106         if (ri->opc2 & 0x1) {
4107             return env->pmsav8.hprlar[index];
4108         } else {
4109             return env->pmsav8.hprbar[index];
4110         }
4111     } else {
4112         if (index >= cpu->pmsav7_dregion) {
4113             return 0x0;
4114         }
4115         if (ri->opc2 & 0x1) {
4116             return env->pmsav8.rlar[M_REG_NS][index];
4117         } else {
4118             return env->pmsav8.rbar[M_REG_NS][index];
4119         }
4120     }
4121 }
4122 
4123 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
4124     { .name = "PRBAR",
4125       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
4126       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4127       .accessfn = access_tvm_trvm,
4128       .readfn = prbar_read, .writefn = prbar_write },
4129     { .name = "PRLAR",
4130       .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
4131       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4132       .accessfn = access_tvm_trvm,
4133       .readfn = prlar_read, .writefn = prlar_write },
4134     { .name = "PRSELR", .resetvalue = 0,
4135       .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
4136       .access = PL1_RW, .accessfn = access_tvm_trvm,
4137       .writefn = prselr_write,
4138       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
4139     { .name = "HPRBAR", .resetvalue = 0,
4140       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
4141       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4142       .readfn = hprbar_read, .writefn = hprbar_write },
4143     { .name = "HPRLAR",
4144       .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
4145       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4146       .readfn = hprlar_read, .writefn = hprlar_write },
4147     { .name = "HPRSELR", .resetvalue = 0,
4148       .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
4149       .access = PL2_RW,
4150       .writefn = hprselr_write,
4151       .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
4152     { .name = "HPRENR",
4153       .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
4154       .access = PL2_RW, .type = ARM_CP_NO_RAW,
4155       .readfn = hprenr_read, .writefn = hprenr_write },
4156 };
4157 
4158 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
4159     /*
4160      * Reset for all these registers is handled in arm_cpu_reset(),
4161      * because the PMSAv7 is also used by M-profile CPUs, which do
4162      * not register cpregs but still need the state to be reset.
4163      */
4164     { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4165       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4166       .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
4167       .readfn = pmsav7_read, .writefn = pmsav7_write,
4168       .resetfn = arm_cp_reset_ignore },
4169     { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4170       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4171       .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
4172       .readfn = pmsav7_read, .writefn = pmsav7_write,
4173       .resetfn = arm_cp_reset_ignore },
4174     { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4175       .access = PL1_RW, .type = ARM_CP_NO_RAW,
4176       .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
4177       .readfn = pmsav7_read, .writefn = pmsav7_write,
4178       .resetfn = arm_cp_reset_ignore },
4179     { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4180       .access = PL1_RW,
4181       .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
4182       .writefn = pmsav7_rgnr_write,
4183       .resetfn = arm_cp_reset_ignore },
4184 };
4185 
4186 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4187     { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4188       .access = PL1_RW, .type = ARM_CP_ALIAS,
4189       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4190       .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4191     { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4192       .access = PL1_RW, .type = ARM_CP_ALIAS,
4193       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4194       .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4195     { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4196       .access = PL1_RW,
4197       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4198       .resetvalue = 0, },
4199     { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4200       .access = PL1_RW,
4201       .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4202       .resetvalue = 0, },
4203     { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4204       .access = PL1_RW,
4205       .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4206     { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4207       .access = PL1_RW,
4208       .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
4209     /* Protection region base and size registers */
4210     { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4211       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4212       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4213     { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4214       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4215       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4216     { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4217       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4218       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4219     { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4220       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4221       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4222     { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4223       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4224       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4225     { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4226       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4227       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4228     { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4229       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4230       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4231     { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4232       .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4233       .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4234 };
4235 
vmsa_ttbcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4236 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4237                              uint64_t value)
4238 {
4239     ARMCPU *cpu = env_archcpu(env);
4240 
4241     if (!arm_feature(env, ARM_FEATURE_V8)) {
4242         if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4243             /*
4244              * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4245              * using Long-descriptor translation table format
4246              */
4247             value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4248         } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4249             /*
4250              * In an implementation that includes the Security Extensions
4251              * TTBCR has additional fields PD0 [4] and PD1 [5] for
4252              * Short-descriptor translation table format.
4253              */
4254             value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4255         } else {
4256             value &= TTBCR_N;
4257         }
4258     }
4259 
4260     if (arm_feature(env, ARM_FEATURE_LPAE)) {
4261         /*
4262          * With LPAE the TTBCR could result in a change of ASID
4263          * via the TTBCR.A1 bit, so do a TLB flush.
4264          */
4265         tlb_flush(CPU(cpu));
4266     }
4267     raw_write(env, ri, value);
4268 }
4269 
vmsa_tcr_el12_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4270 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4271                                uint64_t value)
4272 {
4273     ARMCPU *cpu = env_archcpu(env);
4274 
4275     /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4276     tlb_flush(CPU(cpu));
4277     raw_write(env, ri, value);
4278 }
4279 
vmsa_ttbr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4280 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4281                             uint64_t value)
4282 {
4283     /* If the ASID changes (with a 64-bit write), we must flush the TLB.  */
4284     if (cpreg_field_is_64bit(ri) &&
4285         extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4286         ARMCPU *cpu = env_archcpu(env);
4287         tlb_flush(CPU(cpu));
4288     }
4289     raw_write(env, ri, value);
4290 }
4291 
vmsa_tcr_ttbr_el2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4292 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4293                                     uint64_t value)
4294 {
4295     /*
4296      * If we are running with E2&0 regime, then an ASID is active.
4297      * Flush if that might be changing.  Note we're not checking
4298      * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4299      * holds the active ASID, only checking the field that might.
4300      */
4301     if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4302         (arm_hcr_el2_eff(env) & HCR_E2H)) {
4303         uint16_t mask = ARMMMUIdxBit_E20_2 |
4304                         ARMMMUIdxBit_E20_2_PAN |
4305                         ARMMMUIdxBit_E20_0;
4306         tlb_flush_by_mmuidx(env_cpu(env), mask);
4307     }
4308     raw_write(env, ri, value);
4309 }
4310 
vttbr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4311 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4312                         uint64_t value)
4313 {
4314     ARMCPU *cpu = env_archcpu(env);
4315     CPUState *cs = CPU(cpu);
4316 
4317     /*
4318      * A change in VMID to the stage2 page table (Stage2) invalidates
4319      * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4320      */
4321     if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4322         tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4323     }
4324     raw_write(env, ri, value);
4325 }
4326 
4327 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4328     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4329       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4330       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4331                              offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4332     { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4333       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4334       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4335                              offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4336     { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4337       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4338       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4339                              offsetof(CPUARMState, cp15.dfar_ns) } },
4340     { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4341       .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4342       .access = PL1_RW, .accessfn = access_tvm_trvm,
4343       .fgt = FGT_FAR_EL1,
4344       .nv2_redirect_offset = 0x220 | NV2_REDIR_NV1,
4345       .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4346       .resetvalue = 0, },
4347 };
4348 
4349 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4350     { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4351       .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4352       .access = PL1_RW, .accessfn = access_tvm_trvm,
4353       .fgt = FGT_ESR_EL1,
4354       .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1,
4355       .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4356     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4357       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4358       .access = PL1_RW, .accessfn = access_tvm_trvm,
4359       .fgt = FGT_TTBR0_EL1,
4360       .nv2_redirect_offset = 0x200 | NV2_REDIR_NV1,
4361       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4362       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4363                              offsetof(CPUARMState, cp15.ttbr0_ns) } },
4364     { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4365       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4366       .access = PL1_RW, .accessfn = access_tvm_trvm,
4367       .fgt = FGT_TTBR1_EL1,
4368       .nv2_redirect_offset = 0x210 | NV2_REDIR_NV1,
4369       .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4370       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4371                              offsetof(CPUARMState, cp15.ttbr1_ns) } },
4372     { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4373       .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4374       .access = PL1_RW, .accessfn = access_tvm_trvm,
4375       .fgt = FGT_TCR_EL1,
4376       .nv2_redirect_offset = 0x120 | NV2_REDIR_NV1,
4377       .writefn = vmsa_tcr_el12_write,
4378       .raw_writefn = raw_write,
4379       .resetvalue = 0,
4380       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4381     { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4382       .access = PL1_RW, .accessfn = access_tvm_trvm,
4383       .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4384       .raw_writefn = raw_write,
4385       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4386                              offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4387 };
4388 
4389 /*
4390  * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4391  * qemu tlbs nor adjusting cached masks.
4392  */
4393 static const ARMCPRegInfo ttbcr2_reginfo = {
4394     .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4395     .access = PL1_RW, .accessfn = access_tvm_trvm,
4396     .type = ARM_CP_ALIAS,
4397     .bank_fieldoffsets = {
4398         offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4399         offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4400     },
4401 };
4402 
omap_ticonfig_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4403 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4404                                 uint64_t value)
4405 {
4406     env->cp15.c15_ticonfig = value & 0xe7;
4407     /* The OS_TYPE bit in this register changes the reported CPUID! */
4408     env->cp15.c0_cpuid = (value & (1 << 5)) ?
4409         ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4410 }
4411 
omap_threadid_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4412 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4413                                 uint64_t value)
4414 {
4415     env->cp15.c15_threadid = value & 0xffff;
4416 }
4417 
omap_wfi_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4418 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4419                            uint64_t value)
4420 {
4421     /* Wait-for-interrupt (deprecated) */
4422     cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4423 }
4424 
omap_cachemaint_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4425 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4426                                   uint64_t value)
4427 {
4428     /*
4429      * On OMAP there are registers indicating the max/min index of dcache lines
4430      * containing a dirty line; cache flush operations have to reset these.
4431      */
4432     env->cp15.c15_i_max = 0x000;
4433     env->cp15.c15_i_min = 0xff0;
4434 }
4435 
4436 static const ARMCPRegInfo omap_cp_reginfo[] = {
4437     { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4438       .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4439       .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4440       .resetvalue = 0, },
4441     { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4442       .access = PL1_RW, .type = ARM_CP_NOP },
4443     { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4444       .access = PL1_RW,
4445       .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4446       .writefn = omap_ticonfig_write },
4447     { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4448       .access = PL1_RW,
4449       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4450     { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4451       .access = PL1_RW, .resetvalue = 0xff0,
4452       .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4453     { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4454       .access = PL1_RW,
4455       .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4456       .writefn = omap_threadid_write },
4457     { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4458       .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4459       .type = ARM_CP_NO_RAW,
4460       .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4461     /*
4462      * TODO: Peripheral port remap register:
4463      * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4464      * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4465      * when MMU is off.
4466      */
4467     { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4468       .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4469       .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4470       .writefn = omap_cachemaint_write },
4471     { .name = "C9", .cp = 15, .crn = 9,
4472       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4473       .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4474 };
4475 
xscale_cpar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4476 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4477                               uint64_t value)
4478 {
4479     env->cp15.c15_cpar = value & 0x3fff;
4480 }
4481 
4482 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4483     { .name = "XSCALE_CPAR",
4484       .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4485       .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4486       .writefn = xscale_cpar_write, },
4487     { .name = "XSCALE_AUXCR",
4488       .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4489       .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4490       .resetvalue = 0, },
4491     /*
4492      * XScale specific cache-lockdown: since we have no cache we NOP these
4493      * and hope the guest does not really rely on cache behaviour.
4494      */
4495     { .name = "XSCALE_LOCK_ICACHE_LINE",
4496       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4497       .access = PL1_W, .type = ARM_CP_NOP },
4498     { .name = "XSCALE_UNLOCK_ICACHE",
4499       .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4500       .access = PL1_W, .type = ARM_CP_NOP },
4501     { .name = "XSCALE_DCACHE_LOCK",
4502       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4503       .access = PL1_RW, .type = ARM_CP_NOP },
4504     { .name = "XSCALE_UNLOCK_DCACHE",
4505       .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4506       .access = PL1_W, .type = ARM_CP_NOP },
4507 };
4508 
4509 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4510     /*
4511      * RAZ/WI the whole crn=15 space, when we don't have a more specific
4512      * implementation of this implementation-defined space.
4513      * Ideally this should eventually disappear in favour of actually
4514      * implementing the correct behaviour for all cores.
4515      */
4516     { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4517       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4518       .access = PL1_RW,
4519       .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4520       .resetvalue = 0 },
4521 };
4522 
4523 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4524     /* Cache status: RAZ because we have no cache so it's always clean */
4525     { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4526       .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4527       .resetvalue = 0 },
4528 };
4529 
4530 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4531     /* We never have a block transfer operation in progress */
4532     { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4533       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4534       .resetvalue = 0 },
4535     /* The cache ops themselves: these all NOP for QEMU */
4536     { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4537       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4538     { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4539       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4540     { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4541       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4542     { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4543       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4544     { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4545       .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4546     { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4547       .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4548 };
4549 
4550 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4551     /*
4552      * The cache test-and-clean instructions always return (1 << 30)
4553      * to indicate that there are no dirty cache lines.
4554      */
4555     { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4556       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4557       .resetvalue = (1 << 30) },
4558     { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4559       .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4560       .resetvalue = (1 << 30) },
4561 };
4562 
4563 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4564     /* Ignore ReadBuffer accesses */
4565     { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4566       .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4567       .access = PL1_RW, .resetvalue = 0,
4568       .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4569 };
4570 
midr_read(CPUARMState * env,const ARMCPRegInfo * ri)4571 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4572 {
4573     unsigned int cur_el = arm_current_el(env);
4574 
4575     if (arm_is_el2_enabled(env) && cur_el == 1) {
4576         return env->cp15.vpidr_el2;
4577     }
4578     return raw_read(env, ri);
4579 }
4580 
mpidr_read_val(CPUARMState * env)4581 static uint64_t mpidr_read_val(CPUARMState *env)
4582 {
4583     ARMCPU *cpu = env_archcpu(env);
4584     uint64_t mpidr = cpu->mp_affinity;
4585 
4586     if (arm_feature(env, ARM_FEATURE_V7MP)) {
4587         mpidr |= (1U << 31);
4588         /*
4589          * Cores which are uniprocessor (non-coherent)
4590          * but still implement the MP extensions set
4591          * bit 30. (For instance, Cortex-R5).
4592          */
4593         if (cpu->mp_is_up) {
4594             mpidr |= (1u << 30);
4595         }
4596     }
4597     return mpidr;
4598 }
4599 
mpidr_read(CPUARMState * env,const ARMCPRegInfo * ri)4600 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4601 {
4602     unsigned int cur_el = arm_current_el(env);
4603 
4604     if (arm_is_el2_enabled(env) && cur_el == 1) {
4605         return env->cp15.vmpidr_el2;
4606     }
4607     return mpidr_read_val(env);
4608 }
4609 
4610 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4611     /* NOP AMAIR0/1 */
4612     { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4613       .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4614       .access = PL1_RW, .accessfn = access_tvm_trvm,
4615       .fgt = FGT_AMAIR_EL1,
4616       .nv2_redirect_offset = 0x148 | NV2_REDIR_NV1,
4617       .type = ARM_CP_CONST, .resetvalue = 0 },
4618     /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4619     { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4620       .access = PL1_RW, .accessfn = access_tvm_trvm,
4621       .type = ARM_CP_CONST, .resetvalue = 0 },
4622     { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4623       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4624       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4625                              offsetof(CPUARMState, cp15.par_ns)} },
4626     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4627       .access = PL1_RW, .accessfn = access_tvm_trvm,
4628       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4629       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4630                              offsetof(CPUARMState, cp15.ttbr0_ns) },
4631       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4632     { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4633       .access = PL1_RW, .accessfn = access_tvm_trvm,
4634       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4635       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4636                              offsetof(CPUARMState, cp15.ttbr1_ns) },
4637       .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4638 };
4639 
aa64_fpcr_read(CPUARMState * env,const ARMCPRegInfo * ri)4640 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4641 {
4642     return vfp_get_fpcr(env);
4643 }
4644 
aa64_fpcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4645 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4646                             uint64_t value)
4647 {
4648     vfp_set_fpcr(env, value);
4649 }
4650 
aa64_fpsr_read(CPUARMState * env,const ARMCPRegInfo * ri)4651 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4652 {
4653     return vfp_get_fpsr(env);
4654 }
4655 
aa64_fpsr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4656 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4657                             uint64_t value)
4658 {
4659     vfp_set_fpsr(env, value);
4660 }
4661 
aa64_daif_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4662 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4663                                        bool isread)
4664 {
4665     if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4666         return CP_ACCESS_TRAP_EL1;
4667     }
4668     return CP_ACCESS_OK;
4669 }
4670 
aa64_daif_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4671 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4672                             uint64_t value)
4673 {
4674     env->daif = value & PSTATE_DAIF;
4675 }
4676 
aa64_pan_read(CPUARMState * env,const ARMCPRegInfo * ri)4677 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4678 {
4679     return env->pstate & PSTATE_PAN;
4680 }
4681 
aa64_pan_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4682 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4683                            uint64_t value)
4684 {
4685     env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4686 }
4687 
4688 static const ARMCPRegInfo pan_reginfo = {
4689     .name = "PAN", .state = ARM_CP_STATE_AA64,
4690     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4691     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4692     .readfn = aa64_pan_read, .writefn = aa64_pan_write
4693 };
4694 
aa64_uao_read(CPUARMState * env,const ARMCPRegInfo * ri)4695 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4696 {
4697     return env->pstate & PSTATE_UAO;
4698 }
4699 
aa64_uao_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4700 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4701                            uint64_t value)
4702 {
4703     env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4704 }
4705 
4706 static const ARMCPRegInfo uao_reginfo = {
4707     .name = "UAO", .state = ARM_CP_STATE_AA64,
4708     .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4709     .type = ARM_CP_NO_RAW, .access = PL1_RW,
4710     .readfn = aa64_uao_read, .writefn = aa64_uao_write
4711 };
4712 
aa64_dit_read(CPUARMState * env,const ARMCPRegInfo * ri)4713 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4714 {
4715     return env->pstate & PSTATE_DIT;
4716 }
4717 
aa64_dit_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4718 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4719                            uint64_t value)
4720 {
4721     env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4722 }
4723 
4724 static const ARMCPRegInfo dit_reginfo = {
4725     .name = "DIT", .state = ARM_CP_STATE_AA64,
4726     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4727     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4728     .readfn = aa64_dit_read, .writefn = aa64_dit_write
4729 };
4730 
aa64_ssbs_read(CPUARMState * env,const ARMCPRegInfo * ri)4731 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4732 {
4733     return env->pstate & PSTATE_SSBS;
4734 }
4735 
aa64_ssbs_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4736 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4737                            uint64_t value)
4738 {
4739     env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4740 }
4741 
4742 static const ARMCPRegInfo ssbs_reginfo = {
4743     .name = "SSBS", .state = ARM_CP_STATE_AA64,
4744     .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4745     .type = ARM_CP_NO_RAW, .access = PL0_RW,
4746     .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4747 };
4748 
aa64_cacheop_poc_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4749 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4750                                               const ARMCPRegInfo *ri,
4751                                               bool isread)
4752 {
4753     /* Cache invalidate/clean to Point of Coherency or Persistence...  */
4754     switch (arm_current_el(env)) {
4755     case 0:
4756         /* ... EL0 must trap to EL1 unless SCTLR_EL1.UCI is set.  */
4757         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4758             return CP_ACCESS_TRAP_EL1;
4759         }
4760         /* fall through */
4761     case 1:
4762         /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set.  */
4763         if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4764             return CP_ACCESS_TRAP_EL2;
4765         }
4766         break;
4767     }
4768     return CP_ACCESS_OK;
4769 }
4770 
do_cacheop_pou_access(CPUARMState * env,uint64_t hcrflags)4771 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4772 {
4773     /* Cache invalidate/clean to Point of Unification... */
4774     switch (arm_current_el(env)) {
4775     case 0:
4776         /* ... EL0 must trap to EL1 unless SCTLR_EL1.UCI is set.  */
4777         if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4778             return CP_ACCESS_TRAP_EL1;
4779         }
4780         /* fall through */
4781     case 1:
4782         /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set.  */
4783         if (arm_hcr_el2_eff(env) & hcrflags) {
4784             return CP_ACCESS_TRAP_EL2;
4785         }
4786         break;
4787     }
4788     return CP_ACCESS_OK;
4789 }
4790 
access_ticab(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4791 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4792                                    bool isread)
4793 {
4794     return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4795 }
4796 
access_tocu(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4797 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4798                                   bool isread)
4799 {
4800     return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4801 }
4802 
aa64_zva_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4803 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4804                                       bool isread)
4805 {
4806     int cur_el = arm_current_el(env);
4807 
4808     if (cur_el < 2) {
4809         uint64_t hcr = arm_hcr_el2_eff(env);
4810 
4811         if (cur_el == 0) {
4812             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4813                 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4814                     return CP_ACCESS_TRAP_EL2;
4815                 }
4816             } else {
4817                 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4818                     return CP_ACCESS_TRAP_EL1;
4819                 }
4820                 if (hcr & HCR_TDZ) {
4821                     return CP_ACCESS_TRAP_EL2;
4822                 }
4823             }
4824         } else if (hcr & HCR_TDZ) {
4825             return CP_ACCESS_TRAP_EL2;
4826         }
4827     }
4828     return CP_ACCESS_OK;
4829 }
4830 
aa64_dczid_read(CPUARMState * env,const ARMCPRegInfo * ri)4831 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4832 {
4833     ARMCPU *cpu = env_archcpu(env);
4834     int dzp_bit = 1 << 4;
4835 
4836     /* DZP indicates whether DC ZVA access is allowed */
4837     if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4838         dzp_bit = 0;
4839     }
4840     return cpu->dcz_blocksize | dzp_bit;
4841 }
4842 
sp_el0_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4843 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4844                                     bool isread)
4845 {
4846     if (!(env->pstate & PSTATE_SP)) {
4847         /*
4848          * Access to SP_EL0 is undefined if it's being used as
4849          * the stack pointer.
4850          */
4851         return CP_ACCESS_UNDEFINED;
4852     }
4853     return CP_ACCESS_OK;
4854 }
4855 
spsel_read(CPUARMState * env,const ARMCPRegInfo * ri)4856 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4857 {
4858     return env->pstate & PSTATE_SP;
4859 }
4860 
spsel_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t val)4861 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4862 {
4863     update_spsel(env, val);
4864 }
4865 
sctlr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4866 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4867                         uint64_t value)
4868 {
4869     ARMCPU *cpu = env_archcpu(env);
4870 
4871     if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4872         /* M bit is RAZ/WI for PMSA with no MPU implemented */
4873         value &= ~SCTLR_M;
4874     }
4875 
4876     /* ??? Lots of these bits are not implemented.  */
4877 
4878     if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4879         if (ri->opc1 == 6) { /* SCTLR_EL3 */
4880             value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4881         } else {
4882             value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4883                        SCTLR_ATA0 | SCTLR_ATA);
4884         }
4885     }
4886 
4887     if (raw_read(env, ri) == value) {
4888         /*
4889          * Skip the TLB flush if nothing actually changed; Linux likes
4890          * to do a lot of pointless SCTLR writes.
4891          */
4892         return;
4893     }
4894 
4895     raw_write(env, ri, value);
4896 
4897     /* This may enable/disable the MMU, so do a TLB flush.  */
4898     tlb_flush(CPU(cpu));
4899 
4900     if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
4901         /*
4902          * Normally we would always end the TB on an SCTLR write; see the
4903          * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4904          * is special.  Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4905          * of hflags from the translator, so do it here.
4906          */
4907         arm_rebuild_hflags(env);
4908     }
4909 }
4910 
mdcr_el3_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4911 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4912                            uint64_t value)
4913 {
4914     /*
4915      * Some MDCR_EL3 bits affect whether PMU counters are running:
4916      * if we are trying to change any of those then we must
4917      * bracket this update with PMU start/finish calls.
4918      */
4919     bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
4920 
4921     if (pmu_op) {
4922         pmu_op_start(env);
4923     }
4924     env->cp15.mdcr_el3 = value;
4925     if (pmu_op) {
4926         pmu_op_finish(env);
4927     }
4928 }
4929 
sdcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4930 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4931                        uint64_t value)
4932 {
4933     /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
4934     mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
4935 }
4936 
mdcr_el2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4937 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4938                            uint64_t value)
4939 {
4940     /*
4941      * Some MDCR_EL2 bits affect whether PMU counters are running:
4942      * if we are trying to change any of those then we must
4943      * bracket this update with PMU start/finish calls.
4944      */
4945     bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
4946 
4947     if (pmu_op) {
4948         pmu_op_start(env);
4949     }
4950     env->cp15.mdcr_el2 = value;
4951     if (pmu_op) {
4952         pmu_op_finish(env);
4953     }
4954 }
4955 
access_nv1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4956 static CPAccessResult access_nv1(CPUARMState *env, const ARMCPRegInfo *ri,
4957                                  bool isread)
4958 {
4959     if (arm_current_el(env) == 1) {
4960         uint64_t hcr_nv = arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1 | HCR_NV2);
4961 
4962         if (hcr_nv == (HCR_NV | HCR_NV1)) {
4963             return CP_ACCESS_TRAP_EL2;
4964         }
4965     }
4966     return CP_ACCESS_OK;
4967 }
4968 
4969 #ifdef CONFIG_USER_ONLY
4970 /*
4971  * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
4972  * code to get around W^X restrictions, where one region is writable and the
4973  * other is executable.
4974  *
4975  * Since the executable region is never written to we cannot detect code
4976  * changes when running in user mode, and rely on the emulated JIT telling us
4977  * that the code has changed by executing this instruction.
4978  */
ic_ivau_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4979 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
4980                           uint64_t value)
4981 {
4982     uint64_t icache_line_mask, start_address, end_address;
4983     const ARMCPU *cpu;
4984 
4985     cpu = env_archcpu(env);
4986 
4987     icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
4988     start_address = value & ~icache_line_mask;
4989     end_address = value | icache_line_mask;
4990 
4991     mmap_lock();
4992 
4993     tb_invalidate_phys_range(env_cpu(env), start_address, end_address);
4994 
4995     mmap_unlock();
4996 }
4997 #endif
4998 
4999 static const ARMCPRegInfo v8_cp_reginfo[] = {
5000     /*
5001      * Minimal set of EL0-visible registers. This will need to be expanded
5002      * significantly for system emulation of AArch64 CPUs.
5003      */
5004     { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5005       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5006       .access = PL0_RW, .type = ARM_CP_NZCV },
5007     { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5008       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5009       .type = ARM_CP_NO_RAW,
5010       .access = PL0_RW, .accessfn = aa64_daif_access,
5011       .fieldoffset = offsetof(CPUARMState, daif),
5012       .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5013     { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5014       .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5015       .access = PL0_RW, .type = ARM_CP_FPU,
5016       .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5017     { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5018       .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5019       .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5020       .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5021     { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5022       .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5023       .access = PL0_R, .type = ARM_CP_NO_RAW,
5024       .fgt = FGT_DCZID_EL0,
5025       .readfn = aa64_dczid_read },
5026     { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5027       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5028       .access = PL0_W, .type = ARM_CP_DC_ZVA,
5029 #ifndef CONFIG_USER_ONLY
5030       /* Avoid overhead of an access check that always passes in user-mode */
5031       .accessfn = aa64_zva_access,
5032       .fgt = FGT_DCZVA,
5033 #endif
5034     },
5035     { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5036       .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5037       .access = PL1_R, .type = ARM_CP_CURRENTEL },
5038     /*
5039      * Instruction cache ops. All of these except `IC IVAU` NOP because we
5040      * don't emulate caches.
5041      */
5042     { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5043       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5044       .access = PL1_W, .type = ARM_CP_NOP,
5045       .fgt = FGT_ICIALLUIS,
5046       .accessfn = access_ticab },
5047     { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5048       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5049       .access = PL1_W, .type = ARM_CP_NOP,
5050       .fgt = FGT_ICIALLU,
5051       .accessfn = access_tocu },
5052     { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5053       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5054       .access = PL0_W,
5055       .fgt = FGT_ICIVAU,
5056       .accessfn = access_tocu,
5057 #ifdef CONFIG_USER_ONLY
5058       .type = ARM_CP_NO_RAW,
5059       .writefn = ic_ivau_write
5060 #else
5061       .type = ARM_CP_NOP
5062 #endif
5063     },
5064     /* Cache ops: all NOPs since we don't emulate caches */
5065     { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5066       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5067       .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5068       .fgt = FGT_DCIVAC,
5069       .type = ARM_CP_NOP },
5070     { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5071       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5072       .fgt = FGT_DCISW,
5073       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5074     { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5075       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5076       .access = PL0_W, .type = ARM_CP_NOP,
5077       .fgt = FGT_DCCVAC,
5078       .accessfn = aa64_cacheop_poc_access },
5079     { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5080       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5081       .fgt = FGT_DCCSW,
5082       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5083     { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5084       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5085       .access = PL0_W, .type = ARM_CP_NOP,
5086       .fgt = FGT_DCCVAU,
5087       .accessfn = access_tocu },
5088     { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5089       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5090       .access = PL0_W, .type = ARM_CP_NOP,
5091       .fgt = FGT_DCCIVAC,
5092       .accessfn = aa64_cacheop_poc_access },
5093     { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5094       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5095       .fgt = FGT_DCCISW,
5096       .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5097 #ifndef CONFIG_USER_ONLY
5098     /* 64 bit address translation operations */
5099     { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5100       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5101       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5102       .fgt = FGT_ATS1E1R,
5103       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5104     { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5105       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5106       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5107       .fgt = FGT_ATS1E1W,
5108       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5109     { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5110       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5111       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5112       .fgt = FGT_ATS1E0R,
5113       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5114     { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5115       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5116       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5117       .fgt = FGT_ATS1E0W,
5118       .accessfn = at_s1e01_access, .writefn = ats_write64 },
5119     { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5120       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5121       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5122       .accessfn = at_e012_access, .writefn = ats_write64 },
5123     { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5124       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5125       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5126       .accessfn = at_e012_access, .writefn = ats_write64 },
5127     { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5128       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5129       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5130       .accessfn = at_e012_access, .writefn = ats_write64 },
5131     { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5132       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5133       .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5134       .accessfn = at_e012_access, .writefn = ats_write64 },
5135     /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5136     { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5137       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5138       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5139       .writefn = ats_write64 },
5140     { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5141       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5142       .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5143       .writefn = ats_write64 },
5144     { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5145       .type = ARM_CP_ALIAS,
5146       .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5147       .access = PL1_RW, .resetvalue = 0,
5148       .fgt = FGT_PAR_EL1,
5149       .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5150       .writefn = par_write },
5151 #endif
5152     /* 32 bit cache operations */
5153     { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5154       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5155     { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5156       .type = ARM_CP_NOP, .access = PL1_W },
5157     { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5158       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5159     { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5160       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5161     { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5162       .type = ARM_CP_NOP, .access = PL1_W },
5163     { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5164       .type = ARM_CP_NOP, .access = PL1_W },
5165     { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5166       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5167     { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5168       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5169     { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5170       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5171     { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5172       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5173     { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5174       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5175     { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5176       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5177     { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5178       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5179     /* MMU Domain access control / MPU write buffer control */
5180     { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5181       .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5182       .writefn = dacr_write, .raw_writefn = raw_write,
5183       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5184                              offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5185     { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5186       .type = ARM_CP_ALIAS,
5187       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5188       .access = PL1_RW, .accessfn = access_nv1,
5189       .nv2_redirect_offset = 0x230 | NV2_REDIR_NV1,
5190       .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5191     { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5192       .type = ARM_CP_ALIAS,
5193       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5194       .access = PL1_RW, .accessfn = access_nv1,
5195       .nv2_redirect_offset = 0x160 | NV2_REDIR_NV1,
5196       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5197     /*
5198      * We rely on the access checks not allowing the guest to write to the
5199      * state field when SPSel indicates that it's being used as the stack
5200      * pointer.
5201      */
5202     { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5203       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5204       .access = PL1_RW, .accessfn = sp_el0_access,
5205       .type = ARM_CP_ALIAS,
5206       .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5207     { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5208       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5209       .nv2_redirect_offset = 0x240,
5210       .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5211       .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5212     { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5213       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5214       .type = ARM_CP_NO_RAW,
5215       .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5216     { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5217       .type = ARM_CP_ALIAS,
5218       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5219       .access = PL2_RW,
5220       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5221     { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5222       .type = ARM_CP_ALIAS,
5223       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5224       .access = PL2_RW,
5225       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5226     { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5227       .type = ARM_CP_ALIAS,
5228       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5229       .access = PL2_RW,
5230       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5231     { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5232       .type = ARM_CP_ALIAS,
5233       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5234       .access = PL2_RW,
5235       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5236     { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5237       .type = ARM_CP_IO,
5238       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5239       .resetvalue = 0,
5240       .access = PL3_RW,
5241       .writefn = mdcr_el3_write,
5242       .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5243     { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5244       .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5245       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5246       .writefn = sdcr_write,
5247       .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5248 };
5249 
5250 /* These are present only when EL1 supports AArch32 */
5251 static const ARMCPRegInfo v8_aa32_el1_reginfo[] = {
5252     { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5253       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5254       .access = PL2_RW,
5255       .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5256       .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5257     { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5258       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5259       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5260       .writefn = dacr_write, .raw_writefn = raw_write,
5261       .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5262     { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5263       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5264       .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5265       .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5266 };
5267 
do_hcr_write(CPUARMState * env,uint64_t value,uint64_t valid_mask)5268 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5269 {
5270     ARMCPU *cpu = env_archcpu(env);
5271 
5272     if (arm_feature(env, ARM_FEATURE_V8)) {
5273         valid_mask |= MAKE_64BIT_MASK(0, 34);  /* ARMv8.0 */
5274     } else {
5275         valid_mask |= MAKE_64BIT_MASK(0, 28);  /* ARMv7VE */
5276     }
5277 
5278     if (arm_feature(env, ARM_FEATURE_EL3)) {
5279         valid_mask &= ~HCR_HCD;
5280     } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5281         /*
5282          * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5283          * However, if we're using the SMC PSCI conduit then QEMU is
5284          * effectively acting like EL3 firmware and so the guest at
5285          * EL2 should retain the ability to prevent EL1 from being
5286          * able to make SMC calls into the ersatz firmware, so in
5287          * that case HCR.TSC should be read/write.
5288          */
5289         valid_mask &= ~HCR_TSC;
5290     }
5291 
5292     if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5293         if (cpu_isar_feature(aa64_vh, cpu)) {
5294             valid_mask |= HCR_E2H;
5295         }
5296         if (cpu_isar_feature(aa64_ras, cpu)) {
5297             valid_mask |= HCR_TERR | HCR_TEA;
5298         }
5299         if (cpu_isar_feature(aa64_lor, cpu)) {
5300             valid_mask |= HCR_TLOR;
5301         }
5302         if (cpu_isar_feature(aa64_pauth, cpu)) {
5303             valid_mask |= HCR_API | HCR_APK;
5304         }
5305         if (cpu_isar_feature(aa64_mte, cpu)) {
5306             valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5307         }
5308         if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5309             valid_mask |= HCR_ENSCXT;
5310         }
5311         if (cpu_isar_feature(aa64_fwb, cpu)) {
5312             valid_mask |= HCR_FWB;
5313         }
5314         if (cpu_isar_feature(aa64_rme, cpu)) {
5315             valid_mask |= HCR_GPF;
5316         }
5317         if (cpu_isar_feature(aa64_nv, cpu)) {
5318             valid_mask |= HCR_NV | HCR_NV1 | HCR_AT;
5319         }
5320         if (cpu_isar_feature(aa64_nv2, cpu)) {
5321             valid_mask |= HCR_NV2;
5322         }
5323     }
5324 
5325     if (cpu_isar_feature(any_evt, cpu)) {
5326         valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5327     } else if (cpu_isar_feature(any_half_evt, cpu)) {
5328         valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5329     }
5330 
5331     /* Clear RES0 bits.  */
5332     value &= valid_mask;
5333 
5334     /* RW is RAO/WI if EL1 is AArch64 only */
5335     if (!cpu_isar_feature(aa64_aa32_el1, cpu)) {
5336         value |= HCR_RW;
5337     }
5338 
5339     /*
5340      * These bits change the MMU setup:
5341      * HCR_VM enables stage 2 translation
5342      * HCR_PTW forbids certain page-table setups
5343      * HCR_DC disables stage1 and enables stage2 translation
5344      * HCR_DCT enables tagging on (disabled) stage1 translation
5345      * HCR_FWB changes the interpretation of stage2 descriptor bits
5346      * HCR_NV and HCR_NV1 affect interpretation of descriptor bits
5347      */
5348     if ((env->cp15.hcr_el2 ^ value) &
5349         (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) {
5350         tlb_flush(CPU(cpu));
5351     }
5352     env->cp15.hcr_el2 = value;
5353 
5354     /*
5355      * Updates to VI and VF require us to update the status of
5356      * virtual interrupts, which are the logical OR of these bits
5357      * and the state of the input lines from the GIC. (This requires
5358      * that we have the BQL, which is done by marking the
5359      * reginfo structs as ARM_CP_IO.)
5360      * Note that if a write to HCR pends a VIRQ or VFIQ or VINMI or
5361      * VFNMI, it is never possible for it to be taken immediately
5362      * because VIRQ, VFIQ, VINMI and VFNMI are masked unless running
5363      * at EL0 or EL1, and HCR can only be written at EL2.
5364      */
5365     g_assert(bql_locked());
5366     arm_cpu_update_virq(cpu);
5367     arm_cpu_update_vfiq(cpu);
5368     arm_cpu_update_vserr(cpu);
5369     if (cpu_isar_feature(aa64_nmi, cpu)) {
5370         arm_cpu_update_vinmi(cpu);
5371         arm_cpu_update_vfnmi(cpu);
5372     }
5373 }
5374 
hcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5375 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5376 {
5377     do_hcr_write(env, value, 0);
5378 }
5379 
hcr_writehigh(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5380 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5381                           uint64_t value)
5382 {
5383     /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5384     value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5385     do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5386 }
5387 
hcr_writelow(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5388 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5389                          uint64_t value)
5390 {
5391     /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5392     value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5393     do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5394 }
5395 
hcr_reset(CPUARMState * env,const ARMCPRegInfo * ri)5396 static void hcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
5397 {
5398     /* hcr_write will set the RES1 bits on an AArch64-only CPU */
5399     hcr_write(env, ri, 0);
5400 }
5401 
5402 /*
5403  * Return the effective value of HCR_EL2, at the given security state.
5404  * Bits that are not included here:
5405  * RW       (read from SCR_EL3.RW as needed)
5406  */
arm_hcr_el2_eff_secstate(CPUARMState * env,ARMSecuritySpace space)5407 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
5408 {
5409     uint64_t ret = env->cp15.hcr_el2;
5410 
5411     assert(space != ARMSS_Root);
5412 
5413     if (!arm_is_el2_enabled_secstate(env, space)) {
5414         /*
5415          * "This register has no effect if EL2 is not enabled in the
5416          * current Security state".  This is ARMv8.4-SecEL2 speak for
5417          * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5418          *
5419          * Prior to that, the language was "In an implementation that
5420          * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5421          * as if this field is 0 for all purposes other than a direct
5422          * read or write access of HCR_EL2".  With lots of enumeration
5423          * on a per-field basis.  In current QEMU, this is condition
5424          * is arm_is_secure_below_el3.
5425          *
5426          * Since the v8.4 language applies to the entire register, and
5427          * appears to be backward compatible, use that.
5428          */
5429         return 0;
5430     }
5431 
5432     /*
5433      * For a cpu that supports both aarch64 and aarch32, we can set bits
5434      * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5435      * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5436      */
5437     if (!arm_el_is_aa64(env, 2)) {
5438         uint64_t aa32_valid;
5439 
5440         /*
5441          * These bits are up-to-date as of ARMv8.6.
5442          * For HCR, it's easiest to list just the 2 bits that are invalid.
5443          * For HCR2, list those that are valid.
5444          */
5445         aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5446         aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5447                        HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5448         ret &= aa32_valid;
5449     }
5450 
5451     if (ret & HCR_TGE) {
5452         /* These bits are up-to-date as of ARMv8.6.  */
5453         if (ret & HCR_E2H) {
5454             ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5455                      HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5456                      HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5457                      HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5458                      HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5459                      HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5460         } else {
5461             ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5462         }
5463         ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5464                  HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5465                  HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5466                  HCR_TLOR);
5467     }
5468 
5469     return ret;
5470 }
5471 
arm_hcr_el2_eff(CPUARMState * env)5472 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5473 {
5474     if (arm_feature(env, ARM_FEATURE_M)) {
5475         return 0;
5476     }
5477     return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
5478 }
5479 
5480 /*
5481  * Corresponds to ARM pseudocode function ELIsInHost().
5482  */
el_is_in_host(CPUARMState * env,int el)5483 bool el_is_in_host(CPUARMState *env, int el)
5484 {
5485     uint64_t mask;
5486 
5487     /*
5488      * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5489      * Perform the simplest bit tests first, and validate EL2 afterward.
5490      */
5491     if (el & 1) {
5492         return false; /* EL1 or EL3 */
5493     }
5494 
5495     /*
5496      * Note that hcr_write() checks isar_feature_aa64_vh(),
5497      * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5498      */
5499     mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5500     if ((env->cp15.hcr_el2 & mask) != mask) {
5501         return false;
5502     }
5503 
5504     /* TGE and/or E2H set: double check those bits are currently legal. */
5505     return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5506 }
5507 
hcrx_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5508 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5509                        uint64_t value)
5510 {
5511     ARMCPU *cpu = env_archcpu(env);
5512     uint64_t valid_mask = 0;
5513 
5514     /* FEAT_MOPS adds MSCEn and MCE2 */
5515     if (cpu_isar_feature(aa64_mops, cpu)) {
5516         valid_mask |= HCRX_MSCEN | HCRX_MCE2;
5517     }
5518 
5519     /* FEAT_NMI adds TALLINT, VINMI and VFNMI */
5520     if (cpu_isar_feature(aa64_nmi, cpu)) {
5521         valid_mask |= HCRX_TALLINT | HCRX_VINMI | HCRX_VFNMI;
5522     }
5523     /* FEAT_CMOW adds CMOW */
5524     if (cpu_isar_feature(aa64_cmow, cpu)) {
5525         valid_mask |= HCRX_CMOW;
5526     }
5527     /* FEAT_XS adds FGTnXS, FnXS */
5528     if (cpu_isar_feature(aa64_xs, cpu)) {
5529         valid_mask |= HCRX_FGTNXS | HCRX_FNXS;
5530     }
5531 
5532     /* Clear RES0 bits.  */
5533     env->cp15.hcrx_el2 = value & valid_mask;
5534 
5535     /*
5536      * Updates to VINMI and VFNMI require us to update the status of
5537      * virtual NMI, which are the logical OR of these bits
5538      * and the state of the input lines from the GIC. (This requires
5539      * that we have the BQL, which is done by marking the
5540      * reginfo structs as ARM_CP_IO.)
5541      * Note that if a write to HCRX pends a VINMI or VFNMI it is never
5542      * possible for it to be taken immediately, because VINMI and
5543      * VFNMI are masked unless running at EL0 or EL1, and HCRX
5544      * can only be written at EL2.
5545      */
5546     if (cpu_isar_feature(aa64_nmi, cpu)) {
5547         g_assert(bql_locked());
5548         arm_cpu_update_vinmi(cpu);
5549         arm_cpu_update_vfnmi(cpu);
5550     }
5551 }
5552 
access_hxen(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)5553 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5554                                   bool isread)
5555 {
5556     if (arm_current_el(env) == 2
5557         && arm_feature(env, ARM_FEATURE_EL3)
5558         && !(env->cp15.scr_el3 & SCR_HXEN)) {
5559         return CP_ACCESS_TRAP_EL3;
5560     }
5561     return CP_ACCESS_OK;
5562 }
5563 
5564 static const ARMCPRegInfo hcrx_el2_reginfo = {
5565     .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5566     .type = ARM_CP_IO,
5567     .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5568     .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5569     .nv2_redirect_offset = 0xa0,
5570     .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5571 };
5572 
5573 /* Return the effective value of HCRX_EL2.  */
arm_hcrx_el2_eff(CPUARMState * env)5574 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5575 {
5576     /*
5577      * The bits in this register behave as 0 for all purposes other than
5578      * direct reads of the register if SCR_EL3.HXEn is 0.
5579      * If EL2 is not enabled in the current security state, then the
5580      * bit may behave as if 0, or as if 1, depending on the bit.
5581      * For the moment, we treat the EL2-disabled case as taking
5582      * priority over the HXEn-disabled case. This is true for the only
5583      * bit for a feature which we implement where the answer is different
5584      * for the two cases (MSCEn for FEAT_MOPS).
5585      * This may need to be revisited for future bits.
5586      */
5587     if (!arm_is_el2_enabled(env)) {
5588         uint64_t hcrx = 0;
5589         if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
5590             /* MSCEn behaves as 1 if EL2 is not enabled */
5591             hcrx |= HCRX_MSCEN;
5592         }
5593         return hcrx;
5594     }
5595     if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
5596         return 0;
5597     }
5598     return env->cp15.hcrx_el2;
5599 }
5600 
cptr_el2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5601 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5602                            uint64_t value)
5603 {
5604     /*
5605      * For A-profile AArch32 EL3, if NSACR.CP10
5606      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5607      */
5608     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5609         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5610         uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5611         value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5612     }
5613     env->cp15.cptr_el[2] = value;
5614 }
5615 
cptr_el2_read(CPUARMState * env,const ARMCPRegInfo * ri)5616 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5617 {
5618     /*
5619      * For A-profile AArch32 EL3, if NSACR.CP10
5620      * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5621      */
5622     uint64_t value = env->cp15.cptr_el[2];
5623 
5624     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5625         !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5626         value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5627     }
5628     return value;
5629 }
5630 
5631 static const ARMCPRegInfo el2_cp_reginfo[] = {
5632     { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5633       .type = ARM_CP_IO,
5634       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5635       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5636       .nv2_redirect_offset = 0x78,
5637       .resetfn = hcr_reset,
5638       .writefn = hcr_write, .raw_writefn = raw_write },
5639     { .name = "HCR", .state = ARM_CP_STATE_AA32,
5640       .type = ARM_CP_ALIAS | ARM_CP_IO,
5641       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5642       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5643       .writefn = hcr_writelow },
5644     { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5645       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5646       .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5647     { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5648       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
5649       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5650       .access = PL2_RW,
5651       .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5652     { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5653       .type = ARM_CP_NV2_REDIRECT,
5654       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5655       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5656     { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5657       .type = ARM_CP_NV2_REDIRECT,
5658       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5659       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5660     { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5661       .type = ARM_CP_ALIAS,
5662       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5663       .access = PL2_RW,
5664       .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5665     { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5666       .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
5667       .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5668       .access = PL2_RW,
5669       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5670     { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5671       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5672       .access = PL2_RW, .writefn = vbar_write,
5673       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5674       .resetvalue = 0 },
5675     { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5676       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5677       .access = PL3_RW, .type = ARM_CP_ALIAS,
5678       .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5679     { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5680       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5681       .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5682       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5683       .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5684     { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5685       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5686       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5687       .resetvalue = 0 },
5688     { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5689       .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5690       .access = PL2_RW, .type = ARM_CP_ALIAS,
5691       .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5692     { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5693       .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5694       .access = PL2_RW, .type = ARM_CP_CONST,
5695       .resetvalue = 0 },
5696     /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5697     { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5698       .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5699       .access = PL2_RW, .type = ARM_CP_CONST,
5700       .resetvalue = 0 },
5701     { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5702       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5703       .access = PL2_RW, .type = ARM_CP_CONST,
5704       .resetvalue = 0 },
5705     { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5706       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5707       .access = PL2_RW, .type = ARM_CP_CONST,
5708       .resetvalue = 0 },
5709     { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5710       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5711       .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5712       .raw_writefn = raw_write,
5713       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5714     { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5715       .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5716       .type = ARM_CP_ALIAS,
5717       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5718       .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
5719     { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5720       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5721       .access = PL2_RW,
5722       .nv2_redirect_offset = 0x40,
5723       /* no .writefn needed as this can't cause an ASID change */
5724       .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5725     { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5726       .cp = 15, .opc1 = 6, .crm = 2,
5727       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5728       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5729       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5730       .writefn = vttbr_write, .raw_writefn = raw_write },
5731     { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5732       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5733       .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
5734       .nv2_redirect_offset = 0x20,
5735       .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5736     { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5737       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5738       .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5739       .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5740     { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5741       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5742       .access = PL2_RW, .resetvalue = 0,
5743       .nv2_redirect_offset = 0x90,
5744       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5745     { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5746       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5747       .access = PL2_RW, .resetvalue = 0,
5748       .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
5749       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5750     { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5751       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5752       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5753 #ifndef CONFIG_USER_ONLY
5754     /*
5755      * Unlike the other EL2-related AT operations, these must
5756      * UNDEF from EL3 if EL2 is not implemented, which is why we
5757      * define them here rather than with the rest of the AT ops.
5758      */
5759     { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5760       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5761       .access = PL2_W, .accessfn = at_s1e2_access,
5762       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5763       .writefn = ats_write64 },
5764     { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5765       .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5766       .access = PL2_W, .accessfn = at_s1e2_access,
5767       .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5768       .writefn = ats_write64 },
5769     /*
5770      * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5771      * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5772      * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5773      * to behave as if SCR.NS was 1.
5774      */
5775     { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5776       .access = PL2_W,
5777       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5778     { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5779       .access = PL2_W,
5780       .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5781     { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5782       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5783       /*
5784        * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5785        * reset values as IMPDEF. We choose to reset to 3 to comply with
5786        * both ARMv7 and ARMv8.
5787        */
5788       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
5789       .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
5790       .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5791     { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5792       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5793       .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5794       .writefn = gt_cntvoff_write,
5795       .nv2_redirect_offset = 0x60,
5796       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5797     { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5798       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5799       .writefn = gt_cntvoff_write,
5800       .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5801     { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5802       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5803       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5804       .type = ARM_CP_IO, .access = PL2_RW,
5805       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5806     { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5807       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5808       .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5809       .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5810     { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5811       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5812       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5813       .resetfn = gt_hyp_timer_reset,
5814       .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5815     { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5816       .type = ARM_CP_IO,
5817       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5818       .access = PL2_RW,
5819       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5820       .resetvalue = 0,
5821       .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5822 #endif
5823     { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5824       .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5825       .access = PL2_RW, .accessfn = access_el3_aa32ns,
5826       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5827     { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5828       .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5829       .access = PL2_RW,
5830       .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5831     { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5832       .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5833       .access = PL2_RW,
5834       .nv2_redirect_offset = 0x80,
5835       .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5836 };
5837 
5838 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5839     { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5840       .type = ARM_CP_ALIAS | ARM_CP_IO,
5841       .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5842       .access = PL2_RW,
5843       .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5844       .writefn = hcr_writehigh },
5845 };
5846 
sel2_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)5847 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5848                                   bool isread)
5849 {
5850     if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5851         return CP_ACCESS_OK;
5852     }
5853     return CP_ACCESS_UNDEFINED;
5854 }
5855 
5856 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5857     { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5858       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5859       .access = PL2_RW, .accessfn = sel2_access,
5860       .nv2_redirect_offset = 0x30,
5861       .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5862     { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5863       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5864       .access = PL2_RW, .accessfn = sel2_access,
5865       .nv2_redirect_offset = 0x48,
5866       .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5867 #ifndef CONFIG_USER_ONLY
5868     /* Secure EL2 Physical Timer */
5869     { .name = "CNTHPS_TVAL_EL2", .state = ARM_CP_STATE_AA64,
5870       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 0,
5871       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5872       .accessfn = gt_sel2timer_access,
5873       .readfn = gt_sec_pel2_tval_read,
5874       .writefn = gt_sec_pel2_tval_write,
5875       .resetfn = gt_sec_pel2_timer_reset,
5876     },
5877     { .name = "CNTHPS_CTL_EL2", .state = ARM_CP_STATE_AA64,
5878       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 1,
5879       .type = ARM_CP_IO, .access = PL2_RW,
5880       .accessfn = gt_sel2timer_access,
5881       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_PHYS].ctl),
5882       .resetvalue = 0,
5883       .writefn = gt_sec_pel2_ctl_write, .raw_writefn = raw_write,
5884     },
5885     { .name = "CNTHPS_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5886       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 2,
5887       .type = ARM_CP_IO, .access = PL2_RW,
5888       .accessfn = gt_sel2timer_access,
5889       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_PHYS].cval),
5890       .writefn = gt_sec_pel2_cval_write, .raw_writefn = raw_write,
5891     },
5892     /* Secure EL2 Virtual Timer */
5893     { .name = "CNTHVS_TVAL_EL2", .state = ARM_CP_STATE_AA64,
5894       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 0,
5895       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5896       .accessfn = gt_sel2timer_access,
5897       .readfn = gt_sec_vel2_tval_read,
5898       .writefn = gt_sec_vel2_tval_write,
5899       .resetfn = gt_sec_vel2_timer_reset,
5900     },
5901     { .name = "CNTHVS_CTL_EL2", .state = ARM_CP_STATE_AA64,
5902       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 1,
5903       .type = ARM_CP_IO, .access = PL2_RW,
5904       .accessfn = gt_sel2timer_access,
5905       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_VIRT].ctl),
5906       .resetvalue = 0,
5907       .writefn = gt_sec_vel2_ctl_write, .raw_writefn = raw_write,
5908     },
5909     { .name = "CNTHVS_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5910       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 2,
5911       .type = ARM_CP_IO, .access = PL2_RW,
5912       .accessfn = gt_sel2timer_access,
5913       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_VIRT].cval),
5914       .writefn = gt_sec_vel2_cval_write, .raw_writefn = raw_write,
5915     },
5916 #endif
5917 };
5918 
nsacr_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)5919 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5920                                    bool isread)
5921 {
5922     /*
5923      * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5924      * At Secure EL1 it traps to EL3 or EL2.
5925      */
5926     if (arm_current_el(env) == 3) {
5927         return CP_ACCESS_OK;
5928     }
5929     if (arm_is_secure_below_el3(env)) {
5930         if (env->cp15.scr_el3 & SCR_EEL2) {
5931             return CP_ACCESS_TRAP_EL2;
5932         }
5933         return CP_ACCESS_TRAP_EL3;
5934     }
5935     /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5936     if (isread) {
5937         return CP_ACCESS_OK;
5938     }
5939     return CP_ACCESS_UNDEFINED;
5940 }
5941 
5942 static const ARMCPRegInfo el3_cp_reginfo[] = {
5943     { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5944       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5945       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5946       .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
5947     { .name = "SCR",  .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5948       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5949       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5950       .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5951       .writefn = scr_write, .raw_writefn = raw_write },
5952     { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5953       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5954       .access = PL3_RW, .resetvalue = 0,
5955       .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5956     { .name = "SDER",
5957       .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5958       .access = PL3_RW, .resetvalue = 0,
5959       .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5960     { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5961       .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5962       .writefn = vbar_write, .resetvalue = 0,
5963       .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5964     { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5965       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5966       .access = PL3_RW, .resetvalue = 0,
5967       .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5968     { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5969       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5970       .access = PL3_RW,
5971       /* no .writefn needed as this can't cause an ASID change */
5972       .resetvalue = 0,
5973       .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5974     { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5975       .type = ARM_CP_ALIAS,
5976       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5977       .access = PL3_RW,
5978       .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5979     { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5980       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5981       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5982     { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5983       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5984       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5985     { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5986       .type = ARM_CP_ALIAS,
5987       .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5988       .access = PL3_RW,
5989       .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5990     { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5991       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5992       .access = PL3_RW, .writefn = vbar_write,
5993       .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5994       .resetvalue = 0 },
5995     { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5996       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5997       .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5998       .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5999     { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6000       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6001       .access = PL3_RW, .resetvalue = 0,
6002       .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
6003     { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6004       .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6005       .access = PL3_RW, .type = ARM_CP_CONST,
6006       .resetvalue = 0 },
6007     { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6008       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6009       .access = PL3_RW, .type = ARM_CP_CONST,
6010       .resetvalue = 0 },
6011     { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6012       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6013       .access = PL3_RW, .type = ARM_CP_CONST,
6014       .resetvalue = 0 },
6015 };
6016 
6017 #ifndef CONFIG_USER_ONLY
6018 
e2h_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6019 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
6020                                  bool isread)
6021 {
6022     if (arm_current_el(env) == 1) {
6023         /* This must be a FEAT_NV access */
6024         return CP_ACCESS_OK;
6025     }
6026     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6027         return CP_ACCESS_UNDEFINED;
6028     }
6029     return CP_ACCESS_OK;
6030 }
6031 
access_el1nvpct(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6032 static CPAccessResult access_el1nvpct(CPUARMState *env, const ARMCPRegInfo *ri,
6033                                       bool isread)
6034 {
6035     if (arm_current_el(env) == 1) {
6036         /* This must be a FEAT_NV access with NVx == 101 */
6037         if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVPCT)) {
6038             return CP_ACCESS_TRAP_EL2;
6039         }
6040     }
6041     return e2h_access(env, ri, isread);
6042 }
6043 
access_el1nvvct(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6044 static CPAccessResult access_el1nvvct(CPUARMState *env, const ARMCPRegInfo *ri,
6045                                       bool isread)
6046 {
6047     if (arm_current_el(env) == 1) {
6048         /* This must be a FEAT_NV access with NVx == 101 */
6049         if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVVCT)) {
6050             return CP_ACCESS_TRAP_EL2;
6051         }
6052     }
6053     return e2h_access(env, ri, isread);
6054 }
6055 
6056 /* Test if system register redirection is to occur in the current state.  */
redirect_for_e2h(CPUARMState * env)6057 static bool redirect_for_e2h(CPUARMState *env)
6058 {
6059     return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6060 }
6061 
el2_e2h_read(CPUARMState * env,const ARMCPRegInfo * ri)6062 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6063 {
6064     CPReadFn *readfn;
6065 
6066     if (redirect_for_e2h(env)) {
6067         /* Switch to the saved EL2 version of the register.  */
6068         ri = ri->opaque;
6069         readfn = ri->readfn;
6070     } else {
6071         readfn = ri->orig_readfn;
6072     }
6073     if (readfn == NULL) {
6074         readfn = raw_read;
6075     }
6076     return readfn(env, ri);
6077 }
6078 
el2_e2h_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6079 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6080                           uint64_t value)
6081 {
6082     CPWriteFn *writefn;
6083 
6084     if (redirect_for_e2h(env)) {
6085         /* Switch to the saved EL2 version of the register.  */
6086         ri = ri->opaque;
6087         writefn = ri->writefn;
6088     } else {
6089         writefn = ri->orig_writefn;
6090     }
6091     if (writefn == NULL) {
6092         writefn = raw_write;
6093     }
6094     writefn(env, ri, value);
6095 }
6096 
el2_e2h_e12_read(CPUARMState * env,const ARMCPRegInfo * ri)6097 static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri)
6098 {
6099     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6100     return ri->orig_readfn(env, ri->opaque);
6101 }
6102 
el2_e2h_e12_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6103 static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri,
6104                               uint64_t value)
6105 {
6106     /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6107     return ri->orig_writefn(env, ri->opaque, value);
6108 }
6109 
el2_e2h_e12_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6110 static CPAccessResult el2_e2h_e12_access(CPUARMState *env,
6111                                          const ARMCPRegInfo *ri,
6112                                          bool isread)
6113 {
6114     if (arm_current_el(env) == 1) {
6115         /*
6116          * This must be a FEAT_NV access (will either trap or redirect
6117          * to memory). None of the registers with _EL12 aliases want to
6118          * apply their trap controls for this kind of access, so don't
6119          * call the orig_accessfn or do the "UNDEF when E2H is 0" check.
6120          */
6121         return CP_ACCESS_OK;
6122     }
6123     /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */
6124     if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6125         return CP_ACCESS_UNDEFINED;
6126     }
6127     if (ri->orig_accessfn) {
6128         return ri->orig_accessfn(env, ri->opaque, isread);
6129     }
6130     return CP_ACCESS_OK;
6131 }
6132 
define_arm_vh_e2h_redirects_aliases(ARMCPU * cpu)6133 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6134 {
6135     struct E2HAlias {
6136         uint32_t src_key, dst_key, new_key;
6137         const char *src_name, *dst_name, *new_name;
6138         bool (*feature)(const ARMISARegisters *id);
6139     };
6140 
6141 #define K(op0, op1, crn, crm, op2) \
6142     ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6143 
6144     static const struct E2HAlias aliases[] = {
6145         { K(3, 0,  1, 0, 0), K(3, 4,  1, 0, 0), K(3, 5, 1, 0, 0),
6146           "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6147         { K(3, 0,  1, 0, 2), K(3, 4,  1, 1, 2), K(3, 5, 1, 0, 2),
6148           "CPACR", "CPTR_EL2", "CPACR_EL12" },
6149         { K(3, 0,  2, 0, 0), K(3, 4,  2, 0, 0), K(3, 5, 2, 0, 0),
6150           "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6151         { K(3, 0,  2, 0, 1), K(3, 4,  2, 0, 1), K(3, 5, 2, 0, 1),
6152           "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6153         { K(3, 0,  2, 0, 2), K(3, 4,  2, 0, 2), K(3, 5, 2, 0, 2),
6154           "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6155         { K(3, 0,  4, 0, 0), K(3, 4,  4, 0, 0), K(3, 5, 4, 0, 0),
6156           "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6157         { K(3, 0,  4, 0, 1), K(3, 4,  4, 0, 1), K(3, 5, 4, 0, 1),
6158           "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6159         { K(3, 0,  5, 1, 0), K(3, 4,  5, 1, 0), K(3, 5, 5, 1, 0),
6160           "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6161         { K(3, 0,  5, 1, 1), K(3, 4,  5, 1, 1), K(3, 5, 5, 1, 1),
6162           "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6163         { K(3, 0,  5, 2, 0), K(3, 4,  5, 2, 0), K(3, 5, 5, 2, 0),
6164           "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6165         { K(3, 0,  6, 0, 0), K(3, 4,  6, 0, 0), K(3, 5, 6, 0, 0),
6166           "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6167         { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6168           "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6169         { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6170           "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6171         { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6172           "VBAR", "VBAR_EL2", "VBAR_EL12" },
6173         { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6174           "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6175         { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6176           "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6177 
6178         /*
6179          * Note that redirection of ZCR is mentioned in the description
6180          * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6181          * not in the summary table.
6182          */
6183         { K(3, 0,  1, 2, 0), K(3, 4,  1, 2, 0), K(3, 5, 1, 2, 0),
6184           "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6185         { K(3, 0,  1, 2, 6), K(3, 4,  1, 2, 6), K(3, 5, 1, 2, 6),
6186           "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6187 
6188         { K(3, 0,  5, 6, 0), K(3, 4,  5, 6, 0), K(3, 5, 5, 6, 0),
6189           "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6190 
6191         { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6192           "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6193           isar_feature_aa64_scxtnum },
6194 
6195         /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6196         /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6197     };
6198 #undef K
6199 
6200     size_t i;
6201 
6202     for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6203         const struct E2HAlias *a = &aliases[i];
6204         ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6205         bool ok;
6206 
6207         if (a->feature && !a->feature(&cpu->isar)) {
6208             continue;
6209         }
6210 
6211         src_reg = g_hash_table_lookup(cpu->cp_regs,
6212                                       (gpointer)(uintptr_t)a->src_key);
6213         dst_reg = g_hash_table_lookup(cpu->cp_regs,
6214                                       (gpointer)(uintptr_t)a->dst_key);
6215         g_assert(src_reg != NULL);
6216         g_assert(dst_reg != NULL);
6217 
6218         /* Cross-compare names to detect typos in the keys.  */
6219         g_assert(strcmp(src_reg->name, a->src_name) == 0);
6220         g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6221 
6222         /* None of the core system registers use opaque; we will.  */
6223         g_assert(src_reg->opaque == NULL);
6224 
6225         /* Create alias before redirection so we dup the right data. */
6226         new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6227 
6228         new_reg->name = a->new_name;
6229         new_reg->type |= ARM_CP_ALIAS;
6230         /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place.  */
6231         new_reg->access &= PL2_RW | PL3_RW;
6232         /* The new_reg op fields are as per new_key, not the target reg */
6233         new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK)
6234             >> CP_REG_ARM64_SYSREG_CRN_SHIFT;
6235         new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK)
6236             >> CP_REG_ARM64_SYSREG_CRM_SHIFT;
6237         new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK)
6238             >> CP_REG_ARM64_SYSREG_OP0_SHIFT;
6239         new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK)
6240             >> CP_REG_ARM64_SYSREG_OP1_SHIFT;
6241         new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK)
6242             >> CP_REG_ARM64_SYSREG_OP2_SHIFT;
6243         new_reg->opaque = src_reg;
6244         new_reg->orig_readfn = src_reg->readfn ?: raw_read;
6245         new_reg->orig_writefn = src_reg->writefn ?: raw_write;
6246         new_reg->orig_accessfn = src_reg->accessfn;
6247         if (!new_reg->raw_readfn) {
6248             new_reg->raw_readfn = raw_read;
6249         }
6250         if (!new_reg->raw_writefn) {
6251             new_reg->raw_writefn = raw_write;
6252         }
6253         new_reg->readfn = el2_e2h_e12_read;
6254         new_reg->writefn = el2_e2h_e12_write;
6255         new_reg->accessfn = el2_e2h_e12_access;
6256 
6257         /*
6258          * If the _EL1 register is redirected to memory by FEAT_NV2,
6259          * then it shares the offset with the _EL12 register,
6260          * and which one is redirected depends on HCR_EL2.NV1.
6261          */
6262         if (new_reg->nv2_redirect_offset) {
6263             assert(new_reg->nv2_redirect_offset & NV2_REDIR_NV1);
6264             new_reg->nv2_redirect_offset &= ~NV2_REDIR_NV1;
6265             new_reg->nv2_redirect_offset |= NV2_REDIR_NO_NV1;
6266         }
6267 
6268         ok = g_hash_table_insert(cpu->cp_regs,
6269                                  (gpointer)(uintptr_t)a->new_key, new_reg);
6270         g_assert(ok);
6271 
6272         src_reg->opaque = dst_reg;
6273         src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6274         src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6275         if (!src_reg->raw_readfn) {
6276             src_reg->raw_readfn = raw_read;
6277         }
6278         if (!src_reg->raw_writefn) {
6279             src_reg->raw_writefn = raw_write;
6280         }
6281         src_reg->readfn = el2_e2h_read;
6282         src_reg->writefn = el2_e2h_write;
6283     }
6284 }
6285 #endif
6286 
ctr_el0_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6287 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6288                                      bool isread)
6289 {
6290     int cur_el = arm_current_el(env);
6291 
6292     if (cur_el < 2) {
6293         uint64_t hcr = arm_hcr_el2_eff(env);
6294 
6295         if (cur_el == 0) {
6296             if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6297                 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6298                     return CP_ACCESS_TRAP_EL2;
6299                 }
6300             } else {
6301                 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6302                     return CP_ACCESS_TRAP_EL1;
6303                 }
6304                 if (hcr & HCR_TID2) {
6305                     return CP_ACCESS_TRAP_EL2;
6306                 }
6307             }
6308         } else if (hcr & HCR_TID2) {
6309             return CP_ACCESS_TRAP_EL2;
6310         }
6311     }
6312 
6313     if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6314         return CP_ACCESS_TRAP_EL2;
6315     }
6316 
6317     return CP_ACCESS_OK;
6318 }
6319 
6320 /*
6321  * Check for traps to RAS registers, which are controlled
6322  * by HCR_EL2.TERR and SCR_EL3.TERR.
6323  */
access_terr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6324 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6325                                   bool isread)
6326 {
6327     int el = arm_current_el(env);
6328 
6329     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6330         return CP_ACCESS_TRAP_EL2;
6331     }
6332     if (!arm_is_el3_or_mon(env) && (env->cp15.scr_el3 & SCR_TERR)) {
6333         return CP_ACCESS_TRAP_EL3;
6334     }
6335     return CP_ACCESS_OK;
6336 }
6337 
disr_read(CPUARMState * env,const ARMCPRegInfo * ri)6338 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6339 {
6340     int el = arm_current_el(env);
6341 
6342     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6343         return env->cp15.vdisr_el2;
6344     }
6345     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6346         return 0; /* RAZ/WI */
6347     }
6348     return env->cp15.disr_el1;
6349 }
6350 
disr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t val)6351 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6352 {
6353     int el = arm_current_el(env);
6354 
6355     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6356         env->cp15.vdisr_el2 = val;
6357         return;
6358     }
6359     if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6360         return; /* RAZ/WI */
6361     }
6362     env->cp15.disr_el1 = val;
6363 }
6364 
6365 /*
6366  * Minimal RAS implementation with no Error Records.
6367  * Which means that all of the Error Record registers:
6368  *   ERXADDR_EL1
6369  *   ERXCTLR_EL1
6370  *   ERXFR_EL1
6371  *   ERXMISC0_EL1
6372  *   ERXMISC1_EL1
6373  *   ERXMISC2_EL1
6374  *   ERXMISC3_EL1
6375  *   ERXPFGCDN_EL1  (RASv1p1)
6376  *   ERXPFGCTL_EL1  (RASv1p1)
6377  *   ERXPFGF_EL1    (RASv1p1)
6378  *   ERXSTATUS_EL1
6379  * and
6380  *   ERRSELR_EL1
6381  * may generate UNDEFINED, which is the effect we get by not
6382  * listing them at all.
6383  *
6384  * These registers have fine-grained trap bits, but UNDEF-to-EL1
6385  * is higher priority than FGT-to-EL2 so we do not need to list them
6386  * in order to check for an FGT.
6387  */
6388 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6389     { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6390       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6391       .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6392       .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6393     { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6394       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6395       .access = PL1_R, .accessfn = access_terr,
6396       .fgt = FGT_ERRIDR_EL1,
6397       .type = ARM_CP_CONST, .resetvalue = 0 },
6398     { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6399       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6400       .nv2_redirect_offset = 0x500,
6401       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6402     { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6403       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6404       .nv2_redirect_offset = 0x508,
6405       .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6406 };
6407 
6408 /*
6409  * Return the exception level to which exceptions should be taken
6410  * via SVEAccessTrap.  This excludes the check for whether the exception
6411  * should be routed through AArch64.AdvSIMDFPAccessTrap.  That can easily
6412  * be found by testing 0 < fp_exception_el < sve_exception_el.
6413  *
6414  * C.f. the ARM pseudocode function CheckSVEEnabled.  Note that the
6415  * pseudocode does *not* separate out the FP trap checks, but has them
6416  * all in one function.
6417  */
sve_exception_el(CPUARMState * env,int el)6418 int sve_exception_el(CPUARMState *env, int el)
6419 {
6420 #ifndef CONFIG_USER_ONLY
6421     if (el <= 1 && !el_is_in_host(env, el)) {
6422         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6423         case 1:
6424             if (el != 0) {
6425                 break;
6426             }
6427             /* fall through */
6428         case 0:
6429         case 2:
6430             return 1;
6431         }
6432     }
6433 
6434     if (el <= 2 && arm_is_el2_enabled(env)) {
6435         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6436         if (env->cp15.hcr_el2 & HCR_E2H) {
6437             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6438             case 1:
6439                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6440                     break;
6441                 }
6442                 /* fall through */
6443             case 0:
6444             case 2:
6445                 return 2;
6446             }
6447         } else {
6448             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6449                 return 2;
6450             }
6451         }
6452     }
6453 
6454     /* CPTR_EL3.  Since EZ is negative we must check for EL3.  */
6455     if (arm_feature(env, ARM_FEATURE_EL3)
6456         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6457         return 3;
6458     }
6459 #endif
6460     return 0;
6461 }
6462 
6463 /*
6464  * Return the exception level to which exceptions should be taken for SME.
6465  * C.f. the ARM pseudocode function CheckSMEAccess.
6466  */
sme_exception_el(CPUARMState * env,int el)6467 int sme_exception_el(CPUARMState *env, int el)
6468 {
6469 #ifndef CONFIG_USER_ONLY
6470     if (el <= 1 && !el_is_in_host(env, el)) {
6471         switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6472         case 1:
6473             if (el != 0) {
6474                 break;
6475             }
6476             /* fall through */
6477         case 0:
6478         case 2:
6479             return 1;
6480         }
6481     }
6482 
6483     if (el <= 2 && arm_is_el2_enabled(env)) {
6484         /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6485         if (env->cp15.hcr_el2 & HCR_E2H) {
6486             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6487             case 1:
6488                 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6489                     break;
6490                 }
6491                 /* fall through */
6492             case 0:
6493             case 2:
6494                 return 2;
6495             }
6496         } else {
6497             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6498                 return 2;
6499             }
6500         }
6501     }
6502 
6503     /* CPTR_EL3.  Since ESM is negative we must check for EL3.  */
6504     if (arm_feature(env, ARM_FEATURE_EL3)
6505         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6506         return 3;
6507     }
6508 #endif
6509     return 0;
6510 }
6511 
6512 /*
6513  * Given that SVE is enabled, return the vector length for EL.
6514  */
sve_vqm1_for_el_sm(CPUARMState * env,int el,bool sm)6515 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6516 {
6517     ARMCPU *cpu = env_archcpu(env);
6518     uint64_t *cr = env->vfp.zcr_el;
6519     uint32_t map = cpu->sve_vq.map;
6520     uint32_t len = ARM_MAX_VQ - 1;
6521 
6522     if (sm) {
6523         cr = env->vfp.smcr_el;
6524         map = cpu->sme_vq.map;
6525     }
6526 
6527     if (el <= 1 && !el_is_in_host(env, el)) {
6528         len = MIN(len, 0xf & (uint32_t)cr[1]);
6529     }
6530     if (el <= 2 && arm_is_el2_enabled(env)) {
6531         len = MIN(len, 0xf & (uint32_t)cr[2]);
6532     }
6533     if (arm_feature(env, ARM_FEATURE_EL3)) {
6534         len = MIN(len, 0xf & (uint32_t)cr[3]);
6535     }
6536 
6537     map &= MAKE_64BIT_MASK(0, len + 1);
6538     if (map != 0) {
6539         return 31 - clz32(map);
6540     }
6541 
6542     /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6543     assert(sm);
6544     return ctz32(cpu->sme_vq.map);
6545 }
6546 
sve_vqm1_for_el(CPUARMState * env,int el)6547 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6548 {
6549     return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6550 }
6551 
zcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6552 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6553                       uint64_t value)
6554 {
6555     int cur_el = arm_current_el(env);
6556     int old_len = sve_vqm1_for_el(env, cur_el);
6557     int new_len;
6558 
6559     /* Bits other than [3:0] are RAZ/WI.  */
6560     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6561     raw_write(env, ri, value & 0xf);
6562 
6563     /*
6564      * Because we arrived here, we know both FP and SVE are enabled;
6565      * otherwise we would have trapped access to the ZCR_ELn register.
6566      */
6567     new_len = sve_vqm1_for_el(env, cur_el);
6568     if (new_len < old_len) {
6569         aarch64_sve_narrow_vq(env, new_len + 1);
6570     }
6571 }
6572 
6573 static const ARMCPRegInfo zcr_reginfo[] = {
6574     { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6575       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6576       .nv2_redirect_offset = 0x1e0 | NV2_REDIR_NV1,
6577       .access = PL1_RW, .type = ARM_CP_SVE,
6578       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6579       .writefn = zcr_write, .raw_writefn = raw_write },
6580     { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6581       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6582       .access = PL2_RW, .type = ARM_CP_SVE,
6583       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6584       .writefn = zcr_write, .raw_writefn = raw_write },
6585     { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6586       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6587       .access = PL3_RW, .type = ARM_CP_SVE,
6588       .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6589       .writefn = zcr_write, .raw_writefn = raw_write },
6590 };
6591 
access_tpidr2(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6592 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6593                                     bool isread)
6594 {
6595     int el = arm_current_el(env);
6596 
6597     if (el == 0) {
6598         uint64_t sctlr = arm_sctlr(env, el);
6599         if (!(sctlr & SCTLR_EnTP2)) {
6600             return CP_ACCESS_TRAP_EL1;
6601         }
6602     }
6603     /* TODO: FEAT_FGT */
6604     if (el < 3
6605         && arm_feature(env, ARM_FEATURE_EL3)
6606         && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6607         return CP_ACCESS_TRAP_EL3;
6608     }
6609     return CP_ACCESS_OK;
6610 }
6611 
access_smprimap(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6612 static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri,
6613                                       bool isread)
6614 {
6615     /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */
6616     if (arm_current_el(env) == 2
6617         && arm_feature(env, ARM_FEATURE_EL3)
6618         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6619         return CP_ACCESS_TRAP_EL3;
6620     }
6621     return CP_ACCESS_OK;
6622 }
6623 
access_smpri(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6624 static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri,
6625                                    bool isread)
6626 {
6627     if (arm_current_el(env) < 3
6628         && arm_feature(env, ARM_FEATURE_EL3)
6629         && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6630         return CP_ACCESS_TRAP_EL3;
6631     }
6632     return CP_ACCESS_OK;
6633 }
6634 
6635 /* ResetSVEState */
arm_reset_sve_state(CPUARMState * env)6636 static void arm_reset_sve_state(CPUARMState *env)
6637 {
6638     memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
6639     /* Recall that FFR is stored as pregs[16]. */
6640     memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
6641     vfp_set_fpsr(env, 0x0800009f);
6642 }
6643 
aarch64_set_svcr(CPUARMState * env,uint64_t new,uint64_t mask)6644 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
6645 {
6646     uint64_t change = (env->svcr ^ new) & mask;
6647 
6648     if (change == 0) {
6649         return;
6650     }
6651     env->svcr ^= change;
6652 
6653     if (change & R_SVCR_SM_MASK) {
6654         arm_reset_sve_state(env);
6655     }
6656 
6657     /*
6658      * ResetSMEState.
6659      *
6660      * SetPSTATE_ZA zeros on enable and disable.  We can zero this only
6661      * on enable: while disabled, the storage is inaccessible and the
6662      * value does not matter.  We're not saving the storage in vmstate
6663      * when disabled either.
6664      */
6665     if (change & new & R_SVCR_ZA_MASK) {
6666         memset(env->zarray, 0, sizeof(env->zarray));
6667     }
6668 
6669     if (tcg_enabled()) {
6670         arm_rebuild_hflags(env);
6671     }
6672 }
6673 
svcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6674 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6675                        uint64_t value)
6676 {
6677     aarch64_set_svcr(env, value, -1);
6678 }
6679 
smcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6680 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6681                        uint64_t value)
6682 {
6683     int cur_el = arm_current_el(env);
6684     int old_len = sve_vqm1_for_el(env, cur_el);
6685     int new_len;
6686 
6687     QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6688     value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6689     raw_write(env, ri, value);
6690 
6691     /*
6692      * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6693      * when SVL is widened (old values kept, or zeros).  Choose to keep the
6694      * current values for simplicity.  But for QEMU internals, we must still
6695      * apply the narrower SVL to the Zregs and Pregs -- see the comment
6696      * above aarch64_sve_narrow_vq.
6697      */
6698     new_len = sve_vqm1_for_el(env, cur_el);
6699     if (new_len < old_len) {
6700         aarch64_sve_narrow_vq(env, new_len + 1);
6701     }
6702 }
6703 
6704 static const ARMCPRegInfo sme_reginfo[] = {
6705     { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6706       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6707       .access = PL0_RW, .accessfn = access_tpidr2,
6708       .fgt = FGT_NTPIDR2_EL0,
6709       .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6710     { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6711       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6712       .access = PL0_RW, .type = ARM_CP_SME,
6713       .fieldoffset = offsetof(CPUARMState, svcr),
6714       .writefn = svcr_write, .raw_writefn = raw_write },
6715     { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6716       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6717       .nv2_redirect_offset = 0x1f0 | NV2_REDIR_NV1,
6718       .access = PL1_RW, .type = ARM_CP_SME,
6719       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6720       .writefn = smcr_write, .raw_writefn = raw_write },
6721     { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6722       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6723       .access = PL2_RW, .type = ARM_CP_SME,
6724       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6725       .writefn = smcr_write, .raw_writefn = raw_write },
6726     { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6727       .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6728       .access = PL3_RW, .type = ARM_CP_SME,
6729       .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6730       .writefn = smcr_write, .raw_writefn = raw_write },
6731     { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6732       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6733       .access = PL1_R, .accessfn = access_aa64_tid1,
6734       /*
6735        * IMPLEMENTOR = 0 (software)
6736        * REVISION    = 0 (implementation defined)
6737        * SMPS        = 0 (no streaming execution priority in QEMU)
6738        * AFFINITY    = 0 (streaming sve mode not shared with other PEs)
6739        */
6740       .type = ARM_CP_CONST, .resetvalue = 0, },
6741     /*
6742      * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6743      */
6744     { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6745       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6746       .access = PL1_RW, .accessfn = access_smpri,
6747       .fgt = FGT_NSMPRI_EL1,
6748       .type = ARM_CP_CONST, .resetvalue = 0 },
6749     { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6750       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6751       .nv2_redirect_offset = 0x1f8,
6752       .access = PL2_RW, .accessfn = access_smprimap,
6753       .type = ARM_CP_CONST, .resetvalue = 0 },
6754 };
6755 
gpccr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6756 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6757                         uint64_t value)
6758 {
6759     /* L0GPTSZ is RO; other bits not mentioned are RES0. */
6760     uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
6761         R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
6762         R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
6763 
6764     env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
6765 }
6766 
gpccr_reset(CPUARMState * env,const ARMCPRegInfo * ri)6767 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
6768 {
6769     env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
6770                                      env_archcpu(env)->reset_l0gptsz);
6771 }
6772 
6773 static const ARMCPRegInfo rme_reginfo[] = {
6774     { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
6775       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
6776       .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
6777       .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
6778     { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
6779       .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
6780       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
6781     { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
6782       .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
6783       .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
6784     { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
6785       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
6786       .access = PL3_W, .type = ARM_CP_NOP },
6787 };
6788 
6789 static const ARMCPRegInfo rme_mte_reginfo[] = {
6790     { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
6791       .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
6792       .access = PL3_W, .type = ARM_CP_NOP },
6793 };
6794 
aa64_allint_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6795 static void aa64_allint_write(CPUARMState *env, const ARMCPRegInfo *ri,
6796                               uint64_t value)
6797 {
6798     env->pstate = (env->pstate & ~PSTATE_ALLINT) | (value & PSTATE_ALLINT);
6799 }
6800 
aa64_allint_read(CPUARMState * env,const ARMCPRegInfo * ri)6801 static uint64_t aa64_allint_read(CPUARMState *env, const ARMCPRegInfo *ri)
6802 {
6803     return env->pstate & PSTATE_ALLINT;
6804 }
6805 
aa64_allint_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6806 static CPAccessResult aa64_allint_access(CPUARMState *env,
6807                                          const ARMCPRegInfo *ri, bool isread)
6808 {
6809     if (!isread && arm_current_el(env) == 1 &&
6810         (arm_hcrx_el2_eff(env) & HCRX_TALLINT)) {
6811         return CP_ACCESS_TRAP_EL2;
6812     }
6813     return CP_ACCESS_OK;
6814 }
6815 
6816 static const ARMCPRegInfo nmi_reginfo[] = {
6817     { .name = "ALLINT", .state = ARM_CP_STATE_AA64,
6818       .opc0 = 3, .opc1 = 0, .opc2 = 0, .crn = 4, .crm = 3,
6819       .type = ARM_CP_NO_RAW,
6820       .access = PL1_RW, .accessfn = aa64_allint_access,
6821       .fieldoffset = offsetof(CPUARMState, pstate),
6822       .writefn = aa64_allint_write, .readfn = aa64_allint_read,
6823       .resetfn = arm_cp_reset_ignore },
6824 };
6825 
define_pmu_regs(ARMCPU * cpu)6826 static void define_pmu_regs(ARMCPU *cpu)
6827 {
6828     /*
6829      * v7 performance monitor control register: same implementor
6830      * field as main ID register, and we implement four counters in
6831      * addition to the cycle count register.
6832      */
6833     unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6834     ARMCPRegInfo pmcr = {
6835         .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6836         .access = PL0_RW,
6837         .fgt = FGT_PMCR_EL0,
6838         .type = ARM_CP_IO | ARM_CP_ALIAS,
6839         .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6840         .accessfn = pmreg_access,
6841         .readfn = pmcr_read, .raw_readfn = raw_read,
6842         .writefn = pmcr_write, .raw_writefn = raw_write,
6843     };
6844     ARMCPRegInfo pmcr64 = {
6845         .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6846         .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6847         .access = PL0_RW, .accessfn = pmreg_access,
6848         .fgt = FGT_PMCR_EL0,
6849         .type = ARM_CP_IO,
6850         .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6851         .resetvalue = cpu->isar.reset_pmcr_el0,
6852         .readfn = pmcr_read, .raw_readfn = raw_read,
6853         .writefn = pmcr_write, .raw_writefn = raw_write,
6854     };
6855 
6856     define_one_arm_cp_reg(cpu, &pmcr);
6857     define_one_arm_cp_reg(cpu, &pmcr64);
6858     for (i = 0; i < pmcrn; i++) {
6859         char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6860         char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6861         char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6862         char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6863         ARMCPRegInfo pmev_regs[] = {
6864             { .name = pmevcntr_name, .cp = 15, .crn = 14,
6865               .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6866               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6867               .fgt = FGT_PMEVCNTRN_EL0,
6868               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6869               .accessfn = pmreg_access_xevcntr },
6870             { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6871               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6872               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6873               .type = ARM_CP_IO,
6874               .fgt = FGT_PMEVCNTRN_EL0,
6875               .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6876               .raw_readfn = pmevcntr_rawread,
6877               .raw_writefn = pmevcntr_rawwrite },
6878             { .name = pmevtyper_name, .cp = 15, .crn = 14,
6879               .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6880               .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6881               .fgt = FGT_PMEVTYPERN_EL0,
6882               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6883               .accessfn = pmreg_access },
6884             { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6885               .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6886               .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6887               .fgt = FGT_PMEVTYPERN_EL0,
6888               .type = ARM_CP_IO,
6889               .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6890               .raw_writefn = pmevtyper_rawwrite },
6891         };
6892         define_arm_cp_regs(cpu, pmev_regs);
6893         g_free(pmevcntr_name);
6894         g_free(pmevcntr_el0_name);
6895         g_free(pmevtyper_name);
6896         g_free(pmevtyper_el0_name);
6897     }
6898     if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
6899         ARMCPRegInfo v81_pmu_regs[] = {
6900             { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6901               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6902               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6903               .fgt = FGT_PMCEIDN_EL0,
6904               .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6905             { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6906               .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6907               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6908               .fgt = FGT_PMCEIDN_EL0,
6909               .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6910         };
6911         define_arm_cp_regs(cpu, v81_pmu_regs);
6912     }
6913     if (cpu_isar_feature(any_pmuv3p4, cpu)) {
6914         static const ARMCPRegInfo v84_pmmir = {
6915             .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6916             .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6917             .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6918             .fgt = FGT_PMMIR_EL1,
6919             .resetvalue = 0
6920         };
6921         define_one_arm_cp_reg(cpu, &v84_pmmir);
6922     }
6923 }
6924 
6925 #ifndef CONFIG_USER_ONLY
6926 /*
6927  * We don't know until after realize whether there's a GICv3
6928  * attached, and that is what registers the gicv3 sysregs.
6929  * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6930  * at runtime.
6931  */
id_pfr1_read(CPUARMState * env,const ARMCPRegInfo * ri)6932 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6933 {
6934     ARMCPU *cpu = env_archcpu(env);
6935     uint64_t pfr1 = cpu->isar.id_pfr1;
6936 
6937     if (env->gicv3state) {
6938         pfr1 |= 1 << 28;
6939     }
6940     return pfr1;
6941 }
6942 
id_aa64pfr0_read(CPUARMState * env,const ARMCPRegInfo * ri)6943 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6944 {
6945     ARMCPU *cpu = env_archcpu(env);
6946     uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6947 
6948     if (env->gicv3state) {
6949         pfr0 |= 1 << 24;
6950     }
6951     return pfr0;
6952 }
6953 #endif
6954 
6955 /*
6956  * Shared logic between LORID and the rest of the LOR* registers.
6957  * Secure state exclusion has already been dealt with.
6958  */
access_lor_ns(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6959 static CPAccessResult access_lor_ns(CPUARMState *env,
6960                                     const ARMCPRegInfo *ri, bool isread)
6961 {
6962     int el = arm_current_el(env);
6963 
6964     if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6965         return CP_ACCESS_TRAP_EL2;
6966     }
6967     if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6968         return CP_ACCESS_TRAP_EL3;
6969     }
6970     return CP_ACCESS_OK;
6971 }
6972 
access_lor_other(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6973 static CPAccessResult access_lor_other(CPUARMState *env,
6974                                        const ARMCPRegInfo *ri, bool isread)
6975 {
6976     if (arm_is_secure_below_el3(env)) {
6977         /* UNDEF if SCR_EL3.NS == 0 */
6978         return CP_ACCESS_UNDEFINED;
6979     }
6980     return access_lor_ns(env, ri, isread);
6981 }
6982 
6983 /*
6984  * A trivial implementation of ARMv8.1-LOR leaves all of these
6985  * registers fixed at 0, which indicates that there are zero
6986  * supported Limited Ordering regions.
6987  */
6988 static const ARMCPRegInfo lor_reginfo[] = {
6989     { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6990       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6991       .access = PL1_RW, .accessfn = access_lor_other,
6992       .fgt = FGT_LORSA_EL1,
6993       .type = ARM_CP_CONST, .resetvalue = 0 },
6994     { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6995       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6996       .access = PL1_RW, .accessfn = access_lor_other,
6997       .fgt = FGT_LOREA_EL1,
6998       .type = ARM_CP_CONST, .resetvalue = 0 },
6999     { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7000       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7001       .access = PL1_RW, .accessfn = access_lor_other,
7002       .fgt = FGT_LORN_EL1,
7003       .type = ARM_CP_CONST, .resetvalue = 0 },
7004     { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7005       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7006       .access = PL1_RW, .accessfn = access_lor_other,
7007       .fgt = FGT_LORC_EL1,
7008       .type = ARM_CP_CONST, .resetvalue = 0 },
7009     { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7010       .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7011       .access = PL1_R, .accessfn = access_lor_ns,
7012       .fgt = FGT_LORID_EL1,
7013       .type = ARM_CP_CONST, .resetvalue = 0 },
7014 };
7015 
access_pauth(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7016 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7017                                    bool isread)
7018 {
7019     int el = arm_current_el(env);
7020 
7021     if (el < 2 &&
7022         arm_is_el2_enabled(env) &&
7023         !(arm_hcr_el2_eff(env) & HCR_APK)) {
7024         return CP_ACCESS_TRAP_EL2;
7025     }
7026     if (el < 3 &&
7027         arm_feature(env, ARM_FEATURE_EL3) &&
7028         !(env->cp15.scr_el3 & SCR_APK)) {
7029         return CP_ACCESS_TRAP_EL3;
7030     }
7031     return CP_ACCESS_OK;
7032 }
7033 
7034 static const ARMCPRegInfo pauth_reginfo[] = {
7035     { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7036       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7037       .access = PL1_RW, .accessfn = access_pauth,
7038       .fgt = FGT_APDAKEY,
7039       .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7040     { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7041       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7042       .access = PL1_RW, .accessfn = access_pauth,
7043       .fgt = FGT_APDAKEY,
7044       .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7045     { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7046       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7047       .access = PL1_RW, .accessfn = access_pauth,
7048       .fgt = FGT_APDBKEY,
7049       .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7050     { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7051       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7052       .access = PL1_RW, .accessfn = access_pauth,
7053       .fgt = FGT_APDBKEY,
7054       .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7055     { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7056       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7057       .access = PL1_RW, .accessfn = access_pauth,
7058       .fgt = FGT_APGAKEY,
7059       .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7060     { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7061       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7062       .access = PL1_RW, .accessfn = access_pauth,
7063       .fgt = FGT_APGAKEY,
7064       .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7065     { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7066       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7067       .access = PL1_RW, .accessfn = access_pauth,
7068       .fgt = FGT_APIAKEY,
7069       .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7070     { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7071       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7072       .access = PL1_RW, .accessfn = access_pauth,
7073       .fgt = FGT_APIAKEY,
7074       .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7075     { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7076       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7077       .access = PL1_RW, .accessfn = access_pauth,
7078       .fgt = FGT_APIBKEY,
7079       .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7080     { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7081       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7082       .access = PL1_RW, .accessfn = access_pauth,
7083       .fgt = FGT_APIBKEY,
7084       .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7085 };
7086 
rndr_readfn(CPUARMState * env,const ARMCPRegInfo * ri)7087 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7088 {
7089     Error *err = NULL;
7090     uint64_t ret;
7091 
7092     /* Success sets NZCV = 0000.  */
7093     env->NF = env->CF = env->VF = 0, env->ZF = 1;
7094 
7095     if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7096         /*
7097          * ??? Failed, for unknown reasons in the crypto subsystem.
7098          * The best we can do is log the reason and return the
7099          * timed-out indication to the guest.  There is no reason
7100          * we know to expect this failure to be transitory, so the
7101          * guest may well hang retrying the operation.
7102          */
7103         qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7104                       ri->name, error_get_pretty(err));
7105         error_free(err);
7106 
7107         env->ZF = 0; /* NZCF = 0100 */
7108         return 0;
7109     }
7110     return ret;
7111 }
7112 
7113 /* We do not support re-seeding, so the two registers operate the same.  */
7114 static const ARMCPRegInfo rndr_reginfo[] = {
7115     { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7116       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7117       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7118       .access = PL0_R, .readfn = rndr_readfn },
7119     { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7120       .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7121       .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7122       .access = PL0_R, .readfn = rndr_readfn },
7123 };
7124 
dccvap_writefn(CPUARMState * env,const ARMCPRegInfo * opaque,uint64_t value)7125 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7126                           uint64_t value)
7127 {
7128 #ifdef CONFIG_TCG
7129     ARMCPU *cpu = env_archcpu(env);
7130     /* CTR_EL0 System register -> DminLine, bits [19:16] */
7131     uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7132     uint64_t vaddr_in = (uint64_t) value;
7133     uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7134     void *haddr;
7135     int mem_idx = arm_env_mmu_index(env);
7136 
7137     /* This won't be crossing page boundaries */
7138     haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7139     if (haddr) {
7140 #ifndef CONFIG_USER_ONLY
7141 
7142         ram_addr_t offset;
7143         MemoryRegion *mr;
7144 
7145         /* RCU lock is already being held */
7146         mr = memory_region_from_host(haddr, &offset);
7147 
7148         if (mr) {
7149             memory_region_writeback(mr, offset, dline_size);
7150         }
7151 #endif /*CONFIG_USER_ONLY*/
7152     }
7153 #else
7154     /* Handled by hardware accelerator. */
7155     g_assert_not_reached();
7156 #endif /* CONFIG_TCG */
7157 }
7158 
7159 static const ARMCPRegInfo dcpop_reg[] = {
7160     { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7161       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7162       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7163       .fgt = FGT_DCCVAP,
7164       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7165 };
7166 
7167 static const ARMCPRegInfo dcpodp_reg[] = {
7168     { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7169       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7170       .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7171       .fgt = FGT_DCCVADP,
7172       .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7173 };
7174 
access_aa64_tid5(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7175 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7176                                        bool isread)
7177 {
7178     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7179         return CP_ACCESS_TRAP_EL2;
7180     }
7181 
7182     return CP_ACCESS_OK;
7183 }
7184 
access_mte(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7185 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7186                                  bool isread)
7187 {
7188     int el = arm_current_el(env);
7189     if (el < 2 && arm_is_el2_enabled(env)) {
7190         uint64_t hcr = arm_hcr_el2_eff(env);
7191         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7192             return CP_ACCESS_TRAP_EL2;
7193         }
7194     }
7195     if (el < 3 &&
7196         arm_feature(env, ARM_FEATURE_EL3) &&
7197         !(env->cp15.scr_el3 & SCR_ATA)) {
7198         return CP_ACCESS_TRAP_EL3;
7199     }
7200     return CP_ACCESS_OK;
7201 }
7202 
access_tfsr_el1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7203 static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri,
7204                                       bool isread)
7205 {
7206     CPAccessResult nv1 = access_nv1(env, ri, isread);
7207 
7208     if (nv1 != CP_ACCESS_OK) {
7209         return nv1;
7210     }
7211     return access_mte(env, ri, isread);
7212 }
7213 
access_tfsr_el2(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7214 static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri,
7215                                       bool isread)
7216 {
7217     /*
7218      * TFSR_EL2: similar to generic access_mte(), but we need to
7219      * account for FEAT_NV. At EL1 this must be a FEAT_NV access;
7220      * if NV2 is enabled then we will redirect this to TFSR_EL1
7221      * after doing the HCR and SCR ATA traps; otherwise this will
7222      * be a trap to EL2 and the HCR/SCR traps do not apply.
7223      */
7224     int el = arm_current_el(env);
7225 
7226     if (el == 1 && (arm_hcr_el2_eff(env) & HCR_NV2)) {
7227         return CP_ACCESS_OK;
7228     }
7229     if (el < 2 && arm_is_el2_enabled(env)) {
7230         uint64_t hcr = arm_hcr_el2_eff(env);
7231         if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7232             return CP_ACCESS_TRAP_EL2;
7233         }
7234     }
7235     if (el < 3 &&
7236         arm_feature(env, ARM_FEATURE_EL3) &&
7237         !(env->cp15.scr_el3 & SCR_ATA)) {
7238         return CP_ACCESS_TRAP_EL3;
7239     }
7240     return CP_ACCESS_OK;
7241 }
7242 
tco_read(CPUARMState * env,const ARMCPRegInfo * ri)7243 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7244 {
7245     return env->pstate & PSTATE_TCO;
7246 }
7247 
tco_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t val)7248 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7249 {
7250     env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7251 }
7252 
7253 static const ARMCPRegInfo mte_reginfo[] = {
7254     { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7255       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7256       .access = PL1_RW, .accessfn = access_mte,
7257       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7258     { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7259       .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7260       .access = PL1_RW, .accessfn = access_tfsr_el1,
7261       .nv2_redirect_offset = 0x190 | NV2_REDIR_NV1,
7262       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7263     { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7264       .type = ARM_CP_NV2_REDIRECT,
7265       .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7266       .access = PL2_RW, .accessfn = access_tfsr_el2,
7267       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7268     { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7269       .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7270       .access = PL3_RW,
7271       .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7272     { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7273       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7274       .access = PL1_RW, .accessfn = access_mte,
7275       .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7276     { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7277       .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7278       .access = PL1_RW, .accessfn = access_mte,
7279       .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7280     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7281       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7282       .type = ARM_CP_NO_RAW,
7283       .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7284     { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7285       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7286       .type = ARM_CP_NOP, .access = PL1_W,
7287       .fgt = FGT_DCIVAC,
7288       .accessfn = aa64_cacheop_poc_access },
7289     { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7290       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7291       .fgt = FGT_DCISW,
7292       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7293     { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7294       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7295       .type = ARM_CP_NOP, .access = PL1_W,
7296       .fgt = FGT_DCIVAC,
7297       .accessfn = aa64_cacheop_poc_access },
7298     { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7299       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7300       .fgt = FGT_DCISW,
7301       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7302     { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7303       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7304       .fgt = FGT_DCCSW,
7305       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7306     { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7307       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7308       .fgt = FGT_DCCSW,
7309       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7310     { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7311       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7312       .fgt = FGT_DCCISW,
7313       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7314     { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7315       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7316       .fgt = FGT_DCCISW,
7317       .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7318 };
7319 
7320 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7321     { .name = "TCO", .state = ARM_CP_STATE_AA64,
7322       .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7323       .type = ARM_CP_CONST, .access = PL0_RW, },
7324 };
7325 
7326 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7327     { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7328       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7329       .type = ARM_CP_NOP, .access = PL0_W,
7330       .fgt = FGT_DCCVAC,
7331       .accessfn = aa64_cacheop_poc_access },
7332     { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7333       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7334       .type = ARM_CP_NOP, .access = PL0_W,
7335       .fgt = FGT_DCCVAC,
7336       .accessfn = aa64_cacheop_poc_access },
7337     { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7338       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7339       .type = ARM_CP_NOP, .access = PL0_W,
7340       .fgt = FGT_DCCVAP,
7341       .accessfn = aa64_cacheop_poc_access },
7342     { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7343       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7344       .type = ARM_CP_NOP, .access = PL0_W,
7345       .fgt = FGT_DCCVAP,
7346       .accessfn = aa64_cacheop_poc_access },
7347     { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7348       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7349       .type = ARM_CP_NOP, .access = PL0_W,
7350       .fgt = FGT_DCCVADP,
7351       .accessfn = aa64_cacheop_poc_access },
7352     { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7353       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7354       .type = ARM_CP_NOP, .access = PL0_W,
7355       .fgt = FGT_DCCVADP,
7356       .accessfn = aa64_cacheop_poc_access },
7357     { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7358       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7359       .type = ARM_CP_NOP, .access = PL0_W,
7360       .fgt = FGT_DCCIVAC,
7361       .accessfn = aa64_cacheop_poc_access },
7362     { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7363       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7364       .type = ARM_CP_NOP, .access = PL0_W,
7365       .fgt = FGT_DCCIVAC,
7366       .accessfn = aa64_cacheop_poc_access },
7367     { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7368       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7369       .access = PL0_W, .type = ARM_CP_DC_GVA,
7370 #ifndef CONFIG_USER_ONLY
7371       /* Avoid overhead of an access check that always passes in user-mode */
7372       .accessfn = aa64_zva_access,
7373       .fgt = FGT_DCZVA,
7374 #endif
7375     },
7376     { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7377       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7378       .access = PL0_W, .type = ARM_CP_DC_GZVA,
7379 #ifndef CONFIG_USER_ONLY
7380       /* Avoid overhead of an access check that always passes in user-mode */
7381       .accessfn = aa64_zva_access,
7382       .fgt = FGT_DCZVA,
7383 #endif
7384     },
7385 };
7386 
access_scxtnum(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7387 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7388                                      bool isread)
7389 {
7390     uint64_t hcr = arm_hcr_el2_eff(env);
7391     int el = arm_current_el(env);
7392 
7393     if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7394         if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7395             if (hcr & HCR_TGE) {
7396                 return CP_ACCESS_TRAP_EL2;
7397             }
7398             return CP_ACCESS_TRAP_EL1;
7399         }
7400     } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7401         return CP_ACCESS_TRAP_EL2;
7402     }
7403     if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7404         return CP_ACCESS_TRAP_EL2;
7405     }
7406     if (el < 3
7407         && arm_feature(env, ARM_FEATURE_EL3)
7408         && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7409         return CP_ACCESS_TRAP_EL3;
7410     }
7411     return CP_ACCESS_OK;
7412 }
7413 
access_scxtnum_el1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7414 static CPAccessResult access_scxtnum_el1(CPUARMState *env,
7415                                          const ARMCPRegInfo *ri,
7416                                          bool isread)
7417 {
7418     CPAccessResult nv1 = access_nv1(env, ri, isread);
7419 
7420     if (nv1 != CP_ACCESS_OK) {
7421         return nv1;
7422     }
7423     return access_scxtnum(env, ri, isread);
7424 }
7425 
7426 static const ARMCPRegInfo scxtnum_reginfo[] = {
7427     { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7428       .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7429       .access = PL0_RW, .accessfn = access_scxtnum,
7430       .fgt = FGT_SCXTNUM_EL0,
7431       .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7432     { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7433       .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7434       .access = PL1_RW, .accessfn = access_scxtnum_el1,
7435       .fgt = FGT_SCXTNUM_EL1,
7436       .nv2_redirect_offset = 0x188 | NV2_REDIR_NV1,
7437       .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7438     { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7439       .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7440       .access = PL2_RW, .accessfn = access_scxtnum,
7441       .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7442     { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7443       .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7444       .access = PL3_RW,
7445       .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7446 };
7447 
access_fgt(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7448 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
7449                                  bool isread)
7450 {
7451     if (arm_current_el(env) == 2 &&
7452         arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
7453         return CP_ACCESS_TRAP_EL3;
7454     }
7455     return CP_ACCESS_OK;
7456 }
7457 
7458 static const ARMCPRegInfo fgt_reginfo[] = {
7459     { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7460       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
7461       .nv2_redirect_offset = 0x1b8,
7462       .access = PL2_RW, .accessfn = access_fgt,
7463       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
7464     { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7465       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
7466       .nv2_redirect_offset = 0x1c0,
7467       .access = PL2_RW, .accessfn = access_fgt,
7468       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
7469     { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7470       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
7471       .nv2_redirect_offset = 0x1d0,
7472       .access = PL2_RW, .accessfn = access_fgt,
7473       .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
7474     { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7475       .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
7476       .nv2_redirect_offset = 0x1d8,
7477       .access = PL2_RW, .accessfn = access_fgt,
7478       .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
7479     { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
7480       .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
7481       .nv2_redirect_offset = 0x1c8,
7482       .access = PL2_RW, .accessfn = access_fgt,
7483       .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
7484 };
7485 
vncr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7486 static void vncr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7487                        uint64_t value)
7488 {
7489     /*
7490      * Clear the RES0 bottom 12 bits; this means at runtime we can guarantee
7491      * that VNCR_EL2 + offset is 64-bit aligned. We don't need to do anything
7492      * about the RESS bits at the top -- we choose the "generate an EL2
7493      * translation abort on use" CONSTRAINED UNPREDICTABLE option (i.e. let
7494      * the ptw.c code detect the resulting invalid address).
7495      */
7496     env->cp15.vncr_el2 = value & ~0xfffULL;
7497 }
7498 
7499 static const ARMCPRegInfo nv2_reginfo[] = {
7500     { .name = "VNCR_EL2", .state = ARM_CP_STATE_AA64,
7501       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 2, .opc2 = 0,
7502       .access = PL2_RW,
7503       .writefn = vncr_write,
7504       .nv2_redirect_offset = 0xb0,
7505       .fieldoffset = offsetof(CPUARMState, cp15.vncr_el2) },
7506 };
7507 
access_predinv(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7508 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7509                                      bool isread)
7510 {
7511     int el = arm_current_el(env);
7512 
7513     if (el == 0) {
7514         uint64_t sctlr = arm_sctlr(env, el);
7515         if (!(sctlr & SCTLR_EnRCTX)) {
7516             return CP_ACCESS_TRAP_EL1;
7517         }
7518     } else if (el == 1) {
7519         uint64_t hcr = arm_hcr_el2_eff(env);
7520         if (hcr & HCR_NV) {
7521             return CP_ACCESS_TRAP_EL2;
7522         }
7523     }
7524     return CP_ACCESS_OK;
7525 }
7526 
7527 static const ARMCPRegInfo predinv_reginfo[] = {
7528     { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7529       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7530       .fgt = FGT_CFPRCTX,
7531       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7532     { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7533       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7534       .fgt = FGT_DVPRCTX,
7535       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7536     { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7537       .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7538       .fgt = FGT_CPPRCTX,
7539       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7540     /*
7541      * Note the AArch32 opcodes have a different OPC1.
7542      */
7543     { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7544       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7545       .fgt = FGT_CFPRCTX,
7546       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7547     { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7548       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7549       .fgt = FGT_DVPRCTX,
7550       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7551     { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7552       .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7553       .fgt = FGT_CPPRCTX,
7554       .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7555 };
7556 
ccsidr2_read(CPUARMState * env,const ARMCPRegInfo * ri)7557 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7558 {
7559     /* Read the high 32 bits of the current CCSIDR */
7560     return extract64(ccsidr_read(env, ri), 32, 32);
7561 }
7562 
7563 static const ARMCPRegInfo ccsidr2_reginfo[] = {
7564     { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7565       .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7566       .access = PL1_R,
7567       .accessfn = access_tid4,
7568       .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
7569 };
7570 
access_aa64_tid3(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7571 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7572                                        bool isread)
7573 {
7574     if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7575         return CP_ACCESS_TRAP_EL2;
7576     }
7577 
7578     return CP_ACCESS_OK;
7579 }
7580 
access_aa32_tid3(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7581 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7582                                        bool isread)
7583 {
7584     if (arm_feature(env, ARM_FEATURE_V8)) {
7585         return access_aa64_tid3(env, ri, isread);
7586     }
7587 
7588     return CP_ACCESS_OK;
7589 }
7590 
access_jazelle(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7591 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7592                                      bool isread)
7593 {
7594     if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7595         return CP_ACCESS_TRAP_EL2;
7596     }
7597 
7598     return CP_ACCESS_OK;
7599 }
7600 
access_joscr_jmcr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7601 static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7602                                         const ARMCPRegInfo *ri, bool isread)
7603 {
7604     /*
7605      * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7606      * in v7A, not in v8A.
7607      */
7608     if (!arm_feature(env, ARM_FEATURE_V8) &&
7609         arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7610         (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7611         return CP_ACCESS_TRAP_EL2;
7612     }
7613     return CP_ACCESS_OK;
7614 }
7615 
7616 static const ARMCPRegInfo jazelle_regs[] = {
7617     { .name = "JIDR",
7618       .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7619       .access = PL1_R, .accessfn = access_jazelle,
7620       .type = ARM_CP_CONST, .resetvalue = 0 },
7621     { .name = "JOSCR",
7622       .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
7623       .accessfn = access_joscr_jmcr,
7624       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7625     { .name = "JMCR",
7626       .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
7627       .accessfn = access_joscr_jmcr,
7628       .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7629 };
7630 
7631 static const ARMCPRegInfo contextidr_el2 = {
7632     .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7633     .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7634     .access = PL2_RW,
7635     .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7636 };
7637 
7638 static const ARMCPRegInfo vhe_reginfo[] = {
7639     { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7640       .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7641       .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7642       .raw_writefn = raw_write,
7643       .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
7644 #ifndef CONFIG_USER_ONLY
7645     { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7646       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7647       .fieldoffset =
7648         offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7649       .type = ARM_CP_IO, .access = PL2_RW,
7650       .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7651     { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7652       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7653       .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7654       .resetfn = gt_hv_timer_reset,
7655       .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7656     { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7657       .type = ARM_CP_IO,
7658       .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7659       .access = PL2_RW,
7660       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7661       .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
7662     { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7663       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7664       .type = ARM_CP_IO | ARM_CP_ALIAS,
7665       .access = PL2_RW, .accessfn = access_el1nvpct,
7666       .nv2_redirect_offset = 0x180 | NV2_REDIR_NO_NV1,
7667       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7668       .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7669     { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7670       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7671       .type = ARM_CP_IO | ARM_CP_ALIAS,
7672       .access = PL2_RW, .accessfn = access_el1nvvct,
7673       .nv2_redirect_offset = 0x170 | NV2_REDIR_NO_NV1,
7674       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7675       .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7676     { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7677       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7678       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7679       .access = PL2_RW, .accessfn = e2h_access,
7680       .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7681     { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7682       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7683       .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7684       .access = PL2_RW, .accessfn = e2h_access,
7685       .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7686     { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7687       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7688       .type = ARM_CP_IO | ARM_CP_ALIAS,
7689       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7690       .nv2_redirect_offset = 0x178 | NV2_REDIR_NO_NV1,
7691       .access = PL2_RW, .accessfn = access_el1nvpct,
7692       .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7693     { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7694       .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7695       .type = ARM_CP_IO | ARM_CP_ALIAS,
7696       .nv2_redirect_offset = 0x168 | NV2_REDIR_NO_NV1,
7697       .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7698       .access = PL2_RW, .accessfn = access_el1nvvct,
7699       .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
7700 #endif
7701 };
7702 
7703 #ifndef CONFIG_USER_ONLY
7704 static const ARMCPRegInfo ats1e1_reginfo[] = {
7705     { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
7706       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7707       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7708       .fgt = FGT_ATS1E1RP,
7709       .accessfn = at_s1e01_access, .writefn = ats_write64 },
7710     { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
7711       .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7712       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7713       .fgt = FGT_ATS1E1WP,
7714       .accessfn = at_s1e01_access, .writefn = ats_write64 },
7715 };
7716 
7717 static const ARMCPRegInfo ats1cp_reginfo[] = {
7718     { .name = "ATS1CPRP",
7719       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7720       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7721       .writefn = ats_write },
7722     { .name = "ATS1CPWP",
7723       .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7724       .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7725       .writefn = ats_write },
7726 };
7727 #endif
7728 
7729 /*
7730  * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7731  * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7732  * is non-zero, which is never for ARMv7, optionally in ARMv8
7733  * and mandatorily for ARMv8.2 and up.
7734  * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7735  * implementation is RAZ/WI we can ignore this detail, as we
7736  * do for ACTLR.
7737  */
7738 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7739     { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7740       .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
7741       .access = PL1_RW, .accessfn = access_tacr,
7742       .type = ARM_CP_CONST, .resetvalue = 0 },
7743     { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7744       .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7745       .access = PL2_RW, .type = ARM_CP_CONST,
7746       .resetvalue = 0 },
7747 };
7748 
register_cp_regs_for_features(ARMCPU * cpu)7749 void register_cp_regs_for_features(ARMCPU *cpu)
7750 {
7751     /* Register all the coprocessor registers based on feature bits */
7752     CPUARMState *env = &cpu->env;
7753     if (arm_feature(env, ARM_FEATURE_M)) {
7754         /* M profile has no coprocessor registers */
7755         return;
7756     }
7757 
7758     define_arm_cp_regs(cpu, cp_reginfo);
7759     if (!arm_feature(env, ARM_FEATURE_V8)) {
7760         /*
7761          * Must go early as it is full of wildcards that may be
7762          * overridden by later definitions.
7763          */
7764         define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7765     }
7766 
7767 #ifndef CONFIG_USER_ONLY
7768     define_tlb_insn_regs(cpu);
7769 #endif
7770 
7771     if (arm_feature(env, ARM_FEATURE_V6)) {
7772         /* The ID registers all have impdef reset values */
7773         ARMCPRegInfo v6_idregs[] = {
7774             { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7775               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7776               .access = PL1_R, .type = ARM_CP_CONST,
7777               .accessfn = access_aa32_tid3,
7778               .resetvalue = cpu->isar.id_pfr0 },
7779             /*
7780              * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7781              * the value of the GIC field until after we define these regs.
7782              */
7783             { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7784               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7785               .access = PL1_R, .type = ARM_CP_NO_RAW,
7786               .accessfn = access_aa32_tid3,
7787 #ifdef CONFIG_USER_ONLY
7788               .type = ARM_CP_CONST,
7789               .resetvalue = cpu->isar.id_pfr1,
7790 #else
7791               .type = ARM_CP_NO_RAW,
7792               .accessfn = access_aa32_tid3,
7793               .readfn = id_pfr1_read,
7794               .writefn = arm_cp_write_ignore
7795 #endif
7796             },
7797             { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7798               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7799               .access = PL1_R, .type = ARM_CP_CONST,
7800               .accessfn = access_aa32_tid3,
7801               .resetvalue = cpu->isar.id_dfr0 },
7802             { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7803               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7804               .access = PL1_R, .type = ARM_CP_CONST,
7805               .accessfn = access_aa32_tid3,
7806               .resetvalue = cpu->id_afr0 },
7807             { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7808               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7809               .access = PL1_R, .type = ARM_CP_CONST,
7810               .accessfn = access_aa32_tid3,
7811               .resetvalue = cpu->isar.id_mmfr0 },
7812             { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7813               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7814               .access = PL1_R, .type = ARM_CP_CONST,
7815               .accessfn = access_aa32_tid3,
7816               .resetvalue = cpu->isar.id_mmfr1 },
7817             { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7818               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7819               .access = PL1_R, .type = ARM_CP_CONST,
7820               .accessfn = access_aa32_tid3,
7821               .resetvalue = cpu->isar.id_mmfr2 },
7822             { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7823               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7824               .access = PL1_R, .type = ARM_CP_CONST,
7825               .accessfn = access_aa32_tid3,
7826               .resetvalue = cpu->isar.id_mmfr3 },
7827             { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7828               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7829               .access = PL1_R, .type = ARM_CP_CONST,
7830               .accessfn = access_aa32_tid3,
7831               .resetvalue = cpu->isar.id_isar0 },
7832             { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7833               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7834               .access = PL1_R, .type = ARM_CP_CONST,
7835               .accessfn = access_aa32_tid3,
7836               .resetvalue = cpu->isar.id_isar1 },
7837             { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7838               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7839               .access = PL1_R, .type = ARM_CP_CONST,
7840               .accessfn = access_aa32_tid3,
7841               .resetvalue = cpu->isar.id_isar2 },
7842             { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7843               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7844               .access = PL1_R, .type = ARM_CP_CONST,
7845               .accessfn = access_aa32_tid3,
7846               .resetvalue = cpu->isar.id_isar3 },
7847             { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7848               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7849               .access = PL1_R, .type = ARM_CP_CONST,
7850               .accessfn = access_aa32_tid3,
7851               .resetvalue = cpu->isar.id_isar4 },
7852             { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7853               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7854               .access = PL1_R, .type = ARM_CP_CONST,
7855               .accessfn = access_aa32_tid3,
7856               .resetvalue = cpu->isar.id_isar5 },
7857             { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7858               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7859               .access = PL1_R, .type = ARM_CP_CONST,
7860               .accessfn = access_aa32_tid3,
7861               .resetvalue = cpu->isar.id_mmfr4 },
7862             { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7863               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7864               .access = PL1_R, .type = ARM_CP_CONST,
7865               .accessfn = access_aa32_tid3,
7866               .resetvalue = cpu->isar.id_isar6 },
7867         };
7868         define_arm_cp_regs(cpu, v6_idregs);
7869         define_arm_cp_regs(cpu, v6_cp_reginfo);
7870     } else {
7871         define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7872     }
7873     if (arm_feature(env, ARM_FEATURE_V6K)) {
7874         define_arm_cp_regs(cpu, v6k_cp_reginfo);
7875     }
7876     if (arm_feature(env, ARM_FEATURE_V7VE)) {
7877         define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7878     }
7879     if (arm_feature(env, ARM_FEATURE_V7)) {
7880         ARMCPRegInfo clidr = {
7881             .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7882             .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7883             .access = PL1_R, .type = ARM_CP_CONST,
7884             .accessfn = access_tid4,
7885             .fgt = FGT_CLIDR_EL1,
7886             .resetvalue = cpu->clidr
7887         };
7888         define_one_arm_cp_reg(cpu, &clidr);
7889         define_arm_cp_regs(cpu, v7_cp_reginfo);
7890         define_debug_regs(cpu);
7891         define_pmu_regs(cpu);
7892     } else {
7893         define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7894     }
7895     if (arm_feature(env, ARM_FEATURE_V8)) {
7896         /*
7897          * v8 ID registers, which all have impdef reset values.
7898          * Note that within the ID register ranges the unused slots
7899          * must all RAZ, not UNDEF; future architecture versions may
7900          * define new registers here.
7901          * ID registers which are AArch64 views of the AArch32 ID registers
7902          * which already existed in v6 and v7 are handled elsewhere,
7903          * in v6_idregs[].
7904          */
7905         int i;
7906         ARMCPRegInfo v8_idregs[] = {
7907             /*
7908              * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7909              * emulation because we don't know the right value for the
7910              * GIC field until after we define these regs.
7911              */
7912             { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7913               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7914               .access = PL1_R,
7915 #ifdef CONFIG_USER_ONLY
7916               .type = ARM_CP_CONST,
7917               .resetvalue = cpu->isar.id_aa64pfr0
7918 #else
7919               .type = ARM_CP_NO_RAW,
7920               .accessfn = access_aa64_tid3,
7921               .readfn = id_aa64pfr0_read,
7922               .writefn = arm_cp_write_ignore
7923 #endif
7924             },
7925             { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7926               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7927               .access = PL1_R, .type = ARM_CP_CONST,
7928               .accessfn = access_aa64_tid3,
7929               .resetvalue = cpu->isar.id_aa64pfr1},
7930             { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7931               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7932               .access = PL1_R, .type = ARM_CP_CONST,
7933               .accessfn = access_aa64_tid3,
7934               .resetvalue = 0 },
7935             { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7936               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7937               .access = PL1_R, .type = ARM_CP_CONST,
7938               .accessfn = access_aa64_tid3,
7939               .resetvalue = 0 },
7940             { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7941               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7942               .access = PL1_R, .type = ARM_CP_CONST,
7943               .accessfn = access_aa64_tid3,
7944               .resetvalue = cpu->isar.id_aa64zfr0 },
7945             { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7946               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7947               .access = PL1_R, .type = ARM_CP_CONST,
7948               .accessfn = access_aa64_tid3,
7949               .resetvalue = cpu->isar.id_aa64smfr0 },
7950             { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7951               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7952               .access = PL1_R, .type = ARM_CP_CONST,
7953               .accessfn = access_aa64_tid3,
7954               .resetvalue = 0 },
7955             { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7956               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7957               .access = PL1_R, .type = ARM_CP_CONST,
7958               .accessfn = access_aa64_tid3,
7959               .resetvalue = 0 },
7960             { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7961               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7962               .access = PL1_R, .type = ARM_CP_CONST,
7963               .accessfn = access_aa64_tid3,
7964               .resetvalue = cpu->isar.id_aa64dfr0 },
7965             { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7966               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7967               .access = PL1_R, .type = ARM_CP_CONST,
7968               .accessfn = access_aa64_tid3,
7969               .resetvalue = cpu->isar.id_aa64dfr1 },
7970             { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7971               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7972               .access = PL1_R, .type = ARM_CP_CONST,
7973               .accessfn = access_aa64_tid3,
7974               .resetvalue = 0 },
7975             { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7976               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7977               .access = PL1_R, .type = ARM_CP_CONST,
7978               .accessfn = access_aa64_tid3,
7979               .resetvalue = 0 },
7980             { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7981               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7982               .access = PL1_R, .type = ARM_CP_CONST,
7983               .accessfn = access_aa64_tid3,
7984               .resetvalue = cpu->id_aa64afr0 },
7985             { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7986               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7987               .access = PL1_R, .type = ARM_CP_CONST,
7988               .accessfn = access_aa64_tid3,
7989               .resetvalue = cpu->id_aa64afr1 },
7990             { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7991               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7992               .access = PL1_R, .type = ARM_CP_CONST,
7993               .accessfn = access_aa64_tid3,
7994               .resetvalue = 0 },
7995             { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7996               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7997               .access = PL1_R, .type = ARM_CP_CONST,
7998               .accessfn = access_aa64_tid3,
7999               .resetvalue = 0 },
8000             { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8001               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8002               .access = PL1_R, .type = ARM_CP_CONST,
8003               .accessfn = access_aa64_tid3,
8004               .resetvalue = cpu->isar.id_aa64isar0 },
8005             { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8006               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8007               .access = PL1_R, .type = ARM_CP_CONST,
8008               .accessfn = access_aa64_tid3,
8009               .resetvalue = cpu->isar.id_aa64isar1 },
8010             { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
8011               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8012               .access = PL1_R, .type = ARM_CP_CONST,
8013               .accessfn = access_aa64_tid3,
8014               .resetvalue = cpu->isar.id_aa64isar2 },
8015             { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8016               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8017               .access = PL1_R, .type = ARM_CP_CONST,
8018               .accessfn = access_aa64_tid3,
8019               .resetvalue = 0 },
8020             { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8021               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8022               .access = PL1_R, .type = ARM_CP_CONST,
8023               .accessfn = access_aa64_tid3,
8024               .resetvalue = 0 },
8025             { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8026               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8027               .access = PL1_R, .type = ARM_CP_CONST,
8028               .accessfn = access_aa64_tid3,
8029               .resetvalue = 0 },
8030             { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8031               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8032               .access = PL1_R, .type = ARM_CP_CONST,
8033               .accessfn = access_aa64_tid3,
8034               .resetvalue = 0 },
8035             { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8036               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8037               .access = PL1_R, .type = ARM_CP_CONST,
8038               .accessfn = access_aa64_tid3,
8039               .resetvalue = 0 },
8040             { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8041               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8042               .access = PL1_R, .type = ARM_CP_CONST,
8043               .accessfn = access_aa64_tid3,
8044               .resetvalue = cpu->isar.id_aa64mmfr0 },
8045             { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8046               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8047               .access = PL1_R, .type = ARM_CP_CONST,
8048               .accessfn = access_aa64_tid3,
8049               .resetvalue = cpu->isar.id_aa64mmfr1 },
8050             { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
8051               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8052               .access = PL1_R, .type = ARM_CP_CONST,
8053               .accessfn = access_aa64_tid3,
8054               .resetvalue = cpu->isar.id_aa64mmfr2 },
8055             { .name = "ID_AA64MMFR3_EL1", .state = ARM_CP_STATE_AA64,
8056               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8057               .access = PL1_R, .type = ARM_CP_CONST,
8058               .accessfn = access_aa64_tid3,
8059               .resetvalue = cpu->isar.id_aa64mmfr3 },
8060             { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8061               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8062               .access = PL1_R, .type = ARM_CP_CONST,
8063               .accessfn = access_aa64_tid3,
8064               .resetvalue = 0 },
8065             { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8066               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8067               .access = PL1_R, .type = ARM_CP_CONST,
8068               .accessfn = access_aa64_tid3,
8069               .resetvalue = 0 },
8070             { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8071               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8072               .access = PL1_R, .type = ARM_CP_CONST,
8073               .accessfn = access_aa64_tid3,
8074               .resetvalue = 0 },
8075             { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8076               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8077               .access = PL1_R, .type = ARM_CP_CONST,
8078               .accessfn = access_aa64_tid3,
8079               .resetvalue = 0 },
8080             { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8081               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8082               .access = PL1_R, .type = ARM_CP_CONST,
8083               .accessfn = access_aa64_tid3,
8084               .resetvalue = cpu->isar.mvfr0 },
8085             { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8086               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8087               .access = PL1_R, .type = ARM_CP_CONST,
8088               .accessfn = access_aa64_tid3,
8089               .resetvalue = cpu->isar.mvfr1 },
8090             { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8091               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8092               .access = PL1_R, .type = ARM_CP_CONST,
8093               .accessfn = access_aa64_tid3,
8094               .resetvalue = cpu->isar.mvfr2 },
8095             /*
8096              * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8097              * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8098              * as RAZ, since it is in the "reserved for future ID
8099              * registers, RAZ" part of the AArch32 encoding space.
8100              */
8101             { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8102               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8103               .access = PL1_R, .type = ARM_CP_CONST,
8104               .accessfn = access_aa64_tid3,
8105               .resetvalue = 0 },
8106             { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8107               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8108               .access = PL1_R, .type = ARM_CP_CONST,
8109               .accessfn = access_aa64_tid3,
8110               .resetvalue = 0 },
8111             { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8112               .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8113               .access = PL1_R, .type = ARM_CP_CONST,
8114               .accessfn = access_aa64_tid3,
8115               .resetvalue = 0 },
8116             /*
8117              * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8118              * they're also RAZ for AArch64, and in v8 are gradually
8119              * being filled with AArch64-view-of-AArch32-ID-register
8120              * for new ID registers.
8121              */
8122             { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
8123               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8124               .access = PL1_R, .type = ARM_CP_CONST,
8125               .accessfn = access_aa64_tid3,
8126               .resetvalue = 0 },
8127             { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
8128               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8129               .access = PL1_R, .type = ARM_CP_CONST,
8130               .accessfn = access_aa64_tid3,
8131               .resetvalue = cpu->isar.id_pfr2 },
8132             { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
8133               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8134               .access = PL1_R, .type = ARM_CP_CONST,
8135               .accessfn = access_aa64_tid3,
8136               .resetvalue = cpu->isar.id_dfr1 },
8137             { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
8138               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8139               .access = PL1_R, .type = ARM_CP_CONST,
8140               .accessfn = access_aa64_tid3,
8141               .resetvalue = cpu->isar.id_mmfr5 },
8142             { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
8143               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8144               .access = PL1_R, .type = ARM_CP_CONST,
8145               .accessfn = access_aa64_tid3,
8146               .resetvalue = 0 },
8147             { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8148               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8149               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8150               .fgt = FGT_PMCEIDN_EL0,
8151               .resetvalue = extract64(cpu->pmceid0, 0, 32) },
8152             { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8153               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8154               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8155               .fgt = FGT_PMCEIDN_EL0,
8156               .resetvalue = cpu->pmceid0 },
8157             { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8158               .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8159               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8160               .fgt = FGT_PMCEIDN_EL0,
8161               .resetvalue = extract64(cpu->pmceid1, 0, 32) },
8162             { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8163               .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8164               .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8165               .fgt = FGT_PMCEIDN_EL0,
8166               .resetvalue = cpu->pmceid1 },
8167         };
8168 #ifdef CONFIG_USER_ONLY
8169         static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
8170             { .name = "ID_AA64PFR0_EL1",
8171               .exported_bits = R_ID_AA64PFR0_FP_MASK |
8172                                R_ID_AA64PFR0_ADVSIMD_MASK |
8173                                R_ID_AA64PFR0_SVE_MASK |
8174                                R_ID_AA64PFR0_DIT_MASK,
8175               .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8176                             (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
8177             { .name = "ID_AA64PFR1_EL1",
8178               .exported_bits = R_ID_AA64PFR1_BT_MASK |
8179                                R_ID_AA64PFR1_SSBS_MASK |
8180                                R_ID_AA64PFR1_MTE_MASK |
8181                                R_ID_AA64PFR1_SME_MASK },
8182             { .name = "ID_AA64PFR*_EL1_RESERVED",
8183               .is_glob = true },
8184             { .name = "ID_AA64ZFR0_EL1",
8185               .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8186                                R_ID_AA64ZFR0_AES_MASK |
8187                                R_ID_AA64ZFR0_BITPERM_MASK |
8188                                R_ID_AA64ZFR0_BFLOAT16_MASK |
8189                                R_ID_AA64ZFR0_B16B16_MASK |
8190                                R_ID_AA64ZFR0_SHA3_MASK |
8191                                R_ID_AA64ZFR0_SM4_MASK |
8192                                R_ID_AA64ZFR0_I8MM_MASK |
8193                                R_ID_AA64ZFR0_F32MM_MASK |
8194                                R_ID_AA64ZFR0_F64MM_MASK },
8195             { .name = "ID_AA64SMFR0_EL1",
8196               .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8197                                R_ID_AA64SMFR0_BI32I32_MASK |
8198                                R_ID_AA64SMFR0_B16F32_MASK |
8199                                R_ID_AA64SMFR0_F16F32_MASK |
8200                                R_ID_AA64SMFR0_I8I32_MASK |
8201                                R_ID_AA64SMFR0_F16F16_MASK |
8202                                R_ID_AA64SMFR0_B16B16_MASK |
8203                                R_ID_AA64SMFR0_I16I32_MASK |
8204                                R_ID_AA64SMFR0_F64F64_MASK |
8205                                R_ID_AA64SMFR0_I16I64_MASK |
8206                                R_ID_AA64SMFR0_SMEVER_MASK |
8207                                R_ID_AA64SMFR0_FA64_MASK },
8208             { .name = "ID_AA64MMFR0_EL1",
8209               .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8210               .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8211                             (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8212             { .name = "ID_AA64MMFR1_EL1",
8213               .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8214             { .name = "ID_AA64MMFR2_EL1",
8215               .exported_bits = R_ID_AA64MMFR2_AT_MASK },
8216             { .name = "ID_AA64MMFR3_EL1",
8217               .exported_bits = 0 },
8218             { .name = "ID_AA64MMFR*_EL1_RESERVED",
8219               .is_glob = true },
8220             { .name = "ID_AA64DFR0_EL1",
8221               .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8222             { .name = "ID_AA64DFR1_EL1" },
8223             { .name = "ID_AA64DFR*_EL1_RESERVED",
8224               .is_glob = true },
8225             { .name = "ID_AA64AFR*",
8226               .is_glob = true },
8227             { .name = "ID_AA64ISAR0_EL1",
8228               .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8229                                R_ID_AA64ISAR0_SHA1_MASK |
8230                                R_ID_AA64ISAR0_SHA2_MASK |
8231                                R_ID_AA64ISAR0_CRC32_MASK |
8232                                R_ID_AA64ISAR0_ATOMIC_MASK |
8233                                R_ID_AA64ISAR0_RDM_MASK |
8234                                R_ID_AA64ISAR0_SHA3_MASK |
8235                                R_ID_AA64ISAR0_SM3_MASK |
8236                                R_ID_AA64ISAR0_SM4_MASK |
8237                                R_ID_AA64ISAR0_DP_MASK |
8238                                R_ID_AA64ISAR0_FHM_MASK |
8239                                R_ID_AA64ISAR0_TS_MASK |
8240                                R_ID_AA64ISAR0_RNDR_MASK },
8241             { .name = "ID_AA64ISAR1_EL1",
8242               .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8243                                R_ID_AA64ISAR1_APA_MASK |
8244                                R_ID_AA64ISAR1_API_MASK |
8245                                R_ID_AA64ISAR1_JSCVT_MASK |
8246                                R_ID_AA64ISAR1_FCMA_MASK |
8247                                R_ID_AA64ISAR1_LRCPC_MASK |
8248                                R_ID_AA64ISAR1_GPA_MASK |
8249                                R_ID_AA64ISAR1_GPI_MASK |
8250                                R_ID_AA64ISAR1_FRINTTS_MASK |
8251                                R_ID_AA64ISAR1_SB_MASK |
8252                                R_ID_AA64ISAR1_BF16_MASK |
8253                                R_ID_AA64ISAR1_DGH_MASK |
8254                                R_ID_AA64ISAR1_I8MM_MASK },
8255             { .name = "ID_AA64ISAR2_EL1",
8256               .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8257                                R_ID_AA64ISAR2_RPRES_MASK |
8258                                R_ID_AA64ISAR2_GPA3_MASK |
8259                                R_ID_AA64ISAR2_APA3_MASK |
8260                                R_ID_AA64ISAR2_MOPS_MASK |
8261                                R_ID_AA64ISAR2_BC_MASK |
8262                                R_ID_AA64ISAR2_RPRFM_MASK |
8263                                R_ID_AA64ISAR2_CSSC_MASK },
8264             { .name = "ID_AA64ISAR*_EL1_RESERVED",
8265               .is_glob = true },
8266         };
8267         modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8268 #endif
8269         /*
8270          * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
8271          * TODO: For RMR, a write with bit 1 set should do something with
8272          * cpu_reset(). In the meantime, "the bit is strictly a request",
8273          * so we are in spec just ignoring writes.
8274          */
8275         if (!arm_feature(env, ARM_FEATURE_EL3) &&
8276             !arm_feature(env, ARM_FEATURE_EL2)) {
8277             ARMCPRegInfo el1_reset_regs[] = {
8278                 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
8279                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8280                   .access = PL1_R,
8281                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8282                 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
8283                   .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8284                   .access = PL1_RW, .type = ARM_CP_CONST,
8285                   .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
8286             };
8287             define_arm_cp_regs(cpu, el1_reset_regs);
8288         }
8289         define_arm_cp_regs(cpu, v8_idregs);
8290         define_arm_cp_regs(cpu, v8_cp_reginfo);
8291         if (cpu_isar_feature(aa64_aa32_el1, cpu)) {
8292             define_arm_cp_regs(cpu, v8_aa32_el1_reginfo);
8293         }
8294 
8295         for (i = 4; i < 16; i++) {
8296             /*
8297              * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8298              * For pre-v8 cores there are RAZ patterns for these in
8299              * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8300              * v8 extends the "must RAZ" part of the ID register space
8301              * to also cover c0, 0, c{8-15}, {0-7}.
8302              * These are STATE_AA32 because in the AArch64 sysreg space
8303              * c4-c7 is where the AArch64 ID registers live (and we've
8304              * already defined those in v8_idregs[]), and c8-c15 are not
8305              * "must RAZ" for AArch64.
8306              */
8307             g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8308             ARMCPRegInfo v8_aa32_raz_idregs = {
8309                 .name = name,
8310                 .state = ARM_CP_STATE_AA32,
8311                 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8312                 .access = PL1_R, .type = ARM_CP_CONST,
8313                 .accessfn = access_aa64_tid3,
8314                 .resetvalue = 0 };
8315             define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8316         }
8317     }
8318 
8319     /*
8320      * Register the base EL2 cpregs.
8321      * Pre v8, these registers are implemented only as part of the
8322      * Virtualization Extensions (EL2 present).  Beginning with v8,
8323      * if EL2 is missing but EL3 is enabled, mostly these become
8324      * RES0 from EL3, with some specific exceptions.
8325      */
8326     if (arm_feature(env, ARM_FEATURE_EL2)
8327         || (arm_feature(env, ARM_FEATURE_EL3)
8328             && arm_feature(env, ARM_FEATURE_V8))) {
8329         uint64_t vmpidr_def = mpidr_read_val(env);
8330         ARMCPRegInfo vpidr_regs[] = {
8331             { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8332               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8333               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8334               .resetvalue = cpu->midr,
8335               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8336               .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
8337             { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8338               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8339               .access = PL2_RW, .resetvalue = cpu->midr,
8340               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8341               .nv2_redirect_offset = 0x88,
8342               .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8343             { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8344               .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8345               .access = PL2_RW, .accessfn = access_el3_aa32ns,
8346               .resetvalue = vmpidr_def,
8347               .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8348               .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
8349             { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8350               .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8351               .access = PL2_RW, .resetvalue = vmpidr_def,
8352               .type = ARM_CP_EL3_NO_EL2_C_NZ,
8353               .nv2_redirect_offset = 0x50,
8354               .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
8355         };
8356         /*
8357          * The only field of MDCR_EL2 that has a defined architectural reset
8358          * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8359          */
8360         ARMCPRegInfo mdcr_el2 = {
8361             .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
8362             .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
8363             .writefn = mdcr_el2_write,
8364             .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8365             .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8366         };
8367         define_one_arm_cp_reg(cpu, &mdcr_el2);
8368         define_arm_cp_regs(cpu, vpidr_regs);
8369         define_arm_cp_regs(cpu, el2_cp_reginfo);
8370         if (arm_feature(env, ARM_FEATURE_V8)) {
8371             define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8372         }
8373         if (cpu_isar_feature(aa64_sel2, cpu)) {
8374             define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8375         }
8376         /*
8377          * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
8378          * See commentary near RMR_EL1.
8379          */
8380         if (!arm_feature(env, ARM_FEATURE_EL3)) {
8381             static const ARMCPRegInfo el2_reset_regs[] = {
8382                 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8383                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8384                   .access = PL2_R,
8385                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8386                 { .name = "RVBAR", .type = ARM_CP_ALIAS,
8387                   .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8388                   .access = PL2_R,
8389                   .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8390                 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
8391                   .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
8392                   .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8393             };
8394             define_arm_cp_regs(cpu, el2_reset_regs);
8395         }
8396     }
8397 
8398     /* Register the base EL3 cpregs. */
8399     if (arm_feature(env, ARM_FEATURE_EL3)) {
8400         define_arm_cp_regs(cpu, el3_cp_reginfo);
8401         ARMCPRegInfo el3_regs[] = {
8402             { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8403               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8404               .access = PL3_R,
8405               .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
8406             { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
8407               .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
8408               .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8409             { .name = "RMR", .state = ARM_CP_STATE_AA32,
8410               .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8411               .access = PL3_RW, .type = ARM_CP_CONST,
8412               .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
8413             { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8414               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8415               .access = PL3_RW,
8416               .raw_writefn = raw_write, .writefn = sctlr_write,
8417               .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8418               .resetvalue = cpu->reset_sctlr },
8419         };
8420 
8421         define_arm_cp_regs(cpu, el3_regs);
8422     }
8423     /*
8424      * The behaviour of NSACR is sufficiently various that we don't
8425      * try to describe it in a single reginfo:
8426      *  if EL3 is 64 bit, then trap to EL3 from S EL1,
8427      *     reads as constant 0xc00 from NS EL1 and NS EL2
8428      *  if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8429      *  if v7 without EL3, register doesn't exist
8430      *  if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8431      */
8432     if (arm_feature(env, ARM_FEATURE_EL3)) {
8433         if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8434             static const ARMCPRegInfo nsacr = {
8435                 .name = "NSACR", .type = ARM_CP_CONST,
8436                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8437                 .access = PL1_RW, .accessfn = nsacr_access,
8438                 .resetvalue = 0xc00
8439             };
8440             define_one_arm_cp_reg(cpu, &nsacr);
8441         } else {
8442             static const ARMCPRegInfo nsacr = {
8443                 .name = "NSACR",
8444                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8445                 .access = PL3_RW | PL1_R,
8446                 .resetvalue = 0,
8447                 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8448             };
8449             define_one_arm_cp_reg(cpu, &nsacr);
8450         }
8451     } else {
8452         if (arm_feature(env, ARM_FEATURE_V8)) {
8453             static const ARMCPRegInfo nsacr = {
8454                 .name = "NSACR", .type = ARM_CP_CONST,
8455                 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8456                 .access = PL1_R,
8457                 .resetvalue = 0xc00
8458             };
8459             define_one_arm_cp_reg(cpu, &nsacr);
8460         }
8461     }
8462 
8463     if (arm_feature(env, ARM_FEATURE_PMSA)) {
8464         if (arm_feature(env, ARM_FEATURE_V6)) {
8465             /* PMSAv6 not implemented */
8466             assert(arm_feature(env, ARM_FEATURE_V7));
8467             define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8468             define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8469         } else {
8470             define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8471         }
8472     } else {
8473         define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8474         define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8475         /* TTCBR2 is introduced with ARMv8.2-AA32HPD.  */
8476         if (cpu_isar_feature(aa32_hpd, cpu)) {
8477             define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8478         }
8479     }
8480     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8481         define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8482     }
8483     if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8484         define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8485     }
8486     if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
8487         define_arm_cp_regs(cpu, gen_timer_ecv_cp_reginfo);
8488     }
8489 #ifndef CONFIG_USER_ONLY
8490     if (cpu_isar_feature(aa64_ecv, cpu)) {
8491         define_one_arm_cp_reg(cpu, &gen_timer_cntpoff_reginfo);
8492     }
8493 #endif
8494     if (arm_feature(env, ARM_FEATURE_VAPA)) {
8495         ARMCPRegInfo vapa_cp_reginfo[] = {
8496             { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
8497               .access = PL1_RW, .resetvalue = 0,
8498               .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
8499                                      offsetoflow32(CPUARMState, cp15.par_ns) },
8500               .writefn = par_write},
8501 #ifndef CONFIG_USER_ONLY
8502             /* This underdecoding is safe because the reginfo is NO_RAW. */
8503             { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
8504               .access = PL1_W, .accessfn = ats_access,
8505               .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
8506 #endif
8507         };
8508 
8509         /*
8510          * When LPAE exists this 32-bit PAR register is an alias of the
8511          * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
8512          */
8513         if (arm_feature(env, ARM_FEATURE_LPAE)) {
8514             vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
8515         }
8516         define_arm_cp_regs(cpu, vapa_cp_reginfo);
8517     }
8518     if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8519         define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8520     }
8521     if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8522         define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8523     }
8524     if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8525         define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8526     }
8527     if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8528         define_arm_cp_regs(cpu, omap_cp_reginfo);
8529     }
8530     if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8531         define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8532     }
8533     if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8534         define_arm_cp_regs(cpu, xscale_cp_reginfo);
8535     }
8536     if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8537         define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8538     }
8539     if (arm_feature(env, ARM_FEATURE_LPAE)) {
8540         define_arm_cp_regs(cpu, lpae_cp_reginfo);
8541     }
8542     if (cpu_isar_feature(aa32_jazelle, cpu)) {
8543         define_arm_cp_regs(cpu, jazelle_regs);
8544     }
8545     /*
8546      * Slightly awkwardly, the OMAP and StrongARM cores need all of
8547      * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8548      * be read-only (ie write causes UNDEF exception).
8549      */
8550     {
8551         ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8552             /*
8553              * Pre-v8 MIDR space.
8554              * Note that the MIDR isn't a simple constant register because
8555              * of the TI925 behaviour where writes to another register can
8556              * cause the MIDR value to change.
8557              *
8558              * Unimplemented registers in the c15 0 0 0 space default to
8559              * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8560              * and friends override accordingly.
8561              */
8562             { .name = "MIDR",
8563               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8564               .access = PL1_R, .resetvalue = cpu->midr,
8565               .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8566               .readfn = midr_read,
8567               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8568               .type = ARM_CP_OVERRIDE },
8569             /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8570             { .name = "DUMMY",
8571               .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8572               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8573             { .name = "DUMMY",
8574               .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8575               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8576             { .name = "DUMMY",
8577               .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8578               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8579             { .name = "DUMMY",
8580               .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8581               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8582             { .name = "DUMMY",
8583               .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8584               .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8585         };
8586         ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8587             { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8588               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8589               .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8590               .fgt = FGT_MIDR_EL1,
8591               .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8592               .readfn = midr_read },
8593             /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
8594             { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8595               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8596               .access = PL1_R, .resetvalue = cpu->midr },
8597             { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8598               .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8599               .access = PL1_R,
8600               .accessfn = access_aa64_tid1,
8601               .fgt = FGT_REVIDR_EL1,
8602               .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8603         };
8604         ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
8605             .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
8606             .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8607             .access = PL1_R, .resetvalue = cpu->midr
8608         };
8609         ARMCPRegInfo id_cp_reginfo[] = {
8610             /* These are common to v8 and pre-v8 */
8611             { .name = "CTR",
8612               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8613               .access = PL1_R, .accessfn = ctr_el0_access,
8614               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8615             { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8616               .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8617               .access = PL0_R, .accessfn = ctr_el0_access,
8618               .fgt = FGT_CTR_EL0,
8619               .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8620             /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8621             { .name = "TCMTR",
8622               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8623               .access = PL1_R,
8624               .accessfn = access_aa32_tid1,
8625               .type = ARM_CP_CONST, .resetvalue = 0 },
8626         };
8627         /* TLBTR is specific to VMSA */
8628         ARMCPRegInfo id_tlbtr_reginfo = {
8629               .name = "TLBTR",
8630               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8631               .access = PL1_R,
8632               .accessfn = access_aa32_tid1,
8633               .type = ARM_CP_CONST, .resetvalue = 0,
8634         };
8635         /* MPUIR is specific to PMSA V6+ */
8636         ARMCPRegInfo id_mpuir_reginfo = {
8637               .name = "MPUIR",
8638               .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8639               .access = PL1_R, .type = ARM_CP_CONST,
8640               .resetvalue = cpu->pmsav7_dregion << 8
8641         };
8642         /* HMPUIR is specific to PMSA V8 */
8643         ARMCPRegInfo id_hmpuir_reginfo = {
8644             .name = "HMPUIR",
8645             .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
8646             .access = PL2_R, .type = ARM_CP_CONST,
8647             .resetvalue = cpu->pmsav8r_hdregion
8648         };
8649         static const ARMCPRegInfo crn0_wi_reginfo = {
8650             .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8651             .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8652             .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8653         };
8654 #ifdef CONFIG_USER_ONLY
8655         static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8656             { .name = "MIDR_EL1",
8657               .exported_bits = R_MIDR_EL1_REVISION_MASK |
8658                                R_MIDR_EL1_PARTNUM_MASK |
8659                                R_MIDR_EL1_ARCHITECTURE_MASK |
8660                                R_MIDR_EL1_VARIANT_MASK |
8661                                R_MIDR_EL1_IMPLEMENTER_MASK },
8662             { .name = "REVIDR_EL1" },
8663         };
8664         modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8665 #endif
8666         if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8667             arm_feature(env, ARM_FEATURE_STRONGARM)) {
8668             size_t i;
8669             /*
8670              * Register the blanket "writes ignored" value first to cover the
8671              * whole space. Then update the specific ID registers to allow write
8672              * access, so that they ignore writes rather than causing them to
8673              * UNDEF.
8674              */
8675             define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8676             for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8677                 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8678             }
8679             for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8680                 id_cp_reginfo[i].access = PL1_RW;
8681             }
8682             id_mpuir_reginfo.access = PL1_RW;
8683             id_tlbtr_reginfo.access = PL1_RW;
8684         }
8685         if (arm_feature(env, ARM_FEATURE_V8)) {
8686             define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8687             if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8688                 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
8689             }
8690         } else {
8691             define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8692         }
8693         define_arm_cp_regs(cpu, id_cp_reginfo);
8694         if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8695             define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8696         } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
8697                    arm_feature(env, ARM_FEATURE_V8)) {
8698             uint32_t i = 0;
8699             char *tmp_string;
8700 
8701             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8702             define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
8703             define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
8704 
8705             /* Register alias is only valid for first 32 indexes */
8706             for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
8707                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8708                 uint8_t opc1 = extract32(i, 4, 1);
8709                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8710 
8711                 tmp_string = g_strdup_printf("PRBAR%u", i);
8712                 ARMCPRegInfo tmp_prbarn_reginfo = {
8713                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8714                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8715                     .access = PL1_RW, .resetvalue = 0,
8716                     .accessfn = access_tvm_trvm,
8717                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8718                 };
8719                 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
8720                 g_free(tmp_string);
8721 
8722                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8723                 tmp_string = g_strdup_printf("PRLAR%u", i);
8724                 ARMCPRegInfo tmp_prlarn_reginfo = {
8725                     .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8726                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8727                     .access = PL1_RW, .resetvalue = 0,
8728                     .accessfn = access_tvm_trvm,
8729                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8730                 };
8731                 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
8732                 g_free(tmp_string);
8733             }
8734 
8735             /* Register alias is only valid for first 32 indexes */
8736             for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
8737                 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8738                 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
8739                 uint8_t opc2 = extract32(i, 0, 1) << 2;
8740 
8741                 tmp_string = g_strdup_printf("HPRBAR%u", i);
8742                 ARMCPRegInfo tmp_hprbarn_reginfo = {
8743                     .name = tmp_string,
8744                     .type = ARM_CP_NO_RAW,
8745                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8746                     .access = PL2_RW, .resetvalue = 0,
8747                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8748                 };
8749                 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
8750                 g_free(tmp_string);
8751 
8752                 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8753                 tmp_string = g_strdup_printf("HPRLAR%u", i);
8754                 ARMCPRegInfo tmp_hprlarn_reginfo = {
8755                     .name = tmp_string,
8756                     .type = ARM_CP_NO_RAW,
8757                     .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8758                     .access = PL2_RW, .resetvalue = 0,
8759                     .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8760                 };
8761                 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
8762                 g_free(tmp_string);
8763             }
8764         } else if (arm_feature(env, ARM_FEATURE_V7)) {
8765             define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8766         }
8767     }
8768 
8769     if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8770         ARMCPRegInfo mpidr_cp_reginfo[] = {
8771             { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8772               .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8773               .fgt = FGT_MPIDR_EL1,
8774               .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8775         };
8776 #ifdef CONFIG_USER_ONLY
8777         static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8778             { .name = "MPIDR_EL1",
8779               .fixed_bits = 0x0000000080000000 },
8780         };
8781         modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8782 #endif
8783         define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8784     }
8785 
8786     if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8787         ARMCPRegInfo auxcr_reginfo[] = {
8788             { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8789               .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8790               .access = PL1_RW, .accessfn = access_tacr,
8791               .nv2_redirect_offset = 0x118,
8792               .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8793             { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8794               .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8795               .access = PL2_RW, .type = ARM_CP_CONST,
8796               .resetvalue = 0 },
8797             { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8798               .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8799               .access = PL3_RW, .type = ARM_CP_CONST,
8800               .resetvalue = 0 },
8801         };
8802         define_arm_cp_regs(cpu, auxcr_reginfo);
8803         if (cpu_isar_feature(aa32_ac2, cpu)) {
8804             define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8805         }
8806     }
8807 
8808     if (arm_feature(env, ARM_FEATURE_CBAR)) {
8809         /*
8810          * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8811          * There are two flavours:
8812          *  (1) older 32-bit only cores have a simple 32-bit CBAR
8813          *  (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8814          *      32-bit register visible to AArch32 at a different encoding
8815          *      to the "flavour 1" register and with the bits rearranged to
8816          *      be able to squash a 64-bit address into the 32-bit view.
8817          * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8818          * in future if we support AArch32-only configs of some of the
8819          * AArch64 cores we might need to add a specific feature flag
8820          * to indicate cores with "flavour 2" CBAR.
8821          */
8822         if (arm_feature(env, ARM_FEATURE_V8)) {
8823             /* 32 bit view is [31:18] 0...0 [43:32]. */
8824             uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8825                 | extract64(cpu->reset_cbar, 32, 12);
8826             ARMCPRegInfo cbar_reginfo[] = {
8827                 { .name = "CBAR",
8828                   .type = ARM_CP_CONST,
8829                   .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8830                   .access = PL1_R, .resetvalue = cbar32 },
8831                 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8832                   .type = ARM_CP_CONST,
8833                   .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8834                   .access = PL1_R, .resetvalue = cpu->reset_cbar },
8835             };
8836             /* We don't implement a r/w 64 bit CBAR currently */
8837             assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8838             define_arm_cp_regs(cpu, cbar_reginfo);
8839         } else {
8840             ARMCPRegInfo cbar = {
8841                 .name = "CBAR",
8842                 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8843                 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
8844                 .fieldoffset = offsetof(CPUARMState,
8845                                         cp15.c15_config_base_address)
8846             };
8847             if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8848                 cbar.access = PL1_R;
8849                 cbar.fieldoffset = 0;
8850                 cbar.type = ARM_CP_CONST;
8851             }
8852             define_one_arm_cp_reg(cpu, &cbar);
8853         }
8854     }
8855 
8856     if (arm_feature(env, ARM_FEATURE_VBAR)) {
8857         static const ARMCPRegInfo vbar_cp_reginfo[] = {
8858             { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8859               .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8860               .access = PL1_RW, .writefn = vbar_write,
8861               .accessfn = access_nv1,
8862               .fgt = FGT_VBAR_EL1,
8863               .nv2_redirect_offset = 0x250 | NV2_REDIR_NV1,
8864               .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8865                                      offsetof(CPUARMState, cp15.vbar_ns) },
8866               .resetvalue = 0 },
8867         };
8868         define_arm_cp_regs(cpu, vbar_cp_reginfo);
8869     }
8870 
8871     /* Generic registers whose values depend on the implementation */
8872     {
8873         ARMCPRegInfo sctlr = {
8874             .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8875             .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8876             .access = PL1_RW, .accessfn = access_tvm_trvm,
8877             .fgt = FGT_SCTLR_EL1,
8878             .nv2_redirect_offset = 0x110 | NV2_REDIR_NV1,
8879             .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8880                                    offsetof(CPUARMState, cp15.sctlr_ns) },
8881             .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8882             .raw_writefn = raw_write,
8883         };
8884         if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8885             /*
8886              * Normally we would always end the TB on an SCTLR write, but Linux
8887              * arch/arm/mach-pxa/sleep.S expects two instructions following
8888              * an MMU enable to execute from cache.  Imitate this behaviour.
8889              */
8890             sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8891         }
8892         define_one_arm_cp_reg(cpu, &sctlr);
8893 
8894         if (arm_feature(env, ARM_FEATURE_PMSA) &&
8895             arm_feature(env, ARM_FEATURE_V8)) {
8896             ARMCPRegInfo vsctlr = {
8897                 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
8898                 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
8899                 .access = PL2_RW, .resetvalue = 0x0,
8900                 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
8901             };
8902             define_one_arm_cp_reg(cpu, &vsctlr);
8903         }
8904     }
8905 
8906     if (cpu_isar_feature(aa64_lor, cpu)) {
8907         define_arm_cp_regs(cpu, lor_reginfo);
8908     }
8909     if (cpu_isar_feature(aa64_pan, cpu)) {
8910         define_one_arm_cp_reg(cpu, &pan_reginfo);
8911     }
8912 #ifndef CONFIG_USER_ONLY
8913     if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8914         define_arm_cp_regs(cpu, ats1e1_reginfo);
8915     }
8916     if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8917         define_arm_cp_regs(cpu, ats1cp_reginfo);
8918     }
8919 #endif
8920     if (cpu_isar_feature(aa64_uao, cpu)) {
8921         define_one_arm_cp_reg(cpu, &uao_reginfo);
8922     }
8923 
8924     if (cpu_isar_feature(aa64_dit, cpu)) {
8925         define_one_arm_cp_reg(cpu, &dit_reginfo);
8926     }
8927     if (cpu_isar_feature(aa64_ssbs, cpu)) {
8928         define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8929     }
8930     if (cpu_isar_feature(any_ras, cpu)) {
8931         define_arm_cp_regs(cpu, minimal_ras_reginfo);
8932     }
8933 
8934     if (cpu_isar_feature(aa64_vh, cpu) ||
8935         cpu_isar_feature(aa64_debugv8p2, cpu)) {
8936         define_one_arm_cp_reg(cpu, &contextidr_el2);
8937     }
8938     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8939         define_arm_cp_regs(cpu, vhe_reginfo);
8940     }
8941 
8942     if (cpu_isar_feature(aa64_sve, cpu)) {
8943         define_arm_cp_regs(cpu, zcr_reginfo);
8944     }
8945 
8946     if (cpu_isar_feature(aa64_hcx, cpu)) {
8947         define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8948     }
8949 
8950     if (cpu_isar_feature(aa64_sme, cpu)) {
8951         define_arm_cp_regs(cpu, sme_reginfo);
8952     }
8953     if (cpu_isar_feature(aa64_pauth, cpu)) {
8954         define_arm_cp_regs(cpu, pauth_reginfo);
8955     }
8956     if (cpu_isar_feature(aa64_rndr, cpu)) {
8957         define_arm_cp_regs(cpu, rndr_reginfo);
8958     }
8959     /* Data Cache clean instructions up to PoP */
8960     if (cpu_isar_feature(aa64_dcpop, cpu)) {
8961         define_one_arm_cp_reg(cpu, dcpop_reg);
8962 
8963         if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8964             define_one_arm_cp_reg(cpu, dcpodp_reg);
8965         }
8966     }
8967 
8968     /*
8969      * If full MTE is enabled, add all of the system registers.
8970      * If only "instructions available at EL0" are enabled,
8971      * then define only a RAZ/WI version of PSTATE.TCO.
8972      */
8973     if (cpu_isar_feature(aa64_mte, cpu)) {
8974         ARMCPRegInfo gmid_reginfo = {
8975             .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
8976             .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
8977             .access = PL1_R, .accessfn = access_aa64_tid5,
8978             .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
8979         };
8980         define_one_arm_cp_reg(cpu, &gmid_reginfo);
8981         define_arm_cp_regs(cpu, mte_reginfo);
8982         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8983     } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8984         define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8985         define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8986     }
8987 
8988     if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8989         define_arm_cp_regs(cpu, scxtnum_reginfo);
8990     }
8991 
8992     if (cpu_isar_feature(aa64_fgt, cpu)) {
8993         define_arm_cp_regs(cpu, fgt_reginfo);
8994     }
8995 
8996     if (cpu_isar_feature(aa64_rme, cpu)) {
8997         define_arm_cp_regs(cpu, rme_reginfo);
8998         if (cpu_isar_feature(aa64_mte, cpu)) {
8999             define_arm_cp_regs(cpu, rme_mte_reginfo);
9000         }
9001     }
9002 
9003     if (cpu_isar_feature(aa64_nv2, cpu)) {
9004         define_arm_cp_regs(cpu, nv2_reginfo);
9005     }
9006 
9007     if (cpu_isar_feature(aa64_nmi, cpu)) {
9008         define_arm_cp_regs(cpu, nmi_reginfo);
9009     }
9010 
9011     if (cpu_isar_feature(any_predinv, cpu)) {
9012         define_arm_cp_regs(cpu, predinv_reginfo);
9013     }
9014 
9015     if (cpu_isar_feature(any_ccidx, cpu)) {
9016         define_arm_cp_regs(cpu, ccsidr2_reginfo);
9017     }
9018 
9019 #ifndef CONFIG_USER_ONLY
9020     /*
9021      * Register redirections and aliases must be done last,
9022      * after the registers from the other extensions have been defined.
9023      */
9024     if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9025         define_arm_vh_e2h_redirects_aliases(cpu);
9026     }
9027 #endif
9028 }
9029 
9030 /*
9031  * Private utility function for define_one_arm_cp_reg_with_opaque():
9032  * add a single reginfo struct to the hash table.
9033  */
add_cpreg_to_hashtable(ARMCPU * cpu,const ARMCPRegInfo * r,void * opaque,CPState state,CPSecureState secstate,int crm,int opc1,int opc2,const char * name)9034 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
9035                                    void *opaque, CPState state,
9036                                    CPSecureState secstate,
9037                                    int crm, int opc1, int opc2,
9038                                    const char *name)
9039 {
9040     CPUARMState *env = &cpu->env;
9041     uint32_t key;
9042     ARMCPRegInfo *r2;
9043     bool is64 = r->type & ARM_CP_64BIT;
9044     bool ns = secstate & ARM_CP_SECSTATE_NS;
9045     int cp = r->cp;
9046     size_t name_len;
9047     bool make_const;
9048 
9049     switch (state) {
9050     case ARM_CP_STATE_AA32:
9051         /* We assume it is a cp15 register if the .cp field is left unset. */
9052         if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9053             cp = 15;
9054         }
9055         key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9056         break;
9057     case ARM_CP_STATE_AA64:
9058         /*
9059          * To allow abbreviation of ARMCPRegInfo definitions, we treat
9060          * cp == 0 as equivalent to the value for "standard guest-visible
9061          * sysreg".  STATE_BOTH definitions are also always "standard sysreg"
9062          * in their AArch64 view (the .cp value may be non-zero for the
9063          * benefit of the AArch32 view).
9064          */
9065         if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9066             cp = CP_REG_ARM64_SYSREG_CP;
9067         }
9068         key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9069         break;
9070     default:
9071         g_assert_not_reached();
9072     }
9073 
9074     /* Overriding of an existing definition must be explicitly requested. */
9075     if (!(r->type & ARM_CP_OVERRIDE)) {
9076         const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9077         if (oldreg) {
9078             assert(oldreg->type & ARM_CP_OVERRIDE);
9079         }
9080     }
9081 
9082     /*
9083      * Eliminate registers that are not present because the EL is missing.
9084      * Doing this here makes it easier to put all registers for a given
9085      * feature into the same ARMCPRegInfo array and define them all at once.
9086      */
9087     make_const = false;
9088     if (arm_feature(env, ARM_FEATURE_EL3)) {
9089         /*
9090          * An EL2 register without EL2 but with EL3 is (usually) RES0.
9091          * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9092          */
9093         int min_el = ctz32(r->access) / 2;
9094         if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9095             if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9096                 return;
9097             }
9098             make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9099         }
9100     } else {
9101         CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9102                                  ? PL2_RW : PL1_RW);
9103         if ((r->access & max_el) == 0) {
9104             return;
9105         }
9106     }
9107 
9108     /* Combine cpreg and name into one allocation. */
9109     name_len = strlen(name) + 1;
9110     r2 = g_malloc(sizeof(*r2) + name_len);
9111     *r2 = *r;
9112     r2->name = memcpy(r2 + 1, name, name_len);
9113 
9114     /*
9115      * Update fields to match the instantiation, overwiting wildcards
9116      * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
9117      */
9118     r2->cp = cp;
9119     r2->crm = crm;
9120     r2->opc1 = opc1;
9121     r2->opc2 = opc2;
9122     r2->state = state;
9123     r2->secure = secstate;
9124     if (opaque) {
9125         r2->opaque = opaque;
9126     }
9127 
9128     if (make_const) {
9129         /* This should not have been a very special register to begin. */
9130         int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9131         assert(old_special == 0 || old_special == ARM_CP_NOP);
9132         /*
9133          * Set the special function to CONST, retaining the other flags.
9134          * This is important for e.g. ARM_CP_SVE so that we still
9135          * take the SVE trap if CPTR_EL3.EZ == 0.
9136          */
9137         r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9138         /*
9139          * Usually, these registers become RES0, but there are a few
9140          * special cases like VPIDR_EL2 which have a constant non-zero
9141          * value with writes ignored.
9142          */
9143         if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9144             r2->resetvalue = 0;
9145         }
9146         /*
9147          * ARM_CP_CONST has precedence, so removing the callbacks and
9148          * offsets are not strictly necessary, but it is potentially
9149          * less confusing to debug later.
9150          */
9151         r2->readfn = NULL;
9152         r2->writefn = NULL;
9153         r2->raw_readfn = NULL;
9154         r2->raw_writefn = NULL;
9155         r2->resetfn = NULL;
9156         r2->fieldoffset = 0;
9157         r2->bank_fieldoffsets[0] = 0;
9158         r2->bank_fieldoffsets[1] = 0;
9159     } else {
9160         bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
9161 
9162         if (isbanked) {
9163             /*
9164              * Register is banked (using both entries in array).
9165              * Overwriting fieldoffset as the array is only used to define
9166              * banked registers but later only fieldoffset is used.
9167              */
9168             r2->fieldoffset = r->bank_fieldoffsets[ns];
9169         }
9170         if (state == ARM_CP_STATE_AA32) {
9171             if (isbanked) {
9172                 /*
9173                  * If the register is banked then we don't need to migrate or
9174                  * reset the 32-bit instance in certain cases:
9175                  *
9176                  * 1) If the register has both 32-bit and 64-bit instances
9177                  *    then we can count on the 64-bit instance taking care
9178                  *    of the non-secure bank.
9179                  * 2) If ARMv8 is enabled then we can count on a 64-bit
9180                  *    version taking care of the secure bank.  This requires
9181                  *    that separate 32 and 64-bit definitions are provided.
9182                  */
9183                 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9184                     (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9185                     r2->type |= ARM_CP_ALIAS;
9186                 }
9187             } else if ((secstate != r->secure) && !ns) {
9188                 /*
9189                  * The register is not banked so we only want to allow
9190                  * migration of the non-secure instance.
9191                  */
9192                 r2->type |= ARM_CP_ALIAS;
9193             }
9194 
9195             if (HOST_BIG_ENDIAN &&
9196                 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9197                 r2->fieldoffset += sizeof(uint32_t);
9198             }
9199         }
9200     }
9201 
9202     /*
9203      * By convention, for wildcarded registers only the first
9204      * entry is used for migration; the others are marked as
9205      * ALIAS so we don't try to transfer the register
9206      * multiple times. Special registers (ie NOP/WFI) are
9207      * never migratable and not even raw-accessible.
9208      */
9209     if (r2->type & ARM_CP_SPECIAL_MASK) {
9210         r2->type |= ARM_CP_NO_RAW;
9211     }
9212     if (((r->crm == CP_ANY) && crm != 0) ||
9213         ((r->opc1 == CP_ANY) && opc1 != 0) ||
9214         ((r->opc2 == CP_ANY) && opc2 != 0)) {
9215         r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
9216     }
9217 
9218     /*
9219      * Check that raw accesses are either forbidden or handled. Note that
9220      * we can't assert this earlier because the setup of fieldoffset for
9221      * banked registers has to be done first.
9222      */
9223     if (!(r2->type & ARM_CP_NO_RAW)) {
9224         assert(!raw_accessors_invalid(r2));
9225     }
9226 
9227     g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
9228 }
9229 
9230 
define_one_arm_cp_reg_with_opaque(ARMCPU * cpu,const ARMCPRegInfo * r,void * opaque)9231 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9232                                        const ARMCPRegInfo *r, void *opaque)
9233 {
9234     /*
9235      * Define implementations of coprocessor registers.
9236      * We store these in a hashtable because typically
9237      * there are less than 150 registers in a space which
9238      * is 16*16*16*8*8 = 262144 in size.
9239      * Wildcarding is supported for the crm, opc1 and opc2 fields.
9240      * If a register is defined twice then the second definition is
9241      * used, so this can be used to define some generic registers and
9242      * then override them with implementation specific variations.
9243      * At least one of the original and the second definition should
9244      * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9245      * against accidental use.
9246      *
9247      * The state field defines whether the register is to be
9248      * visible in the AArch32 or AArch64 execution state. If the
9249      * state is set to ARM_CP_STATE_BOTH then we synthesise a
9250      * reginfo structure for the AArch32 view, which sees the lower
9251      * 32 bits of the 64 bit register.
9252      *
9253      * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9254      * be wildcarded. AArch64 registers are always considered to be 64
9255      * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9256      * the register, if any.
9257      */
9258     int crm, opc1, opc2;
9259     int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9260     int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9261     int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9262     int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9263     int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9264     int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
9265     CPState state;
9266 
9267     /* 64 bit registers have only CRm and Opc1 fields */
9268     assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
9269     /* op0 only exists in the AArch64 encodings */
9270     assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9271     /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9272     assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
9273     /*
9274      * This API is only for Arm's system coprocessors (14 and 15) or
9275      * (M-profile or v7A-and-earlier only) for implementation defined
9276      * coprocessors in the range 0..7.  Our decode assumes this, since
9277      * 8..13 can be used for other insns including VFP and Neon. See
9278      * valid_cp() in translate.c.  Assert here that we haven't tried
9279      * to use an invalid coprocessor number.
9280      */
9281     switch (r->state) {
9282     case ARM_CP_STATE_BOTH:
9283         /* 0 has a special meaning, but otherwise the same rules as AA32. */
9284         if (r->cp == 0) {
9285             break;
9286         }
9287         /* fall through */
9288     case ARM_CP_STATE_AA32:
9289         if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9290             !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9291             assert(r->cp >= 14 && r->cp <= 15);
9292         } else {
9293             assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9294         }
9295         break;
9296     case ARM_CP_STATE_AA64:
9297         assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9298         break;
9299     default:
9300         g_assert_not_reached();
9301     }
9302     /*
9303      * The AArch64 pseudocode CheckSystemAccess() specifies that op1
9304      * encodes a minimum access level for the register. We roll this
9305      * runtime check into our general permission check code, so check
9306      * here that the reginfo's specified permissions are strict enough
9307      * to encompass the generic architectural permission check.
9308      */
9309     if (r->state != ARM_CP_STATE_AA32) {
9310         CPAccessRights mask;
9311         switch (r->opc1) {
9312         case 0:
9313             /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9314             mask = PL0U_R | PL1_RW;
9315             break;
9316         case 1: case 2:
9317             /* min_EL EL1 */
9318             mask = PL1_RW;
9319             break;
9320         case 3:
9321             /* min_EL EL0 */
9322             mask = PL0_RW;
9323             break;
9324         case 4:
9325         case 5:
9326             /* min_EL EL2 */
9327             mask = PL2_RW;
9328             break;
9329         case 6:
9330             /* min_EL EL3 */
9331             mask = PL3_RW;
9332             break;
9333         case 7:
9334             /* min_EL EL1, secure mode only (we don't check the latter) */
9335             mask = PL1_RW;
9336             break;
9337         default:
9338             /* broken reginfo with out-of-range opc1 */
9339             g_assert_not_reached();
9340         }
9341         /* assert our permissions are not too lax (stricter is fine) */
9342         assert((r->access & ~mask) == 0);
9343     }
9344 
9345     /*
9346      * Check that the register definition has enough info to handle
9347      * reads and writes if they are permitted.
9348      */
9349     if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
9350         if (r->access & PL3_R) {
9351             assert((r->fieldoffset ||
9352                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9353                    r->readfn);
9354         }
9355         if (r->access & PL3_W) {
9356             assert((r->fieldoffset ||
9357                    (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9358                    r->writefn);
9359         }
9360     }
9361 
9362     for (crm = crmmin; crm <= crmmax; crm++) {
9363         for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9364             for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
9365                 for (state = ARM_CP_STATE_AA32;
9366                      state <= ARM_CP_STATE_AA64; state++) {
9367                     if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9368                         continue;
9369                     }
9370                     if ((r->type & ARM_CP_ADD_TLBI_NXS) &&
9371                         cpu_isar_feature(aa64_xs, cpu)) {
9372                         /*
9373                          * This is a TLBI insn which has an NXS variant. The
9374                          * NXS variant is at the same encoding except that
9375                          * crn is +1, and has the same behaviour except for
9376                          * fine-grained trapping. Add the NXS insn here and
9377                          * then fall through to add the normal register.
9378                          * add_cpreg_to_hashtable() copies the cpreg struct
9379                          * and name that it is passed, so it's OK to use
9380                          * a local struct here.
9381                          */
9382                         ARMCPRegInfo nxs_ri = *r;
9383                         g_autofree char *name = g_strdup_printf("%sNXS", r->name);
9384 
9385                         assert(state == ARM_CP_STATE_AA64);
9386                         assert(nxs_ri.crn < 0xf);
9387                         nxs_ri.crn++;
9388                         if (nxs_ri.fgt) {
9389                             nxs_ri.fgt |= R_FGT_NXS_MASK;
9390                         }
9391                         add_cpreg_to_hashtable(cpu, &nxs_ri, opaque, state,
9392                                                ARM_CP_SECSTATE_NS,
9393                                                crm, opc1, opc2, name);
9394                     }
9395                     if (state == ARM_CP_STATE_AA32) {
9396                         /*
9397                          * Under AArch32 CP registers can be common
9398                          * (same for secure and non-secure world) or banked.
9399                          */
9400                         char *name;
9401 
9402                         switch (r->secure) {
9403                         case ARM_CP_SECSTATE_S:
9404                         case ARM_CP_SECSTATE_NS:
9405                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9406                                                    r->secure, crm, opc1, opc2,
9407                                                    r->name);
9408                             break;
9409                         case ARM_CP_SECSTATE_BOTH:
9410                             name = g_strdup_printf("%s_S", r->name);
9411                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9412                                                    ARM_CP_SECSTATE_S,
9413                                                    crm, opc1, opc2, name);
9414                             g_free(name);
9415                             add_cpreg_to_hashtable(cpu, r, opaque, state,
9416                                                    ARM_CP_SECSTATE_NS,
9417                                                    crm, opc1, opc2, r->name);
9418                             break;
9419                         default:
9420                             g_assert_not_reached();
9421                         }
9422                     } else {
9423                         /*
9424                          * AArch64 registers get mapped to non-secure instance
9425                          * of AArch32
9426                          */
9427                         add_cpreg_to_hashtable(cpu, r, opaque, state,
9428                                                ARM_CP_SECSTATE_NS,
9429                                                crm, opc1, opc2, r->name);
9430                     }
9431                 }
9432             }
9433         }
9434     }
9435 }
9436 
9437 /* Define a whole list of registers */
define_arm_cp_regs_with_opaque_len(ARMCPU * cpu,const ARMCPRegInfo * regs,void * opaque,size_t len)9438 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9439                                         void *opaque, size_t len)
9440 {
9441     size_t i;
9442     for (i = 0; i < len; ++i) {
9443         define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
9444     }
9445 }
9446 
9447 /*
9448  * Modify ARMCPRegInfo for access from userspace.
9449  *
9450  * This is a data driven modification directed by
9451  * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9452  * user-space cannot alter any values and dynamic values pertaining to
9453  * execution state are hidden from user space view anyway.
9454  */
modify_arm_cp_regs_with_len(ARMCPRegInfo * regs,size_t regs_len,const ARMCPRegUserSpaceInfo * mods,size_t mods_len)9455 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9456                                  const ARMCPRegUserSpaceInfo *mods,
9457                                  size_t mods_len)
9458 {
9459     for (size_t mi = 0; mi < mods_len; ++mi) {
9460         const ARMCPRegUserSpaceInfo *m = mods + mi;
9461         GPatternSpec *pat = NULL;
9462 
9463         if (m->is_glob) {
9464             pat = g_pattern_spec_new(m->name);
9465         }
9466         for (size_t ri = 0; ri < regs_len; ++ri) {
9467             ARMCPRegInfo *r = regs + ri;
9468 
9469             if (pat && g_pattern_match_string(pat, r->name)) {
9470                 r->type = ARM_CP_CONST;
9471                 r->access = PL0U_R;
9472                 r->resetvalue = 0;
9473                 /* continue */
9474             } else if (strcmp(r->name, m->name) == 0) {
9475                 r->type = ARM_CP_CONST;
9476                 r->access = PL0U_R;
9477                 r->resetvalue &= m->exported_bits;
9478                 r->resetvalue |= m->fixed_bits;
9479                 break;
9480             }
9481         }
9482         if (pat) {
9483             g_pattern_spec_free(pat);
9484         }
9485     }
9486 }
9487 
get_arm_cp_reginfo(GHashTable * cpregs,uint32_t encoded_cp)9488 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
9489 {
9490     return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
9491 }
9492 
arm_cp_write_ignore(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)9493 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9494                          uint64_t value)
9495 {
9496     /* Helper coprocessor write function for write-ignore registers */
9497 }
9498 
arm_cp_read_zero(CPUARMState * env,const ARMCPRegInfo * ri)9499 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
9500 {
9501     /* Helper coprocessor write function for read-as-zero registers */
9502     return 0;
9503 }
9504 
arm_cp_reset_ignore(CPUARMState * env,const ARMCPRegInfo * opaque)9505 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9506 {
9507     /* Helper coprocessor reset function for do-nothing-on-reset registers */
9508 }
9509 
bad_mode_switch(CPUARMState * env,int mode,CPSRWriteType write_type)9510 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
9511 {
9512     /*
9513      * Return true if it is not valid for us to switch to
9514      * this CPU mode (ie all the UNPREDICTABLE cases in
9515      * the ARM ARM CPSRWriteByInstr pseudocode).
9516      */
9517 
9518     /* Changes to or from Hyp via MSR and CPS are illegal. */
9519     if (write_type == CPSRWriteByInstr &&
9520         ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9521          mode == ARM_CPU_MODE_HYP)) {
9522         return 1;
9523     }
9524 
9525     switch (mode) {
9526     case ARM_CPU_MODE_USR:
9527         return 0;
9528     case ARM_CPU_MODE_SYS:
9529     case ARM_CPU_MODE_SVC:
9530     case ARM_CPU_MODE_ABT:
9531     case ARM_CPU_MODE_UND:
9532     case ARM_CPU_MODE_IRQ:
9533     case ARM_CPU_MODE_FIQ:
9534         /*
9535          * Note that we don't implement the IMPDEF NSACR.RFR which in v7
9536          * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9537          */
9538         /*
9539          * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
9540          * and CPS are treated as illegal mode changes.
9541          */
9542         if (write_type == CPSRWriteByInstr &&
9543             (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
9544             (arm_hcr_el2_eff(env) & HCR_TGE)) {
9545             return 1;
9546         }
9547         return 0;
9548     case ARM_CPU_MODE_HYP:
9549         return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
9550     case ARM_CPU_MODE_MON:
9551         return arm_current_el(env) < 3;
9552     default:
9553         return 1;
9554     }
9555 }
9556 
cpsr_read(CPUARMState * env)9557 uint32_t cpsr_read(CPUARMState *env)
9558 {
9559     int ZF;
9560     ZF = (env->ZF == 0);
9561     return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
9562         (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9563         | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9564         | ((env->condexec_bits & 0xfc) << 8)
9565         | (env->GE << 16) | (env->daif & CPSR_AIF);
9566 }
9567 
cpsr_write(CPUARMState * env,uint32_t val,uint32_t mask,CPSRWriteType write_type)9568 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9569                 CPSRWriteType write_type)
9570 {
9571     uint32_t changed_daif;
9572     bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9573         (mask & (CPSR_M | CPSR_E | CPSR_IL));
9574 
9575     if (mask & CPSR_NZCV) {
9576         env->ZF = (~val) & CPSR_Z;
9577         env->NF = val;
9578         env->CF = (val >> 29) & 1;
9579         env->VF = (val << 3) & 0x80000000;
9580     }
9581     if (mask & CPSR_Q) {
9582         env->QF = ((val & CPSR_Q) != 0);
9583     }
9584     if (mask & CPSR_T) {
9585         env->thumb = ((val & CPSR_T) != 0);
9586     }
9587     if (mask & CPSR_IT_0_1) {
9588         env->condexec_bits &= ~3;
9589         env->condexec_bits |= (val >> 25) & 3;
9590     }
9591     if (mask & CPSR_IT_2_7) {
9592         env->condexec_bits &= 3;
9593         env->condexec_bits |= (val >> 8) & 0xfc;
9594     }
9595     if (mask & CPSR_GE) {
9596         env->GE = (val >> 16) & 0xf;
9597     }
9598 
9599     /*
9600      * In a V7 implementation that includes the security extensions but does
9601      * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9602      * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9603      * bits respectively.
9604      *
9605      * In a V8 implementation, it is permitted for privileged software to
9606      * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9607      */
9608     if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
9609         arm_feature(env, ARM_FEATURE_EL3) &&
9610         !arm_feature(env, ARM_FEATURE_EL2) &&
9611         !arm_is_secure(env)) {
9612 
9613         changed_daif = (env->daif ^ val) & mask;
9614 
9615         if (changed_daif & CPSR_A) {
9616             /*
9617              * Check to see if we are allowed to change the masking of async
9618              * abort exceptions from a non-secure state.
9619              */
9620             if (!(env->cp15.scr_el3 & SCR_AW)) {
9621                 qemu_log_mask(LOG_GUEST_ERROR,
9622                               "Ignoring attempt to switch CPSR_A flag from "
9623                               "non-secure world with SCR.AW bit clear\n");
9624                 mask &= ~CPSR_A;
9625             }
9626         }
9627 
9628         if (changed_daif & CPSR_F) {
9629             /*
9630              * Check to see if we are allowed to change the masking of FIQ
9631              * exceptions from a non-secure state.
9632              */
9633             if (!(env->cp15.scr_el3 & SCR_FW)) {
9634                 qemu_log_mask(LOG_GUEST_ERROR,
9635                               "Ignoring attempt to switch CPSR_F flag from "
9636                               "non-secure world with SCR.FW bit clear\n");
9637                 mask &= ~CPSR_F;
9638             }
9639 
9640             /*
9641              * Check whether non-maskable FIQ (NMFI) support is enabled.
9642              * If this bit is set software is not allowed to mask
9643              * FIQs, but is allowed to set CPSR_F to 0.
9644              */
9645             if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9646                 (val & CPSR_F)) {
9647                 qemu_log_mask(LOG_GUEST_ERROR,
9648                               "Ignoring attempt to enable CPSR_F flag "
9649                               "(non-maskable FIQ [NMFI] support enabled)\n");
9650                 mask &= ~CPSR_F;
9651             }
9652         }
9653     }
9654 
9655     env->daif &= ~(CPSR_AIF & mask);
9656     env->daif |= val & CPSR_AIF & mask;
9657 
9658     if (write_type != CPSRWriteRaw &&
9659         ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
9660         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9661             /*
9662              * Note that we can only get here in USR mode if this is a
9663              * gdb stub write; for this case we follow the architectural
9664              * behaviour for guest writes in USR mode of ignoring an attempt
9665              * to switch mode. (Those are caught by translate.c for writes
9666              * triggered by guest instructions.)
9667              */
9668             mask &= ~CPSR_M;
9669         } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9670             /*
9671              * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
9672              * v7, and has defined behaviour in v8:
9673              *  + leave CPSR.M untouched
9674              *  + allow changes to the other CPSR fields
9675              *  + set PSTATE.IL
9676              * For user changes via the GDB stub, we don't set PSTATE.IL,
9677              * as this would be unnecessarily harsh for a user error.
9678              */
9679             mask &= ~CPSR_M;
9680             if (write_type != CPSRWriteByGDBStub &&
9681                 arm_feature(env, ARM_FEATURE_V8)) {
9682                 mask |= CPSR_IL;
9683                 val |= CPSR_IL;
9684             }
9685             qemu_log_mask(LOG_GUEST_ERROR,
9686                           "Illegal AArch32 mode switch attempt from %s to %s\n",
9687                           aarch32_mode_name(env->uncached_cpsr),
9688                           aarch32_mode_name(val));
9689         } else {
9690             qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9691                           write_type == CPSRWriteExceptionReturn ?
9692                           "Exception return from AArch32" :
9693                           "AArch32 mode switch from",
9694                           aarch32_mode_name(env->uncached_cpsr),
9695                           aarch32_mode_name(val), env->regs[15]);
9696             switch_mode(env, val & CPSR_M);
9697         }
9698     }
9699     mask &= ~CACHED_CPSR_BITS;
9700     env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
9701     if (tcg_enabled() && rebuild_hflags) {
9702         arm_rebuild_hflags(env);
9703     }
9704 }
9705 
9706 #ifdef CONFIG_USER_ONLY
9707 
switch_mode(CPUARMState * env,int mode)9708 static void switch_mode(CPUARMState *env, int mode)
9709 {
9710     ARMCPU *cpu = env_archcpu(env);
9711 
9712     if (mode != ARM_CPU_MODE_USR) {
9713         cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9714     }
9715 }
9716 
arm_phys_excp_target_el(CPUState * cs,uint32_t excp_idx,uint32_t cur_el,bool secure)9717 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9718                                  uint32_t cur_el, bool secure)
9719 {
9720     return 1;
9721 }
9722 
aarch64_sync_64_to_32(CPUARMState * env)9723 void aarch64_sync_64_to_32(CPUARMState *env)
9724 {
9725     g_assert_not_reached();
9726 }
9727 
9728 #else
9729 
switch_mode(CPUARMState * env,int mode)9730 static void switch_mode(CPUARMState *env, int mode)
9731 {
9732     int old_mode;
9733     int i;
9734 
9735     old_mode = env->uncached_cpsr & CPSR_M;
9736     if (mode == old_mode) {
9737         return;
9738     }
9739 
9740     if (old_mode == ARM_CPU_MODE_FIQ) {
9741         memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9742         memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
9743     } else if (mode == ARM_CPU_MODE_FIQ) {
9744         memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9745         memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
9746     }
9747 
9748     i = bank_number(old_mode);
9749     env->banked_r13[i] = env->regs[13];
9750     env->banked_spsr[i] = env->spsr;
9751 
9752     i = bank_number(mode);
9753     env->regs[13] = env->banked_r13[i];
9754     env->spsr = env->banked_spsr[i];
9755 
9756     env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9757     env->regs[14] = env->banked_r14[r14_bank_number(mode)];
9758 }
9759 
9760 /*
9761  * Physical Interrupt Target EL Lookup Table
9762  *
9763  * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9764  *
9765  * The below multi-dimensional table is used for looking up the target
9766  * exception level given numerous condition criteria.  Specifically, the
9767  * target EL is based on SCR and HCR routing controls as well as the
9768  * currently executing EL and secure state.
9769  *
9770  *    Dimensions:
9771  *    target_el_table[2][2][2][2][2][4]
9772  *                    |  |  |  |  |  +--- Current EL
9773  *                    |  |  |  |  +------ Non-secure(0)/Secure(1)
9774  *                    |  |  |  +--------- HCR mask override
9775  *                    |  |  +------------ SCR exec state control
9776  *                    |  +--------------- SCR mask override
9777  *                    +------------------ 32-bit(0)/64-bit(1) EL3
9778  *
9779  *    The table values are as such:
9780  *    0-3 = EL0-EL3
9781  *     -1 = Cannot occur
9782  *
9783  * The ARM ARM target EL table includes entries indicating that an "exception
9784  * is not taken".  The two cases where this is applicable are:
9785  *    1) An exception is taken from EL3 but the SCR does not have the exception
9786  *    routed to EL3.
9787  *    2) An exception is taken from EL2 but the HCR does not have the exception
9788  *    routed to EL2.
9789  * In these two cases, the below table contain a target of EL1.  This value is
9790  * returned as it is expected that the consumer of the table data will check
9791  * for "target EL >= current EL" to ensure the exception is not taken.
9792  *
9793  *            SCR     HCR
9794  *         64  EA     AMO                 From
9795  *        BIT IRQ     IMO      Non-secure         Secure
9796  *        EL3 FIQ  RW FMO   EL0 EL1 EL2 EL3   EL0 EL1 EL2 EL3
9797  */
9798 static const int8_t target_el_table[2][2][2][2][2][4] = {
9799     {{{{/* 0   0   0   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9800        {/* 0   0   0   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},
9801       {{/* 0   0   1   0 */{ 1,  1,  2, -1 },{ 3, -1, -1,  3 },},
9802        {/* 0   0   1   1 */{ 2,  2,  2, -1 },{ 3, -1, -1,  3 },},},},
9803      {{{/* 0   1   0   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9804        {/* 0   1   0   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},
9805       {{/* 0   1   1   0 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},
9806        {/* 0   1   1   1 */{ 3,  3,  3, -1 },{ 3, -1, -1,  3 },},},},},
9807     {{{{/* 1   0   0   0 */{ 1,  1,  2, -1 },{ 1,  1, -1,  1 },},
9808        {/* 1   0   0   1 */{ 2,  2,  2, -1 },{ 2,  2, -1,  1 },},},
9809       {{/* 1   0   1   0 */{ 1,  1,  1, -1 },{ 1,  1,  1,  1 },},
9810        {/* 1   0   1   1 */{ 2,  2,  2, -1 },{ 2,  2,  2,  1 },},},},
9811      {{{/* 1   1   0   0 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},
9812        {/* 1   1   0   1 */{ 3,  3,  3, -1 },{ 3,  3, -1,  3 },},},
9813       {{/* 1   1   1   0 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},
9814        {/* 1   1   1   1 */{ 3,  3,  3, -1 },{ 3,  3,  3,  3 },},},},},
9815 };
9816 
9817 /*
9818  * Determine the target EL for physical exceptions
9819  */
arm_phys_excp_target_el(CPUState * cs,uint32_t excp_idx,uint32_t cur_el,bool secure)9820 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9821                                  uint32_t cur_el, bool secure)
9822 {
9823     CPUARMState *env = cpu_env(cs);
9824     bool rw;
9825     bool scr;
9826     bool hcr;
9827     int target_el;
9828     /* Is the highest EL AArch64? */
9829     bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
9830     uint64_t hcr_el2;
9831 
9832     if (arm_feature(env, ARM_FEATURE_EL3)) {
9833         rw = arm_scr_rw_eff(env);
9834     } else {
9835         /*
9836          * Either EL2 is the highest EL (and so the EL2 register width
9837          * is given by is64); or there is no EL2 or EL3, in which case
9838          * the value of 'rw' does not affect the table lookup anyway.
9839          */
9840         rw = is64;
9841     }
9842 
9843     hcr_el2 = arm_hcr_el2_eff(env);
9844     switch (excp_idx) {
9845     case EXCP_IRQ:
9846     case EXCP_NMI:
9847         scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
9848         hcr = hcr_el2 & HCR_IMO;
9849         break;
9850     case EXCP_FIQ:
9851         scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
9852         hcr = hcr_el2 & HCR_FMO;
9853         break;
9854     default:
9855         scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
9856         hcr = hcr_el2 & HCR_AMO;
9857         break;
9858     };
9859 
9860     /*
9861      * For these purposes, TGE and AMO/IMO/FMO both force the
9862      * interrupt to EL2.  Fold TGE into the bit extracted above.
9863      */
9864     hcr |= (hcr_el2 & HCR_TGE) != 0;
9865 
9866     /* Perform a table-lookup for the target EL given the current state */
9867     target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
9868 
9869     assert(target_el > 0);
9870 
9871     return target_el;
9872 }
9873 
arm_log_exception(CPUState * cs)9874 void arm_log_exception(CPUState *cs)
9875 {
9876     int idx = cs->exception_index;
9877 
9878     if (qemu_loglevel_mask(CPU_LOG_INT)) {
9879         const char *exc = NULL;
9880         static const char * const excnames[] = {
9881             [EXCP_UDEF] = "Undefined Instruction",
9882             [EXCP_SWI] = "SVC",
9883             [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
9884             [EXCP_DATA_ABORT] = "Data Abort",
9885             [EXCP_IRQ] = "IRQ",
9886             [EXCP_FIQ] = "FIQ",
9887             [EXCP_BKPT] = "Breakpoint",
9888             [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
9889             [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
9890             [EXCP_HVC] = "Hypervisor Call",
9891             [EXCP_HYP_TRAP] = "Hypervisor Trap",
9892             [EXCP_SMC] = "Secure Monitor Call",
9893             [EXCP_VIRQ] = "Virtual IRQ",
9894             [EXCP_VFIQ] = "Virtual FIQ",
9895             [EXCP_SEMIHOST] = "Semihosting call",
9896             [EXCP_NOCP] = "v7M NOCP UsageFault",
9897             [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
9898             [EXCP_STKOF] = "v8M STKOF UsageFault",
9899             [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
9900             [EXCP_LSERR] = "v8M LSERR UsageFault",
9901             [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
9902             [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
9903             [EXCP_VSERR] = "Virtual SERR",
9904             [EXCP_GPC] = "Granule Protection Check",
9905             [EXCP_NMI] = "NMI",
9906             [EXCP_VINMI] = "Virtual IRQ NMI",
9907             [EXCP_VFNMI] = "Virtual FIQ NMI",
9908             [EXCP_MON_TRAP] = "Monitor Trap",
9909         };
9910 
9911         if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
9912             exc = excnames[idx];
9913         }
9914         if (!exc) {
9915             exc = "unknown";
9916         }
9917         qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
9918                       idx, exc, cs->cpu_index);
9919     }
9920 }
9921 
9922 /*
9923  * Function used to synchronize QEMU's AArch64 register set with AArch32
9924  * register set.  This is necessary when switching between AArch32 and AArch64
9925  * execution state.
9926  */
aarch64_sync_32_to_64(CPUARMState * env)9927 void aarch64_sync_32_to_64(CPUARMState *env)
9928 {
9929     int i;
9930     uint32_t mode = env->uncached_cpsr & CPSR_M;
9931 
9932     /* We can blanket copy R[0:7] to X[0:7] */
9933     for (i = 0; i < 8; i++) {
9934         env->xregs[i] = env->regs[i];
9935     }
9936 
9937     /*
9938      * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
9939      * Otherwise, they come from the banked user regs.
9940      */
9941     if (mode == ARM_CPU_MODE_FIQ) {
9942         for (i = 8; i < 13; i++) {
9943             env->xregs[i] = env->usr_regs[i - 8];
9944         }
9945     } else {
9946         for (i = 8; i < 13; i++) {
9947             env->xregs[i] = env->regs[i];
9948         }
9949     }
9950 
9951     /*
9952      * Registers x13-x23 are the various mode SP and FP registers. Registers
9953      * r13 and r14 are only copied if we are in that mode, otherwise we copy
9954      * from the mode banked register.
9955      */
9956     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
9957         env->xregs[13] = env->regs[13];
9958         env->xregs[14] = env->regs[14];
9959     } else {
9960         env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
9961         /* HYP is an exception in that it is copied from r14 */
9962         if (mode == ARM_CPU_MODE_HYP) {
9963             env->xregs[14] = env->regs[14];
9964         } else {
9965             env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
9966         }
9967     }
9968 
9969     if (mode == ARM_CPU_MODE_HYP) {
9970         env->xregs[15] = env->regs[13];
9971     } else {
9972         env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
9973     }
9974 
9975     if (mode == ARM_CPU_MODE_IRQ) {
9976         env->xregs[16] = env->regs[14];
9977         env->xregs[17] = env->regs[13];
9978     } else {
9979         env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
9980         env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
9981     }
9982 
9983     if (mode == ARM_CPU_MODE_SVC) {
9984         env->xregs[18] = env->regs[14];
9985         env->xregs[19] = env->regs[13];
9986     } else {
9987         env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
9988         env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
9989     }
9990 
9991     if (mode == ARM_CPU_MODE_ABT) {
9992         env->xregs[20] = env->regs[14];
9993         env->xregs[21] = env->regs[13];
9994     } else {
9995         env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
9996         env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
9997     }
9998 
9999     if (mode == ARM_CPU_MODE_UND) {
10000         env->xregs[22] = env->regs[14];
10001         env->xregs[23] = env->regs[13];
10002     } else {
10003         env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10004         env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
10005     }
10006 
10007     /*
10008      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10009      * mode, then we can copy from r8-r14.  Otherwise, we copy from the
10010      * FIQ bank for r8-r14.
10011      */
10012     if (mode == ARM_CPU_MODE_FIQ) {
10013         for (i = 24; i < 31; i++) {
10014             env->xregs[i] = env->regs[i - 16];   /* X[24:30] <- R[8:14] */
10015         }
10016     } else {
10017         for (i = 24; i < 29; i++) {
10018             env->xregs[i] = env->fiq_regs[i - 24];
10019         }
10020         env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10021         env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
10022     }
10023 
10024     env->pc = env->regs[15];
10025 }
10026 
10027 /*
10028  * Function used to synchronize QEMU's AArch32 register set with AArch64
10029  * register set.  This is necessary when switching between AArch32 and AArch64
10030  * execution state.
10031  */
aarch64_sync_64_to_32(CPUARMState * env)10032 void aarch64_sync_64_to_32(CPUARMState *env)
10033 {
10034     int i;
10035     uint32_t mode = env->uncached_cpsr & CPSR_M;
10036 
10037     /* We can blanket copy X[0:7] to R[0:7] */
10038     for (i = 0; i < 8; i++) {
10039         env->regs[i] = env->xregs[i];
10040     }
10041 
10042     /*
10043      * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10044      * Otherwise, we copy x8-x12 into the banked user regs.
10045      */
10046     if (mode == ARM_CPU_MODE_FIQ) {
10047         for (i = 8; i < 13; i++) {
10048             env->usr_regs[i - 8] = env->xregs[i];
10049         }
10050     } else {
10051         for (i = 8; i < 13; i++) {
10052             env->regs[i] = env->xregs[i];
10053         }
10054     }
10055 
10056     /*
10057      * Registers r13 & r14 depend on the current mode.
10058      * If we are in a given mode, we copy the corresponding x registers to r13
10059      * and r14.  Otherwise, we copy the x register to the banked r13 and r14
10060      * for the mode.
10061      */
10062     if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10063         env->regs[13] = env->xregs[13];
10064         env->regs[14] = env->xregs[14];
10065     } else {
10066         env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
10067 
10068         /*
10069          * HYP is an exception in that it does not have its own banked r14 but
10070          * shares the USR r14
10071          */
10072         if (mode == ARM_CPU_MODE_HYP) {
10073             env->regs[14] = env->xregs[14];
10074         } else {
10075             env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10076         }
10077     }
10078 
10079     if (mode == ARM_CPU_MODE_HYP) {
10080         env->regs[13] = env->xregs[15];
10081     } else {
10082         env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
10083     }
10084 
10085     if (mode == ARM_CPU_MODE_IRQ) {
10086         env->regs[14] = env->xregs[16];
10087         env->regs[13] = env->xregs[17];
10088     } else {
10089         env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10090         env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
10091     }
10092 
10093     if (mode == ARM_CPU_MODE_SVC) {
10094         env->regs[14] = env->xregs[18];
10095         env->regs[13] = env->xregs[19];
10096     } else {
10097         env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10098         env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
10099     }
10100 
10101     if (mode == ARM_CPU_MODE_ABT) {
10102         env->regs[14] = env->xregs[20];
10103         env->regs[13] = env->xregs[21];
10104     } else {
10105         env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10106         env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
10107     }
10108 
10109     if (mode == ARM_CPU_MODE_UND) {
10110         env->regs[14] = env->xregs[22];
10111         env->regs[13] = env->xregs[23];
10112     } else {
10113         env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
10114         env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
10115     }
10116 
10117     /*
10118      * Registers x24-x30 are mapped to r8-r14 in FIQ mode.  If we are in FIQ
10119      * mode, then we can copy to r8-r14.  Otherwise, we copy to the
10120      * FIQ bank for r8-r14.
10121      */
10122     if (mode == ARM_CPU_MODE_FIQ) {
10123         for (i = 24; i < 31; i++) {
10124             env->regs[i - 16] = env->xregs[i];   /* X[24:30] -> R[8:14] */
10125         }
10126     } else {
10127         for (i = 24; i < 29; i++) {
10128             env->fiq_regs[i - 24] = env->xregs[i];
10129         }
10130         env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
10131         env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
10132     }
10133 
10134     env->regs[15] = env->pc;
10135 }
10136 
take_aarch32_exception(CPUARMState * env,int new_mode,uint32_t mask,uint32_t offset,uint32_t newpc)10137 static void take_aarch32_exception(CPUARMState *env, int new_mode,
10138                                    uint32_t mask, uint32_t offset,
10139                                    uint32_t newpc)
10140 {
10141     int new_el;
10142 
10143     /* Change the CPU state so as to actually take the exception. */
10144     switch_mode(env, new_mode);
10145 
10146     /*
10147      * For exceptions taken to AArch32 we must clear the SS bit in both
10148      * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10149      */
10150     env->pstate &= ~PSTATE_SS;
10151     env->spsr = cpsr_read(env);
10152     /* Clear IT bits.  */
10153     env->condexec_bits = 0;
10154     /* Switch to the new mode, and to the correct instruction set.  */
10155     env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
10156 
10157     /* This must be after mode switching. */
10158     new_el = arm_current_el(env);
10159 
10160     /* Set new mode endianness */
10161     env->uncached_cpsr &= ~CPSR_E;
10162     if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
10163         env->uncached_cpsr |= CPSR_E;
10164     }
10165     /* J and IL must always be cleared for exception entry */
10166     env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
10167     env->daif |= mask;
10168 
10169     if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10170         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10171             env->uncached_cpsr |= CPSR_SSBS;
10172         } else {
10173             env->uncached_cpsr &= ~CPSR_SSBS;
10174         }
10175     }
10176 
10177     if (new_mode == ARM_CPU_MODE_HYP) {
10178         env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10179         env->elr_el[2] = env->regs[15];
10180     } else {
10181         /* CPSR.PAN is normally preserved preserved unless...  */
10182         if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
10183             switch (new_el) {
10184             case 3:
10185                 if (!arm_is_secure_below_el3(env)) {
10186                     /* ... the target is EL3, from non-secure state.  */
10187                     env->uncached_cpsr &= ~CPSR_PAN;
10188                     break;
10189                 }
10190                 /* ... the target is EL3, from secure state ... */
10191                 /* fall through */
10192             case 1:
10193                 /* ... the target is EL1 and SCTLR.SPAN is 0.  */
10194                 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10195                     env->uncached_cpsr |= CPSR_PAN;
10196                 }
10197                 break;
10198             }
10199         }
10200         /*
10201          * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10202          * and we should just guard the thumb mode on V4
10203          */
10204         if (arm_feature(env, ARM_FEATURE_V4T)) {
10205             env->thumb =
10206                 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10207         }
10208         env->regs[14] = env->regs[15] + offset;
10209     }
10210     env->regs[15] = newpc;
10211 
10212     if (tcg_enabled()) {
10213         arm_rebuild_hflags(env);
10214     }
10215 }
10216 
arm_cpu_do_interrupt_aarch32_hyp(CPUState * cs)10217 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10218 {
10219     /*
10220      * Handle exception entry to Hyp mode; this is sufficiently
10221      * different to entry to other AArch32 modes that we handle it
10222      * separately here.
10223      *
10224      * The vector table entry used is always the 0x14 Hyp mode entry point,
10225      * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
10226      * The offset applied to the preferred return address is always zero
10227      * (see DDI0487C.a section G1.12.3).
10228      * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10229      */
10230     uint32_t addr, mask;
10231     ARMCPU *cpu = ARM_CPU(cs);
10232     CPUARMState *env = &cpu->env;
10233 
10234     switch (cs->exception_index) {
10235     case EXCP_UDEF:
10236         addr = 0x04;
10237         break;
10238     case EXCP_SWI:
10239         addr = 0x08;
10240         break;
10241     case EXCP_BKPT:
10242         /* Fall through to prefetch abort.  */
10243     case EXCP_PREFETCH_ABORT:
10244         env->cp15.ifar_s = env->exception.vaddress;
10245         qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10246                       (uint32_t)env->exception.vaddress);
10247         addr = 0x0c;
10248         break;
10249     case EXCP_DATA_ABORT:
10250         env->cp15.dfar_s = env->exception.vaddress;
10251         qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10252                       (uint32_t)env->exception.vaddress);
10253         addr = 0x10;
10254         break;
10255     case EXCP_IRQ:
10256         addr = 0x18;
10257         break;
10258     case EXCP_FIQ:
10259         addr = 0x1c;
10260         break;
10261     case EXCP_HVC:
10262         addr = 0x08;
10263         break;
10264     case EXCP_HYP_TRAP:
10265         addr = 0x14;
10266         break;
10267     default:
10268         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10269     }
10270 
10271     if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
10272         if (!arm_feature(env, ARM_FEATURE_V8)) {
10273             /*
10274              * QEMU syndrome values are v8-style. v7 has the IL bit
10275              * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10276              * If this is a v7 CPU, squash the IL bit in those cases.
10277              */
10278             if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10279                 (cs->exception_index == EXCP_DATA_ABORT &&
10280                  !(env->exception.syndrome & ARM_EL_ISV)) ||
10281                 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10282                 env->exception.syndrome &= ~ARM_EL_IL;
10283             }
10284         }
10285         env->cp15.esr_el[2] = env->exception.syndrome;
10286     }
10287 
10288     if (arm_current_el(env) != 2 && addr < 0x14) {
10289         addr = 0x14;
10290     }
10291 
10292     mask = 0;
10293     if (!(env->cp15.scr_el3 & SCR_EA)) {
10294         mask |= CPSR_A;
10295     }
10296     if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10297         mask |= CPSR_I;
10298     }
10299     if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10300         mask |= CPSR_F;
10301     }
10302 
10303     addr += env->cp15.hvbar;
10304 
10305     take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10306 }
10307 
arm_cpu_do_interrupt_aarch32(CPUState * cs)10308 static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
10309 {
10310     ARMCPU *cpu = ARM_CPU(cs);
10311     CPUARMState *env = &cpu->env;
10312     uint32_t addr;
10313     uint32_t mask;
10314     int new_mode;
10315     uint32_t offset;
10316     uint32_t moe;
10317 
10318     /* If this is a debug exception we must update the DBGDSCR.MOE bits */
10319     switch (syn_get_ec(env->exception.syndrome)) {
10320     case EC_BREAKPOINT:
10321     case EC_BREAKPOINT_SAME_EL:
10322         moe = 1;
10323         break;
10324     case EC_WATCHPOINT:
10325     case EC_WATCHPOINT_SAME_EL:
10326         moe = 10;
10327         break;
10328     case EC_AA32_BKPT:
10329         moe = 3;
10330         break;
10331     case EC_VECTORCATCH:
10332         moe = 5;
10333         break;
10334     default:
10335         moe = 0;
10336         break;
10337     }
10338 
10339     if (moe) {
10340         env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10341     }
10342 
10343     if (env->exception.target_el == 2) {
10344         /* Debug exceptions are reported differently on AArch32 */
10345         switch (syn_get_ec(env->exception.syndrome)) {
10346         case EC_BREAKPOINT:
10347         case EC_BREAKPOINT_SAME_EL:
10348         case EC_AA32_BKPT:
10349         case EC_VECTORCATCH:
10350             env->exception.syndrome = syn_insn_abort(arm_current_el(env) == 2,
10351                                                      0, 0, 0x22);
10352             break;
10353         case EC_WATCHPOINT:
10354             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
10355                                                  EC_DATAABORT);
10356             break;
10357         case EC_WATCHPOINT_SAME_EL:
10358             env->exception.syndrome = syn_set_ec(env->exception.syndrome,
10359                                                  EC_DATAABORT_SAME_EL);
10360             break;
10361         }
10362         arm_cpu_do_interrupt_aarch32_hyp(cs);
10363         return;
10364     }
10365 
10366     switch (cs->exception_index) {
10367     case EXCP_UDEF:
10368         new_mode = ARM_CPU_MODE_UND;
10369         addr = 0x04;
10370         mask = CPSR_I;
10371         if (env->thumb) {
10372             offset = 2;
10373         } else {
10374             offset = 4;
10375         }
10376         break;
10377     case EXCP_SWI:
10378         new_mode = ARM_CPU_MODE_SVC;
10379         addr = 0x08;
10380         mask = CPSR_I;
10381         /* The PC already points to the next instruction.  */
10382         offset = 0;
10383         break;
10384     case EXCP_BKPT:
10385         /* Fall through to prefetch abort.  */
10386     case EXCP_PREFETCH_ABORT:
10387         A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
10388         A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
10389         qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
10390                       env->exception.fsr, (uint32_t)env->exception.vaddress);
10391         new_mode = ARM_CPU_MODE_ABT;
10392         addr = 0x0c;
10393         mask = CPSR_A | CPSR_I;
10394         offset = 4;
10395         break;
10396     case EXCP_DATA_ABORT:
10397         A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10398         A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
10399         qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
10400                       env->exception.fsr,
10401                       (uint32_t)env->exception.vaddress);
10402         new_mode = ARM_CPU_MODE_ABT;
10403         addr = 0x10;
10404         mask = CPSR_A | CPSR_I;
10405         offset = 8;
10406         break;
10407     case EXCP_IRQ:
10408         new_mode = ARM_CPU_MODE_IRQ;
10409         addr = 0x18;
10410         /* Disable IRQ and imprecise data aborts.  */
10411         mask = CPSR_A | CPSR_I;
10412         offset = 4;
10413         if (env->cp15.scr_el3 & SCR_IRQ) {
10414             /* IRQ routed to monitor mode */
10415             new_mode = ARM_CPU_MODE_MON;
10416             mask |= CPSR_F;
10417         }
10418         break;
10419     case EXCP_FIQ:
10420         new_mode = ARM_CPU_MODE_FIQ;
10421         addr = 0x1c;
10422         /* Disable FIQ, IRQ and imprecise data aborts.  */
10423         mask = CPSR_A | CPSR_I | CPSR_F;
10424         if (env->cp15.scr_el3 & SCR_FIQ) {
10425             /* FIQ routed to monitor mode */
10426             new_mode = ARM_CPU_MODE_MON;
10427         }
10428         offset = 4;
10429         break;
10430     case EXCP_VIRQ:
10431         new_mode = ARM_CPU_MODE_IRQ;
10432         addr = 0x18;
10433         /* Disable IRQ and imprecise data aborts.  */
10434         mask = CPSR_A | CPSR_I;
10435         offset = 4;
10436         break;
10437     case EXCP_VFIQ:
10438         new_mode = ARM_CPU_MODE_FIQ;
10439         addr = 0x1c;
10440         /* Disable FIQ, IRQ and imprecise data aborts.  */
10441         mask = CPSR_A | CPSR_I | CPSR_F;
10442         offset = 4;
10443         break;
10444     case EXCP_VSERR:
10445         {
10446             /*
10447              * Note that this is reported as a data abort, but the DFAR
10448              * has an UNKNOWN value.  Construct the SError syndrome from
10449              * AET and ExT fields.
10450              */
10451             ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10452 
10453             if (extended_addresses_enabled(env)) {
10454                 env->exception.fsr = arm_fi_to_lfsc(&fi);
10455             } else {
10456                 env->exception.fsr = arm_fi_to_sfsc(&fi);
10457             }
10458             env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10459             A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10460             qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10461                           env->exception.fsr);
10462 
10463             new_mode = ARM_CPU_MODE_ABT;
10464             addr = 0x10;
10465             mask = CPSR_A | CPSR_I;
10466             offset = 8;
10467         }
10468         break;
10469     case EXCP_SMC:
10470         new_mode = ARM_CPU_MODE_MON;
10471         addr = 0x08;
10472         mask = CPSR_A | CPSR_I | CPSR_F;
10473         offset = 0;
10474         break;
10475     case EXCP_MON_TRAP:
10476         new_mode = ARM_CPU_MODE_MON;
10477         addr = 0x04;
10478         mask = CPSR_A | CPSR_I | CPSR_F;
10479         if (env->thumb) {
10480             offset = 2;
10481         } else {
10482             offset = 4;
10483         }
10484         break;
10485     default:
10486         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10487         return; /* Never happens.  Keep compiler happy.  */
10488     }
10489 
10490     if (new_mode == ARM_CPU_MODE_MON) {
10491         addr += env->cp15.mvbar;
10492     } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
10493         /* High vectors. When enabled, base address cannot be remapped. */
10494         addr += 0xffff0000;
10495     } else {
10496         /*
10497          * ARM v7 architectures provide a vector base address register to remap
10498          * the interrupt vector table.
10499          * This register is only followed in non-monitor mode, and is banked.
10500          * Note: only bits 31:5 are valid.
10501          */
10502         addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
10503     }
10504 
10505     if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10506         env->cp15.scr_el3 &= ~SCR_NS;
10507     }
10508 
10509     take_aarch32_exception(env, new_mode, mask, offset, addr);
10510 }
10511 
aarch64_regnum(CPUARMState * env,int aarch32_reg)10512 static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10513 {
10514     /*
10515      * Return the register number of the AArch64 view of the AArch32
10516      * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10517      * be that of the AArch32 mode the exception came from.
10518      */
10519     int mode = env->uncached_cpsr & CPSR_M;
10520 
10521     switch (aarch32_reg) {
10522     case 0 ... 7:
10523         return aarch32_reg;
10524     case 8 ... 12:
10525         return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10526     case 13:
10527         switch (mode) {
10528         case ARM_CPU_MODE_USR:
10529         case ARM_CPU_MODE_SYS:
10530             return 13;
10531         case ARM_CPU_MODE_HYP:
10532             return 15;
10533         case ARM_CPU_MODE_IRQ:
10534             return 17;
10535         case ARM_CPU_MODE_SVC:
10536             return 19;
10537         case ARM_CPU_MODE_ABT:
10538             return 21;
10539         case ARM_CPU_MODE_UND:
10540             return 23;
10541         case ARM_CPU_MODE_FIQ:
10542             return 29;
10543         default:
10544             g_assert_not_reached();
10545         }
10546     case 14:
10547         switch (mode) {
10548         case ARM_CPU_MODE_USR:
10549         case ARM_CPU_MODE_SYS:
10550         case ARM_CPU_MODE_HYP:
10551             return 14;
10552         case ARM_CPU_MODE_IRQ:
10553             return 16;
10554         case ARM_CPU_MODE_SVC:
10555             return 18;
10556         case ARM_CPU_MODE_ABT:
10557             return 20;
10558         case ARM_CPU_MODE_UND:
10559             return 22;
10560         case ARM_CPU_MODE_FIQ:
10561             return 30;
10562         default:
10563             g_assert_not_reached();
10564         }
10565     case 15:
10566         return 31;
10567     default:
10568         g_assert_not_reached();
10569     }
10570 }
10571 
cpsr_read_for_spsr_elx(CPUARMState * env)10572 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10573 {
10574     uint32_t ret = cpsr_read(env);
10575 
10576     /* Move DIT to the correct location for SPSR_ELx */
10577     if (ret & CPSR_DIT) {
10578         ret &= ~CPSR_DIT;
10579         ret |= PSTATE_DIT;
10580     }
10581     /* Merge PSTATE.SS into SPSR_ELx */
10582     ret |= env->pstate & PSTATE_SS;
10583 
10584     return ret;
10585 }
10586 
syndrome_is_sync_extabt(uint32_t syndrome)10587 static bool syndrome_is_sync_extabt(uint32_t syndrome)
10588 {
10589     /* Return true if this syndrome value is a synchronous external abort */
10590     switch (syn_get_ec(syndrome)) {
10591     case EC_INSNABORT:
10592     case EC_INSNABORT_SAME_EL:
10593     case EC_DATAABORT:
10594     case EC_DATAABORT_SAME_EL:
10595         /* Look at fault status code for all the synchronous ext abort cases */
10596         switch (syndrome & 0x3f) {
10597         case 0x10:
10598         case 0x13:
10599         case 0x14:
10600         case 0x15:
10601         case 0x16:
10602         case 0x17:
10603             return true;
10604         default:
10605             return false;
10606         }
10607     default:
10608         return false;
10609     }
10610 }
10611 
10612 /* Handle exception entry to a target EL which is using AArch64 */
arm_cpu_do_interrupt_aarch64(CPUState * cs)10613 static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
10614 {
10615     ARMCPU *cpu = ARM_CPU(cs);
10616     CPUARMState *env = &cpu->env;
10617     unsigned int new_el = env->exception.target_el;
10618     vaddr addr = env->cp15.vbar_el[new_el];
10619     unsigned int new_mode = aarch64_pstate_mode(new_el, true);
10620     unsigned int old_mode;
10621     unsigned int cur_el = arm_current_el(env);
10622     int rt;
10623 
10624     if (tcg_enabled()) {
10625         /*
10626          * Note that new_el can never be 0.  If cur_el is 0, then
10627          * el0_a64 is is_a64(), else el0_a64 is ignored.
10628          */
10629         aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10630     }
10631 
10632     if (cur_el < new_el) {
10633         /*
10634          * Entry vector offset depends on whether the implemented EL
10635          * immediately lower than the target level is using AArch32 or AArch64
10636          */
10637         bool is_aa64;
10638         uint64_t hcr;
10639 
10640         switch (new_el) {
10641         case 3:
10642             is_aa64 = arm_scr_rw_eff(env);
10643             break;
10644         case 2:
10645             hcr = arm_hcr_el2_eff(env);
10646             if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10647                 is_aa64 = (hcr & HCR_RW) != 0;
10648                 break;
10649             }
10650             /* fall through */
10651         case 1:
10652             is_aa64 = is_a64(env);
10653             break;
10654         default:
10655             g_assert_not_reached();
10656         }
10657 
10658         if (is_aa64) {
10659             addr += 0x400;
10660         } else {
10661             addr += 0x600;
10662         }
10663     } else if (pstate_read(env) & PSTATE_SP) {
10664         addr += 0x200;
10665     }
10666 
10667     switch (cs->exception_index) {
10668     case EXCP_GPC:
10669         qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n",
10670                       env->cp15.mfar_el3);
10671         /* fall through */
10672     case EXCP_PREFETCH_ABORT:
10673     case EXCP_DATA_ABORT:
10674         /*
10675          * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10676          * to be taken to the SError vector entrypoint.
10677          */
10678         if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10679             syndrome_is_sync_extabt(env->exception.syndrome)) {
10680             addr += 0x180;
10681         }
10682         env->cp15.far_el[new_el] = env->exception.vaddress;
10683         qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10684                       env->cp15.far_el[new_el]);
10685         /* fall through */
10686     case EXCP_BKPT:
10687     case EXCP_UDEF:
10688     case EXCP_SWI:
10689     case EXCP_HVC:
10690     case EXCP_HYP_TRAP:
10691     case EXCP_SMC:
10692         switch (syn_get_ec(env->exception.syndrome)) {
10693         case EC_ADVSIMDFPACCESSTRAP:
10694             /*
10695              * QEMU internal FP/SIMD syndromes from AArch32 include the
10696              * TA and coproc fields which are only exposed if the exception
10697              * is taken to AArch32 Hyp mode. Mask them out to get a valid
10698              * AArch64 format syndrome.
10699              */
10700             env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
10701             break;
10702         case EC_CP14RTTRAP:
10703         case EC_CP15RTTRAP:
10704         case EC_CP14DTTRAP:
10705             /*
10706              * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10707              * the raw register field from the insn; when taking this to
10708              * AArch64 we must convert it to the AArch64 view of the register
10709              * number. Notice that we read a 4-bit AArch32 register number and
10710              * write back a 5-bit AArch64 one.
10711              */
10712             rt = extract32(env->exception.syndrome, 5, 4);
10713             rt = aarch64_regnum(env, rt);
10714             env->exception.syndrome = deposit32(env->exception.syndrome,
10715                                                 5, 5, rt);
10716             break;
10717         case EC_CP15RRTTRAP:
10718         case EC_CP14RRTTRAP:
10719             /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10720             rt = extract32(env->exception.syndrome, 5, 4);
10721             rt = aarch64_regnum(env, rt);
10722             env->exception.syndrome = deposit32(env->exception.syndrome,
10723                                                 5, 5, rt);
10724             rt = extract32(env->exception.syndrome, 10, 4);
10725             rt = aarch64_regnum(env, rt);
10726             env->exception.syndrome = deposit32(env->exception.syndrome,
10727                                                 10, 5, rt);
10728             break;
10729         }
10730         env->cp15.esr_el[new_el] = env->exception.syndrome;
10731         break;
10732     case EXCP_IRQ:
10733     case EXCP_VIRQ:
10734     case EXCP_NMI:
10735     case EXCP_VINMI:
10736         addr += 0x80;
10737         break;
10738     case EXCP_FIQ:
10739     case EXCP_VFIQ:
10740     case EXCP_VFNMI:
10741         addr += 0x100;
10742         break;
10743     case EXCP_VSERR:
10744         addr += 0x180;
10745         /* Construct the SError syndrome from IDS and ISS fields. */
10746         env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10747         env->cp15.esr_el[new_el] = env->exception.syndrome;
10748         break;
10749     default:
10750         cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10751     }
10752 
10753     if (is_a64(env)) {
10754         old_mode = pstate_read(env);
10755         aarch64_save_sp(env, arm_current_el(env));
10756         env->elr_el[new_el] = env->pc;
10757 
10758         if (cur_el == 1 && new_el == 1) {
10759             uint64_t hcr = arm_hcr_el2_eff(env);
10760             if ((hcr & (HCR_NV | HCR_NV1 | HCR_NV2)) == HCR_NV ||
10761                 (hcr & (HCR_NV | HCR_NV2)) == (HCR_NV | HCR_NV2)) {
10762                 /*
10763                  * FEAT_NV, FEAT_NV2 may need to report EL2 in the SPSR
10764                  * by setting M[3:2] to 0b10.
10765                  * If NV2 is disabled, change SPSR when NV,NV1 == 1,0 (I_ZJRNN)
10766                  * If NV2 is enabled, change SPSR when NV is 1 (I_DBTLM)
10767                  */
10768                 old_mode = deposit32(old_mode, 2, 2, 2);
10769             }
10770         }
10771     } else {
10772         old_mode = cpsr_read_for_spsr_elx(env);
10773         env->elr_el[new_el] = env->regs[15];
10774 
10775         aarch64_sync_32_to_64(env);
10776 
10777         env->condexec_bits = 0;
10778     }
10779     env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10780 
10781     qemu_log_mask(CPU_LOG_INT, "...with SPSR 0x%x\n", old_mode);
10782     qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10783                   env->elr_el[new_el]);
10784 
10785     if (cpu_isar_feature(aa64_pan, cpu)) {
10786         /* The value of PSTATE.PAN is normally preserved, except when ... */
10787         new_mode |= old_mode & PSTATE_PAN;
10788         switch (new_el) {
10789         case 2:
10790             /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ...  */
10791             if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10792                 != (HCR_E2H | HCR_TGE)) {
10793                 break;
10794             }
10795             /* fall through */
10796         case 1:
10797             /* ... the target is EL1 ... */
10798             /* ... and SCTLR_ELx.SPAN == 0, then set to 1.  */
10799             if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10800                 new_mode |= PSTATE_PAN;
10801             }
10802             break;
10803         }
10804     }
10805     if (cpu_isar_feature(aa64_mte, cpu)) {
10806         new_mode |= PSTATE_TCO;
10807     }
10808 
10809     if (cpu_isar_feature(aa64_ssbs, cpu)) {
10810         if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10811             new_mode |= PSTATE_SSBS;
10812         } else {
10813             new_mode &= ~PSTATE_SSBS;
10814         }
10815     }
10816 
10817     if (cpu_isar_feature(aa64_nmi, cpu)) {
10818         if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPINTMASK)) {
10819             new_mode |= PSTATE_ALLINT;
10820         } else {
10821             new_mode &= ~PSTATE_ALLINT;
10822         }
10823     }
10824 
10825     pstate_write(env, PSTATE_DAIF | new_mode);
10826     env->aarch64 = true;
10827     aarch64_restore_sp(env, new_el);
10828 
10829     if (tcg_enabled()) {
10830         helper_rebuild_hflags_a64(env, new_el);
10831     }
10832 
10833     env->pc = addr;
10834 
10835     qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10836                   new_el, env->pc, pstate_read(env));
10837 }
10838 
10839 /*
10840  * Do semihosting call and set the appropriate return value. All the
10841  * permission and validity checks have been done at translate time.
10842  *
10843  * We only see semihosting exceptions in TCG only as they are not
10844  * trapped to the hypervisor in KVM.
10845  */
10846 #ifdef CONFIG_TCG
tcg_handle_semihosting(CPUState * cs)10847 static void tcg_handle_semihosting(CPUState *cs)
10848 {
10849     ARMCPU *cpu = ARM_CPU(cs);
10850     CPUARMState *env = &cpu->env;
10851 
10852     if (is_a64(env)) {
10853         qemu_log_mask(CPU_LOG_INT,
10854                       "...handling as semihosting call 0x%" PRIx64 "\n",
10855                       env->xregs[0]);
10856         do_common_semihosting(cs);
10857         env->pc += 4;
10858     } else {
10859         qemu_log_mask(CPU_LOG_INT,
10860                       "...handling as semihosting call 0x%x\n",
10861                       env->regs[0]);
10862         do_common_semihosting(cs);
10863         env->regs[15] += env->thumb ? 2 : 4;
10864     }
10865 }
10866 #endif
10867 
10868 /*
10869  * Handle a CPU exception for A and R profile CPUs.
10870  * Do any appropriate logging, handle PSCI calls, and then hand off
10871  * to the AArch64-entry or AArch32-entry function depending on the
10872  * target exception level's register width.
10873  *
10874  * Note: this is used for both TCG (as the do_interrupt tcg op),
10875  *       and KVM to re-inject guest debug exceptions, and to
10876  *       inject a Synchronous-External-Abort.
10877  */
arm_cpu_do_interrupt(CPUState * cs)10878 void arm_cpu_do_interrupt(CPUState *cs)
10879 {
10880     ARMCPU *cpu = ARM_CPU(cs);
10881     CPUARMState *env = &cpu->env;
10882     unsigned int new_el = env->exception.target_el;
10883 
10884     assert(!arm_feature(env, ARM_FEATURE_M));
10885 
10886     arm_log_exception(cs);
10887     qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
10888                   new_el);
10889     if (qemu_loglevel_mask(CPU_LOG_INT)
10890         && !excp_is_internal(cs->exception_index)) {
10891         qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
10892                       syn_get_ec(env->exception.syndrome),
10893                       env->exception.syndrome);
10894     }
10895 
10896     if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
10897         arm_handle_psci_call(cpu);
10898         qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
10899         return;
10900     }
10901 
10902     /*
10903      * Semihosting semantics depend on the register width of the code
10904      * that caused the exception, not the target exception level, so
10905      * must be handled here.
10906      */
10907 #ifdef CONFIG_TCG
10908     if (cs->exception_index == EXCP_SEMIHOST) {
10909         tcg_handle_semihosting(cs);
10910         return;
10911     }
10912 #endif
10913 
10914     /*
10915      * Hooks may change global state so BQL should be held, also the
10916      * BQL needs to be held for any modification of
10917      * cs->interrupt_request.
10918      */
10919     g_assert(bql_locked());
10920 
10921     arm_call_pre_el_change_hook(cpu);
10922 
10923     assert(!excp_is_internal(cs->exception_index));
10924     if (arm_el_is_aa64(env, new_el)) {
10925         arm_cpu_do_interrupt_aarch64(cs);
10926     } else {
10927         arm_cpu_do_interrupt_aarch32(cs);
10928     }
10929 
10930     arm_call_el_change_hook(cpu);
10931 
10932     if (!kvm_enabled()) {
10933         cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
10934     }
10935 }
10936 #endif /* !CONFIG_USER_ONLY */
10937 
arm_sctlr(CPUARMState * env,int el)10938 uint64_t arm_sctlr(CPUARMState *env, int el)
10939 {
10940     /* Only EL0 needs to be adjusted for EL1&0 or EL2&0 or EL3&0 */
10941     if (el == 0) {
10942         ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
10943         switch (mmu_idx) {
10944         case ARMMMUIdx_E20_0:
10945             el = 2;
10946             break;
10947         case ARMMMUIdx_E30_0:
10948             el = 3;
10949             break;
10950         default:
10951             el = 1;
10952             break;
10953         }
10954     }
10955     return env->cp15.sctlr_el[el];
10956 }
10957 
aa64_va_parameter_tbi(uint64_t tcr,ARMMMUIdx mmu_idx)10958 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
10959 {
10960     if (regime_has_2_ranges(mmu_idx)) {
10961         return extract64(tcr, 37, 2);
10962     } else if (regime_is_stage2(mmu_idx)) {
10963         return 0; /* VTCR_EL2 */
10964     } else {
10965         /* Replicate the single TBI bit so we always have 2 bits.  */
10966         return extract32(tcr, 20, 1) * 3;
10967     }
10968 }
10969 
aa64_va_parameter_tbid(uint64_t tcr,ARMMMUIdx mmu_idx)10970 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
10971 {
10972     if (regime_has_2_ranges(mmu_idx)) {
10973         return extract64(tcr, 51, 2);
10974     } else if (regime_is_stage2(mmu_idx)) {
10975         return 0; /* VTCR_EL2 */
10976     } else {
10977         /* Replicate the single TBID bit so we always have 2 bits.  */
10978         return extract32(tcr, 29, 1) * 3;
10979     }
10980 }
10981 
aa64_va_parameter_tcma(uint64_t tcr,ARMMMUIdx mmu_idx)10982 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
10983 {
10984     if (regime_has_2_ranges(mmu_idx)) {
10985         return extract64(tcr, 57, 2);
10986     } else {
10987         /* Replicate the single TCMA bit so we always have 2 bits.  */
10988         return extract32(tcr, 30, 1) * 3;
10989     }
10990 }
10991 
tg0_to_gran_size(int tg)10992 static ARMGranuleSize tg0_to_gran_size(int tg)
10993 {
10994     switch (tg) {
10995     case 0:
10996         return Gran4K;
10997     case 1:
10998         return Gran64K;
10999     case 2:
11000         return Gran16K;
11001     default:
11002         return GranInvalid;
11003     }
11004 }
11005 
tg1_to_gran_size(int tg)11006 static ARMGranuleSize tg1_to_gran_size(int tg)
11007 {
11008     switch (tg) {
11009     case 1:
11010         return Gran16K;
11011     case 2:
11012         return Gran4K;
11013     case 3:
11014         return Gran64K;
11015     default:
11016         return GranInvalid;
11017     }
11018 }
11019 
have4k(ARMCPU * cpu,bool stage2)11020 static inline bool have4k(ARMCPU *cpu, bool stage2)
11021 {
11022     return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
11023         : cpu_isar_feature(aa64_tgran4, cpu);
11024 }
11025 
have16k(ARMCPU * cpu,bool stage2)11026 static inline bool have16k(ARMCPU *cpu, bool stage2)
11027 {
11028     return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
11029         : cpu_isar_feature(aa64_tgran16, cpu);
11030 }
11031 
have64k(ARMCPU * cpu,bool stage2)11032 static inline bool have64k(ARMCPU *cpu, bool stage2)
11033 {
11034     return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
11035         : cpu_isar_feature(aa64_tgran64, cpu);
11036 }
11037 
sanitize_gran_size(ARMCPU * cpu,ARMGranuleSize gran,bool stage2)11038 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
11039                                          bool stage2)
11040 {
11041     switch (gran) {
11042     case Gran4K:
11043         if (have4k(cpu, stage2)) {
11044             return gran;
11045         }
11046         break;
11047     case Gran16K:
11048         if (have16k(cpu, stage2)) {
11049             return gran;
11050         }
11051         break;
11052     case Gran64K:
11053         if (have64k(cpu, stage2)) {
11054             return gran;
11055         }
11056         break;
11057     case GranInvalid:
11058         break;
11059     }
11060     /*
11061      * If the guest selects a granule size that isn't implemented,
11062      * the architecture requires that we behave as if it selected one
11063      * that is (with an IMPDEF choice of which one to pick). We choose
11064      * to implement the smallest supported granule size.
11065      */
11066     if (have4k(cpu, stage2)) {
11067         return Gran4K;
11068     }
11069     if (have16k(cpu, stage2)) {
11070         return Gran16K;
11071     }
11072     assert(have64k(cpu, stage2));
11073     return Gran64K;
11074 }
11075 
aa64_va_parameters(CPUARMState * env,uint64_t va,ARMMMUIdx mmu_idx,bool data,bool el1_is_aa32)11076 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11077                                    ARMMMUIdx mmu_idx, bool data,
11078                                    bool el1_is_aa32)
11079 {
11080     uint64_t tcr = regime_tcr(env, mmu_idx);
11081     bool epd, hpd, tsz_oob, ds, ha, hd;
11082     int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
11083     ARMGranuleSize gran;
11084     ARMCPU *cpu = env_archcpu(env);
11085     bool stage2 = regime_is_stage2(mmu_idx);
11086 
11087     if (!regime_has_2_ranges(mmu_idx)) {
11088         select = 0;
11089         tsz = extract32(tcr, 0, 6);
11090         gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11091         if (stage2) {
11092             /* VTCR_EL2 */
11093             hpd = false;
11094         } else {
11095             hpd = extract32(tcr, 24, 1);
11096         }
11097         epd = false;
11098         sh = extract32(tcr, 12, 2);
11099         ps = extract32(tcr, 16, 3);
11100         ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11101         hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11102         ds = extract64(tcr, 32, 1);
11103     } else {
11104         bool e0pd;
11105 
11106         /*
11107          * Bit 55 is always between the two regions, and is canonical for
11108          * determining if address tagging is enabled.
11109          */
11110         select = extract64(va, 55, 1);
11111         if (!select) {
11112             tsz = extract32(tcr, 0, 6);
11113             gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11114             epd = extract32(tcr, 7, 1);
11115             sh = extract32(tcr, 12, 2);
11116             hpd = extract64(tcr, 41, 1);
11117             e0pd = extract64(tcr, 55, 1);
11118         } else {
11119             tsz = extract32(tcr, 16, 6);
11120             gran = tg1_to_gran_size(extract32(tcr, 30, 2));
11121             epd = extract32(tcr, 23, 1);
11122             sh = extract32(tcr, 28, 2);
11123             hpd = extract64(tcr, 42, 1);
11124             e0pd = extract64(tcr, 56, 1);
11125         }
11126         ps = extract64(tcr, 32, 3);
11127         ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11128         hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
11129         ds = extract64(tcr, 59, 1);
11130 
11131         if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11132             regime_is_user(env, mmu_idx)) {
11133             epd = true;
11134         }
11135     }
11136 
11137     gran = sanitize_gran_size(cpu, gran, stage2);
11138 
11139     if (cpu_isar_feature(aa64_st, cpu)) {
11140         max_tsz = 48 - (gran == Gran64K);
11141     } else {
11142         max_tsz = 39;
11143     }
11144 
11145     /*
11146      * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11147      * adjust the effective value of DS, as documented.
11148      */
11149     min_tsz = 16;
11150     if (gran == Gran64K) {
11151         if (cpu_isar_feature(aa64_lva, cpu)) {
11152             min_tsz = 12;
11153         }
11154         ds = false;
11155     } else if (ds) {
11156         if (regime_is_stage2(mmu_idx)) {
11157             if (gran == Gran16K) {
11158                 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11159             } else {
11160                 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11161             }
11162         } else {
11163             if (gran == Gran16K) {
11164                 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11165             } else {
11166                 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11167             }
11168         }
11169         if (ds) {
11170             min_tsz = 12;
11171         }
11172     }
11173 
11174     if (stage2 && el1_is_aa32) {
11175         /*
11176          * For AArch32 EL1 the min txsz (and thus max IPA size) requirements
11177          * are loosened: a configured IPA of 40 bits is permitted even if
11178          * the implemented PA is less than that (and so a 40 bit IPA would
11179          * fault for an AArch64 EL1). See R_DTLMN.
11180          */
11181         min_tsz = MIN(min_tsz, 24);
11182     }
11183 
11184     if (tsz > max_tsz) {
11185         tsz = max_tsz;
11186         tsz_oob = true;
11187     } else if (tsz < min_tsz) {
11188         tsz = min_tsz;
11189         tsz_oob = true;
11190     } else {
11191         tsz_oob = false;
11192     }
11193 
11194     /* Present TBI as a composite with TBID.  */
11195     tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11196     if (!data) {
11197         tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11198     }
11199     tbi = (tbi >> select) & 1;
11200 
11201     return (ARMVAParameters) {
11202         .tsz = tsz,
11203         .ps = ps,
11204         .sh = sh,
11205         .select = select,
11206         .tbi = tbi,
11207         .epd = epd,
11208         .hpd = hpd,
11209         .tsz_oob = tsz_oob,
11210         .ds = ds,
11211         .ha = ha,
11212         .hd = ha && hd,
11213         .gran = gran,
11214     };
11215 }
11216 
11217 
11218 /*
11219  * Return the exception level to which FP-disabled exceptions should
11220  * be taken, or 0 if FP is enabled.
11221  */
fp_exception_el(CPUARMState * env,int cur_el)11222 int fp_exception_el(CPUARMState *env, int cur_el)
11223 {
11224 #ifndef CONFIG_USER_ONLY
11225     uint64_t hcr_el2;
11226 
11227     /*
11228      * CPACR and the CPTR registers don't exist before v6, so FP is
11229      * always accessible
11230      */
11231     if (!arm_feature(env, ARM_FEATURE_V6)) {
11232         return 0;
11233     }
11234 
11235     if (arm_feature(env, ARM_FEATURE_M)) {
11236         /* CPACR can cause a NOCP UsageFault taken to current security state */
11237         if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11238             return 1;
11239         }
11240 
11241         if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11242             if (!extract32(env->v7m.nsacr, 10, 1)) {
11243                 /* FP insns cause a NOCP UsageFault taken to Secure */
11244                 return 3;
11245             }
11246         }
11247 
11248         return 0;
11249     }
11250 
11251     hcr_el2 = arm_hcr_el2_eff(env);
11252 
11253     /*
11254      * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
11255      * 0, 2 : trap EL0 and EL1/PL1 accesses
11256      * 1    : trap only EL0 accesses
11257      * 3    : trap no accesses
11258      * This register is ignored if E2H+TGE are both set.
11259      */
11260     if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
11261         int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
11262 
11263         switch (fpen) {
11264         case 1:
11265             if (cur_el != 0) {
11266                 break;
11267             }
11268             /* fall through */
11269         case 0:
11270         case 2:
11271             /* Trap from Secure PL0 or PL1 to Secure PL1. */
11272             if (!arm_el_is_aa64(env, 3)
11273                 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
11274                 return 3;
11275             }
11276             if (cur_el <= 1) {
11277                 return 1;
11278             }
11279             break;
11280         }
11281     }
11282 
11283     /*
11284      * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11285      * to control non-secure access to the FPU. It doesn't have any
11286      * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11287      */
11288     if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11289          cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11290         if (!extract32(env->cp15.nsacr, 10, 1)) {
11291             /* FP insns act as UNDEF */
11292             return cur_el == 2 ? 2 : 1;
11293         }
11294     }
11295 
11296     /*
11297      * CPTR_EL2 is present in v7VE or v8, and changes format
11298      * with HCR_EL2.E2H (regardless of TGE).
11299      */
11300     if (cur_el <= 2) {
11301         if (hcr_el2 & HCR_E2H) {
11302             switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
11303             case 1:
11304                 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11305                     break;
11306                 }
11307                 /* fall through */
11308             case 0:
11309             case 2:
11310                 return 2;
11311             }
11312         } else if (arm_is_el2_enabled(env)) {
11313             if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
11314                 return 2;
11315             }
11316         }
11317     }
11318 
11319     /* CPTR_EL3 : present in v8 */
11320     if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
11321         /* Trap all FP ops to EL3 */
11322         return 3;
11323     }
11324 #endif
11325     return 0;
11326 }
11327 
11328 /* Return the exception level we're running at if this is our mmu_idx */
arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)11329 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11330 {
11331     if (mmu_idx & ARM_MMU_IDX_M) {
11332         return mmu_idx & ARM_MMU_IDX_M_PRIV;
11333     }
11334 
11335     switch (mmu_idx) {
11336     case ARMMMUIdx_E10_0:
11337     case ARMMMUIdx_E20_0:
11338     case ARMMMUIdx_E30_0:
11339         return 0;
11340     case ARMMMUIdx_E10_1:
11341     case ARMMMUIdx_E10_1_PAN:
11342         return 1;
11343     case ARMMMUIdx_E2:
11344     case ARMMMUIdx_E20_2:
11345     case ARMMMUIdx_E20_2_PAN:
11346         return 2;
11347     case ARMMMUIdx_E3:
11348     case ARMMMUIdx_E30_3_PAN:
11349         return 3;
11350     default:
11351         g_assert_not_reached();
11352     }
11353 }
11354 
11355 #ifndef CONFIG_TCG
arm_v7m_mmu_idx_for_secstate(CPUARMState * env,bool secstate)11356 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11357 {
11358     g_assert_not_reached();
11359 }
11360 #endif
11361 
arm_mmu_idx_el(CPUARMState * env,int el)11362 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
11363 {
11364     ARMMMUIdx idx;
11365     uint64_t hcr;
11366 
11367     if (arm_feature(env, ARM_FEATURE_M)) {
11368         return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
11369     }
11370 
11371     /* See ARM pseudo-function ELIsInHost.  */
11372     switch (el) {
11373     case 0:
11374         hcr = arm_hcr_el2_eff(env);
11375         if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11376             idx = ARMMMUIdx_E20_0;
11377         } else if (arm_is_secure_below_el3(env) &&
11378                    !arm_el_is_aa64(env, 3)) {
11379             idx = ARMMMUIdx_E30_0;
11380         } else {
11381             idx = ARMMMUIdx_E10_0;
11382         }
11383         break;
11384     case 1:
11385         if (arm_pan_enabled(env)) {
11386             idx = ARMMMUIdx_E10_1_PAN;
11387         } else {
11388             idx = ARMMMUIdx_E10_1;
11389         }
11390         break;
11391     case 2:
11392         /* Note that TGE does not apply at EL2.  */
11393         if (arm_hcr_el2_eff(env) & HCR_E2H) {
11394             if (arm_pan_enabled(env)) {
11395                 idx = ARMMMUIdx_E20_2_PAN;
11396             } else {
11397                 idx = ARMMMUIdx_E20_2;
11398             }
11399         } else {
11400             idx = ARMMMUIdx_E2;
11401         }
11402         break;
11403     case 3:
11404         if (!arm_el_is_aa64(env, 3) && arm_pan_enabled(env)) {
11405             return ARMMMUIdx_E30_3_PAN;
11406         }
11407         return ARMMMUIdx_E3;
11408     default:
11409         g_assert_not_reached();
11410     }
11411 
11412     return idx;
11413 }
11414 
arm_mmu_idx(CPUARMState * env)11415 ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11416 {
11417     return arm_mmu_idx_el(env, arm_current_el(env));
11418 }
11419 
11420 /*
11421  * The manual says that when SVE is enabled and VQ is widened the
11422  * implementation is allowed to zero the previously inaccessible
11423  * portion of the registers.  The corollary to that is that when
11424  * SVE is enabled and VQ is narrowed we are also allowed to zero
11425  * the now inaccessible portion of the registers.
11426  *
11427  * The intent of this is that no predicate bit beyond VQ is ever set.
11428  * Which means that some operations on predicate registers themselves
11429  * may operate on full uint64_t or even unrolled across the maximum
11430  * uint64_t[4].  Performing 4 bits of host arithmetic unconditionally
11431  * may well be cheaper than conditionals to restrict the operation
11432  * to the relevant portion of a uint16_t[16].
11433  */
aarch64_sve_narrow_vq(CPUARMState * env,unsigned vq)11434 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11435 {
11436     int i, j;
11437     uint64_t pmask;
11438 
11439     assert(vq >= 1 && vq <= ARM_MAX_VQ);
11440     assert(vq <= env_archcpu(env)->sve_max_vq);
11441 
11442     /* Zap the high bits of the zregs.  */
11443     for (i = 0; i < 32; i++) {
11444         memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11445     }
11446 
11447     /* Zap the high bits of the pregs and ffr.  */
11448     pmask = 0;
11449     if (vq & 3) {
11450         pmask = ~(-1ULL << (16 * (vq & 3)));
11451     }
11452     for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11453         for (i = 0; i < 17; ++i) {
11454             env->vfp.pregs[i].p[j] &= pmask;
11455         }
11456         pmask = 0;
11457     }
11458 }
11459 
sve_vqm1_for_el_sm_ena(CPUARMState * env,int el,bool sm)11460 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
11461 {
11462     int exc_el;
11463 
11464     if (sm) {
11465         exc_el = sme_exception_el(env, el);
11466     } else {
11467         exc_el = sve_exception_el(env, el);
11468     }
11469     if (exc_el) {
11470         return 0; /* disabled */
11471     }
11472     return sve_vqm1_for_el_sm(env, el, sm);
11473 }
11474 
11475 /*
11476  * Notice a change in SVE vector size when changing EL.
11477  */
aarch64_sve_change_el(CPUARMState * env,int old_el,int new_el,bool el0_a64)11478 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11479                            int new_el, bool el0_a64)
11480 {
11481     ARMCPU *cpu = env_archcpu(env);
11482     int old_len, new_len;
11483     bool old_a64, new_a64, sm;
11484 
11485     /* Nothing to do if no SVE.  */
11486     if (!cpu_isar_feature(aa64_sve, cpu)) {
11487         return;
11488     }
11489 
11490     /* Nothing to do if FP is disabled in either EL.  */
11491     if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11492         return;
11493     }
11494 
11495     old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11496     new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11497 
11498     /*
11499      * Both AArch64.TakeException and AArch64.ExceptionReturn
11500      * invoke ResetSVEState when taking an exception from, or
11501      * returning to, AArch32 state when PSTATE.SM is enabled.
11502      */
11503     sm = FIELD_EX64(env->svcr, SVCR, SM);
11504     if (old_a64 != new_a64 && sm) {
11505         arm_reset_sve_state(env);
11506         return;
11507     }
11508 
11509     /*
11510      * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11511      * at ELx, or not available because the EL is in AArch32 state, then
11512      * for all purposes other than a direct read, the ZCR_ELx.LEN field
11513      * has an effective value of 0".
11514      *
11515      * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11516      * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11517      * from EL2->EL1.  Thus we go ahead and narrow when entering aa32 so that
11518      * we already have the correct register contents when encountering the
11519      * vq0->vq0 transition between EL0->EL1.
11520      */
11521     old_len = new_len = 0;
11522     if (old_a64) {
11523         old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
11524     }
11525     if (new_a64) {
11526         new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
11527     }
11528 
11529     /* When changing vector length, clear inaccessible state.  */
11530     if (new_len < old_len) {
11531         aarch64_sve_narrow_vq(env, new_len + 1);
11532     }
11533 }
11534 
11535 #ifndef CONFIG_USER_ONLY
arm_security_space(CPUARMState * env)11536 ARMSecuritySpace arm_security_space(CPUARMState *env)
11537 {
11538     if (arm_feature(env, ARM_FEATURE_M)) {
11539         return arm_secure_to_space(env->v7m.secure);
11540     }
11541 
11542     /*
11543      * If EL3 is not supported then the secure state is implementation
11544      * defined, in which case QEMU defaults to non-secure.
11545      */
11546     if (!arm_feature(env, ARM_FEATURE_EL3)) {
11547         return ARMSS_NonSecure;
11548     }
11549 
11550     /* Check for AArch64 EL3 or AArch32 Mon. */
11551     if (is_a64(env)) {
11552         if (extract32(env->pstate, 2, 2) == 3) {
11553             if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
11554                 return ARMSS_Root;
11555             } else {
11556                 return ARMSS_Secure;
11557             }
11558         }
11559     } else {
11560         if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
11561             return ARMSS_Secure;
11562         }
11563     }
11564 
11565     return arm_security_space_below_el3(env);
11566 }
11567 
arm_security_space_below_el3(CPUARMState * env)11568 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
11569 {
11570     assert(!arm_feature(env, ARM_FEATURE_M));
11571 
11572     /*
11573      * If EL3 is not supported then the secure state is implementation
11574      * defined, in which case QEMU defaults to non-secure.
11575      */
11576     if (!arm_feature(env, ARM_FEATURE_EL3)) {
11577         return ARMSS_NonSecure;
11578     }
11579 
11580     /*
11581      * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
11582      * Ignoring NSE when !NS retains consistency without having to
11583      * modify other predicates.
11584      */
11585     if (!(env->cp15.scr_el3 & SCR_NS)) {
11586         return ARMSS_Secure;
11587     } else if (env->cp15.scr_el3 & SCR_NSE) {
11588         return ARMSS_Realm;
11589     } else {
11590         return ARMSS_NonSecure;
11591     }
11592 }
11593 #endif /* !CONFIG_USER_ONLY */
11594