]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/helper.c
target/arm: Mark up sysregs for HFGITR bits 0..11
[mirror_qemu.git] / target / arm / helper.c
CommitLineData
ed3baad1
PMD
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 */
db725815 8
74c21bd0 9#include "qemu/osdep.h"
cd617484 10#include "qemu/log.h"
194cbc49 11#include "trace.h"
b5ff1b31 12#include "cpu.h"
ccd38087 13#include "internals.h"
2ef6175a 14#include "exec/helper-proto.h"
db725815 15#include "qemu/main-loop.h"
b8012ecf 16#include "qemu/timer.h"
1de7afc9 17#include "qemu/bitops.h"
eb0ecd5a 18#include "qemu/crc32c.h"
0442428a 19#include "qemu/qemu-print.h"
63c91552 20#include "exec/exec-all.h"
eb0ecd5a 21#include <zlib.h> /* For crc32 */
64552b6b 22#include "hw/irq.h"
740b1759 23#include "sysemu/cpu-timers.h"
f3a9b694 24#include "sysemu/kvm.h"
7f7b4e7a 25#include "qapi/qapi-commands-machine-target.h"
de390645
RH
26#include "qapi/error.h"
27#include "qemu/guest-random.h"
91f78c58 28#ifdef CONFIG_TCG
6b5fe137 29#include "semihosting/common-semi.h"
91f78c58 30#endif
cf7c6d10 31#include "cpregs.h"
0b03bdfc 32
352c98e5
LV
33#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
34
affdb64d
PM
35static void switch_mode(CPUARMState *env, int mode);
36
c4241c7d 37static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 38{
375421cc 39 assert(ri->fieldoffset);
67ed771d 40 if (cpreg_field_is_64bit(ri)) {
c4241c7d 41 return CPREG_FIELD64(env, ri);
22d9e1a9 42 } else {
c4241c7d 43 return CPREG_FIELD32(env, ri);
22d9e1a9 44 }
d4e6df63
PM
45}
46
f43ee493 47void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
d4e6df63 48{
375421cc 49 assert(ri->fieldoffset);
67ed771d 50 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
51 CPREG_FIELD64(env, ri) = value;
52 } else {
53 CPREG_FIELD32(env, ri) = value;
54 }
d4e6df63
PM
55}
56
11f136ee
FA
57static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
58{
59 return (char *)env + ri->fieldoffset;
60}
61
49a66191 62uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 63{
59a1c327 64 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 65 if (ri->type & ARM_CP_CONST) {
59a1c327 66 return ri->resetvalue;
721fae12 67 } else if (ri->raw_readfn) {
59a1c327 68 return ri->raw_readfn(env, ri);
721fae12 69 } else if (ri->readfn) {
59a1c327 70 return ri->readfn(env, ri);
721fae12 71 } else {
59a1c327 72 return raw_read(env, ri);
721fae12 73 }
721fae12
PM
74}
75
59a1c327 76static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 77 uint64_t v)
721fae12 78{
9b37a28c
FR
79 /*
80 * Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
81 * Note that constant registers are treated as write-ignored; the
82 * caller should check for success by whether a readback gives the
83 * value written.
84 */
85 if (ri->type & ARM_CP_CONST) {
59a1c327 86 return;
721fae12 87 } else if (ri->raw_writefn) {
c4241c7d 88 ri->raw_writefn(env, ri, v);
721fae12 89 } else if (ri->writefn) {
c4241c7d 90 ri->writefn(env, ri, v);
721fae12 91 } else {
afb2530f 92 raw_write(env, ri, v);
721fae12 93 }
721fae12
PM
94}
95
375421cc
PM
96static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
97{
9b37a28c
FR
98 /*
99 * Return true if the regdef would cause an assertion if you called
375421cc
PM
100 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
101 * program bug for it not to have the NO_RAW flag).
102 * NB that returning false here doesn't necessarily mean that calling
103 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
104 * read/write access functions which are safe for raw use" from "has
105 * read/write access functions which have side effects but has forgotten
106 * to provide raw access functions".
107 * The tests here line up with the conditions in read/write_raw_cp_reg()
108 * and assertions in raw_read()/raw_write().
109 */
110 if ((ri->type & ARM_CP_CONST) ||
111 ri->fieldoffset ||
112 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
113 return false;
114 }
115 return true;
116}
117
b698e4ee 118bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
721fae12
PM
119{
120 /* Write the coprocessor state from cpu->env to the (index,value) list. */
121 int i;
122 bool ok = true;
123
124 for (i = 0; i < cpu->cpreg_array_len; i++) {
125 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
126 const ARMCPRegInfo *ri;
b698e4ee 127 uint64_t newval;
59a1c327 128
60322b39 129 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
130 if (!ri) {
131 ok = false;
132 continue;
133 }
7a0e58fa 134 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
135 continue;
136 }
b698e4ee
PM
137
138 newval = read_raw_cp_reg(&cpu->env, ri);
139 if (kvm_sync) {
140 /*
141 * Only sync if the previous list->cpustate sync succeeded.
142 * Rather than tracking the success/failure state for every
143 * item in the list, we just recheck "does the raw write we must
144 * have made in write_list_to_cpustate() read back OK" here.
145 */
146 uint64_t oldval = cpu->cpreg_values[i];
147
148 if (oldval == newval) {
149 continue;
150 }
151
152 write_raw_cp_reg(&cpu->env, ri, oldval);
153 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
154 continue;
155 }
156
157 write_raw_cp_reg(&cpu->env, ri, newval);
158 }
159 cpu->cpreg_values[i] = newval;
721fae12
PM
160 }
161 return ok;
162}
163
164bool write_list_to_cpustate(ARMCPU *cpu)
165{
166 int i;
167 bool ok = true;
168
169 for (i = 0; i < cpu->cpreg_array_len; i++) {
170 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
171 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
172 const ARMCPRegInfo *ri;
173
60322b39 174 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
175 if (!ri) {
176 ok = false;
177 continue;
178 }
7a0e58fa 179 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
180 continue;
181 }
9b37a28c
FR
182 /*
183 * Write value and confirm it reads back as written
721fae12
PM
184 * (to catch read-only registers and partially read-only
185 * registers where the incoming migration value doesn't match)
186 */
59a1c327
PM
187 write_raw_cp_reg(&cpu->env, ri, v);
188 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
189 ok = false;
190 }
191 }
192 return ok;
193}
194
195static void add_cpreg_to_list(gpointer key, gpointer opaque)
196{
197 ARMCPU *cpu = opaque;
5860362d
RH
198 uint32_t regidx = (uintptr_t)key;
199 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 200
04215eb1 201 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
721fae12
PM
202 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
203 /* The value array need not be initialized at this point */
204 cpu->cpreg_array_len++;
205 }
206}
207
208static void count_cpreg(gpointer key, gpointer opaque)
209{
210 ARMCPU *cpu = opaque;
721fae12
PM
211 const ARMCPRegInfo *ri;
212
5860362d 213 ri = g_hash_table_lookup(cpu->cp_regs, key);
721fae12 214
04215eb1 215 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
721fae12
PM
216 cpu->cpreg_array_len++;
217 }
218}
219
220static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
221{
5860362d
RH
222 uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
223 uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
721fae12 224
cbf239b7
AR
225 if (aidx > bidx) {
226 return 1;
227 }
228 if (aidx < bidx) {
229 return -1;
230 }
231 return 0;
721fae12
PM
232}
233
234void init_cpreg_list(ARMCPU *cpu)
235{
9b37a28c
FR
236 /*
237 * Initialise the cpreg_tuples[] array based on the cp_regs hash.
721fae12
PM
238 * Note that we require cpreg_tuples[] to be sorted by key ID.
239 */
57b6d95e 240 GList *keys;
721fae12
PM
241 int arraylen;
242
57b6d95e 243 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
244 keys = g_list_sort(keys, cpreg_key_compare);
245
246 cpu->cpreg_array_len = 0;
247
248 g_list_foreach(keys, count_cpreg, cpu);
249
250 arraylen = cpu->cpreg_array_len;
251 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
252 cpu->cpreg_values = g_new(uint64_t, arraylen);
253 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
254 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
255 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
256 cpu->cpreg_array_len = 0;
257
258 g_list_foreach(keys, add_cpreg_to_list, cpu);
259
260 assert(cpu->cpreg_array_len == arraylen);
261
262 g_list_free(keys);
263}
264
68e9c2fe 265/*
93dd1e61 266 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
68e9c2fe
EI
267 */
268static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
269 const ARMCPRegInfo *ri,
270 bool isread)
68e9c2fe 271{
93dd1e61
EI
272 if (!is_a64(env) && arm_current_el(env) == 3 &&
273 arm_is_secure_below_el3(env)) {
68e9c2fe
EI
274 return CP_ACCESS_TRAP_UNCATEGORIZED;
275 }
276 return CP_ACCESS_OK;
277}
278
9b37a28c
FR
279/*
280 * Some secure-only AArch32 registers trap to EL3 if used from
5513c3ab
PM
281 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
282 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
283 * We assume that the .access field is set to PL1_RW.
284 */
285static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
286 const ARMCPRegInfo *ri,
287 bool isread)
5513c3ab
PM
288{
289 if (arm_current_el(env) == 3) {
290 return CP_ACCESS_OK;
291 }
292 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
293 if (env->cp15.scr_el3 & SCR_EEL2) {
294 return CP_ACCESS_TRAP_EL2;
295 }
5513c3ab
PM
296 return CP_ACCESS_TRAP_EL3;
297 }
298 /* This will be EL1 NS and EL2 NS, which just UNDEF */
299 return CP_ACCESS_TRAP_UNCATEGORIZED;
300}
301
9b37a28c
FR
302/*
303 * Check for traps to performance monitor registers, which are controlled
1fce1ba9
PM
304 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
305 */
306static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
307 bool isread)
308{
309 int el = arm_current_el(env);
59dd089c 310 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1fce1ba9 311
59dd089c 312 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1fce1ba9
PM
313 return CP_ACCESS_TRAP_EL2;
314 }
315 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
316 return CP_ACCESS_TRAP_EL3;
317 }
318 return CP_ACCESS_OK;
319}
320
84929218
RH
321/* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
322static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
323 bool isread)
324{
325 if (arm_current_el(env) == 1) {
326 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
327 if (arm_hcr_el2_eff(env) & trap) {
328 return CP_ACCESS_TRAP_EL2;
329 }
330 }
331 return CP_ACCESS_OK;
332}
333
1803d271
RH
334/* Check for traps from EL1 due to HCR_EL2.TSW. */
335static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
336 bool isread)
337{
338 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
339 return CP_ACCESS_TRAP_EL2;
340 }
341 return CP_ACCESS_OK;
342}
343
99602377
RH
344/* Check for traps from EL1 due to HCR_EL2.TACR. */
345static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
346 bool isread)
347{
348 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
349 return CP_ACCESS_TRAP_EL2;
350 }
351 return CP_ACCESS_OK;
352}
353
30881b73
RH
354/* Check for traps from EL1 due to HCR_EL2.TTLB. */
355static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
356 bool isread)
357{
358 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
359 return CP_ACCESS_TRAP_EL2;
360 }
361 return CP_ACCESS_OK;
362}
363
0f66d223
PM
364/* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBIS. */
365static CPAccessResult access_ttlbis(CPUARMState *env, const ARMCPRegInfo *ri,
366 bool isread)
367{
368 if (arm_current_el(env) == 1 &&
369 (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBIS))) {
370 return CP_ACCESS_TRAP_EL2;
371 }
372 return CP_ACCESS_OK;
373}
374
fe3ca86c
PM
375#ifdef TARGET_AARCH64
376/* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBOS. */
377static CPAccessResult access_ttlbos(CPUARMState *env, const ARMCPRegInfo *ri,
378 bool isread)
379{
380 if (arm_current_el(env) == 1 &&
381 (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBOS))) {
382 return CP_ACCESS_TRAP_EL2;
383 }
384 return CP_ACCESS_OK;
385}
386#endif
387
c4241c7d 388static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 389{
2fc0cc0e 390 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 391
8d5c773e 392 raw_write(env, ri, value);
d10eb08f 393 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
394}
395
c4241c7d 396static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 397{
2fc0cc0e 398 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 399
8d5c773e 400 if (raw_read(env, ri) != value) {
9b37a28c
FR
401 /*
402 * Unlike real hardware the qemu TLB uses virtual addresses,
08de207b
PM
403 * not modified virtual addresses, so this causes a TLB flush.
404 */
d10eb08f 405 tlb_flush(CPU(cpu));
8d5c773e 406 raw_write(env, ri, value);
08de207b 407 }
08de207b 408}
c4241c7d
PM
409
410static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
411 uint64_t value)
08de207b 412{
2fc0cc0e 413 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 414
452a0955 415 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 416 && !extended_addresses_enabled(env)) {
9b37a28c
FR
417 /*
418 * For VMSA (when not using the LPAE long descriptor page table
08de207b
PM
419 * format) this register includes the ASID, so do a TLB flush.
420 * For PMSA it is purely a process ID and no action is needed.
421 */
d10eb08f 422 tlb_flush(CPU(cpu));
08de207b 423 }
8d5c773e 424 raw_write(env, ri, value);
08de207b
PM
425}
426
575a94af
RH
427static int alle1_tlbmask(CPUARMState *env)
428{
429 /*
430 * Note that the 'ALL' scope must invalidate both stage 1 and
431 * stage 2 translations, whereas most other scopes only invalidate
432 * stage 1 translations.
433 */
434 return (ARMMMUIdxBit_E10_1 |
435 ARMMMUIdxBit_E10_1_PAN |
436 ARMMMUIdxBit_E10_0 |
437 ARMMMUIdxBit_Stage2 |
438 ARMMMUIdxBit_Stage2_S);
439}
440
441
b4ab8ce9
PM
442/* IS variants of TLB operations must affect all cores */
443static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
444 uint64_t value)
445{
29a0af61 446 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
447
448 tlb_flush_all_cpus_synced(cs);
449}
450
451static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
452 uint64_t value)
453{
29a0af61 454 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
455
456 tlb_flush_all_cpus_synced(cs);
457}
458
459static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
460 uint64_t value)
461{
29a0af61 462 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
463
464 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
465}
466
467static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
468 uint64_t value)
469{
29a0af61 470 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
471
472 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
473}
474
475/*
476 * Non-IS variants of TLB operations are upgraded to
373e7ffd 477 * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
b4ab8ce9
PM
478 * force broadcast of these operations.
479 */
480static bool tlb_force_broadcast(CPUARMState *env)
481{
373e7ffd 482 return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
b4ab8ce9
PM
483}
484
c4241c7d
PM
485static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
486 uint64_t value)
d929823f
PM
487{
488 /* Invalidate all (TLBIALL) */
527db2be 489 CPUState *cs = env_cpu(env);
00c8cb0a 490
b4ab8ce9 491 if (tlb_force_broadcast(env)) {
527db2be
RH
492 tlb_flush_all_cpus_synced(cs);
493 } else {
494 tlb_flush(cs);
b4ab8ce9 495 }
d929823f
PM
496}
497
c4241c7d
PM
498static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
499 uint64_t value)
d929823f
PM
500{
501 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
527db2be 502 CPUState *cs = env_cpu(env);
31b030d4 503
527db2be 504 value &= TARGET_PAGE_MASK;
b4ab8ce9 505 if (tlb_force_broadcast(env)) {
527db2be
RH
506 tlb_flush_page_all_cpus_synced(cs, value);
507 } else {
508 tlb_flush_page(cs, value);
b4ab8ce9 509 }
d929823f
PM
510}
511
c4241c7d
PM
512static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
513 uint64_t value)
d929823f
PM
514{
515 /* Invalidate by ASID (TLBIASID) */
527db2be 516 CPUState *cs = env_cpu(env);
00c8cb0a 517
b4ab8ce9 518 if (tlb_force_broadcast(env)) {
527db2be
RH
519 tlb_flush_all_cpus_synced(cs);
520 } else {
521 tlb_flush(cs);
b4ab8ce9 522 }
d929823f
PM
523}
524
c4241c7d
PM
525static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
526 uint64_t value)
d929823f
PM
527{
528 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
527db2be 529 CPUState *cs = env_cpu(env);
31b030d4 530
527db2be 531 value &= TARGET_PAGE_MASK;
b4ab8ce9 532 if (tlb_force_broadcast(env)) {
527db2be
RH
533 tlb_flush_page_all_cpus_synced(cs, value);
534 } else {
535 tlb_flush_page(cs, value);
b4ab8ce9 536 }
fa439fc5
PM
537}
538
541ef8c2
SS
539static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
540 uint64_t value)
541{
29a0af61 542 CPUState *cs = env_cpu(env);
541ef8c2 543
575a94af 544 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
541ef8c2
SS
545}
546
547static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
548 uint64_t value)
549{
29a0af61 550 CPUState *cs = env_cpu(env);
541ef8c2 551
575a94af 552 tlb_flush_by_mmuidx_all_cpus_synced(cs, alle1_tlbmask(env));
541ef8c2
SS
553}
554
541ef8c2
SS
555
556static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
557 uint64_t value)
558{
29a0af61 559 CPUState *cs = env_cpu(env);
541ef8c2 560
e013b741 561 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
562}
563
564static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
565 uint64_t value)
566{
29a0af61 567 CPUState *cs = env_cpu(env);
541ef8c2 568
e013b741 569 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
570}
571
572static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
573 uint64_t value)
574{
29a0af61 575 CPUState *cs = env_cpu(env);
541ef8c2
SS
576 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
577
e013b741 578 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
541ef8c2
SS
579}
580
581static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
582 uint64_t value)
583{
29a0af61 584 CPUState *cs = env_cpu(env);
541ef8c2
SS
585 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
586
a67cf277 587 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 588 ARMMMUIdxBit_E2);
541ef8c2
SS
589}
590
575a94af
RH
591static void tlbiipas2_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
592 uint64_t value)
593{
594 CPUState *cs = env_cpu(env);
595 uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
596
597 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
598}
599
600static void tlbiipas2is_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
601 uint64_t value)
602{
603 CPUState *cs = env_cpu(env);
604 uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
605
606 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, ARMMMUIdxBit_Stage2);
607}
608
e9aa6c21 609static const ARMCPRegInfo cp_reginfo[] = {
9b37a28c
FR
610 /*
611 * Define the secure and non-secure FCSE identifier CP registers
54bf36ed
FA
612 * separately because there is no secure bank in V8 (no _EL3). This allows
613 * the secure register to be properly reset and migrated. There is also no
614 * v8 EL1 version of the register so the non-secure instance stands alone.
615 */
9c513e78 616 { .name = "FCSEIDR",
54bf36ed
FA
617 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
618 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
619 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
620 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9c513e78 621 { .name = "FCSEIDR_S",
54bf36ed
FA
622 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
623 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
624 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 625 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9b37a28c
FR
626 /*
627 * Define the secure and non-secure context identifier CP registers
54bf36ed
FA
628 * separately because there is no secure bank in V8 (no _EL3). This allows
629 * the secure register to be properly reset and migrated. In the
630 * non-secure case, the 32-bit register will have reset and migration
631 * disabled during registration as it is handled by the 64-bit instance.
632 */
633 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 634 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218 635 .access = PL1_RW, .accessfn = access_tvm_trvm,
158c276c 636 .fgt = FGT_CONTEXTIDR_EL1,
84929218 637 .secure = ARM_CP_SECSTATE_NS,
54bf36ed
FA
638 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
639 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9c513e78 640 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
54bf36ed 641 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
642 .access = PL1_RW, .accessfn = access_tvm_trvm,
643 .secure = ARM_CP_SECSTATE_S,
54bf36ed 644 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 645 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
646};
647
648static const ARMCPRegInfo not_v8_cp_reginfo[] = {
9b37a28c
FR
649 /*
650 * NB: Some of these registers exist in v8 but with more precise
9449fdf6
PM
651 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
652 */
653 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
654 { .name = "DACR",
655 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
84929218 656 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
657 .writefn = dacr_write, .raw_writefn = raw_write,
658 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
659 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
9b37a28c
FR
660 /*
661 * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
a903c449 662 * For v6 and v5, these mappings are overly broad.
4fdd17dd 663 */
a903c449
EI
664 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
665 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
666 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
667 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
668 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
669 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
670 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 671 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
672 /* Cache maintenance ops; some of this space may be overridden later. */
673 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
674 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
675 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
676};
677
7d57f408 678static const ARMCPRegInfo not_v6_cp_reginfo[] = {
9b37a28c
FR
679 /*
680 * Not all pre-v6 cores implemented this WFI, so this is slightly
7d57f408
PM
681 * over-broad.
682 */
683 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
684 .access = PL1_W, .type = ARM_CP_WFI },
7d57f408
PM
685};
686
687static const ARMCPRegInfo not_v7_cp_reginfo[] = {
9b37a28c
FR
688 /*
689 * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
7d57f408
PM
690 * is UNPREDICTABLE; we choose to NOP as most implementations do).
691 */
692 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
693 .access = PL1_W, .type = ARM_CP_WFI },
9b37a28c
FR
694 /*
695 * L1 cache lockdown. Not architectural in v6 and earlier but in practice
34f90529
PM
696 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
697 * OMAPCP will override this space.
698 */
699 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
700 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
701 .resetvalue = 0 },
702 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
703 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
704 .resetvalue = 0 },
776d4e5c
PM
705 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
706 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 707 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 708 .resetvalue = 0 },
9b37a28c
FR
709 /*
710 * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
50300698
PM
711 * implementing it as RAZ means the "debug architecture version" bits
712 * will read as a reserved value, which should cause Linux to not try
713 * to use the debug hardware.
714 */
715 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
716 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9b37a28c
FR
717 /*
718 * MMU TLB control. Note that the wildcarding means we cover not just
995939a6
PM
719 * the unified TLB ops but also the dside/iside/inner-shareable variants.
720 */
721 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
722 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 723 .type = ARM_CP_NO_RAW },
995939a6
PM
724 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
725 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 726 .type = ARM_CP_NO_RAW },
995939a6
PM
727 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
728 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 729 .type = ARM_CP_NO_RAW },
995939a6
PM
730 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
731 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 732 .type = ARM_CP_NO_RAW },
a903c449
EI
733 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
734 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
735 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
736 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
737};
738
c4241c7d
PM
739static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
740 uint64_t value)
2771db27 741{
f0aff255
FA
742 uint32_t mask = 0;
743
744 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
745 if (!arm_feature(env, ARM_FEATURE_V8)) {
9b37a28c
FR
746 /*
747 * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
f0aff255
FA
748 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
749 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
750 */
7fbc6a40 751 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
f0aff255 752 /* VFP coprocessor: cp10 & cp11 [23:20] */
fab8ad39
RH
753 mask |= R_CPACR_ASEDIS_MASK |
754 R_CPACR_D32DIS_MASK |
755 R_CPACR_CP11_MASK |
756 R_CPACR_CP10_MASK;
f0aff255
FA
757
758 if (!arm_feature(env, ARM_FEATURE_NEON)) {
759 /* ASEDIS [31] bit is RAO/WI */
fab8ad39 760 value |= R_CPACR_ASEDIS_MASK;
f0aff255
FA
761 }
762
9b37a28c
FR
763 /*
764 * VFPv3 and upwards with NEON implement 32 double precision
f0aff255
FA
765 * registers (D0-D31).
766 */
a6627f5f 767 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
f0aff255 768 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
fab8ad39 769 value |= R_CPACR_D32DIS_MASK;
f0aff255
FA
770 }
771 }
772 value &= mask;
2771db27 773 }
fc1120a7
PM
774
775 /*
776 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
777 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
778 */
779 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
780 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39
RH
781 mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
782 value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
fc1120a7
PM
783 }
784
7ebd5f2e 785 env->cp15.cpacr_el1 = value;
2771db27
PM
786}
787
fc1120a7
PM
788static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
789{
790 /*
791 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
792 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
793 */
794 uint64_t value = env->cp15.cpacr_el1;
795
796 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
797 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39 798 value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
fc1120a7
PM
799 }
800 return value;
801}
802
803
5deac39c
PM
804static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
805{
9b37a28c
FR
806 /*
807 * Call cpacr_write() so that we reset with the correct RAO bits set
5deac39c
PM
808 * for our CPU features.
809 */
810 cpacr_write(env, ri, 0);
811}
812
3f208fd7
PM
813static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
814 bool isread)
c6f19164
GB
815{
816 if (arm_feature(env, ARM_FEATURE_V8)) {
817 /* Check if CPACR accesses are to be trapped to EL2 */
e6ef0169 818 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
fab8ad39 819 FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
c6f19164
GB
820 return CP_ACCESS_TRAP_EL2;
821 /* Check if CPACR accesses are to be trapped to EL3 */
822 } else if (arm_current_el(env) < 3 &&
fab8ad39 823 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
c6f19164
GB
824 return CP_ACCESS_TRAP_EL3;
825 }
826 }
827
828 return CP_ACCESS_OK;
829}
830
3f208fd7
PM
831static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
832 bool isread)
c6f19164
GB
833{
834 /* Check if CPTR accesses are set to trap to EL3 */
fab8ad39
RH
835 if (arm_current_el(env) == 2 &&
836 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
c6f19164
GB
837 return CP_ACCESS_TRAP_EL3;
838 }
839
840 return CP_ACCESS_OK;
841}
842
7d57f408
PM
843static const ARMCPRegInfo v6_cp_reginfo[] = {
844 /* prefetch by MVA in v6, NOP in v7 */
845 { .name = "MVA_prefetch",
846 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
847 .access = PL1_W, .type = ARM_CP_NOP },
9b37a28c
FR
848 /*
849 * We need to break the TB after ISB to execute self-modifying code
6df99dec
SS
850 * correctly and also to take any pending interrupts immediately.
851 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
852 */
7d57f408 853 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 854 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 855 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 856 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 857 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 858 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 859 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218 860 .access = PL1_RW, .accessfn = access_tvm_trvm,
b848ce2b
FA
861 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
862 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31 863 .resetvalue = 0, },
9b37a28c
FR
864 /*
865 * Watchpoint Fault Address Register : should actually only be present
06d76f31
PM
866 * for 1136, 1176, 11MPCore.
867 */
868 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
869 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 870 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 871 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
b19ed03c 872 .fgt = FGT_CPACR_EL1,
7ebd5f2e 873 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
fc1120a7 874 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
7d57f408
PM
875};
876
57a4a11b
AL
877typedef struct pm_event {
878 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
879 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
880 bool (*supported)(CPUARMState *);
881 /*
882 * Retrieve the current count of the underlying event. The programmed
883 * counters hold a difference from the return value from this function
884 */
885 uint64_t (*get_count)(CPUARMState *);
4e7beb0c
AL
886 /*
887 * Return how many nanoseconds it will take (at a minimum) for count events
888 * to occur. A negative value indicates the counter will never overflow, or
889 * that the counter has otherwise arranged for the overflow bit to be set
890 * and the PMU interrupt to be raised on overflow.
891 */
892 int64_t (*ns_per_count)(uint64_t);
57a4a11b
AL
893} pm_event;
894
b2e23725
AL
895static bool event_always_supported(CPUARMState *env)
896{
897 return true;
898}
899
0d4bfd7d
AL
900static uint64_t swinc_get_count(CPUARMState *env)
901{
902 /*
903 * SW_INCR events are written directly to the pmevcntr's by writes to
904 * PMSWINC, so there is no underlying count maintained by the PMU itself
905 */
906 return 0;
907}
908
4e7beb0c
AL
909static int64_t swinc_ns_per(uint64_t ignored)
910{
911 return -1;
912}
913
b2e23725
AL
914/*
915 * Return the underlying cycle count for the PMU cycle counters. If we're in
916 * usermode, simply return 0.
917 */
918static uint64_t cycles_get_count(CPUARMState *env)
919{
920#ifndef CONFIG_USER_ONLY
921 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
922 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
923#else
924 return cpu_get_host_ticks();
925#endif
926}
927
928#ifndef CONFIG_USER_ONLY
4e7beb0c
AL
929static int64_t cycles_ns_per(uint64_t cycles)
930{
931 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
932}
933
b2e23725
AL
934static bool instructions_supported(CPUARMState *env)
935{
740b1759 936 return icount_enabled() == 1; /* Precise instruction counting */
b2e23725
AL
937}
938
939static uint64_t instructions_get_count(CPUARMState *env)
940{
8191d368 941 return (uint64_t)icount_get_raw();
b2e23725 942}
4e7beb0c
AL
943
944static int64_t instructions_ns_per(uint64_t icount)
945{
8191d368 946 return icount_to_ns((int64_t)icount);
4e7beb0c 947}
b2e23725
AL
948#endif
949
a793bcd0 950static bool pmuv3p1_events_supported(CPUARMState *env)
0727f63b
PM
951{
952 /* For events which are supported in any v8.1 PMU */
a793bcd0 953 return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
0727f63b
PM
954}
955
a793bcd0 956static bool pmuv3p4_events_supported(CPUARMState *env)
15dd1ebd
PM
957{
958 /* For events which are supported in any v8.1 PMU */
a793bcd0 959 return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
15dd1ebd
PM
960}
961
0727f63b
PM
962static uint64_t zero_event_get_count(CPUARMState *env)
963{
964 /* For events which on QEMU never fire, so their count is always zero */
965 return 0;
966}
967
968static int64_t zero_event_ns_per(uint64_t cycles)
969{
970 /* An event which never fires can never overflow */
971 return -1;
972}
973
57a4a11b 974static const pm_event pm_events[] = {
0d4bfd7d
AL
975 { .number = 0x000, /* SW_INCR */
976 .supported = event_always_supported,
977 .get_count = swinc_get_count,
4e7beb0c 978 .ns_per_count = swinc_ns_per,
0d4bfd7d 979 },
b2e23725
AL
980#ifndef CONFIG_USER_ONLY
981 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
982 .supported = instructions_supported,
983 .get_count = instructions_get_count,
4e7beb0c 984 .ns_per_count = instructions_ns_per,
b2e23725
AL
985 },
986 { .number = 0x011, /* CPU_CYCLES, Cycle */
987 .supported = event_always_supported,
988 .get_count = cycles_get_count,
4e7beb0c 989 .ns_per_count = cycles_ns_per,
0727f63b 990 },
b2e23725 991#endif
0727f63b 992 { .number = 0x023, /* STALL_FRONTEND */
a793bcd0 993 .supported = pmuv3p1_events_supported,
0727f63b
PM
994 .get_count = zero_event_get_count,
995 .ns_per_count = zero_event_ns_per,
996 },
997 { .number = 0x024, /* STALL_BACKEND */
a793bcd0 998 .supported = pmuv3p1_events_supported,
0727f63b
PM
999 .get_count = zero_event_get_count,
1000 .ns_per_count = zero_event_ns_per,
1001 },
15dd1ebd 1002 { .number = 0x03c, /* STALL */
a793bcd0 1003 .supported = pmuv3p4_events_supported,
15dd1ebd
PM
1004 .get_count = zero_event_get_count,
1005 .ns_per_count = zero_event_ns_per,
1006 },
57a4a11b
AL
1007};
1008
1009/*
1010 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1011 * events (i.e. the statistical profiling extension), this implementation
1012 * should first be updated to something sparse instead of the current
1013 * supported_event_map[] array.
1014 */
15dd1ebd 1015#define MAX_EVENT_ID 0x3c
57a4a11b
AL
1016#define UNSUPPORTED_EVENT UINT16_MAX
1017static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1018
1019/*
bf8d0969
AL
1020 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1021 * of ARM event numbers to indices in our pm_events array.
57a4a11b
AL
1022 *
1023 * Note: Events in the 0x40XX range are not currently supported.
1024 */
bf8d0969 1025void pmu_init(ARMCPU *cpu)
57a4a11b 1026{
57a4a11b
AL
1027 unsigned int i;
1028
bf8d0969
AL
1029 /*
1030 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1031 * events to them
1032 */
57a4a11b
AL
1033 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1034 supported_event_map[i] = UNSUPPORTED_EVENT;
1035 }
bf8d0969
AL
1036 cpu->pmceid0 = 0;
1037 cpu->pmceid1 = 0;
57a4a11b
AL
1038
1039 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1040 const pm_event *cnt = &pm_events[i];
1041 assert(cnt->number <= MAX_EVENT_ID);
1042 /* We do not currently support events in the 0x40xx range */
1043 assert(cnt->number <= 0x3f);
1044
bf8d0969 1045 if (cnt->supported(&cpu->env)) {
57a4a11b 1046 supported_event_map[cnt->number] = i;
67da43d6 1047 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
bf8d0969
AL
1048 if (cnt->number & 0x20) {
1049 cpu->pmceid1 |= event_mask;
1050 } else {
1051 cpu->pmceid0 |= event_mask;
1052 }
57a4a11b
AL
1053 }
1054 }
57a4a11b
AL
1055}
1056
5ecdd3e4
AL
1057/*
1058 * Check at runtime whether a PMU event is supported for the current machine
1059 */
1060static bool event_supported(uint16_t number)
1061{
1062 if (number > MAX_EVENT_ID) {
1063 return false;
1064 }
1065 return supported_event_map[number] != UNSUPPORTED_EVENT;
1066}
1067
3f208fd7
PM
1068static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1069 bool isread)
200ac0ef 1070{
9b37a28c
FR
1071 /*
1072 * Performance monitor registers user accessibility is controlled
1fce1ba9
PM
1073 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1074 * trapping to EL2 or EL3 for other accesses.
200ac0ef 1075 */
1fce1ba9 1076 int el = arm_current_el(env);
59dd089c 1077 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1fce1ba9 1078
6ecd0b6b 1079 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 1080 return CP_ACCESS_TRAP;
200ac0ef 1081 }
59dd089c 1082 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1fce1ba9
PM
1083 return CP_ACCESS_TRAP_EL2;
1084 }
1085 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1086 return CP_ACCESS_TRAP_EL3;
1087 }
1088
fcd25206 1089 return CP_ACCESS_OK;
200ac0ef
PM
1090}
1091
6ecd0b6b
AB
1092static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1093 const ARMCPRegInfo *ri,
1094 bool isread)
1095{
1096 /* ER: event counter read trap control */
1097 if (arm_feature(env, ARM_FEATURE_V8)
1098 && arm_current_el(env) == 0
1099 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1100 && isread) {
1101 return CP_ACCESS_OK;
1102 }
1103
1104 return pmreg_access(env, ri, isread);
1105}
1106
1107static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1108 const ARMCPRegInfo *ri,
1109 bool isread)
1110{
1111 /* SW: software increment write trap control */
1112 if (arm_feature(env, ARM_FEATURE_V8)
1113 && arm_current_el(env) == 0
1114 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1115 && !isread) {
1116 return CP_ACCESS_OK;
1117 }
1118
1119 return pmreg_access(env, ri, isread);
1120}
1121
6ecd0b6b
AB
1122static CPAccessResult pmreg_access_selr(CPUARMState *env,
1123 const ARMCPRegInfo *ri,
1124 bool isread)
1125{
1126 /* ER: event counter read trap control */
1127 if (arm_feature(env, ARM_FEATURE_V8)
1128 && arm_current_el(env) == 0
1129 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1130 return CP_ACCESS_OK;
1131 }
1132
1133 return pmreg_access(env, ri, isread);
1134}
1135
1136static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1137 const ARMCPRegInfo *ri,
1138 bool isread)
1139{
1140 /* CR: cycle counter read trap control */
1141 if (arm_feature(env, ARM_FEATURE_V8)
1142 && arm_current_el(env) == 0
1143 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1144 && isread) {
1145 return CP_ACCESS_OK;
1146 }
1147
1148 return pmreg_access(env, ri, isread);
1149}
1150
01765386
PM
1151/*
1152 * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1153 * We use these to decide whether we need to wrap a write to MDCR_EL2
1154 * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1155 */
47b385da
PM
1156#define MDCR_EL2_PMU_ENABLE_BITS \
1157 (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
0b42f4fa 1158#define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
01765386 1159
9b37a28c
FR
1160/*
1161 * Returns true if the counter (pass 31 for PMCCNTR) should count events using
033614c4
AL
1162 * the current EL, security state, and register configuration.
1163 */
1164static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
87124fde 1165{
033614c4
AL
1166 uint64_t filter;
1167 bool e, p, u, nsk, nsu, nsh, m;
872d2034 1168 bool enabled, prohibited = false, filtered;
033614c4
AL
1169 bool secure = arm_is_secure(env);
1170 int el = arm_current_el(env);
59dd089c
RDC
1171 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1172 uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
87124fde 1173
cbbb3041
AJ
1174 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1175 return false;
1176 }
1177
033614c4
AL
1178 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1179 (counter < hpmn || counter == 31)) {
1180 e = env->cp15.c9_pmcr & PMCRE;
1181 } else {
59dd089c 1182 e = mdcr_el2 & MDCR_HPME;
87124fde 1183 }
033614c4 1184 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
87124fde 1185
872d2034
PM
1186 /* Is event counting prohibited? */
1187 if (el == 2 && (counter < hpmn || counter == 31)) {
1188 prohibited = mdcr_el2 & MDCR_HPMD;
1189 }
1190 if (secure) {
1191 prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
033614c4
AL
1192 }
1193
0b42f4fa
PM
1194 if (counter == 31) {
1195 /*
1196 * The cycle counter defaults to running. PMCR.DP says "disable
1197 * the cycle counter when event counting is prohibited".
1198 * Some MDCR bits disable the cycle counter specifically.
1199 */
1200 prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1201 if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1202 if (secure) {
1203 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1204 }
1205 if (el == 2) {
1206 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1207 }
1208 }
033614c4
AL
1209 }
1210
5ecdd3e4
AL
1211 if (counter == 31) {
1212 filter = env->cp15.pmccfiltr_el0;
1213 } else {
1214 filter = env->cp15.c14_pmevtyper[counter];
1215 }
033614c4
AL
1216
1217 p = filter & PMXEVTYPER_P;
1218 u = filter & PMXEVTYPER_U;
1219 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1220 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1221 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1222 m = arm_el_is_aa64(env, 1) &&
1223 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1224
1225 if (el == 0) {
1226 filtered = secure ? u : u != nsu;
1227 } else if (el == 1) {
1228 filtered = secure ? p : p != nsk;
1229 } else if (el == 2) {
1230 filtered = !nsh;
1231 } else { /* EL3 */
1232 filtered = m != p;
1233 }
1234
5ecdd3e4
AL
1235 if (counter != 31) {
1236 /*
1237 * If not checking PMCCNTR, ensure the counter is setup to an event we
1238 * support
1239 */
1240 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1241 if (!event_supported(event)) {
1242 return false;
1243 }
1244 }
1245
033614c4 1246 return enabled && !prohibited && !filtered;
87124fde 1247}
033614c4 1248
f4efb4b2
AL
1249static void pmu_update_irq(CPUARMState *env)
1250{
2fc0cc0e 1251 ARMCPU *cpu = env_archcpu(env);
f4efb4b2
AL
1252 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1253 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1254}
1255
b57aa7bd
PM
1256static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1257{
1258 /*
1259 * Return true if the clock divider is enabled and the cycle counter
1260 * is supposed to tick only once every 64 clock cycles. This is
1261 * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1262 * (64-bit) cycle counter PMCR.D has no effect.
1263 */
1264 return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1265}
1266
47b385da
PM
1267static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1268{
1269 /* Return true if the specified event counter is configured to be 64 bit */
1270
1271 /* This isn't intended to be used with the cycle counter */
1272 assert(counter < 31);
1273
1274 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1275 return false;
1276 }
1277
1278 if (arm_feature(env, ARM_FEATURE_EL2)) {
1279 /*
1280 * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1281 * current security state, so we don't use arm_mdcr_el2_eff() here.
1282 */
1283 bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1284 int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1285
1286 if (hpmn != 0 && counter >= hpmn) {
1287 return hlp;
1288 }
1289 }
1290 return env->cp15.c9_pmcr & PMCRLP;
1291}
1292
5d05b9d4
AL
1293/*
1294 * Ensure c15_ccnt is the guest-visible count so that operations such as
1295 * enabling/disabling the counter or filtering, modifying the count itself,
1296 * etc. can be done logically. This is essentially a no-op if the counter is
1297 * not enabled at the time of the call.
1298 */
f2b2f53f 1299static void pmccntr_op_start(CPUARMState *env)
ec7b4ce4 1300{
b2e23725 1301 uint64_t cycles = cycles_get_count(env);
ec7b4ce4 1302
033614c4 1303 if (pmu_counter_enabled(env, 31)) {
5d05b9d4 1304 uint64_t eff_cycles = cycles;
b57aa7bd 1305 if (pmccntr_clockdiv_enabled(env)) {
5d05b9d4
AL
1306 eff_cycles /= 64;
1307 }
1308
f4efb4b2
AL
1309 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1310
1311 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1312 1ull << 63 : 1ull << 31;
1313 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
76e25d41 1314 env->cp15.c9_pmovsr |= (1ULL << 31);
f4efb4b2
AL
1315 pmu_update_irq(env);
1316 }
1317
1318 env->cp15.c15_ccnt = new_pmccntr;
ec7b4ce4 1319 }
5d05b9d4
AL
1320 env->cp15.c15_ccnt_delta = cycles;
1321}
ec7b4ce4 1322
5d05b9d4
AL
1323/*
1324 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1325 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1326 * pmccntr_op_start.
1327 */
f2b2f53f 1328static void pmccntr_op_finish(CPUARMState *env)
5d05b9d4 1329{
033614c4 1330 if (pmu_counter_enabled(env, 31)) {
4e7beb0c
AL
1331#ifndef CONFIG_USER_ONLY
1332 /* Calculate when the counter will next overflow */
1333 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1334 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1335 remaining_cycles = (uint32_t)remaining_cycles;
1336 }
1337 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1338
1339 if (overflow_in > 0) {
f1dd2506
PM
1340 int64_t overflow_at;
1341
1342 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1343 overflow_in, &overflow_at)) {
1344 ARMCPU *cpu = env_archcpu(env);
1345 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1346 }
4e7beb0c
AL
1347 }
1348#endif
5d05b9d4 1349
4e7beb0c 1350 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
b57aa7bd 1351 if (pmccntr_clockdiv_enabled(env)) {
5d05b9d4
AL
1352 prev_cycles /= 64;
1353 }
5d05b9d4 1354 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
ec7b4ce4
AF
1355 }
1356}
1357
5ecdd3e4
AL
1358static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1359{
1360
1361 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1362 uint64_t count = 0;
1363 if (event_supported(event)) {
1364 uint16_t event_idx = supported_event_map[event];
1365 count = pm_events[event_idx].get_count(env);
1366 }
1367
1368 if (pmu_counter_enabled(env, counter)) {
47b385da
PM
1369 uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1370 uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1371 1ULL << 63 : 1ULL << 31;
f4efb4b2 1372
47b385da 1373 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
f4efb4b2
AL
1374 env->cp15.c9_pmovsr |= (1 << counter);
1375 pmu_update_irq(env);
1376 }
1377 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
5ecdd3e4
AL
1378 }
1379 env->cp15.c14_pmevcntr_delta[counter] = count;
1380}
1381
1382static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1383{
1384 if (pmu_counter_enabled(env, counter)) {
4e7beb0c
AL
1385#ifndef CONFIG_USER_ONLY
1386 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1387 uint16_t event_idx = supported_event_map[event];
47b385da
PM
1388 uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1389 int64_t overflow_in;
1390
1391 if (!pmevcntr_is_64_bit(env, counter)) {
1392 delta = (uint32_t)delta;
1393 }
1394 overflow_in = pm_events[event_idx].ns_per_count(delta);
4e7beb0c
AL
1395
1396 if (overflow_in > 0) {
f1dd2506
PM
1397 int64_t overflow_at;
1398
1399 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1400 overflow_in, &overflow_at)) {
1401 ARMCPU *cpu = env_archcpu(env);
1402 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1403 }
4e7beb0c
AL
1404 }
1405#endif
1406
5ecdd3e4
AL
1407 env->cp15.c14_pmevcntr_delta[counter] -=
1408 env->cp15.c14_pmevcntr[counter];
1409 }
1410}
1411
5d05b9d4
AL
1412void pmu_op_start(CPUARMState *env)
1413{
5ecdd3e4 1414 unsigned int i;
5d05b9d4 1415 pmccntr_op_start(env);
5ecdd3e4
AL
1416 for (i = 0; i < pmu_num_counters(env); i++) {
1417 pmevcntr_op_start(env, i);
1418 }
5d05b9d4
AL
1419}
1420
1421void pmu_op_finish(CPUARMState *env)
1422{
5ecdd3e4 1423 unsigned int i;
5d05b9d4 1424 pmccntr_op_finish(env);
5ecdd3e4
AL
1425 for (i = 0; i < pmu_num_counters(env); i++) {
1426 pmevcntr_op_finish(env, i);
1427 }
5d05b9d4
AL
1428}
1429
033614c4
AL
1430void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1431{
1432 pmu_op_start(&cpu->env);
1433}
1434
1435void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1436{
1437 pmu_op_finish(&cpu->env);
1438}
1439
4e7beb0c
AL
1440void arm_pmu_timer_cb(void *opaque)
1441{
1442 ARMCPU *cpu = opaque;
1443
1444 /*
1445 * Update all the counter values based on the current underlying counts,
1446 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1447 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1448 * counter may expire.
1449 */
1450 pmu_op_start(&cpu->env);
1451 pmu_op_finish(&cpu->env);
1452}
1453
c4241c7d
PM
1454static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1455 uint64_t value)
200ac0ef 1456{
5d05b9d4 1457 pmu_op_start(env);
7c2cb42b
AF
1458
1459 if (value & PMCRC) {
1460 /* The counter has been reset */
1461 env->cp15.c15_ccnt = 0;
1462 }
1463
5ecdd3e4
AL
1464 if (value & PMCRP) {
1465 unsigned int i;
1466 for (i = 0; i < pmu_num_counters(env); i++) {
1467 env->cp15.c14_pmevcntr[i] = 0;
1468 }
1469 }
1470
9323e79f
PM
1471 env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1472 env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
7c2cb42b 1473
5d05b9d4 1474 pmu_op_finish(env);
7c2cb42b
AF
1475}
1476
0d4bfd7d
AL
1477static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1478 uint64_t value)
1479{
1480 unsigned int i;
47b385da
PM
1481 uint64_t overflow_mask, new_pmswinc;
1482
0d4bfd7d
AL
1483 for (i = 0; i < pmu_num_counters(env); i++) {
1484 /* Increment a counter's count iff: */
1485 if ((value & (1 << i)) && /* counter's bit is set */
1486 /* counter is enabled and not filtered */
1487 pmu_counter_enabled(env, i) &&
1488 /* counter is SW_INCR */
1489 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1490 pmevcntr_op_start(env, i);
f4efb4b2
AL
1491
1492 /*
1493 * Detect if this write causes an overflow since we can't predict
1494 * PMSWINC overflows like we can for other events
1495 */
47b385da
PM
1496 new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1497
1498 overflow_mask = pmevcntr_is_64_bit(env, i) ?
1499 1ULL << 63 : 1ULL << 31;
f4efb4b2 1500
47b385da 1501 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
f4efb4b2
AL
1502 env->cp15.c9_pmovsr |= (1 << i);
1503 pmu_update_irq(env);
1504 }
1505
1506 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1507
0d4bfd7d
AL
1508 pmevcntr_op_finish(env, i);
1509 }
1510 }
1511}
1512
7c2cb42b
AF
1513static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1514{
5d05b9d4
AL
1515 uint64_t ret;
1516 pmccntr_op_start(env);
1517 ret = env->cp15.c15_ccnt;
1518 pmccntr_op_finish(env);
1519 return ret;
7c2cb42b
AF
1520}
1521
6b040780
WH
1522static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1523 uint64_t value)
1524{
9b37a28c
FR
1525 /*
1526 * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
6b040780
WH
1527 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1528 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1529 * accessed.
1530 */
1531 env->cp15.c9_pmselr = value & 0x1f;
1532}
1533
7c2cb42b
AF
1534static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1535 uint64_t value)
1536{
5d05b9d4
AL
1537 pmccntr_op_start(env);
1538 env->cp15.c15_ccnt = value;
1539 pmccntr_op_finish(env);
200ac0ef 1540}
421c7ebd
PC
1541
1542static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1543 uint64_t value)
1544{
1545 uint64_t cur_val = pmccntr_read(env, NULL);
1546
1547 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1548}
1549
0614601c
AF
1550static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1551 uint64_t value)
1552{
5d05b9d4 1553 pmccntr_op_start(env);
4b8afa1f
AL
1554 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1555 pmccntr_op_finish(env);
1556}
1557
1558static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1559 uint64_t value)
1560{
1561 pmccntr_op_start(env);
1562 /* M is not accessible from AArch32 */
1563 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1564 (value & PMCCFILTR);
5d05b9d4 1565 pmccntr_op_finish(env);
0614601c
AF
1566}
1567
4b8afa1f
AL
1568static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1569{
1570 /* M is not visible in AArch32 */
1571 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1572}
1573
c4241c7d 1574static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1575 uint64_t value)
1576{
01765386 1577 pmu_op_start(env);
7ece99b1 1578 value &= pmu_counter_mask(env);
200ac0ef 1579 env->cp15.c9_pmcnten |= value;
01765386 1580 pmu_op_finish(env);
200ac0ef
PM
1581}
1582
c4241c7d
PM
1583static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1584 uint64_t value)
200ac0ef 1585{
01765386 1586 pmu_op_start(env);
7ece99b1 1587 value &= pmu_counter_mask(env);
200ac0ef 1588 env->cp15.c9_pmcnten &= ~value;
01765386 1589 pmu_op_finish(env);
200ac0ef
PM
1590}
1591
c4241c7d
PM
1592static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1593 uint64_t value)
200ac0ef 1594{
599b71e2 1595 value &= pmu_counter_mask(env);
200ac0ef 1596 env->cp15.c9_pmovsr &= ~value;
f4efb4b2 1597 pmu_update_irq(env);
200ac0ef
PM
1598}
1599
327dd510
AL
1600static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1601 uint64_t value)
1602{
1603 value &= pmu_counter_mask(env);
1604 env->cp15.c9_pmovsr |= value;
f4efb4b2 1605 pmu_update_irq(env);
327dd510
AL
1606}
1607
5ecdd3e4
AL
1608static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1609 uint64_t value, const uint8_t counter)
200ac0ef 1610{
5ecdd3e4
AL
1611 if (counter == 31) {
1612 pmccfiltr_write(env, ri, value);
1613 } else if (counter < pmu_num_counters(env)) {
1614 pmevcntr_op_start(env, counter);
1615
1616 /*
1617 * If this counter's event type is changing, store the current
1618 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1619 * pmevcntr_op_finish has the correct baseline when it converts back to
1620 * a delta.
1621 */
1622 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1623 PMXEVTYPER_EVTCOUNT;
1624 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1625 if (old_event != new_event) {
1626 uint64_t count = 0;
1627 if (event_supported(new_event)) {
1628 uint16_t event_idx = supported_event_map[new_event];
1629 count = pm_events[event_idx].get_count(env);
1630 }
1631 env->cp15.c14_pmevcntr_delta[counter] = count;
1632 }
1633
1634 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1635 pmevcntr_op_finish(env, counter);
1636 }
9b37a28c
FR
1637 /*
1638 * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
fdb86656
WH
1639 * PMSELR value is equal to or greater than the number of implemented
1640 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1641 */
5ecdd3e4
AL
1642}
1643
1644static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1645 const uint8_t counter)
1646{
1647 if (counter == 31) {
1648 return env->cp15.pmccfiltr_el0;
1649 } else if (counter < pmu_num_counters(env)) {
1650 return env->cp15.c14_pmevtyper[counter];
1651 } else {
1652 /*
1653 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1654 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1655 */
1656 return 0;
1657 }
1658}
1659
1660static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1661 uint64_t value)
1662{
1663 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1664 pmevtyper_write(env, ri, value, counter);
1665}
1666
1667static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1668 uint64_t value)
1669{
1670 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1671 env->cp15.c14_pmevtyper[counter] = value;
1672
1673 /*
1674 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1675 * pmu_op_finish calls when loading saved state for a migration. Because
1676 * we're potentially updating the type of event here, the value written to
1677 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1678 * different counter type. Therefore, we need to set this value to the
1679 * current count for the counter type we're writing so that pmu_op_finish
1680 * has the correct count for its calculation.
1681 */
1682 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1683 if (event_supported(event)) {
1684 uint16_t event_idx = supported_event_map[event];
1685 env->cp15.c14_pmevcntr_delta[counter] =
1686 pm_events[event_idx].get_count(env);
fdb86656
WH
1687 }
1688}
1689
5ecdd3e4
AL
1690static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1691{
1692 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1693 return pmevtyper_read(env, ri, counter);
1694}
1695
1696static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1697 uint64_t value)
1698{
1699 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1700}
1701
fdb86656
WH
1702static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1703{
5ecdd3e4
AL
1704 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1705}
1706
1707static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1708 uint64_t value, uint8_t counter)
1709{
47b385da
PM
1710 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1711 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1712 value &= MAKE_64BIT_MASK(0, 32);
1713 }
5ecdd3e4
AL
1714 if (counter < pmu_num_counters(env)) {
1715 pmevcntr_op_start(env, counter);
1716 env->cp15.c14_pmevcntr[counter] = value;
1717 pmevcntr_op_finish(env, counter);
1718 }
1719 /*
1720 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1721 * are CONSTRAINED UNPREDICTABLE.
fdb86656 1722 */
5ecdd3e4
AL
1723}
1724
1725static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1726 uint8_t counter)
1727{
1728 if (counter < pmu_num_counters(env)) {
1729 uint64_t ret;
1730 pmevcntr_op_start(env, counter);
1731 ret = env->cp15.c14_pmevcntr[counter];
1732 pmevcntr_op_finish(env, counter);
47b385da
PM
1733 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1734 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1735 ret &= MAKE_64BIT_MASK(0, 32);
1736 }
5ecdd3e4 1737 return ret;
fdb86656 1738 } else {
9b37a28c
FR
1739 /*
1740 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1741 * are CONSTRAINED UNPREDICTABLE.
1742 */
fdb86656
WH
1743 return 0;
1744 }
200ac0ef
PM
1745}
1746
5ecdd3e4
AL
1747static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1748 uint64_t value)
1749{
1750 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1751 pmevcntr_write(env, ri, value, counter);
1752}
1753
1754static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1755{
1756 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1757 return pmevcntr_read(env, ri, counter);
1758}
1759
1760static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1761 uint64_t value)
1762{
1763 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1764 assert(counter < pmu_num_counters(env));
1765 env->cp15.c14_pmevcntr[counter] = value;
1766 pmevcntr_write(env, ri, value, counter);
1767}
1768
1769static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1770{
1771 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1772 assert(counter < pmu_num_counters(env));
1773 return env->cp15.c14_pmevcntr[counter];
1774}
1775
1776static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1777 uint64_t value)
1778{
1779 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1780}
1781
1782static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1783{
1784 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1785}
1786
c4241c7d 1787static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1788 uint64_t value)
1789{
6ecd0b6b
AB
1790 if (arm_feature(env, ARM_FEATURE_V8)) {
1791 env->cp15.c9_pmuserenr = value & 0xf;
1792 } else {
1793 env->cp15.c9_pmuserenr = value & 1;
1794 }
200ac0ef
PM
1795}
1796
c4241c7d
PM
1797static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1798 uint64_t value)
200ac0ef
PM
1799{
1800 /* We have no event counters so only the C bit can be changed */
7ece99b1 1801 value &= pmu_counter_mask(env);
200ac0ef 1802 env->cp15.c9_pminten |= value;
f4efb4b2 1803 pmu_update_irq(env);
200ac0ef
PM
1804}
1805
c4241c7d
PM
1806static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1807 uint64_t value)
200ac0ef 1808{
7ece99b1 1809 value &= pmu_counter_mask(env);
200ac0ef 1810 env->cp15.c9_pminten &= ~value;
f4efb4b2 1811 pmu_update_irq(env);
200ac0ef
PM
1812}
1813
c4241c7d
PM
1814static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1815 uint64_t value)
8641136c 1816{
9b37a28c
FR
1817 /*
1818 * Note that even though the AArch64 view of this register has bits
a505d7fe
PM
1819 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1820 * architectural requirements for bits which are RES0 only in some
1821 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1822 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1823 */
855ea66d 1824 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1825}
1826
64e0e2de
EI
1827static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1828{
ea22747c 1829 /* Begin with base v8.0 state. */
06f2adcc 1830 uint64_t valid_mask = 0x3fff;
2fc0cc0e 1831 ARMCPU *cpu = env_archcpu(env);
d902ae75 1832 uint64_t changed;
ea22747c 1833
bfe43e3d
RH
1834 /*
1835 * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1836 * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1837 * Instead, choose the format based on the mode of EL3.
1838 */
1839 if (arm_el_is_aa64(env, 3)) {
1840 value |= SCR_FW | SCR_AW; /* RES1 */
1841 valid_mask &= ~SCR_NET; /* RES0 */
252e8c69 1842
6bcbb07a
RH
1843 if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1844 !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1845 value |= SCR_RW; /* RAO/WI */
1846 }
da3d8b13
RH
1847 if (cpu_isar_feature(aa64_ras, cpu)) {
1848 valid_mask |= SCR_TERR;
1849 }
252e8c69
RH
1850 if (cpu_isar_feature(aa64_lor, cpu)) {
1851 valid_mask |= SCR_TLOR;
1852 }
1853 if (cpu_isar_feature(aa64_pauth, cpu)) {
1854 valid_mask |= SCR_API | SCR_APK;
1855 }
926c1b97
RDC
1856 if (cpu_isar_feature(aa64_sel2, cpu)) {
1857 valid_mask |= SCR_EEL2;
1858 }
8ddb300b
RH
1859 if (cpu_isar_feature(aa64_mte, cpu)) {
1860 valid_mask |= SCR_ATA;
1861 }
7cb1e618
RH
1862 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1863 valid_mask |= SCR_ENSCXT;
1864 }
7ac61020
PM
1865 if (cpu_isar_feature(aa64_doublefault, cpu)) {
1866 valid_mask |= SCR_EASE | SCR_NMEA;
1867 }
06f2adcc
JF
1868 if (cpu_isar_feature(aa64_sme, cpu)) {
1869 valid_mask |= SCR_ENTP2;
1870 }
08899b5c
EI
1871 if (cpu_isar_feature(aa64_hcx, cpu)) {
1872 valid_mask |= SCR_HXEN;
1873 }
15126d9c
PM
1874 if (cpu_isar_feature(aa64_fgt, cpu)) {
1875 valid_mask |= SCR_FGTEN;
1876 }
ea22747c
RH
1877 } else {
1878 valid_mask &= ~(SCR_RW | SCR_ST);
da3d8b13
RH
1879 if (cpu_isar_feature(aa32_ras, cpu)) {
1880 valid_mask |= SCR_TERR;
1881 }
ea22747c 1882 }
64e0e2de
EI
1883
1884 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1885 valid_mask &= ~SCR_HCE;
1886
9b37a28c
FR
1887 /*
1888 * On ARMv7, SMD (or SCD as it is called in v7) is only
64e0e2de
EI
1889 * supported if EL2 exists. The bit is UNK/SBZP when
1890 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1891 * when EL2 is unavailable.
4eb27640 1892 * On ARMv8, this bit is always available.
64e0e2de 1893 */
4eb27640
GB
1894 if (arm_feature(env, ARM_FEATURE_V7) &&
1895 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1896 valid_mask &= ~SCR_SMD;
1897 }
1898 }
1899
1900 /* Clear all-context RES0 bits. */
1901 value &= valid_mask;
d902ae75
RH
1902 changed = env->cp15.scr_el3 ^ value;
1903 env->cp15.scr_el3 = value;
1904
1905 /*
1906 * If SCR_EL3.NS changes, i.e. arm_is_secure_below_el3, then
1907 * we must invalidate all TLBs below EL3.
1908 */
1909 if (changed & SCR_NS) {
1910 tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1911 ARMMMUIdxBit_E20_0 |
1912 ARMMMUIdxBit_E10_1 |
1913 ARMMMUIdxBit_E20_2 |
1914 ARMMMUIdxBit_E10_1_PAN |
1915 ARMMMUIdxBit_E20_2_PAN |
1916 ARMMMUIdxBit_E2));
1917 }
64e0e2de
EI
1918}
1919
10d0ef3e
MN
1920static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1921{
1922 /*
1923 * scr_write will set the RES1 bits on an AArch64-only CPU.
1924 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1925 */
1926 scr_write(env, ri, 0);
1927}
1928
e2ce5fcd
PM
1929static CPAccessResult access_tid4(CPUARMState *env,
1930 const ARMCPRegInfo *ri,
1931 bool isread)
630fcd4d 1932{
e2ce5fcd
PM
1933 if (arm_current_el(env) == 1 &&
1934 (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
630fcd4d
MZ
1935 return CP_ACCESS_TRAP_EL2;
1936 }
1937
1938 return CP_ACCESS_OK;
1939}
1940
c4241c7d 1941static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c 1942{
2fc0cc0e 1943 ARMCPU *cpu = env_archcpu(env);
b85a1fd6 1944
9b37a28c
FR
1945 /*
1946 * Acquire the CSSELR index from the bank corresponding to the CCSIDR
b85a1fd6
FA
1947 * bank
1948 */
1949 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1950 ri->secure & ARM_CP_SECSTATE_S);
1951
1952 return cpu->ccsidr[index];
776d4e5c
PM
1953}
1954
c4241c7d
PM
1955static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1956 uint64_t value)
776d4e5c 1957{
8d5c773e 1958 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1959}
1960
1090b9c6
PM
1961static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1962{
29a0af61 1963 CPUState *cs = env_cpu(env);
cc974d5c
RDC
1964 bool el1 = arm_current_el(env) == 1;
1965 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1090b9c6
PM
1966 uint64_t ret = 0;
1967
cc974d5c 1968 if (hcr_el2 & HCR_IMO) {
636540e9
PM
1969 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1970 ret |= CPSR_I;
1971 }
1972 } else {
1973 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1974 ret |= CPSR_I;
1975 }
1090b9c6 1976 }
636540e9 1977
cc974d5c 1978 if (hcr_el2 & HCR_FMO) {
636540e9
PM
1979 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1980 ret |= CPSR_F;
1981 }
1982 } else {
1983 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1984 ret |= CPSR_F;
1985 }
1090b9c6 1986 }
636540e9 1987
3c29632f
RH
1988 if (hcr_el2 & HCR_AMO) {
1989 if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1990 ret |= CPSR_A;
1991 }
1992 }
1993
1090b9c6
PM
1994 return ret;
1995}
1996
93fbc983
MZ
1997static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1998 bool isread)
1999{
2000 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2001 return CP_ACCESS_TRAP_EL2;
2002 }
2003
2004 return CP_ACCESS_OK;
2005}
2006
2007static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2008 bool isread)
2009{
2010 if (arm_feature(env, ARM_FEATURE_V8)) {
2011 return access_aa64_tid1(env, ri, isread);
2012 }
2013
2014 return CP_ACCESS_OK;
2015}
2016
e9aa6c21 2017static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
2018 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2019 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2020 .access = PL1_W, .type = ARM_CP_NOP },
9b37a28c
FR
2021 /*
2022 * Performance monitors are implementation defined in v7,
200ac0ef 2023 * but with an ARM recommended set of registers, which we
ac689a2e 2024 * follow.
200ac0ef
PM
2025 *
2026 * Performance registers fall into three categories:
2027 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2028 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2029 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2030 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2031 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2032 */
2033 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7f4fbfb5 2034 .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
8521466b 2035 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2036 .writefn = pmcntenset_write,
2037 .accessfn = pmreg_access,
dc780233 2038 .fgt = FGT_PMCNTEN,
fcd25206 2039 .raw_writefn = raw_write },
7f4fbfb5 2040 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
8521466b
AF
2041 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2042 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2043 .fgt = FGT_PMCNTEN,
8521466b
AF
2044 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2045 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 2046 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
2047 .access = PL0_RW,
2048 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206 2049 .accessfn = pmreg_access,
dc780233 2050 .fgt = FGT_PMCNTEN,
fcd25206 2051 .writefn = pmcntenclr_write,
7f4fbfb5 2052 .type = ARM_CP_ALIAS | ARM_CP_IO },
8521466b
AF
2053 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2054 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2055 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2056 .fgt = FGT_PMCNTEN,
7f4fbfb5 2057 .type = ARM_CP_ALIAS | ARM_CP_IO,
8521466b
AF
2058 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2059 .writefn = pmcntenclr_write },
200ac0ef 2060 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
f4efb4b2 2061 .access = PL0_RW, .type = ARM_CP_IO,
e4e91a21 2062 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206 2063 .accessfn = pmreg_access,
dc780233 2064 .fgt = FGT_PMOVS,
fcd25206
PM
2065 .writefn = pmovsr_write,
2066 .raw_writefn = raw_write },
978364f1
AF
2067 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2068 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2069 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2070 .fgt = FGT_PMOVS,
f4efb4b2 2071 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2072 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2073 .writefn = pmovsr_write,
2074 .raw_writefn = raw_write },
200ac0ef 2075 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
f4efb4b2 2076 .access = PL0_W, .accessfn = pmreg_access_swinc,
dc780233 2077 .fgt = FGT_PMSWINC_EL0,
f4efb4b2 2078 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d
AL
2079 .writefn = pmswinc_write },
2080 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2081 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
f4efb4b2 2082 .access = PL0_W, .accessfn = pmreg_access_swinc,
dc780233 2083 .fgt = FGT_PMSWINC_EL0,
f4efb4b2 2084 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d 2085 .writefn = pmswinc_write },
6b040780
WH
2086 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2087 .access = PL0_RW, .type = ARM_CP_ALIAS,
dc780233 2088 .fgt = FGT_PMSELR_EL0,
6b040780 2089 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 2090 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
2091 .raw_writefn = raw_write},
2092 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2093 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 2094 .access = PL0_RW, .accessfn = pmreg_access_selr,
dc780233 2095 .fgt = FGT_PMSELR_EL0,
6b040780
WH
2096 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2097 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 2098 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 2099 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
dc780233 2100 .fgt = FGT_PMCCNTR_EL0,
421c7ebd 2101 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 2102 .accessfn = pmreg_access_ccntr },
8521466b
AF
2103 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2104 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 2105 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
dc780233 2106 .fgt = FGT_PMCCNTR_EL0,
8521466b 2107 .type = ARM_CP_IO,
980ebe87
AL
2108 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2109 .readfn = pmccntr_read, .writefn = pmccntr_write,
2110 .raw_readfn = raw_read, .raw_writefn = raw_write, },
4b8afa1f
AL
2111 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2112 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2113 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2114 .fgt = FGT_PMCCFILTR_EL0,
4b8afa1f
AL
2115 .type = ARM_CP_ALIAS | ARM_CP_IO,
2116 .resetvalue = 0, },
8521466b
AF
2117 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2118 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
980ebe87 2119 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
8521466b 2120 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2121 .fgt = FGT_PMCCFILTR_EL0,
8521466b
AF
2122 .type = ARM_CP_IO,
2123 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2124 .resetvalue = 0, },
200ac0ef 2125 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
5ecdd3e4
AL
2126 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2127 .accessfn = pmreg_access,
dc780233 2128 .fgt = FGT_PMEVTYPERN_EL0,
fdb86656
WH
2129 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2130 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2131 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
5ecdd3e4
AL
2132 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2133 .accessfn = pmreg_access,
dc780233 2134 .fgt = FGT_PMEVTYPERN_EL0,
fdb86656 2135 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
200ac0ef 2136 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
5ecdd3e4
AL
2137 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2138 .accessfn = pmreg_access_xevcntr,
dc780233 2139 .fgt = FGT_PMEVCNTRN_EL0,
5ecdd3e4
AL
2140 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2141 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2142 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2143 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2144 .accessfn = pmreg_access_xevcntr,
dc780233 2145 .fgt = FGT_PMEVCNTRN_EL0,
5ecdd3e4 2146 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
200ac0ef 2147 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 2148 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 2149 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 2150 .resetvalue = 0,
d4e6df63 2151 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
2152 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2153 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 2154 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
2155 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2156 .resetvalue = 0,
2157 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 2158 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 2159 .access = PL1_RW, .accessfn = access_tpm,
dc780233 2160 .fgt = FGT_PMINTEN,
b7d793ad 2161 .type = ARM_CP_ALIAS | ARM_CP_IO,
e6ec5457 2162 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 2163 .resetvalue = 0,
d4e6df63 2164 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
2165 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2166 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2167 .access = PL1_RW, .accessfn = access_tpm,
dc780233 2168 .fgt = FGT_PMINTEN,
e6ec5457
WH
2169 .type = ARM_CP_IO,
2170 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2171 .writefn = pmintenset_write, .raw_writefn = raw_write,
2172 .resetvalue = 0x0 },
200ac0ef 2173 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
fc5f6856 2174 .access = PL1_RW, .accessfn = access_tpm,
dc780233 2175 .fgt = FGT_PMINTEN,
887c0f15 2176 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
200ac0ef 2177 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 2178 .writefn = pmintenclr_write, },
978364f1
AF
2179 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2180 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
fc5f6856 2181 .access = PL1_RW, .accessfn = access_tpm,
dc780233 2182 .fgt = FGT_PMINTEN,
887c0f15 2183 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
978364f1
AF
2184 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2185 .writefn = pmintenclr_write },
7da845b0
PM
2186 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2187 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
630fcd4d 2188 .access = PL1_R,
e2ce5fcd 2189 .accessfn = access_tid4,
158c276c 2190 .fgt = FGT_CCSIDR_EL1,
630fcd4d 2191 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
2192 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2193 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
630fcd4d 2194 .access = PL1_RW,
e2ce5fcd 2195 .accessfn = access_tid4,
b19ed03c 2196 .fgt = FGT_CSSELR_EL1,
630fcd4d 2197 .writefn = csselr_write, .resetvalue = 0,
b85a1fd6
FA
2198 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2199 offsetof(CPUARMState, cp15.csselr_ns) } },
9b37a28c
FR
2200 /*
2201 * Auxiliary ID register: this actually has an IMPDEF value but for now
776d4e5c
PM
2202 * just RAZ for all cores:
2203 */
0ff644a7
PM
2204 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2205 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
93fbc983
MZ
2206 .access = PL1_R, .type = ARM_CP_CONST,
2207 .accessfn = access_aa64_tid1,
158c276c 2208 .fgt = FGT_AIDR_EL1,
93fbc983 2209 .resetvalue = 0 },
9b37a28c
FR
2210 /*
2211 * Auxiliary fault status registers: these also are IMPDEF, and we
f32cdad5
PM
2212 * choose to RAZ/WI for all cores.
2213 */
2214 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2215 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
84929218 2216 .access = PL1_RW, .accessfn = access_tvm_trvm,
158c276c 2217 .fgt = FGT_AFSR0_EL1,
84929218 2218 .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
2219 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2220 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
84929218 2221 .access = PL1_RW, .accessfn = access_tvm_trvm,
158c276c 2222 .fgt = FGT_AFSR1_EL1,
84929218 2223 .type = ARM_CP_CONST, .resetvalue = 0 },
9b37a28c
FR
2224 /*
2225 * MAIR can just read-as-written because we don't implement caches
b0fe2427
PM
2226 * and so don't need to care about memory attributes.
2227 */
2228 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2229 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
84929218 2230 .access = PL1_RW, .accessfn = access_tvm_trvm,
67dd8030 2231 .fgt = FGT_MAIR_EL1,
84929218 2232 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 2233 .resetvalue = 0 },
4cfb8ad8
PM
2234 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2235 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2236 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2237 .resetvalue = 0 },
9b37a28c
FR
2238 /*
2239 * For non-long-descriptor page tables these are PRRR and NMRR;
b0fe2427 2240 * regardless they still act as reads-as-written for QEMU.
b0fe2427 2241 */
9b37a28c
FR
2242 /*
2243 * MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
2244 * allows them to assign the correct fieldoffset based on the endianness
2245 * handled in the field definitions.
2246 */
a903c449 2247 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
84929218
RH
2248 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2249 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2250 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2251 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 2252 .resetfn = arm_cp_reset_ignore },
a903c449 2253 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
84929218
RH
2254 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2255 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2256 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2257 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 2258 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
2259 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2260 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
b19ed03c 2261 .fgt = FGT_ISR_EL1,
7a0e58fa 2262 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
2263 /* 32 bit ITLB invalidates */
2264 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
30881b73
RH
2265 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2266 .writefn = tlbiall_write },
995939a6 2267 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
30881b73
RH
2268 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2269 .writefn = tlbimva_write },
995939a6 2270 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
30881b73
RH
2271 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2272 .writefn = tlbiasid_write },
995939a6
PM
2273 /* 32 bit DTLB invalidates */
2274 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
30881b73
RH
2275 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2276 .writefn = tlbiall_write },
995939a6 2277 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
30881b73
RH
2278 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2279 .writefn = tlbimva_write },
995939a6 2280 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
30881b73
RH
2281 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2282 .writefn = tlbiasid_write },
995939a6
PM
2283 /* 32 bit TLB invalidates */
2284 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73
RH
2285 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2286 .writefn = tlbiall_write },
995939a6 2287 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73
RH
2288 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2289 .writefn = tlbimva_write },
995939a6 2290 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73
RH
2291 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2292 .writefn = tlbiasid_write },
995939a6 2293 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73
RH
2294 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2295 .writefn = tlbimvaa_write },
995939a6
PM
2296};
2297
2298static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2299 /* 32 bit TLB invalidates, Inner Shareable */
2300 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
0f66d223 2301 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
30881b73 2302 .writefn = tlbiall_is_write },
995939a6 2303 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
0f66d223 2304 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
30881b73 2305 .writefn = tlbimva_is_write },
995939a6 2306 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
0f66d223 2307 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
fa439fc5 2308 .writefn = tlbiasid_is_write },
995939a6 2309 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
0f66d223 2310 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
fa439fc5 2311 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
2312};
2313
327dd510
AL
2314static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2315 /* PMOVSSET is not implemented in v7 before v7ve */
2316 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2317 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2318 .fgt = FGT_PMOVS,
f4efb4b2 2319 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2320 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2321 .writefn = pmovsset_write,
2322 .raw_writefn = raw_write },
2323 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2324 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2325 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2326 .fgt = FGT_PMOVS,
f4efb4b2 2327 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2328 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2329 .writefn = pmovsset_write,
2330 .raw_writefn = raw_write },
327dd510
AL
2331};
2332
c4241c7d
PM
2333static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2334 uint64_t value)
c326b979
PM
2335{
2336 value &= 1;
2337 env->teecr = value;
c326b979
PM
2338}
2339
cc7613bf
PM
2340static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2341 bool isread)
2342{
2343 /*
2344 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2345 * at all, so we don't need to check whether we're v8A.
2346 */
2347 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2348 (env->cp15.hstr_el2 & HSTR_TTEE)) {
2349 return CP_ACCESS_TRAP_EL2;
2350 }
2351 return CP_ACCESS_OK;
2352}
2353
3f208fd7
PM
2354static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2355 bool isread)
c326b979 2356{
dcbff19b 2357 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 2358 return CP_ACCESS_TRAP;
c326b979 2359 }
cc7613bf 2360 return teecr_access(env, ri, isread);
c326b979
PM
2361}
2362
2363static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2364 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2365 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2366 .resetvalue = 0,
cc7613bf 2367 .writefn = teecr_write, .accessfn = teecr_access },
c326b979
PM
2368 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2369 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 2370 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
2371};
2372
4d31c596 2373static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
2374 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2375 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2376 .access = PL0_RW,
67dd8030 2377 .fgt = FGT_TPIDR_EL0,
54bf36ed 2378 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
2379 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2380 .access = PL0_RW,
67dd8030 2381 .fgt = FGT_TPIDR_EL0,
54bf36ed
FA
2382 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2383 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
2384 .resetfn = arm_cp_reset_ignore },
2385 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2386 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
04215eb1 2387 .access = PL0_R | PL1_W,
67dd8030 2388 .fgt = FGT_TPIDRRO_EL0,
54bf36ed
FA
2389 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2390 .resetvalue = 0},
4d31c596 2391 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
04215eb1 2392 .access = PL0_R | PL1_W,
67dd8030 2393 .fgt = FGT_TPIDRRO_EL0,
54bf36ed
FA
2394 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2395 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 2396 .resetfn = arm_cp_reset_ignore },
54bf36ed 2397 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 2398 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 2399 .access = PL1_RW,
67dd8030 2400 .fgt = FGT_TPIDR_EL1,
54bf36ed
FA
2401 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2402 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2403 .access = PL1_RW,
2404 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2405 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2406 .resetvalue = 0 },
4d31c596
PM
2407};
2408
55d284af
PM
2409#ifndef CONFIG_USER_ONLY
2410
3f208fd7
PM
2411static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2412 bool isread)
00108f2d 2413{
9b37a28c
FR
2414 /*
2415 * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
75502672
PM
2416 * Writable only at the highest implemented exception level.
2417 */
2418 int el = arm_current_el(env);
5bc84371
RH
2419 uint64_t hcr;
2420 uint32_t cntkctl;
75502672
PM
2421
2422 switch (el) {
2423 case 0:
5bc84371
RH
2424 hcr = arm_hcr_el2_eff(env);
2425 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2426 cntkctl = env->cp15.cnthctl_el2;
2427 } else {
2428 cntkctl = env->cp15.c14_cntkctl;
2429 }
2430 if (!extract32(cntkctl, 0, 2)) {
75502672
PM
2431 return CP_ACCESS_TRAP;
2432 }
2433 break;
2434 case 1:
2435 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2436 arm_is_secure_below_el3(env)) {
2437 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2438 return CP_ACCESS_TRAP_UNCATEGORIZED;
2439 }
2440 break;
2441 case 2:
2442 case 3:
2443 break;
00108f2d 2444 }
75502672
PM
2445
2446 if (!isread && el < arm_highest_el(env)) {
2447 return CP_ACCESS_TRAP_UNCATEGORIZED;
2448 }
2449
00108f2d
PM
2450 return CP_ACCESS_OK;
2451}
2452
3f208fd7
PM
2453static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2454 bool isread)
00108f2d 2455{
0b6440af 2456 unsigned int cur_el = arm_current_el(env);
e6ef0169 2457 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2458 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2459
5bc84371
RH
2460 switch (cur_el) {
2461 case 0:
2462 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2463 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2464 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2465 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2466 }
0b6440af 2467
5bc84371
RH
2468 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2469 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2470 return CP_ACCESS_TRAP;
2471 }
2472
2473 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2474 if (hcr & HCR_E2H) {
2475 if (timeridx == GTIMER_PHYS &&
2476 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2477 return CP_ACCESS_TRAP_EL2;
2478 }
2479 } else {
2480 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
e6ef0169 2481 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2482 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2483 return CP_ACCESS_TRAP_EL2;
2484 }
2485 }
2486 break;
2487
2488 case 1:
2489 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
e6ef0169 2490 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2491 (hcr & HCR_E2H
2492 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2493 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2494 return CP_ACCESS_TRAP_EL2;
2495 }
2496 break;
0b6440af 2497 }
00108f2d
PM
2498 return CP_ACCESS_OK;
2499}
2500
3f208fd7
PM
2501static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2502 bool isread)
00108f2d 2503{
0b6440af 2504 unsigned int cur_el = arm_current_el(env);
e6ef0169 2505 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2506 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2507
5bc84371
RH
2508 switch (cur_el) {
2509 case 0:
2510 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2511 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2512 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2513 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2514 }
0b6440af 2515
5bc84371
RH
2516 /*
2517 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2518 * EL0 if EL0[PV]TEN is zero.
2519 */
2520 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2521 return CP_ACCESS_TRAP;
2522 }
2523 /* fall through */
2524
2525 case 1:
e6ef0169 2526 if (has_el2 && timeridx == GTIMER_PHYS) {
5bc84371
RH
2527 if (hcr & HCR_E2H) {
2528 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2529 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2530 return CP_ACCESS_TRAP_EL2;
2531 }
2532 } else {
2533 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2534 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2535 return CP_ACCESS_TRAP_EL2;
2536 }
2537 }
2538 }
2539 break;
0b6440af 2540 }
00108f2d
PM
2541 return CP_ACCESS_OK;
2542}
2543
2544static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
2545 const ARMCPRegInfo *ri,
2546 bool isread)
00108f2d 2547{
3f208fd7 2548 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2549}
2550
2551static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
2552 const ARMCPRegInfo *ri,
2553 bool isread)
00108f2d 2554{
3f208fd7 2555 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2556}
2557
3f208fd7
PM
2558static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2559 bool isread)
00108f2d 2560{
3f208fd7 2561 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2562}
2563
3f208fd7
PM
2564static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2565 bool isread)
00108f2d 2566{
3f208fd7 2567 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2568}
2569
b4d3978c 2570static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
2571 const ARMCPRegInfo *ri,
2572 bool isread)
b4d3978c 2573{
9b37a28c
FR
2574 /*
2575 * The AArch64 register view of the secure physical timer is
b4d3978c
PM
2576 * always accessible from EL3, and configurably accessible from
2577 * Secure EL1.
2578 */
2579 switch (arm_current_el(env)) {
2580 case 1:
2581 if (!arm_is_secure(env)) {
2582 return CP_ACCESS_TRAP;
2583 }
2584 if (!(env->cp15.scr_el3 & SCR_ST)) {
2585 return CP_ACCESS_TRAP_EL3;
2586 }
2587 return CP_ACCESS_OK;
2588 case 0:
2589 case 2:
2590 return CP_ACCESS_TRAP;
2591 case 3:
2592 return CP_ACCESS_OK;
2593 default:
2594 g_assert_not_reached();
2595 }
2596}
2597
55d284af
PM
2598static uint64_t gt_get_countervalue(CPUARMState *env)
2599{
7def8754
AJ
2600 ARMCPU *cpu = env_archcpu(env);
2601
2602 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
55d284af
PM
2603}
2604
2605static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2606{
2607 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2608
2609 if (gt->ctl & 1) {
9b37a28c
FR
2610 /*
2611 * Timer enabled: calculate and set current ISTATUS, irq, and
55d284af
PM
2612 * reset timer to when ISTATUS next has to change
2613 */
edac4d8a
EI
2614 uint64_t offset = timeridx == GTIMER_VIRT ?
2615 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
2616 uint64_t count = gt_get_countervalue(&cpu->env);
2617 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 2618 int istatus = count - offset >= gt->cval;
55d284af 2619 uint64_t nexttick;
194cbc49 2620 int irqstate;
55d284af
PM
2621
2622 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
2623
2624 irqstate = (istatus && !(gt->ctl & 2));
2625 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2626
55d284af
PM
2627 if (istatus) {
2628 /* Next transition is when count rolls back over to zero */
2629 nexttick = UINT64_MAX;
2630 } else {
2631 /* Next transition is when we hit cval */
edac4d8a 2632 nexttick = gt->cval + offset;
55d284af 2633 }
9b37a28c
FR
2634 /*
2635 * Note that the desired next expiry time might be beyond the
55d284af
PM
2636 * signed-64-bit range of a QEMUTimer -- in this case we just
2637 * set the timer for as far in the future as possible. When the
2638 * timer expires we will reset the timer for any remaining period.
2639 */
7def8754 2640 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
4a0245b6
AJ
2641 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2642 } else {
2643 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af 2644 }
194cbc49 2645 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
2646 } else {
2647 /* Timer disabled: ISTATUS and timer output always clear */
2648 gt->ctl &= ~4;
2649 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 2650 timer_del(cpu->gt_timer[timeridx]);
194cbc49 2651 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
2652 }
2653}
2654
0e3eca4c
EI
2655static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2656 int timeridx)
55d284af 2657{
2fc0cc0e 2658 ARMCPU *cpu = env_archcpu(env);
55d284af 2659
bc72ad67 2660 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
2661}
2662
c4241c7d 2663static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 2664{
c4241c7d 2665 return gt_get_countervalue(env);
55d284af
PM
2666}
2667
53d1f856
RH
2668static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2669{
2670 uint64_t hcr;
2671
2672 switch (arm_current_el(env)) {
2673 case 2:
2674 hcr = arm_hcr_el2_eff(env);
2675 if (hcr & HCR_E2H) {
2676 return 0;
2677 }
2678 break;
2679 case 0:
2680 hcr = arm_hcr_el2_eff(env);
2681 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2682 return 0;
2683 }
2684 break;
2685 }
2686
2687 return env->cp15.cntvoff_el2;
2688}
2689
edac4d8a
EI
2690static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2691{
53d1f856 2692 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
edac4d8a
EI
2693}
2694
c4241c7d 2695static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2696 int timeridx,
c4241c7d 2697 uint64_t value)
55d284af 2698{
194cbc49 2699 trace_arm_gt_cval_write(timeridx, value);
55d284af 2700 env->cp15.c14_timer[timeridx].cval = value;
2fc0cc0e 2701 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af 2702}
c4241c7d 2703
0e3eca4c
EI
2704static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2705 int timeridx)
55d284af 2706{
53d1f856
RH
2707 uint64_t offset = 0;
2708
2709 switch (timeridx) {
2710 case GTIMER_VIRT:
8c94b071 2711 case GTIMER_HYPVIRT:
53d1f856
RH
2712 offset = gt_virt_cnt_offset(env);
2713 break;
2714 }
55d284af 2715
c4241c7d 2716 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 2717 (gt_get_countervalue(env) - offset));
55d284af
PM
2718}
2719
c4241c7d 2720static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2721 int timeridx,
c4241c7d 2722 uint64_t value)
55d284af 2723{
53d1f856
RH
2724 uint64_t offset = 0;
2725
2726 switch (timeridx) {
2727 case GTIMER_VIRT:
8c94b071 2728 case GTIMER_HYPVIRT:
53d1f856
RH
2729 offset = gt_virt_cnt_offset(env);
2730 break;
2731 }
55d284af 2732
194cbc49 2733 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 2734 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 2735 sextract64(value, 0, 32);
2fc0cc0e 2736 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af
PM
2737}
2738
c4241c7d 2739static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2740 int timeridx,
c4241c7d 2741 uint64_t value)
55d284af 2742{
2fc0cc0e 2743 ARMCPU *cpu = env_archcpu(env);
55d284af
PM
2744 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2745
194cbc49 2746 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 2747 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
2748 if ((oldval ^ value) & 1) {
2749 /* Enable toggled */
2750 gt_recalc_timer(cpu, timeridx);
d3afacc7 2751 } else if ((oldval ^ value) & 2) {
9b37a28c
FR
2752 /*
2753 * IMASK toggled: don't need to recalculate,
55d284af
PM
2754 * just set the interrupt line based on ISTATUS
2755 */
194cbc49
PM
2756 int irqstate = (oldval & 4) && !(value & 2);
2757
2758 trace_arm_gt_imask_toggle(timeridx, irqstate);
2759 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 2760 }
55d284af
PM
2761}
2762
0e3eca4c
EI
2763static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2764{
2765 gt_timer_reset(env, ri, GTIMER_PHYS);
2766}
2767
2768static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2769 uint64_t value)
2770{
2771 gt_cval_write(env, ri, GTIMER_PHYS, value);
2772}
2773
2774static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2775{
2776 return gt_tval_read(env, ri, GTIMER_PHYS);
2777}
2778
2779static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2780 uint64_t value)
2781{
2782 gt_tval_write(env, ri, GTIMER_PHYS, value);
2783}
2784
2785static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2786 uint64_t value)
2787{
2788 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2789}
2790
bb5972e4
RH
2791static int gt_phys_redir_timeridx(CPUARMState *env)
2792{
2793 switch (arm_mmu_idx(env)) {
2794 case ARMMMUIdx_E20_0:
2795 case ARMMMUIdx_E20_2:
452ef8cb 2796 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2797 return GTIMER_HYP;
2798 default:
2799 return GTIMER_PHYS;
2800 }
2801}
2802
2803static int gt_virt_redir_timeridx(CPUARMState *env)
2804{
2805 switch (arm_mmu_idx(env)) {
2806 case ARMMMUIdx_E20_0:
2807 case ARMMMUIdx_E20_2:
452ef8cb 2808 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2809 return GTIMER_HYPVIRT;
2810 default:
2811 return GTIMER_VIRT;
2812 }
2813}
2814
2815static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2816 const ARMCPRegInfo *ri)
2817{
2818 int timeridx = gt_phys_redir_timeridx(env);
2819 return env->cp15.c14_timer[timeridx].cval;
2820}
2821
2822static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2823 uint64_t value)
2824{
2825 int timeridx = gt_phys_redir_timeridx(env);
2826 gt_cval_write(env, ri, timeridx, value);
2827}
2828
2829static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2830 const ARMCPRegInfo *ri)
2831{
2832 int timeridx = gt_phys_redir_timeridx(env);
2833 return gt_tval_read(env, ri, timeridx);
2834}
2835
2836static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2837 uint64_t value)
2838{
2839 int timeridx = gt_phys_redir_timeridx(env);
2840 gt_tval_write(env, ri, timeridx, value);
2841}
2842
2843static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2844 const ARMCPRegInfo *ri)
2845{
2846 int timeridx = gt_phys_redir_timeridx(env);
2847 return env->cp15.c14_timer[timeridx].ctl;
2848}
2849
2850static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2851 uint64_t value)
2852{
2853 int timeridx = gt_phys_redir_timeridx(env);
2854 gt_ctl_write(env, ri, timeridx, value);
2855}
2856
0e3eca4c
EI
2857static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2858{
2859 gt_timer_reset(env, ri, GTIMER_VIRT);
2860}
2861
2862static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2863 uint64_t value)
2864{
2865 gt_cval_write(env, ri, GTIMER_VIRT, value);
2866}
2867
2868static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2869{
2870 return gt_tval_read(env, ri, GTIMER_VIRT);
2871}
2872
2873static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2874 uint64_t value)
2875{
2876 gt_tval_write(env, ri, GTIMER_VIRT, value);
2877}
2878
2879static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2880 uint64_t value)
2881{
2882 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2883}
2884
edac4d8a
EI
2885static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2886 uint64_t value)
2887{
2fc0cc0e 2888 ARMCPU *cpu = env_archcpu(env);
edac4d8a 2889
194cbc49 2890 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
2891 raw_write(env, ri, value);
2892 gt_recalc_timer(cpu, GTIMER_VIRT);
2893}
2894
bb5972e4
RH
2895static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2896 const ARMCPRegInfo *ri)
2897{
2898 int timeridx = gt_virt_redir_timeridx(env);
2899 return env->cp15.c14_timer[timeridx].cval;
2900}
2901
2902static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2903 uint64_t value)
2904{
2905 int timeridx = gt_virt_redir_timeridx(env);
2906 gt_cval_write(env, ri, timeridx, value);
2907}
2908
2909static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2910 const ARMCPRegInfo *ri)
2911{
2912 int timeridx = gt_virt_redir_timeridx(env);
2913 return gt_tval_read(env, ri, timeridx);
2914}
2915
2916static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2917 uint64_t value)
2918{
2919 int timeridx = gt_virt_redir_timeridx(env);
2920 gt_tval_write(env, ri, timeridx, value);
2921}
2922
2923static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2924 const ARMCPRegInfo *ri)
2925{
2926 int timeridx = gt_virt_redir_timeridx(env);
2927 return env->cp15.c14_timer[timeridx].ctl;
2928}
2929
2930static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2931 uint64_t value)
2932{
2933 int timeridx = gt_virt_redir_timeridx(env);
2934 gt_ctl_write(env, ri, timeridx, value);
2935}
2936
b0e66d95
EI
2937static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2938{
2939 gt_timer_reset(env, ri, GTIMER_HYP);
2940}
2941
2942static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2943 uint64_t value)
2944{
2945 gt_cval_write(env, ri, GTIMER_HYP, value);
2946}
2947
2948static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2949{
2950 return gt_tval_read(env, ri, GTIMER_HYP);
2951}
2952
2953static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2954 uint64_t value)
2955{
2956 gt_tval_write(env, ri, GTIMER_HYP, value);
2957}
2958
2959static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2960 uint64_t value)
2961{
2962 gt_ctl_write(env, ri, GTIMER_HYP, value);
2963}
2964
b4d3978c
PM
2965static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2966{
2967 gt_timer_reset(env, ri, GTIMER_SEC);
2968}
2969
2970static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2971 uint64_t value)
2972{
2973 gt_cval_write(env, ri, GTIMER_SEC, value);
2974}
2975
2976static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2977{
2978 return gt_tval_read(env, ri, GTIMER_SEC);
2979}
2980
2981static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2982 uint64_t value)
2983{
2984 gt_tval_write(env, ri, GTIMER_SEC, value);
2985}
2986
2987static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2988 uint64_t value)
2989{
2990 gt_ctl_write(env, ri, GTIMER_SEC, value);
2991}
2992
8c94b071
RH
2993static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2994{
2995 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2996}
2997
2998static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2999 uint64_t value)
3000{
3001 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3002}
3003
3004static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3005{
3006 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3007}
3008
3009static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3010 uint64_t value)
3011{
3012 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3013}
3014
3015static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3016 uint64_t value)
3017{
3018 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3019}
3020
55d284af
PM
3021void arm_gt_ptimer_cb(void *opaque)
3022{
3023 ARMCPU *cpu = opaque;
3024
3025 gt_recalc_timer(cpu, GTIMER_PHYS);
3026}
3027
3028void arm_gt_vtimer_cb(void *opaque)
3029{
3030 ARMCPU *cpu = opaque;
3031
3032 gt_recalc_timer(cpu, GTIMER_VIRT);
3033}
3034
b0e66d95
EI
3035void arm_gt_htimer_cb(void *opaque)
3036{
3037 ARMCPU *cpu = opaque;
3038
3039 gt_recalc_timer(cpu, GTIMER_HYP);
3040}
3041
b4d3978c
PM
3042void arm_gt_stimer_cb(void *opaque)
3043{
3044 ARMCPU *cpu = opaque;
3045
3046 gt_recalc_timer(cpu, GTIMER_SEC);
3047}
3048
8c94b071
RH
3049void arm_gt_hvtimer_cb(void *opaque)
3050{
3051 ARMCPU *cpu = opaque;
3052
3053 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3054}
3055
96eec6b2
AJ
3056static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3057{
3058 ARMCPU *cpu = env_archcpu(env);
3059
3060 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3061}
3062
55d284af 3063static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
9b37a28c
FR
3064 /*
3065 * Note that CNTFRQ is purely reads-as-written for the benefit
55d284af
PM
3066 * of software; writing it doesn't actually change the timer frequency.
3067 * Our reset value matches the fixed frequency we implement the timer at.
3068 */
3069 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3070 .type = ARM_CP_ALIAS,
a7adc4b7
PM
3071 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3072 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
3073 },
3074 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3075 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3076 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af 3077 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
96eec6b2 3078 .resetfn = arm_gt_cntfrq_reset,
55d284af
PM
3079 },
3080 /* overall control: mostly access permissions */
a7adc4b7
PM
3081 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3082 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
3083 .access = PL1_RW,
3084 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3085 .resetvalue = 0,
3086 },
3087 /* per-timer control */
3088 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 3089 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3090 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3091 .accessfn = gt_ptimer_access,
3092 .fieldoffset = offsetoflow32(CPUARMState,
3093 cp15.c14_timer[GTIMER_PHYS].ctl),
bb5972e4
RH
3094 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3095 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7 3096 },
9c513e78 3097 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
3098 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3099 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3100 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
9ff9dd3c
PM
3101 .accessfn = gt_ptimer_access,
3102 .fieldoffset = offsetoflow32(CPUARMState,
3103 cp15.c14_timer[GTIMER_SEC].ctl),
3104 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3105 },
a7adc4b7
PM
3106 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3107 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
daf1dc5f 3108 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3109 .accessfn = gt_ptimer_access,
55d284af
PM
3110 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3111 .resetvalue = 0,
bb5972e4
RH
3112 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3113 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3114 },
3115 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
daf1dc5f 3116 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3117 .accessfn = gt_vtimer_access,
3118 .fieldoffset = offsetoflow32(CPUARMState,
3119 cp15.c14_timer[GTIMER_VIRT].ctl),
bb5972e4
RH
3120 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3121 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
3122 },
3123 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3124 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
daf1dc5f 3125 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3126 .accessfn = gt_vtimer_access,
55d284af
PM
3127 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3128 .resetvalue = 0,
bb5972e4
RH
3129 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3130 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3131 },
3132 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3133 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 3134 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3135 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3136 .accessfn = gt_ptimer_access,
bb5972e4 3137 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
55d284af 3138 },
9c513e78 3139 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
3140 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3141 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3142 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
9ff9dd3c
PM
3143 .accessfn = gt_ptimer_access,
3144 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3145 },
a7adc4b7
PM
3146 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3147 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
daf1dc5f 3148 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3149 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
bb5972e4 3150 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
a7adc4b7 3151 },
55d284af 3152 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
daf1dc5f 3153 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3154 .accessfn = gt_vtimer_access,
bb5972e4 3155 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
55d284af 3156 },
a7adc4b7
PM
3157 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3158 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
daf1dc5f 3159 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3160 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
bb5972e4 3161 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
a7adc4b7 3162 },
55d284af
PM
3163 /* The counter itself */
3164 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 3165 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3166 .accessfn = gt_pct_access,
a7adc4b7
PM
3167 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3168 },
3169 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3170 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 3171 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3172 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
3173 },
3174 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 3175 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3176 .accessfn = gt_vct_access,
edac4d8a 3177 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
3178 },
3179 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3180 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 3181 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3182 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
3183 },
3184 /* Comparison value, indicating when the timer goes off */
3185 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3186 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3187 .access = PL0_RW,
7a0e58fa 3188 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3189 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 3190 .accessfn = gt_ptimer_access,
bb5972e4
RH
3191 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3192 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7 3193 },
9c513e78 3194 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3195 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3196 .access = PL0_RW,
9ff9dd3c
PM
3197 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3198 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3199 .accessfn = gt_ptimer_access,
3200 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3201 },
a7adc4b7
PM
3202 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3203 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
daf1dc5f 3204 .access = PL0_RW,
a7adc4b7
PM
3205 .type = ARM_CP_IO,
3206 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 3207 .resetvalue = 0, .accessfn = gt_ptimer_access,
bb5972e4
RH
3208 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3209 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
55d284af
PM
3210 },
3211 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
daf1dc5f 3212 .access = PL0_RW,
7a0e58fa 3213 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3214 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 3215 .accessfn = gt_vtimer_access,
bb5972e4
RH
3216 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3217 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
3218 },
3219 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3220 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
daf1dc5f 3221 .access = PL0_RW,
a7adc4b7
PM
3222 .type = ARM_CP_IO,
3223 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3224 .resetvalue = 0, .accessfn = gt_vtimer_access,
bb5972e4
RH
3225 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3226 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
55d284af 3227 },
9b37a28c
FR
3228 /*
3229 * Secure timer -- this is actually restricted to only EL3
b4d3978c
PM
3230 * and configurably Secure-EL1 via the accessfn.
3231 */
3232 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3233 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3234 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3235 .accessfn = gt_stimer_access,
3236 .readfn = gt_sec_tval_read,
3237 .writefn = gt_sec_tval_write,
3238 .resetfn = gt_sec_timer_reset,
3239 },
3240 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3241 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3242 .type = ARM_CP_IO, .access = PL1_RW,
3243 .accessfn = gt_stimer_access,
3244 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3245 .resetvalue = 0,
3246 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3247 },
3248 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3249 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3250 .type = ARM_CP_IO, .access = PL1_RW,
3251 .accessfn = gt_stimer_access,
3252 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3253 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3254 },
55d284af
PM
3255};
3256
bb5972e4
RH
3257static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3258 bool isread)
3259{
3260 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3261 return CP_ACCESS_TRAP;
3262 }
3263 return CP_ACCESS_OK;
3264}
3265
55d284af 3266#else
26c4a83b 3267
9b37a28c
FR
3268/*
3269 * In user-mode most of the generic timer registers are inaccessible
26c4a83b 3270 * however modern kernels (4.12+) allow access to cntvct_el0
55d284af 3271 */
26c4a83b
AB
3272
3273static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3274{
7def8754
AJ
3275 ARMCPU *cpu = env_archcpu(env);
3276
9b37a28c
FR
3277 /*
3278 * Currently we have no support for QEMUTimer in linux-user so we
26c4a83b
AB
3279 * can't call gt_get_countervalue(env), instead we directly
3280 * call the lower level functions.
3281 */
7def8754 3282 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
26c4a83b
AB
3283}
3284
6cc7a3ae 3285static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
26c4a83b
AB
3286 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3287 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3288 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3289 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3290 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3291 },
3292 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3293 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3294 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3295 .readfn = gt_virt_cnt_read,
3296 },
6cc7a3ae
PM
3297};
3298
55d284af
PM
3299#endif
3300
c4241c7d 3301static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 3302{
891a2fe7 3303 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 3304 raw_write(env, ri, value);
891a2fe7 3305 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 3306 raw_write(env, ri, value & 0xfffff6ff);
4a501606 3307 } else {
8d5c773e 3308 raw_write(env, ri, value & 0xfffff1ff);
4a501606 3309 }
4a501606
PM
3310}
3311
3312#ifndef CONFIG_USER_ONLY
3313/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 3314
3f208fd7
PM
3315static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3316 bool isread)
92611c00
PM
3317{
3318 if (ri->opc2 & 4) {
9b37a28c
FR
3319 /*
3320 * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
87562e4f
PM
3321 * Secure EL1 (which can only happen if EL3 is AArch64).
3322 * They are simply UNDEF if executed from NS EL1.
3323 * They function normally from EL2 or EL3.
92611c00 3324 */
87562e4f
PM
3325 if (arm_current_el(env) == 1) {
3326 if (arm_is_secure_below_el3(env)) {
926c1b97 3327 if (env->cp15.scr_el3 & SCR_EEL2) {
ce9a8863 3328 return CP_ACCESS_TRAP_EL2;
926c1b97 3329 }
ce9a8863 3330 return CP_ACCESS_TRAP_EL3;
87562e4f
PM
3331 }
3332 return CP_ACCESS_TRAP_UNCATEGORIZED;
3333 }
92611c00
PM
3334 }
3335 return CP_ACCESS_OK;
3336}
3337
9fb005b0 3338#ifdef CONFIG_TCG
060e8a48 3339static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
7aee3cb9
RH
3340 MMUAccessType access_type, ARMMMUIdx mmu_idx,
3341 bool is_secure)
4a501606 3342{
b7cc4e82 3343 bool ret;
01c097f7 3344 uint64_t par64;
1313e2d7 3345 bool format64 = false;
e14b5a23 3346 ARMMMUFaultInfo fi = {};
de05a709 3347 GetPhysAddrResult res = {};
4a501606 3348
7aee3cb9
RH
3349 ret = get_phys_addr_with_secure(env, value, access_type, mmu_idx,
3350 is_secure, &res, &fi);
1313e2d7 3351
9f225e60
PM
3352 /*
3353 * ATS operations only do S1 or S1+S2 translations, so we never
3354 * have to deal with the ARMCacheAttrs format for S2 only.
3355 */
de05a709 3356 assert(!res.cacheattrs.is_s2_format);
9f225e60 3357
0710b2fa
PM
3358 if (ret) {
3359 /*
3360 * Some kinds of translation fault must cause exceptions rather
3361 * than being reported in the PAR.
3362 */
3363 int current_el = arm_current_el(env);
3364 int target_el;
3365 uint32_t syn, fsr, fsc;
3366 bool take_exc = false;
3367
b1a10c86 3368 if (fi.s1ptw && current_el == 1
fee7aa46 3369 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
0710b2fa
PM
3370 /*
3371 * Synchronous stage 2 fault on an access made as part of the
3372 * translation table walk for AT S1E0* or AT S1E1* insn
3373 * executed from NS EL1. If this is a synchronous external abort
3374 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3375 * to EL3. Otherwise the fault is taken as an exception to EL2,
3376 * and HPFAR_EL2 holds the faulting IPA.
3377 */
3378 if (fi.type == ARMFault_SyncExternalOnWalk &&
3379 (env->cp15.scr_el3 & SCR_EA)) {
3380 target_el = 3;
3381 } else {
3382 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
9861248f
RDC
3383 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3384 env->cp15.hpfar_el2 |= HPFAR_NS;
3385 }
0710b2fa
PM
3386 target_el = 2;
3387 }
3388 take_exc = true;
3389 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3390 /*
3391 * Synchronous external aborts during a translation table walk
3392 * are taken as Data Abort exceptions.
3393 */
3394 if (fi.stage2) {
3395 if (current_el == 3) {
3396 target_el = 3;
3397 } else {
3398 target_el = 2;
3399 }
3400 } else {
3401 target_el = exception_target_el(env);
3402 }
3403 take_exc = true;
3404 }
3405
3406 if (take_exc) {
3407 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3408 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3409 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3410 fsr = arm_fi_to_lfsc(&fi);
3411 fsc = extract32(fsr, 0, 6);
3412 } else {
3413 fsr = arm_fi_to_sfsc(&fi);
3414 fsc = 0x3f;
3415 }
3416 /*
3417 * Report exception with ESR indicating a fault due to a
3418 * translation table walk for a cache maintenance instruction.
3419 */
e24fd076 3420 syn = syn_data_abort_no_iss(current_el == target_el, 0,
0710b2fa
PM
3421 fi.ea, 1, fi.s1ptw, 1, fsc);
3422 env->exception.vaddress = value;
3423 env->exception.fsr = fsr;
3424 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3425 }
3426 }
3427
1313e2d7
EI
3428 if (is_a64(env)) {
3429 format64 = true;
3430 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3431 /*
3432 * ATS1Cxx:
3433 * * TTBCR.EAE determines whether the result is returned using the
3434 * 32-bit or the 64-bit PAR format
3435 * * Instructions executed in Hyp mode always use the 64bit format
3436 *
3437 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3438 * * The Non-secure TTBCR.EAE bit is set to 1
3439 * * The implementation includes EL2, and the value of HCR.VM is 1
3440 *
9d1bab33
PM
3441 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3442 *
23463e0e 3443 * ATS1Hx always uses the 64bit format.
1313e2d7
EI
3444 */
3445 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3446
3447 if (arm_feature(env, ARM_FEATURE_EL2)) {
452ef8cb
RH
3448 if (mmu_idx == ARMMMUIdx_E10_0 ||
3449 mmu_idx == ARMMMUIdx_E10_1 ||
3450 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9d1bab33 3451 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
1313e2d7
EI
3452 } else {
3453 format64 |= arm_current_el(env) == 2;
3454 }
3455 }
3456 }
3457
3458 if (format64) {
5efe9ed4 3459 /* Create a 64-bit PAR */
01c097f7 3460 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 3461 if (!ret) {
7fa7ea8f
RH
3462 par64 |= res.f.phys_addr & ~0xfffULL;
3463 if (!res.f.attrs.secure) {
8bf5b6a9
PM
3464 par64 |= (1 << 9); /* NS */
3465 }
de05a709
RH
3466 par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3467 par64 |= res.cacheattrs.shareability << 7; /* SH */
4a501606 3468 } else {
5efe9ed4
PM
3469 uint32_t fsr = arm_fi_to_lfsc(&fi);
3470
702a9357 3471 par64 |= 1; /* F */
b7cc4e82 3472 par64 |= (fsr & 0x3f) << 1; /* FS */
0f7b791b
PM
3473 if (fi.stage2) {
3474 par64 |= (1 << 9); /* S */
3475 }
3476 if (fi.s1ptw) {
3477 par64 |= (1 << 8); /* PTW */
3478 }
4a501606
PM
3479 }
3480 } else {
9b37a28c
FR
3481 /*
3482 * fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
3483 * translation table format (with WnR always clear).
3484 * Convert it to a 32-bit PAR.
3485 */
b7cc4e82 3486 if (!ret) {
702a9357 3487 /* We do not set any attribute bits in the PAR */
7fa7ea8f 3488 if (res.f.lg_page_size == 24
702a9357 3489 && arm_feature(env, ARM_FEATURE_V7)) {
7fa7ea8f 3490 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
702a9357 3491 } else {
7fa7ea8f 3492 par64 = res.f.phys_addr & 0xfffff000;
702a9357 3493 }
7fa7ea8f 3494 if (!res.f.attrs.secure) {
8bf5b6a9
PM
3495 par64 |= (1 << 9); /* NS */
3496 }
702a9357 3497 } else {
5efe9ed4
PM
3498 uint32_t fsr = arm_fi_to_sfsc(&fi);
3499
b7cc4e82
PC
3500 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3501 ((fsr & 0xf) << 1) | 1;
702a9357 3502 }
4a501606 3503 }
060e8a48
PM
3504 return par64;
3505}
9fb005b0 3506#endif /* CONFIG_TCG */
060e8a48
PM
3507
3508static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3509{
9fb005b0 3510#ifdef CONFIG_TCG
03ae85f8 3511 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 3512 uint64_t par64;
d3649702
PM
3513 ARMMMUIdx mmu_idx;
3514 int el = arm_current_el(env);
3515 bool secure = arm_is_secure_below_el3(env);
060e8a48 3516
d3649702
PM
3517 switch (ri->opc2 & 6) {
3518 case 0:
04b07d29 3519 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
d3649702
PM
3520 switch (el) {
3521 case 3:
d902ae75 3522 mmu_idx = ARMMMUIdx_E3;
7aee3cb9 3523 secure = true;
d3649702
PM
3524 break;
3525 case 2:
b6ad6062 3526 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
04b07d29 3527 /* fall through */
d3649702 3528 case 1:
04b07d29 3529 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
d902ae75 3530 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
04b07d29 3531 } else {
d902ae75 3532 mmu_idx = ARMMMUIdx_Stage1_E1;
04b07d29 3533 }
d3649702
PM
3534 break;
3535 default:
3536 g_assert_not_reached();
3537 }
3538 break;
3539 case 2:
3540 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3541 switch (el) {
3542 case 3:
d902ae75 3543 mmu_idx = ARMMMUIdx_E10_0;
7aee3cb9 3544 secure = true;
d3649702
PM
3545 break;
3546 case 2:
b1a10c86 3547 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
2859d7b5 3548 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3549 break;
3550 case 1:
d902ae75 3551 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3552 break;
3553 default:
3554 g_assert_not_reached();
3555 }
3556 break;
3557 case 4:
3558 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
01b98b68 3559 mmu_idx = ARMMMUIdx_E10_1;
7aee3cb9 3560 secure = false;
d3649702
PM
3561 break;
3562 case 6:
3563 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
01b98b68 3564 mmu_idx = ARMMMUIdx_E10_0;
7aee3cb9 3565 secure = false;
d3649702
PM
3566 break;
3567 default:
3568 g_assert_not_reached();
3569 }
3570
7aee3cb9 3571 par64 = do_ats_write(env, value, access_type, mmu_idx, secure);
01c097f7
FA
3572
3573 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3574#else
3575 /* Handled by hardware accelerator. */
3576 g_assert_not_reached();
3577#endif /* CONFIG_TCG */
4a501606 3578}
060e8a48 3579
14db7fe0
PM
3580static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3581 uint64_t value)
3582{
9fb005b0 3583#ifdef CONFIG_TCG
03ae85f8 3584 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
3585 uint64_t par64;
3586
7aee3cb9
RH
3587 /* There is no SecureEL2 for AArch32. */
3588 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2, false);
14db7fe0
PM
3589
3590 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3591#else
3592 /* Handled by hardware accelerator. */
3593 g_assert_not_reached();
3594#endif /* CONFIG_TCG */
14db7fe0
PM
3595}
3596
3f208fd7
PM
3597static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3598 bool isread)
2a47df95 3599{
926c1b97
RDC
3600 if (arm_current_el(env) == 3 &&
3601 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
2a47df95
PM
3602 return CP_ACCESS_TRAP;
3603 }
3604 return CP_ACCESS_OK;
3605}
3606
060e8a48
PM
3607static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3608 uint64_t value)
3609{
9fb005b0 3610#ifdef CONFIG_TCG
03ae85f8 3611 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
3612 ARMMMUIdx mmu_idx;
3613 int secure = arm_is_secure_below_el3(env);
638d5dbd
AK
3614 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3615 bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
d3649702
PM
3616
3617 switch (ri->opc2 & 6) {
3618 case 0:
3619 switch (ri->opc1) {
04b07d29
RH
3620 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3621 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
638d5dbd
AK
3622 mmu_idx = regime_e20 ?
3623 ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
04b07d29 3624 } else {
638d5dbd 3625 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
04b07d29 3626 }
d3649702
PM
3627 break;
3628 case 4: /* AT S1E2R, AT S1E2W */
638d5dbd 3629 mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
d3649702
PM
3630 break;
3631 case 6: /* AT S1E3R, AT S1E3W */
d902ae75 3632 mmu_idx = ARMMMUIdx_E3;
7aee3cb9 3633 secure = true;
d3649702
PM
3634 break;
3635 default:
3636 g_assert_not_reached();
3637 }
3638 break;
3639 case 2: /* AT S1E0R, AT S1E0W */
638d5dbd 3640 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3641 break;
3642 case 4: /* AT S12E1R, AT S12E1W */
638d5dbd 3643 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
d3649702
PM
3644 break;
3645 case 6: /* AT S12E0R, AT S12E0W */
638d5dbd 3646 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
d3649702
PM
3647 break;
3648 default:
3649 g_assert_not_reached();
3650 }
060e8a48 3651
7aee3cb9
RH
3652 env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3653 mmu_idx, secure);
9fb005b0
PMD
3654#else
3655 /* Handled by hardware accelerator. */
3656 g_assert_not_reached();
3657#endif /* CONFIG_TCG */
060e8a48 3658}
4a501606
PM
3659#endif
3660
3661static const ARMCPRegInfo vapa_cp_reginfo[] = {
3662 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3663 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
3664 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3665 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
3666 .writefn = par_write },
3667#ifndef CONFIG_USER_ONLY
87562e4f 3668 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 3669 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 3670 .access = PL1_W, .accessfn = ats_access,
0710b2fa 3671 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
4a501606 3672#endif
4a501606
PM
3673};
3674
18032bec
PM
3675/* Return basic MPU access permission bits. */
3676static uint32_t simple_mpu_ap_bits(uint32_t val)
3677{
3678 uint32_t ret;
3679 uint32_t mask;
3680 int i;
3681 ret = 0;
3682 mask = 3;
3683 for (i = 0; i < 16; i += 2) {
3684 ret |= (val >> i) & mask;
3685 mask <<= 2;
3686 }
3687 return ret;
3688}
3689
3690/* Pad basic MPU access permission bits to extended format. */
3691static uint32_t extended_mpu_ap_bits(uint32_t val)
3692{
3693 uint32_t ret;
3694 uint32_t mask;
3695 int i;
3696 ret = 0;
3697 mask = 3;
3698 for (i = 0; i < 16; i += 2) {
3699 ret |= (val & mask) << i;
3700 mask <<= 2;
3701 }
3702 return ret;
3703}
3704
c4241c7d
PM
3705static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3706 uint64_t value)
18032bec 3707{
7e09797c 3708 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
3709}
3710
c4241c7d 3711static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3712{
7e09797c 3713 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
3714}
3715
c4241c7d
PM
3716static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3717 uint64_t value)
18032bec 3718{
7e09797c 3719 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
3720}
3721
c4241c7d 3722static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3723{
7e09797c 3724 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
3725}
3726
6cb0b013
PC
3727static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3728{
3729 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3730
3731 if (!u32p) {
3732 return 0;
3733 }
3734
1bc04a88 3735 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
3736 return *u32p;
3737}
3738
3739static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3740 uint64_t value)
3741{
2fc0cc0e 3742 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3743 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3744
3745 if (!u32p) {
3746 return;
3747 }
3748
1bc04a88 3749 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 3750 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
3751 *u32p = value;
3752}
3753
6cb0b013
PC
3754static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3755 uint64_t value)
3756{
2fc0cc0e 3757 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3758 uint32_t nrgs = cpu->pmsav7_dregion;
3759
3760 if (value >= nrgs) {
3761 qemu_log_mask(LOG_GUEST_ERROR,
3762 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3763 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3764 return;
3765 }
3766
3767 raw_write(env, ri, value);
3768}
3769
761c4642
TR
3770static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3771 uint64_t value)
3772{
3773 ARMCPU *cpu = env_archcpu(env);
3774
3775 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3776 env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3777}
3778
3779static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3780{
3781 return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3782}
3783
3784static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3785 uint64_t value)
3786{
3787 ARMCPU *cpu = env_archcpu(env);
3788
3789 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3790 env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3791}
3792
3793static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3794{
3795 return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3796}
3797
3798static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3799 uint64_t value)
3800{
3801 ARMCPU *cpu = env_archcpu(env);
3802
3803 /*
3804 * Ignore writes that would select not implemented region.
3805 * This is architecturally UNPREDICTABLE.
3806 */
3807 if (value >= cpu->pmsav7_dregion) {
3808 return;
3809 }
3810
3811 env->pmsav7.rnr[M_REG_NS] = value;
3812}
3813
3814static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3815 uint64_t value)
3816{
3817 ARMCPU *cpu = env_archcpu(env);
3818
3819 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3820 env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3821}
3822
3823static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3824{
3825 return env->pmsav8.hprbar[env->pmsav8.hprselr];
3826}
3827
3828static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3829 uint64_t value)
3830{
3831 ARMCPU *cpu = env_archcpu(env);
3832
3833 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3834 env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
3835}
3836
3837static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3838{
3839 return env->pmsav8.hprlar[env->pmsav8.hprselr];
3840}
3841
3842static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3843 uint64_t value)
3844{
3845 uint32_t n;
3846 uint32_t bit;
3847 ARMCPU *cpu = env_archcpu(env);
3848
3849 /* Ignore writes to unimplemented regions */
3850 int rmax = MIN(cpu->pmsav8r_hdregion, 32);
3851 value &= MAKE_64BIT_MASK(0, rmax);
3852
3853 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3854
3855 /* Register alias is only valid for first 32 indexes */
3856 for (n = 0; n < rmax; ++n) {
3857 bit = extract32(value, n, 1);
3858 env->pmsav8.hprlar[n] = deposit32(
3859 env->pmsav8.hprlar[n], 0, 1, bit);
3860 }
3861}
3862
3863static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3864{
3865 uint32_t n;
3866 uint32_t result = 0x0;
3867 ARMCPU *cpu = env_archcpu(env);
3868
3869 /* Register alias is only valid for first 32 indexes */
3870 for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
3871 if (env->pmsav8.hprlar[n] & 0x1) {
3872 result |= (0x1 << n);
3873 }
3874 }
3875 return result;
3876}
3877
3878static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3879 uint64_t value)
3880{
3881 ARMCPU *cpu = env_archcpu(env);
3882
3883 /*
3884 * Ignore writes that would select not implemented region.
3885 * This is architecturally UNPREDICTABLE.
3886 */
3887 if (value >= cpu->pmsav8r_hdregion) {
3888 return;
3889 }
3890
3891 env->pmsav8.hprselr = value;
3892}
3893
3894static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
3895 uint64_t value)
3896{
3897 ARMCPU *cpu = env_archcpu(env);
3898 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3899 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3900
3901 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3902
3903 if (ri->opc1 & 4) {
3904 if (index >= cpu->pmsav8r_hdregion) {
3905 return;
3906 }
3907 if (ri->opc2 & 0x1) {
3908 env->pmsav8.hprlar[index] = value;
3909 } else {
3910 env->pmsav8.hprbar[index] = value;
3911 }
3912 } else {
3913 if (index >= cpu->pmsav7_dregion) {
3914 return;
3915 }
3916 if (ri->opc2 & 0x1) {
3917 env->pmsav8.rlar[M_REG_NS][index] = value;
3918 } else {
3919 env->pmsav8.rbar[M_REG_NS][index] = value;
3920 }
3921 }
3922}
3923
3924static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
3925{
3926 ARMCPU *cpu = env_archcpu(env);
3927 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3928 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3929
3930 if (ri->opc1 & 4) {
3931 if (index >= cpu->pmsav8r_hdregion) {
3932 return 0x0;
3933 }
3934 if (ri->opc2 & 0x1) {
3935 return env->pmsav8.hprlar[index];
3936 } else {
3937 return env->pmsav8.hprbar[index];
3938 }
3939 } else {
3940 if (index >= cpu->pmsav7_dregion) {
3941 return 0x0;
3942 }
3943 if (ri->opc2 & 0x1) {
3944 return env->pmsav8.rlar[M_REG_NS][index];
3945 } else {
3946 return env->pmsav8.rbar[M_REG_NS][index];
3947 }
3948 }
3949}
3950
3951static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
3952 { .name = "PRBAR",
3953 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
3954 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3955 .accessfn = access_tvm_trvm,
3956 .readfn = prbar_read, .writefn = prbar_write },
3957 { .name = "PRLAR",
3958 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
3959 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3960 .accessfn = access_tvm_trvm,
3961 .readfn = prlar_read, .writefn = prlar_write },
3962 { .name = "PRSELR", .resetvalue = 0,
3963 .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
3964 .access = PL1_RW, .accessfn = access_tvm_trvm,
3965 .writefn = prselr_write,
3966 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
3967 { .name = "HPRBAR", .resetvalue = 0,
3968 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
3969 .access = PL2_RW, .type = ARM_CP_NO_RAW,
3970 .readfn = hprbar_read, .writefn = hprbar_write },
3971 { .name = "HPRLAR",
3972 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
3973 .access = PL2_RW, .type = ARM_CP_NO_RAW,
3974 .readfn = hprlar_read, .writefn = hprlar_write },
3975 { .name = "HPRSELR", .resetvalue = 0,
3976 .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
3977 .access = PL2_RW,
3978 .writefn = hprselr_write,
3979 .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
3980 { .name = "HPRENR",
3981 .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
3982 .access = PL2_RW, .type = ARM_CP_NO_RAW,
3983 .readfn = hprenr_read, .writefn = hprenr_write },
3984};
3985
6cb0b013 3986static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
9b37a28c
FR
3987 /*
3988 * Reset for all these registers is handled in arm_cpu_reset(),
69ceea64
PM
3989 * because the PMSAv7 is also used by M-profile CPUs, which do
3990 * not register cpregs but still need the state to be reset.
3991 */
6cb0b013
PC
3992 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3993 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3994 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
3995 .readfn = pmsav7_read, .writefn = pmsav7_write,
3996 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3997 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3998 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3999 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
4000 .readfn = pmsav7_read, .writefn = pmsav7_write,
4001 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
4002 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4003 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4004 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
4005 .readfn = pmsav7_read, .writefn = pmsav7_write,
4006 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
4007 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4008 .access = PL1_RW,
1bc04a88 4009 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
4010 .writefn = pmsav7_rgnr_write,
4011 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
4012};
4013
18032bec
PM
4014static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4015 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 4016 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 4017 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
4018 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4019 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 4020 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 4021 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
4022 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4023 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4024 .access = PL1_RW,
7e09797c
PM
4025 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4026 .resetvalue = 0, },
18032bec
PM
4027 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4028 .access = PL1_RW,
7e09797c
PM
4029 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4030 .resetvalue = 0, },
ecce5c3c
PM
4031 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4032 .access = PL1_RW,
4033 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4034 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4035 .access = PL1_RW,
4036 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 4037 /* Protection region base and size registers */
e508a92b
PM
4038 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4039 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4040 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4041 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4042 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4043 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4044 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4045 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4046 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4047 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4048 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4049 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4050 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4051 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4052 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4053 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4054 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4055 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4056 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4057 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4058 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4059 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4060 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4061 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
4062};
4063
cb4a0a34
PM
4064static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4065 uint64_t value)
ecce5c3c 4066{
cb4a0a34 4067 ARMCPU *cpu = env_archcpu(env);
2ebcebe2 4068
e389be16
FA
4069 if (!arm_feature(env, ARM_FEATURE_V8)) {
4070 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
cb4a0a34
PM
4071 /*
4072 * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4073 * using Long-descriptor translation table format
4074 */
e389be16
FA
4075 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4076 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
cb4a0a34
PM
4077 /*
4078 * In an implementation that includes the Security Extensions
e389be16
FA
4079 * TTBCR has additional fields PD0 [4] and PD1 [5] for
4080 * Short-descriptor translation table format.
4081 */
4082 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4083 } else {
4084 value &= TTBCR_N;
4085 }
e42c4db3 4086 }
e389be16 4087
d4e6df63 4088 if (arm_feature(env, ARM_FEATURE_LPAE)) {
9b37a28c
FR
4089 /*
4090 * With LPAE the TTBCR could result in a change of ASID
d4e6df63
PM
4091 * via the TTBCR.A1 bit, so do a TLB flush.
4092 */
d10eb08f 4093 tlb_flush(CPU(cpu));
d4e6df63 4094 }
cb4a0a34 4095 raw_write(env, ri, value);
ecce5c3c
PM
4096}
4097
d06dc933 4098static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
cb2e37df
PM
4099 uint64_t value)
4100{
2fc0cc0e 4101 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 4102
cb2e37df 4103 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 4104 tlb_flush(CPU(cpu));
cb4a0a34 4105 raw_write(env, ri, value);
cb2e37df
PM
4106}
4107
327ed10f
PM
4108static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4109 uint64_t value)
4110{
93f379b0
RH
4111 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
4112 if (cpreg_field_is_64bit(ri) &&
4113 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
2fc0cc0e 4114 ARMCPU *cpu = env_archcpu(env);
d10eb08f 4115 tlb_flush(CPU(cpu));
327ed10f
PM
4116 }
4117 raw_write(env, ri, value);
4118}
4119
ed30da8e
RH
4120static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4121 uint64_t value)
4122{
d06dc933
RH
4123 /*
4124 * If we are running with E2&0 regime, then an ASID is active.
4125 * Flush if that might be changing. Note we're not checking
4126 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4127 * holds the active ASID, only checking the field that might.
4128 */
4129 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4130 (arm_hcr_el2_eff(env) & HCR_E2H)) {
b6ad6062
RDC
4131 uint16_t mask = ARMMMUIdxBit_E20_2 |
4132 ARMMMUIdxBit_E20_2_PAN |
4133 ARMMMUIdxBit_E20_0;
b6ad6062 4134 tlb_flush_by_mmuidx(env_cpu(env), mask);
d06dc933 4135 }
ed30da8e
RH
4136 raw_write(env, ri, value);
4137}
4138
b698e9cf
EI
4139static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4140 uint64_t value)
4141{
2fc0cc0e 4142 ARMCPU *cpu = env_archcpu(env);
b698e9cf
EI
4143 CPUState *cs = CPU(cpu);
4144
97fa9350
RH
4145 /*
4146 * A change in VMID to the stage2 page table (Stage2) invalidates
575a94af 4147 * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
97fa9350 4148 */
00b20ee4 4149 if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
575a94af 4150 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
b698e9cf 4151 }
00b20ee4 4152 raw_write(env, ri, value);
b698e9cf
EI
4153}
4154
8e5d75c9 4155static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 4156 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218 4157 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4a7e2d73 4158 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 4159 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 4160 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
84929218 4161 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
88ca1c2d
FA
4162 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4163 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9 4164 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
84929218 4165 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
8e5d75c9
PC
4166 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4167 offsetof(CPUARMState, cp15.dfar_ns) } },
4168 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4169 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218 4170 .access = PL1_RW, .accessfn = access_tvm_trvm,
b19ed03c 4171 .fgt = FGT_FAR_EL1,
84929218 4172 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
8e5d75c9 4173 .resetvalue = 0, },
8e5d75c9
PC
4174};
4175
4176static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
4177 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4178 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
84929218 4179 .access = PL1_RW, .accessfn = access_tvm_trvm,
b19ed03c 4180 .fgt = FGT_ESR_EL1,
d81c519c 4181 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 4182 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 4183 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
84929218 4184 .access = PL1_RW, .accessfn = access_tvm_trvm,
bd8db7d9 4185 .fgt = FGT_TTBR0_EL1,
84929218 4186 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
4187 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4188 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 4189 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 4190 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
84929218 4191 .access = PL1_RW, .accessfn = access_tvm_trvm,
bd8db7d9 4192 .fgt = FGT_TTBR1_EL1,
84929218 4193 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
4194 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4195 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
4196 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4197 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218 4198 .access = PL1_RW, .accessfn = access_tvm_trvm,
67dd8030 4199 .fgt = FGT_TCR_EL1,
84929218 4200 .writefn = vmsa_tcr_el12_write,
cb4a0a34
PM
4201 .raw_writefn = raw_write,
4202 .resetvalue = 0,
11f136ee 4203 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 4204 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
4205 .access = PL1_RW, .accessfn = access_tvm_trvm,
4206 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
cb4a0a34
PM
4207 .raw_writefn = raw_write,
4208 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4209 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
4210};
4211
9b37a28c
FR
4212/*
4213 * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
ab638a32
RH
4214 * qemu tlbs nor adjusting cached masks.
4215 */
4216static const ARMCPRegInfo ttbcr2_reginfo = {
4217 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
84929218
RH
4218 .access = PL1_RW, .accessfn = access_tvm_trvm,
4219 .type = ARM_CP_ALIAS,
d102058e 4220 .bank_fieldoffsets = {
cb4a0a34
PM
4221 offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4222 offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
d102058e 4223 },
ab638a32
RH
4224};
4225
c4241c7d
PM
4226static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4227 uint64_t value)
1047b9d7
PM
4228{
4229 env->cp15.c15_ticonfig = value & 0xe7;
4230 /* The OS_TYPE bit in this register changes the reported CPUID! */
4231 env->cp15.c0_cpuid = (value & (1 << 5)) ?
4232 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
4233}
4234
c4241c7d
PM
4235static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4236 uint64_t value)
1047b9d7
PM
4237{
4238 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
4239}
4240
c4241c7d
PM
4241static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4242 uint64_t value)
1047b9d7
PM
4243{
4244 /* Wait-for-interrupt (deprecated) */
2fc0cc0e 4245 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
1047b9d7
PM
4246}
4247
c4241c7d
PM
4248static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4249 uint64_t value)
c4804214 4250{
9b37a28c
FR
4251 /*
4252 * On OMAP there are registers indicating the max/min index of dcache lines
c4804214
PM
4253 * containing a dirty line; cache flush operations have to reset these.
4254 */
4255 env->cp15.c15_i_max = 0x000;
4256 env->cp15.c15_i_min = 0xff0;
c4804214
PM
4257}
4258
18032bec
PM
4259static const ARMCPRegInfo omap_cp_reginfo[] = {
4260 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4261 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 4262 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 4263 .resetvalue = 0, },
1047b9d7
PM
4264 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4265 .access = PL1_RW, .type = ARM_CP_NOP },
4266 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4267 .access = PL1_RW,
4268 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4269 .writefn = omap_ticonfig_write },
4270 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4271 .access = PL1_RW,
4272 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4273 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4274 .access = PL1_RW, .resetvalue = 0xff0,
4275 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4276 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4277 .access = PL1_RW,
4278 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4279 .writefn = omap_threadid_write },
4280 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4281 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 4282 .type = ARM_CP_NO_RAW,
1047b9d7 4283 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
9b37a28c
FR
4284 /*
4285 * TODO: Peripheral port remap register:
1047b9d7
PM
4286 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4287 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4288 * when MMU is off.
4289 */
c4804214 4290 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 4291 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 4292 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 4293 .writefn = omap_cachemaint_write },
34f90529
PM
4294 { .name = "C9", .cp = 15, .crn = 9,
4295 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4296 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
4297};
4298
c4241c7d
PM
4299static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4300 uint64_t value)
1047b9d7 4301{
c0f4af17 4302 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
4303}
4304
4305static const ARMCPRegInfo xscale_cp_reginfo[] = {
4306 { .name = "XSCALE_CPAR",
4307 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4308 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4309 .writefn = xscale_cpar_write, },
2771db27
PM
4310 { .name = "XSCALE_AUXCR",
4311 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4312 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4313 .resetvalue = 0, },
9b37a28c
FR
4314 /*
4315 * XScale specific cache-lockdown: since we have no cache we NOP these
3b771579
PM
4316 * and hope the guest does not really rely on cache behaviour.
4317 */
4318 { .name = "XSCALE_LOCK_ICACHE_LINE",
4319 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4320 .access = PL1_W, .type = ARM_CP_NOP },
4321 { .name = "XSCALE_UNLOCK_ICACHE",
4322 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4323 .access = PL1_W, .type = ARM_CP_NOP },
4324 { .name = "XSCALE_DCACHE_LOCK",
4325 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4326 .access = PL1_RW, .type = ARM_CP_NOP },
4327 { .name = "XSCALE_UNLOCK_DCACHE",
4328 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4329 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
4330};
4331
4332static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
9b37a28c
FR
4333 /*
4334 * RAZ/WI the whole crn=15 space, when we don't have a more specific
1047b9d7
PM
4335 * implementation of this implementation-defined space.
4336 * Ideally this should eventually disappear in favour of actually
4337 * implementing the correct behaviour for all cores.
4338 */
4339 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4340 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 4341 .access = PL1_RW,
7a0e58fa 4342 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 4343 .resetvalue = 0 },
18032bec
PM
4344};
4345
c4804214
PM
4346static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4347 /* Cache status: RAZ because we have no cache so it's always clean */
4348 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 4349 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4350 .resetvalue = 0 },
c4804214
PM
4351};
4352
4353static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
a07d9df0 4354 /* We never have a block transfer operation in progress */
c4804214 4355 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 4356 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4357 .resetvalue = 0 },
30b05bba
PM
4358 /* The cache ops themselves: these all NOP for QEMU */
4359 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
04215eb1 4360 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4361 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
04215eb1 4362 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4363 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
04215eb1 4364 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4365 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
04215eb1 4366 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4367 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
04215eb1 4368 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4369 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
04215eb1 4370 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
c4804214
PM
4371};
4372
4373static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
9b37a28c
FR
4374 /*
4375 * The cache test-and-clean instructions always return (1 << 30)
c4804214
PM
4376 * to indicate that there are no dirty cache lines.
4377 */
4378 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 4379 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4380 .resetvalue = (1 << 30) },
c4804214 4381 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 4382 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4383 .resetvalue = (1 << 30) },
c4804214
PM
4384};
4385
34f90529
PM
4386static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4387 /* Ignore ReadBuffer accesses */
4388 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4389 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 4390 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 4391 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
4392};
4393
731de9e6
EI
4394static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4395{
731de9e6 4396 unsigned int cur_el = arm_current_el(env);
731de9e6 4397
e6ef0169 4398 if (arm_is_el2_enabled(env) && cur_el == 1) {
731de9e6
EI
4399 return env->cp15.vpidr_el2;
4400 }
4401 return raw_read(env, ri);
4402}
4403
06a7e647 4404static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 4405{
2fc0cc0e 4406 ARMCPU *cpu = env_archcpu(env);
eb5e1d3c
PF
4407 uint64_t mpidr = cpu->mp_affinity;
4408
81bdde9d 4409 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 4410 mpidr |= (1U << 31);
9b37a28c
FR
4411 /*
4412 * Cores which are uniprocessor (non-coherent)
81bdde9d 4413 * but still implement the MP extensions set
a8e81b31 4414 * bit 30. (For instance, Cortex-R5).
81bdde9d 4415 */
a8e81b31
PC
4416 if (cpu->mp_is_up) {
4417 mpidr |= (1u << 30);
4418 }
81bdde9d 4419 }
c4241c7d 4420 return mpidr;
81bdde9d
PM
4421}
4422
06a7e647
EI
4423static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4424{
f0d574d6 4425 unsigned int cur_el = arm_current_el(env);
f0d574d6 4426
e6ef0169 4427 if (arm_is_el2_enabled(env) && cur_el == 1) {
f0d574d6
EI
4428 return env->cp15.vmpidr_el2;
4429 }
06a7e647
EI
4430 return mpidr_read_val(env);
4431}
4432
7ac681cf 4433static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 4434 /* NOP AMAIR0/1 */
b0fe2427
PM
4435 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4436 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
84929218 4437 .access = PL1_RW, .accessfn = access_tvm_trvm,
158c276c 4438 .fgt = FGT_AMAIR_EL1,
84929218 4439 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427 4440 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 4441 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
84929218
RH
4442 .access = PL1_RW, .accessfn = access_tvm_trvm,
4443 .type = ARM_CP_CONST, .resetvalue = 0 },
891a2fe7 4444 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
4445 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4446 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4447 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 4448 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
84929218
RH
4449 .access = PL1_RW, .accessfn = access_tvm_trvm,
4450 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4451 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4452 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 4453 .writefn = vmsa_ttbr_write, },
891a2fe7 4454 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
84929218
RH
4455 .access = PL1_RW, .accessfn = access_tvm_trvm,
4456 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4457 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4458 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 4459 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
4460};
4461
c4241c7d 4462static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4463{
c4241c7d 4464 return vfp_get_fpcr(env);
b0d2b7d0
PM
4465}
4466
c4241c7d
PM
4467static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4468 uint64_t value)
b0d2b7d0
PM
4469{
4470 vfp_set_fpcr(env, value);
b0d2b7d0
PM
4471}
4472
c4241c7d 4473static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4474{
c4241c7d 4475 return vfp_get_fpsr(env);
b0d2b7d0
PM
4476}
4477
c4241c7d
PM
4478static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4479 uint64_t value)
b0d2b7d0
PM
4480{
4481 vfp_set_fpsr(env, value);
b0d2b7d0
PM
4482}
4483
3f208fd7
PM
4484static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4485 bool isread)
c2b820fe 4486{
aaec1432 4487 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
c2b820fe
PM
4488 return CP_ACCESS_TRAP;
4489 }
4490 return CP_ACCESS_OK;
4491}
4492
4493static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4494 uint64_t value)
4495{
4496 env->daif = value & PSTATE_DAIF;
4497}
4498
220f508f
RH
4499static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4500{
4501 return env->pstate & PSTATE_PAN;
4502}
4503
4504static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4505 uint64_t value)
4506{
4507 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4508}
4509
4510static const ARMCPRegInfo pan_reginfo = {
4511 .name = "PAN", .state = ARM_CP_STATE_AA64,
4512 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4513 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4514 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4515};
4516
9eeb7a1c
RH
4517static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4518{
4519 return env->pstate & PSTATE_UAO;
4520}
4521
4522static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4523 uint64_t value)
4524{
4525 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4526}
4527
4528static const ARMCPRegInfo uao_reginfo = {
4529 .name = "UAO", .state = ARM_CP_STATE_AA64,
4530 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4531 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4532 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4533};
4534
dc8b1853
RC
4535static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4536{
4537 return env->pstate & PSTATE_DIT;
4538}
4539
4540static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4541 uint64_t value)
4542{
4543 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4544}
4545
4546static const ARMCPRegInfo dit_reginfo = {
4547 .name = "DIT", .state = ARM_CP_STATE_AA64,
4548 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4549 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4550 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4551};
4552
f2f68a78
RC
4553static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4554{
4555 return env->pstate & PSTATE_SSBS;
4556}
4557
4558static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4559 uint64_t value)
4560{
4561 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4562}
4563
4564static const ARMCPRegInfo ssbs_reginfo = {
4565 .name = "SSBS", .state = ARM_CP_STATE_AA64,
4566 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4567 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4568 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4569};
4570
38262d8a
RH
4571static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4572 const ARMCPRegInfo *ri,
4573 bool isread)
8af35c37 4574{
38262d8a
RH
4575 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4576 switch (arm_current_el(env)) {
4577 case 0:
4578 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4579 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4580 return CP_ACCESS_TRAP;
4581 }
4582 /* fall through */
4583 case 1:
4584 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4585 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4586 return CP_ACCESS_TRAP_EL2;
4587 }
4588 break;
8af35c37
PM
4589 }
4590 return CP_ACCESS_OK;
4591}
4592
2d3ce4c6 4593static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
1bed4d2e 4594{
38262d8a 4595 /* Cache invalidate/clean to Point of Unification... */
1bed4d2e
RH
4596 switch (arm_current_el(env)) {
4597 case 0:
4598 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4599 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4600 return CP_ACCESS_TRAP;
4601 }
4602 /* fall through */
4603 case 1:
2d3ce4c6
PM
4604 /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set. */
4605 if (arm_hcr_el2_eff(env) & hcrflags) {
1bed4d2e
RH
4606 return CP_ACCESS_TRAP_EL2;
4607 }
4608 break;
4609 }
4610 return CP_ACCESS_OK;
4611}
4612
2d3ce4c6
PM
4613static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4614 bool isread)
4615{
4616 return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4617}
4618
4619static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4620 bool isread)
4621{
4622 return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4623}
4624
9b37a28c
FR
4625/*
4626 * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
dbb1fb27
AB
4627 * Page D4-1736 (DDI0487A.b)
4628 */
4629
b7e0730d
RH
4630static int vae1_tlbmask(CPUARMState *env)
4631{
e04a5752 4632 uint64_t hcr = arm_hcr_el2_eff(env);
bc944d3a 4633 uint16_t mask;
e04a5752
RDC
4634
4635 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
bc944d3a
RDC
4636 mask = ARMMMUIdxBit_E20_2 |
4637 ARMMMUIdxBit_E20_2_PAN |
4638 ARMMMUIdxBit_E20_0;
b7e0730d 4639 } else {
bc944d3a 4640 mask = ARMMMUIdxBit_E10_1 |
452ef8cb
RH
4641 ARMMMUIdxBit_E10_1_PAN |
4642 ARMMMUIdxBit_E10_0;
b7e0730d 4643 }
bc944d3a 4644 return mask;
b7e0730d
RH
4645}
4646
ea04dce7
RH
4647/* Return 56 if TBI is enabled, 64 otherwise. */
4648static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4649 uint64_t addr)
4650{
c1547bba 4651 uint64_t tcr = regime_tcr(env, mmu_idx);
ea04dce7
RH
4652 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4653 int select = extract64(addr, 55, 1);
4654
4655 return (tbi >> select) & 1 ? 56 : 64;
4656}
4657
4658static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4659{
b6ad6062 4660 uint64_t hcr = arm_hcr_el2_eff(env);
ea04dce7
RH
4661 ARMMMUIdx mmu_idx;
4662
4663 /* Only the regime of the mmu_idx below is significant. */
b6ad6062 4664 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
ea04dce7
RH
4665 mmu_idx = ARMMMUIdx_E20_0;
4666 } else {
4667 mmu_idx = ARMMMUIdx_E10_0;
4668 }
b6ad6062 4669
ea04dce7
RH
4670 return tlbbits_for_regime(env, mmu_idx, addr);
4671}
4672
fd3ed969
PM
4673static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4674 uint64_t value)
168aa23b 4675{
29a0af61 4676 CPUState *cs = env_cpu(env);
b7e0730d 4677 int mask = vae1_tlbmask(env);
dbb1fb27 4678
b7e0730d 4679 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
168aa23b
PM
4680}
4681
b4ab8ce9
PM
4682static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4683 uint64_t value)
4684{
29a0af61 4685 CPUState *cs = env_cpu(env);
b7e0730d 4686 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4687
4688 if (tlb_force_broadcast(env)) {
527db2be
RH
4689 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4690 } else {
4691 tlb_flush_by_mmuidx(cs, mask);
b4ab8ce9 4692 }
b4ab8ce9
PM
4693}
4694
85d0dc9f
RH
4695static int e2_tlbmask(CPUARMState *env)
4696{
d902ae75
RH
4697 return (ARMMMUIdxBit_E20_0 |
4698 ARMMMUIdxBit_E20_2 |
4699 ARMMMUIdxBit_E20_2_PAN |
4700 ARMMMUIdxBit_E2);
85d0dc9f
RH
4701}
4702
90c19cdf
RH
4703static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4704 uint64_t value)
4705{
4706 CPUState *cs = env_cpu(env);
4707 int mask = alle1_tlbmask(env);
4708
4709 tlb_flush_by_mmuidx(cs, mask);
4710}
4711
fd3ed969 4712static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
4713 uint64_t value)
4714{
85d0dc9f
RH
4715 CPUState *cs = env_cpu(env);
4716 int mask = e2_tlbmask(env);
fd3ed969 4717
85d0dc9f 4718 tlb_flush_by_mmuidx(cs, mask);
fd3ed969
PM
4719}
4720
43efaa33
PM
4721static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4722 uint64_t value)
4723{
2fc0cc0e 4724 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4725 CPUState *cs = CPU(cpu);
4726
d902ae75 4727 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
43efaa33
PM
4728}
4729
fd3ed969
PM
4730static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4731 uint64_t value)
4732{
29a0af61 4733 CPUState *cs = env_cpu(env);
90c19cdf
RH
4734 int mask = alle1_tlbmask(env);
4735
4736 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
fa439fc5
PM
4737}
4738
2bfb9d75
PM
4739static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4740 uint64_t value)
4741{
29a0af61 4742 CPUState *cs = env_cpu(env);
85d0dc9f 4743 int mask = e2_tlbmask(env);
2bfb9d75 4744
85d0dc9f 4745 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
2bfb9d75
PM
4746}
4747
43efaa33
PM
4748static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4749 uint64_t value)
4750{
29a0af61 4751 CPUState *cs = env_cpu(env);
43efaa33 4752
d902ae75 4753 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
43efaa33
PM
4754}
4755
fd3ed969
PM
4756static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4757 uint64_t value)
fa439fc5 4758{
9b37a28c
FR
4759 /*
4760 * Invalidate by VA, EL2
fd3ed969
PM
4761 * Currently handles both VAE2 and VALE2, since we don't support
4762 * flush-last-level-only.
4763 */
85d0dc9f
RH
4764 CPUState *cs = env_cpu(env);
4765 int mask = e2_tlbmask(env);
fd3ed969
PM
4766 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4767
85d0dc9f 4768 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
fd3ed969
PM
4769}
4770
43efaa33
PM
4771static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4772 uint64_t value)
4773{
9b37a28c
FR
4774 /*
4775 * Invalidate by VA, EL3
43efaa33
PM
4776 * Currently handles both VAE3 and VALE3, since we don't support
4777 * flush-last-level-only.
4778 */
2fc0cc0e 4779 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4780 CPUState *cs = CPU(cpu);
4781 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4782
d902ae75 4783 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
43efaa33
PM
4784}
4785
fd3ed969
PM
4786static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4787 uint64_t value)
4788{
90c19cdf
RH
4789 CPUState *cs = env_cpu(env);
4790 int mask = vae1_tlbmask(env);
fa439fc5 4791 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4792 int bits = vae1_tlbbits(env, pageaddr);
fa439fc5 4793
ea04dce7 4794 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
fa439fc5
PM
4795}
4796
b4ab8ce9
PM
4797static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4798 uint64_t value)
4799{
9b37a28c
FR
4800 /*
4801 * Invalidate by VA, EL1&0 (AArch64 version).
b4ab8ce9
PM
4802 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4803 * since we don't support flush-for-specific-ASID-only or
4804 * flush-last-level-only.
4805 */
90c19cdf
RH
4806 CPUState *cs = env_cpu(env);
4807 int mask = vae1_tlbmask(env);
b4ab8ce9 4808 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4809 int bits = vae1_tlbbits(env, pageaddr);
b4ab8ce9
PM
4810
4811 if (tlb_force_broadcast(env)) {
ea04dce7 4812 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
527db2be 4813 } else {
ea04dce7 4814 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
b4ab8ce9 4815 }
b4ab8ce9
PM
4816}
4817
fd3ed969
PM
4818static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4819 uint64_t value)
fa439fc5 4820{
29a0af61 4821 CPUState *cs = env_cpu(env);
fd3ed969 4822 uint64_t pageaddr = sextract64(value << 12, 0, 56);
d902ae75 4823 int bits = tlbbits_for_regime(env, ARMMMUIdx_E2, pageaddr);
fa439fc5 4824
d902ae75
RH
4825 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4826 ARMMMUIdxBit_E2, bits);
fa439fc5
PM
4827}
4828
43efaa33
PM
4829static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4830 uint64_t value)
4831{
29a0af61 4832 CPUState *cs = env_cpu(env);
43efaa33 4833 uint64_t pageaddr = sextract64(value << 12, 0, 56);
d902ae75 4834 int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
43efaa33 4835
ea04dce7 4836 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
d902ae75 4837 ARMMMUIdxBit_E3, bits);
43efaa33
PM
4838}
4839
575a94af
RH
4840static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
4841{
4842 /*
4843 * The MSB of value is the NS field, which only applies if SEL2
4844 * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
4845 */
4846 return (value >= 0
4847 && cpu_isar_feature(aa64_sel2, env_archcpu(env))
4848 && arm_is_secure_below_el3(env)
4849 ? ARMMMUIdxBit_Stage2_S
4850 : ARMMMUIdxBit_Stage2);
4851}
4852
4853static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4854 uint64_t value)
4855{
4856 CPUState *cs = env_cpu(env);
4857 int mask = ipas2e1_tlbmask(env, value);
4858 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4859
4860 if (tlb_force_broadcast(env)) {
4861 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4862 } else {
4863 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4864 }
4865}
4866
4867static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4868 uint64_t value)
4869{
4870 CPUState *cs = env_cpu(env);
4871 int mask = ipas2e1_tlbmask(env, value);
4872 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4873
4874 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4875}
4876
84940ed8 4877#ifdef TARGET_AARCH64
ab1cdb47
RH
4878typedef struct {
4879 uint64_t base;
84940ed8 4880 uint64_t length;
ab1cdb47
RH
4881} TLBIRange;
4882
3c003f70
PM
4883static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
4884{
4885 /*
4886 * Note that the TLBI range TG field encoding differs from both
4887 * TG0 and TG1 encodings.
4888 */
4889 switch (tg) {
4890 case 1:
4891 return Gran4K;
4892 case 2:
4893 return Gran16K;
4894 case 3:
4895 return Gran64K;
4896 default:
4897 return GranInvalid;
4898 }
4899}
4900
ab1cdb47
RH
4901static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4902 uint64_t value)
4903{
4904 unsigned int page_size_granule, page_shift, num, scale, exponent;
3974ff93
RH
4905 /* Extract one bit to represent the va selector in use. */
4906 uint64_t select = sextract64(value, 36, 1);
4907 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
ab1cdb47 4908 TLBIRange ret = { };
3c003f70 4909 ARMGranuleSize gran;
84940ed8 4910
84940ed8 4911 page_size_granule = extract64(value, 46, 2);
3c003f70 4912 gran = tlbi_range_tg_to_gran_size(page_size_granule);
84940ed8 4913
3974ff93 4914 /* The granule encoded in value must match the granule in use. */
3c003f70 4915 if (gran != param.gran) {
3974ff93 4916 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
84940ed8 4917 page_size_granule);
ab1cdb47 4918 return ret;
84940ed8
RC
4919 }
4920
3c003f70 4921 page_shift = arm_granule_bits(gran);
ab1cdb47
RH
4922 num = extract64(value, 39, 5);
4923 scale = extract64(value, 44, 2);
84940ed8 4924 exponent = (5 * scale) + 1;
84940ed8 4925
ab1cdb47 4926 ret.length = (num + 1) << (exponent + page_shift);
84940ed8 4927
3974ff93 4928 if (param.select) {
d976de21 4929 ret.base = sextract64(value, 0, 37);
84940ed8 4930 } else {
d976de21 4931 ret.base = extract64(value, 0, 37);
84940ed8 4932 }
ef56c242
RH
4933 if (param.ds) {
4934 /*
4935 * With DS=1, BaseADDR is always shifted 16 so that it is able
4936 * to address all 52 va bits. The input address is perforce
4937 * aligned on a 64k boundary regardless of translation granule.
4938 */
4939 page_shift = 16;
4940 }
d976de21 4941 ret.base <<= page_shift;
84940ed8 4942
ab1cdb47 4943 return ret;
84940ed8
RC
4944}
4945
4946static void do_rvae_write(CPUARMState *env, uint64_t value,
4947 int idxmap, bool synced)
4948{
4949 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
ab1cdb47 4950 TLBIRange range;
84940ed8
RC
4951 int bits;
4952
ab1cdb47
RH
4953 range = tlbi_aa64_get_range(env, one_idx, value);
4954 bits = tlbbits_for_regime(env, one_idx, range.base);
84940ed8
RC
4955
4956 if (synced) {
4957 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
ab1cdb47
RH
4958 range.base,
4959 range.length,
84940ed8
RC
4960 idxmap,
4961 bits);
4962 } else {
ab1cdb47
RH
4963 tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4964 range.length, idxmap, bits);
84940ed8
RC
4965 }
4966}
4967
4968static void tlbi_aa64_rvae1_write(CPUARMState *env,
4969 const ARMCPRegInfo *ri,
4970 uint64_t value)
4971{
4972 /*
4973 * Invalidate by VA range, EL1&0.
4974 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4975 * since we don't support flush-for-specific-ASID-only or
4976 * flush-last-level-only.
4977 */
4978
4979 do_rvae_write(env, value, vae1_tlbmask(env),
4980 tlb_force_broadcast(env));
4981}
4982
4983static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4984 const ARMCPRegInfo *ri,
4985 uint64_t value)
4986{
4987 /*
4988 * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4989 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4990 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4991 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4992 * shareable specific flushes.
4993 */
4994
4995 do_rvae_write(env, value, vae1_tlbmask(env), true);
4996}
4997
4998static int vae2_tlbmask(CPUARMState *env)
4999{
d902ae75 5000 return ARMMMUIdxBit_E2;
84940ed8
RC
5001}
5002
5003static void tlbi_aa64_rvae2_write(CPUARMState *env,
5004 const ARMCPRegInfo *ri,
5005 uint64_t value)
5006{
5007 /*
5008 * Invalidate by VA range, EL2.
5009 * Currently handles all of RVAE2 and RVALE2,
5010 * since we don't support flush-for-specific-ASID-only or
5011 * flush-last-level-only.
5012 */
5013
5014 do_rvae_write(env, value, vae2_tlbmask(env),
5015 tlb_force_broadcast(env));
5016
5017
5018}
5019
5020static void tlbi_aa64_rvae2is_write(CPUARMState *env,
5021 const ARMCPRegInfo *ri,
5022 uint64_t value)
5023{
5024 /*
5025 * Invalidate by VA range, Inner/Outer Shareable, EL2.
5026 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
5027 * since we don't support flush-for-specific-ASID-only,
5028 * flush-last-level-only or inner/outer shareable specific flushes.
5029 */
5030
5031 do_rvae_write(env, value, vae2_tlbmask(env), true);
5032
5033}
5034
5035static void tlbi_aa64_rvae3_write(CPUARMState *env,
5036 const ARMCPRegInfo *ri,
5037 uint64_t value)
5038{
5039 /*
5040 * Invalidate by VA range, EL3.
5041 * Currently handles all of RVAE3 and RVALE3,
5042 * since we don't support flush-for-specific-ASID-only or
5043 * flush-last-level-only.
5044 */
5045
d902ae75 5046 do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
84940ed8
RC
5047}
5048
5049static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5050 const ARMCPRegInfo *ri,
5051 uint64_t value)
5052{
5053 /*
5054 * Invalidate by VA range, EL3, Inner/Outer Shareable.
5055 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5056 * since we don't support flush-for-specific-ASID-only,
5057 * flush-last-level-only or inner/outer specific flushes.
5058 */
5059
d902ae75 5060 do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
84940ed8 5061}
575a94af
RH
5062
5063static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5064 uint64_t value)
5065{
5066 do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5067 tlb_force_broadcast(env));
5068}
5069
5070static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5071 const ARMCPRegInfo *ri,
5072 uint64_t value)
5073{
5074 do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5075}
84940ed8
RC
5076#endif
5077
3f208fd7
PM
5078static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5079 bool isread)
aca3f40b 5080{
4351cb72
RH
5081 int cur_el = arm_current_el(env);
5082
5083 if (cur_el < 2) {
5084 uint64_t hcr = arm_hcr_el2_eff(env);
5085
5086 if (cur_el == 0) {
5087 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5088 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5089 return CP_ACCESS_TRAP_EL2;
5090 }
5091 } else {
5092 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5093 return CP_ACCESS_TRAP;
5094 }
5095 if (hcr & HCR_TDZ) {
5096 return CP_ACCESS_TRAP_EL2;
5097 }
5098 }
5099 } else if (hcr & HCR_TDZ) {
5100 return CP_ACCESS_TRAP_EL2;
5101 }
aca3f40b
PM
5102 }
5103 return CP_ACCESS_OK;
5104}
5105
5106static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5107{
2fc0cc0e 5108 ARMCPU *cpu = env_archcpu(env);
aca3f40b
PM
5109 int dzp_bit = 1 << 4;
5110
5111 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 5112 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
5113 dzp_bit = 0;
5114 }
5115 return cpu->dcz_blocksize | dzp_bit;
5116}
5117
3f208fd7
PM
5118static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5119 bool isread)
f502cfc2 5120{
cdcf1405 5121 if (!(env->pstate & PSTATE_SP)) {
9b37a28c
FR
5122 /*
5123 * Access to SP_EL0 is undefined if it's being used as
f502cfc2
PM
5124 * the stack pointer.
5125 */
5126 return CP_ACCESS_TRAP_UNCATEGORIZED;
5127 }
5128 return CP_ACCESS_OK;
5129}
5130
5131static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5132{
5133 return env->pstate & PSTATE_SP;
5134}
5135
5136static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5137{
5138 update_spsel(env, val);
5139}
5140
137feaa9
FA
5141static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5142 uint64_t value)
5143{
2fc0cc0e 5144 ARMCPU *cpu = env_archcpu(env);
137feaa9 5145
f00faf13
RH
5146 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5147 /* M bit is RAZ/WI for PMSA with no MPU implemented */
5148 value &= ~SCTLR_M;
5149 }
5150
5151 /* ??? Lots of these bits are not implemented. */
5152
5153 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5154 if (ri->opc1 == 6) { /* SCTLR_EL3 */
5155 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5156 } else {
5157 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5158 SCTLR_ATA0 | SCTLR_ATA);
5159 }
5160 }
5161
137feaa9 5162 if (raw_read(env, ri) == value) {
9b37a28c
FR
5163 /*
5164 * Skip the TLB flush if nothing actually changed; Linux likes
137feaa9
FA
5165 * to do a lot of pointless SCTLR writes.
5166 */
5167 return;
5168 }
5169
5170 raw_write(env, ri, value);
f00faf13 5171
137feaa9 5172 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 5173 tlb_flush(CPU(cpu));
2e5dcf36
RH
5174
5175 if (ri->type & ARM_CP_SUPPRESS_TB_END) {
5176 /*
5177 * Normally we would always end the TB on an SCTLR write; see the
5178 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5179 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5180 * of hflags from the translator, so do it here.
5181 */
5182 arm_rebuild_hflags(env);
5183 }
137feaa9
FA
5184}
5185
80d2b43b
PM
5186static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5187 uint64_t value)
a8d64e73 5188{
01765386
PM
5189 /*
5190 * Some MDCR_EL3 bits affect whether PMU counters are running:
5191 * if we are trying to change any of those then we must
5192 * bracket this update with PMU start/finish calls.
5193 */
5194 bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5195
5196 if (pmu_op) {
5197 pmu_op_start(env);
5198 }
80d2b43b 5199 env->cp15.mdcr_el3 = value;
01765386
PM
5200 if (pmu_op) {
5201 pmu_op_finish(env);
5202 }
5203}
5204
80d2b43b
PM
5205static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5206 uint64_t value)
5207{
5208 /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5209 mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5210}
5211
01765386
PM
5212static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5213 uint64_t value)
5214{
5215 /*
5216 * Some MDCR_EL2 bits affect whether PMU counters are running:
5217 * if we are trying to change any of those then we must
5218 * bracket this update with PMU start/finish calls.
5219 */
5220 bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5221
5222 if (pmu_op) {
5223 pmu_op_start(env);
5224 }
5225 env->cp15.mdcr_el2 = value;
5226 if (pmu_op) {
5227 pmu_op_finish(env);
5228 }
a8d64e73
PM
5229}
5230
b0d2b7d0 5231static const ARMCPRegInfo v8_cp_reginfo[] = {
9b37a28c
FR
5232 /*
5233 * Minimal set of EL0-visible registers. This will need to be expanded
b0d2b7d0
PM
5234 * significantly for system emulation of AArch64 CPUs.
5235 */
5236 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5237 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5238 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
5239 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5240 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 5241 .type = ARM_CP_NO_RAW,
c2b820fe
PM
5242 .access = PL0_RW, .accessfn = aa64_daif_access,
5243 .fieldoffset = offsetof(CPUARMState, daif),
5244 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
5245 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5246 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 5247 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 5248 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
5249 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5250 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 5251 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 5252 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
5253 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5254 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 5255 .access = PL0_R, .type = ARM_CP_NO_RAW,
b19ed03c 5256 .fgt = FGT_DCZID_EL0,
aca3f40b
PM
5257 .readfn = aa64_dczid_read },
5258 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5259 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5260 .access = PL0_W, .type = ARM_CP_DC_ZVA,
5261#ifndef CONFIG_USER_ONLY
5262 /* Avoid overhead of an access check that always passes in user-mode */
5263 .accessfn = aa64_zva_access,
dd345653 5264 .fgt = FGT_DCZVA,
aca3f40b
PM
5265#endif
5266 },
0eef9d98
PM
5267 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5268 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5269 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
5270 /* Cache ops: all NOPs since we don't emulate caches */
5271 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5272 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a 5273 .access = PL1_W, .type = ARM_CP_NOP,
dd345653 5274 .fgt = FGT_ICIALLUIS,
2d3ce4c6 5275 .accessfn = access_ticab },
8af35c37
PM
5276 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5277 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a 5278 .access = PL1_W, .type = ARM_CP_NOP,
dd345653 5279 .fgt = FGT_ICIALLU,
2d3ce4c6 5280 .accessfn = access_tocu },
8af35c37
PM
5281 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5282 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5283 .access = PL0_W, .type = ARM_CP_NOP,
dd345653 5284 .fgt = FGT_ICIVAU,
2d3ce4c6 5285 .accessfn = access_tocu },
8af35c37
PM
5286 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5287 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e 5288 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
dd345653 5289 .fgt = FGT_DCIVAC,
1bed4d2e 5290 .type = ARM_CP_NOP },
8af35c37
PM
5291 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5292 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
dd345653 5293 .fgt = FGT_DCISW,
1803d271 5294 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
5295 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5296 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5297 .access = PL0_W, .type = ARM_CP_NOP,
1bed4d2e 5298 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
5299 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5300 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
dd345653 5301 .fgt = FGT_DCCSW,
1803d271 5302 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
5303 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5304 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5305 .access = PL0_W, .type = ARM_CP_NOP,
dd345653 5306 .fgt = FGT_DCCVAU,
2d3ce4c6 5307 .accessfn = access_tocu },
8af35c37
PM
5308 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5309 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5310 .access = PL0_W, .type = ARM_CP_NOP,
dd345653 5311 .fgt = FGT_DCCIVAC,
1bed4d2e 5312 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
5313 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5314 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
dd345653 5315 .fgt = FGT_DCCISW,
1803d271 5316 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
168aa23b
PM
5317 /* TLBI operations */
5318 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5319 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
0f66d223 5320 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5321 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 5322 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5323 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
0f66d223 5324 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5325 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5326 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5327 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
0f66d223 5328 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5329 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 5330 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5331 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
0f66d223 5332 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5333 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5334 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5335 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
0f66d223 5336 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5337 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5338 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5339 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
0f66d223 5340 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
fd3ed969 5341 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5342 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5343 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73 5344 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5345 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 5346 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5347 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73 5348 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5349 .writefn = tlbi_aa64_vae1_write },
168aa23b 5350 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5351 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73 5352 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5353 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 5354 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5355 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73 5356 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5357 .writefn = tlbi_aa64_vae1_write },
168aa23b 5358 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5359 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73 5360 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5361 .writefn = tlbi_aa64_vae1_write },
168aa23b 5362 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5363 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73 5364 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
fd3ed969 5365 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
5366 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5367 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
575a94af
RH
5368 .access = PL2_W, .type = ARM_CP_NO_RAW,
5369 .writefn = tlbi_aa64_ipas2e1is_write },
cea66e91
PM
5370 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5371 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
575a94af
RH
5372 .access = PL2_W, .type = ARM_CP_NO_RAW,
5373 .writefn = tlbi_aa64_ipas2e1is_write },
83ddf975
PM
5374 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5375 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5376 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 5377 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
5378 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5379 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5380 .access = PL2_W, .type = ARM_CP_NO_RAW,
5381 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
5382 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5383 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
575a94af
RH
5384 .access = PL2_W, .type = ARM_CP_NO_RAW,
5385 .writefn = tlbi_aa64_ipas2e1_write },
cea66e91
PM
5386 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5387 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
575a94af
RH
5388 .access = PL2_W, .type = ARM_CP_NO_RAW,
5389 .writefn = tlbi_aa64_ipas2e1_write },
83ddf975
PM
5390 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5391 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5392 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 5393 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
5394 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5395 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5396 .access = PL2_W, .type = ARM_CP_NO_RAW,
5397 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
5398#ifndef CONFIG_USER_ONLY
5399 /* 64 bit address translation operations */
5400 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5401 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
5402 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5403 .writefn = ats_write64 },
19525524
PM
5404 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5405 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
5406 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5407 .writefn = ats_write64 },
19525524
PM
5408 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5409 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
0710b2fa
PM
5410 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5411 .writefn = ats_write64 },
19525524
PM
5412 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5413 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
0710b2fa
PM
5414 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5415 .writefn = ats_write64 },
2a47df95 5416 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 5417 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
0710b2fa
PM
5418 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5419 .writefn = ats_write64 },
2a47df95 5420 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 5421 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
0710b2fa
PM
5422 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5423 .writefn = ats_write64 },
2a47df95 5424 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 5425 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
0710b2fa
PM
5426 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5427 .writefn = ats_write64 },
2a47df95 5428 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 5429 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
0710b2fa
PM
5430 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5431 .writefn = ats_write64 },
2a47df95
PM
5432 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5433 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5434 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
5435 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5436 .writefn = ats_write64 },
2a47df95
PM
5437 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5438 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
5439 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5440 .writefn = ats_write64 },
c96fc9b5
EI
5441 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5442 .type = ARM_CP_ALIAS,
5443 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5444 .access = PL1_RW, .resetvalue = 0,
67dd8030 5445 .fgt = FGT_PAR_EL1,
c96fc9b5
EI
5446 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5447 .writefn = par_write },
19525524 5448#endif
995939a6 5449 /* TLB invalidate last level of translation table walk */
9449fdf6 5450 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
0f66d223 5451 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
30881b73 5452 .writefn = tlbimva_is_write },
9449fdf6 5453 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
0f66d223 5454 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
fa439fc5 5455 .writefn = tlbimvaa_is_write },
9449fdf6 5456 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73
RH
5457 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5458 .writefn = tlbimva_write },
9449fdf6 5459 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73
RH
5460 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5461 .writefn = tlbimvaa_write },
541ef8c2
SS
5462 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5463 .type = ARM_CP_NO_RAW, .access = PL2_W,
5464 .writefn = tlbimva_hyp_write },
5465 { .name = "TLBIMVALHIS",
5466 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5467 .type = ARM_CP_NO_RAW, .access = PL2_W,
5468 .writefn = tlbimva_hyp_is_write },
5469 { .name = "TLBIIPAS2",
5470 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
575a94af
RH
5471 .type = ARM_CP_NO_RAW, .access = PL2_W,
5472 .writefn = tlbiipas2_hyp_write },
541ef8c2
SS
5473 { .name = "TLBIIPAS2IS",
5474 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
575a94af
RH
5475 .type = ARM_CP_NO_RAW, .access = PL2_W,
5476 .writefn = tlbiipas2is_hyp_write },
541ef8c2
SS
5477 { .name = "TLBIIPAS2L",
5478 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
575a94af
RH
5479 .type = ARM_CP_NO_RAW, .access = PL2_W,
5480 .writefn = tlbiipas2_hyp_write },
541ef8c2
SS
5481 { .name = "TLBIIPAS2LIS",
5482 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
575a94af
RH
5483 .type = ARM_CP_NO_RAW, .access = PL2_W,
5484 .writefn = tlbiipas2is_hyp_write },
9449fdf6
PM
5485 /* 32 bit cache operations */
5486 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
2d3ce4c6 5487 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
9449fdf6
PM
5488 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5489 .type = ARM_CP_NOP, .access = PL1_W },
5490 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
2d3ce4c6 5491 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
9449fdf6 5492 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
2d3ce4c6 5493 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
9449fdf6
PM
5494 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5495 .type = ARM_CP_NOP, .access = PL1_W },
5496 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5497 .type = ARM_CP_NOP, .access = PL1_W },
5498 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e 5499 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5500 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 5501 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5502 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
1bed4d2e 5503 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5504 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 5505 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5506 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
2d3ce4c6 5507 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
9449fdf6 5508 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
1bed4d2e 5509 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5510 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 5511 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5512 /* MMU Domain access control / MPU write buffer control */
0c17d68c 5513 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
84929218 5514 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
5515 .writefn = dacr_write, .raw_writefn = raw_write,
5516 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5517 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 5518 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5519 .type = ARM_CP_ALIAS,
a0618a19 5520 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
5521 .access = PL1_RW,
5522 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 5523 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5524 .type = ARM_CP_ALIAS,
a65f1de9 5525 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5526 .access = PL1_RW,
5527 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
9b37a28c
FR
5528 /*
5529 * We rely on the access checks not allowing the guest to write to the
f502cfc2
PM
5530 * state field when SPSel indicates that it's being used as the stack
5531 * pointer.
5532 */
5533 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5534 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5535 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 5536 .type = ARM_CP_ALIAS,
f502cfc2 5537 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
5538 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5539 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
beeec926 5540 .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
884b4dee 5541 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
5542 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5543 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 5544 .type = ARM_CP_NO_RAW,
f502cfc2 5545 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
5546 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5547 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
696ba377
RH
5548 .access = PL2_RW,
5549 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
a4c88675 5550 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
6a43e0b6
PM
5551 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5552 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
696ba377 5553 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6a43e0b6
PM
5554 .writefn = dacr_write, .raw_writefn = raw_write,
5555 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5556 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5557 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
696ba377 5558 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6a43e0b6
PM
5559 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5560 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5561 .type = ARM_CP_ALIAS,
5562 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5563 .access = PL2_RW,
5564 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5565 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5566 .type = ARM_CP_ALIAS,
5567 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5568 .access = PL2_RW,
5569 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5570 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5571 .type = ARM_CP_ALIAS,
5572 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5573 .access = PL2_RW,
5574 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5575 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5576 .type = ARM_CP_ALIAS,
5577 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5578 .access = PL2_RW,
5579 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73 5580 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
80d2b43b 5581 .type = ARM_CP_IO,
a8d64e73
PM
5582 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5583 .resetvalue = 0,
80d2b43b
PM
5584 .access = PL3_RW,
5585 .writefn = mdcr_el3_write,
5586 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
7f4fbfb5 5587 { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
a8d64e73
PM
5588 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5589 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5590 .writefn = sdcr_write,
5591 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
5592};
5593
d1fb4da2 5594static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
f149e3e8 5595{
2fc0cc0e 5596 ARMCPU *cpu = env_archcpu(env);
d1fb4da2
RH
5597
5598 if (arm_feature(env, ARM_FEATURE_V8)) {
5599 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5600 } else {
5601 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5602 }
f149e3e8
EI
5603
5604 if (arm_feature(env, ARM_FEATURE_EL3)) {
5605 valid_mask &= ~HCR_HCD;
77077a83 5606 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
9b37a28c
FR
5607 /*
5608 * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
77077a83
JK
5609 * However, if we're using the SMC PSCI conduit then QEMU is
5610 * effectively acting like EL3 firmware and so the guest at
5611 * EL2 should retain the ability to prevent EL1 from being
5612 * able to make SMC calls into the ersatz firmware, so in
5613 * that case HCR.TSC should be read/write.
5614 */
f149e3e8
EI
5615 valid_mask &= ~HCR_TSC;
5616 }
d1fb4da2
RH
5617
5618 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5619 if (cpu_isar_feature(aa64_vh, cpu)) {
5620 valid_mask |= HCR_E2H;
5621 }
da3d8b13
RH
5622 if (cpu_isar_feature(aa64_ras, cpu)) {
5623 valid_mask |= HCR_TERR | HCR_TEA;
5624 }
d1fb4da2
RH
5625 if (cpu_isar_feature(aa64_lor, cpu)) {
5626 valid_mask |= HCR_TLOR;
5627 }
5628 if (cpu_isar_feature(aa64_pauth, cpu)) {
5629 valid_mask |= HCR_API | HCR_APK;
5630 }
8ddb300b
RH
5631 if (cpu_isar_feature(aa64_mte, cpu)) {
5632 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5633 }
7cb1e618
RH
5634 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5635 valid_mask |= HCR_ENSCXT;
5636 }
8c7e17ef
PM
5637 if (cpu_isar_feature(aa64_fwb, cpu)) {
5638 valid_mask |= HCR_FWB;
5639 }
ef682cdb 5640 }
f149e3e8 5641
d2fd9313
PM
5642 if (cpu_isar_feature(any_evt, cpu)) {
5643 valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5644 } else if (cpu_isar_feature(any_half_evt, cpu)) {
5645 valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5646 }
5647
f149e3e8
EI
5648 /* Clear RES0 bits. */
5649 value &= valid_mask;
5650
8ddb300b
RH
5651 /*
5652 * These bits change the MMU setup:
f149e3e8
EI
5653 * HCR_VM enables stage 2 translation
5654 * HCR_PTW forbids certain page-table setups
8ddb300b
RH
5655 * HCR_DC disables stage1 and enables stage2 translation
5656 * HCR_DCT enables tagging on (disabled) stage1 translation
8c7e17ef 5657 * HCR_FWB changes the interpretation of stage2 descriptor bits
f149e3e8 5658 */
8c7e17ef
PM
5659 if ((env->cp15.hcr_el2 ^ value) &
5660 (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
d10eb08f 5661 tlb_flush(CPU(cpu));
f149e3e8 5662 }
ce4afed8 5663 env->cp15.hcr_el2 = value;
89430fc6
PM
5664
5665 /*
5666 * Updates to VI and VF require us to update the status of
5667 * virtual interrupts, which are the logical OR of these bits
5668 * and the state of the input lines from the GIC. (This requires
5669 * that we have the iothread lock, which is done by marking the
5670 * reginfo structs as ARM_CP_IO.)
5671 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5672 * possible for it to be taken immediately, because VIRQ and
5673 * VFIQ are masked unless running at EL0 or EL1, and HCR
5674 * can only be written at EL2.
5675 */
5676 g_assert(qemu_mutex_iothread_locked());
5677 arm_cpu_update_virq(cpu);
5678 arm_cpu_update_vfiq(cpu);
3c29632f 5679 arm_cpu_update_vserr(cpu);
ce4afed8
PM
5680}
5681
d1fb4da2
RH
5682static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5683{
5684 do_hcr_write(env, value, 0);
5685}
5686
ce4afed8
PM
5687static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5688 uint64_t value)
5689{
5690 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5691 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
d1fb4da2 5692 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
ce4afed8
PM
5693}
5694
5695static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5696 uint64_t value)
5697{
5698 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5699 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
d1fb4da2 5700 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
f149e3e8
EI
5701}
5702
f7778444 5703/*
b74c0443 5704 * Return the effective value of HCR_EL2, at the given security state.
f7778444
RH
5705 * Bits that are not included here:
5706 * RW (read from SCR_EL3.RW as needed)
5707 */
b74c0443 5708uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, bool secure)
f7778444
RH
5709{
5710 uint64_t ret = env->cp15.hcr_el2;
5711
b74c0443 5712 if (!arm_is_el2_enabled_secstate(env, secure)) {
f7778444
RH
5713 /*
5714 * "This register has no effect if EL2 is not enabled in the
5715 * current Security state". This is ARMv8.4-SecEL2 speak for
5716 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5717 *
5718 * Prior to that, the language was "In an implementation that
5719 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5720 * as if this field is 0 for all purposes other than a direct
5721 * read or write access of HCR_EL2". With lots of enumeration
5722 * on a per-field basis. In current QEMU, this is condition
5723 * is arm_is_secure_below_el3.
5724 *
5725 * Since the v8.4 language applies to the entire register, and
5726 * appears to be backward compatible, use that.
5727 */
4990e1d3
RH
5728 return 0;
5729 }
5730
5731 /*
5732 * For a cpu that supports both aarch64 and aarch32, we can set bits
5733 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5734 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5735 */
5736 if (!arm_el_is_aa64(env, 2)) {
5737 uint64_t aa32_valid;
5738
5739 /*
5740 * These bits are up-to-date as of ARMv8.6.
5741 * For HCR, it's easiest to list just the 2 bits that are invalid.
5742 * For HCR2, list those that are valid.
5743 */
5744 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5745 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5746 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5747 ret &= aa32_valid;
5748 }
5749
5750 if (ret & HCR_TGE) {
5751 /* These bits are up-to-date as of ARMv8.6. */
f7778444
RH
5752 if (ret & HCR_E2H) {
5753 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5754 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5755 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4990e1d3
RH
5756 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5757 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5758 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
f7778444
RH
5759 } else {
5760 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5761 }
5762 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5763 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5764 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5765 HCR_TLOR);
5766 }
5767
5768 return ret;
5769}
5770
b74c0443
RH
5771uint64_t arm_hcr_el2_eff(CPUARMState *env)
5772{
5773 return arm_hcr_el2_eff_secstate(env, arm_is_secure_below_el3(env));
5774}
5775
19668718
RH
5776/*
5777 * Corresponds to ARM pseudocode function ELIsInHost().
5778 */
5779bool el_is_in_host(CPUARMState *env, int el)
5780{
5781 uint64_t mask;
5782
5783 /*
5784 * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5785 * Perform the simplest bit tests first, and validate EL2 afterward.
5786 */
5787 if (el & 1) {
5788 return false; /* EL1 or EL3 */
5789 }
5790
5791 /*
5792 * Note that hcr_write() checks isar_feature_aa64_vh(),
5793 * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5794 */
5795 mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5796 if ((env->cp15.hcr_el2 & mask) != mask) {
5797 return false;
5798 }
5799
5800 /* TGE and/or E2H set: double check those bits are currently legal. */
5801 return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5802}
5803
5814d587
RH
5804static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5805 uint64_t value)
5806{
5807 uint64_t valid_mask = 0;
5808
5809 /* No features adding bits to HCRX are implemented. */
5810
5811 /* Clear RES0 bits. */
5812 env->cp15.hcrx_el2 = value & valid_mask;
5813}
5814
5815static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5816 bool isread)
5817{
5818 if (arm_current_el(env) < 3
5819 && arm_feature(env, ARM_FEATURE_EL3)
5820 && !(env->cp15.scr_el3 & SCR_HXEN)) {
5821 return CP_ACCESS_TRAP_EL3;
5822 }
5823 return CP_ACCESS_OK;
5824}
5825
5826static const ARMCPRegInfo hcrx_el2_reginfo = {
5827 .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5828 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5829 .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5830 .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5831};
5832
5833/* Return the effective value of HCRX_EL2. */
5834uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5835{
5836 /*
5837 * The bits in this register behave as 0 for all purposes other than
5838 * direct reads of the register if:
5839 * - EL2 is not enabled in the current security state,
5840 * - SCR_EL3.HXEn is 0.
5841 */
5842 if (!arm_is_el2_enabled(env)
5843 || (arm_feature(env, ARM_FEATURE_EL3)
5844 && !(env->cp15.scr_el3 & SCR_HXEN))) {
5845 return 0;
5846 }
5847 return env->cp15.hcrx_el2;
5848}
5849
fc1120a7
PM
5850static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5851 uint64_t value)
5852{
5853 /*
5854 * For A-profile AArch32 EL3, if NSACR.CP10
5855 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5856 */
5857 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5858 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39
RH
5859 uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5860 value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
fc1120a7
PM
5861 }
5862 env->cp15.cptr_el[2] = value;
5863}
5864
5865static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5866{
5867 /*
5868 * For A-profile AArch32 EL3, if NSACR.CP10
5869 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5870 */
5871 uint64_t value = env->cp15.cptr_el[2];
5872
5873 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5874 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39 5875 value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
fc1120a7
PM
5876 }
5877 return value;
5878}
5879
4771cd01 5880static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8 5881 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
89430fc6 5882 .type = ARM_CP_IO,
f149e3e8
EI
5883 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5884 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5885 .writefn = hcr_write },
ce4afed8 5886 { .name = "HCR", .state = ARM_CP_STATE_AA32,
89430fc6 5887 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5888 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5889 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5890 .writefn = hcr_writelow },
831a2fca
PM
5891 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5892 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5893 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3b685ba7 5894 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5895 .type = ARM_CP_ALIAS,
3b685ba7
EI
5896 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5897 .access = PL2_RW,
5898 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
68e78e33 5899 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
f2c30f42
EI
5900 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5901 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
cba517c3 5902 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
63b60551
EI
5903 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5904 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
cba517c3
PM
5905 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5906 .type = ARM_CP_ALIAS,
5907 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5908 .access = PL2_RW,
5909 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
3b685ba7 5910 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5911 .type = ARM_CP_ALIAS,
3b685ba7 5912 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5913 .access = PL2_RW,
5914 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d79e0c06 5915 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5916 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5917 .access = PL2_RW, .writefn = vbar_write,
5918 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5919 .resetvalue = 0 },
884b4dee
GB
5920 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5921 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5922 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 5923 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
5924 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5925 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5926 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
fc1120a7
PM
5927 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5928 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
95f949ac
EI
5929 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5930 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5931 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5932 .resetvalue = 0 },
5933 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5934 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac
EI
5935 .access = PL2_RW, .type = ARM_CP_ALIAS,
5936 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
5937 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5938 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5939 .access = PL2_RW, .type = ARM_CP_CONST,
5940 .resetvalue = 0 },
5941 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
55b53c71 5942 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5943 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5944 .access = PL2_RW, .type = ARM_CP_CONST,
5945 .resetvalue = 0 },
37cd6c24
PM
5946 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5947 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5948 .access = PL2_RW, .type = ARM_CP_CONST,
5949 .resetvalue = 0 },
5950 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5951 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5952 .access = PL2_RW, .type = ARM_CP_CONST,
5953 .resetvalue = 0 },
06ec4c8c
EI
5954 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5955 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
d06dc933 5956 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
06ec4c8c 5957 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
5958 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5959 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5960 .type = ARM_CP_ALIAS,
68e9c2fe 5961 .access = PL2_RW, .accessfn = access_el3_aa32ns,
afbb181c 5962 .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
68e9c2fe
EI
5963 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5964 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5965 .access = PL2_RW,
988cc190 5966 /* no .writefn needed as this can't cause an ASID change */
68e9c2fe 5967 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
5968 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5969 .cp = 15, .opc1 = 6, .crm = 2,
5970 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5971 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5972 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5973 .writefn = vttbr_write },
5974 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5975 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5976 .access = PL2_RW, .writefn = vttbr_write,
5977 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
5978 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5979 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5980 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5981 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
5982 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
5983 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
5984 .access = PL2_RW, .resetvalue = 0,
5985 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
5986 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
5987 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
ed30da8e 5988 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
a57633c0
EI
5989 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
5990 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
5991 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 5992 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
5993 { .name = "TLBIALLNSNH",
5994 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5995 .type = ARM_CP_NO_RAW, .access = PL2_W,
5996 .writefn = tlbiall_nsnh_write },
5997 { .name = "TLBIALLNSNHIS",
5998 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5999 .type = ARM_CP_NO_RAW, .access = PL2_W,
6000 .writefn = tlbiall_nsnh_is_write },
6001 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6002 .type = ARM_CP_NO_RAW, .access = PL2_W,
6003 .writefn = tlbiall_hyp_write },
6004 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6005 .type = ARM_CP_NO_RAW, .access = PL2_W,
6006 .writefn = tlbiall_hyp_is_write },
6007 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6008 .type = ARM_CP_NO_RAW, .access = PL2_W,
6009 .writefn = tlbimva_hyp_write },
6010 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6011 .type = ARM_CP_NO_RAW, .access = PL2_W,
6012 .writefn = tlbimva_hyp_is_write },
51da9014
EI
6013 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
6014 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
696ba377 6015 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 6016 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
6017 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
6018 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
696ba377 6019 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 6020 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
6021 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
6022 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
696ba377 6023 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75
PM
6024 .writefn = tlbi_aa64_vae2_write },
6025 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
6026 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
696ba377 6027 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75 6028 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
6029 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
6030 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
696ba377 6031 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 6032 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
6033 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
6034 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
696ba377 6035 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75 6036 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 6037#ifndef CONFIG_USER_ONLY
9b37a28c
FR
6038 /*
6039 * Unlike the other EL2-related AT operations, these must
2a47df95
PM
6040 * UNDEF from EL3 if EL2 is not implemented, which is why we
6041 * define them here rather than with the rest of the AT ops.
6042 */
6043 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
6044 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6045 .access = PL2_W, .accessfn = at_s1e2_access,
696ba377
RH
6046 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6047 .writefn = ats_write64 },
2a47df95
PM
6048 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
6049 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6050 .access = PL2_W, .accessfn = at_s1e2_access,
696ba377
RH
6051 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6052 .writefn = ats_write64 },
9b37a28c
FR
6053 /*
6054 * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
14db7fe0
PM
6055 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
6056 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
6057 * to behave as if SCR.NS was 1.
6058 */
6059 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6060 .access = PL2_W,
0710b2fa 6061 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
14db7fe0
PM
6062 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6063 .access = PL2_W,
0710b2fa 6064 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
0b6440af
EI
6065 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6066 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
9b37a28c
FR
6067 /*
6068 * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
0b6440af
EI
6069 * reset values as IMPDEF. We choose to reset to 3 to comply with
6070 * both ARMv7 and ARMv8.
6071 */
6072 .access = PL2_RW, .resetvalue = 3,
6073 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
6074 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6075 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6076 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6077 .writefn = gt_cntvoff_write,
6078 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6079 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6080 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6081 .writefn = gt_cntvoff_write,
6082 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
6083 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6084 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6085 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6086 .type = ARM_CP_IO, .access = PL2_RW,
6087 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6088 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6089 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6090 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6091 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6092 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6093 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 6094 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
6095 .resetfn = gt_hyp_timer_reset,
6096 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6097 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6098 .type = ARM_CP_IO,
6099 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6100 .access = PL2_RW,
6101 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6102 .resetvalue = 0,
6103 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 6104#endif
59e05530
EI
6105 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6106 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6107 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6108 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6109 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6110 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6111 .access = PL2_RW,
6112 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
6113 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6114 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6115 .access = PL2_RW,
6116 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
6117};
6118
ce4afed8
PM
6119static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6120 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
89430fc6 6121 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
6122 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6123 .access = PL2_RW,
6124 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6125 .writefn = hcr_writehigh },
ce4afed8
PM
6126};
6127
e9152ee9
RDC
6128static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6129 bool isread)
6130{
6131 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6132 return CP_ACCESS_OK;
6133 }
6134 return CP_ACCESS_TRAP_UNCATEGORIZED;
6135}
6136
6137static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6138 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6139 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6140 .access = PL2_RW, .accessfn = sel2_access,
6141 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6142 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6143 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6144 .access = PL2_RW, .accessfn = sel2_access,
6145 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
e9152ee9
RDC
6146};
6147
2f027fc5
PM
6148static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6149 bool isread)
6150{
9b37a28c
FR
6151 /*
6152 * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
926c1b97 6153 * At Secure EL1 it traps to EL3 or EL2.
2f027fc5
PM
6154 */
6155 if (arm_current_el(env) == 3) {
6156 return CP_ACCESS_OK;
6157 }
6158 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
6159 if (env->cp15.scr_el3 & SCR_EEL2) {
6160 return CP_ACCESS_TRAP_EL2;
6161 }
2f027fc5
PM
6162 return CP_ACCESS_TRAP_EL3;
6163 }
6164 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6165 if (isread) {
6166 return CP_ACCESS_OK;
6167 }
6168 return CP_ACCESS_TRAP_UNCATEGORIZED;
6169}
6170
60fb1a87
GB
6171static const ARMCPRegInfo el3_cp_reginfo[] = {
6172 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6173 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6174 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
10d0ef3e 6175 .resetfn = scr_reset, .writefn = scr_write },
f80741d1 6176 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
60fb1a87 6177 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
6178 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6179 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 6180 .writefn = scr_write },
60fb1a87
GB
6181 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6182 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6183 .access = PL3_RW, .resetvalue = 0,
6184 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6185 { .name = "SDER",
6186 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6187 .access = PL3_RW, .resetvalue = 0,
6188 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 6189 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
6190 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6191 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 6192 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
6193 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6194 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
f478847f 6195 .access = PL3_RW, .resetvalue = 0,
7dd8c9af 6196 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
6197 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6198 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c 6199 .access = PL3_RW,
cb4a0a34
PM
6200 /* no .writefn needed as this can't cause an ASID change */
6201 .resetvalue = 0,
11f136ee 6202 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 6203 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 6204 .type = ARM_CP_ALIAS,
81547d66
EI
6205 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6206 .access = PL3_RW,
6207 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 6208 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
6209 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6210 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
6211 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6212 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6213 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 6214 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 6215 .type = ARM_CP_ALIAS,
81547d66 6216 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
6217 .access = PL3_RW,
6218 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
6219 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6220 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6221 .access = PL3_RW, .writefn = vbar_write,
6222 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6223 .resetvalue = 0 },
c6f19164
GB
6224 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6225 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6226 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6227 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
6228 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6229 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6230 .access = PL3_RW, .resetvalue = 0,
6231 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
6232 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6233 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6234 .access = PL3_RW, .type = ARM_CP_CONST,
6235 .resetvalue = 0 },
37cd6c24
PM
6236 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6237 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6238 .access = PL3_RW, .type = ARM_CP_CONST,
6239 .resetvalue = 0 },
6240 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6241 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6242 .access = PL3_RW, .type = ARM_CP_CONST,
6243 .resetvalue = 0 },
43efaa33
PM
6244 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6245 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6246 .access = PL3_W, .type = ARM_CP_NO_RAW,
6247 .writefn = tlbi_aa64_alle3is_write },
6248 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6249 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6250 .access = PL3_W, .type = ARM_CP_NO_RAW,
6251 .writefn = tlbi_aa64_vae3is_write },
6252 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6253 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6254 .access = PL3_W, .type = ARM_CP_NO_RAW,
6255 .writefn = tlbi_aa64_vae3is_write },
6256 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6257 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6258 .access = PL3_W, .type = ARM_CP_NO_RAW,
6259 .writefn = tlbi_aa64_alle3_write },
6260 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6261 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6262 .access = PL3_W, .type = ARM_CP_NO_RAW,
6263 .writefn = tlbi_aa64_vae3_write },
6264 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6265 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6266 .access = PL3_W, .type = ARM_CP_NO_RAW,
6267 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
6268};
6269
e2cce18f
RH
6270#ifndef CONFIG_USER_ONLY
6271/* Test if system register redirection is to occur in the current state. */
6272static bool redirect_for_e2h(CPUARMState *env)
6273{
6274 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6275}
6276
6277static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6278{
6279 CPReadFn *readfn;
6280
6281 if (redirect_for_e2h(env)) {
6282 /* Switch to the saved EL2 version of the register. */
6283 ri = ri->opaque;
6284 readfn = ri->readfn;
6285 } else {
6286 readfn = ri->orig_readfn;
6287 }
6288 if (readfn == NULL) {
6289 readfn = raw_read;
6290 }
6291 return readfn(env, ri);
6292}
6293
6294static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6295 uint64_t value)
6296{
6297 CPWriteFn *writefn;
6298
6299 if (redirect_for_e2h(env)) {
6300 /* Switch to the saved EL2 version of the register. */
6301 ri = ri->opaque;
6302 writefn = ri->writefn;
6303 } else {
6304 writefn = ri->orig_writefn;
6305 }
6306 if (writefn == NULL) {
6307 writefn = raw_write;
6308 }
6309 writefn(env, ri, value);
6310}
6311
6312static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6313{
6314 struct E2HAlias {
6315 uint32_t src_key, dst_key, new_key;
6316 const char *src_name, *dst_name, *new_name;
6317 bool (*feature)(const ARMISARegisters *id);
6318 };
6319
6320#define K(op0, op1, crn, crm, op2) \
6321 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6322
6323 static const struct E2HAlias aliases[] = {
6324 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
6325 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6326 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
6327 "CPACR", "CPTR_EL2", "CPACR_EL12" },
6328 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
6329 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6330 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
6331 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6332 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
6333 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6334 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
6335 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6336 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
6337 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6338 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
6339 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6340 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
6341 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6342 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
6343 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6344 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
6345 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6346 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6347 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6348 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6349 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6350 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6351 "VBAR", "VBAR_EL2", "VBAR_EL12" },
6352 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6353 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6354 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6355 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6356
6357 /*
6358 * Note that redirection of ZCR is mentioned in the description
6359 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6360 * not in the summary table.
6361 */
6362 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
6363 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
de561988
RH
6364 { K(3, 0, 1, 2, 6), K(3, 4, 1, 2, 6), K(3, 5, 1, 2, 6),
6365 "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
e2cce18f 6366
4b779ceb
RH
6367 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
6368 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6369
7cb1e618
RH
6370 { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6371 "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6372 isar_feature_aa64_scxtnum },
6373
e2cce18f
RH
6374 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6375 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6376 };
6377#undef K
6378
6379 size_t i;
6380
6381 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6382 const struct E2HAlias *a = &aliases[i];
9da35a40 6383 ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
9da35a40 6384 bool ok;
e2cce18f
RH
6385
6386 if (a->feature && !a->feature(&cpu->isar)) {
6387 continue;
6388 }
6389
5860362d
RH
6390 src_reg = g_hash_table_lookup(cpu->cp_regs,
6391 (gpointer)(uintptr_t)a->src_key);
6392 dst_reg = g_hash_table_lookup(cpu->cp_regs,
6393 (gpointer)(uintptr_t)a->dst_key);
e2cce18f
RH
6394 g_assert(src_reg != NULL);
6395 g_assert(dst_reg != NULL);
6396
6397 /* Cross-compare names to detect typos in the keys. */
6398 g_assert(strcmp(src_reg->name, a->src_name) == 0);
6399 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6400
6401 /* None of the core system registers use opaque; we will. */
6402 g_assert(src_reg->opaque == NULL);
6403
6404 /* Create alias before redirection so we dup the right data. */
9da35a40 6405 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
9da35a40
RH
6406
6407 new_reg->name = a->new_name;
6408 new_reg->type |= ARM_CP_ALIAS;
6409 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
6410 new_reg->access &= PL2_RW | PL3_RW;
6411
5860362d
RH
6412 ok = g_hash_table_insert(cpu->cp_regs,
6413 (gpointer)(uintptr_t)a->new_key, new_reg);
9da35a40 6414 g_assert(ok);
e2cce18f
RH
6415
6416 src_reg->opaque = dst_reg;
6417 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6418 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6419 if (!src_reg->raw_readfn) {
6420 src_reg->raw_readfn = raw_read;
6421 }
6422 if (!src_reg->raw_writefn) {
6423 src_reg->raw_writefn = raw_write;
6424 }
6425 src_reg->readfn = el2_e2h_read;
6426 src_reg->writefn = el2_e2h_write;
6427 }
6428}
6429#endif
6430
3f208fd7
PM
6431static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6432 bool isread)
7da845b0 6433{
97475a89
RH
6434 int cur_el = arm_current_el(env);
6435
6436 if (cur_el < 2) {
6437 uint64_t hcr = arm_hcr_el2_eff(env);
6438
6439 if (cur_el == 0) {
6440 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6441 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6442 return CP_ACCESS_TRAP_EL2;
6443 }
6444 } else {
6445 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6446 return CP_ACCESS_TRAP;
6447 }
6448 if (hcr & HCR_TID2) {
6449 return CP_ACCESS_TRAP_EL2;
6450 }
6451 }
6452 } else if (hcr & HCR_TID2) {
6453 return CP_ACCESS_TRAP_EL2;
6454 }
7da845b0 6455 }
630fcd4d
MZ
6456
6457 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6458 return CP_ACCESS_TRAP_EL2;
6459 }
6460
7da845b0
PM
6461 return CP_ACCESS_OK;
6462}
6463
58e93b48
RH
6464/*
6465 * Check for traps to RAS registers, which are controlled
6466 * by HCR_EL2.TERR and SCR_EL3.TERR.
6467 */
6468static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6469 bool isread)
6470{
6471 int el = arm_current_el(env);
6472
6473 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6474 return CP_ACCESS_TRAP_EL2;
6475 }
6476 if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6477 return CP_ACCESS_TRAP_EL3;
6478 }
6479 return CP_ACCESS_OK;
6480}
6481
6482static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6483{
6484 int el = arm_current_el(env);
6485
6486 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6487 return env->cp15.vdisr_el2;
6488 }
6489 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6490 return 0; /* RAZ/WI */
6491 }
6492 return env->cp15.disr_el1;
6493}
6494
6495static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6496{
6497 int el = arm_current_el(env);
6498
6499 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6500 env->cp15.vdisr_el2 = val;
6501 return;
6502 }
6503 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6504 return; /* RAZ/WI */
6505 }
6506 env->cp15.disr_el1 = val;
6507}
6508
6509/*
6510 * Minimal RAS implementation with no Error Records.
6511 * Which means that all of the Error Record registers:
6512 * ERXADDR_EL1
6513 * ERXCTLR_EL1
6514 * ERXFR_EL1
6515 * ERXMISC0_EL1
6516 * ERXMISC1_EL1
6517 * ERXMISC2_EL1
6518 * ERXMISC3_EL1
6519 * ERXPFGCDN_EL1 (RASv1p1)
6520 * ERXPFGCTL_EL1 (RASv1p1)
6521 * ERXPFGF_EL1 (RASv1p1)
6522 * ERXSTATUS_EL1
6523 * and
6524 * ERRSELR_EL1
6525 * may generate UNDEFINED, which is the effect we get by not
6526 * listing them at all.
bd8db7d9
PM
6527 *
6528 * These registers have fine-grained trap bits, but UNDEF-to-EL1
6529 * is higher priority than FGT-to-EL2 so we do not need to list them
6530 * in order to check for an FGT.
58e93b48
RH
6531 */
6532static const ARMCPRegInfo minimal_ras_reginfo[] = {
6533 { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6534 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6535 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6536 .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6537 { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6538 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6539 .access = PL1_R, .accessfn = access_terr,
bd8db7d9 6540 .fgt = FGT_ERRIDR_EL1,
58e93b48
RH
6541 .type = ARM_CP_CONST, .resetvalue = 0 },
6542 { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6543 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6544 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6545 { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6546 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6547 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6548};
6549
397d922c
RH
6550/*
6551 * Return the exception level to which exceptions should be taken
6552 * via SVEAccessTrap. This excludes the check for whether the exception
6553 * should be routed through AArch64.AdvSIMDFPAccessTrap. That can easily
6554 * be found by testing 0 < fp_exception_el < sve_exception_el.
6555 *
6556 * C.f. the ARM pseudocode function CheckSVEEnabled. Note that the
6557 * pseudocode does *not* separate out the FP trap checks, but has them
6558 * all in one function.
5be5e8ed 6559 */
ced31551 6560int sve_exception_el(CPUARMState *env, int el)
5be5e8ed
RH
6561{
6562#ifndef CONFIG_USER_ONLY
aa4451b6 6563 if (el <= 1 && !el_is_in_host(env, el)) {
fab8ad39 6564 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
7701cee5
RH
6565 case 1:
6566 if (el != 0) {
6567 break;
6568 }
6569 /* fall through */
6570 case 0:
6571 case 2:
61a8c23a 6572 return 1;
5be5e8ed 6573 }
5be5e8ed
RH
6574 }
6575
7d38cb92
RH
6576 if (el <= 2 && arm_is_el2_enabled(env)) {
6577 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6578 if (env->cp15.hcr_el2 & HCR_E2H) {
fab8ad39 6579 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
d5a6fa2d 6580 case 1:
7d38cb92 6581 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
d5a6fa2d
RH
6582 break;
6583 }
6584 /* fall through */
6585 case 0:
6586 case 2:
6587 return 2;
6588 }
7d38cb92 6589 } else {
fab8ad39 6590 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
d5a6fa2d
RH
6591 return 2;
6592 }
60eed086 6593 }
5be5e8ed
RH
6594 }
6595
60eed086
RH
6596 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6597 if (arm_feature(env, ARM_FEATURE_EL3)
fab8ad39 6598 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
5be5e8ed
RH
6599 return 3;
6600 }
6601#endif
6602 return 0;
6603}
6604
6b2ca83e
RH
6605/*
6606 * Return the exception level to which exceptions should be taken for SME.
6607 * C.f. the ARM pseudocode function CheckSMEAccess.
6608 */
6609int sme_exception_el(CPUARMState *env, int el)
6610{
6611#ifndef CONFIG_USER_ONLY
6612 if (el <= 1 && !el_is_in_host(env, el)) {
6613 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6614 case 1:
6615 if (el != 0) {
6616 break;
6617 }
6618 /* fall through */
6619 case 0:
6620 case 2:
6621 return 1;
6622 }
6623 }
6624
6625 if (el <= 2 && arm_is_el2_enabled(env)) {
6626 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6627 if (env->cp15.hcr_el2 & HCR_E2H) {
6628 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6629 case 1:
6630 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6631 break;
6632 }
6633 /* fall through */
6634 case 0:
6635 case 2:
6636 return 2;
6637 }
6638 } else {
6639 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6640 return 2;
6641 }
6642 }
6643 }
6644
6645 /* CPTR_EL3. Since ESM is negative we must check for EL3. */
6646 if (arm_feature(env, ARM_FEATURE_EL3)
6647 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6648 return 3;
6649 }
6650#endif
6651 return 0;
6652}
6653
75fe8356
RH
6654/* This corresponds to the ARM pseudocode function IsFullA64Enabled(). */
6655static bool sme_fa64(CPUARMState *env, int el)
6656{
6657 if (!cpu_isar_feature(aa64_sme_fa64, env_archcpu(env))) {
6658 return false;
6659 }
6660
6661 if (el <= 1 && !el_is_in_host(env, el)) {
6662 if (!FIELD_EX64(env->vfp.smcr_el[1], SMCR, FA64)) {
6663 return false;
6664 }
6665 }
6666 if (el <= 2 && arm_is_el2_enabled(env)) {
6667 if (!FIELD_EX64(env->vfp.smcr_el[2], SMCR, FA64)) {
6668 return false;
6669 }
6670 }
6671 if (arm_feature(env, ARM_FEATURE_EL3)) {
6672 if (!FIELD_EX64(env->vfp.smcr_el[3], SMCR, FA64)) {
6673 return false;
6674 }
6675 }
6676
6677 return true;
6678}
6679
0ab5953b
RH
6680/*
6681 * Given that SVE is enabled, return the vector length for EL.
6682 */
6ca54aa9 6683uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
0ab5953b 6684{
2fc0cc0e 6685 ARMCPU *cpu = env_archcpu(env);
6ca54aa9
RH
6686 uint64_t *cr = env->vfp.zcr_el;
6687 uint32_t map = cpu->sve_vq.map;
6688 uint32_t len = ARM_MAX_VQ - 1;
6689
6690 if (sm) {
6691 cr = env->vfp.smcr_el;
6692 map = cpu->sme_vq.map;
6693 }
0ab5953b 6694
c6225beb 6695 if (el <= 1 && !el_is_in_host(env, el)) {
6ca54aa9 6696 len = MIN(len, 0xf & (uint32_t)cr[1]);
0ab5953b 6697 }
6a02a732 6698 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6ca54aa9 6699 len = MIN(len, 0xf & (uint32_t)cr[2]);
0ab5953b 6700 }
6a02a732 6701 if (arm_feature(env, ARM_FEATURE_EL3)) {
6ca54aa9
RH
6702 len = MIN(len, 0xf & (uint32_t)cr[3]);
6703 }
6704
6705 map &= MAKE_64BIT_MASK(0, len + 1);
6706 if (map != 0) {
6707 return 31 - clz32(map);
0ab5953b 6708 }
0df9142d 6709
6ca54aa9
RH
6710 /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6711 assert(sm);
6712 return ctz32(cpu->sme_vq.map);
6713}
6714
6715uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6716{
6717 return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
0ab5953b
RH
6718}
6719
5be5e8ed
RH
6720static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6721 uint64_t value)
6722{
0ab5953b 6723 int cur_el = arm_current_el(env);
5ef3cc56 6724 int old_len = sve_vqm1_for_el(env, cur_el);
0ab5953b
RH
6725 int new_len;
6726
5be5e8ed 6727 /* Bits other than [3:0] are RAZ/WI. */
7b351d98 6728 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5be5e8ed 6729 raw_write(env, ri, value & 0xf);
0ab5953b
RH
6730
6731 /*
6732 * Because we arrived here, we know both FP and SVE are enabled;
6733 * otherwise we would have trapped access to the ZCR_ELn register.
6734 */
5ef3cc56 6735 new_len = sve_vqm1_for_el(env, cur_el);
0ab5953b
RH
6736 if (new_len < old_len) {
6737 aarch64_sve_narrow_vq(env, new_len + 1);
6738 }
5be5e8ed
RH
6739}
6740
60360d82
RH
6741static const ARMCPRegInfo zcr_reginfo[] = {
6742 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6743 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6744 .access = PL1_RW, .type = ARM_CP_SVE,
6745 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6746 .writefn = zcr_write, .raw_writefn = raw_write },
6747 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6748 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6749 .access = PL2_RW, .type = ARM_CP_SVE,
6750 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6751 .writefn = zcr_write, .raw_writefn = raw_write },
6752 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6753 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6754 .access = PL3_RW, .type = ARM_CP_SVE,
6755 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6756 .writefn = zcr_write, .raw_writefn = raw_write },
5be5e8ed
RH
6757};
6758
9e5ec745
RH
6759#ifdef TARGET_AARCH64
6760static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6761 bool isread)
6762{
6763 int el = arm_current_el(env);
6764
6765 if (el == 0) {
6766 uint64_t sctlr = arm_sctlr(env, el);
6767 if (!(sctlr & SCTLR_EnTP2)) {
6768 return CP_ACCESS_TRAP;
6769 }
6770 }
6771 /* TODO: FEAT_FGT */
6772 if (el < 3
6773 && arm_feature(env, ARM_FEATURE_EL3)
6774 && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6775 return CP_ACCESS_TRAP_EL3;
6776 }
6777 return CP_ACCESS_OK;
6778}
6779
d5b1223a
RH
6780static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6781 bool isread)
6782{
6783 /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6784 if (arm_current_el(env) < 3
6785 && arm_feature(env, ARM_FEATURE_EL3)
6786 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6787 return CP_ACCESS_TRAP_EL3;
6788 }
6789 return CP_ACCESS_OK;
6790}
6791
7f2a01e7
RH
6792/* ResetSVEState */
6793static void arm_reset_sve_state(CPUARMState *env)
6794{
6795 memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
6796 /* Recall that FFR is stored as pregs[16]. */
6797 memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
6798 vfp_set_fpcr(env, 0x0800009f);
6799}
6800
2a8af382
RH
6801void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
6802{
6803 uint64_t change = (env->svcr ^ new) & mask;
6804
f4318557
RH
6805 if (change == 0) {
6806 return;
6807 }
2a8af382 6808 env->svcr ^= change;
7f2a01e7
RH
6809
6810 if (change & R_SVCR_SM_MASK) {
6811 arm_reset_sve_state(env);
6812 }
fccb4918
RH
6813
6814 /*
6815 * ResetSMEState.
6816 *
6817 * SetPSTATE_ZA zeros on enable and disable. We can zero this only
6818 * on enable: while disabled, the storage is inaccessible and the
6819 * value does not matter. We're not saving the storage in vmstate
6820 * when disabled either.
6821 */
6822 if (change & new & R_SVCR_ZA_MASK) {
6823 memset(env->zarray, 0, sizeof(env->zarray));
6824 }
f4318557
RH
6825
6826 arm_rebuild_hflags(env);
2a8af382
RH
6827}
6828
c37e6ac9
RH
6829static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6830 uint64_t value)
6831{
2a8af382 6832 aarch64_set_svcr(env, value, -1);
c37e6ac9
RH
6833}
6834
de561988
RH
6835static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6836 uint64_t value)
6837{
6838 int cur_el = arm_current_el(env);
6839 int old_len = sve_vqm1_for_el(env, cur_el);
6840 int new_len;
6841
6842 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6843 value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6844 raw_write(env, ri, value);
6845
6846 /*
6847 * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6848 * when SVL is widened (old values kept, or zeros). Choose to keep the
6849 * current values for simplicity. But for QEMU internals, we must still
6850 * apply the narrower SVL to the Zregs and Pregs -- see the comment
6851 * above aarch64_sve_narrow_vq.
6852 */
6853 new_len = sve_vqm1_for_el(env, cur_el);
6854 if (new_len < old_len) {
6855 aarch64_sve_narrow_vq(env, new_len + 1);
6856 }
6857}
6858
9e5ec745
RH
6859static const ARMCPRegInfo sme_reginfo[] = {
6860 { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6861 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6862 .access = PL0_RW, .accessfn = access_tpidr2,
bd8db7d9 6863 .fgt = FGT_NTPIDR2_EL0,
9e5ec745 6864 .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
c37e6ac9
RH
6865 { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6866 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6867 .access = PL0_RW, .type = ARM_CP_SME,
6868 .fieldoffset = offsetof(CPUARMState, svcr),
6869 .writefn = svcr_write, .raw_writefn = raw_write },
de561988
RH
6870 { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6871 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6872 .access = PL1_RW, .type = ARM_CP_SME,
6873 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6874 .writefn = smcr_write, .raw_writefn = raw_write },
6875 { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6876 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6877 .access = PL2_RW, .type = ARM_CP_SME,
6878 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6879 .writefn = smcr_write, .raw_writefn = raw_write },
6880 { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6881 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6882 .access = PL3_RW, .type = ARM_CP_SME,
6883 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6884 .writefn = smcr_write, .raw_writefn = raw_write },
d5b1223a
RH
6885 { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6886 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6887 .access = PL1_R, .accessfn = access_aa64_tid1,
6888 /*
6889 * IMPLEMENTOR = 0 (software)
6890 * REVISION = 0 (implementation defined)
6891 * SMPS = 0 (no streaming execution priority in QEMU)
6892 * AFFINITY = 0 (streaming sve mode not shared with other PEs)
6893 */
6894 .type = ARM_CP_CONST, .resetvalue = 0, },
6895 /*
6896 * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6897 */
6898 { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6899 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6900 .access = PL1_RW, .accessfn = access_esm,
bd8db7d9 6901 .fgt = FGT_NSMPRI_EL1,
d5b1223a
RH
6902 .type = ARM_CP_CONST, .resetvalue = 0 },
6903 { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6904 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6905 .access = PL2_RW, .accessfn = access_esm,
6906 .type = ARM_CP_CONST, .resetvalue = 0 },
9e5ec745
RH
6907};
6908#endif /* TARGET_AARCH64 */
6909
24183fb6
PM
6910static void define_pmu_regs(ARMCPU *cpu)
6911{
6912 /*
6913 * v7 performance monitor control register: same implementor
6914 * field as main ID register, and we implement four counters in
6915 * addition to the cycle count register.
6916 */
24526bb9 6917 unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
24183fb6
PM
6918 ARMCPRegInfo pmcr = {
6919 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6920 .access = PL0_RW,
dc780233 6921 .fgt = FGT_PMCR_EL0,
24183fb6
PM
6922 .type = ARM_CP_IO | ARM_CP_ALIAS,
6923 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6924 .accessfn = pmreg_access, .writefn = pmcr_write,
6925 .raw_writefn = raw_write,
6926 };
6927 ARMCPRegInfo pmcr64 = {
6928 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6929 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6930 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 6931 .fgt = FGT_PMCR_EL0,
24183fb6
PM
6932 .type = ARM_CP_IO,
6933 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
24526bb9 6934 .resetvalue = cpu->isar.reset_pmcr_el0,
24183fb6
PM
6935 .writefn = pmcr_write, .raw_writefn = raw_write,
6936 };
24526bb9 6937
24183fb6
PM
6938 define_one_arm_cp_reg(cpu, &pmcr);
6939 define_one_arm_cp_reg(cpu, &pmcr64);
6940 for (i = 0; i < pmcrn; i++) {
6941 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6942 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6943 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6944 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6945 ARMCPRegInfo pmev_regs[] = {
6946 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6947 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6948 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
dc780233 6949 .fgt = FGT_PMEVCNTRN_EL0,
24183fb6 6950 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
99a50d1a 6951 .accessfn = pmreg_access_xevcntr },
24183fb6
PM
6952 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6953 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
99a50d1a 6954 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
24183fb6 6955 .type = ARM_CP_IO,
dc780233 6956 .fgt = FGT_PMEVCNTRN_EL0,
24183fb6
PM
6957 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6958 .raw_readfn = pmevcntr_rawread,
6959 .raw_writefn = pmevcntr_rawwrite },
6960 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6961 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6962 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
dc780233 6963 .fgt = FGT_PMEVTYPERN_EL0,
24183fb6
PM
6964 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6965 .accessfn = pmreg_access },
6966 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6967 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6968 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
dc780233 6969 .fgt = FGT_PMEVTYPERN_EL0,
24183fb6
PM
6970 .type = ARM_CP_IO,
6971 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6972 .raw_writefn = pmevtyper_rawwrite },
24183fb6
PM
6973 };
6974 define_arm_cp_regs(cpu, pmev_regs);
6975 g_free(pmevcntr_name);
6976 g_free(pmevcntr_el0_name);
6977 g_free(pmevtyper_name);
6978 g_free(pmevtyper_el0_name);
6979 }
a793bcd0 6980 if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
24183fb6
PM
6981 ARMCPRegInfo v81_pmu_regs[] = {
6982 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6983 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6984 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 6985 .fgt = FGT_PMCEIDN_EL0,
24183fb6
PM
6986 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6987 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6988 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6989 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 6990 .fgt = FGT_PMCEIDN_EL0,
24183fb6 6991 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
24183fb6
PM
6992 };
6993 define_arm_cp_regs(cpu, v81_pmu_regs);
6994 }
a793bcd0 6995 if (cpu_isar_feature(any_pmuv3p4, cpu)) {
15dd1ebd
PM
6996 static const ARMCPRegInfo v84_pmmir = {
6997 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6998 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6999 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 7000 .fgt = FGT_PMMIR_EL1,
15dd1ebd
PM
7001 .resetvalue = 0
7002 };
7003 define_one_arm_cp_reg(cpu, &v84_pmmir);
7004 }
24183fb6
PM
7005}
7006
9b37a28c
FR
7007/*
7008 * We don't know until after realize whether there's a GICv3
96a8b92e
PM
7009 * attached, and that is what registers the gicv3 sysregs.
7010 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
7011 * at runtime.
7012 */
7013static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
7014{
2fc0cc0e 7015 ARMCPU *cpu = env_archcpu(env);
8a130a7b 7016 uint64_t pfr1 = cpu->isar.id_pfr1;
96a8b92e
PM
7017
7018 if (env->gicv3state) {
7019 pfr1 |= 1 << 28;
7020 }
7021 return pfr1;
7022}
7023
976b99b6 7024#ifndef CONFIG_USER_ONLY
96a8b92e
PM
7025static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
7026{
2fc0cc0e 7027 ARMCPU *cpu = env_archcpu(env);
47576b94 7028 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
96a8b92e
PM
7029
7030 if (env->gicv3state) {
7031 pfr0 |= 1 << 24;
7032 }
7033 return pfr0;
7034}
976b99b6 7035#endif
96a8b92e 7036
9b37a28c
FR
7037/*
7038 * Shared logic between LORID and the rest of the LOR* registers.
9bd268ba 7039 * Secure state exclusion has already been dealt with.
2d7137c1 7040 */
9bd268ba
RDC
7041static CPAccessResult access_lor_ns(CPUARMState *env,
7042 const ARMCPRegInfo *ri, bool isread)
2d7137c1
RH
7043{
7044 int el = arm_current_el(env);
7045
7046 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
7047 return CP_ACCESS_TRAP_EL2;
7048 }
7049 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
7050 return CP_ACCESS_TRAP_EL3;
7051 }
7052 return CP_ACCESS_OK;
7053}
7054
2d7137c1
RH
7055static CPAccessResult access_lor_other(CPUARMState *env,
7056 const ARMCPRegInfo *ri, bool isread)
7057{
7058 if (arm_is_secure_below_el3(env)) {
7059 /* Access denied in secure mode. */
7060 return CP_ACCESS_TRAP;
7061 }
9bd268ba 7062 return access_lor_ns(env, ri, isread);
2d7137c1
RH
7063}
7064
d8564ee4
RH
7065/*
7066 * A trivial implementation of ARMv8.1-LOR leaves all of these
7067 * registers fixed at 0, which indicates that there are zero
7068 * supported Limited Ordering regions.
7069 */
7070static const ARMCPRegInfo lor_reginfo[] = {
7071 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
7072 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
7073 .access = PL1_RW, .accessfn = access_lor_other,
b19ed03c 7074 .fgt = FGT_LORSA_EL1,
d8564ee4
RH
7075 .type = ARM_CP_CONST, .resetvalue = 0 },
7076 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7077 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7078 .access = PL1_RW, .accessfn = access_lor_other,
b19ed03c 7079 .fgt = FGT_LOREA_EL1,
d8564ee4
RH
7080 .type = ARM_CP_CONST, .resetvalue = 0 },
7081 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7082 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7083 .access = PL1_RW, .accessfn = access_lor_other,
b19ed03c 7084 .fgt = FGT_LORN_EL1,
d8564ee4
RH
7085 .type = ARM_CP_CONST, .resetvalue = 0 },
7086 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7087 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7088 .access = PL1_RW, .accessfn = access_lor_other,
b19ed03c 7089 .fgt = FGT_LORC_EL1,
d8564ee4
RH
7090 .type = ARM_CP_CONST, .resetvalue = 0 },
7091 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7092 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
9bd268ba 7093 .access = PL1_R, .accessfn = access_lor_ns,
b19ed03c 7094 .fgt = FGT_LORID_EL1,
d8564ee4 7095 .type = ARM_CP_CONST, .resetvalue = 0 },
d8564ee4
RH
7096};
7097
967aa94f
RH
7098#ifdef TARGET_AARCH64
7099static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7100 bool isread)
7101{
7102 int el = arm_current_el(env);
7103
7104 if (el < 2 &&
07b034ea 7105 arm_is_el2_enabled(env) &&
967aa94f
RH
7106 !(arm_hcr_el2_eff(env) & HCR_APK)) {
7107 return CP_ACCESS_TRAP_EL2;
7108 }
7109 if (el < 3 &&
7110 arm_feature(env, ARM_FEATURE_EL3) &&
7111 !(env->cp15.scr_el3 & SCR_APK)) {
7112 return CP_ACCESS_TRAP_EL3;
7113 }
7114 return CP_ACCESS_OK;
7115}
7116
7117static const ARMCPRegInfo pauth_reginfo[] = {
7118 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7119 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7120 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7121 .fgt = FGT_APDAKEY,
108b3ba8 7122 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
967aa94f
RH
7123 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7124 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7125 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7126 .fgt = FGT_APDAKEY,
108b3ba8 7127 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
967aa94f
RH
7128 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7129 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7130 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7131 .fgt = FGT_APDBKEY,
108b3ba8 7132 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
967aa94f
RH
7133 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7134 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7135 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7136 .fgt = FGT_APDBKEY,
108b3ba8 7137 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
967aa94f
RH
7138 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7139 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7140 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7141 .fgt = FGT_APGAKEY,
108b3ba8 7142 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
967aa94f
RH
7143 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7144 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7145 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7146 .fgt = FGT_APGAKEY,
108b3ba8 7147 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
967aa94f
RH
7148 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7149 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7150 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7151 .fgt = FGT_APIAKEY,
108b3ba8 7152 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
967aa94f
RH
7153 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7154 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7155 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7156 .fgt = FGT_APIAKEY,
108b3ba8 7157 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
967aa94f
RH
7158 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7159 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7160 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7161 .fgt = FGT_APIBKEY,
108b3ba8 7162 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
967aa94f
RH
7163 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7164 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7165 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7166 .fgt = FGT_APIBKEY,
108b3ba8 7167 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
967aa94f 7168};
de390645 7169
84940ed8
RC
7170static const ARMCPRegInfo tlbirange_reginfo[] = {
7171 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7172 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
0f66d223 7173 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
84940ed8
RC
7174 .writefn = tlbi_aa64_rvae1is_write },
7175 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7176 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
0f66d223 7177 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
84940ed8
RC
7178 .writefn = tlbi_aa64_rvae1is_write },
7179 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7180 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
0f66d223 7181 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
84940ed8
RC
7182 .writefn = tlbi_aa64_rvae1is_write },
7183 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7184 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
0f66d223 7185 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
84940ed8
RC
7186 .writefn = tlbi_aa64_rvae1is_write },
7187 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7188 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
fe3ca86c 7189 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
84940ed8
RC
7190 .writefn = tlbi_aa64_rvae1is_write },
7191 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7192 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
fe3ca86c 7193 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
84940ed8
RC
7194 .writefn = tlbi_aa64_rvae1is_write },
7195 { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64,
7196 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5,
fe3ca86c 7197 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
84940ed8
RC
7198 .writefn = tlbi_aa64_rvae1is_write },
7199 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7200 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
fe3ca86c 7201 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
84940ed8
RC
7202 .writefn = tlbi_aa64_rvae1is_write },
7203 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7204 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
4870f38b 7205 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
84940ed8
RC
7206 .writefn = tlbi_aa64_rvae1_write },
7207 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7208 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
4870f38b 7209 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
84940ed8
RC
7210 .writefn = tlbi_aa64_rvae1_write },
7211 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7212 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
4870f38b 7213 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
84940ed8
RC
7214 .writefn = tlbi_aa64_rvae1_write },
7215 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7216 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
4870f38b 7217 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
84940ed8
RC
7218 .writefn = tlbi_aa64_rvae1_write },
7219 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7220 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
575a94af
RH
7221 .access = PL2_W, .type = ARM_CP_NO_RAW,
7222 .writefn = tlbi_aa64_ripas2e1is_write },
84940ed8
RC
7223 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7224 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
575a94af
RH
7225 .access = PL2_W, .type = ARM_CP_NO_RAW,
7226 .writefn = tlbi_aa64_ripas2e1is_write },
84940ed8
RC
7227 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7228 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
696ba377 7229 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7230 .writefn = tlbi_aa64_rvae2is_write },
7231 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7232 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
696ba377 7233 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7234 .writefn = tlbi_aa64_rvae2is_write },
7235 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7236 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
575a94af
RH
7237 .access = PL2_W, .type = ARM_CP_NO_RAW,
7238 .writefn = tlbi_aa64_ripas2e1_write },
7239 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
84940ed8 7240 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
575a94af
RH
7241 .access = PL2_W, .type = ARM_CP_NO_RAW,
7242 .writefn = tlbi_aa64_ripas2e1_write },
84940ed8
RC
7243 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7244 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
696ba377 7245 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7246 .writefn = tlbi_aa64_rvae2is_write },
7247 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7248 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
696ba377 7249 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7250 .writefn = tlbi_aa64_rvae2is_write },
7251 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7252 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
696ba377 7253 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7254 .writefn = tlbi_aa64_rvae2_write },
7255 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7256 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
696ba377 7257 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7258 .writefn = tlbi_aa64_rvae2_write },
7259 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7260 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7261 .access = PL3_W, .type = ARM_CP_NO_RAW,
7262 .writefn = tlbi_aa64_rvae3is_write },
7263 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7264 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7265 .access = PL3_W, .type = ARM_CP_NO_RAW,
7266 .writefn = tlbi_aa64_rvae3is_write },
7267 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7268 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7269 .access = PL3_W, .type = ARM_CP_NO_RAW,
7270 .writefn = tlbi_aa64_rvae3is_write },
7271 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7272 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7273 .access = PL3_W, .type = ARM_CP_NO_RAW,
7274 .writefn = tlbi_aa64_rvae3is_write },
7275 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7276 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7277 .access = PL3_W, .type = ARM_CP_NO_RAW,
7278 .writefn = tlbi_aa64_rvae3_write },
7279 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7280 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7281 .access = PL3_W, .type = ARM_CP_NO_RAW,
7282 .writefn = tlbi_aa64_rvae3_write },
84940ed8
RC
7283};
7284
7113d618
RC
7285static const ARMCPRegInfo tlbios_reginfo[] = {
7286 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7287 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
fe3ca86c 7288 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7113d618 7289 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
7290 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7291 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
fe3ca86c 7292 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
b7469ef9 7293 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
7294 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7295 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
fe3ca86c 7296 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
7113d618 7297 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
7298 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7299 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
fe3ca86c 7300 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
b7469ef9
IH
7301 .writefn = tlbi_aa64_vae1is_write },
7302 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7303 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
fe3ca86c 7304 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
b7469ef9
IH
7305 .writefn = tlbi_aa64_vae1is_write },
7306 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7307 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
fe3ca86c 7308 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
b7469ef9 7309 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
7310 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7311 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
696ba377 7312 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7113d618 7313 .writefn = tlbi_aa64_alle2is_write },
b7469ef9
IH
7314 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7315 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
696ba377 7316 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
b7469ef9 7317 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
7318 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7319 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7320 .access = PL2_W, .type = ARM_CP_NO_RAW,
7321 .writefn = tlbi_aa64_alle1is_write },
b7469ef9
IH
7322 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7323 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
696ba377 7324 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
b7469ef9 7325 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
7326 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7327 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7328 .access = PL2_W, .type = ARM_CP_NO_RAW,
7329 .writefn = tlbi_aa64_alle1is_write },
7330 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7331 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7332 .access = PL2_W, .type = ARM_CP_NOP },
7333 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7334 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7335 .access = PL2_W, .type = ARM_CP_NOP },
7336 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7337 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7338 .access = PL2_W, .type = ARM_CP_NOP },
7339 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7340 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7341 .access = PL2_W, .type = ARM_CP_NOP },
7342 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7343 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7344 .access = PL3_W, .type = ARM_CP_NO_RAW,
7345 .writefn = tlbi_aa64_alle3is_write },
b7469ef9
IH
7346 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7347 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7348 .access = PL3_W, .type = ARM_CP_NO_RAW,
7349 .writefn = tlbi_aa64_vae3is_write },
7350 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7351 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7352 .access = PL3_W, .type = ARM_CP_NO_RAW,
7353 .writefn = tlbi_aa64_vae3is_write },
7113d618
RC
7354};
7355
de390645
RH
7356static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7357{
7358 Error *err = NULL;
7359 uint64_t ret;
7360
7361 /* Success sets NZCV = 0000. */
7362 env->NF = env->CF = env->VF = 0, env->ZF = 1;
7363
7364 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7365 /*
7366 * ??? Failed, for unknown reasons in the crypto subsystem.
7367 * The best we can do is log the reason and return the
7368 * timed-out indication to the guest. There is no reason
7369 * we know to expect this failure to be transitory, so the
7370 * guest may well hang retrying the operation.
7371 */
7372 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7373 ri->name, error_get_pretty(err));
7374 error_free(err);
7375
7376 env->ZF = 0; /* NZCF = 0100 */
7377 return 0;
7378 }
7379 return ret;
7380}
7381
7382/* We do not support re-seeding, so the two registers operate the same. */
7383static const ARMCPRegInfo rndr_reginfo[] = {
7384 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7385 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7386 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7387 .access = PL0_R, .readfn = rndr_readfn },
7388 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7389 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7390 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7391 .access = PL0_R, .readfn = rndr_readfn },
de390645 7392};
0d57b499
BM
7393
7394#ifndef CONFIG_USER_ONLY
7395static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7396 uint64_t value)
7397{
7398 ARMCPU *cpu = env_archcpu(env);
7399 /* CTR_EL0 System register -> DminLine, bits [19:16] */
7400 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7401 uint64_t vaddr_in = (uint64_t) value;
7402 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7403 void *haddr;
7404 int mem_idx = cpu_mmu_index(env, false);
7405
7406 /* This won't be crossing page boundaries */
7407 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7408 if (haddr) {
7409
7410 ram_addr_t offset;
7411 MemoryRegion *mr;
7412
7413 /* RCU lock is already being held */
7414 mr = memory_region_from_host(haddr, &offset);
7415
7416 if (mr) {
4dfe59d1 7417 memory_region_writeback(mr, offset, dline_size);
0d57b499
BM
7418 }
7419 }
7420}
7421
7422static const ARMCPRegInfo dcpop_reg[] = {
7423 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7424 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7425 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
dd345653 7426 .fgt = FGT_DCCVAP,
1bed4d2e 7427 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
7428};
7429
7430static const ARMCPRegInfo dcpodp_reg[] = {
7431 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7432 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7433 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
dd345653 7434 .fgt = FGT_DCCVADP,
1bed4d2e 7435 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
7436};
7437#endif /*CONFIG_USER_ONLY*/
7438
4b779ceb
RH
7439static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7440 bool isread)
7441{
7442 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7443 return CP_ACCESS_TRAP_EL2;
7444 }
7445
7446 return CP_ACCESS_OK;
7447}
7448
7449static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7450 bool isread)
7451{
7452 int el = arm_current_el(env);
7453
0da067f2 7454 if (el < 2 && arm_is_el2_enabled(env)) {
4301acd7
RH
7455 uint64_t hcr = arm_hcr_el2_eff(env);
7456 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7457 return CP_ACCESS_TRAP_EL2;
7458 }
4b779ceb
RH
7459 }
7460 if (el < 3 &&
7461 arm_feature(env, ARM_FEATURE_EL3) &&
7462 !(env->cp15.scr_el3 & SCR_ATA)) {
7463 return CP_ACCESS_TRAP_EL3;
7464 }
7465 return CP_ACCESS_OK;
7466}
7467
7468static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7469{
7470 return env->pstate & PSTATE_TCO;
7471}
7472
7473static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7474{
7475 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7476}
7477
7478static const ARMCPRegInfo mte_reginfo[] = {
7479 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7480 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7481 .access = PL1_RW, .accessfn = access_mte,
7482 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7483 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7484 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7485 .access = PL1_RW, .accessfn = access_mte,
7486 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7487 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7488 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7489 .access = PL2_RW, .accessfn = access_mte,
7490 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7491 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7492 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7493 .access = PL3_RW,
7494 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7495 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7496 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7497 .access = PL1_RW, .accessfn = access_mte,
7498 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7499 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7500 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7501 .access = PL1_RW, .accessfn = access_mte,
7502 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7503 { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7504 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7505 .access = PL1_R, .accessfn = access_aa64_tid5,
7506 .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7507 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7508 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7509 .type = ARM_CP_NO_RAW,
7510 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
5463df16
RH
7511 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7512 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7513 .type = ARM_CP_NOP, .access = PL1_W,
dd345653 7514 .fgt = FGT_DCIVAC,
5463df16
RH
7515 .accessfn = aa64_cacheop_poc_access },
7516 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7517 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
dd345653 7518 .fgt = FGT_DCISW,
5463df16
RH
7519 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7520 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7521 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7522 .type = ARM_CP_NOP, .access = PL1_W,
dd345653 7523 .fgt = FGT_DCIVAC,
5463df16
RH
7524 .accessfn = aa64_cacheop_poc_access },
7525 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7526 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
dd345653 7527 .fgt = FGT_DCISW,
5463df16
RH
7528 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7529 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7530 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
dd345653 7531 .fgt = FGT_DCCSW,
5463df16
RH
7532 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7533 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7534 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
dd345653 7535 .fgt = FGT_DCCSW,
5463df16
RH
7536 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7537 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7538 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
dd345653 7539 .fgt = FGT_DCCISW,
5463df16
RH
7540 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7541 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7542 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
dd345653 7543 .fgt = FGT_DCCISW,
5463df16 7544 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
4b779ceb
RH
7545};
7546
7547static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7548 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7549 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7550 .type = ARM_CP_CONST, .access = PL0_RW, },
4b779ceb 7551};
5463df16
RH
7552
7553static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7554 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7555 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7556 .type = ARM_CP_NOP, .access = PL0_W,
7557 .accessfn = aa64_cacheop_poc_access },
7558 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7559 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7560 .type = ARM_CP_NOP, .access = PL0_W,
7561 .accessfn = aa64_cacheop_poc_access },
7562 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7563 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7564 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7565 .fgt = FGT_DCCVAP,
5463df16
RH
7566 .accessfn = aa64_cacheop_poc_access },
7567 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7568 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7569 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7570 .fgt = FGT_DCCVAP,
5463df16
RH
7571 .accessfn = aa64_cacheop_poc_access },
7572 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7573 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7574 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7575 .fgt = FGT_DCCVADP,
5463df16
RH
7576 .accessfn = aa64_cacheop_poc_access },
7577 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7578 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7579 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7580 .fgt = FGT_DCCVADP,
5463df16
RH
7581 .accessfn = aa64_cacheop_poc_access },
7582 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7583 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7584 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7585 .fgt = FGT_DCCIVAC,
5463df16
RH
7586 .accessfn = aa64_cacheop_poc_access },
7587 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7588 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7589 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7590 .fgt = FGT_DCCIVAC,
5463df16 7591 .accessfn = aa64_cacheop_poc_access },
eb821168
RH
7592 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7593 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7594 .access = PL0_W, .type = ARM_CP_DC_GVA,
7595#ifndef CONFIG_USER_ONLY
7596 /* Avoid overhead of an access check that always passes in user-mode */
7597 .accessfn = aa64_zva_access,
dd345653 7598 .fgt = FGT_DCZVA,
eb821168
RH
7599#endif
7600 },
7601 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7602 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7603 .access = PL0_W, .type = ARM_CP_DC_GZVA,
7604#ifndef CONFIG_USER_ONLY
7605 /* Avoid overhead of an access check that always passes in user-mode */
7606 .accessfn = aa64_zva_access,
dd345653 7607 .fgt = FGT_DCZVA,
eb821168
RH
7608#endif
7609 },
5463df16
RH
7610};
7611
7cb1e618
RH
7612static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7613 bool isread)
7614{
7615 uint64_t hcr = arm_hcr_el2_eff(env);
7616 int el = arm_current_el(env);
7617
7618 if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7619 if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7620 if (hcr & HCR_TGE) {
7621 return CP_ACCESS_TRAP_EL2;
7622 }
7623 return CP_ACCESS_TRAP;
7624 }
7625 } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7626 return CP_ACCESS_TRAP_EL2;
7627 }
7628 if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7629 return CP_ACCESS_TRAP_EL2;
7630 }
7631 if (el < 3
7632 && arm_feature(env, ARM_FEATURE_EL3)
7633 && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7634 return CP_ACCESS_TRAP_EL3;
7635 }
7636 return CP_ACCESS_OK;
7637}
7638
7639static const ARMCPRegInfo scxtnum_reginfo[] = {
7640 { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7641 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7642 .access = PL0_RW, .accessfn = access_scxtnum,
67dd8030 7643 .fgt = FGT_SCXTNUM_EL0,
7cb1e618
RH
7644 .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7645 { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7646 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7647 .access = PL1_RW, .accessfn = access_scxtnum,
67dd8030 7648 .fgt = FGT_SCXTNUM_EL1,
7cb1e618
RH
7649 .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7650 { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7651 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7652 .access = PL2_RW, .accessfn = access_scxtnum,
7653 .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7654 { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7655 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7656 .access = PL3_RW,
7657 .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7658};
15126d9c
PM
7659
7660static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
7661 bool isread)
7662{
7663 if (arm_current_el(env) == 2 &&
7664 arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
7665 return CP_ACCESS_TRAP_EL3;
7666 }
7667 return CP_ACCESS_OK;
7668}
7669
7670static const ARMCPRegInfo fgt_reginfo[] = {
7671 { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7672 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
7673 .access = PL2_RW, .accessfn = access_fgt,
7674 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
7675 { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7676 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
7677 .access = PL2_RW, .accessfn = access_fgt,
7678 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
7679 { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7680 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
7681 .access = PL2_RW, .accessfn = access_fgt,
7682 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
7683 { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7684 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
7685 .access = PL2_RW, .accessfn = access_fgt,
7686 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
7687 { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
7688 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
7689 .access = PL2_RW, .accessfn = access_fgt,
7690 .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
7691};
7cb1e618 7692#endif /* TARGET_AARCH64 */
967aa94f 7693
cb570bd3
RH
7694static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7695 bool isread)
7696{
7697 int el = arm_current_el(env);
7698
7699 if (el == 0) {
7700 uint64_t sctlr = arm_sctlr(env, el);
7701 if (!(sctlr & SCTLR_EnRCTX)) {
7702 return CP_ACCESS_TRAP;
7703 }
7704 } else if (el == 1) {
7705 uint64_t hcr = arm_hcr_el2_eff(env);
7706 if (hcr & HCR_NV) {
7707 return CP_ACCESS_TRAP_EL2;
7708 }
7709 }
7710 return CP_ACCESS_OK;
7711}
7712
7713static const ARMCPRegInfo predinv_reginfo[] = {
7714 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7715 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
7716 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7717 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7718 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
7719 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7720 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7721 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
7722 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7723 /*
7724 * Note the AArch32 opcodes have a different OPC1.
7725 */
7726 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7727 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
7728 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7729 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7730 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
7731 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7732 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7733 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
7734 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
cb570bd3
RH
7735};
7736
957e6155
PM
7737static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7738{
7739 /* Read the high 32 bits of the current CCSIDR */
7740 return extract64(ccsidr_read(env, ri), 32, 32);
7741}
7742
7743static const ARMCPRegInfo ccsidr2_reginfo[] = {
7744 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7745 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7746 .access = PL1_R,
e2ce5fcd 7747 .accessfn = access_tid4,
957e6155 7748 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
957e6155
PM
7749};
7750
6a4ef4e5
MZ
7751static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7752 bool isread)
7753{
7754 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7755 return CP_ACCESS_TRAP_EL2;
7756 }
7757
7758 return CP_ACCESS_OK;
7759}
7760
7761static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7762 bool isread)
7763{
7764 if (arm_feature(env, ARM_FEATURE_V8)) {
7765 return access_aa64_tid3(env, ri, isread);
7766 }
7767
7768 return CP_ACCESS_OK;
7769}
7770
f96f3d5f
MZ
7771static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7772 bool isread)
7773{
7774 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7775 return CP_ACCESS_TRAP_EL2;
7776 }
7777
7778 return CP_ACCESS_OK;
7779}
7780
8e228c9e
PM
7781static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7782 const ARMCPRegInfo *ri, bool isread)
7783{
7784 /*
7785 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7786 * in v7A, not in v8A.
7787 */
7788 if (!arm_feature(env, ARM_FEATURE_V8) &&
7789 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7790 (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7791 return CP_ACCESS_TRAP_EL2;
7792 }
7793 return CP_ACCESS_OK;
7794}
7795
f96f3d5f
MZ
7796static const ARMCPRegInfo jazelle_regs[] = {
7797 { .name = "JIDR",
7798 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7799 .access = PL1_R, .accessfn = access_jazelle,
7800 .type = ARM_CP_CONST, .resetvalue = 0 },
7801 { .name = "JOSCR",
7802 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7803 .accessfn = access_joscr_jmcr,
f96f3d5f
MZ
7804 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7805 { .name = "JMCR",
7806 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7807 .accessfn = access_joscr_jmcr,
f96f3d5f 7808 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
f96f3d5f
MZ
7809};
7810
52d18727
RH
7811static const ARMCPRegInfo contextidr_el2 = {
7812 .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7813 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7814 .access = PL2_RW,
7815 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7816};
7817
e2a1a461 7818static const ARMCPRegInfo vhe_reginfo[] = {
ed30da8e
RH
7819 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7820 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7821 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7822 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8c94b071
RH
7823#ifndef CONFIG_USER_ONLY
7824 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7825 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7826 .fieldoffset =
7827 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7828 .type = ARM_CP_IO, .access = PL2_RW,
7829 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7830 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7831 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7832 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7833 .resetfn = gt_hv_timer_reset,
7834 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7835 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7836 .type = ARM_CP_IO,
7837 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7838 .access = PL2_RW,
7839 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7840 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
bb5972e4
RH
7841 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7842 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7843 .type = ARM_CP_IO | ARM_CP_ALIAS,
7844 .access = PL2_RW, .accessfn = e2h_access,
7845 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7846 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7847 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7848 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7849 .type = ARM_CP_IO | ARM_CP_ALIAS,
7850 .access = PL2_RW, .accessfn = e2h_access,
7851 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7852 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7853 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7854 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7855 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7856 .access = PL2_RW, .accessfn = e2h_access,
7857 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7858 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7859 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7860 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7861 .access = PL2_RW, .accessfn = e2h_access,
7862 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7863 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7864 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7865 .type = ARM_CP_IO | ARM_CP_ALIAS,
7866 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7867 .access = PL2_RW, .accessfn = e2h_access,
7868 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7869 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7870 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7871 .type = ARM_CP_IO | ARM_CP_ALIAS,
7872 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7873 .access = PL2_RW, .accessfn = e2h_access,
7874 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8c94b071 7875#endif
e2a1a461
RH
7876};
7877
04b07d29
RH
7878#ifndef CONFIG_USER_ONLY
7879static const ARMCPRegInfo ats1e1_reginfo[] = {
3999d2d2 7880 { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
04b07d29
RH
7881 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7882 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7883 .writefn = ats_write64 },
3999d2d2 7884 { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
04b07d29
RH
7885 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7886 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7887 .writefn = ats_write64 },
04b07d29
RH
7888};
7889
7890static const ARMCPRegInfo ats1cp_reginfo[] = {
7891 { .name = "ATS1CPRP",
7892 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7893 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7894 .writefn = ats_write },
7895 { .name = "ATS1CPWP",
7896 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7897 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7898 .writefn = ats_write },
04b07d29
RH
7899};
7900#endif
7901
f6287c24
PM
7902/*
7903 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7904 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7905 * is non-zero, which is never for ARMv7, optionally in ARMv8
7906 * and mandatorily for ARMv8.2 and up.
7907 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7908 * implementation is RAZ/WI we can ignore this detail, as we
7909 * do for ACTLR.
7910 */
7911static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7912 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7913 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
99602377
RH
7914 .access = PL1_RW, .accessfn = access_tacr,
7915 .type = ARM_CP_CONST, .resetvalue = 0 },
f6287c24
PM
7916 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7917 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7918 .access = PL2_RW, .type = ARM_CP_CONST,
7919 .resetvalue = 0 },
f6287c24
PM
7920};
7921
2ceb98c0
PM
7922void register_cp_regs_for_features(ARMCPU *cpu)
7923{
7924 /* Register all the coprocessor registers based on feature bits */
7925 CPUARMState *env = &cpu->env;
7926 if (arm_feature(env, ARM_FEATURE_M)) {
7927 /* M profile has no coprocessor registers */
7928 return;
7929 }
7930
e9aa6c21 7931 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6 7932 if (!arm_feature(env, ARM_FEATURE_V8)) {
9b37a28c
FR
7933 /*
7934 * Must go early as it is full of wildcards that may be
9449fdf6
PM
7935 * overridden by later definitions.
7936 */
7937 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7938 }
7939
7d57f408 7940 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
7941 /* The ID registers all have impdef reset values */
7942 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
7943 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7944 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7945 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7946 .accessfn = access_aa32_tid3,
8a130a7b 7947 .resetvalue = cpu->isar.id_pfr0 },
9b37a28c
FR
7948 /*
7949 * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
96a8b92e
PM
7950 * the value of the GIC field until after we define these regs.
7951 */
0ff644a7
PM
7952 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7953 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e 7954 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 7955 .accessfn = access_aa32_tid3,
96a8b92e
PM
7956 .readfn = id_pfr1_read,
7957 .writefn = arm_cp_write_ignore },
0ff644a7
PM
7958 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7959 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7960 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7961 .accessfn = access_aa32_tid3,
a6179538 7962 .resetvalue = cpu->isar.id_dfr0 },
0ff644a7
PM
7963 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7964 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7965 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7966 .accessfn = access_aa32_tid3,
8515a092 7967 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
7968 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7969 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
7970 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7971 .accessfn = access_aa32_tid3,
10054016 7972 .resetvalue = cpu->isar.id_mmfr0 },
0ff644a7
PM
7973 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
7974 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
7975 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7976 .accessfn = access_aa32_tid3,
10054016 7977 .resetvalue = cpu->isar.id_mmfr1 },
0ff644a7
PM
7978 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
7979 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
7980 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7981 .accessfn = access_aa32_tid3,
10054016 7982 .resetvalue = cpu->isar.id_mmfr2 },
0ff644a7
PM
7983 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
7984 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
7985 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7986 .accessfn = access_aa32_tid3,
10054016 7987 .resetvalue = cpu->isar.id_mmfr3 },
0ff644a7
PM
7988 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
7989 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
7990 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7991 .accessfn = access_aa32_tid3,
47576b94 7992 .resetvalue = cpu->isar.id_isar0 },
0ff644a7
PM
7993 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
7994 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
7995 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7996 .accessfn = access_aa32_tid3,
47576b94 7997 .resetvalue = cpu->isar.id_isar1 },
0ff644a7
PM
7998 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
7999 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
8000 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8001 .accessfn = access_aa32_tid3,
47576b94 8002 .resetvalue = cpu->isar.id_isar2 },
0ff644a7
PM
8003 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
8004 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
8005 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8006 .accessfn = access_aa32_tid3,
47576b94 8007 .resetvalue = cpu->isar.id_isar3 },
0ff644a7
PM
8008 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
8009 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
8010 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8011 .accessfn = access_aa32_tid3,
47576b94 8012 .resetvalue = cpu->isar.id_isar4 },
0ff644a7
PM
8013 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
8014 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
8015 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8016 .accessfn = access_aa32_tid3,
47576b94 8017 .resetvalue = cpu->isar.id_isar5 },
e20d84c1
PM
8018 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
8019 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
8020 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8021 .accessfn = access_aa32_tid3,
10054016 8022 .resetvalue = cpu->isar.id_mmfr4 },
802abf40 8023 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8024 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
8025 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8026 .accessfn = access_aa32_tid3,
47576b94 8027 .resetvalue = cpu->isar.id_isar6 },
8515a092
PM
8028 };
8029 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
8030 define_arm_cp_regs(cpu, v6_cp_reginfo);
8031 } else {
8032 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
8033 }
4d31c596
PM
8034 if (arm_feature(env, ARM_FEATURE_V6K)) {
8035 define_arm_cp_regs(cpu, v6k_cp_reginfo);
8036 }
5e5cf9e3 8037 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 8038 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
8039 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
8040 }
327dd510
AL
8041 if (arm_feature(env, ARM_FEATURE_V7VE)) {
8042 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
8043 }
e9aa6c21 8044 if (arm_feature(env, ARM_FEATURE_V7)) {
776d4e5c 8045 ARMCPRegInfo clidr = {
7da845b0
PM
8046 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
8047 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
630fcd4d 8048 .access = PL1_R, .type = ARM_CP_CONST,
e2ce5fcd 8049 .accessfn = access_tid4,
158c276c 8050 .fgt = FGT_CLIDR_EL1,
630fcd4d 8051 .resetvalue = cpu->clidr
776d4e5c 8052 };
776d4e5c 8053 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 8054 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 8055 define_debug_regs(cpu);
24183fb6 8056 define_pmu_regs(cpu);
7d57f408
PM
8057 } else {
8058 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 8059 }
b0d2b7d0 8060 if (arm_feature(env, ARM_FEATURE_V8)) {
dde4d028
PM
8061 /*
8062 * v8 ID registers, which all have impdef reset values.
e20d84c1
PM
8063 * Note that within the ID register ranges the unused slots
8064 * must all RAZ, not UNDEF; future architecture versions may
8065 * define new registers here.
dde4d028
PM
8066 * ID registers which are AArch64 views of the AArch32 ID registers
8067 * which already existed in v6 and v7 are handled elsewhere,
8068 * in v6_idregs[].
e20d84c1 8069 */
dde4d028 8070 int i;
e60cef86 8071 ARMCPRegInfo v8_idregs[] = {
976b99b6
AB
8072 /*
8073 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
8074 * emulation because we don't know the right value for the
8075 * GIC field until after we define these regs.
96a8b92e 8076 */
e60cef86
PM
8077 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
8078 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
976b99b6
AB
8079 .access = PL1_R,
8080#ifdef CONFIG_USER_ONLY
8081 .type = ARM_CP_CONST,
8082 .resetvalue = cpu->isar.id_aa64pfr0
8083#else
8084 .type = ARM_CP_NO_RAW,
6a4ef4e5 8085 .accessfn = access_aa64_tid3,
96a8b92e 8086 .readfn = id_aa64pfr0_read,
976b99b6
AB
8087 .writefn = arm_cp_write_ignore
8088#endif
8089 },
e60cef86
PM
8090 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
8091 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
8092 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8093 .accessfn = access_aa64_tid3,
47576b94 8094 .resetvalue = cpu->isar.id_aa64pfr1},
e20d84c1
PM
8095 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8096 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
8097 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8098 .accessfn = access_aa64_tid3,
e20d84c1
PM
8099 .resetvalue = 0 },
8100 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8101 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
8102 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8103 .accessfn = access_aa64_tid3,
e20d84c1 8104 .resetvalue = 0 },
9516d772 8105 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
8106 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
8107 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8108 .accessfn = access_aa64_tid3,
2dc10fa2 8109 .resetvalue = cpu->isar.id_aa64zfr0 },
414c54d5 8110 { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
8111 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
8112 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8113 .accessfn = access_aa64_tid3,
414c54d5 8114 .resetvalue = cpu->isar.id_aa64smfr0 },
e20d84c1
PM
8115 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8116 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
8117 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8118 .accessfn = access_aa64_tid3,
e20d84c1
PM
8119 .resetvalue = 0 },
8120 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8121 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
8122 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8123 .accessfn = access_aa64_tid3,
e20d84c1 8124 .resetvalue = 0 },
e60cef86
PM
8125 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
8126 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
8127 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8128 .accessfn = access_aa64_tid3,
2a609df8 8129 .resetvalue = cpu->isar.id_aa64dfr0 },
e60cef86
PM
8130 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
8131 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
8132 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8133 .accessfn = access_aa64_tid3,
2a609df8 8134 .resetvalue = cpu->isar.id_aa64dfr1 },
e20d84c1
PM
8135 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8136 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
8137 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8138 .accessfn = access_aa64_tid3,
e20d84c1
PM
8139 .resetvalue = 0 },
8140 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8141 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
8142 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8143 .accessfn = access_aa64_tid3,
e20d84c1 8144 .resetvalue = 0 },
e60cef86
PM
8145 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
8146 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
8147 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8148 .accessfn = access_aa64_tid3,
e60cef86
PM
8149 .resetvalue = cpu->id_aa64afr0 },
8150 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
8151 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
8152 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8153 .accessfn = access_aa64_tid3,
e60cef86 8154 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
8155 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8156 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
8157 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8158 .accessfn = access_aa64_tid3,
e20d84c1
PM
8159 .resetvalue = 0 },
8160 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8161 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8162 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8163 .accessfn = access_aa64_tid3,
e20d84c1 8164 .resetvalue = 0 },
e60cef86
PM
8165 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8166 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8167 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8168 .accessfn = access_aa64_tid3,
47576b94 8169 .resetvalue = cpu->isar.id_aa64isar0 },
e60cef86
PM
8170 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8171 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8172 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8173 .accessfn = access_aa64_tid3,
47576b94 8174 .resetvalue = cpu->isar.id_aa64isar1 },
e20d84c1
PM
8175 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8176 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8177 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8178 .accessfn = access_aa64_tid3,
e20d84c1
PM
8179 .resetvalue = 0 },
8180 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8181 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8182 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8183 .accessfn = access_aa64_tid3,
e20d84c1
PM
8184 .resetvalue = 0 },
8185 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8186 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8187 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8188 .accessfn = access_aa64_tid3,
e20d84c1
PM
8189 .resetvalue = 0 },
8190 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8191 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8192 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8193 .accessfn = access_aa64_tid3,
e20d84c1
PM
8194 .resetvalue = 0 },
8195 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8196 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8197 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8198 .accessfn = access_aa64_tid3,
e20d84c1
PM
8199 .resetvalue = 0 },
8200 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8201 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8202 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8203 .accessfn = access_aa64_tid3,
e20d84c1 8204 .resetvalue = 0 },
e60cef86
PM
8205 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8206 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8207 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8208 .accessfn = access_aa64_tid3,
3dc91ddb 8209 .resetvalue = cpu->isar.id_aa64mmfr0 },
e60cef86
PM
8210 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8211 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8212 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8213 .accessfn = access_aa64_tid3,
3dc91ddb 8214 .resetvalue = cpu->isar.id_aa64mmfr1 },
64761e10 8215 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
8216 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8217 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8218 .accessfn = access_aa64_tid3,
64761e10 8219 .resetvalue = cpu->isar.id_aa64mmfr2 },
e20d84c1
PM
8220 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8221 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8222 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8223 .accessfn = access_aa64_tid3,
e20d84c1
PM
8224 .resetvalue = 0 },
8225 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8226 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8227 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8228 .accessfn = access_aa64_tid3,
e20d84c1
PM
8229 .resetvalue = 0 },
8230 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8231 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8232 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8233 .accessfn = access_aa64_tid3,
e20d84c1
PM
8234 .resetvalue = 0 },
8235 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8236 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8237 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8238 .accessfn = access_aa64_tid3,
e20d84c1
PM
8239 .resetvalue = 0 },
8240 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8241 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8242 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8243 .accessfn = access_aa64_tid3,
e20d84c1 8244 .resetvalue = 0 },
a50c0f51
PM
8245 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8246 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8247 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8248 .accessfn = access_aa64_tid3,
47576b94 8249 .resetvalue = cpu->isar.mvfr0 },
a50c0f51
PM
8250 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8251 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8252 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8253 .accessfn = access_aa64_tid3,
47576b94 8254 .resetvalue = cpu->isar.mvfr1 },
a50c0f51
PM
8255 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8256 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8257 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8258 .accessfn = access_aa64_tid3,
47576b94 8259 .resetvalue = cpu->isar.mvfr2 },
dde4d028
PM
8260 /*
8261 * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8262 * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8263 * as RAZ, since it is in the "reserved for future ID
8264 * registers, RAZ" part of the AArch32 encoding space.
8265 */
8266 { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8267 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8268 .access = PL1_R, .type = ARM_CP_CONST,
8269 .accessfn = access_aa64_tid3,
8270 .resetvalue = 0 },
8271 { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8272 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8273 .access = PL1_R, .type = ARM_CP_CONST,
8274 .accessfn = access_aa64_tid3,
8275 .resetvalue = 0 },
8276 { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8277 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8278 .access = PL1_R, .type = ARM_CP_CONST,
8279 .accessfn = access_aa64_tid3,
8280 .resetvalue = 0 },
8281 /*
8282 * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8283 * they're also RAZ for AArch64, and in v8 are gradually
8284 * being filled with AArch64-view-of-AArch32-ID-register
8285 * for new ID registers.
8286 */
8287 { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8288 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8289 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8290 .accessfn = access_aa64_tid3,
e20d84c1 8291 .resetvalue = 0 },
1d51bc96 8292 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8293 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8294 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8295 .accessfn = access_aa64_tid3,
1d51bc96 8296 .resetvalue = cpu->isar.id_pfr2 },
d22c5649 8297 { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8298 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8299 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8300 .accessfn = access_aa64_tid3,
d22c5649 8301 .resetvalue = cpu->isar.id_dfr1 },
32957aad 8302 { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8303 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8304 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8305 .accessfn = access_aa64_tid3,
32957aad 8306 .resetvalue = cpu->isar.id_mmfr5 },
dde4d028 8307 { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8308 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8309 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8310 .accessfn = access_aa64_tid3,
e20d84c1 8311 .resetvalue = 0 },
4054bfa9
AF
8312 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8313 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8314 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 8315 .fgt = FGT_PMCEIDN_EL0,
cad86737 8316 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
4054bfa9
AF
8317 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8318 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8319 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 8320 .fgt = FGT_PMCEIDN_EL0,
4054bfa9
AF
8321 .resetvalue = cpu->pmceid0 },
8322 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8323 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8324 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 8325 .fgt = FGT_PMCEIDN_EL0,
cad86737 8326 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
4054bfa9
AF
8327 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8328 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8329 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 8330 .fgt = FGT_PMCEIDN_EL0,
4054bfa9 8331 .resetvalue = cpu->pmceid1 },
e60cef86 8332 };
6c5c0fec 8333#ifdef CONFIG_USER_ONLY
10b0220e 8334 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
6c5c0fec 8335 { .name = "ID_AA64PFR0_EL1",
bc6bd20e
ZS
8336 .exported_bits = R_ID_AA64PFR0_FP_MASK |
8337 R_ID_AA64PFR0_ADVSIMD_MASK |
8338 R_ID_AA64PFR0_SVE_MASK |
8339 R_ID_AA64PFR0_DIT_MASK,
8340 .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8341 (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
6c5c0fec 8342 { .name = "ID_AA64PFR1_EL1",
bc6bd20e
ZS
8343 .exported_bits = R_ID_AA64PFR1_BT_MASK |
8344 R_ID_AA64PFR1_SSBS_MASK |
8345 R_ID_AA64PFR1_MTE_MASK |
8346 R_ID_AA64PFR1_SME_MASK },
d040242e 8347 { .name = "ID_AA64PFR*_EL1_RESERVED",
bc6bd20e
ZS
8348 .is_glob = true },
8349 { .name = "ID_AA64ZFR0_EL1",
8350 .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8351 R_ID_AA64ZFR0_AES_MASK |
8352 R_ID_AA64ZFR0_BITPERM_MASK |
8353 R_ID_AA64ZFR0_BFLOAT16_MASK |
8354 R_ID_AA64ZFR0_SHA3_MASK |
8355 R_ID_AA64ZFR0_SM4_MASK |
8356 R_ID_AA64ZFR0_I8MM_MASK |
8357 R_ID_AA64ZFR0_F32MM_MASK |
8358 R_ID_AA64ZFR0_F64MM_MASK },
8359 { .name = "ID_AA64SMFR0_EL1",
8360 .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8361 R_ID_AA64SMFR0_B16F32_MASK |
8362 R_ID_AA64SMFR0_F16F32_MASK |
8363 R_ID_AA64SMFR0_I8I32_MASK |
8364 R_ID_AA64SMFR0_F64F64_MASK |
8365 R_ID_AA64SMFR0_I16I64_MASK |
8366 R_ID_AA64SMFR0_FA64_MASK },
6c5c0fec 8367 { .name = "ID_AA64MMFR0_EL1",
bc6bd20e
ZS
8368 .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8369 .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8370 (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8371 { .name = "ID_AA64MMFR1_EL1",
8372 .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8373 { .name = "ID_AA64MMFR2_EL1",
8374 .exported_bits = R_ID_AA64MMFR2_AT_MASK },
d040242e 8375 { .name = "ID_AA64MMFR*_EL1_RESERVED",
bc6bd20e 8376 .is_glob = true },
6c5c0fec 8377 { .name = "ID_AA64DFR0_EL1",
bc6bd20e
ZS
8378 .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8379 { .name = "ID_AA64DFR1_EL1" },
d040242e 8380 { .name = "ID_AA64DFR*_EL1_RESERVED",
bc6bd20e 8381 .is_glob = true },
d040242e 8382 { .name = "ID_AA64AFR*",
bc6bd20e 8383 .is_glob = true },
6c5c0fec 8384 { .name = "ID_AA64ISAR0_EL1",
bc6bd20e
ZS
8385 .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8386 R_ID_AA64ISAR0_SHA1_MASK |
8387 R_ID_AA64ISAR0_SHA2_MASK |
8388 R_ID_AA64ISAR0_CRC32_MASK |
8389 R_ID_AA64ISAR0_ATOMIC_MASK |
8390 R_ID_AA64ISAR0_RDM_MASK |
8391 R_ID_AA64ISAR0_SHA3_MASK |
8392 R_ID_AA64ISAR0_SM3_MASK |
8393 R_ID_AA64ISAR0_SM4_MASK |
8394 R_ID_AA64ISAR0_DP_MASK |
8395 R_ID_AA64ISAR0_FHM_MASK |
8396 R_ID_AA64ISAR0_TS_MASK |
8397 R_ID_AA64ISAR0_RNDR_MASK },
6c5c0fec 8398 { .name = "ID_AA64ISAR1_EL1",
bc6bd20e
ZS
8399 .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8400 R_ID_AA64ISAR1_APA_MASK |
8401 R_ID_AA64ISAR1_API_MASK |
8402 R_ID_AA64ISAR1_JSCVT_MASK |
8403 R_ID_AA64ISAR1_FCMA_MASK |
8404 R_ID_AA64ISAR1_LRCPC_MASK |
8405 R_ID_AA64ISAR1_GPA_MASK |
8406 R_ID_AA64ISAR1_GPI_MASK |
8407 R_ID_AA64ISAR1_FRINTTS_MASK |
8408 R_ID_AA64ISAR1_SB_MASK |
8409 R_ID_AA64ISAR1_BF16_MASK |
8410 R_ID_AA64ISAR1_DGH_MASK |
8411 R_ID_AA64ISAR1_I8MM_MASK },
8412 { .name = "ID_AA64ISAR2_EL1",
8413 .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8414 R_ID_AA64ISAR2_RPRES_MASK |
8415 R_ID_AA64ISAR2_GPA3_MASK |
8416 R_ID_AA64ISAR2_APA3_MASK },
d040242e 8417 { .name = "ID_AA64ISAR*_EL1_RESERVED",
bc6bd20e 8418 .is_glob = true },
6c5c0fec
AB
8419 };
8420 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8421#endif
be8e8128
GB
8422 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
8423 if (!arm_feature(env, ARM_FEATURE_EL3) &&
8424 !arm_feature(env, ARM_FEATURE_EL2)) {
8425 ARMCPRegInfo rvbar = {
910e4f24 8426 .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
be8e8128 8427 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
8428 .access = PL1_R,
8429 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
be8e8128
GB
8430 };
8431 define_one_arm_cp_reg(cpu, &rvbar);
8432 }
e60cef86 8433 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0 8434 define_arm_cp_regs(cpu, v8_cp_reginfo);
dde4d028
PM
8435
8436 for (i = 4; i < 16; i++) {
8437 /*
8438 * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8439 * For pre-v8 cores there are RAZ patterns for these in
8440 * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8441 * v8 extends the "must RAZ" part of the ID register space
8442 * to also cover c0, 0, c{8-15}, {0-7}.
8443 * These are STATE_AA32 because in the AArch64 sysreg space
8444 * c4-c7 is where the AArch64 ID registers live (and we've
8445 * already defined those in v8_idregs[]), and c8-c15 are not
8446 * "must RAZ" for AArch64.
8447 */
8448 g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8449 ARMCPRegInfo v8_aa32_raz_idregs = {
8450 .name = name,
8451 .state = ARM_CP_STATE_AA32,
8452 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8453 .access = PL1_R, .type = ARM_CP_CONST,
8454 .accessfn = access_aa64_tid3,
8455 .resetvalue = 0 };
8456 define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8457 }
b0d2b7d0 8458 }
99a90811
RH
8459
8460 /*
8461 * Register the base EL2 cpregs.
8462 * Pre v8, these registers are implemented only as part of the
8463 * Virtualization Extensions (EL2 present). Beginning with v8,
8464 * if EL2 is missing but EL3 is enabled, mostly these become
8465 * RES0 from EL3, with some specific exceptions.
8466 */
8467 if (arm_feature(env, ARM_FEATURE_EL2)
8468 || (arm_feature(env, ARM_FEATURE_EL3)
8469 && arm_feature(env, ARM_FEATURE_V8))) {
f0d574d6 8470 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
8471 ARMCPRegInfo vpidr_regs[] = {
8472 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8473 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8474 .access = PL2_RW, .accessfn = access_el3_aa32ns,
696ba377
RH
8475 .resetvalue = cpu->midr,
8476 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
36476562 8477 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
8478 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8479 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8480 .access = PL2_RW, .resetvalue = cpu->midr,
696ba377 8481 .type = ARM_CP_EL3_NO_EL2_C_NZ,
731de9e6 8482 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
8483 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8484 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8485 .access = PL2_RW, .accessfn = access_el3_aa32ns,
696ba377
RH
8486 .resetvalue = vmpidr_def,
8487 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
36476562 8488 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
8489 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8490 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
696ba377
RH
8491 .access = PL2_RW, .resetvalue = vmpidr_def,
8492 .type = ARM_CP_EL3_NO_EL2_C_NZ,
f0d574d6 8493 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6 8494 };
24526bb9
PM
8495 /*
8496 * The only field of MDCR_EL2 that has a defined architectural reset
8497 * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8498 */
8499 ARMCPRegInfo mdcr_el2 = {
7f4fbfb5 8500 .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
24526bb9 8501 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
01765386 8502 .writefn = mdcr_el2_write,
24526bb9
PM
8503 .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8504 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8505 };
8506 define_one_arm_cp_reg(cpu, &mdcr_el2);
731de9e6 8507 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 8508 define_arm_cp_regs(cpu, el2_cp_reginfo);
ce4afed8
PM
8509 if (arm_feature(env, ARM_FEATURE_V8)) {
8510 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8511 }
e9152ee9
RDC
8512 if (cpu_isar_feature(aa64_sel2, cpu)) {
8513 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8514 }
be8e8128
GB
8515 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
8516 if (!arm_feature(env, ARM_FEATURE_EL3)) {
910e4f24
TR
8517 ARMCPRegInfo rvbar[] = {
8518 {
8519 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8520 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8521 .access = PL2_R,
8522 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8523 },
8524 { .name = "RVBAR", .type = ARM_CP_ALIAS,
8525 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8526 .access = PL2_R,
8527 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8528 },
be8e8128 8529 };
910e4f24 8530 define_arm_cp_regs(cpu, rvbar);
be8e8128 8531 }
3b685ba7 8532 }
99a90811
RH
8533
8534 /* Register the base EL3 cpregs. */
81547d66 8535 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 8536 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
8537 ARMCPRegInfo el3_regs[] = {
8538 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8539 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
8540 .access = PL3_R,
8541 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8542 },
e24fdd23
PM
8543 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8544 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8545 .access = PL3_RW,
8546 .raw_writefn = raw_write, .writefn = sctlr_write,
8547 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8548 .resetvalue = cpu->reset_sctlr },
be8e8128 8549 };
e24fdd23
PM
8550
8551 define_arm_cp_regs(cpu, el3_regs);
81547d66 8552 }
9b37a28c
FR
8553 /*
8554 * The behaviour of NSACR is sufficiently various that we don't
2f027fc5
PM
8555 * try to describe it in a single reginfo:
8556 * if EL3 is 64 bit, then trap to EL3 from S EL1,
8557 * reads as constant 0xc00 from NS EL1 and NS EL2
8558 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8559 * if v7 without EL3, register doesn't exist
8560 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8561 */
8562 if (arm_feature(env, ARM_FEATURE_EL3)) {
8563 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
10b0220e 8564 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8565 .name = "NSACR", .type = ARM_CP_CONST,
8566 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8567 .access = PL1_RW, .accessfn = nsacr_access,
8568 .resetvalue = 0xc00
8569 };
8570 define_one_arm_cp_reg(cpu, &nsacr);
8571 } else {
10b0220e 8572 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8573 .name = "NSACR",
8574 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8575 .access = PL3_RW | PL1_R,
8576 .resetvalue = 0,
8577 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8578 };
8579 define_one_arm_cp_reg(cpu, &nsacr);
8580 }
8581 } else {
8582 if (arm_feature(env, ARM_FEATURE_V8)) {
10b0220e 8583 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8584 .name = "NSACR", .type = ARM_CP_CONST,
8585 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8586 .access = PL1_R,
8587 .resetvalue = 0xc00
8588 };
8589 define_one_arm_cp_reg(cpu, &nsacr);
8590 }
8591 }
8592
452a0955 8593 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
8594 if (arm_feature(env, ARM_FEATURE_V6)) {
8595 /* PMSAv6 not implemented */
8596 assert(arm_feature(env, ARM_FEATURE_V7));
8597 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8598 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8599 } else {
8600 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8601 }
18032bec 8602 } else {
8e5d75c9 8603 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec 8604 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4036b7d1
PM
8605 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
8606 if (cpu_isar_feature(aa32_hpd, cpu)) {
ab638a32
RH
8607 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8608 }
18032bec 8609 }
c326b979
PM
8610 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8611 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8612 }
6cc7a3ae
PM
8613 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8614 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8615 }
4a501606
PM
8616 if (arm_feature(env, ARM_FEATURE_VAPA)) {
8617 define_arm_cp_regs(cpu, vapa_cp_reginfo);
8618 }
c4804214
PM
8619 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8620 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8621 }
8622 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8623 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8624 }
8625 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8626 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8627 }
18032bec
PM
8628 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8629 define_arm_cp_regs(cpu, omap_cp_reginfo);
8630 }
34f90529
PM
8631 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8632 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8633 }
1047b9d7
PM
8634 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8635 define_arm_cp_regs(cpu, xscale_cp_reginfo);
8636 }
8637 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8638 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8639 }
7ac681cf
PM
8640 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8641 define_arm_cp_regs(cpu, lpae_cp_reginfo);
8642 }
873b73c0 8643 if (cpu_isar_feature(aa32_jazelle, cpu)) {
f96f3d5f
MZ
8644 define_arm_cp_regs(cpu, jazelle_regs);
8645 }
9b37a28c
FR
8646 /*
8647 * Slightly awkwardly, the OMAP and StrongARM cores need all of
7884849c
PM
8648 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8649 * be read-only (ie write causes UNDEF exception).
8650 */
8651 {
00a29f3d 8652 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
9b37a28c
FR
8653 /*
8654 * Pre-v8 MIDR space.
00a29f3d 8655 * Note that the MIDR isn't a simple constant register because
7884849c
PM
8656 * of the TI925 behaviour where writes to another register can
8657 * cause the MIDR value to change.
97ce8d61
PC
8658 *
8659 * Unimplemented registers in the c15 0 0 0 space default to
8660 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8661 * and friends override accordingly.
7884849c
PM
8662 */
8663 { .name = "MIDR",
97ce8d61 8664 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 8665 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 8666 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 8667 .readfn = midr_read,
97ce8d61
PC
8668 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8669 .type = ARM_CP_OVERRIDE },
7884849c
PM
8670 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8671 { .name = "DUMMY",
8672 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8673 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8674 { .name = "DUMMY",
8675 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8676 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8677 { .name = "DUMMY",
8678 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8679 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8680 { .name = "DUMMY",
8681 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8682 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8683 { .name = "DUMMY",
8684 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8685 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7884849c 8686 };
00a29f3d 8687 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
8688 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8689 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6 8690 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
67dd8030 8691 .fgt = FGT_MIDR_EL1,
731de9e6
EI
8692 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8693 .readfn = midr_read },
c7f786ab 8694 /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
ac00c79f
SF
8695 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8696 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8697 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
8698 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8699 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
93fbc983
MZ
8700 .access = PL1_R,
8701 .accessfn = access_aa64_tid1,
67dd8030 8702 .fgt = FGT_REVIDR_EL1,
93fbc983 8703 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d 8704 };
c7f786ab
TR
8705 ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
8706 .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8707 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8708 .access = PL1_R, .resetvalue = cpu->midr
8709 };
00a29f3d
PM
8710 ARMCPRegInfo id_cp_reginfo[] = {
8711 /* These are common to v8 and pre-v8 */
8712 { .name = "CTR",
8713 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
630fcd4d
MZ
8714 .access = PL1_R, .accessfn = ctr_el0_access,
8715 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
00a29f3d
PM
8716 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8717 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8718 .access = PL0_R, .accessfn = ctr_el0_access,
b19ed03c 8719 .fgt = FGT_CTR_EL0,
00a29f3d
PM
8720 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8721 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8722 { .name = "TCMTR",
8723 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
93fbc983
MZ
8724 .access = PL1_R,
8725 .accessfn = access_aa32_tid1,
8726 .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d 8727 };
8085ce63
PC
8728 /* TLBTR is specific to VMSA */
8729 ARMCPRegInfo id_tlbtr_reginfo = {
8730 .name = "TLBTR",
8731 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
93fbc983
MZ
8732 .access = PL1_R,
8733 .accessfn = access_aa32_tid1,
8734 .type = ARM_CP_CONST, .resetvalue = 0,
8085ce63 8735 };
3281af81
PC
8736 /* MPUIR is specific to PMSA V6+ */
8737 ARMCPRegInfo id_mpuir_reginfo = {
8738 .name = "MPUIR",
8739 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8740 .access = PL1_R, .type = ARM_CP_CONST,
8741 .resetvalue = cpu->pmsav7_dregion << 8
8742 };
761c4642
TR
8743 /* HMPUIR is specific to PMSA V8 */
8744 ARMCPRegInfo id_hmpuir_reginfo = {
8745 .name = "HMPUIR",
8746 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
8747 .access = PL2_R, .type = ARM_CP_CONST,
8748 .resetvalue = cpu->pmsav8r_hdregion
8749 };
10b0220e 8750 static const ARMCPRegInfo crn0_wi_reginfo = {
7884849c
PM
8751 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8752 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8753 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8754 };
6c5c0fec 8755#ifdef CONFIG_USER_ONLY
10b0220e 8756 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
6c5c0fec 8757 { .name = "MIDR_EL1",
bc6bd20e
ZS
8758 .exported_bits = R_MIDR_EL1_REVISION_MASK |
8759 R_MIDR_EL1_PARTNUM_MASK |
8760 R_MIDR_EL1_ARCHITECTURE_MASK |
8761 R_MIDR_EL1_VARIANT_MASK |
8762 R_MIDR_EL1_IMPLEMENTER_MASK },
8763 { .name = "REVIDR_EL1" },
6c5c0fec
AB
8764 };
8765 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8766#endif
7884849c
PM
8767 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8768 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5809ac57 8769 size_t i;
9b37a28c
FR
8770 /*
8771 * Register the blanket "writes ignored" value first to cover the
a703eda1
PC
8772 * whole space. Then update the specific ID registers to allow write
8773 * access, so that they ignore writes rather than causing them to
8774 * UNDEF.
7884849c
PM
8775 */
8776 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
5809ac57
RH
8777 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8778 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
00a29f3d 8779 }
5809ac57
RH
8780 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8781 id_cp_reginfo[i].access = PL1_RW;
7884849c 8782 }
10006112 8783 id_mpuir_reginfo.access = PL1_RW;
3281af81 8784 id_tlbtr_reginfo.access = PL1_RW;
7884849c 8785 }
00a29f3d
PM
8786 if (arm_feature(env, ARM_FEATURE_V8)) {
8787 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
c7f786ab
TR
8788 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8789 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
8790 }
00a29f3d
PM
8791 } else {
8792 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8793 }
a703eda1 8794 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 8795 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 8796 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
761c4642
TR
8797 } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
8798 arm_feature(env, ARM_FEATURE_V8)) {
8799 uint32_t i = 0;
8800 char *tmp_string;
8801
8802 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8803 define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
8804 define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
8805
8806 /* Register alias is only valid for first 32 indexes */
8807 for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
8808 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8809 uint8_t opc1 = extract32(i, 4, 1);
8810 uint8_t opc2 = extract32(i, 0, 1) << 2;
8811
8812 tmp_string = g_strdup_printf("PRBAR%u", i);
8813 ARMCPRegInfo tmp_prbarn_reginfo = {
8814 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8815 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8816 .access = PL1_RW, .resetvalue = 0,
8817 .accessfn = access_tvm_trvm,
8818 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8819 };
8820 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
8821 g_free(tmp_string);
8822
8823 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8824 tmp_string = g_strdup_printf("PRLAR%u", i);
8825 ARMCPRegInfo tmp_prlarn_reginfo = {
8826 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8827 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8828 .access = PL1_RW, .resetvalue = 0,
8829 .accessfn = access_tvm_trvm,
8830 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8831 };
8832 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
8833 g_free(tmp_string);
8834 }
8835
8836 /* Register alias is only valid for first 32 indexes */
8837 for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
8838 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8839 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
8840 uint8_t opc2 = extract32(i, 0, 1) << 2;
8841
8842 tmp_string = g_strdup_printf("HPRBAR%u", i);
8843 ARMCPRegInfo tmp_hprbarn_reginfo = {
8844 .name = tmp_string,
8845 .type = ARM_CP_NO_RAW,
8846 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8847 .access = PL2_RW, .resetvalue = 0,
8848 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8849 };
8850 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
8851 g_free(tmp_string);
8852
8853 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8854 tmp_string = g_strdup_printf("HPRLAR%u", i);
8855 ARMCPRegInfo tmp_hprlarn_reginfo = {
8856 .name = tmp_string,
8857 .type = ARM_CP_NO_RAW,
8858 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8859 .access = PL2_RW, .resetvalue = 0,
8860 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8861 };
8862 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
8863 g_free(tmp_string);
8864 }
3281af81
PC
8865 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8866 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 8867 }
7884849c
PM
8868 }
8869
97ce8d61 8870 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
52264166
AB
8871 ARMCPRegInfo mpidr_cp_reginfo[] = {
8872 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8873 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
67dd8030 8874 .fgt = FGT_MPIDR_EL1,
52264166 8875 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
52264166
AB
8876 };
8877#ifdef CONFIG_USER_ONLY
10b0220e 8878 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
52264166
AB
8879 { .name = "MPIDR_EL1",
8880 .fixed_bits = 0x0000000080000000 },
52264166
AB
8881 };
8882 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8883#endif
97ce8d61
PC
8884 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8885 }
8886
2771db27 8887 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
8888 ARMCPRegInfo auxcr_reginfo[] = {
8889 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8890 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
99602377
RH
8891 .access = PL1_RW, .accessfn = access_tacr,
8892 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
834a6c69
PM
8893 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8894 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8895 .access = PL2_RW, .type = ARM_CP_CONST,
8896 .resetvalue = 0 },
8897 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8898 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8899 .access = PL3_RW, .type = ARM_CP_CONST,
8900 .resetvalue = 0 },
2771db27 8901 };
834a6c69 8902 define_arm_cp_regs(cpu, auxcr_reginfo);
f6287c24
PM
8903 if (cpu_isar_feature(aa32_ac2, cpu)) {
8904 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
0e0456ab 8905 }
2771db27
PM
8906 }
8907
d8ba780b 8908 if (arm_feature(env, ARM_FEATURE_CBAR)) {
d56974af
LM
8909 /*
8910 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8911 * There are two flavours:
8912 * (1) older 32-bit only cores have a simple 32-bit CBAR
8913 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8914 * 32-bit register visible to AArch32 at a different encoding
8915 * to the "flavour 1" register and with the bits rearranged to
8916 * be able to squash a 64-bit address into the 32-bit view.
8917 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8918 * in future if we support AArch32-only configs of some of the
8919 * AArch64 cores we might need to add a specific feature flag
8920 * to indicate cores with "flavour 2" CBAR.
8921 */
f318cec6
PM
8922 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8923 /* 32 bit view is [31:18] 0...0 [43:32]. */
8924 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8925 | extract64(cpu->reset_cbar, 32, 12);
8926 ARMCPRegInfo cbar_reginfo[] = {
8927 { .name = "CBAR",
8928 .type = ARM_CP_CONST,
d56974af
LM
8929 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8930 .access = PL1_R, .resetvalue = cbar32 },
f318cec6
PM
8931 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8932 .type = ARM_CP_CONST,
8933 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
d56974af 8934 .access = PL1_R, .resetvalue = cpu->reset_cbar },
f318cec6
PM
8935 };
8936 /* We don't implement a r/w 64 bit CBAR currently */
8937 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8938 define_arm_cp_regs(cpu, cbar_reginfo);
8939 } else {
8940 ARMCPRegInfo cbar = {
8941 .name = "CBAR",
8942 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
04215eb1 8943 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
f318cec6
PM
8944 .fieldoffset = offsetof(CPUARMState,
8945 cp15.c15_config_base_address)
8946 };
8947 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8948 cbar.access = PL1_R;
8949 cbar.fieldoffset = 0;
8950 cbar.type = ARM_CP_CONST;
8951 }
8952 define_one_arm_cp_reg(cpu, &cbar);
8953 }
d8ba780b
PC
8954 }
8955
91db4642 8956 if (arm_feature(env, ARM_FEATURE_VBAR)) {
10b0220e 8957 static const ARMCPRegInfo vbar_cp_reginfo[] = {
91db4642
CLG
8958 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8959 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8960 .access = PL1_RW, .writefn = vbar_write,
bd8db7d9 8961 .fgt = FGT_VBAR_EL1,
91db4642
CLG
8962 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8963 offsetof(CPUARMState, cp15.vbar_ns) },
8964 .resetvalue = 0 },
91db4642
CLG
8965 };
8966 define_arm_cp_regs(cpu, vbar_cp_reginfo);
8967 }
8968
2771db27
PM
8969 /* Generic registers whose values depend on the implementation */
8970 {
8971 ARMCPRegInfo sctlr = {
5ebafdf3 8972 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9 8973 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
84929218 8974 .access = PL1_RW, .accessfn = access_tvm_trvm,
67dd8030 8975 .fgt = FGT_SCTLR_EL1,
137feaa9
FA
8976 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
8977 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
8978 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
8979 .raw_writefn = raw_write,
2771db27
PM
8980 };
8981 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9b37a28c
FR
8982 /*
8983 * Normally we would always end the TB on an SCTLR write, but Linux
2771db27
PM
8984 * arch/arm/mach-pxa/sleep.S expects two instructions following
8985 * an MMU enable to execute from cache. Imitate this behaviour.
8986 */
8987 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
8988 }
8989 define_one_arm_cp_reg(cpu, &sctlr);
761c4642
TR
8990
8991 if (arm_feature(env, ARM_FEATURE_PMSA) &&
8992 arm_feature(env, ARM_FEATURE_V8)) {
8993 ARMCPRegInfo vsctlr = {
8994 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
8995 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
8996 .access = PL2_RW, .resetvalue = 0x0,
8997 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
8998 };
8999 define_one_arm_cp_reg(cpu, &vsctlr);
9000 }
2771db27 9001 }
5be5e8ed 9002
2d7137c1 9003 if (cpu_isar_feature(aa64_lor, cpu)) {
2d7137c1
RH
9004 define_arm_cp_regs(cpu, lor_reginfo);
9005 }
220f508f
RH
9006 if (cpu_isar_feature(aa64_pan, cpu)) {
9007 define_one_arm_cp_reg(cpu, &pan_reginfo);
9008 }
04b07d29
RH
9009#ifndef CONFIG_USER_ONLY
9010 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
9011 define_arm_cp_regs(cpu, ats1e1_reginfo);
9012 }
9013 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
9014 define_arm_cp_regs(cpu, ats1cp_reginfo);
9015 }
9016#endif
9eeb7a1c
RH
9017 if (cpu_isar_feature(aa64_uao, cpu)) {
9018 define_one_arm_cp_reg(cpu, &uao_reginfo);
9019 }
2d7137c1 9020
dc8b1853
RC
9021 if (cpu_isar_feature(aa64_dit, cpu)) {
9022 define_one_arm_cp_reg(cpu, &dit_reginfo);
9023 }
f2f68a78
RC
9024 if (cpu_isar_feature(aa64_ssbs, cpu)) {
9025 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
9026 }
58e93b48
RH
9027 if (cpu_isar_feature(any_ras, cpu)) {
9028 define_arm_cp_regs(cpu, minimal_ras_reginfo);
9029 }
dc8b1853 9030
52d18727
RH
9031 if (cpu_isar_feature(aa64_vh, cpu) ||
9032 cpu_isar_feature(aa64_debugv8p2, cpu)) {
9033 define_one_arm_cp_reg(cpu, &contextidr_el2);
9034 }
e2a1a461
RH
9035 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9036 define_arm_cp_regs(cpu, vhe_reginfo);
9037 }
9038
cd208a1c 9039 if (cpu_isar_feature(aa64_sve, cpu)) {
60360d82 9040 define_arm_cp_regs(cpu, zcr_reginfo);
5be5e8ed 9041 }
967aa94f 9042
5814d587
RH
9043 if (cpu_isar_feature(aa64_hcx, cpu)) {
9044 define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
9045 }
9046
967aa94f 9047#ifdef TARGET_AARCH64
9e5ec745
RH
9048 if (cpu_isar_feature(aa64_sme, cpu)) {
9049 define_arm_cp_regs(cpu, sme_reginfo);
9050 }
967aa94f
RH
9051 if (cpu_isar_feature(aa64_pauth, cpu)) {
9052 define_arm_cp_regs(cpu, pauth_reginfo);
9053 }
de390645
RH
9054 if (cpu_isar_feature(aa64_rndr, cpu)) {
9055 define_arm_cp_regs(cpu, rndr_reginfo);
9056 }
84940ed8
RC
9057 if (cpu_isar_feature(aa64_tlbirange, cpu)) {
9058 define_arm_cp_regs(cpu, tlbirange_reginfo);
9059 }
7113d618
RC
9060 if (cpu_isar_feature(aa64_tlbios, cpu)) {
9061 define_arm_cp_regs(cpu, tlbios_reginfo);
9062 }
0d57b499
BM
9063#ifndef CONFIG_USER_ONLY
9064 /* Data Cache clean instructions up to PoP */
9065 if (cpu_isar_feature(aa64_dcpop, cpu)) {
9066 define_one_arm_cp_reg(cpu, dcpop_reg);
9067
9068 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
9069 define_one_arm_cp_reg(cpu, dcpodp_reg);
9070 }
9071 }
9072#endif /*CONFIG_USER_ONLY*/
4b779ceb
RH
9073
9074 /*
9075 * If full MTE is enabled, add all of the system registers.
9076 * If only "instructions available at EL0" are enabled,
9077 * then define only a RAZ/WI version of PSTATE.TCO.
9078 */
9079 if (cpu_isar_feature(aa64_mte, cpu)) {
9080 define_arm_cp_regs(cpu, mte_reginfo);
5463df16 9081 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb
RH
9082 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
9083 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
5463df16 9084 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb 9085 }
7cb1e618
RH
9086
9087 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
9088 define_arm_cp_regs(cpu, scxtnum_reginfo);
9089 }
15126d9c
PM
9090
9091 if (cpu_isar_feature(aa64_fgt, cpu)) {
9092 define_arm_cp_regs(cpu, fgt_reginfo);
9093 }
967aa94f 9094#endif
cb570bd3 9095
22e57073 9096 if (cpu_isar_feature(any_predinv, cpu)) {
cb570bd3
RH
9097 define_arm_cp_regs(cpu, predinv_reginfo);
9098 }
e2cce18f 9099
957e6155
PM
9100 if (cpu_isar_feature(any_ccidx, cpu)) {
9101 define_arm_cp_regs(cpu, ccsidr2_reginfo);
9102 }
9103
e2cce18f
RH
9104#ifndef CONFIG_USER_ONLY
9105 /*
9106 * Register redirections and aliases must be done last,
9107 * after the registers from the other extensions have been defined.
9108 */
9109 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9110 define_arm_vh_e2h_redirects_aliases(cpu);
9111 }
9112#endif
2ceb98c0
PM
9113}
9114
777dc784
PM
9115/* Sort alphabetically by type name, except for "any". */
9116static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 9117{
777dc784
PM
9118 ObjectClass *class_a = (ObjectClass *)a;
9119 ObjectClass *class_b = (ObjectClass *)b;
9120 const char *name_a, *name_b;
5adb4839 9121
777dc784
PM
9122 name_a = object_class_get_name(class_a);
9123 name_b = object_class_get_name(class_b);
51492fd1 9124 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 9125 return 1;
51492fd1 9126 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
9127 return -1;
9128 } else {
9129 return strcmp(name_a, name_b);
5adb4839
PB
9130 }
9131}
9132
777dc784 9133static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 9134{
777dc784 9135 ObjectClass *oc = data;
977c33ba 9136 CPUClass *cc = CPU_CLASS(oc);
51492fd1
AF
9137 const char *typename;
9138 char *name;
3371d272 9139
51492fd1
AF
9140 typename = object_class_get_name(oc);
9141 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
977c33ba
DB
9142 if (cc->deprecation_note) {
9143 qemu_printf(" %s (deprecated)\n", name);
9144 } else {
9145 qemu_printf(" %s\n", name);
9146 }
51492fd1 9147 g_free(name);
777dc784
PM
9148}
9149
0442428a 9150void arm_cpu_list(void)
777dc784 9151{
777dc784
PM
9152 GSList *list;
9153
9154 list = object_class_get_list(TYPE_ARM_CPU, false);
9155 list = g_slist_sort(list, arm_cpu_list_compare);
0442428a
MA
9156 qemu_printf("Available CPUs:\n");
9157 g_slist_foreach(list, arm_cpu_list_entry, NULL);
777dc784 9158 g_slist_free(list);
40f137e1
PB
9159}
9160
78027bb6
CR
9161static void arm_cpu_add_definition(gpointer data, gpointer user_data)
9162{
9163 ObjectClass *oc = data;
9164 CpuDefinitionInfoList **cpu_list = user_data;
78027bb6
CR
9165 CpuDefinitionInfo *info;
9166 const char *typename;
9167
9168 typename = object_class_get_name(oc);
9169 info = g_malloc0(sizeof(*info));
9170 info->name = g_strndup(typename,
9171 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 9172 info->q_typename = g_strdup(typename);
78027bb6 9173
54aa3de7 9174 QAPI_LIST_PREPEND(*cpu_list, info);
78027bb6
CR
9175}
9176
25a9d6ca 9177CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
78027bb6
CR
9178{
9179 CpuDefinitionInfoList *cpu_list = NULL;
9180 GSList *list;
9181
9182 list = object_class_get_list(TYPE_ARM_CPU, false);
9183 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
9184 g_slist_free(list);
9185
9186 return cpu_list;
9187}
9188
1859f8c3
RH
9189/*
9190 * Private utility function for define_one_arm_cp_reg_with_opaque():
9191 * add a single reginfo struct to the hash table.
9192 */
6e6efd61 9193static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
cbe64585
RH
9194 void *opaque, CPState state,
9195 CPSecureState secstate,
9c513e78
AB
9196 int crm, int opc1, int opc2,
9197 const char *name)
6e6efd61 9198{
696ba377 9199 CPUARMState *env = &cpu->env;
5860362d 9200 uint32_t key;
c27f5d3a 9201 ARMCPRegInfo *r2;
4c8c4541
RH
9202 bool is64 = r->type & ARM_CP_64BIT;
9203 bool ns = secstate & ARM_CP_SECSTATE_NS;
cac65299 9204 int cp = r->cp;
c27f5d3a 9205 size_t name_len;
696ba377 9206 bool make_const;
c27f5d3a 9207
cac65299
RH
9208 switch (state) {
9209 case ARM_CP_STATE_AA32:
9210 /* We assume it is a cp15 register if the .cp field is left unset. */
9211 if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9212 cp = 15;
9213 }
9214 key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9215 break;
9216 case ARM_CP_STATE_AA64:
9217 /*
9218 * To allow abbreviation of ARMCPRegInfo definitions, we treat
9219 * cp == 0 as equivalent to the value for "standard guest-visible
9220 * sysreg". STATE_BOTH definitions are also always "standard sysreg"
9221 * in their AArch64 view (the .cp value may be non-zero for the
9222 * benefit of the AArch32 view).
9223 */
9224 if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9225 cp = CP_REG_ARM64_SYSREG_CP;
9226 }
9227 key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9228 break;
9229 default:
9230 g_assert_not_reached();
9231 }
9232
dc44545b
RH
9233 /* Overriding of an existing definition must be explicitly requested. */
9234 if (!(r->type & ARM_CP_OVERRIDE)) {
9235 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9236 if (oldreg) {
9237 assert(oldreg->type & ARM_CP_OVERRIDE);
9238 }
9239 }
9240
696ba377
RH
9241 /*
9242 * Eliminate registers that are not present because the EL is missing.
9243 * Doing this here makes it easier to put all registers for a given
9244 * feature into the same ARMCPRegInfo array and define them all at once.
9245 */
9246 make_const = false;
9247 if (arm_feature(env, ARM_FEATURE_EL3)) {
9248 /*
9249 * An EL2 register without EL2 but with EL3 is (usually) RES0.
9250 * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9251 */
9252 int min_el = ctz32(r->access) / 2;
9253 if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9254 if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9255 return;
9256 }
9257 make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9258 }
9259 } else {
9260 CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9261 ? PL2_RW : PL1_RW);
9262 if ((r->access & max_el) == 0) {
9263 return;
9264 }
9265 }
9266
c27f5d3a
RH
9267 /* Combine cpreg and name into one allocation. */
9268 name_len = strlen(name) + 1;
9269 r2 = g_malloc(sizeof(*r2) + name_len);
9270 *r2 = *r;
9271 r2->name = memcpy(r2 + 1, name, name_len);
3f3c82a5 9272
cc946d96
RH
9273 /*
9274 * Update fields to match the instantiation, overwiting wildcards
9275 * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
3f3c82a5 9276 */
cc946d96
RH
9277 r2->cp = cp;
9278 r2->crm = crm;
9279 r2->opc1 = opc1;
9280 r2->opc2 = opc2;
9281 r2->state = state;
3f3c82a5 9282 r2->secure = secstate;
cc946d96
RH
9283 if (opaque) {
9284 r2->opaque = opaque;
9285 }
3f3c82a5 9286
696ba377
RH
9287 if (make_const) {
9288 /* This should not have been a very special register to begin. */
9289 int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9290 assert(old_special == 0 || old_special == ARM_CP_NOP);
1859f8c3 9291 /*
696ba377
RH
9292 * Set the special function to CONST, retaining the other flags.
9293 * This is important for e.g. ARM_CP_SVE so that we still
9294 * take the SVE trap if CPTR_EL3.EZ == 0.
f5a0a5a5 9295 */
696ba377
RH
9296 r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9297 /*
9298 * Usually, these registers become RES0, but there are a few
9299 * special cases like VPIDR_EL2 which have a constant non-zero
9300 * value with writes ignored.
9301 */
9302 if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9303 r2->resetvalue = 0;
9304 }
9305 /*
9306 * ARM_CP_CONST has precedence, so removing the callbacks and
9307 * offsets are not strictly necessary, but it is potentially
9308 * less confusing to debug later.
9309 */
9310 r2->readfn = NULL;
9311 r2->writefn = NULL;
9312 r2->raw_readfn = NULL;
9313 r2->raw_writefn = NULL;
9314 r2->resetfn = NULL;
9315 r2->fieldoffset = 0;
9316 r2->bank_fieldoffsets[0] = 0;
9317 r2->bank_fieldoffsets[1] = 0;
9318 } else {
9319 bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
3f3c82a5 9320
10748a96 9321 if (isbanked) {
1859f8c3 9322 /*
696ba377
RH
9323 * Register is banked (using both entries in array).
9324 * Overwriting fieldoffset as the array is only used to define
9325 * banked registers but later only fieldoffset is used.
3f3c82a5 9326 */
696ba377
RH
9327 r2->fieldoffset = r->bank_fieldoffsets[ns];
9328 }
9329 if (state == ARM_CP_STATE_AA32) {
9330 if (isbanked) {
9331 /*
9332 * If the register is banked then we don't need to migrate or
9333 * reset the 32-bit instance in certain cases:
9334 *
9335 * 1) If the register has both 32-bit and 64-bit instances
9336 * then we can count on the 64-bit instance taking care
9337 * of the non-secure bank.
9338 * 2) If ARMv8 is enabled then we can count on a 64-bit
9339 * version taking care of the secure bank. This requires
9340 * that separate 32 and 64-bit definitions are provided.
9341 */
9342 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9343 (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9344 r2->type |= ARM_CP_ALIAS;
9345 }
9346 } else if ((secstate != r->secure) && !ns) {
9347 /*
9348 * The register is not banked so we only want to allow
9349 * migration of the non-secure instance.
9350 */
7a0e58fa 9351 r2->type |= ARM_CP_ALIAS;
3f3c82a5 9352 }
3f3c82a5 9353
696ba377
RH
9354 if (HOST_BIG_ENDIAN &&
9355 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9356 r2->fieldoffset += sizeof(uint32_t);
9357 }
3f3c82a5 9358 }
f5a0a5a5 9359 }
cc946d96 9360
1859f8c3
RH
9361 /*
9362 * By convention, for wildcarded registers only the first
6e6efd61 9363 * entry is used for migration; the others are marked as
7a0e58fa 9364 * ALIAS so we don't try to transfer the register
6e6efd61 9365 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 9366 * never migratable and not even raw-accessible.
6e6efd61 9367 */
696ba377 9368 if (r2->type & ARM_CP_SPECIAL_MASK) {
7a0e58fa
PM
9369 r2->type |= ARM_CP_NO_RAW;
9370 }
9371 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
9372 ((r->opc1 == CP_ANY) && opc1 != 0) ||
9373 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 9374 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
9375 }
9376
1859f8c3
RH
9377 /*
9378 * Check that raw accesses are either forbidden or handled. Note that
375421cc
PM
9379 * we can't assert this earlier because the setup of fieldoffset for
9380 * banked registers has to be done first.
9381 */
9382 if (!(r2->type & ARM_CP_NO_RAW)) {
9383 assert(!raw_accessors_invalid(r2));
9384 }
9385
5860362d 9386 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
6e6efd61
PM
9387}
9388
9389
4b6a83fb
PM
9390void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9391 const ARMCPRegInfo *r, void *opaque)
9392{
9b37a28c
FR
9393 /*
9394 * Define implementations of coprocessor registers.
4b6a83fb
PM
9395 * We store these in a hashtable because typically
9396 * there are less than 150 registers in a space which
9397 * is 16*16*16*8*8 = 262144 in size.
9398 * Wildcarding is supported for the crm, opc1 and opc2 fields.
9399 * If a register is defined twice then the second definition is
9400 * used, so this can be used to define some generic registers and
9401 * then override them with implementation specific variations.
9402 * At least one of the original and the second definition should
9403 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9404 * against accidental use.
f5a0a5a5
PM
9405 *
9406 * The state field defines whether the register is to be
9407 * visible in the AArch32 or AArch64 execution state. If the
9408 * state is set to ARM_CP_STATE_BOTH then we synthesise a
9409 * reginfo structure for the AArch32 view, which sees the lower
9410 * 32 bits of the 64 bit register.
9411 *
9412 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9413 * be wildcarded. AArch64 registers are always considered to be 64
9414 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9415 * the register, if any.
4b6a83fb 9416 */
d95101d6 9417 int crm, opc1, opc2;
4b6a83fb
PM
9418 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9419 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9420 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9421 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9422 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9423 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
d95101d6
RH
9424 CPState state;
9425
4b6a83fb
PM
9426 /* 64 bit registers have only CRm and Opc1 fields */
9427 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
9428 /* op0 only exists in the AArch64 encodings */
9429 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9430 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9431 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
cd8be50e
PM
9432 /*
9433 * This API is only for Arm's system coprocessors (14 and 15) or
9434 * (M-profile or v7A-and-earlier only) for implementation defined
9435 * coprocessors in the range 0..7. Our decode assumes this, since
9436 * 8..13 can be used for other insns including VFP and Neon. See
9437 * valid_cp() in translate.c. Assert here that we haven't tried
9438 * to use an invalid coprocessor number.
9439 */
9440 switch (r->state) {
9441 case ARM_CP_STATE_BOTH:
9442 /* 0 has a special meaning, but otherwise the same rules as AA32. */
9443 if (r->cp == 0) {
9444 break;
9445 }
9446 /* fall through */
9447 case ARM_CP_STATE_AA32:
9448 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9449 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9450 assert(r->cp >= 14 && r->cp <= 15);
9451 } else {
9452 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9453 }
9454 break;
9455 case ARM_CP_STATE_AA64:
9456 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9457 break;
9458 default:
9459 g_assert_not_reached();
9460 }
9b37a28c
FR
9461 /*
9462 * The AArch64 pseudocode CheckSystemAccess() specifies that op1
f5a0a5a5
PM
9463 * encodes a minimum access level for the register. We roll this
9464 * runtime check into our general permission check code, so check
9465 * here that the reginfo's specified permissions are strict enough
9466 * to encompass the generic architectural permission check.
9467 */
9468 if (r->state != ARM_CP_STATE_AA32) {
39107337 9469 CPAccessRights mask;
f5a0a5a5 9470 switch (r->opc1) {
b5bd7440
AB
9471 case 0:
9472 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9473 mask = PL0U_R | PL1_RW;
9474 break;
9475 case 1: case 2:
f5a0a5a5
PM
9476 /* min_EL EL1 */
9477 mask = PL1_RW;
9478 break;
9479 case 3:
9480 /* min_EL EL0 */
9481 mask = PL0_RW;
9482 break;
9483 case 4:
b4ecf60f 9484 case 5:
f5a0a5a5
PM
9485 /* min_EL EL2 */
9486 mask = PL2_RW;
9487 break;
f5a0a5a5
PM
9488 case 6:
9489 /* min_EL EL3 */
9490 mask = PL3_RW;
9491 break;
9492 case 7:
9493 /* min_EL EL1, secure mode only (we don't check the latter) */
9494 mask = PL1_RW;
9495 break;
9496 default:
9497 /* broken reginfo with out-of-range opc1 */
d385a605 9498 g_assert_not_reached();
f5a0a5a5
PM
9499 }
9500 /* assert our permissions are not too lax (stricter is fine) */
9501 assert((r->access & ~mask) == 0);
9502 }
9503
9b37a28c
FR
9504 /*
9505 * Check that the register definition has enough info to handle
4b6a83fb
PM
9506 * reads and writes if they are permitted.
9507 */
87c3f0f2 9508 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
4b6a83fb 9509 if (r->access & PL3_R) {
3f3c82a5
FA
9510 assert((r->fieldoffset ||
9511 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9512 r->readfn);
4b6a83fb
PM
9513 }
9514 if (r->access & PL3_W) {
3f3c82a5
FA
9515 assert((r->fieldoffset ||
9516 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9517 r->writefn);
4b6a83fb
PM
9518 }
9519 }
5809ac57 9520
4b6a83fb
PM
9521 for (crm = crmmin; crm <= crmmax; crm++) {
9522 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9523 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
9524 for (state = ARM_CP_STATE_AA32;
9525 state <= ARM_CP_STATE_AA64; state++) {
9526 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9527 continue;
9528 }
3f3c82a5 9529 if (state == ARM_CP_STATE_AA32) {
9b37a28c
FR
9530 /*
9531 * Under AArch32 CP registers can be common
3f3c82a5
FA
9532 * (same for secure and non-secure world) or banked.
9533 */
9c513e78
AB
9534 char *name;
9535
3f3c82a5
FA
9536 switch (r->secure) {
9537 case ARM_CP_SECSTATE_S:
9538 case ARM_CP_SECSTATE_NS:
9539 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
9540 r->secure, crm, opc1, opc2,
9541 r->name);
3f3c82a5 9542 break;
cbe64585 9543 case ARM_CP_SECSTATE_BOTH:
9c513e78 9544 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
9545 add_cpreg_to_hashtable(cpu, r, opaque, state,
9546 ARM_CP_SECSTATE_S,
9c513e78
AB
9547 crm, opc1, opc2, name);
9548 g_free(name);
3f3c82a5
FA
9549 add_cpreg_to_hashtable(cpu, r, opaque, state,
9550 ARM_CP_SECSTATE_NS,
9c513e78 9551 crm, opc1, opc2, r->name);
3f3c82a5 9552 break;
cbe64585
RH
9553 default:
9554 g_assert_not_reached();
3f3c82a5
FA
9555 }
9556 } else {
9b37a28c
FR
9557 /*
9558 * AArch64 registers get mapped to non-secure instance
9559 * of AArch32
9560 */
3f3c82a5
FA
9561 add_cpreg_to_hashtable(cpu, r, opaque, state,
9562 ARM_CP_SECSTATE_NS,
9c513e78 9563 crm, opc1, opc2, r->name);
3f3c82a5 9564 }
f5a0a5a5 9565 }
4b6a83fb
PM
9566 }
9567 }
9568 }
9569}
9570
5809ac57
RH
9571/* Define a whole list of registers */
9572void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9573 void *opaque, size_t len)
4b6a83fb 9574{
5809ac57
RH
9575 size_t i;
9576 for (i = 0; i < len; ++i) {
9577 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
4b6a83fb
PM
9578 }
9579}
9580
6c5c0fec
AB
9581/*
9582 * Modify ARMCPRegInfo for access from userspace.
9583 *
9584 * This is a data driven modification directed by
9585 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9586 * user-space cannot alter any values and dynamic values pertaining to
9587 * execution state are hidden from user space view anyway.
9588 */
5809ac57
RH
9589void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9590 const ARMCPRegUserSpaceInfo *mods,
9591 size_t mods_len)
6c5c0fec 9592{
5809ac57
RH
9593 for (size_t mi = 0; mi < mods_len; ++mi) {
9594 const ARMCPRegUserSpaceInfo *m = mods + mi;
d040242e 9595 GPatternSpec *pat = NULL;
5809ac57 9596
d040242e
AB
9597 if (m->is_glob) {
9598 pat = g_pattern_spec_new(m->name);
9599 }
5809ac57
RH
9600 for (size_t ri = 0; ri < regs_len; ++ri) {
9601 ARMCPRegInfo *r = regs + ri;
9602
d040242e
AB
9603 if (pat && g_pattern_match_string(pat, r->name)) {
9604 r->type = ARM_CP_CONST;
9605 r->access = PL0U_R;
9606 r->resetvalue = 0;
9607 /* continue */
9608 } else if (strcmp(r->name, m->name) == 0) {
6c5c0fec
AB
9609 r->type = ARM_CP_CONST;
9610 r->access = PL0U_R;
9611 r->resetvalue &= m->exported_bits;
9612 r->resetvalue |= m->fixed_bits;
9613 break;
9614 }
9615 }
d040242e
AB
9616 if (pat) {
9617 g_pattern_spec_free(pat);
9618 }
6c5c0fec
AB
9619 }
9620}
9621
60322b39 9622const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 9623{
5860362d 9624 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
4b6a83fb
PM
9625}
9626
c4241c7d
PM
9627void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9628 uint64_t value)
4b6a83fb
PM
9629{
9630 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
9631}
9632
c4241c7d 9633uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
9634{
9635 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
9636 return 0;
9637}
9638
f5a0a5a5
PM
9639void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9640{
9641 /* Helper coprocessor reset function for do-nothing-on-reset registers */
9642}
9643
af393ffc 9644static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b 9645{
9b37a28c
FR
9646 /*
9647 * Return true if it is not valid for us to switch to
37064a8b
PM
9648 * this CPU mode (ie all the UNPREDICTABLE cases in
9649 * the ARM ARM CPSRWriteByInstr pseudocode).
9650 */
af393ffc
PM
9651
9652 /* Changes to or from Hyp via MSR and CPS are illegal. */
9653 if (write_type == CPSRWriteByInstr &&
9654 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9655 mode == ARM_CPU_MODE_HYP)) {
9656 return 1;
9657 }
9658
37064a8b
PM
9659 switch (mode) {
9660 case ARM_CPU_MODE_USR:
10eacda7 9661 return 0;
37064a8b
PM
9662 case ARM_CPU_MODE_SYS:
9663 case ARM_CPU_MODE_SVC:
9664 case ARM_CPU_MODE_ABT:
9665 case ARM_CPU_MODE_UND:
9666 case ARM_CPU_MODE_IRQ:
9667 case ARM_CPU_MODE_FIQ:
9b37a28c
FR
9668 /*
9669 * Note that we don't implement the IMPDEF NSACR.RFR which in v7
52ff951b
PM
9670 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9671 */
9b37a28c
FR
9672 /*
9673 * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
10eacda7
PM
9674 * and CPS are treated as illegal mode changes.
9675 */
9676 if (write_type == CPSRWriteByInstr &&
10eacda7 9677 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7c208e0f 9678 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10eacda7
PM
9679 return 1;
9680 }
37064a8b 9681 return 0;
e6c8fc07 9682 case ARM_CPU_MODE_HYP:
e6ef0169 9683 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
027fc527 9684 case ARM_CPU_MODE_MON:
58ae2d1f 9685 return arm_current_el(env) < 3;
37064a8b
PM
9686 default:
9687 return 1;
9688 }
9689}
9690
2f4a40e5
AZ
9691uint32_t cpsr_read(CPUARMState *env)
9692{
9693 int ZF;
6fbe23d5
PB
9694 ZF = (env->ZF == 0);
9695 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
9696 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9697 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9698 | ((env->condexec_bits & 0xfc) << 8)
af519934 9699 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
9700}
9701
50866ba5
PM
9702void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9703 CPSRWriteType write_type)
2f4a40e5 9704{
6e8801f9 9705 uint32_t changed_daif;
e784807c
PM
9706 bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9707 (mask & (CPSR_M | CPSR_E | CPSR_IL));
6e8801f9 9708
2f4a40e5 9709 if (mask & CPSR_NZCV) {
6fbe23d5
PB
9710 env->ZF = (~val) & CPSR_Z;
9711 env->NF = val;
2f4a40e5
AZ
9712 env->CF = (val >> 29) & 1;
9713 env->VF = (val << 3) & 0x80000000;
9714 }
f927dbda 9715 if (mask & CPSR_Q) {
2f4a40e5 9716 env->QF = ((val & CPSR_Q) != 0);
f927dbda
FR
9717 }
9718 if (mask & CPSR_T) {
2f4a40e5 9719 env->thumb = ((val & CPSR_T) != 0);
f927dbda 9720 }
2f4a40e5
AZ
9721 if (mask & CPSR_IT_0_1) {
9722 env->condexec_bits &= ~3;
9723 env->condexec_bits |= (val >> 25) & 3;
9724 }
9725 if (mask & CPSR_IT_2_7) {
9726 env->condexec_bits &= 3;
9727 env->condexec_bits |= (val >> 8) & 0xfc;
9728 }
9729 if (mask & CPSR_GE) {
9730 env->GE = (val >> 16) & 0xf;
9731 }
9732
9b37a28c
FR
9733 /*
9734 * In a V7 implementation that includes the security extensions but does
6e8801f9
FA
9735 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9736 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9737 * bits respectively.
9738 *
9739 * In a V8 implementation, it is permitted for privileged software to
9740 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9741 */
f8c88bbc 9742 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
9743 arm_feature(env, ARM_FEATURE_EL3) &&
9744 !arm_feature(env, ARM_FEATURE_EL2) &&
9745 !arm_is_secure(env)) {
9746
9747 changed_daif = (env->daif ^ val) & mask;
9748
9749 if (changed_daif & CPSR_A) {
9b37a28c
FR
9750 /*
9751 * Check to see if we are allowed to change the masking of async
6e8801f9
FA
9752 * abort exceptions from a non-secure state.
9753 */
9754 if (!(env->cp15.scr_el3 & SCR_AW)) {
9755 qemu_log_mask(LOG_GUEST_ERROR,
9756 "Ignoring attempt to switch CPSR_A flag from "
9757 "non-secure world with SCR.AW bit clear\n");
9758 mask &= ~CPSR_A;
9759 }
9760 }
9761
9762 if (changed_daif & CPSR_F) {
9b37a28c
FR
9763 /*
9764 * Check to see if we are allowed to change the masking of FIQ
6e8801f9
FA
9765 * exceptions from a non-secure state.
9766 */
9767 if (!(env->cp15.scr_el3 & SCR_FW)) {
9768 qemu_log_mask(LOG_GUEST_ERROR,
9769 "Ignoring attempt to switch CPSR_F flag from "
9770 "non-secure world with SCR.FW bit clear\n");
9771 mask &= ~CPSR_F;
9772 }
9773
9b37a28c
FR
9774 /*
9775 * Check whether non-maskable FIQ (NMFI) support is enabled.
6e8801f9
FA
9776 * If this bit is set software is not allowed to mask
9777 * FIQs, but is allowed to set CPSR_F to 0.
9778 */
9779 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9780 (val & CPSR_F)) {
9781 qemu_log_mask(LOG_GUEST_ERROR,
9782 "Ignoring attempt to enable CPSR_F flag "
9783 "(non-maskable FIQ [NMFI] support enabled)\n");
9784 mask &= ~CPSR_F;
9785 }
9786 }
9787 }
9788
4cc35614
PM
9789 env->daif &= ~(CPSR_AIF & mask);
9790 env->daif |= val & CPSR_AIF & mask;
9791
f8c88bbc
PM
9792 if (write_type != CPSRWriteRaw &&
9793 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9 9794 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9b37a28c
FR
9795 /*
9796 * Note that we can only get here in USR mode if this is a
8c4f0eb9
PM
9797 * gdb stub write; for this case we follow the architectural
9798 * behaviour for guest writes in USR mode of ignoring an attempt
9799 * to switch mode. (Those are caught by translate.c for writes
9800 * triggered by guest instructions.)
9801 */
9802 mask &= ~CPSR_M;
9803 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9b37a28c
FR
9804 /*
9805 * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
81907a58
PM
9806 * v7, and has defined behaviour in v8:
9807 * + leave CPSR.M untouched
9808 * + allow changes to the other CPSR fields
9809 * + set PSTATE.IL
9810 * For user changes via the GDB stub, we don't set PSTATE.IL,
9811 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
9812 */
9813 mask &= ~CPSR_M;
81907a58
PM
9814 if (write_type != CPSRWriteByGDBStub &&
9815 arm_feature(env, ARM_FEATURE_V8)) {
9816 mask |= CPSR_IL;
9817 val |= CPSR_IL;
9818 }
81e37284
PM
9819 qemu_log_mask(LOG_GUEST_ERROR,
9820 "Illegal AArch32 mode switch attempt from %s to %s\n",
9821 aarch32_mode_name(env->uncached_cpsr),
9822 aarch32_mode_name(val));
37064a8b 9823 } else {
81e37284
PM
9824 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9825 write_type == CPSRWriteExceptionReturn ?
9826 "Exception return from AArch32" :
9827 "AArch32 mode switch from",
9828 aarch32_mode_name(env->uncached_cpsr),
9829 aarch32_mode_name(val), env->regs[15]);
37064a8b
PM
9830 switch_mode(env, val & CPSR_M);
9831 }
2f4a40e5
AZ
9832 }
9833 mask &= ~CACHED_CPSR_BITS;
9834 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
e784807c
PM
9835 if (rebuild_hflags) {
9836 arm_rebuild_hflags(env);
9837 }
2f4a40e5
AZ
9838}
9839
b26eefb6
PB
9840/* Sign/zero extend */
9841uint32_t HELPER(sxtb16)(uint32_t x)
9842{
9843 uint32_t res;
9844 res = (uint16_t)(int8_t)x;
9845 res |= (uint32_t)(int8_t)(x >> 16) << 16;
9846 return res;
9847}
9848
e5346292
PM
9849static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9850{
9851 /*
9852 * Take a division-by-zero exception if necessary; otherwise return
9853 * to get the usual non-trapping division behaviour (result of 0)
9854 */
9855 if (arm_feature(env, ARM_FEATURE_M)
9856 && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9857 raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9858 }
9859}
9860
b26eefb6
PB
9861uint32_t HELPER(uxtb16)(uint32_t x)
9862{
9863 uint32_t res;
9864 res = (uint16_t)(uint8_t)x;
9865 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9866 return res;
9867}
9868
e5346292 9869int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
3670669c 9870{
fc7a5038 9871 if (den == 0) {
e5346292 9872 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9873 return 0;
9874 }
9875 if (num == INT_MIN && den == -1) {
9876 return INT_MIN;
9877 }
3670669c
PB
9878 return num / den;
9879}
9880
e5346292 9881uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
3670669c 9882{
fc7a5038 9883 if (den == 0) {
e5346292 9884 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9885 return 0;
9886 }
3670669c
PB
9887 return num / den;
9888}
9889
9890uint32_t HELPER(rbit)(uint32_t x)
9891{
42fedbca 9892 return revbit32(x);
3670669c
PB
9893}
9894
c47eaf9f 9895#ifdef CONFIG_USER_ONLY
b5ff1b31 9896
affdb64d 9897static void switch_mode(CPUARMState *env, int mode)
b5ff1b31 9898{
2fc0cc0e 9899 ARMCPU *cpu = env_archcpu(env);
a47dddd7
AF
9900
9901 if (mode != ARM_CPU_MODE_USR) {
9902 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9903 }
b5ff1b31
FB
9904}
9905
012a906b
GB
9906uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9907 uint32_t cur_el, bool secure)
9e729b57
EI
9908{
9909 return 1;
9910}
9911
ce02049d
GB
9912void aarch64_sync_64_to_32(CPUARMState *env)
9913{
9914 g_assert_not_reached();
9915}
9916
b5ff1b31
FB
9917#else
9918
affdb64d 9919static void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
9920{
9921 int old_mode;
9922 int i;
9923
9924 old_mode = env->uncached_cpsr & CPSR_M;
f927dbda 9925 if (mode == old_mode) {
b5ff1b31 9926 return;
f927dbda 9927 }
b5ff1b31
FB
9928
9929 if (old_mode == ARM_CPU_MODE_FIQ) {
04215eb1
FR
9930 memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9931 memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31 9932 } else if (mode == ARM_CPU_MODE_FIQ) {
04215eb1
FR
9933 memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9934 memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
9935 }
9936
f5206413 9937 i = bank_number(old_mode);
b5ff1b31 9938 env->banked_r13[i] = env->regs[13];
b5ff1b31
FB
9939 env->banked_spsr[i] = env->spsr;
9940
f5206413 9941 i = bank_number(mode);
b5ff1b31 9942 env->regs[13] = env->banked_r13[i];
b5ff1b31 9943 env->spsr = env->banked_spsr[i];
593cfa2b
PM
9944
9945 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9946 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
b5ff1b31
FB
9947}
9948
9b37a28c
FR
9949/*
9950 * Physical Interrupt Target EL Lookup Table
0eeb17d6
GB
9951 *
9952 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9953 *
9954 * The below multi-dimensional table is used for looking up the target
9955 * exception level given numerous condition criteria. Specifically, the
9956 * target EL is based on SCR and HCR routing controls as well as the
9957 * currently executing EL and secure state.
9958 *
9959 * Dimensions:
9960 * target_el_table[2][2][2][2][2][4]
9961 * | | | | | +--- Current EL
9962 * | | | | +------ Non-secure(0)/Secure(1)
9963 * | | | +--------- HCR mask override
9964 * | | +------------ SCR exec state control
9965 * | +--------------- SCR mask override
9966 * +------------------ 32-bit(0)/64-bit(1) EL3
9967 *
9968 * The table values are as such:
9969 * 0-3 = EL0-EL3
9970 * -1 = Cannot occur
9971 *
9972 * The ARM ARM target EL table includes entries indicating that an "exception
9973 * is not taken". The two cases where this is applicable are:
9974 * 1) An exception is taken from EL3 but the SCR does not have the exception
9975 * routed to EL3.
9976 * 2) An exception is taken from EL2 but the HCR does not have the exception
9977 * routed to EL2.
9978 * In these two cases, the below table contain a target of EL1. This value is
9979 * returned as it is expected that the consumer of the table data will check
9980 * for "target EL >= current EL" to ensure the exception is not taken.
9981 *
9982 * SCR HCR
9983 * 64 EA AMO From
9984 * BIT IRQ IMO Non-secure Secure
9985 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
9986 */
82c39f6a 9987static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
9988 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9989 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
9990 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
9991 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
9992 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9993 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
9994 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
9995 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
9996 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6c85f906
RDC
9997 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
9998 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
9999 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
0eeb17d6
GB
10000 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
10001 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6c85f906
RDC
10002 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
10003 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
0eeb17d6
GB
10004};
10005
10006/*
10007 * Determine the target EL for physical exceptions
10008 */
012a906b
GB
10009uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10010 uint32_t cur_el, bool secure)
0eeb17d6
GB
10011{
10012 CPUARMState *env = cs->env_ptr;
f7778444
RH
10013 bool rw;
10014 bool scr;
10015 bool hcr;
0eeb17d6 10016 int target_el;
2cde031f 10017 /* Is the highest EL AArch64? */
f7778444
RH
10018 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
10019 uint64_t hcr_el2;
2cde031f
SS
10020
10021 if (arm_feature(env, ARM_FEATURE_EL3)) {
10022 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
10023 } else {
9b37a28c
FR
10024 /*
10025 * Either EL2 is the highest EL (and so the EL2 register width
2cde031f
SS
10026 * is given by is64); or there is no EL2 or EL3, in which case
10027 * the value of 'rw' does not affect the table lookup anyway.
10028 */
10029 rw = is64;
10030 }
0eeb17d6 10031
f7778444 10032 hcr_el2 = arm_hcr_el2_eff(env);
0eeb17d6
GB
10033 switch (excp_idx) {
10034 case EXCP_IRQ:
10035 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
f7778444 10036 hcr = hcr_el2 & HCR_IMO;
0eeb17d6
GB
10037 break;
10038 case EXCP_FIQ:
10039 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
f7778444 10040 hcr = hcr_el2 & HCR_FMO;
0eeb17d6
GB
10041 break;
10042 default:
10043 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
f7778444 10044 hcr = hcr_el2 & HCR_AMO;
0eeb17d6
GB
10045 break;
10046 };
10047
d1b31428
RH
10048 /*
10049 * For these purposes, TGE and AMO/IMO/FMO both force the
10050 * interrupt to EL2. Fold TGE into the bit extracted above.
10051 */
10052 hcr |= (hcr_el2 & HCR_TGE) != 0;
10053
0eeb17d6
GB
10054 /* Perform a table-lookup for the target EL given the current state */
10055 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
10056
10057 assert(target_el > 0);
10058
10059 return target_el;
10060}
10061
fc6177af 10062void arm_log_exception(CPUState *cs)
b59f479b 10063{
fc6177af
PM
10064 int idx = cs->exception_index;
10065
b59f479b
PMD
10066 if (qemu_loglevel_mask(CPU_LOG_INT)) {
10067 const char *exc = NULL;
10068 static const char * const excnames[] = {
10069 [EXCP_UDEF] = "Undefined Instruction",
10070 [EXCP_SWI] = "SVC",
10071 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
10072 [EXCP_DATA_ABORT] = "Data Abort",
10073 [EXCP_IRQ] = "IRQ",
10074 [EXCP_FIQ] = "FIQ",
10075 [EXCP_BKPT] = "Breakpoint",
10076 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
10077 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
10078 [EXCP_HVC] = "Hypervisor Call",
10079 [EXCP_HYP_TRAP] = "Hypervisor Trap",
10080 [EXCP_SMC] = "Secure Monitor Call",
10081 [EXCP_VIRQ] = "Virtual IRQ",
10082 [EXCP_VFIQ] = "Virtual FIQ",
10083 [EXCP_SEMIHOST] = "Semihosting call",
10084 [EXCP_NOCP] = "v7M NOCP UsageFault",
10085 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
10086 [EXCP_STKOF] = "v8M STKOF UsageFault",
10087 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
10088 [EXCP_LSERR] = "v8M LSERR UsageFault",
10089 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
e5346292 10090 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
3c29632f 10091 [EXCP_VSERR] = "Virtual SERR",
b59f479b
PMD
10092 };
10093
10094 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
10095 exc = excnames[idx];
10096 }
10097 if (!exc) {
10098 exc = "unknown";
10099 }
fc6177af
PM
10100 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
10101 idx, exc, cs->cpu_index);
b59f479b
PMD
10102 }
10103}
10104
a356dacf 10105/*
7aab5a8c
PMD
10106 * Function used to synchronize QEMU's AArch64 register set with AArch32
10107 * register set. This is necessary when switching between AArch32 and AArch64
10108 * execution state.
a356dacf 10109 */
7aab5a8c 10110void aarch64_sync_32_to_64(CPUARMState *env)
9ee6e8bb 10111{
7aab5a8c
PMD
10112 int i;
10113 uint32_t mode = env->uncached_cpsr & CPSR_M;
10114
10115 /* We can blanket copy R[0:7] to X[0:7] */
10116 for (i = 0; i < 8; i++) {
10117 env->xregs[i] = env->regs[i];
fd592d89 10118 }
70d74660 10119
9a223097 10120 /*
7aab5a8c
PMD
10121 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
10122 * Otherwise, they come from the banked user regs.
fd592d89 10123 */
7aab5a8c
PMD
10124 if (mode == ARM_CPU_MODE_FIQ) {
10125 for (i = 8; i < 13; i++) {
10126 env->xregs[i] = env->usr_regs[i - 8];
10127 }
10128 } else {
10129 for (i = 8; i < 13; i++) {
10130 env->xregs[i] = env->regs[i];
10131 }
fd592d89 10132 }
9ee6e8bb 10133
7aab5a8c
PMD
10134 /*
10135 * Registers x13-x23 are the various mode SP and FP registers. Registers
10136 * r13 and r14 are only copied if we are in that mode, otherwise we copy
10137 * from the mode banked register.
10138 */
10139 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10140 env->xregs[13] = env->regs[13];
10141 env->xregs[14] = env->regs[14];
10142 } else {
10143 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
10144 /* HYP is an exception in that it is copied from r14 */
10145 if (mode == ARM_CPU_MODE_HYP) {
10146 env->xregs[14] = env->regs[14];
95695eff 10147 } else {
7aab5a8c 10148 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
95695eff 10149 }
95695eff
PM
10150 }
10151
7aab5a8c
PMD
10152 if (mode == ARM_CPU_MODE_HYP) {
10153 env->xregs[15] = env->regs[13];
10154 } else {
10155 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
95695eff
PM
10156 }
10157
7aab5a8c
PMD
10158 if (mode == ARM_CPU_MODE_IRQ) {
10159 env->xregs[16] = env->regs[14];
10160 env->xregs[17] = env->regs[13];
10161 } else {
10162 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
10163 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
10164 }
95695eff 10165
7aab5a8c
PMD
10166 if (mode == ARM_CPU_MODE_SVC) {
10167 env->xregs[18] = env->regs[14];
10168 env->xregs[19] = env->regs[13];
10169 } else {
10170 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
10171 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
10172 }
95695eff 10173
7aab5a8c
PMD
10174 if (mode == ARM_CPU_MODE_ABT) {
10175 env->xregs[20] = env->regs[14];
10176 env->xregs[21] = env->regs[13];
10177 } else {
10178 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10179 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10180 }
e33cf0f8 10181
7aab5a8c
PMD
10182 if (mode == ARM_CPU_MODE_UND) {
10183 env->xregs[22] = env->regs[14];
10184 env->xregs[23] = env->regs[13];
10185 } else {
10186 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10187 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
e33cf0f8
PM
10188 }
10189
10190 /*
7aab5a8c
PMD
10191 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
10192 * mode, then we can copy from r8-r14. Otherwise, we copy from the
10193 * FIQ bank for r8-r14.
e33cf0f8 10194 */
7aab5a8c
PMD
10195 if (mode == ARM_CPU_MODE_FIQ) {
10196 for (i = 24; i < 31; i++) {
10197 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
10198 }
10199 } else {
10200 for (i = 24; i < 29; i++) {
10201 env->xregs[i] = env->fiq_regs[i - 24];
e33cf0f8 10202 }
7aab5a8c
PMD
10203 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10204 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
e33cf0f8 10205 }
7aab5a8c
PMD
10206
10207 env->pc = env->regs[15];
e33cf0f8
PM
10208}
10209
9a223097 10210/*
7aab5a8c
PMD
10211 * Function used to synchronize QEMU's AArch32 register set with AArch64
10212 * register set. This is necessary when switching between AArch32 and AArch64
10213 * execution state.
de2db7ec 10214 */
7aab5a8c 10215void aarch64_sync_64_to_32(CPUARMState *env)
9ee6e8bb 10216{
7aab5a8c
PMD
10217 int i;
10218 uint32_t mode = env->uncached_cpsr & CPSR_M;
abc24d86 10219
7aab5a8c
PMD
10220 /* We can blanket copy X[0:7] to R[0:7] */
10221 for (i = 0; i < 8; i++) {
10222 env->regs[i] = env->xregs[i];
de2db7ec 10223 }
3f0cddee 10224
9a223097 10225 /*
7aab5a8c
PMD
10226 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10227 * Otherwise, we copy x8-x12 into the banked user regs.
de2db7ec 10228 */
7aab5a8c
PMD
10229 if (mode == ARM_CPU_MODE_FIQ) {
10230 for (i = 8; i < 13; i++) {
10231 env->usr_regs[i - 8] = env->xregs[i];
10232 }
10233 } else {
10234 for (i = 8; i < 13; i++) {
10235 env->regs[i] = env->xregs[i];
10236 }
fb602cb7
PM
10237 }
10238
9a223097 10239 /*
7aab5a8c
PMD
10240 * Registers r13 & r14 depend on the current mode.
10241 * If we are in a given mode, we copy the corresponding x registers to r13
10242 * and r14. Otherwise, we copy the x register to the banked r13 and r14
10243 * for the mode.
fb602cb7 10244 */
7aab5a8c
PMD
10245 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10246 env->regs[13] = env->xregs[13];
10247 env->regs[14] = env->xregs[14];
fb602cb7 10248 } else {
7aab5a8c 10249 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
fb602cb7 10250
7aab5a8c
PMD
10251 /*
10252 * HYP is an exception in that it does not have its own banked r14 but
10253 * shares the USR r14
10254 */
10255 if (mode == ARM_CPU_MODE_HYP) {
10256 env->regs[14] = env->xregs[14];
10257 } else {
10258 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10259 }
10260 }
fb602cb7 10261
7aab5a8c
PMD
10262 if (mode == ARM_CPU_MODE_HYP) {
10263 env->regs[13] = env->xregs[15];
fb602cb7 10264 } else {
7aab5a8c 10265 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
fb602cb7 10266 }
d02a8698 10267
7aab5a8c
PMD
10268 if (mode == ARM_CPU_MODE_IRQ) {
10269 env->regs[14] = env->xregs[16];
10270 env->regs[13] = env->xregs[17];
d02a8698 10271 } else {
7aab5a8c
PMD
10272 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10273 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
d02a8698
PM
10274 }
10275
7aab5a8c
PMD
10276 if (mode == ARM_CPU_MODE_SVC) {
10277 env->regs[14] = env->xregs[18];
10278 env->regs[13] = env->xregs[19];
10279 } else {
10280 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10281 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
fb602cb7
PM
10282 }
10283
7aab5a8c
PMD
10284 if (mode == ARM_CPU_MODE_ABT) {
10285 env->regs[14] = env->xregs[20];
10286 env->regs[13] = env->xregs[21];
10287 } else {
10288 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10289 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
10290 }
10291
10292 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
10293 env->regs[14] = env->xregs[22];
10294 env->regs[13] = env->xregs[23];
ce02049d 10295 } else {
593cfa2b 10296 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
3a9148d0 10297 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
10298 }
10299
9b37a28c
FR
10300 /*
10301 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
ce02049d
GB
10302 * mode, then we can copy to r8-r14. Otherwise, we copy to the
10303 * FIQ bank for r8-r14.
10304 */
10305 if (mode == ARM_CPU_MODE_FIQ) {
10306 for (i = 24; i < 31; i++) {
10307 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
10308 }
10309 } else {
10310 for (i = 24; i < 29; i++) {
10311 env->fiq_regs[i - 24] = env->xregs[i];
10312 }
10313 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
593cfa2b 10314 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
ce02049d
GB
10315 }
10316
10317 env->regs[15] = env->pc;
10318}
10319
dea8378b
PM
10320static void take_aarch32_exception(CPUARMState *env, int new_mode,
10321 uint32_t mask, uint32_t offset,
10322 uint32_t newpc)
10323{
4a2696c0
RH
10324 int new_el;
10325
dea8378b
PM
10326 /* Change the CPU state so as to actually take the exception. */
10327 switch_mode(env, new_mode);
4a2696c0 10328
dea8378b
PM
10329 /*
10330 * For exceptions taken to AArch32 we must clear the SS bit in both
10331 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10332 */
f944a854 10333 env->pstate &= ~PSTATE_SS;
dea8378b
PM
10334 env->spsr = cpsr_read(env);
10335 /* Clear IT bits. */
10336 env->condexec_bits = 0;
10337 /* Switch to the new mode, and to the correct instruction set. */
10338 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
88828bf1
CD
10339
10340 /* This must be after mode switching. */
10341 new_el = arm_current_el(env);
10342
dea8378b
PM
10343 /* Set new mode endianness */
10344 env->uncached_cpsr &= ~CPSR_E;
4a2696c0 10345 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
dea8378b
PM
10346 env->uncached_cpsr |= CPSR_E;
10347 }
829f9fd3
PM
10348 /* J and IL must always be cleared for exception entry */
10349 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
dea8378b
PM
10350 env->daif |= mask;
10351
f2f68a78
RC
10352 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10353 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10354 env->uncached_cpsr |= CPSR_SSBS;
10355 } else {
10356 env->uncached_cpsr &= ~CPSR_SSBS;
10357 }
10358 }
10359
dea8378b
PM
10360 if (new_mode == ARM_CPU_MODE_HYP) {
10361 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10362 env->elr_el[2] = env->regs[15];
10363 } else {
4a2696c0 10364 /* CPSR.PAN is normally preserved preserved unless... */
f8af1143 10365 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
4a2696c0
RH
10366 switch (new_el) {
10367 case 3:
10368 if (!arm_is_secure_below_el3(env)) {
10369 /* ... the target is EL3, from non-secure state. */
10370 env->uncached_cpsr &= ~CPSR_PAN;
10371 break;
10372 }
10373 /* ... the target is EL3, from secure state ... */
10374 /* fall through */
10375 case 1:
10376 /* ... the target is EL1 and SCTLR.SPAN is 0. */
10377 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10378 env->uncached_cpsr |= CPSR_PAN;
10379 }
10380 break;
10381 }
10382 }
dea8378b
PM
10383 /*
10384 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10385 * and we should just guard the thumb mode on V4
10386 */
10387 if (arm_feature(env, ARM_FEATURE_V4T)) {
10388 env->thumb =
10389 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10390 }
10391 env->regs[14] = env->regs[15] + offset;
10392 }
10393 env->regs[15] = newpc;
a8a79c7a 10394 arm_rebuild_hflags(env);
dea8378b
PM
10395}
10396
b9bc21ff
PM
10397static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10398{
10399 /*
10400 * Handle exception entry to Hyp mode; this is sufficiently
10401 * different to entry to other AArch32 modes that we handle it
10402 * separately here.
10403 *
10404 * The vector table entry used is always the 0x14 Hyp mode entry point,
2c023d36 10405 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
b9bc21ff
PM
10406 * The offset applied to the preferred return address is always zero
10407 * (see DDI0487C.a section G1.12.3).
10408 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10409 */
10410 uint32_t addr, mask;
10411 ARMCPU *cpu = ARM_CPU(cs);
10412 CPUARMState *env = &cpu->env;
10413
10414 switch (cs->exception_index) {
10415 case EXCP_UDEF:
10416 addr = 0x04;
10417 break;
10418 case EXCP_SWI:
2c023d36 10419 addr = 0x08;
b9bc21ff
PM
10420 break;
10421 case EXCP_BKPT:
10422 /* Fall through to prefetch abort. */
10423 case EXCP_PREFETCH_ABORT:
10424 env->cp15.ifar_s = env->exception.vaddress;
10425 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10426 (uint32_t)env->exception.vaddress);
10427 addr = 0x0c;
10428 break;
10429 case EXCP_DATA_ABORT:
10430 env->cp15.dfar_s = env->exception.vaddress;
10431 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10432 (uint32_t)env->exception.vaddress);
10433 addr = 0x10;
10434 break;
10435 case EXCP_IRQ:
10436 addr = 0x18;
10437 break;
10438 case EXCP_FIQ:
10439 addr = 0x1c;
10440 break;
10441 case EXCP_HVC:
10442 addr = 0x08;
10443 break;
10444 case EXCP_HYP_TRAP:
10445 addr = 0x14;
9bbb4ef9 10446 break;
b9bc21ff
PM
10447 default:
10448 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10449 }
10450
10451 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
2ed08180
PM
10452 if (!arm_feature(env, ARM_FEATURE_V8)) {
10453 /*
10454 * QEMU syndrome values are v8-style. v7 has the IL bit
10455 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10456 * If this is a v7 CPU, squash the IL bit in those cases.
10457 */
10458 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10459 (cs->exception_index == EXCP_DATA_ABORT &&
10460 !(env->exception.syndrome & ARM_EL_ISV)) ||
10461 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10462 env->exception.syndrome &= ~ARM_EL_IL;
10463 }
10464 }
b9bc21ff
PM
10465 env->cp15.esr_el[2] = env->exception.syndrome;
10466 }
10467
10468 if (arm_current_el(env) != 2 && addr < 0x14) {
10469 addr = 0x14;
10470 }
10471
10472 mask = 0;
10473 if (!(env->cp15.scr_el3 & SCR_EA)) {
10474 mask |= CPSR_A;
10475 }
10476 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10477 mask |= CPSR_I;
10478 }
10479 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10480 mask |= CPSR_F;
10481 }
10482
10483 addr += env->cp15.hvbar;
10484
10485 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10486}
10487
966f758c 10488static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 10489{
97a8ea5a
AF
10490 ARMCPU *cpu = ARM_CPU(cs);
10491 CPUARMState *env = &cpu->env;
b5ff1b31
FB
10492 uint32_t addr;
10493 uint32_t mask;
10494 int new_mode;
10495 uint32_t offset;
16a906fd 10496 uint32_t moe;
b5ff1b31 10497
16a906fd 10498 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
64b91e3f 10499 switch (syn_get_ec(env->exception.syndrome)) {
16a906fd
PM
10500 case EC_BREAKPOINT:
10501 case EC_BREAKPOINT_SAME_EL:
10502 moe = 1;
10503 break;
10504 case EC_WATCHPOINT:
10505 case EC_WATCHPOINT_SAME_EL:
10506 moe = 10;
10507 break;
10508 case EC_AA32_BKPT:
10509 moe = 3;
10510 break;
10511 case EC_VECTORCATCH:
10512 moe = 5;
10513 break;
10514 default:
10515 moe = 0;
10516 break;
10517 }
10518
10519 if (moe) {
10520 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10521 }
10522
b9bc21ff
PM
10523 if (env->exception.target_el == 2) {
10524 arm_cpu_do_interrupt_aarch32_hyp(cs);
10525 return;
10526 }
10527
27103424 10528 switch (cs->exception_index) {
b5ff1b31
FB
10529 case EXCP_UDEF:
10530 new_mode = ARM_CPU_MODE_UND;
10531 addr = 0x04;
10532 mask = CPSR_I;
f927dbda 10533 if (env->thumb) {
b5ff1b31 10534 offset = 2;
f927dbda 10535 } else {
b5ff1b31 10536 offset = 4;
f927dbda 10537 }
b5ff1b31
FB
10538 break;
10539 case EXCP_SWI:
10540 new_mode = ARM_CPU_MODE_SVC;
10541 addr = 0x08;
10542 mask = CPSR_I;
601d70b9 10543 /* The PC already points to the next instruction. */
b5ff1b31
FB
10544 offset = 0;
10545 break;
06c949e6 10546 case EXCP_BKPT:
9ee6e8bb
PB
10547 /* Fall through to prefetch abort. */
10548 case EXCP_PREFETCH_ABORT:
88ca1c2d 10549 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 10550 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 10551 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 10552 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
10553 new_mode = ARM_CPU_MODE_ABT;
10554 addr = 0x0c;
10555 mask = CPSR_A | CPSR_I;
10556 offset = 4;
10557 break;
10558 case EXCP_DATA_ABORT:
4a7e2d73 10559 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 10560 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 10561 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 10562 env->exception.fsr,
6cd8a264 10563 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
10564 new_mode = ARM_CPU_MODE_ABT;
10565 addr = 0x10;
10566 mask = CPSR_A | CPSR_I;
10567 offset = 8;
10568 break;
10569 case EXCP_IRQ:
10570 new_mode = ARM_CPU_MODE_IRQ;
10571 addr = 0x18;
10572 /* Disable IRQ and imprecise data aborts. */
10573 mask = CPSR_A | CPSR_I;
10574 offset = 4;
de38d23b
FA
10575 if (env->cp15.scr_el3 & SCR_IRQ) {
10576 /* IRQ routed to monitor mode */
10577 new_mode = ARM_CPU_MODE_MON;
10578 mask |= CPSR_F;
10579 }
b5ff1b31
FB
10580 break;
10581 case EXCP_FIQ:
10582 new_mode = ARM_CPU_MODE_FIQ;
10583 addr = 0x1c;
10584 /* Disable FIQ, IRQ and imprecise data aborts. */
10585 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
10586 if (env->cp15.scr_el3 & SCR_FIQ) {
10587 /* FIQ routed to monitor mode */
10588 new_mode = ARM_CPU_MODE_MON;
10589 }
b5ff1b31
FB
10590 offset = 4;
10591 break;
87a4b270
PM
10592 case EXCP_VIRQ:
10593 new_mode = ARM_CPU_MODE_IRQ;
10594 addr = 0x18;
10595 /* Disable IRQ and imprecise data aborts. */
10596 mask = CPSR_A | CPSR_I;
10597 offset = 4;
10598 break;
10599 case EXCP_VFIQ:
10600 new_mode = ARM_CPU_MODE_FIQ;
10601 addr = 0x1c;
10602 /* Disable FIQ, IRQ and imprecise data aborts. */
10603 mask = CPSR_A | CPSR_I | CPSR_F;
10604 offset = 4;
10605 break;
3c29632f
RH
10606 case EXCP_VSERR:
10607 {
10608 /*
10609 * Note that this is reported as a data abort, but the DFAR
10610 * has an UNKNOWN value. Construct the SError syndrome from
10611 * AET and ExT fields.
10612 */
10613 ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10614
10615 if (extended_addresses_enabled(env)) {
10616 env->exception.fsr = arm_fi_to_lfsc(&fi);
10617 } else {
10618 env->exception.fsr = arm_fi_to_sfsc(&fi);
10619 }
10620 env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10621 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10622 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10623 env->exception.fsr);
10624
10625 new_mode = ARM_CPU_MODE_ABT;
10626 addr = 0x10;
10627 mask = CPSR_A | CPSR_I;
10628 offset = 8;
10629 }
10630 break;
dbe9d163
FA
10631 case EXCP_SMC:
10632 new_mode = ARM_CPU_MODE_MON;
10633 addr = 0x08;
10634 mask = CPSR_A | CPSR_I | CPSR_F;
10635 offset = 0;
10636 break;
b5ff1b31 10637 default:
a47dddd7 10638 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
10639 return; /* Never happens. Keep compiler happy. */
10640 }
e89e51a1
FA
10641
10642 if (new_mode == ARM_CPU_MODE_MON) {
10643 addr += env->cp15.mvbar;
137feaa9 10644 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 10645 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 10646 addr += 0xffff0000;
8641136c 10647 } else {
9b37a28c
FR
10648 /*
10649 * ARM v7 architectures provide a vector base address register to remap
8641136c 10650 * the interrupt vector table.
e89e51a1 10651 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
10652 * Note: only bits 31:5 are valid.
10653 */
fb6c91ba 10654 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 10655 }
dbe9d163
FA
10656
10657 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10658 env->cp15.scr_el3 &= ~SCR_NS;
10659 }
10660
dea8378b 10661 take_aarch32_exception(env, new_mode, mask, offset, addr);
b5ff1b31
FB
10662}
10663
a65dabf7
PM
10664static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10665{
10666 /*
10667 * Return the register number of the AArch64 view of the AArch32
10668 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10669 * be that of the AArch32 mode the exception came from.
10670 */
10671 int mode = env->uncached_cpsr & CPSR_M;
10672
10673 switch (aarch32_reg) {
10674 case 0 ... 7:
10675 return aarch32_reg;
10676 case 8 ... 12:
10677 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10678 case 13:
10679 switch (mode) {
10680 case ARM_CPU_MODE_USR:
10681 case ARM_CPU_MODE_SYS:
10682 return 13;
10683 case ARM_CPU_MODE_HYP:
10684 return 15;
10685 case ARM_CPU_MODE_IRQ:
10686 return 17;
10687 case ARM_CPU_MODE_SVC:
10688 return 19;
10689 case ARM_CPU_MODE_ABT:
10690 return 21;
10691 case ARM_CPU_MODE_UND:
10692 return 23;
10693 case ARM_CPU_MODE_FIQ:
10694 return 29;
10695 default:
10696 g_assert_not_reached();
10697 }
10698 case 14:
10699 switch (mode) {
10700 case ARM_CPU_MODE_USR:
10701 case ARM_CPU_MODE_SYS:
10702 case ARM_CPU_MODE_HYP:
10703 return 14;
10704 case ARM_CPU_MODE_IRQ:
10705 return 16;
10706 case ARM_CPU_MODE_SVC:
10707 return 18;
10708 case ARM_CPU_MODE_ABT:
10709 return 20;
10710 case ARM_CPU_MODE_UND:
10711 return 22;
10712 case ARM_CPU_MODE_FIQ:
10713 return 30;
10714 default:
10715 g_assert_not_reached();
10716 }
10717 case 15:
10718 return 31;
10719 default:
10720 g_assert_not_reached();
10721 }
10722}
10723
f944a854
RC
10724static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10725{
10726 uint32_t ret = cpsr_read(env);
10727
10728 /* Move DIT to the correct location for SPSR_ELx */
10729 if (ret & CPSR_DIT) {
10730 ret &= ~CPSR_DIT;
10731 ret |= PSTATE_DIT;
10732 }
10733 /* Merge PSTATE.SS into SPSR_ELx */
10734 ret |= env->pstate & PSTATE_SS;
10735
10736 return ret;
10737}
10738
7ac61020
PM
10739static bool syndrome_is_sync_extabt(uint32_t syndrome)
10740{
10741 /* Return true if this syndrome value is a synchronous external abort */
10742 switch (syn_get_ec(syndrome)) {
10743 case EC_INSNABORT:
10744 case EC_INSNABORT_SAME_EL:
10745 case EC_DATAABORT:
10746 case EC_DATAABORT_SAME_EL:
10747 /* Look at fault status code for all the synchronous ext abort cases */
10748 switch (syndrome & 0x3f) {
10749 case 0x10:
10750 case 0x13:
10751 case 0x14:
10752 case 0x15:
10753 case 0x16:
10754 case 0x17:
10755 return true;
10756 default:
10757 return false;
10758 }
10759 default:
10760 return false;
10761 }
10762}
10763
966f758c
PM
10764/* Handle exception entry to a target EL which is using AArch64 */
10765static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
10766{
10767 ARMCPU *cpu = ARM_CPU(cs);
10768 CPUARMState *env = &cpu->env;
10769 unsigned int new_el = env->exception.target_el;
10770 target_ulong addr = env->cp15.vbar_el[new_el];
10771 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
4a2696c0 10772 unsigned int old_mode;
0ab5953b 10773 unsigned int cur_el = arm_current_el(env);
a65dabf7 10774 int rt;
0ab5953b 10775
9a05f7b6
RH
10776 /*
10777 * Note that new_el can never be 0. If cur_el is 0, then
10778 * el0_a64 is is_a64(), else el0_a64 is ignored.
10779 */
10780 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
f3a9b694 10781
0ab5953b 10782 if (cur_el < new_el) {
9b37a28c
FR
10783 /*
10784 * Entry vector offset depends on whether the implemented EL
3d6f7617
PM
10785 * immediately lower than the target level is using AArch32 or AArch64
10786 */
10787 bool is_aa64;
cb092fbb 10788 uint64_t hcr;
3d6f7617
PM
10789
10790 switch (new_el) {
10791 case 3:
10792 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10793 break;
10794 case 2:
cb092fbb
RH
10795 hcr = arm_hcr_el2_eff(env);
10796 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10797 is_aa64 = (hcr & HCR_RW) != 0;
10798 break;
10799 }
10800 /* fall through */
3d6f7617
PM
10801 case 1:
10802 is_aa64 = is_a64(env);
10803 break;
10804 default:
10805 g_assert_not_reached();
10806 }
10807
10808 if (is_aa64) {
f3a9b694
PM
10809 addr += 0x400;
10810 } else {
10811 addr += 0x600;
10812 }
10813 } else if (pstate_read(env) & PSTATE_SP) {
10814 addr += 0x200;
10815 }
10816
f3a9b694
PM
10817 switch (cs->exception_index) {
10818 case EXCP_PREFETCH_ABORT:
10819 case EXCP_DATA_ABORT:
7ac61020
PM
10820 /*
10821 * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10822 * to be taken to the SError vector entrypoint.
10823 */
10824 if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10825 syndrome_is_sync_extabt(env->exception.syndrome)) {
10826 addr += 0x180;
10827 }
f3a9b694
PM
10828 env->cp15.far_el[new_el] = env->exception.vaddress;
10829 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10830 env->cp15.far_el[new_el]);
10831 /* fall through */
10832 case EXCP_BKPT:
10833 case EXCP_UDEF:
10834 case EXCP_SWI:
10835 case EXCP_HVC:
10836 case EXCP_HYP_TRAP:
10837 case EXCP_SMC:
a65dabf7
PM
10838 switch (syn_get_ec(env->exception.syndrome)) {
10839 case EC_ADVSIMDFPACCESSTRAP:
4be42f40
PM
10840 /*
10841 * QEMU internal FP/SIMD syndromes from AArch32 include the
10842 * TA and coproc fields which are only exposed if the exception
10843 * is taken to AArch32 Hyp mode. Mask them out to get a valid
10844 * AArch64 format syndrome.
10845 */
10846 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
a65dabf7
PM
10847 break;
10848 case EC_CP14RTTRAP:
10849 case EC_CP15RTTRAP:
10850 case EC_CP14DTTRAP:
10851 /*
10852 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10853 * the raw register field from the insn; when taking this to
10854 * AArch64 we must convert it to the AArch64 view of the register
10855 * number. Notice that we read a 4-bit AArch32 register number and
10856 * write back a 5-bit AArch64 one.
10857 */
10858 rt = extract32(env->exception.syndrome, 5, 4);
10859 rt = aarch64_regnum(env, rt);
10860 env->exception.syndrome = deposit32(env->exception.syndrome,
10861 5, 5, rt);
10862 break;
10863 case EC_CP15RRTTRAP:
10864 case EC_CP14RRTTRAP:
10865 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10866 rt = extract32(env->exception.syndrome, 5, 4);
10867 rt = aarch64_regnum(env, rt);
10868 env->exception.syndrome = deposit32(env->exception.syndrome,
10869 5, 5, rt);
10870 rt = extract32(env->exception.syndrome, 10, 4);
10871 rt = aarch64_regnum(env, rt);
10872 env->exception.syndrome = deposit32(env->exception.syndrome,
10873 10, 5, rt);
10874 break;
4be42f40 10875 }
f3a9b694
PM
10876 env->cp15.esr_el[new_el] = env->exception.syndrome;
10877 break;
10878 case EXCP_IRQ:
10879 case EXCP_VIRQ:
10880 addr += 0x80;
10881 break;
10882 case EXCP_FIQ:
10883 case EXCP_VFIQ:
10884 addr += 0x100;
10885 break;
3c29632f
RH
10886 case EXCP_VSERR:
10887 addr += 0x180;
10888 /* Construct the SError syndrome from IDS and ISS fields. */
10889 env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10890 env->cp15.esr_el[new_el] = env->exception.syndrome;
10891 break;
f3a9b694
PM
10892 default:
10893 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10894 }
10895
10896 if (is_a64(env)) {
4a2696c0 10897 old_mode = pstate_read(env);
f3a9b694
PM
10898 aarch64_save_sp(env, arm_current_el(env));
10899 env->elr_el[new_el] = env->pc;
10900 } else {
f944a854 10901 old_mode = cpsr_read_for_spsr_elx(env);
f3a9b694
PM
10902 env->elr_el[new_el] = env->regs[15];
10903
10904 aarch64_sync_32_to_64(env);
10905
10906 env->condexec_bits = 0;
10907 }
4a2696c0
RH
10908 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10909
f3a9b694
PM
10910 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10911 env->elr_el[new_el]);
10912
4a2696c0
RH
10913 if (cpu_isar_feature(aa64_pan, cpu)) {
10914 /* The value of PSTATE.PAN is normally preserved, except when ... */
10915 new_mode |= old_mode & PSTATE_PAN;
10916 switch (new_el) {
10917 case 2:
10918 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
10919 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10920 != (HCR_E2H | HCR_TGE)) {
10921 break;
10922 }
10923 /* fall through */
10924 case 1:
10925 /* ... the target is EL1 ... */
10926 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
10927 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10928 new_mode |= PSTATE_PAN;
10929 }
10930 break;
10931 }
10932 }
34669338
RH
10933 if (cpu_isar_feature(aa64_mte, cpu)) {
10934 new_mode |= PSTATE_TCO;
10935 }
4a2696c0 10936
f2f68a78
RC
10937 if (cpu_isar_feature(aa64_ssbs, cpu)) {
10938 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10939 new_mode |= PSTATE_SSBS;
10940 } else {
10941 new_mode &= ~PSTATE_SSBS;
10942 }
10943 }
10944
f3a9b694 10945 pstate_write(env, PSTATE_DAIF | new_mode);
53221552 10946 env->aarch64 = true;
f3a9b694 10947 aarch64_restore_sp(env, new_el);
a8a79c7a 10948 helper_rebuild_hflags_a64(env, new_el);
f3a9b694
PM
10949
10950 env->pc = addr;
10951
10952 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10953 new_el, env->pc, pstate_read(env));
966f758c
PM
10954}
10955
ed6e6ba9
AB
10956/*
10957 * Do semihosting call and set the appropriate return value. All the
10958 * permission and validity checks have been done at translate time.
10959 *
10960 * We only see semihosting exceptions in TCG only as they are not
10961 * trapped to the hypervisor in KVM.
10962 */
91f78c58 10963#ifdef CONFIG_TCG
ed6e6ba9
AB
10964static void handle_semihosting(CPUState *cs)
10965{
904c04de
PM
10966 ARMCPU *cpu = ARM_CPU(cs);
10967 CPUARMState *env = &cpu->env;
10968
10969 if (is_a64(env)) {
ed6e6ba9
AB
10970 qemu_log_mask(CPU_LOG_INT,
10971 "...handling as semihosting call 0x%" PRIx64 "\n",
10972 env->xregs[0]);
ed3a06b1 10973 do_common_semihosting(cs);
4ff5ef9e 10974 env->pc += 4;
904c04de 10975 } else {
904c04de
PM
10976 qemu_log_mask(CPU_LOG_INT,
10977 "...handling as semihosting call 0x%x\n",
10978 env->regs[0]);
ed3a06b1 10979 do_common_semihosting(cs);
4ff5ef9e 10980 env->regs[15] += env->thumb ? 2 : 4;
904c04de
PM
10981 }
10982}
ed6e6ba9 10983#endif
904c04de 10984
9b37a28c
FR
10985/*
10986 * Handle a CPU exception for A and R profile CPUs.
966f758c
PM
10987 * Do any appropriate logging, handle PSCI calls, and then hand off
10988 * to the AArch64-entry or AArch32-entry function depending on the
10989 * target exception level's register width.
853bfef4
CF
10990 *
10991 * Note: this is used for both TCG (as the do_interrupt tcg op),
10992 * and KVM to re-inject guest debug exceptions, and to
10993 * inject a Synchronous-External-Abort.
966f758c
PM
10994 */
10995void arm_cpu_do_interrupt(CPUState *cs)
10996{
10997 ARMCPU *cpu = ARM_CPU(cs);
10998 CPUARMState *env = &cpu->env;
10999 unsigned int new_el = env->exception.target_el;
11000
531c60a9 11001 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c 11002
fc6177af 11003 arm_log_exception(cs);
966f758c
PM
11004 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
11005 new_el);
11006 if (qemu_loglevel_mask(CPU_LOG_INT)
11007 && !excp_is_internal(cs->exception_index)) {
6568da45 11008 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
64b91e3f 11009 syn_get_ec(env->exception.syndrome),
966f758c
PM
11010 env->exception.syndrome);
11011 }
11012
11013 if (arm_is_psci_call(cpu, cs->exception_index)) {
11014 arm_handle_psci_call(cpu);
11015 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
11016 return;
11017 }
11018
ed6e6ba9
AB
11019 /*
11020 * Semihosting semantics depend on the register width of the code
11021 * that caused the exception, not the target exception level, so
11022 * must be handled here.
966f758c 11023 */
ed6e6ba9
AB
11024#ifdef CONFIG_TCG
11025 if (cs->exception_index == EXCP_SEMIHOST) {
11026 handle_semihosting(cs);
904c04de
PM
11027 return;
11028 }
ed6e6ba9 11029#endif
904c04de 11030
9b37a28c
FR
11031 /*
11032 * Hooks may change global state so BQL should be held, also the
b5c53d1b
AL
11033 * BQL needs to be held for any modification of
11034 * cs->interrupt_request.
11035 */
11036 g_assert(qemu_mutex_iothread_locked());
11037
11038 arm_call_pre_el_change_hook(cpu);
11039
904c04de
PM
11040 assert(!excp_is_internal(cs->exception_index));
11041 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
11042 arm_cpu_do_interrupt_aarch64(cs);
11043 } else {
11044 arm_cpu_do_interrupt_aarch32(cs);
11045 }
f3a9b694 11046
bd7d00fc
PM
11047 arm_call_el_change_hook(cpu);
11048
f3a9b694
PM
11049 if (!kvm_enabled()) {
11050 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
11051 }
11052}
c47eaf9f 11053#endif /* !CONFIG_USER_ONLY */
0480f69a 11054
aaec1432
RH
11055uint64_t arm_sctlr(CPUARMState *env, int el)
11056{
11057 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
11058 if (el == 0) {
11059 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
d902ae75 11060 el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
aaec1432
RH
11061 }
11062 return env->cp15.sctlr_el[el];
11063}
c47eaf9f 11064
8ae08860 11065int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
b830a5ee
RH
11066{
11067 if (regime_has_2_ranges(mmu_idx)) {
11068 return extract64(tcr, 37, 2);
edc05dd4 11069 } else if (regime_is_stage2(mmu_idx)) {
b830a5ee
RH
11070 return 0; /* VTCR_EL2 */
11071 } else {
3e270f67
RH
11072 /* Replicate the single TBI bit so we always have 2 bits. */
11073 return extract32(tcr, 20, 1) * 3;
b830a5ee
RH
11074 }
11075}
11076
8ae08860 11077int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
b830a5ee
RH
11078{
11079 if (regime_has_2_ranges(mmu_idx)) {
11080 return extract64(tcr, 51, 2);
edc05dd4 11081 } else if (regime_is_stage2(mmu_idx)) {
b830a5ee
RH
11082 return 0; /* VTCR_EL2 */
11083 } else {
3e270f67
RH
11084 /* Replicate the single TBID bit so we always have 2 bits. */
11085 return extract32(tcr, 29, 1) * 3;
b830a5ee
RH
11086 }
11087}
11088
81ae05fa
RH
11089static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
11090{
11091 if (regime_has_2_ranges(mmu_idx)) {
11092 return extract64(tcr, 57, 2);
11093 } else {
11094 /* Replicate the single TCMA bit so we always have 2 bits. */
11095 return extract32(tcr, 30, 1) * 3;
11096 }
11097}
11098
104f703d
PM
11099static ARMGranuleSize tg0_to_gran_size(int tg)
11100{
11101 switch (tg) {
11102 case 0:
11103 return Gran4K;
11104 case 1:
11105 return Gran64K;
11106 case 2:
11107 return Gran16K;
11108 default:
11109 return GranInvalid;
11110 }
11111}
11112
11113static ARMGranuleSize tg1_to_gran_size(int tg)
11114{
11115 switch (tg) {
11116 case 1:
11117 return Gran16K;
11118 case 2:
11119 return Gran4K;
11120 case 3:
11121 return Gran64K;
11122 default:
11123 return GranInvalid;
11124 }
11125}
11126
11127static inline bool have4k(ARMCPU *cpu, bool stage2)
11128{
11129 return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
11130 : cpu_isar_feature(aa64_tgran4, cpu);
11131}
11132
11133static inline bool have16k(ARMCPU *cpu, bool stage2)
11134{
11135 return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
11136 : cpu_isar_feature(aa64_tgran16, cpu);
11137}
11138
11139static inline bool have64k(ARMCPU *cpu, bool stage2)
11140{
11141 return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
11142 : cpu_isar_feature(aa64_tgran64, cpu);
11143}
11144
11145static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
11146 bool stage2)
11147{
11148 switch (gran) {
11149 case Gran4K:
11150 if (have4k(cpu, stage2)) {
11151 return gran;
11152 }
11153 break;
11154 case Gran16K:
11155 if (have16k(cpu, stage2)) {
11156 return gran;
11157 }
11158 break;
11159 case Gran64K:
11160 if (have64k(cpu, stage2)) {
11161 return gran;
11162 }
11163 break;
11164 case GranInvalid:
11165 break;
11166 }
11167 /*
11168 * If the guest selects a granule size that isn't implemented,
11169 * the architecture requires that we behave as if it selected one
11170 * that is (with an IMPDEF choice of which one to pick). We choose
11171 * to implement the smallest supported granule size.
11172 */
11173 if (have4k(cpu, stage2)) {
11174 return Gran4K;
11175 }
11176 if (have16k(cpu, stage2)) {
11177 return Gran16K;
11178 }
11179 assert(have64k(cpu, stage2));
11180 return Gran64K;
11181}
11182
b830a5ee
RH
11183ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11184 ARMMMUIdx mmu_idx, bool data)
ba97be9f 11185{
c1547bba 11186 uint64_t tcr = regime_tcr(env, mmu_idx);
89739227 11187 bool epd, hpd, tsz_oob, ds, ha, hd;
ef56c242 11188 int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
104f703d 11189 ARMGranuleSize gran;
ef56c242 11190 ARMCPU *cpu = env_archcpu(env);
edc05dd4 11191 bool stage2 = regime_is_stage2(mmu_idx);
ba97be9f 11192
339370b9 11193 if (!regime_has_2_ranges(mmu_idx)) {
71d18164 11194 select = 0;
ba97be9f 11195 tsz = extract32(tcr, 0, 6);
104f703d
PM
11196 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11197 if (stage2) {
ba97be9f 11198 /* VTCR_EL2 */
b830a5ee 11199 hpd = false;
ba97be9f 11200 } else {
ba97be9f
RH
11201 hpd = extract32(tcr, 24, 1);
11202 }
11203 epd = false;
ef56c242 11204 sh = extract32(tcr, 12, 2);
f4ecc015 11205 ps = extract32(tcr, 16, 3);
89739227
RH
11206 ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11207 hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
ef56c242 11208 ds = extract64(tcr, 32, 1);
ba97be9f 11209 } else {
e4c93e44
PM
11210 bool e0pd;
11211
71d18164
RH
11212 /*
11213 * Bit 55 is always between the two regions, and is canonical for
11214 * determining if address tagging is enabled.
11215 */
11216 select = extract64(va, 55, 1);
11217 if (!select) {
11218 tsz = extract32(tcr, 0, 6);
104f703d 11219 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
71d18164 11220 epd = extract32(tcr, 7, 1);
ef56c242 11221 sh = extract32(tcr, 12, 2);
71d18164 11222 hpd = extract64(tcr, 41, 1);
e4c93e44 11223 e0pd = extract64(tcr, 55, 1);
71d18164 11224 } else {
71d18164 11225 tsz = extract32(tcr, 16, 6);
104f703d 11226 gran = tg1_to_gran_size(extract32(tcr, 30, 2));
71d18164 11227 epd = extract32(tcr, 23, 1);
ef56c242 11228 sh = extract32(tcr, 28, 2);
71d18164 11229 hpd = extract64(tcr, 42, 1);
e4c93e44 11230 e0pd = extract64(tcr, 56, 1);
71d18164 11231 }
f4ecc015 11232 ps = extract64(tcr, 32, 3);
89739227
RH
11233 ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11234 hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
ef56c242 11235 ds = extract64(tcr, 59, 1);
e4c93e44
PM
11236
11237 if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11238 regime_is_user(env, mmu_idx)) {
11239 epd = true;
11240 }
ba97be9f 11241 }
c36c65ea 11242
104f703d 11243 gran = sanitize_gran_size(cpu, gran, stage2);
104f703d 11244
ef56c242 11245 if (cpu_isar_feature(aa64_st, cpu)) {
3c003f70 11246 max_tsz = 48 - (gran == Gran64K);
c36c65ea
RDC
11247 } else {
11248 max_tsz = 39;
11249 }
0af312b6 11250
ef56c242
RH
11251 /*
11252 * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11253 * adjust the effective value of DS, as documented.
11254 */
0af312b6 11255 min_tsz = 16;
3c003f70 11256 if (gran == Gran64K) {
ef56c242
RH
11257 if (cpu_isar_feature(aa64_lva, cpu)) {
11258 min_tsz = 12;
11259 }
11260 ds = false;
11261 } else if (ds) {
edc05dd4 11262 if (regime_is_stage2(mmu_idx)) {
3c003f70 11263 if (gran == Gran16K) {
ef56c242
RH
11264 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11265 } else {
11266 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11267 }
edc05dd4 11268 } else {
3c003f70 11269 if (gran == Gran16K) {
ef56c242
RH
11270 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11271 } else {
11272 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11273 }
ef56c242
RH
11274 }
11275 if (ds) {
0af312b6
RH
11276 min_tsz = 12;
11277 }
11278 }
c36c65ea 11279
ebf93ce7
RH
11280 if (tsz > max_tsz) {
11281 tsz = max_tsz;
11282 tsz_oob = true;
11283 } else if (tsz < min_tsz) {
11284 tsz = min_tsz;
11285 tsz_oob = true;
11286 } else {
11287 tsz_oob = false;
11288 }
ba97be9f 11289
b830a5ee
RH
11290 /* Present TBI as a composite with TBID. */
11291 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11292 if (!data) {
11293 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11294 }
11295 tbi = (tbi >> select) & 1;
11296
ba97be9f
RH
11297 return (ARMVAParameters) {
11298 .tsz = tsz,
f4ecc015 11299 .ps = ps,
ef56c242 11300 .sh = sh,
ba97be9f
RH
11301 .select = select,
11302 .tbi = tbi,
11303 .epd = epd,
11304 .hpd = hpd,
ebf93ce7 11305 .tsz_oob = tsz_oob,
ef56c242 11306 .ds = ds,
89739227
RH
11307 .ha = ha,
11308 .hd = ha && hd,
3c003f70 11309 .gran = gran,
ba97be9f
RH
11310 };
11311}
11312
9b37a28c
FR
11313/*
11314 * Note that signed overflow is undefined in C. The following routines are
11315 * careful to use unsigned types where modulo arithmetic is required.
11316 * Failure to do so _will_ break on newer gcc.
11317 */
6ddbc6e4
PB
11318
11319/* Signed saturating arithmetic. */
11320
1654b2d6 11321/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
11322static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11323{
11324 uint16_t res;
11325
11326 res = a + b;
11327 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
f927dbda 11328 if (a & 0x8000) {
6ddbc6e4 11329 res = 0x8000;
f927dbda 11330 } else {
6ddbc6e4 11331 res = 0x7fff;
f927dbda 11332 }
6ddbc6e4
PB
11333 }
11334 return res;
11335}
11336
1654b2d6 11337/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
11338static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11339{
11340 uint8_t res;
11341
11342 res = a + b;
11343 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
f927dbda 11344 if (a & 0x80) {
6ddbc6e4 11345 res = 0x80;
f927dbda 11346 } else {
6ddbc6e4 11347 res = 0x7f;
f927dbda 11348 }
6ddbc6e4
PB
11349 }
11350 return res;
11351}
11352
1654b2d6 11353/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
11354static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11355{
11356 uint16_t res;
11357
11358 res = a - b;
11359 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
f927dbda 11360 if (a & 0x8000) {
6ddbc6e4 11361 res = 0x8000;
f927dbda 11362 } else {
6ddbc6e4 11363 res = 0x7fff;
f927dbda 11364 }
6ddbc6e4
PB
11365 }
11366 return res;
11367}
11368
1654b2d6 11369/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
11370static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11371{
11372 uint8_t res;
11373
11374 res = a - b;
11375 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
f927dbda 11376 if (a & 0x80) {
6ddbc6e4 11377 res = 0x80;
f927dbda 11378 } else {
6ddbc6e4 11379 res = 0x7f;
f927dbda 11380 }
6ddbc6e4
PB
11381 }
11382 return res;
11383}
11384
11385#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11386#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11387#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
11388#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
11389#define PFX q
11390
11391#include "op_addsub.h"
11392
11393/* Unsigned saturating arithmetic. */
460a09c1 11394static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
11395{
11396 uint16_t res;
11397 res = a + b;
f927dbda 11398 if (res < a) {
6ddbc6e4 11399 res = 0xffff;
f927dbda 11400 }
6ddbc6e4
PB
11401 return res;
11402}
11403
460a09c1 11404static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 11405{
f927dbda 11406 if (a > b) {
6ddbc6e4 11407 return a - b;
f927dbda 11408 } else {
6ddbc6e4 11409 return 0;
f927dbda 11410 }
6ddbc6e4
PB
11411}
11412
11413static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11414{
11415 uint8_t res;
11416 res = a + b;
f927dbda 11417 if (res < a) {
6ddbc6e4 11418 res = 0xff;
f927dbda 11419 }
6ddbc6e4
PB
11420 return res;
11421}
11422
11423static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11424{
f927dbda 11425 if (a > b) {
6ddbc6e4 11426 return a - b;
f927dbda 11427 } else {
6ddbc6e4 11428 return 0;
f927dbda 11429 }
6ddbc6e4
PB
11430}
11431
11432#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11433#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11434#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
11435#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
11436#define PFX uq
11437
11438#include "op_addsub.h"
11439
11440/* Signed modulo arithmetic. */
11441#define SARITH16(a, b, n, op) do { \
11442 int32_t sum; \
db6e2e65 11443 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
11444 RESULT(sum, n, 16); \
11445 if (sum >= 0) \
11446 ge |= 3 << (n * 2); \
04215eb1 11447 } while (0)
6ddbc6e4
PB
11448
11449#define SARITH8(a, b, n, op) do { \
11450 int32_t sum; \
db6e2e65 11451 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
11452 RESULT(sum, n, 8); \
11453 if (sum >= 0) \
11454 ge |= 1 << n; \
04215eb1 11455 } while (0)
6ddbc6e4
PB
11456
11457
11458#define ADD16(a, b, n) SARITH16(a, b, n, +)
11459#define SUB16(a, b, n) SARITH16(a, b, n, -)
11460#define ADD8(a, b, n) SARITH8(a, b, n, +)
11461#define SUB8(a, b, n) SARITH8(a, b, n, -)
11462#define PFX s
11463#define ARITH_GE
11464
11465#include "op_addsub.h"
11466
11467/* Unsigned modulo arithmetic. */
11468#define ADD16(a, b, n) do { \
11469 uint32_t sum; \
11470 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11471 RESULT(sum, n, 16); \
a87aa10b 11472 if ((sum >> 16) == 1) \
6ddbc6e4 11473 ge |= 3 << (n * 2); \
04215eb1 11474 } while (0)
6ddbc6e4
PB
11475
11476#define ADD8(a, b, n) do { \
11477 uint32_t sum; \
11478 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11479 RESULT(sum, n, 8); \
a87aa10b
AZ
11480 if ((sum >> 8) == 1) \
11481 ge |= 1 << n; \
04215eb1 11482 } while (0)
6ddbc6e4
PB
11483
11484#define SUB16(a, b, n) do { \
11485 uint32_t sum; \
11486 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11487 RESULT(sum, n, 16); \
11488 if ((sum >> 16) == 0) \
11489 ge |= 3 << (n * 2); \
04215eb1 11490 } while (0)
6ddbc6e4
PB
11491
11492#define SUB8(a, b, n) do { \
11493 uint32_t sum; \
11494 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11495 RESULT(sum, n, 8); \
11496 if ((sum >> 8) == 0) \
a87aa10b 11497 ge |= 1 << n; \
04215eb1 11498 } while (0)
6ddbc6e4
PB
11499
11500#define PFX u
11501#define ARITH_GE
11502
11503#include "op_addsub.h"
11504
11505/* Halved signed arithmetic. */
11506#define ADD16(a, b, n) \
11507 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11508#define SUB16(a, b, n) \
11509 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11510#define ADD8(a, b, n) \
11511 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11512#define SUB8(a, b, n) \
11513 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11514#define PFX sh
11515
11516#include "op_addsub.h"
11517
11518/* Halved unsigned arithmetic. */
11519#define ADD16(a, b, n) \
11520 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11521#define SUB16(a, b, n) \
11522 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11523#define ADD8(a, b, n) \
11524 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11525#define SUB8(a, b, n) \
11526 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11527#define PFX uh
11528
11529#include "op_addsub.h"
11530
11531static inline uint8_t do_usad(uint8_t a, uint8_t b)
11532{
f927dbda 11533 if (a > b) {
6ddbc6e4 11534 return a - b;
f927dbda 11535 } else {
6ddbc6e4 11536 return b - a;
f927dbda 11537 }
6ddbc6e4
PB
11538}
11539
11540/* Unsigned sum of absolute byte differences. */
11541uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11542{
11543 uint32_t sum;
11544 sum = do_usad(a, b);
11545 sum += do_usad(a >> 8, b >> 8);
bdc3b6f5 11546 sum += do_usad(a >> 16, b >> 16);
6ddbc6e4
PB
11547 sum += do_usad(a >> 24, b >> 24);
11548 return sum;
11549}
11550
11551/* For ARMv6 SEL instruction. */
11552uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11553{
11554 uint32_t mask;
11555
11556 mask = 0;
f927dbda 11557 if (flags & 1) {
6ddbc6e4 11558 mask |= 0xff;
f927dbda
FR
11559 }
11560 if (flags & 2) {
6ddbc6e4 11561 mask |= 0xff00;
f927dbda
FR
11562 }
11563 if (flags & 4) {
6ddbc6e4 11564 mask |= 0xff0000;
f927dbda
FR
11565 }
11566 if (flags & 8) {
6ddbc6e4 11567 mask |= 0xff000000;
f927dbda 11568 }
6ddbc6e4
PB
11569 return (a & mask) | (b & ~mask);
11570}
11571
9b37a28c
FR
11572/*
11573 * CRC helpers.
aa633469
PM
11574 * The upper bytes of val (above the number specified by 'bytes') must have
11575 * been zeroed out by the caller.
11576 */
eb0ecd5a
WN
11577uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11578{
11579 uint8_t buf[4];
11580
aa633469 11581 stl_le_p(buf, val);
eb0ecd5a
WN
11582
11583 /* zlib crc32 converts the accumulator and output to one's complement. */
11584 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11585}
11586
11587uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11588{
11589 uint8_t buf[4];
11590
aa633469 11591 stl_le_p(buf, val);
eb0ecd5a
WN
11592
11593 /* Linux crc32c converts the output to one's complement. */
11594 return crc32c(acc, buf, bytes) ^ 0xffffffff;
11595}
a9e01311 11596
9b37a28c
FR
11597/*
11598 * Return the exception level to which FP-disabled exceptions should
a9e01311
RH
11599 * be taken, or 0 if FP is enabled.
11600 */
ced31551 11601int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 11602{
55faa212 11603#ifndef CONFIG_USER_ONLY
d5a6fa2d
RH
11604 uint64_t hcr_el2;
11605
9b37a28c
FR
11606 /*
11607 * CPACR and the CPTR registers don't exist before v6, so FP is
a9e01311
RH
11608 * always accessible
11609 */
11610 if (!arm_feature(env, ARM_FEATURE_V6)) {
11611 return 0;
11612 }
11613
d87513c0
PM
11614 if (arm_feature(env, ARM_FEATURE_M)) {
11615 /* CPACR can cause a NOCP UsageFault taken to current security state */
11616 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11617 return 1;
11618 }
11619
11620 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11621 if (!extract32(env->v7m.nsacr, 10, 1)) {
11622 /* FP insns cause a NOCP UsageFault taken to Secure */
11623 return 3;
11624 }
11625 }
11626
11627 return 0;
11628 }
11629
d5a6fa2d
RH
11630 hcr_el2 = arm_hcr_el2_eff(env);
11631
9b37a28c
FR
11632 /*
11633 * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
a9e01311
RH
11634 * 0, 2 : trap EL0 and EL1/PL1 accesses
11635 * 1 : trap only EL0 accesses
11636 * 3 : trap no accesses
c2ddb7cf 11637 * This register is ignored if E2H+TGE are both set.
a9e01311 11638 */
d5a6fa2d 11639 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
fab8ad39 11640 int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
c2ddb7cf
RH
11641
11642 switch (fpen) {
02e1de14
RH
11643 case 1:
11644 if (cur_el != 0) {
11645 break;
11646 }
11647 /* fall through */
c2ddb7cf
RH
11648 case 0:
11649 case 2:
02e1de14
RH
11650 /* Trap from Secure PL0 or PL1 to Secure PL1. */
11651 if (!arm_el_is_aa64(env, 3)
11652 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
a9e01311
RH
11653 return 3;
11654 }
02e1de14 11655 if (cur_el <= 1) {
c2ddb7cf
RH
11656 return 1;
11657 }
11658 break;
a9e01311 11659 }
a9e01311
RH
11660 }
11661
fc1120a7
PM
11662 /*
11663 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11664 * to control non-secure access to the FPU. It doesn't have any
11665 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11666 */
11667 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11668 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11669 if (!extract32(env->cp15.nsacr, 10, 1)) {
11670 /* FP insns act as UNDEF */
11671 return cur_el == 2 ? 2 : 1;
11672 }
11673 }
11674
d5a6fa2d
RH
11675 /*
11676 * CPTR_EL2 is present in v7VE or v8, and changes format
11677 * with HCR_EL2.E2H (regardless of TGE).
a9e01311 11678 */
d5a6fa2d
RH
11679 if (cur_el <= 2) {
11680 if (hcr_el2 & HCR_E2H) {
fab8ad39 11681 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
d5a6fa2d
RH
11682 case 1:
11683 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11684 break;
11685 }
11686 /* fall through */
11687 case 0:
11688 case 2:
11689 return 2;
11690 }
11691 } else if (arm_is_el2_enabled(env)) {
fab8ad39 11692 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
d5a6fa2d
RH
11693 return 2;
11694 }
11695 }
a9e01311
RH
11696 }
11697
11698 /* CPTR_EL3 : present in v8 */
fab8ad39 11699 if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
a9e01311
RH
11700 /* Trap all FP ops to EL3 */
11701 return 3;
11702 }
55faa212 11703#endif
a9e01311
RH
11704 return 0;
11705}
11706
b9f6033c
RH
11707/* Return the exception level we're running at if this is our mmu_idx */
11708int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11709{
11710 if (mmu_idx & ARM_MMU_IDX_M) {
11711 return mmu_idx & ARM_MMU_IDX_M_PRIV;
11712 }
11713
11714 switch (mmu_idx) {
11715 case ARMMMUIdx_E10_0:
11716 case ARMMMUIdx_E20_0:
b9f6033c
RH
11717 return 0;
11718 case ARMMMUIdx_E10_1:
452ef8cb 11719 case ARMMMUIdx_E10_1_PAN:
b9f6033c
RH
11720 return 1;
11721 case ARMMMUIdx_E2:
11722 case ARMMMUIdx_E20_2:
452ef8cb 11723 case ARMMMUIdx_E20_2_PAN:
b9f6033c 11724 return 2;
d902ae75 11725 case ARMMMUIdx_E3:
b9f6033c
RH
11726 return 3;
11727 default:
11728 g_assert_not_reached();
11729 }
11730}
11731
7aab5a8c 11732#ifndef CONFIG_TCG
65e4655c
RH
11733ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11734{
7aab5a8c 11735 g_assert_not_reached();
65e4655c 11736}
7aab5a8c 11737#endif
65e4655c 11738
6f2d9d74
TK
11739static bool arm_pan_enabled(CPUARMState *env)
11740{
11741 if (is_a64(env)) {
11742 return env->pstate & PSTATE_PAN;
11743 } else {
11744 return env->uncached_cpsr & CPSR_PAN;
11745 }
11746}
11747
164690b2 11748ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 11749{
b6ad6062
RDC
11750 ARMMMUIdx idx;
11751 uint64_t hcr;
11752
65e4655c 11753 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 11754 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
11755 }
11756
6003d980 11757 /* See ARM pseudo-function ELIsInHost. */
b9f6033c
RH
11758 switch (el) {
11759 case 0:
b6ad6062
RDC
11760 hcr = arm_hcr_el2_eff(env);
11761 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11762 idx = ARMMMUIdx_E20_0;
11763 } else {
11764 idx = ARMMMUIdx_E10_0;
6003d980 11765 }
b6ad6062 11766 break;
b9f6033c 11767 case 1:
6f2d9d74 11768 if (arm_pan_enabled(env)) {
b6ad6062
RDC
11769 idx = ARMMMUIdx_E10_1_PAN;
11770 } else {
11771 idx = ARMMMUIdx_E10_1;
66412260 11772 }
b6ad6062 11773 break;
b9f6033c 11774 case 2:
6003d980 11775 /* Note that TGE does not apply at EL2. */
b6ad6062 11776 if (arm_hcr_el2_eff(env) & HCR_E2H) {
6f2d9d74 11777 if (arm_pan_enabled(env)) {
b6ad6062
RDC
11778 idx = ARMMMUIdx_E20_2_PAN;
11779 } else {
11780 idx = ARMMMUIdx_E20_2;
66412260 11781 }
b6ad6062
RDC
11782 } else {
11783 idx = ARMMMUIdx_E2;
6003d980 11784 }
b6ad6062 11785 break;
b9f6033c 11786 case 3:
d902ae75 11787 return ARMMMUIdx_E3;
b9f6033c
RH
11788 default:
11789 g_assert_not_reached();
65e4655c 11790 }
b6ad6062 11791
b6ad6062 11792 return idx;
50494a27
RH
11793}
11794
164690b2
RH
11795ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11796{
11797 return arm_mmu_idx_el(env, arm_current_el(env));
11798}
11799
3902bfc6
RH
11800static CPUARMTBFlags rebuild_hflags_common(CPUARMState *env, int fp_el,
11801 ARMMMUIdx mmu_idx,
11802 CPUARMTBFlags flags)
fdd1b228 11803{
a729a46b
RH
11804 DP_TBFLAG_ANY(flags, FPEXC_EL, fp_el);
11805 DP_TBFLAG_ANY(flags, MMUIDX, arm_to_core_mmu_idx(mmu_idx));
fdd1b228 11806
fdd1b228 11807 if (arm_singlestep_active(env)) {
a729a46b 11808 DP_TBFLAG_ANY(flags, SS_ACTIVE, 1);
fdd1b228 11809 }
361c33f6 11810
fdd1b228
RH
11811 return flags;
11812}
11813
3902bfc6
RH
11814static CPUARMTBFlags rebuild_hflags_common_32(CPUARMState *env, int fp_el,
11815 ARMMMUIdx mmu_idx,
11816 CPUARMTBFlags flags)
43eccfb6 11817{
8061a649
RH
11818 bool sctlr_b = arm_sctlr_b(env);
11819
11820 if (sctlr_b) {
a729a46b 11821 DP_TBFLAG_A32(flags, SCTLR__B, 1);
8061a649
RH
11822 }
11823 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) {
a729a46b 11824 DP_TBFLAG_ANY(flags, BE_DATA, 1);
8061a649 11825 }
a729a46b 11826 DP_TBFLAG_A32(flags, NS, !access_secure_reg(env));
43eccfb6
RH
11827
11828 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
11829}
11830
3902bfc6
RH
11831static CPUARMTBFlags rebuild_hflags_m32(CPUARMState *env, int fp_el,
11832 ARMMMUIdx mmu_idx)
6e33ced5 11833{
3902bfc6 11834 CPUARMTBFlags flags = {};
4479ec30
RH
11835 uint32_t ccr = env->v7m.ccr[env->v7m.secure];
11836
11837 /* Without HaveMainExt, CCR.UNALIGN_TRP is RES1. */
11838 if (ccr & R_V7M_CCR_UNALIGN_TRP_MASK) {
11839 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11840 }
6e33ced5
RH
11841
11842 if (arm_v7m_is_handler_mode(env)) {
a729a46b 11843 DP_TBFLAG_M32(flags, HANDLER, 1);
6e33ced5
RH
11844 }
11845
11846 /*
11847 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN
11848 * is suppressing them because the requested execution priority
11849 * is less than 0.
11850 */
11851 if (arm_feature(env, ARM_FEATURE_V8) &&
11852 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) &&
4479ec30 11853 (ccr & R_V7M_CCR_STKOFHFNMIGN_MASK))) {
a729a46b 11854 DP_TBFLAG_M32(flags, STACKCHECK, 1);
6e33ced5
RH
11855 }
11856
a393dee0
RH
11857 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && env->v7m.secure) {
11858 DP_TBFLAG_M32(flags, SECURE, 1);
11859 }
11860
6e33ced5
RH
11861 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
11862}
11863
3902bfc6
RH
11864static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,
11865 ARMMMUIdx mmu_idx)
c747224c 11866{
8480e933 11867 CPUARMTBFlags flags = {};
4479ec30
RH
11868 int el = arm_current_el(env);
11869
11870 if (arm_sctlr(env, el) & SCTLR_A) {
11871 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11872 }
0a54d68e
RH
11873
11874 if (arm_el_is_aa64(env, 1)) {
a729a46b 11875 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 11876 }
5bb0a20b 11877
034bb45a 11878 if (el < 2 && env->cp15.hstr_el2 && arm_is_el2_enabled(env) &&
5bb0a20b 11879 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
a729a46b 11880 DP_TBFLAG_A32(flags, HSTR_ACTIVE, 1);
5bb0a20b
MZ
11881 }
11882
361c33f6
PM
11883 if (arm_fgt_active(env, el)) {
11884 DP_TBFLAG_ANY(flags, FGT_ACTIVE, 1);
11885 }
11886
520d1621
PM
11887 if (env->uncached_cpsr & CPSR_IL) {
11888 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
11889 }
11890
75fe8356
RH
11891 /*
11892 * The SME exception we are testing for is raised via
11893 * AArch64.CheckFPAdvSIMDEnabled(), as called from
11894 * AArch32.CheckAdvSIMDOrFPEnabled().
11895 */
11896 if (el == 0
11897 && FIELD_EX64(env->svcr, SVCR, SM)
11898 && (!arm_is_el2_enabled(env)
11899 || (arm_el_is_aa64(env, 2) && !(env->cp15.hcr_el2 & HCR_TGE)))
11900 && arm_el_is_aa64(env, 1)
11901 && !sme_fa64(env, el)) {
11902 DP_TBFLAG_A32(flags, SME_TRAP_NONSTREAMING, 1);
11903 }
11904
83f4baef 11905 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags);
c747224c
RH
11906}
11907
3902bfc6
RH
11908static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el,
11909 ARMMMUIdx mmu_idx)
a9e01311 11910{
8480e933 11911 CPUARMTBFlags flags = {};
d4d7503a 11912 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx);
c1547bba 11913 uint64_t tcr = regime_tcr(env, mmu_idx);
d4d7503a
RH
11914 uint64_t sctlr;
11915 int tbii, tbid;
b9adaa70 11916
a729a46b 11917 DP_TBFLAG_ANY(flags, AARCH64_STATE, 1);
cd208a1c 11918
339370b9 11919 /* Get control bits for tagged addresses. */
b830a5ee
RH
11920 tbid = aa64_va_parameter_tbi(tcr, mmu_idx);
11921 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx);
5d8634f5 11922
a729a46b
RH
11923 DP_TBFLAG_A64(flags, TBII, tbii);
11924 DP_TBFLAG_A64(flags, TBID, tbid);
d4d7503a
RH
11925
11926 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) {
11927 int sve_el = sve_exception_el(env, el);
5d8634f5 11928
d4d7503a 11929 /*
397d922c
RH
11930 * If either FP or SVE are disabled, translator does not need len.
11931 * If SVE EL > FP EL, FP exception has precedence, and translator
11932 * does not need SVE EL. Save potential re-translations by forcing
11933 * the unneeded data to zero.
d4d7503a 11934 */
397d922c
RH
11935 if (fp_el != 0) {
11936 if (sve_el > fp_el) {
11937 sve_el = 0;
11938 }
11939 } else if (sve_el == 0) {
5ef3cc56 11940 DP_TBFLAG_A64(flags, VL, sve_vqm1_for_el(env, el));
5d8634f5 11941 }
a729a46b 11942 DP_TBFLAG_A64(flags, SVEEXC_EL, sve_el);
d4d7503a 11943 }
6b2ca83e 11944 if (cpu_isar_feature(aa64_sme, env_archcpu(env))) {
5d7953ad 11945 int sme_el = sme_exception_el(env, el);
62151133 11946 bool sm = FIELD_EX64(env->svcr, SVCR, SM);
5d7953ad
RH
11947
11948 DP_TBFLAG_A64(flags, SMEEXC_EL, sme_el);
11949 if (sme_el == 0) {
11950 /* Similarly, do not compute SVL if SME is disabled. */
62151133
RH
11951 int svl = sve_vqm1_for_el_sm(env, el, true);
11952 DP_TBFLAG_A64(flags, SVL, svl);
11953 if (sm) {
11954 /* If SVE is disabled, we will not have set VL above. */
11955 DP_TBFLAG_A64(flags, VL, svl);
11956 }
5d7953ad 11957 }
62151133 11958 if (sm) {
a3637e88 11959 DP_TBFLAG_A64(flags, PSTATE_SM, 1);
75fe8356 11960 DP_TBFLAG_A64(flags, SME_TRAP_NONSTREAMING, !sme_fa64(env, el));
a3637e88
RH
11961 }
11962 DP_TBFLAG_A64(flags, PSTATE_ZA, FIELD_EX64(env->svcr, SVCR, ZA));
6b2ca83e 11963 }
1db5e96c 11964
aaec1432 11965 sctlr = regime_sctlr(env, stage1);
1db5e96c 11966
4479ec30
RH
11967 if (sctlr & SCTLR_A) {
11968 DP_TBFLAG_ANY(flags, ALIGN_MEM, 1);
11969 }
11970
8061a649 11971 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) {
a729a46b 11972 DP_TBFLAG_ANY(flags, BE_DATA, 1);
8061a649
RH
11973 }
11974
d4d7503a
RH
11975 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) {
11976 /*
11977 * In order to save space in flags, we record only whether
11978 * pauth is "inactive", meaning all insns are implemented as
11979 * a nop, or "active" when some action must be performed.
11980 * The decision of which action to take is left to a helper.
11981 */
11982 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) {
a729a46b 11983 DP_TBFLAG_A64(flags, PAUTH_ACTIVE, 1);
1db5e96c 11984 }
d4d7503a 11985 }
0816ef1b 11986
d4d7503a
RH
11987 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
11988 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */
11989 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) {
a729a46b 11990 DP_TBFLAG_A64(flags, BT, 1);
0816ef1b 11991 }
d4d7503a 11992 }
08f1434a 11993
cc28fc30 11994 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */
7a8014ab
RH
11995 if (!(env->pstate & PSTATE_UAO)) {
11996 switch (mmu_idx) {
11997 case ARMMMUIdx_E10_1:
11998 case ARMMMUIdx_E10_1_PAN:
7a8014ab 11999 /* TODO: ARMv8.3-NV */
a729a46b 12000 DP_TBFLAG_A64(flags, UNPRIV, 1);
7a8014ab
RH
12001 break;
12002 case ARMMMUIdx_E20_2:
12003 case ARMMMUIdx_E20_2_PAN:
7a8014ab
RH
12004 /*
12005 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is
12006 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR.
12007 */
12008 if (env->cp15.hcr_el2 & HCR_TGE) {
a729a46b 12009 DP_TBFLAG_A64(flags, UNPRIV, 1);
7a8014ab
RH
12010 }
12011 break;
12012 default:
12013 break;
cc28fc30 12014 }
cc28fc30
RH
12015 }
12016
520d1621
PM
12017 if (env->pstate & PSTATE_IL) {
12018 DP_TBFLAG_ANY(flags, PSTATE__IL, 1);
12019 }
12020
361c33f6
PM
12021 if (arm_fgt_active(env, el)) {
12022 DP_TBFLAG_ANY(flags, FGT_ACTIVE, 1);
12023 }
12024
81ae05fa
RH
12025 if (cpu_isar_feature(aa64_mte, env_archcpu(env))) {
12026 /*
12027 * Set MTE_ACTIVE if any access may be Checked, and leave clear
12028 * if all accesses must be Unchecked:
12029 * 1) If no TBI, then there are no tags in the address to check,
12030 * 2) If Tag Check Override, then all accesses are Unchecked,
12031 * 3) If Tag Check Fail == 0, then Checked access have no effect,
12032 * 4) If no Allocation Tag Access, then all accesses are Unchecked.
12033 */
12034 if (allocation_tag_access_enabled(env, el, sctlr)) {
a729a46b 12035 DP_TBFLAG_A64(flags, ATA, 1);
81ae05fa
RH
12036 if (tbid
12037 && !(env->pstate & PSTATE_TCO)
12038 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) {
a729a46b 12039 DP_TBFLAG_A64(flags, MTE_ACTIVE, 1);
81ae05fa
RH
12040 }
12041 }
12042 /* And again for unprivileged accesses, if required. */
a729a46b 12043 if (EX_TBFLAG_A64(flags, UNPRIV)
81ae05fa
RH
12044 && tbid
12045 && !(env->pstate & PSTATE_TCO)
2d928adf 12046 && (sctlr & SCTLR_TCF0)
81ae05fa 12047 && allocation_tag_access_enabled(env, 0, sctlr)) {
a729a46b 12048 DP_TBFLAG_A64(flags, MTE0_ACTIVE, 1);
81ae05fa
RH
12049 }
12050 /* Cache TCMA as well as TBI. */
a729a46b 12051 DP_TBFLAG_A64(flags, TCMA, aa64_va_parameter_tcma(tcr, mmu_idx));
81ae05fa
RH
12052 }
12053
d4d7503a
RH
12054 return rebuild_hflags_common(env, fp_el, mmu_idx, flags);
12055}
12056
3902bfc6 12057static CPUARMTBFlags rebuild_hflags_internal(CPUARMState *env)
3d74e2e9
RH
12058{
12059 int el = arm_current_el(env);
12060 int fp_el = fp_exception_el(env, el);
164690b2 12061 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3d74e2e9
RH
12062
12063 if (is_a64(env)) {
12064 return rebuild_hflags_a64(env, el, fp_el, mmu_idx);
12065 } else if (arm_feature(env, ARM_FEATURE_M)) {
12066 return rebuild_hflags_m32(env, fp_el, mmu_idx);
12067 } else {
12068 return rebuild_hflags_a32(env, fp_el, mmu_idx);
12069 }
12070}
12071
12072void arm_rebuild_hflags(CPUARMState *env)
12073{
12074 env->hflags = rebuild_hflags_internal(env);
12075}
12076
19717e9b
PM
12077/*
12078 * If we have triggered a EL state change we can't rely on the
12079 * translator having passed it to us, we need to recompute.
12080 */
12081void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env)
12082{
12083 int el = arm_current_el(env);
12084 int fp_el = fp_exception_el(env, el);
12085 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
3902bfc6 12086
19717e9b
PM
12087 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
12088}
12089
14f3c588
RH
12090void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el)
12091{
12092 int fp_el = fp_exception_el(env, el);
12093 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12094
12095 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx);
12096}
12097
f80741d1
AB
12098/*
12099 * If we have triggered a EL state change we can't rely on the
563152e0 12100 * translator having passed it to us, we need to recompute.
f80741d1
AB
12101 */
12102void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env)
12103{
12104 int el = arm_current_el(env);
12105 int fp_el = fp_exception_el(env, el);
12106 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12107 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
12108}
12109
14f3c588
RH
12110void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el)
12111{
12112 int fp_el = fp_exception_el(env, el);
12113 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12114
12115 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx);
12116}
12117
12118void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el)
12119{
12120 int fp_el = fp_exception_el(env, el);
12121 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el);
12122
12123 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx);
12124}
12125
0ee8b24a
PMD
12126static inline void assert_hflags_rebuild_correctly(CPUARMState *env)
12127{
12128#ifdef CONFIG_DEBUG_TCG
3902bfc6
RH
12129 CPUARMTBFlags c = env->hflags;
12130 CPUARMTBFlags r = rebuild_hflags_internal(env);
0ee8b24a 12131
a378206a
RH
12132 if (unlikely(c.flags != r.flags || c.flags2 != r.flags2)) {
12133 fprintf(stderr, "TCG hflags mismatch "
12134 "(current:(0x%08x,0x" TARGET_FMT_lx ")"
12135 " rebuilt:(0x%08x,0x" TARGET_FMT_lx ")\n",
12136 c.flags, c.flags2, r.flags, r.flags2);
0ee8b24a
PMD
12137 abort();
12138 }
12139#endif
12140}
12141
26702213
PM
12142static bool mve_no_pred(CPUARMState *env)
12143{
12144 /*
12145 * Return true if there is definitely no predication of MVE
12146 * instructions by VPR or LTPSIZE. (Returning false even if there
12147 * isn't any predication is OK; generated code will just be
12148 * a little worse.)
12149 * If the CPU does not implement MVE then this TB flag is always 0.
12150 *
12151 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
12152 * logic in gen_update_fp_context() needs to be updated to match.
12153 *
12154 * We do not include the effect of the ECI bits here -- they are
12155 * tracked in other TB flags. This simplifies the logic for
12156 * "when did we emit code that changes the MVE_NO_PRED TB flag
12157 * and thus need to end the TB?".
12158 */
12159 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
12160 return false;
12161 }
12162 if (env->v7m.vpr) {
12163 return false;
12164 }
12165 if (env->v7m.ltpsize < 4) {
12166 return false;
12167 }
12168 return true;
12169}
12170
d4d7503a
RH
12171void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
12172 target_ulong *cs_base, uint32_t *pflags)
12173{
3902bfc6 12174 CPUARMTBFlags flags;
d4d7503a 12175
0ee8b24a 12176 assert_hflags_rebuild_correctly(env);
3902bfc6 12177 flags = env->hflags;
3d74e2e9 12178
a729a46b 12179 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
d4d7503a 12180 *pc = env->pc;
d4d7503a 12181 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
a729a46b 12182 DP_TBFLAG_A64(flags, BTYPE, env->btype);
08f1434a 12183 }
a9e01311
RH
12184 } else {
12185 *pc = env->regs[15];
6e33ced5
RH
12186
12187 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
12188 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12189 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
12190 != env->v7m.secure) {
a729a46b 12191 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
9550d1bd
RH
12192 }
12193
12194 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
12195 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
12196 (env->v7m.secure &&
12197 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
12198 /*
12199 * ASPEN is set, but FPCA/SFPA indicate that there is no
12200 * active FP context; we must create a new FP context before
12201 * executing any FP insn.
12202 */
a729a46b 12203 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
9550d1bd
RH
12204 }
12205
12206 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
12207 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
a729a46b 12208 DP_TBFLAG_M32(flags, LSPACT, 1);
9550d1bd 12209 }
26702213
PM
12210
12211 if (mve_no_pred(env)) {
12212 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
12213 }
6e33ced5 12214 } else {
bbad7c62
RH
12215 /*
12216 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
12217 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
12218 */
12219 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
a729a46b 12220 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
bbad7c62 12221 } else {
a729a46b
RH
12222 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
12223 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
bbad7c62 12224 }
0a54d68e 12225 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
a729a46b 12226 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 12227 }
6e33ced5
RH
12228 }
12229
a729a46b
RH
12230 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
12231 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
d4d7503a 12232 }
a9e01311 12233
60e12c37
RH
12234 /*
12235 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
12236 * states defined in the ARM ARM for software singlestep:
12237 * SS_ACTIVE PSTATE.SS State
12238 * 0 x Inactive (the TB flag for SS is always 0)
12239 * 1 0 Active-pending
12240 * 1 1 Active-not-pending
ae6eb1e9 12241 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
a9e01311 12242 */
a729a46b
RH
12243 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
12244 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
a9e01311 12245 }
a9e01311 12246
3902bfc6 12247 *pflags = flags.flags;
a378206a 12248 *cs_base = flags.flags2;
a9e01311 12249}
0ab5953b
RH
12250
12251#ifdef TARGET_AARCH64
12252/*
12253 * The manual says that when SVE is enabled and VQ is widened the
12254 * implementation is allowed to zero the previously inaccessible
12255 * portion of the registers. The corollary to that is that when
12256 * SVE is enabled and VQ is narrowed we are also allowed to zero
12257 * the now inaccessible portion of the registers.
12258 *
12259 * The intent of this is that no predicate bit beyond VQ is ever set.
12260 * Which means that some operations on predicate registers themselves
12261 * may operate on full uint64_t or even unrolled across the maximum
12262 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
12263 * may well be cheaper than conditionals to restrict the operation
12264 * to the relevant portion of a uint16_t[16].
12265 */
12266void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
12267{
12268 int i, j;
12269 uint64_t pmask;
12270
12271 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 12272 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
12273
12274 /* Zap the high bits of the zregs. */
12275 for (i = 0; i < 32; i++) {
12276 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
12277 }
12278
12279 /* Zap the high bits of the pregs and ffr. */
12280 pmask = 0;
12281 if (vq & 3) {
12282 pmask = ~(-1ULL << (16 * (vq & 3)));
12283 }
12284 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
12285 for (i = 0; i < 17; ++i) {
12286 env->vfp.pregs[i].p[j] &= pmask;
12287 }
12288 pmask = 0;
12289 }
12290}
12291
6a775fd6
RH
12292static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
12293{
12294 int exc_el;
12295
12296 if (sm) {
12297 exc_el = sme_exception_el(env, el);
12298 } else {
12299 exc_el = sve_exception_el(env, el);
12300 }
12301 if (exc_el) {
12302 return 0; /* disabled */
12303 }
12304 return sve_vqm1_for_el_sm(env, el, sm);
12305}
12306
0ab5953b
RH
12307/*
12308 * Notice a change in SVE vector size when changing EL.
12309 */
9a05f7b6
RH
12310void aarch64_sve_change_el(CPUARMState *env, int old_el,
12311 int new_el, bool el0_a64)
0ab5953b 12312{
2fc0cc0e 12313 ARMCPU *cpu = env_archcpu(env);
0ab5953b 12314 int old_len, new_len;
6a775fd6 12315 bool old_a64, new_a64, sm;
0ab5953b
RH
12316
12317 /* Nothing to do if no SVE. */
cd208a1c 12318 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
12319 return;
12320 }
12321
12322 /* Nothing to do if FP is disabled in either EL. */
12323 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12324 return;
12325 }
12326
04fbce76
RH
12327 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12328 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12329
12330 /*
12331 * Both AArch64.TakeException and AArch64.ExceptionReturn
12332 * invoke ResetSVEState when taking an exception from, or
12333 * returning to, AArch32 state when PSTATE.SM is enabled.
12334 */
6a775fd6
RH
12335 sm = FIELD_EX64(env->svcr, SVCR, SM);
12336 if (old_a64 != new_a64 && sm) {
04fbce76
RH
12337 arm_reset_sve_state(env);
12338 return;
12339 }
12340
0ab5953b
RH
12341 /*
12342 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12343 * at ELx, or not available because the EL is in AArch32 state, then
12344 * for all purposes other than a direct read, the ZCR_ELx.LEN field
12345 * has an effective value of 0".
12346 *
12347 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12348 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12349 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
12350 * we already have the correct register contents when encountering the
12351 * vq0->vq0 transition between EL0->EL1.
12352 */
6a775fd6
RH
12353 old_len = new_len = 0;
12354 if (old_a64) {
12355 old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12356 }
12357 if (new_a64) {
12358 new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12359 }
0ab5953b
RH
12360
12361 /* When changing vector length, clear inaccessible state. */
12362 if (new_len < old_len) {
12363 aarch64_sve_narrow_vq(env, new_len + 1);
12364 }
12365}
12366#endif