]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/helper.c
target/arm: Move hflags code into the tcg directory
[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"
0c1aaa66 25#include "sysemu/tcg.h"
7f7b4e7a 26#include "qapi/qapi-commands-machine-target.h"
de390645
RH
27#include "qapi/error.h"
28#include "qemu/guest-random.h"
91f78c58 29#ifdef CONFIG_TCG
6b5fe137 30#include "semihosting/common-semi.h"
91f78c58 31#endif
cf7c6d10 32#include "cpregs.h"
0b03bdfc 33
352c98e5
LV
34#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
35
affdb64d
PM
36static void switch_mode(CPUARMState *env, int mode);
37
c4241c7d 38static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
d4e6df63 39{
375421cc 40 assert(ri->fieldoffset);
67ed771d 41 if (cpreg_field_is_64bit(ri)) {
c4241c7d 42 return CPREG_FIELD64(env, ri);
22d9e1a9 43 } else {
c4241c7d 44 return CPREG_FIELD32(env, ri);
22d9e1a9 45 }
d4e6df63
PM
46}
47
f43ee493 48void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
d4e6df63 49{
375421cc 50 assert(ri->fieldoffset);
67ed771d 51 if (cpreg_field_is_64bit(ri)) {
22d9e1a9
PM
52 CPREG_FIELD64(env, ri) = value;
53 } else {
54 CPREG_FIELD32(env, ri) = value;
55 }
d4e6df63
PM
56}
57
11f136ee
FA
58static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
59{
60 return (char *)env + ri->fieldoffset;
61}
62
49a66191 63uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
721fae12 64{
59a1c327 65 /* Raw read of a coprocessor register (as needed for migration, etc). */
721fae12 66 if (ri->type & ARM_CP_CONST) {
59a1c327 67 return ri->resetvalue;
721fae12 68 } else if (ri->raw_readfn) {
59a1c327 69 return ri->raw_readfn(env, ri);
721fae12 70 } else if (ri->readfn) {
59a1c327 71 return ri->readfn(env, ri);
721fae12 72 } else {
59a1c327 73 return raw_read(env, ri);
721fae12 74 }
721fae12
PM
75}
76
59a1c327 77static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
7900e9f1 78 uint64_t v)
721fae12 79{
9b37a28c
FR
80 /*
81 * Raw write of a coprocessor register (as needed for migration, etc).
721fae12
PM
82 * Note that constant registers are treated as write-ignored; the
83 * caller should check for success by whether a readback gives the
84 * value written.
85 */
86 if (ri->type & ARM_CP_CONST) {
59a1c327 87 return;
721fae12 88 } else if (ri->raw_writefn) {
c4241c7d 89 ri->raw_writefn(env, ri, v);
721fae12 90 } else if (ri->writefn) {
c4241c7d 91 ri->writefn(env, ri, v);
721fae12 92 } else {
afb2530f 93 raw_write(env, ri, v);
721fae12 94 }
721fae12
PM
95}
96
375421cc
PM
97static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
98{
9b37a28c
FR
99 /*
100 * Return true if the regdef would cause an assertion if you called
375421cc
PM
101 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
102 * program bug for it not to have the NO_RAW flag).
103 * NB that returning false here doesn't necessarily mean that calling
104 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
105 * read/write access functions which are safe for raw use" from "has
106 * read/write access functions which have side effects but has forgotten
107 * to provide raw access functions".
108 * The tests here line up with the conditions in read/write_raw_cp_reg()
109 * and assertions in raw_read()/raw_write().
110 */
111 if ((ri->type & ARM_CP_CONST) ||
112 ri->fieldoffset ||
113 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
114 return false;
115 }
116 return true;
117}
118
b698e4ee 119bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync)
721fae12
PM
120{
121 /* Write the coprocessor state from cpu->env to the (index,value) list. */
122 int i;
123 bool ok = true;
124
125 for (i = 0; i < cpu->cpreg_array_len; i++) {
126 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
127 const ARMCPRegInfo *ri;
b698e4ee 128 uint64_t newval;
59a1c327 129
60322b39 130 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
131 if (!ri) {
132 ok = false;
133 continue;
134 }
7a0e58fa 135 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
136 continue;
137 }
b698e4ee
PM
138
139 newval = read_raw_cp_reg(&cpu->env, ri);
140 if (kvm_sync) {
141 /*
142 * Only sync if the previous list->cpustate sync succeeded.
143 * Rather than tracking the success/failure state for every
144 * item in the list, we just recheck "does the raw write we must
145 * have made in write_list_to_cpustate() read back OK" here.
146 */
147 uint64_t oldval = cpu->cpreg_values[i];
148
149 if (oldval == newval) {
150 continue;
151 }
152
153 write_raw_cp_reg(&cpu->env, ri, oldval);
154 if (read_raw_cp_reg(&cpu->env, ri) != oldval) {
155 continue;
156 }
157
158 write_raw_cp_reg(&cpu->env, ri, newval);
159 }
160 cpu->cpreg_values[i] = newval;
721fae12
PM
161 }
162 return ok;
163}
164
165bool write_list_to_cpustate(ARMCPU *cpu)
166{
167 int i;
168 bool ok = true;
169
170 for (i = 0; i < cpu->cpreg_array_len; i++) {
171 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
172 uint64_t v = cpu->cpreg_values[i];
721fae12
PM
173 const ARMCPRegInfo *ri;
174
60322b39 175 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12
PM
176 if (!ri) {
177 ok = false;
178 continue;
179 }
7a0e58fa 180 if (ri->type & ARM_CP_NO_RAW) {
721fae12
PM
181 continue;
182 }
9b37a28c
FR
183 /*
184 * Write value and confirm it reads back as written
721fae12
PM
185 * (to catch read-only registers and partially read-only
186 * registers where the incoming migration value doesn't match)
187 */
59a1c327
PM
188 write_raw_cp_reg(&cpu->env, ri, v);
189 if (read_raw_cp_reg(&cpu->env, ri) != v) {
721fae12
PM
190 ok = false;
191 }
192 }
193 return ok;
194}
195
196static void add_cpreg_to_list(gpointer key, gpointer opaque)
197{
198 ARMCPU *cpu = opaque;
5860362d
RH
199 uint32_t regidx = (uintptr_t)key;
200 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
721fae12 201
04215eb1 202 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
721fae12
PM
203 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
204 /* The value array need not be initialized at this point */
205 cpu->cpreg_array_len++;
206 }
207}
208
209static void count_cpreg(gpointer key, gpointer opaque)
210{
211 ARMCPU *cpu = opaque;
721fae12
PM
212 const ARMCPRegInfo *ri;
213
5860362d 214 ri = g_hash_table_lookup(cpu->cp_regs, key);
721fae12 215
04215eb1 216 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
721fae12
PM
217 cpu->cpreg_array_len++;
218 }
219}
220
221static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
222{
5860362d
RH
223 uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a);
224 uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b);
721fae12 225
cbf239b7
AR
226 if (aidx > bidx) {
227 return 1;
228 }
229 if (aidx < bidx) {
230 return -1;
231 }
232 return 0;
721fae12
PM
233}
234
235void init_cpreg_list(ARMCPU *cpu)
236{
9b37a28c
FR
237 /*
238 * Initialise the cpreg_tuples[] array based on the cp_regs hash.
721fae12
PM
239 * Note that we require cpreg_tuples[] to be sorted by key ID.
240 */
57b6d95e 241 GList *keys;
721fae12
PM
242 int arraylen;
243
57b6d95e 244 keys = g_hash_table_get_keys(cpu->cp_regs);
721fae12
PM
245 keys = g_list_sort(keys, cpreg_key_compare);
246
247 cpu->cpreg_array_len = 0;
248
249 g_list_foreach(keys, count_cpreg, cpu);
250
251 arraylen = cpu->cpreg_array_len;
252 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
253 cpu->cpreg_values = g_new(uint64_t, arraylen);
254 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
255 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
256 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
257 cpu->cpreg_array_len = 0;
258
259 g_list_foreach(keys, add_cpreg_to_list, cpu);
260
261 assert(cpu->cpreg_array_len == arraylen);
262
263 g_list_free(keys);
264}
265
68e9c2fe 266/*
93dd1e61 267 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0.
68e9c2fe
EI
268 */
269static CPAccessResult access_el3_aa32ns(CPUARMState *env,
3f208fd7
PM
270 const ARMCPRegInfo *ri,
271 bool isread)
68e9c2fe 272{
93dd1e61
EI
273 if (!is_a64(env) && arm_current_el(env) == 3 &&
274 arm_is_secure_below_el3(env)) {
68e9c2fe
EI
275 return CP_ACCESS_TRAP_UNCATEGORIZED;
276 }
277 return CP_ACCESS_OK;
278}
279
9b37a28c
FR
280/*
281 * Some secure-only AArch32 registers trap to EL3 if used from
5513c3ab
PM
282 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
283 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
284 * We assume that the .access field is set to PL1_RW.
285 */
286static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
3f208fd7
PM
287 const ARMCPRegInfo *ri,
288 bool isread)
5513c3ab
PM
289{
290 if (arm_current_el(env) == 3) {
291 return CP_ACCESS_OK;
292 }
293 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
294 if (env->cp15.scr_el3 & SCR_EEL2) {
295 return CP_ACCESS_TRAP_EL2;
296 }
5513c3ab
PM
297 return CP_ACCESS_TRAP_EL3;
298 }
299 /* This will be EL1 NS and EL2 NS, which just UNDEF */
300 return CP_ACCESS_TRAP_UNCATEGORIZED;
301}
302
9b37a28c
FR
303/*
304 * Check for traps to performance monitor registers, which are controlled
1fce1ba9
PM
305 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
306 */
307static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
308 bool isread)
309{
310 int el = arm_current_el(env);
59dd089c 311 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1fce1ba9 312
59dd089c 313 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1fce1ba9
PM
314 return CP_ACCESS_TRAP_EL2;
315 }
316 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
317 return CP_ACCESS_TRAP_EL3;
318 }
319 return CP_ACCESS_OK;
320}
321
84929218
RH
322/* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */
323static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri,
324 bool isread)
325{
326 if (arm_current_el(env) == 1) {
327 uint64_t trap = isread ? HCR_TRVM : HCR_TVM;
328 if (arm_hcr_el2_eff(env) & trap) {
329 return CP_ACCESS_TRAP_EL2;
330 }
331 }
332 return CP_ACCESS_OK;
333}
334
1803d271
RH
335/* Check for traps from EL1 due to HCR_EL2.TSW. */
336static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri,
337 bool isread)
338{
339 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) {
340 return CP_ACCESS_TRAP_EL2;
341 }
342 return CP_ACCESS_OK;
343}
344
99602377
RH
345/* Check for traps from EL1 due to HCR_EL2.TACR. */
346static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri,
347 bool isread)
348{
349 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) {
350 return CP_ACCESS_TRAP_EL2;
351 }
352 return CP_ACCESS_OK;
353}
354
30881b73
RH
355/* Check for traps from EL1 due to HCR_EL2.TTLB. */
356static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri,
357 bool isread)
358{
359 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) {
360 return CP_ACCESS_TRAP_EL2;
361 }
362 return CP_ACCESS_OK;
363}
364
0f66d223
PM
365/* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBIS. */
366static CPAccessResult access_ttlbis(CPUARMState *env, const ARMCPRegInfo *ri,
367 bool isread)
368{
369 if (arm_current_el(env) == 1 &&
370 (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBIS))) {
371 return CP_ACCESS_TRAP_EL2;
372 }
373 return CP_ACCESS_OK;
374}
375
fe3ca86c
PM
376#ifdef TARGET_AARCH64
377/* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBOS. */
378static CPAccessResult access_ttlbos(CPUARMState *env, const ARMCPRegInfo *ri,
379 bool isread)
380{
381 if (arm_current_el(env) == 1 &&
382 (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBOS))) {
383 return CP_ACCESS_TRAP_EL2;
384 }
385 return CP_ACCESS_OK;
386}
387#endif
388
c4241c7d 389static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
c983fe6c 390{
2fc0cc0e 391 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 392
8d5c773e 393 raw_write(env, ri, value);
d10eb08f 394 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */
c983fe6c
PM
395}
396
c4241c7d 397static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
08de207b 398{
2fc0cc0e 399 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 400
8d5c773e 401 if (raw_read(env, ri) != value) {
9b37a28c
FR
402 /*
403 * Unlike real hardware the qemu TLB uses virtual addresses,
08de207b
PM
404 * not modified virtual addresses, so this causes a TLB flush.
405 */
d10eb08f 406 tlb_flush(CPU(cpu));
8d5c773e 407 raw_write(env, ri, value);
08de207b 408 }
08de207b 409}
c4241c7d
PM
410
411static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
412 uint64_t value)
08de207b 413{
2fc0cc0e 414 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 415
452a0955 416 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA)
014406b5 417 && !extended_addresses_enabled(env)) {
9b37a28c
FR
418 /*
419 * For VMSA (when not using the LPAE long descriptor page table
08de207b
PM
420 * format) this register includes the ASID, so do a TLB flush.
421 * For PMSA it is purely a process ID and no action is needed.
422 */
d10eb08f 423 tlb_flush(CPU(cpu));
08de207b 424 }
8d5c773e 425 raw_write(env, ri, value);
08de207b
PM
426}
427
575a94af
RH
428static int alle1_tlbmask(CPUARMState *env)
429{
430 /*
431 * Note that the 'ALL' scope must invalidate both stage 1 and
432 * stage 2 translations, whereas most other scopes only invalidate
433 * stage 1 translations.
434 */
435 return (ARMMMUIdxBit_E10_1 |
436 ARMMMUIdxBit_E10_1_PAN |
437 ARMMMUIdxBit_E10_0 |
438 ARMMMUIdxBit_Stage2 |
439 ARMMMUIdxBit_Stage2_S);
440}
441
442
b4ab8ce9
PM
443/* IS variants of TLB operations must affect all cores */
444static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
445 uint64_t value)
446{
29a0af61 447 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
448
449 tlb_flush_all_cpus_synced(cs);
450}
451
452static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
453 uint64_t value)
454{
29a0af61 455 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
456
457 tlb_flush_all_cpus_synced(cs);
458}
459
460static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
461 uint64_t value)
462{
29a0af61 463 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
464
465 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
466}
467
468static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
469 uint64_t value)
470{
29a0af61 471 CPUState *cs = env_cpu(env);
b4ab8ce9
PM
472
473 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK);
474}
475
476/*
477 * Non-IS variants of TLB operations are upgraded to
373e7ffd 478 * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to
b4ab8ce9
PM
479 * force broadcast of these operations.
480 */
481static bool tlb_force_broadcast(CPUARMState *env)
482{
373e7ffd 483 return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB);
b4ab8ce9
PM
484}
485
c4241c7d
PM
486static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
487 uint64_t value)
d929823f
PM
488{
489 /* Invalidate all (TLBIALL) */
527db2be 490 CPUState *cs = env_cpu(env);
00c8cb0a 491
b4ab8ce9 492 if (tlb_force_broadcast(env)) {
527db2be
RH
493 tlb_flush_all_cpus_synced(cs);
494 } else {
495 tlb_flush(cs);
b4ab8ce9 496 }
d929823f
PM
497}
498
c4241c7d
PM
499static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
500 uint64_t value)
d929823f
PM
501{
502 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
527db2be 503 CPUState *cs = env_cpu(env);
31b030d4 504
527db2be 505 value &= TARGET_PAGE_MASK;
b4ab8ce9 506 if (tlb_force_broadcast(env)) {
527db2be
RH
507 tlb_flush_page_all_cpus_synced(cs, value);
508 } else {
509 tlb_flush_page(cs, value);
b4ab8ce9 510 }
d929823f
PM
511}
512
c4241c7d
PM
513static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
514 uint64_t value)
d929823f
PM
515{
516 /* Invalidate by ASID (TLBIASID) */
527db2be 517 CPUState *cs = env_cpu(env);
00c8cb0a 518
b4ab8ce9 519 if (tlb_force_broadcast(env)) {
527db2be
RH
520 tlb_flush_all_cpus_synced(cs);
521 } else {
522 tlb_flush(cs);
b4ab8ce9 523 }
d929823f
PM
524}
525
c4241c7d
PM
526static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
527 uint64_t value)
d929823f
PM
528{
529 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
527db2be 530 CPUState *cs = env_cpu(env);
31b030d4 531
527db2be 532 value &= TARGET_PAGE_MASK;
b4ab8ce9 533 if (tlb_force_broadcast(env)) {
527db2be
RH
534 tlb_flush_page_all_cpus_synced(cs, value);
535 } else {
536 tlb_flush_page(cs, value);
b4ab8ce9 537 }
fa439fc5
PM
538}
539
541ef8c2
SS
540static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri,
541 uint64_t value)
542{
29a0af61 543 CPUState *cs = env_cpu(env);
541ef8c2 544
575a94af 545 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
541ef8c2
SS
546}
547
548static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
549 uint64_t value)
550{
29a0af61 551 CPUState *cs = env_cpu(env);
541ef8c2 552
575a94af 553 tlb_flush_by_mmuidx_all_cpus_synced(cs, alle1_tlbmask(env));
541ef8c2
SS
554}
555
541ef8c2
SS
556
557static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
558 uint64_t value)
559{
29a0af61 560 CPUState *cs = env_cpu(env);
541ef8c2 561
e013b741 562 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
563}
564
565static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
566 uint64_t value)
567{
29a0af61 568 CPUState *cs = env_cpu(env);
541ef8c2 569
e013b741 570 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2);
541ef8c2
SS
571}
572
573static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
574 uint64_t value)
575{
29a0af61 576 CPUState *cs = env_cpu(env);
541ef8c2
SS
577 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
578
e013b741 579 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2);
541ef8c2
SS
580}
581
582static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
583 uint64_t value)
584{
29a0af61 585 CPUState *cs = env_cpu(env);
541ef8c2
SS
586 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12);
587
a67cf277 588 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr,
e013b741 589 ARMMMUIdxBit_E2);
541ef8c2
SS
590}
591
575a94af
RH
592static void tlbiipas2_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
593 uint64_t value)
594{
595 CPUState *cs = env_cpu(env);
596 uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
597
598 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2);
599}
600
601static void tlbiipas2is_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri,
602 uint64_t value)
603{
604 CPUState *cs = env_cpu(env);
605 uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12;
606
607 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, ARMMMUIdxBit_Stage2);
608}
609
e9aa6c21 610static const ARMCPRegInfo cp_reginfo[] = {
9b37a28c
FR
611 /*
612 * Define the secure and non-secure FCSE identifier CP registers
54bf36ed
FA
613 * separately because there is no secure bank in V8 (no _EL3). This allows
614 * the secure register to be properly reset and migrated. There is also no
615 * v8 EL1 version of the register so the non-secure instance stands alone.
616 */
9c513e78 617 { .name = "FCSEIDR",
54bf36ed
FA
618 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
619 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
620 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
621 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9c513e78 622 { .name = "FCSEIDR_S",
54bf36ed
FA
623 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
624 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
625 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
d4e6df63 626 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
9b37a28c
FR
627 /*
628 * Define the secure and non-secure context identifier CP registers
54bf36ed
FA
629 * separately because there is no secure bank in V8 (no _EL3). This allows
630 * the secure register to be properly reset and migrated. In the
631 * non-secure case, the 32-bit register will have reset and migration
632 * disabled during registration as it is handled by the 64-bit instance.
633 */
634 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
014406b5 635 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218 636 .access = PL1_RW, .accessfn = access_tvm_trvm,
158c276c 637 .fgt = FGT_CONTEXTIDR_EL1,
84929218 638 .secure = ARM_CP_SECSTATE_NS,
54bf36ed
FA
639 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
640 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9c513e78 641 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32,
54bf36ed 642 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
84929218
RH
643 .access = PL1_RW, .accessfn = access_tvm_trvm,
644 .secure = ARM_CP_SECSTATE_S,
54bf36ed 645 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
d4e6df63 646 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
9449fdf6
PM
647};
648
649static const ARMCPRegInfo not_v8_cp_reginfo[] = {
9b37a28c
FR
650 /*
651 * NB: Some of these registers exist in v8 but with more precise
9449fdf6
PM
652 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
653 */
654 /* MMU Domain access control / MPU write buffer control */
0c17d68c
FA
655 { .name = "DACR",
656 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
84929218 657 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
658 .writefn = dacr_write, .raw_writefn = raw_write,
659 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
660 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
9b37a28c
FR
661 /*
662 * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
a903c449 663 * For v6 and v5, these mappings are overly broad.
4fdd17dd 664 */
a903c449
EI
665 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
666 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
667 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
668 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
669 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
670 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
671 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
4fdd17dd 672 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
c4804214
PM
673 /* Cache maintenance ops; some of this space may be overridden later. */
674 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
675 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
676 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
e9aa6c21
PM
677};
678
7d57f408 679static const ARMCPRegInfo not_v6_cp_reginfo[] = {
9b37a28c
FR
680 /*
681 * Not all pre-v6 cores implemented this WFI, so this is slightly
7d57f408
PM
682 * over-broad.
683 */
684 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
685 .access = PL1_W, .type = ARM_CP_WFI },
7d57f408
PM
686};
687
688static const ARMCPRegInfo not_v7_cp_reginfo[] = {
9b37a28c
FR
689 /*
690 * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
7d57f408
PM
691 * is UNPREDICTABLE; we choose to NOP as most implementations do).
692 */
693 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
694 .access = PL1_W, .type = ARM_CP_WFI },
9b37a28c
FR
695 /*
696 * L1 cache lockdown. Not architectural in v6 and earlier but in practice
34f90529
PM
697 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
698 * OMAPCP will override this space.
699 */
700 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
701 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
702 .resetvalue = 0 },
703 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
704 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
705 .resetvalue = 0 },
776d4e5c
PM
706 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
707 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
7a0e58fa 708 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 709 .resetvalue = 0 },
9b37a28c
FR
710 /*
711 * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
50300698
PM
712 * implementing it as RAZ means the "debug architecture version" bits
713 * will read as a reserved value, which should cause Linux to not try
714 * to use the debug hardware.
715 */
716 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
717 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
9b37a28c
FR
718 /*
719 * MMU TLB control. Note that the wildcarding means we cover not just
995939a6
PM
720 * the unified TLB ops but also the dside/iside/inner-shareable variants.
721 */
722 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
723 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
7a0e58fa 724 .type = ARM_CP_NO_RAW },
995939a6
PM
725 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
726 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
7a0e58fa 727 .type = ARM_CP_NO_RAW },
995939a6
PM
728 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
729 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
7a0e58fa 730 .type = ARM_CP_NO_RAW },
995939a6
PM
731 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
732 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
7a0e58fa 733 .type = ARM_CP_NO_RAW },
a903c449
EI
734 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
735 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
736 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
737 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
7d57f408
PM
738};
739
c4241c7d
PM
740static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
741 uint64_t value)
2771db27 742{
f0aff255
FA
743 uint32_t mask = 0;
744
745 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
746 if (!arm_feature(env, ARM_FEATURE_V8)) {
9b37a28c
FR
747 /*
748 * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
f0aff255
FA
749 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
750 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
751 */
7fbc6a40 752 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
f0aff255 753 /* VFP coprocessor: cp10 & cp11 [23:20] */
fab8ad39
RH
754 mask |= R_CPACR_ASEDIS_MASK |
755 R_CPACR_D32DIS_MASK |
756 R_CPACR_CP11_MASK |
757 R_CPACR_CP10_MASK;
f0aff255
FA
758
759 if (!arm_feature(env, ARM_FEATURE_NEON)) {
760 /* ASEDIS [31] bit is RAO/WI */
fab8ad39 761 value |= R_CPACR_ASEDIS_MASK;
f0aff255
FA
762 }
763
9b37a28c
FR
764 /*
765 * VFPv3 and upwards with NEON implement 32 double precision
f0aff255
FA
766 * registers (D0-D31).
767 */
a6627f5f 768 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) {
f0aff255 769 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
fab8ad39 770 value |= R_CPACR_D32DIS_MASK;
f0aff255
FA
771 }
772 }
773 value &= mask;
2771db27 774 }
fc1120a7
PM
775
776 /*
777 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
778 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
779 */
780 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
781 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39
RH
782 mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK;
783 value = (value & ~mask) | (env->cp15.cpacr_el1 & mask);
fc1120a7
PM
784 }
785
7ebd5f2e 786 env->cp15.cpacr_el1 = value;
2771db27
PM
787}
788
fc1120a7
PM
789static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri)
790{
791 /*
792 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10
793 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00.
794 */
795 uint64_t value = env->cp15.cpacr_el1;
796
797 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
798 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39 799 value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK);
fc1120a7
PM
800 }
801 return value;
802}
803
804
5deac39c
PM
805static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
806{
9b37a28c
FR
807 /*
808 * Call cpacr_write() so that we reset with the correct RAO bits set
5deac39c
PM
809 * for our CPU features.
810 */
811 cpacr_write(env, ri, 0);
812}
813
3f208fd7
PM
814static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
815 bool isread)
c6f19164
GB
816{
817 if (arm_feature(env, ARM_FEATURE_V8)) {
818 /* Check if CPACR accesses are to be trapped to EL2 */
e6ef0169 819 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) &&
fab8ad39 820 FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) {
c6f19164
GB
821 return CP_ACCESS_TRAP_EL2;
822 /* Check if CPACR accesses are to be trapped to EL3 */
823 } else if (arm_current_el(env) < 3 &&
fab8ad39 824 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
c6f19164
GB
825 return CP_ACCESS_TRAP_EL3;
826 }
827 }
828
829 return CP_ACCESS_OK;
830}
831
3f208fd7
PM
832static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
833 bool isread)
c6f19164
GB
834{
835 /* Check if CPTR accesses are set to trap to EL3 */
fab8ad39
RH
836 if (arm_current_el(env) == 2 &&
837 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) {
c6f19164
GB
838 return CP_ACCESS_TRAP_EL3;
839 }
840
841 return CP_ACCESS_OK;
842}
843
7d57f408
PM
844static const ARMCPRegInfo v6_cp_reginfo[] = {
845 /* prefetch by MVA in v6, NOP in v7 */
846 { .name = "MVA_prefetch",
847 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
848 .access = PL1_W, .type = ARM_CP_NOP },
9b37a28c
FR
849 /*
850 * We need to break the TB after ISB to execute self-modifying code
6df99dec
SS
851 * correctly and also to take any pending interrupts immediately.
852 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
853 */
7d57f408 854 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
6df99dec 855 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
091fd17c 856 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
7d57f408 857 .access = PL0_W, .type = ARM_CP_NOP },
091fd17c 858 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
7d57f408 859 .access = PL0_W, .type = ARM_CP_NOP },
06d76f31 860 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218 861 .access = PL1_RW, .accessfn = access_tvm_trvm,
b848ce2b
FA
862 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
863 offsetof(CPUARMState, cp15.ifar_ns) },
06d76f31 864 .resetvalue = 0, },
9b37a28c
FR
865 /*
866 * Watchpoint Fault Address Register : should actually only be present
06d76f31
PM
867 * for 1136, 1176, 11MPCore.
868 */
869 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
870 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
34222fb8 871 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
c6f19164 872 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
b19ed03c 873 .fgt = FGT_CPACR_EL1,
7ebd5f2e 874 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
fc1120a7 875 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read },
7d57f408
PM
876};
877
57a4a11b
AL
878typedef struct pm_event {
879 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */
880 /* If the event is supported on this CPU (used to generate PMCEID[01]) */
881 bool (*supported)(CPUARMState *);
882 /*
883 * Retrieve the current count of the underlying event. The programmed
884 * counters hold a difference from the return value from this function
885 */
886 uint64_t (*get_count)(CPUARMState *);
4e7beb0c
AL
887 /*
888 * Return how many nanoseconds it will take (at a minimum) for count events
889 * to occur. A negative value indicates the counter will never overflow, or
890 * that the counter has otherwise arranged for the overflow bit to be set
891 * and the PMU interrupt to be raised on overflow.
892 */
893 int64_t (*ns_per_count)(uint64_t);
57a4a11b
AL
894} pm_event;
895
b2e23725
AL
896static bool event_always_supported(CPUARMState *env)
897{
898 return true;
899}
900
0d4bfd7d
AL
901static uint64_t swinc_get_count(CPUARMState *env)
902{
903 /*
904 * SW_INCR events are written directly to the pmevcntr's by writes to
905 * PMSWINC, so there is no underlying count maintained by the PMU itself
906 */
907 return 0;
908}
909
4e7beb0c
AL
910static int64_t swinc_ns_per(uint64_t ignored)
911{
912 return -1;
913}
914
b2e23725
AL
915/*
916 * Return the underlying cycle count for the PMU cycle counters. If we're in
917 * usermode, simply return 0.
918 */
919static uint64_t cycles_get_count(CPUARMState *env)
920{
921#ifndef CONFIG_USER_ONLY
922 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
923 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
924#else
925 return cpu_get_host_ticks();
926#endif
927}
928
929#ifndef CONFIG_USER_ONLY
4e7beb0c
AL
930static int64_t cycles_ns_per(uint64_t cycles)
931{
932 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles;
933}
934
b2e23725
AL
935static bool instructions_supported(CPUARMState *env)
936{
740b1759 937 return icount_enabled() == 1; /* Precise instruction counting */
b2e23725
AL
938}
939
940static uint64_t instructions_get_count(CPUARMState *env)
941{
8191d368 942 return (uint64_t)icount_get_raw();
b2e23725 943}
4e7beb0c
AL
944
945static int64_t instructions_ns_per(uint64_t icount)
946{
8191d368 947 return icount_to_ns((int64_t)icount);
4e7beb0c 948}
b2e23725
AL
949#endif
950
a793bcd0 951static bool pmuv3p1_events_supported(CPUARMState *env)
0727f63b
PM
952{
953 /* For events which are supported in any v8.1 PMU */
a793bcd0 954 return cpu_isar_feature(any_pmuv3p1, env_archcpu(env));
0727f63b
PM
955}
956
a793bcd0 957static bool pmuv3p4_events_supported(CPUARMState *env)
15dd1ebd
PM
958{
959 /* For events which are supported in any v8.1 PMU */
a793bcd0 960 return cpu_isar_feature(any_pmuv3p4, env_archcpu(env));
15dd1ebd
PM
961}
962
0727f63b
PM
963static uint64_t zero_event_get_count(CPUARMState *env)
964{
965 /* For events which on QEMU never fire, so their count is always zero */
966 return 0;
967}
968
969static int64_t zero_event_ns_per(uint64_t cycles)
970{
971 /* An event which never fires can never overflow */
972 return -1;
973}
974
57a4a11b 975static const pm_event pm_events[] = {
0d4bfd7d
AL
976 { .number = 0x000, /* SW_INCR */
977 .supported = event_always_supported,
978 .get_count = swinc_get_count,
4e7beb0c 979 .ns_per_count = swinc_ns_per,
0d4bfd7d 980 },
b2e23725
AL
981#ifndef CONFIG_USER_ONLY
982 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */
983 .supported = instructions_supported,
984 .get_count = instructions_get_count,
4e7beb0c 985 .ns_per_count = instructions_ns_per,
b2e23725
AL
986 },
987 { .number = 0x011, /* CPU_CYCLES, Cycle */
988 .supported = event_always_supported,
989 .get_count = cycles_get_count,
4e7beb0c 990 .ns_per_count = cycles_ns_per,
0727f63b 991 },
b2e23725 992#endif
0727f63b 993 { .number = 0x023, /* STALL_FRONTEND */
a793bcd0 994 .supported = pmuv3p1_events_supported,
0727f63b
PM
995 .get_count = zero_event_get_count,
996 .ns_per_count = zero_event_ns_per,
997 },
998 { .number = 0x024, /* STALL_BACKEND */
a793bcd0 999 .supported = pmuv3p1_events_supported,
0727f63b
PM
1000 .get_count = zero_event_get_count,
1001 .ns_per_count = zero_event_ns_per,
1002 },
15dd1ebd 1003 { .number = 0x03c, /* STALL */
a793bcd0 1004 .supported = pmuv3p4_events_supported,
15dd1ebd
PM
1005 .get_count = zero_event_get_count,
1006 .ns_per_count = zero_event_ns_per,
1007 },
57a4a11b
AL
1008};
1009
1010/*
1011 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of
1012 * events (i.e. the statistical profiling extension), this implementation
1013 * should first be updated to something sparse instead of the current
1014 * supported_event_map[] array.
1015 */
15dd1ebd 1016#define MAX_EVENT_ID 0x3c
57a4a11b
AL
1017#define UNSUPPORTED_EVENT UINT16_MAX
1018static uint16_t supported_event_map[MAX_EVENT_ID + 1];
1019
1020/*
bf8d0969
AL
1021 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map
1022 * of ARM event numbers to indices in our pm_events array.
57a4a11b
AL
1023 *
1024 * Note: Events in the 0x40XX range are not currently supported.
1025 */
bf8d0969 1026void pmu_init(ARMCPU *cpu)
57a4a11b 1027{
57a4a11b
AL
1028 unsigned int i;
1029
bf8d0969
AL
1030 /*
1031 * Empty supported_event_map and cpu->pmceid[01] before adding supported
1032 * events to them
1033 */
57a4a11b
AL
1034 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) {
1035 supported_event_map[i] = UNSUPPORTED_EVENT;
1036 }
bf8d0969
AL
1037 cpu->pmceid0 = 0;
1038 cpu->pmceid1 = 0;
57a4a11b
AL
1039
1040 for (i = 0; i < ARRAY_SIZE(pm_events); i++) {
1041 const pm_event *cnt = &pm_events[i];
1042 assert(cnt->number <= MAX_EVENT_ID);
1043 /* We do not currently support events in the 0x40xx range */
1044 assert(cnt->number <= 0x3f);
1045
bf8d0969 1046 if (cnt->supported(&cpu->env)) {
57a4a11b 1047 supported_event_map[cnt->number] = i;
67da43d6 1048 uint64_t event_mask = 1ULL << (cnt->number & 0x1f);
bf8d0969
AL
1049 if (cnt->number & 0x20) {
1050 cpu->pmceid1 |= event_mask;
1051 } else {
1052 cpu->pmceid0 |= event_mask;
1053 }
57a4a11b
AL
1054 }
1055 }
57a4a11b
AL
1056}
1057
5ecdd3e4
AL
1058/*
1059 * Check at runtime whether a PMU event is supported for the current machine
1060 */
1061static bool event_supported(uint16_t number)
1062{
1063 if (number > MAX_EVENT_ID) {
1064 return false;
1065 }
1066 return supported_event_map[number] != UNSUPPORTED_EVENT;
1067}
1068
3f208fd7
PM
1069static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
1070 bool isread)
200ac0ef 1071{
9b37a28c
FR
1072 /*
1073 * Performance monitor registers user accessibility is controlled
1fce1ba9
PM
1074 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
1075 * trapping to EL2 or EL3 for other accesses.
200ac0ef 1076 */
1fce1ba9 1077 int el = arm_current_el(env);
59dd089c 1078 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1fce1ba9 1079
6ecd0b6b 1080 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
fcd25206 1081 return CP_ACCESS_TRAP;
200ac0ef 1082 }
59dd089c 1083 if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
1fce1ba9
PM
1084 return CP_ACCESS_TRAP_EL2;
1085 }
1086 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
1087 return CP_ACCESS_TRAP_EL3;
1088 }
1089
fcd25206 1090 return CP_ACCESS_OK;
200ac0ef
PM
1091}
1092
6ecd0b6b
AB
1093static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
1094 const ARMCPRegInfo *ri,
1095 bool isread)
1096{
1097 /* ER: event counter read trap control */
1098 if (arm_feature(env, ARM_FEATURE_V8)
1099 && arm_current_el(env) == 0
1100 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0
1101 && isread) {
1102 return CP_ACCESS_OK;
1103 }
1104
1105 return pmreg_access(env, ri, isread);
1106}
1107
1108static CPAccessResult pmreg_access_swinc(CPUARMState *env,
1109 const ARMCPRegInfo *ri,
1110 bool isread)
1111{
1112 /* SW: software increment write trap control */
1113 if (arm_feature(env, ARM_FEATURE_V8)
1114 && arm_current_el(env) == 0
1115 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0
1116 && !isread) {
1117 return CP_ACCESS_OK;
1118 }
1119
1120 return pmreg_access(env, ri, isread);
1121}
1122
6ecd0b6b
AB
1123static CPAccessResult pmreg_access_selr(CPUARMState *env,
1124 const ARMCPRegInfo *ri,
1125 bool isread)
1126{
1127 /* ER: event counter read trap control */
1128 if (arm_feature(env, ARM_FEATURE_V8)
1129 && arm_current_el(env) == 0
1130 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) {
1131 return CP_ACCESS_OK;
1132 }
1133
1134 return pmreg_access(env, ri, isread);
1135}
1136
1137static CPAccessResult pmreg_access_ccntr(CPUARMState *env,
1138 const ARMCPRegInfo *ri,
1139 bool isread)
1140{
1141 /* CR: cycle counter read trap control */
1142 if (arm_feature(env, ARM_FEATURE_V8)
1143 && arm_current_el(env) == 0
1144 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0
1145 && isread) {
1146 return CP_ACCESS_OK;
1147 }
1148
1149 return pmreg_access(env, ri, isread);
1150}
1151
01765386
PM
1152/*
1153 * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at.
1154 * We use these to decide whether we need to wrap a write to MDCR_EL2
1155 * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls.
1156 */
47b385da
PM
1157#define MDCR_EL2_PMU_ENABLE_BITS \
1158 (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP)
0b42f4fa 1159#define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD)
01765386 1160
9b37a28c
FR
1161/*
1162 * Returns true if the counter (pass 31 for PMCCNTR) should count events using
033614c4
AL
1163 * the current EL, security state, and register configuration.
1164 */
1165static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
87124fde 1166{
033614c4
AL
1167 uint64_t filter;
1168 bool e, p, u, nsk, nsu, nsh, m;
872d2034 1169 bool enabled, prohibited = false, filtered;
033614c4
AL
1170 bool secure = arm_is_secure(env);
1171 int el = arm_current_el(env);
59dd089c
RDC
1172 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
1173 uint8_t hpmn = mdcr_el2 & MDCR_HPMN;
87124fde 1174
cbbb3041
AJ
1175 if (!arm_feature(env, ARM_FEATURE_PMU)) {
1176 return false;
1177 }
1178
033614c4
AL
1179 if (!arm_feature(env, ARM_FEATURE_EL2) ||
1180 (counter < hpmn || counter == 31)) {
1181 e = env->cp15.c9_pmcr & PMCRE;
1182 } else {
59dd089c 1183 e = mdcr_el2 & MDCR_HPME;
87124fde 1184 }
033614c4 1185 enabled = e && (env->cp15.c9_pmcnten & (1 << counter));
87124fde 1186
872d2034
PM
1187 /* Is event counting prohibited? */
1188 if (el == 2 && (counter < hpmn || counter == 31)) {
1189 prohibited = mdcr_el2 & MDCR_HPMD;
1190 }
1191 if (secure) {
1192 prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME);
033614c4
AL
1193 }
1194
0b42f4fa
PM
1195 if (counter == 31) {
1196 /*
1197 * The cycle counter defaults to running. PMCR.DP says "disable
1198 * the cycle counter when event counting is prohibited".
1199 * Some MDCR bits disable the cycle counter specifically.
1200 */
1201 prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP;
1202 if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1203 if (secure) {
1204 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD);
1205 }
1206 if (el == 2) {
1207 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD);
1208 }
1209 }
033614c4
AL
1210 }
1211
5ecdd3e4
AL
1212 if (counter == 31) {
1213 filter = env->cp15.pmccfiltr_el0;
1214 } else {
1215 filter = env->cp15.c14_pmevtyper[counter];
1216 }
033614c4
AL
1217
1218 p = filter & PMXEVTYPER_P;
1219 u = filter & PMXEVTYPER_U;
1220 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK);
1221 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU);
1222 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH);
1223 m = arm_el_is_aa64(env, 1) &&
1224 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M);
1225
1226 if (el == 0) {
1227 filtered = secure ? u : u != nsu;
1228 } else if (el == 1) {
1229 filtered = secure ? p : p != nsk;
1230 } else if (el == 2) {
1231 filtered = !nsh;
1232 } else { /* EL3 */
1233 filtered = m != p;
1234 }
1235
5ecdd3e4
AL
1236 if (counter != 31) {
1237 /*
1238 * If not checking PMCCNTR, ensure the counter is setup to an event we
1239 * support
1240 */
1241 uint16_t event = filter & PMXEVTYPER_EVTCOUNT;
1242 if (!event_supported(event)) {
1243 return false;
1244 }
1245 }
1246
033614c4 1247 return enabled && !prohibited && !filtered;
87124fde 1248}
033614c4 1249
f4efb4b2
AL
1250static void pmu_update_irq(CPUARMState *env)
1251{
2fc0cc0e 1252 ARMCPU *cpu = env_archcpu(env);
f4efb4b2
AL
1253 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) &&
1254 (env->cp15.c9_pminten & env->cp15.c9_pmovsr));
1255}
1256
b57aa7bd
PM
1257static bool pmccntr_clockdiv_enabled(CPUARMState *env)
1258{
1259 /*
1260 * Return true if the clock divider is enabled and the cycle counter
1261 * is supposed to tick only once every 64 clock cycles. This is
1262 * controlled by PMCR.D, but if PMCR.LC is set to enable the long
1263 * (64-bit) cycle counter PMCR.D has no effect.
1264 */
1265 return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD;
1266}
1267
47b385da
PM
1268static bool pmevcntr_is_64_bit(CPUARMState *env, int counter)
1269{
1270 /* Return true if the specified event counter is configured to be 64 bit */
1271
1272 /* This isn't intended to be used with the cycle counter */
1273 assert(counter < 31);
1274
1275 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1276 return false;
1277 }
1278
1279 if (arm_feature(env, ARM_FEATURE_EL2)) {
1280 /*
1281 * MDCR_EL2.HLP still applies even when EL2 is disabled in the
1282 * current security state, so we don't use arm_mdcr_el2_eff() here.
1283 */
1284 bool hlp = env->cp15.mdcr_el2 & MDCR_HLP;
1285 int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
1286
1287 if (hpmn != 0 && counter >= hpmn) {
1288 return hlp;
1289 }
1290 }
1291 return env->cp15.c9_pmcr & PMCRLP;
1292}
1293
5d05b9d4
AL
1294/*
1295 * Ensure c15_ccnt is the guest-visible count so that operations such as
1296 * enabling/disabling the counter or filtering, modifying the count itself,
1297 * etc. can be done logically. This is essentially a no-op if the counter is
1298 * not enabled at the time of the call.
1299 */
f2b2f53f 1300static void pmccntr_op_start(CPUARMState *env)
ec7b4ce4 1301{
b2e23725 1302 uint64_t cycles = cycles_get_count(env);
ec7b4ce4 1303
033614c4 1304 if (pmu_counter_enabled(env, 31)) {
5d05b9d4 1305 uint64_t eff_cycles = cycles;
b57aa7bd 1306 if (pmccntr_clockdiv_enabled(env)) {
5d05b9d4
AL
1307 eff_cycles /= 64;
1308 }
1309
f4efb4b2
AL
1310 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta;
1311
1312 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \
1313 1ull << 63 : 1ull << 31;
1314 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) {
76e25d41 1315 env->cp15.c9_pmovsr |= (1ULL << 31);
f4efb4b2
AL
1316 pmu_update_irq(env);
1317 }
1318
1319 env->cp15.c15_ccnt = new_pmccntr;
ec7b4ce4 1320 }
5d05b9d4
AL
1321 env->cp15.c15_ccnt_delta = cycles;
1322}
ec7b4ce4 1323
5d05b9d4
AL
1324/*
1325 * If PMCCNTR is enabled, recalculate the delta between the clock and the
1326 * guest-visible count. A call to pmccntr_op_finish should follow every call to
1327 * pmccntr_op_start.
1328 */
f2b2f53f 1329static void pmccntr_op_finish(CPUARMState *env)
5d05b9d4 1330{
033614c4 1331 if (pmu_counter_enabled(env, 31)) {
4e7beb0c
AL
1332#ifndef CONFIG_USER_ONLY
1333 /* Calculate when the counter will next overflow */
1334 uint64_t remaining_cycles = -env->cp15.c15_ccnt;
1335 if (!(env->cp15.c9_pmcr & PMCRLC)) {
1336 remaining_cycles = (uint32_t)remaining_cycles;
1337 }
1338 int64_t overflow_in = cycles_ns_per(remaining_cycles);
1339
1340 if (overflow_in > 0) {
f1dd2506
PM
1341 int64_t overflow_at;
1342
1343 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1344 overflow_in, &overflow_at)) {
1345 ARMCPU *cpu = env_archcpu(env);
1346 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1347 }
4e7beb0c
AL
1348 }
1349#endif
5d05b9d4 1350
4e7beb0c 1351 uint64_t prev_cycles = env->cp15.c15_ccnt_delta;
b57aa7bd 1352 if (pmccntr_clockdiv_enabled(env)) {
5d05b9d4
AL
1353 prev_cycles /= 64;
1354 }
5d05b9d4 1355 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt;
ec7b4ce4
AF
1356 }
1357}
1358
5ecdd3e4
AL
1359static void pmevcntr_op_start(CPUARMState *env, uint8_t counter)
1360{
1361
1362 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1363 uint64_t count = 0;
1364 if (event_supported(event)) {
1365 uint16_t event_idx = supported_event_map[event];
1366 count = pm_events[event_idx].get_count(env);
1367 }
1368
1369 if (pmu_counter_enabled(env, counter)) {
47b385da
PM
1370 uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter];
1371 uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ?
1372 1ULL << 63 : 1ULL << 31;
f4efb4b2 1373
47b385da 1374 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) {
f4efb4b2
AL
1375 env->cp15.c9_pmovsr |= (1 << counter);
1376 pmu_update_irq(env);
1377 }
1378 env->cp15.c14_pmevcntr[counter] = new_pmevcntr;
5ecdd3e4
AL
1379 }
1380 env->cp15.c14_pmevcntr_delta[counter] = count;
1381}
1382
1383static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter)
1384{
1385 if (pmu_counter_enabled(env, counter)) {
4e7beb0c
AL
1386#ifndef CONFIG_USER_ONLY
1387 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT;
1388 uint16_t event_idx = supported_event_map[event];
47b385da
PM
1389 uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1);
1390 int64_t overflow_in;
1391
1392 if (!pmevcntr_is_64_bit(env, counter)) {
1393 delta = (uint32_t)delta;
1394 }
1395 overflow_in = pm_events[event_idx].ns_per_count(delta);
4e7beb0c
AL
1396
1397 if (overflow_in > 0) {
f1dd2506
PM
1398 int64_t overflow_at;
1399
1400 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
1401 overflow_in, &overflow_at)) {
1402 ARMCPU *cpu = env_archcpu(env);
1403 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at);
1404 }
4e7beb0c
AL
1405 }
1406#endif
1407
5ecdd3e4
AL
1408 env->cp15.c14_pmevcntr_delta[counter] -=
1409 env->cp15.c14_pmevcntr[counter];
1410 }
1411}
1412
5d05b9d4
AL
1413void pmu_op_start(CPUARMState *env)
1414{
5ecdd3e4 1415 unsigned int i;
5d05b9d4 1416 pmccntr_op_start(env);
5ecdd3e4
AL
1417 for (i = 0; i < pmu_num_counters(env); i++) {
1418 pmevcntr_op_start(env, i);
1419 }
5d05b9d4
AL
1420}
1421
1422void pmu_op_finish(CPUARMState *env)
1423{
5ecdd3e4 1424 unsigned int i;
5d05b9d4 1425 pmccntr_op_finish(env);
5ecdd3e4
AL
1426 for (i = 0; i < pmu_num_counters(env); i++) {
1427 pmevcntr_op_finish(env, i);
1428 }
5d05b9d4
AL
1429}
1430
033614c4
AL
1431void pmu_pre_el_change(ARMCPU *cpu, void *ignored)
1432{
1433 pmu_op_start(&cpu->env);
1434}
1435
1436void pmu_post_el_change(ARMCPU *cpu, void *ignored)
1437{
1438 pmu_op_finish(&cpu->env);
1439}
1440
4e7beb0c
AL
1441void arm_pmu_timer_cb(void *opaque)
1442{
1443 ARMCPU *cpu = opaque;
1444
1445 /*
1446 * Update all the counter values based on the current underlying counts,
1447 * triggering interrupts to be raised, if necessary. pmu_op_finish() also
1448 * has the effect of setting the cpu->pmu_timer to the next earliest time a
1449 * counter may expire.
1450 */
1451 pmu_op_start(&cpu->env);
1452 pmu_op_finish(&cpu->env);
1453}
1454
c4241c7d
PM
1455static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1456 uint64_t value)
200ac0ef 1457{
5d05b9d4 1458 pmu_op_start(env);
7c2cb42b
AF
1459
1460 if (value & PMCRC) {
1461 /* The counter has been reset */
1462 env->cp15.c15_ccnt = 0;
1463 }
1464
5ecdd3e4
AL
1465 if (value & PMCRP) {
1466 unsigned int i;
1467 for (i = 0; i < pmu_num_counters(env); i++) {
1468 env->cp15.c14_pmevcntr[i] = 0;
1469 }
1470 }
1471
9323e79f
PM
1472 env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK;
1473 env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK);
7c2cb42b 1474
5d05b9d4 1475 pmu_op_finish(env);
7c2cb42b
AF
1476}
1477
0d4bfd7d
AL
1478static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri,
1479 uint64_t value)
1480{
1481 unsigned int i;
47b385da
PM
1482 uint64_t overflow_mask, new_pmswinc;
1483
0d4bfd7d
AL
1484 for (i = 0; i < pmu_num_counters(env); i++) {
1485 /* Increment a counter's count iff: */
1486 if ((value & (1 << i)) && /* counter's bit is set */
1487 /* counter is enabled and not filtered */
1488 pmu_counter_enabled(env, i) &&
1489 /* counter is SW_INCR */
1490 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) {
1491 pmevcntr_op_start(env, i);
f4efb4b2
AL
1492
1493 /*
1494 * Detect if this write causes an overflow since we can't predict
1495 * PMSWINC overflows like we can for other events
1496 */
47b385da
PM
1497 new_pmswinc = env->cp15.c14_pmevcntr[i] + 1;
1498
1499 overflow_mask = pmevcntr_is_64_bit(env, i) ?
1500 1ULL << 63 : 1ULL << 31;
f4efb4b2 1501
47b385da 1502 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) {
f4efb4b2
AL
1503 env->cp15.c9_pmovsr |= (1 << i);
1504 pmu_update_irq(env);
1505 }
1506
1507 env->cp15.c14_pmevcntr[i] = new_pmswinc;
1508
0d4bfd7d
AL
1509 pmevcntr_op_finish(env, i);
1510 }
1511 }
1512}
1513
7c2cb42b
AF
1514static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1515{
5d05b9d4
AL
1516 uint64_t ret;
1517 pmccntr_op_start(env);
1518 ret = env->cp15.c15_ccnt;
1519 pmccntr_op_finish(env);
1520 return ret;
7c2cb42b
AF
1521}
1522
6b040780
WH
1523static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1524 uint64_t value)
1525{
9b37a28c
FR
1526 /*
1527 * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and
6b040780
WH
1528 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the
1529 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are
1530 * accessed.
1531 */
1532 env->cp15.c9_pmselr = value & 0x1f;
1533}
1534
7c2cb42b
AF
1535static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1536 uint64_t value)
1537{
5d05b9d4
AL
1538 pmccntr_op_start(env);
1539 env->cp15.c15_ccnt = value;
1540 pmccntr_op_finish(env);
200ac0ef 1541}
421c7ebd
PC
1542
1543static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
1544 uint64_t value)
1545{
1546 uint64_t cur_val = pmccntr_read(env, NULL);
1547
1548 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
1549}
1550
0614601c
AF
1551static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1552 uint64_t value)
1553{
5d05b9d4 1554 pmccntr_op_start(env);
4b8afa1f
AL
1555 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0;
1556 pmccntr_op_finish(env);
1557}
1558
1559static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri,
1560 uint64_t value)
1561{
1562 pmccntr_op_start(env);
1563 /* M is not accessible from AArch32 */
1564 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) |
1565 (value & PMCCFILTR);
5d05b9d4 1566 pmccntr_op_finish(env);
0614601c
AF
1567}
1568
4b8afa1f
AL
1569static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri)
1570{
1571 /* M is not visible in AArch32 */
1572 return env->cp15.pmccfiltr_el0 & PMCCFILTR;
1573}
1574
c4241c7d 1575static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1576 uint64_t value)
1577{
01765386 1578 pmu_op_start(env);
7ece99b1 1579 value &= pmu_counter_mask(env);
200ac0ef 1580 env->cp15.c9_pmcnten |= value;
01765386 1581 pmu_op_finish(env);
200ac0ef
PM
1582}
1583
c4241c7d
PM
1584static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1585 uint64_t value)
200ac0ef 1586{
01765386 1587 pmu_op_start(env);
7ece99b1 1588 value &= pmu_counter_mask(env);
200ac0ef 1589 env->cp15.c9_pmcnten &= ~value;
01765386 1590 pmu_op_finish(env);
200ac0ef
PM
1591}
1592
c4241c7d
PM
1593static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1594 uint64_t value)
200ac0ef 1595{
599b71e2 1596 value &= pmu_counter_mask(env);
200ac0ef 1597 env->cp15.c9_pmovsr &= ~value;
f4efb4b2 1598 pmu_update_irq(env);
200ac0ef
PM
1599}
1600
327dd510
AL
1601static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1602 uint64_t value)
1603{
1604 value &= pmu_counter_mask(env);
1605 env->cp15.c9_pmovsr |= value;
f4efb4b2 1606 pmu_update_irq(env);
327dd510
AL
1607}
1608
5ecdd3e4
AL
1609static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1610 uint64_t value, const uint8_t counter)
200ac0ef 1611{
5ecdd3e4
AL
1612 if (counter == 31) {
1613 pmccfiltr_write(env, ri, value);
1614 } else if (counter < pmu_num_counters(env)) {
1615 pmevcntr_op_start(env, counter);
1616
1617 /*
1618 * If this counter's event type is changing, store the current
1619 * underlying count for the new type in c14_pmevcntr_delta[counter] so
1620 * pmevcntr_op_finish has the correct baseline when it converts back to
1621 * a delta.
1622 */
1623 uint16_t old_event = env->cp15.c14_pmevtyper[counter] &
1624 PMXEVTYPER_EVTCOUNT;
1625 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT;
1626 if (old_event != new_event) {
1627 uint64_t count = 0;
1628 if (event_supported(new_event)) {
1629 uint16_t event_idx = supported_event_map[new_event];
1630 count = pm_events[event_idx].get_count(env);
1631 }
1632 env->cp15.c14_pmevcntr_delta[counter] = count;
1633 }
1634
1635 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK;
1636 pmevcntr_op_finish(env, counter);
1637 }
9b37a28c
FR
1638 /*
1639 * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when
fdb86656
WH
1640 * PMSELR value is equal to or greater than the number of implemented
1641 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI.
1642 */
5ecdd3e4
AL
1643}
1644
1645static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri,
1646 const uint8_t counter)
1647{
1648 if (counter == 31) {
1649 return env->cp15.pmccfiltr_el0;
1650 } else if (counter < pmu_num_counters(env)) {
1651 return env->cp15.c14_pmevtyper[counter];
1652 } else {
1653 /*
1654 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER
1655 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write().
1656 */
1657 return 0;
1658 }
1659}
1660
1661static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1662 uint64_t value)
1663{
1664 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1665 pmevtyper_write(env, ri, value, counter);
1666}
1667
1668static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1669 uint64_t value)
1670{
1671 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1672 env->cp15.c14_pmevtyper[counter] = value;
1673
1674 /*
1675 * pmevtyper_rawwrite is called between a pair of pmu_op_start and
1676 * pmu_op_finish calls when loading saved state for a migration. Because
1677 * we're potentially updating the type of event here, the value written to
1678 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a
1679 * different counter type. Therefore, we need to set this value to the
1680 * current count for the counter type we're writing so that pmu_op_finish
1681 * has the correct count for its calculation.
1682 */
1683 uint16_t event = value & PMXEVTYPER_EVTCOUNT;
1684 if (event_supported(event)) {
1685 uint16_t event_idx = supported_event_map[event];
1686 env->cp15.c14_pmevcntr_delta[counter] =
1687 pm_events[event_idx].get_count(env);
fdb86656
WH
1688 }
1689}
1690
5ecdd3e4
AL
1691static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1692{
1693 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1694 return pmevtyper_read(env, ri, counter);
1695}
1696
1697static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
1698 uint64_t value)
1699{
1700 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31);
1701}
1702
fdb86656
WH
1703static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri)
1704{
5ecdd3e4
AL
1705 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31);
1706}
1707
1708static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1709 uint64_t value, uint8_t counter)
1710{
47b385da
PM
1711 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1712 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1713 value &= MAKE_64BIT_MASK(0, 32);
1714 }
5ecdd3e4
AL
1715 if (counter < pmu_num_counters(env)) {
1716 pmevcntr_op_start(env, counter);
1717 env->cp15.c14_pmevcntr[counter] = value;
1718 pmevcntr_op_finish(env, counter);
1719 }
1720 /*
1721 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1722 * are CONSTRAINED UNPREDICTABLE.
fdb86656 1723 */
5ecdd3e4
AL
1724}
1725
1726static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri,
1727 uint8_t counter)
1728{
1729 if (counter < pmu_num_counters(env)) {
1730 uint64_t ret;
1731 pmevcntr_op_start(env, counter);
1732 ret = env->cp15.c14_pmevcntr[counter];
1733 pmevcntr_op_finish(env, counter);
47b385da
PM
1734 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) {
1735 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */
1736 ret &= MAKE_64BIT_MASK(0, 32);
1737 }
5ecdd3e4 1738 return ret;
fdb86656 1739 } else {
9b37a28c
FR
1740 /*
1741 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR
1742 * are CONSTRAINED UNPREDICTABLE.
1743 */
fdb86656
WH
1744 return 0;
1745 }
200ac0ef
PM
1746}
1747
5ecdd3e4
AL
1748static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri,
1749 uint64_t value)
1750{
1751 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1752 pmevcntr_write(env, ri, value, counter);
1753}
1754
1755static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
1756{
1757 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1758 return pmevcntr_read(env, ri, counter);
1759}
1760
1761static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri,
1762 uint64_t value)
1763{
1764 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1765 assert(counter < pmu_num_counters(env));
1766 env->cp15.c14_pmevcntr[counter] = value;
1767 pmevcntr_write(env, ri, value, counter);
1768}
1769
1770static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri)
1771{
1772 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7);
1773 assert(counter < pmu_num_counters(env));
1774 return env->cp15.c14_pmevcntr[counter];
1775}
1776
1777static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1778 uint64_t value)
1779{
1780 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31);
1781}
1782
1783static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1784{
1785 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31);
1786}
1787
c4241c7d 1788static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
200ac0ef
PM
1789 uint64_t value)
1790{
6ecd0b6b
AB
1791 if (arm_feature(env, ARM_FEATURE_V8)) {
1792 env->cp15.c9_pmuserenr = value & 0xf;
1793 } else {
1794 env->cp15.c9_pmuserenr = value & 1;
1795 }
200ac0ef
PM
1796}
1797
c4241c7d
PM
1798static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
1799 uint64_t value)
200ac0ef
PM
1800{
1801 /* We have no event counters so only the C bit can be changed */
7ece99b1 1802 value &= pmu_counter_mask(env);
200ac0ef 1803 env->cp15.c9_pminten |= value;
f4efb4b2 1804 pmu_update_irq(env);
200ac0ef
PM
1805}
1806
c4241c7d
PM
1807static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1808 uint64_t value)
200ac0ef 1809{
7ece99b1 1810 value &= pmu_counter_mask(env);
200ac0ef 1811 env->cp15.c9_pminten &= ~value;
f4efb4b2 1812 pmu_update_irq(env);
200ac0ef
PM
1813}
1814
c4241c7d
PM
1815static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
1816 uint64_t value)
8641136c 1817{
9b37a28c
FR
1818 /*
1819 * Note that even though the AArch64 view of this register has bits
a505d7fe
PM
1820 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
1821 * architectural requirements for bits which are RES0 only in some
1822 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
1823 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
1824 */
855ea66d 1825 raw_write(env, ri, value & ~0x1FULL);
8641136c
NR
1826}
1827
64e0e2de
EI
1828static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1829{
ea22747c 1830 /* Begin with base v8.0 state. */
06f2adcc 1831 uint64_t valid_mask = 0x3fff;
2fc0cc0e 1832 ARMCPU *cpu = env_archcpu(env);
d902ae75 1833 uint64_t changed;
ea22747c 1834
bfe43e3d
RH
1835 /*
1836 * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always
1837 * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64.
1838 * Instead, choose the format based on the mode of EL3.
1839 */
1840 if (arm_el_is_aa64(env, 3)) {
1841 value |= SCR_FW | SCR_AW; /* RES1 */
1842 valid_mask &= ~SCR_NET; /* RES0 */
252e8c69 1843
6bcbb07a
RH
1844 if (!cpu_isar_feature(aa64_aa32_el1, cpu) &&
1845 !cpu_isar_feature(aa64_aa32_el2, cpu)) {
1846 value |= SCR_RW; /* RAO/WI */
1847 }
da3d8b13
RH
1848 if (cpu_isar_feature(aa64_ras, cpu)) {
1849 valid_mask |= SCR_TERR;
1850 }
252e8c69
RH
1851 if (cpu_isar_feature(aa64_lor, cpu)) {
1852 valid_mask |= SCR_TLOR;
1853 }
1854 if (cpu_isar_feature(aa64_pauth, cpu)) {
1855 valid_mask |= SCR_API | SCR_APK;
1856 }
926c1b97
RDC
1857 if (cpu_isar_feature(aa64_sel2, cpu)) {
1858 valid_mask |= SCR_EEL2;
1859 }
8ddb300b
RH
1860 if (cpu_isar_feature(aa64_mte, cpu)) {
1861 valid_mask |= SCR_ATA;
1862 }
7cb1e618
RH
1863 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
1864 valid_mask |= SCR_ENSCXT;
1865 }
7ac61020
PM
1866 if (cpu_isar_feature(aa64_doublefault, cpu)) {
1867 valid_mask |= SCR_EASE | SCR_NMEA;
1868 }
06f2adcc
JF
1869 if (cpu_isar_feature(aa64_sme, cpu)) {
1870 valid_mask |= SCR_ENTP2;
1871 }
08899b5c
EI
1872 if (cpu_isar_feature(aa64_hcx, cpu)) {
1873 valid_mask |= SCR_HXEN;
1874 }
15126d9c
PM
1875 if (cpu_isar_feature(aa64_fgt, cpu)) {
1876 valid_mask |= SCR_FGTEN;
1877 }
ea22747c
RH
1878 } else {
1879 valid_mask &= ~(SCR_RW | SCR_ST);
da3d8b13
RH
1880 if (cpu_isar_feature(aa32_ras, cpu)) {
1881 valid_mask |= SCR_TERR;
1882 }
ea22747c 1883 }
64e0e2de
EI
1884
1885 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1886 valid_mask &= ~SCR_HCE;
1887
9b37a28c
FR
1888 /*
1889 * On ARMv7, SMD (or SCD as it is called in v7) is only
64e0e2de
EI
1890 * supported if EL2 exists. The bit is UNK/SBZP when
1891 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
1892 * when EL2 is unavailable.
4eb27640 1893 * On ARMv8, this bit is always available.
64e0e2de 1894 */
4eb27640
GB
1895 if (arm_feature(env, ARM_FEATURE_V7) &&
1896 !arm_feature(env, ARM_FEATURE_V8)) {
64e0e2de
EI
1897 valid_mask &= ~SCR_SMD;
1898 }
1899 }
1900
1901 /* Clear all-context RES0 bits. */
1902 value &= valid_mask;
d902ae75
RH
1903 changed = env->cp15.scr_el3 ^ value;
1904 env->cp15.scr_el3 = value;
1905
1906 /*
1907 * If SCR_EL3.NS changes, i.e. arm_is_secure_below_el3, then
1908 * we must invalidate all TLBs below EL3.
1909 */
1910 if (changed & SCR_NS) {
1911 tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 |
1912 ARMMMUIdxBit_E20_0 |
1913 ARMMMUIdxBit_E10_1 |
1914 ARMMMUIdxBit_E20_2 |
1915 ARMMMUIdxBit_E10_1_PAN |
1916 ARMMMUIdxBit_E20_2_PAN |
1917 ARMMMUIdxBit_E2));
1918 }
64e0e2de
EI
1919}
1920
10d0ef3e
MN
1921static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1922{
1923 /*
1924 * scr_write will set the RES1 bits on an AArch64-only CPU.
1925 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise.
1926 */
1927 scr_write(env, ri, 0);
1928}
1929
e2ce5fcd
PM
1930static CPAccessResult access_tid4(CPUARMState *env,
1931 const ARMCPRegInfo *ri,
1932 bool isread)
630fcd4d 1933{
e2ce5fcd
PM
1934 if (arm_current_el(env) == 1 &&
1935 (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) {
630fcd4d
MZ
1936 return CP_ACCESS_TRAP_EL2;
1937 }
1938
1939 return CP_ACCESS_OK;
1940}
1941
c4241c7d 1942static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
776d4e5c 1943{
2fc0cc0e 1944 ARMCPU *cpu = env_archcpu(env);
b85a1fd6 1945
9b37a28c
FR
1946 /*
1947 * Acquire the CSSELR index from the bank corresponding to the CCSIDR
b85a1fd6
FA
1948 * bank
1949 */
1950 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1951 ri->secure & ARM_CP_SECSTATE_S);
1952
1953 return cpu->ccsidr[index];
776d4e5c
PM
1954}
1955
c4241c7d
PM
1956static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1957 uint64_t value)
776d4e5c 1958{
8d5c773e 1959 raw_write(env, ri, value & 0xf);
776d4e5c
PM
1960}
1961
1090b9c6
PM
1962static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1963{
29a0af61 1964 CPUState *cs = env_cpu(env);
cc974d5c
RDC
1965 bool el1 = arm_current_el(env) == 1;
1966 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0;
1090b9c6
PM
1967 uint64_t ret = 0;
1968
cc974d5c 1969 if (hcr_el2 & HCR_IMO) {
636540e9
PM
1970 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) {
1971 ret |= CPSR_I;
1972 }
1973 } else {
1974 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1975 ret |= CPSR_I;
1976 }
1090b9c6 1977 }
636540e9 1978
cc974d5c 1979 if (hcr_el2 & HCR_FMO) {
636540e9
PM
1980 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) {
1981 ret |= CPSR_F;
1982 }
1983 } else {
1984 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1985 ret |= CPSR_F;
1986 }
1090b9c6 1987 }
636540e9 1988
3c29632f
RH
1989 if (hcr_el2 & HCR_AMO) {
1990 if (cs->interrupt_request & CPU_INTERRUPT_VSERR) {
1991 ret |= CPSR_A;
1992 }
1993 }
1994
1090b9c6
PM
1995 return ret;
1996}
1997
93fbc983
MZ
1998static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
1999 bool isread)
2000{
2001 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) {
2002 return CP_ACCESS_TRAP_EL2;
2003 }
2004
2005 return CP_ACCESS_OK;
2006}
2007
2008static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri,
2009 bool isread)
2010{
2011 if (arm_feature(env, ARM_FEATURE_V8)) {
2012 return access_aa64_tid1(env, ri, isread);
2013 }
2014
2015 return CP_ACCESS_OK;
2016}
2017
e9aa6c21 2018static const ARMCPRegInfo v7_cp_reginfo[] = {
7d57f408
PM
2019 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
2020 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
2021 .access = PL1_W, .type = ARM_CP_NOP },
9b37a28c
FR
2022 /*
2023 * Performance monitors are implementation defined in v7,
200ac0ef 2024 * but with an ARM recommended set of registers, which we
ac689a2e 2025 * follow.
200ac0ef
PM
2026 *
2027 * Performance registers fall into three categories:
2028 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
2029 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
2030 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
2031 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
2032 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
2033 */
2034 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
7f4fbfb5 2035 .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO,
8521466b 2036 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206
PM
2037 .writefn = pmcntenset_write,
2038 .accessfn = pmreg_access,
dc780233 2039 .fgt = FGT_PMCNTEN,
fcd25206 2040 .raw_writefn = raw_write },
7f4fbfb5 2041 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO,
8521466b
AF
2042 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
2043 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2044 .fgt = FGT_PMCNTEN,
8521466b
AF
2045 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
2046 .writefn = pmcntenset_write, .raw_writefn = raw_write },
200ac0ef 2047 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
8521466b
AF
2048 .access = PL0_RW,
2049 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
fcd25206 2050 .accessfn = pmreg_access,
dc780233 2051 .fgt = FGT_PMCNTEN,
fcd25206 2052 .writefn = pmcntenclr_write,
7f4fbfb5 2053 .type = ARM_CP_ALIAS | ARM_CP_IO },
8521466b
AF
2054 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
2055 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
2056 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2057 .fgt = FGT_PMCNTEN,
7f4fbfb5 2058 .type = ARM_CP_ALIAS | ARM_CP_IO,
8521466b
AF
2059 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
2060 .writefn = pmcntenclr_write },
200ac0ef 2061 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
f4efb4b2 2062 .access = PL0_RW, .type = ARM_CP_IO,
e4e91a21 2063 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
fcd25206 2064 .accessfn = pmreg_access,
dc780233 2065 .fgt = FGT_PMOVS,
fcd25206
PM
2066 .writefn = pmovsr_write,
2067 .raw_writefn = raw_write },
978364f1
AF
2068 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
2069 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
2070 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2071 .fgt = FGT_PMOVS,
f4efb4b2 2072 .type = ARM_CP_ALIAS | ARM_CP_IO,
978364f1
AF
2073 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2074 .writefn = pmovsr_write,
2075 .raw_writefn = raw_write },
200ac0ef 2076 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
f4efb4b2 2077 .access = PL0_W, .accessfn = pmreg_access_swinc,
dc780233 2078 .fgt = FGT_PMSWINC_EL0,
f4efb4b2 2079 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d
AL
2080 .writefn = pmswinc_write },
2081 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64,
2082 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4,
f4efb4b2 2083 .access = PL0_W, .accessfn = pmreg_access_swinc,
dc780233 2084 .fgt = FGT_PMSWINC_EL0,
f4efb4b2 2085 .type = ARM_CP_NO_RAW | ARM_CP_IO,
0d4bfd7d 2086 .writefn = pmswinc_write },
6b040780
WH
2087 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
2088 .access = PL0_RW, .type = ARM_CP_ALIAS,
dc780233 2089 .fgt = FGT_PMSELR_EL0,
6b040780 2090 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr),
6ecd0b6b 2091 .accessfn = pmreg_access_selr, .writefn = pmselr_write,
6b040780
WH
2092 .raw_writefn = raw_write},
2093 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64,
2094 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5,
6ecd0b6b 2095 .access = PL0_RW, .accessfn = pmreg_access_selr,
dc780233 2096 .fgt = FGT_PMSELR_EL0,
6b040780
WH
2097 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr),
2098 .writefn = pmselr_write, .raw_writefn = raw_write, },
200ac0ef 2099 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
169c8938 2100 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO,
dc780233 2101 .fgt = FGT_PMCCNTR_EL0,
421c7ebd 2102 .readfn = pmccntr_read, .writefn = pmccntr_write32,
6ecd0b6b 2103 .accessfn = pmreg_access_ccntr },
8521466b
AF
2104 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
2105 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
6ecd0b6b 2106 .access = PL0_RW, .accessfn = pmreg_access_ccntr,
dc780233 2107 .fgt = FGT_PMCCNTR_EL0,
8521466b 2108 .type = ARM_CP_IO,
980ebe87
AL
2109 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt),
2110 .readfn = pmccntr_read, .writefn = pmccntr_write,
2111 .raw_readfn = raw_read, .raw_writefn = raw_write, },
4b8afa1f
AL
2112 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7,
2113 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32,
2114 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2115 .fgt = FGT_PMCCFILTR_EL0,
4b8afa1f
AL
2116 .type = ARM_CP_ALIAS | ARM_CP_IO,
2117 .resetvalue = 0, },
8521466b
AF
2118 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
2119 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
980ebe87 2120 .writefn = pmccfiltr_write, .raw_writefn = raw_write,
8521466b 2121 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2122 .fgt = FGT_PMCCFILTR_EL0,
8521466b
AF
2123 .type = ARM_CP_IO,
2124 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
2125 .resetvalue = 0, },
200ac0ef 2126 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
5ecdd3e4
AL
2127 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2128 .accessfn = pmreg_access,
dc780233 2129 .fgt = FGT_PMEVTYPERN_EL0,
fdb86656
WH
2130 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
2131 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64,
2132 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1,
5ecdd3e4
AL
2133 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2134 .accessfn = pmreg_access,
dc780233 2135 .fgt = FGT_PMEVTYPERN_EL0,
fdb86656 2136 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read },
200ac0ef 2137 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
5ecdd3e4
AL
2138 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2139 .accessfn = pmreg_access_xevcntr,
dc780233 2140 .fgt = FGT_PMEVCNTRN_EL0,
5ecdd3e4
AL
2141 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
2142 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64,
2143 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2,
2144 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO,
2145 .accessfn = pmreg_access_xevcntr,
dc780233 2146 .fgt = FGT_PMEVCNTRN_EL0,
5ecdd3e4 2147 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read },
200ac0ef 2148 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
1fce1ba9 2149 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
e4e91a21 2150 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr),
200ac0ef 2151 .resetvalue = 0,
d4e6df63 2152 .writefn = pmuserenr_write, .raw_writefn = raw_write },
8a83ffc2
AF
2153 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
2154 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
1fce1ba9 2155 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
8a83ffc2
AF
2156 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
2157 .resetvalue = 0,
2158 .writefn = pmuserenr_write, .raw_writefn = raw_write },
200ac0ef 2159 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
1fce1ba9 2160 .access = PL1_RW, .accessfn = access_tpm,
dc780233 2161 .fgt = FGT_PMINTEN,
b7d793ad 2162 .type = ARM_CP_ALIAS | ARM_CP_IO,
e6ec5457 2163 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten),
200ac0ef 2164 .resetvalue = 0,
d4e6df63 2165 .writefn = pmintenset_write, .raw_writefn = raw_write },
e6ec5457
WH
2166 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64,
2167 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1,
2168 .access = PL1_RW, .accessfn = access_tpm,
dc780233 2169 .fgt = FGT_PMINTEN,
e6ec5457
WH
2170 .type = ARM_CP_IO,
2171 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2172 .writefn = pmintenset_write, .raw_writefn = raw_write,
2173 .resetvalue = 0x0 },
200ac0ef 2174 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
fc5f6856 2175 .access = PL1_RW, .accessfn = access_tpm,
dc780233 2176 .fgt = FGT_PMINTEN,
887c0f15 2177 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
200ac0ef 2178 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
b061a82b 2179 .writefn = pmintenclr_write, },
978364f1
AF
2180 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
2181 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
fc5f6856 2182 .access = PL1_RW, .accessfn = access_tpm,
dc780233 2183 .fgt = FGT_PMINTEN,
887c0f15 2184 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW,
978364f1
AF
2185 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
2186 .writefn = pmintenclr_write },
7da845b0
PM
2187 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
2188 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
630fcd4d 2189 .access = PL1_R,
e2ce5fcd 2190 .accessfn = access_tid4,
158c276c 2191 .fgt = FGT_CCSIDR_EL1,
630fcd4d 2192 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
7da845b0
PM
2193 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
2194 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
630fcd4d 2195 .access = PL1_RW,
e2ce5fcd 2196 .accessfn = access_tid4,
b19ed03c 2197 .fgt = FGT_CSSELR_EL1,
630fcd4d 2198 .writefn = csselr_write, .resetvalue = 0,
b85a1fd6
FA
2199 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
2200 offsetof(CPUARMState, cp15.csselr_ns) } },
9b37a28c
FR
2201 /*
2202 * Auxiliary ID register: this actually has an IMPDEF value but for now
776d4e5c
PM
2203 * just RAZ for all cores:
2204 */
0ff644a7
PM
2205 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
2206 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
93fbc983
MZ
2207 .access = PL1_R, .type = ARM_CP_CONST,
2208 .accessfn = access_aa64_tid1,
158c276c 2209 .fgt = FGT_AIDR_EL1,
93fbc983 2210 .resetvalue = 0 },
9b37a28c
FR
2211 /*
2212 * Auxiliary fault status registers: these also are IMPDEF, and we
f32cdad5
PM
2213 * choose to RAZ/WI for all cores.
2214 */
2215 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
2216 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
84929218 2217 .access = PL1_RW, .accessfn = access_tvm_trvm,
158c276c 2218 .fgt = FGT_AFSR0_EL1,
84929218 2219 .type = ARM_CP_CONST, .resetvalue = 0 },
f32cdad5
PM
2220 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
2221 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
84929218 2222 .access = PL1_RW, .accessfn = access_tvm_trvm,
158c276c 2223 .fgt = FGT_AFSR1_EL1,
84929218 2224 .type = ARM_CP_CONST, .resetvalue = 0 },
9b37a28c
FR
2225 /*
2226 * MAIR can just read-as-written because we don't implement caches
b0fe2427
PM
2227 * and so don't need to care about memory attributes.
2228 */
2229 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
2230 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
84929218 2231 .access = PL1_RW, .accessfn = access_tvm_trvm,
67dd8030 2232 .fgt = FGT_MAIR_EL1,
84929218 2233 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
b0fe2427 2234 .resetvalue = 0 },
4cfb8ad8
PM
2235 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
2236 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
2237 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
2238 .resetvalue = 0 },
9b37a28c
FR
2239 /*
2240 * For non-long-descriptor page tables these are PRRR and NMRR;
b0fe2427 2241 * regardless they still act as reads-as-written for QEMU.
b0fe2427 2242 */
9b37a28c
FR
2243 /*
2244 * MAIR0/1 are defined separately from their 64-bit counterpart which
be693c87
GB
2245 * allows them to assign the correct fieldoffset based on the endianness
2246 * handled in the field definitions.
2247 */
a903c449 2248 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
84929218
RH
2249 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
2250 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2251 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
2252 offsetof(CPUARMState, cp15.mair0_ns) },
b0fe2427 2253 .resetfn = arm_cp_reset_ignore },
a903c449 2254 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
84929218
RH
2255 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
2256 .access = PL1_RW, .accessfn = access_tvm_trvm,
be693c87
GB
2257 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
2258 offsetof(CPUARMState, cp15.mair1_ns) },
b0fe2427 2259 .resetfn = arm_cp_reset_ignore },
1090b9c6
PM
2260 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
2261 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
b19ed03c 2262 .fgt = FGT_ISR_EL1,
7a0e58fa 2263 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
995939a6
PM
2264 /* 32 bit ITLB invalidates */
2265 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
30881b73
RH
2266 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2267 .writefn = tlbiall_write },
995939a6 2268 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
30881b73
RH
2269 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2270 .writefn = tlbimva_write },
995939a6 2271 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
30881b73
RH
2272 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2273 .writefn = tlbiasid_write },
995939a6
PM
2274 /* 32 bit DTLB invalidates */
2275 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
30881b73
RH
2276 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2277 .writefn = tlbiall_write },
995939a6 2278 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
30881b73
RH
2279 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2280 .writefn = tlbimva_write },
995939a6 2281 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
30881b73
RH
2282 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2283 .writefn = tlbiasid_write },
995939a6
PM
2284 /* 32 bit TLB invalidates */
2285 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73
RH
2286 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2287 .writefn = tlbiall_write },
995939a6 2288 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73
RH
2289 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2290 .writefn = tlbimva_write },
995939a6 2291 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73
RH
2292 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2293 .writefn = tlbiasid_write },
995939a6 2294 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73
RH
2295 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
2296 .writefn = tlbimvaa_write },
995939a6
PM
2297};
2298
2299static const ARMCPRegInfo v7mp_cp_reginfo[] = {
2300 /* 32 bit TLB invalidates, Inner Shareable */
2301 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
0f66d223 2302 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
30881b73 2303 .writefn = tlbiall_is_write },
995939a6 2304 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
0f66d223 2305 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
30881b73 2306 .writefn = tlbimva_is_write },
995939a6 2307 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
0f66d223 2308 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
fa439fc5 2309 .writefn = tlbiasid_is_write },
995939a6 2310 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
0f66d223 2311 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
fa439fc5 2312 .writefn = tlbimvaa_is_write },
e9aa6c21
PM
2313};
2314
327dd510
AL
2315static const ARMCPRegInfo pmovsset_cp_reginfo[] = {
2316 /* PMOVSSET is not implemented in v7 before v7ve */
2317 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3,
2318 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2319 .fgt = FGT_PMOVS,
f4efb4b2 2320 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2321 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr),
2322 .writefn = pmovsset_write,
2323 .raw_writefn = raw_write },
2324 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64,
2325 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3,
2326 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 2327 .fgt = FGT_PMOVS,
f4efb4b2 2328 .type = ARM_CP_ALIAS | ARM_CP_IO,
327dd510
AL
2329 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
2330 .writefn = pmovsset_write,
2331 .raw_writefn = raw_write },
327dd510
AL
2332};
2333
c4241c7d
PM
2334static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2335 uint64_t value)
c326b979
PM
2336{
2337 value &= 1;
2338 env->teecr = value;
c326b979
PM
2339}
2340
cc7613bf
PM
2341static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2342 bool isread)
2343{
2344 /*
2345 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE
2346 * at all, so we don't need to check whether we're v8A.
2347 */
2348 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
2349 (env->cp15.hstr_el2 & HSTR_TTEE)) {
2350 return CP_ACCESS_TRAP_EL2;
2351 }
2352 return CP_ACCESS_OK;
2353}
2354
3f208fd7
PM
2355static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
2356 bool isread)
c326b979 2357{
dcbff19b 2358 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
92611c00 2359 return CP_ACCESS_TRAP;
c326b979 2360 }
cc7613bf 2361 return teecr_access(env, ri, isread);
c326b979
PM
2362}
2363
2364static const ARMCPRegInfo t2ee_cp_reginfo[] = {
2365 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
2366 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
2367 .resetvalue = 0,
cc7613bf 2368 .writefn = teecr_write, .accessfn = teecr_access },
c326b979
PM
2369 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
2370 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
92611c00 2371 .accessfn = teehbr_access, .resetvalue = 0 },
c326b979
PM
2372};
2373
4d31c596 2374static const ARMCPRegInfo v6k_cp_reginfo[] = {
e4fe830b
PM
2375 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
2376 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
2377 .access = PL0_RW,
67dd8030 2378 .fgt = FGT_TPIDR_EL0,
54bf36ed 2379 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
4d31c596
PM
2380 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
2381 .access = PL0_RW,
67dd8030 2382 .fgt = FGT_TPIDR_EL0,
54bf36ed
FA
2383 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
2384 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
e4fe830b
PM
2385 .resetfn = arm_cp_reset_ignore },
2386 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
2387 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
04215eb1 2388 .access = PL0_R | PL1_W,
67dd8030 2389 .fgt = FGT_TPIDRRO_EL0,
54bf36ed
FA
2390 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
2391 .resetvalue = 0},
4d31c596 2392 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
04215eb1 2393 .access = PL0_R | PL1_W,
67dd8030 2394 .fgt = FGT_TPIDRRO_EL0,
54bf36ed
FA
2395 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
2396 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
e4fe830b 2397 .resetfn = arm_cp_reset_ignore },
54bf36ed 2398 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
e4fe830b 2399 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
4d31c596 2400 .access = PL1_RW,
67dd8030 2401 .fgt = FGT_TPIDR_EL1,
54bf36ed
FA
2402 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
2403 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
2404 .access = PL1_RW,
2405 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
2406 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
2407 .resetvalue = 0 },
4d31c596
PM
2408};
2409
55d284af
PM
2410#ifndef CONFIG_USER_ONLY
2411
3f208fd7
PM
2412static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
2413 bool isread)
00108f2d 2414{
9b37a28c
FR
2415 /*
2416 * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
75502672
PM
2417 * Writable only at the highest implemented exception level.
2418 */
2419 int el = arm_current_el(env);
5bc84371
RH
2420 uint64_t hcr;
2421 uint32_t cntkctl;
75502672
PM
2422
2423 switch (el) {
2424 case 0:
5bc84371
RH
2425 hcr = arm_hcr_el2_eff(env);
2426 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2427 cntkctl = env->cp15.cnthctl_el2;
2428 } else {
2429 cntkctl = env->cp15.c14_cntkctl;
2430 }
2431 if (!extract32(cntkctl, 0, 2)) {
75502672
PM
2432 return CP_ACCESS_TRAP;
2433 }
2434 break;
2435 case 1:
2436 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
2437 arm_is_secure_below_el3(env)) {
2438 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
2439 return CP_ACCESS_TRAP_UNCATEGORIZED;
2440 }
2441 break;
2442 case 2:
2443 case 3:
2444 break;
00108f2d 2445 }
75502672
PM
2446
2447 if (!isread && el < arm_highest_el(env)) {
2448 return CP_ACCESS_TRAP_UNCATEGORIZED;
2449 }
2450
00108f2d
PM
2451 return CP_ACCESS_OK;
2452}
2453
3f208fd7
PM
2454static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
2455 bool isread)
00108f2d 2456{
0b6440af 2457 unsigned int cur_el = arm_current_el(env);
e6ef0169 2458 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2459 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2460
5bc84371
RH
2461 switch (cur_el) {
2462 case 0:
2463 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */
2464 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2465 return (extract32(env->cp15.cnthctl_el2, timeridx, 1)
2466 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2467 }
0b6440af 2468
5bc84371
RH
2469 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */
2470 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
2471 return CP_ACCESS_TRAP;
2472 }
2473
2474 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */
2475 if (hcr & HCR_E2H) {
2476 if (timeridx == GTIMER_PHYS &&
2477 !extract32(env->cp15.cnthctl_el2, 10, 1)) {
2478 return CP_ACCESS_TRAP_EL2;
2479 }
2480 } else {
2481 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
e6ef0169 2482 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2483 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
2484 return CP_ACCESS_TRAP_EL2;
2485 }
2486 }
2487 break;
2488
2489 case 1:
2490 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */
e6ef0169 2491 if (has_el2 && timeridx == GTIMER_PHYS &&
5bc84371
RH
2492 (hcr & HCR_E2H
2493 ? !extract32(env->cp15.cnthctl_el2, 10, 1)
2494 : !extract32(env->cp15.cnthctl_el2, 0, 1))) {
2495 return CP_ACCESS_TRAP_EL2;
2496 }
2497 break;
0b6440af 2498 }
00108f2d
PM
2499 return CP_ACCESS_OK;
2500}
2501
3f208fd7
PM
2502static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
2503 bool isread)
00108f2d 2504{
0b6440af 2505 unsigned int cur_el = arm_current_el(env);
e6ef0169 2506 bool has_el2 = arm_is_el2_enabled(env);
5bc84371 2507 uint64_t hcr = arm_hcr_el2_eff(env);
0b6440af 2508
5bc84371
RH
2509 switch (cur_el) {
2510 case 0:
2511 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2512 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */
2513 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1)
2514 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2);
2515 }
0b6440af 2516
5bc84371
RH
2517 /*
2518 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from
2519 * EL0 if EL0[PV]TEN is zero.
2520 */
2521 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
2522 return CP_ACCESS_TRAP;
2523 }
2524 /* fall through */
2525
2526 case 1:
e6ef0169 2527 if (has_el2 && timeridx == GTIMER_PHYS) {
5bc84371
RH
2528 if (hcr & HCR_E2H) {
2529 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */
2530 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) {
2531 return CP_ACCESS_TRAP_EL2;
2532 }
2533 } else {
2534 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */
2535 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) {
2536 return CP_ACCESS_TRAP_EL2;
2537 }
2538 }
2539 }
2540 break;
0b6440af 2541 }
00108f2d
PM
2542 return CP_ACCESS_OK;
2543}
2544
2545static CPAccessResult gt_pct_access(CPUARMState *env,
3f208fd7
PM
2546 const ARMCPRegInfo *ri,
2547 bool isread)
00108f2d 2548{
3f208fd7 2549 return gt_counter_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2550}
2551
2552static CPAccessResult gt_vct_access(CPUARMState *env,
3f208fd7
PM
2553 const ARMCPRegInfo *ri,
2554 bool isread)
00108f2d 2555{
3f208fd7 2556 return gt_counter_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2557}
2558
3f208fd7
PM
2559static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2560 bool isread)
00108f2d 2561{
3f208fd7 2562 return gt_timer_access(env, GTIMER_PHYS, isread);
00108f2d
PM
2563}
2564
3f208fd7
PM
2565static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
2566 bool isread)
00108f2d 2567{
3f208fd7 2568 return gt_timer_access(env, GTIMER_VIRT, isread);
00108f2d
PM
2569}
2570
b4d3978c 2571static CPAccessResult gt_stimer_access(CPUARMState *env,
3f208fd7
PM
2572 const ARMCPRegInfo *ri,
2573 bool isread)
b4d3978c 2574{
9b37a28c
FR
2575 /*
2576 * The AArch64 register view of the secure physical timer is
b4d3978c
PM
2577 * always accessible from EL3, and configurably accessible from
2578 * Secure EL1.
2579 */
2580 switch (arm_current_el(env)) {
2581 case 1:
2582 if (!arm_is_secure(env)) {
2583 return CP_ACCESS_TRAP;
2584 }
2585 if (!(env->cp15.scr_el3 & SCR_ST)) {
2586 return CP_ACCESS_TRAP_EL3;
2587 }
2588 return CP_ACCESS_OK;
2589 case 0:
2590 case 2:
2591 return CP_ACCESS_TRAP;
2592 case 3:
2593 return CP_ACCESS_OK;
2594 default:
2595 g_assert_not_reached();
2596 }
2597}
2598
55d284af
PM
2599static uint64_t gt_get_countervalue(CPUARMState *env)
2600{
7def8754
AJ
2601 ARMCPU *cpu = env_archcpu(env);
2602
2603 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu);
55d284af
PM
2604}
2605
2606static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
2607{
2608 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
2609
2610 if (gt->ctl & 1) {
9b37a28c
FR
2611 /*
2612 * Timer enabled: calculate and set current ISTATUS, irq, and
55d284af
PM
2613 * reset timer to when ISTATUS next has to change
2614 */
edac4d8a
EI
2615 uint64_t offset = timeridx == GTIMER_VIRT ?
2616 cpu->env.cp15.cntvoff_el2 : 0;
55d284af
PM
2617 uint64_t count = gt_get_countervalue(&cpu->env);
2618 /* Note that this must be unsigned 64 bit arithmetic: */
edac4d8a 2619 int istatus = count - offset >= gt->cval;
55d284af 2620 uint64_t nexttick;
194cbc49 2621 int irqstate;
55d284af
PM
2622
2623 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
194cbc49
PM
2624
2625 irqstate = (istatus && !(gt->ctl & 2));
2626 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
2627
55d284af
PM
2628 if (istatus) {
2629 /* Next transition is when count rolls back over to zero */
2630 nexttick = UINT64_MAX;
2631 } else {
2632 /* Next transition is when we hit cval */
edac4d8a 2633 nexttick = gt->cval + offset;
55d284af 2634 }
9b37a28c
FR
2635 /*
2636 * Note that the desired next expiry time might be beyond the
55d284af
PM
2637 * signed-64-bit range of a QEMUTimer -- in this case we just
2638 * set the timer for as far in the future as possible. When the
2639 * timer expires we will reset the timer for any remaining period.
2640 */
7def8754 2641 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
4a0245b6
AJ
2642 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX);
2643 } else {
2644 timer_mod(cpu->gt_timer[timeridx], nexttick);
55d284af 2645 }
194cbc49 2646 trace_arm_gt_recalc(timeridx, irqstate, nexttick);
55d284af
PM
2647 } else {
2648 /* Timer disabled: ISTATUS and timer output always clear */
2649 gt->ctl &= ~4;
2650 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
bc72ad67 2651 timer_del(cpu->gt_timer[timeridx]);
194cbc49 2652 trace_arm_gt_recalc_disabled(timeridx);
55d284af
PM
2653 }
2654}
2655
0e3eca4c
EI
2656static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
2657 int timeridx)
55d284af 2658{
2fc0cc0e 2659 ARMCPU *cpu = env_archcpu(env);
55d284af 2660
bc72ad67 2661 timer_del(cpu->gt_timer[timeridx]);
55d284af
PM
2662}
2663
c4241c7d 2664static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
55d284af 2665{
c4241c7d 2666 return gt_get_countervalue(env);
55d284af
PM
2667}
2668
53d1f856
RH
2669static uint64_t gt_virt_cnt_offset(CPUARMState *env)
2670{
2671 uint64_t hcr;
2672
2673 switch (arm_current_el(env)) {
2674 case 2:
2675 hcr = arm_hcr_el2_eff(env);
2676 if (hcr & HCR_E2H) {
2677 return 0;
2678 }
2679 break;
2680 case 0:
2681 hcr = arm_hcr_el2_eff(env);
2682 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
2683 return 0;
2684 }
2685 break;
2686 }
2687
2688 return env->cp15.cntvoff_el2;
2689}
2690
edac4d8a
EI
2691static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
2692{
53d1f856 2693 return gt_get_countervalue(env) - gt_virt_cnt_offset(env);
edac4d8a
EI
2694}
2695
c4241c7d 2696static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2697 int timeridx,
c4241c7d 2698 uint64_t value)
55d284af 2699{
194cbc49 2700 trace_arm_gt_cval_write(timeridx, value);
55d284af 2701 env->cp15.c14_timer[timeridx].cval = value;
2fc0cc0e 2702 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af 2703}
c4241c7d 2704
0e3eca4c
EI
2705static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
2706 int timeridx)
55d284af 2707{
53d1f856
RH
2708 uint64_t offset = 0;
2709
2710 switch (timeridx) {
2711 case GTIMER_VIRT:
8c94b071 2712 case GTIMER_HYPVIRT:
53d1f856
RH
2713 offset = gt_virt_cnt_offset(env);
2714 break;
2715 }
55d284af 2716
c4241c7d 2717 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
edac4d8a 2718 (gt_get_countervalue(env) - offset));
55d284af
PM
2719}
2720
c4241c7d 2721static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2722 int timeridx,
c4241c7d 2723 uint64_t value)
55d284af 2724{
53d1f856
RH
2725 uint64_t offset = 0;
2726
2727 switch (timeridx) {
2728 case GTIMER_VIRT:
8c94b071 2729 case GTIMER_HYPVIRT:
53d1f856
RH
2730 offset = gt_virt_cnt_offset(env);
2731 break;
2732 }
55d284af 2733
194cbc49 2734 trace_arm_gt_tval_write(timeridx, value);
edac4d8a 2735 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
18084b2f 2736 sextract64(value, 0, 32);
2fc0cc0e 2737 gt_recalc_timer(env_archcpu(env), timeridx);
55d284af
PM
2738}
2739
c4241c7d 2740static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
0e3eca4c 2741 int timeridx,
c4241c7d 2742 uint64_t value)
55d284af 2743{
2fc0cc0e 2744 ARMCPU *cpu = env_archcpu(env);
55d284af
PM
2745 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
2746
194cbc49 2747 trace_arm_gt_ctl_write(timeridx, value);
d3afacc7 2748 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
55d284af
PM
2749 if ((oldval ^ value) & 1) {
2750 /* Enable toggled */
2751 gt_recalc_timer(cpu, timeridx);
d3afacc7 2752 } else if ((oldval ^ value) & 2) {
9b37a28c
FR
2753 /*
2754 * IMASK toggled: don't need to recalculate,
55d284af
PM
2755 * just set the interrupt line based on ISTATUS
2756 */
194cbc49
PM
2757 int irqstate = (oldval & 4) && !(value & 2);
2758
2759 trace_arm_gt_imask_toggle(timeridx, irqstate);
2760 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate);
55d284af 2761 }
55d284af
PM
2762}
2763
0e3eca4c
EI
2764static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2765{
2766 gt_timer_reset(env, ri, GTIMER_PHYS);
2767}
2768
2769static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2770 uint64_t value)
2771{
2772 gt_cval_write(env, ri, GTIMER_PHYS, value);
2773}
2774
2775static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2776{
2777 return gt_tval_read(env, ri, GTIMER_PHYS);
2778}
2779
2780static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2781 uint64_t value)
2782{
2783 gt_tval_write(env, ri, GTIMER_PHYS, value);
2784}
2785
2786static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2787 uint64_t value)
2788{
2789 gt_ctl_write(env, ri, GTIMER_PHYS, value);
2790}
2791
bb5972e4
RH
2792static int gt_phys_redir_timeridx(CPUARMState *env)
2793{
2794 switch (arm_mmu_idx(env)) {
2795 case ARMMMUIdx_E20_0:
2796 case ARMMMUIdx_E20_2:
452ef8cb 2797 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2798 return GTIMER_HYP;
2799 default:
2800 return GTIMER_PHYS;
2801 }
2802}
2803
2804static int gt_virt_redir_timeridx(CPUARMState *env)
2805{
2806 switch (arm_mmu_idx(env)) {
2807 case ARMMMUIdx_E20_0:
2808 case ARMMMUIdx_E20_2:
452ef8cb 2809 case ARMMMUIdx_E20_2_PAN:
bb5972e4
RH
2810 return GTIMER_HYPVIRT;
2811 default:
2812 return GTIMER_VIRT;
2813 }
2814}
2815
2816static uint64_t gt_phys_redir_cval_read(CPUARMState *env,
2817 const ARMCPRegInfo *ri)
2818{
2819 int timeridx = gt_phys_redir_timeridx(env);
2820 return env->cp15.c14_timer[timeridx].cval;
2821}
2822
2823static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2824 uint64_t value)
2825{
2826 int timeridx = gt_phys_redir_timeridx(env);
2827 gt_cval_write(env, ri, timeridx, value);
2828}
2829
2830static uint64_t gt_phys_redir_tval_read(CPUARMState *env,
2831 const ARMCPRegInfo *ri)
2832{
2833 int timeridx = gt_phys_redir_timeridx(env);
2834 return gt_tval_read(env, ri, timeridx);
2835}
2836
2837static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2838 uint64_t value)
2839{
2840 int timeridx = gt_phys_redir_timeridx(env);
2841 gt_tval_write(env, ri, timeridx, value);
2842}
2843
2844static uint64_t gt_phys_redir_ctl_read(CPUARMState *env,
2845 const ARMCPRegInfo *ri)
2846{
2847 int timeridx = gt_phys_redir_timeridx(env);
2848 return env->cp15.c14_timer[timeridx].ctl;
2849}
2850
2851static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2852 uint64_t value)
2853{
2854 int timeridx = gt_phys_redir_timeridx(env);
2855 gt_ctl_write(env, ri, timeridx, value);
2856}
2857
0e3eca4c
EI
2858static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2859{
2860 gt_timer_reset(env, ri, GTIMER_VIRT);
2861}
2862
2863static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2864 uint64_t value)
2865{
2866 gt_cval_write(env, ri, GTIMER_VIRT, value);
2867}
2868
2869static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2870{
2871 return gt_tval_read(env, ri, GTIMER_VIRT);
2872}
2873
2874static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2875 uint64_t value)
2876{
2877 gt_tval_write(env, ri, GTIMER_VIRT, value);
2878}
2879
2880static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2881 uint64_t value)
2882{
2883 gt_ctl_write(env, ri, GTIMER_VIRT, value);
2884}
2885
edac4d8a
EI
2886static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
2887 uint64_t value)
2888{
2fc0cc0e 2889 ARMCPU *cpu = env_archcpu(env);
edac4d8a 2890
194cbc49 2891 trace_arm_gt_cntvoff_write(value);
edac4d8a
EI
2892 raw_write(env, ri, value);
2893 gt_recalc_timer(cpu, GTIMER_VIRT);
2894}
2895
bb5972e4
RH
2896static uint64_t gt_virt_redir_cval_read(CPUARMState *env,
2897 const ARMCPRegInfo *ri)
2898{
2899 int timeridx = gt_virt_redir_timeridx(env);
2900 return env->cp15.c14_timer[timeridx].cval;
2901}
2902
2903static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2904 uint64_t value)
2905{
2906 int timeridx = gt_virt_redir_timeridx(env);
2907 gt_cval_write(env, ri, timeridx, value);
2908}
2909
2910static uint64_t gt_virt_redir_tval_read(CPUARMState *env,
2911 const ARMCPRegInfo *ri)
2912{
2913 int timeridx = gt_virt_redir_timeridx(env);
2914 return gt_tval_read(env, ri, timeridx);
2915}
2916
2917static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2918 uint64_t value)
2919{
2920 int timeridx = gt_virt_redir_timeridx(env);
2921 gt_tval_write(env, ri, timeridx, value);
2922}
2923
2924static uint64_t gt_virt_redir_ctl_read(CPUARMState *env,
2925 const ARMCPRegInfo *ri)
2926{
2927 int timeridx = gt_virt_redir_timeridx(env);
2928 return env->cp15.c14_timer[timeridx].ctl;
2929}
2930
2931static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2932 uint64_t value)
2933{
2934 int timeridx = gt_virt_redir_timeridx(env);
2935 gt_ctl_write(env, ri, timeridx, value);
2936}
2937
b0e66d95
EI
2938static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2939{
2940 gt_timer_reset(env, ri, GTIMER_HYP);
2941}
2942
2943static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2944 uint64_t value)
2945{
2946 gt_cval_write(env, ri, GTIMER_HYP, value);
2947}
2948
2949static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2950{
2951 return gt_tval_read(env, ri, GTIMER_HYP);
2952}
2953
2954static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2955 uint64_t value)
2956{
2957 gt_tval_write(env, ri, GTIMER_HYP, value);
2958}
2959
2960static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2961 uint64_t value)
2962{
2963 gt_ctl_write(env, ri, GTIMER_HYP, value);
2964}
2965
b4d3978c
PM
2966static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2967{
2968 gt_timer_reset(env, ri, GTIMER_SEC);
2969}
2970
2971static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2972 uint64_t value)
2973{
2974 gt_cval_write(env, ri, GTIMER_SEC, value);
2975}
2976
2977static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
2978{
2979 return gt_tval_read(env, ri, GTIMER_SEC);
2980}
2981
2982static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
2983 uint64_t value)
2984{
2985 gt_tval_write(env, ri, GTIMER_SEC, value);
2986}
2987
2988static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
2989 uint64_t value)
2990{
2991 gt_ctl_write(env, ri, GTIMER_SEC, value);
2992}
2993
8c94b071
RH
2994static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2995{
2996 gt_timer_reset(env, ri, GTIMER_HYPVIRT);
2997}
2998
2999static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3000 uint64_t value)
3001{
3002 gt_cval_write(env, ri, GTIMER_HYPVIRT, value);
3003}
3004
3005static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
3006{
3007 return gt_tval_read(env, ri, GTIMER_HYPVIRT);
3008}
3009
3010static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
3011 uint64_t value)
3012{
3013 gt_tval_write(env, ri, GTIMER_HYPVIRT, value);
3014}
3015
3016static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
3017 uint64_t value)
3018{
3019 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value);
3020}
3021
55d284af
PM
3022void arm_gt_ptimer_cb(void *opaque)
3023{
3024 ARMCPU *cpu = opaque;
3025
3026 gt_recalc_timer(cpu, GTIMER_PHYS);
3027}
3028
3029void arm_gt_vtimer_cb(void *opaque)
3030{
3031 ARMCPU *cpu = opaque;
3032
3033 gt_recalc_timer(cpu, GTIMER_VIRT);
3034}
3035
b0e66d95
EI
3036void arm_gt_htimer_cb(void *opaque)
3037{
3038 ARMCPU *cpu = opaque;
3039
3040 gt_recalc_timer(cpu, GTIMER_HYP);
3041}
3042
b4d3978c
PM
3043void arm_gt_stimer_cb(void *opaque)
3044{
3045 ARMCPU *cpu = opaque;
3046
3047 gt_recalc_timer(cpu, GTIMER_SEC);
3048}
3049
8c94b071
RH
3050void arm_gt_hvtimer_cb(void *opaque)
3051{
3052 ARMCPU *cpu = opaque;
3053
3054 gt_recalc_timer(cpu, GTIMER_HYPVIRT);
3055}
3056
96eec6b2
AJ
3057static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque)
3058{
3059 ARMCPU *cpu = env_archcpu(env);
3060
3061 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz;
3062}
3063
55d284af 3064static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
9b37a28c
FR
3065 /*
3066 * Note that CNTFRQ is purely reads-as-written for the benefit
55d284af
PM
3067 * of software; writing it doesn't actually change the timer frequency.
3068 * Our reset value matches the fixed frequency we implement the timer at.
3069 */
3070 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 3071 .type = ARM_CP_ALIAS,
a7adc4b7
PM
3072 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
3073 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
a7adc4b7
PM
3074 },
3075 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3076 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3077 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
55d284af 3078 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
96eec6b2 3079 .resetfn = arm_gt_cntfrq_reset,
55d284af
PM
3080 },
3081 /* overall control: mostly access permissions */
a7adc4b7
PM
3082 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
3083 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
55d284af
PM
3084 .access = PL1_RW,
3085 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
3086 .resetvalue = 0,
3087 },
3088 /* per-timer control */
3089 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
9ff9dd3c 3090 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3091 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3092 .accessfn = gt_ptimer_access,
3093 .fieldoffset = offsetoflow32(CPUARMState,
3094 cp15.c14_timer[GTIMER_PHYS].ctl),
bb5972e4
RH
3095 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3096 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7 3097 },
9c513e78 3098 { .name = "CNTP_CTL_S",
9ff9dd3c
PM
3099 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
3100 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3101 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
9ff9dd3c
PM
3102 .accessfn = gt_ptimer_access,
3103 .fieldoffset = offsetoflow32(CPUARMState,
3104 cp15.c14_timer[GTIMER_SEC].ctl),
3105 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3106 },
a7adc4b7
PM
3107 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
3108 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
daf1dc5f 3109 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3110 .accessfn = gt_ptimer_access,
55d284af
PM
3111 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
3112 .resetvalue = 0,
bb5972e4
RH
3113 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read,
3114 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3115 },
3116 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
daf1dc5f 3117 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW,
a7adc4b7
PM
3118 .accessfn = gt_vtimer_access,
3119 .fieldoffset = offsetoflow32(CPUARMState,
3120 cp15.c14_timer[GTIMER_VIRT].ctl),
bb5972e4
RH
3121 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3122 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
a7adc4b7
PM
3123 },
3124 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
3125 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
daf1dc5f 3126 .type = ARM_CP_IO, .access = PL0_RW,
a7adc4b7 3127 .accessfn = gt_vtimer_access,
55d284af
PM
3128 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
3129 .resetvalue = 0,
bb5972e4
RH
3130 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read,
3131 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write,
55d284af
PM
3132 },
3133 /* TimerValue views: a 32 bit downcounting view of the underlying state */
3134 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
9ff9dd3c 3135 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3136 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3137 .accessfn = gt_ptimer_access,
bb5972e4 3138 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
55d284af 3139 },
9c513e78 3140 { .name = "CNTP_TVAL_S",
9ff9dd3c
PM
3141 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
3142 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3143 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
9ff9dd3c
PM
3144 .accessfn = gt_ptimer_access,
3145 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
3146 },
a7adc4b7
PM
3147 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3148 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
daf1dc5f 3149 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3150 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
bb5972e4 3151 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write,
a7adc4b7 3152 },
55d284af 3153 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
daf1dc5f 3154 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
00108f2d 3155 .accessfn = gt_vtimer_access,
bb5972e4 3156 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
55d284af 3157 },
a7adc4b7
PM
3158 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
3159 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
daf1dc5f 3160 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW,
0e3eca4c 3161 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
bb5972e4 3162 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write,
a7adc4b7 3163 },
55d284af
PM
3164 /* The counter itself */
3165 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
7a0e58fa 3166 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3167 .accessfn = gt_pct_access,
a7adc4b7
PM
3168 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
3169 },
3170 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
3171 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
7a0e58fa 3172 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3173 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
55d284af
PM
3174 },
3175 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
7a0e58fa 3176 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
00108f2d 3177 .accessfn = gt_vct_access,
edac4d8a 3178 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
a7adc4b7
PM
3179 },
3180 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3181 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
7a0e58fa 3182 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
d57b9ee8 3183 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
55d284af
PM
3184 },
3185 /* Comparison value, indicating when the timer goes off */
3186 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3187 .secure = ARM_CP_SECSTATE_NS,
daf1dc5f 3188 .access = PL0_RW,
7a0e58fa 3189 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3190 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
b061a82b 3191 .accessfn = gt_ptimer_access,
bb5972e4
RH
3192 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3193 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7 3194 },
9c513e78 3195 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2,
9ff9dd3c 3196 .secure = ARM_CP_SECSTATE_S,
daf1dc5f 3197 .access = PL0_RW,
9ff9dd3c
PM
3198 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
3199 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3200 .accessfn = gt_ptimer_access,
3201 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3202 },
a7adc4b7
PM
3203 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3204 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
daf1dc5f 3205 .access = PL0_RW,
a7adc4b7
PM
3206 .type = ARM_CP_IO,
3207 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
12cde08a 3208 .resetvalue = 0, .accessfn = gt_ptimer_access,
bb5972e4
RH
3209 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read,
3210 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write,
55d284af
PM
3211 },
3212 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
daf1dc5f 3213 .access = PL0_RW,
7a0e58fa 3214 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
55d284af 3215 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
b061a82b 3216 .accessfn = gt_vtimer_access,
bb5972e4
RH
3217 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3218 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
a7adc4b7
PM
3219 },
3220 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
3221 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
daf1dc5f 3222 .access = PL0_RW,
a7adc4b7
PM
3223 .type = ARM_CP_IO,
3224 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
3225 .resetvalue = 0, .accessfn = gt_vtimer_access,
bb5972e4
RH
3226 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read,
3227 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write,
55d284af 3228 },
9b37a28c
FR
3229 /*
3230 * Secure timer -- this is actually restricted to only EL3
b4d3978c
PM
3231 * and configurably Secure-EL1 via the accessfn.
3232 */
3233 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
3234 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
3235 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
3236 .accessfn = gt_stimer_access,
3237 .readfn = gt_sec_tval_read,
3238 .writefn = gt_sec_tval_write,
3239 .resetfn = gt_sec_timer_reset,
3240 },
3241 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
3242 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
3243 .type = ARM_CP_IO, .access = PL1_RW,
3244 .accessfn = gt_stimer_access,
3245 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
3246 .resetvalue = 0,
3247 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
3248 },
3249 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
3250 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
3251 .type = ARM_CP_IO, .access = PL1_RW,
3252 .accessfn = gt_stimer_access,
3253 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
3254 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
3255 },
55d284af
PM
3256};
3257
bb5972e4
RH
3258static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri,
3259 bool isread)
3260{
3261 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) {
3262 return CP_ACCESS_TRAP;
3263 }
3264 return CP_ACCESS_OK;
3265}
3266
55d284af 3267#else
26c4a83b 3268
9b37a28c
FR
3269/*
3270 * In user-mode most of the generic timer registers are inaccessible
26c4a83b 3271 * however modern kernels (4.12+) allow access to cntvct_el0
55d284af 3272 */
26c4a83b
AB
3273
3274static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
3275{
7def8754
AJ
3276 ARMCPU *cpu = env_archcpu(env);
3277
9b37a28c
FR
3278 /*
3279 * Currently we have no support for QEMUTimer in linux-user so we
26c4a83b
AB
3280 * can't call gt_get_countervalue(env), instead we directly
3281 * call the lower level functions.
3282 */
7def8754 3283 return cpu_get_clock() / gt_cntfrq_period_ns(cpu);
26c4a83b
AB
3284}
3285
6cc7a3ae 3286static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
26c4a83b
AB
3287 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
3288 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
3289 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */,
3290 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
3291 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE,
3292 },
3293 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
3294 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
3295 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
3296 .readfn = gt_virt_cnt_read,
3297 },
6cc7a3ae
PM
3298};
3299
55d284af
PM
3300#endif
3301
c4241c7d 3302static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
4a501606 3303{
891a2fe7 3304 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8d5c773e 3305 raw_write(env, ri, value);
891a2fe7 3306 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8d5c773e 3307 raw_write(env, ri, value & 0xfffff6ff);
4a501606 3308 } else {
8d5c773e 3309 raw_write(env, ri, value & 0xfffff1ff);
4a501606 3310 }
4a501606
PM
3311}
3312
3313#ifndef CONFIG_USER_ONLY
3314/* get_phys_addr() isn't present for user-mode-only targets */
702a9357 3315
3f208fd7
PM
3316static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
3317 bool isread)
92611c00
PM
3318{
3319 if (ri->opc2 & 4) {
9b37a28c
FR
3320 /*
3321 * The ATS12NSO* operations must trap to EL3 or EL2 if executed in
87562e4f
PM
3322 * Secure EL1 (which can only happen if EL3 is AArch64).
3323 * They are simply UNDEF if executed from NS EL1.
3324 * They function normally from EL2 or EL3.
92611c00 3325 */
87562e4f
PM
3326 if (arm_current_el(env) == 1) {
3327 if (arm_is_secure_below_el3(env)) {
926c1b97 3328 if (env->cp15.scr_el3 & SCR_EEL2) {
ce9a8863 3329 return CP_ACCESS_TRAP_EL2;
926c1b97 3330 }
ce9a8863 3331 return CP_ACCESS_TRAP_EL3;
87562e4f
PM
3332 }
3333 return CP_ACCESS_TRAP_UNCATEGORIZED;
3334 }
92611c00
PM
3335 }
3336 return CP_ACCESS_OK;
3337}
3338
9fb005b0 3339#ifdef CONFIG_TCG
060e8a48 3340static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
7aee3cb9
RH
3341 MMUAccessType access_type, ARMMMUIdx mmu_idx,
3342 bool is_secure)
4a501606 3343{
b7cc4e82 3344 bool ret;
01c097f7 3345 uint64_t par64;
1313e2d7 3346 bool format64 = false;
e14b5a23 3347 ARMMMUFaultInfo fi = {};
de05a709 3348 GetPhysAddrResult res = {};
4a501606 3349
7aee3cb9
RH
3350 ret = get_phys_addr_with_secure(env, value, access_type, mmu_idx,
3351 is_secure, &res, &fi);
1313e2d7 3352
9f225e60
PM
3353 /*
3354 * ATS operations only do S1 or S1+S2 translations, so we never
3355 * have to deal with the ARMCacheAttrs format for S2 only.
3356 */
de05a709 3357 assert(!res.cacheattrs.is_s2_format);
9f225e60 3358
0710b2fa
PM
3359 if (ret) {
3360 /*
3361 * Some kinds of translation fault must cause exceptions rather
3362 * than being reported in the PAR.
3363 */
3364 int current_el = arm_current_el(env);
3365 int target_el;
3366 uint32_t syn, fsr, fsc;
3367 bool take_exc = false;
3368
b1a10c86 3369 if (fi.s1ptw && current_el == 1
fee7aa46 3370 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) {
0710b2fa
PM
3371 /*
3372 * Synchronous stage 2 fault on an access made as part of the
3373 * translation table walk for AT S1E0* or AT S1E1* insn
3374 * executed from NS EL1. If this is a synchronous external abort
3375 * and SCR_EL3.EA == 1, then we take a synchronous external abort
3376 * to EL3. Otherwise the fault is taken as an exception to EL2,
3377 * and HPFAR_EL2 holds the faulting IPA.
3378 */
3379 if (fi.type == ARMFault_SyncExternalOnWalk &&
3380 (env->cp15.scr_el3 & SCR_EA)) {
3381 target_el = 3;
3382 } else {
3383 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
9861248f
RDC
3384 if (arm_is_secure_below_el3(env) && fi.s1ns) {
3385 env->cp15.hpfar_el2 |= HPFAR_NS;
3386 }
0710b2fa
PM
3387 target_el = 2;
3388 }
3389 take_exc = true;
3390 } else if (fi.type == ARMFault_SyncExternalOnWalk) {
3391 /*
3392 * Synchronous external aborts during a translation table walk
3393 * are taken as Data Abort exceptions.
3394 */
3395 if (fi.stage2) {
3396 if (current_el == 3) {
3397 target_el = 3;
3398 } else {
3399 target_el = 2;
3400 }
3401 } else {
3402 target_el = exception_target_el(env);
3403 }
3404 take_exc = true;
3405 }
3406
3407 if (take_exc) {
3408 /* Construct FSR and FSC using same logic as arm_deliver_fault() */
3409 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
3410 arm_s1_regime_using_lpae_format(env, mmu_idx)) {
3411 fsr = arm_fi_to_lfsc(&fi);
3412 fsc = extract32(fsr, 0, 6);
3413 } else {
3414 fsr = arm_fi_to_sfsc(&fi);
3415 fsc = 0x3f;
3416 }
3417 /*
3418 * Report exception with ESR indicating a fault due to a
3419 * translation table walk for a cache maintenance instruction.
3420 */
e24fd076 3421 syn = syn_data_abort_no_iss(current_el == target_el, 0,
0710b2fa
PM
3422 fi.ea, 1, fi.s1ptw, 1, fsc);
3423 env->exception.vaddress = value;
3424 env->exception.fsr = fsr;
3425 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
3426 }
3427 }
3428
1313e2d7
EI
3429 if (is_a64(env)) {
3430 format64 = true;
3431 } else if (arm_feature(env, ARM_FEATURE_LPAE)) {
3432 /*
3433 * ATS1Cxx:
3434 * * TTBCR.EAE determines whether the result is returned using the
3435 * 32-bit or the 64-bit PAR format
3436 * * Instructions executed in Hyp mode always use the 64bit format
3437 *
3438 * ATS1S2NSOxx uses the 64bit format if any of the following is true:
3439 * * The Non-secure TTBCR.EAE bit is set to 1
3440 * * The implementation includes EL2, and the value of HCR.VM is 1
3441 *
9d1bab33
PM
3442 * (Note that HCR.DC makes HCR.VM behave as if it is 1.)
3443 *
23463e0e 3444 * ATS1Hx always uses the 64bit format.
1313e2d7
EI
3445 */
3446 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx);
3447
3448 if (arm_feature(env, ARM_FEATURE_EL2)) {
452ef8cb
RH
3449 if (mmu_idx == ARMMMUIdx_E10_0 ||
3450 mmu_idx == ARMMMUIdx_E10_1 ||
3451 mmu_idx == ARMMMUIdx_E10_1_PAN) {
9d1bab33 3452 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC);
1313e2d7
EI
3453 } else {
3454 format64 |= arm_current_el(env) == 2;
3455 }
3456 }
3457 }
3458
3459 if (format64) {
5efe9ed4 3460 /* Create a 64-bit PAR */
01c097f7 3461 par64 = (1 << 11); /* LPAE bit always set */
b7cc4e82 3462 if (!ret) {
7fa7ea8f
RH
3463 par64 |= res.f.phys_addr & ~0xfffULL;
3464 if (!res.f.attrs.secure) {
8bf5b6a9
PM
3465 par64 |= (1 << 9); /* NS */
3466 }
de05a709
RH
3467 par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */
3468 par64 |= res.cacheattrs.shareability << 7; /* SH */
4a501606 3469 } else {
5efe9ed4
PM
3470 uint32_t fsr = arm_fi_to_lfsc(&fi);
3471
702a9357 3472 par64 |= 1; /* F */
b7cc4e82 3473 par64 |= (fsr & 0x3f) << 1; /* FS */
0f7b791b
PM
3474 if (fi.stage2) {
3475 par64 |= (1 << 9); /* S */
3476 }
3477 if (fi.s1ptw) {
3478 par64 |= (1 << 8); /* PTW */
3479 }
4a501606
PM
3480 }
3481 } else {
9b37a28c
FR
3482 /*
3483 * fsr is a DFSR/IFSR value for the short descriptor
702a9357
PM
3484 * translation table format (with WnR always clear).
3485 * Convert it to a 32-bit PAR.
3486 */
b7cc4e82 3487 if (!ret) {
702a9357 3488 /* We do not set any attribute bits in the PAR */
7fa7ea8f 3489 if (res.f.lg_page_size == 24
702a9357 3490 && arm_feature(env, ARM_FEATURE_V7)) {
7fa7ea8f 3491 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1);
702a9357 3492 } else {
7fa7ea8f 3493 par64 = res.f.phys_addr & 0xfffff000;
702a9357 3494 }
7fa7ea8f 3495 if (!res.f.attrs.secure) {
8bf5b6a9
PM
3496 par64 |= (1 << 9); /* NS */
3497 }
702a9357 3498 } else {
5efe9ed4
PM
3499 uint32_t fsr = arm_fi_to_sfsc(&fi);
3500
b7cc4e82
PC
3501 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
3502 ((fsr & 0xf) << 1) | 1;
702a9357 3503 }
4a501606 3504 }
060e8a48
PM
3505 return par64;
3506}
9fb005b0 3507#endif /* CONFIG_TCG */
060e8a48
PM
3508
3509static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3510{
9fb005b0 3511#ifdef CONFIG_TCG
03ae85f8 3512 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
060e8a48 3513 uint64_t par64;
d3649702
PM
3514 ARMMMUIdx mmu_idx;
3515 int el = arm_current_el(env);
3516 bool secure = arm_is_secure_below_el3(env);
060e8a48 3517
d3649702
PM
3518 switch (ri->opc2 & 6) {
3519 case 0:
04b07d29 3520 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */
d3649702
PM
3521 switch (el) {
3522 case 3:
d902ae75 3523 mmu_idx = ARMMMUIdx_E3;
7aee3cb9 3524 secure = true;
d3649702
PM
3525 break;
3526 case 2:
b6ad6062 3527 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
04b07d29 3528 /* fall through */
d3649702 3529 case 1:
04b07d29 3530 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) {
d902ae75 3531 mmu_idx = ARMMMUIdx_Stage1_E1_PAN;
04b07d29 3532 } else {
d902ae75 3533 mmu_idx = ARMMMUIdx_Stage1_E1;
04b07d29 3534 }
d3649702
PM
3535 break;
3536 default:
3537 g_assert_not_reached();
3538 }
3539 break;
3540 case 2:
3541 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
3542 switch (el) {
3543 case 3:
d902ae75 3544 mmu_idx = ARMMMUIdx_E10_0;
7aee3cb9 3545 secure = true;
d3649702
PM
3546 break;
3547 case 2:
b1a10c86 3548 g_assert(!secure); /* ARMv8.4-SecEL2 is 64-bit only */
2859d7b5 3549 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3550 break;
3551 case 1:
d902ae75 3552 mmu_idx = ARMMMUIdx_Stage1_E0;
d3649702
PM
3553 break;
3554 default:
3555 g_assert_not_reached();
3556 }
3557 break;
3558 case 4:
3559 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
01b98b68 3560 mmu_idx = ARMMMUIdx_E10_1;
7aee3cb9 3561 secure = false;
d3649702
PM
3562 break;
3563 case 6:
3564 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
01b98b68 3565 mmu_idx = ARMMMUIdx_E10_0;
7aee3cb9 3566 secure = false;
d3649702
PM
3567 break;
3568 default:
3569 g_assert_not_reached();
3570 }
3571
7aee3cb9 3572 par64 = do_ats_write(env, value, access_type, mmu_idx, secure);
01c097f7
FA
3573
3574 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3575#else
3576 /* Handled by hardware accelerator. */
3577 g_assert_not_reached();
3578#endif /* CONFIG_TCG */
4a501606 3579}
060e8a48 3580
14db7fe0
PM
3581static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
3582 uint64_t value)
3583{
9fb005b0 3584#ifdef CONFIG_TCG
03ae85f8 3585 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
14db7fe0
PM
3586 uint64_t par64;
3587
7aee3cb9
RH
3588 /* There is no SecureEL2 for AArch32. */
3589 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2, false);
14db7fe0
PM
3590
3591 A32_BANKED_CURRENT_REG_SET(env, par, par64);
9fb005b0
PMD
3592#else
3593 /* Handled by hardware accelerator. */
3594 g_assert_not_reached();
3595#endif /* CONFIG_TCG */
14db7fe0
PM
3596}
3597
3f208fd7
PM
3598static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
3599 bool isread)
2a47df95 3600{
926c1b97
RDC
3601 if (arm_current_el(env) == 3 &&
3602 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) {
2a47df95
PM
3603 return CP_ACCESS_TRAP;
3604 }
3605 return CP_ACCESS_OK;
3606}
3607
060e8a48
PM
3608static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
3609 uint64_t value)
3610{
9fb005b0 3611#ifdef CONFIG_TCG
03ae85f8 3612 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD;
d3649702
PM
3613 ARMMMUIdx mmu_idx;
3614 int secure = arm_is_secure_below_el3(env);
638d5dbd
AK
3615 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
3616 bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE);
d3649702
PM
3617
3618 switch (ri->opc2 & 6) {
3619 case 0:
3620 switch (ri->opc1) {
04b07d29
RH
3621 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */
3622 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) {
638d5dbd
AK
3623 mmu_idx = regime_e20 ?
3624 ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN;
04b07d29 3625 } else {
638d5dbd 3626 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1;
04b07d29 3627 }
d3649702
PM
3628 break;
3629 case 4: /* AT S1E2R, AT S1E2W */
638d5dbd 3630 mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2;
d3649702
PM
3631 break;
3632 case 6: /* AT S1E3R, AT S1E3W */
d902ae75 3633 mmu_idx = ARMMMUIdx_E3;
7aee3cb9 3634 secure = true;
d3649702
PM
3635 break;
3636 default:
3637 g_assert_not_reached();
3638 }
3639 break;
3640 case 2: /* AT S1E0R, AT S1E0W */
638d5dbd 3641 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0;
d3649702
PM
3642 break;
3643 case 4: /* AT S12E1R, AT S12E1W */
638d5dbd 3644 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1;
d3649702
PM
3645 break;
3646 case 6: /* AT S12E0R, AT S12E0W */
638d5dbd 3647 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0;
d3649702
PM
3648 break;
3649 default:
3650 g_assert_not_reached();
3651 }
060e8a48 3652
7aee3cb9
RH
3653 env->cp15.par_el[1] = do_ats_write(env, value, access_type,
3654 mmu_idx, secure);
9fb005b0
PMD
3655#else
3656 /* Handled by hardware accelerator. */
3657 g_assert_not_reached();
3658#endif /* CONFIG_TCG */
060e8a48 3659}
4a501606
PM
3660#endif
3661
3662static const ARMCPRegInfo vapa_cp_reginfo[] = {
3663 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
3664 .access = PL1_RW, .resetvalue = 0,
01c097f7
FA
3665 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
3666 offsetoflow32(CPUARMState, cp15.par_ns) },
4a501606
PM
3667 .writefn = par_write },
3668#ifndef CONFIG_USER_ONLY
87562e4f 3669 /* This underdecoding is safe because the reginfo is NO_RAW. */
4a501606 3670 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
92611c00 3671 .access = PL1_W, .accessfn = ats_access,
0710b2fa 3672 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
4a501606 3673#endif
4a501606
PM
3674};
3675
18032bec
PM
3676/* Return basic MPU access permission bits. */
3677static uint32_t simple_mpu_ap_bits(uint32_t val)
3678{
3679 uint32_t ret;
3680 uint32_t mask;
3681 int i;
3682 ret = 0;
3683 mask = 3;
3684 for (i = 0; i < 16; i += 2) {
3685 ret |= (val >> i) & mask;
3686 mask <<= 2;
3687 }
3688 return ret;
3689}
3690
3691/* Pad basic MPU access permission bits to extended format. */
3692static uint32_t extended_mpu_ap_bits(uint32_t val)
3693{
3694 uint32_t ret;
3695 uint32_t mask;
3696 int i;
3697 ret = 0;
3698 mask = 3;
3699 for (i = 0; i < 16; i += 2) {
3700 ret |= (val & mask) << i;
3701 mask <<= 2;
3702 }
3703 return ret;
3704}
3705
c4241c7d
PM
3706static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3707 uint64_t value)
18032bec 3708{
7e09797c 3709 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
18032bec
PM
3710}
3711
c4241c7d 3712static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3713{
7e09797c 3714 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
18032bec
PM
3715}
3716
c4241c7d
PM
3717static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
3718 uint64_t value)
18032bec 3719{
7e09797c 3720 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
18032bec
PM
3721}
3722
c4241c7d 3723static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
18032bec 3724{
7e09797c 3725 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
18032bec
PM
3726}
3727
6cb0b013
PC
3728static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
3729{
3730 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3731
3732 if (!u32p) {
3733 return 0;
3734 }
3735
1bc04a88 3736 u32p += env->pmsav7.rnr[M_REG_NS];
6cb0b013
PC
3737 return *u32p;
3738}
3739
3740static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
3741 uint64_t value)
3742{
2fc0cc0e 3743 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3744 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
3745
3746 if (!u32p) {
3747 return;
3748 }
3749
1bc04a88 3750 u32p += env->pmsav7.rnr[M_REG_NS];
d10eb08f 3751 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
6cb0b013
PC
3752 *u32p = value;
3753}
3754
6cb0b013
PC
3755static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3756 uint64_t value)
3757{
2fc0cc0e 3758 ARMCPU *cpu = env_archcpu(env);
6cb0b013
PC
3759 uint32_t nrgs = cpu->pmsav7_dregion;
3760
3761 if (value >= nrgs) {
3762 qemu_log_mask(LOG_GUEST_ERROR,
3763 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
3764 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
3765 return;
3766 }
3767
3768 raw_write(env, ri, value);
3769}
3770
761c4642
TR
3771static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3772 uint64_t value)
3773{
3774 ARMCPU *cpu = env_archcpu(env);
3775
3776 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3777 env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3778}
3779
3780static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3781{
3782 return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3783}
3784
3785static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3786 uint64_t value)
3787{
3788 ARMCPU *cpu = env_archcpu(env);
3789
3790 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3791 env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value;
3792}
3793
3794static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3795{
3796 return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]];
3797}
3798
3799static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3800 uint64_t value)
3801{
3802 ARMCPU *cpu = env_archcpu(env);
3803
3804 /*
3805 * Ignore writes that would select not implemented region.
3806 * This is architecturally UNPREDICTABLE.
3807 */
3808 if (value >= cpu->pmsav7_dregion) {
3809 return;
3810 }
3811
3812 env->pmsav7.rnr[M_REG_NS] = value;
3813}
3814
3815static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3816 uint64_t value)
3817{
3818 ARMCPU *cpu = env_archcpu(env);
3819
3820 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3821 env->pmsav8.hprbar[env->pmsav8.hprselr] = value;
3822}
3823
3824static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3825{
3826 return env->pmsav8.hprbar[env->pmsav8.hprselr];
3827}
3828
3829static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3830 uint64_t value)
3831{
3832 ARMCPU *cpu = env_archcpu(env);
3833
3834 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3835 env->pmsav8.hprlar[env->pmsav8.hprselr] = value;
3836}
3837
3838static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri)
3839{
3840 return env->pmsav8.hprlar[env->pmsav8.hprselr];
3841}
3842
3843static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3844 uint64_t value)
3845{
3846 uint32_t n;
3847 uint32_t bit;
3848 ARMCPU *cpu = env_archcpu(env);
3849
3850 /* Ignore writes to unimplemented regions */
3851 int rmax = MIN(cpu->pmsav8r_hdregion, 32);
3852 value &= MAKE_64BIT_MASK(0, rmax);
3853
3854 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3855
3856 /* Register alias is only valid for first 32 indexes */
3857 for (n = 0; n < rmax; ++n) {
3858 bit = extract32(value, n, 1);
3859 env->pmsav8.hprlar[n] = deposit32(
3860 env->pmsav8.hprlar[n], 0, 1, bit);
3861 }
3862}
3863
3864static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri)
3865{
3866 uint32_t n;
3867 uint32_t result = 0x0;
3868 ARMCPU *cpu = env_archcpu(env);
3869
3870 /* Register alias is only valid for first 32 indexes */
3871 for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) {
3872 if (env->pmsav8.hprlar[n] & 0x1) {
3873 result |= (0x1 << n);
3874 }
3875 }
3876 return result;
3877}
3878
3879static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3880 uint64_t value)
3881{
3882 ARMCPU *cpu = env_archcpu(env);
3883
3884 /*
3885 * Ignore writes that would select not implemented region.
3886 * This is architecturally UNPREDICTABLE.
3887 */
3888 if (value >= cpu->pmsav8r_hdregion) {
3889 return;
3890 }
3891
3892 env->pmsav8.hprselr = value;
3893}
3894
3895static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri,
3896 uint64_t value)
3897{
3898 ARMCPU *cpu = env_archcpu(env);
3899 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3900 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3901
3902 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */
3903
3904 if (ri->opc1 & 4) {
3905 if (index >= cpu->pmsav8r_hdregion) {
3906 return;
3907 }
3908 if (ri->opc2 & 0x1) {
3909 env->pmsav8.hprlar[index] = value;
3910 } else {
3911 env->pmsav8.hprbar[index] = value;
3912 }
3913 } else {
3914 if (index >= cpu->pmsav7_dregion) {
3915 return;
3916 }
3917 if (ri->opc2 & 0x1) {
3918 env->pmsav8.rlar[M_REG_NS][index] = value;
3919 } else {
3920 env->pmsav8.rbar[M_REG_NS][index] = value;
3921 }
3922 }
3923}
3924
3925static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri)
3926{
3927 ARMCPU *cpu = env_archcpu(env);
3928 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) |
3929 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1);
3930
3931 if (ri->opc1 & 4) {
3932 if (index >= cpu->pmsav8r_hdregion) {
3933 return 0x0;
3934 }
3935 if (ri->opc2 & 0x1) {
3936 return env->pmsav8.hprlar[index];
3937 } else {
3938 return env->pmsav8.hprbar[index];
3939 }
3940 } else {
3941 if (index >= cpu->pmsav7_dregion) {
3942 return 0x0;
3943 }
3944 if (ri->opc2 & 0x1) {
3945 return env->pmsav8.rlar[M_REG_NS][index];
3946 } else {
3947 return env->pmsav8.rbar[M_REG_NS][index];
3948 }
3949 }
3950}
3951
3952static const ARMCPRegInfo pmsav8r_cp_reginfo[] = {
3953 { .name = "PRBAR",
3954 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0,
3955 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3956 .accessfn = access_tvm_trvm,
3957 .readfn = prbar_read, .writefn = prbar_write },
3958 { .name = "PRLAR",
3959 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1,
3960 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3961 .accessfn = access_tvm_trvm,
3962 .readfn = prlar_read, .writefn = prlar_write },
3963 { .name = "PRSELR", .resetvalue = 0,
3964 .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1,
3965 .access = PL1_RW, .accessfn = access_tvm_trvm,
3966 .writefn = prselr_write,
3967 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) },
3968 { .name = "HPRBAR", .resetvalue = 0,
3969 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0,
3970 .access = PL2_RW, .type = ARM_CP_NO_RAW,
3971 .readfn = hprbar_read, .writefn = hprbar_write },
3972 { .name = "HPRLAR",
3973 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1,
3974 .access = PL2_RW, .type = ARM_CP_NO_RAW,
3975 .readfn = hprlar_read, .writefn = hprlar_write },
3976 { .name = "HPRSELR", .resetvalue = 0,
3977 .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1,
3978 .access = PL2_RW,
3979 .writefn = hprselr_write,
3980 .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) },
3981 { .name = "HPRENR",
3982 .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1,
3983 .access = PL2_RW, .type = ARM_CP_NO_RAW,
3984 .readfn = hprenr_read, .writefn = hprenr_write },
3985};
3986
6cb0b013 3987static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
9b37a28c
FR
3988 /*
3989 * Reset for all these registers is handled in arm_cpu_reset(),
69ceea64
PM
3990 * because the PMSAv7 is also used by M-profile CPUs, which do
3991 * not register cpregs but still need the state to be reset.
3992 */
6cb0b013
PC
3993 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
3994 .access = PL1_RW, .type = ARM_CP_NO_RAW,
3995 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
69ceea64
PM
3996 .readfn = pmsav7_read, .writefn = pmsav7_write,
3997 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
3998 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
3999 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4000 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
69ceea64
PM
4001 .readfn = pmsav7_read, .writefn = pmsav7_write,
4002 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
4003 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
4004 .access = PL1_RW, .type = ARM_CP_NO_RAW,
4005 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
69ceea64
PM
4006 .readfn = pmsav7_read, .writefn = pmsav7_write,
4007 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
4008 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
4009 .access = PL1_RW,
1bc04a88 4010 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]),
69ceea64
PM
4011 .writefn = pmsav7_rgnr_write,
4012 .resetfn = arm_cp_reset_ignore },
6cb0b013
PC
4013};
4014
18032bec
PM
4015static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
4016 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
7a0e58fa 4017 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 4018 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
18032bec
PM
4019 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
4020 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
7a0e58fa 4021 .access = PL1_RW, .type = ARM_CP_ALIAS,
7e09797c 4022 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
18032bec
PM
4023 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
4024 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
4025 .access = PL1_RW,
7e09797c
PM
4026 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
4027 .resetvalue = 0, },
18032bec
PM
4028 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
4029 .access = PL1_RW,
7e09797c
PM
4030 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
4031 .resetvalue = 0, },
ecce5c3c
PM
4032 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
4033 .access = PL1_RW,
4034 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
4035 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
4036 .access = PL1_RW,
4037 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
06d76f31 4038 /* Protection region base and size registers */
e508a92b
PM
4039 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
4040 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4041 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
4042 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
4043 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4044 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
4045 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
4046 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4047 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
4048 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
4049 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4050 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
4051 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
4052 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4053 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
4054 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
4055 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4056 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
4057 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
4058 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4059 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
4060 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
4061 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
4062 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
18032bec
PM
4063};
4064
cb4a0a34
PM
4065static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4066 uint64_t value)
ecce5c3c 4067{
cb4a0a34 4068 ARMCPU *cpu = env_archcpu(env);
2ebcebe2 4069
e389be16
FA
4070 if (!arm_feature(env, ARM_FEATURE_V8)) {
4071 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
cb4a0a34
PM
4072 /*
4073 * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
4074 * using Long-descriptor translation table format
4075 */
e389be16
FA
4076 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
4077 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
cb4a0a34
PM
4078 /*
4079 * In an implementation that includes the Security Extensions
e389be16
FA
4080 * TTBCR has additional fields PD0 [4] and PD1 [5] for
4081 * Short-descriptor translation table format.
4082 */
4083 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
4084 } else {
4085 value &= TTBCR_N;
4086 }
e42c4db3 4087 }
e389be16 4088
d4e6df63 4089 if (arm_feature(env, ARM_FEATURE_LPAE)) {
9b37a28c
FR
4090 /*
4091 * With LPAE the TTBCR could result in a change of ASID
d4e6df63
PM
4092 * via the TTBCR.A1 bit, so do a TLB flush.
4093 */
d10eb08f 4094 tlb_flush(CPU(cpu));
d4e6df63 4095 }
cb4a0a34 4096 raw_write(env, ri, value);
ecce5c3c
PM
4097}
4098
d06dc933 4099static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri,
cb2e37df
PM
4100 uint64_t value)
4101{
2fc0cc0e 4102 ARMCPU *cpu = env_archcpu(env);
00c8cb0a 4103
cb2e37df 4104 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
d10eb08f 4105 tlb_flush(CPU(cpu));
cb4a0a34 4106 raw_write(env, ri, value);
cb2e37df
PM
4107}
4108
327ed10f
PM
4109static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4110 uint64_t value)
4111{
93f379b0
RH
4112 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */
4113 if (cpreg_field_is_64bit(ri) &&
4114 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
2fc0cc0e 4115 ARMCPU *cpu = env_archcpu(env);
d10eb08f 4116 tlb_flush(CPU(cpu));
327ed10f
PM
4117 }
4118 raw_write(env, ri, value);
4119}
4120
ed30da8e
RH
4121static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4122 uint64_t value)
4123{
d06dc933
RH
4124 /*
4125 * If we are running with E2&0 regime, then an ASID is active.
4126 * Flush if that might be changing. Note we're not checking
4127 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that
4128 * holds the active ASID, only checking the field that might.
4129 */
4130 if (extract64(raw_read(env, ri) ^ value, 48, 16) &&
4131 (arm_hcr_el2_eff(env) & HCR_E2H)) {
b6ad6062
RDC
4132 uint16_t mask = ARMMMUIdxBit_E20_2 |
4133 ARMMMUIdxBit_E20_2_PAN |
4134 ARMMMUIdxBit_E20_0;
b6ad6062 4135 tlb_flush_by_mmuidx(env_cpu(env), mask);
d06dc933 4136 }
ed30da8e
RH
4137 raw_write(env, ri, value);
4138}
4139
b698e9cf
EI
4140static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4141 uint64_t value)
4142{
2fc0cc0e 4143 ARMCPU *cpu = env_archcpu(env);
b698e9cf
EI
4144 CPUState *cs = CPU(cpu);
4145
97fa9350
RH
4146 /*
4147 * A change in VMID to the stage2 page table (Stage2) invalidates
575a94af 4148 * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0).
97fa9350 4149 */
00b20ee4 4150 if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) {
575a94af 4151 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env));
b698e9cf 4152 }
00b20ee4 4153 raw_write(env, ri, value);
b698e9cf
EI
4154}
4155
8e5d75c9 4156static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
18032bec 4157 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218 4158 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
4a7e2d73 4159 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
b061a82b 4160 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
18032bec 4161 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
84929218 4162 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
88ca1c2d
FA
4163 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
4164 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
8e5d75c9 4165 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
84929218 4166 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
8e5d75c9
PC
4167 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
4168 offsetof(CPUARMState, cp15.dfar_ns) } },
4169 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
4170 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
84929218 4171 .access = PL1_RW, .accessfn = access_tvm_trvm,
b19ed03c 4172 .fgt = FGT_FAR_EL1,
84929218 4173 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
8e5d75c9 4174 .resetvalue = 0, },
8e5d75c9
PC
4175};
4176
4177static const ARMCPRegInfo vmsa_cp_reginfo[] = {
6cd8a264
RH
4178 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
4179 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
84929218 4180 .access = PL1_RW, .accessfn = access_tvm_trvm,
b19ed03c 4181 .fgt = FGT_ESR_EL1,
d81c519c 4182 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
327ed10f 4183 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 4184 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
84929218 4185 .access = PL1_RW, .accessfn = access_tvm_trvm,
bd8db7d9 4186 .fgt = FGT_TTBR0_EL1,
84929218 4187 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
4188 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4189 offsetof(CPUARMState, cp15.ttbr0_ns) } },
327ed10f 4190 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
7dd8c9af 4191 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
84929218 4192 .access = PL1_RW, .accessfn = access_tvm_trvm,
bd8db7d9 4193 .fgt = FGT_TTBR1_EL1,
84929218 4194 .writefn = vmsa_ttbr_write, .resetvalue = 0,
7dd8c9af
FA
4195 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4196 offsetof(CPUARMState, cp15.ttbr1_ns) } },
cb2e37df
PM
4197 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
4198 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218 4199 .access = PL1_RW, .accessfn = access_tvm_trvm,
67dd8030 4200 .fgt = FGT_TCR_EL1,
84929218 4201 .writefn = vmsa_tcr_el12_write,
cb4a0a34
PM
4202 .raw_writefn = raw_write,
4203 .resetvalue = 0,
11f136ee 4204 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
cb2e37df 4205 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
84929218
RH
4206 .access = PL1_RW, .accessfn = access_tvm_trvm,
4207 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
cb4a0a34
PM
4208 .raw_writefn = raw_write,
4209 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
4210 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
18032bec
PM
4211};
4212
9b37a28c
FR
4213/*
4214 * Note that unlike TTBCR, writing to TTBCR2 does not require flushing
ab638a32
RH
4215 * qemu tlbs nor adjusting cached masks.
4216 */
4217static const ARMCPRegInfo ttbcr2_reginfo = {
4218 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3,
84929218
RH
4219 .access = PL1_RW, .accessfn = access_tvm_trvm,
4220 .type = ARM_CP_ALIAS,
d102058e 4221 .bank_fieldoffsets = {
cb4a0a34
PM
4222 offsetofhigh32(CPUARMState, cp15.tcr_el[3]),
4223 offsetofhigh32(CPUARMState, cp15.tcr_el[1]),
d102058e 4224 },
ab638a32
RH
4225};
4226
c4241c7d
PM
4227static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
4228 uint64_t value)
1047b9d7
PM
4229{
4230 env->cp15.c15_ticonfig = value & 0xe7;
4231 /* The OS_TYPE bit in this register changes the reported CPUID! */
4232 env->cp15.c0_cpuid = (value & (1 << 5)) ?
4233 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1047b9d7
PM
4234}
4235
c4241c7d
PM
4236static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
4237 uint64_t value)
1047b9d7
PM
4238{
4239 env->cp15.c15_threadid = value & 0xffff;
1047b9d7
PM
4240}
4241
c4241c7d
PM
4242static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
4243 uint64_t value)
1047b9d7
PM
4244{
4245 /* Wait-for-interrupt (deprecated) */
2fc0cc0e 4246 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT);
1047b9d7
PM
4247}
4248
c4241c7d
PM
4249static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
4250 uint64_t value)
c4804214 4251{
9b37a28c
FR
4252 /*
4253 * On OMAP there are registers indicating the max/min index of dcache lines
c4804214
PM
4254 * containing a dirty line; cache flush operations have to reset these.
4255 */
4256 env->cp15.c15_i_max = 0x000;
4257 env->cp15.c15_i_min = 0xff0;
c4804214
PM
4258}
4259
18032bec
PM
4260static const ARMCPRegInfo omap_cp_reginfo[] = {
4261 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
4262 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
d81c519c 4263 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
6cd8a264 4264 .resetvalue = 0, },
1047b9d7
PM
4265 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
4266 .access = PL1_RW, .type = ARM_CP_NOP },
4267 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
4268 .access = PL1_RW,
4269 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
4270 .writefn = omap_ticonfig_write },
4271 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
4272 .access = PL1_RW,
4273 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
4274 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
4275 .access = PL1_RW, .resetvalue = 0xff0,
4276 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
4277 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
4278 .access = PL1_RW,
4279 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
4280 .writefn = omap_threadid_write },
4281 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
4282 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
7a0e58fa 4283 .type = ARM_CP_NO_RAW,
1047b9d7 4284 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
9b37a28c
FR
4285 /*
4286 * TODO: Peripheral port remap register:
1047b9d7
PM
4287 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
4288 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
4289 * when MMU is off.
4290 */
c4804214 4291 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
d4e6df63 4292 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
7a0e58fa 4293 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
c4804214 4294 .writefn = omap_cachemaint_write },
34f90529
PM
4295 { .name = "C9", .cp = 15, .crn = 9,
4296 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
4297 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
1047b9d7
PM
4298};
4299
c4241c7d
PM
4300static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
4301 uint64_t value)
1047b9d7 4302{
c0f4af17 4303 env->cp15.c15_cpar = value & 0x3fff;
1047b9d7
PM
4304}
4305
4306static const ARMCPRegInfo xscale_cp_reginfo[] = {
4307 { .name = "XSCALE_CPAR",
4308 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
4309 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
4310 .writefn = xscale_cpar_write, },
2771db27
PM
4311 { .name = "XSCALE_AUXCR",
4312 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
4313 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
4314 .resetvalue = 0, },
9b37a28c
FR
4315 /*
4316 * XScale specific cache-lockdown: since we have no cache we NOP these
3b771579
PM
4317 * and hope the guest does not really rely on cache behaviour.
4318 */
4319 { .name = "XSCALE_LOCK_ICACHE_LINE",
4320 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
4321 .access = PL1_W, .type = ARM_CP_NOP },
4322 { .name = "XSCALE_UNLOCK_ICACHE",
4323 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
4324 .access = PL1_W, .type = ARM_CP_NOP },
4325 { .name = "XSCALE_DCACHE_LOCK",
4326 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
4327 .access = PL1_RW, .type = ARM_CP_NOP },
4328 { .name = "XSCALE_UNLOCK_DCACHE",
4329 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
4330 .access = PL1_W, .type = ARM_CP_NOP },
1047b9d7
PM
4331};
4332
4333static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
9b37a28c
FR
4334 /*
4335 * RAZ/WI the whole crn=15 space, when we don't have a more specific
1047b9d7
PM
4336 * implementation of this implementation-defined space.
4337 * Ideally this should eventually disappear in favour of actually
4338 * implementing the correct behaviour for all cores.
4339 */
4340 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
4341 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
3671cd87 4342 .access = PL1_RW,
7a0e58fa 4343 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
d4e6df63 4344 .resetvalue = 0 },
18032bec
PM
4345};
4346
c4804214
PM
4347static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
4348 /* Cache status: RAZ because we have no cache so it's always clean */
4349 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
7a0e58fa 4350 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4351 .resetvalue = 0 },
c4804214
PM
4352};
4353
4354static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
a07d9df0 4355 /* We never have a block transfer operation in progress */
c4804214 4356 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
7a0e58fa 4357 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4358 .resetvalue = 0 },
30b05bba
PM
4359 /* The cache ops themselves: these all NOP for QEMU */
4360 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
04215eb1 4361 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4362 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
04215eb1 4363 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4364 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
04215eb1 4365 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4366 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
04215eb1 4367 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4368 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
04215eb1 4369 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
30b05bba 4370 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
04215eb1 4371 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT },
c4804214
PM
4372};
4373
4374static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
9b37a28c
FR
4375 /*
4376 * The cache test-and-clean instructions always return (1 << 30)
c4804214
PM
4377 * to indicate that there are no dirty cache lines.
4378 */
4379 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
7a0e58fa 4380 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4381 .resetvalue = (1 << 30) },
c4804214 4382 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
7a0e58fa 4383 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
d4e6df63 4384 .resetvalue = (1 << 30) },
c4804214
PM
4385};
4386
34f90529
PM
4387static const ARMCPRegInfo strongarm_cp_reginfo[] = {
4388 /* Ignore ReadBuffer accesses */
4389 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
4390 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
d4e6df63 4391 .access = PL1_RW, .resetvalue = 0,
7a0e58fa 4392 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
34f90529
PM
4393};
4394
731de9e6
EI
4395static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4396{
731de9e6 4397 unsigned int cur_el = arm_current_el(env);
731de9e6 4398
e6ef0169 4399 if (arm_is_el2_enabled(env) && cur_el == 1) {
731de9e6
EI
4400 return env->cp15.vpidr_el2;
4401 }
4402 return raw_read(env, ri);
4403}
4404
06a7e647 4405static uint64_t mpidr_read_val(CPUARMState *env)
81bdde9d 4406{
2fc0cc0e 4407 ARMCPU *cpu = env_archcpu(env);
eb5e1d3c
PF
4408 uint64_t mpidr = cpu->mp_affinity;
4409
81bdde9d 4410 if (arm_feature(env, ARM_FEATURE_V7MP)) {
78dbbbe4 4411 mpidr |= (1U << 31);
9b37a28c
FR
4412 /*
4413 * Cores which are uniprocessor (non-coherent)
81bdde9d 4414 * but still implement the MP extensions set
a8e81b31 4415 * bit 30. (For instance, Cortex-R5).
81bdde9d 4416 */
a8e81b31
PC
4417 if (cpu->mp_is_up) {
4418 mpidr |= (1u << 30);
4419 }
81bdde9d 4420 }
c4241c7d 4421 return mpidr;
81bdde9d
PM
4422}
4423
06a7e647
EI
4424static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
4425{
f0d574d6 4426 unsigned int cur_el = arm_current_el(env);
f0d574d6 4427
e6ef0169 4428 if (arm_is_el2_enabled(env) && cur_el == 1) {
f0d574d6
EI
4429 return env->cp15.vmpidr_el2;
4430 }
06a7e647
EI
4431 return mpidr_read_val(env);
4432}
4433
7ac681cf 4434static const ARMCPRegInfo lpae_cp_reginfo[] = {
a903c449 4435 /* NOP AMAIR0/1 */
b0fe2427
PM
4436 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
4437 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
84929218 4438 .access = PL1_RW, .accessfn = access_tvm_trvm,
158c276c 4439 .fgt = FGT_AMAIR_EL1,
84929218 4440 .type = ARM_CP_CONST, .resetvalue = 0 },
b0fe2427 4441 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
7ac681cf 4442 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
84929218
RH
4443 .access = PL1_RW, .accessfn = access_tvm_trvm,
4444 .type = ARM_CP_CONST, .resetvalue = 0 },
891a2fe7 4445 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
01c097f7
FA
4446 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
4447 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
4448 offsetof(CPUARMState, cp15.par_ns)} },
891a2fe7 4449 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
84929218
RH
4450 .access = PL1_RW, .accessfn = access_tvm_trvm,
4451 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4452 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
4453 offsetof(CPUARMState, cp15.ttbr0_ns) },
b061a82b 4454 .writefn = vmsa_ttbr_write, },
891a2fe7 4455 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
84929218
RH
4456 .access = PL1_RW, .accessfn = access_tvm_trvm,
4457 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
7dd8c9af
FA
4458 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
4459 offsetof(CPUARMState, cp15.ttbr1_ns) },
b061a82b 4460 .writefn = vmsa_ttbr_write, },
7ac681cf
PM
4461};
4462
c4241c7d 4463static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4464{
c4241c7d 4465 return vfp_get_fpcr(env);
b0d2b7d0
PM
4466}
4467
c4241c7d
PM
4468static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4469 uint64_t value)
b0d2b7d0
PM
4470{
4471 vfp_set_fpcr(env, value);
b0d2b7d0
PM
4472}
4473
c4241c7d 4474static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
b0d2b7d0 4475{
c4241c7d 4476 return vfp_get_fpsr(env);
b0d2b7d0
PM
4477}
4478
c4241c7d
PM
4479static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4480 uint64_t value)
b0d2b7d0
PM
4481{
4482 vfp_set_fpsr(env, value);
b0d2b7d0
PM
4483}
4484
3f208fd7
PM
4485static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
4486 bool isread)
c2b820fe 4487{
aaec1432 4488 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) {
c2b820fe
PM
4489 return CP_ACCESS_TRAP;
4490 }
4491 return CP_ACCESS_OK;
4492}
4493
4494static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
4495 uint64_t value)
4496{
4497 env->daif = value & PSTATE_DAIF;
4498}
4499
220f508f
RH
4500static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri)
4501{
4502 return env->pstate & PSTATE_PAN;
4503}
4504
4505static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri,
4506 uint64_t value)
4507{
4508 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN);
4509}
4510
4511static const ARMCPRegInfo pan_reginfo = {
4512 .name = "PAN", .state = ARM_CP_STATE_AA64,
4513 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3,
4514 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4515 .readfn = aa64_pan_read, .writefn = aa64_pan_write
4516};
4517
9eeb7a1c
RH
4518static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri)
4519{
4520 return env->pstate & PSTATE_UAO;
4521}
4522
4523static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri,
4524 uint64_t value)
4525{
4526 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO);
4527}
4528
4529static const ARMCPRegInfo uao_reginfo = {
4530 .name = "UAO", .state = ARM_CP_STATE_AA64,
4531 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4,
4532 .type = ARM_CP_NO_RAW, .access = PL1_RW,
4533 .readfn = aa64_uao_read, .writefn = aa64_uao_write
4534};
4535
dc8b1853
RC
4536static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri)
4537{
4538 return env->pstate & PSTATE_DIT;
4539}
4540
4541static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri,
4542 uint64_t value)
4543{
4544 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT);
4545}
4546
4547static const ARMCPRegInfo dit_reginfo = {
4548 .name = "DIT", .state = ARM_CP_STATE_AA64,
4549 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5,
4550 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4551 .readfn = aa64_dit_read, .writefn = aa64_dit_write
4552};
4553
f2f68a78
RC
4554static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri)
4555{
4556 return env->pstate & PSTATE_SSBS;
4557}
4558
4559static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri,
4560 uint64_t value)
4561{
4562 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS);
4563}
4564
4565static const ARMCPRegInfo ssbs_reginfo = {
4566 .name = "SSBS", .state = ARM_CP_STATE_AA64,
4567 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6,
4568 .type = ARM_CP_NO_RAW, .access = PL0_RW,
4569 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write
4570};
4571
38262d8a
RH
4572static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env,
4573 const ARMCPRegInfo *ri,
4574 bool isread)
8af35c37 4575{
38262d8a
RH
4576 /* Cache invalidate/clean to Point of Coherency or Persistence... */
4577 switch (arm_current_el(env)) {
4578 case 0:
4579 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4580 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4581 return CP_ACCESS_TRAP;
4582 }
4583 /* fall through */
4584 case 1:
4585 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */
4586 if (arm_hcr_el2_eff(env) & HCR_TPCP) {
4587 return CP_ACCESS_TRAP_EL2;
4588 }
4589 break;
8af35c37
PM
4590 }
4591 return CP_ACCESS_OK;
4592}
4593
2d3ce4c6 4594static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags)
1bed4d2e 4595{
38262d8a 4596 /* Cache invalidate/clean to Point of Unification... */
1bed4d2e
RH
4597 switch (arm_current_el(env)) {
4598 case 0:
4599 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */
4600 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) {
4601 return CP_ACCESS_TRAP;
4602 }
4603 /* fall through */
4604 case 1:
2d3ce4c6
PM
4605 /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set. */
4606 if (arm_hcr_el2_eff(env) & hcrflags) {
1bed4d2e
RH
4607 return CP_ACCESS_TRAP_EL2;
4608 }
4609 break;
4610 }
4611 return CP_ACCESS_OK;
4612}
4613
2d3ce4c6
PM
4614static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri,
4615 bool isread)
4616{
4617 return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU);
4618}
4619
4620static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri,
4621 bool isread)
4622{
4623 return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU);
4624}
4625
9b37a28c
FR
4626/*
4627 * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
dbb1fb27
AB
4628 * Page D4-1736 (DDI0487A.b)
4629 */
4630
b7e0730d
RH
4631static int vae1_tlbmask(CPUARMState *env)
4632{
e04a5752 4633 uint64_t hcr = arm_hcr_el2_eff(env);
bc944d3a 4634 uint16_t mask;
e04a5752
RDC
4635
4636 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
bc944d3a
RDC
4637 mask = ARMMMUIdxBit_E20_2 |
4638 ARMMMUIdxBit_E20_2_PAN |
4639 ARMMMUIdxBit_E20_0;
b7e0730d 4640 } else {
bc944d3a 4641 mask = ARMMMUIdxBit_E10_1 |
452ef8cb
RH
4642 ARMMMUIdxBit_E10_1_PAN |
4643 ARMMMUIdxBit_E10_0;
b7e0730d 4644 }
bc944d3a 4645 return mask;
b7e0730d
RH
4646}
4647
ea04dce7
RH
4648/* Return 56 if TBI is enabled, 64 otherwise. */
4649static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx,
4650 uint64_t addr)
4651{
c1547bba 4652 uint64_t tcr = regime_tcr(env, mmu_idx);
ea04dce7
RH
4653 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
4654 int select = extract64(addr, 55, 1);
4655
4656 return (tbi >> select) & 1 ? 56 : 64;
4657}
4658
4659static int vae1_tlbbits(CPUARMState *env, uint64_t addr)
4660{
b6ad6062 4661 uint64_t hcr = arm_hcr_el2_eff(env);
ea04dce7
RH
4662 ARMMMUIdx mmu_idx;
4663
4664 /* Only the regime of the mmu_idx below is significant. */
b6ad6062 4665 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
ea04dce7
RH
4666 mmu_idx = ARMMMUIdx_E20_0;
4667 } else {
4668 mmu_idx = ARMMMUIdx_E10_0;
4669 }
b6ad6062 4670
ea04dce7
RH
4671 return tlbbits_for_regime(env, mmu_idx, addr);
4672}
4673
fd3ed969
PM
4674static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4675 uint64_t value)
168aa23b 4676{
29a0af61 4677 CPUState *cs = env_cpu(env);
b7e0730d 4678 int mask = vae1_tlbmask(env);
dbb1fb27 4679
b7e0730d 4680 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
168aa23b
PM
4681}
4682
b4ab8ce9
PM
4683static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4684 uint64_t value)
4685{
29a0af61 4686 CPUState *cs = env_cpu(env);
b7e0730d 4687 int mask = vae1_tlbmask(env);
b4ab8ce9
PM
4688
4689 if (tlb_force_broadcast(env)) {
527db2be
RH
4690 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
4691 } else {
4692 tlb_flush_by_mmuidx(cs, mask);
b4ab8ce9 4693 }
b4ab8ce9
PM
4694}
4695
85d0dc9f
RH
4696static int e2_tlbmask(CPUARMState *env)
4697{
d902ae75
RH
4698 return (ARMMMUIdxBit_E20_0 |
4699 ARMMMUIdxBit_E20_2 |
4700 ARMMMUIdxBit_E20_2_PAN |
4701 ARMMMUIdxBit_E2);
85d0dc9f
RH
4702}
4703
90c19cdf
RH
4704static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4705 uint64_t value)
4706{
4707 CPUState *cs = env_cpu(env);
4708 int mask = alle1_tlbmask(env);
4709
4710 tlb_flush_by_mmuidx(cs, mask);
4711}
4712
fd3ed969 4713static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
fa439fc5
PM
4714 uint64_t value)
4715{
85d0dc9f
RH
4716 CPUState *cs = env_cpu(env);
4717 int mask = e2_tlbmask(env);
fd3ed969 4718
85d0dc9f 4719 tlb_flush_by_mmuidx(cs, mask);
fd3ed969
PM
4720}
4721
43efaa33
PM
4722static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4723 uint64_t value)
4724{
2fc0cc0e 4725 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4726 CPUState *cs = CPU(cpu);
4727
d902ae75 4728 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3);
43efaa33
PM
4729}
4730
fd3ed969
PM
4731static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4732 uint64_t value)
4733{
29a0af61 4734 CPUState *cs = env_cpu(env);
90c19cdf
RH
4735 int mask = alle1_tlbmask(env);
4736
4737 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
fa439fc5
PM
4738}
4739
2bfb9d75
PM
4740static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4741 uint64_t value)
4742{
29a0af61 4743 CPUState *cs = env_cpu(env);
85d0dc9f 4744 int mask = e2_tlbmask(env);
2bfb9d75 4745
85d0dc9f 4746 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask);
2bfb9d75
PM
4747}
4748
43efaa33
PM
4749static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4750 uint64_t value)
4751{
29a0af61 4752 CPUState *cs = env_cpu(env);
43efaa33 4753
d902ae75 4754 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3);
43efaa33
PM
4755}
4756
fd3ed969
PM
4757static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
4758 uint64_t value)
fa439fc5 4759{
9b37a28c
FR
4760 /*
4761 * Invalidate by VA, EL2
fd3ed969
PM
4762 * Currently handles both VAE2 and VALE2, since we don't support
4763 * flush-last-level-only.
4764 */
85d0dc9f
RH
4765 CPUState *cs = env_cpu(env);
4766 int mask = e2_tlbmask(env);
fd3ed969
PM
4767 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4768
85d0dc9f 4769 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
fd3ed969
PM
4770}
4771
43efaa33
PM
4772static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
4773 uint64_t value)
4774{
9b37a28c
FR
4775 /*
4776 * Invalidate by VA, EL3
43efaa33
PM
4777 * Currently handles both VAE3 and VALE3, since we don't support
4778 * flush-last-level-only.
4779 */
2fc0cc0e 4780 ARMCPU *cpu = env_archcpu(env);
43efaa33
PM
4781 CPUState *cs = CPU(cpu);
4782 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4783
d902ae75 4784 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3);
43efaa33
PM
4785}
4786
fd3ed969
PM
4787static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4788 uint64_t value)
4789{
90c19cdf
RH
4790 CPUState *cs = env_cpu(env);
4791 int mask = vae1_tlbmask(env);
fa439fc5 4792 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4793 int bits = vae1_tlbbits(env, pageaddr);
fa439fc5 4794
ea04dce7 4795 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
fa439fc5
PM
4796}
4797
b4ab8ce9
PM
4798static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4799 uint64_t value)
4800{
9b37a28c
FR
4801 /*
4802 * Invalidate by VA, EL1&0 (AArch64 version).
b4ab8ce9
PM
4803 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
4804 * since we don't support flush-for-specific-ASID-only or
4805 * flush-last-level-only.
4806 */
90c19cdf
RH
4807 CPUState *cs = env_cpu(env);
4808 int mask = vae1_tlbmask(env);
b4ab8ce9 4809 uint64_t pageaddr = sextract64(value << 12, 0, 56);
ea04dce7 4810 int bits = vae1_tlbbits(env, pageaddr);
b4ab8ce9
PM
4811
4812 if (tlb_force_broadcast(env)) {
ea04dce7 4813 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits);
527db2be 4814 } else {
ea04dce7 4815 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits);
b4ab8ce9 4816 }
b4ab8ce9
PM
4817}
4818
fd3ed969
PM
4819static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4820 uint64_t value)
fa439fc5 4821{
29a0af61 4822 CPUState *cs = env_cpu(env);
fd3ed969 4823 uint64_t pageaddr = sextract64(value << 12, 0, 56);
d902ae75 4824 int bits = tlbbits_for_regime(env, ARMMMUIdx_E2, pageaddr);
fa439fc5 4825
d902ae75
RH
4826 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
4827 ARMMMUIdxBit_E2, bits);
fa439fc5
PM
4828}
4829
43efaa33
PM
4830static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4831 uint64_t value)
4832{
29a0af61 4833 CPUState *cs = env_cpu(env);
43efaa33 4834 uint64_t pageaddr = sextract64(value << 12, 0, 56);
d902ae75 4835 int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr);
43efaa33 4836
ea04dce7 4837 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr,
d902ae75 4838 ARMMMUIdxBit_E3, bits);
43efaa33
PM
4839}
4840
575a94af
RH
4841static int ipas2e1_tlbmask(CPUARMState *env, int64_t value)
4842{
4843 /*
4844 * The MSB of value is the NS field, which only applies if SEL2
4845 * is implemented and SCR_EL3.NS is not set (i.e. in secure mode).
4846 */
4847 return (value >= 0
4848 && cpu_isar_feature(aa64_sel2, env_archcpu(env))
4849 && arm_is_secure_below_el3(env)
4850 ? ARMMMUIdxBit_Stage2_S
4851 : ARMMMUIdxBit_Stage2);
4852}
4853
4854static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
4855 uint64_t value)
4856{
4857 CPUState *cs = env_cpu(env);
4858 int mask = ipas2e1_tlbmask(env, value);
4859 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4860
4861 if (tlb_force_broadcast(env)) {
4862 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4863 } else {
4864 tlb_flush_page_by_mmuidx(cs, pageaddr, mask);
4865 }
4866}
4867
4868static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
4869 uint64_t value)
4870{
4871 CPUState *cs = env_cpu(env);
4872 int mask = ipas2e1_tlbmask(env, value);
4873 uint64_t pageaddr = sextract64(value << 12, 0, 56);
4874
4875 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask);
4876}
4877
84940ed8 4878#ifdef TARGET_AARCH64
ab1cdb47
RH
4879typedef struct {
4880 uint64_t base;
84940ed8 4881 uint64_t length;
ab1cdb47
RH
4882} TLBIRange;
4883
3c003f70
PM
4884static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg)
4885{
4886 /*
4887 * Note that the TLBI range TG field encoding differs from both
4888 * TG0 and TG1 encodings.
4889 */
4890 switch (tg) {
4891 case 1:
4892 return Gran4K;
4893 case 2:
4894 return Gran16K;
4895 case 3:
4896 return Gran64K;
4897 default:
4898 return GranInvalid;
4899 }
4900}
4901
ab1cdb47
RH
4902static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx,
4903 uint64_t value)
4904{
4905 unsigned int page_size_granule, page_shift, num, scale, exponent;
3974ff93
RH
4906 /* Extract one bit to represent the va selector in use. */
4907 uint64_t select = sextract64(value, 36, 1);
4908 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true);
ab1cdb47 4909 TLBIRange ret = { };
3c003f70 4910 ARMGranuleSize gran;
84940ed8 4911
84940ed8 4912 page_size_granule = extract64(value, 46, 2);
3c003f70 4913 gran = tlbi_range_tg_to_gran_size(page_size_granule);
84940ed8 4914
3974ff93 4915 /* The granule encoded in value must match the granule in use. */
3c003f70 4916 if (gran != param.gran) {
3974ff93 4917 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n",
84940ed8 4918 page_size_granule);
ab1cdb47 4919 return ret;
84940ed8
RC
4920 }
4921
3c003f70 4922 page_shift = arm_granule_bits(gran);
ab1cdb47
RH
4923 num = extract64(value, 39, 5);
4924 scale = extract64(value, 44, 2);
84940ed8 4925 exponent = (5 * scale) + 1;
84940ed8 4926
ab1cdb47 4927 ret.length = (num + 1) << (exponent + page_shift);
84940ed8 4928
3974ff93 4929 if (param.select) {
d976de21 4930 ret.base = sextract64(value, 0, 37);
84940ed8 4931 } else {
d976de21 4932 ret.base = extract64(value, 0, 37);
84940ed8 4933 }
ef56c242
RH
4934 if (param.ds) {
4935 /*
4936 * With DS=1, BaseADDR is always shifted 16 so that it is able
4937 * to address all 52 va bits. The input address is perforce
4938 * aligned on a 64k boundary regardless of translation granule.
4939 */
4940 page_shift = 16;
4941 }
d976de21 4942 ret.base <<= page_shift;
84940ed8 4943
ab1cdb47 4944 return ret;
84940ed8
RC
4945}
4946
4947static void do_rvae_write(CPUARMState *env, uint64_t value,
4948 int idxmap, bool synced)
4949{
4950 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap);
ab1cdb47 4951 TLBIRange range;
84940ed8
RC
4952 int bits;
4953
ab1cdb47
RH
4954 range = tlbi_aa64_get_range(env, one_idx, value);
4955 bits = tlbbits_for_regime(env, one_idx, range.base);
84940ed8
RC
4956
4957 if (synced) {
4958 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env),
ab1cdb47
RH
4959 range.base,
4960 range.length,
84940ed8
RC
4961 idxmap,
4962 bits);
4963 } else {
ab1cdb47
RH
4964 tlb_flush_range_by_mmuidx(env_cpu(env), range.base,
4965 range.length, idxmap, bits);
84940ed8
RC
4966 }
4967}
4968
4969static void tlbi_aa64_rvae1_write(CPUARMState *env,
4970 const ARMCPRegInfo *ri,
4971 uint64_t value)
4972{
4973 /*
4974 * Invalidate by VA range, EL1&0.
4975 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1,
4976 * since we don't support flush-for-specific-ASID-only or
4977 * flush-last-level-only.
4978 */
4979
4980 do_rvae_write(env, value, vae1_tlbmask(env),
4981 tlb_force_broadcast(env));
4982}
4983
4984static void tlbi_aa64_rvae1is_write(CPUARMState *env,
4985 const ARMCPRegInfo *ri,
4986 uint64_t value)
4987{
4988 /*
4989 * Invalidate by VA range, Inner/Outer Shareable EL1&0.
4990 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS,
4991 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support
4992 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer
4993 * shareable specific flushes.
4994 */
4995
4996 do_rvae_write(env, value, vae1_tlbmask(env), true);
4997}
4998
4999static int vae2_tlbmask(CPUARMState *env)
5000{
d902ae75 5001 return ARMMMUIdxBit_E2;
84940ed8
RC
5002}
5003
5004static void tlbi_aa64_rvae2_write(CPUARMState *env,
5005 const ARMCPRegInfo *ri,
5006 uint64_t value)
5007{
5008 /*
5009 * Invalidate by VA range, EL2.
5010 * Currently handles all of RVAE2 and RVALE2,
5011 * since we don't support flush-for-specific-ASID-only or
5012 * flush-last-level-only.
5013 */
5014
5015 do_rvae_write(env, value, vae2_tlbmask(env),
5016 tlb_force_broadcast(env));
5017
5018
5019}
5020
5021static void tlbi_aa64_rvae2is_write(CPUARMState *env,
5022 const ARMCPRegInfo *ri,
5023 uint64_t value)
5024{
5025 /*
5026 * Invalidate by VA range, Inner/Outer Shareable, EL2.
5027 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS,
5028 * since we don't support flush-for-specific-ASID-only,
5029 * flush-last-level-only or inner/outer shareable specific flushes.
5030 */
5031
5032 do_rvae_write(env, value, vae2_tlbmask(env), true);
5033
5034}
5035
5036static void tlbi_aa64_rvae3_write(CPUARMState *env,
5037 const ARMCPRegInfo *ri,
5038 uint64_t value)
5039{
5040 /*
5041 * Invalidate by VA range, EL3.
5042 * Currently handles all of RVAE3 and RVALE3,
5043 * since we don't support flush-for-specific-ASID-only or
5044 * flush-last-level-only.
5045 */
5046
d902ae75 5047 do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env));
84940ed8
RC
5048}
5049
5050static void tlbi_aa64_rvae3is_write(CPUARMState *env,
5051 const ARMCPRegInfo *ri,
5052 uint64_t value)
5053{
5054 /*
5055 * Invalidate by VA range, EL3, Inner/Outer Shareable.
5056 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS,
5057 * since we don't support flush-for-specific-ASID-only,
5058 * flush-last-level-only or inner/outer specific flushes.
5059 */
5060
d902ae75 5061 do_rvae_write(env, value, ARMMMUIdxBit_E3, true);
84940ed8 5062}
575a94af
RH
5063
5064static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
5065 uint64_t value)
5066{
5067 do_rvae_write(env, value, ipas2e1_tlbmask(env, value),
5068 tlb_force_broadcast(env));
5069}
5070
5071static void tlbi_aa64_ripas2e1is_write(CPUARMState *env,
5072 const ARMCPRegInfo *ri,
5073 uint64_t value)
5074{
5075 do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true);
5076}
84940ed8
RC
5077#endif
5078
3f208fd7
PM
5079static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
5080 bool isread)
aca3f40b 5081{
4351cb72
RH
5082 int cur_el = arm_current_el(env);
5083
5084 if (cur_el < 2) {
5085 uint64_t hcr = arm_hcr_el2_eff(env);
5086
5087 if (cur_el == 0) {
5088 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
5089 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) {
5090 return CP_ACCESS_TRAP_EL2;
5091 }
5092 } else {
5093 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
5094 return CP_ACCESS_TRAP;
5095 }
5096 if (hcr & HCR_TDZ) {
5097 return CP_ACCESS_TRAP_EL2;
5098 }
5099 }
5100 } else if (hcr & HCR_TDZ) {
5101 return CP_ACCESS_TRAP_EL2;
5102 }
aca3f40b
PM
5103 }
5104 return CP_ACCESS_OK;
5105}
5106
5107static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
5108{
2fc0cc0e 5109 ARMCPU *cpu = env_archcpu(env);
aca3f40b
PM
5110 int dzp_bit = 1 << 4;
5111
5112 /* DZP indicates whether DC ZVA access is allowed */
3f208fd7 5113 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
aca3f40b
PM
5114 dzp_bit = 0;
5115 }
5116 return cpu->dcz_blocksize | dzp_bit;
5117}
5118
3f208fd7
PM
5119static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
5120 bool isread)
f502cfc2 5121{
cdcf1405 5122 if (!(env->pstate & PSTATE_SP)) {
9b37a28c
FR
5123 /*
5124 * Access to SP_EL0 is undefined if it's being used as
f502cfc2
PM
5125 * the stack pointer.
5126 */
5127 return CP_ACCESS_TRAP_UNCATEGORIZED;
5128 }
5129 return CP_ACCESS_OK;
5130}
5131
5132static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
5133{
5134 return env->pstate & PSTATE_SP;
5135}
5136
5137static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
5138{
5139 update_spsel(env, val);
5140}
5141
137feaa9
FA
5142static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5143 uint64_t value)
5144{
2fc0cc0e 5145 ARMCPU *cpu = env_archcpu(env);
137feaa9 5146
f00faf13
RH
5147 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) {
5148 /* M bit is RAZ/WI for PMSA with no MPU implemented */
5149 value &= ~SCTLR_M;
5150 }
5151
5152 /* ??? Lots of these bits are not implemented. */
5153
5154 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) {
5155 if (ri->opc1 == 6) { /* SCTLR_EL3 */
5156 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA);
5157 } else {
5158 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF |
5159 SCTLR_ATA0 | SCTLR_ATA);
5160 }
5161 }
5162
137feaa9 5163 if (raw_read(env, ri) == value) {
9b37a28c
FR
5164 /*
5165 * Skip the TLB flush if nothing actually changed; Linux likes
137feaa9
FA
5166 * to do a lot of pointless SCTLR writes.
5167 */
5168 return;
5169 }
5170
5171 raw_write(env, ri, value);
f00faf13 5172
137feaa9 5173 /* This may enable/disable the MMU, so do a TLB flush. */
d10eb08f 5174 tlb_flush(CPU(cpu));
2e5dcf36 5175
2b77ad4d 5176 if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) {
2e5dcf36
RH
5177 /*
5178 * Normally we would always end the TB on an SCTLR write; see the
5179 * comment in ARMCPRegInfo sctlr initialization below for why Xscale
5180 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild
5181 * of hflags from the translator, so do it here.
5182 */
5183 arm_rebuild_hflags(env);
5184 }
137feaa9
FA
5185}
5186
80d2b43b
PM
5187static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
5188 uint64_t value)
a8d64e73 5189{
01765386
PM
5190 /*
5191 * Some MDCR_EL3 bits affect whether PMU counters are running:
5192 * if we are trying to change any of those then we must
5193 * bracket this update with PMU start/finish calls.
5194 */
5195 bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS;
5196
5197 if (pmu_op) {
5198 pmu_op_start(env);
5199 }
80d2b43b 5200 env->cp15.mdcr_el3 = value;
01765386
PM
5201 if (pmu_op) {
5202 pmu_op_finish(env);
5203 }
5204}
5205
80d2b43b
PM
5206static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
5207 uint64_t value)
5208{
5209 /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */
5210 mdcr_el3_write(env, ri, value & SDCR_VALID_MASK);
5211}
5212
01765386
PM
5213static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5214 uint64_t value)
5215{
5216 /*
5217 * Some MDCR_EL2 bits affect whether PMU counters are running:
5218 * if we are trying to change any of those then we must
5219 * bracket this update with PMU start/finish calls.
5220 */
5221 bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS;
5222
5223 if (pmu_op) {
5224 pmu_op_start(env);
5225 }
5226 env->cp15.mdcr_el2 = value;
5227 if (pmu_op) {
5228 pmu_op_finish(env);
5229 }
a8d64e73
PM
5230}
5231
b0d2b7d0 5232static const ARMCPRegInfo v8_cp_reginfo[] = {
9b37a28c
FR
5233 /*
5234 * Minimal set of EL0-visible registers. This will need to be expanded
b0d2b7d0
PM
5235 * significantly for system emulation of AArch64 CPUs.
5236 */
5237 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
5238 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
5239 .access = PL0_RW, .type = ARM_CP_NZCV },
c2b820fe
PM
5240 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
5241 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
7a0e58fa 5242 .type = ARM_CP_NO_RAW,
c2b820fe
PM
5243 .access = PL0_RW, .accessfn = aa64_daif_access,
5244 .fieldoffset = offsetof(CPUARMState, daif),
5245 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
b0d2b7d0
PM
5246 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
5247 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
b916c9c3 5248 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 5249 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
b0d2b7d0
PM
5250 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
5251 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
b916c9c3 5252 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END,
fe03d45f 5253 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
b0d2b7d0
PM
5254 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
5255 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
7a0e58fa 5256 .access = PL0_R, .type = ARM_CP_NO_RAW,
b19ed03c 5257 .fgt = FGT_DCZID_EL0,
aca3f40b
PM
5258 .readfn = aa64_dczid_read },
5259 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
5260 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
5261 .access = PL0_W, .type = ARM_CP_DC_ZVA,
5262#ifndef CONFIG_USER_ONLY
5263 /* Avoid overhead of an access check that always passes in user-mode */
5264 .accessfn = aa64_zva_access,
dd345653 5265 .fgt = FGT_DCZVA,
aca3f40b
PM
5266#endif
5267 },
0eef9d98
PM
5268 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
5269 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
5270 .access = PL1_R, .type = ARM_CP_CURRENTEL },
8af35c37
PM
5271 /* Cache ops: all NOPs since we don't emulate caches */
5272 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
5273 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
38262d8a 5274 .access = PL1_W, .type = ARM_CP_NOP,
dd345653 5275 .fgt = FGT_ICIALLUIS,
2d3ce4c6 5276 .accessfn = access_ticab },
8af35c37
PM
5277 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
5278 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
38262d8a 5279 .access = PL1_W, .type = ARM_CP_NOP,
dd345653 5280 .fgt = FGT_ICIALLU,
2d3ce4c6 5281 .accessfn = access_tocu },
8af35c37
PM
5282 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
5283 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
5284 .access = PL0_W, .type = ARM_CP_NOP,
dd345653 5285 .fgt = FGT_ICIVAU,
2d3ce4c6 5286 .accessfn = access_tocu },
8af35c37
PM
5287 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
5288 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e 5289 .access = PL1_W, .accessfn = aa64_cacheop_poc_access,
dd345653 5290 .fgt = FGT_DCIVAC,
1bed4d2e 5291 .type = ARM_CP_NOP },
8af35c37
PM
5292 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
5293 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
dd345653 5294 .fgt = FGT_DCISW,
1803d271 5295 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
5296 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
5297 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
5298 .access = PL0_W, .type = ARM_CP_NOP,
950037e2 5299 .fgt = FGT_DCCVAC,
1bed4d2e 5300 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
5301 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
5302 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
dd345653 5303 .fgt = FGT_DCCSW,
1803d271 5304 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
8af35c37
PM
5305 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
5306 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
5307 .access = PL0_W, .type = ARM_CP_NOP,
dd345653 5308 .fgt = FGT_DCCVAU,
2d3ce4c6 5309 .accessfn = access_tocu },
8af35c37
PM
5310 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
5311 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
5312 .access = PL0_W, .type = ARM_CP_NOP,
dd345653 5313 .fgt = FGT_DCCIVAC,
1bed4d2e 5314 .accessfn = aa64_cacheop_poc_access },
8af35c37
PM
5315 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
5316 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
dd345653 5317 .fgt = FGT_DCCISW,
1803d271 5318 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP },
168aa23b
PM
5319 /* TLBI operations */
5320 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5321 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
0f66d223 5322 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 5323 .fgt = FGT_TLBIVMALLE1IS,
fd3ed969 5324 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 5325 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5326 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
0f66d223 5327 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 5328 .fgt = FGT_TLBIVAE1IS,
fd3ed969 5329 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5330 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5331 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
0f66d223 5332 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 5333 .fgt = FGT_TLBIASIDE1IS,
fd3ed969 5334 .writefn = tlbi_aa64_vmalle1is_write },
168aa23b 5335 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5336 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
0f66d223 5337 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 5338 .fgt = FGT_TLBIVAAE1IS,
fd3ed969 5339 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5340 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5341 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
0f66d223 5342 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 5343 .fgt = FGT_TLBIVALE1IS,
fd3ed969 5344 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5345 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
6ab9f499 5346 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
0f66d223 5347 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 5348 .fgt = FGT_TLBIVAALE1IS,
fd3ed969 5349 .writefn = tlbi_aa64_vae1is_write },
168aa23b 5350 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5351 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
30881b73 5352 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 5353 .fgt = FGT_TLBIVMALLE1,
fd3ed969 5354 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 5355 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5356 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
30881b73 5357 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 5358 .fgt = FGT_TLBIVAE1,
fd3ed969 5359 .writefn = tlbi_aa64_vae1_write },
168aa23b 5360 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5361 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
30881b73 5362 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 5363 .fgt = FGT_TLBIASIDE1,
fd3ed969 5364 .writefn = tlbi_aa64_vmalle1_write },
168aa23b 5365 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5366 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
30881b73 5367 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 5368 .fgt = FGT_TLBIVAAE1,
fd3ed969 5369 .writefn = tlbi_aa64_vae1_write },
168aa23b 5370 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5371 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73 5372 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 5373 .fgt = FGT_TLBIVALE1,
fd3ed969 5374 .writefn = tlbi_aa64_vae1_write },
168aa23b 5375 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
6ab9f499 5376 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73 5377 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 5378 .fgt = FGT_TLBIVAALE1,
fd3ed969 5379 .writefn = tlbi_aa64_vae1_write },
cea66e91
PM
5380 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
5381 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
575a94af
RH
5382 .access = PL2_W, .type = ARM_CP_NO_RAW,
5383 .writefn = tlbi_aa64_ipas2e1is_write },
cea66e91
PM
5384 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
5385 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
575a94af
RH
5386 .access = PL2_W, .type = ARM_CP_NO_RAW,
5387 .writefn = tlbi_aa64_ipas2e1is_write },
83ddf975
PM
5388 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
5389 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
5390 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 5391 .writefn = tlbi_aa64_alle1is_write },
43efaa33
PM
5392 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
5393 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
5394 .access = PL2_W, .type = ARM_CP_NO_RAW,
5395 .writefn = tlbi_aa64_alle1is_write },
cea66e91
PM
5396 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
5397 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
575a94af
RH
5398 .access = PL2_W, .type = ARM_CP_NO_RAW,
5399 .writefn = tlbi_aa64_ipas2e1_write },
cea66e91
PM
5400 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
5401 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
575a94af
RH
5402 .access = PL2_W, .type = ARM_CP_NO_RAW,
5403 .writefn = tlbi_aa64_ipas2e1_write },
83ddf975
PM
5404 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
5405 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
5406 .access = PL2_W, .type = ARM_CP_NO_RAW,
fd3ed969 5407 .writefn = tlbi_aa64_alle1_write },
43efaa33
PM
5408 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
5409 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
5410 .access = PL2_W, .type = ARM_CP_NO_RAW,
5411 .writefn = tlbi_aa64_alle1is_write },
19525524
PM
5412#ifndef CONFIG_USER_ONLY
5413 /* 64 bit address translation operations */
5414 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
5415 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa 5416 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
132c98cd 5417 .fgt = FGT_ATS1E1R,
0710b2fa 5418 .writefn = ats_write64 },
19525524
PM
5419 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
5420 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa 5421 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
132c98cd 5422 .fgt = FGT_ATS1E1W,
0710b2fa 5423 .writefn = ats_write64 },
19525524
PM
5424 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
5425 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
0710b2fa 5426 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
132c98cd 5427 .fgt = FGT_ATS1E0R,
0710b2fa 5428 .writefn = ats_write64 },
19525524
PM
5429 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
5430 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
0710b2fa 5431 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
132c98cd 5432 .fgt = FGT_ATS1E0W,
0710b2fa 5433 .writefn = ats_write64 },
2a47df95 5434 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
7a379c7e 5435 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
0710b2fa
PM
5436 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5437 .writefn = ats_write64 },
2a47df95 5438 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
7a379c7e 5439 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
0710b2fa
PM
5440 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5441 .writefn = ats_write64 },
2a47df95 5442 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
7a379c7e 5443 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
0710b2fa
PM
5444 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5445 .writefn = ats_write64 },
2a47df95 5446 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
7a379c7e 5447 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
0710b2fa
PM
5448 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5449 .writefn = ats_write64 },
2a47df95
PM
5450 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
5451 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
5452 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
0710b2fa
PM
5453 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5454 .writefn = ats_write64 },
2a47df95
PM
5455 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
5456 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
0710b2fa
PM
5457 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
5458 .writefn = ats_write64 },
c96fc9b5
EI
5459 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
5460 .type = ARM_CP_ALIAS,
5461 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
5462 .access = PL1_RW, .resetvalue = 0,
67dd8030 5463 .fgt = FGT_PAR_EL1,
c96fc9b5
EI
5464 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
5465 .writefn = par_write },
19525524 5466#endif
995939a6 5467 /* TLB invalidate last level of translation table walk */
9449fdf6 5468 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
0f66d223 5469 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
30881b73 5470 .writefn = tlbimva_is_write },
9449fdf6 5471 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
0f66d223 5472 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis,
fa439fc5 5473 .writefn = tlbimvaa_is_write },
9449fdf6 5474 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
30881b73
RH
5475 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5476 .writefn = tlbimva_write },
9449fdf6 5477 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
30881b73
RH
5478 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb,
5479 .writefn = tlbimvaa_write },
541ef8c2
SS
5480 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
5481 .type = ARM_CP_NO_RAW, .access = PL2_W,
5482 .writefn = tlbimva_hyp_write },
5483 { .name = "TLBIMVALHIS",
5484 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
5485 .type = ARM_CP_NO_RAW, .access = PL2_W,
5486 .writefn = tlbimva_hyp_is_write },
5487 { .name = "TLBIIPAS2",
5488 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
575a94af
RH
5489 .type = ARM_CP_NO_RAW, .access = PL2_W,
5490 .writefn = tlbiipas2_hyp_write },
541ef8c2
SS
5491 { .name = "TLBIIPAS2IS",
5492 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
575a94af
RH
5493 .type = ARM_CP_NO_RAW, .access = PL2_W,
5494 .writefn = tlbiipas2is_hyp_write },
541ef8c2
SS
5495 { .name = "TLBIIPAS2L",
5496 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
575a94af
RH
5497 .type = ARM_CP_NO_RAW, .access = PL2_W,
5498 .writefn = tlbiipas2_hyp_write },
541ef8c2
SS
5499 { .name = "TLBIIPAS2LIS",
5500 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
575a94af
RH
5501 .type = ARM_CP_NO_RAW, .access = PL2_W,
5502 .writefn = tlbiipas2is_hyp_write },
9449fdf6
PM
5503 /* 32 bit cache operations */
5504 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
2d3ce4c6 5505 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab },
9449fdf6
PM
5506 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
5507 .type = ARM_CP_NOP, .access = PL1_W },
5508 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
2d3ce4c6 5509 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
9449fdf6 5510 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
2d3ce4c6 5511 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
9449fdf6
PM
5512 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
5513 .type = ARM_CP_NOP, .access = PL1_W },
5514 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
5515 .type = ARM_CP_NOP, .access = PL1_W },
5516 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
1bed4d2e 5517 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5518 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
1803d271 5519 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5520 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
1bed4d2e 5521 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5522 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
1803d271 5523 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5524 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
2d3ce4c6 5525 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu },
9449fdf6 5526 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
1bed4d2e 5527 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access },
9449fdf6 5528 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
1803d271 5529 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
9449fdf6 5530 /* MMU Domain access control / MPU write buffer control */
0c17d68c 5531 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
84929218 5532 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0,
0c17d68c
FA
5533 .writefn = dacr_write, .raw_writefn = raw_write,
5534 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
5535 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
a0618a19 5536 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5537 .type = ARM_CP_ALIAS,
a0618a19 5538 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
6947f059
EI
5539 .access = PL1_RW,
5540 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
a65f1de9 5541 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
7a0e58fa 5542 .type = ARM_CP_ALIAS,
a65f1de9 5543 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5544 .access = PL1_RW,
5545 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
9b37a28c
FR
5546 /*
5547 * We rely on the access checks not allowing the guest to write to the
f502cfc2
PM
5548 * state field when SPSel indicates that it's being used as the stack
5549 * pointer.
5550 */
5551 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
5552 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
5553 .access = PL1_RW, .accessfn = sp_el0_access,
7a0e58fa 5554 .type = ARM_CP_ALIAS,
f502cfc2 5555 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
884b4dee
GB
5556 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
5557 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
beeec926 5558 .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP,
884b4dee 5559 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
f502cfc2
PM
5560 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
5561 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
7a0e58fa 5562 .type = ARM_CP_NO_RAW,
f502cfc2 5563 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
03fbf20f
PM
5564 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
5565 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
696ba377
RH
5566 .access = PL2_RW,
5567 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP,
a4c88675 5568 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) },
6a43e0b6
PM
5569 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
5570 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
696ba377 5571 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6a43e0b6
PM
5572 .writefn = dacr_write, .raw_writefn = raw_write,
5573 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
5574 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
5575 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
696ba377 5576 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP,
6a43e0b6
PM
5577 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
5578 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
5579 .type = ARM_CP_ALIAS,
5580 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
5581 .access = PL2_RW,
5582 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
5583 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
5584 .type = ARM_CP_ALIAS,
5585 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
5586 .access = PL2_RW,
5587 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
5588 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
5589 .type = ARM_CP_ALIAS,
5590 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
5591 .access = PL2_RW,
5592 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
5593 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
5594 .type = ARM_CP_ALIAS,
5595 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
5596 .access = PL2_RW,
5597 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
a8d64e73 5598 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
80d2b43b 5599 .type = ARM_CP_IO,
a8d64e73
PM
5600 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
5601 .resetvalue = 0,
80d2b43b
PM
5602 .access = PL3_RW,
5603 .writefn = mdcr_el3_write,
5604 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
7f4fbfb5 5605 { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO,
a8d64e73
PM
5606 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
5607 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
5608 .writefn = sdcr_write,
5609 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
b0d2b7d0
PM
5610};
5611
d1fb4da2 5612static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask)
f149e3e8 5613{
2fc0cc0e 5614 ARMCPU *cpu = env_archcpu(env);
d1fb4da2
RH
5615
5616 if (arm_feature(env, ARM_FEATURE_V8)) {
5617 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */
5618 } else {
5619 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */
5620 }
f149e3e8
EI
5621
5622 if (arm_feature(env, ARM_FEATURE_EL3)) {
5623 valid_mask &= ~HCR_HCD;
77077a83 5624 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
9b37a28c
FR
5625 /*
5626 * Architecturally HCR.TSC is RES0 if EL3 is not implemented.
77077a83
JK
5627 * However, if we're using the SMC PSCI conduit then QEMU is
5628 * effectively acting like EL3 firmware and so the guest at
5629 * EL2 should retain the ability to prevent EL1 from being
5630 * able to make SMC calls into the ersatz firmware, so in
5631 * that case HCR.TSC should be read/write.
5632 */
f149e3e8
EI
5633 valid_mask &= ~HCR_TSC;
5634 }
d1fb4da2
RH
5635
5636 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
5637 if (cpu_isar_feature(aa64_vh, cpu)) {
5638 valid_mask |= HCR_E2H;
5639 }
da3d8b13
RH
5640 if (cpu_isar_feature(aa64_ras, cpu)) {
5641 valid_mask |= HCR_TERR | HCR_TEA;
5642 }
d1fb4da2
RH
5643 if (cpu_isar_feature(aa64_lor, cpu)) {
5644 valid_mask |= HCR_TLOR;
5645 }
5646 if (cpu_isar_feature(aa64_pauth, cpu)) {
5647 valid_mask |= HCR_API | HCR_APK;
5648 }
8ddb300b
RH
5649 if (cpu_isar_feature(aa64_mte, cpu)) {
5650 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5;
5651 }
7cb1e618
RH
5652 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
5653 valid_mask |= HCR_ENSCXT;
5654 }
8c7e17ef
PM
5655 if (cpu_isar_feature(aa64_fwb, cpu)) {
5656 valid_mask |= HCR_FWB;
5657 }
ef682cdb 5658 }
f149e3e8 5659
d2fd9313
PM
5660 if (cpu_isar_feature(any_evt, cpu)) {
5661 valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4;
5662 } else if (cpu_isar_feature(any_half_evt, cpu)) {
5663 valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4;
5664 }
5665
f149e3e8
EI
5666 /* Clear RES0 bits. */
5667 value &= valid_mask;
5668
8ddb300b
RH
5669 /*
5670 * These bits change the MMU setup:
f149e3e8
EI
5671 * HCR_VM enables stage 2 translation
5672 * HCR_PTW forbids certain page-table setups
8ddb300b
RH
5673 * HCR_DC disables stage1 and enables stage2 translation
5674 * HCR_DCT enables tagging on (disabled) stage1 translation
8c7e17ef 5675 * HCR_FWB changes the interpretation of stage2 descriptor bits
f149e3e8 5676 */
8c7e17ef
PM
5677 if ((env->cp15.hcr_el2 ^ value) &
5678 (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB)) {
d10eb08f 5679 tlb_flush(CPU(cpu));
f149e3e8 5680 }
ce4afed8 5681 env->cp15.hcr_el2 = value;
89430fc6
PM
5682
5683 /*
5684 * Updates to VI and VF require us to update the status of
5685 * virtual interrupts, which are the logical OR of these bits
5686 * and the state of the input lines from the GIC. (This requires
5687 * that we have the iothread lock, which is done by marking the
5688 * reginfo structs as ARM_CP_IO.)
5689 * Note that if a write to HCR pends a VIRQ or VFIQ it is never
5690 * possible for it to be taken immediately, because VIRQ and
5691 * VFIQ are masked unless running at EL0 or EL1, and HCR
5692 * can only be written at EL2.
5693 */
5694 g_assert(qemu_mutex_iothread_locked());
5695 arm_cpu_update_virq(cpu);
5696 arm_cpu_update_vfiq(cpu);
3c29632f 5697 arm_cpu_update_vserr(cpu);
ce4afed8
PM
5698}
5699
d1fb4da2
RH
5700static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
5701{
5702 do_hcr_write(env, value, 0);
5703}
5704
ce4afed8
PM
5705static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri,
5706 uint64_t value)
5707{
5708 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */
5709 value = deposit64(env->cp15.hcr_el2, 32, 32, value);
d1fb4da2 5710 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32));
ce4afed8
PM
5711}
5712
5713static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri,
5714 uint64_t value)
5715{
5716 /* Handle HCR write, i.e. write to low half of HCR_EL2 */
5717 value = deposit64(env->cp15.hcr_el2, 0, 32, value);
d1fb4da2 5718 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32));
f149e3e8
EI
5719}
5720
f7778444 5721/*
b74c0443 5722 * Return the effective value of HCR_EL2, at the given security state.
f7778444
RH
5723 * Bits that are not included here:
5724 * RW (read from SCR_EL3.RW as needed)
5725 */
b74c0443 5726uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, bool secure)
f7778444
RH
5727{
5728 uint64_t ret = env->cp15.hcr_el2;
5729
b74c0443 5730 if (!arm_is_el2_enabled_secstate(env, secure)) {
f7778444
RH
5731 /*
5732 * "This register has no effect if EL2 is not enabled in the
5733 * current Security state". This is ARMv8.4-SecEL2 speak for
5734 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1).
5735 *
5736 * Prior to that, the language was "In an implementation that
5737 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves
5738 * as if this field is 0 for all purposes other than a direct
5739 * read or write access of HCR_EL2". With lots of enumeration
5740 * on a per-field basis. In current QEMU, this is condition
5741 * is arm_is_secure_below_el3.
5742 *
5743 * Since the v8.4 language applies to the entire register, and
5744 * appears to be backward compatible, use that.
5745 */
4990e1d3
RH
5746 return 0;
5747 }
5748
5749 /*
5750 * For a cpu that supports both aarch64 and aarch32, we can set bits
5751 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32.
5752 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32.
5753 */
5754 if (!arm_el_is_aa64(env, 2)) {
5755 uint64_t aa32_valid;
5756
5757 /*
5758 * These bits are up-to-date as of ARMv8.6.
5759 * For HCR, it's easiest to list just the 2 bits that are invalid.
5760 * For HCR2, list those that are valid.
5761 */
5762 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ);
5763 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE |
5764 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS);
5765 ret &= aa32_valid;
5766 }
5767
5768 if (ret & HCR_TGE) {
5769 /* These bits are up-to-date as of ARMv8.6. */
f7778444
RH
5770 if (ret & HCR_E2H) {
5771 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO |
5772 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE |
5773 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU |
4990e1d3
RH
5774 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE |
5775 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT |
5776 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5);
f7778444
RH
5777 } else {
5778 ret |= HCR_FMO | HCR_IMO | HCR_AMO;
5779 }
5780 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE |
5781 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR |
5782 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM |
5783 HCR_TLOR);
5784 }
5785
5786 return ret;
5787}
5788
b74c0443
RH
5789uint64_t arm_hcr_el2_eff(CPUARMState *env)
5790{
5791 return arm_hcr_el2_eff_secstate(env, arm_is_secure_below_el3(env));
5792}
5793
19668718
RH
5794/*
5795 * Corresponds to ARM pseudocode function ELIsInHost().
5796 */
5797bool el_is_in_host(CPUARMState *env, int el)
5798{
5799 uint64_t mask;
5800
5801 /*
5802 * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff().
5803 * Perform the simplest bit tests first, and validate EL2 afterward.
5804 */
5805 if (el & 1) {
5806 return false; /* EL1 or EL3 */
5807 }
5808
5809 /*
5810 * Note that hcr_write() checks isar_feature_aa64_vh(),
5811 * aka HaveVirtHostExt(), in allowing HCR_E2H to be set.
5812 */
5813 mask = el ? HCR_E2H : HCR_E2H | HCR_TGE;
5814 if ((env->cp15.hcr_el2 & mask) != mask) {
5815 return false;
5816 }
5817
5818 /* TGE and/or E2H set: double check those bits are currently legal. */
5819 return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2);
5820}
5821
5814d587
RH
5822static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri,
5823 uint64_t value)
5824{
5825 uint64_t valid_mask = 0;
5826
5827 /* No features adding bits to HCRX are implemented. */
5828
5829 /* Clear RES0 bits. */
5830 env->cp15.hcrx_el2 = value & valid_mask;
5831}
5832
5833static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri,
5834 bool isread)
5835{
5836 if (arm_current_el(env) < 3
5837 && arm_feature(env, ARM_FEATURE_EL3)
5838 && !(env->cp15.scr_el3 & SCR_HXEN)) {
5839 return CP_ACCESS_TRAP_EL3;
5840 }
5841 return CP_ACCESS_OK;
5842}
5843
5844static const ARMCPRegInfo hcrx_el2_reginfo = {
5845 .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64,
5846 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2,
5847 .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen,
5848 .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2),
5849};
5850
5851/* Return the effective value of HCRX_EL2. */
5852uint64_t arm_hcrx_el2_eff(CPUARMState *env)
5853{
5854 /*
5855 * The bits in this register behave as 0 for all purposes other than
5856 * direct reads of the register if:
5857 * - EL2 is not enabled in the current security state,
5858 * - SCR_EL3.HXEn is 0.
5859 */
5860 if (!arm_is_el2_enabled(env)
5861 || (arm_feature(env, ARM_FEATURE_EL3)
5862 && !(env->cp15.scr_el3 & SCR_HXEN))) {
5863 return 0;
5864 }
5865 return env->cp15.hcrx_el2;
5866}
5867
fc1120a7
PM
5868static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri,
5869 uint64_t value)
5870{
5871 /*
5872 * For A-profile AArch32 EL3, if NSACR.CP10
5873 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5874 */
5875 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5876 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39
RH
5877 uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
5878 value = (value & ~mask) | (env->cp15.cptr_el[2] & mask);
fc1120a7
PM
5879 }
5880 env->cp15.cptr_el[2] = value;
5881}
5882
5883static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri)
5884{
5885 /*
5886 * For A-profile AArch32 EL3, if NSACR.CP10
5887 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1.
5888 */
5889 uint64_t value = env->cp15.cptr_el[2];
5890
5891 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
5892 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) {
fab8ad39 5893 value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK;
fc1120a7
PM
5894 }
5895 return value;
5896}
5897
4771cd01 5898static const ARMCPRegInfo el2_cp_reginfo[] = {
f149e3e8 5899 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
89430fc6 5900 .type = ARM_CP_IO,
f149e3e8
EI
5901 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5902 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5903 .writefn = hcr_write },
ce4afed8 5904 { .name = "HCR", .state = ARM_CP_STATE_AA32,
89430fc6 5905 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
5906 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
5907 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
c624ea0f 5908 .writefn = hcr_writelow },
831a2fca
PM
5909 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH,
5910 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7,
5911 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3b685ba7 5912 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5913 .type = ARM_CP_ALIAS,
3b685ba7
EI
5914 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
5915 .access = PL2_RW,
5916 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
68e78e33 5917 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH,
f2c30f42
EI
5918 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
5919 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
cba517c3 5920 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH,
63b60551
EI
5921 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
5922 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
cba517c3
PM
5923 { .name = "HIFAR", .state = ARM_CP_STATE_AA32,
5924 .type = ARM_CP_ALIAS,
5925 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2,
5926 .access = PL2_RW,
5927 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) },
3b685ba7 5928 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
7a0e58fa 5929 .type = ARM_CP_ALIAS,
3b685ba7 5930 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
5931 .access = PL2_RW,
5932 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
d79e0c06 5933 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH,
d42e3c26
EI
5934 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
5935 .access = PL2_RW, .writefn = vbar_write,
5936 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
5937 .resetvalue = 0 },
884b4dee
GB
5938 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
5939 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
7a0e58fa 5940 .access = PL3_RW, .type = ARM_CP_ALIAS,
884b4dee 5941 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
c6f19164
GB
5942 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
5943 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
5944 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
fc1120a7
PM
5945 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]),
5946 .readfn = cptr_el2_read, .writefn = cptr_el2_write },
95f949ac
EI
5947 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
5948 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
5949 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
5950 .resetvalue = 0 },
5951 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5952 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
95f949ac
EI
5953 .access = PL2_RW, .type = ARM_CP_ALIAS,
5954 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
2179ef95
PM
5955 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
5956 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
5957 .access = PL2_RW, .type = ARM_CP_CONST,
5958 .resetvalue = 0 },
5959 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
55b53c71 5960 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32,
b5ede85b 5961 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
2179ef95
PM
5962 .access = PL2_RW, .type = ARM_CP_CONST,
5963 .resetvalue = 0 },
37cd6c24
PM
5964 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
5965 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
5966 .access = PL2_RW, .type = ARM_CP_CONST,
5967 .resetvalue = 0 },
5968 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
5969 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
5970 .access = PL2_RW, .type = ARM_CP_CONST,
5971 .resetvalue = 0 },
06ec4c8c
EI
5972 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
5973 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
d06dc933 5974 .access = PL2_RW, .writefn = vmsa_tcr_el12_write,
06ec4c8c 5975 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
68e9c2fe
EI
5976 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
5977 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5978 .type = ARM_CP_ALIAS,
68e9c2fe 5979 .access = PL2_RW, .accessfn = access_el3_aa32ns,
afbb181c 5980 .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) },
68e9c2fe
EI
5981 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
5982 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
bf06c112 5983 .access = PL2_RW,
988cc190 5984 /* no .writefn needed as this can't cause an ASID change */
68e9c2fe 5985 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
b698e9cf
EI
5986 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
5987 .cp = 15, .opc1 = 6, .crm = 2,
5988 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
5989 .access = PL2_RW, .accessfn = access_el3_aa32ns,
5990 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
5991 .writefn = vttbr_write },
5992 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
5993 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
5994 .access = PL2_RW, .writefn = vttbr_write,
5995 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
b9cb5323
EI
5996 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
5997 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
5998 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
5999 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
ff05f37b
EI
6000 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
6001 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
6002 .access = PL2_RW, .resetvalue = 0,
6003 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
a57633c0
EI
6004 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
6005 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
ed30da8e 6006 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write,
a57633c0
EI
6007 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
6008 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
6009 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
a57633c0 6010 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
541ef8c2
SS
6011 { .name = "TLBIALLNSNH",
6012 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
6013 .type = ARM_CP_NO_RAW, .access = PL2_W,
6014 .writefn = tlbiall_nsnh_write },
6015 { .name = "TLBIALLNSNHIS",
6016 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
6017 .type = ARM_CP_NO_RAW, .access = PL2_W,
6018 .writefn = tlbiall_nsnh_is_write },
6019 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
6020 .type = ARM_CP_NO_RAW, .access = PL2_W,
6021 .writefn = tlbiall_hyp_write },
6022 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
6023 .type = ARM_CP_NO_RAW, .access = PL2_W,
6024 .writefn = tlbiall_hyp_is_write },
6025 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
6026 .type = ARM_CP_NO_RAW, .access = PL2_W,
6027 .writefn = tlbimva_hyp_write },
6028 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
6029 .type = ARM_CP_NO_RAW, .access = PL2_W,
6030 .writefn = tlbimva_hyp_is_write },
51da9014
EI
6031 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
6032 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
696ba377 6033 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 6034 .writefn = tlbi_aa64_alle2_write },
8742d49d
EI
6035 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
6036 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
696ba377 6037 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 6038 .writefn = tlbi_aa64_vae2_write },
2bfb9d75
PM
6039 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
6040 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
696ba377 6041 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75
PM
6042 .writefn = tlbi_aa64_vae2_write },
6043 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
6044 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
696ba377 6045 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75 6046 .writefn = tlbi_aa64_alle2is_write },
8742d49d
EI
6047 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
6048 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
696ba377 6049 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
fd3ed969 6050 .writefn = tlbi_aa64_vae2is_write },
2bfb9d75
PM
6051 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
6052 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
696ba377 6053 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
2bfb9d75 6054 .writefn = tlbi_aa64_vae2is_write },
edac4d8a 6055#ifndef CONFIG_USER_ONLY
9b37a28c
FR
6056 /*
6057 * Unlike the other EL2-related AT operations, these must
2a47df95
PM
6058 * UNDEF from EL3 if EL2 is not implemented, which is why we
6059 * define them here rather than with the rest of the AT ops.
6060 */
6061 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
6062 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6063 .access = PL2_W, .accessfn = at_s1e2_access,
696ba377
RH
6064 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6065 .writefn = ats_write64 },
2a47df95
PM
6066 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
6067 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6068 .access = PL2_W, .accessfn = at_s1e2_access,
696ba377
RH
6069 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF,
6070 .writefn = ats_write64 },
9b37a28c
FR
6071 /*
6072 * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
14db7fe0
PM
6073 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
6074 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
6075 * to behave as if SCR.NS was 1.
6076 */
6077 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
6078 .access = PL2_W,
0710b2fa 6079 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
14db7fe0
PM
6080 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
6081 .access = PL2_W,
0710b2fa 6082 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC },
0b6440af
EI
6083 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
6084 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
9b37a28c
FR
6085 /*
6086 * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
0b6440af
EI
6087 * reset values as IMPDEF. We choose to reset to 3 to comply with
6088 * both ARMv7 and ARMv8.
6089 */
6090 .access = PL2_RW, .resetvalue = 3,
6091 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
edac4d8a
EI
6092 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
6093 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
6094 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
6095 .writefn = gt_cntvoff_write,
6096 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
6097 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
6098 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
6099 .writefn = gt_cntvoff_write,
6100 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
b0e66d95
EI
6101 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
6102 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
6103 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6104 .type = ARM_CP_IO, .access = PL2_RW,
6105 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6106 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
6107 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
6108 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
6109 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
6110 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
6111 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
d44ec156 6112 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
b0e66d95
EI
6113 .resetfn = gt_hyp_timer_reset,
6114 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
6115 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
6116 .type = ARM_CP_IO,
6117 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
6118 .access = PL2_RW,
6119 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
6120 .resetvalue = 0,
6121 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
edac4d8a 6122#endif
59e05530
EI
6123 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
6124 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6125 .access = PL2_RW, .accessfn = access_el3_aa32ns,
6126 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
6127 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
6128 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
6129 .access = PL2_RW,
6130 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
2a5a9abd
AF
6131 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH,
6132 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3,
6133 .access = PL2_RW,
6134 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) },
3b685ba7
EI
6135};
6136
ce4afed8
PM
6137static const ARMCPRegInfo el2_v8_cp_reginfo[] = {
6138 { .name = "HCR2", .state = ARM_CP_STATE_AA32,
89430fc6 6139 .type = ARM_CP_ALIAS | ARM_CP_IO,
ce4afed8
PM
6140 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
6141 .access = PL2_RW,
6142 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2),
6143 .writefn = hcr_writehigh },
ce4afed8
PM
6144};
6145
e9152ee9
RDC
6146static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri,
6147 bool isread)
6148{
6149 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) {
6150 return CP_ACCESS_OK;
6151 }
6152 return CP_ACCESS_TRAP_UNCATEGORIZED;
6153}
6154
6155static const ARMCPRegInfo el2_sec_cp_reginfo[] = {
6156 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64,
6157 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0,
6158 .access = PL2_RW, .accessfn = sel2_access,
6159 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) },
6160 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64,
6161 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2,
6162 .access = PL2_RW, .accessfn = sel2_access,
6163 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) },
e9152ee9
RDC
6164};
6165
2f027fc5
PM
6166static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
6167 bool isread)
6168{
9b37a28c
FR
6169 /*
6170 * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
926c1b97 6171 * At Secure EL1 it traps to EL3 or EL2.
2f027fc5
PM
6172 */
6173 if (arm_current_el(env) == 3) {
6174 return CP_ACCESS_OK;
6175 }
6176 if (arm_is_secure_below_el3(env)) {
926c1b97
RDC
6177 if (env->cp15.scr_el3 & SCR_EEL2) {
6178 return CP_ACCESS_TRAP_EL2;
6179 }
2f027fc5
PM
6180 return CP_ACCESS_TRAP_EL3;
6181 }
6182 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
6183 if (isread) {
6184 return CP_ACCESS_OK;
6185 }
6186 return CP_ACCESS_TRAP_UNCATEGORIZED;
6187}
6188
60fb1a87
GB
6189static const ARMCPRegInfo el3_cp_reginfo[] = {
6190 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
6191 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
6192 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
10d0ef3e 6193 .resetfn = scr_reset, .writefn = scr_write },
f80741d1 6194 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL,
60fb1a87 6195 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
efe4a274
PM
6196 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6197 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
b061a82b 6198 .writefn = scr_write },
60fb1a87
GB
6199 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
6200 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
6201 .access = PL3_RW, .resetvalue = 0,
6202 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
6203 { .name = "SDER",
6204 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
6205 .access = PL3_RW, .resetvalue = 0,
6206 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
60fb1a87 6207 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
efe4a274
PM
6208 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
6209 .writefn = vbar_write, .resetvalue = 0,
60fb1a87 6210 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
7dd8c9af
FA
6211 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
6212 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
f478847f 6213 .access = PL3_RW, .resetvalue = 0,
7dd8c9af 6214 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
11f136ee
FA
6215 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
6216 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
6459b94c 6217 .access = PL3_RW,
cb4a0a34
PM
6218 /* no .writefn needed as this can't cause an ASID change */
6219 .resetvalue = 0,
11f136ee 6220 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
81547d66 6221 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 6222 .type = ARM_CP_ALIAS,
81547d66
EI
6223 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
6224 .access = PL3_RW,
6225 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
f2c30f42 6226 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
f2c30f42
EI
6227 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
6228 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
63b60551
EI
6229 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
6230 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
6231 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
81547d66 6232 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
7a0e58fa 6233 .type = ARM_CP_ALIAS,
81547d66 6234 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
99a99c1f
SB
6235 .access = PL3_RW,
6236 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
a1ba125c
EI
6237 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
6238 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
6239 .access = PL3_RW, .writefn = vbar_write,
6240 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
6241 .resetvalue = 0 },
c6f19164
GB
6242 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
6243 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
6244 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
6245 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
4cfb8ad8
PM
6246 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
6247 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
6248 .access = PL3_RW, .resetvalue = 0,
6249 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
2179ef95
PM
6250 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
6251 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
6252 .access = PL3_RW, .type = ARM_CP_CONST,
6253 .resetvalue = 0 },
37cd6c24
PM
6254 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
6255 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
6256 .access = PL3_RW, .type = ARM_CP_CONST,
6257 .resetvalue = 0 },
6258 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
6259 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
6260 .access = PL3_RW, .type = ARM_CP_CONST,
6261 .resetvalue = 0 },
43efaa33
PM
6262 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
6263 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
6264 .access = PL3_W, .type = ARM_CP_NO_RAW,
6265 .writefn = tlbi_aa64_alle3is_write },
6266 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
6267 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
6268 .access = PL3_W, .type = ARM_CP_NO_RAW,
6269 .writefn = tlbi_aa64_vae3is_write },
6270 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
6271 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
6272 .access = PL3_W, .type = ARM_CP_NO_RAW,
6273 .writefn = tlbi_aa64_vae3is_write },
6274 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
6275 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
6276 .access = PL3_W, .type = ARM_CP_NO_RAW,
6277 .writefn = tlbi_aa64_alle3_write },
6278 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
6279 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
6280 .access = PL3_W, .type = ARM_CP_NO_RAW,
6281 .writefn = tlbi_aa64_vae3_write },
6282 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
6283 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
6284 .access = PL3_W, .type = ARM_CP_NO_RAW,
6285 .writefn = tlbi_aa64_vae3_write },
0f1a3b24
FA
6286};
6287
e2cce18f
RH
6288#ifndef CONFIG_USER_ONLY
6289/* Test if system register redirection is to occur in the current state. */
6290static bool redirect_for_e2h(CPUARMState *env)
6291{
6292 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H);
6293}
6294
6295static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri)
6296{
6297 CPReadFn *readfn;
6298
6299 if (redirect_for_e2h(env)) {
6300 /* Switch to the saved EL2 version of the register. */
6301 ri = ri->opaque;
6302 readfn = ri->readfn;
6303 } else {
6304 readfn = ri->orig_readfn;
6305 }
6306 if (readfn == NULL) {
6307 readfn = raw_read;
6308 }
6309 return readfn(env, ri);
6310}
6311
6312static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri,
6313 uint64_t value)
6314{
6315 CPWriteFn *writefn;
6316
6317 if (redirect_for_e2h(env)) {
6318 /* Switch to the saved EL2 version of the register. */
6319 ri = ri->opaque;
6320 writefn = ri->writefn;
6321 } else {
6322 writefn = ri->orig_writefn;
6323 }
6324 if (writefn == NULL) {
6325 writefn = raw_write;
6326 }
6327 writefn(env, ri, value);
6328}
6329
6330static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu)
6331{
6332 struct E2HAlias {
6333 uint32_t src_key, dst_key, new_key;
6334 const char *src_name, *dst_name, *new_name;
6335 bool (*feature)(const ARMISARegisters *id);
6336 };
6337
6338#define K(op0, op1, crn, crm, op2) \
6339 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2)
6340
6341 static const struct E2HAlias aliases[] = {
6342 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0),
6343 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" },
6344 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2),
6345 "CPACR", "CPTR_EL2", "CPACR_EL12" },
6346 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0),
6347 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" },
6348 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1),
6349 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" },
6350 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2),
6351 "TCR_EL1", "TCR_EL2", "TCR_EL12" },
6352 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0),
6353 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" },
6354 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1),
6355 "ELR_EL1", "ELR_EL2", "ELR_EL12" },
6356 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0),
6357 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" },
6358 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1),
6359 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" },
6360 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0),
6361 "ESR_EL1", "ESR_EL2", "ESR_EL12" },
6362 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0),
6363 "FAR_EL1", "FAR_EL2", "FAR_EL12" },
6364 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0),
6365 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" },
6366 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0),
6367 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" },
6368 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0),
6369 "VBAR", "VBAR_EL2", "VBAR_EL12" },
6370 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1),
6371 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" },
6372 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0),
6373 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" },
6374
6375 /*
6376 * Note that redirection of ZCR is mentioned in the description
6377 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but
6378 * not in the summary table.
6379 */
6380 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0),
6381 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve },
de561988
RH
6382 { K(3, 0, 1, 2, 6), K(3, 4, 1, 2, 6), K(3, 5, 1, 2, 6),
6383 "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme },
e2cce18f 6384
4b779ceb
RH
6385 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0),
6386 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte },
6387
7cb1e618
RH
6388 { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7),
6389 "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12",
6390 isar_feature_aa64_scxtnum },
6391
e2cce18f
RH
6392 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */
6393 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */
6394 };
6395#undef K
6396
6397 size_t i;
6398
6399 for (i = 0; i < ARRAY_SIZE(aliases); i++) {
6400 const struct E2HAlias *a = &aliases[i];
9da35a40 6401 ARMCPRegInfo *src_reg, *dst_reg, *new_reg;
9da35a40 6402 bool ok;
e2cce18f
RH
6403
6404 if (a->feature && !a->feature(&cpu->isar)) {
6405 continue;
6406 }
6407
5860362d
RH
6408 src_reg = g_hash_table_lookup(cpu->cp_regs,
6409 (gpointer)(uintptr_t)a->src_key);
6410 dst_reg = g_hash_table_lookup(cpu->cp_regs,
6411 (gpointer)(uintptr_t)a->dst_key);
e2cce18f
RH
6412 g_assert(src_reg != NULL);
6413 g_assert(dst_reg != NULL);
6414
6415 /* Cross-compare names to detect typos in the keys. */
6416 g_assert(strcmp(src_reg->name, a->src_name) == 0);
6417 g_assert(strcmp(dst_reg->name, a->dst_name) == 0);
6418
6419 /* None of the core system registers use opaque; we will. */
6420 g_assert(src_reg->opaque == NULL);
6421
6422 /* Create alias before redirection so we dup the right data. */
9da35a40 6423 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo));
9da35a40
RH
6424
6425 new_reg->name = a->new_name;
6426 new_reg->type |= ARM_CP_ALIAS;
6427 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */
6428 new_reg->access &= PL2_RW | PL3_RW;
6429
5860362d
RH
6430 ok = g_hash_table_insert(cpu->cp_regs,
6431 (gpointer)(uintptr_t)a->new_key, new_reg);
9da35a40 6432 g_assert(ok);
e2cce18f
RH
6433
6434 src_reg->opaque = dst_reg;
6435 src_reg->orig_readfn = src_reg->readfn ?: raw_read;
6436 src_reg->orig_writefn = src_reg->writefn ?: raw_write;
6437 if (!src_reg->raw_readfn) {
6438 src_reg->raw_readfn = raw_read;
6439 }
6440 if (!src_reg->raw_writefn) {
6441 src_reg->raw_writefn = raw_write;
6442 }
6443 src_reg->readfn = el2_e2h_read;
6444 src_reg->writefn = el2_e2h_write;
6445 }
6446}
6447#endif
6448
3f208fd7
PM
6449static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
6450 bool isread)
7da845b0 6451{
97475a89
RH
6452 int cur_el = arm_current_el(env);
6453
6454 if (cur_el < 2) {
6455 uint64_t hcr = arm_hcr_el2_eff(env);
6456
6457 if (cur_el == 0) {
6458 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
6459 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) {
6460 return CP_ACCESS_TRAP_EL2;
6461 }
6462 } else {
6463 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
6464 return CP_ACCESS_TRAP;
6465 }
6466 if (hcr & HCR_TID2) {
6467 return CP_ACCESS_TRAP_EL2;
6468 }
6469 }
6470 } else if (hcr & HCR_TID2) {
6471 return CP_ACCESS_TRAP_EL2;
6472 }
7da845b0 6473 }
630fcd4d
MZ
6474
6475 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) {
6476 return CP_ACCESS_TRAP_EL2;
6477 }
6478
7da845b0
PM
6479 return CP_ACCESS_OK;
6480}
6481
58e93b48
RH
6482/*
6483 * Check for traps to RAS registers, which are controlled
6484 * by HCR_EL2.TERR and SCR_EL3.TERR.
6485 */
6486static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri,
6487 bool isread)
6488{
6489 int el = arm_current_el(env);
6490
6491 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) {
6492 return CP_ACCESS_TRAP_EL2;
6493 }
6494 if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) {
6495 return CP_ACCESS_TRAP_EL3;
6496 }
6497 return CP_ACCESS_OK;
6498}
6499
6500static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri)
6501{
6502 int el = arm_current_el(env);
6503
6504 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6505 return env->cp15.vdisr_el2;
6506 }
6507 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6508 return 0; /* RAZ/WI */
6509 }
6510 return env->cp15.disr_el1;
6511}
6512
6513static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
6514{
6515 int el = arm_current_el(env);
6516
6517 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) {
6518 env->cp15.vdisr_el2 = val;
6519 return;
6520 }
6521 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) {
6522 return; /* RAZ/WI */
6523 }
6524 env->cp15.disr_el1 = val;
6525}
6526
6527/*
6528 * Minimal RAS implementation with no Error Records.
6529 * Which means that all of the Error Record registers:
6530 * ERXADDR_EL1
6531 * ERXCTLR_EL1
6532 * ERXFR_EL1
6533 * ERXMISC0_EL1
6534 * ERXMISC1_EL1
6535 * ERXMISC2_EL1
6536 * ERXMISC3_EL1
6537 * ERXPFGCDN_EL1 (RASv1p1)
6538 * ERXPFGCTL_EL1 (RASv1p1)
6539 * ERXPFGF_EL1 (RASv1p1)
6540 * ERXSTATUS_EL1
6541 * and
6542 * ERRSELR_EL1
6543 * may generate UNDEFINED, which is the effect we get by not
6544 * listing them at all.
bd8db7d9
PM
6545 *
6546 * These registers have fine-grained trap bits, but UNDEF-to-EL1
6547 * is higher priority than FGT-to-EL2 so we do not need to list them
6548 * in order to check for an FGT.
58e93b48
RH
6549 */
6550static const ARMCPRegInfo minimal_ras_reginfo[] = {
6551 { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH,
6552 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1,
6553 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1),
6554 .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write },
6555 { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH,
6556 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0,
6557 .access = PL1_R, .accessfn = access_terr,
bd8db7d9 6558 .fgt = FGT_ERRIDR_EL1,
58e93b48
RH
6559 .type = ARM_CP_CONST, .resetvalue = 0 },
6560 { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH,
6561 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1,
6562 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) },
6563 { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH,
6564 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3,
6565 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) },
6566};
6567
397d922c
RH
6568/*
6569 * Return the exception level to which exceptions should be taken
6570 * via SVEAccessTrap. This excludes the check for whether the exception
6571 * should be routed through AArch64.AdvSIMDFPAccessTrap. That can easily
6572 * be found by testing 0 < fp_exception_el < sve_exception_el.
6573 *
6574 * C.f. the ARM pseudocode function CheckSVEEnabled. Note that the
6575 * pseudocode does *not* separate out the FP trap checks, but has them
6576 * all in one function.
5be5e8ed 6577 */
ced31551 6578int sve_exception_el(CPUARMState *env, int el)
5be5e8ed
RH
6579{
6580#ifndef CONFIG_USER_ONLY
aa4451b6 6581 if (el <= 1 && !el_is_in_host(env, el)) {
fab8ad39 6582 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) {
7701cee5
RH
6583 case 1:
6584 if (el != 0) {
6585 break;
6586 }
6587 /* fall through */
6588 case 0:
6589 case 2:
61a8c23a 6590 return 1;
5be5e8ed 6591 }
5be5e8ed
RH
6592 }
6593
7d38cb92
RH
6594 if (el <= 2 && arm_is_el2_enabled(env)) {
6595 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6596 if (env->cp15.hcr_el2 & HCR_E2H) {
fab8ad39 6597 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) {
d5a6fa2d 6598 case 1:
7d38cb92 6599 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
d5a6fa2d
RH
6600 break;
6601 }
6602 /* fall through */
6603 case 0:
6604 case 2:
6605 return 2;
6606 }
7d38cb92 6607 } else {
fab8ad39 6608 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) {
d5a6fa2d
RH
6609 return 2;
6610 }
60eed086 6611 }
5be5e8ed
RH
6612 }
6613
60eed086
RH
6614 /* CPTR_EL3. Since EZ is negative we must check for EL3. */
6615 if (arm_feature(env, ARM_FEATURE_EL3)
fab8ad39 6616 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) {
5be5e8ed
RH
6617 return 3;
6618 }
6619#endif
6620 return 0;
6621}
6622
6b2ca83e
RH
6623/*
6624 * Return the exception level to which exceptions should be taken for SME.
6625 * C.f. the ARM pseudocode function CheckSMEAccess.
6626 */
6627int sme_exception_el(CPUARMState *env, int el)
6628{
6629#ifndef CONFIG_USER_ONLY
6630 if (el <= 1 && !el_is_in_host(env, el)) {
6631 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) {
6632 case 1:
6633 if (el != 0) {
6634 break;
6635 }
6636 /* fall through */
6637 case 0:
6638 case 2:
6639 return 1;
6640 }
6641 }
6642
6643 if (el <= 2 && arm_is_el2_enabled(env)) {
6644 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */
6645 if (env->cp15.hcr_el2 & HCR_E2H) {
6646 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) {
6647 case 1:
6648 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) {
6649 break;
6650 }
6651 /* fall through */
6652 case 0:
6653 case 2:
6654 return 2;
6655 }
6656 } else {
6657 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) {
6658 return 2;
6659 }
6660 }
6661 }
6662
6663 /* CPTR_EL3. Since ESM is negative we must check for EL3. */
6664 if (arm_feature(env, ARM_FEATURE_EL3)
6665 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6666 return 3;
6667 }
6668#endif
6669 return 0;
6670}
6671
0ab5953b
RH
6672/*
6673 * Given that SVE is enabled, return the vector length for EL.
6674 */
6ca54aa9 6675uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)
0ab5953b 6676{
2fc0cc0e 6677 ARMCPU *cpu = env_archcpu(env);
6ca54aa9
RH
6678 uint64_t *cr = env->vfp.zcr_el;
6679 uint32_t map = cpu->sve_vq.map;
6680 uint32_t len = ARM_MAX_VQ - 1;
6681
6682 if (sm) {
6683 cr = env->vfp.smcr_el;
6684 map = cpu->sme_vq.map;
6685 }
0ab5953b 6686
c6225beb 6687 if (el <= 1 && !el_is_in_host(env, el)) {
6ca54aa9 6688 len = MIN(len, 0xf & (uint32_t)cr[1]);
0ab5953b 6689 }
6a02a732 6690 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) {
6ca54aa9 6691 len = MIN(len, 0xf & (uint32_t)cr[2]);
0ab5953b 6692 }
6a02a732 6693 if (arm_feature(env, ARM_FEATURE_EL3)) {
6ca54aa9
RH
6694 len = MIN(len, 0xf & (uint32_t)cr[3]);
6695 }
6696
6697 map &= MAKE_64BIT_MASK(0, len + 1);
6698 if (map != 0) {
6699 return 31 - clz32(map);
0ab5953b 6700 }
0df9142d 6701
6ca54aa9
RH
6702 /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */
6703 assert(sm);
6704 return ctz32(cpu->sme_vq.map);
6705}
6706
6707uint32_t sve_vqm1_for_el(CPUARMState *env, int el)
6708{
6709 return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM));
0ab5953b
RH
6710}
6711
5be5e8ed
RH
6712static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6713 uint64_t value)
6714{
0ab5953b 6715 int cur_el = arm_current_el(env);
5ef3cc56 6716 int old_len = sve_vqm1_for_el(env, cur_el);
0ab5953b
RH
6717 int new_len;
6718
5be5e8ed 6719 /* Bits other than [3:0] are RAZ/WI. */
7b351d98 6720 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16);
5be5e8ed 6721 raw_write(env, ri, value & 0xf);
0ab5953b
RH
6722
6723 /*
6724 * Because we arrived here, we know both FP and SVE are enabled;
6725 * otherwise we would have trapped access to the ZCR_ELn register.
6726 */
5ef3cc56 6727 new_len = sve_vqm1_for_el(env, cur_el);
0ab5953b
RH
6728 if (new_len < old_len) {
6729 aarch64_sve_narrow_vq(env, new_len + 1);
6730 }
5be5e8ed
RH
6731}
6732
60360d82
RH
6733static const ARMCPRegInfo zcr_reginfo[] = {
6734 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64,
6735 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0,
6736 .access = PL1_RW, .type = ARM_CP_SVE,
6737 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]),
6738 .writefn = zcr_write, .raw_writefn = raw_write },
6739 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64,
6740 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0,
6741 .access = PL2_RW, .type = ARM_CP_SVE,
6742 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]),
6743 .writefn = zcr_write, .raw_writefn = raw_write },
6744 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64,
6745 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0,
6746 .access = PL3_RW, .type = ARM_CP_SVE,
6747 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]),
6748 .writefn = zcr_write, .raw_writefn = raw_write },
5be5e8ed
RH
6749};
6750
9e5ec745
RH
6751#ifdef TARGET_AARCH64
6752static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri,
6753 bool isread)
6754{
6755 int el = arm_current_el(env);
6756
6757 if (el == 0) {
6758 uint64_t sctlr = arm_sctlr(env, el);
6759 if (!(sctlr & SCTLR_EnTP2)) {
6760 return CP_ACCESS_TRAP;
6761 }
6762 }
6763 /* TODO: FEAT_FGT */
6764 if (el < 3
6765 && arm_feature(env, ARM_FEATURE_EL3)
6766 && !(env->cp15.scr_el3 & SCR_ENTP2)) {
6767 return CP_ACCESS_TRAP_EL3;
6768 }
6769 return CP_ACCESS_OK;
6770}
6771
d5b1223a
RH
6772static CPAccessResult access_esm(CPUARMState *env, const ARMCPRegInfo *ri,
6773 bool isread)
6774{
6775 /* TODO: FEAT_FGT for SMPRI_EL1 but not SMPRIMAP_EL2 */
6776 if (arm_current_el(env) < 3
6777 && arm_feature(env, ARM_FEATURE_EL3)
6778 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) {
6779 return CP_ACCESS_TRAP_EL3;
6780 }
6781 return CP_ACCESS_OK;
6782}
6783
7f2a01e7
RH
6784/* ResetSVEState */
6785static void arm_reset_sve_state(CPUARMState *env)
6786{
6787 memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs));
6788 /* Recall that FFR is stored as pregs[16]. */
6789 memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs));
6790 vfp_set_fpcr(env, 0x0800009f);
6791}
6792
2a8af382
RH
6793void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask)
6794{
6795 uint64_t change = (env->svcr ^ new) & mask;
6796
f4318557
RH
6797 if (change == 0) {
6798 return;
6799 }
2a8af382 6800 env->svcr ^= change;
7f2a01e7
RH
6801
6802 if (change & R_SVCR_SM_MASK) {
6803 arm_reset_sve_state(env);
6804 }
fccb4918
RH
6805
6806 /*
6807 * ResetSMEState.
6808 *
6809 * SetPSTATE_ZA zeros on enable and disable. We can zero this only
6810 * on enable: while disabled, the storage is inaccessible and the
6811 * value does not matter. We're not saving the storage in vmstate
6812 * when disabled either.
6813 */
6814 if (change & new & R_SVCR_ZA_MASK) {
6815 memset(env->zarray, 0, sizeof(env->zarray));
6816 }
f4318557 6817
2b77ad4d
FR
6818 if (tcg_enabled()) {
6819 arm_rebuild_hflags(env);
6820 }
2a8af382
RH
6821}
6822
c37e6ac9
RH
6823static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6824 uint64_t value)
6825{
2a8af382 6826 aarch64_set_svcr(env, value, -1);
c37e6ac9
RH
6827}
6828
de561988
RH
6829static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
6830 uint64_t value)
6831{
6832 int cur_el = arm_current_el(env);
6833 int old_len = sve_vqm1_for_el(env, cur_el);
6834 int new_len;
6835
6836 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1);
6837 value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK;
6838 raw_write(env, ri, value);
6839
6840 /*
6841 * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage
6842 * when SVL is widened (old values kept, or zeros). Choose to keep the
6843 * current values for simplicity. But for QEMU internals, we must still
6844 * apply the narrower SVL to the Zregs and Pregs -- see the comment
6845 * above aarch64_sve_narrow_vq.
6846 */
6847 new_len = sve_vqm1_for_el(env, cur_el);
6848 if (new_len < old_len) {
6849 aarch64_sve_narrow_vq(env, new_len + 1);
6850 }
6851}
6852
9e5ec745
RH
6853static const ARMCPRegInfo sme_reginfo[] = {
6854 { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64,
6855 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5,
6856 .access = PL0_RW, .accessfn = access_tpidr2,
bd8db7d9 6857 .fgt = FGT_NTPIDR2_EL0,
9e5ec745 6858 .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) },
c37e6ac9
RH
6859 { .name = "SVCR", .state = ARM_CP_STATE_AA64,
6860 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2,
6861 .access = PL0_RW, .type = ARM_CP_SME,
6862 .fieldoffset = offsetof(CPUARMState, svcr),
6863 .writefn = svcr_write, .raw_writefn = raw_write },
de561988
RH
6864 { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64,
6865 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6,
6866 .access = PL1_RW, .type = ARM_CP_SME,
6867 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]),
6868 .writefn = smcr_write, .raw_writefn = raw_write },
6869 { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64,
6870 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6,
6871 .access = PL2_RW, .type = ARM_CP_SME,
6872 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]),
6873 .writefn = smcr_write, .raw_writefn = raw_write },
6874 { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64,
6875 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6,
6876 .access = PL3_RW, .type = ARM_CP_SME,
6877 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]),
6878 .writefn = smcr_write, .raw_writefn = raw_write },
d5b1223a
RH
6879 { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64,
6880 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6,
6881 .access = PL1_R, .accessfn = access_aa64_tid1,
6882 /*
6883 * IMPLEMENTOR = 0 (software)
6884 * REVISION = 0 (implementation defined)
6885 * SMPS = 0 (no streaming execution priority in QEMU)
6886 * AFFINITY = 0 (streaming sve mode not shared with other PEs)
6887 */
6888 .type = ARM_CP_CONST, .resetvalue = 0, },
6889 /*
6890 * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0.
6891 */
6892 { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64,
6893 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4,
6894 .access = PL1_RW, .accessfn = access_esm,
bd8db7d9 6895 .fgt = FGT_NSMPRI_EL1,
d5b1223a
RH
6896 .type = ARM_CP_CONST, .resetvalue = 0 },
6897 { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64,
6898 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5,
6899 .access = PL2_RW, .accessfn = access_esm,
6900 .type = ARM_CP_CONST, .resetvalue = 0 },
9e5ec745
RH
6901};
6902#endif /* TARGET_AARCH64 */
6903
24183fb6
PM
6904static void define_pmu_regs(ARMCPU *cpu)
6905{
6906 /*
6907 * v7 performance monitor control register: same implementor
6908 * field as main ID register, and we implement four counters in
6909 * addition to the cycle count register.
6910 */
24526bb9 6911 unsigned int i, pmcrn = pmu_num_counters(&cpu->env);
24183fb6
PM
6912 ARMCPRegInfo pmcr = {
6913 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
6914 .access = PL0_RW,
dc780233 6915 .fgt = FGT_PMCR_EL0,
24183fb6
PM
6916 .type = ARM_CP_IO | ARM_CP_ALIAS,
6917 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
6918 .accessfn = pmreg_access, .writefn = pmcr_write,
6919 .raw_writefn = raw_write,
6920 };
6921 ARMCPRegInfo pmcr64 = {
6922 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
6923 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
6924 .access = PL0_RW, .accessfn = pmreg_access,
dc780233 6925 .fgt = FGT_PMCR_EL0,
24183fb6
PM
6926 .type = ARM_CP_IO,
6927 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
24526bb9 6928 .resetvalue = cpu->isar.reset_pmcr_el0,
24183fb6
PM
6929 .writefn = pmcr_write, .raw_writefn = raw_write,
6930 };
24526bb9 6931
24183fb6
PM
6932 define_one_arm_cp_reg(cpu, &pmcr);
6933 define_one_arm_cp_reg(cpu, &pmcr64);
6934 for (i = 0; i < pmcrn; i++) {
6935 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i);
6936 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i);
6937 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i);
6938 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i);
6939 ARMCPRegInfo pmev_regs[] = {
6940 { .name = pmevcntr_name, .cp = 15, .crn = 14,
6941 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6942 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
dc780233 6943 .fgt = FGT_PMEVCNTRN_EL0,
24183fb6 6944 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
99a50d1a 6945 .accessfn = pmreg_access_xevcntr },
24183fb6
PM
6946 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64,
6947 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)),
99a50d1a 6948 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr,
24183fb6 6949 .type = ARM_CP_IO,
dc780233 6950 .fgt = FGT_PMEVCNTRN_EL0,
24183fb6
PM
6951 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn,
6952 .raw_readfn = pmevcntr_rawread,
6953 .raw_writefn = pmevcntr_rawwrite },
6954 { .name = pmevtyper_name, .cp = 15, .crn = 14,
6955 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7,
6956 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS,
dc780233 6957 .fgt = FGT_PMEVTYPERN_EL0,
24183fb6
PM
6958 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6959 .accessfn = pmreg_access },
6960 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64,
6961 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)),
6962 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access,
dc780233 6963 .fgt = FGT_PMEVTYPERN_EL0,
24183fb6
PM
6964 .type = ARM_CP_IO,
6965 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn,
6966 .raw_writefn = pmevtyper_rawwrite },
24183fb6
PM
6967 };
6968 define_arm_cp_regs(cpu, pmev_regs);
6969 g_free(pmevcntr_name);
6970 g_free(pmevcntr_el0_name);
6971 g_free(pmevtyper_name);
6972 g_free(pmevtyper_el0_name);
6973 }
a793bcd0 6974 if (cpu_isar_feature(aa32_pmuv3p1, cpu)) {
24183fb6
PM
6975 ARMCPRegInfo v81_pmu_regs[] = {
6976 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32,
6977 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4,
6978 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 6979 .fgt = FGT_PMCEIDN_EL0,
24183fb6
PM
6980 .resetvalue = extract64(cpu->pmceid0, 32, 32) },
6981 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32,
6982 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5,
6983 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 6984 .fgt = FGT_PMCEIDN_EL0,
24183fb6 6985 .resetvalue = extract64(cpu->pmceid1, 32, 32) },
24183fb6
PM
6986 };
6987 define_arm_cp_regs(cpu, v81_pmu_regs);
6988 }
a793bcd0 6989 if (cpu_isar_feature(any_pmuv3p4, cpu)) {
15dd1ebd
PM
6990 static const ARMCPRegInfo v84_pmmir = {
6991 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH,
6992 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6,
6993 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 6994 .fgt = FGT_PMMIR_EL1,
15dd1ebd
PM
6995 .resetvalue = 0
6996 };
6997 define_one_arm_cp_reg(cpu, &v84_pmmir);
6998 }
24183fb6
PM
6999}
7000
0f150c84 7001#ifndef CONFIG_USER_ONLY
9b37a28c
FR
7002/*
7003 * We don't know until after realize whether there's a GICv3
96a8b92e
PM
7004 * attached, and that is what registers the gicv3 sysregs.
7005 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1
7006 * at runtime.
7007 */
7008static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri)
7009{
2fc0cc0e 7010 ARMCPU *cpu = env_archcpu(env);
8a130a7b 7011 uint64_t pfr1 = cpu->isar.id_pfr1;
96a8b92e
PM
7012
7013 if (env->gicv3state) {
7014 pfr1 |= 1 << 28;
7015 }
7016 return pfr1;
7017}
7018
7019static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri)
7020{
2fc0cc0e 7021 ARMCPU *cpu = env_archcpu(env);
47576b94 7022 uint64_t pfr0 = cpu->isar.id_aa64pfr0;
96a8b92e
PM
7023
7024 if (env->gicv3state) {
7025 pfr0 |= 1 << 24;
7026 }
7027 return pfr0;
7028}
976b99b6 7029#endif
96a8b92e 7030
9b37a28c
FR
7031/*
7032 * Shared logic between LORID and the rest of the LOR* registers.
9bd268ba 7033 * Secure state exclusion has already been dealt with.
2d7137c1 7034 */
9bd268ba
RDC
7035static CPAccessResult access_lor_ns(CPUARMState *env,
7036 const ARMCPRegInfo *ri, bool isread)
2d7137c1
RH
7037{
7038 int el = arm_current_el(env);
7039
7040 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) {
7041 return CP_ACCESS_TRAP_EL2;
7042 }
7043 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) {
7044 return CP_ACCESS_TRAP_EL3;
7045 }
7046 return CP_ACCESS_OK;
7047}
7048
2d7137c1
RH
7049static CPAccessResult access_lor_other(CPUARMState *env,
7050 const ARMCPRegInfo *ri, bool isread)
7051{
7052 if (arm_is_secure_below_el3(env)) {
7053 /* Access denied in secure mode. */
7054 return CP_ACCESS_TRAP;
7055 }
9bd268ba 7056 return access_lor_ns(env, ri, isread);
2d7137c1
RH
7057}
7058
d8564ee4
RH
7059/*
7060 * A trivial implementation of ARMv8.1-LOR leaves all of these
7061 * registers fixed at 0, which indicates that there are zero
7062 * supported Limited Ordering regions.
7063 */
7064static const ARMCPRegInfo lor_reginfo[] = {
7065 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64,
7066 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0,
7067 .access = PL1_RW, .accessfn = access_lor_other,
b19ed03c 7068 .fgt = FGT_LORSA_EL1,
d8564ee4
RH
7069 .type = ARM_CP_CONST, .resetvalue = 0 },
7070 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64,
7071 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1,
7072 .access = PL1_RW, .accessfn = access_lor_other,
b19ed03c 7073 .fgt = FGT_LOREA_EL1,
d8564ee4
RH
7074 .type = ARM_CP_CONST, .resetvalue = 0 },
7075 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64,
7076 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2,
7077 .access = PL1_RW, .accessfn = access_lor_other,
b19ed03c 7078 .fgt = FGT_LORN_EL1,
d8564ee4
RH
7079 .type = ARM_CP_CONST, .resetvalue = 0 },
7080 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64,
7081 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3,
7082 .access = PL1_RW, .accessfn = access_lor_other,
b19ed03c 7083 .fgt = FGT_LORC_EL1,
d8564ee4
RH
7084 .type = ARM_CP_CONST, .resetvalue = 0 },
7085 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64,
7086 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7,
9bd268ba 7087 .access = PL1_R, .accessfn = access_lor_ns,
b19ed03c 7088 .fgt = FGT_LORID_EL1,
d8564ee4 7089 .type = ARM_CP_CONST, .resetvalue = 0 },
d8564ee4
RH
7090};
7091
967aa94f
RH
7092#ifdef TARGET_AARCH64
7093static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri,
7094 bool isread)
7095{
7096 int el = arm_current_el(env);
7097
7098 if (el < 2 &&
07b034ea 7099 arm_is_el2_enabled(env) &&
967aa94f
RH
7100 !(arm_hcr_el2_eff(env) & HCR_APK)) {
7101 return CP_ACCESS_TRAP_EL2;
7102 }
7103 if (el < 3 &&
7104 arm_feature(env, ARM_FEATURE_EL3) &&
7105 !(env->cp15.scr_el3 & SCR_APK)) {
7106 return CP_ACCESS_TRAP_EL3;
7107 }
7108 return CP_ACCESS_OK;
7109}
7110
7111static const ARMCPRegInfo pauth_reginfo[] = {
7112 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7113 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0,
7114 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7115 .fgt = FGT_APDAKEY,
108b3ba8 7116 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) },
967aa94f
RH
7117 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7118 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1,
7119 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7120 .fgt = FGT_APDAKEY,
108b3ba8 7121 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) },
967aa94f
RH
7122 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7123 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2,
7124 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7125 .fgt = FGT_APDBKEY,
108b3ba8 7126 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) },
967aa94f
RH
7127 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7128 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3,
7129 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7130 .fgt = FGT_APDBKEY,
108b3ba8 7131 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) },
967aa94f
RH
7132 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7133 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0,
7134 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7135 .fgt = FGT_APGAKEY,
108b3ba8 7136 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) },
967aa94f
RH
7137 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7138 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1,
7139 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7140 .fgt = FGT_APGAKEY,
108b3ba8 7141 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) },
967aa94f
RH
7142 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7143 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0,
7144 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7145 .fgt = FGT_APIAKEY,
108b3ba8 7146 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) },
967aa94f
RH
7147 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7148 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1,
7149 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7150 .fgt = FGT_APIAKEY,
108b3ba8 7151 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) },
967aa94f
RH
7152 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64,
7153 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2,
7154 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7155 .fgt = FGT_APIBKEY,
108b3ba8 7156 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) },
967aa94f
RH
7157 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64,
7158 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3,
7159 .access = PL1_RW, .accessfn = access_pauth,
158c276c 7160 .fgt = FGT_APIBKEY,
108b3ba8 7161 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) },
967aa94f 7162};
de390645 7163
84940ed8
RC
7164static const ARMCPRegInfo tlbirange_reginfo[] = {
7165 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64,
7166 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1,
0f66d223 7167 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 7168 .fgt = FGT_TLBIRVAE1IS,
84940ed8
RC
7169 .writefn = tlbi_aa64_rvae1is_write },
7170 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64,
7171 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3,
0f66d223 7172 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 7173 .fgt = FGT_TLBIRVAAE1IS,
84940ed8
RC
7174 .writefn = tlbi_aa64_rvae1is_write },
7175 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64,
7176 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5,
0f66d223 7177 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 7178 .fgt = FGT_TLBIRVALE1IS,
84940ed8
RC
7179 .writefn = tlbi_aa64_rvae1is_write },
7180 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64,
7181 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7,
0f66d223 7182 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW,
bf2f0625 7183 .fgt = FGT_TLBIRVAALE1IS,
84940ed8
RC
7184 .writefn = tlbi_aa64_rvae1is_write },
7185 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64,
7186 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
fe3ca86c 7187 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
bf2f0625 7188 .fgt = FGT_TLBIRVAE1OS,
84940ed8
RC
7189 .writefn = tlbi_aa64_rvae1is_write },
7190 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64,
7191 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3,
fe3ca86c 7192 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
bf2f0625 7193 .fgt = FGT_TLBIRVAAE1OS,
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,
bf2f0625 7198 .fgt = FGT_TLBIRVALE1OS,
84940ed8
RC
7199 .writefn = tlbi_aa64_rvae1is_write },
7200 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64,
7201 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7,
fe3ca86c 7202 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
bf2f0625 7203 .fgt = FGT_TLBIRVAALE1OS,
84940ed8
RC
7204 .writefn = tlbi_aa64_rvae1is_write },
7205 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64,
7206 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
4870f38b 7207 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 7208 .fgt = FGT_TLBIRVAE1,
84940ed8
RC
7209 .writefn = tlbi_aa64_rvae1_write },
7210 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64,
7211 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3,
4870f38b 7212 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 7213 .fgt = FGT_TLBIRVAAE1,
84940ed8
RC
7214 .writefn = tlbi_aa64_rvae1_write },
7215 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64,
7216 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5,
4870f38b 7217 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 7218 .fgt = FGT_TLBIRVALE1,
84940ed8
RC
7219 .writefn = tlbi_aa64_rvae1_write },
7220 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64,
7221 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7,
4870f38b 7222 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW,
bf2f0625 7223 .fgt = FGT_TLBIRVAALE1,
84940ed8
RC
7224 .writefn = tlbi_aa64_rvae1_write },
7225 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64,
7226 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2,
575a94af
RH
7227 .access = PL2_W, .type = ARM_CP_NO_RAW,
7228 .writefn = tlbi_aa64_ripas2e1is_write },
84940ed8
RC
7229 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64,
7230 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6,
575a94af
RH
7231 .access = PL2_W, .type = ARM_CP_NO_RAW,
7232 .writefn = tlbi_aa64_ripas2e1is_write },
84940ed8
RC
7233 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64,
7234 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1,
696ba377 7235 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7236 .writefn = tlbi_aa64_rvae2is_write },
7237 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64,
7238 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5,
696ba377 7239 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7240 .writefn = tlbi_aa64_rvae2is_write },
7241 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64,
7242 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2,
575a94af
RH
7243 .access = PL2_W, .type = ARM_CP_NO_RAW,
7244 .writefn = tlbi_aa64_ripas2e1_write },
7245 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64,
84940ed8 7246 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6,
575a94af
RH
7247 .access = PL2_W, .type = ARM_CP_NO_RAW,
7248 .writefn = tlbi_aa64_ripas2e1_write },
84940ed8
RC
7249 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64,
7250 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1,
696ba377 7251 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7252 .writefn = tlbi_aa64_rvae2is_write },
7253 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64,
7254 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5,
696ba377 7255 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7256 .writefn = tlbi_aa64_rvae2is_write },
7257 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64,
7258 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1,
696ba377 7259 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7260 .writefn = tlbi_aa64_rvae2_write },
7261 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64,
7262 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5,
696ba377 7263 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
84940ed8
RC
7264 .writefn = tlbi_aa64_rvae2_write },
7265 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64,
7266 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1,
7267 .access = PL3_W, .type = ARM_CP_NO_RAW,
7268 .writefn = tlbi_aa64_rvae3is_write },
7269 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64,
7270 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5,
7271 .access = PL3_W, .type = ARM_CP_NO_RAW,
7272 .writefn = tlbi_aa64_rvae3is_write },
7273 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64,
7274 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1,
7275 .access = PL3_W, .type = ARM_CP_NO_RAW,
7276 .writefn = tlbi_aa64_rvae3is_write },
7277 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64,
7278 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5,
7279 .access = PL3_W, .type = ARM_CP_NO_RAW,
7280 .writefn = tlbi_aa64_rvae3is_write },
7281 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64,
7282 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1,
7283 .access = PL3_W, .type = ARM_CP_NO_RAW,
7284 .writefn = tlbi_aa64_rvae3_write },
7285 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64,
7286 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5,
7287 .access = PL3_W, .type = ARM_CP_NO_RAW,
7288 .writefn = tlbi_aa64_rvae3_write },
84940ed8
RC
7289};
7290
7113d618
RC
7291static const ARMCPRegInfo tlbios_reginfo[] = {
7292 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64,
7293 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0,
fe3ca86c 7294 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
bf2f0625 7295 .fgt = FGT_TLBIVMALLE1OS,
7113d618 7296 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
7297 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64,
7298 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1,
bf2f0625 7299 .fgt = FGT_TLBIVAE1OS,
fe3ca86c 7300 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
b7469ef9 7301 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
7302 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64,
7303 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2,
fe3ca86c 7304 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
bf2f0625 7305 .fgt = FGT_TLBIASIDE1OS,
7113d618 7306 .writefn = tlbi_aa64_vmalle1is_write },
b7469ef9
IH
7307 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64,
7308 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3,
fe3ca86c 7309 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
bf2f0625 7310 .fgt = FGT_TLBIVAAE1OS,
b7469ef9
IH
7311 .writefn = tlbi_aa64_vae1is_write },
7312 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64,
7313 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5,
fe3ca86c 7314 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
bf2f0625 7315 .fgt = FGT_TLBIVALE1OS,
b7469ef9
IH
7316 .writefn = tlbi_aa64_vae1is_write },
7317 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64,
7318 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7,
fe3ca86c 7319 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW,
bf2f0625 7320 .fgt = FGT_TLBIVAALE1OS,
b7469ef9 7321 .writefn = tlbi_aa64_vae1is_write },
7113d618
RC
7322 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64,
7323 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0,
696ba377 7324 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
7113d618 7325 .writefn = tlbi_aa64_alle2is_write },
b7469ef9
IH
7326 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64,
7327 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1,
696ba377 7328 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
b7469ef9 7329 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
7330 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64,
7331 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4,
7332 .access = PL2_W, .type = ARM_CP_NO_RAW,
7333 .writefn = tlbi_aa64_alle1is_write },
b7469ef9
IH
7334 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64,
7335 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5,
696ba377 7336 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF,
b7469ef9 7337 .writefn = tlbi_aa64_vae2is_write },
7113d618
RC
7338 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64,
7339 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6,
7340 .access = PL2_W, .type = ARM_CP_NO_RAW,
7341 .writefn = tlbi_aa64_alle1is_write },
7342 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64,
7343 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0,
7344 .access = PL2_W, .type = ARM_CP_NOP },
7345 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64,
7346 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3,
7347 .access = PL2_W, .type = ARM_CP_NOP },
7348 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7349 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4,
7350 .access = PL2_W, .type = ARM_CP_NOP },
7351 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64,
7352 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7,
7353 .access = PL2_W, .type = ARM_CP_NOP },
7354 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64,
7355 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0,
7356 .access = PL3_W, .type = ARM_CP_NO_RAW,
7357 .writefn = tlbi_aa64_alle3is_write },
b7469ef9
IH
7358 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64,
7359 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1,
7360 .access = PL3_W, .type = ARM_CP_NO_RAW,
7361 .writefn = tlbi_aa64_vae3is_write },
7362 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64,
7363 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5,
7364 .access = PL3_W, .type = ARM_CP_NO_RAW,
7365 .writefn = tlbi_aa64_vae3is_write },
7113d618
RC
7366};
7367
de390645
RH
7368static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri)
7369{
7370 Error *err = NULL;
7371 uint64_t ret;
7372
7373 /* Success sets NZCV = 0000. */
7374 env->NF = env->CF = env->VF = 0, env->ZF = 1;
7375
7376 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) {
7377 /*
7378 * ??? Failed, for unknown reasons in the crypto subsystem.
7379 * The best we can do is log the reason and return the
7380 * timed-out indication to the guest. There is no reason
7381 * we know to expect this failure to be transitory, so the
7382 * guest may well hang retrying the operation.
7383 */
7384 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
7385 ri->name, error_get_pretty(err));
7386 error_free(err);
7387
7388 env->ZF = 0; /* NZCF = 0100 */
7389 return 0;
7390 }
7391 return ret;
7392}
7393
7394/* We do not support re-seeding, so the two registers operate the same. */
7395static const ARMCPRegInfo rndr_reginfo[] = {
7396 { .name = "RNDR", .state = ARM_CP_STATE_AA64,
7397 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7398 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0,
7399 .access = PL0_R, .readfn = rndr_readfn },
7400 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64,
7401 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO,
7402 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1,
7403 .access = PL0_R, .readfn = rndr_readfn },
de390645 7404};
0d57b499
BM
7405
7406#ifndef CONFIG_USER_ONLY
7407static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque,
7408 uint64_t value)
7409{
7410 ARMCPU *cpu = env_archcpu(env);
7411 /* CTR_EL0 System register -> DminLine, bits [19:16] */
7412 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF);
7413 uint64_t vaddr_in = (uint64_t) value;
7414 uint64_t vaddr = vaddr_in & ~(dline_size - 1);
7415 void *haddr;
7416 int mem_idx = cpu_mmu_index(env, false);
7417
7418 /* This won't be crossing page boundaries */
7419 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC());
7420 if (haddr) {
7421
7422 ram_addr_t offset;
7423 MemoryRegion *mr;
7424
7425 /* RCU lock is already being held */
7426 mr = memory_region_from_host(haddr, &offset);
7427
7428 if (mr) {
4dfe59d1 7429 memory_region_writeback(mr, offset, dline_size);
0d57b499
BM
7430 }
7431 }
7432}
7433
7434static const ARMCPRegInfo dcpop_reg[] = {
7435 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64,
7436 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1,
7437 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
dd345653 7438 .fgt = FGT_DCCVAP,
1bed4d2e 7439 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
7440};
7441
7442static const ARMCPRegInfo dcpodp_reg[] = {
7443 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64,
7444 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1,
7445 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END,
dd345653 7446 .fgt = FGT_DCCVADP,
1bed4d2e 7447 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn },
0d57b499
BM
7448};
7449#endif /*CONFIG_USER_ONLY*/
7450
4b779ceb
RH
7451static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri,
7452 bool isread)
7453{
7454 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) {
7455 return CP_ACCESS_TRAP_EL2;
7456 }
7457
7458 return CP_ACCESS_OK;
7459}
7460
7461static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri,
7462 bool isread)
7463{
7464 int el = arm_current_el(env);
7465
0da067f2 7466 if (el < 2 && arm_is_el2_enabled(env)) {
4301acd7
RH
7467 uint64_t hcr = arm_hcr_el2_eff(env);
7468 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) {
7469 return CP_ACCESS_TRAP_EL2;
7470 }
4b779ceb
RH
7471 }
7472 if (el < 3 &&
7473 arm_feature(env, ARM_FEATURE_EL3) &&
7474 !(env->cp15.scr_el3 & SCR_ATA)) {
7475 return CP_ACCESS_TRAP_EL3;
7476 }
7477 return CP_ACCESS_OK;
7478}
7479
7480static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri)
7481{
7482 return env->pstate & PSTATE_TCO;
7483}
7484
7485static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
7486{
7487 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO);
7488}
7489
7490static const ARMCPRegInfo mte_reginfo[] = {
7491 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64,
7492 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1,
7493 .access = PL1_RW, .accessfn = access_mte,
7494 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) },
7495 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64,
7496 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0,
7497 .access = PL1_RW, .accessfn = access_mte,
7498 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) },
7499 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64,
7500 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0,
7501 .access = PL2_RW, .accessfn = access_mte,
7502 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) },
7503 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64,
7504 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0,
7505 .access = PL3_RW,
7506 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) },
7507 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64,
7508 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5,
7509 .access = PL1_RW, .accessfn = access_mte,
7510 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) },
7511 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64,
7512 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6,
7513 .access = PL1_RW, .accessfn = access_mte,
7514 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) },
7515 { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64,
7516 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4,
7517 .access = PL1_R, .accessfn = access_aa64_tid5,
7518 .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS },
7519 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7520 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7521 .type = ARM_CP_NO_RAW,
7522 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write },
5463df16
RH
7523 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64,
7524 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3,
7525 .type = ARM_CP_NOP, .access = PL1_W,
dd345653 7526 .fgt = FGT_DCIVAC,
5463df16
RH
7527 .accessfn = aa64_cacheop_poc_access },
7528 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64,
7529 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4,
dd345653 7530 .fgt = FGT_DCISW,
5463df16
RH
7531 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7532 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64,
7533 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5,
7534 .type = ARM_CP_NOP, .access = PL1_W,
dd345653 7535 .fgt = FGT_DCIVAC,
5463df16
RH
7536 .accessfn = aa64_cacheop_poc_access },
7537 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64,
7538 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6,
dd345653 7539 .fgt = FGT_DCISW,
5463df16
RH
7540 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7541 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64,
7542 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4,
dd345653 7543 .fgt = FGT_DCCSW,
5463df16
RH
7544 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7545 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64,
7546 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6,
dd345653 7547 .fgt = FGT_DCCSW,
5463df16
RH
7548 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7549 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64,
7550 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4,
dd345653 7551 .fgt = FGT_DCCISW,
5463df16
RH
7552 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
7553 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64,
7554 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6,
dd345653 7555 .fgt = FGT_DCCISW,
5463df16 7556 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw },
4b779ceb
RH
7557};
7558
7559static const ARMCPRegInfo mte_tco_ro_reginfo[] = {
7560 { .name = "TCO", .state = ARM_CP_STATE_AA64,
7561 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7,
7562 .type = ARM_CP_CONST, .access = PL0_RW, },
4b779ceb 7563};
5463df16
RH
7564
7565static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = {
7566 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64,
7567 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3,
7568 .type = ARM_CP_NOP, .access = PL0_W,
950037e2 7569 .fgt = FGT_DCCVAC,
5463df16
RH
7570 .accessfn = aa64_cacheop_poc_access },
7571 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64,
7572 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5,
7573 .type = ARM_CP_NOP, .access = PL0_W,
950037e2 7574 .fgt = FGT_DCCVAC,
5463df16
RH
7575 .accessfn = aa64_cacheop_poc_access },
7576 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64,
7577 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3,
7578 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7579 .fgt = FGT_DCCVAP,
5463df16
RH
7580 .accessfn = aa64_cacheop_poc_access },
7581 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64,
7582 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5,
7583 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7584 .fgt = FGT_DCCVAP,
5463df16
RH
7585 .accessfn = aa64_cacheop_poc_access },
7586 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64,
7587 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3,
7588 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7589 .fgt = FGT_DCCVADP,
5463df16
RH
7590 .accessfn = aa64_cacheop_poc_access },
7591 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64,
7592 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5,
7593 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7594 .fgt = FGT_DCCVADP,
5463df16
RH
7595 .accessfn = aa64_cacheop_poc_access },
7596 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64,
7597 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3,
7598 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7599 .fgt = FGT_DCCIVAC,
5463df16
RH
7600 .accessfn = aa64_cacheop_poc_access },
7601 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64,
7602 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5,
7603 .type = ARM_CP_NOP, .access = PL0_W,
dd345653 7604 .fgt = FGT_DCCIVAC,
5463df16 7605 .accessfn = aa64_cacheop_poc_access },
eb821168
RH
7606 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64,
7607 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3,
7608 .access = PL0_W, .type = ARM_CP_DC_GVA,
7609#ifndef CONFIG_USER_ONLY
7610 /* Avoid overhead of an access check that always passes in user-mode */
7611 .accessfn = aa64_zva_access,
dd345653 7612 .fgt = FGT_DCZVA,
eb821168
RH
7613#endif
7614 },
7615 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64,
7616 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4,
7617 .access = PL0_W, .type = ARM_CP_DC_GZVA,
7618#ifndef CONFIG_USER_ONLY
7619 /* Avoid overhead of an access check that always passes in user-mode */
7620 .accessfn = aa64_zva_access,
dd345653 7621 .fgt = FGT_DCZVA,
eb821168
RH
7622#endif
7623 },
5463df16
RH
7624};
7625
7cb1e618
RH
7626static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri,
7627 bool isread)
7628{
7629 uint64_t hcr = arm_hcr_el2_eff(env);
7630 int el = arm_current_el(env);
7631
7632 if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) {
7633 if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) {
7634 if (hcr & HCR_TGE) {
7635 return CP_ACCESS_TRAP_EL2;
7636 }
7637 return CP_ACCESS_TRAP;
7638 }
7639 } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) {
7640 return CP_ACCESS_TRAP_EL2;
7641 }
7642 if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) {
7643 return CP_ACCESS_TRAP_EL2;
7644 }
7645 if (el < 3
7646 && arm_feature(env, ARM_FEATURE_EL3)
7647 && !(env->cp15.scr_el3 & SCR_ENSCXT)) {
7648 return CP_ACCESS_TRAP_EL3;
7649 }
7650 return CP_ACCESS_OK;
7651}
7652
7653static const ARMCPRegInfo scxtnum_reginfo[] = {
7654 { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64,
7655 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7,
7656 .access = PL0_RW, .accessfn = access_scxtnum,
67dd8030 7657 .fgt = FGT_SCXTNUM_EL0,
7cb1e618
RH
7658 .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) },
7659 { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64,
7660 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7,
7661 .access = PL1_RW, .accessfn = access_scxtnum,
67dd8030 7662 .fgt = FGT_SCXTNUM_EL1,
7cb1e618
RH
7663 .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) },
7664 { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64,
7665 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7,
7666 .access = PL2_RW, .accessfn = access_scxtnum,
7667 .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) },
7668 { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64,
7669 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7,
7670 .access = PL3_RW,
7671 .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) },
7672};
15126d9c
PM
7673
7674static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri,
7675 bool isread)
7676{
7677 if (arm_current_el(env) == 2 &&
7678 arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) {
7679 return CP_ACCESS_TRAP_EL3;
7680 }
7681 return CP_ACCESS_OK;
7682}
7683
7684static const ARMCPRegInfo fgt_reginfo[] = {
7685 { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7686 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4,
7687 .access = PL2_RW, .accessfn = access_fgt,
7688 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) },
7689 { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7690 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5,
7691 .access = PL2_RW, .accessfn = access_fgt,
7692 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) },
7693 { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64,
7694 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4,
7695 .access = PL2_RW, .accessfn = access_fgt,
7696 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) },
7697 { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64,
7698 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5,
7699 .access = PL2_RW, .accessfn = access_fgt,
7700 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) },
7701 { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64,
7702 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6,
7703 .access = PL2_RW, .accessfn = access_fgt,
7704 .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) },
7705};
7cb1e618 7706#endif /* TARGET_AARCH64 */
967aa94f 7707
cb570bd3
RH
7708static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri,
7709 bool isread)
7710{
7711 int el = arm_current_el(env);
7712
7713 if (el == 0) {
7714 uint64_t sctlr = arm_sctlr(env, el);
7715 if (!(sctlr & SCTLR_EnRCTX)) {
7716 return CP_ACCESS_TRAP;
7717 }
7718 } else if (el == 1) {
7719 uint64_t hcr = arm_hcr_el2_eff(env);
7720 if (hcr & HCR_NV) {
7721 return CP_ACCESS_TRAP_EL2;
7722 }
7723 }
7724 return CP_ACCESS_OK;
7725}
7726
7727static const ARMCPRegInfo predinv_reginfo[] = {
7728 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64,
7729 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4,
950037e2 7730 .fgt = FGT_CFPRCTX,
cb570bd3
RH
7731 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7732 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64,
7733 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5,
950037e2 7734 .fgt = FGT_DVPRCTX,
cb570bd3
RH
7735 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7736 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64,
7737 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7,
950037e2 7738 .fgt = FGT_CPPRCTX,
cb570bd3
RH
7739 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7740 /*
7741 * Note the AArch32 opcodes have a different OPC1.
7742 */
7743 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32,
7744 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4,
950037e2 7745 .fgt = FGT_CFPRCTX,
cb570bd3
RH
7746 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7747 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32,
7748 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5,
950037e2 7749 .fgt = FGT_DVPRCTX,
cb570bd3
RH
7750 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
7751 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32,
7752 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7,
950037e2 7753 .fgt = FGT_CPPRCTX,
cb570bd3 7754 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv },
cb570bd3
RH
7755};
7756
957e6155
PM
7757static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri)
7758{
7759 /* Read the high 32 bits of the current CCSIDR */
7760 return extract64(ccsidr_read(env, ri), 32, 32);
7761}
7762
7763static const ARMCPRegInfo ccsidr2_reginfo[] = {
7764 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH,
7765 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2,
7766 .access = PL1_R,
e2ce5fcd 7767 .accessfn = access_tid4,
957e6155 7768 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW },
957e6155
PM
7769};
7770
6a4ef4e5
MZ
7771static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7772 bool isread)
7773{
7774 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) {
7775 return CP_ACCESS_TRAP_EL2;
7776 }
7777
7778 return CP_ACCESS_OK;
7779}
7780
7781static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri,
7782 bool isread)
7783{
7784 if (arm_feature(env, ARM_FEATURE_V8)) {
7785 return access_aa64_tid3(env, ri, isread);
7786 }
7787
7788 return CP_ACCESS_OK;
7789}
7790
f96f3d5f
MZ
7791static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri,
7792 bool isread)
7793{
7794 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) {
7795 return CP_ACCESS_TRAP_EL2;
7796 }
7797
7798 return CP_ACCESS_OK;
7799}
7800
8e228c9e
PM
7801static CPAccessResult access_joscr_jmcr(CPUARMState *env,
7802 const ARMCPRegInfo *ri, bool isread)
7803{
7804 /*
7805 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only
7806 * in v7A, not in v8A.
7807 */
7808 if (!arm_feature(env, ARM_FEATURE_V8) &&
7809 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) &&
7810 (env->cp15.hstr_el2 & HSTR_TJDBX)) {
7811 return CP_ACCESS_TRAP_EL2;
7812 }
7813 return CP_ACCESS_OK;
7814}
7815
f96f3d5f
MZ
7816static const ARMCPRegInfo jazelle_regs[] = {
7817 { .name = "JIDR",
7818 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0,
7819 .access = PL1_R, .accessfn = access_jazelle,
7820 .type = ARM_CP_CONST, .resetvalue = 0 },
7821 { .name = "JOSCR",
7822 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7823 .accessfn = access_joscr_jmcr,
f96f3d5f
MZ
7824 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
7825 { .name = "JMCR",
7826 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0,
8e228c9e 7827 .accessfn = access_joscr_jmcr,
f96f3d5f 7828 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
f96f3d5f
MZ
7829};
7830
52d18727
RH
7831static const ARMCPRegInfo contextidr_el2 = {
7832 .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64,
7833 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1,
7834 .access = PL2_RW,
7835 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2])
7836};
7837
e2a1a461 7838static const ARMCPRegInfo vhe_reginfo[] = {
ed30da8e
RH
7839 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64,
7840 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1,
7841 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write,
7842 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) },
8c94b071
RH
7843#ifndef CONFIG_USER_ONLY
7844 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64,
7845 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2,
7846 .fieldoffset =
7847 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval),
7848 .type = ARM_CP_IO, .access = PL2_RW,
7849 .writefn = gt_hv_cval_write, .raw_writefn = raw_write },
7850 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
7851 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0,
7852 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
7853 .resetfn = gt_hv_timer_reset,
7854 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write },
7855 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH,
7856 .type = ARM_CP_IO,
7857 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1,
7858 .access = PL2_RW,
7859 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl),
7860 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write },
bb5972e4
RH
7861 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64,
7862 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1,
7863 .type = ARM_CP_IO | ARM_CP_ALIAS,
7864 .access = PL2_RW, .accessfn = e2h_access,
7865 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
7866 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write },
7867 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64,
7868 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1,
7869 .type = ARM_CP_IO | ARM_CP_ALIAS,
7870 .access = PL2_RW, .accessfn = e2h_access,
7871 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
7872 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write },
7873 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7874 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0,
7875 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7876 .access = PL2_RW, .accessfn = e2h_access,
7877 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write },
7878 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64,
7879 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0,
7880 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS,
7881 .access = PL2_RW, .accessfn = e2h_access,
7882 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write },
7883 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7884 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2,
7885 .type = ARM_CP_IO | ARM_CP_ALIAS,
7886 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
7887 .access = PL2_RW, .accessfn = e2h_access,
7888 .writefn = gt_phys_cval_write, .raw_writefn = raw_write },
7889 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64,
7890 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2,
7891 .type = ARM_CP_IO | ARM_CP_ALIAS,
7892 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
7893 .access = PL2_RW, .accessfn = e2h_access,
7894 .writefn = gt_virt_cval_write, .raw_writefn = raw_write },
8c94b071 7895#endif
e2a1a461
RH
7896};
7897
04b07d29
RH
7898#ifndef CONFIG_USER_ONLY
7899static const ARMCPRegInfo ats1e1_reginfo[] = {
3999d2d2 7900 { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64,
04b07d29
RH
7901 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7902 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
132c98cd 7903 .fgt = FGT_ATS1E1RP,
04b07d29 7904 .writefn = ats_write64 },
3999d2d2 7905 { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64,
04b07d29
RH
7906 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7907 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
132c98cd 7908 .fgt = FGT_ATS1E1WP,
04b07d29 7909 .writefn = ats_write64 },
04b07d29
RH
7910};
7911
7912static const ARMCPRegInfo ats1cp_reginfo[] = {
7913 { .name = "ATS1CPRP",
7914 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0,
7915 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7916 .writefn = ats_write },
7917 { .name = "ATS1CPWP",
7918 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1,
7919 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC,
7920 .writefn = ats_write },
04b07d29
RH
7921};
7922#endif
7923
f6287c24
PM
7924/*
7925 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and
7926 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field
7927 * is non-zero, which is never for ARMv7, optionally in ARMv8
7928 * and mandatorily for ARMv8.2 and up.
7929 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's
7930 * implementation is RAZ/WI we can ignore this detail, as we
7931 * do for ACTLR.
7932 */
7933static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = {
7934 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32,
7935 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3,
99602377
RH
7936 .access = PL1_RW, .accessfn = access_tacr,
7937 .type = ARM_CP_CONST, .resetvalue = 0 },
f6287c24
PM
7938 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32,
7939 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3,
7940 .access = PL2_RW, .type = ARM_CP_CONST,
7941 .resetvalue = 0 },
f6287c24
PM
7942};
7943
2ceb98c0
PM
7944void register_cp_regs_for_features(ARMCPU *cpu)
7945{
7946 /* Register all the coprocessor registers based on feature bits */
7947 CPUARMState *env = &cpu->env;
7948 if (arm_feature(env, ARM_FEATURE_M)) {
7949 /* M profile has no coprocessor registers */
7950 return;
7951 }
7952
e9aa6c21 7953 define_arm_cp_regs(cpu, cp_reginfo);
9449fdf6 7954 if (!arm_feature(env, ARM_FEATURE_V8)) {
9b37a28c
FR
7955 /*
7956 * Must go early as it is full of wildcards that may be
9449fdf6
PM
7957 * overridden by later definitions.
7958 */
7959 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
7960 }
7961
7d57f408 7962 if (arm_feature(env, ARM_FEATURE_V6)) {
8515a092
PM
7963 /* The ID registers all have impdef reset values */
7964 ARMCPRegInfo v6_idregs[] = {
0ff644a7
PM
7965 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
7966 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
7967 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7968 .accessfn = access_aa32_tid3,
8a130a7b 7969 .resetvalue = cpu->isar.id_pfr0 },
9b37a28c
FR
7970 /*
7971 * ID_PFR1 is not a plain ARM_CP_CONST because we don't know
96a8b92e
PM
7972 * the value of the GIC field until after we define these regs.
7973 */
0ff644a7
PM
7974 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
7975 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
96a8b92e 7976 .access = PL1_R, .type = ARM_CP_NO_RAW,
6a4ef4e5 7977 .accessfn = access_aa32_tid3,
0f150c84
PMD
7978#ifdef CONFIG_USER_ONLY
7979 .type = ARM_CP_CONST,
7980 .resetvalue = cpu->isar.id_pfr1,
7981#else
7982 .type = ARM_CP_NO_RAW,
7983 .accessfn = access_aa32_tid3,
96a8b92e 7984 .readfn = id_pfr1_read,
0f150c84
PMD
7985 .writefn = arm_cp_write_ignore
7986#endif
7987 },
0ff644a7
PM
7988 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
7989 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
7990 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7991 .accessfn = access_aa32_tid3,
a6179538 7992 .resetvalue = cpu->isar.id_dfr0 },
0ff644a7
PM
7993 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
7994 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
7995 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 7996 .accessfn = access_aa32_tid3,
8515a092 7997 .resetvalue = cpu->id_afr0 },
0ff644a7
PM
7998 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
7999 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
8000 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8001 .accessfn = access_aa32_tid3,
10054016 8002 .resetvalue = cpu->isar.id_mmfr0 },
0ff644a7
PM
8003 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
8004 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
8005 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8006 .accessfn = access_aa32_tid3,
10054016 8007 .resetvalue = cpu->isar.id_mmfr1 },
0ff644a7
PM
8008 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
8009 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
8010 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8011 .accessfn = access_aa32_tid3,
10054016 8012 .resetvalue = cpu->isar.id_mmfr2 },
0ff644a7
PM
8013 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
8014 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
8015 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8016 .accessfn = access_aa32_tid3,
10054016 8017 .resetvalue = cpu->isar.id_mmfr3 },
0ff644a7
PM
8018 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
8019 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
8020 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8021 .accessfn = access_aa32_tid3,
47576b94 8022 .resetvalue = cpu->isar.id_isar0 },
0ff644a7
PM
8023 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
8024 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
8025 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8026 .accessfn = access_aa32_tid3,
47576b94 8027 .resetvalue = cpu->isar.id_isar1 },
0ff644a7
PM
8028 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
8029 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
8030 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8031 .accessfn = access_aa32_tid3,
47576b94 8032 .resetvalue = cpu->isar.id_isar2 },
0ff644a7
PM
8033 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
8034 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
8035 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8036 .accessfn = access_aa32_tid3,
47576b94 8037 .resetvalue = cpu->isar.id_isar3 },
0ff644a7
PM
8038 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
8039 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
8040 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8041 .accessfn = access_aa32_tid3,
47576b94 8042 .resetvalue = cpu->isar.id_isar4 },
0ff644a7
PM
8043 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
8044 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
8045 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8046 .accessfn = access_aa32_tid3,
47576b94 8047 .resetvalue = cpu->isar.id_isar5 },
e20d84c1
PM
8048 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
8049 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
8050 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8051 .accessfn = access_aa32_tid3,
10054016 8052 .resetvalue = cpu->isar.id_mmfr4 },
802abf40 8053 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8054 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
8055 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8056 .accessfn = access_aa32_tid3,
47576b94 8057 .resetvalue = cpu->isar.id_isar6 },
8515a092
PM
8058 };
8059 define_arm_cp_regs(cpu, v6_idregs);
7d57f408
PM
8060 define_arm_cp_regs(cpu, v6_cp_reginfo);
8061 } else {
8062 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
8063 }
4d31c596
PM
8064 if (arm_feature(env, ARM_FEATURE_V6K)) {
8065 define_arm_cp_regs(cpu, v6k_cp_reginfo);
8066 }
5e5cf9e3 8067 if (arm_feature(env, ARM_FEATURE_V7MP) &&
452a0955 8068 !arm_feature(env, ARM_FEATURE_PMSA)) {
995939a6
PM
8069 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
8070 }
327dd510
AL
8071 if (arm_feature(env, ARM_FEATURE_V7VE)) {
8072 define_arm_cp_regs(cpu, pmovsset_cp_reginfo);
8073 }
e9aa6c21 8074 if (arm_feature(env, ARM_FEATURE_V7)) {
776d4e5c 8075 ARMCPRegInfo clidr = {
7da845b0
PM
8076 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
8077 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
630fcd4d 8078 .access = PL1_R, .type = ARM_CP_CONST,
e2ce5fcd 8079 .accessfn = access_tid4,
158c276c 8080 .fgt = FGT_CLIDR_EL1,
630fcd4d 8081 .resetvalue = cpu->clidr
776d4e5c 8082 };
776d4e5c 8083 define_one_arm_cp_reg(cpu, &clidr);
e9aa6c21 8084 define_arm_cp_regs(cpu, v7_cp_reginfo);
50300698 8085 define_debug_regs(cpu);
24183fb6 8086 define_pmu_regs(cpu);
7d57f408
PM
8087 } else {
8088 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
e9aa6c21 8089 }
b0d2b7d0 8090 if (arm_feature(env, ARM_FEATURE_V8)) {
dde4d028
PM
8091 /*
8092 * v8 ID registers, which all have impdef reset values.
e20d84c1
PM
8093 * Note that within the ID register ranges the unused slots
8094 * must all RAZ, not UNDEF; future architecture versions may
8095 * define new registers here.
dde4d028
PM
8096 * ID registers which are AArch64 views of the AArch32 ID registers
8097 * which already existed in v6 and v7 are handled elsewhere,
8098 * in v6_idregs[].
e20d84c1 8099 */
dde4d028 8100 int i;
e60cef86 8101 ARMCPRegInfo v8_idregs[] = {
976b99b6
AB
8102 /*
8103 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system
8104 * emulation because we don't know the right value for the
8105 * GIC field until after we define these regs.
96a8b92e 8106 */
e60cef86
PM
8107 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
8108 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
976b99b6
AB
8109 .access = PL1_R,
8110#ifdef CONFIG_USER_ONLY
8111 .type = ARM_CP_CONST,
8112 .resetvalue = cpu->isar.id_aa64pfr0
8113#else
8114 .type = ARM_CP_NO_RAW,
6a4ef4e5 8115 .accessfn = access_aa64_tid3,
96a8b92e 8116 .readfn = id_aa64pfr0_read,
976b99b6
AB
8117 .writefn = arm_cp_write_ignore
8118#endif
8119 },
e60cef86
PM
8120 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
8121 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
8122 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8123 .accessfn = access_aa64_tid3,
47576b94 8124 .resetvalue = cpu->isar.id_aa64pfr1},
e20d84c1
PM
8125 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8126 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
8127 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8128 .accessfn = access_aa64_tid3,
e20d84c1
PM
8129 .resetvalue = 0 },
8130 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8131 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
8132 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8133 .accessfn = access_aa64_tid3,
e20d84c1 8134 .resetvalue = 0 },
9516d772 8135 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
8136 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
8137 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8138 .accessfn = access_aa64_tid3,
2dc10fa2 8139 .resetvalue = cpu->isar.id_aa64zfr0 },
414c54d5 8140 { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
8141 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
8142 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8143 .accessfn = access_aa64_tid3,
414c54d5 8144 .resetvalue = cpu->isar.id_aa64smfr0 },
e20d84c1
PM
8145 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8146 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
8147 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8148 .accessfn = access_aa64_tid3,
e20d84c1
PM
8149 .resetvalue = 0 },
8150 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8151 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
8152 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8153 .accessfn = access_aa64_tid3,
e20d84c1 8154 .resetvalue = 0 },
e60cef86
PM
8155 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
8156 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
8157 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8158 .accessfn = access_aa64_tid3,
2a609df8 8159 .resetvalue = cpu->isar.id_aa64dfr0 },
e60cef86
PM
8160 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
8161 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
8162 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8163 .accessfn = access_aa64_tid3,
2a609df8 8164 .resetvalue = cpu->isar.id_aa64dfr1 },
e20d84c1
PM
8165 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8166 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
8167 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8168 .accessfn = access_aa64_tid3,
e20d84c1
PM
8169 .resetvalue = 0 },
8170 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8171 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
8172 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8173 .accessfn = access_aa64_tid3,
e20d84c1 8174 .resetvalue = 0 },
e60cef86
PM
8175 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
8176 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
8177 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8178 .accessfn = access_aa64_tid3,
e60cef86
PM
8179 .resetvalue = cpu->id_aa64afr0 },
8180 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
8181 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
8182 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8183 .accessfn = access_aa64_tid3,
e60cef86 8184 .resetvalue = cpu->id_aa64afr1 },
e20d84c1
PM
8185 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8186 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
8187 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8188 .accessfn = access_aa64_tid3,
e20d84c1
PM
8189 .resetvalue = 0 },
8190 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8191 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
8192 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8193 .accessfn = access_aa64_tid3,
e20d84c1 8194 .resetvalue = 0 },
e60cef86
PM
8195 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
8196 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
8197 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8198 .accessfn = access_aa64_tid3,
47576b94 8199 .resetvalue = cpu->isar.id_aa64isar0 },
e60cef86
PM
8200 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
8201 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
8202 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8203 .accessfn = access_aa64_tid3,
47576b94 8204 .resetvalue = cpu->isar.id_aa64isar1 },
e20d84c1
PM
8205 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8206 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
8207 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8208 .accessfn = access_aa64_tid3,
e20d84c1
PM
8209 .resetvalue = 0 },
8210 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8211 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
8212 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8213 .accessfn = access_aa64_tid3,
e20d84c1
PM
8214 .resetvalue = 0 },
8215 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8216 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
8217 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8218 .accessfn = access_aa64_tid3,
e20d84c1
PM
8219 .resetvalue = 0 },
8220 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8221 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
8222 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8223 .accessfn = access_aa64_tid3,
e20d84c1
PM
8224 .resetvalue = 0 },
8225 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8226 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
8227 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8228 .accessfn = access_aa64_tid3,
e20d84c1
PM
8229 .resetvalue = 0 },
8230 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8231 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
8232 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8233 .accessfn = access_aa64_tid3,
e20d84c1 8234 .resetvalue = 0 },
e60cef86
PM
8235 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
8236 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
8237 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8238 .accessfn = access_aa64_tid3,
3dc91ddb 8239 .resetvalue = cpu->isar.id_aa64mmfr0 },
e60cef86
PM
8240 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
8241 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
8242 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8243 .accessfn = access_aa64_tid3,
3dc91ddb 8244 .resetvalue = cpu->isar.id_aa64mmfr1 },
64761e10 8245 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64,
e20d84c1
PM
8246 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
8247 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8248 .accessfn = access_aa64_tid3,
64761e10 8249 .resetvalue = cpu->isar.id_aa64mmfr2 },
e20d84c1
PM
8250 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8251 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
8252 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8253 .accessfn = access_aa64_tid3,
e20d84c1
PM
8254 .resetvalue = 0 },
8255 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8256 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
8257 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8258 .accessfn = access_aa64_tid3,
e20d84c1
PM
8259 .resetvalue = 0 },
8260 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8261 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
8262 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8263 .accessfn = access_aa64_tid3,
e20d84c1
PM
8264 .resetvalue = 0 },
8265 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8266 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
8267 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8268 .accessfn = access_aa64_tid3,
e20d84c1
PM
8269 .resetvalue = 0 },
8270 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
8271 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
8272 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8273 .accessfn = access_aa64_tid3,
e20d84c1 8274 .resetvalue = 0 },
a50c0f51
PM
8275 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
8276 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8277 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8278 .accessfn = access_aa64_tid3,
47576b94 8279 .resetvalue = cpu->isar.mvfr0 },
a50c0f51
PM
8280 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
8281 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8282 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8283 .accessfn = access_aa64_tid3,
47576b94 8284 .resetvalue = cpu->isar.mvfr1 },
a50c0f51
PM
8285 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
8286 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8287 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8288 .accessfn = access_aa64_tid3,
47576b94 8289 .resetvalue = cpu->isar.mvfr2 },
dde4d028
PM
8290 /*
8291 * "0, c0, c3, {0,1,2}" are the encodings corresponding to
8292 * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding
8293 * as RAZ, since it is in the "reserved for future ID
8294 * registers, RAZ" part of the AArch32 encoding space.
8295 */
8296 { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32,
8297 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
8298 .access = PL1_R, .type = ARM_CP_CONST,
8299 .accessfn = access_aa64_tid3,
8300 .resetvalue = 0 },
8301 { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32,
8302 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
8303 .access = PL1_R, .type = ARM_CP_CONST,
8304 .accessfn = access_aa64_tid3,
8305 .resetvalue = 0 },
8306 { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32,
8307 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
8308 .access = PL1_R, .type = ARM_CP_CONST,
8309 .accessfn = access_aa64_tid3,
8310 .resetvalue = 0 },
8311 /*
8312 * Other encodings in "0, c0, c3, ..." are STATE_BOTH because
8313 * they're also RAZ for AArch64, and in v8 are gradually
8314 * being filled with AArch64-view-of-AArch32-ID-register
8315 * for new ID registers.
8316 */
8317 { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8318 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
8319 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8320 .accessfn = access_aa64_tid3,
e20d84c1 8321 .resetvalue = 0 },
1d51bc96 8322 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8323 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
8324 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8325 .accessfn = access_aa64_tid3,
1d51bc96 8326 .resetvalue = cpu->isar.id_pfr2 },
d22c5649 8327 { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8328 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
8329 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8330 .accessfn = access_aa64_tid3,
d22c5649 8331 .resetvalue = cpu->isar.id_dfr1 },
32957aad 8332 { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8333 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
8334 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8335 .accessfn = access_aa64_tid3,
32957aad 8336 .resetvalue = cpu->isar.id_mmfr5 },
dde4d028 8337 { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH,
e20d84c1
PM
8338 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
8339 .access = PL1_R, .type = ARM_CP_CONST,
6a4ef4e5 8340 .accessfn = access_aa64_tid3,
e20d84c1 8341 .resetvalue = 0 },
4054bfa9
AF
8342 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
8343 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
8344 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 8345 .fgt = FGT_PMCEIDN_EL0,
cad86737 8346 .resetvalue = extract64(cpu->pmceid0, 0, 32) },
4054bfa9
AF
8347 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
8348 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
8349 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 8350 .fgt = FGT_PMCEIDN_EL0,
4054bfa9
AF
8351 .resetvalue = cpu->pmceid0 },
8352 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
8353 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
8354 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 8355 .fgt = FGT_PMCEIDN_EL0,
cad86737 8356 .resetvalue = extract64(cpu->pmceid1, 0, 32) },
4054bfa9
AF
8357 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
8358 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
8359 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
dc780233 8360 .fgt = FGT_PMCEIDN_EL0,
4054bfa9 8361 .resetvalue = cpu->pmceid1 },
e60cef86 8362 };
6c5c0fec 8363#ifdef CONFIG_USER_ONLY
10b0220e 8364 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = {
6c5c0fec 8365 { .name = "ID_AA64PFR0_EL1",
bc6bd20e
ZS
8366 .exported_bits = R_ID_AA64PFR0_FP_MASK |
8367 R_ID_AA64PFR0_ADVSIMD_MASK |
8368 R_ID_AA64PFR0_SVE_MASK |
8369 R_ID_AA64PFR0_DIT_MASK,
8370 .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) |
8371 (0x1u << R_ID_AA64PFR0_EL1_SHIFT) },
6c5c0fec 8372 { .name = "ID_AA64PFR1_EL1",
bc6bd20e
ZS
8373 .exported_bits = R_ID_AA64PFR1_BT_MASK |
8374 R_ID_AA64PFR1_SSBS_MASK |
8375 R_ID_AA64PFR1_MTE_MASK |
8376 R_ID_AA64PFR1_SME_MASK },
d040242e 8377 { .name = "ID_AA64PFR*_EL1_RESERVED",
bc6bd20e
ZS
8378 .is_glob = true },
8379 { .name = "ID_AA64ZFR0_EL1",
8380 .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK |
8381 R_ID_AA64ZFR0_AES_MASK |
8382 R_ID_AA64ZFR0_BITPERM_MASK |
8383 R_ID_AA64ZFR0_BFLOAT16_MASK |
8384 R_ID_AA64ZFR0_SHA3_MASK |
8385 R_ID_AA64ZFR0_SM4_MASK |
8386 R_ID_AA64ZFR0_I8MM_MASK |
8387 R_ID_AA64ZFR0_F32MM_MASK |
8388 R_ID_AA64ZFR0_F64MM_MASK },
8389 { .name = "ID_AA64SMFR0_EL1",
8390 .exported_bits = R_ID_AA64SMFR0_F32F32_MASK |
8391 R_ID_AA64SMFR0_B16F32_MASK |
8392 R_ID_AA64SMFR0_F16F32_MASK |
8393 R_ID_AA64SMFR0_I8I32_MASK |
8394 R_ID_AA64SMFR0_F64F64_MASK |
8395 R_ID_AA64SMFR0_I16I64_MASK |
8396 R_ID_AA64SMFR0_FA64_MASK },
6c5c0fec 8397 { .name = "ID_AA64MMFR0_EL1",
bc6bd20e
ZS
8398 .exported_bits = R_ID_AA64MMFR0_ECV_MASK,
8399 .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) |
8400 (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) },
8401 { .name = "ID_AA64MMFR1_EL1",
8402 .exported_bits = R_ID_AA64MMFR1_AFP_MASK },
8403 { .name = "ID_AA64MMFR2_EL1",
8404 .exported_bits = R_ID_AA64MMFR2_AT_MASK },
d040242e 8405 { .name = "ID_AA64MMFR*_EL1_RESERVED",
bc6bd20e 8406 .is_glob = true },
6c5c0fec 8407 { .name = "ID_AA64DFR0_EL1",
bc6bd20e
ZS
8408 .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) },
8409 { .name = "ID_AA64DFR1_EL1" },
d040242e 8410 { .name = "ID_AA64DFR*_EL1_RESERVED",
bc6bd20e 8411 .is_glob = true },
d040242e 8412 { .name = "ID_AA64AFR*",
bc6bd20e 8413 .is_glob = true },
6c5c0fec 8414 { .name = "ID_AA64ISAR0_EL1",
bc6bd20e
ZS
8415 .exported_bits = R_ID_AA64ISAR0_AES_MASK |
8416 R_ID_AA64ISAR0_SHA1_MASK |
8417 R_ID_AA64ISAR0_SHA2_MASK |
8418 R_ID_AA64ISAR0_CRC32_MASK |
8419 R_ID_AA64ISAR0_ATOMIC_MASK |
8420 R_ID_AA64ISAR0_RDM_MASK |
8421 R_ID_AA64ISAR0_SHA3_MASK |
8422 R_ID_AA64ISAR0_SM3_MASK |
8423 R_ID_AA64ISAR0_SM4_MASK |
8424 R_ID_AA64ISAR0_DP_MASK |
8425 R_ID_AA64ISAR0_FHM_MASK |
8426 R_ID_AA64ISAR0_TS_MASK |
8427 R_ID_AA64ISAR0_RNDR_MASK },
6c5c0fec 8428 { .name = "ID_AA64ISAR1_EL1",
bc6bd20e
ZS
8429 .exported_bits = R_ID_AA64ISAR1_DPB_MASK |
8430 R_ID_AA64ISAR1_APA_MASK |
8431 R_ID_AA64ISAR1_API_MASK |
8432 R_ID_AA64ISAR1_JSCVT_MASK |
8433 R_ID_AA64ISAR1_FCMA_MASK |
8434 R_ID_AA64ISAR1_LRCPC_MASK |
8435 R_ID_AA64ISAR1_GPA_MASK |
8436 R_ID_AA64ISAR1_GPI_MASK |
8437 R_ID_AA64ISAR1_FRINTTS_MASK |
8438 R_ID_AA64ISAR1_SB_MASK |
8439 R_ID_AA64ISAR1_BF16_MASK |
8440 R_ID_AA64ISAR1_DGH_MASK |
8441 R_ID_AA64ISAR1_I8MM_MASK },
8442 { .name = "ID_AA64ISAR2_EL1",
8443 .exported_bits = R_ID_AA64ISAR2_WFXT_MASK |
8444 R_ID_AA64ISAR2_RPRES_MASK |
8445 R_ID_AA64ISAR2_GPA3_MASK |
8446 R_ID_AA64ISAR2_APA3_MASK },
d040242e 8447 { .name = "ID_AA64ISAR*_EL1_RESERVED",
bc6bd20e 8448 .is_glob = true },
6c5c0fec
AB
8449 };
8450 modify_arm_cp_regs(v8_idregs, v8_user_idregs);
8451#endif
be8e8128
GB
8452 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
8453 if (!arm_feature(env, ARM_FEATURE_EL3) &&
8454 !arm_feature(env, ARM_FEATURE_EL2)) {
8455 ARMCPRegInfo rvbar = {
910e4f24 8456 .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH,
be8e8128 8457 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
8458 .access = PL1_R,
8459 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
be8e8128
GB
8460 };
8461 define_one_arm_cp_reg(cpu, &rvbar);
8462 }
e60cef86 8463 define_arm_cp_regs(cpu, v8_idregs);
b0d2b7d0 8464 define_arm_cp_regs(cpu, v8_cp_reginfo);
dde4d028
PM
8465
8466 for (i = 4; i < 16; i++) {
8467 /*
8468 * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32.
8469 * For pre-v8 cores there are RAZ patterns for these in
8470 * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here.
8471 * v8 extends the "must RAZ" part of the ID register space
8472 * to also cover c0, 0, c{8-15}, {0-7}.
8473 * These are STATE_AA32 because in the AArch64 sysreg space
8474 * c4-c7 is where the AArch64 ID registers live (and we've
8475 * already defined those in v8_idregs[]), and c8-c15 are not
8476 * "must RAZ" for AArch64.
8477 */
8478 g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i);
8479 ARMCPRegInfo v8_aa32_raz_idregs = {
8480 .name = name,
8481 .state = ARM_CP_STATE_AA32,
8482 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY,
8483 .access = PL1_R, .type = ARM_CP_CONST,
8484 .accessfn = access_aa64_tid3,
8485 .resetvalue = 0 };
8486 define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs);
8487 }
b0d2b7d0 8488 }
99a90811
RH
8489
8490 /*
8491 * Register the base EL2 cpregs.
8492 * Pre v8, these registers are implemented only as part of the
8493 * Virtualization Extensions (EL2 present). Beginning with v8,
8494 * if EL2 is missing but EL3 is enabled, mostly these become
8495 * RES0 from EL3, with some specific exceptions.
8496 */
8497 if (arm_feature(env, ARM_FEATURE_EL2)
8498 || (arm_feature(env, ARM_FEATURE_EL3)
8499 && arm_feature(env, ARM_FEATURE_V8))) {
f0d574d6 8500 uint64_t vmpidr_def = mpidr_read_val(env);
731de9e6
EI
8501 ARMCPRegInfo vpidr_regs[] = {
8502 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
8503 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8504 .access = PL2_RW, .accessfn = access_el3_aa32ns,
696ba377
RH
8505 .resetvalue = cpu->midr,
8506 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
36476562 8507 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) },
731de9e6
EI
8508 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
8509 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
8510 .access = PL2_RW, .resetvalue = cpu->midr,
696ba377 8511 .type = ARM_CP_EL3_NO_EL2_C_NZ,
731de9e6 8512 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
f0d574d6
EI
8513 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
8514 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
8515 .access = PL2_RW, .accessfn = access_el3_aa32ns,
696ba377
RH
8516 .resetvalue = vmpidr_def,
8517 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ,
36476562 8518 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) },
f0d574d6
EI
8519 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
8520 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
696ba377
RH
8521 .access = PL2_RW, .resetvalue = vmpidr_def,
8522 .type = ARM_CP_EL3_NO_EL2_C_NZ,
f0d574d6 8523 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
731de9e6 8524 };
24526bb9
PM
8525 /*
8526 * The only field of MDCR_EL2 that has a defined architectural reset
8527 * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N.
8528 */
8529 ARMCPRegInfo mdcr_el2 = {
7f4fbfb5 8530 .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO,
24526bb9 8531 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
01765386 8532 .writefn = mdcr_el2_write,
24526bb9
PM
8533 .access = PL2_RW, .resetvalue = pmu_num_counters(env),
8534 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2),
8535 };
8536 define_one_arm_cp_reg(cpu, &mdcr_el2);
731de9e6 8537 define_arm_cp_regs(cpu, vpidr_regs);
4771cd01 8538 define_arm_cp_regs(cpu, el2_cp_reginfo);
ce4afed8
PM
8539 if (arm_feature(env, ARM_FEATURE_V8)) {
8540 define_arm_cp_regs(cpu, el2_v8_cp_reginfo);
8541 }
e9152ee9
RDC
8542 if (cpu_isar_feature(aa64_sel2, cpu)) {
8543 define_arm_cp_regs(cpu, el2_sec_cp_reginfo);
8544 }
be8e8128
GB
8545 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
8546 if (!arm_feature(env, ARM_FEATURE_EL3)) {
910e4f24
TR
8547 ARMCPRegInfo rvbar[] = {
8548 {
8549 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
8550 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
8551 .access = PL2_R,
8552 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8553 },
8554 { .name = "RVBAR", .type = ARM_CP_ALIAS,
8555 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
8556 .access = PL2_R,
8557 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8558 },
be8e8128 8559 };
910e4f24 8560 define_arm_cp_regs(cpu, rvbar);
be8e8128 8561 }
3b685ba7 8562 }
99a90811
RH
8563
8564 /* Register the base EL3 cpregs. */
81547d66 8565 if (arm_feature(env, ARM_FEATURE_EL3)) {
0f1a3b24 8566 define_arm_cp_regs(cpu, el3_cp_reginfo);
e24fdd23
PM
8567 ARMCPRegInfo el3_regs[] = {
8568 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
8569 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4a7319b7
EI
8570 .access = PL3_R,
8571 .fieldoffset = offsetof(CPUARMState, cp15.rvbar),
8572 },
e24fdd23
PM
8573 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
8574 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
8575 .access = PL3_RW,
8576 .raw_writefn = raw_write, .writefn = sctlr_write,
8577 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
8578 .resetvalue = cpu->reset_sctlr },
be8e8128 8579 };
e24fdd23
PM
8580
8581 define_arm_cp_regs(cpu, el3_regs);
81547d66 8582 }
9b37a28c
FR
8583 /*
8584 * The behaviour of NSACR is sufficiently various that we don't
2f027fc5
PM
8585 * try to describe it in a single reginfo:
8586 * if EL3 is 64 bit, then trap to EL3 from S EL1,
8587 * reads as constant 0xc00 from NS EL1 and NS EL2
8588 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
8589 * if v7 without EL3, register doesn't exist
8590 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
8591 */
8592 if (arm_feature(env, ARM_FEATURE_EL3)) {
8593 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
10b0220e 8594 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8595 .name = "NSACR", .type = ARM_CP_CONST,
8596 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8597 .access = PL1_RW, .accessfn = nsacr_access,
8598 .resetvalue = 0xc00
8599 };
8600 define_one_arm_cp_reg(cpu, &nsacr);
8601 } else {
10b0220e 8602 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8603 .name = "NSACR",
8604 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8605 .access = PL3_RW | PL1_R,
8606 .resetvalue = 0,
8607 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
8608 };
8609 define_one_arm_cp_reg(cpu, &nsacr);
8610 }
8611 } else {
8612 if (arm_feature(env, ARM_FEATURE_V8)) {
10b0220e 8613 static const ARMCPRegInfo nsacr = {
2f027fc5
PM
8614 .name = "NSACR", .type = ARM_CP_CONST,
8615 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
8616 .access = PL1_R,
8617 .resetvalue = 0xc00
8618 };
8619 define_one_arm_cp_reg(cpu, &nsacr);
8620 }
8621 }
8622
452a0955 8623 if (arm_feature(env, ARM_FEATURE_PMSA)) {
6cb0b013
PC
8624 if (arm_feature(env, ARM_FEATURE_V6)) {
8625 /* PMSAv6 not implemented */
8626 assert(arm_feature(env, ARM_FEATURE_V7));
8627 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
8628 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
8629 } else {
8630 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
8631 }
18032bec 8632 } else {
8e5d75c9 8633 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
18032bec 8634 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4036b7d1
PM
8635 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */
8636 if (cpu_isar_feature(aa32_hpd, cpu)) {
ab638a32
RH
8637 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo);
8638 }
18032bec 8639 }
c326b979
PM
8640 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
8641 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
8642 }
6cc7a3ae
PM
8643 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
8644 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
8645 }
4a501606
PM
8646 if (arm_feature(env, ARM_FEATURE_VAPA)) {
8647 define_arm_cp_regs(cpu, vapa_cp_reginfo);
8648 }
c4804214
PM
8649 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
8650 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
8651 }
8652 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
8653 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
8654 }
8655 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
8656 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
8657 }
18032bec
PM
8658 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
8659 define_arm_cp_regs(cpu, omap_cp_reginfo);
8660 }
34f90529
PM
8661 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
8662 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
8663 }
1047b9d7
PM
8664 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
8665 define_arm_cp_regs(cpu, xscale_cp_reginfo);
8666 }
8667 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
8668 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
8669 }
7ac681cf
PM
8670 if (arm_feature(env, ARM_FEATURE_LPAE)) {
8671 define_arm_cp_regs(cpu, lpae_cp_reginfo);
8672 }
873b73c0 8673 if (cpu_isar_feature(aa32_jazelle, cpu)) {
f96f3d5f
MZ
8674 define_arm_cp_regs(cpu, jazelle_regs);
8675 }
9b37a28c
FR
8676 /*
8677 * Slightly awkwardly, the OMAP and StrongARM cores need all of
7884849c
PM
8678 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
8679 * be read-only (ie write causes UNDEF exception).
8680 */
8681 {
00a29f3d 8682 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
9b37a28c
FR
8683 /*
8684 * Pre-v8 MIDR space.
00a29f3d 8685 * Note that the MIDR isn't a simple constant register because
7884849c
PM
8686 * of the TI925 behaviour where writes to another register can
8687 * cause the MIDR value to change.
97ce8d61
PC
8688 *
8689 * Unimplemented registers in the c15 0 0 0 space default to
8690 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
8691 * and friends override accordingly.
7884849c
PM
8692 */
8693 { .name = "MIDR",
97ce8d61 8694 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
7884849c 8695 .access = PL1_R, .resetvalue = cpu->midr,
d4e6df63 8696 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
731de9e6 8697 .readfn = midr_read,
97ce8d61
PC
8698 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8699 .type = ARM_CP_OVERRIDE },
7884849c
PM
8700 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
8701 { .name = "DUMMY",
8702 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
8703 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8704 { .name = "DUMMY",
8705 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
8706 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8707 { .name = "DUMMY",
8708 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
8709 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8710 { .name = "DUMMY",
8711 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
8712 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
8713 { .name = "DUMMY",
8714 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
8715 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
7884849c 8716 };
00a29f3d 8717 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
00a29f3d
PM
8718 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
8719 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
731de9e6 8720 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
67dd8030 8721 .fgt = FGT_MIDR_EL1,
731de9e6
EI
8722 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
8723 .readfn = midr_read },
c7f786ab 8724 /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */
ac00c79f
SF
8725 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8726 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
8727 .access = PL1_R, .resetvalue = cpu->midr },
00a29f3d
PM
8728 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
8729 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
93fbc983
MZ
8730 .access = PL1_R,
8731 .accessfn = access_aa64_tid1,
67dd8030 8732 .fgt = FGT_REVIDR_EL1,
93fbc983 8733 .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
00a29f3d 8734 };
c7f786ab
TR
8735 ARMCPRegInfo id_v8_midr_alias_cp_reginfo = {
8736 .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
8737 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8738 .access = PL1_R, .resetvalue = cpu->midr
8739 };
00a29f3d
PM
8740 ARMCPRegInfo id_cp_reginfo[] = {
8741 /* These are common to v8 and pre-v8 */
8742 { .name = "CTR",
8743 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
630fcd4d
MZ
8744 .access = PL1_R, .accessfn = ctr_el0_access,
8745 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
00a29f3d
PM
8746 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
8747 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
8748 .access = PL0_R, .accessfn = ctr_el0_access,
b19ed03c 8749 .fgt = FGT_CTR_EL0,
00a29f3d
PM
8750 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
8751 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
8752 { .name = "TCMTR",
8753 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
93fbc983
MZ
8754 .access = PL1_R,
8755 .accessfn = access_aa32_tid1,
8756 .type = ARM_CP_CONST, .resetvalue = 0 },
00a29f3d 8757 };
8085ce63
PC
8758 /* TLBTR is specific to VMSA */
8759 ARMCPRegInfo id_tlbtr_reginfo = {
8760 .name = "TLBTR",
8761 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
93fbc983
MZ
8762 .access = PL1_R,
8763 .accessfn = access_aa32_tid1,
8764 .type = ARM_CP_CONST, .resetvalue = 0,
8085ce63 8765 };
3281af81
PC
8766 /* MPUIR is specific to PMSA V6+ */
8767 ARMCPRegInfo id_mpuir_reginfo = {
8768 .name = "MPUIR",
8769 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
8770 .access = PL1_R, .type = ARM_CP_CONST,
8771 .resetvalue = cpu->pmsav7_dregion << 8
8772 };
761c4642
TR
8773 /* HMPUIR is specific to PMSA V8 */
8774 ARMCPRegInfo id_hmpuir_reginfo = {
8775 .name = "HMPUIR",
8776 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4,
8777 .access = PL2_R, .type = ARM_CP_CONST,
8778 .resetvalue = cpu->pmsav8r_hdregion
8779 };
10b0220e 8780 static const ARMCPRegInfo crn0_wi_reginfo = {
7884849c
PM
8781 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
8782 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
8783 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
8784 };
6c5c0fec 8785#ifdef CONFIG_USER_ONLY
10b0220e 8786 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = {
6c5c0fec 8787 { .name = "MIDR_EL1",
bc6bd20e
ZS
8788 .exported_bits = R_MIDR_EL1_REVISION_MASK |
8789 R_MIDR_EL1_PARTNUM_MASK |
8790 R_MIDR_EL1_ARCHITECTURE_MASK |
8791 R_MIDR_EL1_VARIANT_MASK |
8792 R_MIDR_EL1_IMPLEMENTER_MASK },
8793 { .name = "REVIDR_EL1" },
6c5c0fec
AB
8794 };
8795 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo);
8796#endif
7884849c
PM
8797 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
8798 arm_feature(env, ARM_FEATURE_STRONGARM)) {
5809ac57 8799 size_t i;
9b37a28c
FR
8800 /*
8801 * Register the blanket "writes ignored" value first to cover the
a703eda1
PC
8802 * whole space. Then update the specific ID registers to allow write
8803 * access, so that they ignore writes rather than causing them to
8804 * UNDEF.
7884849c
PM
8805 */
8806 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
5809ac57
RH
8807 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) {
8808 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW;
00a29f3d 8809 }
5809ac57
RH
8810 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) {
8811 id_cp_reginfo[i].access = PL1_RW;
7884849c 8812 }
10006112 8813 id_mpuir_reginfo.access = PL1_RW;
3281af81 8814 id_tlbtr_reginfo.access = PL1_RW;
7884849c 8815 }
00a29f3d
PM
8816 if (arm_feature(env, ARM_FEATURE_V8)) {
8817 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
c7f786ab
TR
8818 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8819 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo);
8820 }
00a29f3d
PM
8821 } else {
8822 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
8823 }
a703eda1 8824 define_arm_cp_regs(cpu, id_cp_reginfo);
452a0955 8825 if (!arm_feature(env, ARM_FEATURE_PMSA)) {
8085ce63 8826 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
761c4642
TR
8827 } else if (arm_feature(env, ARM_FEATURE_PMSA) &&
8828 arm_feature(env, ARM_FEATURE_V8)) {
8829 uint32_t i = 0;
8830 char *tmp_string;
8831
8832 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8833 define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo);
8834 define_arm_cp_regs(cpu, pmsav8r_cp_reginfo);
8835
8836 /* Register alias is only valid for first 32 indexes */
8837 for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) {
8838 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8839 uint8_t opc1 = extract32(i, 4, 1);
8840 uint8_t opc2 = extract32(i, 0, 1) << 2;
8841
8842 tmp_string = g_strdup_printf("PRBAR%u", i);
8843 ARMCPRegInfo tmp_prbarn_reginfo = {
8844 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8845 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8846 .access = PL1_RW, .resetvalue = 0,
8847 .accessfn = access_tvm_trvm,
8848 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8849 };
8850 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo);
8851 g_free(tmp_string);
8852
8853 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8854 tmp_string = g_strdup_printf("PRLAR%u", i);
8855 ARMCPRegInfo tmp_prlarn_reginfo = {
8856 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW,
8857 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8858 .access = PL1_RW, .resetvalue = 0,
8859 .accessfn = access_tvm_trvm,
8860 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8861 };
8862 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo);
8863 g_free(tmp_string);
8864 }
8865
8866 /* Register alias is only valid for first 32 indexes */
8867 for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) {
8868 uint8_t crm = 0b1000 | extract32(i, 1, 3);
8869 uint8_t opc1 = 0b100 | extract32(i, 4, 1);
8870 uint8_t opc2 = extract32(i, 0, 1) << 2;
8871
8872 tmp_string = g_strdup_printf("HPRBAR%u", i);
8873 ARMCPRegInfo tmp_hprbarn_reginfo = {
8874 .name = tmp_string,
8875 .type = ARM_CP_NO_RAW,
8876 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8877 .access = PL2_RW, .resetvalue = 0,
8878 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8879 };
8880 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo);
8881 g_free(tmp_string);
8882
8883 opc2 = extract32(i, 0, 1) << 2 | 0x1;
8884 tmp_string = g_strdup_printf("HPRLAR%u", i);
8885 ARMCPRegInfo tmp_hprlarn_reginfo = {
8886 .name = tmp_string,
8887 .type = ARM_CP_NO_RAW,
8888 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2,
8889 .access = PL2_RW, .resetvalue = 0,
8890 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read
8891 };
8892 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo);
8893 g_free(tmp_string);
8894 }
3281af81
PC
8895 } else if (arm_feature(env, ARM_FEATURE_V7)) {
8896 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
8085ce63 8897 }
7884849c
PM
8898 }
8899
97ce8d61 8900 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
52264166
AB
8901 ARMCPRegInfo mpidr_cp_reginfo[] = {
8902 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH,
8903 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
67dd8030 8904 .fgt = FGT_MPIDR_EL1,
52264166 8905 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
52264166
AB
8906 };
8907#ifdef CONFIG_USER_ONLY
10b0220e 8908 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = {
52264166
AB
8909 { .name = "MPIDR_EL1",
8910 .fixed_bits = 0x0000000080000000 },
52264166
AB
8911 };
8912 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo);
8913#endif
97ce8d61
PC
8914 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
8915 }
8916
2771db27 8917 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
834a6c69
PM
8918 ARMCPRegInfo auxcr_reginfo[] = {
8919 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
8920 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
99602377
RH
8921 .access = PL1_RW, .accessfn = access_tacr,
8922 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr },
834a6c69
PM
8923 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
8924 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
8925 .access = PL2_RW, .type = ARM_CP_CONST,
8926 .resetvalue = 0 },
8927 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
8928 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
8929 .access = PL3_RW, .type = ARM_CP_CONST,
8930 .resetvalue = 0 },
2771db27 8931 };
834a6c69 8932 define_arm_cp_regs(cpu, auxcr_reginfo);
f6287c24
PM
8933 if (cpu_isar_feature(aa32_ac2, cpu)) {
8934 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo);
0e0456ab 8935 }
2771db27
PM
8936 }
8937
d8ba780b 8938 if (arm_feature(env, ARM_FEATURE_CBAR)) {
d56974af
LM
8939 /*
8940 * CBAR is IMPDEF, but common on Arm Cortex-A implementations.
8941 * There are two flavours:
8942 * (1) older 32-bit only cores have a simple 32-bit CBAR
8943 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a
8944 * 32-bit register visible to AArch32 at a different encoding
8945 * to the "flavour 1" register and with the bits rearranged to
8946 * be able to squash a 64-bit address into the 32-bit view.
8947 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but
8948 * in future if we support AArch32-only configs of some of the
8949 * AArch64 cores we might need to add a specific feature flag
8950 * to indicate cores with "flavour 2" CBAR.
8951 */
f318cec6
PM
8952 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
8953 /* 32 bit view is [31:18] 0...0 [43:32]. */
8954 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
8955 | extract64(cpu->reset_cbar, 32, 12);
8956 ARMCPRegInfo cbar_reginfo[] = {
8957 { .name = "CBAR",
8958 .type = ARM_CP_CONST,
d56974af
LM
8959 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0,
8960 .access = PL1_R, .resetvalue = cbar32 },
f318cec6
PM
8961 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
8962 .type = ARM_CP_CONST,
8963 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
d56974af 8964 .access = PL1_R, .resetvalue = cpu->reset_cbar },
f318cec6
PM
8965 };
8966 /* We don't implement a r/w 64 bit CBAR currently */
8967 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
8968 define_arm_cp_regs(cpu, cbar_reginfo);
8969 } else {
8970 ARMCPRegInfo cbar = {
8971 .name = "CBAR",
8972 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
04215eb1 8973 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar,
f318cec6
PM
8974 .fieldoffset = offsetof(CPUARMState,
8975 cp15.c15_config_base_address)
8976 };
8977 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
8978 cbar.access = PL1_R;
8979 cbar.fieldoffset = 0;
8980 cbar.type = ARM_CP_CONST;
8981 }
8982 define_one_arm_cp_reg(cpu, &cbar);
8983 }
d8ba780b
PC
8984 }
8985
91db4642 8986 if (arm_feature(env, ARM_FEATURE_VBAR)) {
10b0220e 8987 static const ARMCPRegInfo vbar_cp_reginfo[] = {
91db4642
CLG
8988 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
8989 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
8990 .access = PL1_RW, .writefn = vbar_write,
bd8db7d9 8991 .fgt = FGT_VBAR_EL1,
91db4642
CLG
8992 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
8993 offsetof(CPUARMState, cp15.vbar_ns) },
8994 .resetvalue = 0 },
91db4642
CLG
8995 };
8996 define_arm_cp_regs(cpu, vbar_cp_reginfo);
8997 }
8998
2771db27
PM
8999 /* Generic registers whose values depend on the implementation */
9000 {
9001 ARMCPRegInfo sctlr = {
5ebafdf3 9002 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
137feaa9 9003 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
84929218 9004 .access = PL1_RW, .accessfn = access_tvm_trvm,
67dd8030 9005 .fgt = FGT_SCTLR_EL1,
137feaa9
FA
9006 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
9007 offsetof(CPUARMState, cp15.sctlr_ns) },
d4e6df63
PM
9008 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
9009 .raw_writefn = raw_write,
2771db27
PM
9010 };
9011 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
9b37a28c
FR
9012 /*
9013 * Normally we would always end the TB on an SCTLR write, but Linux
2771db27
PM
9014 * arch/arm/mach-pxa/sleep.S expects two instructions following
9015 * an MMU enable to execute from cache. Imitate this behaviour.
9016 */
9017 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
9018 }
9019 define_one_arm_cp_reg(cpu, &sctlr);
761c4642
TR
9020
9021 if (arm_feature(env, ARM_FEATURE_PMSA) &&
9022 arm_feature(env, ARM_FEATURE_V8)) {
9023 ARMCPRegInfo vsctlr = {
9024 .name = "VSCTLR", .state = ARM_CP_STATE_AA32,
9025 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
9026 .access = PL2_RW, .resetvalue = 0x0,
9027 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr),
9028 };
9029 define_one_arm_cp_reg(cpu, &vsctlr);
9030 }
2771db27 9031 }
5be5e8ed 9032
2d7137c1 9033 if (cpu_isar_feature(aa64_lor, cpu)) {
2d7137c1
RH
9034 define_arm_cp_regs(cpu, lor_reginfo);
9035 }
220f508f
RH
9036 if (cpu_isar_feature(aa64_pan, cpu)) {
9037 define_one_arm_cp_reg(cpu, &pan_reginfo);
9038 }
04b07d29
RH
9039#ifndef CONFIG_USER_ONLY
9040 if (cpu_isar_feature(aa64_ats1e1, cpu)) {
9041 define_arm_cp_regs(cpu, ats1e1_reginfo);
9042 }
9043 if (cpu_isar_feature(aa32_ats1e1, cpu)) {
9044 define_arm_cp_regs(cpu, ats1cp_reginfo);
9045 }
9046#endif
9eeb7a1c
RH
9047 if (cpu_isar_feature(aa64_uao, cpu)) {
9048 define_one_arm_cp_reg(cpu, &uao_reginfo);
9049 }
2d7137c1 9050
dc8b1853
RC
9051 if (cpu_isar_feature(aa64_dit, cpu)) {
9052 define_one_arm_cp_reg(cpu, &dit_reginfo);
9053 }
f2f68a78
RC
9054 if (cpu_isar_feature(aa64_ssbs, cpu)) {
9055 define_one_arm_cp_reg(cpu, &ssbs_reginfo);
9056 }
58e93b48
RH
9057 if (cpu_isar_feature(any_ras, cpu)) {
9058 define_arm_cp_regs(cpu, minimal_ras_reginfo);
9059 }
dc8b1853 9060
52d18727
RH
9061 if (cpu_isar_feature(aa64_vh, cpu) ||
9062 cpu_isar_feature(aa64_debugv8p2, cpu)) {
9063 define_one_arm_cp_reg(cpu, &contextidr_el2);
9064 }
e2a1a461
RH
9065 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9066 define_arm_cp_regs(cpu, vhe_reginfo);
9067 }
9068
cd208a1c 9069 if (cpu_isar_feature(aa64_sve, cpu)) {
60360d82 9070 define_arm_cp_regs(cpu, zcr_reginfo);
5be5e8ed 9071 }
967aa94f 9072
5814d587
RH
9073 if (cpu_isar_feature(aa64_hcx, cpu)) {
9074 define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo);
9075 }
9076
967aa94f 9077#ifdef TARGET_AARCH64
9e5ec745
RH
9078 if (cpu_isar_feature(aa64_sme, cpu)) {
9079 define_arm_cp_regs(cpu, sme_reginfo);
9080 }
967aa94f
RH
9081 if (cpu_isar_feature(aa64_pauth, cpu)) {
9082 define_arm_cp_regs(cpu, pauth_reginfo);
9083 }
de390645
RH
9084 if (cpu_isar_feature(aa64_rndr, cpu)) {
9085 define_arm_cp_regs(cpu, rndr_reginfo);
9086 }
84940ed8
RC
9087 if (cpu_isar_feature(aa64_tlbirange, cpu)) {
9088 define_arm_cp_regs(cpu, tlbirange_reginfo);
9089 }
7113d618
RC
9090 if (cpu_isar_feature(aa64_tlbios, cpu)) {
9091 define_arm_cp_regs(cpu, tlbios_reginfo);
9092 }
0d57b499
BM
9093#ifndef CONFIG_USER_ONLY
9094 /* Data Cache clean instructions up to PoP */
9095 if (cpu_isar_feature(aa64_dcpop, cpu)) {
9096 define_one_arm_cp_reg(cpu, dcpop_reg);
9097
9098 if (cpu_isar_feature(aa64_dcpodp, cpu)) {
9099 define_one_arm_cp_reg(cpu, dcpodp_reg);
9100 }
9101 }
9102#endif /*CONFIG_USER_ONLY*/
4b779ceb
RH
9103
9104 /*
9105 * If full MTE is enabled, add all of the system registers.
9106 * If only "instructions available at EL0" are enabled,
9107 * then define only a RAZ/WI version of PSTATE.TCO.
9108 */
9109 if (cpu_isar_feature(aa64_mte, cpu)) {
9110 define_arm_cp_regs(cpu, mte_reginfo);
5463df16 9111 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb
RH
9112 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) {
9113 define_arm_cp_regs(cpu, mte_tco_ro_reginfo);
5463df16 9114 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo);
4b779ceb 9115 }
7cb1e618
RH
9116
9117 if (cpu_isar_feature(aa64_scxtnum, cpu)) {
9118 define_arm_cp_regs(cpu, scxtnum_reginfo);
9119 }
15126d9c
PM
9120
9121 if (cpu_isar_feature(aa64_fgt, cpu)) {
9122 define_arm_cp_regs(cpu, fgt_reginfo);
9123 }
967aa94f 9124#endif
cb570bd3 9125
22e57073 9126 if (cpu_isar_feature(any_predinv, cpu)) {
cb570bd3
RH
9127 define_arm_cp_regs(cpu, predinv_reginfo);
9128 }
e2cce18f 9129
957e6155
PM
9130 if (cpu_isar_feature(any_ccidx, cpu)) {
9131 define_arm_cp_regs(cpu, ccsidr2_reginfo);
9132 }
9133
e2cce18f
RH
9134#ifndef CONFIG_USER_ONLY
9135 /*
9136 * Register redirections and aliases must be done last,
9137 * after the registers from the other extensions have been defined.
9138 */
9139 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) {
9140 define_arm_vh_e2h_redirects_aliases(cpu);
9141 }
9142#endif
2ceb98c0
PM
9143}
9144
777dc784
PM
9145/* Sort alphabetically by type name, except for "any". */
9146static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
5adb4839 9147{
777dc784
PM
9148 ObjectClass *class_a = (ObjectClass *)a;
9149 ObjectClass *class_b = (ObjectClass *)b;
9150 const char *name_a, *name_b;
5adb4839 9151
777dc784
PM
9152 name_a = object_class_get_name(class_a);
9153 name_b = object_class_get_name(class_b);
51492fd1 9154 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
777dc784 9155 return 1;
51492fd1 9156 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
777dc784
PM
9157 return -1;
9158 } else {
9159 return strcmp(name_a, name_b);
5adb4839
PB
9160 }
9161}
9162
777dc784 9163static void arm_cpu_list_entry(gpointer data, gpointer user_data)
40f137e1 9164{
777dc784 9165 ObjectClass *oc = data;
977c33ba 9166 CPUClass *cc = CPU_CLASS(oc);
51492fd1
AF
9167 const char *typename;
9168 char *name;
3371d272 9169
51492fd1
AF
9170 typename = object_class_get_name(oc);
9171 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
977c33ba
DB
9172 if (cc->deprecation_note) {
9173 qemu_printf(" %s (deprecated)\n", name);
9174 } else {
9175 qemu_printf(" %s\n", name);
9176 }
51492fd1 9177 g_free(name);
777dc784
PM
9178}
9179
0442428a 9180void arm_cpu_list(void)
777dc784 9181{
777dc784
PM
9182 GSList *list;
9183
9184 list = object_class_get_list(TYPE_ARM_CPU, false);
9185 list = g_slist_sort(list, arm_cpu_list_compare);
0442428a
MA
9186 qemu_printf("Available CPUs:\n");
9187 g_slist_foreach(list, arm_cpu_list_entry, NULL);
777dc784 9188 g_slist_free(list);
40f137e1
PB
9189}
9190
78027bb6
CR
9191static void arm_cpu_add_definition(gpointer data, gpointer user_data)
9192{
9193 ObjectClass *oc = data;
9194 CpuDefinitionInfoList **cpu_list = user_data;
78027bb6
CR
9195 CpuDefinitionInfo *info;
9196 const char *typename;
9197
9198 typename = object_class_get_name(oc);
9199 info = g_malloc0(sizeof(*info));
9200 info->name = g_strndup(typename,
9201 strlen(typename) - strlen("-" TYPE_ARM_CPU));
8ed877b7 9202 info->q_typename = g_strdup(typename);
78027bb6 9203
54aa3de7 9204 QAPI_LIST_PREPEND(*cpu_list, info);
78027bb6
CR
9205}
9206
25a9d6ca 9207CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
78027bb6
CR
9208{
9209 CpuDefinitionInfoList *cpu_list = NULL;
9210 GSList *list;
9211
9212 list = object_class_get_list(TYPE_ARM_CPU, false);
9213 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
9214 g_slist_free(list);
9215
9216 return cpu_list;
9217}
9218
1859f8c3
RH
9219/*
9220 * Private utility function for define_one_arm_cp_reg_with_opaque():
9221 * add a single reginfo struct to the hash table.
9222 */
6e6efd61 9223static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
cbe64585
RH
9224 void *opaque, CPState state,
9225 CPSecureState secstate,
9c513e78
AB
9226 int crm, int opc1, int opc2,
9227 const char *name)
6e6efd61 9228{
696ba377 9229 CPUARMState *env = &cpu->env;
5860362d 9230 uint32_t key;
c27f5d3a 9231 ARMCPRegInfo *r2;
4c8c4541
RH
9232 bool is64 = r->type & ARM_CP_64BIT;
9233 bool ns = secstate & ARM_CP_SECSTATE_NS;
cac65299 9234 int cp = r->cp;
c27f5d3a 9235 size_t name_len;
696ba377 9236 bool make_const;
c27f5d3a 9237
cac65299
RH
9238 switch (state) {
9239 case ARM_CP_STATE_AA32:
9240 /* We assume it is a cp15 register if the .cp field is left unset. */
9241 if (cp == 0 && r->state == ARM_CP_STATE_BOTH) {
9242 cp = 15;
9243 }
9244 key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2);
9245 break;
9246 case ARM_CP_STATE_AA64:
9247 /*
9248 * To allow abbreviation of ARMCPRegInfo definitions, we treat
9249 * cp == 0 as equivalent to the value for "standard guest-visible
9250 * sysreg". STATE_BOTH definitions are also always "standard sysreg"
9251 * in their AArch64 view (the .cp value may be non-zero for the
9252 * benefit of the AArch32 view).
9253 */
9254 if (cp == 0 || r->state == ARM_CP_STATE_BOTH) {
9255 cp = CP_REG_ARM64_SYSREG_CP;
9256 }
9257 key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2);
9258 break;
9259 default:
9260 g_assert_not_reached();
9261 }
9262
dc44545b
RH
9263 /* Overriding of an existing definition must be explicitly requested. */
9264 if (!(r->type & ARM_CP_OVERRIDE)) {
9265 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key);
9266 if (oldreg) {
9267 assert(oldreg->type & ARM_CP_OVERRIDE);
9268 }
9269 }
9270
696ba377
RH
9271 /*
9272 * Eliminate registers that are not present because the EL is missing.
9273 * Doing this here makes it easier to put all registers for a given
9274 * feature into the same ARMCPRegInfo array and define them all at once.
9275 */
9276 make_const = false;
9277 if (arm_feature(env, ARM_FEATURE_EL3)) {
9278 /*
9279 * An EL2 register without EL2 but with EL3 is (usually) RES0.
9280 * See rule RJFFP in section D1.1.3 of DDI0487H.a.
9281 */
9282 int min_el = ctz32(r->access) / 2;
9283 if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) {
9284 if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) {
9285 return;
9286 }
9287 make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP);
9288 }
9289 } else {
9290 CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2)
9291 ? PL2_RW : PL1_RW);
9292 if ((r->access & max_el) == 0) {
9293 return;
9294 }
9295 }
9296
c27f5d3a
RH
9297 /* Combine cpreg and name into one allocation. */
9298 name_len = strlen(name) + 1;
9299 r2 = g_malloc(sizeof(*r2) + name_len);
9300 *r2 = *r;
9301 r2->name = memcpy(r2 + 1, name, name_len);
3f3c82a5 9302
cc946d96
RH
9303 /*
9304 * Update fields to match the instantiation, overwiting wildcards
9305 * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH.
3f3c82a5 9306 */
cc946d96
RH
9307 r2->cp = cp;
9308 r2->crm = crm;
9309 r2->opc1 = opc1;
9310 r2->opc2 = opc2;
9311 r2->state = state;
3f3c82a5 9312 r2->secure = secstate;
cc946d96
RH
9313 if (opaque) {
9314 r2->opaque = opaque;
9315 }
3f3c82a5 9316
696ba377
RH
9317 if (make_const) {
9318 /* This should not have been a very special register to begin. */
9319 int old_special = r2->type & ARM_CP_SPECIAL_MASK;
9320 assert(old_special == 0 || old_special == ARM_CP_NOP);
1859f8c3 9321 /*
696ba377
RH
9322 * Set the special function to CONST, retaining the other flags.
9323 * This is important for e.g. ARM_CP_SVE so that we still
9324 * take the SVE trap if CPTR_EL3.EZ == 0.
f5a0a5a5 9325 */
696ba377
RH
9326 r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST;
9327 /*
9328 * Usually, these registers become RES0, but there are a few
9329 * special cases like VPIDR_EL2 which have a constant non-zero
9330 * value with writes ignored.
9331 */
9332 if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) {
9333 r2->resetvalue = 0;
9334 }
9335 /*
9336 * ARM_CP_CONST has precedence, so removing the callbacks and
9337 * offsets are not strictly necessary, but it is potentially
9338 * less confusing to debug later.
9339 */
9340 r2->readfn = NULL;
9341 r2->writefn = NULL;
9342 r2->raw_readfn = NULL;
9343 r2->raw_writefn = NULL;
9344 r2->resetfn = NULL;
9345 r2->fieldoffset = 0;
9346 r2->bank_fieldoffsets[0] = 0;
9347 r2->bank_fieldoffsets[1] = 0;
9348 } else {
9349 bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1];
3f3c82a5 9350
10748a96 9351 if (isbanked) {
1859f8c3 9352 /*
696ba377
RH
9353 * Register is banked (using both entries in array).
9354 * Overwriting fieldoffset as the array is only used to define
9355 * banked registers but later only fieldoffset is used.
3f3c82a5 9356 */
696ba377
RH
9357 r2->fieldoffset = r->bank_fieldoffsets[ns];
9358 }
9359 if (state == ARM_CP_STATE_AA32) {
9360 if (isbanked) {
9361 /*
9362 * If the register is banked then we don't need to migrate or
9363 * reset the 32-bit instance in certain cases:
9364 *
9365 * 1) If the register has both 32-bit and 64-bit instances
9366 * then we can count on the 64-bit instance taking care
9367 * of the non-secure bank.
9368 * 2) If ARMv8 is enabled then we can count on a 64-bit
9369 * version taking care of the secure bank. This requires
9370 * that separate 32 and 64-bit definitions are provided.
9371 */
9372 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
9373 (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
9374 r2->type |= ARM_CP_ALIAS;
9375 }
9376 } else if ((secstate != r->secure) && !ns) {
9377 /*
9378 * The register is not banked so we only want to allow
9379 * migration of the non-secure instance.
9380 */
7a0e58fa 9381 r2->type |= ARM_CP_ALIAS;
3f3c82a5 9382 }
3f3c82a5 9383
696ba377
RH
9384 if (HOST_BIG_ENDIAN &&
9385 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) {
9386 r2->fieldoffset += sizeof(uint32_t);
9387 }
3f3c82a5 9388 }
f5a0a5a5 9389 }
cc946d96 9390
1859f8c3
RH
9391 /*
9392 * By convention, for wildcarded registers only the first
6e6efd61 9393 * entry is used for migration; the others are marked as
7a0e58fa 9394 * ALIAS so we don't try to transfer the register
6e6efd61 9395 * multiple times. Special registers (ie NOP/WFI) are
7a0e58fa 9396 * never migratable and not even raw-accessible.
6e6efd61 9397 */
696ba377 9398 if (r2->type & ARM_CP_SPECIAL_MASK) {
7a0e58fa
PM
9399 r2->type |= ARM_CP_NO_RAW;
9400 }
9401 if (((r->crm == CP_ANY) && crm != 0) ||
6e6efd61
PM
9402 ((r->opc1 == CP_ANY) && opc1 != 0) ||
9403 ((r->opc2 == CP_ANY) && opc2 != 0)) {
1f163787 9404 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB;
6e6efd61
PM
9405 }
9406
1859f8c3
RH
9407 /*
9408 * Check that raw accesses are either forbidden or handled. Note that
375421cc
PM
9409 * we can't assert this earlier because the setup of fieldoffset for
9410 * banked registers has to be done first.
9411 */
9412 if (!(r2->type & ARM_CP_NO_RAW)) {
9413 assert(!raw_accessors_invalid(r2));
9414 }
9415
5860362d 9416 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2);
6e6efd61
PM
9417}
9418
9419
4b6a83fb
PM
9420void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
9421 const ARMCPRegInfo *r, void *opaque)
9422{
9b37a28c
FR
9423 /*
9424 * Define implementations of coprocessor registers.
4b6a83fb
PM
9425 * We store these in a hashtable because typically
9426 * there are less than 150 registers in a space which
9427 * is 16*16*16*8*8 = 262144 in size.
9428 * Wildcarding is supported for the crm, opc1 and opc2 fields.
9429 * If a register is defined twice then the second definition is
9430 * used, so this can be used to define some generic registers and
9431 * then override them with implementation specific variations.
9432 * At least one of the original and the second definition should
9433 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
9434 * against accidental use.
f5a0a5a5
PM
9435 *
9436 * The state field defines whether the register is to be
9437 * visible in the AArch32 or AArch64 execution state. If the
9438 * state is set to ARM_CP_STATE_BOTH then we synthesise a
9439 * reginfo structure for the AArch32 view, which sees the lower
9440 * 32 bits of the 64 bit register.
9441 *
9442 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
9443 * be wildcarded. AArch64 registers are always considered to be 64
9444 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
9445 * the register, if any.
4b6a83fb 9446 */
d95101d6 9447 int crm, opc1, opc2;
4b6a83fb
PM
9448 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
9449 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
9450 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
9451 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
9452 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
9453 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
d95101d6
RH
9454 CPState state;
9455
4b6a83fb
PM
9456 /* 64 bit registers have only CRm and Opc1 fields */
9457 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
f5a0a5a5
PM
9458 /* op0 only exists in the AArch64 encodings */
9459 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
9460 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
9461 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
cd8be50e
PM
9462 /*
9463 * This API is only for Arm's system coprocessors (14 and 15) or
9464 * (M-profile or v7A-and-earlier only) for implementation defined
9465 * coprocessors in the range 0..7. Our decode assumes this, since
9466 * 8..13 can be used for other insns including VFP and Neon. See
9467 * valid_cp() in translate.c. Assert here that we haven't tried
9468 * to use an invalid coprocessor number.
9469 */
9470 switch (r->state) {
9471 case ARM_CP_STATE_BOTH:
9472 /* 0 has a special meaning, but otherwise the same rules as AA32. */
9473 if (r->cp == 0) {
9474 break;
9475 }
9476 /* fall through */
9477 case ARM_CP_STATE_AA32:
9478 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
9479 !arm_feature(&cpu->env, ARM_FEATURE_M)) {
9480 assert(r->cp >= 14 && r->cp <= 15);
9481 } else {
9482 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15));
9483 }
9484 break;
9485 case ARM_CP_STATE_AA64:
9486 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP);
9487 break;
9488 default:
9489 g_assert_not_reached();
9490 }
9b37a28c
FR
9491 /*
9492 * The AArch64 pseudocode CheckSystemAccess() specifies that op1
f5a0a5a5
PM
9493 * encodes a minimum access level for the register. We roll this
9494 * runtime check into our general permission check code, so check
9495 * here that the reginfo's specified permissions are strict enough
9496 * to encompass the generic architectural permission check.
9497 */
9498 if (r->state != ARM_CP_STATE_AA32) {
39107337 9499 CPAccessRights mask;
f5a0a5a5 9500 switch (r->opc1) {
b5bd7440
AB
9501 case 0:
9502 /* min_EL EL1, but some accessible to EL0 via kernel ABI */
9503 mask = PL0U_R | PL1_RW;
9504 break;
9505 case 1: case 2:
f5a0a5a5
PM
9506 /* min_EL EL1 */
9507 mask = PL1_RW;
9508 break;
9509 case 3:
9510 /* min_EL EL0 */
9511 mask = PL0_RW;
9512 break;
9513 case 4:
b4ecf60f 9514 case 5:
f5a0a5a5
PM
9515 /* min_EL EL2 */
9516 mask = PL2_RW;
9517 break;
f5a0a5a5
PM
9518 case 6:
9519 /* min_EL EL3 */
9520 mask = PL3_RW;
9521 break;
9522 case 7:
9523 /* min_EL EL1, secure mode only (we don't check the latter) */
9524 mask = PL1_RW;
9525 break;
9526 default:
9527 /* broken reginfo with out-of-range opc1 */
d385a605 9528 g_assert_not_reached();
f5a0a5a5
PM
9529 }
9530 /* assert our permissions are not too lax (stricter is fine) */
9531 assert((r->access & ~mask) == 0);
9532 }
9533
9b37a28c
FR
9534 /*
9535 * Check that the register definition has enough info to handle
4b6a83fb
PM
9536 * reads and writes if they are permitted.
9537 */
87c3f0f2 9538 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) {
4b6a83fb 9539 if (r->access & PL3_R) {
3f3c82a5
FA
9540 assert((r->fieldoffset ||
9541 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9542 r->readfn);
4b6a83fb
PM
9543 }
9544 if (r->access & PL3_W) {
3f3c82a5
FA
9545 assert((r->fieldoffset ||
9546 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
9547 r->writefn);
4b6a83fb
PM
9548 }
9549 }
5809ac57 9550
4b6a83fb
PM
9551 for (crm = crmmin; crm <= crmmax; crm++) {
9552 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
9553 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
f5a0a5a5
PM
9554 for (state = ARM_CP_STATE_AA32;
9555 state <= ARM_CP_STATE_AA64; state++) {
9556 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
9557 continue;
9558 }
3f3c82a5 9559 if (state == ARM_CP_STATE_AA32) {
9b37a28c
FR
9560 /*
9561 * Under AArch32 CP registers can be common
3f3c82a5
FA
9562 * (same for secure and non-secure world) or banked.
9563 */
9c513e78
AB
9564 char *name;
9565
3f3c82a5
FA
9566 switch (r->secure) {
9567 case ARM_CP_SECSTATE_S:
9568 case ARM_CP_SECSTATE_NS:
9569 add_cpreg_to_hashtable(cpu, r, opaque, state,
9c513e78
AB
9570 r->secure, crm, opc1, opc2,
9571 r->name);
3f3c82a5 9572 break;
cbe64585 9573 case ARM_CP_SECSTATE_BOTH:
9c513e78 9574 name = g_strdup_printf("%s_S", r->name);
3f3c82a5
FA
9575 add_cpreg_to_hashtable(cpu, r, opaque, state,
9576 ARM_CP_SECSTATE_S,
9c513e78
AB
9577 crm, opc1, opc2, name);
9578 g_free(name);
3f3c82a5
FA
9579 add_cpreg_to_hashtable(cpu, r, opaque, state,
9580 ARM_CP_SECSTATE_NS,
9c513e78 9581 crm, opc1, opc2, r->name);
3f3c82a5 9582 break;
cbe64585
RH
9583 default:
9584 g_assert_not_reached();
3f3c82a5
FA
9585 }
9586 } else {
9b37a28c
FR
9587 /*
9588 * AArch64 registers get mapped to non-secure instance
9589 * of AArch32
9590 */
3f3c82a5
FA
9591 add_cpreg_to_hashtable(cpu, r, opaque, state,
9592 ARM_CP_SECSTATE_NS,
9c513e78 9593 crm, opc1, opc2, r->name);
3f3c82a5 9594 }
f5a0a5a5 9595 }
4b6a83fb
PM
9596 }
9597 }
9598 }
9599}
9600
5809ac57
RH
9601/* Define a whole list of registers */
9602void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs,
9603 void *opaque, size_t len)
4b6a83fb 9604{
5809ac57
RH
9605 size_t i;
9606 for (i = 0; i < len; ++i) {
9607 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque);
4b6a83fb
PM
9608 }
9609}
9610
6c5c0fec
AB
9611/*
9612 * Modify ARMCPRegInfo for access from userspace.
9613 *
9614 * This is a data driven modification directed by
9615 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as
9616 * user-space cannot alter any values and dynamic values pertaining to
9617 * execution state are hidden from user space view anyway.
9618 */
5809ac57
RH
9619void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len,
9620 const ARMCPRegUserSpaceInfo *mods,
9621 size_t mods_len)
6c5c0fec 9622{
5809ac57
RH
9623 for (size_t mi = 0; mi < mods_len; ++mi) {
9624 const ARMCPRegUserSpaceInfo *m = mods + mi;
d040242e 9625 GPatternSpec *pat = NULL;
5809ac57 9626
d040242e
AB
9627 if (m->is_glob) {
9628 pat = g_pattern_spec_new(m->name);
9629 }
5809ac57
RH
9630 for (size_t ri = 0; ri < regs_len; ++ri) {
9631 ARMCPRegInfo *r = regs + ri;
9632
d040242e
AB
9633 if (pat && g_pattern_match_string(pat, r->name)) {
9634 r->type = ARM_CP_CONST;
9635 r->access = PL0U_R;
9636 r->resetvalue = 0;
9637 /* continue */
9638 } else if (strcmp(r->name, m->name) == 0) {
6c5c0fec
AB
9639 r->type = ARM_CP_CONST;
9640 r->access = PL0U_R;
9641 r->resetvalue &= m->exported_bits;
9642 r->resetvalue |= m->fixed_bits;
9643 break;
9644 }
9645 }
d040242e
AB
9646 if (pat) {
9647 g_pattern_spec_free(pat);
9648 }
6c5c0fec
AB
9649 }
9650}
9651
60322b39 9652const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
4b6a83fb 9653{
5860362d 9654 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp);
4b6a83fb
PM
9655}
9656
c4241c7d
PM
9657void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
9658 uint64_t value)
4b6a83fb
PM
9659{
9660 /* Helper coprocessor write function for write-ignore registers */
4b6a83fb
PM
9661}
9662
c4241c7d 9663uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
4b6a83fb
PM
9664{
9665 /* Helper coprocessor write function for read-as-zero registers */
4b6a83fb
PM
9666 return 0;
9667}
9668
f5a0a5a5
PM
9669void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
9670{
9671 /* Helper coprocessor reset function for do-nothing-on-reset registers */
9672}
9673
af393ffc 9674static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
37064a8b 9675{
9b37a28c
FR
9676 /*
9677 * Return true if it is not valid for us to switch to
37064a8b
PM
9678 * this CPU mode (ie all the UNPREDICTABLE cases in
9679 * the ARM ARM CPSRWriteByInstr pseudocode).
9680 */
af393ffc
PM
9681
9682 /* Changes to or from Hyp via MSR and CPS are illegal. */
9683 if (write_type == CPSRWriteByInstr &&
9684 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
9685 mode == ARM_CPU_MODE_HYP)) {
9686 return 1;
9687 }
9688
37064a8b
PM
9689 switch (mode) {
9690 case ARM_CPU_MODE_USR:
10eacda7 9691 return 0;
37064a8b
PM
9692 case ARM_CPU_MODE_SYS:
9693 case ARM_CPU_MODE_SVC:
9694 case ARM_CPU_MODE_ABT:
9695 case ARM_CPU_MODE_UND:
9696 case ARM_CPU_MODE_IRQ:
9697 case ARM_CPU_MODE_FIQ:
9b37a28c
FR
9698 /*
9699 * Note that we don't implement the IMPDEF NSACR.RFR which in v7
52ff951b
PM
9700 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
9701 */
9b37a28c
FR
9702 /*
9703 * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
10eacda7
PM
9704 * and CPS are treated as illegal mode changes.
9705 */
9706 if (write_type == CPSRWriteByInstr &&
10eacda7 9707 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
7c208e0f 9708 (arm_hcr_el2_eff(env) & HCR_TGE)) {
10eacda7
PM
9709 return 1;
9710 }
37064a8b 9711 return 0;
e6c8fc07 9712 case ARM_CPU_MODE_HYP:
e6ef0169 9713 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2;
027fc527 9714 case ARM_CPU_MODE_MON:
58ae2d1f 9715 return arm_current_el(env) < 3;
37064a8b
PM
9716 default:
9717 return 1;
9718 }
9719}
9720
2f4a40e5
AZ
9721uint32_t cpsr_read(CPUARMState *env)
9722{
9723 int ZF;
6fbe23d5
PB
9724 ZF = (env->ZF == 0);
9725 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
2f4a40e5
AZ
9726 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
9727 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
9728 | ((env->condexec_bits & 0xfc) << 8)
af519934 9729 | (env->GE << 16) | (env->daif & CPSR_AIF);
2f4a40e5
AZ
9730}
9731
50866ba5
PM
9732void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
9733 CPSRWriteType write_type)
2f4a40e5 9734{
6e8801f9 9735 uint32_t changed_daif;
e784807c
PM
9736 bool rebuild_hflags = (write_type != CPSRWriteRaw) &&
9737 (mask & (CPSR_M | CPSR_E | CPSR_IL));
6e8801f9 9738
2f4a40e5 9739 if (mask & CPSR_NZCV) {
6fbe23d5
PB
9740 env->ZF = (~val) & CPSR_Z;
9741 env->NF = val;
2f4a40e5
AZ
9742 env->CF = (val >> 29) & 1;
9743 env->VF = (val << 3) & 0x80000000;
9744 }
f927dbda 9745 if (mask & CPSR_Q) {
2f4a40e5 9746 env->QF = ((val & CPSR_Q) != 0);
f927dbda
FR
9747 }
9748 if (mask & CPSR_T) {
2f4a40e5 9749 env->thumb = ((val & CPSR_T) != 0);
f927dbda 9750 }
2f4a40e5
AZ
9751 if (mask & CPSR_IT_0_1) {
9752 env->condexec_bits &= ~3;
9753 env->condexec_bits |= (val >> 25) & 3;
9754 }
9755 if (mask & CPSR_IT_2_7) {
9756 env->condexec_bits &= 3;
9757 env->condexec_bits |= (val >> 8) & 0xfc;
9758 }
9759 if (mask & CPSR_GE) {
9760 env->GE = (val >> 16) & 0xf;
9761 }
9762
9b37a28c
FR
9763 /*
9764 * In a V7 implementation that includes the security extensions but does
6e8801f9
FA
9765 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
9766 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
9767 * bits respectively.
9768 *
9769 * In a V8 implementation, it is permitted for privileged software to
9770 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
9771 */
f8c88bbc 9772 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
6e8801f9
FA
9773 arm_feature(env, ARM_FEATURE_EL3) &&
9774 !arm_feature(env, ARM_FEATURE_EL2) &&
9775 !arm_is_secure(env)) {
9776
9777 changed_daif = (env->daif ^ val) & mask;
9778
9779 if (changed_daif & CPSR_A) {
9b37a28c
FR
9780 /*
9781 * Check to see if we are allowed to change the masking of async
6e8801f9
FA
9782 * abort exceptions from a non-secure state.
9783 */
9784 if (!(env->cp15.scr_el3 & SCR_AW)) {
9785 qemu_log_mask(LOG_GUEST_ERROR,
9786 "Ignoring attempt to switch CPSR_A flag from "
9787 "non-secure world with SCR.AW bit clear\n");
9788 mask &= ~CPSR_A;
9789 }
9790 }
9791
9792 if (changed_daif & CPSR_F) {
9b37a28c
FR
9793 /*
9794 * Check to see if we are allowed to change the masking of FIQ
6e8801f9
FA
9795 * exceptions from a non-secure state.
9796 */
9797 if (!(env->cp15.scr_el3 & SCR_FW)) {
9798 qemu_log_mask(LOG_GUEST_ERROR,
9799 "Ignoring attempt to switch CPSR_F flag from "
9800 "non-secure world with SCR.FW bit clear\n");
9801 mask &= ~CPSR_F;
9802 }
9803
9b37a28c
FR
9804 /*
9805 * Check whether non-maskable FIQ (NMFI) support is enabled.
6e8801f9
FA
9806 * If this bit is set software is not allowed to mask
9807 * FIQs, but is allowed to set CPSR_F to 0.
9808 */
9809 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
9810 (val & CPSR_F)) {
9811 qemu_log_mask(LOG_GUEST_ERROR,
9812 "Ignoring attempt to enable CPSR_F flag "
9813 "(non-maskable FIQ [NMFI] support enabled)\n");
9814 mask &= ~CPSR_F;
9815 }
9816 }
9817 }
9818
4cc35614
PM
9819 env->daif &= ~(CPSR_AIF & mask);
9820 env->daif |= val & CPSR_AIF & mask;
9821
f8c88bbc
PM
9822 if (write_type != CPSRWriteRaw &&
9823 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
8c4f0eb9 9824 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
9b37a28c
FR
9825 /*
9826 * Note that we can only get here in USR mode if this is a
8c4f0eb9
PM
9827 * gdb stub write; for this case we follow the architectural
9828 * behaviour for guest writes in USR mode of ignoring an attempt
9829 * to switch mode. (Those are caught by translate.c for writes
9830 * triggered by guest instructions.)
9831 */
9832 mask &= ~CPSR_M;
9833 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
9b37a28c
FR
9834 /*
9835 * Attempt to switch to an invalid mode: this is UNPREDICTABLE in
81907a58
PM
9836 * v7, and has defined behaviour in v8:
9837 * + leave CPSR.M untouched
9838 * + allow changes to the other CPSR fields
9839 * + set PSTATE.IL
9840 * For user changes via the GDB stub, we don't set PSTATE.IL,
9841 * as this would be unnecessarily harsh for a user error.
37064a8b
PM
9842 */
9843 mask &= ~CPSR_M;
81907a58
PM
9844 if (write_type != CPSRWriteByGDBStub &&
9845 arm_feature(env, ARM_FEATURE_V8)) {
9846 mask |= CPSR_IL;
9847 val |= CPSR_IL;
9848 }
81e37284
PM
9849 qemu_log_mask(LOG_GUEST_ERROR,
9850 "Illegal AArch32 mode switch attempt from %s to %s\n",
9851 aarch32_mode_name(env->uncached_cpsr),
9852 aarch32_mode_name(val));
37064a8b 9853 } else {
81e37284
PM
9854 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n",
9855 write_type == CPSRWriteExceptionReturn ?
9856 "Exception return from AArch32" :
9857 "AArch32 mode switch from",
9858 aarch32_mode_name(env->uncached_cpsr),
9859 aarch32_mode_name(val), env->regs[15]);
37064a8b
PM
9860 switch_mode(env, val & CPSR_M);
9861 }
2f4a40e5
AZ
9862 }
9863 mask &= ~CACHED_CPSR_BITS;
9864 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
2b77ad4d 9865 if (tcg_enabled() && rebuild_hflags) {
e784807c
PM
9866 arm_rebuild_hflags(env);
9867 }
2f4a40e5
AZ
9868}
9869
b26eefb6
PB
9870/* Sign/zero extend */
9871uint32_t HELPER(sxtb16)(uint32_t x)
9872{
9873 uint32_t res;
9874 res = (uint16_t)(int8_t)x;
9875 res |= (uint32_t)(int8_t)(x >> 16) << 16;
9876 return res;
9877}
9878
e5346292
PM
9879static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
9880{
9881 /*
9882 * Take a division-by-zero exception if necessary; otherwise return
9883 * to get the usual non-trapping division behaviour (result of 0)
9884 */
9885 if (arm_feature(env, ARM_FEATURE_M)
9886 && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
9887 raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
9888 }
9889}
9890
b26eefb6
PB
9891uint32_t HELPER(uxtb16)(uint32_t x)
9892{
9893 uint32_t res;
9894 res = (uint16_t)(uint8_t)x;
9895 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
9896 return res;
9897}
9898
e5346292 9899int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
3670669c 9900{
fc7a5038 9901 if (den == 0) {
e5346292 9902 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9903 return 0;
9904 }
9905 if (num == INT_MIN && den == -1) {
9906 return INT_MIN;
9907 }
3670669c
PB
9908 return num / den;
9909}
9910
e5346292 9911uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
3670669c 9912{
fc7a5038 9913 if (den == 0) {
e5346292 9914 handle_possible_div0_trap(env, GETPC());
fc7a5038
PM
9915 return 0;
9916 }
3670669c
PB
9917 return num / den;
9918}
9919
9920uint32_t HELPER(rbit)(uint32_t x)
9921{
42fedbca 9922 return revbit32(x);
3670669c
PB
9923}
9924
c47eaf9f 9925#ifdef CONFIG_USER_ONLY
b5ff1b31 9926
affdb64d 9927static void switch_mode(CPUARMState *env, int mode)
b5ff1b31 9928{
2fc0cc0e 9929 ARMCPU *cpu = env_archcpu(env);
a47dddd7
AF
9930
9931 if (mode != ARM_CPU_MODE_USR) {
9932 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
9933 }
b5ff1b31
FB
9934}
9935
012a906b
GB
9936uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
9937 uint32_t cur_el, bool secure)
9e729b57
EI
9938{
9939 return 1;
9940}
9941
ce02049d
GB
9942void aarch64_sync_64_to_32(CPUARMState *env)
9943{
9944 g_assert_not_reached();
9945}
9946
b5ff1b31
FB
9947#else
9948
affdb64d 9949static void switch_mode(CPUARMState *env, int mode)
b5ff1b31
FB
9950{
9951 int old_mode;
9952 int i;
9953
9954 old_mode = env->uncached_cpsr & CPSR_M;
f927dbda 9955 if (mode == old_mode) {
b5ff1b31 9956 return;
f927dbda 9957 }
b5ff1b31
FB
9958
9959 if (old_mode == ARM_CPU_MODE_FIQ) {
04215eb1
FR
9960 memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
9961 memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
b5ff1b31 9962 } else if (mode == ARM_CPU_MODE_FIQ) {
04215eb1
FR
9963 memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
9964 memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
b5ff1b31
FB
9965 }
9966
f5206413 9967 i = bank_number(old_mode);
b5ff1b31 9968 env->banked_r13[i] = env->regs[13];
b5ff1b31
FB
9969 env->banked_spsr[i] = env->spsr;
9970
f5206413 9971 i = bank_number(mode);
b5ff1b31 9972 env->regs[13] = env->banked_r13[i];
b5ff1b31 9973 env->spsr = env->banked_spsr[i];
593cfa2b
PM
9974
9975 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14];
9976 env->regs[14] = env->banked_r14[r14_bank_number(mode)];
b5ff1b31
FB
9977}
9978
9b37a28c
FR
9979/*
9980 * Physical Interrupt Target EL Lookup Table
0eeb17d6
GB
9981 *
9982 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
9983 *
9984 * The below multi-dimensional table is used for looking up the target
9985 * exception level given numerous condition criteria. Specifically, the
9986 * target EL is based on SCR and HCR routing controls as well as the
9987 * currently executing EL and secure state.
9988 *
9989 * Dimensions:
9990 * target_el_table[2][2][2][2][2][4]
9991 * | | | | | +--- Current EL
9992 * | | | | +------ Non-secure(0)/Secure(1)
9993 * | | | +--------- HCR mask override
9994 * | | +------------ SCR exec state control
9995 * | +--------------- SCR mask override
9996 * +------------------ 32-bit(0)/64-bit(1) EL3
9997 *
9998 * The table values are as such:
9999 * 0-3 = EL0-EL3
10000 * -1 = Cannot occur
10001 *
10002 * The ARM ARM target EL table includes entries indicating that an "exception
10003 * is not taken". The two cases where this is applicable are:
10004 * 1) An exception is taken from EL3 but the SCR does not have the exception
10005 * routed to EL3.
10006 * 2) An exception is taken from EL2 but the HCR does not have the exception
10007 * routed to EL2.
10008 * In these two cases, the below table contain a target of EL1. This value is
10009 * returned as it is expected that the consumer of the table data will check
10010 * for "target EL >= current EL" to ensure the exception is not taken.
10011 *
10012 * SCR HCR
10013 * 64 EA AMO From
10014 * BIT IRQ IMO Non-secure Secure
10015 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
10016 */
82c39f6a 10017static const int8_t target_el_table[2][2][2][2][2][4] = {
0eeb17d6
GB
10018 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
10019 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
10020 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
10021 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
10022 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
10023 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
10024 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
10025 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
10026 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
6c85f906
RDC
10027 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},},
10028 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },},
10029 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},},
0eeb17d6
GB
10030 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
10031 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
6c85f906
RDC
10032 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},
10033 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},},
0eeb17d6
GB
10034};
10035
10036/*
10037 * Determine the target EL for physical exceptions
10038 */
012a906b
GB
10039uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
10040 uint32_t cur_el, bool secure)
0eeb17d6
GB
10041{
10042 CPUARMState *env = cs->env_ptr;
f7778444
RH
10043 bool rw;
10044 bool scr;
10045 bool hcr;
0eeb17d6 10046 int target_el;
2cde031f 10047 /* Is the highest EL AArch64? */
f7778444
RH
10048 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64);
10049 uint64_t hcr_el2;
2cde031f
SS
10050
10051 if (arm_feature(env, ARM_FEATURE_EL3)) {
10052 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
10053 } else {
9b37a28c
FR
10054 /*
10055 * Either EL2 is the highest EL (and so the EL2 register width
2cde031f
SS
10056 * is given by is64); or there is no EL2 or EL3, in which case
10057 * the value of 'rw' does not affect the table lookup anyway.
10058 */
10059 rw = is64;
10060 }
0eeb17d6 10061
f7778444 10062 hcr_el2 = arm_hcr_el2_eff(env);
0eeb17d6
GB
10063 switch (excp_idx) {
10064 case EXCP_IRQ:
10065 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
f7778444 10066 hcr = hcr_el2 & HCR_IMO;
0eeb17d6
GB
10067 break;
10068 case EXCP_FIQ:
10069 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
f7778444 10070 hcr = hcr_el2 & HCR_FMO;
0eeb17d6
GB
10071 break;
10072 default:
10073 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
f7778444 10074 hcr = hcr_el2 & HCR_AMO;
0eeb17d6
GB
10075 break;
10076 };
10077
d1b31428
RH
10078 /*
10079 * For these purposes, TGE and AMO/IMO/FMO both force the
10080 * interrupt to EL2. Fold TGE into the bit extracted above.
10081 */
10082 hcr |= (hcr_el2 & HCR_TGE) != 0;
10083
0eeb17d6
GB
10084 /* Perform a table-lookup for the target EL given the current state */
10085 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
10086
10087 assert(target_el > 0);
10088
10089 return target_el;
10090}
10091
fc6177af 10092void arm_log_exception(CPUState *cs)
b59f479b 10093{
fc6177af
PM
10094 int idx = cs->exception_index;
10095
b59f479b
PMD
10096 if (qemu_loglevel_mask(CPU_LOG_INT)) {
10097 const char *exc = NULL;
10098 static const char * const excnames[] = {
10099 [EXCP_UDEF] = "Undefined Instruction",
10100 [EXCP_SWI] = "SVC",
10101 [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
10102 [EXCP_DATA_ABORT] = "Data Abort",
10103 [EXCP_IRQ] = "IRQ",
10104 [EXCP_FIQ] = "FIQ",
10105 [EXCP_BKPT] = "Breakpoint",
10106 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
10107 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
10108 [EXCP_HVC] = "Hypervisor Call",
10109 [EXCP_HYP_TRAP] = "Hypervisor Trap",
10110 [EXCP_SMC] = "Secure Monitor Call",
10111 [EXCP_VIRQ] = "Virtual IRQ",
10112 [EXCP_VFIQ] = "Virtual FIQ",
10113 [EXCP_SEMIHOST] = "Semihosting call",
10114 [EXCP_NOCP] = "v7M NOCP UsageFault",
10115 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault",
10116 [EXCP_STKOF] = "v8M STKOF UsageFault",
10117 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking",
10118 [EXCP_LSERR] = "v8M LSERR UsageFault",
10119 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault",
e5346292 10120 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault",
3c29632f 10121 [EXCP_VSERR] = "Virtual SERR",
b59f479b
PMD
10122 };
10123
10124 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
10125 exc = excnames[idx];
10126 }
10127 if (!exc) {
10128 exc = "unknown";
10129 }
fc6177af
PM
10130 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n",
10131 idx, exc, cs->cpu_index);
b59f479b
PMD
10132 }
10133}
10134
a356dacf 10135/*
7aab5a8c
PMD
10136 * Function used to synchronize QEMU's AArch64 register set with AArch32
10137 * register set. This is necessary when switching between AArch32 and AArch64
10138 * execution state.
a356dacf 10139 */
7aab5a8c 10140void aarch64_sync_32_to_64(CPUARMState *env)
9ee6e8bb 10141{
7aab5a8c
PMD
10142 int i;
10143 uint32_t mode = env->uncached_cpsr & CPSR_M;
10144
10145 /* We can blanket copy R[0:7] to X[0:7] */
10146 for (i = 0; i < 8; i++) {
10147 env->xregs[i] = env->regs[i];
fd592d89 10148 }
70d74660 10149
9a223097 10150 /*
7aab5a8c
PMD
10151 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
10152 * Otherwise, they come from the banked user regs.
fd592d89 10153 */
7aab5a8c
PMD
10154 if (mode == ARM_CPU_MODE_FIQ) {
10155 for (i = 8; i < 13; i++) {
10156 env->xregs[i] = env->usr_regs[i - 8];
10157 }
10158 } else {
10159 for (i = 8; i < 13; i++) {
10160 env->xregs[i] = env->regs[i];
10161 }
fd592d89 10162 }
9ee6e8bb 10163
7aab5a8c
PMD
10164 /*
10165 * Registers x13-x23 are the various mode SP and FP registers. Registers
10166 * r13 and r14 are only copied if we are in that mode, otherwise we copy
10167 * from the mode banked register.
10168 */
10169 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10170 env->xregs[13] = env->regs[13];
10171 env->xregs[14] = env->regs[14];
10172 } else {
10173 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
10174 /* HYP is an exception in that it is copied from r14 */
10175 if (mode == ARM_CPU_MODE_HYP) {
10176 env->xregs[14] = env->regs[14];
95695eff 10177 } else {
7aab5a8c 10178 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)];
95695eff 10179 }
95695eff
PM
10180 }
10181
7aab5a8c
PMD
10182 if (mode == ARM_CPU_MODE_HYP) {
10183 env->xregs[15] = env->regs[13];
10184 } else {
10185 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
95695eff
PM
10186 }
10187
7aab5a8c
PMD
10188 if (mode == ARM_CPU_MODE_IRQ) {
10189 env->xregs[16] = env->regs[14];
10190 env->xregs[17] = env->regs[13];
10191 } else {
10192 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)];
10193 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
10194 }
95695eff 10195
7aab5a8c
PMD
10196 if (mode == ARM_CPU_MODE_SVC) {
10197 env->xregs[18] = env->regs[14];
10198 env->xregs[19] = env->regs[13];
10199 } else {
10200 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)];
10201 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
10202 }
95695eff 10203
7aab5a8c
PMD
10204 if (mode == ARM_CPU_MODE_ABT) {
10205 env->xregs[20] = env->regs[14];
10206 env->xregs[21] = env->regs[13];
10207 } else {
10208 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)];
10209 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
10210 }
e33cf0f8 10211
7aab5a8c
PMD
10212 if (mode == ARM_CPU_MODE_UND) {
10213 env->xregs[22] = env->regs[14];
10214 env->xregs[23] = env->regs[13];
10215 } else {
10216 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)];
10217 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
e33cf0f8
PM
10218 }
10219
10220 /*
7aab5a8c
PMD
10221 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
10222 * mode, then we can copy from r8-r14. Otherwise, we copy from the
10223 * FIQ bank for r8-r14.
e33cf0f8 10224 */
7aab5a8c
PMD
10225 if (mode == ARM_CPU_MODE_FIQ) {
10226 for (i = 24; i < 31; i++) {
10227 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
10228 }
10229 } else {
10230 for (i = 24; i < 29; i++) {
10231 env->xregs[i] = env->fiq_regs[i - 24];
e33cf0f8 10232 }
7aab5a8c
PMD
10233 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
10234 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)];
e33cf0f8 10235 }
7aab5a8c
PMD
10236
10237 env->pc = env->regs[15];
e33cf0f8
PM
10238}
10239
9a223097 10240/*
7aab5a8c
PMD
10241 * Function used to synchronize QEMU's AArch32 register set with AArch64
10242 * register set. This is necessary when switching between AArch32 and AArch64
10243 * execution state.
de2db7ec 10244 */
7aab5a8c 10245void aarch64_sync_64_to_32(CPUARMState *env)
9ee6e8bb 10246{
7aab5a8c
PMD
10247 int i;
10248 uint32_t mode = env->uncached_cpsr & CPSR_M;
abc24d86 10249
7aab5a8c
PMD
10250 /* We can blanket copy X[0:7] to R[0:7] */
10251 for (i = 0; i < 8; i++) {
10252 env->regs[i] = env->xregs[i];
de2db7ec 10253 }
3f0cddee 10254
9a223097 10255 /*
7aab5a8c
PMD
10256 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
10257 * Otherwise, we copy x8-x12 into the banked user regs.
de2db7ec 10258 */
7aab5a8c
PMD
10259 if (mode == ARM_CPU_MODE_FIQ) {
10260 for (i = 8; i < 13; i++) {
10261 env->usr_regs[i - 8] = env->xregs[i];
10262 }
10263 } else {
10264 for (i = 8; i < 13; i++) {
10265 env->regs[i] = env->xregs[i];
10266 }
fb602cb7
PM
10267 }
10268
9a223097 10269 /*
7aab5a8c
PMD
10270 * Registers r13 & r14 depend on the current mode.
10271 * If we are in a given mode, we copy the corresponding x registers to r13
10272 * and r14. Otherwise, we copy the x register to the banked r13 and r14
10273 * for the mode.
fb602cb7 10274 */
7aab5a8c
PMD
10275 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
10276 env->regs[13] = env->xregs[13];
10277 env->regs[14] = env->xregs[14];
fb602cb7 10278 } else {
7aab5a8c 10279 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
fb602cb7 10280
7aab5a8c
PMD
10281 /*
10282 * HYP is an exception in that it does not have its own banked r14 but
10283 * shares the USR r14
10284 */
10285 if (mode == ARM_CPU_MODE_HYP) {
10286 env->regs[14] = env->xregs[14];
10287 } else {
10288 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
10289 }
10290 }
fb602cb7 10291
7aab5a8c
PMD
10292 if (mode == ARM_CPU_MODE_HYP) {
10293 env->regs[13] = env->xregs[15];
fb602cb7 10294 } else {
7aab5a8c 10295 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
fb602cb7 10296 }
d02a8698 10297
7aab5a8c
PMD
10298 if (mode == ARM_CPU_MODE_IRQ) {
10299 env->regs[14] = env->xregs[16];
10300 env->regs[13] = env->xregs[17];
d02a8698 10301 } else {
7aab5a8c
PMD
10302 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
10303 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
d02a8698
PM
10304 }
10305
7aab5a8c
PMD
10306 if (mode == ARM_CPU_MODE_SVC) {
10307 env->regs[14] = env->xregs[18];
10308 env->regs[13] = env->xregs[19];
10309 } else {
10310 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
10311 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
fb602cb7
PM
10312 }
10313
7aab5a8c
PMD
10314 if (mode == ARM_CPU_MODE_ABT) {
10315 env->regs[14] = env->xregs[20];
10316 env->regs[13] = env->xregs[21];
10317 } else {
10318 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
10319 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
ce02049d
GB
10320 }
10321
10322 if (mode == ARM_CPU_MODE_UND) {
3a9148d0
SS
10323 env->regs[14] = env->xregs[22];
10324 env->regs[13] = env->xregs[23];
ce02049d 10325 } else {
593cfa2b 10326 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
3a9148d0 10327 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
ce02049d
GB
10328 }
10329
9b37a28c
FR
10330 /*
10331 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
ce02049d
GB
10332 * mode, then we can copy to r8-r14. Otherwise, we copy to the
10333 * FIQ bank for r8-r14.
10334 */
10335 if (mode == ARM_CPU_MODE_FIQ) {
10336 for (i = 24; i < 31; i++) {
10337 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
10338 }
10339 } else {
10340 for (i = 24; i < 29; i++) {
10341 env->fiq_regs[i - 24] = env->xregs[i];
10342 }
10343 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
593cfa2b 10344 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
ce02049d
GB
10345 }
10346
10347 env->regs[15] = env->pc;
10348}
10349
dea8378b
PM
10350static void take_aarch32_exception(CPUARMState *env, int new_mode,
10351 uint32_t mask, uint32_t offset,
10352 uint32_t newpc)
10353{
4a2696c0
RH
10354 int new_el;
10355
dea8378b
PM
10356 /* Change the CPU state so as to actually take the exception. */
10357 switch_mode(env, new_mode);
4a2696c0 10358
dea8378b
PM
10359 /*
10360 * For exceptions taken to AArch32 we must clear the SS bit in both
10361 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
10362 */
f944a854 10363 env->pstate &= ~PSTATE_SS;
dea8378b
PM
10364 env->spsr = cpsr_read(env);
10365 /* Clear IT bits. */
10366 env->condexec_bits = 0;
10367 /* Switch to the new mode, and to the correct instruction set. */
10368 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
88828bf1
CD
10369
10370 /* This must be after mode switching. */
10371 new_el = arm_current_el(env);
10372
dea8378b
PM
10373 /* Set new mode endianness */
10374 env->uncached_cpsr &= ~CPSR_E;
4a2696c0 10375 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) {
dea8378b
PM
10376 env->uncached_cpsr |= CPSR_E;
10377 }
829f9fd3
PM
10378 /* J and IL must always be cleared for exception entry */
10379 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J);
dea8378b
PM
10380 env->daif |= mask;
10381
f2f68a78
RC
10382 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) {
10383 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) {
10384 env->uncached_cpsr |= CPSR_SSBS;
10385 } else {
10386 env->uncached_cpsr &= ~CPSR_SSBS;
10387 }
10388 }
10389
dea8378b
PM
10390 if (new_mode == ARM_CPU_MODE_HYP) {
10391 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0;
10392 env->elr_el[2] = env->regs[15];
10393 } else {
4a2696c0 10394 /* CPSR.PAN is normally preserved preserved unless... */
f8af1143 10395 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) {
4a2696c0
RH
10396 switch (new_el) {
10397 case 3:
10398 if (!arm_is_secure_below_el3(env)) {
10399 /* ... the target is EL3, from non-secure state. */
10400 env->uncached_cpsr &= ~CPSR_PAN;
10401 break;
10402 }
10403 /* ... the target is EL3, from secure state ... */
10404 /* fall through */
10405 case 1:
10406 /* ... the target is EL1 and SCTLR.SPAN is 0. */
10407 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) {
10408 env->uncached_cpsr |= CPSR_PAN;
10409 }
10410 break;
10411 }
10412 }
dea8378b
PM
10413 /*
10414 * this is a lie, as there was no c1_sys on V4T/V5, but who cares
10415 * and we should just guard the thumb mode on V4
10416 */
10417 if (arm_feature(env, ARM_FEATURE_V4T)) {
10418 env->thumb =
10419 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
10420 }
10421 env->regs[14] = env->regs[15] + offset;
10422 }
10423 env->regs[15] = newpc;
2b77ad4d
FR
10424
10425 if (tcg_enabled()) {
10426 arm_rebuild_hflags(env);
10427 }
dea8378b
PM
10428}
10429
b9bc21ff
PM
10430static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs)
10431{
10432 /*
10433 * Handle exception entry to Hyp mode; this is sufficiently
10434 * different to entry to other AArch32 modes that we handle it
10435 * separately here.
10436 *
10437 * The vector table entry used is always the 0x14 Hyp mode entry point,
2c023d36 10438 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp.
b9bc21ff
PM
10439 * The offset applied to the preferred return address is always zero
10440 * (see DDI0487C.a section G1.12.3).
10441 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values.
10442 */
10443 uint32_t addr, mask;
10444 ARMCPU *cpu = ARM_CPU(cs);
10445 CPUARMState *env = &cpu->env;
10446
10447 switch (cs->exception_index) {
10448 case EXCP_UDEF:
10449 addr = 0x04;
10450 break;
10451 case EXCP_SWI:
2c023d36 10452 addr = 0x08;
b9bc21ff
PM
10453 break;
10454 case EXCP_BKPT:
10455 /* Fall through to prefetch abort. */
10456 case EXCP_PREFETCH_ABORT:
10457 env->cp15.ifar_s = env->exception.vaddress;
10458 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n",
10459 (uint32_t)env->exception.vaddress);
10460 addr = 0x0c;
10461 break;
10462 case EXCP_DATA_ABORT:
10463 env->cp15.dfar_s = env->exception.vaddress;
10464 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n",
10465 (uint32_t)env->exception.vaddress);
10466 addr = 0x10;
10467 break;
10468 case EXCP_IRQ:
10469 addr = 0x18;
10470 break;
10471 case EXCP_FIQ:
10472 addr = 0x1c;
10473 break;
10474 case EXCP_HVC:
10475 addr = 0x08;
10476 break;
10477 case EXCP_HYP_TRAP:
10478 addr = 0x14;
9bbb4ef9 10479 break;
b9bc21ff
PM
10480 default:
10481 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10482 }
10483
10484 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) {
2ed08180
PM
10485 if (!arm_feature(env, ARM_FEATURE_V8)) {
10486 /*
10487 * QEMU syndrome values are v8-style. v7 has the IL bit
10488 * UNK/SBZP for "field not valid" cases, where v8 uses RES1.
10489 * If this is a v7 CPU, squash the IL bit in those cases.
10490 */
10491 if (cs->exception_index == EXCP_PREFETCH_ABORT ||
10492 (cs->exception_index == EXCP_DATA_ABORT &&
10493 !(env->exception.syndrome & ARM_EL_ISV)) ||
10494 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) {
10495 env->exception.syndrome &= ~ARM_EL_IL;
10496 }
10497 }
b9bc21ff
PM
10498 env->cp15.esr_el[2] = env->exception.syndrome;
10499 }
10500
10501 if (arm_current_el(env) != 2 && addr < 0x14) {
10502 addr = 0x14;
10503 }
10504
10505 mask = 0;
10506 if (!(env->cp15.scr_el3 & SCR_EA)) {
10507 mask |= CPSR_A;
10508 }
10509 if (!(env->cp15.scr_el3 & SCR_IRQ)) {
10510 mask |= CPSR_I;
10511 }
10512 if (!(env->cp15.scr_el3 & SCR_FIQ)) {
10513 mask |= CPSR_F;
10514 }
10515
10516 addr += env->cp15.hvbar;
10517
10518 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr);
10519}
10520
966f758c 10521static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
b5ff1b31 10522{
97a8ea5a
AF
10523 ARMCPU *cpu = ARM_CPU(cs);
10524 CPUARMState *env = &cpu->env;
b5ff1b31
FB
10525 uint32_t addr;
10526 uint32_t mask;
10527 int new_mode;
10528 uint32_t offset;
16a906fd 10529 uint32_t moe;
b5ff1b31 10530
16a906fd 10531 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
64b91e3f 10532 switch (syn_get_ec(env->exception.syndrome)) {
16a906fd
PM
10533 case EC_BREAKPOINT:
10534 case EC_BREAKPOINT_SAME_EL:
10535 moe = 1;
10536 break;
10537 case EC_WATCHPOINT:
10538 case EC_WATCHPOINT_SAME_EL:
10539 moe = 10;
10540 break;
10541 case EC_AA32_BKPT:
10542 moe = 3;
10543 break;
10544 case EC_VECTORCATCH:
10545 moe = 5;
10546 break;
10547 default:
10548 moe = 0;
10549 break;
10550 }
10551
10552 if (moe) {
10553 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
10554 }
10555
b9bc21ff
PM
10556 if (env->exception.target_el == 2) {
10557 arm_cpu_do_interrupt_aarch32_hyp(cs);
10558 return;
10559 }
10560
27103424 10561 switch (cs->exception_index) {
b5ff1b31
FB
10562 case EXCP_UDEF:
10563 new_mode = ARM_CPU_MODE_UND;
10564 addr = 0x04;
10565 mask = CPSR_I;
f927dbda 10566 if (env->thumb) {
b5ff1b31 10567 offset = 2;
f927dbda 10568 } else {
b5ff1b31 10569 offset = 4;
f927dbda 10570 }
b5ff1b31
FB
10571 break;
10572 case EXCP_SWI:
10573 new_mode = ARM_CPU_MODE_SVC;
10574 addr = 0x08;
10575 mask = CPSR_I;
601d70b9 10576 /* The PC already points to the next instruction. */
b5ff1b31
FB
10577 offset = 0;
10578 break;
06c949e6 10579 case EXCP_BKPT:
9ee6e8bb
PB
10580 /* Fall through to prefetch abort. */
10581 case EXCP_PREFETCH_ABORT:
88ca1c2d 10582 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
b848ce2b 10583 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
3f1beaca 10584 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
88ca1c2d 10585 env->exception.fsr, (uint32_t)env->exception.vaddress);
b5ff1b31
FB
10586 new_mode = ARM_CPU_MODE_ABT;
10587 addr = 0x0c;
10588 mask = CPSR_A | CPSR_I;
10589 offset = 4;
10590 break;
10591 case EXCP_DATA_ABORT:
4a7e2d73 10592 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
b848ce2b 10593 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
3f1beaca 10594 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
4a7e2d73 10595 env->exception.fsr,
6cd8a264 10596 (uint32_t)env->exception.vaddress);
b5ff1b31
FB
10597 new_mode = ARM_CPU_MODE_ABT;
10598 addr = 0x10;
10599 mask = CPSR_A | CPSR_I;
10600 offset = 8;
10601 break;
10602 case EXCP_IRQ:
10603 new_mode = ARM_CPU_MODE_IRQ;
10604 addr = 0x18;
10605 /* Disable IRQ and imprecise data aborts. */
10606 mask = CPSR_A | CPSR_I;
10607 offset = 4;
de38d23b
FA
10608 if (env->cp15.scr_el3 & SCR_IRQ) {
10609 /* IRQ routed to monitor mode */
10610 new_mode = ARM_CPU_MODE_MON;
10611 mask |= CPSR_F;
10612 }
b5ff1b31
FB
10613 break;
10614 case EXCP_FIQ:
10615 new_mode = ARM_CPU_MODE_FIQ;
10616 addr = 0x1c;
10617 /* Disable FIQ, IRQ and imprecise data aborts. */
10618 mask = CPSR_A | CPSR_I | CPSR_F;
de38d23b
FA
10619 if (env->cp15.scr_el3 & SCR_FIQ) {
10620 /* FIQ routed to monitor mode */
10621 new_mode = ARM_CPU_MODE_MON;
10622 }
b5ff1b31
FB
10623 offset = 4;
10624 break;
87a4b270
PM
10625 case EXCP_VIRQ:
10626 new_mode = ARM_CPU_MODE_IRQ;
10627 addr = 0x18;
10628 /* Disable IRQ and imprecise data aborts. */
10629 mask = CPSR_A | CPSR_I;
10630 offset = 4;
10631 break;
10632 case EXCP_VFIQ:
10633 new_mode = ARM_CPU_MODE_FIQ;
10634 addr = 0x1c;
10635 /* Disable FIQ, IRQ and imprecise data aborts. */
10636 mask = CPSR_A | CPSR_I | CPSR_F;
10637 offset = 4;
10638 break;
3c29632f
RH
10639 case EXCP_VSERR:
10640 {
10641 /*
10642 * Note that this is reported as a data abort, but the DFAR
10643 * has an UNKNOWN value. Construct the SError syndrome from
10644 * AET and ExT fields.
10645 */
10646 ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, };
10647
10648 if (extended_addresses_enabled(env)) {
10649 env->exception.fsr = arm_fi_to_lfsc(&fi);
10650 } else {
10651 env->exception.fsr = arm_fi_to_sfsc(&fi);
10652 }
10653 env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000;
10654 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
10655 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n",
10656 env->exception.fsr);
10657
10658 new_mode = ARM_CPU_MODE_ABT;
10659 addr = 0x10;
10660 mask = CPSR_A | CPSR_I;
10661 offset = 8;
10662 }
10663 break;
dbe9d163
FA
10664 case EXCP_SMC:
10665 new_mode = ARM_CPU_MODE_MON;
10666 addr = 0x08;
10667 mask = CPSR_A | CPSR_I | CPSR_F;
10668 offset = 0;
10669 break;
b5ff1b31 10670 default:
a47dddd7 10671 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
b5ff1b31
FB
10672 return; /* Never happens. Keep compiler happy. */
10673 }
e89e51a1
FA
10674
10675 if (new_mode == ARM_CPU_MODE_MON) {
10676 addr += env->cp15.mvbar;
137feaa9 10677 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
e89e51a1 10678 /* High vectors. When enabled, base address cannot be remapped. */
b5ff1b31 10679 addr += 0xffff0000;
8641136c 10680 } else {
9b37a28c
FR
10681 /*
10682 * ARM v7 architectures provide a vector base address register to remap
8641136c 10683 * the interrupt vector table.
e89e51a1 10684 * This register is only followed in non-monitor mode, and is banked.
8641136c
NR
10685 * Note: only bits 31:5 are valid.
10686 */
fb6c91ba 10687 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
b5ff1b31 10688 }
dbe9d163
FA
10689
10690 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
10691 env->cp15.scr_el3 &= ~SCR_NS;
10692 }
10693
dea8378b 10694 take_aarch32_exception(env, new_mode, mask, offset, addr);
b5ff1b31
FB
10695}
10696
a65dabf7
PM
10697static int aarch64_regnum(CPUARMState *env, int aarch32_reg)
10698{
10699 /*
10700 * Return the register number of the AArch64 view of the AArch32
10701 * register @aarch32_reg. The CPUARMState CPSR is assumed to still
10702 * be that of the AArch32 mode the exception came from.
10703 */
10704 int mode = env->uncached_cpsr & CPSR_M;
10705
10706 switch (aarch32_reg) {
10707 case 0 ... 7:
10708 return aarch32_reg;
10709 case 8 ... 12:
10710 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg;
10711 case 13:
10712 switch (mode) {
10713 case ARM_CPU_MODE_USR:
10714 case ARM_CPU_MODE_SYS:
10715 return 13;
10716 case ARM_CPU_MODE_HYP:
10717 return 15;
10718 case ARM_CPU_MODE_IRQ:
10719 return 17;
10720 case ARM_CPU_MODE_SVC:
10721 return 19;
10722 case ARM_CPU_MODE_ABT:
10723 return 21;
10724 case ARM_CPU_MODE_UND:
10725 return 23;
10726 case ARM_CPU_MODE_FIQ:
10727 return 29;
10728 default:
10729 g_assert_not_reached();
10730 }
10731 case 14:
10732 switch (mode) {
10733 case ARM_CPU_MODE_USR:
10734 case ARM_CPU_MODE_SYS:
10735 case ARM_CPU_MODE_HYP:
10736 return 14;
10737 case ARM_CPU_MODE_IRQ:
10738 return 16;
10739 case ARM_CPU_MODE_SVC:
10740 return 18;
10741 case ARM_CPU_MODE_ABT:
10742 return 20;
10743 case ARM_CPU_MODE_UND:
10744 return 22;
10745 case ARM_CPU_MODE_FIQ:
10746 return 30;
10747 default:
10748 g_assert_not_reached();
10749 }
10750 case 15:
10751 return 31;
10752 default:
10753 g_assert_not_reached();
10754 }
10755}
10756
f944a854
RC
10757static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env)
10758{
10759 uint32_t ret = cpsr_read(env);
10760
10761 /* Move DIT to the correct location for SPSR_ELx */
10762 if (ret & CPSR_DIT) {
10763 ret &= ~CPSR_DIT;
10764 ret |= PSTATE_DIT;
10765 }
10766 /* Merge PSTATE.SS into SPSR_ELx */
10767 ret |= env->pstate & PSTATE_SS;
10768
10769 return ret;
10770}
10771
7ac61020
PM
10772static bool syndrome_is_sync_extabt(uint32_t syndrome)
10773{
10774 /* Return true if this syndrome value is a synchronous external abort */
10775 switch (syn_get_ec(syndrome)) {
10776 case EC_INSNABORT:
10777 case EC_INSNABORT_SAME_EL:
10778 case EC_DATAABORT:
10779 case EC_DATAABORT_SAME_EL:
10780 /* Look at fault status code for all the synchronous ext abort cases */
10781 switch (syndrome & 0x3f) {
10782 case 0x10:
10783 case 0x13:
10784 case 0x14:
10785 case 0x15:
10786 case 0x16:
10787 case 0x17:
10788 return true;
10789 default:
10790 return false;
10791 }
10792 default:
10793 return false;
10794 }
10795}
10796
966f758c
PM
10797/* Handle exception entry to a target EL which is using AArch64 */
10798static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
f3a9b694
PM
10799{
10800 ARMCPU *cpu = ARM_CPU(cs);
10801 CPUARMState *env = &cpu->env;
10802 unsigned int new_el = env->exception.target_el;
10803 target_ulong addr = env->cp15.vbar_el[new_el];
10804 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
4a2696c0 10805 unsigned int old_mode;
0ab5953b 10806 unsigned int cur_el = arm_current_el(env);
a65dabf7 10807 int rt;
0ab5953b 10808
d55b2a2a
CF
10809 if (tcg_enabled()) {
10810 /*
10811 * Note that new_el can never be 0. If cur_el is 0, then
10812 * el0_a64 is is_a64(), else el0_a64 is ignored.
10813 */
10814 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env));
10815 }
f3a9b694 10816
0ab5953b 10817 if (cur_el < new_el) {
9b37a28c
FR
10818 /*
10819 * Entry vector offset depends on whether the implemented EL
3d6f7617
PM
10820 * immediately lower than the target level is using AArch32 or AArch64
10821 */
10822 bool is_aa64;
cb092fbb 10823 uint64_t hcr;
3d6f7617
PM
10824
10825 switch (new_el) {
10826 case 3:
10827 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
10828 break;
10829 case 2:
cb092fbb
RH
10830 hcr = arm_hcr_el2_eff(env);
10831 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
10832 is_aa64 = (hcr & HCR_RW) != 0;
10833 break;
10834 }
10835 /* fall through */
3d6f7617
PM
10836 case 1:
10837 is_aa64 = is_a64(env);
10838 break;
10839 default:
10840 g_assert_not_reached();
10841 }
10842
10843 if (is_aa64) {
f3a9b694
PM
10844 addr += 0x400;
10845 } else {
10846 addr += 0x600;
10847 }
10848 } else if (pstate_read(env) & PSTATE_SP) {
10849 addr += 0x200;
10850 }
10851
f3a9b694
PM
10852 switch (cs->exception_index) {
10853 case EXCP_PREFETCH_ABORT:
10854 case EXCP_DATA_ABORT:
7ac61020
PM
10855 /*
10856 * FEAT_DoubleFault allows synchronous external aborts taken to EL3
10857 * to be taken to the SError vector entrypoint.
10858 */
10859 if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) &&
10860 syndrome_is_sync_extabt(env->exception.syndrome)) {
10861 addr += 0x180;
10862 }
f3a9b694
PM
10863 env->cp15.far_el[new_el] = env->exception.vaddress;
10864 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
10865 env->cp15.far_el[new_el]);
10866 /* fall through */
10867 case EXCP_BKPT:
10868 case EXCP_UDEF:
10869 case EXCP_SWI:
10870 case EXCP_HVC:
10871 case EXCP_HYP_TRAP:
10872 case EXCP_SMC:
a65dabf7
PM
10873 switch (syn_get_ec(env->exception.syndrome)) {
10874 case EC_ADVSIMDFPACCESSTRAP:
4be42f40
PM
10875 /*
10876 * QEMU internal FP/SIMD syndromes from AArch32 include the
10877 * TA and coproc fields which are only exposed if the exception
10878 * is taken to AArch32 Hyp mode. Mask them out to get a valid
10879 * AArch64 format syndrome.
10880 */
10881 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20);
a65dabf7
PM
10882 break;
10883 case EC_CP14RTTRAP:
10884 case EC_CP15RTTRAP:
10885 case EC_CP14DTTRAP:
10886 /*
10887 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently
10888 * the raw register field from the insn; when taking this to
10889 * AArch64 we must convert it to the AArch64 view of the register
10890 * number. Notice that we read a 4-bit AArch32 register number and
10891 * write back a 5-bit AArch64 one.
10892 */
10893 rt = extract32(env->exception.syndrome, 5, 4);
10894 rt = aarch64_regnum(env, rt);
10895 env->exception.syndrome = deposit32(env->exception.syndrome,
10896 5, 5, rt);
10897 break;
10898 case EC_CP15RRTTRAP:
10899 case EC_CP14RRTTRAP:
10900 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */
10901 rt = extract32(env->exception.syndrome, 5, 4);
10902 rt = aarch64_regnum(env, rt);
10903 env->exception.syndrome = deposit32(env->exception.syndrome,
10904 5, 5, rt);
10905 rt = extract32(env->exception.syndrome, 10, 4);
10906 rt = aarch64_regnum(env, rt);
10907 env->exception.syndrome = deposit32(env->exception.syndrome,
10908 10, 5, rt);
10909 break;
4be42f40 10910 }
f3a9b694
PM
10911 env->cp15.esr_el[new_el] = env->exception.syndrome;
10912 break;
10913 case EXCP_IRQ:
10914 case EXCP_VIRQ:
10915 addr += 0x80;
10916 break;
10917 case EXCP_FIQ:
10918 case EXCP_VFIQ:
10919 addr += 0x100;
10920 break;
3c29632f
RH
10921 case EXCP_VSERR:
10922 addr += 0x180;
10923 /* Construct the SError syndrome from IDS and ISS fields. */
10924 env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff);
10925 env->cp15.esr_el[new_el] = env->exception.syndrome;
10926 break;
f3a9b694
PM
10927 default:
10928 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
10929 }
10930
10931 if (is_a64(env)) {
4a2696c0 10932 old_mode = pstate_read(env);
f3a9b694
PM
10933 aarch64_save_sp(env, arm_current_el(env));
10934 env->elr_el[new_el] = env->pc;
10935 } else {
f944a854 10936 old_mode = cpsr_read_for_spsr_elx(env);
f3a9b694
PM
10937 env->elr_el[new_el] = env->regs[15];
10938
10939 aarch64_sync_32_to_64(env);
10940
10941 env->condexec_bits = 0;
10942 }
4a2696c0
RH
10943 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode;
10944
f3a9b694
PM
10945 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
10946 env->elr_el[new_el]);
10947
4a2696c0
RH
10948 if (cpu_isar_feature(aa64_pan, cpu)) {
10949 /* The value of PSTATE.PAN is normally preserved, except when ... */
10950 new_mode |= old_mode & PSTATE_PAN;
10951 switch (new_el) {
10952 case 2:
10953 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */
10954 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE))
10955 != (HCR_E2H | HCR_TGE)) {
10956 break;
10957 }
10958 /* fall through */
10959 case 1:
10960 /* ... the target is EL1 ... */
10961 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */
10962 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) {
10963 new_mode |= PSTATE_PAN;
10964 }
10965 break;
10966 }
10967 }
34669338
RH
10968 if (cpu_isar_feature(aa64_mte, cpu)) {
10969 new_mode |= PSTATE_TCO;
10970 }
4a2696c0 10971
f2f68a78
RC
10972 if (cpu_isar_feature(aa64_ssbs, cpu)) {
10973 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) {
10974 new_mode |= PSTATE_SSBS;
10975 } else {
10976 new_mode &= ~PSTATE_SSBS;
10977 }
10978 }
10979
f3a9b694 10980 pstate_write(env, PSTATE_DAIF | new_mode);
53221552 10981 env->aarch64 = true;
f3a9b694 10982 aarch64_restore_sp(env, new_el);
2b77ad4d
FR
10983
10984 if (tcg_enabled()) {
10985 helper_rebuild_hflags_a64(env, new_el);
10986 }
f3a9b694
PM
10987
10988 env->pc = addr;
10989
10990 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
10991 new_el, env->pc, pstate_read(env));
966f758c
PM
10992}
10993
ed6e6ba9
AB
10994/*
10995 * Do semihosting call and set the appropriate return value. All the
10996 * permission and validity checks have been done at translate time.
10997 *
10998 * We only see semihosting exceptions in TCG only as they are not
10999 * trapped to the hypervisor in KVM.
11000 */
91f78c58 11001#ifdef CONFIG_TCG
a06e3a68 11002static void tcg_handle_semihosting(CPUState *cs)
ed6e6ba9 11003{
904c04de
PM
11004 ARMCPU *cpu = ARM_CPU(cs);
11005 CPUARMState *env = &cpu->env;
11006
11007 if (is_a64(env)) {
ed6e6ba9
AB
11008 qemu_log_mask(CPU_LOG_INT,
11009 "...handling as semihosting call 0x%" PRIx64 "\n",
11010 env->xregs[0]);
ed3a06b1 11011 do_common_semihosting(cs);
4ff5ef9e 11012 env->pc += 4;
904c04de 11013 } else {
904c04de
PM
11014 qemu_log_mask(CPU_LOG_INT,
11015 "...handling as semihosting call 0x%x\n",
11016 env->regs[0]);
ed3a06b1 11017 do_common_semihosting(cs);
4ff5ef9e 11018 env->regs[15] += env->thumb ? 2 : 4;
904c04de
PM
11019 }
11020}
ed6e6ba9 11021#endif
904c04de 11022
9b37a28c
FR
11023/*
11024 * Handle a CPU exception for A and R profile CPUs.
966f758c
PM
11025 * Do any appropriate logging, handle PSCI calls, and then hand off
11026 * to the AArch64-entry or AArch32-entry function depending on the
11027 * target exception level's register width.
853bfef4
CF
11028 *
11029 * Note: this is used for both TCG (as the do_interrupt tcg op),
11030 * and KVM to re-inject guest debug exceptions, and to
11031 * inject a Synchronous-External-Abort.
966f758c
PM
11032 */
11033void arm_cpu_do_interrupt(CPUState *cs)
11034{
11035 ARMCPU *cpu = ARM_CPU(cs);
11036 CPUARMState *env = &cpu->env;
11037 unsigned int new_el = env->exception.target_el;
11038
531c60a9 11039 assert(!arm_feature(env, ARM_FEATURE_M));
966f758c 11040
fc6177af 11041 arm_log_exception(cs);
966f758c
PM
11042 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
11043 new_el);
11044 if (qemu_loglevel_mask(CPU_LOG_INT)
11045 && !excp_is_internal(cs->exception_index)) {
6568da45 11046 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n",
64b91e3f 11047 syn_get_ec(env->exception.syndrome),
966f758c
PM
11048 env->exception.syndrome);
11049 }
11050
0c1aaa66 11051 if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) {
966f758c
PM
11052 arm_handle_psci_call(cpu);
11053 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
11054 return;
11055 }
11056
ed6e6ba9
AB
11057 /*
11058 * Semihosting semantics depend on the register width of the code
11059 * that caused the exception, not the target exception level, so
11060 * must be handled here.
966f758c 11061 */
ed6e6ba9
AB
11062#ifdef CONFIG_TCG
11063 if (cs->exception_index == EXCP_SEMIHOST) {
a06e3a68 11064 tcg_handle_semihosting(cs);
904c04de
PM
11065 return;
11066 }
ed6e6ba9 11067#endif
904c04de 11068
9b37a28c
FR
11069 /*
11070 * Hooks may change global state so BQL should be held, also the
b5c53d1b
AL
11071 * BQL needs to be held for any modification of
11072 * cs->interrupt_request.
11073 */
11074 g_assert(qemu_mutex_iothread_locked());
11075
11076 arm_call_pre_el_change_hook(cpu);
11077
904c04de
PM
11078 assert(!excp_is_internal(cs->exception_index));
11079 if (arm_el_is_aa64(env, new_el)) {
966f758c
PM
11080 arm_cpu_do_interrupt_aarch64(cs);
11081 } else {
11082 arm_cpu_do_interrupt_aarch32(cs);
11083 }
f3a9b694 11084
bd7d00fc
PM
11085 arm_call_el_change_hook(cpu);
11086
f3a9b694
PM
11087 if (!kvm_enabled()) {
11088 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
11089 }
11090}
c47eaf9f 11091#endif /* !CONFIG_USER_ONLY */
0480f69a 11092
aaec1432
RH
11093uint64_t arm_sctlr(CPUARMState *env, int el)
11094{
11095 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */
11096 if (el == 0) {
11097 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
d902ae75 11098 el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
aaec1432
RH
11099 }
11100 return env->cp15.sctlr_el[el];
11101}
c47eaf9f 11102
8ae08860 11103int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx)
b830a5ee
RH
11104{
11105 if (regime_has_2_ranges(mmu_idx)) {
11106 return extract64(tcr, 37, 2);
edc05dd4 11107 } else if (regime_is_stage2(mmu_idx)) {
b830a5ee
RH
11108 return 0; /* VTCR_EL2 */
11109 } else {
3e270f67
RH
11110 /* Replicate the single TBI bit so we always have 2 bits. */
11111 return extract32(tcr, 20, 1) * 3;
b830a5ee
RH
11112 }
11113}
11114
8ae08860 11115int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx)
b830a5ee
RH
11116{
11117 if (regime_has_2_ranges(mmu_idx)) {
11118 return extract64(tcr, 51, 2);
edc05dd4 11119 } else if (regime_is_stage2(mmu_idx)) {
b830a5ee
RH
11120 return 0; /* VTCR_EL2 */
11121 } else {
3e270f67
RH
11122 /* Replicate the single TBID bit so we always have 2 bits. */
11123 return extract32(tcr, 29, 1) * 3;
b830a5ee
RH
11124 }
11125}
11126
671efad1 11127int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx)
81ae05fa
RH
11128{
11129 if (regime_has_2_ranges(mmu_idx)) {
11130 return extract64(tcr, 57, 2);
11131 } else {
11132 /* Replicate the single TCMA bit so we always have 2 bits. */
11133 return extract32(tcr, 30, 1) * 3;
11134 }
11135}
11136
104f703d
PM
11137static ARMGranuleSize tg0_to_gran_size(int tg)
11138{
11139 switch (tg) {
11140 case 0:
11141 return Gran4K;
11142 case 1:
11143 return Gran64K;
11144 case 2:
11145 return Gran16K;
11146 default:
11147 return GranInvalid;
11148 }
11149}
11150
11151static ARMGranuleSize tg1_to_gran_size(int tg)
11152{
11153 switch (tg) {
11154 case 1:
11155 return Gran16K;
11156 case 2:
11157 return Gran4K;
11158 case 3:
11159 return Gran64K;
11160 default:
11161 return GranInvalid;
11162 }
11163}
11164
11165static inline bool have4k(ARMCPU *cpu, bool stage2)
11166{
11167 return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu)
11168 : cpu_isar_feature(aa64_tgran4, cpu);
11169}
11170
11171static inline bool have16k(ARMCPU *cpu, bool stage2)
11172{
11173 return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu)
11174 : cpu_isar_feature(aa64_tgran16, cpu);
11175}
11176
11177static inline bool have64k(ARMCPU *cpu, bool stage2)
11178{
11179 return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu)
11180 : cpu_isar_feature(aa64_tgran64, cpu);
11181}
11182
11183static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran,
11184 bool stage2)
11185{
11186 switch (gran) {
11187 case Gran4K:
11188 if (have4k(cpu, stage2)) {
11189 return gran;
11190 }
11191 break;
11192 case Gran16K:
11193 if (have16k(cpu, stage2)) {
11194 return gran;
11195 }
11196 break;
11197 case Gran64K:
11198 if (have64k(cpu, stage2)) {
11199 return gran;
11200 }
11201 break;
11202 case GranInvalid:
11203 break;
11204 }
11205 /*
11206 * If the guest selects a granule size that isn't implemented,
11207 * the architecture requires that we behave as if it selected one
11208 * that is (with an IMPDEF choice of which one to pick). We choose
11209 * to implement the smallest supported granule size.
11210 */
11211 if (have4k(cpu, stage2)) {
11212 return Gran4K;
11213 }
11214 if (have16k(cpu, stage2)) {
11215 return Gran16K;
11216 }
11217 assert(have64k(cpu, stage2));
11218 return Gran64K;
11219}
11220
b830a5ee
RH
11221ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va,
11222 ARMMMUIdx mmu_idx, bool data)
ba97be9f 11223{
c1547bba 11224 uint64_t tcr = regime_tcr(env, mmu_idx);
89739227 11225 bool epd, hpd, tsz_oob, ds, ha, hd;
ef56c242 11226 int select, tsz, tbi, max_tsz, min_tsz, ps, sh;
104f703d 11227 ARMGranuleSize gran;
ef56c242 11228 ARMCPU *cpu = env_archcpu(env);
edc05dd4 11229 bool stage2 = regime_is_stage2(mmu_idx);
ba97be9f 11230
339370b9 11231 if (!regime_has_2_ranges(mmu_idx)) {
71d18164 11232 select = 0;
ba97be9f 11233 tsz = extract32(tcr, 0, 6);
104f703d
PM
11234 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
11235 if (stage2) {
ba97be9f 11236 /* VTCR_EL2 */
b830a5ee 11237 hpd = false;
ba97be9f 11238 } else {
ba97be9f
RH
11239 hpd = extract32(tcr, 24, 1);
11240 }
11241 epd = false;
ef56c242 11242 sh = extract32(tcr, 12, 2);
f4ecc015 11243 ps = extract32(tcr, 16, 3);
89739227
RH
11244 ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu);
11245 hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu);
ef56c242 11246 ds = extract64(tcr, 32, 1);
ba97be9f 11247 } else {
e4c93e44
PM
11248 bool e0pd;
11249
71d18164
RH
11250 /*
11251 * Bit 55 is always between the two regions, and is canonical for
11252 * determining if address tagging is enabled.
11253 */
11254 select = extract64(va, 55, 1);
11255 if (!select) {
11256 tsz = extract32(tcr, 0, 6);
104f703d 11257 gran = tg0_to_gran_size(extract32(tcr, 14, 2));
71d18164 11258 epd = extract32(tcr, 7, 1);
ef56c242 11259 sh = extract32(tcr, 12, 2);
71d18164 11260 hpd = extract64(tcr, 41, 1);
e4c93e44 11261 e0pd = extract64(tcr, 55, 1);
71d18164 11262 } else {
71d18164 11263 tsz = extract32(tcr, 16, 6);
104f703d 11264 gran = tg1_to_gran_size(extract32(tcr, 30, 2));
71d18164 11265 epd = extract32(tcr, 23, 1);
ef56c242 11266 sh = extract32(tcr, 28, 2);
71d18164 11267 hpd = extract64(tcr, 42, 1);
e4c93e44 11268 e0pd = extract64(tcr, 56, 1);
71d18164 11269 }
f4ecc015 11270 ps = extract64(tcr, 32, 3);
89739227
RH
11271 ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu);
11272 hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu);
ef56c242 11273 ds = extract64(tcr, 59, 1);
e4c93e44
PM
11274
11275 if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) &&
11276 regime_is_user(env, mmu_idx)) {
11277 epd = true;
11278 }
ba97be9f 11279 }
c36c65ea 11280
104f703d 11281 gran = sanitize_gran_size(cpu, gran, stage2);
104f703d 11282
ef56c242 11283 if (cpu_isar_feature(aa64_st, cpu)) {
3c003f70 11284 max_tsz = 48 - (gran == Gran64K);
c36c65ea
RDC
11285 } else {
11286 max_tsz = 39;
11287 }
0af312b6 11288
ef56c242
RH
11289 /*
11290 * DS is RES0 unless FEAT_LPA2 is supported for the given page size;
11291 * adjust the effective value of DS, as documented.
11292 */
0af312b6 11293 min_tsz = 16;
3c003f70 11294 if (gran == Gran64K) {
ef56c242
RH
11295 if (cpu_isar_feature(aa64_lva, cpu)) {
11296 min_tsz = 12;
11297 }
11298 ds = false;
11299 } else if (ds) {
edc05dd4 11300 if (regime_is_stage2(mmu_idx)) {
3c003f70 11301 if (gran == Gran16K) {
ef56c242
RH
11302 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu);
11303 } else {
11304 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu);
11305 }
edc05dd4 11306 } else {
3c003f70 11307 if (gran == Gran16K) {
ef56c242
RH
11308 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu);
11309 } else {
11310 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu);
11311 }
ef56c242
RH
11312 }
11313 if (ds) {
0af312b6
RH
11314 min_tsz = 12;
11315 }
11316 }
c36c65ea 11317
ebf93ce7
RH
11318 if (tsz > max_tsz) {
11319 tsz = max_tsz;
11320 tsz_oob = true;
11321 } else if (tsz < min_tsz) {
11322 tsz = min_tsz;
11323 tsz_oob = true;
11324 } else {
11325 tsz_oob = false;
11326 }
ba97be9f 11327
b830a5ee
RH
11328 /* Present TBI as a composite with TBID. */
11329 tbi = aa64_va_parameter_tbi(tcr, mmu_idx);
11330 if (!data) {
11331 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx);
11332 }
11333 tbi = (tbi >> select) & 1;
11334
ba97be9f
RH
11335 return (ARMVAParameters) {
11336 .tsz = tsz,
f4ecc015 11337 .ps = ps,
ef56c242 11338 .sh = sh,
ba97be9f
RH
11339 .select = select,
11340 .tbi = tbi,
11341 .epd = epd,
11342 .hpd = hpd,
ebf93ce7 11343 .tsz_oob = tsz_oob,
ef56c242 11344 .ds = ds,
89739227
RH
11345 .ha = ha,
11346 .hd = ha && hd,
3c003f70 11347 .gran = gran,
ba97be9f
RH
11348 };
11349}
11350
9b37a28c
FR
11351/*
11352 * Note that signed overflow is undefined in C. The following routines are
11353 * careful to use unsigned types where modulo arithmetic is required.
11354 * Failure to do so _will_ break on newer gcc.
11355 */
6ddbc6e4
PB
11356
11357/* Signed saturating arithmetic. */
11358
1654b2d6 11359/* Perform 16-bit signed saturating addition. */
6ddbc6e4
PB
11360static inline uint16_t add16_sat(uint16_t a, uint16_t b)
11361{
11362 uint16_t res;
11363
11364 res = a + b;
11365 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
f927dbda 11366 if (a & 0x8000) {
6ddbc6e4 11367 res = 0x8000;
f927dbda 11368 } else {
6ddbc6e4 11369 res = 0x7fff;
f927dbda 11370 }
6ddbc6e4
PB
11371 }
11372 return res;
11373}
11374
1654b2d6 11375/* Perform 8-bit signed saturating addition. */
6ddbc6e4
PB
11376static inline uint8_t add8_sat(uint8_t a, uint8_t b)
11377{
11378 uint8_t res;
11379
11380 res = a + b;
11381 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
f927dbda 11382 if (a & 0x80) {
6ddbc6e4 11383 res = 0x80;
f927dbda 11384 } else {
6ddbc6e4 11385 res = 0x7f;
f927dbda 11386 }
6ddbc6e4
PB
11387 }
11388 return res;
11389}
11390
1654b2d6 11391/* Perform 16-bit signed saturating subtraction. */
6ddbc6e4
PB
11392static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
11393{
11394 uint16_t res;
11395
11396 res = a - b;
11397 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
f927dbda 11398 if (a & 0x8000) {
6ddbc6e4 11399 res = 0x8000;
f927dbda 11400 } else {
6ddbc6e4 11401 res = 0x7fff;
f927dbda 11402 }
6ddbc6e4
PB
11403 }
11404 return res;
11405}
11406
1654b2d6 11407/* Perform 8-bit signed saturating subtraction. */
6ddbc6e4
PB
11408static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
11409{
11410 uint8_t res;
11411
11412 res = a - b;
11413 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
f927dbda 11414 if (a & 0x80) {
6ddbc6e4 11415 res = 0x80;
f927dbda 11416 } else {
6ddbc6e4 11417 res = 0x7f;
f927dbda 11418 }
6ddbc6e4
PB
11419 }
11420 return res;
11421}
11422
11423#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
11424#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
11425#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
11426#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
11427#define PFX q
11428
11429#include "op_addsub.h"
11430
11431/* Unsigned saturating arithmetic. */
460a09c1 11432static inline uint16_t add16_usat(uint16_t a, uint16_t b)
6ddbc6e4
PB
11433{
11434 uint16_t res;
11435 res = a + b;
f927dbda 11436 if (res < a) {
6ddbc6e4 11437 res = 0xffff;
f927dbda 11438 }
6ddbc6e4
PB
11439 return res;
11440}
11441
460a09c1 11442static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
6ddbc6e4 11443{
f927dbda 11444 if (a > b) {
6ddbc6e4 11445 return a - b;
f927dbda 11446 } else {
6ddbc6e4 11447 return 0;
f927dbda 11448 }
6ddbc6e4
PB
11449}
11450
11451static inline uint8_t add8_usat(uint8_t a, uint8_t b)
11452{
11453 uint8_t res;
11454 res = a + b;
f927dbda 11455 if (res < a) {
6ddbc6e4 11456 res = 0xff;
f927dbda 11457 }
6ddbc6e4
PB
11458 return res;
11459}
11460
11461static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
11462{
f927dbda 11463 if (a > b) {
6ddbc6e4 11464 return a - b;
f927dbda 11465 } else {
6ddbc6e4 11466 return 0;
f927dbda 11467 }
6ddbc6e4
PB
11468}
11469
11470#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
11471#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
11472#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
11473#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
11474#define PFX uq
11475
11476#include "op_addsub.h"
11477
11478/* Signed modulo arithmetic. */
11479#define SARITH16(a, b, n, op) do { \
11480 int32_t sum; \
db6e2e65 11481 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
6ddbc6e4
PB
11482 RESULT(sum, n, 16); \
11483 if (sum >= 0) \
11484 ge |= 3 << (n * 2); \
04215eb1 11485 } while (0)
6ddbc6e4
PB
11486
11487#define SARITH8(a, b, n, op) do { \
11488 int32_t sum; \
db6e2e65 11489 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
6ddbc6e4
PB
11490 RESULT(sum, n, 8); \
11491 if (sum >= 0) \
11492 ge |= 1 << n; \
04215eb1 11493 } while (0)
6ddbc6e4
PB
11494
11495
11496#define ADD16(a, b, n) SARITH16(a, b, n, +)
11497#define SUB16(a, b, n) SARITH16(a, b, n, -)
11498#define ADD8(a, b, n) SARITH8(a, b, n, +)
11499#define SUB8(a, b, n) SARITH8(a, b, n, -)
11500#define PFX s
11501#define ARITH_GE
11502
11503#include "op_addsub.h"
11504
11505/* Unsigned modulo arithmetic. */
11506#define ADD16(a, b, n) do { \
11507 uint32_t sum; \
11508 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
11509 RESULT(sum, n, 16); \
a87aa10b 11510 if ((sum >> 16) == 1) \
6ddbc6e4 11511 ge |= 3 << (n * 2); \
04215eb1 11512 } while (0)
6ddbc6e4
PB
11513
11514#define ADD8(a, b, n) do { \
11515 uint32_t sum; \
11516 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
11517 RESULT(sum, n, 8); \
a87aa10b
AZ
11518 if ((sum >> 8) == 1) \
11519 ge |= 1 << n; \
04215eb1 11520 } while (0)
6ddbc6e4
PB
11521
11522#define SUB16(a, b, n) do { \
11523 uint32_t sum; \
11524 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
11525 RESULT(sum, n, 16); \
11526 if ((sum >> 16) == 0) \
11527 ge |= 3 << (n * 2); \
04215eb1 11528 } while (0)
6ddbc6e4
PB
11529
11530#define SUB8(a, b, n) do { \
11531 uint32_t sum; \
11532 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
11533 RESULT(sum, n, 8); \
11534 if ((sum >> 8) == 0) \
a87aa10b 11535 ge |= 1 << n; \
04215eb1 11536 } while (0)
6ddbc6e4
PB
11537
11538#define PFX u
11539#define ARITH_GE
11540
11541#include "op_addsub.h"
11542
11543/* Halved signed arithmetic. */
11544#define ADD16(a, b, n) \
11545 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
11546#define SUB16(a, b, n) \
11547 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
11548#define ADD8(a, b, n) \
11549 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
11550#define SUB8(a, b, n) \
11551 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
11552#define PFX sh
11553
11554#include "op_addsub.h"
11555
11556/* Halved unsigned arithmetic. */
11557#define ADD16(a, b, n) \
11558 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11559#define SUB16(a, b, n) \
11560 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
11561#define ADD8(a, b, n) \
11562 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11563#define SUB8(a, b, n) \
11564 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
11565#define PFX uh
11566
11567#include "op_addsub.h"
11568
11569static inline uint8_t do_usad(uint8_t a, uint8_t b)
11570{
f927dbda 11571 if (a > b) {
6ddbc6e4 11572 return a - b;
f927dbda 11573 } else {
6ddbc6e4 11574 return b - a;
f927dbda 11575 }
6ddbc6e4
PB
11576}
11577
11578/* Unsigned sum of absolute byte differences. */
11579uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
11580{
11581 uint32_t sum;
11582 sum = do_usad(a, b);
11583 sum += do_usad(a >> 8, b >> 8);
bdc3b6f5 11584 sum += do_usad(a >> 16, b >> 16);
6ddbc6e4
PB
11585 sum += do_usad(a >> 24, b >> 24);
11586 return sum;
11587}
11588
11589/* For ARMv6 SEL instruction. */
11590uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
11591{
11592 uint32_t mask;
11593
11594 mask = 0;
f927dbda 11595 if (flags & 1) {
6ddbc6e4 11596 mask |= 0xff;
f927dbda
FR
11597 }
11598 if (flags & 2) {
6ddbc6e4 11599 mask |= 0xff00;
f927dbda
FR
11600 }
11601 if (flags & 4) {
6ddbc6e4 11602 mask |= 0xff0000;
f927dbda
FR
11603 }
11604 if (flags & 8) {
6ddbc6e4 11605 mask |= 0xff000000;
f927dbda 11606 }
6ddbc6e4
PB
11607 return (a & mask) | (b & ~mask);
11608}
11609
9b37a28c
FR
11610/*
11611 * CRC helpers.
aa633469
PM
11612 * The upper bytes of val (above the number specified by 'bytes') must have
11613 * been zeroed out by the caller.
11614 */
eb0ecd5a
WN
11615uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
11616{
11617 uint8_t buf[4];
11618
aa633469 11619 stl_le_p(buf, val);
eb0ecd5a
WN
11620
11621 /* zlib crc32 converts the accumulator and output to one's complement. */
11622 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
11623}
11624
11625uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
11626{
11627 uint8_t buf[4];
11628
aa633469 11629 stl_le_p(buf, val);
eb0ecd5a
WN
11630
11631 /* Linux crc32c converts the output to one's complement. */
11632 return crc32c(acc, buf, bytes) ^ 0xffffffff;
11633}
a9e01311 11634
9b37a28c
FR
11635/*
11636 * Return the exception level to which FP-disabled exceptions should
a9e01311
RH
11637 * be taken, or 0 if FP is enabled.
11638 */
ced31551 11639int fp_exception_el(CPUARMState *env, int cur_el)
a9e01311 11640{
55faa212 11641#ifndef CONFIG_USER_ONLY
d5a6fa2d
RH
11642 uint64_t hcr_el2;
11643
9b37a28c
FR
11644 /*
11645 * CPACR and the CPTR registers don't exist before v6, so FP is
a9e01311
RH
11646 * always accessible
11647 */
11648 if (!arm_feature(env, ARM_FEATURE_V6)) {
11649 return 0;
11650 }
11651
d87513c0
PM
11652 if (arm_feature(env, ARM_FEATURE_M)) {
11653 /* CPACR can cause a NOCP UsageFault taken to current security state */
11654 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) {
11655 return 1;
11656 }
11657
11658 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) {
11659 if (!extract32(env->v7m.nsacr, 10, 1)) {
11660 /* FP insns cause a NOCP UsageFault taken to Secure */
11661 return 3;
11662 }
11663 }
11664
11665 return 0;
11666 }
11667
d5a6fa2d
RH
11668 hcr_el2 = arm_hcr_el2_eff(env);
11669
9b37a28c
FR
11670 /*
11671 * The CPACR controls traps to EL1, or PL1 if we're 32 bit:
a9e01311
RH
11672 * 0, 2 : trap EL0 and EL1/PL1 accesses
11673 * 1 : trap only EL0 accesses
11674 * 3 : trap no accesses
c2ddb7cf 11675 * This register is ignored if E2H+TGE are both set.
a9e01311 11676 */
d5a6fa2d 11677 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
fab8ad39 11678 int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN);
c2ddb7cf
RH
11679
11680 switch (fpen) {
02e1de14
RH
11681 case 1:
11682 if (cur_el != 0) {
11683 break;
11684 }
11685 /* fall through */
c2ddb7cf
RH
11686 case 0:
11687 case 2:
02e1de14
RH
11688 /* Trap from Secure PL0 or PL1 to Secure PL1. */
11689 if (!arm_el_is_aa64(env, 3)
11690 && (cur_el == 3 || arm_is_secure_below_el3(env))) {
a9e01311
RH
11691 return 3;
11692 }
02e1de14 11693 if (cur_el <= 1) {
c2ddb7cf
RH
11694 return 1;
11695 }
11696 break;
a9e01311 11697 }
a9e01311
RH
11698 }
11699
fc1120a7
PM
11700 /*
11701 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode
11702 * to control non-secure access to the FPU. It doesn't have any
11703 * effect if EL3 is AArch64 or if EL3 doesn't exist at all.
11704 */
11705 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) &&
11706 cur_el <= 2 && !arm_is_secure_below_el3(env))) {
11707 if (!extract32(env->cp15.nsacr, 10, 1)) {
11708 /* FP insns act as UNDEF */
11709 return cur_el == 2 ? 2 : 1;
11710 }
11711 }
11712
d5a6fa2d
RH
11713 /*
11714 * CPTR_EL2 is present in v7VE or v8, and changes format
11715 * with HCR_EL2.E2H (regardless of TGE).
a9e01311 11716 */
d5a6fa2d
RH
11717 if (cur_el <= 2) {
11718 if (hcr_el2 & HCR_E2H) {
fab8ad39 11719 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) {
d5a6fa2d
RH
11720 case 1:
11721 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) {
11722 break;
11723 }
11724 /* fall through */
11725 case 0:
11726 case 2:
11727 return 2;
11728 }
11729 } else if (arm_is_el2_enabled(env)) {
fab8ad39 11730 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) {
d5a6fa2d
RH
11731 return 2;
11732 }
11733 }
a9e01311
RH
11734 }
11735
11736 /* CPTR_EL3 : present in v8 */
fab8ad39 11737 if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) {
a9e01311
RH
11738 /* Trap all FP ops to EL3 */
11739 return 3;
11740 }
55faa212 11741#endif
a9e01311
RH
11742 return 0;
11743}
11744
b9f6033c
RH
11745/* Return the exception level we're running at if this is our mmu_idx */
11746int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx)
11747{
11748 if (mmu_idx & ARM_MMU_IDX_M) {
11749 return mmu_idx & ARM_MMU_IDX_M_PRIV;
11750 }
11751
11752 switch (mmu_idx) {
11753 case ARMMMUIdx_E10_0:
11754 case ARMMMUIdx_E20_0:
b9f6033c
RH
11755 return 0;
11756 case ARMMMUIdx_E10_1:
452ef8cb 11757 case ARMMMUIdx_E10_1_PAN:
b9f6033c
RH
11758 return 1;
11759 case ARMMMUIdx_E2:
11760 case ARMMMUIdx_E20_2:
452ef8cb 11761 case ARMMMUIdx_E20_2_PAN:
b9f6033c 11762 return 2;
d902ae75 11763 case ARMMMUIdx_E3:
b9f6033c
RH
11764 return 3;
11765 default:
11766 g_assert_not_reached();
11767 }
11768}
11769
7aab5a8c 11770#ifndef CONFIG_TCG
65e4655c
RH
11771ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate)
11772{
7aab5a8c 11773 g_assert_not_reached();
65e4655c 11774}
7aab5a8c 11775#endif
65e4655c 11776
6f2d9d74
TK
11777static bool arm_pan_enabled(CPUARMState *env)
11778{
11779 if (is_a64(env)) {
11780 return env->pstate & PSTATE_PAN;
11781 } else {
11782 return env->uncached_cpsr & CPSR_PAN;
11783 }
11784}
11785
164690b2 11786ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el)
65e4655c 11787{
b6ad6062
RDC
11788 ARMMMUIdx idx;
11789 uint64_t hcr;
11790
65e4655c 11791 if (arm_feature(env, ARM_FEATURE_M)) {
50494a27 11792 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure);
65e4655c
RH
11793 }
11794
6003d980 11795 /* See ARM pseudo-function ELIsInHost. */
b9f6033c
RH
11796 switch (el) {
11797 case 0:
b6ad6062
RDC
11798 hcr = arm_hcr_el2_eff(env);
11799 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) {
11800 idx = ARMMMUIdx_E20_0;
11801 } else {
11802 idx = ARMMMUIdx_E10_0;
6003d980 11803 }
b6ad6062 11804 break;
b9f6033c 11805 case 1:
6f2d9d74 11806 if (arm_pan_enabled(env)) {
b6ad6062
RDC
11807 idx = ARMMMUIdx_E10_1_PAN;
11808 } else {
11809 idx = ARMMMUIdx_E10_1;
66412260 11810 }
b6ad6062 11811 break;
b9f6033c 11812 case 2:
6003d980 11813 /* Note that TGE does not apply at EL2. */
b6ad6062 11814 if (arm_hcr_el2_eff(env) & HCR_E2H) {
6f2d9d74 11815 if (arm_pan_enabled(env)) {
b6ad6062
RDC
11816 idx = ARMMMUIdx_E20_2_PAN;
11817 } else {
11818 idx = ARMMMUIdx_E20_2;
66412260 11819 }
b6ad6062
RDC
11820 } else {
11821 idx = ARMMMUIdx_E2;
6003d980 11822 }
b6ad6062 11823 break;
b9f6033c 11824 case 3:
d902ae75 11825 return ARMMMUIdx_E3;
b9f6033c
RH
11826 default:
11827 g_assert_not_reached();
65e4655c 11828 }
b6ad6062 11829
b6ad6062 11830 return idx;
50494a27
RH
11831}
11832
164690b2
RH
11833ARMMMUIdx arm_mmu_idx(CPUARMState *env)
11834{
11835 return arm_mmu_idx_el(env, arm_current_el(env));
11836}
11837
26702213
PM
11838static bool mve_no_pred(CPUARMState *env)
11839{
11840 /*
11841 * Return true if there is definitely no predication of MVE
11842 * instructions by VPR or LTPSIZE. (Returning false even if there
11843 * isn't any predication is OK; generated code will just be
11844 * a little worse.)
11845 * If the CPU does not implement MVE then this TB flag is always 0.
11846 *
11847 * NOTE: if you change this logic, the "recalculate s->mve_no_pred"
11848 * logic in gen_update_fp_context() needs to be updated to match.
11849 *
11850 * We do not include the effect of the ECI bits here -- they are
11851 * tracked in other TB flags. This simplifies the logic for
11852 * "when did we emit code that changes the MVE_NO_PRED TB flag
11853 * and thus need to end the TB?".
11854 */
11855 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) {
11856 return false;
11857 }
11858 if (env->v7m.vpr) {
11859 return false;
11860 }
11861 if (env->v7m.ltpsize < 4) {
11862 return false;
11863 }
11864 return true;
11865}
11866
d4d7503a
RH
11867void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc,
11868 target_ulong *cs_base, uint32_t *pflags)
11869{
3902bfc6 11870 CPUARMTBFlags flags;
d4d7503a 11871
0ee8b24a 11872 assert_hflags_rebuild_correctly(env);
3902bfc6 11873 flags = env->hflags;
3d74e2e9 11874
a729a46b 11875 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) {
d4d7503a 11876 *pc = env->pc;
d4d7503a 11877 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) {
a729a46b 11878 DP_TBFLAG_A64(flags, BTYPE, env->btype);
08f1434a 11879 }
a9e01311
RH
11880 } else {
11881 *pc = env->regs[15];
6e33ced5
RH
11882
11883 if (arm_feature(env, ARM_FEATURE_M)) {
9550d1bd
RH
11884 if (arm_feature(env, ARM_FEATURE_M_SECURITY) &&
11885 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S)
11886 != env->v7m.secure) {
a729a46b 11887 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1);
9550d1bd
RH
11888 }
11889
11890 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) &&
11891 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) ||
11892 (env->v7m.secure &&
11893 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) {
11894 /*
11895 * ASPEN is set, but FPCA/SFPA indicate that there is no
11896 * active FP context; we must create a new FP context before
11897 * executing any FP insn.
11898 */
a729a46b 11899 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1);
9550d1bd
RH
11900 }
11901
11902 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK;
11903 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) {
a729a46b 11904 DP_TBFLAG_M32(flags, LSPACT, 1);
9550d1bd 11905 }
26702213
PM
11906
11907 if (mve_no_pred(env)) {
11908 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1);
11909 }
6e33ced5 11910 } else {
bbad7c62
RH
11911 /*
11912 * Note that XSCALE_CPAR shares bits with VECSTRIDE.
11913 * Note that VECLEN+VECSTRIDE are RES0 for M-profile.
11914 */
11915 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
a729a46b 11916 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar);
bbad7c62 11917 } else {
a729a46b
RH
11918 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len);
11919 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride);
bbad7c62 11920 }
0a54d68e 11921 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) {
a729a46b 11922 DP_TBFLAG_A32(flags, VFPEN, 1);
0a54d68e 11923 }
6e33ced5
RH
11924 }
11925
a729a46b
RH
11926 DP_TBFLAG_AM32(flags, THUMB, env->thumb);
11927 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits);
d4d7503a 11928 }
a9e01311 11929
60e12c37
RH
11930 /*
11931 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine
a9e01311
RH
11932 * states defined in the ARM ARM for software singlestep:
11933 * SS_ACTIVE PSTATE.SS State
11934 * 0 x Inactive (the TB flag for SS is always 0)
11935 * 1 0 Active-pending
11936 * 1 1 Active-not-pending
ae6eb1e9 11937 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB.
a9e01311 11938 */
a729a46b
RH
11939 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) {
11940 DP_TBFLAG_ANY(flags, PSTATE__SS, 1);
a9e01311 11941 }
a9e01311 11942
3902bfc6 11943 *pflags = flags.flags;
a378206a 11944 *cs_base = flags.flags2;
a9e01311 11945}
0ab5953b
RH
11946
11947#ifdef TARGET_AARCH64
11948/*
11949 * The manual says that when SVE is enabled and VQ is widened the
11950 * implementation is allowed to zero the previously inaccessible
11951 * portion of the registers. The corollary to that is that when
11952 * SVE is enabled and VQ is narrowed we are also allowed to zero
11953 * the now inaccessible portion of the registers.
11954 *
11955 * The intent of this is that no predicate bit beyond VQ is ever set.
11956 * Which means that some operations on predicate registers themselves
11957 * may operate on full uint64_t or even unrolled across the maximum
11958 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally
11959 * may well be cheaper than conditionals to restrict the operation
11960 * to the relevant portion of a uint16_t[16].
11961 */
11962void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq)
11963{
11964 int i, j;
11965 uint64_t pmask;
11966
11967 assert(vq >= 1 && vq <= ARM_MAX_VQ);
2fc0cc0e 11968 assert(vq <= env_archcpu(env)->sve_max_vq);
0ab5953b
RH
11969
11970 /* Zap the high bits of the zregs. */
11971 for (i = 0; i < 32; i++) {
11972 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq));
11973 }
11974
11975 /* Zap the high bits of the pregs and ffr. */
11976 pmask = 0;
11977 if (vq & 3) {
11978 pmask = ~(-1ULL << (16 * (vq & 3)));
11979 }
11980 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) {
11981 for (i = 0; i < 17; ++i) {
11982 env->vfp.pregs[i].p[j] &= pmask;
11983 }
11984 pmask = 0;
11985 }
11986}
11987
6a775fd6
RH
11988static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm)
11989{
11990 int exc_el;
11991
11992 if (sm) {
11993 exc_el = sme_exception_el(env, el);
11994 } else {
11995 exc_el = sve_exception_el(env, el);
11996 }
11997 if (exc_el) {
11998 return 0; /* disabled */
11999 }
12000 return sve_vqm1_for_el_sm(env, el, sm);
12001}
12002
0ab5953b
RH
12003/*
12004 * Notice a change in SVE vector size when changing EL.
12005 */
9a05f7b6
RH
12006void aarch64_sve_change_el(CPUARMState *env, int old_el,
12007 int new_el, bool el0_a64)
0ab5953b 12008{
2fc0cc0e 12009 ARMCPU *cpu = env_archcpu(env);
0ab5953b 12010 int old_len, new_len;
6a775fd6 12011 bool old_a64, new_a64, sm;
0ab5953b
RH
12012
12013 /* Nothing to do if no SVE. */
cd208a1c 12014 if (!cpu_isar_feature(aa64_sve, cpu)) {
0ab5953b
RH
12015 return;
12016 }
12017
12018 /* Nothing to do if FP is disabled in either EL. */
12019 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) {
12020 return;
12021 }
12022
04fbce76
RH
12023 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64;
12024 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64;
12025
12026 /*
12027 * Both AArch64.TakeException and AArch64.ExceptionReturn
12028 * invoke ResetSVEState when taking an exception from, or
12029 * returning to, AArch32 state when PSTATE.SM is enabled.
12030 */
6a775fd6
RH
12031 sm = FIELD_EX64(env->svcr, SVCR, SM);
12032 if (old_a64 != new_a64 && sm) {
04fbce76
RH
12033 arm_reset_sve_state(env);
12034 return;
12035 }
12036
0ab5953b
RH
12037 /*
12038 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped
12039 * at ELx, or not available because the EL is in AArch32 state, then
12040 * for all purposes other than a direct read, the ZCR_ELx.LEN field
12041 * has an effective value of 0".
12042 *
12043 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0).
12044 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition
12045 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that
12046 * we already have the correct register contents when encountering the
12047 * vq0->vq0 transition between EL0->EL1.
12048 */
6a775fd6
RH
12049 old_len = new_len = 0;
12050 if (old_a64) {
12051 old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm);
12052 }
12053 if (new_a64) {
12054 new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm);
12055 }
0ab5953b
RH
12056
12057 /* When changing vector length, clear inaccessible state. */
12058 if (new_len < old_len) {
12059 aarch64_sve_narrow_vq(env, new_len + 1);
12060 }
12061}
12062#endif