1 /*
2 * ARM generic helpers.
3 *
4 * This code is licensed under the GNU GPL v2 or later.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "qemu/osdep.h"
10 #include "qemu/log.h"
11 #include "trace.h"
12 #include "cpu.h"
13 #include "internals.h"
14 #include "cpu-features.h"
15 #include "exec/helper-proto.h"
16 #include "exec/page-protection.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/exec-all.h"
23 #include "exec/translation-block.h"
24 #include "hw/irq.h"
25 #include "system/cpu-timers.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 "semihosting/common-semi.h"
32 #endif
33 #include "cpregs.h"
34 #include "target/arm/gtimer.h"
35
36 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
37
38 static void switch_mode(CPUARMState *env, int mode);
39
raw_read(CPUARMState * env,const ARMCPRegInfo * ri)40 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
41 {
42 assert(ri->fieldoffset);
43 if (cpreg_field_is_64bit(ri)) {
44 return CPREG_FIELD64(env, ri);
45 } else {
46 return CPREG_FIELD32(env, ri);
47 }
48 }
49
raw_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)50 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
51 {
52 assert(ri->fieldoffset);
53 if (cpreg_field_is_64bit(ri)) {
54 CPREG_FIELD64(env, ri) = value;
55 } else {
56 CPREG_FIELD32(env, ri) = value;
57 }
58 }
59
raw_ptr(CPUARMState * env,const ARMCPRegInfo * ri)60 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
61 {
62 return (char *)env + ri->fieldoffset;
63 }
64
read_raw_cp_reg(CPUARMState * env,const ARMCPRegInfo * ri)65 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
66 {
67 /* Raw read of a coprocessor register (as needed for migration, etc). */
68 if (ri->type & ARM_CP_CONST) {
69 return ri->resetvalue;
70 } else if (ri->raw_readfn) {
71 return ri->raw_readfn(env, ri);
72 } else if (ri->readfn) {
73 return ri->readfn(env, ri);
74 } else {
75 return raw_read(env, ri);
76 }
77 }
78
write_raw_cp_reg(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t v)79 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
80 uint64_t v)
81 {
82 /*
83 * Raw write of a coprocessor register (as needed for migration, etc).
84 * Note that constant registers are treated as write-ignored; the
85 * caller should check for success by whether a readback gives the
86 * value written.
87 */
88 if (ri->type & ARM_CP_CONST) {
89 return;
90 } else if (ri->raw_writefn) {
91 ri->raw_writefn(env, ri, v);
92 } else if (ri->writefn) {
93 ri->writefn(env, ri, v);
94 } else {
95 raw_write(env, ri, v);
96 }
97 }
98
raw_accessors_invalid(const ARMCPRegInfo * ri)99 static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
100 {
101 /*
102 * Return true if the regdef would cause an assertion if you called
103 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
104 * program bug for it not to have the NO_RAW flag).
105 * NB that returning false here doesn't necessarily mean that calling
106 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
107 * read/write access functions which are safe for raw use" from "has
108 * read/write access functions which have side effects but has forgotten
109 * to provide raw access functions".
110 * The tests here line up with the conditions in read/write_raw_cp_reg()
111 * and assertions in raw_read()/raw_write().
112 */
113 if ((ri->type & ARM_CP_CONST) ||
114 ri->fieldoffset ||
115 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
116 return false;
117 }
118 return true;
119 }
120
write_cpustate_to_list(ARMCPU * cpu,bool kvm_sync)121 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
122 {
123 /* Write the coprocessor state from cpu->env to the (index,value) list. */
124 int i;
125 bool ok = true;
126
127 for (i = 0; i < cpu->cpreg_array_len; i++) {
128 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
129 const ARMCPRegInfo *ri;
130 uint64_t newval;
131
132 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
133 if (!ri) {
134 ok = false;
135 continue;
136 }
137 if (ri->type & ARM_CP_NO_RAW) {
138 continue;
139 }
140
141 newval = read_raw_cp_reg(&cpu->env, ri);
142 if (kvm_sync) {
143 /*
144 * Only sync if the previous list->cpustate sync succeeded.
145 * Rather than tracking the success/failure state for every
146 * item in the list, we just recheck "does the raw write we must
147 * have made in write_list_to_cpustate() read back OK" here.
148 */
149 uint64_t oldval = cpu->cpreg_values[i];
150
151 if (oldval == newval) {
152 continue;
153 }
154
155 write_raw_cp_reg(&cpu->env, ri, oldval);
156 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
157 continue;
158 }
159
160 write_raw_cp_reg(&cpu->env, ri, newval);
161 }
162 cpu->cpreg_values[i] = newval;
163 }
164 return ok;
165 }
166
write_list_to_cpustate(ARMCPU * cpu)167 bool write_list_to_cpustate(ARMCPU *cpu)
168 {
169 int i;
170 bool ok = true;
171
172 for (i = 0; i < cpu->cpreg_array_len; i++) {
173 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
174 uint64_t v = cpu->cpreg_values[i];
175 const ARMCPRegInfo *ri;
176
177 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
178 if (!ri) {
179 ok = false;
180 continue;
181 }
182 if (ri->type & ARM_CP_NO_RAW) {
183 continue;
184 }
185 /*
186 * Write value and confirm it reads back as written
187 * (to catch read-only registers and partially read-only
188 * registers where the incoming migration value doesn't match)
189 */
190 write_raw_cp_reg(&cpu->env, ri, v);
191 if (read_raw_cp_reg(&cpu->env, ri) != v) {
192 ok = false;
193 }
194 }
195 return ok;
196 }
197
add_cpreg_to_list(gpointer key,gpointer opaque)198 static void add_cpreg_to_list(gpointer key, gpointer opaque)
199 {
200 ARMCPU *cpu = opaque;
201 uint32_t regidx = (uintptr_t)key;
202 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
203
204 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
205 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
206 /* The value array need not be initialized at this point */
207 cpu->cpreg_array_len++;
208 }
209 }
210
count_cpreg(gpointer key,gpointer opaque)211 static void count_cpreg(gpointer key, gpointer opaque)
212 {
213 ARMCPU *cpu = opaque;
214 const ARMCPRegInfo *ri;
215
216 ri = g_hash_table_lookup(cpu->cp_regs, key);
217
218 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
219 cpu->cpreg_array_len++;
220 }
221 }
222
cpreg_key_compare(gconstpointer a,gconstpointer b)223 static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
224 {
225 uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
226 uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
227
228 if (aidx > bidx) {
229 return 1;
230 }
231 if (aidx < bidx) {
232 return -1;
233 }
234 return 0;
235 }
236
init_cpreg_list(ARMCPU * cpu)237 void init_cpreg_list(ARMCPU *cpu)
238 {
239 /*
240 * Initialise the cpreg_tuples[] array based on the cp_regs hash.
241 * Note that we require cpreg_tuples[] to be sorted by key ID.
242 */
243 GList *keys;
244 int arraylen;
245
246 keys = g_hash_table_get_keys(cpu->cp_regs);
247 keys = g_list_sort(keys, cpreg_key_compare);
248
249 cpu->cpreg_array_len = 0;
250
251 g_list_foreach(keys, count_cpreg, cpu);
252
253 arraylen = cpu->cpreg_array_len;
254 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
255 cpu->cpreg_values = g_new(uint64_t, arraylen);
256 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
257 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
258 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
259 cpu->cpreg_array_len = 0;
260
261 g_list_foreach(keys, add_cpreg_to_list, cpu);
262
263 assert(cpu->cpreg_array_len == arraylen);
264
265 g_list_free(keys);
266 }
267
arm_pan_enabled(CPUARMState * env)268 static bool arm_pan_enabled(CPUARMState *env)
269 {
270 if (is_a64(env)) {
271 if ((arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1)) == (HCR_NV | HCR_NV1)) {
272 return false;
273 }
274 return env->pstate & PSTATE_PAN;
275 } else {
276 return env->uncached_cpsr & CPSR_PAN;
277 }
278 }
279
280 /*
281 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
282 */
access_el3_aa32ns(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)283 static CPAccessResult access_el3_aa32ns(CPUARMState *env,
284 const ARMCPRegInfo *ri,
285 bool isread)
286 {
287 if (!is_a64(env) && arm_current_el(env) == 3 &&
288 arm_is_secure_below_el3(env)) {
289 return CP_ACCESS_UNDEFINED;
290 }
291 return CP_ACCESS_OK;
292 }
293
294 /*
295 * Some secure-only AArch32 registers trap to EL3 if used from
296 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
297 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
298 * We assume that the .access field is set to PL1_RW.
299 */
access_trap_aa32s_el1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)300 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
301 const ARMCPRegInfo *ri,
302 bool isread)
303 {
304 if (arm_current_el(env) == 3) {
305 return CP_ACCESS_OK;
306 }
307 if (arm_is_secure_below_el3(env)) {
308 if (env->cp15.scr_el3 & SCR_EEL2) {
309 return CP_ACCESS_TRAP_EL2;
310 }
311 return CP_ACCESS_TRAP_EL3;
312 }
313 /* This will be EL1 NS and EL2 NS, which just UNDEF */
314 return CP_ACCESS_UNDEFINED;
315 }
316
317 /*
318 * Check for traps to performance monitor registers, which are controlled
319 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
320 */
access_tpm(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)321 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
322 bool isread)
323 {
324 int el = arm_current_el(env);
325 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
326
327 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
328 return CP_ACCESS_TRAP_EL2;
329 }
330 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
331 return CP_ACCESS_TRAP_EL3;
332 }
333 return CP_ACCESS_OK;
334 }
335
336 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
access_tvm_trvm(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)337 CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
338 bool isread)
339 {
340 if (arm_current_el(env) == 1) {
341 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
342 if (arm_hcr_el2_eff(env) & trap) {
343 return CP_ACCESS_TRAP_EL2;
344 }
345 }
346 return CP_ACCESS_OK;
347 }
348
349 /* Check for traps from EL1 due to HCR_EL2.TSW. */
access_tsw(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)350 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
351 bool isread)
352 {
353 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
354 return CP_ACCESS_TRAP_EL2;
355 }
356 return CP_ACCESS_OK;
357 }
358
359 /* Check for traps from EL1 due to HCR_EL2.TACR. */
access_tacr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)360 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
361 bool isread)
362 {
363 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
364 return CP_ACCESS_TRAP_EL2;
365 }
366 return CP_ACCESS_OK;
367 }
368
dacr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)369 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
370 {
371 ARMCPU *cpu = env_archcpu(env);
372
373 raw_write(env, ri, value);
374 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
375 }
376
fcse_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)377 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
378 {
379 ARMCPU *cpu = env_archcpu(env);
380
381 if (raw_read(env, ri) != value) {
382 /*
383 * Unlike real hardware the qemu TLB uses virtual addresses,
384 * not modified virtual addresses, so this causes a TLB flush.
385 */
386 tlb_flush(CPU(cpu));
387 raw_write(env, ri, value);
388 }
389 }
390
contextidr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)391 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
392 uint64_t value)
393 {
394 ARMCPU *cpu = env_archcpu(env);
395
396 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
397 && !extended_addresses_enabled(env)) {
398 /*
399 * For VMSA (when not using the LPAE long descriptor page table
400 * format) this register includes the ASID, so do a TLB flush.
401 * For PMSA it is purely a process ID and no action is needed.
402 */
403 tlb_flush(CPU(cpu));
404 }
405 raw_write(env, ri, value);
406 }
407
alle1_tlbmask(CPUARMState * env)408 int alle1_tlbmask(CPUARMState *env)
409 {
410 /*
411 * Note that the 'ALL' scope must invalidate both stage 1 and
412 * stage 2 translations, whereas most other scopes only invalidate
413 * stage 1 translations.
414 *
415 * For AArch32 this is only used for TLBIALLNSNH and VTTBR
416 * writes, so only needs to apply to NS PL1&0, not S PL1&0.
417 */
418 return (ARMMMUIdxBit_E10_1 |
419 ARMMMUIdxBit_E10_1_PAN |
420 ARMMMUIdxBit_E10_0 |
421 ARMMMUIdxBit_Stage2 |
422 ARMMMUIdxBit_Stage2_S);
423 }
424
425 static const ARMCPRegInfo cp_reginfo[] = {
426 /*
427 * Define the secure and non-secure FCSE identifier CP registers
428 * separately because there is no secure bank in V8 (no _EL3). This allows
429 * the secure register to be properly reset and migrated. There is also no
430 * v8 EL1 version of the register so the non-secure instance stands alone.
431 */
432 { .name = "FCSEIDR",
433 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
434 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
435 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
436 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
437 { .name = "FCSEIDR_S",
438 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
439 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
440 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
441 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
442 /*
443 * Define the secure and non-secure context identifier CP registers
444 * separately because there is no secure bank in V8 (no _EL3). This allows
445 * the secure register to be properly reset and migrated. In the
446 * non-secure case, the 32-bit register will have reset and migration
447 * disabled during registration as it is handled by the 64-bit instance.
448 */
449 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
450 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
451 .access = PL1_RW, .accessfn = access_tvm_trvm,
452 .fgt = FGT_CONTEXTIDR_EL1,
453 .nv2_redirect_offset = 0x108 | NV2_REDIR_NV1,
454 .secure = ARM_CP_SECSTATE_NS,
455 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
456 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
457 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
458 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
459 .access = PL1_RW, .accessfn = access_tvm_trvm,
460 .secure = ARM_CP_SECSTATE_S,
461 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
462 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
463 };
464
465 static const ARMCPRegInfo not_v8_cp_reginfo[] = {
466 /*
467 * NB: Some of these registers exist in v8 but with more precise
468 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
469 */
470 /* MMU Domain access control / MPU write buffer control */
471 { .name = "DACR",
472 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
473 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
474 .writefn = dacr_write, .raw_writefn = raw_write,
475 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
476 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
477 /*
478 * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
479 * For v6 and v5, these mappings are overly broad.
480 */
481 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
482 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
483 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
484 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
485 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
486 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
487 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
488 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
489 /* Cache maintenance ops; some of this space may be overridden later. */
490 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
491 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
492 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
493 };
494
495 static const ARMCPRegInfo not_v6_cp_reginfo[] = {
496 /*
497 * Not all pre-v6 cores implemented this WFI, so this is slightly
498 * over-broad.
499 */
500 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
501 .access = PL1_W, .type = ARM_CP_WFI },
502 };
503
504 static const ARMCPRegInfo not_v7_cp_reginfo[] = {
505 /*
506 * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
507 * is UNPREDICTABLE; we choose to NOP as most implementations do).
508 */
509 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
510 .access = PL1_W, .type = ARM_CP_WFI },
511 /*
512 * L1 cache lockdown. Not architectural in v6 and earlier but in practice
513 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
514 * OMAPCP will override this space.
515 */
516 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
517 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
518 .resetvalue = 0 },
519 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
520 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
521 .resetvalue = 0 },
522 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
523 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
524 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
525 .resetvalue = 0 },
526 /*
527 * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
528 * implementing it as RAZ means the "debug architecture version" bits
529 * will read as a reserved value, which should cause Linux to not try
530 * to use the debug hardware.
531 */
532 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
533 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
534 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
535 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
536 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
537 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
538 };
539
cpacr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)540 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
541 uint64_t value)
542 {
543 uint32_t mask = 0;
544
545 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
546 if (!arm_feature(env, ARM_FEATURE_V8)) {
547 /*
548 * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
549 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
550 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
551 */
552 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
553 /* VFP coprocessor: cp10 & cp11 [23:20] */
554 mask |= R_CPACR_ASEDIS_MASK |
555 R_CPACR_D32DIS_MASK |
556 R_CPACR_CP11_MASK |
557 R_CPACR_CP10_MASK;
558
559 if (!arm_feature(env, ARM_FEATURE_NEON)) {
560 /* ASEDIS [31] bit is RAO/WI */
561 value |= R_CPACR_ASEDIS_MASK;
562 }
563
564 /*
565 * VFPv3 and upwards with NEON implement 32 double precision
566 * registers (D0-D31).
567 */
568 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
569 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
570 value |= R_CPACR_D32DIS_MASK;
571 }
572 }
573 value &= mask;
574 }
575
576 /*
577 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
578 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
579 */
580 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
581 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
582 mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
583 value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
584 }
585
586 env->cp15.cpacr_el1 = value;
587 }
588
cpacr_read(CPUARMState * env,const ARMCPRegInfo * ri)589 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
590 {
591 /*
592 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
593 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
594 */
595 uint64_t value = env->cp15.cpacr_el1;
596
597 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
598 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
599 value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
600 }
601 return value;
602 }
603
604
cpacr_reset(CPUARMState * env,const ARMCPRegInfo * ri)605 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
606 {
607 /*
608 * Call cpacr_write() so that we reset with the correct RAO bits set
609 * for our CPU features.
610 */
611 cpacr_write(env, ri, 0);
612 }
613
cpacr_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)614 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
615 bool isread)
616 {
617 if (arm_feature(env, ARM_FEATURE_V8)) {
618 /* Check if CPACR accesses are to be trapped to EL2 */
619 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
620 FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
621 return CP_ACCESS_TRAP_EL2;
622 /* Check if CPACR accesses are to be trapped to EL3 */
623 } else if (arm_current_el(env) < 3 &&
624 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
625 return CP_ACCESS_TRAP_EL3;
626 }
627 }
628
629 return CP_ACCESS_OK;
630 }
631
cptr_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)632 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
633 bool isread)
634 {
635 /* Check if CPTR accesses are set to trap to EL3 */
636 if (arm_current_el(env) == 2 &&
637 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
638 return CP_ACCESS_TRAP_EL3;
639 }
640
641 return CP_ACCESS_OK;
642 }
643
644 static const ARMCPRegInfo v6_cp_reginfo[] = {
645 /* prefetch by MVA in v6, NOP in v7 */
646 { .name = "MVA_prefetch",
647 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
648 .access = PL1_W, .type = ARM_CP_NOP },
649 /*
650 * We need to break the TB after ISB to execute self-modifying code
651 * correctly and also to take any pending interrupts immediately.
652 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
653 */
654 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
655 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
656 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
657 .access = PL0_W, .type = ARM_CP_NOP },
658 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
659 .access = PL0_W, .type = ARM_CP_NOP },
660 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
661 .access = PL1_RW, .accessfn = access_tvm_trvm,
662 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
663 offsetof(CPUARMState, cp15.ifar_ns) },
664 .resetvalue = 0, },
665 /*
666 * Watchpoint Fault Address Register : should actually only be present
667 * for 1136, 1176, 11MPCore.
668 */
669 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
670 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
671 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
672 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
673 .fgt = FGT_CPACR_EL1,
674 .nv2_redirect_offset = 0x100 | NV2_REDIR_NV1,
675 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
676 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
677 };
678
679 typedef struct pm_event {
680 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
681 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
682 bool (*supported)(CPUARMState *);
683 /*
684 * Retrieve the current count of the underlying event. The programmed
685 * counters hold a difference from the return value from this function
686 */
687 uint64_t (*get_count)(CPUARMState *);
688 /*
689 * Return how many nanoseconds it will take (at a minimum) for count events
690 * to occur. A negative value indicates the counter will never overflow, or
691 * that the counter has otherwise arranged for the overflow bit to be set
692 * and the PMU interrupt to be raised on overflow.
693 */
694 int64_t (*ns_per_count)(uint64_t);
695 } pm_event;
696
event_always_supported(CPUARMState * env)697 static bool event_always_supported(CPUARMState *env)
698 {
699 return true;
700 }
701
swinc_get_count(CPUARMState * env)702 static uint64_t swinc_get_count(CPUARMState *env)
703 {
704 /*
705 * SW_INCR events are written directly to the pmevcntr's by writes to
706 * PMSWINC, so there is no underlying count maintained by the PMU itself
707 */
708 return 0;
709 }
710
swinc_ns_per(uint64_t ignored)711 static int64_t swinc_ns_per(uint64_t ignored)
712 {
713 return -1;
714 }
715
716 /*
717 * Return the underlying cycle count for the PMU cycle counters. If we're in
718 * usermode, simply return 0.
719 */
cycles_get_count(CPUARMState * env)720 static uint64_t cycles_get_count(CPUARMState *env)
721 {
722 #ifndef CONFIG_USER_ONLY
723 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
724 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
725 #else
726 return cpu_get_host_ticks();
727 #endif
728 }
729
730 #ifndef CONFIG_USER_ONLY
cycles_ns_per(uint64_t cycles)731 static int64_t cycles_ns_per(uint64_t cycles)
732 {
733 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
734 }
735
instructions_supported(CPUARMState * env)736 static bool instructions_supported(CPUARMState *env)
737 {
738 /* Precise instruction counting */
739 return icount_enabled() == ICOUNT_PRECISE;
740 }
741
instructions_get_count(CPUARMState * env)742 static uint64_t instructions_get_count(CPUARMState *env)
743 {
744 assert(icount_enabled() == ICOUNT_PRECISE);
745 return (uint64_t)icount_get_raw();
746 }
747
instructions_ns_per(uint64_t icount)748 static int64_t instructions_ns_per(uint64_t icount)
749 {
750 assert(icount_enabled() == ICOUNT_PRECISE);
751 return icount_to_ns((int64_t)icount);
752 }
753 #endif
754
pmuv3p1_events_supported(CPUARMState * env)755 static bool pmuv3p1_events_supported(CPUARMState *env)
756 {
757 /* For events which are supported in any v8.1 PMU */
758 return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
759 }
760
pmuv3p4_events_supported(CPUARMState * env)761 static bool pmuv3p4_events_supported(CPUARMState *env)
762 {
763 /* For events which are supported in any v8.1 PMU */
764 return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
765 }
766
zero_event_get_count(CPUARMState * env)767 static uint64_t zero_event_get_count(CPUARMState *env)
768 {
769 /* For events which on QEMU never fire, so their count is always zero */
770 return 0;
771 }
772
zero_event_ns_per(uint64_t cycles)773 static int64_t zero_event_ns_per(uint64_t cycles)
774 {
775 /* An event which never fires can never overflow */
776 return -1;
777 }
778
779 static const pm_event pm_events[] = {
780 { .number = 0x000, /* SW_INCR */
781 .supported = event_always_supported,
782 .get_count = swinc_get_count,
783 .ns_per_count = swinc_ns_per,
784 },
785 #ifndef CONFIG_USER_ONLY
786 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
787 .supported = instructions_supported,
788 .get_count = instructions_get_count,
789 .ns_per_count = instructions_ns_per,
790 },
791 { .number = 0x011, /* CPU_CYCLES, Cycle */
792 .supported = event_always_supported,
793 .get_count = cycles_get_count,
794 .ns_per_count = cycles_ns_per,
795 },
796 #endif
797 { .number = 0x023, /* STALL_FRONTEND */
798 .supported = pmuv3p1_events_supported,
799 .get_count = zero_event_get_count,
800 .ns_per_count = zero_event_ns_per,
801 },
802 { .number = 0x024, /* STALL_BACKEND */
803 .supported = pmuv3p1_events_supported,
804 .get_count = zero_event_get_count,
805 .ns_per_count = zero_event_ns_per,
806 },
807 { .number = 0x03c, /* STALL */
808 .supported = pmuv3p4_events_supported,
809 .get_count = zero_event_get_count,
810 .ns_per_count = zero_event_ns_per,
811 },
812 };
813
814 /*
815 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
816 * events (i.e. the statistical profiling extension), this implementation
817 * should first be updated to something sparse instead of the current
818 * supported_event_map[] array.
819 */
820 #define MAX_EVENT_ID 0x3c
821 #define UNSUPPORTED_EVENT UINT16_MAX
822 static uint16_t supported_event_map[MAX_EVENT_ID + 1];
823
824 /*
825 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
826 * of ARM event numbers to indices in our pm_events array.
827 *
828 * Note: Events in the 0x40XX range are not currently supported.
829 */
pmu_init(ARMCPU * cpu)830 void pmu_init(ARMCPU *cpu)
831 {
832 unsigned int i;
833
834 /*
835 * Empty supported_event_map and cpu->pmceid[01] before adding supported
836 * events to them
837 */
838 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
839 supported_event_map[i] = UNSUPPORTED_EVENT;
840 }
841 cpu->pmceid0 = 0;
842 cpu->pmceid1 = 0;
843
844 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
845 const pm_event *cnt = &pm_events[i];
846 assert(cnt->number <= MAX_EVENT_ID);
847 /* We do not currently support events in the 0x40xx range */
848 assert(cnt->number <= 0x3f);
849
850 if (cnt->supported(&cpu->env)) {
851 supported_event_map[cnt->number] = i;
852 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
853 if (cnt->number & 0x20) {
854 cpu->pmceid1 |= event_mask;
855 } else {
856 cpu->pmceid0 |= event_mask;
857 }
858 }
859 }
860 }
861
862 /*
863 * Check at runtime whether a PMU event is supported for the current machine
864 */
event_supported(uint16_t number)865 static bool event_supported(uint16_t number)
866 {
867 if (number > MAX_EVENT_ID) {
868 return false;
869 }
870 return supported_event_map[number] != UNSUPPORTED_EVENT;
871 }
872
pmreg_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)873 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
874 bool isread)
875 {
876 /*
877 * Performance monitor registers user accessibility is controlled
878 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
879 * trapping to EL2 or EL3 for other accesses.
880 */
881 int el = arm_current_el(env);
882 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
883
884 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
885 return CP_ACCESS_TRAP_EL1;
886 }
887 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
888 return CP_ACCESS_TRAP_EL2;
889 }
890 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
891 return CP_ACCESS_TRAP_EL3;
892 }
893
894 return CP_ACCESS_OK;
895 }
896
pmreg_access_xevcntr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)897 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
898 const ARMCPRegInfo *ri,
899 bool isread)
900 {
901 /* ER: event counter read trap control */
902 if (arm_feature(env, ARM_FEATURE_V8)
903 && arm_current_el(env) == 0
904 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
905 && isread) {
906 return CP_ACCESS_OK;
907 }
908
909 return pmreg_access(env, ri, isread);
910 }
911
pmreg_access_swinc(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)912 static CPAccessResult pmreg_access_swinc(CPUARMState *env,
913 const ARMCPRegInfo *ri,
914 bool isread)
915 {
916 /* SW: software increment write trap control */
917 if (arm_feature(env, ARM_FEATURE_V8)
918 && arm_current_el(env) == 0
919 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
920 && !isread) {
921 return CP_ACCESS_OK;
922 }
923
924 return pmreg_access(env, ri, isread);
925 }
926
pmreg_access_selr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)927 static CPAccessResult pmreg_access_selr(CPUARMState *env,
928 const ARMCPRegInfo *ri,
929 bool isread)
930 {
931 /* ER: event counter read trap control */
932 if (arm_feature(env, ARM_FEATURE_V8)
933 && arm_current_el(env) == 0
934 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
935 return CP_ACCESS_OK;
936 }
937
938 return pmreg_access(env, ri, isread);
939 }
940
pmreg_access_ccntr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)941 static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
942 const ARMCPRegInfo *ri,
943 bool isread)
944 {
945 /* CR: cycle counter read trap control */
946 if (arm_feature(env, ARM_FEATURE_V8)
947 && arm_current_el(env) == 0
948 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
949 && isread) {
950 return CP_ACCESS_OK;
951 }
952
953 return pmreg_access(env, ri, isread);
954 }
955
956 /*
957 * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
958 * We use these to decide whether we need to wrap a write to MDCR_EL2
959 * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
960 */
961 #define MDCR_EL2_PMU_ENABLE_BITS \
962 (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
963 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
964
965 /*
966 * Returns true if the counter (pass 31 for PMCCNTR) should count events using
967 * the current EL, security state, and register configuration.
968 */
pmu_counter_enabled(CPUARMState * env,uint8_t counter)969 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
970 {
971 uint64_t filter;
972 bool e, p, u, nsk, nsu, nsh, m;
973 bool enabled, prohibited = false, filtered;
974 bool secure = arm_is_secure(env);
975 int el = arm_current_el(env);
976 uint64_t mdcr_el2;
977 uint8_t hpmn;
978
979 /*
980 * We might be called for M-profile cores where MDCR_EL2 doesn't
981 * exist and arm_mdcr_el2_eff() will assert, so this early-exit check
982 * must be before we read that value.
983 */
984 if (!arm_feature(env, ARM_FEATURE_PMU)) {
985 return false;
986 }
987
988 mdcr_el2 = arm_mdcr_el2_eff(env);
989 hpmn = mdcr_el2 & MDCR_HPMN;
990
991 if (!arm_feature(env, ARM_FEATURE_EL2) ||
992 (counter < hpmn || counter == 31)) {
993 e = env->cp15.c9_pmcr & PMCRE;
994 } else {
995 e = mdcr_el2 & MDCR_HPME;
996 }
997 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
998
999 /* Is event counting prohibited? */
1000 if (el == 2 && (counter < hpmn || counter == 31)) {
1001 prohibited = mdcr_el2 & MDCR_HPMD;
1002 }
1003 if (secure) {
1004 prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
1005 }
1006
1007 if (counter == 31) {
1008 /*
1009 * The cycle counter defaults to running. PMCR.DP says "disable
1010 * the cycle counter when event counting is prohibited".
1011 * Some MDCR bits disable the cycle counter specifically.
1012 */
1013 prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1014 if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1015 if (secure) {
1016 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1017 }
1018 if (el == 2) {
1019 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1020 }
1021 }
1022 }
1023
1024 if (counter == 31) {
1025 filter = env->cp15.pmccfiltr_el0;
1026 } else {
1027 filter = env->cp15.c14_pmevtyper[counter];
1028 }
1029
1030 p = filter & PMXEVTYPER_P;
1031 u = filter & PMXEVTYPER_U;
1032 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1033 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1034 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1035 m = arm_el_is_aa64(env, 1) &&
1036 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1037
1038 if (el == 0) {
1039 filtered = secure ? u : u != nsu;
1040 } else if (el == 1) {
1041 filtered = secure ? p : p != nsk;
1042 } else if (el == 2) {
1043 filtered = !nsh;
1044 } else { /* EL3 */
1045 filtered = m != p;
1046 }
1047
1048 if (counter != 31) {
1049 /*
1050 * If not checking PMCCNTR, ensure the counter is setup to an event we
1051 * support
1052 */
1053 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1054 if (!event_supported(event)) {
1055 return false;
1056 }
1057 }
1058
1059 return enabled && !prohibited && !filtered;
1060 }
1061
pmu_update_irq(CPUARMState * env)1062 static void pmu_update_irq(CPUARMState *env)
1063 {
1064 ARMCPU *cpu = env_archcpu(env);
1065 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1066 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1067 }
1068
pmccntr_clockdiv_enabled(CPUARMState * env)1069 static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1070 {
1071 /*
1072 * Return true if the clock divider is enabled and the cycle counter
1073 * is supposed to tick only once every 64 clock cycles. This is
1074 * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1075 * (64-bit) cycle counter PMCR.D has no effect.
1076 */
1077 return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1078 }
1079
pmevcntr_is_64_bit(CPUARMState * env,int counter)1080 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1081 {
1082 /* Return true if the specified event counter is configured to be 64 bit */
1083
1084 /* This isn't intended to be used with the cycle counter */
1085 assert(counter < 31);
1086
1087 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1088 return false;
1089 }
1090
1091 if (arm_feature(env, ARM_FEATURE_EL2)) {
1092 /*
1093 * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1094 * current security state, so we don't use arm_mdcr_el2_eff() here.
1095 */
1096 bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1097 int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1098
1099 if (counter >= hpmn) {
1100 return hlp;
1101 }
1102 }
1103 return env->cp15.c9_pmcr & PMCRLP;
1104 }
1105
1106 /*
1107 * Ensure c15_ccnt is the guest-visible count so that operations such as
1108 * enabling/disabling the counter or filtering, modifying the count itself,
1109 * etc. can be done logically. This is essentially a no-op if the counter is
1110 * not enabled at the time of the call.
1111 */
pmccntr_op_start(CPUARMState * env)1112 static void pmccntr_op_start(CPUARMState *env)
1113 {
1114 uint64_t cycles = cycles_get_count(env);
1115
1116 if (pmu_counter_enabled(env, 31)) {
1117 uint64_t eff_cycles = cycles;
1118 if (pmccntr_clockdiv_enabled(env)) {
1119 eff_cycles /= 64;
1120 }
1121
1122 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1123
1124 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1125 1ull << 63 : 1ull << 31;
1126 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
1127 env->cp15.c9_pmovsr |= (1ULL << 31);
1128 pmu_update_irq(env);
1129 }
1130
1131 env->cp15.c15_ccnt = new_pmccntr;
1132 }
1133 env->cp15.c15_ccnt_delta = cycles;
1134 }
1135
1136 /*
1137 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1138 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1139 * pmccntr_op_start.
1140 */
pmccntr_op_finish(CPUARMState * env)1141 static void pmccntr_op_finish(CPUARMState *env)
1142 {
1143 if (pmu_counter_enabled(env, 31)) {
1144 #ifndef CONFIG_USER_ONLY
1145 /* Calculate when the counter will next overflow */
1146 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1147 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1148 remaining_cycles = (uint32_t)remaining_cycles;
1149 }
1150 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1151
1152 if (overflow_in > 0) {
1153 int64_t overflow_at;
1154
1155 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1156 overflow_in, &overflow_at)) {
1157 ARMCPU *cpu = env_archcpu(env);
1158 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1159 }
1160 }
1161 #endif
1162
1163 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
1164 if (pmccntr_clockdiv_enabled(env)) {
1165 prev_cycles /= 64;
1166 }
1167 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
1168 }
1169 }
1170
pmevcntr_op_start(CPUARMState * env,uint8_t counter)1171 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1172 {
1173
1174 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1175 uint64_t count = 0;
1176 if (event_supported(event)) {
1177 uint16_t event_idx = supported_event_map[event];
1178 count = pm_events[event_idx].get_count(env);
1179 }
1180
1181 if (pmu_counter_enabled(env, counter)) {
1182 uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1183 uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1184 1ULL << 63 : 1ULL << 31;
1185
1186 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
1187 env->cp15.c9_pmovsr |= (1 << counter);
1188 pmu_update_irq(env);
1189 }
1190 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
1191 }
1192 env->cp15.c14_pmevcntr_delta[counter] = count;
1193 }
1194
pmevcntr_op_finish(CPUARMState * env,uint8_t counter)1195 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1196 {
1197 if (pmu_counter_enabled(env, counter)) {
1198 #ifndef CONFIG_USER_ONLY
1199 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1200 uint16_t event_idx = supported_event_map[event];
1201 uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1202 int64_t overflow_in;
1203
1204 if (!pmevcntr_is_64_bit(env, counter)) {
1205 delta = (uint32_t)delta;
1206 }
1207 overflow_in = pm_events[event_idx].ns_per_count(delta);
1208
1209 if (overflow_in > 0) {
1210 int64_t overflow_at;
1211
1212 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1213 overflow_in, &overflow_at)) {
1214 ARMCPU *cpu = env_archcpu(env);
1215 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1216 }
1217 }
1218 #endif
1219
1220 env->cp15.c14_pmevcntr_delta[counter] -=
1221 env->cp15.c14_pmevcntr[counter];
1222 }
1223 }
1224
pmu_op_start(CPUARMState * env)1225 void pmu_op_start(CPUARMState *env)
1226 {
1227 unsigned int i;
1228 pmccntr_op_start(env);
1229 for (i = 0; i < pmu_num_counters(env); i++) {
1230 pmevcntr_op_start(env, i);
1231 }
1232 }
1233
pmu_op_finish(CPUARMState * env)1234 void pmu_op_finish(CPUARMState *env)
1235 {
1236 unsigned int i;
1237 pmccntr_op_finish(env);
1238 for (i = 0; i < pmu_num_counters(env); i++) {
1239 pmevcntr_op_finish(env, i);
1240 }
1241 }
1242
pmu_pre_el_change(ARMCPU * cpu,void * ignored)1243 void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1244 {
1245 pmu_op_start(&cpu->env);
1246 }
1247
pmu_post_el_change(ARMCPU * cpu,void * ignored)1248 void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1249 {
1250 pmu_op_finish(&cpu->env);
1251 }
1252
arm_pmu_timer_cb(void * opaque)1253 void arm_pmu_timer_cb(void *opaque)
1254 {
1255 ARMCPU *cpu = opaque;
1256
1257 /*
1258 * Update all the counter values based on the current underlying counts,
1259 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1260 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1261 * counter may expire.
1262 */
1263 pmu_op_start(&cpu->env);
1264 pmu_op_finish(&cpu->env);
1265 }
1266
pmcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1267 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1268 uint64_t value)
1269 {
1270 pmu_op_start(env);
1271
1272 if (value & PMCRC) {
1273 /* The counter has been reset */
1274 env->cp15.c15_ccnt = 0;
1275 }
1276
1277 if (value & PMCRP) {
1278 unsigned int i;
1279 for (i = 0; i < pmu_num_counters(env); i++) {
1280 env->cp15.c14_pmevcntr[i] = 0;
1281 }
1282 }
1283
1284 env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1285 env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
1286
1287 pmu_op_finish(env);
1288 }
1289
pmcr_read(CPUARMState * env,const ARMCPRegInfo * ri)1290 static uint64_t pmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1291 {
1292 uint64_t pmcr = env->cp15.c9_pmcr;
1293
1294 /*
1295 * If EL2 is implemented and enabled for the current security state, reads
1296 * of PMCR.N from EL1 or EL0 return the value of MDCR_EL2.HPMN or HDCR.HPMN.
1297 */
1298 if (arm_current_el(env) <= 1 && arm_is_el2_enabled(env)) {
1299 pmcr &= ~PMCRN_MASK;
1300 pmcr |= (env->cp15.mdcr_el2 & MDCR_HPMN) << PMCRN_SHIFT;
1301 }
1302
1303 return pmcr;
1304 }
1305
pmswinc_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1306 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1307 uint64_t value)
1308 {
1309 unsigned int i;
1310 uint64_t overflow_mask, new_pmswinc;
1311
1312 for (i = 0; i < pmu_num_counters(env); i++) {
1313 /* Increment a counter's count iff: */
1314 if ((value & (1 << i)) && /* counter's bit is set */
1315 /* counter is enabled and not filtered */
1316 pmu_counter_enabled(env, i) &&
1317 /* counter is SW_INCR */
1318 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1319 pmevcntr_op_start(env, i);
1320
1321 /*
1322 * Detect if this write causes an overflow since we can't predict
1323 * PMSWINC overflows like we can for other events
1324 */
1325 new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1326
1327 overflow_mask = pmevcntr_is_64_bit(env, i) ?
1328 1ULL << 63 : 1ULL << 31;
1329
1330 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
1331 env->cp15.c9_pmovsr |= (1 << i);
1332 pmu_update_irq(env);
1333 }
1334
1335 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1336
1337 pmevcntr_op_finish(env, i);
1338 }
1339 }
1340 }
1341
pmccntr_read(CPUARMState * env,const ARMCPRegInfo * ri)1342 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1343 {
1344 uint64_t ret;
1345 pmccntr_op_start(env);
1346 ret = env->cp15.c15_ccnt;
1347 pmccntr_op_finish(env);
1348 return ret;
1349 }
1350
pmselr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1351 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1352 uint64_t value)
1353 {
1354 /*
1355 * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
1356 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1357 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1358 * accessed.
1359 */
1360 env->cp15.c9_pmselr = value & 0x1f;
1361 }
1362
pmccntr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1363 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1364 uint64_t value)
1365 {
1366 pmccntr_op_start(env);
1367 env->cp15.c15_ccnt = value;
1368 pmccntr_op_finish(env);
1369 }
1370
pmccntr_write32(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1371 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1372 uint64_t value)
1373 {
1374 uint64_t cur_val = pmccntr_read(env, NULL);
1375
1376 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1377 }
1378
pmccfiltr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1379 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1380 uint64_t value)
1381 {
1382 pmccntr_op_start(env);
1383 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1384 pmccntr_op_finish(env);
1385 }
1386
pmccfiltr_write_a32(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1387 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1388 uint64_t value)
1389 {
1390 pmccntr_op_start(env);
1391 /* M is not accessible from AArch32 */
1392 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1393 (value & PMCCFILTR);
1394 pmccntr_op_finish(env);
1395 }
1396
pmccfiltr_read_a32(CPUARMState * env,const ARMCPRegInfo * ri)1397 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1398 {
1399 /* M is not visible in AArch32 */
1400 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1401 }
1402
pmcntenset_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1403 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1404 uint64_t value)
1405 {
1406 pmu_op_start(env);
1407 value &= pmu_counter_mask(env);
1408 env->cp15.c9_pmcnten |= value;
1409 pmu_op_finish(env);
1410 }
1411
pmcntenclr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1412 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1413 uint64_t value)
1414 {
1415 pmu_op_start(env);
1416 value &= pmu_counter_mask(env);
1417 env->cp15.c9_pmcnten &= ~value;
1418 pmu_op_finish(env);
1419 }
1420
pmovsr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1421 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1422 uint64_t value)
1423 {
1424 value &= pmu_counter_mask(env);
1425 env->cp15.c9_pmovsr &= ~value;
1426 pmu_update_irq(env);
1427 }
1428
pmovsset_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1429 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1430 uint64_t value)
1431 {
1432 value &= pmu_counter_mask(env);
1433 env->cp15.c9_pmovsr |= value;
1434 pmu_update_irq(env);
1435 }
1436
pmevtyper_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value,const uint8_t counter)1437 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1438 uint64_t value, const uint8_t counter)
1439 {
1440 if (counter == 31) {
1441 pmccfiltr_write(env, ri, value);
1442 } else if (counter < pmu_num_counters(env)) {
1443 pmevcntr_op_start(env, counter);
1444
1445 /*
1446 * If this counter's event type is changing, store the current
1447 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1448 * pmevcntr_op_finish has the correct baseline when it converts back to
1449 * a delta.
1450 */
1451 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1452 PMXEVTYPER_EVTCOUNT;
1453 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1454 if (old_event != new_event) {
1455 uint64_t count = 0;
1456 if (event_supported(new_event)) {
1457 uint16_t event_idx = supported_event_map[new_event];
1458 count = pm_events[event_idx].get_count(env);
1459 }
1460 env->cp15.c14_pmevcntr_delta[counter] = count;
1461 }
1462
1463 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1464 pmevcntr_op_finish(env, counter);
1465 }
1466 /*
1467 * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
1468 * PMSELR value is equal to or greater than the number of implemented
1469 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1470 */
1471 }
1472
pmevtyper_read(CPUARMState * env,const ARMCPRegInfo * ri,const uint8_t counter)1473 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1474 const uint8_t counter)
1475 {
1476 if (counter == 31) {
1477 return env->cp15.pmccfiltr_el0;
1478 } else if (counter < pmu_num_counters(env)) {
1479 return env->cp15.c14_pmevtyper[counter];
1480 } else {
1481 /*
1482 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1483 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1484 */
1485 return 0;
1486 }
1487 }
1488
pmevtyper_writefn(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1489 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1490 uint64_t value)
1491 {
1492 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1493 pmevtyper_write(env, ri, value, counter);
1494 }
1495
pmevtyper_rawwrite(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1496 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1497 uint64_t value)
1498 {
1499 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1500 env->cp15.c14_pmevtyper[counter] = value;
1501
1502 /*
1503 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1504 * pmu_op_finish calls when loading saved state for a migration. Because
1505 * we're potentially updating the type of event here, the value written to
1506 * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a
1507 * different counter type. Therefore, we need to set this value to the
1508 * current count for the counter type we're writing so that pmu_op_finish
1509 * has the correct count for its calculation.
1510 */
1511 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1512 if (event_supported(event)) {
1513 uint16_t event_idx = supported_event_map[event];
1514 env->cp15.c14_pmevcntr_delta[counter] =
1515 pm_events[event_idx].get_count(env);
1516 }
1517 }
1518
pmevtyper_readfn(CPUARMState * env,const ARMCPRegInfo * ri)1519 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1520 {
1521 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1522 return pmevtyper_read(env, ri, counter);
1523 }
1524
pmxevtyper_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1525 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1526 uint64_t value)
1527 {
1528 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1529 }
1530
pmxevtyper_read(CPUARMState * env,const ARMCPRegInfo * ri)1531 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1532 {
1533 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1534 }
1535
pmevcntr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value,uint8_t counter)1536 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1537 uint64_t value, uint8_t counter)
1538 {
1539 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1540 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1541 value &= MAKE_64BIT_MASK(0, 32);
1542 }
1543 if (counter < pmu_num_counters(env)) {
1544 pmevcntr_op_start(env, counter);
1545 env->cp15.c14_pmevcntr[counter] = value;
1546 pmevcntr_op_finish(env, counter);
1547 }
1548 /*
1549 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1550 * are CONSTRAINED UNPREDICTABLE.
1551 */
1552 }
1553
pmevcntr_read(CPUARMState * env,const ARMCPRegInfo * ri,uint8_t counter)1554 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1555 uint8_t counter)
1556 {
1557 if (counter < pmu_num_counters(env)) {
1558 uint64_t ret;
1559 pmevcntr_op_start(env, counter);
1560 ret = env->cp15.c14_pmevcntr[counter];
1561 pmevcntr_op_finish(env, counter);
1562 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1563 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1564 ret &= MAKE_64BIT_MASK(0, 32);
1565 }
1566 return ret;
1567 } else {
1568 /*
1569 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1570 * are CONSTRAINED UNPREDICTABLE.
1571 */
1572 return 0;
1573 }
1574 }
1575
pmevcntr_writefn(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1576 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1577 uint64_t value)
1578 {
1579 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1580 pmevcntr_write(env, ri, value, counter);
1581 }
1582
pmevcntr_readfn(CPUARMState * env,const ARMCPRegInfo * ri)1583 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1584 {
1585 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1586 return pmevcntr_read(env, ri, counter);
1587 }
1588
pmevcntr_rawwrite(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1589 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1590 uint64_t value)
1591 {
1592 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1593 assert(counter < pmu_num_counters(env));
1594 env->cp15.c14_pmevcntr[counter] = value;
1595 pmevcntr_write(env, ri, value, counter);
1596 }
1597
pmevcntr_rawread(CPUARMState * env,const ARMCPRegInfo * ri)1598 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1599 {
1600 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1601 assert(counter < pmu_num_counters(env));
1602 return env->cp15.c14_pmevcntr[counter];
1603 }
1604
pmxevcntr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1605 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1606 uint64_t value)
1607 {
1608 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1609 }
1610
pmxevcntr_read(CPUARMState * env,const ARMCPRegInfo * ri)1611 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1612 {
1613 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1614 }
1615
pmuserenr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1616 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1617 uint64_t value)
1618 {
1619 if (arm_feature(env, ARM_FEATURE_V8)) {
1620 env->cp15.c9_pmuserenr = value & 0xf;
1621 } else {
1622 env->cp15.c9_pmuserenr = value & 1;
1623 }
1624 }
1625
pmintenset_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1626 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1627 uint64_t value)
1628 {
1629 /* We have no event counters so only the C bit can be changed */
1630 value &= pmu_counter_mask(env);
1631 env->cp15.c9_pminten |= value;
1632 pmu_update_irq(env);
1633 }
1634
pmintenclr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1635 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1636 uint64_t value)
1637 {
1638 value &= pmu_counter_mask(env);
1639 env->cp15.c9_pminten &= ~value;
1640 pmu_update_irq(env);
1641 }
1642
vbar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1643 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1644 uint64_t value)
1645 {
1646 /*
1647 * Note that even though the AArch64 view of this register has bits
1648 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1649 * architectural requirements for bits which are RES0 only in some
1650 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1651 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1652 */
1653 raw_write(env, ri, value & ~0x1FULL);
1654 }
1655
scr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1656 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1657 {
1658 /* Begin with base v8.0 state. */
1659 uint64_t valid_mask = 0x3fff;
1660 ARMCPU *cpu = env_archcpu(env);
1661 uint64_t changed;
1662
1663 /*
1664 * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1665 * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1666 * Instead, choose the format based on the mode of EL3.
1667 */
1668 if (arm_el_is_aa64(env, 3)) {
1669 value |= SCR_FW | SCR_AW; /* RES1 */
1670 valid_mask &= ~SCR_NET; /* RES0 */
1671
1672 if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1673 !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1674 value |= SCR_RW; /* RAO/WI */
1675 }
1676 if (cpu_isar_feature(aa64_ras, cpu)) {
1677 valid_mask |= SCR_TERR;
1678 }
1679 if (cpu_isar_feature(aa64_lor, cpu)) {
1680 valid_mask |= SCR_TLOR;
1681 }
1682 if (cpu_isar_feature(aa64_pauth, cpu)) {
1683 valid_mask |= SCR_API | SCR_APK;
1684 }
1685 if (cpu_isar_feature(aa64_sel2, cpu)) {
1686 valid_mask |= SCR_EEL2;
1687 } else if (cpu_isar_feature(aa64_rme, cpu)) {
1688 /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */
1689 value |= SCR_NS;
1690 }
1691 if (cpu_isar_feature(aa64_mte, cpu)) {
1692 valid_mask |= SCR_ATA;
1693 }
1694 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1695 valid_mask |= SCR_ENSCXT;
1696 }
1697 if (cpu_isar_feature(aa64_doublefault, cpu)) {
1698 valid_mask |= SCR_EASE | SCR_NMEA;
1699 }
1700 if (cpu_isar_feature(aa64_sme, cpu)) {
1701 valid_mask |= SCR_ENTP2;
1702 }
1703 if (cpu_isar_feature(aa64_hcx, cpu)) {
1704 valid_mask |= SCR_HXEN;
1705 }
1706 if (cpu_isar_feature(aa64_fgt, cpu)) {
1707 valid_mask |= SCR_FGTEN;
1708 }
1709 if (cpu_isar_feature(aa64_rme, cpu)) {
1710 valid_mask |= SCR_NSE | SCR_GPF;
1711 }
1712 if (cpu_isar_feature(aa64_ecv, cpu)) {
1713 valid_mask |= SCR_ECVEN;
1714 }
1715 } else {
1716 valid_mask &= ~(SCR_RW | SCR_ST);
1717 if (cpu_isar_feature(aa32_ras, cpu)) {
1718 valid_mask |= SCR_TERR;
1719 }
1720 }
1721
1722 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1723 valid_mask &= ~SCR_HCE;
1724
1725 /*
1726 * On ARMv7, SMD (or SCD as it is called in v7) is only
1727 * supported if EL2 exists. The bit is UNK/SBZP when
1728 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1729 * when EL2 is unavailable.
1730 * On ARMv8, this bit is always available.
1731 */
1732 if (arm_feature(env, ARM_FEATURE_V7) &&
1733 !arm_feature(env, ARM_FEATURE_V8)) {
1734 valid_mask &= ~SCR_SMD;
1735 }
1736 }
1737
1738 /* Clear all-context RES0 bits. */
1739 value &= valid_mask;
1740 changed = env->cp15.scr_el3 ^ value;
1741 env->cp15.scr_el3 = value;
1742
1743 /*
1744 * If SCR_EL3.{NS,NSE} changes, i.e. change of security state,
1745 * we must invalidate all TLBs below EL3.
1746 */
1747 if (changed & (SCR_NS | SCR_NSE)) {
1748 tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1749 ARMMMUIdxBit_E20_0 |
1750 ARMMMUIdxBit_E10_1 |
1751 ARMMMUIdxBit_E20_2 |
1752 ARMMMUIdxBit_E10_1_PAN |
1753 ARMMMUIdxBit_E20_2_PAN |
1754 ARMMMUIdxBit_E2));
1755 }
1756 }
1757
scr_reset(CPUARMState * env,const ARMCPRegInfo * ri)1758 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1759 {
1760 /*
1761 * scr_write will set the RES1 bits on an AArch64-only CPU.
1762 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1763 */
1764 scr_write(env, ri, 0);
1765 }
1766
access_tid4(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)1767 static CPAccessResult access_tid4(CPUARMState *env,
1768 const ARMCPRegInfo *ri,
1769 bool isread)
1770 {
1771 if (arm_current_el(env) == 1 &&
1772 (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
1773 return CP_ACCESS_TRAP_EL2;
1774 }
1775
1776 return CP_ACCESS_OK;
1777 }
1778
ccsidr_read(CPUARMState * env,const ARMCPRegInfo * ri)1779 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1780 {
1781 ARMCPU *cpu = env_archcpu(env);
1782
1783 /*
1784 * Acquire the CSSELR index from the bank corresponding to the CCSIDR
1785 * bank
1786 */
1787 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1788 ri->secure & ARM_CP_SECSTATE_S);
1789
1790 return cpu->ccsidr[index];
1791 }
1792
csselr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)1793 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1794 uint64_t value)
1795 {
1796 raw_write(env, ri, value & 0xf);
1797 }
1798
isr_read(CPUARMState * env,const ARMCPRegInfo * ri)1799 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1800 {
1801 CPUState *cs = env_cpu(env);
1802 bool el1 = arm_current_el(env) == 1;
1803 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1804 uint64_t ret = 0;
1805
1806 if (hcr_el2 & HCR_IMO) {
1807 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1808 ret |= CPSR_I;
1809 }
1810 if (cs->interrupt_request & CPU_INTERRUPT_VINMI) {
1811 ret |= ISR_IS;
1812 ret |= CPSR_I;
1813 }
1814 } else {
1815 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1816 ret |= CPSR_I;
1817 }
1818
1819 if (cs->interrupt_request & CPU_INTERRUPT_NMI) {
1820 ret |= ISR_IS;
1821 ret |= CPSR_I;
1822 }
1823 }
1824
1825 if (hcr_el2 & HCR_FMO) {
1826 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1827 ret |= CPSR_F;
1828 }
1829 if (cs->interrupt_request & CPU_INTERRUPT_VFNMI) {
1830 ret |= ISR_FS;
1831 ret |= CPSR_F;
1832 }
1833 } else {
1834 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1835 ret |= CPSR_F;
1836 }
1837 }
1838
1839 if (hcr_el2 & HCR_AMO) {
1840 if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1841 ret |= CPSR_A;
1842 }
1843 }
1844
1845 return ret;
1846 }
1847
access_aa64_tid1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)1848 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1849 bool isread)
1850 {
1851 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
1852 return CP_ACCESS_TRAP_EL2;
1853 }
1854
1855 return CP_ACCESS_OK;
1856 }
1857
access_aa32_tid1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)1858 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1859 bool isread)
1860 {
1861 if (arm_feature(env, ARM_FEATURE_V8)) {
1862 return access_aa64_tid1(env, ri, isread);
1863 }
1864
1865 return CP_ACCESS_OK;
1866 }
1867
1868 static const ARMCPRegInfo v7_cp_reginfo[] = {
1869 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1870 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1871 .access = PL1_W, .type = ARM_CP_NOP },
1872 /*
1873 * Performance monitors are implementation defined in v7,
1874 * but with an ARM recommended set of registers, which we
1875 * follow.
1876 *
1877 * Performance registers fall into three categories:
1878 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1879 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1880 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1881 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1882 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1883 */
1884 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
1885 .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
1886 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1887 .writefn = pmcntenset_write,
1888 .accessfn = pmreg_access,
1889 .fgt = FGT_PMCNTEN,
1890 .raw_writefn = raw_write },
1891 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
1892 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1893 .access = PL0_RW, .accessfn = pmreg_access,
1894 .fgt = FGT_PMCNTEN,
1895 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1896 .writefn = pmcntenset_write, .raw_writefn = raw_write },
1897 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
1898 .access = PL0_RW,
1899 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
1900 .accessfn = pmreg_access,
1901 .fgt = FGT_PMCNTEN,
1902 .writefn = pmcntenclr_write,
1903 .type = ARM_CP_ALIAS | ARM_CP_IO },
1904 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1905 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1906 .access = PL0_RW, .accessfn = pmreg_access,
1907 .fgt = FGT_PMCNTEN,
1908 .type = ARM_CP_ALIAS | ARM_CP_IO,
1909 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1910 .writefn = pmcntenclr_write },
1911 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1912 .access = PL0_RW, .type = ARM_CP_IO,
1913 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
1914 .accessfn = pmreg_access,
1915 .fgt = FGT_PMOVS,
1916 .writefn = pmovsr_write,
1917 .raw_writefn = raw_write },
1918 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1919 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1920 .access = PL0_RW, .accessfn = pmreg_access,
1921 .fgt = FGT_PMOVS,
1922 .type = ARM_CP_ALIAS | ARM_CP_IO,
1923 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1924 .writefn = pmovsr_write,
1925 .raw_writefn = raw_write },
1926 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
1927 .access = PL0_W, .accessfn = pmreg_access_swinc,
1928 .fgt = FGT_PMSWINC_EL0,
1929 .type = ARM_CP_NO_RAW | ARM_CP_IO,
1930 .writefn = pmswinc_write },
1931 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
1932 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
1933 .access = PL0_W, .accessfn = pmreg_access_swinc,
1934 .fgt = FGT_PMSWINC_EL0,
1935 .type = ARM_CP_NO_RAW | ARM_CP_IO,
1936 .writefn = pmswinc_write },
1937 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
1938 .access = PL0_RW, .type = ARM_CP_ALIAS,
1939 .fgt = FGT_PMSELR_EL0,
1940 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
1941 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
1942 .raw_writefn = raw_write},
1943 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
1944 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
1945 .access = PL0_RW, .accessfn = pmreg_access_selr,
1946 .fgt = FGT_PMSELR_EL0,
1947 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
1948 .writefn = pmselr_write, .raw_writefn = raw_write, },
1949 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
1950 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
1951 .fgt = FGT_PMCCNTR_EL0,
1952 .readfn = pmccntr_read, .writefn = pmccntr_write32,
1953 .accessfn = pmreg_access_ccntr },
1954 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1955 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1956 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
1957 .fgt = FGT_PMCCNTR_EL0,
1958 .type = ARM_CP_IO,
1959 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
1960 .readfn = pmccntr_read, .writefn = pmccntr_write,
1961 .raw_readfn = raw_read, .raw_writefn = raw_write, },
1962 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
1963 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
1964 .access = PL0_RW, .accessfn = pmreg_access,
1965 .fgt = FGT_PMCCFILTR_EL0,
1966 .type = ARM_CP_ALIAS | ARM_CP_IO,
1967 .resetvalue = 0, },
1968 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1969 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
1970 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
1971 .access = PL0_RW, .accessfn = pmreg_access,
1972 .fgt = FGT_PMCCFILTR_EL0,
1973 .type = ARM_CP_IO,
1974 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1975 .resetvalue = 0, },
1976 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1977 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1978 .accessfn = pmreg_access,
1979 .fgt = FGT_PMEVTYPERN_EL0,
1980 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1981 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
1982 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
1983 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1984 .accessfn = pmreg_access,
1985 .fgt = FGT_PMEVTYPERN_EL0,
1986 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
1987 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
1988 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1989 .accessfn = pmreg_access_xevcntr,
1990 .fgt = FGT_PMEVCNTRN_EL0,
1991 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
1992 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
1993 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
1994 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
1995 .accessfn = pmreg_access_xevcntr,
1996 .fgt = FGT_PMEVCNTRN_EL0,
1997 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
1998 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1999 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
2000 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
2001 .resetvalue = 0,
2002 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2003 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2004 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
2005 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
2006 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2007 .resetvalue = 0,
2008 .writefn = pmuserenr_write, .raw_writefn = raw_write },
2009 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
2010 .access = PL1_RW, .accessfn = access_tpm,
2011 .fgt = FGT_PMINTEN,
2012 .type = ARM_CP_ALIAS | ARM_CP_IO,
2013 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
2014 .resetvalue = 0,
2015 .writefn = pmintenset_write, .raw_writefn = raw_write },
2016 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2017 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2018 .access = PL1_RW, .accessfn = access_tpm,
2019 .fgt = FGT_PMINTEN,
2020 .type = ARM_CP_IO,
2021 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2022 .writefn = pmintenset_write, .raw_writefn = raw_write,
2023 .resetvalue = 0x0 },
2024 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
2025 .access = PL1_RW, .accessfn = access_tpm,
2026 .fgt = FGT_PMINTEN,
2027 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2028 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2029 .writefn = pmintenclr_write, },
2030 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2031 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
2032 .access = PL1_RW, .accessfn = access_tpm,
2033 .fgt = FGT_PMINTEN,
2034 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
2035 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2036 .writefn = pmintenclr_write },
2037 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2038 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
2039 .access = PL1_R,
2040 .accessfn = access_tid4,
2041 .fgt = FGT_CCSIDR_EL1,
2042 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
2043 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2044 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
2045 .access = PL1_RW,
2046 .accessfn = access_tid4,
2047 .fgt = FGT_CSSELR_EL1,
2048 .writefn = csselr_write, .resetvalue = 0,
2049 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2050 offsetof(CPUARMState, cp15.csselr_ns) } },
2051 /*
2052 * Auxiliary ID register: this actually has an IMPDEF value but for now
2053 * just RAZ for all cores:
2054 */
2055 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2056 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
2057 .access = PL1_R, .type = ARM_CP_CONST,
2058 .accessfn = access_aa64_tid1,
2059 .fgt = FGT_AIDR_EL1,
2060 .resetvalue = 0 },
2061 /*
2062 * Auxiliary fault status registers: these also are IMPDEF, and we
2063 * choose to RAZ/WI for all cores.
2064 */
2065 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2066 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
2067 .access = PL1_RW, .accessfn = access_tvm_trvm,
2068 .fgt = FGT_AFSR0_EL1,
2069 .nv2_redirect_offset = 0x128 | NV2_REDIR_NV1,
2070 .type = ARM_CP_CONST, .resetvalue = 0 },
2071 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2072 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
2073 .access = PL1_RW, .accessfn = access_tvm_trvm,
2074 .fgt = FGT_AFSR1_EL1,
2075 .nv2_redirect_offset = 0x130 | NV2_REDIR_NV1,
2076 .type = ARM_CP_CONST, .resetvalue = 0 },
2077 /*
2078 * MAIR can just read-as-written because we don't implement caches
2079 * and so don't need to care about memory attributes.
2080 */
2081 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2082 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2083 .access = PL1_RW, .accessfn = access_tvm_trvm,
2084 .fgt = FGT_MAIR_EL1,
2085 .nv2_redirect_offset = 0x140 | NV2_REDIR_NV1,
2086 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
2087 .resetvalue = 0 },
2088 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2089 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2090 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2091 .resetvalue = 0 },
2092 /*
2093 * For non-long-descriptor page tables these are PRRR and NMRR;
2094 * regardless they still act as reads-as-written for QEMU.
2095 */
2096 /*
2097 * MAIR0/1 are defined separately from their 64-bit counterpart which
2098 * allows them to assign the correct fieldoffset based on the endianness
2099 * handled in the field definitions.
2100 */
2101 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
2102 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2103 .access = PL1_RW, .accessfn = access_tvm_trvm,
2104 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2105 offsetof(CPUARMState, cp15.mair0_ns) },
2106 .resetfn = arm_cp_reset_ignore },
2107 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
2108 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2109 .access = PL1_RW, .accessfn = access_tvm_trvm,
2110 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2111 offsetof(CPUARMState, cp15.mair1_ns) },
2112 .resetfn = arm_cp_reset_ignore },
2113 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2114 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
2115 .fgt = FGT_ISR_EL1,
2116 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
2117 };
2118
2119 static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2120 /* PMOVSSET is not implemented in v7 before v7ve */
2121 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2122 .access = PL0_RW, .accessfn = pmreg_access,
2123 .fgt = FGT_PMOVS,
2124 .type = ARM_CP_ALIAS | ARM_CP_IO,
2125 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2126 .writefn = pmovsset_write,
2127 .raw_writefn = raw_write },
2128 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2129 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2130 .access = PL0_RW, .accessfn = pmreg_access,
2131 .fgt = FGT_PMOVS,
2132 .type = ARM_CP_ALIAS | ARM_CP_IO,
2133 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2134 .writefn = pmovsset_write,
2135 .raw_writefn = raw_write },
2136 };
2137
teecr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2138 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2139 uint64_t value)
2140 {
2141 value &= 1;
2142 env->teecr = value;
2143 }
2144
teecr_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2145 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2146 bool isread)
2147 {
2148 /*
2149 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2150 * at all, so we don't need to check whether we're v8A.
2151 */
2152 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2153 (env->cp15.hstr_el2 & HSTR_TTEE)) {
2154 return CP_ACCESS_TRAP_EL2;
2155 }
2156 return CP_ACCESS_OK;
2157 }
2158
teehbr_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2159 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2160 bool isread)
2161 {
2162 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
2163 return CP_ACCESS_TRAP_EL1;
2164 }
2165 return teecr_access(env, ri, isread);
2166 }
2167
2168 static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2169 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2170 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2171 .resetvalue = 0,
2172 .writefn = teecr_write, .accessfn = teecr_access },
2173 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2174 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
2175 .accessfn = teehbr_access, .resetvalue = 0 },
2176 };
2177
2178 static const ARMCPRegInfo v6k_cp_reginfo[] = {
2179 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2180 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2181 .access = PL0_RW,
2182 .fgt = FGT_TPIDR_EL0,
2183 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
2184 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2185 .access = PL0_RW,
2186 .fgt = FGT_TPIDR_EL0,
2187 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2188 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
2189 .resetfn = arm_cp_reset_ignore },
2190 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2191 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
2192 .access = PL0_R | PL1_W,
2193 .fgt = FGT_TPIDRRO_EL0,
2194 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2195 .resetvalue = 0},
2196 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
2197 .access = PL0_R | PL1_W,
2198 .fgt = FGT_TPIDRRO_EL0,
2199 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2200 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
2201 .resetfn = arm_cp_reset_ignore },
2202 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
2203 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
2204 .access = PL1_RW,
2205 .fgt = FGT_TPIDR_EL1,
2206 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2207 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2208 .access = PL1_RW,
2209 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2210 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2211 .resetvalue = 0 },
2212 };
2213
arm_gt_cntfrq_reset(CPUARMState * env,const ARMCPRegInfo * opaque)2214 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
2215 {
2216 ARMCPU *cpu = env_archcpu(env);
2217
2218 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
2219 }
2220
2221 #ifndef CONFIG_USER_ONLY
2222
gt_cntfrq_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2223 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2224 bool isread)
2225 {
2226 /*
2227 * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
2228 * Writable only at the highest implemented exception level.
2229 */
2230 int el = arm_current_el(env);
2231 uint64_t hcr;
2232 uint32_t cntkctl;
2233
2234 switch (el) {
2235 case 0:
2236 hcr = arm_hcr_el2_eff(env);
2237 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2238 cntkctl = env->cp15.cnthctl_el2;
2239 } else {
2240 cntkctl = env->cp15.c14_cntkctl;
2241 }
2242 if (!extract32(cntkctl, 0, 2)) {
2243 return CP_ACCESS_TRAP_EL1;
2244 }
2245 break;
2246 case 1:
2247 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2248 arm_is_secure_below_el3(env)) {
2249 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2250 return CP_ACCESS_UNDEFINED;
2251 }
2252 break;
2253 case 2:
2254 case 3:
2255 break;
2256 }
2257
2258 if (!isread && el < arm_highest_el(env)) {
2259 return CP_ACCESS_UNDEFINED;
2260 }
2261
2262 return CP_ACCESS_OK;
2263 }
2264
gt_counter_access(CPUARMState * env,int timeridx,bool isread)2265 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2266 bool isread)
2267 {
2268 unsigned int cur_el = arm_current_el(env);
2269 bool has_el2 = arm_is_el2_enabled(env);
2270 uint64_t hcr = arm_hcr_el2_eff(env);
2271
2272 switch (cur_el) {
2273 case 0:
2274 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2275 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2276 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2277 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2278 }
2279
2280 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2281 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2282 return CP_ACCESS_TRAP_EL1;
2283 }
2284 /* fall through */
2285 case 1:
2286 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
2287 if (has_el2 && timeridx == GTIMER_PHYS &&
2288 (hcr & HCR_E2H
2289 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2290 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2291 return CP_ACCESS_TRAP_EL2;
2292 }
2293 if (has_el2 && timeridx == GTIMER_VIRT) {
2294 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVCT)) {
2295 return CP_ACCESS_TRAP_EL2;
2296 }
2297 }
2298 break;
2299 }
2300 return CP_ACCESS_OK;
2301 }
2302
gt_timer_access(CPUARMState * env,int timeridx,bool isread)2303 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2304 bool isread)
2305 {
2306 unsigned int cur_el = arm_current_el(env);
2307 bool has_el2 = arm_is_el2_enabled(env);
2308 uint64_t hcr = arm_hcr_el2_eff(env);
2309
2310 switch (cur_el) {
2311 case 0:
2312 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2313 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2314 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2315 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2316 }
2317
2318 /*
2319 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2320 * EL0 if EL0[PV]TEN is zero.
2321 */
2322 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2323 return CP_ACCESS_TRAP_EL1;
2324 }
2325 /* fall through */
2326
2327 case 1:
2328 if (has_el2 && timeridx == GTIMER_PHYS) {
2329 if (hcr & HCR_E2H) {
2330 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2331 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2332 return CP_ACCESS_TRAP_EL2;
2333 }
2334 } else {
2335 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2336 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2337 return CP_ACCESS_TRAP_EL2;
2338 }
2339 }
2340 }
2341 if (has_el2 && timeridx == GTIMER_VIRT) {
2342 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVT)) {
2343 return CP_ACCESS_TRAP_EL2;
2344 }
2345 }
2346 break;
2347 }
2348 return CP_ACCESS_OK;
2349 }
2350
gt_pct_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2351 static CPAccessResult gt_pct_access(CPUARMState *env,
2352 const ARMCPRegInfo *ri,
2353 bool isread)
2354 {
2355 return gt_counter_access(env, GTIMER_PHYS, isread);
2356 }
2357
gt_vct_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2358 static CPAccessResult gt_vct_access(CPUARMState *env,
2359 const ARMCPRegInfo *ri,
2360 bool isread)
2361 {
2362 return gt_counter_access(env, GTIMER_VIRT, isread);
2363 }
2364
gt_ptimer_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2365 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2366 bool isread)
2367 {
2368 return gt_timer_access(env, GTIMER_PHYS, isread);
2369 }
2370
gt_vtimer_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2371 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2372 bool isread)
2373 {
2374 return gt_timer_access(env, GTIMER_VIRT, isread);
2375 }
2376
gt_stimer_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2377 static CPAccessResult gt_stimer_access(CPUARMState *env,
2378 const ARMCPRegInfo *ri,
2379 bool isread)
2380 {
2381 /*
2382 * The AArch64 register view of the secure physical timer is
2383 * always accessible from EL3, and configurably accessible from
2384 * Secure EL1.
2385 */
2386 switch (arm_current_el(env)) {
2387 case 1:
2388 if (!arm_is_secure(env)) {
2389 return CP_ACCESS_UNDEFINED;
2390 }
2391 if (arm_is_el2_enabled(env)) {
2392 return CP_ACCESS_UNDEFINED;
2393 }
2394 if (!(env->cp15.scr_el3 & SCR_ST)) {
2395 return CP_ACCESS_TRAP_EL3;
2396 }
2397 return CP_ACCESS_OK;
2398 case 0:
2399 case 2:
2400 return CP_ACCESS_UNDEFINED;
2401 case 3:
2402 return CP_ACCESS_OK;
2403 default:
2404 g_assert_not_reached();
2405 }
2406 }
2407
gt_sel2timer_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)2408 static CPAccessResult gt_sel2timer_access(CPUARMState *env,
2409 const ARMCPRegInfo *ri,
2410 bool isread)
2411 {
2412 /*
2413 * The AArch64 register view of the secure EL2 timers are mostly
2414 * accessible from EL3 and EL2 although can also be trapped to EL2
2415 * from EL1 depending on nested virt config.
2416 */
2417 switch (arm_current_el(env)) {
2418 case 0: /* UNDEFINED */
2419 return CP_ACCESS_UNDEFINED;
2420 case 1:
2421 if (!arm_is_secure(env)) {
2422 /* UNDEFINED */
2423 return CP_ACCESS_UNDEFINED;
2424 } else if (arm_hcr_el2_eff(env) & HCR_NV) {
2425 /* Aarch64.SystemAccessTrap(EL2, 0x18) */
2426 return CP_ACCESS_TRAP_EL2;
2427 }
2428 /* UNDEFINED */
2429 return CP_ACCESS_UNDEFINED;
2430 case 2:
2431 if (!arm_is_secure(env)) {
2432 /* UNDEFINED */
2433 return CP_ACCESS_UNDEFINED;
2434 }
2435 return CP_ACCESS_OK;
2436 case 3:
2437 if (env->cp15.scr_el3 & SCR_EEL2) {
2438 return CP_ACCESS_OK;
2439 } else {
2440 return CP_ACCESS_UNDEFINED;
2441 }
2442 default:
2443 g_assert_not_reached();
2444 }
2445 }
2446
gt_get_countervalue(CPUARMState * env)2447 uint64_t gt_get_countervalue(CPUARMState *env)
2448 {
2449 ARMCPU *cpu = env_archcpu(env);
2450
2451 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
2452 }
2453
gt_update_irq(ARMCPU * cpu,int timeridx)2454 static void gt_update_irq(ARMCPU *cpu, int timeridx)
2455 {
2456 CPUARMState *env = &cpu->env;
2457 uint64_t cnthctl = env->cp15.cnthctl_el2;
2458 ARMSecuritySpace ss = arm_security_space(env);
2459 /* ISTATUS && !IMASK */
2460 int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4;
2461
2462 /*
2463 * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK.
2464 * It is RES0 in Secure and NonSecure state.
2465 */
2466 if ((ss == ARMSS_Root || ss == ARMSS_Realm) &&
2467 ((timeridx == GTIMER_VIRT && (cnthctl & R_CNTHCTL_CNTVMASK_MASK)) ||
2468 (timeridx == GTIMER_PHYS && (cnthctl & R_CNTHCTL_CNTPMASK_MASK)))) {
2469 irqstate = 0;
2470 }
2471
2472 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2473 trace_arm_gt_update_irq(timeridx, irqstate);
2474 }
2475
gt_rme_post_el_change(ARMCPU * cpu,void * ignored)2476 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored)
2477 {
2478 /*
2479 * Changing security state between Root and Secure/NonSecure, which may
2480 * happen when switching EL, can change the effective value of CNTHCTL_EL2
2481 * mask bits. Update the IRQ state accordingly.
2482 */
2483 gt_update_irq(cpu, GTIMER_VIRT);
2484 gt_update_irq(cpu, GTIMER_PHYS);
2485 }
2486
gt_phys_raw_cnt_offset(CPUARMState * env)2487 static uint64_t gt_phys_raw_cnt_offset(CPUARMState *env)
2488 {
2489 if ((env->cp15.scr_el3 & SCR_ECVEN) &&
2490 FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, ECV) &&
2491 arm_is_el2_enabled(env) &&
2492 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
2493 return env->cp15.cntpoff_el2;
2494 }
2495 return 0;
2496 }
2497
gt_indirect_access_timer_offset(CPUARMState * env,int timeridx)2498 static uint64_t gt_indirect_access_timer_offset(CPUARMState *env, int timeridx)
2499 {
2500 /*
2501 * Return the timer offset to use for indirect accesses to the timer.
2502 * This is the Offset value as defined in D12.2.4.1 "Operation of the
2503 * CompareValue views of the timers".
2504 *
2505 * The condition here is not always the same as the condition for
2506 * whether to apply an offset register when doing a direct read of
2507 * the counter sysreg; those conditions are described in the
2508 * access pseudocode for each counter register.
2509 */
2510 switch (timeridx) {
2511 case GTIMER_PHYS:
2512 return gt_phys_raw_cnt_offset(env);
2513 case GTIMER_VIRT:
2514 return env->cp15.cntvoff_el2;
2515 case GTIMER_HYP:
2516 case GTIMER_SEC:
2517 case GTIMER_HYPVIRT:
2518 case GTIMER_S_EL2_PHYS:
2519 case GTIMER_S_EL2_VIRT:
2520 return 0;
2521 default:
2522 g_assert_not_reached();
2523 }
2524 }
2525
gt_direct_access_timer_offset(CPUARMState * env,int timeridx)2526 uint64_t gt_direct_access_timer_offset(CPUARMState *env, int timeridx)
2527 {
2528 /*
2529 * Return the timer offset to use for direct accesses to the
2530 * counter registers CNTPCT and CNTVCT, and for direct accesses
2531 * to the CNT*_TVAL registers.
2532 *
2533 * This isn't exactly the same as the indirect-access offset,
2534 * because here we also care about what EL the register access
2535 * is being made from.
2536 *
2537 * This corresponds to the access pseudocode for the registers.
2538 */
2539 uint64_t hcr;
2540
2541 switch (timeridx) {
2542 case GTIMER_PHYS:
2543 if (arm_current_el(env) >= 2) {
2544 return 0;
2545 }
2546 return gt_phys_raw_cnt_offset(env);
2547 case GTIMER_VIRT:
2548 switch (arm_current_el(env)) {
2549 case 2:
2550 hcr = arm_hcr_el2_eff(env);
2551 if (hcr & HCR_E2H) {
2552 return 0;
2553 }
2554 break;
2555 case 0:
2556 hcr = arm_hcr_el2_eff(env);
2557 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2558 return 0;
2559 }
2560 break;
2561 }
2562 return env->cp15.cntvoff_el2;
2563 case GTIMER_HYP:
2564 case GTIMER_SEC:
2565 case GTIMER_HYPVIRT:
2566 case GTIMER_S_EL2_PHYS:
2567 case GTIMER_S_EL2_VIRT:
2568 return 0;
2569 default:
2570 g_assert_not_reached();
2571 }
2572 }
2573
gt_recalc_timer(ARMCPU * cpu,int timeridx)2574 static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2575 {
2576 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2577
2578 if (gt->ctl & 1) {
2579 /*
2580 * Timer enabled: calculate and set current ISTATUS, irq, and
2581 * reset timer to when ISTATUS next has to change
2582 */
2583 uint64_t offset = gt_indirect_access_timer_offset(&cpu->env, timeridx);
2584 uint64_t count = gt_get_countervalue(&cpu->env);
2585 /* Note that this must be unsigned 64 bit arithmetic: */
2586 int istatus = count - offset >= gt->cval;
2587 uint64_t nexttick;
2588
2589 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
2590
2591 if (istatus) {
2592 /*
2593 * Next transition is when (count - offset) rolls back over to 0.
2594 * If offset > count then this is when count == offset;
2595 * if offset <= count then this is when count == offset + 2^64
2596 * For the latter case we set nexttick to an "as far in future
2597 * as possible" value and let the code below handle it.
2598 */
2599 if (offset > count) {
2600 nexttick = offset;
2601 } else {
2602 nexttick = UINT64_MAX;
2603 }
2604 } else {
2605 /*
2606 * Next transition is when (count - offset) == cval, i.e.
2607 * when count == (cval + offset).
2608 * If that would overflow, then again we set up the next interrupt
2609 * for "as far in the future as possible" for the code below.
2610 */
2611 if (uadd64_overflow(gt->cval, offset, &nexttick)) {
2612 nexttick = UINT64_MAX;
2613 }
2614 }
2615 /*
2616 * Note that the desired next expiry time might be beyond the
2617 * signed-64-bit range of a QEMUTimer -- in this case we just
2618 * set the timer for as far in the future as possible. When the
2619 * timer expires we will reset the timer for any remaining period.
2620 */
2621 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
2622 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2623 } else {
2624 timer_mod(cpu->gt_timer[timeridx], nexttick);
2625 }
2626 trace_arm_gt_recalc(timeridx, nexttick);
2627 } else {
2628 /* Timer disabled: ISTATUS and timer output always clear */
2629 gt->ctl &= ~4;
2630 timer_del(cpu->gt_timer[timeridx]);
2631 trace_arm_gt_recalc_disabled(timeridx);
2632 }
2633 gt_update_irq(cpu, timeridx);
2634 }
2635
gt_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx)2636 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2637 int timeridx)
2638 {
2639 ARMCPU *cpu = env_archcpu(env);
2640
2641 timer_del(cpu->gt_timer[timeridx]);
2642 }
2643
gt_cnt_read(CPUARMState * env,const ARMCPRegInfo * ri)2644 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2645 {
2646 uint64_t offset = gt_direct_access_timer_offset(env, GTIMER_PHYS);
2647 return gt_get_countervalue(env) - offset;
2648 }
2649
gt_virt_cnt_read(CPUARMState * env,const ARMCPRegInfo * ri)2650 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2651 {
2652 uint64_t offset = gt_direct_access_timer_offset(env, GTIMER_VIRT);
2653 return gt_get_countervalue(env) - offset;
2654 }
2655
gt_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx,uint64_t value)2656 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2657 int timeridx,
2658 uint64_t value)
2659 {
2660 trace_arm_gt_cval_write(timeridx, value);
2661 env->cp15.c14_timer[timeridx].cval = value;
2662 gt_recalc_timer(env_archcpu(env), timeridx);
2663 }
2664
do_tval_read(CPUARMState * env,int timeridx,uint64_t offset)2665 static uint64_t do_tval_read(CPUARMState *env, int timeridx, uint64_t offset)
2666 {
2667 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
2668 (gt_get_countervalue(env) - offset));
2669 }
2670
gt_tval_read(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx)2671 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2672 int timeridx)
2673 {
2674 uint64_t offset = gt_direct_access_timer_offset(env, timeridx);
2675
2676 return do_tval_read(env, timeridx, offset);
2677 }
2678
do_tval_write(CPUARMState * env,int timeridx,uint64_t value,uint64_t offset)2679 static void do_tval_write(CPUARMState *env, int timeridx, uint64_t value,
2680 uint64_t offset)
2681 {
2682 trace_arm_gt_tval_write(timeridx, value);
2683 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
2684 sextract64(value, 0, 32);
2685 gt_recalc_timer(env_archcpu(env), timeridx);
2686 }
2687
gt_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx,uint64_t value)2688 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2689 int timeridx,
2690 uint64_t value)
2691 {
2692 uint64_t offset = gt_direct_access_timer_offset(env, timeridx);
2693
2694 do_tval_write(env, timeridx, value, offset);
2695 }
2696
gt_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,int timeridx,uint64_t value)2697 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2698 int timeridx,
2699 uint64_t value)
2700 {
2701 ARMCPU *cpu = env_archcpu(env);
2702 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2703
2704 trace_arm_gt_ctl_write(timeridx, value);
2705 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
2706 if ((oldval ^ value) & 1) {
2707 /* Enable toggled */
2708 gt_recalc_timer(cpu, timeridx);
2709 } else if ((oldval ^ value) & 2) {
2710 /*
2711 * IMASK toggled: don't need to recalculate,
2712 * just set the interrupt line based on ISTATUS
2713 */
2714 trace_arm_gt_imask_toggle(timeridx);
2715 gt_update_irq(cpu, timeridx);
2716 }
2717 }
2718
gt_phys_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)2719 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2720 {
2721 gt_timer_reset(env, ri, GTIMER_PHYS);
2722 }
2723
gt_phys_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2724 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2725 uint64_t value)
2726 {
2727 gt_cval_write(env, ri, GTIMER_PHYS, value);
2728 }
2729
gt_phys_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)2730 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2731 {
2732 return gt_tval_read(env, ri, GTIMER_PHYS);
2733 }
2734
gt_phys_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2735 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2736 uint64_t value)
2737 {
2738 gt_tval_write(env, ri, GTIMER_PHYS, value);
2739 }
2740
gt_phys_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2741 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2742 uint64_t value)
2743 {
2744 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2745 }
2746
gt_phys_redir_timeridx(CPUARMState * env)2747 static int gt_phys_redir_timeridx(CPUARMState *env)
2748 {
2749 switch (arm_mmu_idx(env)) {
2750 case ARMMMUIdx_E20_0:
2751 case ARMMMUIdx_E20_2:
2752 case ARMMMUIdx_E20_2_PAN:
2753 return GTIMER_HYP;
2754 default:
2755 return GTIMER_PHYS;
2756 }
2757 }
2758
gt_virt_redir_timeridx(CPUARMState * env)2759 static int gt_virt_redir_timeridx(CPUARMState *env)
2760 {
2761 switch (arm_mmu_idx(env)) {
2762 case ARMMMUIdx_E20_0:
2763 case ARMMMUIdx_E20_2:
2764 case ARMMMUIdx_E20_2_PAN:
2765 return GTIMER_HYPVIRT;
2766 default:
2767 return GTIMER_VIRT;
2768 }
2769 }
2770
gt_phys_redir_cval_read(CPUARMState * env,const ARMCPRegInfo * ri)2771 static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2772 const ARMCPRegInfo *ri)
2773 {
2774 int timeridx = gt_phys_redir_timeridx(env);
2775 return env->cp15.c14_timer[timeridx].cval;
2776 }
2777
gt_phys_redir_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2778 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2779 uint64_t value)
2780 {
2781 int timeridx = gt_phys_redir_timeridx(env);
2782 gt_cval_write(env, ri, timeridx, value);
2783 }
2784
gt_phys_redir_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)2785 static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2786 const ARMCPRegInfo *ri)
2787 {
2788 int timeridx = gt_phys_redir_timeridx(env);
2789 return gt_tval_read(env, ri, timeridx);
2790 }
2791
gt_phys_redir_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2792 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2793 uint64_t value)
2794 {
2795 int timeridx = gt_phys_redir_timeridx(env);
2796 gt_tval_write(env, ri, timeridx, value);
2797 }
2798
gt_phys_redir_ctl_read(CPUARMState * env,const ARMCPRegInfo * ri)2799 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2800 const ARMCPRegInfo *ri)
2801 {
2802 int timeridx = gt_phys_redir_timeridx(env);
2803 return env->cp15.c14_timer[timeridx].ctl;
2804 }
2805
gt_phys_redir_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2806 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2807 uint64_t value)
2808 {
2809 int timeridx = gt_phys_redir_timeridx(env);
2810 gt_ctl_write(env, ri, timeridx, value);
2811 }
2812
gt_virt_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)2813 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2814 {
2815 gt_timer_reset(env, ri, GTIMER_VIRT);
2816 }
2817
gt_virt_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2818 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2819 uint64_t value)
2820 {
2821 gt_cval_write(env, ri, GTIMER_VIRT, value);
2822 }
2823
gt_virt_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)2824 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2825 {
2826 /*
2827 * This is CNTV_TVAL_EL02; unlike the underlying CNTV_TVAL_EL0
2828 * we always apply CNTVOFF_EL2. Special case that here rather
2829 * than going into the generic gt_tval_read() and then having
2830 * to re-detect that it's this register.
2831 * Note that the accessfn/perms mean we know we're at EL2 or EL3 here.
2832 */
2833 return do_tval_read(env, GTIMER_VIRT, env->cp15.cntvoff_el2);
2834 }
2835
gt_virt_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2836 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2837 uint64_t value)
2838 {
2839 /* Similarly for writes to CNTV_TVAL_EL02 */
2840 do_tval_write(env, GTIMER_VIRT, value, env->cp15.cntvoff_el2);
2841 }
2842
gt_virt_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2843 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2844 uint64_t value)
2845 {
2846 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2847 }
2848
gt_cnthctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2849 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2850 uint64_t value)
2851 {
2852 ARMCPU *cpu = env_archcpu(env);
2853 uint32_t oldval = env->cp15.cnthctl_el2;
2854 uint32_t valid_mask =
2855 R_CNTHCTL_EL0PCTEN_E2H1_MASK |
2856 R_CNTHCTL_EL0VCTEN_E2H1_MASK |
2857 R_CNTHCTL_EVNTEN_MASK |
2858 R_CNTHCTL_EVNTDIR_MASK |
2859 R_CNTHCTL_EVNTI_MASK |
2860 R_CNTHCTL_EL0VTEN_MASK |
2861 R_CNTHCTL_EL0PTEN_MASK |
2862 R_CNTHCTL_EL1PCTEN_E2H1_MASK |
2863 R_CNTHCTL_EL1PTEN_MASK;
2864
2865 if (cpu_isar_feature(aa64_rme, cpu)) {
2866 valid_mask |= R_CNTHCTL_CNTVMASK_MASK | R_CNTHCTL_CNTPMASK_MASK;
2867 }
2868 if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
2869 valid_mask |=
2870 R_CNTHCTL_EL1TVT_MASK |
2871 R_CNTHCTL_EL1TVCT_MASK |
2872 R_CNTHCTL_EL1NVPCT_MASK |
2873 R_CNTHCTL_EL1NVVCT_MASK |
2874 R_CNTHCTL_EVNTIS_MASK;
2875 }
2876 if (cpu_isar_feature(aa64_ecv, cpu)) {
2877 valid_mask |= R_CNTHCTL_ECV_MASK;
2878 }
2879
2880 /* Clear RES0 bits */
2881 value &= valid_mask;
2882
2883 raw_write(env, ri, value);
2884
2885 if ((oldval ^ value) & R_CNTHCTL_CNTVMASK_MASK) {
2886 gt_update_irq(cpu, GTIMER_VIRT);
2887 } else if ((oldval ^ value) & R_CNTHCTL_CNTPMASK_MASK) {
2888 gt_update_irq(cpu, GTIMER_PHYS);
2889 }
2890 }
2891
gt_cntvoff_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2892 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2893 uint64_t value)
2894 {
2895 ARMCPU *cpu = env_archcpu(env);
2896
2897 trace_arm_gt_cntvoff_write(value);
2898 raw_write(env, ri, value);
2899 gt_recalc_timer(cpu, GTIMER_VIRT);
2900 }
2901
gt_virt_redir_cval_read(CPUARMState * env,const ARMCPRegInfo * ri)2902 static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2903 const ARMCPRegInfo *ri)
2904 {
2905 int timeridx = gt_virt_redir_timeridx(env);
2906 return env->cp15.c14_timer[timeridx].cval;
2907 }
2908
gt_virt_redir_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2909 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2910 uint64_t value)
2911 {
2912 int timeridx = gt_virt_redir_timeridx(env);
2913 gt_cval_write(env, ri, timeridx, value);
2914 }
2915
gt_virt_redir_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)2916 static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2917 const ARMCPRegInfo *ri)
2918 {
2919 int timeridx = gt_virt_redir_timeridx(env);
2920 return gt_tval_read(env, ri, timeridx);
2921 }
2922
gt_virt_redir_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2923 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2924 uint64_t value)
2925 {
2926 int timeridx = gt_virt_redir_timeridx(env);
2927 gt_tval_write(env, ri, timeridx, value);
2928 }
2929
gt_virt_redir_ctl_read(CPUARMState * env,const ARMCPRegInfo * ri)2930 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2931 const ARMCPRegInfo *ri)
2932 {
2933 int timeridx = gt_virt_redir_timeridx(env);
2934 return env->cp15.c14_timer[timeridx].ctl;
2935 }
2936
gt_virt_redir_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2937 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2938 uint64_t value)
2939 {
2940 int timeridx = gt_virt_redir_timeridx(env);
2941 gt_ctl_write(env, ri, timeridx, value);
2942 }
2943
gt_hyp_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)2944 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2945 {
2946 gt_timer_reset(env, ri, GTIMER_HYP);
2947 }
2948
gt_hyp_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2949 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2950 uint64_t value)
2951 {
2952 gt_cval_write(env, ri, GTIMER_HYP, value);
2953 }
2954
gt_hyp_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)2955 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2956 {
2957 return gt_tval_read(env, ri, GTIMER_HYP);
2958 }
2959
gt_hyp_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2960 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2961 uint64_t value)
2962 {
2963 gt_tval_write(env, ri, GTIMER_HYP, value);
2964 }
2965
gt_hyp_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2966 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2967 uint64_t value)
2968 {
2969 gt_ctl_write(env, ri, GTIMER_HYP, value);
2970 }
2971
gt_sec_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)2972 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2973 {
2974 gt_timer_reset(env, ri, GTIMER_SEC);
2975 }
2976
gt_sec_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2977 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2978 uint64_t value)
2979 {
2980 gt_cval_write(env, ri, GTIMER_SEC, value);
2981 }
2982
gt_sec_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)2983 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2984 {
2985 return gt_tval_read(env, ri, GTIMER_SEC);
2986 }
2987
gt_sec_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2988 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2989 uint64_t value)
2990 {
2991 gt_tval_write(env, ri, GTIMER_SEC, value);
2992 }
2993
gt_sec_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)2994 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2995 uint64_t value)
2996 {
2997 gt_ctl_write(env, ri, GTIMER_SEC, value);
2998 }
2999
gt_sec_pel2_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)3000 static void gt_sec_pel2_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3001 {
3002 gt_timer_reset(env, ri, GTIMER_S_EL2_PHYS);
3003 }
3004
gt_sec_pel2_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3005 static void gt_sec_pel2_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3006 uint64_t value)
3007 {
3008 gt_cval_write(env, ri, GTIMER_S_EL2_PHYS, value);
3009 }
3010
gt_sec_pel2_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3011 static uint64_t gt_sec_pel2_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3012 {
3013 return gt_tval_read(env, ri, GTIMER_S_EL2_PHYS);
3014 }
3015
gt_sec_pel2_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3016 static void gt_sec_pel2_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3017 uint64_t value)
3018 {
3019 gt_tval_write(env, ri, GTIMER_S_EL2_PHYS, value);
3020 }
3021
gt_sec_pel2_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3022 static void gt_sec_pel2_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3023 uint64_t value)
3024 {
3025 gt_ctl_write(env, ri, GTIMER_S_EL2_PHYS, value);
3026 }
3027
gt_sec_vel2_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)3028 static void gt_sec_vel2_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3029 {
3030 gt_timer_reset(env, ri, GTIMER_S_EL2_VIRT);
3031 }
3032
gt_sec_vel2_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3033 static void gt_sec_vel2_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3034 uint64_t value)
3035 {
3036 gt_cval_write(env, ri, GTIMER_S_EL2_VIRT, value);
3037 }
3038
gt_sec_vel2_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3039 static uint64_t gt_sec_vel2_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3040 {
3041 return gt_tval_read(env, ri, GTIMER_S_EL2_VIRT);
3042 }
3043
gt_sec_vel2_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3044 static void gt_sec_vel2_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3045 uint64_t value)
3046 {
3047 gt_tval_write(env, ri, GTIMER_S_EL2_VIRT, value);
3048 }
3049
gt_sec_vel2_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3050 static void gt_sec_vel2_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3051 uint64_t value)
3052 {
3053 gt_ctl_write(env, ri, GTIMER_S_EL2_VIRT, value);
3054 }
3055
gt_hv_timer_reset(CPUARMState * env,const ARMCPRegInfo * ri)3056 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
3057 {
3058 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
3059 }
3060
gt_hv_cval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3061 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3062 uint64_t value)
3063 {
3064 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3065 }
3066
gt_hv_tval_read(CPUARMState * env,const ARMCPRegInfo * ri)3067 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3068 {
3069 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3070 }
3071
gt_hv_tval_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3072 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3073 uint64_t value)
3074 {
3075 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3076 }
3077
gt_hv_ctl_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3078 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3079 uint64_t value)
3080 {
3081 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3082 }
3083
arm_gt_ptimer_cb(void * opaque)3084 void arm_gt_ptimer_cb(void *opaque)
3085 {
3086 ARMCPU *cpu = opaque;
3087
3088 gt_recalc_timer(cpu, GTIMER_PHYS);
3089 }
3090
arm_gt_vtimer_cb(void * opaque)3091 void arm_gt_vtimer_cb(void *opaque)
3092 {
3093 ARMCPU *cpu = opaque;
3094
3095 gt_recalc_timer(cpu, GTIMER_VIRT);
3096 }
3097
arm_gt_htimer_cb(void * opaque)3098 void arm_gt_htimer_cb(void *opaque)
3099 {
3100 ARMCPU *cpu = opaque;
3101
3102 gt_recalc_timer(cpu, GTIMER_HYP);
3103 }
3104
arm_gt_stimer_cb(void * opaque)3105 void arm_gt_stimer_cb(void *opaque)
3106 {
3107 ARMCPU *cpu = opaque;
3108
3109 gt_recalc_timer(cpu, GTIMER_SEC);
3110 }
3111
arm_gt_sel2timer_cb(void * opaque)3112 void arm_gt_sel2timer_cb(void *opaque)
3113 {
3114 ARMCPU *cpu = opaque;
3115
3116 gt_recalc_timer(cpu, GTIMER_S_EL2_PHYS);
3117 }
3118
arm_gt_sel2vtimer_cb(void * opaque)3119 void arm_gt_sel2vtimer_cb(void *opaque)
3120 {
3121 ARMCPU *cpu = opaque;
3122
3123 gt_recalc_timer(cpu, GTIMER_S_EL2_VIRT);
3124 }
3125
arm_gt_hvtimer_cb(void * opaque)3126 void arm_gt_hvtimer_cb(void *opaque)
3127 {
3128 ARMCPU *cpu = opaque;
3129
3130 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3131 }
3132
3133 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3134 /*
3135 * Note that CNTFRQ is purely reads-as-written for the benefit
3136 * of software; writing it doesn't actually change the timer frequency.
3137 * Our reset value matches the fixed frequency we implement the timer at.
3138 */
3139 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
3140 .type = ARM_CP_ALIAS,
3141 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3142 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
3143 },
3144 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3145 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3146 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3147 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3148 .resetfn = arm_gt_cntfrq_reset,
3149 },
3150 /* overall control: mostly access permissions */
3151 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3152 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
3153 .access = PL1_RW,
3154 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3155 .resetvalue = 0,
3156 },
3157 /* per-timer control */
3158 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3159 .secure = ARM_CP_SECSTATE_NS,
3160 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3161 .accessfn = gt_ptimer_access,
3162 .fieldoffset = offsetoflow32(CPUARMState,
3163 cp15.c14_timer[GTIMER_PHYS].ctl),
3164 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3165 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3166 },
3167 { .name = "CNTP_CTL_S",
3168 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3169 .secure = ARM_CP_SECSTATE_S,
3170 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3171 .accessfn = gt_ptimer_access,
3172 .fieldoffset = offsetoflow32(CPUARMState,
3173 cp15.c14_timer[GTIMER_SEC].ctl),
3174 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3175 },
3176 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3177 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
3178 .type = ARM_CP_IO, .access = PL0_RW,
3179 .accessfn = gt_ptimer_access,
3180 .nv2_redirect_offset = 0x180 | NV2_REDIR_NV1,
3181 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3182 .resetvalue = 0,
3183 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3184 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
3185 },
3186 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
3187 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
3188 .accessfn = gt_vtimer_access,
3189 .fieldoffset = offsetoflow32(CPUARMState,
3190 cp15.c14_timer[GTIMER_VIRT].ctl),
3191 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3192 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3193 },
3194 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3195 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
3196 .type = ARM_CP_IO, .access = PL0_RW,
3197 .accessfn = gt_vtimer_access,
3198 .nv2_redirect_offset = 0x170 | NV2_REDIR_NV1,
3199 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3200 .resetvalue = 0,
3201 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3202 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
3203 },
3204 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3205 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3206 .secure = ARM_CP_SECSTATE_NS,
3207 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3208 .accessfn = gt_ptimer_access,
3209 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3210 },
3211 { .name = "CNTP_TVAL_S",
3212 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3213 .secure = ARM_CP_SECSTATE_S,
3214 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3215 .accessfn = gt_ptimer_access,
3216 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3217 },
3218 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3219 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
3220 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3221 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
3222 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
3223 },
3224 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
3225 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3226 .accessfn = gt_vtimer_access,
3227 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3228 },
3229 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3230 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
3231 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
3232 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
3233 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
3234 },
3235 /* The counter itself */
3236 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
3237 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3238 .accessfn = gt_pct_access,
3239 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3240 },
3241 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3242 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
3243 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3244 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3245 },
3246 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
3247 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3248 .accessfn = gt_vct_access,
3249 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3250 },
3251 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3252 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3253 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3254 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3255 },
3256 /* Comparison value, indicating when the timer goes off */
3257 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
3258 .secure = ARM_CP_SECSTATE_NS,
3259 .access = PL0_RW,
3260 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3261 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3262 .accessfn = gt_ptimer_access,
3263 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3264 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3265 },
3266 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
3267 .secure = ARM_CP_SECSTATE_S,
3268 .access = PL0_RW,
3269 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3270 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3271 .accessfn = gt_ptimer_access,
3272 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3273 },
3274 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3275 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
3276 .access = PL0_RW,
3277 .type = ARM_CP_IO,
3278 .nv2_redirect_offset = 0x178 | NV2_REDIR_NV1,
3279 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
3280 .resetvalue = 0, .accessfn = gt_ptimer_access,
3281 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3282 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
3283 },
3284 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
3285 .access = PL0_RW,
3286 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3287 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3288 .accessfn = gt_vtimer_access,
3289 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3290 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3291 },
3292 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3293 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
3294 .access = PL0_RW,
3295 .type = ARM_CP_IO,
3296 .nv2_redirect_offset = 0x168 | NV2_REDIR_NV1,
3297 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3298 .resetvalue = 0, .accessfn = gt_vtimer_access,
3299 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3300 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
3301 },
3302 /*
3303 * Secure timer -- this is actually restricted to only EL3
3304 * and configurably Secure-EL1 via the accessfn.
3305 */
3306 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3307 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3308 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3309 .accessfn = gt_stimer_access,
3310 .readfn = gt_sec_tval_read,
3311 .writefn = gt_sec_tval_write,
3312 .resetfn = gt_sec_timer_reset,
3313 },
3314 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3315 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3316 .type = ARM_CP_IO, .access = PL1_RW,
3317 .accessfn = gt_stimer_access,
3318 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3319 .resetvalue = 0,
3320 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3321 },
3322 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3323 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3324 .type = ARM_CP_IO, .access = PL1_RW,
3325 .accessfn = gt_stimer_access,
3326 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3327 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3328 },
3329 };
3330
3331 /*
3332 * FEAT_ECV adds extra views of CNTVCT_EL0 and CNTPCT_EL0 which
3333 * are "self-synchronizing". For QEMU all sysregs are self-synchronizing,
3334 * so our implementations here are identical to the normal registers.
3335 */
3336 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3337 { .name = "CNTVCTSS", .cp = 15, .crm = 14, .opc1 = 9,
3338 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3339 .accessfn = gt_vct_access,
3340 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
3341 },
3342 { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3343 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3344 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3345 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
3346 },
3347 { .name = "CNTPCTSS", .cp = 15, .crm = 14, .opc1 = 8,
3348 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
3349 .accessfn = gt_pct_access,
3350 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3351 },
3352 { .name = "CNTPCTSS_EL0", .state = ARM_CP_STATE_AA64,
3353 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 5,
3354 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3355 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
3356 },
3357 };
3358
gt_cntpoff_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)3359 static CPAccessResult gt_cntpoff_access(CPUARMState *env,
3360 const ARMCPRegInfo *ri,
3361 bool isread)
3362 {
3363 if (arm_current_el(env) == 2 && arm_feature(env, ARM_FEATURE_EL3) &&
3364 !(env->cp15.scr_el3 & SCR_ECVEN)) {
3365 return CP_ACCESS_TRAP_EL3;
3366 }
3367 return CP_ACCESS_OK;
3368 }
3369
gt_cntpoff_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3370 static void gt_cntpoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
3371 uint64_t value)
3372 {
3373 ARMCPU *cpu = env_archcpu(env);
3374
3375 trace_arm_gt_cntpoff_write(value);
3376 raw_write(env, ri, value);
3377 gt_recalc_timer(cpu, GTIMER_PHYS);
3378 }
3379
3380 static const ARMCPRegInfo gen_timer_cntpoff_reginfo = {
3381 .name = "CNTPOFF_EL2", .state = ARM_CP_STATE_AA64,
3382 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 6,
3383 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3384 .accessfn = gt_cntpoff_access, .writefn = gt_cntpoff_write,
3385 .nv2_redirect_offset = 0x1a8,
3386 .fieldoffset = offsetof(CPUARMState, cp15.cntpoff_el2),
3387 };
3388 #else
3389
3390 /*
3391 * In user-mode most of the generic timer registers are inaccessible
3392 * however modern kernels (4.12+) allow access to cntvct_el0
3393 */
3394
gt_virt_cnt_read(CPUARMState * env,const ARMCPRegInfo * ri)3395 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3396 {
3397 ARMCPU *cpu = env_archcpu(env);
3398
3399 /*
3400 * Currently we have no support for QEMUTimer in linux-user so we
3401 * can't call gt_get_countervalue(env), instead we directly
3402 * call the lower level functions.
3403 */
3404 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
3405 }
3406
3407 static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
3408 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3409 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3410 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3411 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3412 .resetfn = arm_gt_cntfrq_reset,
3413 },
3414 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3415 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3416 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3417 .readfn = gt_virt_cnt_read,
3418 },
3419 };
3420
3421 /*
3422 * CNTVCTSS_EL0 has the same trap conditions as CNTVCT_EL0, so it also
3423 * is exposed to userspace by Linux.
3424 */
3425 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
3426 { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
3427 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
3428 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3429 .readfn = gt_virt_cnt_read,
3430 },
3431 };
3432
3433 #endif
3434
par_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3435 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3436 {
3437 if (arm_feature(env, ARM_FEATURE_LPAE)) {
3438 raw_write(env, ri, value);
3439 } else if (arm_feature(env, ARM_FEATURE_V7)) {
3440 raw_write(env, ri, value & 0xfffff6ff);
3441 } else {
3442 raw_write(env, ri, value & 0xfffff1ff);
3443 }
3444 }
3445
3446 #ifndef CONFIG_USER_ONLY
3447 /* get_phys_addr() isn't present for user-mode-only targets */
3448
ats_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)3449 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3450 bool isread)
3451 {
3452 if (ri->opc2 & 4) {
3453 /*
3454 * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
3455 * Secure EL1 (which can only happen if EL3 is AArch64).
3456 * They are simply UNDEF if executed from NS EL1.
3457 * They function normally from EL2 or EL3.
3458 */
3459 if (arm_current_el(env) == 1) {
3460 if (arm_is_secure_below_el3(env)) {
3461 if (env->cp15.scr_el3 & SCR_EEL2) {
3462 return CP_ACCESS_TRAP_EL2;
3463 }
3464 return CP_ACCESS_TRAP_EL3;
3465 }
3466 return CP_ACCESS_UNDEFINED;
3467 }
3468 }
3469 return CP_ACCESS_OK;
3470 }
3471
3472 #ifdef CONFIG_TCG
par_el1_shareability(GetPhysAddrResult * res)3473 static int par_el1_shareability(GetPhysAddrResult *res)
3474 {
3475 /*
3476 * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC
3477 * memory -- see pseudocode PAREncodeShareability().
3478 */
3479 if (((res->cacheattrs.attrs & 0xf0) == 0) ||
3480 res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) {
3481 return 2;
3482 }
3483 return res->cacheattrs.shareability;
3484 }
3485
do_ats_write(CPUARMState * env,uint64_t value,MMUAccessType access_type,ARMMMUIdx mmu_idx,ARMSecuritySpace ss)3486 static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
3487 MMUAccessType access_type, ARMMMUIdx mmu_idx,
3488 ARMSecuritySpace ss)
3489 {
3490 bool ret;
3491 uint64_t par64;
3492 bool format64 = false;
3493 ARMMMUFaultInfo fi = {};
3494 GetPhysAddrResult res = {};
3495
3496 /*
3497 * I_MXTJT: Granule protection checks are not performed on the final
3498 * address of a successful translation. This is a translation not a
3499 * memory reference, so "memop = none = 0".
3500 */
3501 ret = get_phys_addr_with_space_nogpc(env, value, access_type, 0,
3502 mmu_idx, ss, &res, &fi);
3503
3504 /*
3505 * ATS operations only do S1 or S1+S2 translations, so we never
3506 * have to deal with the ARMCacheAttrs format for S2 only.
3507 */
3508 assert(!res.cacheattrs.is_s2_format);
3509
3510 if (ret) {
3511 /*
3512 * Some kinds of translation fault must cause exceptions rather
3513 * than being reported in the PAR.
3514 */
3515 int current_el = arm_current_el(env);
3516 int target_el;
3517 uint32_t syn, fsr, fsc;
3518 bool take_exc = false;
3519
3520 if (fi.s1ptw && current_el == 1
3521 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
3522 /*
3523 * Synchronous stage 2 fault on an access made as part of the
3524 * translation table walk for AT S1E0* or AT S1E1* insn
3525 * executed from NS EL1. If this is a synchronous external abort
3526 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3527 * to EL3. Otherwise the fault is taken as an exception to EL2,
3528 * and HPFAR_EL2 holds the faulting IPA.
3529 */
3530 if (fi.type == ARMFault_SyncExternalOnWalk &&
3531 (env->cp15.scr_el3 & SCR_EA)) {
3532 target_el = 3;
3533 } else {
3534 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
3535 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3536 env->cp15.hpfar_el2 |= HPFAR_NS;
3537 }
3538 target_el = 2;
3539 }
3540 take_exc = true;
3541 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3542 /*
3543 * Synchronous external aborts during a translation table walk
3544 * are taken as Data Abort exceptions.
3545 */
3546 if (fi.stage2) {
3547 if (current_el == 3) {
3548 target_el = 3;
3549 } else {
3550 target_el = 2;
3551 }
3552 } else {
3553 target_el = exception_target_el(env);
3554 }
3555 take_exc = true;
3556 }
3557
3558 if (take_exc) {
3559 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3560 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3561 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3562 fsr = arm_fi_to_lfsc(&fi);
3563 fsc = extract32(fsr, 0, 6);
3564 } else {
3565 fsr = arm_fi_to_sfsc(&fi);
3566 fsc = 0x3f;
3567 }
3568 /*
3569 * Report exception with ESR indicating a fault due to a
3570 * translation table walk for a cache maintenance instruction.
3571 */
3572 syn = syn_data_abort_no_iss(current_el == target_el, 0,
3573 fi.ea, 1, fi.s1ptw, 1, fsc);
3574 env->exception.vaddress = value;
3575 env->exception.fsr = fsr;
3576 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3577 }
3578 }
3579
3580 if (is_a64(env)) {
3581 format64 = true;
3582 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3583 /*
3584 * ATS1Cxx:
3585 * * TTBCR.EAE determines whether the result is returned using the
3586 * 32-bit or the 64-bit PAR format
3587 * * Instructions executed in Hyp mode always use the 64bit format
3588 *
3589 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3590 * * The Non-secure TTBCR.EAE bit is set to 1
3591 * * The implementation includes EL2, and the value of HCR.VM is 1
3592 *
3593 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3594 *
3595 * ATS1Hx always uses the 64bit format.
3596 */
3597 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3598
3599 if (arm_feature(env, ARM_FEATURE_EL2)) {
3600 if (mmu_idx == ARMMMUIdx_E10_0 ||
3601 mmu_idx == ARMMMUIdx_E10_1 ||
3602 mmu_idx == ARMMMUIdx_E10_1_PAN) {
3603 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
3604 } else {
3605 format64 |= arm_current_el(env) == 2;
3606 }
3607 }
3608 }
3609
3610 if (format64) {
3611 /* Create a 64-bit PAR */
3612 par64 = (1 << 11); /* LPAE bit always set */
3613 if (!ret) {
3614 par64 |= res.f.phys_addr & ~0xfffULL;
3615 if (!res.f.attrs.secure) {
3616 par64 |= (1 << 9); /* NS */
3617 }
3618 par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3619 par64 |= par_el1_shareability(&res) << 7; /* SH */
3620 } else {
3621 uint32_t fsr = arm_fi_to_lfsc(&fi);
3622
3623 par64 |= 1; /* F */
3624 par64 |= (fsr & 0x3f) << 1; /* FS */
3625 if (fi.stage2) {
3626 par64 |= (1 << 9); /* S */
3627 }
3628 if (fi.s1ptw) {
3629 par64 |= (1 << 8); /* PTW */
3630 }
3631 }
3632 } else {
3633 /*
3634 * fsr is a DFSR/IFSR value for the short descriptor
3635 * translation table format (with WnR always clear).
3636 * Convert it to a 32-bit PAR.
3637 */
3638 if (!ret) {
3639 /* We do not set any attribute bits in the PAR */
3640 if (res.f.lg_page_size == 24
3641 && arm_feature(env, ARM_FEATURE_V7)) {
3642 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
3643 } else {
3644 par64 = res.f.phys_addr & 0xfffff000;
3645 }
3646 if (!res.f.attrs.secure) {
3647 par64 |= (1 << 9); /* NS */
3648 }
3649 } else {
3650 uint32_t fsr = arm_fi_to_sfsc(&fi);
3651
3652 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3653 ((fsr & 0xf) << 1) | 1;
3654 }
3655 }
3656 return par64;
3657 }
3658 #endif /* CONFIG_TCG */
3659
ats_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3660 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3661 {
3662 #ifdef CONFIG_TCG
3663 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3664 uint64_t par64;
3665 ARMMMUIdx mmu_idx;
3666 int el = arm_current_el(env);
3667 ARMSecuritySpace ss = arm_security_space(env);
3668
3669 switch (ri->opc2 & 6) {
3670 case 0:
3671 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
3672 switch (el) {
3673 case 3:
3674 if (ri->crm == 9 && arm_pan_enabled(env)) {
3675 mmu_idx = ARMMMUIdx_E30_3_PAN;
3676 } else {
3677 mmu_idx = ARMMMUIdx_E3;
3678 }
3679 break;
3680 case 2:
3681 g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */
3682 /* fall through */
3683 case 1:
3684 if (ri->crm == 9 && arm_pan_enabled(env)) {
3685 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
3686 } else {
3687 mmu_idx = ARMMMUIdx_Stage1_E1;
3688 }
3689 break;
3690 default:
3691 g_assert_not_reached();
3692 }
3693 break;
3694 case 2:
3695 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3696 switch (el) {
3697 case 3:
3698 mmu_idx = ARMMMUIdx_E30_0;
3699 break;
3700 case 2:
3701 g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */
3702 mmu_idx = ARMMMUIdx_Stage1_E0;
3703 break;
3704 case 1:
3705 mmu_idx = ARMMMUIdx_Stage1_E0;
3706 break;
3707 default:
3708 g_assert_not_reached();
3709 }
3710 break;
3711 case 4:
3712 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
3713 mmu_idx = ARMMMUIdx_E10_1;
3714 ss = ARMSS_NonSecure;
3715 break;
3716 case 6:
3717 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
3718 mmu_idx = ARMMMUIdx_E10_0;
3719 ss = ARMSS_NonSecure;
3720 break;
3721 default:
3722 g_assert_not_reached();
3723 }
3724
3725 par64 = do_ats_write(env, value, access_type, mmu_idx, ss);
3726
3727 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3728 #else
3729 /* Handled by hardware accelerator. */
3730 g_assert_not_reached();
3731 #endif /* CONFIG_TCG */
3732 }
3733
ats1h_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3734 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3735 uint64_t value)
3736 {
3737 #ifdef CONFIG_TCG
3738 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3739 uint64_t par64;
3740
3741 /* There is no SecureEL2 for AArch32. */
3742 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2,
3743 ARMSS_NonSecure);
3744
3745 A32_BANKED_CURRENT_REG_SET(env, par, par64);
3746 #else
3747 /* Handled by hardware accelerator. */
3748 g_assert_not_reached();
3749 #endif /* CONFIG_TCG */
3750 }
3751
at_e012_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)3752 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri,
3753 bool isread)
3754 {
3755 /*
3756 * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level
3757 * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can
3758 * only happen when executing at EL3 because that combination also causes an
3759 * illegal exception return. We don't need to check FEAT_RME either, because
3760 * scr_write() ensures that the NSE bit is not set otherwise.
3761 */
3762 if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) {
3763 return CP_ACCESS_UNDEFINED;
3764 }
3765 return CP_ACCESS_OK;
3766 }
3767
at_s1e2_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)3768 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3769 bool isread)
3770 {
3771 if (arm_current_el(env) == 3 &&
3772 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
3773 return CP_ACCESS_UNDEFINED;
3774 }
3775 return at_e012_access(env, ri, isread);
3776 }
3777
at_s1e01_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)3778 static CPAccessResult at_s1e01_access(CPUARMState *env, const ARMCPRegInfo *ri,
3779 bool isread)
3780 {
3781 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_AT)) {
3782 return CP_ACCESS_TRAP_EL2;
3783 }
3784 return at_e012_access(env, ri, isread);
3785 }
3786
ats_write64(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3787 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3788 uint64_t value)
3789 {
3790 #ifdef CONFIG_TCG
3791 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
3792 ARMMMUIdx mmu_idx;
3793 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3794 bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
3795 bool for_el3 = false;
3796 ARMSecuritySpace ss;
3797
3798 switch (ri->opc2 & 6) {
3799 case 0:
3800 switch (ri->opc1) {
3801 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3802 if (ri->crm == 9 && arm_pan_enabled(env)) {
3803 mmu_idx = regime_e20 ?
3804 ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
3805 } else {
3806 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
3807 }
3808 break;
3809 case 4: /* AT S1E2R, AT S1E2W */
3810 mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
3811 break;
3812 case 6: /* AT S1E3R, AT S1E3W */
3813 mmu_idx = ARMMMUIdx_E3;
3814 for_el3 = true;
3815 break;
3816 default:
3817 g_assert_not_reached();
3818 }
3819 break;
3820 case 2: /* AT S1E0R, AT S1E0W */
3821 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
3822 break;
3823 case 4: /* AT S12E1R, AT S12E1W */
3824 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
3825 break;
3826 case 6: /* AT S12E0R, AT S12E0W */
3827 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
3828 break;
3829 default:
3830 g_assert_not_reached();
3831 }
3832
3833 ss = for_el3 ? arm_security_space(env) : arm_security_space_below_el3(env);
3834 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx, ss);
3835 #else
3836 /* Handled by hardware accelerator. */
3837 g_assert_not_reached();
3838 #endif /* CONFIG_TCG */
3839 }
3840 #endif
3841
3842 /* Return basic MPU access permission bits. */
simple_mpu_ap_bits(uint32_t val)3843 static uint32_t simple_mpu_ap_bits(uint32_t val)
3844 {
3845 uint32_t ret;
3846 uint32_t mask;
3847 int i;
3848 ret = 0;
3849 mask = 3;
3850 for (i = 0; i < 16; i += 2) {
3851 ret |= (val >> i) & mask;
3852 mask <<= 2;
3853 }
3854 return ret;
3855 }
3856
3857 /* Pad basic MPU access permission bits to extended format. */
extended_mpu_ap_bits(uint32_t val)3858 static uint32_t extended_mpu_ap_bits(uint32_t val)
3859 {
3860 uint32_t ret;
3861 uint32_t mask;
3862 int i;
3863 ret = 0;
3864 mask = 3;
3865 for (i = 0; i < 16; i += 2) {
3866 ret |= (val & mask) << i;
3867 mask <<= 2;
3868 }
3869 return ret;
3870 }
3871
pmsav5_data_ap_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3872 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3873 uint64_t value)
3874 {
3875 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
3876 }
3877
pmsav5_data_ap_read(CPUARMState * env,const ARMCPRegInfo * ri)3878 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3879 {
3880 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
3881 }
3882
pmsav5_insn_ap_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3883 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3884 uint64_t value)
3885 {
3886 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
3887 }
3888
pmsav5_insn_ap_read(CPUARMState * env,const ARMCPRegInfo * ri)3889 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
3890 {
3891 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
3892 }
3893
pmsav7_read(CPUARMState * env,const ARMCPRegInfo * ri)3894 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3895 {
3896 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3897
3898 if (!u32p) {
3899 return 0;
3900 }
3901
3902 u32p += env->pmsav7.rnr[M_REG_NS];
3903 return *u32p;
3904 }
3905
pmsav7_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3906 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3907 uint64_t value)
3908 {
3909 ARMCPU *cpu = env_archcpu(env);
3910 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3911
3912 if (!u32p) {
3913 return;
3914 }
3915
3916 u32p += env->pmsav7.rnr[M_REG_NS];
3917 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3918 *u32p = value;
3919 }
3920
pmsav7_rgnr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3921 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3922 uint64_t value)
3923 {
3924 ARMCPU *cpu = env_archcpu(env);
3925 uint32_t nrgs = cpu->pmsav7_dregion;
3926
3927 if (value >= nrgs) {
3928 qemu_log_mask(LOG_GUEST_ERROR,
3929 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3930 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3931 return;
3932 }
3933
3934 raw_write(env, ri, value);
3935 }
3936
prbar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3937 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3938 uint64_t value)
3939 {
3940 ARMCPU *cpu = env_archcpu(env);
3941
3942 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3943 env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3944 }
3945
prbar_read(CPUARMState * env,const ARMCPRegInfo * ri)3946 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3947 {
3948 return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3949 }
3950
prlar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3951 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3952 uint64_t value)
3953 {
3954 ARMCPU *cpu = env_archcpu(env);
3955
3956 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3957 env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3958 }
3959
prlar_read(CPUARMState * env,const ARMCPRegInfo * ri)3960 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3961 {
3962 return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3963 }
3964
prselr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3965 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3966 uint64_t value)
3967 {
3968 ARMCPU *cpu = env_archcpu(env);
3969
3970 /*
3971 * Ignore writes that would select not implemented region.
3972 * This is architecturally UNPREDICTABLE.
3973 */
3974 if (value >= cpu->pmsav7_dregion) {
3975 return;
3976 }
3977
3978 env->pmsav7.rnr[M_REG_NS] = value;
3979 }
3980
hprbar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3981 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3982 uint64_t value)
3983 {
3984 ARMCPU *cpu = env_archcpu(env);
3985
3986 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3987 env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3988 }
3989
hprbar_read(CPUARMState * env,const ARMCPRegInfo * ri)3990 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3991 {
3992 return env->pmsav8.hprbar[env->pmsav8.hprselr];
3993 }
3994
hprlar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)3995 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3996 uint64_t value)
3997 {
3998 ARMCPU *cpu = env_archcpu(env);
3999
4000 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4001 env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
4002 }
4003
hprlar_read(CPUARMState * env,const ARMCPRegInfo * ri)4004 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
4005 {
4006 return env->pmsav8.hprlar[env->pmsav8.hprselr];
4007 }
4008
hprenr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4009 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4010 uint64_t value)
4011 {
4012 uint32_t n;
4013 uint32_t bit;
4014 ARMCPU *cpu = env_archcpu(env);
4015
4016 /* Ignore writes to unimplemented regions */
4017 int rmax = MIN(cpu->pmsav8r_hdregion, 32);
4018 value &= MAKE_64BIT_MASK(0, rmax);
4019
4020 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4021
4022 /* Register alias is only valid for first 32 indexes */
4023 for (n = 0; n < rmax; ++n) {
4024 bit = extract32(value, n, 1);
4025 env->pmsav8.hprlar[n] = deposit32(
4026 env->pmsav8.hprlar[n], 0, 1, bit);
4027 }
4028 }
4029
hprenr_read(CPUARMState * env,const ARMCPRegInfo * ri)4030 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4031 {
4032 uint32_t n;
4033 uint32_t result = 0x0;
4034 ARMCPU *cpu = env_archcpu(env);
4035
4036 /* Register alias is only valid for first 32 indexes */
4037 for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
4038 if (env->pmsav8.hprlar[n] & 0x1) {
4039 result |= (0x1 << n);
4040 }
4041 }
4042 return result;
4043 }
4044
hprselr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4045 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4046 uint64_t value)
4047 {
4048 ARMCPU *cpu = env_archcpu(env);
4049
4050 /*
4051 * Ignore writes that would select not implemented region.
4052 * This is architecturally UNPREDICTABLE.
4053 */
4054 if (value >= cpu->pmsav8r_hdregion) {
4055 return;
4056 }
4057
4058 env->pmsav8.hprselr = value;
4059 }
4060
pmsav8r_regn_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4061 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
4062 uint64_t value)
4063 {
4064 ARMCPU *cpu = env_archcpu(env);
4065 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4066 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4067
4068 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
4069
4070 if (ri->opc1 & 4) {
4071 if (index >= cpu->pmsav8r_hdregion) {
4072 return;
4073 }
4074 if (ri->opc2 & 0x1) {
4075 env->pmsav8.hprlar[index] = value;
4076 } else {
4077 env->pmsav8.hprbar[index] = value;
4078 }
4079 } else {
4080 if (index >= cpu->pmsav7_dregion) {
4081 return;
4082 }
4083 if (ri->opc2 & 0x1) {
4084 env->pmsav8.rlar[M_REG_NS][index] = value;
4085 } else {
4086 env->pmsav8.rbar[M_REG_NS][index] = value;
4087 }
4088 }
4089 }
4090
pmsav8r_regn_read(CPUARMState * env,const ARMCPRegInfo * ri)4091 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
4092 {
4093 ARMCPU *cpu = env_archcpu(env);
4094 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
4095 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
4096
4097 if (ri->opc1 & 4) {
4098 if (index >= cpu->pmsav8r_hdregion) {
4099 return 0x0;
4100 }
4101 if (ri->opc2 & 0x1) {
4102 return env->pmsav8.hprlar[index];
4103 } else {
4104 return env->pmsav8.hprbar[index];
4105 }
4106 } else {
4107 if (index >= cpu->pmsav7_dregion) {
4108 return 0x0;
4109 }
4110 if (ri->opc2 & 0x1) {
4111 return env->pmsav8.rlar[M_REG_NS][index];
4112 } else {
4113 return env->pmsav8.rbar[M_REG_NS][index];
4114 }
4115 }
4116 }
4117
4118 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
4119 { .name = "PRBAR",
4120 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
4121 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4122 .accessfn = access_tvm_trvm,
4123 .readfn = prbar_read, .writefn = prbar_write },
4124 { .name = "PRLAR",
4125 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
4126 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4127 .accessfn = access_tvm_trvm,
4128 .readfn = prlar_read, .writefn = prlar_write },
4129 { .name = "PRSELR", .resetvalue = 0,
4130 .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
4131 .access = PL1_RW, .accessfn = access_tvm_trvm,
4132 .writefn = prselr_write,
4133 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
4134 { .name = "HPRBAR", .resetvalue = 0,
4135 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
4136 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4137 .readfn = hprbar_read, .writefn = hprbar_write },
4138 { .name = "HPRLAR",
4139 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
4140 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4141 .readfn = hprlar_read, .writefn = hprlar_write },
4142 { .name = "HPRSELR", .resetvalue = 0,
4143 .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
4144 .access = PL2_RW,
4145 .writefn = hprselr_write,
4146 .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
4147 { .name = "HPRENR",
4148 .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
4149 .access = PL2_RW, .type = ARM_CP_NO_RAW,
4150 .readfn = hprenr_read, .writefn = hprenr_write },
4151 };
4152
4153 static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
4154 /*
4155 * Reset for all these registers is handled in arm_cpu_reset(),
4156 * because the PMSAv7 is also used by M-profile CPUs, which do
4157 * not register cpregs but still need the state to be reset.
4158 */
4159 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
4160 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4161 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
4162 .readfn = pmsav7_read, .writefn = pmsav7_write,
4163 .resetfn = arm_cp_reset_ignore },
4164 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
4165 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4166 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
4167 .readfn = pmsav7_read, .writefn = pmsav7_write,
4168 .resetfn = arm_cp_reset_ignore },
4169 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4170 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4171 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
4172 .readfn = pmsav7_read, .writefn = pmsav7_write,
4173 .resetfn = arm_cp_reset_ignore },
4174 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4175 .access = PL1_RW,
4176 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
4177 .writefn = pmsav7_rgnr_write,
4178 .resetfn = arm_cp_reset_ignore },
4179 };
4180
4181 static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4182 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4183 .access = PL1_RW, .type = ARM_CP_ALIAS,
4184 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4185 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4186 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4187 .access = PL1_RW, .type = ARM_CP_ALIAS,
4188 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4189 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4190 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4191 .access = PL1_RW,
4192 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4193 .resetvalue = 0, },
4194 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4195 .access = PL1_RW,
4196 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4197 .resetvalue = 0, },
4198 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4199 .access = PL1_RW,
4200 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4201 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4202 .access = PL1_RW,
4203 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
4204 /* Protection region base and size registers */
4205 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4206 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4207 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4208 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4209 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4210 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4211 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4212 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4213 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4214 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4215 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4216 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4217 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4218 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4219 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4220 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4221 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4222 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4223 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4224 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4225 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4226 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4227 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4228 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
4229 };
4230
vmsa_ttbcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4231 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4232 uint64_t value)
4233 {
4234 ARMCPU *cpu = env_archcpu(env);
4235
4236 if (!arm_feature(env, ARM_FEATURE_V8)) {
4237 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
4238 /*
4239 * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4240 * using Long-descriptor translation table format
4241 */
4242 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4243 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
4244 /*
4245 * In an implementation that includes the Security Extensions
4246 * TTBCR has additional fields PD0 [4] and PD1 [5] for
4247 * Short-descriptor translation table format.
4248 */
4249 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4250 } else {
4251 value &= TTBCR_N;
4252 }
4253 }
4254
4255 if (arm_feature(env, ARM_FEATURE_LPAE)) {
4256 /*
4257 * With LPAE the TTBCR could result in a change of ASID
4258 * via the TTBCR.A1 bit, so do a TLB flush.
4259 */
4260 tlb_flush(CPU(cpu));
4261 }
4262 raw_write(env, ri, value);
4263 }
4264
vmsa_tcr_el12_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4265 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
4266 uint64_t value)
4267 {
4268 ARMCPU *cpu = env_archcpu(env);
4269
4270 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
4271 tlb_flush(CPU(cpu));
4272 raw_write(env, ri, value);
4273 }
4274
vmsa_ttbr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4275 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4276 uint64_t value)
4277 {
4278 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
4279 if (cpreg_field_is_64bit(ri) &&
4280 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4281 ARMCPU *cpu = env_archcpu(env);
4282 tlb_flush(CPU(cpu));
4283 }
4284 raw_write(env, ri, value);
4285 }
4286
vmsa_tcr_ttbr_el2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4287 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4288 uint64_t value)
4289 {
4290 /*
4291 * If we are running with E2&0 regime, then an ASID is active.
4292 * Flush if that might be changing. Note we're not checking
4293 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4294 * holds the active ASID, only checking the field that might.
4295 */
4296 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4297 (arm_hcr_el2_eff(env) & HCR_E2H)) {
4298 uint16_t mask = ARMMMUIdxBit_E20_2 |
4299 ARMMMUIdxBit_E20_2_PAN |
4300 ARMMMUIdxBit_E20_0;
4301 tlb_flush_by_mmuidx(env_cpu(env), mask);
4302 }
4303 raw_write(env, ri, value);
4304 }
4305
vttbr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4306 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4307 uint64_t value)
4308 {
4309 ARMCPU *cpu = env_archcpu(env);
4310 CPUState *cs = CPU(cpu);
4311
4312 /*
4313 * A change in VMID to the stage2 page table (Stage2) invalidates
4314 * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
4315 */
4316 if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
4317 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
4318 }
4319 raw_write(env, ri, value);
4320 }
4321
4322 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
4323 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
4324 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4325 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
4326 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
4327 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
4328 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4329 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4330 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
4331 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
4332 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
4333 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4334 offsetof(CPUARMState, cp15.dfar_ns) } },
4335 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4336 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
4337 .access = PL1_RW, .accessfn = access_tvm_trvm,
4338 .fgt = FGT_FAR_EL1,
4339 .nv2_redirect_offset = 0x220 | NV2_REDIR_NV1,
4340 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
4341 .resetvalue = 0, },
4342 };
4343
4344 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
4345 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4346 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
4347 .access = PL1_RW, .accessfn = access_tvm_trvm,
4348 .fgt = FGT_ESR_EL1,
4349 .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1,
4350 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
4351 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
4352 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
4353 .access = PL1_RW, .accessfn = access_tvm_trvm,
4354 .fgt = FGT_TTBR0_EL1,
4355 .nv2_redirect_offset = 0x200 | NV2_REDIR_NV1,
4356 .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4357 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4358 offsetof(CPUARMState, cp15.ttbr0_ns) } },
4359 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
4360 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
4361 .access = PL1_RW, .accessfn = access_tvm_trvm,
4362 .fgt = FGT_TTBR1_EL1,
4363 .nv2_redirect_offset = 0x210 | NV2_REDIR_NV1,
4364 .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write,
4365 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4366 offsetof(CPUARMState, cp15.ttbr1_ns) } },
4367 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4368 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4369 .access = PL1_RW, .accessfn = access_tvm_trvm,
4370 .fgt = FGT_TCR_EL1,
4371 .nv2_redirect_offset = 0x120 | NV2_REDIR_NV1,
4372 .writefn = vmsa_tcr_el12_write,
4373 .raw_writefn = raw_write,
4374 .resetvalue = 0,
4375 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
4376 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
4377 .access = PL1_RW, .accessfn = access_tvm_trvm,
4378 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
4379 .raw_writefn = raw_write,
4380 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4381 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
4382 };
4383
4384 /*
4385 * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
4386 * qemu tlbs nor adjusting cached masks.
4387 */
4388 static const ARMCPRegInfo ttbcr2_reginfo = {
4389 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
4390 .access = PL1_RW, .accessfn = access_tvm_trvm,
4391 .type = ARM_CP_ALIAS,
4392 .bank_fieldoffsets = {
4393 offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4394 offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
4395 },
4396 };
4397
omap_ticonfig_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4398 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4399 uint64_t value)
4400 {
4401 env->cp15.c15_ticonfig = value & 0xe7;
4402 /* The OS_TYPE bit in this register changes the reported CPUID! */
4403 env->cp15.c0_cpuid = (value & (1 << 5)) ?
4404 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
4405 }
4406
omap_threadid_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4407 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4408 uint64_t value)
4409 {
4410 env->cp15.c15_threadid = value & 0xffff;
4411 }
4412
omap_wfi_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4413 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4414 uint64_t value)
4415 {
4416 /* Wait-for-interrupt (deprecated) */
4417 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
4418 }
4419
omap_cachemaint_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4420 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4421 uint64_t value)
4422 {
4423 /*
4424 * On OMAP there are registers indicating the max/min index of dcache lines
4425 * containing a dirty line; cache flush operations have to reset these.
4426 */
4427 env->cp15.c15_i_max = 0x000;
4428 env->cp15.c15_i_min = 0xff0;
4429 }
4430
4431 static const ARMCPRegInfo omap_cp_reginfo[] = {
4432 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4433 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
4434 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
4435 .resetvalue = 0, },
4436 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4437 .access = PL1_RW, .type = ARM_CP_NOP },
4438 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4439 .access = PL1_RW,
4440 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4441 .writefn = omap_ticonfig_write },
4442 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4443 .access = PL1_RW,
4444 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4445 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4446 .access = PL1_RW, .resetvalue = 0xff0,
4447 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4448 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4449 .access = PL1_RW,
4450 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4451 .writefn = omap_threadid_write },
4452 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4453 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4454 .type = ARM_CP_NO_RAW,
4455 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
4456 /*
4457 * TODO: Peripheral port remap register:
4458 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4459 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4460 * when MMU is off.
4461 */
4462 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
4463 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
4464 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
4465 .writefn = omap_cachemaint_write },
4466 { .name = "C9", .cp = 15, .crn = 9,
4467 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4468 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
4469 };
4470
xscale_cpar_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4471 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4472 uint64_t value)
4473 {
4474 env->cp15.c15_cpar = value & 0x3fff;
4475 }
4476
4477 static const ARMCPRegInfo xscale_cp_reginfo[] = {
4478 { .name = "XSCALE_CPAR",
4479 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4480 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4481 .writefn = xscale_cpar_write, },
4482 { .name = "XSCALE_AUXCR",
4483 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4484 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4485 .resetvalue = 0, },
4486 /*
4487 * XScale specific cache-lockdown: since we have no cache we NOP these
4488 * and hope the guest does not really rely on cache behaviour.
4489 */
4490 { .name = "XSCALE_LOCK_ICACHE_LINE",
4491 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4492 .access = PL1_W, .type = ARM_CP_NOP },
4493 { .name = "XSCALE_UNLOCK_ICACHE",
4494 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4495 .access = PL1_W, .type = ARM_CP_NOP },
4496 { .name = "XSCALE_DCACHE_LOCK",
4497 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4498 .access = PL1_RW, .type = ARM_CP_NOP },
4499 { .name = "XSCALE_UNLOCK_DCACHE",
4500 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4501 .access = PL1_W, .type = ARM_CP_NOP },
4502 };
4503
4504 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
4505 /*
4506 * RAZ/WI the whole crn=15 space, when we don't have a more specific
4507 * implementation of this implementation-defined space.
4508 * Ideally this should eventually disappear in favour of actually
4509 * implementing the correct behaviour for all cores.
4510 */
4511 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4512 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4513 .access = PL1_RW,
4514 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
4515 .resetvalue = 0 },
4516 };
4517
4518 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4519 /* Cache status: RAZ because we have no cache so it's always clean */
4520 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
4521 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4522 .resetvalue = 0 },
4523 };
4524
4525 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
4526 /* We never have a block transfer operation in progress */
4527 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
4528 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4529 .resetvalue = 0 },
4530 /* The cache ops themselves: these all NOP for QEMU */
4531 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
4532 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4533 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
4534 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4535 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
4536 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4537 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
4538 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4539 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
4540 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4541 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
4542 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
4543 };
4544
4545 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
4546 /*
4547 * The cache test-and-clean instructions always return (1 << 30)
4548 * to indicate that there are no dirty cache lines.
4549 */
4550 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
4551 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4552 .resetvalue = (1 << 30) },
4553 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
4554 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
4555 .resetvalue = (1 << 30) },
4556 };
4557
4558 static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4559 /* Ignore ReadBuffer accesses */
4560 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4561 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
4562 .access = PL1_RW, .resetvalue = 0,
4563 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
4564 };
4565
midr_read(CPUARMState * env,const ARMCPRegInfo * ri)4566 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4567 {
4568 unsigned int cur_el = arm_current_el(env);
4569
4570 if (arm_is_el2_enabled(env) && cur_el == 1) {
4571 return env->cp15.vpidr_el2;
4572 }
4573 return raw_read(env, ri);
4574 }
4575
mpidr_read_val(CPUARMState * env)4576 static uint64_t mpidr_read_val(CPUARMState *env)
4577 {
4578 ARMCPU *cpu = env_archcpu(env);
4579 uint64_t mpidr = cpu->mp_affinity;
4580
4581 if (arm_feature(env, ARM_FEATURE_V7MP)) {
4582 mpidr |= (1U << 31);
4583 /*
4584 * Cores which are uniprocessor (non-coherent)
4585 * but still implement the MP extensions set
4586 * bit 30. (For instance, Cortex-R5).
4587 */
4588 if (cpu->mp_is_up) {
4589 mpidr |= (1u << 30);
4590 }
4591 }
4592 return mpidr;
4593 }
4594
mpidr_read(CPUARMState * env,const ARMCPRegInfo * ri)4595 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4596 {
4597 unsigned int cur_el = arm_current_el(env);
4598
4599 if (arm_is_el2_enabled(env) && cur_el == 1) {
4600 return env->cp15.vmpidr_el2;
4601 }
4602 return mpidr_read_val(env);
4603 }
4604
4605 static const ARMCPRegInfo lpae_cp_reginfo[] = {
4606 /* NOP AMAIR0/1 */
4607 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4608 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
4609 .access = PL1_RW, .accessfn = access_tvm_trvm,
4610 .fgt = FGT_AMAIR_EL1,
4611 .nv2_redirect_offset = 0x148 | NV2_REDIR_NV1,
4612 .type = ARM_CP_CONST, .resetvalue = 0 },
4613 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
4614 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
4615 .access = PL1_RW, .accessfn = access_tvm_trvm,
4616 .type = ARM_CP_CONST, .resetvalue = 0 },
4617 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
4618 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4619 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4620 offsetof(CPUARMState, cp15.par_ns)} },
4621 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
4622 .access = PL1_RW, .accessfn = access_tvm_trvm,
4623 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4624 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4625 offsetof(CPUARMState, cp15.ttbr0_ns) },
4626 .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4627 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
4628 .access = PL1_RW, .accessfn = access_tvm_trvm,
4629 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
4630 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4631 offsetof(CPUARMState, cp15.ttbr1_ns) },
4632 .writefn = vmsa_ttbr_write, .raw_writefn = raw_write },
4633 };
4634
aa64_fpcr_read(CPUARMState * env,const ARMCPRegInfo * ri)4635 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4636 {
4637 return vfp_get_fpcr(env);
4638 }
4639
aa64_fpcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4640 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4641 uint64_t value)
4642 {
4643 vfp_set_fpcr(env, value);
4644 }
4645
aa64_fpsr_read(CPUARMState * env,const ARMCPRegInfo * ri)4646 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4647 {
4648 return vfp_get_fpsr(env);
4649 }
4650
aa64_fpsr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4651 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4652 uint64_t value)
4653 {
4654 vfp_set_fpsr(env, value);
4655 }
4656
aa64_daif_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4657 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4658 bool isread)
4659 {
4660 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
4661 return CP_ACCESS_TRAP_EL1;
4662 }
4663 return CP_ACCESS_OK;
4664 }
4665
aa64_daif_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4666 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4667 uint64_t value)
4668 {
4669 env->daif = value & PSTATE_DAIF;
4670 }
4671
aa64_pan_read(CPUARMState * env,const ARMCPRegInfo * ri)4672 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4673 {
4674 return env->pstate & PSTATE_PAN;
4675 }
4676
aa64_pan_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4677 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4678 uint64_t value)
4679 {
4680 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4681 }
4682
4683 static const ARMCPRegInfo pan_reginfo = {
4684 .name = "PAN", .state = ARM_CP_STATE_AA64,
4685 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4686 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4687 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4688 };
4689
aa64_uao_read(CPUARMState * env,const ARMCPRegInfo * ri)4690 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4691 {
4692 return env->pstate & PSTATE_UAO;
4693 }
4694
aa64_uao_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4695 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4696 uint64_t value)
4697 {
4698 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4699 }
4700
4701 static const ARMCPRegInfo uao_reginfo = {
4702 .name = "UAO", .state = ARM_CP_STATE_AA64,
4703 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4704 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4705 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4706 };
4707
aa64_dit_read(CPUARMState * env,const ARMCPRegInfo * ri)4708 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4709 {
4710 return env->pstate & PSTATE_DIT;
4711 }
4712
aa64_dit_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4713 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4714 uint64_t value)
4715 {
4716 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4717 }
4718
4719 static const ARMCPRegInfo dit_reginfo = {
4720 .name = "DIT", .state = ARM_CP_STATE_AA64,
4721 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4722 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4723 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4724 };
4725
aa64_ssbs_read(CPUARMState * env,const ARMCPRegInfo * ri)4726 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4727 {
4728 return env->pstate & PSTATE_SSBS;
4729 }
4730
aa64_ssbs_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4731 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4732 uint64_t value)
4733 {
4734 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4735 }
4736
4737 static const ARMCPRegInfo ssbs_reginfo = {
4738 .name = "SSBS", .state = ARM_CP_STATE_AA64,
4739 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4740 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4741 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4742 };
4743
aa64_cacheop_poc_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4744 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4745 const ARMCPRegInfo *ri,
4746 bool isread)
4747 {
4748 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4749 switch (arm_current_el(env)) {
4750 case 0:
4751 /* ... EL0 must trap to EL1 unless SCTLR_EL1.UCI is set. */
4752 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4753 return CP_ACCESS_TRAP_EL1;
4754 }
4755 /* fall through */
4756 case 1:
4757 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4758 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4759 return CP_ACCESS_TRAP_EL2;
4760 }
4761 break;
4762 }
4763 return CP_ACCESS_OK;
4764 }
4765
do_cacheop_pou_access(CPUARMState * env,uint64_t hcrflags)4766 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
4767 {
4768 /* Cache invalidate/clean to Point of Unification... */
4769 switch (arm_current_el(env)) {
4770 case 0:
4771 /* ... EL0 must trap to EL1 unless SCTLR_EL1.UCI is set. */
4772 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4773 return CP_ACCESS_TRAP_EL1;
4774 }
4775 /* fall through */
4776 case 1:
4777 /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set. */
4778 if (arm_hcr_el2_eff(env) & hcrflags) {
4779 return CP_ACCESS_TRAP_EL2;
4780 }
4781 break;
4782 }
4783 return CP_ACCESS_OK;
4784 }
4785
access_ticab(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4786 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4787 bool isread)
4788 {
4789 return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4790 }
4791
access_tocu(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4792 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4793 bool isread)
4794 {
4795 return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4796 }
4797
aa64_zva_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4798 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
4799 bool isread)
4800 {
4801 int cur_el = arm_current_el(env);
4802
4803 if (cur_el < 2) {
4804 uint64_t hcr = arm_hcr_el2_eff(env);
4805
4806 if (cur_el == 0) {
4807 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
4808 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
4809 return CP_ACCESS_TRAP_EL2;
4810 }
4811 } else {
4812 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
4813 return CP_ACCESS_TRAP_EL1;
4814 }
4815 if (hcr & HCR_TDZ) {
4816 return CP_ACCESS_TRAP_EL2;
4817 }
4818 }
4819 } else if (hcr & HCR_TDZ) {
4820 return CP_ACCESS_TRAP_EL2;
4821 }
4822 }
4823 return CP_ACCESS_OK;
4824 }
4825
aa64_dczid_read(CPUARMState * env,const ARMCPRegInfo * ri)4826 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
4827 {
4828 ARMCPU *cpu = env_archcpu(env);
4829 int dzp_bit = 1 << 4;
4830
4831 /* DZP indicates whether DC ZVA access is allowed */
4832 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
4833 dzp_bit = 0;
4834 }
4835 return cpu->dcz_blocksize | dzp_bit;
4836 }
4837
sp_el0_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4838 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
4839 bool isread)
4840 {
4841 if (!(env->pstate & PSTATE_SP)) {
4842 /*
4843 * Access to SP_EL0 is undefined if it's being used as
4844 * the stack pointer.
4845 */
4846 return CP_ACCESS_UNDEFINED;
4847 }
4848 return CP_ACCESS_OK;
4849 }
4850
spsel_read(CPUARMState * env,const ARMCPRegInfo * ri)4851 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
4852 {
4853 return env->pstate & PSTATE_SP;
4854 }
4855
spsel_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t val)4856 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
4857 {
4858 update_spsel(env, val);
4859 }
4860
sctlr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4861 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4862 uint64_t value)
4863 {
4864 ARMCPU *cpu = env_archcpu(env);
4865
4866 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
4867 /* M bit is RAZ/WI for PMSA with no MPU implemented */
4868 value &= ~SCTLR_M;
4869 }
4870
4871 /* ??? Lots of these bits are not implemented. */
4872
4873 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
4874 if (ri->opc1 == 6) { /* SCTLR_EL3 */
4875 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
4876 } else {
4877 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
4878 SCTLR_ATA0 | SCTLR_ATA);
4879 }
4880 }
4881
4882 if (raw_read(env, ri) == value) {
4883 /*
4884 * Skip the TLB flush if nothing actually changed; Linux likes
4885 * to do a lot of pointless SCTLR writes.
4886 */
4887 return;
4888 }
4889
4890 raw_write(env, ri, value);
4891
4892 /* This may enable/disable the MMU, so do a TLB flush. */
4893 tlb_flush(CPU(cpu));
4894
4895 if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
4896 /*
4897 * Normally we would always end the TB on an SCTLR write; see the
4898 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
4899 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
4900 * of hflags from the translator, so do it here.
4901 */
4902 arm_rebuild_hflags(env);
4903 }
4904 }
4905
mdcr_el3_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4906 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4907 uint64_t value)
4908 {
4909 /*
4910 * Some MDCR_EL3 bits affect whether PMU counters are running:
4911 * if we are trying to change any of those then we must
4912 * bracket this update with PMU start/finish calls.
4913 */
4914 bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
4915
4916 if (pmu_op) {
4917 pmu_op_start(env);
4918 }
4919 env->cp15.mdcr_el3 = value;
4920 if (pmu_op) {
4921 pmu_op_finish(env);
4922 }
4923 }
4924
sdcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4925 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4926 uint64_t value)
4927 {
4928 /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
4929 mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
4930 }
4931
mdcr_el2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4932 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4933 uint64_t value)
4934 {
4935 /*
4936 * Some MDCR_EL2 bits affect whether PMU counters are running:
4937 * if we are trying to change any of those then we must
4938 * bracket this update with PMU start/finish calls.
4939 */
4940 bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
4941
4942 if (pmu_op) {
4943 pmu_op_start(env);
4944 }
4945 env->cp15.mdcr_el2 = value;
4946 if (pmu_op) {
4947 pmu_op_finish(env);
4948 }
4949 }
4950
access_nv1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)4951 static CPAccessResult access_nv1(CPUARMState *env, const ARMCPRegInfo *ri,
4952 bool isread)
4953 {
4954 if (arm_current_el(env) == 1) {
4955 uint64_t hcr_nv = arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1 | HCR_NV2);
4956
4957 if (hcr_nv == (HCR_NV | HCR_NV1)) {
4958 return CP_ACCESS_TRAP_EL2;
4959 }
4960 }
4961 return CP_ACCESS_OK;
4962 }
4963
4964 #ifdef CONFIG_USER_ONLY
4965 /*
4966 * `IC IVAU` is handled to improve compatibility with JITs that dual-map their
4967 * code to get around W^X restrictions, where one region is writable and the
4968 * other is executable.
4969 *
4970 * Since the executable region is never written to we cannot detect code
4971 * changes when running in user mode, and rely on the emulated JIT telling us
4972 * that the code has changed by executing this instruction.
4973 */
ic_ivau_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)4974 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri,
4975 uint64_t value)
4976 {
4977 uint64_t icache_line_mask, start_address, end_address;
4978 const ARMCPU *cpu;
4979
4980 cpu = env_archcpu(env);
4981
4982 icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1;
4983 start_address = value & ~icache_line_mask;
4984 end_address = value | icache_line_mask;
4985
4986 mmap_lock();
4987
4988 tb_invalidate_phys_range(start_address, end_address);
4989
4990 mmap_unlock();
4991 }
4992 #endif
4993
4994 static const ARMCPRegInfo v8_cp_reginfo[] = {
4995 /*
4996 * Minimal set of EL0-visible registers. This will need to be expanded
4997 * significantly for system emulation of AArch64 CPUs.
4998 */
4999 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5000 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5001 .access = PL0_RW, .type = ARM_CP_NZCV },
5002 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5003 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
5004 .type = ARM_CP_NO_RAW,
5005 .access = PL0_RW, .accessfn = aa64_daif_access,
5006 .fieldoffset = offsetof(CPUARMState, daif),
5007 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
5008 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5009 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
5010 .access = PL0_RW, .type = ARM_CP_FPU,
5011 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
5012 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5013 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
5014 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
5015 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
5016 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5017 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
5018 .access = PL0_R, .type = ARM_CP_NO_RAW,
5019 .fgt = FGT_DCZID_EL0,
5020 .readfn = aa64_dczid_read },
5021 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5022 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5023 .access = PL0_W, .type = ARM_CP_DC_ZVA,
5024 #ifndef CONFIG_USER_ONLY
5025 /* Avoid overhead of an access check that always passes in user-mode */
5026 .accessfn = aa64_zva_access,
5027 .fgt = FGT_DCZVA,
5028 #endif
5029 },
5030 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5031 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5032 .access = PL1_R, .type = ARM_CP_CURRENTEL },
5033 /*
5034 * Instruction cache ops. All of these except `IC IVAU` NOP because we
5035 * don't emulate caches.
5036 */
5037 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5038 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5039 .access = PL1_W, .type = ARM_CP_NOP,
5040 .fgt = FGT_ICIALLUIS,
5041 .accessfn = access_ticab },
5042 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5043 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5044 .access = PL1_W, .type = ARM_CP_NOP,
5045 .fgt = FGT_ICIALLU,
5046 .accessfn = access_tocu },
5047 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5048 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5049 .access = PL0_W,
5050 .fgt = FGT_ICIVAU,
5051 .accessfn = access_tocu,
5052 #ifdef CONFIG_USER_ONLY
5053 .type = ARM_CP_NO_RAW,
5054 .writefn = ic_ivau_write
5055 #else
5056 .type = ARM_CP_NOP
5057 #endif
5058 },
5059 /* Cache ops: all NOPs since we don't emulate caches */
5060 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5061 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5062 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
5063 .fgt = FGT_DCIVAC,
5064 .type = ARM_CP_NOP },
5065 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5066 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5067 .fgt = FGT_DCISW,
5068 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5069 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5070 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5071 .access = PL0_W, .type = ARM_CP_NOP,
5072 .fgt = FGT_DCCVAC,
5073 .accessfn = aa64_cacheop_poc_access },
5074 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5075 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5076 .fgt = FGT_DCCSW,
5077 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5078 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5079 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5080 .access = PL0_W, .type = ARM_CP_NOP,
5081 .fgt = FGT_DCCVAU,
5082 .accessfn = access_tocu },
5083 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5084 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5085 .access = PL0_W, .type = ARM_CP_NOP,
5086 .fgt = FGT_DCCIVAC,
5087 .accessfn = aa64_cacheop_poc_access },
5088 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5089 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5090 .fgt = FGT_DCCISW,
5091 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
5092 #ifndef CONFIG_USER_ONLY
5093 /* 64 bit address translation operations */
5094 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5095 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
5096 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5097 .fgt = FGT_ATS1E1R,
5098 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5099 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5100 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
5101 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5102 .fgt = FGT_ATS1E1W,
5103 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5104 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5105 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
5106 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5107 .fgt = FGT_ATS1E0R,
5108 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5109 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5110 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
5111 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5112 .fgt = FGT_ATS1E0W,
5113 .accessfn = at_s1e01_access, .writefn = ats_write64 },
5114 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
5115 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
5116 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5117 .accessfn = at_e012_access, .writefn = ats_write64 },
5118 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
5119 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
5120 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5121 .accessfn = at_e012_access, .writefn = ats_write64 },
5122 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
5123 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
5124 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5125 .accessfn = at_e012_access, .writefn = ats_write64 },
5126 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
5127 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
5128 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5129 .accessfn = at_e012_access, .writefn = ats_write64 },
5130 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5131 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5132 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
5133 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5134 .writefn = ats_write64 },
5135 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5136 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
5137 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5138 .writefn = ats_write64 },
5139 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5140 .type = ARM_CP_ALIAS,
5141 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5142 .access = PL1_RW, .resetvalue = 0,
5143 .fgt = FGT_PAR_EL1,
5144 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5145 .writefn = par_write },
5146 #endif
5147 /* 32 bit cache operations */
5148 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
5149 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
5150 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5151 .type = ARM_CP_NOP, .access = PL1_W },
5152 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
5153 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5154 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
5155 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5156 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5157 .type = ARM_CP_NOP, .access = PL1_W },
5158 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5159 .type = ARM_CP_NOP, .access = PL1_W },
5160 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
5161 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5162 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
5163 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5164 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
5165 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5166 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
5167 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5168 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
5169 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
5170 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
5171 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
5172 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
5173 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
5174 /* MMU Domain access control / MPU write buffer control */
5175 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
5176 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
5177 .writefn = dacr_write, .raw_writefn = raw_write,
5178 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5179 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
5180 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
5181 .type = ARM_CP_ALIAS,
5182 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
5183 .access = PL1_RW, .accessfn = access_nv1,
5184 .nv2_redirect_offset = 0x230 | NV2_REDIR_NV1,
5185 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
5186 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
5187 .type = ARM_CP_ALIAS,
5188 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
5189 .access = PL1_RW, .accessfn = access_nv1,
5190 .nv2_redirect_offset = 0x160 | NV2_REDIR_NV1,
5191 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
5192 /*
5193 * We rely on the access checks not allowing the guest to write to the
5194 * state field when SPSel indicates that it's being used as the stack
5195 * pointer.
5196 */
5197 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5198 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5199 .access = PL1_RW, .accessfn = sp_el0_access,
5200 .type = ARM_CP_ALIAS,
5201 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
5202 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5203 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
5204 .nv2_redirect_offset = 0x240,
5205 .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
5206 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
5207 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5208 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
5209 .type = ARM_CP_NO_RAW,
5210 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
5211 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5212 .type = ARM_CP_ALIAS,
5213 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5214 .access = PL2_RW,
5215 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5216 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5217 .type = ARM_CP_ALIAS,
5218 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5219 .access = PL2_RW,
5220 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5221 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5222 .type = ARM_CP_ALIAS,
5223 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5224 .access = PL2_RW,
5225 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5226 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5227 .type = ARM_CP_ALIAS,
5228 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5229 .access = PL2_RW,
5230 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
5231 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
5232 .type = ARM_CP_IO,
5233 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5234 .resetvalue = 0,
5235 .access = PL3_RW,
5236 .writefn = mdcr_el3_write,
5237 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
5238 { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
5239 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5240 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5241 .writefn = sdcr_write,
5242 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
5243 };
5244
5245 /* These are present only when EL1 supports AArch32 */
5246 static const ARMCPRegInfo v8_aa32_el1_reginfo[] = {
5247 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5248 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
5249 .access = PL2_RW,
5250 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
5251 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
5252 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5253 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
5254 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5255 .writefn = dacr_write, .raw_writefn = raw_write,
5256 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5257 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5258 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
5259 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
5260 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5261 };
5262
do_hcr_write(CPUARMState * env,uint64_t value,uint64_t valid_mask)5263 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
5264 {
5265 ARMCPU *cpu = env_archcpu(env);
5266
5267 if (arm_feature(env, ARM_FEATURE_V8)) {
5268 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5269 } else {
5270 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5271 }
5272
5273 if (arm_feature(env, ARM_FEATURE_EL3)) {
5274 valid_mask &= ~HCR_HCD;
5275 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
5276 /*
5277 * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
5278 * However, if we're using the SMC PSCI conduit then QEMU is
5279 * effectively acting like EL3 firmware and so the guest at
5280 * EL2 should retain the ability to prevent EL1 from being
5281 * able to make SMC calls into the ersatz firmware, so in
5282 * that case HCR.TSC should be read/write.
5283 */
5284 valid_mask &= ~HCR_TSC;
5285 }
5286
5287 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5288 if (cpu_isar_feature(aa64_vh, cpu)) {
5289 valid_mask |= HCR_E2H;
5290 }
5291 if (cpu_isar_feature(aa64_ras, cpu)) {
5292 valid_mask |= HCR_TERR | HCR_TEA;
5293 }
5294 if (cpu_isar_feature(aa64_lor, cpu)) {
5295 valid_mask |= HCR_TLOR;
5296 }
5297 if (cpu_isar_feature(aa64_pauth, cpu)) {
5298 valid_mask |= HCR_API | HCR_APK;
5299 }
5300 if (cpu_isar_feature(aa64_mte, cpu)) {
5301 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5302 }
5303 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5304 valid_mask |= HCR_ENSCXT;
5305 }
5306 if (cpu_isar_feature(aa64_fwb, cpu)) {
5307 valid_mask |= HCR_FWB;
5308 }
5309 if (cpu_isar_feature(aa64_rme, cpu)) {
5310 valid_mask |= HCR_GPF;
5311 }
5312 if (cpu_isar_feature(aa64_nv, cpu)) {
5313 valid_mask |= HCR_NV | HCR_NV1 | HCR_AT;
5314 }
5315 if (cpu_isar_feature(aa64_nv2, cpu)) {
5316 valid_mask |= HCR_NV2;
5317 }
5318 }
5319
5320 if (cpu_isar_feature(any_evt, cpu)) {
5321 valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5322 } else if (cpu_isar_feature(any_half_evt, cpu)) {
5323 valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5324 }
5325
5326 /* Clear RES0 bits. */
5327 value &= valid_mask;
5328
5329 /* RW is RAO/WI if EL1 is AArch64 only */
5330 if (!cpu_isar_feature(aa64_aa32_el1, cpu)) {
5331 value |= HCR_RW;
5332 }
5333
5334 /*
5335 * These bits change the MMU setup:
5336 * HCR_VM enables stage 2 translation
5337 * HCR_PTW forbids certain page-table setups
5338 * HCR_DC disables stage1 and enables stage2 translation
5339 * HCR_DCT enables tagging on (disabled) stage1 translation
5340 * HCR_FWB changes the interpretation of stage2 descriptor bits
5341 * HCR_NV and HCR_NV1 affect interpretation of descriptor bits
5342 */
5343 if ((env->cp15.hcr_el2 ^ value) &
5344 (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) {
5345 tlb_flush(CPU(cpu));
5346 }
5347 env->cp15.hcr_el2 = value;
5348
5349 /*
5350 * Updates to VI and VF require us to update the status of
5351 * virtual interrupts, which are the logical OR of these bits
5352 * and the state of the input lines from the GIC. (This requires
5353 * that we have the BQL, which is done by marking the
5354 * reginfo structs as ARM_CP_IO.)
5355 * Note that if a write to HCR pends a VIRQ or VFIQ or VINMI or
5356 * VFNMI, it is never possible for it to be taken immediately
5357 * because VIRQ, VFIQ, VINMI and VFNMI are masked unless running
5358 * at EL0 or EL1, and HCR can only be written at EL2.
5359 */
5360 g_assert(bql_locked());
5361 arm_cpu_update_virq(cpu);
5362 arm_cpu_update_vfiq(cpu);
5363 arm_cpu_update_vserr(cpu);
5364 if (cpu_isar_feature(aa64_nmi, cpu)) {
5365 arm_cpu_update_vinmi(cpu);
5366 arm_cpu_update_vfnmi(cpu);
5367 }
5368 }
5369
hcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5370 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5371 {
5372 do_hcr_write(env, value, 0);
5373 }
5374
hcr_writehigh(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5375 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5376 uint64_t value)
5377 {
5378 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5379 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
5380 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
5381 }
5382
hcr_writelow(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5383 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5384 uint64_t value)
5385 {
5386 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5387 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
5388 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
5389 }
5390
hcr_reset(CPUARMState * env,const ARMCPRegInfo * ri)5391 static void hcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
5392 {
5393 /* hcr_write will set the RES1 bits on an AArch64-only CPU */
5394 hcr_write(env, ri, 0);
5395 }
5396
5397 /*
5398 * Return the effective value of HCR_EL2, at the given security state.
5399 * Bits that are not included here:
5400 * RW (read from SCR_EL3.RW as needed)
5401 */
arm_hcr_el2_eff_secstate(CPUARMState * env,ARMSecuritySpace space)5402 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space)
5403 {
5404 uint64_t ret = env->cp15.hcr_el2;
5405
5406 assert(space != ARMSS_Root);
5407
5408 if (!arm_is_el2_enabled_secstate(env, space)) {
5409 /*
5410 * "This register has no effect if EL2 is not enabled in the
5411 * current Security state". This is ARMv8.4-SecEL2 speak for
5412 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5413 *
5414 * Prior to that, the language was "In an implementation that
5415 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5416 * as if this field is 0 for all purposes other than a direct
5417 * read or write access of HCR_EL2". With lots of enumeration
5418 * on a per-field basis. In current QEMU, this is condition
5419 * is arm_is_secure_below_el3.
5420 *
5421 * Since the v8.4 language applies to the entire register, and
5422 * appears to be backward compatible, use that.
5423 */
5424 return 0;
5425 }
5426
5427 /*
5428 * For a cpu that supports both aarch64 and aarch32, we can set bits
5429 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5430 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5431 */
5432 if (!arm_el_is_aa64(env, 2)) {
5433 uint64_t aa32_valid;
5434
5435 /*
5436 * These bits are up-to-date as of ARMv8.6.
5437 * For HCR, it's easiest to list just the 2 bits that are invalid.
5438 * For HCR2, list those that are valid.
5439 */
5440 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5441 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5442 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5443 ret &= aa32_valid;
5444 }
5445
5446 if (ret & HCR_TGE) {
5447 /* These bits are up-to-date as of ARMv8.6. */
5448 if (ret & HCR_E2H) {
5449 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5450 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5451 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
5452 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5453 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5454 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
5455 } else {
5456 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5457 }
5458 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5459 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5460 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5461 HCR_TLOR);
5462 }
5463
5464 return ret;
5465 }
5466
arm_hcr_el2_eff(CPUARMState * env)5467 uint64_t arm_hcr_el2_eff(CPUARMState *env)
5468 {
5469 if (arm_feature(env, ARM_FEATURE_M)) {
5470 return 0;
5471 }
5472 return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env));
5473 }
5474
5475 /*
5476 * Corresponds to ARM pseudocode function ELIsInHost().
5477 */
el_is_in_host(CPUARMState * env,int el)5478 bool el_is_in_host(CPUARMState *env, int el)
5479 {
5480 uint64_t mask;
5481
5482 /*
5483 * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5484 * Perform the simplest bit tests first, and validate EL2 afterward.
5485 */
5486 if (el & 1) {
5487 return false; /* EL1 or EL3 */
5488 }
5489
5490 /*
5491 * Note that hcr_write() checks isar_feature_aa64_vh(),
5492 * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5493 */
5494 mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5495 if ((env->cp15.hcr_el2 & mask) != mask) {
5496 return false;
5497 }
5498
5499 /* TGE and/or E2H set: double check those bits are currently legal. */
5500 return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5501 }
5502
hcrx_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5503 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5504 uint64_t value)
5505 {
5506 ARMCPU *cpu = env_archcpu(env);
5507 uint64_t valid_mask = 0;
5508
5509 /* FEAT_MOPS adds MSCEn and MCE2 */
5510 if (cpu_isar_feature(aa64_mops, cpu)) {
5511 valid_mask |= HCRX_MSCEN | HCRX_MCE2;
5512 }
5513
5514 /* FEAT_NMI adds TALLINT, VINMI and VFNMI */
5515 if (cpu_isar_feature(aa64_nmi, cpu)) {
5516 valid_mask |= HCRX_TALLINT | HCRX_VINMI | HCRX_VFNMI;
5517 }
5518 /* FEAT_CMOW adds CMOW */
5519 if (cpu_isar_feature(aa64_cmow, cpu)) {
5520 valid_mask |= HCRX_CMOW;
5521 }
5522 /* FEAT_XS adds FGTnXS, FnXS */
5523 if (cpu_isar_feature(aa64_xs, cpu)) {
5524 valid_mask |= HCRX_FGTNXS | HCRX_FNXS;
5525 }
5526
5527 /* Clear RES0 bits. */
5528 env->cp15.hcrx_el2 = value & valid_mask;
5529
5530 /*
5531 * Updates to VINMI and VFNMI require us to update the status of
5532 * virtual NMI, which are the logical OR of these bits
5533 * and the state of the input lines from the GIC. (This requires
5534 * that we have the BQL, which is done by marking the
5535 * reginfo structs as ARM_CP_IO.)
5536 * Note that if a write to HCRX pends a VINMI or VFNMI it is never
5537 * possible for it to be taken immediately, because VINMI and
5538 * VFNMI are masked unless running at EL0 or EL1, and HCRX
5539 * can only be written at EL2.
5540 */
5541 if (cpu_isar_feature(aa64_nmi, cpu)) {
5542 g_assert(bql_locked());
5543 arm_cpu_update_vinmi(cpu);
5544 arm_cpu_update_vfnmi(cpu);
5545 }
5546 }
5547
access_hxen(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)5548 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5549 bool isread)
5550 {
5551 if (arm_current_el(env) == 2
5552 && arm_feature(env, ARM_FEATURE_EL3)
5553 && !(env->cp15.scr_el3 & SCR_HXEN)) {
5554 return CP_ACCESS_TRAP_EL3;
5555 }
5556 return CP_ACCESS_OK;
5557 }
5558
5559 static const ARMCPRegInfo hcrx_el2_reginfo = {
5560 .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5561 .type = ARM_CP_IO,
5562 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5563 .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5564 .nv2_redirect_offset = 0xa0,
5565 .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5566 };
5567
5568 /* Return the effective value of HCRX_EL2. */
arm_hcrx_el2_eff(CPUARMState * env)5569 uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5570 {
5571 /*
5572 * The bits in this register behave as 0 for all purposes other than
5573 * direct reads of the register if SCR_EL3.HXEn is 0.
5574 * If EL2 is not enabled in the current security state, then the
5575 * bit may behave as if 0, or as if 1, depending on the bit.
5576 * For the moment, we treat the EL2-disabled case as taking
5577 * priority over the HXEn-disabled case. This is true for the only
5578 * bit for a feature which we implement where the answer is different
5579 * for the two cases (MSCEn for FEAT_MOPS).
5580 * This may need to be revisited for future bits.
5581 */
5582 if (!arm_is_el2_enabled(env)) {
5583 uint64_t hcrx = 0;
5584 if (cpu_isar_feature(aa64_mops, env_archcpu(env))) {
5585 /* MSCEn behaves as 1 if EL2 is not enabled */
5586 hcrx |= HCRX_MSCEN;
5587 }
5588 return hcrx;
5589 }
5590 if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) {
5591 return 0;
5592 }
5593 return env->cp15.hcrx_el2;
5594 }
5595
cptr_el2_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)5596 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5597 uint64_t value)
5598 {
5599 /*
5600 * For A-profile AArch32 EL3, if NSACR.CP10
5601 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5602 */
5603 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5604 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5605 uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5606 value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
5607 }
5608 env->cp15.cptr_el[2] = value;
5609 }
5610
cptr_el2_read(CPUARMState * env,const ARMCPRegInfo * ri)5611 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5612 {
5613 /*
5614 * For A-profile AArch32 EL3, if NSACR.CP10
5615 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5616 */
5617 uint64_t value = env->cp15.cptr_el[2];
5618
5619 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5620 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
5621 value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5622 }
5623 return value;
5624 }
5625
5626 static const ARMCPRegInfo el2_cp_reginfo[] = {
5627 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
5628 .type = ARM_CP_IO,
5629 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5630 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5631 .nv2_redirect_offset = 0x78,
5632 .resetfn = hcr_reset,
5633 .writefn = hcr_write, .raw_writefn = raw_write },
5634 { .name = "HCR", .state = ARM_CP_STATE_AA32,
5635 .type = ARM_CP_ALIAS | ARM_CP_IO,
5636 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5637 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
5638 .writefn = hcr_writelow },
5639 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5640 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5641 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
5642 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
5643 .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
5644 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5645 .access = PL2_RW,
5646 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
5647 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
5648 .type = ARM_CP_NV2_REDIRECT,
5649 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5650 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
5651 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
5652 .type = ARM_CP_NV2_REDIRECT,
5653 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5654 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
5655 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5656 .type = ARM_CP_ALIAS,
5657 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5658 .access = PL2_RW,
5659 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
5660 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
5661 .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT,
5662 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
5663 .access = PL2_RW,
5664 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
5665 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
5666 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5667 .access = PL2_RW, .writefn = vbar_write,
5668 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5669 .resetvalue = 0 },
5670 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5671 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
5672 .access = PL3_RW, .type = ARM_CP_ALIAS,
5673 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
5674 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5675 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5676 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
5677 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5678 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
5679 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5680 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5681 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5682 .resetvalue = 0 },
5683 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
5684 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
5685 .access = PL2_RW, .type = ARM_CP_ALIAS,
5686 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
5687 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5688 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5689 .access = PL2_RW, .type = ARM_CP_CONST,
5690 .resetvalue = 0 },
5691 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
5692 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
5693 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
5694 .access = PL2_RW, .type = ARM_CP_CONST,
5695 .resetvalue = 0 },
5696 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5697 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5698 .access = PL2_RW, .type = ARM_CP_CONST,
5699 .resetvalue = 0 },
5700 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5701 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5702 .access = PL2_RW, .type = ARM_CP_CONST,
5703 .resetvalue = 0 },
5704 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5705 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
5706 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
5707 .raw_writefn = raw_write,
5708 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
5709 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5710 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5711 .type = ARM_CP_ALIAS,
5712 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5713 .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
5714 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5715 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
5716 .access = PL2_RW,
5717 .nv2_redirect_offset = 0x40,
5718 /* no .writefn needed as this can't cause an ASID change */
5719 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
5720 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5721 .cp = 15, .opc1 = 6, .crm = 2,
5722 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5723 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5724 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5725 .writefn = vttbr_write, .raw_writefn = raw_write },
5726 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5727 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5728 .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write,
5729 .nv2_redirect_offset = 0x20,
5730 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
5731 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5732 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5733 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5734 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
5735 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5736 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5737 .access = PL2_RW, .resetvalue = 0,
5738 .nv2_redirect_offset = 0x90,
5739 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
5740 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5741 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
5742 .access = PL2_RW, .resetvalue = 0,
5743 .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write,
5744 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5745 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5746 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5747 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5748 #ifndef CONFIG_USER_ONLY
5749 /*
5750 * Unlike the other EL2-related AT operations, these must
5751 * UNDEF from EL3 if EL2 is not implemented, which is why we
5752 * define them here rather than with the rest of the AT ops.
5753 */
5754 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
5755 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5756 .access = PL2_W, .accessfn = at_s1e2_access,
5757 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
5758 .writefn = ats_write64 },
5759 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
5760 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
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 /*
5765 * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
5766 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
5767 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
5768 * to behave as if SCR.NS was 1.
5769 */
5770 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
5771 .access = PL2_W,
5772 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5773 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
5774 .access = PL2_W,
5775 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
5776 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
5777 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
5778 /*
5779 * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
5780 * reset values as IMPDEF. We choose to reset to 3 to comply with
5781 * both ARMv7 and ARMv8.
5782 */
5783 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3,
5784 .writefn = gt_cnthctl_write, .raw_writefn = raw_write,
5785 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
5786 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
5787 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
5788 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
5789 .writefn = gt_cntvoff_write,
5790 .nv2_redirect_offset = 0x60,
5791 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5792 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
5793 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
5794 .writefn = gt_cntvoff_write,
5795 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
5796 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5797 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
5798 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5799 .type = ARM_CP_IO, .access = PL2_RW,
5800 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5801 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
5802 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
5803 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
5804 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
5805 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
5806 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
5807 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5808 .resetfn = gt_hyp_timer_reset,
5809 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
5810 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
5811 .type = ARM_CP_IO,
5812 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
5813 .access = PL2_RW,
5814 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
5815 .resetvalue = 0,
5816 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
5817 #endif
5818 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
5819 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5820 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5821 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5822 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
5823 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
5824 .access = PL2_RW,
5825 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
5826 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
5827 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
5828 .access = PL2_RW,
5829 .nv2_redirect_offset = 0x80,
5830 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
5831 };
5832
5833 static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
5834 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
5835 .type = ARM_CP_ALIAS | ARM_CP_IO,
5836 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
5837 .access = PL2_RW,
5838 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
5839 .writefn = hcr_writehigh },
5840 };
5841
sel2_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)5842 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
5843 bool isread)
5844 {
5845 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
5846 return CP_ACCESS_OK;
5847 }
5848 return CP_ACCESS_UNDEFINED;
5849 }
5850
5851 static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
5852 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
5853 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
5854 .access = PL2_RW, .accessfn = sel2_access,
5855 .nv2_redirect_offset = 0x30,
5856 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
5857 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
5858 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
5859 .access = PL2_RW, .accessfn = sel2_access,
5860 .nv2_redirect_offset = 0x48,
5861 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
5862 #ifndef CONFIG_USER_ONLY
5863 /* Secure EL2 Physical Timer */
5864 { .name = "CNTHPS_TVAL_EL2", .state = ARM_CP_STATE_AA64,
5865 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 0,
5866 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5867 .accessfn = gt_sel2timer_access,
5868 .readfn = gt_sec_pel2_tval_read,
5869 .writefn = gt_sec_pel2_tval_write,
5870 .resetfn = gt_sec_pel2_timer_reset,
5871 },
5872 { .name = "CNTHPS_CTL_EL2", .state = ARM_CP_STATE_AA64,
5873 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 1,
5874 .type = ARM_CP_IO, .access = PL2_RW,
5875 .accessfn = gt_sel2timer_access,
5876 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_PHYS].ctl),
5877 .resetvalue = 0,
5878 .writefn = gt_sec_pel2_ctl_write, .raw_writefn = raw_write,
5879 },
5880 { .name = "CNTHPS_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5881 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 2,
5882 .type = ARM_CP_IO, .access = PL2_RW,
5883 .accessfn = gt_sel2timer_access,
5884 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_PHYS].cval),
5885 .writefn = gt_sec_pel2_cval_write, .raw_writefn = raw_write,
5886 },
5887 /* Secure EL2 Virtual Timer */
5888 { .name = "CNTHVS_TVAL_EL2", .state = ARM_CP_STATE_AA64,
5889 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 0,
5890 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
5891 .accessfn = gt_sel2timer_access,
5892 .readfn = gt_sec_vel2_tval_read,
5893 .writefn = gt_sec_vel2_tval_write,
5894 .resetfn = gt_sec_vel2_timer_reset,
5895 },
5896 { .name = "CNTHVS_CTL_EL2", .state = ARM_CP_STATE_AA64,
5897 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 1,
5898 .type = ARM_CP_IO, .access = PL2_RW,
5899 .accessfn = gt_sel2timer_access,
5900 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_VIRT].ctl),
5901 .resetvalue = 0,
5902 .writefn = gt_sec_vel2_ctl_write, .raw_writefn = raw_write,
5903 },
5904 { .name = "CNTHVS_CVAL_EL2", .state = ARM_CP_STATE_AA64,
5905 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 2,
5906 .type = ARM_CP_IO, .access = PL2_RW,
5907 .accessfn = gt_sel2timer_access,
5908 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_VIRT].cval),
5909 .writefn = gt_sec_vel2_cval_write, .raw_writefn = raw_write,
5910 },
5911 #endif
5912 };
5913
nsacr_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)5914 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
5915 bool isread)
5916 {
5917 /*
5918 * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
5919 * At Secure EL1 it traps to EL3 or EL2.
5920 */
5921 if (arm_current_el(env) == 3) {
5922 return CP_ACCESS_OK;
5923 }
5924 if (arm_is_secure_below_el3(env)) {
5925 if (env->cp15.scr_el3 & SCR_EEL2) {
5926 return CP_ACCESS_TRAP_EL2;
5927 }
5928 return CP_ACCESS_TRAP_EL3;
5929 }
5930 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
5931 if (isread) {
5932 return CP_ACCESS_OK;
5933 }
5934 return CP_ACCESS_UNDEFINED;
5935 }
5936
5937 static const ARMCPRegInfo el3_cp_reginfo[] = {
5938 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
5939 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
5940 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
5941 .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write },
5942 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
5943 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
5944 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5945 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
5946 .writefn = scr_write, .raw_writefn = raw_write },
5947 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
5948 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
5949 .access = PL3_RW, .resetvalue = 0,
5950 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
5951 { .name = "SDER",
5952 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
5953 .access = PL3_RW, .resetvalue = 0,
5954 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
5955 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
5956 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5957 .writefn = vbar_write, .resetvalue = 0,
5958 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
5959 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
5960 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
5961 .access = PL3_RW, .resetvalue = 0,
5962 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
5963 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
5964 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
5965 .access = PL3_RW,
5966 /* no .writefn needed as this can't cause an ASID change */
5967 .resetvalue = 0,
5968 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
5969 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
5970 .type = ARM_CP_ALIAS,
5971 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
5972 .access = PL3_RW,
5973 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
5974 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
5975 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
5976 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
5977 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
5978 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
5979 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
5980 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
5981 .type = ARM_CP_ALIAS,
5982 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
5983 .access = PL3_RW,
5984 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
5985 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
5986 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
5987 .access = PL3_RW, .writefn = vbar_write,
5988 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
5989 .resetvalue = 0 },
5990 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
5991 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
5992 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
5993 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
5994 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
5995 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
5996 .access = PL3_RW, .resetvalue = 0,
5997 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
5998 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
5999 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6000 .access = PL3_RW, .type = ARM_CP_CONST,
6001 .resetvalue = 0 },
6002 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6003 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6004 .access = PL3_RW, .type = ARM_CP_CONST,
6005 .resetvalue = 0 },
6006 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6007 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6008 .access = PL3_RW, .type = ARM_CP_CONST,
6009 .resetvalue = 0 },
6010 };
6011
6012 #ifndef CONFIG_USER_ONLY
6013
e2h_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6014 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
6015 bool isread)
6016 {
6017 if (arm_current_el(env) == 1) {
6018 /* This must be a FEAT_NV access */
6019 return CP_ACCESS_OK;
6020 }
6021 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6022 return CP_ACCESS_UNDEFINED;
6023 }
6024 return CP_ACCESS_OK;
6025 }
6026
access_el1nvpct(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6027 static CPAccessResult access_el1nvpct(CPUARMState *env, const ARMCPRegInfo *ri,
6028 bool isread)
6029 {
6030 if (arm_current_el(env) == 1) {
6031 /* This must be a FEAT_NV access with NVx == 101 */
6032 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVPCT)) {
6033 return CP_ACCESS_TRAP_EL2;
6034 }
6035 }
6036 return e2h_access(env, ri, isread);
6037 }
6038
access_el1nvvct(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6039 static CPAccessResult access_el1nvvct(CPUARMState *env, const ARMCPRegInfo *ri,
6040 bool isread)
6041 {
6042 if (arm_current_el(env) == 1) {
6043 /* This must be a FEAT_NV access with NVx == 101 */
6044 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVVCT)) {
6045 return CP_ACCESS_TRAP_EL2;
6046 }
6047 }
6048 return e2h_access(env, ri, isread);
6049 }
6050
6051 /* Test if system register redirection is to occur in the current state. */
redirect_for_e2h(CPUARMState * env)6052 static bool redirect_for_e2h(CPUARMState *env)
6053 {
6054 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6055 }
6056
el2_e2h_read(CPUARMState * env,const ARMCPRegInfo * ri)6057 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6058 {
6059 CPReadFn *readfn;
6060
6061 if (redirect_for_e2h(env)) {
6062 /* Switch to the saved EL2 version of the register. */
6063 ri = ri->opaque;
6064 readfn = ri->readfn;
6065 } else {
6066 readfn = ri->orig_readfn;
6067 }
6068 if (readfn == NULL) {
6069 readfn = raw_read;
6070 }
6071 return readfn(env, ri);
6072 }
6073
el2_e2h_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6074 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6075 uint64_t value)
6076 {
6077 CPWriteFn *writefn;
6078
6079 if (redirect_for_e2h(env)) {
6080 /* Switch to the saved EL2 version of the register. */
6081 ri = ri->opaque;
6082 writefn = ri->writefn;
6083 } else {
6084 writefn = ri->orig_writefn;
6085 }
6086 if (writefn == NULL) {
6087 writefn = raw_write;
6088 }
6089 writefn(env, ri, value);
6090 }
6091
el2_e2h_e12_read(CPUARMState * env,const ARMCPRegInfo * ri)6092 static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri)
6093 {
6094 /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6095 return ri->orig_readfn(env, ri->opaque);
6096 }
6097
el2_e2h_e12_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6098 static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri,
6099 uint64_t value)
6100 {
6101 /* Pass the EL1 register accessor its ri, not the EL12 alias ri */
6102 return ri->orig_writefn(env, ri->opaque, value);
6103 }
6104
el2_e2h_e12_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6105 static CPAccessResult el2_e2h_e12_access(CPUARMState *env,
6106 const ARMCPRegInfo *ri,
6107 bool isread)
6108 {
6109 if (arm_current_el(env) == 1) {
6110 /*
6111 * This must be a FEAT_NV access (will either trap or redirect
6112 * to memory). None of the registers with _EL12 aliases want to
6113 * apply their trap controls for this kind of access, so don't
6114 * call the orig_accessfn or do the "UNDEF when E2H is 0" check.
6115 */
6116 return CP_ACCESS_OK;
6117 }
6118 /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */
6119 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
6120 return CP_ACCESS_UNDEFINED;
6121 }
6122 if (ri->orig_accessfn) {
6123 return ri->orig_accessfn(env, ri->opaque, isread);
6124 }
6125 return CP_ACCESS_OK;
6126 }
6127
define_arm_vh_e2h_redirects_aliases(ARMCPU * cpu)6128 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6129 {
6130 struct E2HAlias {
6131 uint32_t src_key, dst_key, new_key;
6132 const char *src_name, *dst_name, *new_name;
6133 bool (*feature)(const ARMISARegisters *id);
6134 };
6135
6136 #define K(op0, op1, crn, crm, op2) \
6137 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6138
6139 static const struct E2HAlias aliases[] = {
6140 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
6141 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6142 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
6143 "CPACR", "CPTR_EL2", "CPACR_EL12" },
6144 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
6145 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6146 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
6147 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6148 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
6149 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6150 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
6151 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6152 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
6153 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6154 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
6155 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6156 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
6157 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6158 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
6159 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6160 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
6161 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6162 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6163 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6164 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6165 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6166 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6167 "VBAR", "VBAR_EL2", "VBAR_EL12" },
6168 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6169 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6170 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6171 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6172
6173 /*
6174 * Note that redirection of ZCR is mentioned in the description
6175 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6176 * not in the summary table.
6177 */
6178 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
6179 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
6180 { K(3, 0, 1, 2, 6), K(3, 4, 1, 2, 6), K(3, 5, 1, 2, 6),
6181 "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
6182
6183 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
6184 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6185
6186 { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6187 "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6188 isar_feature_aa64_scxtnum },
6189
6190 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6191 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6192 };
6193 #undef K
6194
6195 size_t i;
6196
6197 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6198 const struct E2HAlias *a = &aliases[i];
6199 ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
6200 bool ok;
6201
6202 if (a->feature && !a->feature(&cpu->isar)) {
6203 continue;
6204 }
6205
6206 src_reg = g_hash_table_lookup(cpu->cp_regs,
6207 (gpointer)(uintptr_t)a->src_key);
6208 dst_reg = g_hash_table_lookup(cpu->cp_regs,
6209 (gpointer)(uintptr_t)a->dst_key);
6210 g_assert(src_reg != NULL);
6211 g_assert(dst_reg != NULL);
6212
6213 /* Cross-compare names to detect typos in the keys. */
6214 g_assert(strcmp(src_reg->name, a->src_name) == 0);
6215 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6216
6217 /* None of the core system registers use opaque; we will. */
6218 g_assert(src_reg->opaque == NULL);
6219
6220 /* Create alias before redirection so we dup the right data. */
6221 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
6222
6223 new_reg->name = a->new_name;
6224 new_reg->type |= ARM_CP_ALIAS;
6225 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
6226 new_reg->access &= PL2_RW | PL3_RW;
6227 /* The new_reg op fields are as per new_key, not the target reg */
6228 new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK)
6229 >> CP_REG_ARM64_SYSREG_CRN_SHIFT;
6230 new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK)
6231 >> CP_REG_ARM64_SYSREG_CRM_SHIFT;
6232 new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK)
6233 >> CP_REG_ARM64_SYSREG_OP0_SHIFT;
6234 new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK)
6235 >> CP_REG_ARM64_SYSREG_OP1_SHIFT;
6236 new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK)
6237 >> CP_REG_ARM64_SYSREG_OP2_SHIFT;
6238 new_reg->opaque = src_reg;
6239 new_reg->orig_readfn = src_reg->readfn ?: raw_read;
6240 new_reg->orig_writefn = src_reg->writefn ?: raw_write;
6241 new_reg->orig_accessfn = src_reg->accessfn;
6242 if (!new_reg->raw_readfn) {
6243 new_reg->raw_readfn = raw_read;
6244 }
6245 if (!new_reg->raw_writefn) {
6246 new_reg->raw_writefn = raw_write;
6247 }
6248 new_reg->readfn = el2_e2h_e12_read;
6249 new_reg->writefn = el2_e2h_e12_write;
6250 new_reg->accessfn = el2_e2h_e12_access;
6251
6252 /*
6253 * If the _EL1 register is redirected to memory by FEAT_NV2,
6254 * then it shares the offset with the _EL12 register,
6255 * and which one is redirected depends on HCR_EL2.NV1.
6256 */
6257 if (new_reg->nv2_redirect_offset) {
6258 assert(new_reg->nv2_redirect_offset & NV2_REDIR_NV1);
6259 new_reg->nv2_redirect_offset &= ~NV2_REDIR_NV1;
6260 new_reg->nv2_redirect_offset |= NV2_REDIR_NO_NV1;
6261 }
6262
6263 ok = g_hash_table_insert(cpu->cp_regs,
6264 (gpointer)(uintptr_t)a->new_key, new_reg);
6265 g_assert(ok);
6266
6267 src_reg->opaque = dst_reg;
6268 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6269 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6270 if (!src_reg->raw_readfn) {
6271 src_reg->raw_readfn = raw_read;
6272 }
6273 if (!src_reg->raw_writefn) {
6274 src_reg->raw_writefn = raw_write;
6275 }
6276 src_reg->readfn = el2_e2h_read;
6277 src_reg->writefn = el2_e2h_write;
6278 }
6279 }
6280 #endif
6281
ctr_el0_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6282 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6283 bool isread)
6284 {
6285 int cur_el = arm_current_el(env);
6286
6287 if (cur_el < 2) {
6288 uint64_t hcr = arm_hcr_el2_eff(env);
6289
6290 if (cur_el == 0) {
6291 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6292 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6293 return CP_ACCESS_TRAP_EL2;
6294 }
6295 } else {
6296 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6297 return CP_ACCESS_TRAP_EL1;
6298 }
6299 if (hcr & HCR_TID2) {
6300 return CP_ACCESS_TRAP_EL2;
6301 }
6302 }
6303 } else if (hcr & HCR_TID2) {
6304 return CP_ACCESS_TRAP_EL2;
6305 }
6306 }
6307
6308 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6309 return CP_ACCESS_TRAP_EL2;
6310 }
6311
6312 return CP_ACCESS_OK;
6313 }
6314
6315 /*
6316 * Check for traps to RAS registers, which are controlled
6317 * by HCR_EL2.TERR and SCR_EL3.TERR.
6318 */
access_terr(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6319 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6320 bool isread)
6321 {
6322 int el = arm_current_el(env);
6323
6324 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6325 return CP_ACCESS_TRAP_EL2;
6326 }
6327 if (!arm_is_el3_or_mon(env) && (env->cp15.scr_el3 & SCR_TERR)) {
6328 return CP_ACCESS_TRAP_EL3;
6329 }
6330 return CP_ACCESS_OK;
6331 }
6332
disr_read(CPUARMState * env,const ARMCPRegInfo * ri)6333 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6334 {
6335 int el = arm_current_el(env);
6336
6337 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6338 return env->cp15.vdisr_el2;
6339 }
6340 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6341 return 0; /* RAZ/WI */
6342 }
6343 return env->cp15.disr_el1;
6344 }
6345
disr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t val)6346 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6347 {
6348 int el = arm_current_el(env);
6349
6350 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6351 env->cp15.vdisr_el2 = val;
6352 return;
6353 }
6354 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6355 return; /* RAZ/WI */
6356 }
6357 env->cp15.disr_el1 = val;
6358 }
6359
6360 /*
6361 * Minimal RAS implementation with no Error Records.
6362 * Which means that all of the Error Record registers:
6363 * ERXADDR_EL1
6364 * ERXCTLR_EL1
6365 * ERXFR_EL1
6366 * ERXMISC0_EL1
6367 * ERXMISC1_EL1
6368 * ERXMISC2_EL1
6369 * ERXMISC3_EL1
6370 * ERXPFGCDN_EL1 (RASv1p1)
6371 * ERXPFGCTL_EL1 (RASv1p1)
6372 * ERXPFGF_EL1 (RASv1p1)
6373 * ERXSTATUS_EL1
6374 * and
6375 * ERRSELR_EL1
6376 * may generate UNDEFINED, which is the effect we get by not
6377 * listing them at all.
6378 *
6379 * These registers have fine-grained trap bits, but UNDEF-to-EL1
6380 * is higher priority than FGT-to-EL2 so we do not need to list them
6381 * in order to check for an FGT.
6382 */
6383 static const ARMCPRegInfo minimal_ras_reginfo[] = {
6384 { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6385 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6386 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6387 .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6388 { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6389 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6390 .access = PL1_R, .accessfn = access_terr,
6391 .fgt = FGT_ERRIDR_EL1,
6392 .type = ARM_CP_CONST, .resetvalue = 0 },
6393 { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6394 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6395 .nv2_redirect_offset = 0x500,
6396 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6397 { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6398 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6399 .nv2_redirect_offset = 0x508,
6400 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6401 };
6402
6403 /*
6404 * Return the exception level to which exceptions should be taken
6405 * via SVEAccessTrap. This excludes the check for whether the exception
6406 * should be routed through AArch64.AdvSIMDFPAccessTrap. That can easily
6407 * be found by testing 0 < fp_exception_el < sve_exception_el.
6408 *
6409 * C.f. the ARM pseudocode function CheckSVEEnabled. Note that the
6410 * pseudocode does *not* separate out the FP trap checks, but has them
6411 * all in one function.
6412 */
sve_exception_el(CPUARMState * env,int el)6413 int sve_exception_el(CPUARMState *env, int el)
6414 {
6415 #ifndef CONFIG_USER_ONLY
6416 if (el <= 1 && !el_is_in_host(env, el)) {
6417 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
6418 case 1:
6419 if (el != 0) {
6420 break;
6421 }
6422 /* fall through */
6423 case 0:
6424 case 2:
6425 return 1;
6426 }
6427 }
6428
6429 if (el <= 2 && arm_is_el2_enabled(env)) {
6430 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6431 if (env->cp15.hcr_el2 & HCR_E2H) {
6432 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
6433 case 1:
6434 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6435 break;
6436 }
6437 /* fall through */
6438 case 0:
6439 case 2:
6440 return 2;
6441 }
6442 } else {
6443 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
6444 return 2;
6445 }
6446 }
6447 }
6448
6449 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6450 if (arm_feature(env, ARM_FEATURE_EL3)
6451 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
6452 return 3;
6453 }
6454 #endif
6455 return 0;
6456 }
6457
6458 /*
6459 * Return the exception level to which exceptions should be taken for SME.
6460 * C.f. the ARM pseudocode function CheckSMEAccess.
6461 */
sme_exception_el(CPUARMState * env,int el)6462 int sme_exception_el(CPUARMState *env, int el)
6463 {
6464 #ifndef CONFIG_USER_ONLY
6465 if (el <= 1 && !el_is_in_host(env, el)) {
6466 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6467 case 1:
6468 if (el != 0) {
6469 break;
6470 }
6471 /* fall through */
6472 case 0:
6473 case 2:
6474 return 1;
6475 }
6476 }
6477
6478 if (el <= 2 && arm_is_el2_enabled(env)) {
6479 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6480 if (env->cp15.hcr_el2 & HCR_E2H) {
6481 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6482 case 1:
6483 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6484 break;
6485 }
6486 /* fall through */
6487 case 0:
6488 case 2:
6489 return 2;
6490 }
6491 } else {
6492 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6493 return 2;
6494 }
6495 }
6496 }
6497
6498 /* CPTR_EL3. Since ESM is negative we must check for EL3. */
6499 if (arm_feature(env, ARM_FEATURE_EL3)
6500 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6501 return 3;
6502 }
6503 #endif
6504 return 0;
6505 }
6506
6507 /*
6508 * Given that SVE is enabled, return the vector length for EL.
6509 */
sve_vqm1_for_el_sm(CPUARMState * env,int el,bool sm)6510 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
6511 {
6512 ARMCPU *cpu = env_archcpu(env);
6513 uint64_t *cr = env->vfp.zcr_el;
6514 uint32_t map = cpu->sve_vq.map;
6515 uint32_t len = ARM_MAX_VQ - 1;
6516
6517 if (sm) {
6518 cr = env->vfp.smcr_el;
6519 map = cpu->sme_vq.map;
6520 }
6521
6522 if (el <= 1 && !el_is_in_host(env, el)) {
6523 len = MIN(len, 0xf & (uint32_t)cr[1]);
6524 }
6525 if (el <= 2 && arm_is_el2_enabled(env)) {
6526 len = MIN(len, 0xf & (uint32_t)cr[2]);
6527 }
6528 if (arm_feature(env, ARM_FEATURE_EL3)) {
6529 len = MIN(len, 0xf & (uint32_t)cr[3]);
6530 }
6531
6532 map &= MAKE_64BIT_MASK(0, len + 1);
6533 if (map != 0) {
6534 return 31 - clz32(map);
6535 }
6536
6537 /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6538 assert(sm);
6539 return ctz32(cpu->sme_vq.map);
6540 }
6541
sve_vqm1_for_el(CPUARMState * env,int el)6542 uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6543 {
6544 return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
6545 }
6546
zcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6547 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6548 uint64_t value)
6549 {
6550 int cur_el = arm_current_el(env);
6551 int old_len = sve_vqm1_for_el(env, cur_el);
6552 int new_len;
6553
6554 /* Bits other than [3:0] are RAZ/WI. */
6555 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
6556 raw_write(env, ri, value & 0xf);
6557
6558 /*
6559 * Because we arrived here, we know both FP and SVE are enabled;
6560 * otherwise we would have trapped access to the ZCR_ELn register.
6561 */
6562 new_len = sve_vqm1_for_el(env, cur_el);
6563 if (new_len < old_len) {
6564 aarch64_sve_narrow_vq(env, new_len + 1);
6565 }
6566 }
6567
6568 static const ARMCPRegInfo zcr_reginfo[] = {
6569 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6570 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6571 .nv2_redirect_offset = 0x1e0 | NV2_REDIR_NV1,
6572 .access = PL1_RW, .type = ARM_CP_SVE,
6573 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6574 .writefn = zcr_write, .raw_writefn = raw_write },
6575 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6576 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6577 .access = PL2_RW, .type = ARM_CP_SVE,
6578 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6579 .writefn = zcr_write, .raw_writefn = raw_write },
6580 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6581 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6582 .access = PL3_RW, .type = ARM_CP_SVE,
6583 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6584 .writefn = zcr_write, .raw_writefn = raw_write },
6585 };
6586
6587 #ifdef TARGET_AARCH64
access_tpidr2(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6588 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6589 bool isread)
6590 {
6591 int el = arm_current_el(env);
6592
6593 if (el == 0) {
6594 uint64_t sctlr = arm_sctlr(env, el);
6595 if (!(sctlr & SCTLR_EnTP2)) {
6596 return CP_ACCESS_TRAP_EL1;
6597 }
6598 }
6599 /* TODO: FEAT_FGT */
6600 if (el < 3
6601 && arm_feature(env, ARM_FEATURE_EL3)
6602 && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6603 return CP_ACCESS_TRAP_EL3;
6604 }
6605 return CP_ACCESS_OK;
6606 }
6607
access_smprimap(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6608 static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri,
6609 bool isread)
6610 {
6611 /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */
6612 if (arm_current_el(env) == 2
6613 && arm_feature(env, ARM_FEATURE_EL3)
6614 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6615 return CP_ACCESS_TRAP_EL3;
6616 }
6617 return CP_ACCESS_OK;
6618 }
6619
access_smpri(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6620 static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri,
6621 bool isread)
6622 {
6623 if (arm_current_el(env) < 3
6624 && arm_feature(env, ARM_FEATURE_EL3)
6625 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6626 return CP_ACCESS_TRAP_EL3;
6627 }
6628 return CP_ACCESS_OK;
6629 }
6630
6631 /* ResetSVEState */
arm_reset_sve_state(CPUARMState * env)6632 static void arm_reset_sve_state(CPUARMState *env)
6633 {
6634 memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
6635 /* Recall that FFR is stored as pregs[16]. */
6636 memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
6637 vfp_set_fpsr(env, 0x0800009f);
6638 }
6639
aarch64_set_svcr(CPUARMState * env,uint64_t new,uint64_t mask)6640 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
6641 {
6642 uint64_t change = (env->svcr ^ new) & mask;
6643
6644 if (change == 0) {
6645 return;
6646 }
6647 env->svcr ^= change;
6648
6649 if (change & R_SVCR_SM_MASK) {
6650 arm_reset_sve_state(env);
6651 }
6652
6653 /*
6654 * ResetSMEState.
6655 *
6656 * SetPSTATE_ZA zeros on enable and disable. We can zero this only
6657 * on enable: while disabled, the storage is inaccessible and the
6658 * value does not matter. We're not saving the storage in vmstate
6659 * when disabled either.
6660 */
6661 if (change & new & R_SVCR_ZA_MASK) {
6662 memset(env->zarray, 0, sizeof(env->zarray));
6663 }
6664
6665 if (tcg_enabled()) {
6666 arm_rebuild_hflags(env);
6667 }
6668 }
6669
svcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6670 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6671 uint64_t value)
6672 {
6673 aarch64_set_svcr(env, value, -1);
6674 }
6675
smcr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6676 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6677 uint64_t value)
6678 {
6679 int cur_el = arm_current_el(env);
6680 int old_len = sve_vqm1_for_el(env, cur_el);
6681 int new_len;
6682
6683 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6684 value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6685 raw_write(env, ri, value);
6686
6687 /*
6688 * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6689 * when SVL is widened (old values kept, or zeros). Choose to keep the
6690 * current values for simplicity. But for QEMU internals, we must still
6691 * apply the narrower SVL to the Zregs and Pregs -- see the comment
6692 * above aarch64_sve_narrow_vq.
6693 */
6694 new_len = sve_vqm1_for_el(env, cur_el);
6695 if (new_len < old_len) {
6696 aarch64_sve_narrow_vq(env, new_len + 1);
6697 }
6698 }
6699
6700 static const ARMCPRegInfo sme_reginfo[] = {
6701 { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6702 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6703 .access = PL0_RW, .accessfn = access_tpidr2,
6704 .fgt = FGT_NTPIDR2_EL0,
6705 .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
6706 { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6707 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6708 .access = PL0_RW, .type = ARM_CP_SME,
6709 .fieldoffset = offsetof(CPUARMState, svcr),
6710 .writefn = svcr_write, .raw_writefn = raw_write },
6711 { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6712 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6713 .nv2_redirect_offset = 0x1f0 | NV2_REDIR_NV1,
6714 .access = PL1_RW, .type = ARM_CP_SME,
6715 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6716 .writefn = smcr_write, .raw_writefn = raw_write },
6717 { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6718 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6719 .access = PL2_RW, .type = ARM_CP_SME,
6720 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6721 .writefn = smcr_write, .raw_writefn = raw_write },
6722 { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6723 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6724 .access = PL3_RW, .type = ARM_CP_SME,
6725 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6726 .writefn = smcr_write, .raw_writefn = raw_write },
6727 { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6728 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6729 .access = PL1_R, .accessfn = access_aa64_tid1,
6730 /*
6731 * IMPLEMENTOR = 0 (software)
6732 * REVISION = 0 (implementation defined)
6733 * SMPS = 0 (no streaming execution priority in QEMU)
6734 * AFFINITY = 0 (streaming sve mode not shared with other PEs)
6735 */
6736 .type = ARM_CP_CONST, .resetvalue = 0, },
6737 /*
6738 * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6739 */
6740 { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6741 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6742 .access = PL1_RW, .accessfn = access_smpri,
6743 .fgt = FGT_NSMPRI_EL1,
6744 .type = ARM_CP_CONST, .resetvalue = 0 },
6745 { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6746 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6747 .nv2_redirect_offset = 0x1f8,
6748 .access = PL2_RW, .accessfn = access_smprimap,
6749 .type = ARM_CP_CONST, .resetvalue = 0 },
6750 };
6751
gpccr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6752 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6753 uint64_t value)
6754 {
6755 /* L0GPTSZ is RO; other bits not mentioned are RES0. */
6756 uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK |
6757 R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK |
6758 R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK;
6759
6760 env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask);
6761 }
6762
gpccr_reset(CPUARMState * env,const ARMCPRegInfo * ri)6763 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
6764 {
6765 env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ,
6766 env_archcpu(env)->reset_l0gptsz);
6767 }
6768
6769 static const ARMCPRegInfo rme_reginfo[] = {
6770 { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64,
6771 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6,
6772 .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset,
6773 .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) },
6774 { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64,
6775 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4,
6776 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) },
6777 { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64,
6778 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5,
6779 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) },
6780 { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64,
6781 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1,
6782 .access = PL3_W, .type = ARM_CP_NOP },
6783 };
6784
6785 static const ARMCPRegInfo rme_mte_reginfo[] = {
6786 { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64,
6787 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5,
6788 .access = PL3_W, .type = ARM_CP_NOP },
6789 };
6790
aa64_allint_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)6791 static void aa64_allint_write(CPUARMState *env, const ARMCPRegInfo *ri,
6792 uint64_t value)
6793 {
6794 env->pstate = (env->pstate & ~PSTATE_ALLINT) | (value & PSTATE_ALLINT);
6795 }
6796
aa64_allint_read(CPUARMState * env,const ARMCPRegInfo * ri)6797 static uint64_t aa64_allint_read(CPUARMState *env, const ARMCPRegInfo *ri)
6798 {
6799 return env->pstate & PSTATE_ALLINT;
6800 }
6801
aa64_allint_access(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6802 static CPAccessResult aa64_allint_access(CPUARMState *env,
6803 const ARMCPRegInfo *ri, bool isread)
6804 {
6805 if (!isread && arm_current_el(env) == 1 &&
6806 (arm_hcrx_el2_eff(env) & HCRX_TALLINT)) {
6807 return CP_ACCESS_TRAP_EL2;
6808 }
6809 return CP_ACCESS_OK;
6810 }
6811
6812 static const ARMCPRegInfo nmi_reginfo[] = {
6813 { .name = "ALLINT", .state = ARM_CP_STATE_AA64,
6814 .opc0 = 3, .opc1 = 0, .opc2 = 0, .crn = 4, .crm = 3,
6815 .type = ARM_CP_NO_RAW,
6816 .access = PL1_RW, .accessfn = aa64_allint_access,
6817 .fieldoffset = offsetof(CPUARMState, pstate),
6818 .writefn = aa64_allint_write, .readfn = aa64_allint_read,
6819 .resetfn = arm_cp_reset_ignore },
6820 };
6821 #endif /* TARGET_AARCH64 */
6822
define_pmu_regs(ARMCPU * cpu)6823 static void define_pmu_regs(ARMCPU *cpu)
6824 {
6825 /*
6826 * v7 performance monitor control register: same implementor
6827 * field as main ID register, and we implement four counters in
6828 * addition to the cycle count register.
6829 */
6830 unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
6831 ARMCPRegInfo pmcr = {
6832 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6833 .access = PL0_RW,
6834 .fgt = FGT_PMCR_EL0,
6835 .type = ARM_CP_IO | ARM_CP_ALIAS,
6836 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6837 .accessfn = pmreg_access,
6838 .readfn = pmcr_read, .raw_readfn = raw_read,
6839 .writefn = pmcr_write, .raw_writefn = raw_write,
6840 };
6841 ARMCPRegInfo pmcr64 = {
6842 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6843 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6844 .access = PL0_RW, .accessfn = pmreg_access,
6845 .fgt = FGT_PMCR_EL0,
6846 .type = ARM_CP_IO,
6847 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
6848 .resetvalue = cpu->isar.reset_pmcr_el0,
6849 .readfn = pmcr_read, .raw_readfn = raw_read,
6850 .writefn = pmcr_write, .raw_writefn = raw_write,
6851 };
6852
6853 define_one_arm_cp_reg(cpu, &pmcr);
6854 define_one_arm_cp_reg(cpu, &pmcr64);
6855 for (i = 0; i < pmcrn; i++) {
6856 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6857 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6858 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6859 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6860 ARMCPRegInfo pmev_regs[] = {
6861 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6862 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6863 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6864 .fgt = FGT_PMEVCNTRN_EL0,
6865 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6866 .accessfn = pmreg_access_xevcntr },
6867 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6868 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
6869 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
6870 .type = ARM_CP_IO,
6871 .fgt = FGT_PMEVCNTRN_EL0,
6872 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6873 .raw_readfn = pmevcntr_rawread,
6874 .raw_writefn = pmevcntr_rawwrite },
6875 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6876 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6877 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
6878 .fgt = FGT_PMEVTYPERN_EL0,
6879 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6880 .accessfn = pmreg_access },
6881 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6882 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6883 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
6884 .fgt = FGT_PMEVTYPERN_EL0,
6885 .type = ARM_CP_IO,
6886 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6887 .raw_writefn = pmevtyper_rawwrite },
6888 };
6889 define_arm_cp_regs(cpu, pmev_regs);
6890 g_free(pmevcntr_name);
6891 g_free(pmevcntr_el0_name);
6892 g_free(pmevtyper_name);
6893 g_free(pmevtyper_el0_name);
6894 }
6895 if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
6896 ARMCPRegInfo v81_pmu_regs[] = {
6897 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6898 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6899 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6900 .fgt = FGT_PMCEIDN_EL0,
6901 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6902 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6903 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6904 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6905 .fgt = FGT_PMCEIDN_EL0,
6906 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
6907 };
6908 define_arm_cp_regs(cpu, v81_pmu_regs);
6909 }
6910 if (cpu_isar_feature(any_pmuv3p4, cpu)) {
6911 static const ARMCPRegInfo v84_pmmir = {
6912 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6913 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6914 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
6915 .fgt = FGT_PMMIR_EL1,
6916 .resetvalue = 0
6917 };
6918 define_one_arm_cp_reg(cpu, &v84_pmmir);
6919 }
6920 }
6921
6922 #ifndef CONFIG_USER_ONLY
6923 /*
6924 * We don't know until after realize whether there's a GICv3
6925 * attached, and that is what registers the gicv3 sysregs.
6926 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
6927 * at runtime.
6928 */
id_pfr1_read(CPUARMState * env,const ARMCPRegInfo * ri)6929 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
6930 {
6931 ARMCPU *cpu = env_archcpu(env);
6932 uint64_t pfr1 = cpu->isar.id_pfr1;
6933
6934 if (env->gicv3state) {
6935 pfr1 |= 1 << 28;
6936 }
6937 return pfr1;
6938 }
6939
id_aa64pfr0_read(CPUARMState * env,const ARMCPRegInfo * ri)6940 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
6941 {
6942 ARMCPU *cpu = env_archcpu(env);
6943 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
6944
6945 if (env->gicv3state) {
6946 pfr0 |= 1 << 24;
6947 }
6948 return pfr0;
6949 }
6950 #endif
6951
6952 /*
6953 * Shared logic between LORID and the rest of the LOR* registers.
6954 * Secure state exclusion has already been dealt with.
6955 */
access_lor_ns(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6956 static CPAccessResult access_lor_ns(CPUARMState *env,
6957 const ARMCPRegInfo *ri, bool isread)
6958 {
6959 int el = arm_current_el(env);
6960
6961 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
6962 return CP_ACCESS_TRAP_EL2;
6963 }
6964 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
6965 return CP_ACCESS_TRAP_EL3;
6966 }
6967 return CP_ACCESS_OK;
6968 }
6969
access_lor_other(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)6970 static CPAccessResult access_lor_other(CPUARMState *env,
6971 const ARMCPRegInfo *ri, bool isread)
6972 {
6973 if (arm_is_secure_below_el3(env)) {
6974 /* UNDEF if SCR_EL3.NS == 0 */
6975 return CP_ACCESS_UNDEFINED;
6976 }
6977 return access_lor_ns(env, ri, isread);
6978 }
6979
6980 /*
6981 * A trivial implementation of ARMv8.1-LOR leaves all of these
6982 * registers fixed at 0, which indicates that there are zero
6983 * supported Limited Ordering regions.
6984 */
6985 static const ARMCPRegInfo lor_reginfo[] = {
6986 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
6987 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
6988 .access = PL1_RW, .accessfn = access_lor_other,
6989 .fgt = FGT_LORSA_EL1,
6990 .type = ARM_CP_CONST, .resetvalue = 0 },
6991 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
6992 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
6993 .access = PL1_RW, .accessfn = access_lor_other,
6994 .fgt = FGT_LOREA_EL1,
6995 .type = ARM_CP_CONST, .resetvalue = 0 },
6996 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
6997 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
6998 .access = PL1_RW, .accessfn = access_lor_other,
6999 .fgt = FGT_LORN_EL1,
7000 .type = ARM_CP_CONST, .resetvalue = 0 },
7001 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7002 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7003 .access = PL1_RW, .accessfn = access_lor_other,
7004 .fgt = FGT_LORC_EL1,
7005 .type = ARM_CP_CONST, .resetvalue = 0 },
7006 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7007 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
7008 .access = PL1_R, .accessfn = access_lor_ns,
7009 .fgt = FGT_LORID_EL1,
7010 .type = ARM_CP_CONST, .resetvalue = 0 },
7011 };
7012
7013 #ifdef TARGET_AARCH64
access_pauth(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7014 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7015 bool isread)
7016 {
7017 int el = arm_current_el(env);
7018
7019 if (el < 2 &&
7020 arm_is_el2_enabled(env) &&
7021 !(arm_hcr_el2_eff(env) & HCR_APK)) {
7022 return CP_ACCESS_TRAP_EL2;
7023 }
7024 if (el < 3 &&
7025 arm_feature(env, ARM_FEATURE_EL3) &&
7026 !(env->cp15.scr_el3 & SCR_APK)) {
7027 return CP_ACCESS_TRAP_EL3;
7028 }
7029 return CP_ACCESS_OK;
7030 }
7031
7032 static const ARMCPRegInfo pauth_reginfo[] = {
7033 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7034 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7035 .access = PL1_RW, .accessfn = access_pauth,
7036 .fgt = FGT_APDAKEY,
7037 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
7038 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7039 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7040 .access = PL1_RW, .accessfn = access_pauth,
7041 .fgt = FGT_APDAKEY,
7042 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
7043 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7044 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7045 .access = PL1_RW, .accessfn = access_pauth,
7046 .fgt = FGT_APDBKEY,
7047 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
7048 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7049 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7050 .access = PL1_RW, .accessfn = access_pauth,
7051 .fgt = FGT_APDBKEY,
7052 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
7053 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7054 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7055 .access = PL1_RW, .accessfn = access_pauth,
7056 .fgt = FGT_APGAKEY,
7057 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
7058 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7059 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7060 .access = PL1_RW, .accessfn = access_pauth,
7061 .fgt = FGT_APGAKEY,
7062 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
7063 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7064 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7065 .access = PL1_RW, .accessfn = access_pauth,
7066 .fgt = FGT_APIAKEY,
7067 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
7068 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7069 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7070 .access = PL1_RW, .accessfn = access_pauth,
7071 .fgt = FGT_APIAKEY,
7072 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
7073 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7074 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7075 .access = PL1_RW, .accessfn = access_pauth,
7076 .fgt = FGT_APIBKEY,
7077 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
7078 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7079 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7080 .access = PL1_RW, .accessfn = access_pauth,
7081 .fgt = FGT_APIBKEY,
7082 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
7083 };
7084
rndr_readfn(CPUARMState * env,const ARMCPRegInfo * ri)7085 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7086 {
7087 Error *err = NULL;
7088 uint64_t ret;
7089
7090 /* Success sets NZCV = 0000. */
7091 env->NF = env->CF = env->VF = 0, env->ZF = 1;
7092
7093 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7094 /*
7095 * ??? Failed, for unknown reasons in the crypto subsystem.
7096 * The best we can do is log the reason and return the
7097 * timed-out indication to the guest. There is no reason
7098 * we know to expect this failure to be transitory, so the
7099 * guest may well hang retrying the operation.
7100 */
7101 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7102 ri->name, error_get_pretty(err));
7103 error_free(err);
7104
7105 env->ZF = 0; /* NZCF = 0100 */
7106 return 0;
7107 }
7108 return ret;
7109 }
7110
7111 /* We do not support re-seeding, so the two registers operate the same. */
7112 static const ARMCPRegInfo rndr_reginfo[] = {
7113 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7114 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7115 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7116 .access = PL0_R, .readfn = rndr_readfn },
7117 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7118 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7119 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7120 .access = PL0_R, .readfn = rndr_readfn },
7121 };
7122
dccvap_writefn(CPUARMState * env,const ARMCPRegInfo * opaque,uint64_t value)7123 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7124 uint64_t value)
7125 {
7126 #ifdef CONFIG_TCG
7127 ARMCPU *cpu = env_archcpu(env);
7128 /* CTR_EL0 System register -> DminLine, bits [19:16] */
7129 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7130 uint64_t vaddr_in = (uint64_t) value;
7131 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7132 void *haddr;
7133 int mem_idx = arm_env_mmu_index(env);
7134
7135 /* This won't be crossing page boundaries */
7136 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7137 if (haddr) {
7138 #ifndef CONFIG_USER_ONLY
7139
7140 ram_addr_t offset;
7141 MemoryRegion *mr;
7142
7143 /* RCU lock is already being held */
7144 mr = memory_region_from_host(haddr, &offset);
7145
7146 if (mr) {
7147 memory_region_writeback(mr, offset, dline_size);
7148 }
7149 #endif /*CONFIG_USER_ONLY*/
7150 }
7151 #else
7152 /* Handled by hardware accelerator. */
7153 g_assert_not_reached();
7154 #endif /* CONFIG_TCG */
7155 }
7156
7157 static const ARMCPRegInfo dcpop_reg[] = {
7158 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7159 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7160 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7161 .fgt = FGT_DCCVAP,
7162 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7163 };
7164
7165 static const ARMCPRegInfo dcpodp_reg[] = {
7166 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7167 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7168 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
7169 .fgt = FGT_DCCVADP,
7170 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
7171 };
7172
access_aa64_tid5(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7173 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7174 bool isread)
7175 {
7176 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7177 return CP_ACCESS_TRAP_EL2;
7178 }
7179
7180 return CP_ACCESS_OK;
7181 }
7182
access_mte(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7183 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7184 bool isread)
7185 {
7186 int el = arm_current_el(env);
7187 if (el < 2 && arm_is_el2_enabled(env)) {
7188 uint64_t hcr = arm_hcr_el2_eff(env);
7189 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7190 return CP_ACCESS_TRAP_EL2;
7191 }
7192 }
7193 if (el < 3 &&
7194 arm_feature(env, ARM_FEATURE_EL3) &&
7195 !(env->cp15.scr_el3 & SCR_ATA)) {
7196 return CP_ACCESS_TRAP_EL3;
7197 }
7198 return CP_ACCESS_OK;
7199 }
7200
access_tfsr_el1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7201 static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri,
7202 bool isread)
7203 {
7204 CPAccessResult nv1 = access_nv1(env, ri, isread);
7205
7206 if (nv1 != CP_ACCESS_OK) {
7207 return nv1;
7208 }
7209 return access_mte(env, ri, isread);
7210 }
7211
access_tfsr_el2(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7212 static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri,
7213 bool isread)
7214 {
7215 /*
7216 * TFSR_EL2: similar to generic access_mte(), but we need to
7217 * account for FEAT_NV. At EL1 this must be a FEAT_NV access;
7218 * if NV2 is enabled then we will redirect this to TFSR_EL1
7219 * after doing the HCR and SCR ATA traps; otherwise this will
7220 * be a trap to EL2 and the HCR/SCR traps do not apply.
7221 */
7222 int el = arm_current_el(env);
7223
7224 if (el == 1 && (arm_hcr_el2_eff(env) & HCR_NV2)) {
7225 return CP_ACCESS_OK;
7226 }
7227 if (el < 2 && arm_is_el2_enabled(env)) {
7228 uint64_t hcr = arm_hcr_el2_eff(env);
7229 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7230 return CP_ACCESS_TRAP_EL2;
7231 }
7232 }
7233 if (el < 3 &&
7234 arm_feature(env, ARM_FEATURE_EL3) &&
7235 !(env->cp15.scr_el3 & SCR_ATA)) {
7236 return CP_ACCESS_TRAP_EL3;
7237 }
7238 return CP_ACCESS_OK;
7239 }
7240
tco_read(CPUARMState * env,const ARMCPRegInfo * ri)7241 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7242 {
7243 return env->pstate & PSTATE_TCO;
7244 }
7245
tco_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t val)7246 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7247 {
7248 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7249 }
7250
7251 static const ARMCPRegInfo mte_reginfo[] = {
7252 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7253 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7254 .access = PL1_RW, .accessfn = access_mte,
7255 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7256 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7257 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7258 .access = PL1_RW, .accessfn = access_tfsr_el1,
7259 .nv2_redirect_offset = 0x190 | NV2_REDIR_NV1,
7260 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7261 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7262 .type = ARM_CP_NV2_REDIRECT,
7263 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7264 .access = PL2_RW, .accessfn = access_tfsr_el2,
7265 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7266 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7267 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7268 .access = PL3_RW,
7269 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7270 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7271 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7272 .access = PL1_RW, .accessfn = access_mte,
7273 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7274 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7275 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7276 .access = PL1_RW, .accessfn = access_mte,
7277 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7278 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7279 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7280 .type = ARM_CP_NO_RAW,
7281 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
7282 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7283 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7284 .type = ARM_CP_NOP, .access = PL1_W,
7285 .fgt = FGT_DCIVAC,
7286 .accessfn = aa64_cacheop_poc_access },
7287 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7288 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
7289 .fgt = FGT_DCISW,
7290 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7291 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7292 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7293 .type = ARM_CP_NOP, .access = PL1_W,
7294 .fgt = FGT_DCIVAC,
7295 .accessfn = aa64_cacheop_poc_access },
7296 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7297 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
7298 .fgt = FGT_DCISW,
7299 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7300 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7301 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
7302 .fgt = FGT_DCCSW,
7303 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7304 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7305 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
7306 .fgt = FGT_DCCSW,
7307 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7308 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7309 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
7310 .fgt = FGT_DCCISW,
7311 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7312 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7313 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
7314 .fgt = FGT_DCCISW,
7315 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7316 };
7317
7318 static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7319 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7320 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7321 .type = ARM_CP_CONST, .access = PL0_RW, },
7322 };
7323
7324 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7325 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7326 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7327 .type = ARM_CP_NOP, .access = PL0_W,
7328 .fgt = FGT_DCCVAC,
7329 .accessfn = aa64_cacheop_poc_access },
7330 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7331 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7332 .type = ARM_CP_NOP, .access = PL0_W,
7333 .fgt = FGT_DCCVAC,
7334 .accessfn = aa64_cacheop_poc_access },
7335 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7336 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7337 .type = ARM_CP_NOP, .access = PL0_W,
7338 .fgt = FGT_DCCVAP,
7339 .accessfn = aa64_cacheop_poc_access },
7340 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7341 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7342 .type = ARM_CP_NOP, .access = PL0_W,
7343 .fgt = FGT_DCCVAP,
7344 .accessfn = aa64_cacheop_poc_access },
7345 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7346 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7347 .type = ARM_CP_NOP, .access = PL0_W,
7348 .fgt = FGT_DCCVADP,
7349 .accessfn = aa64_cacheop_poc_access },
7350 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7351 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7352 .type = ARM_CP_NOP, .access = PL0_W,
7353 .fgt = FGT_DCCVADP,
7354 .accessfn = aa64_cacheop_poc_access },
7355 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7356 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7357 .type = ARM_CP_NOP, .access = PL0_W,
7358 .fgt = FGT_DCCIVAC,
7359 .accessfn = aa64_cacheop_poc_access },
7360 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7361 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7362 .type = ARM_CP_NOP, .access = PL0_W,
7363 .fgt = FGT_DCCIVAC,
7364 .accessfn = aa64_cacheop_poc_access },
7365 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7366 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7367 .access = PL0_W, .type = ARM_CP_DC_GVA,
7368 #ifndef CONFIG_USER_ONLY
7369 /* Avoid overhead of an access check that always passes in user-mode */
7370 .accessfn = aa64_zva_access,
7371 .fgt = FGT_DCZVA,
7372 #endif
7373 },
7374 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7375 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7376 .access = PL0_W, .type = ARM_CP_DC_GZVA,
7377 #ifndef CONFIG_USER_ONLY
7378 /* Avoid overhead of an access check that always passes in user-mode */
7379 .accessfn = aa64_zva_access,
7380 .fgt = FGT_DCZVA,
7381 #endif
7382 },
7383 };
7384
access_scxtnum(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7385 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7386 bool isread)
7387 {
7388 uint64_t hcr = arm_hcr_el2_eff(env);
7389 int el = arm_current_el(env);
7390
7391 if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7392 if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7393 if (hcr & HCR_TGE) {
7394 return CP_ACCESS_TRAP_EL2;
7395 }
7396 return CP_ACCESS_TRAP_EL1;
7397 }
7398 } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7399 return CP_ACCESS_TRAP_EL2;
7400 }
7401 if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7402 return CP_ACCESS_TRAP_EL2;
7403 }
7404 if (el < 3
7405 && arm_feature(env, ARM_FEATURE_EL3)
7406 && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7407 return CP_ACCESS_TRAP_EL3;
7408 }
7409 return CP_ACCESS_OK;
7410 }
7411
access_scxtnum_el1(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7412 static CPAccessResult access_scxtnum_el1(CPUARMState *env,
7413 const ARMCPRegInfo *ri,
7414 bool isread)
7415 {
7416 CPAccessResult nv1 = access_nv1(env, ri, isread);
7417
7418 if (nv1 != CP_ACCESS_OK) {
7419 return nv1;
7420 }
7421 return access_scxtnum(env, ri, isread);
7422 }
7423
7424 static const ARMCPRegInfo scxtnum_reginfo[] = {
7425 { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7426 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7427 .access = PL0_RW, .accessfn = access_scxtnum,
7428 .fgt = FGT_SCXTNUM_EL0,
7429 .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7430 { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7431 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7432 .access = PL1_RW, .accessfn = access_scxtnum_el1,
7433 .fgt = FGT_SCXTNUM_EL1,
7434 .nv2_redirect_offset = 0x188 | NV2_REDIR_NV1,
7435 .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7436 { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7437 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7438 .access = PL2_RW, .accessfn = access_scxtnum,
7439 .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7440 { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7441 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7442 .access = PL3_RW,
7443 .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7444 };
7445
access_fgt(CPUARMState * env,const ARMCPRegInfo * ri,bool isread)7446 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
7447 bool isread)
7448 {
7449 if (arm_current_el(env) == 2 &&
7450 arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
7451 return CP_ACCESS_TRAP_EL3;
7452 }
7453 return CP_ACCESS_OK;
7454 }
7455
7456 static const ARMCPRegInfo fgt_reginfo[] = {
7457 { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7458 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
7459 .nv2_redirect_offset = 0x1b8,
7460 .access = PL2_RW, .accessfn = access_fgt,
7461 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
7462 { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7463 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
7464 .nv2_redirect_offset = 0x1c0,
7465 .access = PL2_RW, .accessfn = access_fgt,
7466 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
7467 { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7468 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
7469 .nv2_redirect_offset = 0x1d0,
7470 .access = PL2_RW, .accessfn = access_fgt,
7471 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
7472 { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7473 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
7474 .nv2_redirect_offset = 0x1d8,
7475 .access = PL2_RW, .accessfn = access_fgt,
7476 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
7477 { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
7478 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
7479 .nv2_redirect_offset = 0x1c8,
7480 .access = PL2_RW, .accessfn = access_fgt,
7481 .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
7482 };
7483
vncr_write(CPUARMState * env,const ARMCPRegInfo * ri,uint64_t value)7484 static void vncr_write(CPUARMState *env, const ARMCPRegInfo *ri,
7485 uint64_t value)
7486 {
7487 /*
7488 * Clear the RES0 bottom 12 bits; this means at runtime we can guarantee
7489 * that VNCR_EL2 + offset is 64-bit aligned. We don't need to do anything
7490 * about the RESS bits at the top -- we choose the "generate an EL2
7491 * translation abort on use" CONSTRAINED UNPREDICTABLE option (i.e. let
7492 * the ptw.c code detect the resulting invalid address).
7493 */
7494 env->cp15.vncr_el2 = value & ~0xfffULL;
7495 }
7496
7497 static const ARMCPRegInfo nv2_reginfo[] = {
7498 { .name = "VNCR_EL2", .state = ARM_CP_STATE_AA64,
7499 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 2, .opc2 = 0,
7500 .access = PL2_RW,
7501 .writefn = vncr_write,
7502 .nv2_redirect_offset = 0xb0,
7503 .fieldoffset = offsetof(CPUARMState, cp15.vncr_el2) },
7504 };
7505
7506 #endif /* TARGET_AARCH64 */
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 define_tlb_insn_regs(cpu);
7768
7769 if (arm_feature(env, ARM_FEATURE_V6)) {
7770 /* The ID registers all have impdef reset values */
7771 ARMCPRegInfo v6_idregs[] = {
7772 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7773 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7774 .access = PL1_R, .type = ARM_CP_CONST,
7775 .accessfn = access_aa32_tid3,
7776 .resetvalue = cpu->isar.id_pfr0 },
7777 /*
7778 * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
7779 * the value of the GIC field until after we define these regs.
7780 */
7781 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7782 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
7783 .access = PL1_R, .type = ARM_CP_NO_RAW,
7784 .accessfn = access_aa32_tid3,
7785 #ifdef CONFIG_USER_ONLY
7786 .type = ARM_CP_CONST,
7787 .resetvalue = cpu->isar.id_pfr1,
7788 #else
7789 .type = ARM_CP_NO_RAW,
7790 .accessfn = access_aa32_tid3,
7791 .readfn = id_pfr1_read,
7792 .writefn = arm_cp_write_ignore
7793 #endif
7794 },
7795 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7796 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7797 .access = PL1_R, .type = ARM_CP_CONST,
7798 .accessfn = access_aa32_tid3,
7799 .resetvalue = cpu->isar.id_dfr0 },
7800 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7801 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7802 .access = PL1_R, .type = ARM_CP_CONST,
7803 .accessfn = access_aa32_tid3,
7804 .resetvalue = cpu->id_afr0 },
7805 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7806 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7807 .access = PL1_R, .type = ARM_CP_CONST,
7808 .accessfn = access_aa32_tid3,
7809 .resetvalue = cpu->isar.id_mmfr0 },
7810 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7811 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7812 .access = PL1_R, .type = ARM_CP_CONST,
7813 .accessfn = access_aa32_tid3,
7814 .resetvalue = cpu->isar.id_mmfr1 },
7815 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7816 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7817 .access = PL1_R, .type = ARM_CP_CONST,
7818 .accessfn = access_aa32_tid3,
7819 .resetvalue = cpu->isar.id_mmfr2 },
7820 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7821 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7822 .access = PL1_R, .type = ARM_CP_CONST,
7823 .accessfn = access_aa32_tid3,
7824 .resetvalue = cpu->isar.id_mmfr3 },
7825 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7826 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7827 .access = PL1_R, .type = ARM_CP_CONST,
7828 .accessfn = access_aa32_tid3,
7829 .resetvalue = cpu->isar.id_isar0 },
7830 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7831 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7832 .access = PL1_R, .type = ARM_CP_CONST,
7833 .accessfn = access_aa32_tid3,
7834 .resetvalue = cpu->isar.id_isar1 },
7835 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7836 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
7837 .access = PL1_R, .type = ARM_CP_CONST,
7838 .accessfn = access_aa32_tid3,
7839 .resetvalue = cpu->isar.id_isar2 },
7840 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
7841 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
7842 .access = PL1_R, .type = ARM_CP_CONST,
7843 .accessfn = access_aa32_tid3,
7844 .resetvalue = cpu->isar.id_isar3 },
7845 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
7846 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
7847 .access = PL1_R, .type = ARM_CP_CONST,
7848 .accessfn = access_aa32_tid3,
7849 .resetvalue = cpu->isar.id_isar4 },
7850 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
7851 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
7852 .access = PL1_R, .type = ARM_CP_CONST,
7853 .accessfn = access_aa32_tid3,
7854 .resetvalue = cpu->isar.id_isar5 },
7855 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
7856 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
7857 .access = PL1_R, .type = ARM_CP_CONST,
7858 .accessfn = access_aa32_tid3,
7859 .resetvalue = cpu->isar.id_mmfr4 },
7860 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
7861 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
7862 .access = PL1_R, .type = ARM_CP_CONST,
7863 .accessfn = access_aa32_tid3,
7864 .resetvalue = cpu->isar.id_isar6 },
7865 };
7866 define_arm_cp_regs(cpu, v6_idregs);
7867 define_arm_cp_regs(cpu, v6_cp_reginfo);
7868 } else {
7869 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
7870 }
7871 if (arm_feature(env, ARM_FEATURE_V6K)) {
7872 define_arm_cp_regs(cpu, v6k_cp_reginfo);
7873 }
7874 if (arm_feature(env, ARM_FEATURE_V7VE)) {
7875 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
7876 }
7877 if (arm_feature(env, ARM_FEATURE_V7)) {
7878 ARMCPRegInfo clidr = {
7879 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
7880 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
7881 .access = PL1_R, .type = ARM_CP_CONST,
7882 .accessfn = access_tid4,
7883 .fgt = FGT_CLIDR_EL1,
7884 .resetvalue = cpu->clidr
7885 };
7886 define_one_arm_cp_reg(cpu, &clidr);
7887 define_arm_cp_regs(cpu, v7_cp_reginfo);
7888 define_debug_regs(cpu);
7889 define_pmu_regs(cpu);
7890 } else {
7891 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
7892 }
7893 if (arm_feature(env, ARM_FEATURE_V8)) {
7894 /*
7895 * v8 ID registers, which all have impdef reset values.
7896 * Note that within the ID register ranges the unused slots
7897 * must all RAZ, not UNDEF; future architecture versions may
7898 * define new registers here.
7899 * ID registers which are AArch64 views of the AArch32 ID registers
7900 * which already existed in v6 and v7 are handled elsewhere,
7901 * in v6_idregs[].
7902 */
7903 int i;
7904 ARMCPRegInfo v8_idregs[] = {
7905 /*
7906 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
7907 * emulation because we don't know the right value for the
7908 * GIC field until after we define these regs.
7909 */
7910 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
7911 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
7912 .access = PL1_R,
7913 #ifdef CONFIG_USER_ONLY
7914 .type = ARM_CP_CONST,
7915 .resetvalue = cpu->isar.id_aa64pfr0
7916 #else
7917 .type = ARM_CP_NO_RAW,
7918 .accessfn = access_aa64_tid3,
7919 .readfn = id_aa64pfr0_read,
7920 .writefn = arm_cp_write_ignore
7921 #endif
7922 },
7923 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
7924 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
7925 .access = PL1_R, .type = ARM_CP_CONST,
7926 .accessfn = access_aa64_tid3,
7927 .resetvalue = cpu->isar.id_aa64pfr1},
7928 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7929 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
7930 .access = PL1_R, .type = ARM_CP_CONST,
7931 .accessfn = access_aa64_tid3,
7932 .resetvalue = 0 },
7933 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7934 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
7935 .access = PL1_R, .type = ARM_CP_CONST,
7936 .accessfn = access_aa64_tid3,
7937 .resetvalue = 0 },
7938 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
7939 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
7940 .access = PL1_R, .type = ARM_CP_CONST,
7941 .accessfn = access_aa64_tid3,
7942 .resetvalue = cpu->isar.id_aa64zfr0 },
7943 { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
7944 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
7945 .access = PL1_R, .type = ARM_CP_CONST,
7946 .accessfn = access_aa64_tid3,
7947 .resetvalue = cpu->isar.id_aa64smfr0 },
7948 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7949 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
7950 .access = PL1_R, .type = ARM_CP_CONST,
7951 .accessfn = access_aa64_tid3,
7952 .resetvalue = 0 },
7953 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7954 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
7955 .access = PL1_R, .type = ARM_CP_CONST,
7956 .accessfn = access_aa64_tid3,
7957 .resetvalue = 0 },
7958 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
7959 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
7960 .access = PL1_R, .type = ARM_CP_CONST,
7961 .accessfn = access_aa64_tid3,
7962 .resetvalue = cpu->isar.id_aa64dfr0 },
7963 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
7964 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
7965 .access = PL1_R, .type = ARM_CP_CONST,
7966 .accessfn = access_aa64_tid3,
7967 .resetvalue = cpu->isar.id_aa64dfr1 },
7968 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7969 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
7970 .access = PL1_R, .type = ARM_CP_CONST,
7971 .accessfn = access_aa64_tid3,
7972 .resetvalue = 0 },
7973 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7974 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
7975 .access = PL1_R, .type = ARM_CP_CONST,
7976 .accessfn = access_aa64_tid3,
7977 .resetvalue = 0 },
7978 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
7979 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
7980 .access = PL1_R, .type = ARM_CP_CONST,
7981 .accessfn = access_aa64_tid3,
7982 .resetvalue = cpu->id_aa64afr0 },
7983 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
7984 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
7985 .access = PL1_R, .type = ARM_CP_CONST,
7986 .accessfn = access_aa64_tid3,
7987 .resetvalue = cpu->id_aa64afr1 },
7988 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7989 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
7990 .access = PL1_R, .type = ARM_CP_CONST,
7991 .accessfn = access_aa64_tid3,
7992 .resetvalue = 0 },
7993 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
7994 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
7995 .access = PL1_R, .type = ARM_CP_CONST,
7996 .accessfn = access_aa64_tid3,
7997 .resetvalue = 0 },
7998 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
7999 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8000 .access = PL1_R, .type = ARM_CP_CONST,
8001 .accessfn = access_aa64_tid3,
8002 .resetvalue = cpu->isar.id_aa64isar0 },
8003 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8004 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8005 .access = PL1_R, .type = ARM_CP_CONST,
8006 .accessfn = access_aa64_tid3,
8007 .resetvalue = cpu->isar.id_aa64isar1 },
8008 { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64,
8009 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8010 .access = PL1_R, .type = ARM_CP_CONST,
8011 .accessfn = access_aa64_tid3,
8012 .resetvalue = cpu->isar.id_aa64isar2 },
8013 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8014 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8015 .access = PL1_R, .type = ARM_CP_CONST,
8016 .accessfn = access_aa64_tid3,
8017 .resetvalue = 0 },
8018 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8019 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8020 .access = PL1_R, .type = ARM_CP_CONST,
8021 .accessfn = access_aa64_tid3,
8022 .resetvalue = 0 },
8023 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8024 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8025 .access = PL1_R, .type = ARM_CP_CONST,
8026 .accessfn = access_aa64_tid3,
8027 .resetvalue = 0 },
8028 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8029 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8030 .access = PL1_R, .type = ARM_CP_CONST,
8031 .accessfn = access_aa64_tid3,
8032 .resetvalue = 0 },
8033 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8034 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8035 .access = PL1_R, .type = ARM_CP_CONST,
8036 .accessfn = access_aa64_tid3,
8037 .resetvalue = 0 },
8038 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8039 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8040 .access = PL1_R, .type = ARM_CP_CONST,
8041 .accessfn = access_aa64_tid3,
8042 .resetvalue = cpu->isar.id_aa64mmfr0 },
8043 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8044 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8045 .access = PL1_R, .type = ARM_CP_CONST,
8046 .accessfn = access_aa64_tid3,
8047 .resetvalue = cpu->isar.id_aa64mmfr1 },
8048 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
8049 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8050 .access = PL1_R, .type = ARM_CP_CONST,
8051 .accessfn = access_aa64_tid3,
8052 .resetvalue = cpu->isar.id_aa64mmfr2 },
8053 { .name = "ID_AA64MMFR3_EL1", .state = ARM_CP_STATE_AA64,
8054 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8055 .access = PL1_R, .type = ARM_CP_CONST,
8056 .accessfn = access_aa64_tid3,
8057 .resetvalue = cpu->isar.id_aa64mmfr3 },
8058 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8059 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8060 .access = PL1_R, .type = ARM_CP_CONST,
8061 .accessfn = access_aa64_tid3,
8062 .resetvalue = 0 },
8063 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8064 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8065 .access = PL1_R, .type = ARM_CP_CONST,
8066 .accessfn = access_aa64_tid3,
8067 .resetvalue = 0 },
8068 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8069 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8070 .access = PL1_R, .type = ARM_CP_CONST,
8071 .accessfn = access_aa64_tid3,
8072 .resetvalue = 0 },
8073 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8074 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8075 .access = PL1_R, .type = ARM_CP_CONST,
8076 .accessfn = access_aa64_tid3,
8077 .resetvalue = 0 },
8078 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8079 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8080 .access = PL1_R, .type = ARM_CP_CONST,
8081 .accessfn = access_aa64_tid3,
8082 .resetvalue = cpu->isar.mvfr0 },
8083 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8084 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8085 .access = PL1_R, .type = ARM_CP_CONST,
8086 .accessfn = access_aa64_tid3,
8087 .resetvalue = cpu->isar.mvfr1 },
8088 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8089 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8090 .access = PL1_R, .type = ARM_CP_CONST,
8091 .accessfn = access_aa64_tid3,
8092 .resetvalue = cpu->isar.mvfr2 },
8093 /*
8094 * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8095 * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8096 * as RAZ, since it is in the "reserved for future ID
8097 * registers, RAZ" part of the AArch32 encoding space.
8098 */
8099 { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8100 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8101 .access = PL1_R, .type = ARM_CP_CONST,
8102 .accessfn = access_aa64_tid3,
8103 .resetvalue = 0 },
8104 { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8105 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8106 .access = PL1_R, .type = ARM_CP_CONST,
8107 .accessfn = access_aa64_tid3,
8108 .resetvalue = 0 },
8109 { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8110 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8111 .access = PL1_R, .type = ARM_CP_CONST,
8112 .accessfn = access_aa64_tid3,
8113 .resetvalue = 0 },
8114 /*
8115 * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8116 * they're also RAZ for AArch64, and in v8 are gradually
8117 * being filled with AArch64-view-of-AArch32-ID-register
8118 * for new ID registers.
8119 */
8120 { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
8121 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8122 .access = PL1_R, .type = ARM_CP_CONST,
8123 .accessfn = access_aa64_tid3,
8124 .resetvalue = 0 },
8125 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
8126 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8127 .access = PL1_R, .type = ARM_CP_CONST,
8128 .accessfn = access_aa64_tid3,
8129 .resetvalue = cpu->isar.id_pfr2 },
8130 { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
8131 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8132 .access = PL1_R, .type = ARM_CP_CONST,
8133 .accessfn = access_aa64_tid3,
8134 .resetvalue = cpu->isar.id_dfr1 },
8135 { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
8136 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8137 .access = PL1_R, .type = ARM_CP_CONST,
8138 .accessfn = access_aa64_tid3,
8139 .resetvalue = cpu->isar.id_mmfr5 },
8140 { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
8141 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8142 .access = PL1_R, .type = ARM_CP_CONST,
8143 .accessfn = access_aa64_tid3,
8144 .resetvalue = 0 },
8145 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8146 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8147 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8148 .fgt = FGT_PMCEIDN_EL0,
8149 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
8150 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8151 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8152 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8153 .fgt = FGT_PMCEIDN_EL0,
8154 .resetvalue = cpu->pmceid0 },
8155 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8156 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8157 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8158 .fgt = FGT_PMCEIDN_EL0,
8159 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
8160 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8161 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8162 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
8163 .fgt = FGT_PMCEIDN_EL0,
8164 .resetvalue = cpu->pmceid1 },
8165 };
8166 #ifdef CONFIG_USER_ONLY
8167 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
8168 { .name = "ID_AA64PFR0_EL1",
8169 .exported_bits = R_ID_AA64PFR0_FP_MASK |
8170 R_ID_AA64PFR0_ADVSIMD_MASK |
8171 R_ID_AA64PFR0_SVE_MASK |
8172 R_ID_AA64PFR0_DIT_MASK,
8173 .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8174 (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
8175 { .name = "ID_AA64PFR1_EL1",
8176 .exported_bits = R_ID_AA64PFR1_BT_MASK |
8177 R_ID_AA64PFR1_SSBS_MASK |
8178 R_ID_AA64PFR1_MTE_MASK |
8179 R_ID_AA64PFR1_SME_MASK },
8180 { .name = "ID_AA64PFR*_EL1_RESERVED",
8181 .is_glob = true },
8182 { .name = "ID_AA64ZFR0_EL1",
8183 .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8184 R_ID_AA64ZFR0_AES_MASK |
8185 R_ID_AA64ZFR0_BITPERM_MASK |
8186 R_ID_AA64ZFR0_BFLOAT16_MASK |
8187 R_ID_AA64ZFR0_B16B16_MASK |
8188 R_ID_AA64ZFR0_SHA3_MASK |
8189 R_ID_AA64ZFR0_SM4_MASK |
8190 R_ID_AA64ZFR0_I8MM_MASK |
8191 R_ID_AA64ZFR0_F32MM_MASK |
8192 R_ID_AA64ZFR0_F64MM_MASK },
8193 { .name = "ID_AA64SMFR0_EL1",
8194 .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8195 R_ID_AA64SMFR0_BI32I32_MASK |
8196 R_ID_AA64SMFR0_B16F32_MASK |
8197 R_ID_AA64SMFR0_F16F32_MASK |
8198 R_ID_AA64SMFR0_I8I32_MASK |
8199 R_ID_AA64SMFR0_F16F16_MASK |
8200 R_ID_AA64SMFR0_B16B16_MASK |
8201 R_ID_AA64SMFR0_I16I32_MASK |
8202 R_ID_AA64SMFR0_F64F64_MASK |
8203 R_ID_AA64SMFR0_I16I64_MASK |
8204 R_ID_AA64SMFR0_SMEVER_MASK |
8205 R_ID_AA64SMFR0_FA64_MASK },
8206 { .name = "ID_AA64MMFR0_EL1",
8207 .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8208 .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8209 (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8210 { .name = "ID_AA64MMFR1_EL1",
8211 .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8212 { .name = "ID_AA64MMFR2_EL1",
8213 .exported_bits = R_ID_AA64MMFR2_AT_MASK },
8214 { .name = "ID_AA64MMFR3_EL1",
8215 .exported_bits = 0 },
8216 { .name = "ID_AA64MMFR*_EL1_RESERVED",
8217 .is_glob = true },
8218 { .name = "ID_AA64DFR0_EL1",
8219 .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8220 { .name = "ID_AA64DFR1_EL1" },
8221 { .name = "ID_AA64DFR*_EL1_RESERVED",
8222 .is_glob = true },
8223 { .name = "ID_AA64AFR*",
8224 .is_glob = true },
8225 { .name = "ID_AA64ISAR0_EL1",
8226 .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8227 R_ID_AA64ISAR0_SHA1_MASK |
8228 R_ID_AA64ISAR0_SHA2_MASK |
8229 R_ID_AA64ISAR0_CRC32_MASK |
8230 R_ID_AA64ISAR0_ATOMIC_MASK |
8231 R_ID_AA64ISAR0_RDM_MASK |
8232 R_ID_AA64ISAR0_SHA3_MASK |
8233 R_ID_AA64ISAR0_SM3_MASK |
8234 R_ID_AA64ISAR0_SM4_MASK |
8235 R_ID_AA64ISAR0_DP_MASK |
8236 R_ID_AA64ISAR0_FHM_MASK |
8237 R_ID_AA64ISAR0_TS_MASK |
8238 R_ID_AA64ISAR0_RNDR_MASK },
8239 { .name = "ID_AA64ISAR1_EL1",
8240 .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8241 R_ID_AA64ISAR1_APA_MASK |
8242 R_ID_AA64ISAR1_API_MASK |
8243 R_ID_AA64ISAR1_JSCVT_MASK |
8244 R_ID_AA64ISAR1_FCMA_MASK |
8245 R_ID_AA64ISAR1_LRCPC_MASK |
8246 R_ID_AA64ISAR1_GPA_MASK |
8247 R_ID_AA64ISAR1_GPI_MASK |
8248 R_ID_AA64ISAR1_FRINTTS_MASK |
8249 R_ID_AA64ISAR1_SB_MASK |
8250 R_ID_AA64ISAR1_BF16_MASK |
8251 R_ID_AA64ISAR1_DGH_MASK |
8252 R_ID_AA64ISAR1_I8MM_MASK },
8253 { .name = "ID_AA64ISAR2_EL1",
8254 .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8255 R_ID_AA64ISAR2_RPRES_MASK |
8256 R_ID_AA64ISAR2_GPA3_MASK |
8257 R_ID_AA64ISAR2_APA3_MASK |
8258 R_ID_AA64ISAR2_MOPS_MASK |
8259 R_ID_AA64ISAR2_BC_MASK |
8260 R_ID_AA64ISAR2_RPRFM_MASK |
8261 R_ID_AA64ISAR2_CSSC_MASK },
8262 { .name = "ID_AA64ISAR*_EL1_RESERVED",
8263 .is_glob = true },
8264 };
8265 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8266 #endif
8267 /*
8268 * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL.
8269 * TODO: For RMR, a write with bit 1 set should do something with
8270 * cpu_reset(). In the meantime, "the bit is strictly a request",
8271 * so we are in spec just ignoring writes.
8272 */
8273 if (!arm_feature(env, ARM_FEATURE_EL3) &&
8274 !arm_feature(env, ARM_FEATURE_EL2)) {
8275 ARMCPRegInfo el1_reset_regs[] = {
8276 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
8277 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8278 .access = PL1_R,
8279 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8280 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH,
8281 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8282 .access = PL1_RW, .type = ARM_CP_CONST,
8283 .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }
8284 };
8285 define_arm_cp_regs(cpu, el1_reset_regs);
8286 }
8287 define_arm_cp_regs(cpu, v8_idregs);
8288 define_arm_cp_regs(cpu, v8_cp_reginfo);
8289 if (cpu_isar_feature(aa64_aa32_el1, cpu)) {
8290 define_arm_cp_regs(cpu, v8_aa32_el1_reginfo);
8291 }
8292
8293 for (i = 4; i < 16; i++) {
8294 /*
8295 * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8296 * For pre-v8 cores there are RAZ patterns for these in
8297 * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8298 * v8 extends the "must RAZ" part of the ID register space
8299 * to also cover c0, 0, c{8-15}, {0-7}.
8300 * These are STATE_AA32 because in the AArch64 sysreg space
8301 * c4-c7 is where the AArch64 ID registers live (and we've
8302 * already defined those in v8_idregs[]), and c8-c15 are not
8303 * "must RAZ" for AArch64.
8304 */
8305 g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8306 ARMCPRegInfo v8_aa32_raz_idregs = {
8307 .name = name,
8308 .state = ARM_CP_STATE_AA32,
8309 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8310 .access = PL1_R, .type = ARM_CP_CONST,
8311 .accessfn = access_aa64_tid3,
8312 .resetvalue = 0 };
8313 define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8314 }
8315 }
8316
8317 /*
8318 * Register the base EL2 cpregs.
8319 * Pre v8, these registers are implemented only as part of the
8320 * Virtualization Extensions (EL2 present). Beginning with v8,
8321 * if EL2 is missing but EL3 is enabled, mostly these become
8322 * RES0 from EL3, with some specific exceptions.
8323 */
8324 if (arm_feature(env, ARM_FEATURE_EL2)
8325 || (arm_feature(env, ARM_FEATURE_EL3)
8326 && arm_feature(env, ARM_FEATURE_V8))) {
8327 uint64_t vmpidr_def = mpidr_read_val(env);
8328 ARMCPRegInfo vpidr_regs[] = {
8329 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8330 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8331 .access = PL2_RW, .accessfn = access_el3_aa32ns,
8332 .resetvalue = cpu->midr,
8333 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8334 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
8335 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8336 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8337 .access = PL2_RW, .resetvalue = cpu->midr,
8338 .type = ARM_CP_EL3_NO_EL2_C_NZ,
8339 .nv2_redirect_offset = 0x88,
8340 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
8341 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8342 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8343 .access = PL2_RW, .accessfn = access_el3_aa32ns,
8344 .resetvalue = vmpidr_def,
8345 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
8346 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
8347 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8348 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8349 .access = PL2_RW, .resetvalue = vmpidr_def,
8350 .type = ARM_CP_EL3_NO_EL2_C_NZ,
8351 .nv2_redirect_offset = 0x50,
8352 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
8353 };
8354 /*
8355 * The only field of MDCR_EL2 that has a defined architectural reset
8356 * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8357 */
8358 ARMCPRegInfo mdcr_el2 = {
8359 .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
8360 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
8361 .writefn = mdcr_el2_write,
8362 .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8363 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8364 };
8365 define_one_arm_cp_reg(cpu, &mdcr_el2);
8366 define_arm_cp_regs(cpu, vpidr_regs);
8367 define_arm_cp_regs(cpu, el2_cp_reginfo);
8368 if (arm_feature(env, ARM_FEATURE_V8)) {
8369 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8370 }
8371 if (cpu_isar_feature(aa64_sel2, cpu)) {
8372 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8373 }
8374 /*
8375 * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL.
8376 * See commentary near RMR_EL1.
8377 */
8378 if (!arm_feature(env, ARM_FEATURE_EL3)) {
8379 static const ARMCPRegInfo el2_reset_regs[] = {
8380 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8381 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8382 .access = PL2_R,
8383 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8384 { .name = "RVBAR", .type = ARM_CP_ALIAS,
8385 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8386 .access = PL2_R,
8387 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) },
8388 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64,
8389 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2,
8390 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8391 };
8392 define_arm_cp_regs(cpu, el2_reset_regs);
8393 }
8394 }
8395
8396 /* Register the base EL3 cpregs. */
8397 if (arm_feature(env, ARM_FEATURE_EL3)) {
8398 define_arm_cp_regs(cpu, el3_cp_reginfo);
8399 ARMCPRegInfo el3_regs[] = {
8400 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8401 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
8402 .access = PL3_R,
8403 .fieldoffset = offsetof(CPUARMState, cp15.rvbar), },
8404 { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64,
8405 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2,
8406 .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 },
8407 { .name = "RMR", .state = ARM_CP_STATE_AA32,
8408 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2,
8409 .access = PL3_RW, .type = ARM_CP_CONST,
8410 .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) },
8411 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8412 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8413 .access = PL3_RW,
8414 .raw_writefn = raw_write, .writefn = sctlr_write,
8415 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8416 .resetvalue = cpu->reset_sctlr },
8417 };
8418
8419 define_arm_cp_regs(cpu, el3_regs);
8420 }
8421 /*
8422 * The behaviour of NSACR is sufficiently various that we don't
8423 * try to describe it in a single reginfo:
8424 * if EL3 is 64 bit, then trap to EL3 from S EL1,
8425 * reads as constant 0xc00 from NS EL1 and NS EL2
8426 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8427 * if v7 without EL3, register doesn't exist
8428 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8429 */
8430 if (arm_feature(env, ARM_FEATURE_EL3)) {
8431 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8432 static const ARMCPRegInfo nsacr = {
8433 .name = "NSACR", .type = ARM_CP_CONST,
8434 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8435 .access = PL1_RW, .accessfn = nsacr_access,
8436 .resetvalue = 0xc00
8437 };
8438 define_one_arm_cp_reg(cpu, &nsacr);
8439 } else {
8440 static const ARMCPRegInfo nsacr = {
8441 .name = "NSACR",
8442 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8443 .access = PL3_RW | PL1_R,
8444 .resetvalue = 0,
8445 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8446 };
8447 define_one_arm_cp_reg(cpu, &nsacr);
8448 }
8449 } else {
8450 if (arm_feature(env, ARM_FEATURE_V8)) {
8451 static const ARMCPRegInfo nsacr = {
8452 .name = "NSACR", .type = ARM_CP_CONST,
8453 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8454 .access = PL1_R,
8455 .resetvalue = 0xc00
8456 };
8457 define_one_arm_cp_reg(cpu, &nsacr);
8458 }
8459 }
8460
8461 if (arm_feature(env, ARM_FEATURE_PMSA)) {
8462 if (arm_feature(env, ARM_FEATURE_V6)) {
8463 /* PMSAv6 not implemented */
8464 assert(arm_feature(env, ARM_FEATURE_V7));
8465 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8466 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8467 } else {
8468 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8469 }
8470 } else {
8471 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8472 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
8473 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
8474 if (cpu_isar_feature(aa32_hpd, cpu)) {
8475 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8476 }
8477 }
8478 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8479 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8480 }
8481 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8482 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8483 }
8484 if (cpu_isar_feature(aa64_ecv_traps, cpu)) {
8485 define_arm_cp_regs(cpu, gen_timer_ecv_cp_reginfo);
8486 }
8487 #ifndef CONFIG_USER_ONLY
8488 if (cpu_isar_feature(aa64_ecv, cpu)) {
8489 define_one_arm_cp_reg(cpu, &gen_timer_cntpoff_reginfo);
8490 }
8491 #endif
8492 if (arm_feature(env, ARM_FEATURE_VAPA)) {
8493 ARMCPRegInfo vapa_cp_reginfo[] = {
8494 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
8495 .access = PL1_RW, .resetvalue = 0,
8496 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
8497 offsetoflow32(CPUARMState, cp15.par_ns) },
8498 .writefn = par_write},
8499 #ifndef CONFIG_USER_ONLY
8500 /* This underdecoding is safe because the reginfo is NO_RAW. */
8501 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
8502 .access = PL1_W, .accessfn = ats_access,
8503 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
8504 #endif
8505 };
8506
8507 /*
8508 * When LPAE exists this 32-bit PAR register is an alias of the
8509 * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[]
8510 */
8511 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8512 vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB;
8513 }
8514 define_arm_cp_regs(cpu, vapa_cp_reginfo);
8515 }
8516 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8517 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8518 }
8519 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8520 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8521 }
8522 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8523 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8524 }
8525 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8526 define_arm_cp_regs(cpu, omap_cp_reginfo);
8527 }
8528 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8529 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8530 }
8531 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8532 define_arm_cp_regs(cpu, xscale_cp_reginfo);
8533 }
8534 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8535 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8536 }
8537 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8538 define_arm_cp_regs(cpu, lpae_cp_reginfo);
8539 }
8540 if (cpu_isar_feature(aa32_jazelle, cpu)) {
8541 define_arm_cp_regs(cpu, jazelle_regs);
8542 }
8543 /*
8544 * Slightly awkwardly, the OMAP and StrongARM cores need all of
8545 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8546 * be read-only (ie write causes UNDEF exception).
8547 */
8548 {
8549 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
8550 /*
8551 * Pre-v8 MIDR space.
8552 * Note that the MIDR isn't a simple constant register because
8553 * of the TI925 behaviour where writes to another register can
8554 * cause the MIDR value to change.
8555 *
8556 * Unimplemented registers in the c15 0 0 0 space default to
8557 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8558 * and friends override accordingly.
8559 */
8560 { .name = "MIDR",
8561 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
8562 .access = PL1_R, .resetvalue = cpu->midr,
8563 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
8564 .readfn = midr_read,
8565 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8566 .type = ARM_CP_OVERRIDE },
8567 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8568 { .name = "DUMMY",
8569 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8570 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8571 { .name = "DUMMY",
8572 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8573 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8574 { .name = "DUMMY",
8575 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8576 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8577 { .name = "DUMMY",
8578 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8579 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8580 { .name = "DUMMY",
8581 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8582 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8583 };
8584 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
8585 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8586 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
8587 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
8588 .fgt = FGT_MIDR_EL1,
8589 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8590 .readfn = midr_read },
8591 /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
8592 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8593 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8594 .access = PL1_R, .resetvalue = cpu->midr },
8595 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8596 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
8597 .access = PL1_R,
8598 .accessfn = access_aa64_tid1,
8599 .fgt = FGT_REVIDR_EL1,
8600 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
8601 };
8602 ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
8603 .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB,
8604 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8605 .access = PL1_R, .resetvalue = cpu->midr
8606 };
8607 ARMCPRegInfo id_cp_reginfo[] = {
8608 /* These are common to v8 and pre-v8 */
8609 { .name = "CTR",
8610 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
8611 .access = PL1_R, .accessfn = ctr_el0_access,
8612 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8613 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8614 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8615 .access = PL0_R, .accessfn = ctr_el0_access,
8616 .fgt = FGT_CTR_EL0,
8617 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8618 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8619 { .name = "TCMTR",
8620 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
8621 .access = PL1_R,
8622 .accessfn = access_aa32_tid1,
8623 .type = ARM_CP_CONST, .resetvalue = 0 },
8624 };
8625 /* TLBTR is specific to VMSA */
8626 ARMCPRegInfo id_tlbtr_reginfo = {
8627 .name = "TLBTR",
8628 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
8629 .access = PL1_R,
8630 .accessfn = access_aa32_tid1,
8631 .type = ARM_CP_CONST, .resetvalue = 0,
8632 };
8633 /* MPUIR is specific to PMSA V6+ */
8634 ARMCPRegInfo id_mpuir_reginfo = {
8635 .name = "MPUIR",
8636 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8637 .access = PL1_R, .type = ARM_CP_CONST,
8638 .resetvalue = cpu->pmsav7_dregion << 8
8639 };
8640 /* HMPUIR is specific to PMSA V8 */
8641 ARMCPRegInfo id_hmpuir_reginfo = {
8642 .name = "HMPUIR",
8643 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
8644 .access = PL2_R, .type = ARM_CP_CONST,
8645 .resetvalue = cpu->pmsav8r_hdregion
8646 };
8647 static const ARMCPRegInfo crn0_wi_reginfo = {
8648 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8649 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8650 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8651 };
8652 #ifdef CONFIG_USER_ONLY
8653 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
8654 { .name = "MIDR_EL1",
8655 .exported_bits = R_MIDR_EL1_REVISION_MASK |
8656 R_MIDR_EL1_PARTNUM_MASK |
8657 R_MIDR_EL1_ARCHITECTURE_MASK |
8658 R_MIDR_EL1_VARIANT_MASK |
8659 R_MIDR_EL1_IMPLEMENTER_MASK },
8660 { .name = "REVIDR_EL1" },
8661 };
8662 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8663 #endif
8664 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8665 arm_feature(env, ARM_FEATURE_STRONGARM)) {
8666 size_t i;
8667 /*
8668 * Register the blanket "writes ignored" value first to cover the
8669 * whole space. Then update the specific ID registers to allow write
8670 * access, so that they ignore writes rather than causing them to
8671 * UNDEF.
8672 */
8673 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
8674 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8675 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
8676 }
8677 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8678 id_cp_reginfo[i].access = PL1_RW;
8679 }
8680 id_mpuir_reginfo.access = PL1_RW;
8681 id_tlbtr_reginfo.access = PL1_RW;
8682 }
8683 if (arm_feature(env, ARM_FEATURE_V8)) {
8684 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
8685 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8686 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
8687 }
8688 } else {
8689 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8690 }
8691 define_arm_cp_regs(cpu, id_cp_reginfo);
8692 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8693 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
8694 } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
8695 arm_feature(env, ARM_FEATURE_V8)) {
8696 uint32_t i = 0;
8697 char *tmp_string;
8698
8699 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8700 define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
8701 define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
8702
8703 /* Register alias is only valid for first 32 indexes */
8704 for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
8705 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8706 uint8_t opc1 = extract32(i, 4, 1);
8707 uint8_t opc2 = extract32(i, 0, 1) << 2;
8708
8709 tmp_string = g_strdup_printf("PRBAR%u", i);
8710 ARMCPRegInfo tmp_prbarn_reginfo = {
8711 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8712 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8713 .access = PL1_RW, .resetvalue = 0,
8714 .accessfn = access_tvm_trvm,
8715 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8716 };
8717 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
8718 g_free(tmp_string);
8719
8720 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8721 tmp_string = g_strdup_printf("PRLAR%u", i);
8722 ARMCPRegInfo tmp_prlarn_reginfo = {
8723 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8724 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8725 .access = PL1_RW, .resetvalue = 0,
8726 .accessfn = access_tvm_trvm,
8727 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8728 };
8729 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
8730 g_free(tmp_string);
8731 }
8732
8733 /* Register alias is only valid for first 32 indexes */
8734 for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
8735 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8736 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
8737 uint8_t opc2 = extract32(i, 0, 1) << 2;
8738
8739 tmp_string = g_strdup_printf("HPRBAR%u", i);
8740 ARMCPRegInfo tmp_hprbarn_reginfo = {
8741 .name = tmp_string,
8742 .type = ARM_CP_NO_RAW,
8743 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8744 .access = PL2_RW, .resetvalue = 0,
8745 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8746 };
8747 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
8748 g_free(tmp_string);
8749
8750 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8751 tmp_string = g_strdup_printf("HPRLAR%u", i);
8752 ARMCPRegInfo tmp_hprlarn_reginfo = {
8753 .name = tmp_string,
8754 .type = ARM_CP_NO_RAW,
8755 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8756 .access = PL2_RW, .resetvalue = 0,
8757 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8758 };
8759 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
8760 g_free(tmp_string);
8761 }
8762 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8763 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8764 }
8765 }
8766
8767 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
8768 ARMCPRegInfo mpidr_cp_reginfo[] = {
8769 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8770 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
8771 .fgt = FGT_MPIDR_EL1,
8772 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
8773 };
8774 #ifdef CONFIG_USER_ONLY
8775 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
8776 { .name = "MPIDR_EL1",
8777 .fixed_bits = 0x0000000080000000 },
8778 };
8779 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8780 #endif
8781 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8782 }
8783
8784 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
8785 ARMCPRegInfo auxcr_reginfo[] = {
8786 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8787 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
8788 .access = PL1_RW, .accessfn = access_tacr,
8789 .nv2_redirect_offset = 0x118,
8790 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
8791 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8792 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8793 .access = PL2_RW, .type = ARM_CP_CONST,
8794 .resetvalue = 0 },
8795 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8796 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8797 .access = PL3_RW, .type = ARM_CP_CONST,
8798 .resetvalue = 0 },
8799 };
8800 define_arm_cp_regs(cpu, auxcr_reginfo);
8801 if (cpu_isar_feature(aa32_ac2, cpu)) {
8802 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
8803 }
8804 }
8805
8806 if (arm_feature(env, ARM_FEATURE_CBAR)) {
8807 /*
8808 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8809 * There are two flavours:
8810 * (1) older 32-bit only cores have a simple 32-bit CBAR
8811 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8812 * 32-bit register visible to AArch32 at a different encoding
8813 * to the "flavour 1" register and with the bits rearranged to
8814 * be able to squash a 64-bit address into the 32-bit view.
8815 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8816 * in future if we support AArch32-only configs of some of the
8817 * AArch64 cores we might need to add a specific feature flag
8818 * to indicate cores with "flavour 2" CBAR.
8819 */
8820 if (arm_feature(env, ARM_FEATURE_V8)) {
8821 /* 32 bit view is [31:18] 0...0 [43:32]. */
8822 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8823 | extract64(cpu->reset_cbar, 32, 12);
8824 ARMCPRegInfo cbar_reginfo[] = {
8825 { .name = "CBAR",
8826 .type = ARM_CP_CONST,
8827 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8828 .access = PL1_R, .resetvalue = cbar32 },
8829 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8830 .type = ARM_CP_CONST,
8831 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
8832 .access = PL1_R, .resetvalue = cpu->reset_cbar },
8833 };
8834 /* We don't implement a r/w 64 bit CBAR currently */
8835 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8836 define_arm_cp_regs(cpu, cbar_reginfo);
8837 } else {
8838 ARMCPRegInfo cbar = {
8839 .name = "CBAR",
8840 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
8841 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
8842 .fieldoffset = offsetof(CPUARMState,
8843 cp15.c15_config_base_address)
8844 };
8845 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8846 cbar.access = PL1_R;
8847 cbar.fieldoffset = 0;
8848 cbar.type = ARM_CP_CONST;
8849 }
8850 define_one_arm_cp_reg(cpu, &cbar);
8851 }
8852 }
8853
8854 if (arm_feature(env, ARM_FEATURE_VBAR)) {
8855 static const ARMCPRegInfo vbar_cp_reginfo[] = {
8856 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8857 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8858 .access = PL1_RW, .writefn = vbar_write,
8859 .accessfn = access_nv1,
8860 .fgt = FGT_VBAR_EL1,
8861 .nv2_redirect_offset = 0x250 | NV2_REDIR_NV1,
8862 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8863 offsetof(CPUARMState, cp15.vbar_ns) },
8864 .resetvalue = 0 },
8865 };
8866 define_arm_cp_regs(cpu, vbar_cp_reginfo);
8867 }
8868
8869 /* Generic registers whose values depend on the implementation */
8870 {
8871 ARMCPRegInfo sctlr = {
8872 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
8873 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
8874 .access = PL1_RW, .accessfn = access_tvm_trvm,
8875 .fgt = FGT_SCTLR_EL1,
8876 .nv2_redirect_offset = 0x110 | NV2_REDIR_NV1,
8877 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8878 offsetof(CPUARMState, cp15.sctlr_ns) },
8879 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8880 .raw_writefn = raw_write,
8881 };
8882 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8883 /*
8884 * Normally we would always end the TB on an SCTLR write, but Linux
8885 * arch/arm/mach-pxa/sleep.S expects two instructions following
8886 * an MMU enable to execute from cache. Imitate this behaviour.
8887 */
8888 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8889 }
8890 define_one_arm_cp_reg(cpu, &sctlr);
8891
8892 if (arm_feature(env, ARM_FEATURE_PMSA) &&
8893 arm_feature(env, ARM_FEATURE_V8)) {
8894 ARMCPRegInfo vsctlr = {
8895 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
8896 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
8897 .access = PL2_RW, .resetvalue = 0x0,
8898 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
8899 };
8900 define_one_arm_cp_reg(cpu, &vsctlr);
8901 }
8902 }
8903
8904 if (cpu_isar_feature(aa64_lor, cpu)) {
8905 define_arm_cp_regs(cpu, lor_reginfo);
8906 }
8907 if (cpu_isar_feature(aa64_pan, cpu)) {
8908 define_one_arm_cp_reg(cpu, &pan_reginfo);
8909 }
8910 #ifndef CONFIG_USER_ONLY
8911 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
8912 define_arm_cp_regs(cpu, ats1e1_reginfo);
8913 }
8914 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
8915 define_arm_cp_regs(cpu, ats1cp_reginfo);
8916 }
8917 #endif
8918 if (cpu_isar_feature(aa64_uao, cpu)) {
8919 define_one_arm_cp_reg(cpu, &uao_reginfo);
8920 }
8921
8922 if (cpu_isar_feature(aa64_dit, cpu)) {
8923 define_one_arm_cp_reg(cpu, &dit_reginfo);
8924 }
8925 if (cpu_isar_feature(aa64_ssbs, cpu)) {
8926 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
8927 }
8928 if (cpu_isar_feature(any_ras, cpu)) {
8929 define_arm_cp_regs(cpu, minimal_ras_reginfo);
8930 }
8931
8932 if (cpu_isar_feature(aa64_vh, cpu) ||
8933 cpu_isar_feature(aa64_debugv8p2, cpu)) {
8934 define_one_arm_cp_reg(cpu, &contextidr_el2);
8935 }
8936 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
8937 define_arm_cp_regs(cpu, vhe_reginfo);
8938 }
8939
8940 if (cpu_isar_feature(aa64_sve, cpu)) {
8941 define_arm_cp_regs(cpu, zcr_reginfo);
8942 }
8943
8944 if (cpu_isar_feature(aa64_hcx, cpu)) {
8945 define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
8946 }
8947
8948 #ifdef TARGET_AARCH64
8949 if (cpu_isar_feature(aa64_sme, cpu)) {
8950 define_arm_cp_regs(cpu, sme_reginfo);
8951 }
8952 if (cpu_isar_feature(aa64_pauth, cpu)) {
8953 define_arm_cp_regs(cpu, pauth_reginfo);
8954 }
8955 if (cpu_isar_feature(aa64_rndr, cpu)) {
8956 define_arm_cp_regs(cpu, rndr_reginfo);
8957 }
8958 /* Data Cache clean instructions up to PoP */
8959 if (cpu_isar_feature(aa64_dcpop, cpu)) {
8960 define_one_arm_cp_reg(cpu, dcpop_reg);
8961
8962 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
8963 define_one_arm_cp_reg(cpu, dcpodp_reg);
8964 }
8965 }
8966
8967 /*
8968 * If full MTE is enabled, add all of the system registers.
8969 * If only "instructions available at EL0" are enabled,
8970 * then define only a RAZ/WI version of PSTATE.TCO.
8971 */
8972 if (cpu_isar_feature(aa64_mte, cpu)) {
8973 ARMCPRegInfo gmid_reginfo = {
8974 .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
8975 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
8976 .access = PL1_R, .accessfn = access_aa64_tid5,
8977 .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize,
8978 };
8979 define_one_arm_cp_reg(cpu, &gmid_reginfo);
8980 define_arm_cp_regs(cpu, mte_reginfo);
8981 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8982 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
8983 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
8984 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
8985 }
8986
8987 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
8988 define_arm_cp_regs(cpu, scxtnum_reginfo);
8989 }
8990
8991 if (cpu_isar_feature(aa64_fgt, cpu)) {
8992 define_arm_cp_regs(cpu, fgt_reginfo);
8993 }
8994
8995 if (cpu_isar_feature(aa64_rme, cpu)) {
8996 define_arm_cp_regs(cpu, rme_reginfo);
8997 if (cpu_isar_feature(aa64_mte, cpu)) {
8998 define_arm_cp_regs(cpu, rme_mte_reginfo);
8999 }
9000 }
9001
9002 if (cpu_isar_feature(aa64_nv2, cpu)) {
9003 define_arm_cp_regs(cpu, nv2_reginfo);
9004 }
9005
9006 if (cpu_isar_feature(aa64_nmi, cpu)) {
9007 define_arm_cp_regs(cpu, nmi_reginfo);
9008 }
9009 #endif
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 target_ulong 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
mve_no_pred(CPUARMState * env)11420 static bool mve_no_pred(CPUARMState *env)
11421 {
11422 /*
11423 * Return true if there is definitely no predication of MVE
11424 * instructions by VPR or LTPSIZE. (Returning false even if there
11425 * isn't any predication is OK; generated code will just be
11426 * a little worse.)
11427 * If the CPU does not implement MVE then this TB flag is always 0.
11428 *
11429 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11430 * logic in gen_update_fp_context() needs to be updated to match.
11431 *
11432 * We do not include the effect of the ECI bits here -- they are
11433 * tracked in other TB flags. This simplifies the logic for
11434 * "when did we emit code that changes the MVE_NO_PRED TB flag
11435 * and thus need to end the TB?".
11436 */
11437 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11438 return false;
11439 }
11440 if (env->v7m.vpr) {
11441 return false;
11442 }
11443 if (env->v7m.ltpsize < 4) {
11444 return false;
11445 }
11446 return true;
11447 }
11448
cpu_get_tb_cpu_state(CPUARMState * env,vaddr * pc,uint64_t * cs_base,uint32_t * pflags)11449 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc,
11450 uint64_t *cs_base, uint32_t *pflags)
11451 {
11452 CPUARMTBFlags flags;
11453
11454 assert_hflags_rebuild_correctly(env);
11455 flags = env->hflags;
11456
11457 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
11458 *pc = env->pc;
11459 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11460 DP_TBFLAG_A64(flags, BTYPE, env->btype);
11461 }
11462 } else {
11463 *pc = env->regs[15];
11464
11465 if (arm_feature(env, ARM_FEATURE_M)) {
11466 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11467 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11468 != env->v7m.secure) {
11469 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
11470 }
11471
11472 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11473 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11474 (env->v7m.secure &&
11475 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11476 /*
11477 * ASPEN is set, but FPCA/SFPA indicate that there is no
11478 * active FP context; we must create a new FP context before
11479 * executing any FP insn.
11480 */
11481 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
11482 }
11483
11484 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11485 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
11486 DP_TBFLAG_M32(flags, LSPACT, 1);
11487 }
11488
11489 if (mve_no_pred(env)) {
11490 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11491 }
11492 } else {
11493 /*
11494 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11495 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11496 */
11497 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
11498 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
11499 } else {
11500 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11501 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
11502 }
11503 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
11504 DP_TBFLAG_A32(flags, VFPEN, 1);
11505 }
11506 }
11507
11508 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11509 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
11510 }
11511
11512 /*
11513 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
11514 * states defined in the ARM ARM for software singlestep:
11515 * SS_ACTIVE PSTATE.SS State
11516 * 0 x Inactive (the TB flag for SS is always 0)
11517 * 1 0 Active-pending
11518 * 1 1 Active-not-pending
11519 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
11520 */
11521 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11522 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
11523 }
11524
11525 *pflags = flags.flags;
11526 *cs_base = flags.flags2;
11527 }
11528
11529 #ifdef TARGET_AARCH64
11530 /*
11531 * The manual says that when SVE is enabled and VQ is widened the
11532 * implementation is allowed to zero the previously inaccessible
11533 * portion of the registers. The corollary to that is that when
11534 * SVE is enabled and VQ is narrowed we are also allowed to zero
11535 * the now inaccessible portion of the registers.
11536 *
11537 * The intent of this is that no predicate bit beyond VQ is ever set.
11538 * Which means that some operations on predicate registers themselves
11539 * may operate on full uint64_t or even unrolled across the maximum
11540 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
11541 * may well be cheaper than conditionals to restrict the operation
11542 * to the relevant portion of a uint16_t[16].
11543 */
aarch64_sve_narrow_vq(CPUARMState * env,unsigned vq)11544 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11545 {
11546 int i, j;
11547 uint64_t pmask;
11548
11549 assert(vq >= 1 && vq <= ARM_MAX_VQ);
11550 assert(vq <= env_archcpu(env)->sve_max_vq);
11551
11552 /* Zap the high bits of the zregs. */
11553 for (i = 0; i < 32; i++) {
11554 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11555 }
11556
11557 /* Zap the high bits of the pregs and ffr. */
11558 pmask = 0;
11559 if (vq & 3) {
11560 pmask = ~(-1ULL << (16 * (vq & 3)));
11561 }
11562 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11563 for (i = 0; i < 17; ++i) {
11564 env->vfp.pregs[i].p[j] &= pmask;
11565 }
11566 pmask = 0;
11567 }
11568 }
11569
sve_vqm1_for_el_sm_ena(CPUARMState * env,int el,bool sm)11570 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
11571 {
11572 int exc_el;
11573
11574 if (sm) {
11575 exc_el = sme_exception_el(env, el);
11576 } else {
11577 exc_el = sve_exception_el(env, el);
11578 }
11579 if (exc_el) {
11580 return 0; /* disabled */
11581 }
11582 return sve_vqm1_for_el_sm(env, el, sm);
11583 }
11584
11585 /*
11586 * Notice a change in SVE vector size when changing EL.
11587 */
aarch64_sve_change_el(CPUARMState * env,int old_el,int new_el,bool el0_a64)11588 void aarch64_sve_change_el(CPUARMState *env, int old_el,
11589 int new_el, bool el0_a64)
11590 {
11591 ARMCPU *cpu = env_archcpu(env);
11592 int old_len, new_len;
11593 bool old_a64, new_a64, sm;
11594
11595 /* Nothing to do if no SVE. */
11596 if (!cpu_isar_feature(aa64_sve, cpu)) {
11597 return;
11598 }
11599
11600 /* Nothing to do if FP is disabled in either EL. */
11601 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
11602 return;
11603 }
11604
11605 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
11606 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
11607
11608 /*
11609 * Both AArch64.TakeException and AArch64.ExceptionReturn
11610 * invoke ResetSVEState when taking an exception from, or
11611 * returning to, AArch32 state when PSTATE.SM is enabled.
11612 */
11613 sm = FIELD_EX64(env->svcr, SVCR, SM);
11614 if (old_a64 != new_a64 && sm) {
11615 arm_reset_sve_state(env);
11616 return;
11617 }
11618
11619 /*
11620 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
11621 * at ELx, or not available because the EL is in AArch32 state, then
11622 * for all purposes other than a direct read, the ZCR_ELx.LEN field
11623 * has an effective value of 0".
11624 *
11625 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
11626 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
11627 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
11628 * we already have the correct register contents when encountering the
11629 * vq0->vq0 transition between EL0->EL1.
11630 */
11631 old_len = new_len = 0;
11632 if (old_a64) {
11633 old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
11634 }
11635 if (new_a64) {
11636 new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
11637 }
11638
11639 /* When changing vector length, clear inaccessible state. */
11640 if (new_len < old_len) {
11641 aarch64_sve_narrow_vq(env, new_len + 1);
11642 }
11643 }
11644 #endif
11645
11646 #ifndef CONFIG_USER_ONLY
arm_security_space(CPUARMState * env)11647 ARMSecuritySpace arm_security_space(CPUARMState *env)
11648 {
11649 if (arm_feature(env, ARM_FEATURE_M)) {
11650 return arm_secure_to_space(env->v7m.secure);
11651 }
11652
11653 /*
11654 * If EL3 is not supported then the secure state is implementation
11655 * defined, in which case QEMU defaults to non-secure.
11656 */
11657 if (!arm_feature(env, ARM_FEATURE_EL3)) {
11658 return ARMSS_NonSecure;
11659 }
11660
11661 /* Check for AArch64 EL3 or AArch32 Mon. */
11662 if (is_a64(env)) {
11663 if (extract32(env->pstate, 2, 2) == 3) {
11664 if (cpu_isar_feature(aa64_rme, env_archcpu(env))) {
11665 return ARMSS_Root;
11666 } else {
11667 return ARMSS_Secure;
11668 }
11669 }
11670 } else {
11671 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
11672 return ARMSS_Secure;
11673 }
11674 }
11675
11676 return arm_security_space_below_el3(env);
11677 }
11678
arm_security_space_below_el3(CPUARMState * env)11679 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env)
11680 {
11681 assert(!arm_feature(env, ARM_FEATURE_M));
11682
11683 /*
11684 * If EL3 is not supported then the secure state is implementation
11685 * defined, in which case QEMU defaults to non-secure.
11686 */
11687 if (!arm_feature(env, ARM_FEATURE_EL3)) {
11688 return ARMSS_NonSecure;
11689 }
11690
11691 /*
11692 * Note NSE cannot be set without RME, and NSE & !NS is Reserved.
11693 * Ignoring NSE when !NS retains consistency without having to
11694 * modify other predicates.
11695 */
11696 if (!(env->cp15.scr_el3 & SCR_NS)) {
11697 return ARMSS_Secure;
11698 } else if (env->cp15.scr_el3 & SCR_NSE) {
11699 return ARMSS_Realm;
11700 } else {
11701 return ARMSS_NonSecure;
11702 }
11703 }
11704 #endif /* !CONFIG_USER_ONLY */
11705